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