Coccinelle release-1.0.0-rc11
[bpt/coccinelle.git] / popl / 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 rec stm s =
33 match Ast.unwrap s with
34 Ast.Atomic(ast) ->
35 (match Ast.unwrap ast with
36 Ast.ExprStatement(_,_) -> Past.Term ast
37 | Ast.Exp(_) -> Past.Term ast
38 | Ast.Decl(_,_,_) -> Past.Term ast
39 | _ -> failwith "complex statements not supported")
40 | Ast.Disj(stm1::stm2::stmts) ->
41 List.fold_left
42 (function prev ->
43 function cur ->
44 Past.Or(Past.Seq(prev,Past.Empty),stm_list cur))
45 (Past.Or(stm_list stm1,stm_list stm2)) stmts
46 | Ast.Dots(dots,whencodes,_,_) ->
47 (match whencodes with
48 [Ast.WhenNot(a)] -> Past.DInfo(Past.When(Past.Dots,stm_list a),[],[])
49 | _ -> failwith "only one when != supported")
50 | Ast.Nest(stmt_dots,whencodes,false,_,_) ->
51 let nest = Past.Nest(stm_list stmt_dots) in
52 (match whencodes with
53 [Ast.WhenNot(a)] -> Past.DInfo(Past.When(nest,stm_list a),[],[])
54 | _ -> failwith "only when != supported")
55 | Ast.While(header,body,(_,_,_,aft)) | Ast.For(header,body,(_,_,_,aft)) ->
56 (* only allowed if only the header is significant *)
57 (match (Ast.unwrap body,aft) with
58 (Ast.Atomic(re),Ast.CONTEXT(_,Ast.NOTHING)) ->
59 (match Ast.unwrap re with
60 Ast.MetaStmt(_,Type_cocci.Unitary,_,false) -> Past.Term header
61 | _ -> failwith "unsupported statement1")
62 | _ -> failwith "unsupported statement2")
63 | _ ->
64 Pretty_print_cocci.statement "" s;
65 failwith "unsupported statement3"
66
67 and stm_list s =
68 match Ast.unwrap s with
69 Ast.DOTS(d) ->
70 List.fold_right
71 (function cur -> function rest -> Past.Seq(stm cur, rest))
72 d Past.Empty
73 | _ -> failwith "only DOTS handled"
74
75 let top s =
76 match Ast.unwrap s with
77 Ast.CODE(stmt_dots) -> stm_list stmt_dots
78 | _ -> failwith "only CODE handled"