Commit | Line | Data |
---|---|---|
9bc82bae C |
1 | (* |
2 | * Copyright 2010, INRIA, University of Copenhagen | |
3 | * Julia Lawall, Rene Rydhof Hansen, Gilles Muller, Nicolas Palix | |
4 | * Copyright 2005-2009, Ecole des Mines de Nantes, University of Copenhagen | |
5 | * Yoann Padioleau, Julia Lawall, Rene Rydhof Hansen, Henrik Stuart, Gilles Muller, Nicolas Palix | |
6 | * This file is part of Coccinelle. | |
7 | * | |
8 | * Coccinelle is free software: you can redistribute it and/or modify | |
9 | * it under the terms of the GNU General Public License as published by | |
10 | * the Free Software Foundation, according to version 2 of the License. | |
11 | * | |
12 | * Coccinelle is distributed in the hope that it will be useful, | |
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 | * GNU General Public License for more details. | |
16 | * | |
17 | * You should have received a copy of the GNU General Public License | |
18 | * along with Coccinelle. If not, see <http://www.gnu.org/licenses/>. | |
19 | * | |
20 | * The authors reserve the right to distribute this or future versions of | |
21 | * Coccinelle under other licenses. | |
22 | *) | |
23 | ||
24 | ||
c491d8ee C |
25 | (* |
26 | * Copyright 2010, INRIA, University of Copenhagen | |
27 | * Julia Lawall, Rene Rydhof Hansen, Gilles Muller, Nicolas Palix | |
28 | * Copyright 2005-2009, Ecole des Mines de Nantes, University of Copenhagen | |
29 | * Yoann Padioleau, Julia Lawall, Rene Rydhof Hansen, Henrik Stuart, Gilles Muller, Nicolas Palix | |
30 | * This file is part of Coccinelle. | |
31 | * | |
32 | * Coccinelle is free software: you can redistribute it and/or modify | |
33 | * it under the terms of the GNU General Public License as published by | |
34 | * the Free Software Foundation, according to version 2 of the License. | |
35 | * | |
36 | * Coccinelle is distributed in the hope that it will be useful, | |
37 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
38 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
39 | * GNU General Public License for more details. | |
40 | * | |
41 | * You should have received a copy of the GNU General Public License | |
42 | * along with Coccinelle. If not, see <http://www.gnu.org/licenses/>. | |
43 | * | |
44 | * The authors reserve the right to distribute this or future versions of | |
45 | * Coccinelle under other licenses. | |
46 | *) | |
47 | ||
48 | ||
34e49164 C |
49 | (* find unitary metavariables *) |
50 | module Ast0 = Ast0_cocci | |
51 | module Ast = Ast_cocci | |
52 | module V0 = Visitor_ast0 | |
b1b2de81 | 53 | module VT0 = Visitor_ast0_types |
34e49164 C |
54 | |
55 | let set_minus s minus = List.filter (function n -> not (List.mem n minus)) s | |
56 | ||
57 | let rec nub = function | |
58 | [] -> [] | |
59 | | (x::xs) when (List.mem x xs) -> nub xs | |
60 | | (x::xs) -> x::(nub xs) | |
61 | ||
62 | (* ----------------------------------------------------------------------- *) | |
63 | (* Find the variables that occur free and occur free in a unitary way *) | |
64 | ||
65 | (* take everything *) | |
66 | let minus_checker name = let id = Ast0.unwrap_mcode name in [id] | |
67 | ||
68 | (* take only what is in the plus code *) | |
708f4980 | 69 | let plus_checker (nm,_,_,mc,_,_) = |
951c7801 | 70 | match mc with Ast0.PLUS _ -> [nm] | _ -> [] |
faf9a90c | 71 | |
34e49164 C |
72 | let get_free checker t = |
73 | let bind x y = x @ y in | |
74 | let option_default = [] in | |
faf9a90c | 75 | |
34e49164 C |
76 | (* considers a single list *) |
77 | let collect_unitary_nonunitary free_usage = | |
78 | let free_usage = List.sort compare free_usage in | |
79 | let rec loop1 todrop = function | |
80 | [] -> [] | |
81 | | (x::xs) as all -> if x = todrop then loop1 todrop xs else all in | |
82 | let rec loop2 = function | |
83 | [] -> ([],[]) | |
84 | | [x] -> ([x],[]) | |
85 | | x::y::xs -> | |
86 | if x = y | |
87 | then | |
88 | let (unitary,non_unitary) = loop2(loop1 x xs) in | |
89 | (unitary,x::non_unitary) | |
90 | else | |
91 | let (unitary,non_unitary) = loop2 (y::xs) in | |
92 | (x::unitary,non_unitary) in | |
93 | loop2 free_usage in | |
faf9a90c | 94 | |
34e49164 C |
95 | (* considers a list of lists *) |
96 | let detect_unitary_frees l = | |
97 | let (unitary,nonunitary) = | |
98 | List.split (List.map collect_unitary_nonunitary l) in | |
99 | let unitary = nub (List.concat unitary) in | |
100 | let nonunitary = nub (List.concat nonunitary) in | |
101 | let unitary = | |
102 | List.filter (function x -> not (List.mem x nonunitary)) unitary in | |
103 | unitary@nonunitary@nonunitary in | |
104 | ||
1be43e12 | 105 | let whencode afn bfn expression = function |
34e49164 C |
106 | Ast0.WhenNot(a) -> afn a |
107 | | Ast0.WhenAlways(b) -> bfn b | |
1be43e12 C |
108 | | Ast0.WhenModifier(_) -> option_default |
109 | | Ast0.WhenNotTrue(a) -> expression a | |
110 | | Ast0.WhenNotFalse(a) -> expression a in | |
faf9a90c | 111 | |
34e49164 C |
112 | let ident r k i = |
113 | match Ast0.unwrap i with | |
114 | Ast0.MetaId(name,_,_) | Ast0.MetaFunc(name,_,_) | |
115 | | Ast0.MetaLocalFunc(name,_,_) -> checker name | |
116 | | _ -> k i in | |
faf9a90c | 117 | |
34e49164 C |
118 | let expression r k e = |
119 | match Ast0.unwrap e with | |
120 | Ast0.MetaErr(name,_,_) | Ast0.MetaExpr(name,_,_,_,_) | |
121 | | Ast0.MetaExprList(name,_,_) -> checker name | |
122 | | Ast0.DisjExpr(starter,expr_list,mids,ender) -> | |
b1b2de81 | 123 | detect_unitary_frees(List.map r.VT0.combiner_rec_expression expr_list) |
34e49164 | 124 | | _ -> k e in |
faf9a90c | 125 | |
34e49164 C |
126 | let typeC r k t = |
127 | match Ast0.unwrap t with | |
128 | Ast0.MetaType(name,_) -> checker name | |
129 | | Ast0.DisjType(starter,types,mids,ender) -> | |
b1b2de81 | 130 | detect_unitary_frees(List.map r.VT0.combiner_rec_typeC types) |
34e49164 | 131 | | _ -> k t in |
faf9a90c | 132 | |
34e49164 C |
133 | let parameter r k p = |
134 | match Ast0.unwrap p with | |
135 | Ast0.MetaParam(name,_) | Ast0.MetaParamList(name,_,_) -> checker name | |
136 | | _ -> k p in | |
faf9a90c | 137 | |
34e49164 C |
138 | let declaration r k d = |
139 | match Ast0.unwrap d with | |
413ffc02 C |
140 | Ast0.MetaDecl(name,_) | Ast0.MetaField(name,_) -> checker name |
141 | | Ast0.DisjDecl(starter,decls,mids,ender) -> | |
b1b2de81 | 142 | detect_unitary_frees(List.map r.VT0.combiner_rec_declaration decls) |
34e49164 C |
143 | | _ -> k d in |
144 | ||
fc1ad971 C |
145 | let case_line r k c = |
146 | match Ast0.unwrap c with | |
147 | Ast0.DisjCase(starter,case_lines,mids,ender) -> | |
148 | detect_unitary_frees(List.map r.VT0.combiner_rec_case_line case_lines) | |
149 | | _ -> k c in | |
150 | ||
34e49164 C |
151 | let statement r k s = |
152 | match Ast0.unwrap s with | |
153 | Ast0.MetaStmt(name,_) | Ast0.MetaStmtList(name,_) -> checker name | |
154 | | Ast0.Disj(starter,stmt_list,mids,ender) -> | |
b1b2de81 C |
155 | detect_unitary_frees |
156 | (List.map r.VT0.combiner_rec_statement_dots stmt_list) | |
34e49164 | 157 | | Ast0.Nest(starter,stmt_dots,ender,whn,multi) -> |
b1b2de81 | 158 | bind (r.VT0.combiner_rec_statement_dots stmt_dots) |
faf9a90c | 159 | (detect_unitary_frees |
34e49164 | 160 | (List.map |
b1b2de81 | 161 | (whencode |
ae4735db | 162 | r.VT0.combiner_rec_statement_dots |
b1b2de81 C |
163 | r.VT0.combiner_rec_statement |
164 | r.VT0.combiner_rec_expression) | |
34e49164 C |
165 | whn)) |
166 | | Ast0.Dots(d,whn) | Ast0.Circles(d,whn) | Ast0.Stars(d,whn) -> | |
167 | detect_unitary_frees | |
168 | (List.map | |
b1b2de81 C |
169 | (whencode |
170 | r.VT0.combiner_rec_statement_dots r.VT0.combiner_rec_statement | |
171 | r.VT0.combiner_rec_expression) | |
34e49164 C |
172 | whn) |
173 | | _ -> k s in | |
faf9a90c C |
174 | |
175 | let res = V0.combiner bind option_default | |
b1b2de81 C |
176 | {V0.combiner_functions with |
177 | VT0.combiner_identfn = ident; | |
178 | VT0.combiner_exprfn = expression; | |
179 | VT0.combiner_tyfn = typeC; | |
180 | VT0.combiner_paramfn = parameter; | |
181 | VT0.combiner_declfn = declaration; | |
fc1ad971 C |
182 | VT0.combiner_stmtfn = statement; |
183 | VT0.combiner_casefn = case_line} in | |
faf9a90c | 184 | |
34e49164 | 185 | collect_unitary_nonunitary |
b1b2de81 | 186 | (List.concat (List.map res.VT0.combiner_rec_top_level t)) |
faf9a90c | 187 | |
34e49164 C |
188 | (* ----------------------------------------------------------------------- *) |
189 | (* update the variables that are unitary *) | |
faf9a90c | 190 | |
34e49164 | 191 | let update_unitary unitary = |
34e49164 C |
192 | let is_unitary name = |
193 | match (List.mem (Ast0.unwrap_mcode name) unitary, | |
485bce71 C |
194 | !Flag.sgrep_mode2, Ast0.get_mcode_mcodekind name) with |
195 | (true,true,_) | (true,_,Ast0.CONTEXT(_)) -> Ast0.PureContext | |
196 | | (true,_,_) -> Ast0.Pure | |
197 | | (false,true,_) | (false,_,Ast0.CONTEXT(_)) -> Ast0.Context | |
198 | | (false,_,_) -> Ast0.Impure in | |
34e49164 C |
199 | |
200 | let ident r k i = | |
201 | match Ast0.unwrap i with | |
202 | Ast0.MetaId(name,constraints,_) -> | |
203 | Ast0.rewrap i (Ast0.MetaId(name,constraints,is_unitary name)) | |
204 | | Ast0.MetaFunc(name,constraints,_) -> | |
205 | Ast0.rewrap i (Ast0.MetaFunc(name,constraints,is_unitary name)) | |
206 | | Ast0.MetaLocalFunc(name,constraints,_) -> | |
207 | Ast0.rewrap i (Ast0.MetaLocalFunc(name,constraints,is_unitary name)) | |
208 | | _ -> k i in | |
209 | ||
210 | let expression r k e = | |
211 | match Ast0.unwrap e with | |
212 | Ast0.MetaErr(name,constraints,_) -> | |
213 | Ast0.rewrap e (Ast0.MetaErr(name,constraints,is_unitary name)) | |
214 | | Ast0.MetaExpr(name,constraints,ty,form,_) -> | |
215 | Ast0.rewrap e (Ast0.MetaExpr(name,constraints,ty,form,is_unitary name)) | |
216 | | Ast0.MetaExprList(name,lenname,_) -> | |
217 | Ast0.rewrap e (Ast0.MetaExprList(name,lenname,is_unitary name)) | |
218 | | _ -> k e in | |
faf9a90c | 219 | |
34e49164 C |
220 | let typeC r k t = |
221 | match Ast0.unwrap t with | |
222 | Ast0.MetaType(name,_) -> | |
223 | Ast0.rewrap t (Ast0.MetaType(name,is_unitary name)) | |
224 | | _ -> k t in | |
faf9a90c | 225 | |
34e49164 C |
226 | let parameter r k p = |
227 | match Ast0.unwrap p with | |
228 | Ast0.MetaParam(name,_) -> | |
229 | Ast0.rewrap p (Ast0.MetaParam(name,is_unitary name)) | |
230 | | Ast0.MetaParamList(name,lenname,_) -> | |
231 | Ast0.rewrap p (Ast0.MetaParamList(name,lenname,is_unitary name)) | |
232 | | _ -> k p in | |
faf9a90c | 233 | |
34e49164 C |
234 | let statement r k s = |
235 | match Ast0.unwrap s with | |
236 | Ast0.MetaStmt(name,_) -> | |
237 | Ast0.rewrap s (Ast0.MetaStmt(name,is_unitary name)) | |
238 | | Ast0.MetaStmtList(name,_) -> | |
239 | Ast0.rewrap s (Ast0.MetaStmtList(name,is_unitary name)) | |
240 | | _ -> k s in | |
faf9a90c | 241 | |
34e49164 | 242 | let res = V0.rebuilder |
b1b2de81 C |
243 | {V0.rebuilder_functions with |
244 | VT0.rebuilder_identfn = ident; | |
245 | VT0.rebuilder_exprfn = expression; | |
246 | VT0.rebuilder_tyfn = typeC; | |
247 | VT0.rebuilder_paramfn = parameter; | |
248 | VT0.rebuilder_stmtfn = statement} in | |
34e49164 | 249 | |
b1b2de81 | 250 | List.map res.VT0.rebuilder_rec_top_level |
34e49164 C |
251 | |
252 | (* ----------------------------------------------------------------------- *) | |
253 | ||
254 | let rec split3 = function | |
255 | [] -> ([],[],[]) | |
256 | | (a,b,c)::xs -> let (l1,l2,l3) = split3 xs in (a::l1,b::l2,c::l3) | |
257 | ||
258 | let rec combine3 = function | |
259 | ([],[],[]) -> [] | |
260 | | (a::l1,b::l2,c::l3) -> (a,b,c) :: combine3 (l1,l2,l3) | |
261 | | _ -> failwith "not possible" | |
262 | ||
263 | (* ----------------------------------------------------------------------- *) | |
264 | (* process all rules *) | |
265 | ||
266 | let do_unitary rules = | |
267 | let rec loop = function | |
268 | [] -> ([],[]) | |
269 | | (r::rules) -> | |
270 | match r with | |
413ffc02 | 271 | Ast0.ScriptRule (_,_,_,_,_,_) |
174d1640 | 272 | | Ast0.InitialScriptRule (_,_,_,_) | Ast0.FinalScriptRule (_,_,_,_) -> |
34e49164 C |
273 | let (x,rules) = loop rules in |
274 | (x, r::rules) | |
faf9a90c | 275 | | Ast0.CocciRule ((minus,metavars,chosen_isos),((plus,_) as plusz),rt) -> |
34e49164 C |
276 | let mm1 = List.map Ast.get_meta_name metavars in |
277 | let (used_after, rest) = loop rules in | |
278 | let (m_unitary, m_nonunitary) = get_free minus_checker minus in | |
279 | let (p_unitary, p_nonunitary) = get_free plus_checker plus in | |
faf9a90c | 280 | let p_free = |
34e49164 C |
281 | if !Flag.sgrep_mode2 then [] |
282 | else p_unitary @ p_nonunitary in | |
283 | let (in_p, m_unitary) = | |
284 | List.partition (function x -> List.mem x p_free) m_unitary in | |
285 | let m_nonunitary = in_p @ m_nonunitary in | |
286 | let (m_unitary, not_local) = | |
287 | List.partition (function x -> List.mem x mm1) m_unitary in | |
288 | let m_unitary = | |
289 | List.filter (function x -> not (List.mem x used_after)) | |
290 | m_unitary in | |
291 | let rebuilt = update_unitary m_unitary minus in | |
292 | (set_minus (m_nonunitary @ used_after) mm1, | |
293 | (Ast0.CocciRule | |
faf9a90c | 294 | ((rebuilt, metavars, chosen_isos),plusz,rt))::rest) in |
34e49164 C |
295 | let (_,rules) = loop rules in |
296 | rules | |
297 | ||
298 | (* | |
299 | let do_unitary minus plus = | |
300 | let (minus,metavars,chosen_isos) = split3 minus in | |
301 | let (plus,_) = List.split plus in | |
302 | let rec loop = function | |
303 | ([],[],[]) -> ([],[]) | |
304 | | (mm1::metavars,m1::minus,p1::plus) -> | |
305 | let mm1 = List.map Ast.get_meta_name mm1 in | |
306 | let (used_after,rest) = loop (metavars,minus,plus) in | |
307 | let (m_unitary,m_nonunitary) = get_free minus_checker m1 in | |
308 | let (p_unitary,p_nonunitary) = get_free plus_checker p1 in | |
309 | let p_free = | |
310 | if !Flag.sgrep_mode2 | |
311 | then [] | |
312 | else p_unitary @ p_nonunitary in | |
313 | let (in_p,m_unitary) = | |
314 | List.partition (function x -> List.mem x p_free) m_unitary in | |
315 | let m_nonunitary = in_p@m_nonunitary in | |
316 | let (m_unitary,not_local) = | |
317 | List.partition (function x -> List.mem x mm1) m_unitary in | |
318 | let m_unitary = | |
319 | List.filter (function x -> not(List.mem x used_after)) m_unitary in | |
320 | let rebuilt = update_unitary m_unitary m1 in | |
321 | (set_minus (m_nonunitary @ used_after) mm1, | |
322 | rebuilt::rest) | |
323 | | _ -> failwith "not possible" in | |
324 | let (_,rules) = loop (metavars,minus,plus) in | |
325 | combine3 (rules,metavars,chosen_isos) | |
326 | *) |