Coccinelle release 1.0.0-rc3
[bpt/coccinelle.git] / popl09 / popltoctl.ml
CommitLineData
f537ebc4
C
1(*
2 * Copyright 2010, INRIA, University of Copenhagen
3 * Julia Lawall, Rene Rydhof Hansen, Gilles Muller, Nicolas Palix
4 * Copyright 2005-2009, Ecole des Mines de Nantes, University of Copenhagen
5 * Yoann Padioleau, Julia Lawall, Rene Rydhof Hansen, Henrik Stuart, Gilles Muller, Nicolas Palix
6 * This file is part of Coccinelle.
7 *
8 * Coccinelle is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation, according to version 2 of the License.
11 *
12 * Coccinelle is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with Coccinelle. If not, see <http://www.gnu.org/licenses/>.
19 *
20 * The authors reserve the right to distribute this or future versions of
21 * Coccinelle under other licenses.
22 *)
23
24
951c7801
C
25module Past = Ast_popl
26module Ast = Ast_cocci
27module V = Visitor_ast
28module CTL = Ast_ctl
29
30(* --------------------------------------------------------------------- *)
31(* result type *)
32
33type cocci_predicate = Lib_engine.predicate * Ast.meta_name Ast_ctl.modif
34type formula =
35 (cocci_predicate,Ast_cocci.meta_name, Wrapper_ctl.info) Ast_ctl.generic_ctl
36
37(* --------------------------------------------------------------------- *)
38
39let contains_modif =
40 let bind x y = x or y in
41 let option_default = false in
42 let mcode r (_,_,kind,_) =
43 match kind with
44 Ast.MINUS(_,_,_,_) -> true
45 | Ast.PLUS _ -> failwith "not possible"
46 | Ast.CONTEXT(_,info) -> not (info = Ast.NOTHING) in
47 let do_nothing r k e = k e in
48 let rule_elem r k re =
49 let res = k re in
50 match Ast.unwrap re with
51 Ast.FunHeader(bef,_,fninfo,name,lp,params,rp) ->
8f657093
C
52 bind (mcode r ((),(),bef,[])) res
53 | Ast.Decl(bef,_,decl) -> bind (mcode r ((),(),bef,[])) res
951c7801
C
54 | _ -> res in
55 let recursor =
56 V.combiner bind option_default
57 mcode mcode mcode mcode mcode mcode mcode mcode mcode mcode mcode mcode
c491d8ee 58 do_nothing do_nothing do_nothing do_nothing do_nothing
951c7801
C
59 do_nothing do_nothing do_nothing do_nothing do_nothing do_nothing
60 do_nothing rule_elem do_nothing do_nothing do_nothing do_nothing in
61 recursor.V.combiner_rule_elem
62
63let ctl_exists keep_wit v x =
64 CTL.Exists(!Flag_popl.keep_all_wits or keep_wit,v,x)
65
66let predmaker keep_wit term =
67 if (!Flag_popl.keep_all_wits or keep_wit) &&
68 (!Flag_popl.mark_all or contains_modif term)
69 then
70 let v = ("","_v") in
71 ctl_exists true v
72 (CTL.Pred (Lib_engine.Match(term),CTL.Modif v))
73 else CTL.Pred (Lib_engine.Match(term),CTL.Control)
74
75(* --------------------------------------------------------------------- *)
76
77let is_true = function CTL.True -> true | _ -> false
78
79let is_false = function CTL.False -> true | _ -> false
80
81let ctl_true = CTL.True
82
83let ctl_false = CTL.False
84
85let ctl_and x y =
86 if is_true x then y
87 else if is_true y then x else CTL.And(CTL.STRICT,x,y)
88
89let ctl_or x y =
90 if is_false x then y
91 else if is_false y then x else CTL.Or(x,y)
92
93let ctl_seqor x y = CTL.SeqOr(x,y)
94
95let ctl_not x = CTL.Not(x)
96
97let ctl_ax x =
98 if is_true x then CTL.True
99 else CTL.AX(CTL.FORWARD,CTL.STRICT,x)
100
101let ctl_ex x =
102 if is_true x then CTL.True
103 else CTL.EX(CTL.FORWARD,x)
104
105let ctl_back_ex x =
106 if is_true x then CTL.True
107 else CTL.EX(CTL.BACKWARD,x)
108
109let after = CTL.Pred(Lib_engine.After, CTL.Control)
110let fall = CTL.Pred(Lib_engine.FallThrough, CTL.Control)
111let exit = CTL.Pred(Lib_engine.Exit, CTL.Control)
112let truepred = CTL.Pred(Lib_engine.TrueBranch, CTL.Control)
113let falsepred = CTL.Pred(Lib_engine.FalseBranch, CTL.Control)
114let retpred = CTL.Pred(Lib_engine.Return, CTL.Control)
115
116let string2var x = ("",x)
117
118let labelctr = ref 0
119let get_label_ctr _ =
120 let cur = !labelctr in
121 labelctr := cur + 1;
122 string2var (Printf.sprintf "l%d" cur)
123
124let ctl_au x y = CTL.AU(CTL.FORWARD,CTL.STRICT,x,y)
125
126let ctl_uncheck x = CTL.Uncheck(x)
127
128let make_meta_rule_elem d =
129 let nm = "_S" in
130 Ast.make_meta_rule_elem nm d ([],[],[])
131
132(* --------------------------------------------------------------------- *)
133
134let rec ctl_seq keep_wit a = function
135 Past.Seq(elem,seq) ->
136 ctl_element keep_wit (ctl_seq keep_wit a seq) elem
137 | Past.Empty -> a
138 | Past.SExists(var,seq) -> ctl_exists keep_wit var (ctl_seq keep_wit a seq)
139
140and ctl_term keep_wit a = function
141 Past.Atomic(term) -> ctl_and (predmaker keep_wit term) (ctl_ax a)
142 | Past.IfThen(test,thn,(_,_,_,aft)) -> ifthen keep_wit (Some a) test thn aft
143 | Past.TExists(var,term) ->
144 ctl_exists keep_wit var (ctl_term keep_wit a term)
145
146and ctl_element keep_wit a = function
147 Past.Term(term,ba) ->
148 do_between_dots keep_wit ba (ctl_term keep_wit a term) a
149 | Past.Or(seq1,seq2) ->
150 ctl_seqor (ctl_seq keep_wit a seq1) (ctl_seq keep_wit a seq2)
151 | Past.DInfo(dots) -> ctl_au (guard_ctl_dots keep_wit a dots) a
152 | Past.EExists(var,elem) ->
153 ctl_exists keep_wit var (ctl_element keep_wit a elem)
154
155(* --------------------------------------------------------------------- *)
156
157and guard_ctl_seq keep_wit a = function
158 Past.Seq(elem,Past.Empty) -> guard_ctl_element keep_wit a elem
159 | Past.Seq(elem,seq) ->
160 ctl_element keep_wit (guard_ctl_seq keep_wit a seq) elem
161 | Past.Empty -> ctl_true
162 | Past.SExists(var,seq) ->
163 ctl_exists keep_wit var (guard_ctl_seq keep_wit a seq)
164
165and guard_ctl_term keep_wit = function
166 Past.Atomic(term) -> predmaker keep_wit term
167 | Past.IfThen(test,thn,(_,_,_,aft)) -> ifthen keep_wit None test thn aft
168 | Past.TExists(var,term) ->
169 ctl_exists keep_wit var (guard_ctl_term keep_wit term)
170
171and guard_ctl_element keep_wit a = function
172 Past.Term(term,_) -> guard_ctl_term keep_wit term
173 | Past.Or(seq1,seq2) ->
174 ctl_seqor
175 (guard_ctl_seq keep_wit a seq1) (guard_ctl_seq keep_wit a seq2)
176 | Past.DInfo(dots) -> ctl_au (guard_ctl_dots keep_wit a dots) a
177 | Past.EExists(var,elem) ->
178 ctl_exists keep_wit var (guard_ctl_element keep_wit a elem)
179
180and guard_ctl_dots keep_wit a = function
181 Past.Dots -> ctl_true
182(* | Past.Nest(_) when not keep_wit -> ctl_true
183 a possible optimization, but irrelevant to popl example *)
184 | Past.Nest(seq) ->
185 ctl_or
186 (guard_ctl_seq true a seq)
187 (ctl_not (guard_ctl_seq false a seq))
188 | Past.When(dots,seq) ->
189 ctl_and
190 (guard_ctl_dots keep_wit a dots)
191 (ctl_not (guard_ctl_seq false a seq))
192
193(* --------------------------------------------------------------------- *)
194
195and ifthen keep_wit a test thn aft =
196(* "if (test) thn; after" becomes:
197 if(test) & AX((TrueBranch & AX thn) v FallThrough v (After & AXAX after))
198 & EX After
199 (* doesn't work for C code if (x) return 1; else return 2; *)
200*)
201 let end_code =
202 match (aft,a) with
203 (Ast.CONTEXT(_,Ast.NOTHING),None) -> ctl_true
204 | (Ast.CONTEXT(_,Ast.NOTHING),Some a) -> ctl_ax (ctl_ax a)
205 | (_,None) -> failwith "not possible"
206 | (_,Some a) ->
207 ctl_ax
208 (ctl_and
209 (predmaker keep_wit (make_meta_rule_elem aft))
210 (ctl_ax a)) in
211 let body =
212 ctl_or
213 (ctl_and truepred
214 (ctl_ax
215 (guard_ctl_term keep_wit thn)))
216 (ctl_or fall
217 (ctl_and after end_code)) in
218 ctl_and (ctl_term keep_wit body test)
219 (match a with Some CTL.True | None -> ctl_true | Some _ -> ctl_ex after)
220
221and do_between_dots keep_wit ba term after =
222 match ba with
223 Past.AddingBetweenDots (brace_term,n)
224 | Past.DroppingBetweenDots (brace_term,n) ->
225 (* not sure at all what to do here for after... *)
226 let match_brace = ctl_term keep_wit after brace_term in
227 let v = Printf.sprintf "_r_%d" n in
228 let case1 = ctl_and (CTL.Ref v) match_brace in
229 let case2 = ctl_and (ctl_not (CTL.Ref v)) term in
230 CTL.Let
231 (v,ctl_or
232 (ctl_back_ex truepred)
233 (ctl_back_ex (ctl_back_ex falsepred)),
ae4735db 234 ctl_or case1 case2)
951c7801
C
235 | Past.NoDots -> term
236
237(* --------------------------------------------------------------------- *)
238
65038c61 239let toctl sl = Asttoctl2.CODE (ctl_seq true ctl_true sl)