Release coccinelle-0.2.3rc1
[bpt/coccinelle.git] / parsing_cocci / parse_aux.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
5636bb2c
C
23(*
24 * Copyright 2005-2010, Ecole des Mines de Nantes, University of Copenhagen
25 * Yoann Padioleau, Julia Lawall, Rene Rydhof Hansen, Henrik Stuart, Gilles Muller, Nicolas Palix
26 * This file is part of Coccinelle.
27 *
28 * Coccinelle is free software: you can redistribute it and/or modify
29 * it under the terms of the GNU General Public License as published by
30 * the Free Software Foundation, according to version 2 of the License.
31 *
32 * Coccinelle is distributed in the hope that it will be useful,
33 * but WITHOUT ANY WARRANTY; without even the implied warranty of
34 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
35 * GNU General Public License for more details.
36 *
37 * You should have received a copy of the GNU General Public License
38 * along with Coccinelle. If not, see <http://www.gnu.org/licenses/>.
39 *
40 * The authors reserve the right to distribute this or future versions of
41 * Coccinelle under other licenses.
42 *)
43
44
34e49164
C
45(* exports everything, used only by parser_cocci_menhir.mly *)
46module Ast0 = Ast0_cocci
47module Ast = Ast_cocci
48
49(* types for metavariable tokens *)
50type info = Ast.meta_name * Ast0.pure * Data.clt
51type idinfo = Ast.meta_name * Data.iconstraints * Ast0.pure * Data.clt
52type expinfo = Ast.meta_name * Data.econstraints * Ast0.pure * Data.clt
53type tyinfo = Ast.meta_name * Ast0.typeC list * Ast0.pure * Data.clt
54type list_info = Ast.meta_name * Ast.meta_name option * Ast0.pure * Data.clt
951c7801 55type typed_expinfo =
34e49164
C
56 Ast.meta_name * Data.econstraints * Ast0.pure *
57 Type_cocci.typeC list option * Data.clt
58type pos_info = Ast.meta_name * Data.pconstraints * Ast.meta_collect * Data.clt
59
34e49164
C
60let get_option fn = function
61 None -> None
62 | Some x -> Some (fn x)
63
64let make_info line logical_line offset col strbef straft =
0708f913
C
65 let new_pos_info =
66 {Ast0.line_start = line; Ast0.line_end = line;
67 Ast0.logical_start = logical_line; Ast0.logical_end = logical_line;
708f4980 68 Ast0.column = col; Ast0.offset = offset; } in
0708f913 69 { Ast0.pos_info = new_pos_info;
34e49164
C
70 Ast0.attachable_start = true; Ast0.attachable_end = true;
71 Ast0.mcode_start = []; Ast0.mcode_end = [];
34e49164
C
72 Ast0.strings_before = strbef; Ast0.strings_after = straft; }
73
74let clt2info (_,line,logical_line,offset,col,strbef,straft,pos) =
75 make_info line logical_line offset col strbef straft
76
77let drop_bef (arity,line,lline,offset,col,strbef,straft,pos) =
78 (arity,line,lline,offset,col,[],straft,pos)
79
80let drop_aft (arity,line,lline,offset,col,strbef,straft,pos) =
81 (arity,line,lline,offset,col,strbef,[],pos)
82
7f004419
C
83let drop_pos (arity,line,lline,offset,col,strbef,straft,pos) =
84 (arity,line,lline,offset,col,strbef,straft,Ast0.NoMetaPos)
85
34e49164
C
86let clt2mcode str = function
87 (Data.MINUS,line,lline,offset,col,strbef,straft,pos) ->
88 (str,Ast0.NONE,make_info line lline offset col strbef straft,
708f4980 89 Ast0.MINUS(ref([],Ast0.default_token_info)),ref pos,-1)
34e49164
C
90 | (Data.OPTMINUS,line,lline,offset,col,strbef,straft,pos) ->
91 (str,Ast0.OPT,make_info line lline offset col strbef straft,
708f4980 92 Ast0.MINUS(ref([],Ast0.default_token_info)),ref pos,-1)
34e49164
C
93 | (Data.UNIQUEMINUS,line,lline,offset,col,strbef,straft,pos) ->
94 (str,Ast0.UNIQUE,make_info line lline offset col strbef straft,
708f4980 95 Ast0.MINUS(ref([],Ast0.default_token_info)),ref pos,-1)
34e49164 96 | (Data.PLUS,line,lline,offset,col,strbef,straft,pos) ->
951c7801
C
97 (str,Ast0.NONE,make_info line lline offset col strbef straft,
98 Ast0.PLUS(Ast.ONE),ref pos,-1)
99 | (Data.PLUSPLUS,line,lline,offset,col,strbef,straft,pos) ->
100 (str,Ast0.NONE,make_info line lline offset col strbef straft,
101 Ast0.PLUS(Ast.MANY),ref pos,-1)
34e49164
C
102 | (Data.CONTEXT,line,lline,offset,col,strbef,straft,pos) ->
103 (str,Ast0.NONE,make_info line lline offset col strbef straft,
104 Ast0.CONTEXT(ref(Ast.NOTHING,
105 Ast0.default_token_info,Ast0.default_token_info)),
708f4980 106 ref pos,-1)
34e49164
C
107 | (Data.OPT,line,lline,offset,col,strbef,straft,pos) ->
108 (str,Ast0.OPT,make_info line lline offset col strbef straft,
109 Ast0.CONTEXT(ref(Ast.NOTHING,
110 Ast0.default_token_info,Ast0.default_token_info)),
708f4980 111 ref pos,-1)
34e49164
C
112 | (Data.UNIQUE,line,lline,offset,col,strbef,straft,pos) ->
113 (str,Ast0.UNIQUE,make_info line lline offset col strbef straft,
114 Ast0.CONTEXT(ref(Ast.NOTHING,
115 Ast0.default_token_info,Ast0.default_token_info)),
708f4980 116 ref pos,-1)
34e49164
C
117
118let id2name (name, clt) = name
119let id2clt (name, clt) = clt
120let id2mcode (name, clt) = clt2mcode name clt
121
122let mkdots str (dot,whencode) =
123 match str with
124 "..." -> Ast0.wrap(Ast0.Dots(clt2mcode str dot, whencode))
125 | "ooo" -> Ast0.wrap(Ast0.Circles(clt2mcode str dot, whencode))
126 | "***" -> Ast0.wrap(Ast0.Stars(clt2mcode str dot, whencode))
127 | _ -> failwith "cannot happen"
128
129let mkedots str (dot,whencode) =
130 match str with
131 "..." -> Ast0.wrap(Ast0.Edots(clt2mcode str dot, whencode))
132 | "ooo" -> Ast0.wrap(Ast0.Ecircles(clt2mcode str dot, whencode))
133 | "***" -> Ast0.wrap(Ast0.Estars(clt2mcode str dot, whencode))
134 | _ -> failwith "cannot happen"
135
136let mkdpdots str dot =
137 match str with
138 "..." -> Ast0.wrap(Ast0.DPdots(clt2mcode str dot))
139 | "ooo" -> Ast0.wrap(Ast0.DPcircles(clt2mcode str dot))
140 | _ -> failwith "cannot happen"
141
142let mkidots str (dot,whencode) =
143 match str with
144 "..." -> Ast0.wrap(Ast0.Idots(clt2mcode str dot, whencode))
145 | _ -> failwith "cannot happen"
146
147let mkddots str (dot,whencode) =
148 match (str,whencode) with
149 ("...",None) -> Ast0.wrap(Ast0.Ddots(clt2mcode str dot, None))
150 | ("...",Some [w]) -> Ast0.wrap(Ast0.Ddots(clt2mcode str dot, Some w))
151 | _ -> failwith "cannot happen"
152
153let mkpdots str dot =
154 match str with
155 "..." -> Ast0.wrap(Ast0.Pdots(clt2mcode str dot))
156 | "ooo" -> Ast0.wrap(Ast0.Pcircles(clt2mcode str dot))
157 | _ -> failwith "cannot happen"
158
159let arith_op ast_op left op right =
160 Ast0.wrap
161 (Ast0.Binary(left, clt2mcode (Ast.Arith ast_op) op, right))
162
163let logic_op ast_op left op right =
164 Ast0.wrap
165 (Ast0.Binary(left, clt2mcode (Ast.Logical ast_op) op, right))
166
167let make_cv cv ty =
168 match cv with None -> ty | Some x -> Ast0.wrap (Ast0.ConstVol(x,ty))
169
170let top_dots l =
171 let circle x =
172 match Ast0.unwrap x with Ast0.Circles(_) -> true | _ -> false in
173 let star x =
174 match Ast0.unwrap x with Ast0.Stars(_) -> true | _ -> false in
175 if List.exists circle l
176 then Ast0.wrap(Ast0.CIRCLES(l))
177 else
178 if List.exists star l
179 then Ast0.wrap(Ast0.STARS(l))
180 else Ast0.wrap(Ast0.DOTS(l))
181
182(* here the offset is that of the first in the sequence of *s, not that of
183each * individually *)
184let pointerify ty m =
185 List.fold_left
186 (function inner ->
187 function cur ->
188 Ast0.wrap(Ast0.Pointer(inner,clt2mcode "*" cur)))
189 ty m
190
191let ty_pointerify ty m =
192 List.fold_left
193 (function inner -> function cur -> Type_cocci.Pointer(inner))
194 ty m
195
196(* Left is <=>, Right is =>. Collect <=>s. *)
197(* The parser should have done this, with precedences. But whatever... *)
c3e37e97 198let iso_adjust first_fn fn first rest =
34e49164
C
199 let rec loop = function
200 [] -> [[]]
201 | (Common.Left x)::rest ->
202 (match loop rest with
203 front::after -> (fn x::front)::after
204 | _ -> failwith "not possible")
205 | (Common.Right x)::rest ->
206 (match loop rest with
207 front::after -> []::(fn x::front)::after
208 | _ -> failwith "not possible") in
209 match loop rest with
c3e37e97 210 front::after -> (first_fn first::front)::after
34e49164
C
211 | _ -> failwith "not possible"
212
5636bb2c 213let check_meta_tyopt type_irrelevant tok =
34e49164
C
214 let lookup rule name =
215 try
216 let info = Hashtbl.find Data.all_metadecls rule in
217 List.find (function mv -> Ast.get_meta_name mv = (rule,name)) info
218 with
219 Not_found ->
220 raise
221 (Semantic_cocci.Semantic
222 ("bad rule "^rule^" or bad variable "^name)) in
223 match tok with
224 Ast.MetaIdDecl(Ast.NONE,(rule,name)) ->
225 (match lookup rule name with
226 Ast.MetaIdDecl(_,_) | Ast.MetaFreshIdDecl(_,_) -> ()
227 | _ ->
228 raise
229 (Semantic_cocci.Semantic
230 ("incompatible inheritance declaration "^name)))
b1b2de81 231 | Ast.MetaFreshIdDecl((rule,name),seed) ->
34e49164
C
232 raise
233 (Semantic_cocci.Semantic
234 "can't inherit the freshness of an identifier")
235 | Ast.MetaListlenDecl((rule,name)) ->
236 (match lookup rule name with
237 Ast.MetaListlenDecl(_) -> ()
238 | _ ->
239 raise
240 (Semantic_cocci.Semantic
241 ("incompatible inheritance declaration "^name)))
242 | Ast.MetaTypeDecl(Ast.NONE,(rule,name)) ->
243 (match lookup rule name with
244 Ast.MetaTypeDecl(_,_) -> ()
245 | _ ->
246 raise
247 (Semantic_cocci.Semantic
248 ("incompatible inheritance declaration "^name)))
113803cf
C
249 | Ast.MetaInitDecl(Ast.NONE,(rule,name)) ->
250 (match lookup rule name with
251 Ast.MetaInitDecl(_,_) -> ()
252 | _ ->
253 raise
254 (Semantic_cocci.Semantic
255 ("incompatible inheritance declaration "^name)))
34e49164
C
256 | Ast.MetaParamDecl(Ast.NONE,(rule,name)) ->
257 (match lookup rule name with
258 Ast.MetaParamDecl(_,_) -> ()
259 | _ ->
260 raise
261 (Semantic_cocci.Semantic
262 ("incompatible inheritance declaration "^name)))
263 | Ast.MetaParamListDecl(Ast.NONE,(rule,name),len_name) ->
264 (match lookup rule name with
265 Ast.MetaParamListDecl(_,_,_) -> ()
266 | _ ->
267 raise
268 (Semantic_cocci.Semantic
269 ("incompatible inheritance declaration "^name)))
270 | Ast.MetaErrDecl(Ast.NONE,(rule,name)) ->
271 (match lookup rule name with
272 Ast.MetaErrDecl(_,_) -> ()
273 | _ ->
274 raise
275 (Semantic_cocci.Semantic
276 ("incompatible inheritance declaration "^name)))
277 | Ast.MetaExpDecl(Ast.NONE,(rule,name),ty) ->
278 (match lookup rule name with
5636bb2c 279 Ast.MetaExpDecl(_,_,ty1) when type_irrelevant or ty = ty1 -> ()
34e49164
C
280 | _ ->
281 raise
282 (Semantic_cocci.Semantic
283 ("incompatible inheritance declaration "^name)))
284 | Ast.MetaIdExpDecl(Ast.NONE,(rule,name),ty) ->
285 (match lookup rule name with
5636bb2c 286 Ast.MetaIdExpDecl(_,_,ty1) when type_irrelevant or ty = ty1 -> ()
34e49164
C
287 | _ ->
288 raise
289 (Semantic_cocci.Semantic
290 ("incompatible inheritance declaration "^name)))
291 | Ast.MetaLocalIdExpDecl(Ast.NONE,(rule,name),ty) ->
292 (match lookup rule name with
5636bb2c 293 Ast.MetaLocalIdExpDecl(_,_,ty1) when type_irrelevant or ty = ty1 -> ()
34e49164
C
294 | _ ->
295 raise
296 (Semantic_cocci.Semantic
297 ("incompatible inheritance declaration "^name)))
298 | Ast.MetaExpListDecl(Ast.NONE,(rule,name),len_name) ->
299 (match lookup rule name with
300 Ast.MetaExpListDecl(_,_,_) -> ()
faf9a90c 301 | Ast.MetaParamListDecl(_,_,_) when not (!Flag.make_hrule = None) -> ()
34e49164
C
302 | _ ->
303 raise
304 (Semantic_cocci.Semantic
305 ("incompatible inheritance declaration "^name)))
306 | Ast.MetaStmDecl(Ast.NONE,(rule,name)) ->
307 (match lookup rule name with
308 Ast.MetaStmDecl(_,_) -> ()
309 | _ ->
310 raise
311 (Semantic_cocci.Semantic
312 ("incompatible inheritance declaration "^name)))
313 | Ast.MetaStmListDecl(Ast.NONE,(rule,name)) ->
314 (match lookup rule name with
315 Ast.MetaStmListDecl(_,_) -> ()
316 | _ ->
317 raise
318 (Semantic_cocci.Semantic
319 ("incompatible inheritance declaration "^name)))
320 | Ast.MetaFuncDecl(Ast.NONE,(rule,name)) ->
321 (match lookup rule name with
322 Ast.MetaFuncDecl(_,_) -> ()
323 | _ ->
324 raise
325 (Semantic_cocci.Semantic
326 ("incompatible inheritance declaration "^name)))
327 | Ast.MetaLocalFuncDecl(Ast.NONE,(rule,name)) ->
328 (match lookup rule name with
329 Ast.MetaLocalFuncDecl(_,_) -> ()
330 | _ ->
331 raise
332 (Semantic_cocci.Semantic
333 ("incompatible inheritance declaration "^name)))
334 | Ast.MetaConstDecl(Ast.NONE,(rule,name),ty) ->
335 (match lookup rule name with
5636bb2c 336 Ast.MetaConstDecl(_,_,ty1) when type_irrelevant or ty = ty1 -> ()
34e49164
C
337 | _ ->
338 raise
339 (Semantic_cocci.Semantic
340 ("incompatible inheritance declaration "^name)))
341 | Ast.MetaPosDecl(Ast.NONE,(rule,name)) ->
342 (match lookup rule name with
343 Ast.MetaPosDecl(_,_) ->
344 if not (List.mem rule !Data.inheritable_positions)
345 then
346 raise
347 (Semantic_cocci.Semantic
348 ("position cannot be inherited over modifications: "^name))
349 | _ ->
350 raise
351 (Semantic_cocci.Semantic
352 ("incompatible inheritance declaration "^name)))
353 | _ ->
354 raise
355 (Semantic_cocci.Semantic ("arity not allowed on imported declaration"))
356
5636bb2c
C
357let check_meta m = check_meta_tyopt false m
358
359let check_inherited_constraint meta_name fn =
360 match meta_name with
361 (None,_) -> failwith "constraint must be an inherited variable"
362 | (Some rule,name) ->
363 let i = (rule,name) in
364 check_meta_tyopt true (fn i);
365 i
366
34e49164
C
367let create_metadec ar ispure kindfn ids current_rule =
368 List.concat
369 (List.map
370 (function (rule,nm) ->
371 let (rule,checker) =
372 match rule with
373 None -> ((current_rule,nm),function x -> [Common.Left x])
374 | Some rule ->
375 ((rule,nm),
376 function x -> check_meta x; [Common.Right x]) in
377 kindfn ar rule ispure checker)
378 ids)
379
ae4735db
C
380
381let create_metadec_virt ar ispure kindfn ids current_rule =
382 List.concat
383 (List.map
384 (function nm ->
385 let checker = function x -> [Common.Right x] in
386 kindfn ar nm ispure checker !Flag.defined_virtual_env)
387 ids)
388
b1b2de81
C
389let create_fresh_metadec kindfn ids current_rule =
390 List.concat
391 (List.map
392 (function ((rule,nm),seed) ->
393 let (rule,checker) =
394 match rule with
395 None -> ((current_rule,nm),function x -> [Common.Left x])
396 | Some rule ->
397 ((rule,nm),
398 function x -> check_meta x; [Common.Right x]) in
399 kindfn rule checker seed)
400 ids)
401
951c7801 402let create_metadec_with_constraints ar ispure kindfn ids current_rule =
34e49164
C
403 List.concat
404 (List.map
405 (function ((rule,nm),constraints) ->
406 let (rule,checker) =
407 match rule with
951c7801
C
408 None -> ((current_rule,nm),function x -> [Common.Left x])
409 | Some rule ->
410 ((rule,nm),
411 function x -> check_meta x; [Common.Right x]) in
412 kindfn ar rule ispure checker constraints)
34e49164
C
413 ids)
414
415let create_metadec_ty ar ispure kindfn ids current_rule =
416 List.concat
417 (List.map
418 (function ((rule,nm),constraints) ->
419 let (rule,checker) =
420 match rule with
421 None -> ((current_rule,nm),function x -> [Common.Left x])
422 | Some rule ->
423 ((rule,nm),
424 function x -> check_meta x; [Common.Right x]) in
425 kindfn ar rule ispure checker constraints)
426 ids)
427
428let create_len_metadec ar ispure kindfn lenid ids current_rule =
429 let lendec =
430 create_metadec Ast.NONE Ast0.Impure
431 (fun _ name _ check_meta -> check_meta(Ast.MetaListlenDecl(name)))
432 [lenid] current_rule in
433 let lenname =
434 match lendec with
435 [Common.Left (Ast.MetaListlenDecl(x))] -> x
436 | [Common.Right (Ast.MetaListlenDecl(x))] -> x
437 | _ -> failwith "unexpected length declaration" in
438 lendec@(create_metadec ar ispure (kindfn lenname) ids current_rule)
439
440(* ---------------------------------------------------------------------- *)
441
442let str2inc s =
443 let elements = Str.split (Str.regexp "/") s in
444 List.map (function "..." -> Ast.IncDots | s -> Ast.IncPath s) elements
445
446(* ---------------------------------------------------------------------- *)
447(* statements *)
448
449let meta_stm name =
450 let (nm,pure,clt) = name in
451 Ast0.wrap(Ast0.MetaStmt(clt2mcode nm clt,pure))
452
453let exp_stm exp pv =
454 Ast0.wrap(Ast0.ExprStatement (exp, clt2mcode ";" pv))
455
456let ifthen iff lp tst rp thn =
457 Ast0.wrap(Ast0.IfThen(clt2mcode "if" iff,
458 clt2mcode "(" lp,tst,clt2mcode ")" rp,thn,
459 (Ast0.default_info(),Ast0.context_befaft())))
460
461let ifthenelse iff lp tst rp thn e els =
462 Ast0.wrap(Ast0.IfThenElse(clt2mcode "if" iff,
463 clt2mcode "(" lp,tst,clt2mcode ")" rp,thn,
464 clt2mcode "else" e,els,
465 (Ast0.default_info(),Ast0.context_befaft())))
466
467let forloop fr lp e1 sc1 e2 sc2 e3 rp s =
468 Ast0.wrap(Ast0.For(clt2mcode "for" fr,clt2mcode "(" lp,e1,
469 clt2mcode ";" sc1,e2,
470 clt2mcode ";" sc2,e3,clt2mcode ")" rp,s,
471 (Ast0.default_info(),Ast0.context_befaft())))
472
473let whileloop w lp e rp s =
474 Ast0.wrap(Ast0.While(clt2mcode "while" w,clt2mcode "(" lp,
475 e,clt2mcode ")" rp,s,
476 (Ast0.default_info(),Ast0.context_befaft())))
477
478let doloop d s w lp e rp pv =
479 Ast0.wrap(Ast0.Do(clt2mcode "do" d,s,clt2mcode "while" w,
480 clt2mcode "(" lp,e,clt2mcode ")" rp,
481 clt2mcode ";" pv))
482
483let iterator i lp e rp s =
484 Ast0.wrap(Ast0.Iterator(i,clt2mcode "(" lp,e,clt2mcode ")" rp,s,
485 (Ast0.default_info(),Ast0.context_befaft())))
486
fc1ad971
C
487let switch s lp e rp lb d c rb =
488 let d =
489 List.map
490 (function d ->
491 Ast0.wrap(Ast0.Decl((Ast0.default_info(),Ast0.context_befaft()),d)))
492 d in
34e49164
C
493 Ast0.wrap(Ast0.Switch(clt2mcode "switch" s,clt2mcode "(" lp,e,
494 clt2mcode ")" rp,clt2mcode "{" lb,
fc1ad971 495 Ast0.wrap(Ast0.DOTS(d)),
34e49164
C
496 Ast0.wrap(Ast0.DOTS(c)),clt2mcode "}" rb))
497
498let ret_exp r e pv =
499 Ast0.wrap(Ast0.ReturnExpr(clt2mcode "return" r,e,clt2mcode ";" pv))
500
501let ret r pv =
502 Ast0.wrap(Ast0.Return(clt2mcode "return" r,clt2mcode ";" pv))
503
504let break b pv =
505 Ast0.wrap(Ast0.Break(clt2mcode "break" b,clt2mcode ";" pv))
506
507let cont c pv =
508 Ast0.wrap(Ast0.Continue(clt2mcode "continue" c,clt2mcode ";" pv))
509
510let label i dd =
511 Ast0.wrap(Ast0.Label(i,clt2mcode ":" dd))
512
513let goto g i pv =
514 Ast0.wrap(Ast0.Goto(clt2mcode "goto" g,i,clt2mcode ";" pv))
515
516let seq lb s rb =
517 Ast0.wrap(Ast0.Seq(clt2mcode "{" lb,s,clt2mcode "}" rb))
518
519(* ---------------------------------------------------------------------- *)
520
521let make_iso_rule_name_result n =
522 (try let _ = Hashtbl.find Data.all_metadecls n in
523 raise (Semantic_cocci.Semantic ("repeated rule name"))
524 with Not_found -> ());
525 Ast.CocciRulename (Some n,Ast.NoDep,[],[],Ast.Undetermined,false (*discarded*))
526
527let make_cocci_rule_name_result nm d i a e ee =
528 match nm with
529 Some nm ->
530 let n = id2name nm in
531 (try let _ = Hashtbl.find Data.all_metadecls n in
532 raise (Semantic_cocci.Semantic ("repeated rule name"))
533 with Not_found -> ());
534 Ast.CocciRulename (Some n,d,i,a,e,ee)
535 | None -> Ast.CocciRulename (None,d,i,a,e,ee)
536
faf9a90c
C
537let make_generated_rule_name_result nm d i a e ee =
538 match nm with
539 Some nm ->
540 let n = id2name nm in
541 (try let _ = Hashtbl.find Data.all_metadecls n in
542 raise (Semantic_cocci.Semantic ("repeated rule name"))
543 with Not_found -> ());
544 Ast.GeneratedRulename (Some n,d,i,a,e,ee)
545 | None -> Ast.GeneratedRulename (None,d,i,a,e,ee)
546
1be43e12 547let make_script_rule_name_result lang deps =
34e49164 548 let l = id2name lang in
708f4980 549 Ast.ScriptRulename (l,deps)
b1b2de81 550
c3e37e97 551let make_initial_script_rule_name_result lang deps =
b1b2de81 552 let l = id2name lang in
c3e37e97 553 Ast.InitialScriptRulename(l,deps)
b1b2de81 554
c3e37e97 555let make_final_script_rule_name_result lang deps =
b1b2de81 556 let l = id2name lang in
c3e37e97 557 Ast.FinalScriptRulename(l,deps)
978fd7e5
C
558
559(* Allows type alone only when it is void and only when there is only one
560 parameter. This avoids ambiguity problems in the parser. *)
561let verify_parameter_declarations = function
562 [] -> ()
563 | [x] ->
564 (match Ast0.unwrap x with
565 Ast0.Param(t, None) ->
566 (match Ast0.unwrap t with
567 Ast0.BaseType(Ast.VoidType,_) -> ()
568 | _ ->
569 failwith
570 (Printf.sprintf
571 "%d: only void can be a parameter without an identifier"
572 (Ast0.get_line t)))
573 | _ -> ())
574 | l ->
575 List.iter
576 (function x ->
577 match Ast0.unwrap x with
578 Ast0.Param(t, None) ->
579 failwith
580 (Printf.sprintf
581 "%d: only void alone can be a parameter without an identifier"
582 (Ast0.get_line t))
583 | _ -> ())
584 l