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