Commit | Line | Data |
---|---|---|
f537ebc4 | 1 | (* |
17ba0788 C |
2 | * Copyright 2012, INRIA |
3 | * Julia Lawall, Gilles Muller | |
4 | * Copyright 2010-2011, INRIA, University of Copenhagen | |
f537ebc4 C |
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 | ||
951c7801 C |
27 | module Past = Ast_popl |
28 | module Ast = Ast_cocci | |
29 | module V = Visitor_ast | |
30 | module CTL = Ast_ctl | |
31 | ||
32 | (* --------------------------------------------------------------------- *) | |
33 | (* result type *) | |
34 | ||
35 | type cocci_predicate = Lib_engine.predicate * Ast.meta_name Ast_ctl.modif | |
36 | type formula = | |
37 | (cocci_predicate,Ast_cocci.meta_name, Wrapper_ctl.info) Ast_ctl.generic_ctl | |
38 | ||
39 | (* --------------------------------------------------------------------- *) | |
40 | ||
41 | let contains_modif = | |
42 | let bind x y = x or y in | |
43 | let option_default = false in | |
44 | let mcode r (_,_,kind,_) = | |
45 | match kind with | |
46 | Ast.MINUS(_,_) -> true | |
47 | | Ast.PLUS -> failwith "not possible" | |
48 | | Ast.CONTEXT(_,info) -> not (info = Ast.NOTHING) in | |
49 | let do_nothing r k e = k e in | |
50 | let rule_elem r k re = | |
51 | let res = k re in | |
52 | match Ast.unwrap re with | |
53 | Ast.FunHeader(bef,_,fninfo,name,lp,params,rp) -> | |
8f657093 C |
54 | bind (mcode r ((),(),bef,[])) res |
55 | | Ast.Decl(bef,_,decl) -> bind (mcode r ((),(),bef,[])) res | |
951c7801 C |
56 | | _ -> res in |
57 | let recursor = | |
58 | V.combiner bind option_default | |
59 | mcode mcode mcode mcode mcode mcode mcode mcode mcode mcode mcode mcode | |
60 | mcode | |
61 | do_nothing do_nothing do_nothing do_nothing | |
62 | do_nothing do_nothing do_nothing do_nothing do_nothing do_nothing | |
63 | do_nothing rule_elem do_nothing do_nothing do_nothing do_nothing in | |
64 | recursor.V.combiner_rule_elem | |
65 | ||
66 | let ctl_exists v x keep_wit = CTL.Exists(v,x,keep_wit) | |
67 | ||
68 | let predmaker guard term = | |
69 | let pos = ("","_p") in | |
70 | ctl_exists true pos | |
71 | (if guard && contains_modif term | |
72 | then | |
73 | let v = ("","_v") in | |
74 | ctl_exists true v | |
75 | (CTL.Pred (Lib_engine.Match(term),CTL.Modif v)) | |
76 | else CTL.Pred (Lib_engine.Match(term),CTL.Control)) | |
77 | ||
78 | (* --------------------------------------------------------------------- *) | |
79 | ||
80 | let is_true = function CTL.True -> true | _ -> false | |
81 | ||
82 | let is_false = function CTL.False -> true | _ -> false | |
83 | ||
84 | let ctl_true = CTL.True | |
85 | ||
86 | let ctl_false = CTL.False | |
87 | ||
88 | let ctl_and x y = | |
89 | if is_true x then y | |
90 | else if is_true y then x else CTL.And(CTL.STRICT,x,y) | |
91 | ||
92 | let ctl_or x y = | |
93 | if is_false x then y | |
94 | else if is_false y then x else CTL.Or(x,y) | |
95 | ||
96 | let ctl_seqor x y = CTL.SeqOr(x,y) | |
97 | ||
98 | let ctl_not x = CTL.Not(x) | |
99 | ||
100 | let ctl_ax x = | |
101 | if is_true x then CTL.True | |
102 | else CTL.AX(CTL.FORWARD,CTL.STRICT,x) | |
103 | ||
104 | let after = CTL.Pred(Lib_engine.After, CTL.Control) | |
105 | let exit = CTL.Pred(Lib_engine.Exit, CTL.Control) | |
106 | let truepred = CTL.Pred(Lib_engine.TrueBranch, CTL.Control) | |
107 | let retpred = CTL.Pred(Lib_engine.Return, CTL.Control) | |
108 | ||
109 | let string2var x = ("",x) | |
110 | ||
111 | let labelctr = ref 0 | |
112 | let get_label_ctr _ = | |
113 | let cur = !labelctr in | |
114 | labelctr := cur + 1; | |
115 | string2var (Printf.sprintf "l%d" cur) | |
116 | ||
117 | let ctl_au x seq_after y = | |
118 | let lv = get_label_ctr() in | |
119 | let labelpred = CTL.Pred(Lib_engine.Label lv,CTL.Control) in | |
120 | let preflabelpred = CTL.Pred(Lib_engine.PrefixLabel lv,CTL.Control) in | |
121 | let matchgoto = CTL.Pred(Lib_engine.Goto,CTL.Control) in | |
122 | let matchbreak = | |
123 | predmaker false | |
124 | (Ast.make_term | |
125 | (Ast.Break(Ast.make_mcode "break",Ast.make_mcode ";"))) in | |
126 | let matchcontinue = | |
127 | predmaker false | |
128 | (Ast.make_term | |
129 | (Ast.Continue(Ast.make_mcode "continue",Ast.make_mcode ";"))) in | |
130 | let stop_early = | |
131 | ctl_or after | |
132 | (ctl_and (ctl_and truepred labelpred) | |
133 | (CTL.AU | |
134 | (CTL.FORWARD,CTL.STRICT,preflabelpred, | |
135 | ctl_and preflabelpred | |
136 | (ctl_or retpred | |
137 | (ctl_and (ctl_or (ctl_or matchgoto matchbreak) matchcontinue) | |
138 | (CTL.AG | |
139 | (CTL.FORWARD,CTL.STRICT, | |
140 | ctl_not seq_after))))))) in | |
141 | CTL.AU(CTL.FORWARD,CTL.STRICT,x,ctl_or y stop_early) | |
142 | ||
143 | let ctl_uncheck x = CTL.Uncheck(x) | |
144 | ||
145 | (* --------------------------------------------------------------------- *) | |
146 | ||
147 | let rec ctl_seq keep_wit a = function | |
148 | Past.Seq(elem,seq) -> | |
149 | ctl_element keep_wit (ctl_seq keep_wit a seq) elem | |
150 | | Past.Empty -> a | |
151 | | Past.SExists(var,seq) -> ctl_exists keep_wit var (ctl_seq keep_wit a seq) | |
152 | ||
153 | and ctl_element keep_wit a = function | |
154 | Past.Term(term) -> ctl_and (predmaker keep_wit term) (ctl_ax a) | |
155 | | Past.Or(seq1,seq2) -> | |
156 | ctl_seqor (ctl_seq keep_wit a seq1) (ctl_seq keep_wit a seq2) | |
157 | | Past.DInfo(dots,seq_bef,seq_aft) -> | |
158 | let shortest l = | |
159 | List.fold_left ctl_or ctl_false | |
160 | (List.map (ctl_element false ctl_true) l) in | |
161 | let s = shortest (Common.union_set seq_bef seq_aft) in | |
162 | ctl_au (ctl_and (guard_ctl_dots keep_wit dots) (ctl_not s)) | |
163 | (shortest seq_aft) a | |
164 | | Past.EExists(var,elem) -> | |
165 | ctl_exists keep_wit var (ctl_element keep_wit a elem) | |
166 | ||
167 | (* --------------------------------------------------------------------- *) | |
168 | ||
169 | and guard_ctl_seq keep_wit = function | |
170 | Past.Seq(elem,Past.Empty) -> guard_ctl_element keep_wit elem | |
171 | | Past.Seq(elem,seq) -> | |
172 | ctl_element keep_wit (guard_ctl_seq keep_wit seq) elem | |
173 | | Past.Empty -> ctl_true | |
174 | | Past.SExists(var,seq) -> | |
175 | ctl_exists keep_wit var (guard_ctl_seq keep_wit seq) | |
176 | ||
177 | and guard_ctl_element keep_wit = function | |
178 | Past.Term(term) -> predmaker keep_wit term | |
179 | | Past.Or(seq1,seq2) -> | |
180 | ctl_seqor (guard_ctl_seq keep_wit seq1) (guard_ctl_seq keep_wit seq2) | |
181 | | Past.DInfo(dots,seq_bef,seq_aft) -> | |
182 | let shortest l = | |
183 | List.fold_left ctl_or ctl_false | |
184 | (List.map (ctl_element false ctl_true) l) in | |
185 | let s = shortest (Common.union_set seq_bef seq_aft) in | |
186 | let aft = ctl_or s exit in | |
187 | ctl_au (ctl_and (guard_ctl_dots keep_wit dots) (ctl_not s)) | |
188 | (shortest seq_aft) aft | |
189 | | Past.EExists(var,elem) -> | |
190 | ctl_exists keep_wit var (guard_ctl_element keep_wit elem) | |
191 | ||
192 | and guard_ctl_dots keep_wit = function | |
193 | Past.Dots -> ctl_true | |
194 | | Past.Nest(_) when not keep_wit -> ctl_true | |
195 | | Past.Nest(seq) -> | |
196 | ctl_or (guard_ctl_seq true seq) (ctl_not (guard_ctl_seq false seq)) | |
197 | | Past.When(dots,seq) -> | |
198 | ctl_and | |
199 | (guard_ctl_dots keep_wit dots) | |
200 | (ctl_not (ctl_seq false ctl_true seq)) | |
201 | | Past.DExists(var,dots) -> | |
202 | ctl_exists keep_wit var (guard_ctl_dots keep_wit dots) | |
203 | ||
204 | (* --------------------------------------------------------------------- *) | |
205 | ||
65038c61 | 206 | let toctl sl = Asttoctl2.CODE (ctl_seq true ctl_true sl) |