Release coccinelle-0.2.3rc1
[bpt/coccinelle.git] / parsing_cocci / unitary_ast0.ml
CommitLineData
5636bb2c
C
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
9f8e26f4 23(*
ae4735db 24 * Copyright 2005-2010, Ecole des Mines de Nantes, University of Copenhagen
9f8e26f4
C
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
34e49164
C
45(* find unitary metavariables *)
46module Ast0 = Ast0_cocci
47module Ast = Ast_cocci
48module V0 = Visitor_ast0
b1b2de81 49module VT0 = Visitor_ast0_types
34e49164
C
50
51let set_minus s minus = List.filter (function n -> not (List.mem n minus)) s
52
53let rec nub = function
54 [] -> []
55 | (x::xs) when (List.mem x xs) -> nub xs
56 | (x::xs) -> x::(nub xs)
57
58(* ----------------------------------------------------------------------- *)
59(* Find the variables that occur free and occur free in a unitary way *)
60
61(* take everything *)
62let minus_checker name = let id = Ast0.unwrap_mcode name in [id]
63
64(* take only what is in the plus code *)
708f4980 65let plus_checker (nm,_,_,mc,_,_) =
951c7801 66 match mc with Ast0.PLUS _ -> [nm] | _ -> []
faf9a90c 67
34e49164
C
68let get_free checker t =
69 let bind x y = x @ y in
70 let option_default = [] in
faf9a90c 71
34e49164
C
72 (* considers a single list *)
73 let collect_unitary_nonunitary free_usage =
74 let free_usage = List.sort compare free_usage in
75 let rec loop1 todrop = function
76 [] -> []
77 | (x::xs) as all -> if x = todrop then loop1 todrop xs else all in
78 let rec loop2 = function
79 [] -> ([],[])
80 | [x] -> ([x],[])
81 | x::y::xs ->
82 if x = y
83 then
84 let (unitary,non_unitary) = loop2(loop1 x xs) in
85 (unitary,x::non_unitary)
86 else
87 let (unitary,non_unitary) = loop2 (y::xs) in
88 (x::unitary,non_unitary) in
89 loop2 free_usage in
faf9a90c 90
34e49164
C
91 (* considers a list of lists *)
92 let detect_unitary_frees l =
93 let (unitary,nonunitary) =
94 List.split (List.map collect_unitary_nonunitary l) in
95 let unitary = nub (List.concat unitary) in
96 let nonunitary = nub (List.concat nonunitary) in
97 let unitary =
98 List.filter (function x -> not (List.mem x nonunitary)) unitary in
99 unitary@nonunitary@nonunitary in
100
1be43e12 101 let whencode afn bfn expression = function
34e49164
C
102 Ast0.WhenNot(a) -> afn a
103 | Ast0.WhenAlways(b) -> bfn b
1be43e12
C
104 | Ast0.WhenModifier(_) -> option_default
105 | Ast0.WhenNotTrue(a) -> expression a
106 | Ast0.WhenNotFalse(a) -> expression a in
faf9a90c 107
34e49164
C
108 let ident r k i =
109 match Ast0.unwrap i with
110 Ast0.MetaId(name,_,_) | Ast0.MetaFunc(name,_,_)
111 | Ast0.MetaLocalFunc(name,_,_) -> checker name
112 | _ -> k i in
faf9a90c 113
34e49164
C
114 let expression r k e =
115 match Ast0.unwrap e with
116 Ast0.MetaErr(name,_,_) | Ast0.MetaExpr(name,_,_,_,_)
117 | Ast0.MetaExprList(name,_,_) -> checker name
118 | Ast0.DisjExpr(starter,expr_list,mids,ender) ->
b1b2de81 119 detect_unitary_frees(List.map r.VT0.combiner_rec_expression expr_list)
34e49164 120 | _ -> k e in
faf9a90c 121
34e49164
C
122 let typeC r k t =
123 match Ast0.unwrap t with
124 Ast0.MetaType(name,_) -> checker name
125 | Ast0.DisjType(starter,types,mids,ender) ->
b1b2de81 126 detect_unitary_frees(List.map r.VT0.combiner_rec_typeC types)
34e49164 127 | _ -> k t in
faf9a90c 128
34e49164
C
129 let parameter r k p =
130 match Ast0.unwrap p with
131 Ast0.MetaParam(name,_) | Ast0.MetaParamList(name,_,_) -> checker name
132 | _ -> k p in
faf9a90c 133
34e49164
C
134 let declaration r k d =
135 match Ast0.unwrap d with
136 Ast0.DisjDecl(starter,decls,mids,ender) ->
b1b2de81 137 detect_unitary_frees(List.map r.VT0.combiner_rec_declaration decls)
34e49164
C
138 | _ -> k d in
139
fc1ad971
C
140 let case_line r k c =
141 match Ast0.unwrap c with
142 Ast0.DisjCase(starter,case_lines,mids,ender) ->
143 detect_unitary_frees(List.map r.VT0.combiner_rec_case_line case_lines)
144 | _ -> k c in
145
34e49164
C
146 let statement r k s =
147 match Ast0.unwrap s with
148 Ast0.MetaStmt(name,_) | Ast0.MetaStmtList(name,_) -> checker name
149 | Ast0.Disj(starter,stmt_list,mids,ender) ->
b1b2de81
C
150 detect_unitary_frees
151 (List.map r.VT0.combiner_rec_statement_dots stmt_list)
34e49164 152 | Ast0.Nest(starter,stmt_dots,ender,whn,multi) ->
b1b2de81 153 bind (r.VT0.combiner_rec_statement_dots stmt_dots)
faf9a90c 154 (detect_unitary_frees
34e49164 155 (List.map
b1b2de81 156 (whencode
ae4735db 157 r.VT0.combiner_rec_statement_dots
b1b2de81
C
158 r.VT0.combiner_rec_statement
159 r.VT0.combiner_rec_expression)
34e49164
C
160 whn))
161 | Ast0.Dots(d,whn) | Ast0.Circles(d,whn) | Ast0.Stars(d,whn) ->
162 detect_unitary_frees
163 (List.map
b1b2de81
C
164 (whencode
165 r.VT0.combiner_rec_statement_dots r.VT0.combiner_rec_statement
166 r.VT0.combiner_rec_expression)
34e49164
C
167 whn)
168 | _ -> k s in
faf9a90c
C
169
170 let res = V0.combiner bind option_default
b1b2de81
C
171 {V0.combiner_functions with
172 VT0.combiner_identfn = ident;
173 VT0.combiner_exprfn = expression;
174 VT0.combiner_tyfn = typeC;
175 VT0.combiner_paramfn = parameter;
176 VT0.combiner_declfn = declaration;
fc1ad971
C
177 VT0.combiner_stmtfn = statement;
178 VT0.combiner_casefn = case_line} in
faf9a90c 179
34e49164 180 collect_unitary_nonunitary
b1b2de81 181 (List.concat (List.map res.VT0.combiner_rec_top_level t))
faf9a90c 182
34e49164
C
183(* ----------------------------------------------------------------------- *)
184(* update the variables that are unitary *)
faf9a90c 185
34e49164 186let update_unitary unitary =
34e49164
C
187 let is_unitary name =
188 match (List.mem (Ast0.unwrap_mcode name) unitary,
485bce71
C
189 !Flag.sgrep_mode2, Ast0.get_mcode_mcodekind name) with
190 (true,true,_) | (true,_,Ast0.CONTEXT(_)) -> Ast0.PureContext
191 | (true,_,_) -> Ast0.Pure
192 | (false,true,_) | (false,_,Ast0.CONTEXT(_)) -> Ast0.Context
193 | (false,_,_) -> Ast0.Impure in
34e49164
C
194
195 let ident r k i =
196 match Ast0.unwrap i with
197 Ast0.MetaId(name,constraints,_) ->
198 Ast0.rewrap i (Ast0.MetaId(name,constraints,is_unitary name))
199 | Ast0.MetaFunc(name,constraints,_) ->
200 Ast0.rewrap i (Ast0.MetaFunc(name,constraints,is_unitary name))
201 | Ast0.MetaLocalFunc(name,constraints,_) ->
202 Ast0.rewrap i (Ast0.MetaLocalFunc(name,constraints,is_unitary name))
203 | _ -> k i in
204
205 let expression r k e =
206 match Ast0.unwrap e with
207 Ast0.MetaErr(name,constraints,_) ->
208 Ast0.rewrap e (Ast0.MetaErr(name,constraints,is_unitary name))
209 | Ast0.MetaExpr(name,constraints,ty,form,_) ->
210 Ast0.rewrap e (Ast0.MetaExpr(name,constraints,ty,form,is_unitary name))
211 | Ast0.MetaExprList(name,lenname,_) ->
212 Ast0.rewrap e (Ast0.MetaExprList(name,lenname,is_unitary name))
213 | _ -> k e in
faf9a90c 214
34e49164
C
215 let typeC r k t =
216 match Ast0.unwrap t with
217 Ast0.MetaType(name,_) ->
218 Ast0.rewrap t (Ast0.MetaType(name,is_unitary name))
219 | _ -> k t in
faf9a90c 220
34e49164
C
221 let parameter r k p =
222 match Ast0.unwrap p with
223 Ast0.MetaParam(name,_) ->
224 Ast0.rewrap p (Ast0.MetaParam(name,is_unitary name))
225 | Ast0.MetaParamList(name,lenname,_) ->
226 Ast0.rewrap p (Ast0.MetaParamList(name,lenname,is_unitary name))
227 | _ -> k p in
faf9a90c 228
34e49164
C
229 let statement r k s =
230 match Ast0.unwrap s with
231 Ast0.MetaStmt(name,_) ->
232 Ast0.rewrap s (Ast0.MetaStmt(name,is_unitary name))
233 | Ast0.MetaStmtList(name,_) ->
234 Ast0.rewrap s (Ast0.MetaStmtList(name,is_unitary name))
235 | _ -> k s in
faf9a90c 236
34e49164 237 let res = V0.rebuilder
b1b2de81
C
238 {V0.rebuilder_functions with
239 VT0.rebuilder_identfn = ident;
240 VT0.rebuilder_exprfn = expression;
241 VT0.rebuilder_tyfn = typeC;
242 VT0.rebuilder_paramfn = parameter;
243 VT0.rebuilder_stmtfn = statement} in
34e49164 244
b1b2de81 245 List.map res.VT0.rebuilder_rec_top_level
34e49164
C
246
247(* ----------------------------------------------------------------------- *)
248
249let rec split3 = function
250 [] -> ([],[],[])
251 | (a,b,c)::xs -> let (l1,l2,l3) = split3 xs in (a::l1,b::l2,c::l3)
252
253let rec combine3 = function
254 ([],[],[]) -> []
255 | (a::l1,b::l2,c::l3) -> (a,b,c) :: combine3 (l1,l2,l3)
256 | _ -> failwith "not possible"
257
258(* ----------------------------------------------------------------------- *)
259(* process all rules *)
260
261let do_unitary rules =
262 let rec loop = function
263 [] -> ([],[])
264 | (r::rules) ->
265 match r with
b1b2de81 266 Ast0.ScriptRule (_,_,_,_)
c3e37e97 267 | Ast0.InitialScriptRule (_,_,_) | Ast0.FinalScriptRule (_,_,_) ->
34e49164
C
268 let (x,rules) = loop rules in
269 (x, r::rules)
faf9a90c 270 | Ast0.CocciRule ((minus,metavars,chosen_isos),((plus,_) as plusz),rt) ->
34e49164
C
271 let mm1 = List.map Ast.get_meta_name metavars in
272 let (used_after, rest) = loop rules in
273 let (m_unitary, m_nonunitary) = get_free minus_checker minus in
274 let (p_unitary, p_nonunitary) = get_free plus_checker plus in
faf9a90c 275 let p_free =
34e49164
C
276 if !Flag.sgrep_mode2 then []
277 else p_unitary @ p_nonunitary in
278 let (in_p, m_unitary) =
279 List.partition (function x -> List.mem x p_free) m_unitary in
280 let m_nonunitary = in_p @ m_nonunitary in
281 let (m_unitary, not_local) =
282 List.partition (function x -> List.mem x mm1) m_unitary in
283 let m_unitary =
284 List.filter (function x -> not (List.mem x used_after))
285 m_unitary in
286 let rebuilt = update_unitary m_unitary minus in
287 (set_minus (m_nonunitary @ used_after) mm1,
288 (Ast0.CocciRule
faf9a90c 289 ((rebuilt, metavars, chosen_isos),plusz,rt))::rest) in
34e49164
C
290 let (_,rules) = loop rules in
291 rules
292
293(*
294let do_unitary minus plus =
295 let (minus,metavars,chosen_isos) = split3 minus in
296 let (plus,_) = List.split plus in
297 let rec loop = function
298 ([],[],[]) -> ([],[])
299 | (mm1::metavars,m1::minus,p1::plus) ->
300 let mm1 = List.map Ast.get_meta_name mm1 in
301 let (used_after,rest) = loop (metavars,minus,plus) in
302 let (m_unitary,m_nonunitary) = get_free minus_checker m1 in
303 let (p_unitary,p_nonunitary) = get_free plus_checker p1 in
304 let p_free =
305 if !Flag.sgrep_mode2
306 then []
307 else p_unitary @ p_nonunitary in
308 let (in_p,m_unitary) =
309 List.partition (function x -> List.mem x p_free) m_unitary in
310 let m_nonunitary = in_p@m_nonunitary in
311 let (m_unitary,not_local) =
312 List.partition (function x -> List.mem x mm1) m_unitary in
313 let m_unitary =
314 List.filter (function x -> not(List.mem x used_after)) m_unitary in
315 let rebuilt = update_unitary m_unitary m1 in
316 (set_minus (m_nonunitary @ used_after) mm1,
317 rebuilt::rest)
318 | _ -> failwith "not possible" in
319 let (_,rules) = loop (metavars,minus,plus) in
320 combine3 (rules,metavars,chosen_isos)
321*)