Release coccinelle-0.2.3rc1
[bpt/coccinelle.git] / popl / 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 rec stm s =
51 match Ast.unwrap s with
52 Ast.Atomic(ast) ->
53 (match Ast.unwrap ast with
54 Ast.ExprStatement(_,_) -> Past.Term ast
55 | Ast.Exp(_) -> Past.Term ast
56 | Ast.Decl(_,_,_) -> Past.Term ast
57 | _ -> failwith "complex statements not supported")
58 | Ast.Disj(stm1::stm2::stmts) ->
59 List.fold_left
60 (function prev ->
61 function cur ->
62 Past.Or(Past.Seq(prev,Past.Empty),stm_list cur))
63 (Past.Or(stm_list stm1,stm_list stm2)) stmts
64 | Ast.Dots(dots,whencodes,_,_) ->
65 (match whencodes with
66 [Ast.WhenNot(a)] -> Past.DInfo(Past.When(Past.Dots,stm_list a),[],[])
67 | _ -> failwith "only one when != supported")
68 | Ast.Nest(stmt_dots,whencodes,false,_,_) ->
69 let nest = Past.Nest(stm_list stmt_dots) in
70 (match whencodes with
71 [Ast.WhenNot(a)] -> Past.DInfo(Past.When(nest,stm_list a),[],[])
72 | _ -> failwith "only when != supported")
73 | Ast.While(header,body,(_,_,_,aft)) | Ast.For(header,body,(_,_,_,aft)) ->
74 (* only allowed if only the header is significant *)
75 (match (Ast.unwrap body,aft) with
76 (Ast.Atomic(re),Ast.CONTEXT(_,Ast.NOTHING)) ->
77 (match Ast.unwrap re with
78 Ast.MetaStmt(_,Type_cocci.Unitary,_,false) -> Past.Term header
79 | _ -> failwith "unsupported statement1")
80 | _ -> failwith "unsupported statement2")
81 | _ ->
82 Pretty_print_cocci.statement "" s;
83 failwith "unsupported statement3"
84
85 and stm_list s =
86 match Ast.unwrap s with
87 Ast.DOTS(d) ->
88 List.fold_right
89 (function cur -> function rest -> Past.Seq(stm cur, rest))
90 d Past.Empty
91 | _ -> failwith "only DOTS handled"
92
93 let top s =
94 match Ast.unwrap s with
95 Ast.CODE(stmt_dots) -> stm_list stmt_dots
96 | _ -> failwith "only CODE handled"