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