Release coccinelle-0.2.0
[bpt/coccinelle.git] / popl / 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 mcode
57 do_nothing do_nothing do_nothing do_nothing
58 do_nothing do_nothing do_nothing do_nothing do_nothing do_nothing
59 do_nothing rule_elem do_nothing do_nothing do_nothing do_nothing in
60 recursor.V.combiner_rule_elem
61
62 let ctl_exists v x keep_wit = CTL.Exists(v,x,keep_wit)
63
64 let predmaker guard term =
65 let pos = ("","_p") in
66 ctl_exists true pos
67 (if guard && contains_modif term
68 then
69 let v = ("","_v") in
70 ctl_exists true v
71 (CTL.Pred (Lib_engine.Match(term),CTL.Modif v))
72 else CTL.Pred (Lib_engine.Match(term),CTL.Control))
73
74 (* --------------------------------------------------------------------- *)
75
76 let is_true = function CTL.True -> true | _ -> false
77
78 let is_false = function CTL.False -> true | _ -> false
79
80 let ctl_true = CTL.True
81
82 let ctl_false = CTL.False
83
84 let ctl_and x y =
85 if is_true x then y
86 else if is_true y then x else CTL.And(CTL.STRICT,x,y)
87
88 let ctl_or x y =
89 if is_false x then y
90 else if is_false y then x else CTL.Or(x,y)
91
92 let ctl_seqor x y = CTL.SeqOr(x,y)
93
94 let ctl_not x = CTL.Not(x)
95
96 let ctl_ax x =
97 if is_true x then CTL.True
98 else CTL.AX(CTL.FORWARD,CTL.STRICT,x)
99
100 let after = CTL.Pred(Lib_engine.After, CTL.Control)
101 let exit = CTL.Pred(Lib_engine.Exit, CTL.Control)
102 let truepred = CTL.Pred(Lib_engine.TrueBranch, CTL.Control)
103 let retpred = CTL.Pred(Lib_engine.Return, CTL.Control)
104
105 let string2var x = ("",x)
106
107 let labelctr = ref 0
108 let get_label_ctr _ =
109 let cur = !labelctr in
110 labelctr := cur + 1;
111 string2var (Printf.sprintf "l%d" cur)
112
113 let ctl_au x seq_after y =
114 let lv = get_label_ctr() in
115 let labelpred = CTL.Pred(Lib_engine.Label lv,CTL.Control) in
116 let preflabelpred = CTL.Pred(Lib_engine.PrefixLabel lv,CTL.Control) in
117 let matchgoto = CTL.Pred(Lib_engine.Goto,CTL.Control) in
118 let matchbreak =
119 predmaker false
120 (Ast.make_term
121 (Ast.Break(Ast.make_mcode "break",Ast.make_mcode ";"))) in
122 let matchcontinue =
123 predmaker false
124 (Ast.make_term
125 (Ast.Continue(Ast.make_mcode "continue",Ast.make_mcode ";"))) in
126 let stop_early =
127 ctl_or after
128 (ctl_and (ctl_and truepred labelpred)
129 (CTL.AU
130 (CTL.FORWARD,CTL.STRICT,preflabelpred,
131 ctl_and preflabelpred
132 (ctl_or retpred
133 (ctl_and (ctl_or (ctl_or matchgoto matchbreak) matchcontinue)
134 (CTL.AG
135 (CTL.FORWARD,CTL.STRICT,
136 ctl_not seq_after))))))) in
137 CTL.AU(CTL.FORWARD,CTL.STRICT,x,ctl_or y stop_early)
138
139 let ctl_uncheck x = CTL.Uncheck(x)
140
141 (* --------------------------------------------------------------------- *)
142
143 let rec ctl_seq keep_wit a = function
144 Past.Seq(elem,seq) ->
145 ctl_element keep_wit (ctl_seq keep_wit a seq) elem
146 | Past.Empty -> a
147 | Past.SExists(var,seq) -> ctl_exists keep_wit var (ctl_seq keep_wit a seq)
148
149 and ctl_element keep_wit a = function
150 Past.Term(term) -> ctl_and (predmaker keep_wit term) (ctl_ax 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,seq_bef,seq_aft) ->
154 let shortest l =
155 List.fold_left ctl_or ctl_false
156 (List.map (ctl_element false ctl_true) l) in
157 let s = shortest (Common.union_set seq_bef seq_aft) in
158 ctl_au (ctl_and (guard_ctl_dots keep_wit dots) (ctl_not s))
159 (shortest seq_aft) a
160 | Past.EExists(var,elem) ->
161 ctl_exists keep_wit var (ctl_element keep_wit a elem)
162
163 (* --------------------------------------------------------------------- *)
164
165 and guard_ctl_seq keep_wit = function
166 Past.Seq(elem,Past.Empty) -> guard_ctl_element keep_wit elem
167 | Past.Seq(elem,seq) ->
168 ctl_element keep_wit (guard_ctl_seq keep_wit seq) elem
169 | Past.Empty -> ctl_true
170 | Past.SExists(var,seq) ->
171 ctl_exists keep_wit var (guard_ctl_seq keep_wit seq)
172
173 and guard_ctl_element keep_wit = function
174 Past.Term(term) -> predmaker keep_wit term
175 | Past.Or(seq1,seq2) ->
176 ctl_seqor (guard_ctl_seq keep_wit seq1) (guard_ctl_seq keep_wit seq2)
177 | Past.DInfo(dots,seq_bef,seq_aft) ->
178 let shortest l =
179 List.fold_left ctl_or ctl_false
180 (List.map (ctl_element false ctl_true) l) in
181 let s = shortest (Common.union_set seq_bef seq_aft) in
182 let aft = ctl_or s exit in
183 ctl_au (ctl_and (guard_ctl_dots keep_wit dots) (ctl_not s))
184 (shortest seq_aft) aft
185 | Past.EExists(var,elem) ->
186 ctl_exists keep_wit var (guard_ctl_element keep_wit elem)
187
188 and guard_ctl_dots keep_wit = function
189 Past.Dots -> ctl_true
190 | Past.Nest(_) when not keep_wit -> ctl_true
191 | Past.Nest(seq) ->
192 ctl_or (guard_ctl_seq true seq) (ctl_not (guard_ctl_seq false seq))
193 | Past.When(dots,seq) ->
194 ctl_and
195 (guard_ctl_dots keep_wit dots)
196 (ctl_not (ctl_seq false ctl_true seq))
197 | Past.DExists(var,dots) ->
198 ctl_exists keep_wit var (guard_ctl_dots keep_wit dots)
199
200 (* --------------------------------------------------------------------- *)
201
202 let toctl sl = ctl_seq true ctl_true sl