| 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 | # 0 "./asttopopl.ml" |
| 28 | module Ast = Ast_cocci |
| 29 | module Past = Ast_popl |
| 30 | |
| 31 | (* --------------------------------------------------------------------- *) |
| 32 | |
| 33 | let term s inif = |
| 34 | let fail _ = |
| 35 | Pretty_print_cocci.statement "" s; |
| 36 | Format.print_newline(); |
| 37 | failwith "complex statements not supported" in |
| 38 | match Ast.unwrap s with |
| 39 | Ast.Atomic(ast) -> |
| 40 | (match Ast.unwrap ast with |
| 41 | Ast.ExprStatement(_,_) -> Past.Atomic ast |
| 42 | | Ast.Exp(_) -> Past.Atomic ast |
| 43 | | Ast.Decl(_,_,_) -> Past.Atomic ast |
| 44 | | Ast.ReturnExpr(_,_,_) -> Past.Atomic ast |
| 45 | | Ast.MetaStmt(_,_,_,_) when inif -> Past.Atomic ast |
| 46 | | Ast.DisjRuleElem(_) -> Past.Atomic ast |
| 47 | | _ -> fail()) |
| 48 | | _ -> fail() |
| 49 | |
| 50 | let rec stm s = |
| 51 | match Ast.unwrap s with |
| 52 | Ast.Atomic(ast) -> Past.Term(term s false,dots_bef_aft s false) |
| 53 | | Ast.IfThen(header,body,aft) -> |
| 54 | Past.Term( |
| 55 | Past.IfThen(Past.Atomic header,term body true,aft), |
| 56 | dots_bef_aft s true) |
| 57 | | Ast.Disj(stm1::stm2::stmts) -> |
| 58 | List.fold_left |
| 59 | (function prev -> |
| 60 | function cur -> |
| 61 | Past.Or(Past.Seq(prev,Past.Empty),stm_list cur)) |
| 62 | (Past.Or(stm_list stm1,stm_list stm2)) stmts |
| 63 | | Ast.Dots(dots,whencodes,_,_) -> |
| 64 | Past.DInfo |
| 65 | (List.fold_left |
| 66 | (function prev -> |
| 67 | function |
| 68 | Ast.WhenNot(a) -> Past.When(prev,stm_list a) |
| 69 | | _ -> failwith "only when != supported") |
| 70 | Past.Dots whencodes) |
| 71 | | Ast.Nest(starter,stmt_dots,ender,whencodes,false,_,_) -> |
| 72 | (match Ast.get_mcodekind starter with |
| 73 | Ast.MINUS _ -> failwith "only context nests supported" |
| 74 | | _ -> ()); |
| 75 | let nest = Past.Nest(stm_list stmt_dots) in |
| 76 | Past.DInfo |
| 77 | (List.fold_left |
| 78 | (function prev -> |
| 79 | function |
| 80 | Ast.WhenNot(a) -> Past.When(prev,stm_list a) |
| 81 | | _ -> failwith "only when != supported") |
| 82 | nest whencodes) |
| 83 | | _ -> |
| 84 | Pretty_print_cocci.statement "" s; |
| 85 | failwith "unsupported statement3" |
| 86 | |
| 87 | and dots_bef_aft s inif = |
| 88 | match Ast.get_dots_bef_aft s with |
| 89 | Ast.AddingBetweenDots (brace_term,n) -> |
| 90 | Past.AddingBetweenDots (term brace_term inif,n) |
| 91 | | Ast.DroppingBetweenDots (brace_term,n) -> |
| 92 | Past.DroppingBetweenDots (term brace_term inif,n) |
| 93 | | Ast.NoDots -> Past.NoDots |
| 94 | |
| 95 | and stm_list s = |
| 96 | match Ast.unwrap s with |
| 97 | Ast.DOTS(d) -> |
| 98 | List.fold_right |
| 99 | (function cur -> function rest -> Past.Seq(stm cur, rest)) |
| 100 | d Past.Empty |
| 101 | | _ -> failwith "only DOTS handled" |
| 102 | |
| 103 | let top s = |
| 104 | match Ast.unwrap s with |
| 105 | Ast.CODE(stmt_dots) -> stm_list stmt_dots |
| 106 | | _ -> failwith "only CODE handled" |