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