Coccinelle release-1.0.0-rc11
[bpt/coccinelle.git] / popl09 / asttopopl.ml
1 (*
2 * Copyright 2012, INRIA
3 * Julia Lawall, Gilles Muller
4 * Copyright 2010-2011, INRIA, University of Copenhagen
5 * Julia Lawall, Rene Rydhof Hansen, Gilles Muller, Nicolas Palix
6 * Copyright 2005-2009, Ecole des Mines de Nantes, University of Copenhagen
7 * Yoann Padioleau, Julia Lawall, Rene Rydhof Hansen, Henrik Stuart, Gilles Muller, Nicolas Palix
8 * This file is part of Coccinelle.
9 *
10 * Coccinelle is free software: you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation, according to version 2 of the License.
13 *
14 * Coccinelle is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with Coccinelle. If not, see <http://www.gnu.org/licenses/>.
21 *
22 * The authors reserve the right to distribute this or future versions of
23 * Coccinelle under other licenses.
24 *)
25
26
27 module Ast = Ast_cocci
28 module Past = Ast_popl
29
30 (* --------------------------------------------------------------------- *)
31
32 let term s inif =
33 let fail _ =
34 Pretty_print_cocci.statement "" s;
35 Format.print_newline();
36 failwith "complex statements not supported" in
37 match Ast.unwrap s with
38 Ast.Atomic(ast) ->
39 (match Ast.unwrap ast with
40 Ast.ExprStatement(_,_) -> Past.Atomic ast
41 | Ast.Exp(_) -> Past.Atomic ast
42 | Ast.Decl(_,_,_) -> Past.Atomic ast
43 | Ast.ReturnExpr(_,_,_) -> Past.Atomic ast
44 | Ast.MetaStmt(_,_,_,_) when inif -> Past.Atomic ast
45 | Ast.DisjRuleElem(_) -> Past.Atomic ast
46 | _ -> fail())
47 | _ -> fail()
48
49 let rec stm s =
50 match Ast.unwrap s with
51 Ast.Atomic(ast) -> Past.Term(term s false,dots_bef_aft s false)
52 | Ast.IfThen(header,body,aft) ->
53 Past.Term(
54 Past.IfThen(Past.Atomic header,term body true,aft),
55 dots_bef_aft s true)
56 | Ast.Disj(stm1::stm2::stmts) ->
57 List.fold_left
58 (function prev ->
59 function cur ->
60 Past.Or(Past.Seq(prev,Past.Empty),stm_list cur))
61 (Past.Or(stm_list stm1,stm_list stm2)) stmts
62 | Ast.Dots(dots,whencodes,_,_) ->
63 Past.DInfo
64 (List.fold_left
65 (function prev ->
66 function
67 Ast.WhenNot(a) -> Past.When(prev,stm_list a)
68 | _ -> failwith "only when != supported")
69 Past.Dots whencodes)
70 | Ast.Nest(starter,stmt_dots,ender,whencodes,false,_,_) ->
71 (match Ast.get_mcodekind starter with
72 Ast.MINUS _ -> failwith "only context nests supported"
73 | _ -> ());
74 let nest = Past.Nest(stm_list stmt_dots) in
75 Past.DInfo
76 (List.fold_left
77 (function prev ->
78 function
79 Ast.WhenNot(a) -> Past.When(prev,stm_list a)
80 | _ -> failwith "only when != supported")
81 nest whencodes)
82 | _ ->
83 Pretty_print_cocci.statement "" s;
84 failwith "unsupported statement3"
85
86 and dots_bef_aft s inif =
87 match Ast.get_dots_bef_aft s with
88 Ast.AddingBetweenDots (brace_term,n) ->
89 Past.AddingBetweenDots (term brace_term inif,n)
90 | Ast.DroppingBetweenDots (brace_term,n) ->
91 Past.DroppingBetweenDots (term brace_term inif,n)
92 | Ast.NoDots -> Past.NoDots
93
94 and stm_list s =
95 match Ast.unwrap s with
96 Ast.DOTS(d) ->
97 List.fold_right
98 (function cur -> function rest -> Past.Seq(stm cur, rest))
99 d Past.Empty
100 | _ -> failwith "only DOTS handled"
101
102 let top s =
103 match Ast.unwrap s with
104 Ast.CODE(stmt_dots) -> stm_list stmt_dots
105 | _ -> failwith "only CODE handled"