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