Coccinelle release 1.0.0-rc13
[bpt/coccinelle.git] / popl / popltoctl.ml
CommitLineData
f537ebc4 1(*
17ba0788 2 * Copyright 2012, INRIA
d6ce1786
C
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 "./popltoctl.ml"
28(*
29 * Copyright 2012, INRIA
17ba0788
C
30 * Julia Lawall, Gilles Muller
31 * Copyright 2010-2011, INRIA, University of Copenhagen
f537ebc4
C
32 * Julia Lawall, Rene Rydhof Hansen, Gilles Muller, Nicolas Palix
33 * Copyright 2005-2009, Ecole des Mines de Nantes, University of Copenhagen
34 * Yoann Padioleau, Julia Lawall, Rene Rydhof Hansen, Henrik Stuart, Gilles Muller, Nicolas Palix
35 * This file is part of Coccinelle.
36 *
37 * Coccinelle is free software: you can redistribute it and/or modify
38 * it under the terms of the GNU General Public License as published by
39 * the Free Software Foundation, according to version 2 of the License.
40 *
41 * Coccinelle is distributed in the hope that it will be useful,
42 * but WITHOUT ANY WARRANTY; without even the implied warranty of
43 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
44 * GNU General Public License for more details.
45 *
46 * You should have received a copy of the GNU General Public License
47 * along with Coccinelle. If not, see <http://www.gnu.org/licenses/>.
48 *
49 * The authors reserve the right to distribute this or future versions of
50 * Coccinelle under other licenses.
51 *)
52
53
feec80c3 54# 0 "./popltoctl.ml"
951c7801
C
55module Past = Ast_popl
56module Ast = Ast_cocci
57module V = Visitor_ast
58module CTL = Ast_ctl
59
60(* --------------------------------------------------------------------- *)
61(* result type *)
62
63type cocci_predicate = Lib_engine.predicate * Ast.meta_name Ast_ctl.modif
64type formula =
65 (cocci_predicate,Ast_cocci.meta_name, Wrapper_ctl.info) Ast_ctl.generic_ctl
66
67(* --------------------------------------------------------------------- *)
68
69let contains_modif =
70 let bind x y = x or y in
71 let option_default = false in
72 let mcode r (_,_,kind,_) =
73 match kind with
74 Ast.MINUS(_,_) -> true
75 | Ast.PLUS -> failwith "not possible"
76 | Ast.CONTEXT(_,info) -> not (info = Ast.NOTHING) in
77 let do_nothing r k e = k e in
78 let rule_elem r k re =
79 let res = k re in
80 match Ast.unwrap re with
81 Ast.FunHeader(bef,_,fninfo,name,lp,params,rp) ->
8f657093
C
82 bind (mcode r ((),(),bef,[])) res
83 | Ast.Decl(bef,_,decl) -> bind (mcode r ((),(),bef,[])) res
951c7801
C
84 | _ -> res in
85 let recursor =
86 V.combiner bind option_default
87 mcode mcode mcode mcode mcode mcode mcode mcode mcode mcode mcode mcode
88 mcode
89 do_nothing do_nothing do_nothing do_nothing
90 do_nothing do_nothing do_nothing do_nothing do_nothing do_nothing
91 do_nothing rule_elem do_nothing do_nothing do_nothing do_nothing in
92 recursor.V.combiner_rule_elem
93
94let ctl_exists v x keep_wit = CTL.Exists(v,x,keep_wit)
95
96let predmaker guard term =
97 let pos = ("","_p") in
98 ctl_exists true pos
99 (if guard && contains_modif term
100 then
101 let v = ("","_v") in
102 ctl_exists true v
103 (CTL.Pred (Lib_engine.Match(term),CTL.Modif v))
104 else CTL.Pred (Lib_engine.Match(term),CTL.Control))
105
106(* --------------------------------------------------------------------- *)
107
108let is_true = function CTL.True -> true | _ -> false
109
110let is_false = function CTL.False -> true | _ -> false
111
112let ctl_true = CTL.True
113
114let ctl_false = CTL.False
115
116let ctl_and x y =
117 if is_true x then y
118 else if is_true y then x else CTL.And(CTL.STRICT,x,y)
119
120let ctl_or x y =
121 if is_false x then y
122 else if is_false y then x else CTL.Or(x,y)
123
124let ctl_seqor x y = CTL.SeqOr(x,y)
125
126let ctl_not x = CTL.Not(x)
127
128let ctl_ax x =
129 if is_true x then CTL.True
130 else CTL.AX(CTL.FORWARD,CTL.STRICT,x)
131
132let after = CTL.Pred(Lib_engine.After, CTL.Control)
133let exit = CTL.Pred(Lib_engine.Exit, CTL.Control)
134let truepred = CTL.Pred(Lib_engine.TrueBranch, CTL.Control)
135let retpred = CTL.Pred(Lib_engine.Return, CTL.Control)
136
137let string2var x = ("",x)
138
139let labelctr = ref 0
140let get_label_ctr _ =
141 let cur = !labelctr in
142 labelctr := cur + 1;
143 string2var (Printf.sprintf "l%d" cur)
144
145let ctl_au x seq_after y =
146 let lv = get_label_ctr() in
147 let labelpred = CTL.Pred(Lib_engine.Label lv,CTL.Control) in
148 let preflabelpred = CTL.Pred(Lib_engine.PrefixLabel lv,CTL.Control) in
149 let matchgoto = CTL.Pred(Lib_engine.Goto,CTL.Control) in
150 let matchbreak =
151 predmaker false
152 (Ast.make_term
153 (Ast.Break(Ast.make_mcode "break",Ast.make_mcode ";"))) in
154 let matchcontinue =
155 predmaker false
156 (Ast.make_term
157 (Ast.Continue(Ast.make_mcode "continue",Ast.make_mcode ";"))) in
158 let stop_early =
159 ctl_or after
160 (ctl_and (ctl_and truepred labelpred)
161 (CTL.AU
162 (CTL.FORWARD,CTL.STRICT,preflabelpred,
163 ctl_and preflabelpred
164 (ctl_or retpred
165 (ctl_and (ctl_or (ctl_or matchgoto matchbreak) matchcontinue)
166 (CTL.AG
167 (CTL.FORWARD,CTL.STRICT,
168 ctl_not seq_after))))))) in
169 CTL.AU(CTL.FORWARD,CTL.STRICT,x,ctl_or y stop_early)
170
171let ctl_uncheck x = CTL.Uncheck(x)
172
173(* --------------------------------------------------------------------- *)
174
175let rec ctl_seq keep_wit a = function
176 Past.Seq(elem,seq) ->
177 ctl_element keep_wit (ctl_seq keep_wit a seq) elem
178 | Past.Empty -> a
179 | Past.SExists(var,seq) -> ctl_exists keep_wit var (ctl_seq keep_wit a seq)
180
181and ctl_element keep_wit a = function
182 Past.Term(term) -> ctl_and (predmaker keep_wit term) (ctl_ax a)
183 | Past.Or(seq1,seq2) ->
184 ctl_seqor (ctl_seq keep_wit a seq1) (ctl_seq keep_wit a seq2)
185 | Past.DInfo(dots,seq_bef,seq_aft) ->
186 let shortest l =
187 List.fold_left ctl_or ctl_false
188 (List.map (ctl_element false ctl_true) l) in
189 let s = shortest (Common.union_set seq_bef seq_aft) in
190 ctl_au (ctl_and (guard_ctl_dots keep_wit dots) (ctl_not s))
191 (shortest seq_aft) a
192 | Past.EExists(var,elem) ->
193 ctl_exists keep_wit var (ctl_element keep_wit a elem)
194
195(* --------------------------------------------------------------------- *)
196
197and guard_ctl_seq keep_wit = function
198 Past.Seq(elem,Past.Empty) -> guard_ctl_element keep_wit elem
199 | Past.Seq(elem,seq) ->
200 ctl_element keep_wit (guard_ctl_seq keep_wit seq) elem
201 | Past.Empty -> ctl_true
202 | Past.SExists(var,seq) ->
203 ctl_exists keep_wit var (guard_ctl_seq keep_wit seq)
204
205and guard_ctl_element keep_wit = function
206 Past.Term(term) -> predmaker keep_wit term
207 | Past.Or(seq1,seq2) ->
208 ctl_seqor (guard_ctl_seq keep_wit seq1) (guard_ctl_seq keep_wit seq2)
209 | Past.DInfo(dots,seq_bef,seq_aft) ->
210 let shortest l =
211 List.fold_left ctl_or ctl_false
212 (List.map (ctl_element false ctl_true) l) in
213 let s = shortest (Common.union_set seq_bef seq_aft) in
214 let aft = ctl_or s exit in
215 ctl_au (ctl_and (guard_ctl_dots keep_wit dots) (ctl_not s))
216 (shortest seq_aft) aft
217 | Past.EExists(var,elem) ->
218 ctl_exists keep_wit var (guard_ctl_element keep_wit elem)
219
220and guard_ctl_dots keep_wit = function
221 Past.Dots -> ctl_true
222 | Past.Nest(_) when not keep_wit -> ctl_true
223 | Past.Nest(seq) ->
224 ctl_or (guard_ctl_seq true seq) (ctl_not (guard_ctl_seq false seq))
225 | Past.When(dots,seq) ->
226 ctl_and
227 (guard_ctl_dots keep_wit dots)
228 (ctl_not (ctl_seq false ctl_true seq))
229 | Past.DExists(var,dots) ->
230 ctl_exists keep_wit var (guard_ctl_dots keep_wit dots)
231
232(* --------------------------------------------------------------------- *)
233
65038c61 234let toctl sl = Asttoctl2.CODE (ctl_seq true ctl_true sl)