2 * Copyright 2005-2009, Ecole des Mines de Nantes, University of Copenhagen
3 * Yoann Padioleau, Julia Lawall, Rene Rydhof Hansen, Henrik Stuart, Gilles Muller, Nicolas Palix
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 arity
= OPT
| UNIQUE
| NONE
29 { tline_start
: int; tline_end
: int;
30 left_offset
: int; right_offset
: int }
31 val default_token_info
: token_info
34 MINUS
of (Ast_cocci.anything list list
* token_info
) ref
35 | PLUS
of Ast_cocci.count
36 | CONTEXT
of (Ast_cocci.anything
Ast_cocci.befaft
*
37 token_info
* token_info
) ref
38 | MIXED
of (Ast_cocci.anything
Ast_cocci.befaft
*
39 token_info
* token_info
) ref
41 type position_info
= { line_start
: int; line_end
: int;
42 logical_start
: int; logical_end
: int;
43 column
: int; offset
: int; }
45 type info
= { pos_info
: position_info
;
46 attachable_start
: bool; attachable_end
: bool;
47 mcode_start
: mcodekind list
; mcode_end
: mcodekind list
;
48 (* the following are only for + code *)
49 strings_before
: (string * position_info
) list
;
50 strings_after
: (string * position_info
) list
}
53 'a
* arity
* info
* mcodekind
* meta_pos
ref (* pos, - only *) *
54 int (* adjacency_index *)
60 mcodekind
: mcodekind
ref;
61 exp_ty
: Type_cocci.typeC
option ref; (* only for expressions *)
62 bef_aft
: dots_bef_aft
; (* only for statements *)
63 true_if_arg
: bool; (* true if "arg_exp", only for exprs *)
64 true_if_test
: bool; (* true if "test position", only for exprs *)
65 true_if_test_exp
: bool;(* true if "test_exp from iso", only for exprs *)
66 (*nonempty if this represents the use of an iso*)
67 iso_info
: (string*anything
) list
}
70 NoDots
| AddingBetweenDots
of statement
| DroppingBetweenDots
of statement
72 (* for iso metavariables, true if they can only match nonmodified, unitary
74 for SP metavariables, true if the metavariable is unitary (valid up to
75 isomorphism phase only) *)
76 and pure
= Impure
| Pure
| Context
| PureContext
(* pure and only context *)
78 (* --------------------------------------------------------------------- *)
79 (* --------------------------------------------------------------------- *)
87 and 'a dots
= 'a base_dots wrap
89 (* --------------------------------------------------------------------- *)
94 | MetaId
of Ast_cocci.meta_name mcode
* Ast_cocci.idconstraint
* pure
95 | MetaFunc
of Ast_cocci.meta_name mcode
* Ast_cocci.idconstraint
* pure
96 | MetaLocalFunc
of Ast_cocci.meta_name mcode
* Ast_cocci.idconstraint
* pure
98 | UniqueIdent
of ident
100 and ident
= base_ident wrap
102 (* --------------------------------------------------------------------- *)
105 and base_expression
=
107 | Constant
of Ast_cocci.constant mcode
108 | FunCall
of expression
* string mcode
(* ( *) *
109 expression dots
* string mcode
(* ) *)
110 | Assignment
of expression
* Ast_cocci.assignOp mcode
* expression
*
111 bool (* true if it can match an initialization *)
112 | CondExpr
of expression
* string mcode
(* ? *) * expression
option *
113 string mcode
(* : *) * expression
114 | Postfix
of expression
* Ast_cocci.fixOp mcode
115 | Infix
of expression
* Ast_cocci.fixOp mcode
116 | Unary
of expression
* Ast_cocci.unaryOp mcode
117 | Binary
of expression
* Ast_cocci.binaryOp mcode
* expression
118 | Nested
of expression
* Ast_cocci.binaryOp mcode
* expression
119 | Paren
of string mcode
(* ( *) * expression
*
121 | ArrayAccess
of expression
* string mcode
(* [ *) * expression
*
123 | RecordAccess
of expression
* string mcode
(* . *) * ident
124 | RecordPtAccess
of expression
* string mcode
(* -> *) * ident
125 | Cast
of string mcode
(* ( *) * typeC
* string mcode
(* ) *) *
127 | SizeOfExpr
of string mcode
(* sizeof *) * expression
128 | SizeOfType
of string mcode
(* sizeof *) * string mcode
(* ( *) *
129 typeC
* string mcode
(* ) *)
131 | MetaErr
of Ast_cocci.meta_name mcode
* constraints
* pure
132 | MetaExpr
of Ast_cocci.meta_name mcode
* constraints
*
133 Type_cocci.typeC list
option * Ast_cocci.form
* pure
134 | MetaExprList
of Ast_cocci.meta_name mcode
(* only in arglists *) *
136 | EComma
of string mcode
(* only in arglists *)
137 | DisjExpr
of string mcode
* expression list
* string mcode list
*
139 | NestExpr
of string mcode
* expression dots
* string mcode
*
140 expression
option * Ast_cocci.multi
141 | Edots
of string mcode
(* ... *) * expression
option
142 | Ecircles
of string mcode
(* ooo *) * expression
option
143 | Estars
of string mcode
(* *** *) * expression
option
144 | OptExp
of expression
145 | UniqueExp
of expression
147 and expression
= base_expression wrap
151 | NotIdCstrt
of Ast_cocci.idconstraint
152 | NotExpCstrt
of expression list
154 and listlen
= Ast_cocci.meta_name mcode
option
156 (* --------------------------------------------------------------------- *)
160 ConstVol
of Ast_cocci.const_vol mcode
* typeC
161 | BaseType
of Ast_cocci.baseType
* string mcode list
162 | Signed
of Ast_cocci.sign mcode
* typeC
option
163 | Pointer
of typeC
* string mcode
(* * *)
164 | FunctionPointer
of typeC
*
165 string mcode
(* ( *)*string mcode
(* * *)*string mcode
(* ) *)*
166 string mcode
(* ( *)*parameter_list
*string mcode
(* ) *)
167 | FunctionType
of typeC
option *
168 string mcode
(* ( *) * parameter_list
*
170 | Array
of typeC
* string mcode
(* [ *) *
171 expression
option * string mcode
(* ] *)
172 | EnumName
of string mcode
(*enum*) * ident
(* name *)
173 | StructUnionName
of Ast_cocci.structUnion mcode
* ident
option (* name *)
174 | StructUnionDef
of typeC
(* either StructUnionName or metavar *) *
175 string mcode
(* { *) * declaration dots
* string mcode
(* } *)
176 | TypeName
of string mcode
177 | MetaType
of Ast_cocci.meta_name mcode
* pure
178 | DisjType
of string mcode
* typeC list
* (* only after iso *)
179 string mcode list
(* the |s *) * string mcode
181 | UniqueType
of typeC
183 and typeC
= base_typeC wrap
185 (* --------------------------------------------------------------------- *)
186 (* Variable declaration *)
187 (* Even if the Cocci program specifies a list of declarations, they are
188 split out into multiple declarations of a single variable each. *)
190 and base_declaration
=
191 Init
of Ast_cocci.storage mcode
option * typeC
* ident
*
192 string mcode
(*=*) * initialiser
* string mcode
(*;*)
193 | UnInit
of Ast_cocci.storage mcode
option * typeC
* ident
*
195 | TyDecl
of typeC
* string mcode
(* ; *)
196 | MacroDecl
of ident
(* name *) * string mcode
(* ( *) *
197 expression dots
* string mcode
(* ) *) * string mcode
(* ; *)
198 | Typedef
of string mcode
(* typedef *) * typeC
* typeC
* string mcode
(*;*)
199 | DisjDecl
of string mcode
* declaration list
* string mcode list
*
201 | Ddots
of string mcode
(* ... *) * declaration
option (* whencode *)
202 | OptDecl
of declaration
203 | UniqueDecl
of declaration
205 and declaration
= base_declaration wrap
207 (* --------------------------------------------------------------------- *)
210 and base_initialiser
=
211 MetaInit
of Ast_cocci.meta_name mcode
* pure
212 | InitExpr
of expression
213 | InitList
of string mcode
(*{*) * initialiser_list
* string mcode
(*}*)
215 designator list
(* name *) * string mcode
(*=*) *
216 initialiser
(* gccext: *)
217 | InitGccName
of ident
(* name *) * string mcode
(*:*) *
219 | IComma
of string mcode
220 | Idots
of string mcode
(* ... *) * initialiser
option (* whencode *)
221 | OptIni
of initialiser
222 | UniqueIni
of initialiser
225 DesignatorField
of string mcode
(* . *) * ident
226 | DesignatorIndex
of string mcode
(* [ *) * expression
* string mcode
(* ] *)
228 string mcode
(* [ *) * expression
* string mcode
(* ... *) *
229 expression
* string mcode
(* ] *)
231 and initialiser
= base_initialiser wrap
233 and initialiser_list
= initialiser dots
235 (* --------------------------------------------------------------------- *)
238 and base_parameterTypeDef
=
240 | Param
of typeC
* ident
option
241 | MetaParam
of Ast_cocci.meta_name mcode
* pure
242 | MetaParamList
of Ast_cocci.meta_name mcode
* listlen
* pure
243 | PComma
of string mcode
244 | Pdots
of string mcode
(* ... *)
245 | Pcircles
of string mcode
(* ooo *)
246 | OptParam
of parameterTypeDef
247 | UniqueParam
of parameterTypeDef
249 and parameterTypeDef
= base_parameterTypeDef wrap
251 and parameter_list
= parameterTypeDef dots
253 (* --------------------------------------------------------------------- *)
254 (* #define Parameters *)
256 and base_define_param
=
258 | DPComma
of string mcode
259 | DPdots
of string mcode
(* ... *)
260 | DPcircles
of string mcode
(* ooo *)
261 | OptDParam
of define_param
262 | UniqueDParam
of define_param
264 and define_param
= base_define_param wrap
266 and base_define_parameters
=
268 | DParams
of string mcode
(*( *) * define_param dots
* string mcode
(* )*)
270 and define_parameters
= base_define_parameters wrap
272 (* --------------------------------------------------------------------- *)
276 Decl
of (info
* mcodekind
) (* before the decl *) * declaration
277 | Seq
of string mcode
(* { *) * statement dots
*
279 | ExprStatement
of expression
* string mcode
(*;*)
280 | IfThen
of string mcode
(* if *) * string mcode
(* ( *) *
281 expression
* string mcode
(* ) *) *
282 statement
* (info
* mcodekind
)
283 | IfThenElse
of string mcode
(* if *) * string mcode
(* ( *) *
284 expression
* string mcode
(* ) *) *
285 statement
* string mcode
(* else *) * statement
*
287 | While
of string mcode
(* while *) * string mcode
(* ( *) *
288 expression
* string mcode
(* ) *) *
289 statement
* (info
* mcodekind
) (* after info *)
290 | Do
of string mcode
(* do *) * statement
*
291 string mcode
(* while *) * string mcode
(* ( *) *
292 expression
* string mcode
(* ) *) *
294 | For
of string mcode
(* for *) * string mcode
(* ( *) *
295 expression
option * string mcode
(*;*) *
296 expression
option * string mcode
(*;*) *
297 expression
option * string mcode
(* ) *) * statement
*
298 (info
* mcodekind
) (* after info *)
299 | Iterator
of ident
(* name *) * string mcode
(* ( *) *
300 expression dots
* string mcode
(* ) *) *
301 statement
* (info
* mcodekind
) (* after info *)
302 | Switch
of string mcode
(* switch *) * string mcode
(* ( *) *
303 expression
* string mcode
(* ) *) * string mcode
(* { *) *
304 statement
(*decl*) dots
*
305 case_line dots
* string mcode
(* } *)
306 | Break
of string mcode
(* break *) * string mcode
(* ; *)
307 | Continue
of string mcode
(* continue *) * string mcode
(* ; *)
308 | Label
of ident
* string mcode
(* : *)
309 | Goto
of string mcode
(* goto *) * ident
* string mcode
(* ; *)
310 | Return
of string mcode
(* return *) * string mcode
(* ; *)
311 | ReturnExpr
of string mcode
(* return *) * expression
*
313 | MetaStmt
of Ast_cocci.meta_name mcode
* pure
314 | MetaStmtList
of Ast_cocci.meta_name mcode
(*only in statement lists*) *
316 | Exp
of expression
(* only in dotted statement lists *)
317 | TopExp
of expression
(* for macros body *)
318 | Ty
of typeC
(* only at top level *)
319 | TopInit
of initialiser
(* only at top level *)
320 | Disj
of string mcode
* statement dots list
* string mcode list
*
322 | Nest
of string mcode
* statement dots
* string mcode
*
323 (statement dots
,statement
) whencode list
* Ast_cocci.multi
324 | Dots
of string mcode
(* ... *) *
325 (statement dots
,statement
) whencode list
326 | Circles
of string mcode
(* ooo *) *
327 (statement dots
,statement
) whencode list
328 | Stars
of string mcode
(* *** *) *
329 (statement dots
,statement
) whencode list
330 | FunDecl
of (info
* mcodekind
) (* before the function decl *) *
331 fninfo list
* ident
(* name *) *
332 string mcode
(* ( *) * parameter_list
* string mcode
(* ) *) *
333 string mcode
(* { *) * statement dots
*
335 | Include
of string mcode
(* #include *) * Ast_cocci.inc_file mcode
(* file *)
336 | Define
of string mcode
(* #define *) * ident
(* name *) *
337 define_parameters
(*params*) * statement dots
338 | OptStm
of statement
339 | UniqueStm
of statement
342 FStorage
of Ast_cocci.storage mcode
344 | FInline
of string mcode
345 | FAttr
of string mcode
347 and ('a
,'b
) whencode
=
350 | WhenModifier
of Ast_cocci.when_modifier
351 | WhenNotTrue
of expression
352 | WhenNotFalse
of expression
354 and statement
= base_statement wrap
357 Default
of string mcode
(* default *) * string mcode
(*:*) * statement dots
358 | Case
of string mcode
(* case *) * expression
* string mcode
(*:*) *
360 | DisjCase
of string mcode
* case_line list
*
361 string mcode list
(* the |s *) * string mcode
362 | OptCase
of case_line
364 and case_line
= base_case_line wrap
366 (* --------------------------------------------------------------------- *)
370 MetaPos
of Ast_cocci.meta_name mcode
* Ast_cocci.meta_name list
*
371 Ast_cocci.meta_collect
374 (* --------------------------------------------------------------------- *)
379 | CODE
of statement dots
380 | FILEINFO
of string mcode
(* old file *) * string mcode
(* new file *)
381 | ERRORWORDS
of expression list
382 | OTHER
of statement
(* temporary, disappears after top_level.ml *)
384 and top_level
= base_top_level wrap
385 and rule
= top_level list
389 (rule
* Ast_cocci.metavar list
*
390 (string list
* string list
* Ast_cocci.dependency
* string *
392 (rule
* Ast_cocci.metavar list
) * Ast_cocci.ruletype
394 string * Ast_cocci.dependency
* (string * Ast_cocci.meta_name
) list
*
396 | InitialScriptRule
of string (*language*) * string (*code*)
397 | FinalScriptRule
of string (*language*) * string (*code*)
399 (* --------------------------------------------------------------------- *)
402 DotsExprTag
of expression dots
403 | DotsInitTag
of initialiser dots
404 | DotsParamTag
of parameterTypeDef dots
405 | DotsStmtTag
of statement dots
406 | DotsDeclTag
of declaration dots
407 | DotsCaseTag
of case_line dots
409 | ExprTag
of expression
410 | ArgExprTag
of expression
(* for isos *)
411 | TestExprTag
of expression
(* for isos *)
413 | ParamTag
of parameterTypeDef
414 | InitTag
of initialiser
415 | DeclTag
of declaration
416 | StmtTag
of statement
417 | CaseLineTag
of case_line
418 | TopTag
of top_level
419 | IsoWhenTag
of Ast_cocci.when_modifier
(*only for when code, in iso phase*)
420 | IsoWhenTTag
of expression
(*only for when code, in iso phase*)
421 | IsoWhenFTag
of expression
(*only for when code, in iso phase*)
422 | MetaPosTag
of meta_pos
(* only in iso phase *)
424 val dotsExpr
: expression dots
-> anything
425 val dotsInit
: initialiser dots
-> anything
426 val dotsParam
: parameterTypeDef dots
-> anything
427 val dotsStmt
: statement dots
-> anything
428 val dotsDecl
: declaration dots
-> anything
429 val dotsCase
: case_line dots
-> anything
430 val ident
: ident
-> anything
431 val expr
: expression
-> anything
432 val typeC
: typeC
-> anything
433 val param
: parameterTypeDef
-> anything
434 val ini
: initialiser
-> anything
435 val decl
: declaration
-> anything
436 val stmt
: statement
-> anything
437 val case_line
: case_line
-> anything
438 val top
: top_level
-> anything
440 (* --------------------------------------------------------------------- *)
442 val undots
: 'a dots
-> 'a list
444 (* --------------------------------------------------------------------- *)
445 (* Avoid cluttering the parser. Calculated in compute_lines.ml. *)
447 val default_info
: unit -> info
448 val default_befaft
: unit -> mcodekind
449 val context_befaft
: unit -> mcodekind
450 val wrap
: 'a
-> 'a wrap
451 val context_wrap
: 'a
-> 'a wrap
452 val unwrap
: 'a wrap
-> 'a
453 val unwrap_mcode
: 'a mcode
-> 'a
454 val rewrap
: 'a wrap
-> 'b
-> 'b wrap
455 val rewrap_mcode
: 'a mcode
-> 'b
-> 'b mcode
456 val copywrap
: 'a wrap
-> 'b
-> 'b wrap
457 val get_pos
: 'a mcode
-> meta_pos
458 val get_pos_ref
: 'a mcode
-> meta_pos
ref
459 val set_pos
: meta_pos
-> 'a mcode
-> 'a mcode
460 val get_info
: 'a wrap
-> info
461 val set_info
: 'a wrap
-> info
-> 'a wrap
462 val get_index
: 'a wrap
-> int
463 val set_index
: 'a wrap
-> int -> unit
464 val get_line
: 'a wrap
-> int
465 val get_line_end
: 'a wrap
-> int
466 val get_mcodekind
: 'a wrap
-> mcodekind
467 val get_mcode_mcodekind
: 'a mcode
-> mcodekind
468 val get_mcodekind_ref
: 'a wrap
-> mcodekind
ref
469 val set_mcodekind
: 'a wrap
-> mcodekind
-> unit
470 val set_type
: 'a wrap
-> Type_cocci.typeC
option -> unit
471 val get_type
: 'a wrap
-> Type_cocci.typeC
option
472 val set_dots_bef_aft
: statement
-> dots_bef_aft
-> statement
473 val get_dots_bef_aft
: 'a wrap
-> dots_bef_aft
474 val set_arg_exp
: expression
-> expression
475 val get_arg_exp
: expression
-> bool
476 val set_test_pos
: expression
-> expression
477 val get_test_pos
: 'a wrap
-> bool
478 val set_test_exp
: expression
-> expression
479 val get_test_exp
: 'a wrap
-> bool
480 val set_iso
: 'a wrap
-> (string*anything
) list
-> 'a wrap
481 val get_iso
: 'a wrap
-> (string*anything
) list
482 val fresh_index
: unit -> int
483 val set_mcode_data
: 'a
-> 'a mcode
-> 'a mcode
484 val make_mcode
: 'a
-> 'a mcode
485 val make_mcode_info
: 'a
-> info
-> 'a mcode
487 val ast0_type_to_type
: typeC
-> Type_cocci.typeC
488 val reverse_type
: Type_cocci.typeC
-> base_typeC
491 val lub_pure
: pure
-> pure
-> pure
493 (* --------------------------------------------------------------------- *)
495 val rule_name
: string ref (* for the convenience of the parser *)