2 * Copyright 2005-2008, 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.
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.
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.
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/>.
18 * The authors reserve the right to distribute this or future versions of
19 * Coccinelle under other licenses.
23 (* --------------------------------------------------------------------- *)
26 type info = { line : int; column : int;
27 strbef : string list; straft : string list }
29 type meta_name = string * string
30 (* need to be careful about rewrapping, to avoid duplicating pos info
31 currently, the pos info is always None until asttoctl2. *)
35 free_vars : meta_name list; (*free vars*)
36 minus_free_vars : meta_name list; (*minus free vars*)
37 fresh_vars : meta_name list; (*fresh vars*)
38 inherited : meta_name list; (*inherited vars*)
39 saved_witness : meta_name list; (*witness vars*)
40 bef_aft : dots_bef_aft;
41 (* the following is for or expressions *)
42 pos_info : meta_name mcode option; (* pos info, try not to duplicate *)
43 true_if_test_exp : bool;(* true if "test_exp from iso", only for exprs *)
44 (* isos relevant to the term; ultimately only used for rule_elems *)
45 iso_info : (string*anything) list }
48 BEFORE of 'a list list
49 | AFTER of 'a list list
50 | BEFOREAFTER of 'a list list * 'a list list
53 and 'a mcode = 'a * info * mcodekind * meta_pos (* pos variable *)
54 (* pos is an offset indicating where in the C code the mcodekind has an
57 MINUS of pos * anything list list
58 | CONTEXT of pos * anything befaft
61 Real of int (* charpos *) | Virt of int * int (* charpos + offset *)
62 and pos = NoPos | DontCarePos | FixPos of (fixpos * fixpos)
66 | AddingBetweenDots of statement * int (*index of let var*)
67 | DroppingBetweenDots of statement * int (*index of let var*)
69 and inherited = Type_cocci.inherited
70 and keep_binding = Type_cocci.keep_binding
71 and multi = bool (*true if a nest is one or more, false if it is zero or more*)
74 meta_name list (*free vars*) * meta_name list (*inherited vars*) *
75 meta_name list (*witness vars*) * mcodekind
77 (* --------------------------------------------------------------------- *)
80 and arity = UNIQUE | OPT | MULTI | NONE
83 MetaIdDecl of arity * meta_name (* name *)
84 | MetaFreshIdDecl of arity * meta_name (* name *)
85 | MetaTypeDecl of arity * meta_name (* name *)
86 | MetaListlenDecl of meta_name (* name *)
87 | MetaParamDecl of arity * meta_name (* name *)
88 | MetaParamListDecl of arity * meta_name (*name*) * meta_name option (*len*)
90 arity * meta_name (* name *) * Type_cocci.typeC list option
91 | MetaErrDecl of arity * meta_name (* name *)
93 arity * meta_name (* name *) * Type_cocci.typeC list option
95 arity * meta_name (* name *) * Type_cocci.typeC list option
96 | MetaLocalIdExpDecl of
97 arity * meta_name (* name *) * Type_cocci.typeC list option
98 | MetaExpListDecl of arity * meta_name (*name*) * meta_name option (*len*)
99 | MetaStmDecl of arity * meta_name (* name *)
100 | MetaStmListDecl of arity * meta_name (* name *)
101 | MetaFuncDecl of arity * meta_name (* name *)
102 | MetaLocalFuncDecl of arity * meta_name (* name *)
103 | MetaPosDecl of arity * meta_name (* name *)
104 | MetaDeclarerDecl of arity * meta_name (* name *)
105 | MetaIteratorDecl of arity * meta_name (* name *)
107 (* --------------------------------------------------------------------- *)
108 (* --------------------------------------------------------------------- *)
116 and 'a dots = 'a base_dots wrap
118 (* --------------------------------------------------------------------- *)
124 | MetaId of meta_name mcode * ident list * keep_binding * inherited
125 | MetaFunc of meta_name mcode * ident list * keep_binding * inherited
126 | MetaLocalFunc of meta_name mcode * ident list * keep_binding * inherited
129 | UniqueIdent of ident
131 and ident = base_ident wrap
133 (* --------------------------------------------------------------------- *)
136 and base_expression =
138 | Constant of constant mcode
139 | FunCall of expression * string mcode (* ( *) *
140 expression dots * string mcode (* ) *)
141 | Assignment of expression * assignOp mcode * expression *
142 bool (* true if it can match an initialization *)
143 | CondExpr of expression * string mcode (* ? *) * expression option *
144 string mcode (* : *) * expression
145 | Postfix of expression * fixOp mcode
146 | Infix of expression * fixOp mcode
147 | Unary of expression * unaryOp mcode
148 | Binary of expression * binaryOp mcode * expression
149 | Nested of expression * binaryOp mcode * expression
150 | ArrayAccess of expression * string mcode (* [ *) * expression *
152 | RecordAccess of expression * string mcode (* . *) * ident
153 | RecordPtAccess of expression * string mcode (* -> *) * ident
154 | Cast of string mcode (* ( *) * fullType * string mcode (* ) *) *
156 | SizeOfExpr of string mcode (* sizeof *) * expression
157 | SizeOfType of string mcode (* sizeof *) * string mcode (* ( *) *
158 fullType * string mcode (* ) *)
159 | TypeExp of fullType (*type name used as an expression, only in
162 | Paren of string mcode (* ( *) * expression *
165 | MetaErr of meta_name mcode * expression list * keep_binding *
167 | MetaExpr of meta_name mcode * expression list * keep_binding *
168 Type_cocci.typeC list option * form * inherited
169 | MetaExprList of meta_name mcode * listlen option * keep_binding *
170 inherited (* only in arg lists *)
172 | EComma of string mcode (* only in arg lists *)
174 | DisjExpr of expression list
175 | NestExpr of expression dots * expression option * multi
177 (* can appear in arg lists, and also inside Nest, as in:
178 if(< ... X ... Y ...>)
179 In the following, the expression option is the WHEN *)
180 | Edots of string mcode (* ... *) * expression option
181 | Ecircles of string mcode (* ooo *) * expression option
182 | Estars of string mcode (* *** *) * expression option
184 | OptExp of expression
185 | UniqueExp of expression
187 (* ANY = int E; ID = idexpression int X; CONST = constant int X; *)
188 and form = ANY | ID | LocalID | CONST (* form for MetaExp *)
190 and expression = base_expression wrap
192 and listlen = meta_name mcode * keep_binding * inherited
194 and unaryOp = GetRef | DeRef | UnPlus | UnMinus | Tilde | Not
195 and assignOp = SimpleAssign | OpAssign of arithOp
196 and fixOp = Dec | Inc
198 and binaryOp = Arith of arithOp | Logical of logicalOp
200 Plus | Minus | Mul | Div | Mod | DecLeft | DecRight | And | Or | Xor
201 and logicalOp = Inf | Sup | InfEq | SupEq | Eq | NotEq | AndLog | OrLog
209 (* --------------------------------------------------------------------- *)
213 Type of const_vol mcode option * typeC
214 | DisjType of fullType list (* only after iso *)
215 | OptType of fullType
216 | UniqueType of fullType
219 BaseType of baseType mcode * sign mcode option
220 | ImplicitInt of sign mcode
221 | Pointer of fullType * string mcode (* * *)
222 | FunctionPointer of fullType *
223 string mcode(* ( *)*string mcode(* * *)*string mcode(* ) *)*
224 string mcode (* ( *)*parameter_list*string mcode(* ) *)
226 (* used for the automatic managment of prototypes *)
227 | FunctionType of bool (* true if all minus for dropping return type *) *
229 string mcode (* ( *) * parameter_list *
232 | Array of fullType * string mcode (* [ *) *
233 expression option * string mcode (* ] *)
234 | StructUnionName of structUnion mcode * ident option (* name *)
235 | StructUnionDef of fullType (* either StructUnionName or metavar *) *
236 string mcode (* { *) * declaration dots * string mcode (* } *)
237 | TypeName of string mcode
239 | MetaType of meta_name mcode * keep_binding * inherited
241 and fullType = base_fullType wrap
242 and typeC = base_typeC wrap
244 and baseType = VoidType | CharType | ShortType | IntType | DoubleType
245 | FloatType | LongType
247 and structUnion = Struct | Union
249 and sign = Signed | Unsigned
251 and const_vol = Const | Volatile
253 (* --------------------------------------------------------------------- *)
254 (* Variable declaration *)
255 (* Even if the Cocci program specifies a list of declarations, they are
256 split out into multiple declarations of a single variable each. *)
258 and base_declaration =
259 Init of storage mcode option * fullType * ident * string mcode (*=*) *
260 initialiser * string mcode (*;*)
261 | UnInit of storage mcode option * fullType * ident * string mcode (* ; *)
262 | TyDecl of fullType * string mcode (* ; *)
263 | MacroDecl of ident (* name *) * string mcode (* ( *) *
264 expression dots * string mcode (* ) *) * string mcode (* ; *)
265 | Typedef of string mcode (*typedef*) * fullType *
266 typeC (* either TypeName or metavar *) * string mcode (*;*)
267 | DisjDecl of declaration list
268 (* Ddots is for a structure declaration *)
269 | Ddots of string mcode (* ... *) * declaration option (* whencode *)
271 | MetaDecl of meta_name mcode * keep_binding * inherited
273 | OptDecl of declaration
274 | UniqueDecl of declaration
276 and declaration = base_declaration wrap
278 (* --------------------------------------------------------------------- *)
281 and base_initialiser =
282 InitExpr of expression
283 | InitList of string mcode (*{*) * initialiser list * string mcode (*}*) *
284 initialiser list (* whencode: elements that shouldn't appear in init *)
286 string mcode (*.*) * ident (* name *) * string mcode (*=*) *
287 initialiser (* gccext: *)
288 | InitGccName of ident (* name *) * string mcode (*:*) *
291 string mcode (*[*) * expression * string mcode (*]*) *
292 string mcode (*=*) * initialiser
294 string mcode (*[*) * expression * string mcode (*...*) *
295 expression * string mcode (*]*) * string mcode (*=*) * initialiser
296 | IComma of string mcode (* , *)
298 | OptIni of initialiser
299 | UniqueIni of initialiser
301 and initialiser = base_initialiser wrap
303 (* --------------------------------------------------------------------- *)
306 and base_parameterTypeDef =
307 VoidParam of fullType
308 | Param of fullType * ident option
310 | MetaParam of meta_name mcode * keep_binding * inherited
311 | MetaParamList of meta_name mcode * listlen option * keep_binding *
314 | PComma of string mcode
316 | Pdots of string mcode (* ... *)
317 | Pcircles of string mcode (* ooo *)
319 | OptParam of parameterTypeDef
320 | UniqueParam of parameterTypeDef
322 and parameterTypeDef = base_parameterTypeDef wrap
324 and parameter_list = parameterTypeDef dots
326 (* --------------------------------------------------------------------- *)
327 (* #define Parameters *)
329 and base_define_param =
331 | DPComma of string mcode
332 | DPdots of string mcode (* ... *)
333 | DPcircles of string mcode (* ooo *)
334 | OptDParam of define_param
335 | UniqueDParam of define_param
337 and define_param = base_define_param wrap
339 and base_define_parameters =
340 NoParams (* not parameter list, not an empty one *)
341 | DParams of string mcode(*( *) * define_param dots * string mcode(* )*)
343 and define_parameters = base_define_parameters wrap
345 (* --------------------------------------------------------------------- *)
348 (* PER = keep bindings separate, ALL = collect them *)
349 and meta_collect = PER | ALL
352 MetaPos of meta_name mcode * meta_name list *
353 meta_collect * keep_binding * inherited
356 (* --------------------------------------------------------------------- *)
357 (* Function declaration *)
359 and storage = Static | Auto | Register | Extern
361 (* --------------------------------------------------------------------- *)
365 FunHeader of mcodekind (* before the function header *) *
366 bool (* true if all minus, for dropping static, etc *) *
367 fninfo list * ident (* name *) *
368 string mcode (* ( *) * parameter_list *
370 | Decl of mcodekind (* before the decl *) *
371 bool (* true if all minus *) * declaration
373 | SeqStart of string mcode (* { *)
374 | SeqEnd of string mcode (* } *)
376 | ExprStatement of expression * string mcode (*;*)
377 | IfHeader of string mcode (* if *) * string mcode (* ( *) *
378 expression * string mcode (* ) *)
379 | Else of string mcode (* else *)
380 | WhileHeader of string mcode (* while *) * string mcode (* ( *) *
381 expression * string mcode (* ) *)
382 | DoHeader of string mcode (* do *)
383 | WhileTail of string mcode (* while *) * string mcode (* ( *) *
384 expression * string mcode (* ) *) *
386 | ForHeader of string mcode (* for *) * string mcode (* ( *) *
387 expression option * string mcode (*;*) *
388 expression option * string mcode (*;*) *
389 expression option * string mcode (* ) *)
390 | IteratorHeader of ident (* name *) * string mcode (* ( *) *
391 expression dots * string mcode (* ) *)
392 | SwitchHeader of string mcode (* switch *) * string mcode (* ( *) *
393 expression * string mcode (* ) *)
394 | Break of string mcode (* break *) * string mcode (* ; *)
395 | Continue of string mcode (* continue *) * string mcode (* ; *)
396 | Label of ident * string mcode (* : *)
397 | Goto of string mcode (* goto *) * ident * string mcode (* ; *)
398 | Return of string mcode (* return *) * string mcode (* ; *)
399 | ReturnExpr of string mcode (* return *) * expression *
402 | MetaRuleElem of meta_name mcode * keep_binding * inherited
403 | MetaStmt of meta_name mcode * keep_binding * metaStmtInfo *
405 | MetaStmtList of meta_name mcode * keep_binding * inherited
407 | Exp of expression (* matches a subterm *)
408 | TopExp of expression (* for macros body, exp at top level,
410 | Ty of fullType (* only at SP top level, matches a subterm *)
411 | Include of string mcode (*#include*) * inc_file mcode (*file *)
412 | DefineHeader of string mcode (* #define *) * ident (* name *) *
413 define_parameters (*params*)
414 | Case of string mcode (* case *) * expression * string mcode (*:*)
415 | Default of string mcode (* default *) * string mcode (*:*)
416 | DisjRuleElem of rule_elem list
419 FStorage of storage mcode
421 | FInline of string mcode
422 | FAttr of string mcode
425 NotSequencible | SequencibleAfterDots of dots_whencode list | Sequencible
427 and rule_elem = base_rule_elem wrap
430 Seq of rule_elem (* { *) * statement dots *
431 statement dots * rule_elem (* } *)
432 | IfThen of rule_elem (* header *) * statement * end_info (* endif *)
433 | IfThenElse of rule_elem (* header *) * statement *
434 rule_elem (* else *) * statement * end_info (* endif *)
435 | While of rule_elem (* header *) * statement * end_info (*endwhile*)
436 | Do of rule_elem (* do *) * statement * rule_elem (* tail *)
437 | For of rule_elem (* header *) * statement * end_info (*endfor*)
438 | Iterator of rule_elem (* header *) * statement * end_info (*enditer*)
439 | Switch of rule_elem (* header *) * rule_elem (* { *) *
440 case_line list * rule_elem (* } *)
441 | Atomic of rule_elem
442 | Disj of statement dots list
443 | Nest of statement dots *
444 (statement dots,statement) whencode list * multi *
445 dots_whencode list * dots_whencode list
446 | FunDecl of rule_elem (* header *) * rule_elem (* { *) *
447 statement dots * statement dots * rule_elem (* } *)
448 | Define of rule_elem (* header *) * statement dots
449 | Dots of string mcode (* ... *) *
450 (statement dots,statement) whencode list *
451 dots_whencode list * dots_whencode list
452 | Circles of string mcode (* ooo *) *
453 (statement dots,statement) whencode list *
454 dots_whencode list * dots_whencode list
455 | Stars of string mcode (* *** *) *
456 (statement dots,statement) whencode list *
457 dots_whencode list * dots_whencode list
458 | OptStm of statement
459 | UniqueStm of statement
461 and ('a,'b) whencode =
464 | WhenModifier of when_modifier
465 | WhenNotTrue of rule_elem (* useful for fvs *)
466 | WhenNotFalse of rule_elem
469 (* The following removes the shortest path constraint. It can be used
470 with other when modifiers *)
472 (* The following removes the special consideration of error paths. It
473 can be used with other when modifiers *)
478 (* only used with asttoctl *)
480 WParen of rule_elem * meta_name (*pren_var*)
482 | Other_dots of statement dots
484 and statement = base_statement wrap
487 CaseLine of rule_elem (* case/default header *) * statement dots
488 | OptCase of case_line
490 and case_line = base_case_line wrap
493 Local of inc_elem list
494 | NonLocal of inc_elem list
502 | CODE of statement dots
503 | FILEINFO of string mcode (* old file *) * string mcode (* new file *)
504 | ERRORWORDS of expression list
506 and top_level = base_top_level wrap
509 CocciRulename of string option * dependency *
510 string list * string list * exists * bool
511 | ScriptRulename of string * dependency
514 CocciRule of string (* name *) *
515 (dependency * string list (* dropped isos *) * exists) * top_level list
517 | ScriptRule of string * dependency * (string * meta_name) list * string
520 Dep of string (* rule applies for the current binding *)
521 | AntiDep of string (* rule doesn't apply for the current binding *)
522 | EverDep of string (* rule applies for some binding *)
523 | NeverDep of string (* rule never applies for any binding *)
524 | AndDep of dependency * dependency
525 | OrDep of dependency * dependency
528 and rule_with_metavars = metavar list * rule
531 FullTypeTag of fullType
532 | BaseTypeTag of baseType
533 | StructUnionTag of structUnion
536 | ExpressionTag of expression
537 | ConstantTag of constant
538 | UnaryOpTag of unaryOp
539 | AssignOpTag of assignOp
541 | BinaryOpTag of binaryOp
542 | ArithOpTag of arithOp
543 | LogicalOpTag of logicalOp
544 | DeclarationTag of declaration
545 | InitTag of initialiser
546 | StorageTag of storage
547 | IncFileTag of inc_file
548 | Rule_elemTag of rule_elem
549 | StatementTag of statement
550 | CaseLineTag of case_line
551 | ConstVolTag of const_vol
552 | Token of string * info option
554 | ExprDotsTag of expression dots
555 | ParamDotsTag of parameterTypeDef dots
556 | StmtDotsTag of statement dots
557 | DeclDotsTag of declaration dots
559 | ParamTag of parameterTypeDef
560 | SgrepStartTag of string
561 | SgrepEndTag of string
563 (* --------------------------------------------------------------------- *)
565 and exists = Exists | Forall | ReverseForall | Undetermined
567 (* --------------------------------------------------------------------- *)
569 let mkToken x = Token (x,None)
571 (* --------------------------------------------------------------------- *)
573 let rewrap model x = {model with node = x}
574 let rewrap_mcode (_,a,b,c) x = (x,a,b,c)
575 let unwrap x = x.node
576 let unwrap_mcode (x,_,_,_) = x
577 let get_mcodekind (_,_,x,_) = x
578 let get_line x = x.node_line
579 let get_mcode_line (_,l,_,_) = l.line
580 let get_fvs x = x.free_vars
581 let set_fvs fvs x = {x with free_vars = fvs}
582 let get_mfvs x = x.minus_free_vars
583 let set_mfvs mfvs x = {x with minus_free_vars = mfvs}
584 let get_fresh x = x.fresh_vars
585 let get_inherited x = x.inherited
586 let get_saved x = x.saved_witness
587 let get_dots_bef_aft x = x.bef_aft
588 let set_dots_bef_aft d x = {x with bef_aft = d}
589 let get_pos x = x.pos_info
590 let set_pos x pos = {x with pos_info = pos}
591 let get_test_exp x = x.true_if_test_exp
592 let set_test_exp x = {x with true_if_test_exp = true}
593 let get_isos x = x.iso_info
594 let set_isos x isos = {x with iso_info = isos}
595 let get_pos_var (_,_,_,p) = p
596 let set_pos_var vr (a,b,c,_) = (a,b,c,vr)
597 let drop_pos (a,b,c,_) = (a,b,c,NoMetaPos)
599 let get_wcfvs (whencode : ('a wrap, 'b wrap) whencode list) =
603 WhenNot(a) -> get_fvs a
604 | WhenAlways(a) -> get_fvs a
605 | WhenModifier(_) -> []
606 | WhenNotTrue(e) -> get_fvs e
607 | WhenNotFalse(e) -> get_fvs e)
610 (* --------------------------------------------------------------------- *)
612 let get_meta_name = function
613 MetaIdDecl(ar,nm) -> nm
614 | MetaFreshIdDecl(ar,nm) -> nm
615 | MetaTypeDecl(ar,nm) -> nm
616 | MetaListlenDecl(nm) -> nm
617 | MetaParamDecl(ar,nm) -> nm
618 | MetaParamListDecl(ar,nm,nm1) -> nm
619 | MetaConstDecl(ar,nm,ty) -> nm
620 | MetaErrDecl(ar,nm) -> nm
621 | MetaExpDecl(ar,nm,ty) -> nm
622 | MetaIdExpDecl(ar,nm,ty) -> nm
623 | MetaLocalIdExpDecl(ar,nm,ty) -> nm
624 | MetaExpListDecl(ar,nm,nm1) -> nm
625 | MetaStmDecl(ar,nm) -> nm
626 | MetaStmListDecl(ar,nm) -> nm
627 | MetaFuncDecl(ar,nm) -> nm
628 | MetaLocalFuncDecl(ar,nm) -> nm
629 | MetaPosDecl(ar,nm) -> nm
630 | MetaDeclarerDecl(ar,nm) -> nm
631 | MetaIteratorDecl(ar,nm) -> nm
633 (* --------------------------------------------------------------------- *)
635 let no_info = { line = 0; column = 0; strbef = []; straft = [] }
641 minus_free_vars = [];
647 true_if_test_exp = false;
650 let make_meta_rule_elem s d (fvs,fresh,inh) =
652 (MetaRuleElem((("",s),no_info,d,NoMetaPos),Type_cocci.Unitary,false)))
653 with free_vars = fvs; fresh_vars = fresh; inherited = inh}
655 let make_meta_decl s d (fvs,fresh,inh) =
657 (MetaDecl((("",s),no_info,d,NoMetaPos),Type_cocci.Unitary,false))) with
658 free_vars = fvs; fresh_vars = fresh; inherited = inh}
660 let make_mcode x = (x,no_info,CONTEXT(NoPos,NOTHING),NoMetaPos)
662 (* --------------------------------------------------------------------- *)
664 let equal_pos x y = x = y
666 (* --------------------------------------------------------------------- *)