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