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