Release coccinelle-0.1.6
[bpt/coccinelle.git] / parsing_cocci / .#parse_aux.ml.1.27
1 (*
2 * Copyright 2005-2009, Ecole des Mines de Nantes, University of Copenhagen
3 * Yoann Padioleau, Julia Lawall, Rene Rydhof Hansen, Henrik Stuart, Gilles Muller
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
23 (* exports everything, used only by parser_cocci_menhir.mly *)
24 module Ast0 = Ast0_cocci
25 module Ast = Ast_cocci
26
27 (* types for metavariable tokens *)
28 type info = Ast.meta_name * Ast0.pure * Data.clt
29 type idinfo = Ast.meta_name * Data.iconstraints * Ast0.pure * Data.clt
30 type expinfo = Ast.meta_name * Data.econstraints * Ast0.pure * Data.clt
31 type tyinfo = Ast.meta_name * Ast0.typeC list * Ast0.pure * Data.clt
32 type list_info = Ast.meta_name * Ast.meta_name option * Ast0.pure * Data.clt
33 type typed_info =
34 Ast.meta_name * Data.econstraints * Ast0.pure *
35 Type_cocci.typeC list option * Data.clt
36 type pos_info = Ast.meta_name * Data.pconstraints * Ast.meta_collect * Data.clt
37
38
39 let get_option fn = function
40 None -> None
41 | Some x -> Some (fn x)
42
43 let make_info line logical_line offset col strbef straft =
44 { Ast0.line_start = line; Ast0.line_end = line;
45 Ast0.logical_start = logical_line; Ast0.logical_end = logical_line;
46 Ast0.attachable_start = true; Ast0.attachable_end = true;
47 Ast0.mcode_start = []; Ast0.mcode_end = [];
48 Ast0.column = col; Ast0.offset = offset;
49 Ast0.strings_before = strbef; Ast0.strings_after = straft; }
50
51 let clt2info (_,line,logical_line,offset,col,strbef,straft,pos) =
52 make_info line logical_line offset col strbef straft
53
54 let drop_bef (arity,line,lline,offset,col,strbef,straft,pos) =
55 (arity,line,lline,offset,col,[],straft,pos)
56
57 let drop_aft (arity,line,lline,offset,col,strbef,straft,pos) =
58 (arity,line,lline,offset,col,strbef,[],pos)
59
60 let clt2mcode str = function
61 (Data.MINUS,line,lline,offset,col,strbef,straft,pos) ->
62 (str,Ast0.NONE,make_info line lline offset col strbef straft,
63 Ast0.MINUS(ref([],Ast0.default_token_info)),ref pos)
64 | (Data.OPTMINUS,line,lline,offset,col,strbef,straft,pos) ->
65 (str,Ast0.OPT,make_info line lline offset col strbef straft,
66 Ast0.MINUS(ref([],Ast0.default_token_info)),ref pos)
67 | (Data.UNIQUEMINUS,line,lline,offset,col,strbef,straft,pos) ->
68 (str,Ast0.UNIQUE,make_info line lline offset col strbef straft,
69 Ast0.MINUS(ref([],Ast0.default_token_info)),ref pos)
70 | (Data.PLUS,line,lline,offset,col,strbef,straft,pos) ->
71 (str,Ast0.NONE,make_info line lline offset col strbef straft,Ast0.PLUS,
72 ref pos)
73 | (Data.CONTEXT,line,lline,offset,col,strbef,straft,pos) ->
74 (str,Ast0.NONE,make_info line lline offset col strbef straft,
75 Ast0.CONTEXT(ref(Ast.NOTHING,
76 Ast0.default_token_info,Ast0.default_token_info)),
77 ref pos)
78 | (Data.OPT,line,lline,offset,col,strbef,straft,pos) ->
79 (str,Ast0.OPT,make_info line lline offset col strbef straft,
80 Ast0.CONTEXT(ref(Ast.NOTHING,
81 Ast0.default_token_info,Ast0.default_token_info)),
82 ref pos)
83 | (Data.UNIQUE,line,lline,offset,col,strbef,straft,pos) ->
84 (str,Ast0.UNIQUE,make_info line lline offset col strbef straft,
85 Ast0.CONTEXT(ref(Ast.NOTHING,
86 Ast0.default_token_info,Ast0.default_token_info)),
87 ref pos)
88
89 let id2name (name, clt) = name
90 let id2clt (name, clt) = clt
91 let id2mcode (name, clt) = clt2mcode name clt
92
93 let mkdots str (dot,whencode) =
94 match str with
95 "..." -> Ast0.wrap(Ast0.Dots(clt2mcode str dot, whencode))
96 | "ooo" -> Ast0.wrap(Ast0.Circles(clt2mcode str dot, whencode))
97 | "***" -> Ast0.wrap(Ast0.Stars(clt2mcode str dot, whencode))
98 | _ -> failwith "cannot happen"
99
100 let mkedots str (dot,whencode) =
101 match str with
102 "..." -> Ast0.wrap(Ast0.Edots(clt2mcode str dot, whencode))
103 | "ooo" -> Ast0.wrap(Ast0.Ecircles(clt2mcode str dot, whencode))
104 | "***" -> Ast0.wrap(Ast0.Estars(clt2mcode str dot, whencode))
105 | _ -> failwith "cannot happen"
106
107 let mkdpdots str dot =
108 match str with
109 "..." -> Ast0.wrap(Ast0.DPdots(clt2mcode str dot))
110 | "ooo" -> Ast0.wrap(Ast0.DPcircles(clt2mcode str dot))
111 | _ -> failwith "cannot happen"
112
113 let mkidots str (dot,whencode) =
114 match str with
115 "..." -> Ast0.wrap(Ast0.Idots(clt2mcode str dot, whencode))
116 | _ -> failwith "cannot happen"
117
118 let mkddots str (dot,whencode) =
119 match (str,whencode) with
120 ("...",None) -> Ast0.wrap(Ast0.Ddots(clt2mcode str dot, None))
121 | ("...",Some [w]) -> Ast0.wrap(Ast0.Ddots(clt2mcode str dot, Some w))
122 | _ -> failwith "cannot happen"
123
124 let mkpdots str dot =
125 match str with
126 "..." -> Ast0.wrap(Ast0.Pdots(clt2mcode str dot))
127 | "ooo" -> Ast0.wrap(Ast0.Pcircles(clt2mcode str dot))
128 | _ -> failwith "cannot happen"
129
130 let arith_op ast_op left op right =
131 Ast0.wrap
132 (Ast0.Binary(left, clt2mcode (Ast.Arith ast_op) op, right))
133
134 let logic_op ast_op left op right =
135 Ast0.wrap
136 (Ast0.Binary(left, clt2mcode (Ast.Logical ast_op) op, right))
137
138 let make_cv cv ty =
139 match cv with None -> ty | Some x -> Ast0.wrap (Ast0.ConstVol(x,ty))
140
141 let top_dots l =
142 let circle x =
143 match Ast0.unwrap x with Ast0.Circles(_) -> true | _ -> false in
144 let star x =
145 match Ast0.unwrap x with Ast0.Stars(_) -> true | _ -> false in
146 if List.exists circle l
147 then Ast0.wrap(Ast0.CIRCLES(l))
148 else
149 if List.exists star l
150 then Ast0.wrap(Ast0.STARS(l))
151 else Ast0.wrap(Ast0.DOTS(l))
152
153 (* here the offset is that of the first in the sequence of *s, not that of
154 each * individually *)
155 let pointerify ty m =
156 List.fold_left
157 (function inner ->
158 function cur ->
159 Ast0.wrap(Ast0.Pointer(inner,clt2mcode "*" cur)))
160 ty m
161
162 let ty_pointerify ty m =
163 List.fold_left
164 (function inner -> function cur -> Type_cocci.Pointer(inner))
165 ty m
166
167 (* Left is <=>, Right is =>. Collect <=>s. *)
168 (* The parser should have done this, with precedences. But whatever... *)
169 let iso_adjust fn first rest =
170 let rec loop = function
171 [] -> [[]]
172 | (Common.Left x)::rest ->
173 (match loop rest with
174 front::after -> (fn x::front)::after
175 | _ -> failwith "not possible")
176 | (Common.Right x)::rest ->
177 (match loop rest with
178 front::after -> []::(fn x::front)::after
179 | _ -> failwith "not possible") in
180 match loop rest with
181 front::after -> (fn first::front)::after
182 | _ -> failwith "not possible"
183
184 let check_meta tok =
185 let lookup rule name =
186 try
187 let info = Hashtbl.find Data.all_metadecls rule in
188 List.find (function mv -> Ast.get_meta_name mv = (rule,name)) info
189 with
190 Not_found ->
191 raise
192 (Semantic_cocci.Semantic
193 ("bad rule "^rule^" or bad variable "^name)) in
194 match tok with
195 Ast.MetaIdDecl(Ast.NONE,(rule,name)) ->
196 (match lookup rule name with
197 Ast.MetaIdDecl(_,_) | Ast.MetaFreshIdDecl(_,_) -> ()
198 | _ ->
199 raise
200 (Semantic_cocci.Semantic
201 ("incompatible inheritance declaration "^name)))
202 | Ast.MetaFreshIdDecl(Ast.NONE,(rule,name)) ->
203 raise
204 (Semantic_cocci.Semantic
205 "can't inherit the freshness of an identifier")
206 | Ast.MetaListlenDecl((rule,name)) ->
207 (match lookup rule name with
208 Ast.MetaListlenDecl(_) -> ()
209 | _ ->
210 raise
211 (Semantic_cocci.Semantic
212 ("incompatible inheritance declaration "^name)))
213 | Ast.MetaTypeDecl(Ast.NONE,(rule,name)) ->
214 (match lookup rule name with
215 Ast.MetaTypeDecl(_,_) -> ()
216 | _ ->
217 raise
218 (Semantic_cocci.Semantic
219 ("incompatible inheritance declaration "^name)))
220 | Ast.MetaInitDecl(Ast.NONE,(rule,name)) ->
221 (match lookup rule name with
222 Ast.MetaInitDecl(_,_) -> ()
223 | _ ->
224 raise
225 (Semantic_cocci.Semantic
226 ("incompatible inheritance declaration "^name)))
227 | Ast.MetaParamDecl(Ast.NONE,(rule,name)) ->
228 (match lookup rule name with
229 Ast.MetaParamDecl(_,_) -> ()
230 | _ ->
231 raise
232 (Semantic_cocci.Semantic
233 ("incompatible inheritance declaration "^name)))
234 | Ast.MetaParamListDecl(Ast.NONE,(rule,name),len_name) ->
235 (match lookup rule name with
236 Ast.MetaParamListDecl(_,_,_) -> ()
237 | _ ->
238 raise
239 (Semantic_cocci.Semantic
240 ("incompatible inheritance declaration "^name)))
241 | Ast.MetaErrDecl(Ast.NONE,(rule,name)) ->
242 (match lookup rule name with
243 Ast.MetaErrDecl(_,_) -> ()
244 | _ ->
245 raise
246 (Semantic_cocci.Semantic
247 ("incompatible inheritance declaration "^name)))
248 | Ast.MetaExpDecl(Ast.NONE,(rule,name),ty) ->
249 (match lookup rule name with
250 Ast.MetaExpDecl(_,_,ty1) when ty = ty1 -> ()
251 | _ ->
252 raise
253 (Semantic_cocci.Semantic
254 ("incompatible inheritance declaration "^name)))
255 | Ast.MetaIdExpDecl(Ast.NONE,(rule,name),ty) ->
256 (match lookup rule name with
257 Ast.MetaIdExpDecl(_,_,ty1) when ty = ty1 -> ()
258 | _ ->
259 raise
260 (Semantic_cocci.Semantic
261 ("incompatible inheritance declaration "^name)))
262 | Ast.MetaLocalIdExpDecl(Ast.NONE,(rule,name),ty) ->
263 (match lookup rule name with
264 Ast.MetaLocalIdExpDecl(_,_,ty1) when ty = ty1 -> ()
265 | _ ->
266 raise
267 (Semantic_cocci.Semantic
268 ("incompatible inheritance declaration "^name)))
269 | Ast.MetaExpListDecl(Ast.NONE,(rule,name),len_name) ->
270 (match lookup rule name with
271 Ast.MetaExpListDecl(_,_,_) -> ()
272 | Ast.MetaParamListDecl(_,_,_) when not (!Flag.make_hrule = None) -> ()
273 | _ ->
274 raise
275 (Semantic_cocci.Semantic
276 ("incompatible inheritance declaration "^name)))
277 | Ast.MetaStmDecl(Ast.NONE,(rule,name)) ->
278 (match lookup rule name with
279 Ast.MetaStmDecl(_,_) -> ()
280 | _ ->
281 raise
282 (Semantic_cocci.Semantic
283 ("incompatible inheritance declaration "^name)))
284 | Ast.MetaStmListDecl(Ast.NONE,(rule,name)) ->
285 (match lookup rule name with
286 Ast.MetaStmListDecl(_,_) -> ()
287 | _ ->
288 raise
289 (Semantic_cocci.Semantic
290 ("incompatible inheritance declaration "^name)))
291 | Ast.MetaFuncDecl(Ast.NONE,(rule,name)) ->
292 (match lookup rule name with
293 Ast.MetaFuncDecl(_,_) -> ()
294 | _ ->
295 raise
296 (Semantic_cocci.Semantic
297 ("incompatible inheritance declaration "^name)))
298 | Ast.MetaLocalFuncDecl(Ast.NONE,(rule,name)) ->
299 (match lookup rule name with
300 Ast.MetaLocalFuncDecl(_,_) -> ()
301 | _ ->
302 raise
303 (Semantic_cocci.Semantic
304 ("incompatible inheritance declaration "^name)))
305 | Ast.MetaConstDecl(Ast.NONE,(rule,name),ty) ->
306 (match lookup rule name with
307 Ast.MetaConstDecl(_,_,ty1) when ty = ty1 -> ()
308 | _ ->
309 raise
310 (Semantic_cocci.Semantic
311 ("incompatible inheritance declaration "^name)))
312 | Ast.MetaPosDecl(Ast.NONE,(rule,name)) ->
313 (match lookup rule name with
314 Ast.MetaPosDecl(_,_) ->
315 if not (List.mem rule !Data.inheritable_positions)
316 then
317 raise
318 (Semantic_cocci.Semantic
319 ("position cannot be inherited over modifications: "^name))
320 | _ ->
321 raise
322 (Semantic_cocci.Semantic
323 ("incompatible inheritance declaration "^name)))
324 | _ ->
325 raise
326 (Semantic_cocci.Semantic ("arity not allowed on imported declaration"))
327
328 let create_metadec ar ispure kindfn ids current_rule =
329 List.concat
330 (List.map
331 (function (rule,nm) ->
332 let (rule,checker) =
333 match rule with
334 None -> ((current_rule,nm),function x -> [Common.Left x])
335 | Some rule ->
336 ((rule,nm),
337 function x -> check_meta x; [Common.Right x]) in
338 kindfn ar rule ispure checker)
339 ids)
340
341 let create_metadec_ne ar ispure kindfn ids current_rule =
342 List.concat
343 (List.map
344 (function ((rule,nm),constraints) ->
345 let (rule,checker) =
346 match rule with
347 None -> ((current_rule,nm),function x -> [Common.Left x])
348 | Some rule ->
349 ((rule,nm),
350 function x -> check_meta x; [Common.Right x]) in
351 kindfn ar rule ispure checker constraints)
352 ids)
353
354 let create_metadec_ty ar ispure kindfn ids current_rule =
355 List.concat
356 (List.map
357 (function ((rule,nm),constraints) ->
358 let (rule,checker) =
359 match rule with
360 None -> ((current_rule,nm),function x -> [Common.Left x])
361 | Some rule ->
362 ((rule,nm),
363 function x -> check_meta x; [Common.Right x]) in
364 kindfn ar rule ispure checker constraints)
365 ids)
366
367 let create_len_metadec ar ispure kindfn lenid ids current_rule =
368 let lendec =
369 create_metadec Ast.NONE Ast0.Impure
370 (fun _ name _ check_meta -> check_meta(Ast.MetaListlenDecl(name)))
371 [lenid] current_rule in
372 let lenname =
373 match lendec with
374 [Common.Left (Ast.MetaListlenDecl(x))] -> x
375 | [Common.Right (Ast.MetaListlenDecl(x))] -> x
376 | _ -> failwith "unexpected length declaration" in
377 lendec@(create_metadec ar ispure (kindfn lenname) ids current_rule)
378
379 (* ---------------------------------------------------------------------- *)
380
381 let str2inc s =
382 let elements = Str.split (Str.regexp "/") s in
383 List.map (function "..." -> Ast.IncDots | s -> Ast.IncPath s) elements
384
385 (* ---------------------------------------------------------------------- *)
386 (* statements *)
387
388 let meta_stm name =
389 let (nm,pure,clt) = name in
390 Ast0.wrap(Ast0.MetaStmt(clt2mcode nm clt,pure))
391
392 let exp_stm exp pv =
393 Ast0.wrap(Ast0.ExprStatement (exp, clt2mcode ";" pv))
394
395 let ifthen iff lp tst rp thn =
396 Ast0.wrap(Ast0.IfThen(clt2mcode "if" iff,
397 clt2mcode "(" lp,tst,clt2mcode ")" rp,thn,
398 (Ast0.default_info(),Ast0.context_befaft())))
399
400 let ifthenelse iff lp tst rp thn e els =
401 Ast0.wrap(Ast0.IfThenElse(clt2mcode "if" iff,
402 clt2mcode "(" lp,tst,clt2mcode ")" rp,thn,
403 clt2mcode "else" e,els,
404 (Ast0.default_info(),Ast0.context_befaft())))
405
406 let forloop fr lp e1 sc1 e2 sc2 e3 rp s =
407 Ast0.wrap(Ast0.For(clt2mcode "for" fr,clt2mcode "(" lp,e1,
408 clt2mcode ";" sc1,e2,
409 clt2mcode ";" sc2,e3,clt2mcode ")" rp,s,
410 (Ast0.default_info(),Ast0.context_befaft())))
411
412 let whileloop w lp e rp s =
413 Ast0.wrap(Ast0.While(clt2mcode "while" w,clt2mcode "(" lp,
414 e,clt2mcode ")" rp,s,
415 (Ast0.default_info(),Ast0.context_befaft())))
416
417 let doloop d s w lp e rp pv =
418 Ast0.wrap(Ast0.Do(clt2mcode "do" d,s,clt2mcode "while" w,
419 clt2mcode "(" lp,e,clt2mcode ")" rp,
420 clt2mcode ";" pv))
421
422 let iterator i lp e rp s =
423 Ast0.wrap(Ast0.Iterator(i,clt2mcode "(" lp,e,clt2mcode ")" rp,s,
424 (Ast0.default_info(),Ast0.context_befaft())))
425
426 let switch s lp e rp lb c rb =
427 Ast0.wrap(Ast0.Switch(clt2mcode "switch" s,clt2mcode "(" lp,e,
428 clt2mcode ")" rp,clt2mcode "{" lb,
429 Ast0.wrap(Ast0.DOTS(c)),clt2mcode "}" rb))
430
431 let ret_exp r e pv =
432 Ast0.wrap(Ast0.ReturnExpr(clt2mcode "return" r,e,clt2mcode ";" pv))
433
434 let ret r pv =
435 Ast0.wrap(Ast0.Return(clt2mcode "return" r,clt2mcode ";" pv))
436
437 let break b pv =
438 Ast0.wrap(Ast0.Break(clt2mcode "break" b,clt2mcode ";" pv))
439
440 let cont c pv =
441 Ast0.wrap(Ast0.Continue(clt2mcode "continue" c,clt2mcode ";" pv))
442
443 let label i dd =
444 Ast0.wrap(Ast0.Label(i,clt2mcode ":" dd))
445
446 let goto g i pv =
447 Ast0.wrap(Ast0.Goto(clt2mcode "goto" g,i,clt2mcode ";" pv))
448
449 let seq lb s rb =
450 Ast0.wrap(Ast0.Seq(clt2mcode "{" lb,s,clt2mcode "}" rb))
451
452 (* ---------------------------------------------------------------------- *)
453
454 let make_iso_rule_name_result n =
455 (try let _ = Hashtbl.find Data.all_metadecls n in
456 raise (Semantic_cocci.Semantic ("repeated rule name"))
457 with Not_found -> ());
458 Ast.CocciRulename (Some n,Ast.NoDep,[],[],Ast.Undetermined,false (*discarded*))
459
460 let make_cocci_rule_name_result nm d i a e ee =
461 match nm with
462 Some nm ->
463 let n = id2name nm in
464 (try let _ = Hashtbl.find Data.all_metadecls n in
465 raise (Semantic_cocci.Semantic ("repeated rule name"))
466 with Not_found -> ());
467 Ast.CocciRulename (Some n,d,i,a,e,ee)
468 | None -> Ast.CocciRulename (None,d,i,a,e,ee)
469
470 let make_generated_rule_name_result nm d i a e ee =
471 match nm with
472 Some nm ->
473 let n = id2name nm in
474 (try let _ = Hashtbl.find Data.all_metadecls n in
475 raise (Semantic_cocci.Semantic ("repeated rule name"))
476 with Not_found -> ());
477 Ast.GeneratedRulename (Some n,d,i,a,e,ee)
478 | None -> Ast.GeneratedRulename (None,d,i,a,e,ee)
479
480 let make_script_rule_name_result lang deps =
481 let l = id2name lang in
482 Ast.ScriptRulename (l,deps)