Release coccinelle-0.2.0
[bpt/coccinelle.git] / popl09 / popltoctl.ml
1 (*
2 * Copyright 2005-2009, 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 module Past = Ast_popl
24 module Ast = Ast_cocci
25 module V = Visitor_ast
26 module CTL = Ast_ctl
27
28 (* --------------------------------------------------------------------- *)
29 (* result type *)
30
31 type cocci_predicate = Lib_engine.predicate * Ast.meta_name Ast_ctl.modif
32 type formula =
33 (cocci_predicate,Ast_cocci.meta_name, Wrapper_ctl.info) Ast_ctl.generic_ctl
34
35 (* --------------------------------------------------------------------- *)
36
37 let contains_modif =
38 let bind x y = x or y in
39 let option_default = false in
40 let mcode r (_,_,kind,_) =
41 match kind with
42 Ast.MINUS(_,_,_,_) -> true
43 | Ast.PLUS _ -> failwith "not possible"
44 | Ast.CONTEXT(_,info) -> not (info = Ast.NOTHING) in
45 let do_nothing r k e = k e in
46 let rule_elem r k re =
47 let res = k re in
48 match Ast.unwrap re with
49 Ast.FunHeader(bef,_,fninfo,name,lp,params,rp) ->
50 bind (mcode r ((),(),bef,Ast.NoMetaPos)) res
51 | Ast.Decl(bef,_,decl) -> bind (mcode r ((),(),bef,Ast.NoMetaPos)) res
52 | _ -> res in
53 let recursor =
54 V.combiner bind option_default
55 mcode mcode mcode mcode mcode mcode mcode mcode mcode mcode mcode mcode
56 do_nothing do_nothing do_nothing do_nothing
57 do_nothing do_nothing do_nothing do_nothing do_nothing do_nothing
58 do_nothing rule_elem do_nothing do_nothing do_nothing do_nothing in
59 recursor.V.combiner_rule_elem
60
61 let ctl_exists keep_wit v x =
62 CTL.Exists(!Flag_popl.keep_all_wits or keep_wit,v,x)
63
64 let predmaker keep_wit term =
65 if (!Flag_popl.keep_all_wits or keep_wit) &&
66 (!Flag_popl.mark_all or contains_modif term)
67 then
68 let v = ("","_v") in
69 ctl_exists true v
70 (CTL.Pred (Lib_engine.Match(term),CTL.Modif v))
71 else CTL.Pred (Lib_engine.Match(term),CTL.Control)
72
73 (* --------------------------------------------------------------------- *)
74
75 let is_true = function CTL.True -> true | _ -> false
76
77 let is_false = function CTL.False -> true | _ -> false
78
79 let ctl_true = CTL.True
80
81 let ctl_false = CTL.False
82
83 let ctl_and x y =
84 if is_true x then y
85 else if is_true y then x else CTL.And(CTL.STRICT,x,y)
86
87 let ctl_or x y =
88 if is_false x then y
89 else if is_false y then x else CTL.Or(x,y)
90
91 let ctl_seqor x y = CTL.SeqOr(x,y)
92
93 let ctl_not x = CTL.Not(x)
94
95 let ctl_ax x =
96 if is_true x then CTL.True
97 else CTL.AX(CTL.FORWARD,CTL.STRICT,x)
98
99 let ctl_ex x =
100 if is_true x then CTL.True
101 else CTL.EX(CTL.FORWARD,x)
102
103 let ctl_back_ex x =
104 if is_true x then CTL.True
105 else CTL.EX(CTL.BACKWARD,x)
106
107 let after = CTL.Pred(Lib_engine.After, CTL.Control)
108 let fall = CTL.Pred(Lib_engine.FallThrough, CTL.Control)
109 let exit = CTL.Pred(Lib_engine.Exit, CTL.Control)
110 let truepred = CTL.Pred(Lib_engine.TrueBranch, CTL.Control)
111 let falsepred = CTL.Pred(Lib_engine.FalseBranch, CTL.Control)
112 let retpred = CTL.Pred(Lib_engine.Return, CTL.Control)
113
114 let string2var x = ("",x)
115
116 let labelctr = ref 0
117 let get_label_ctr _ =
118 let cur = !labelctr in
119 labelctr := cur + 1;
120 string2var (Printf.sprintf "l%d" cur)
121
122 let ctl_au x y = CTL.AU(CTL.FORWARD,CTL.STRICT,x,y)
123
124 let ctl_uncheck x = CTL.Uncheck(x)
125
126 let make_meta_rule_elem d =
127 let nm = "_S" in
128 Ast.make_meta_rule_elem nm d ([],[],[])
129
130 (* --------------------------------------------------------------------- *)
131
132 let rec ctl_seq keep_wit a = function
133 Past.Seq(elem,seq) ->
134 ctl_element keep_wit (ctl_seq keep_wit a seq) elem
135 | Past.Empty -> a
136 | Past.SExists(var,seq) -> ctl_exists keep_wit var (ctl_seq keep_wit a seq)
137
138 and ctl_term keep_wit a = function
139 Past.Atomic(term) -> ctl_and (predmaker keep_wit term) (ctl_ax a)
140 | Past.IfThen(test,thn,(_,_,_,aft)) -> ifthen keep_wit (Some a) test thn aft
141 | Past.TExists(var,term) ->
142 ctl_exists keep_wit var (ctl_term keep_wit a term)
143
144 and ctl_element keep_wit a = function
145 Past.Term(term,ba) ->
146 do_between_dots keep_wit ba (ctl_term keep_wit a term) a
147 | Past.Or(seq1,seq2) ->
148 ctl_seqor (ctl_seq keep_wit a seq1) (ctl_seq keep_wit a seq2)
149 | Past.DInfo(dots) -> ctl_au (guard_ctl_dots keep_wit a dots) a
150 | Past.EExists(var,elem) ->
151 ctl_exists keep_wit var (ctl_element keep_wit a elem)
152
153 (* --------------------------------------------------------------------- *)
154
155 and guard_ctl_seq keep_wit a = function
156 Past.Seq(elem,Past.Empty) -> guard_ctl_element keep_wit a elem
157 | Past.Seq(elem,seq) ->
158 ctl_element keep_wit (guard_ctl_seq keep_wit a seq) elem
159 | Past.Empty -> ctl_true
160 | Past.SExists(var,seq) ->
161 ctl_exists keep_wit var (guard_ctl_seq keep_wit a seq)
162
163 and guard_ctl_term keep_wit = function
164 Past.Atomic(term) -> predmaker keep_wit term
165 | Past.IfThen(test,thn,(_,_,_,aft)) -> ifthen keep_wit None test thn aft
166 | Past.TExists(var,term) ->
167 ctl_exists keep_wit var (guard_ctl_term keep_wit term)
168
169 and guard_ctl_element keep_wit a = function
170 Past.Term(term,_) -> guard_ctl_term keep_wit term
171 | Past.Or(seq1,seq2) ->
172 ctl_seqor
173 (guard_ctl_seq keep_wit a seq1) (guard_ctl_seq keep_wit a seq2)
174 | Past.DInfo(dots) -> ctl_au (guard_ctl_dots keep_wit a dots) a
175 | Past.EExists(var,elem) ->
176 ctl_exists keep_wit var (guard_ctl_element keep_wit a elem)
177
178 and guard_ctl_dots keep_wit a = function
179 Past.Dots -> ctl_true
180 (* | Past.Nest(_) when not keep_wit -> ctl_true
181 a possible optimization, but irrelevant to popl example *)
182 | Past.Nest(seq) ->
183 ctl_or
184 (guard_ctl_seq true a seq)
185 (ctl_not (guard_ctl_seq false a seq))
186 | Past.When(dots,seq) ->
187 ctl_and
188 (guard_ctl_dots keep_wit a dots)
189 (ctl_not (guard_ctl_seq false a seq))
190
191 (* --------------------------------------------------------------------- *)
192
193 and ifthen keep_wit a test thn aft =
194 (* "if (test) thn; after" becomes:
195 if(test) & AX((TrueBranch & AX thn) v FallThrough v (After & AXAX after))
196 & EX After
197 (* doesn't work for C code if (x) return 1; else return 2; *)
198 *)
199 let end_code =
200 match (aft,a) with
201 (Ast.CONTEXT(_,Ast.NOTHING),None) -> ctl_true
202 | (Ast.CONTEXT(_,Ast.NOTHING),Some a) -> ctl_ax (ctl_ax a)
203 | (_,None) -> failwith "not possible"
204 | (_,Some a) ->
205 ctl_ax
206 (ctl_and
207 (predmaker keep_wit (make_meta_rule_elem aft))
208 (ctl_ax a)) in
209 let body =
210 ctl_or
211 (ctl_and truepred
212 (ctl_ax
213 (guard_ctl_term keep_wit thn)))
214 (ctl_or fall
215 (ctl_and after end_code)) in
216 ctl_and (ctl_term keep_wit body test)
217 (match a with Some CTL.True | None -> ctl_true | Some _ -> ctl_ex after)
218
219 and do_between_dots keep_wit ba term after =
220 match ba with
221 Past.AddingBetweenDots (brace_term,n)
222 | Past.DroppingBetweenDots (brace_term,n) ->
223 (* not sure at all what to do here for after... *)
224 let match_brace = ctl_term keep_wit after brace_term in
225 let v = Printf.sprintf "_r_%d" n in
226 let case1 = ctl_and (CTL.Ref v) match_brace in
227 let case2 = ctl_and (ctl_not (CTL.Ref v)) term in
228 CTL.Let
229 (v,ctl_or
230 (ctl_back_ex truepred)
231 (ctl_back_ex (ctl_back_ex falsepred)),
232 ctl_or case1 case2)
233 | Past.NoDots -> term
234
235 (* --------------------------------------------------------------------- *)
236
237 let toctl sl = ctl_seq true ctl_true sl