Coccinelle release 1.0.0c7.
[bpt/coccinelle.git] / parsing_cocci / ast0_cocci.mli
1 (*
2 * Copyright 2010, INRIA, University of Copenhagen
3 * Julia Lawall, Rene Rydhof Hansen, Gilles Muller, Nicolas Palix
4 * Copyright 2005-2009, Ecole des Mines de Nantes, University of Copenhagen
5 * Yoann Padioleau, Julia Lawall, Rene Rydhof Hansen, Henrik Stuart, Gilles Muller, Nicolas Palix
6 * This file is part of Coccinelle.
7 *
8 * Coccinelle is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation, according to version 2 of the License.
11 *
12 * Coccinelle is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with Coccinelle. If not, see <http://www.gnu.org/licenses/>.
19 *
20 * The authors reserve the right to distribute this or future versions of
21 * Coccinelle under other licenses.
22 *)
23
24
25 (* --------------------------------------------------------------------- *)
26 (* Modified code *)
27
28 type arity = OPT | UNIQUE | NONE
29
30 type token_info =
31 { tline_start : int; tline_end : int;
32 left_offset : int; right_offset : int }
33 val default_token_info : token_info
34
35 type mcodekind =
36 MINUS of (Ast_cocci.anything Ast_cocci.replacement * token_info) ref
37 | PLUS of Ast_cocci.count
38 | CONTEXT of (Ast_cocci.anything Ast_cocci.befaft *
39 token_info * token_info) ref
40 | MIXED of (Ast_cocci.anything Ast_cocci.befaft *
41 token_info * token_info) ref
42
43 type position_info = { line_start : int; line_end : int;
44 logical_start : int; logical_end : int;
45 column : int; offset : int; }
46
47 type info = { pos_info : position_info;
48 attachable_start : bool; attachable_end : bool;
49 mcode_start : mcodekind list; mcode_end : mcodekind list;
50 (* the following are only for + code *)
51 strings_before : (Ast_cocci.added_string * position_info) list;
52 strings_after : (Ast_cocci.added_string * position_info) list }
53
54 type 'a mcode =
55 'a * arity * info * mcodekind * meta_pos list ref (* pos, - only *) *
56 int (* adjacency_index *)
57
58 and 'a wrap =
59 { node : 'a;
60 info : info;
61 index : int ref;
62 mcodekind : mcodekind ref;
63 exp_ty : Type_cocci.typeC option ref; (* only for expressions *)
64 bef_aft : dots_bef_aft; (* only for statements *)
65 true_if_arg : bool; (* true if "arg_exp", only for exprs *)
66 true_if_test : bool; (* true if "test position", only for exprs *)
67 true_if_test_exp : bool;(* true if "test_exp from iso", only for exprs *)
68 (*nonempty if this represents the use of an iso*)
69 iso_info : (string*anything) list }
70
71 and dots_bef_aft =
72 NoDots | AddingBetweenDots of statement | DroppingBetweenDots of statement
73
74 (* for iso metavariables, true if they can only match nonmodified, unitary
75 metavariables
76 for SP metavariables, true if the metavariable is unitary (valid up to
77 isomorphism phase only) *)
78 and pure = Impure | Pure | Context | PureContext (* pure and only context *)
79
80 (* --------------------------------------------------------------------- *)
81 (* --------------------------------------------------------------------- *)
82 (* Dots *)
83
84 and 'a base_dots =
85 DOTS of 'a list
86 | CIRCLES of 'a list
87 | STARS of 'a list
88
89 and 'a dots = 'a base_dots wrap
90
91 (* --------------------------------------------------------------------- *)
92 (* Identifier *)
93
94 and base_ident =
95 Id of string mcode
96 | MetaId of Ast_cocci.meta_name mcode * Ast_cocci.idconstraint *
97 Ast_cocci.seed * pure
98 | MetaFunc of Ast_cocci.meta_name mcode * Ast_cocci.idconstraint * pure
99 | MetaLocalFunc of Ast_cocci.meta_name mcode * Ast_cocci.idconstraint * pure
100 | DisjId of string mcode * ident list *
101 string mcode list (* the |s *) * string mcode
102 | OptIdent of ident
103 | UniqueIdent of ident
104
105 and ident = base_ident wrap
106
107 (* --------------------------------------------------------------------- *)
108 (* Expression *)
109
110 and base_expression =
111 Ident of ident
112 | Constant of Ast_cocci.constant mcode
113 | FunCall of expression * string mcode (* ( *) *
114 expression dots * string mcode (* ) *)
115 | Assignment of expression * Ast_cocci.assignOp mcode * expression *
116 bool (* true if it can match an initialization *)
117 | CondExpr of expression * string mcode (* ? *) * expression option *
118 string mcode (* : *) * expression
119 | Postfix of expression * Ast_cocci.fixOp mcode
120 | Infix of expression * Ast_cocci.fixOp mcode
121 | Unary of expression * Ast_cocci.unaryOp mcode
122 | Binary of expression * Ast_cocci.binaryOp mcode * expression
123 | Nested of expression * Ast_cocci.binaryOp mcode * expression
124 | Paren of string mcode (* ( *) * expression *
125 string mcode (* ) *)
126 | ArrayAccess of expression * string mcode (* [ *) * expression *
127 string mcode (* ] *)
128 | RecordAccess of expression * string mcode (* . *) * ident
129 | RecordPtAccess of expression * string mcode (* -> *) * ident
130 | Cast of string mcode (* ( *) * typeC * string mcode (* ) *) *
131 expression
132 | SizeOfExpr of string mcode (* sizeof *) * expression
133 | SizeOfType of string mcode (* sizeof *) * string mcode (* ( *) *
134 typeC * string mcode (* ) *)
135 | TypeExp of typeC
136 | Constructor of string mcode (* ( *) * typeC * string mcode (* ) *) *
137 initialiser
138 | MetaErr of Ast_cocci.meta_name mcode * constraints * pure
139 | MetaExpr of Ast_cocci.meta_name mcode * constraints *
140 Type_cocci.typeC list option * Ast_cocci.form * pure
141 | MetaExprList of Ast_cocci.meta_name mcode (* only in arglists *) *
142 listlen * pure
143 | EComma of string mcode (* only in arglists *)
144 | DisjExpr of string mcode * expression list * string mcode list *
145 string mcode
146 | NestExpr of string mcode * expression dots * string mcode *
147 expression option * Ast_cocci.multi
148 | Edots of string mcode (* ... *) * expression option
149 | Ecircles of string mcode (* ooo *) * expression option
150 | Estars of string mcode (* *** *) * expression option
151 | OptExp of expression
152 | UniqueExp of expression
153
154 and expression = base_expression wrap
155
156 and constraints =
157 NoConstraint
158 | NotIdCstrt of Ast_cocci.reconstraint
159 | NotExpCstrt of expression list
160 | SubExpCstrt of Ast_cocci.meta_name list
161
162 and listlen =
163 MetaListLen of Ast_cocci.meta_name mcode
164 | CstListLen of int
165 | AnyListLen
166
167 (* --------------------------------------------------------------------- *)
168 (* Types *)
169
170 and base_typeC =
171 ConstVol of Ast_cocci.const_vol mcode * typeC
172 | BaseType of Ast_cocci.baseType * string mcode list
173 | Signed of Ast_cocci.sign mcode * typeC option
174 | Pointer of typeC * string mcode (* * *)
175 | FunctionPointer of typeC *
176 string mcode(* ( *)*string mcode(* * *)*string mcode(* ) *)*
177 string mcode (* ( *)*parameter_list*string mcode(* ) *)
178 | FunctionType of typeC option *
179 string mcode (* ( *) * parameter_list *
180 string mcode (* ) *)
181 | Array of typeC * string mcode (* [ *) *
182 expression option * string mcode (* ] *)
183 | EnumName of string mcode (*enum*) * ident option (* name *)
184 | EnumDef of typeC (* either StructUnionName or metavar *) *
185 string mcode (* { *) * expression dots * string mcode (* } *)
186 | StructUnionName of Ast_cocci.structUnion mcode * ident option (* name *)
187 | StructUnionDef of typeC (* either StructUnionName or metavar *) *
188 string mcode (* { *) * declaration dots * string mcode (* } *)
189 | TypeName of string mcode
190 | MetaType of Ast_cocci.meta_name mcode * pure
191 | DisjType of string mcode * typeC list * (* only after iso *)
192 string mcode list (* the |s *) * string mcode
193 | OptType of typeC
194 | UniqueType of typeC
195
196 and typeC = base_typeC wrap
197
198 (* --------------------------------------------------------------------- *)
199 (* Variable declaration *)
200 (* Even if the Cocci program specifies a list of declarations, they are
201 split out into multiple declarations of a single variable each. *)
202
203 and base_declaration =
204 MetaDecl of Ast_cocci.meta_name mcode * pure (* variables *)
205 | MetaField of Ast_cocci.meta_name mcode * pure (* structure fields *)
206 | MetaFieldList of Ast_cocci.meta_name mcode * listlen * pure
207 | Init of Ast_cocci.storage mcode option * typeC * ident *
208 string mcode (*=*) * initialiser * string mcode (*;*)
209 | UnInit of Ast_cocci.storage mcode option * typeC * ident *
210 string mcode (* ; *)
211 | TyDecl of typeC * string mcode (* ; *)
212 | MacroDecl of ident (* name *) * string mcode (* ( *) *
213 expression dots * string mcode (* ) *) * string mcode (* ; *)
214 | Typedef of string mcode (* typedef *) * typeC * typeC * string mcode (*;*)
215 | DisjDecl of string mcode * declaration list * string mcode list *
216 string mcode
217 | Ddots of string mcode (* ... *) * declaration option (* whencode *)
218 | OptDecl of declaration
219 | UniqueDecl of declaration
220
221 and declaration = base_declaration wrap
222
223 (* --------------------------------------------------------------------- *)
224 (* Initializers *)
225
226 and base_initialiser =
227 MetaInit of Ast_cocci.meta_name mcode * pure
228 | MetaInitList of Ast_cocci.meta_name mcode * listlen * pure
229 | InitExpr of expression
230 | InitList of string mcode (*{*) * initialiser_list * string mcode (*}*) *
231 bool (* true if ordered, false if unordered *)
232 | InitGccExt of
233 designator list (* name *) * string mcode (*=*) *
234 initialiser (* gccext: *)
235 | InitGccName of ident (* name *) * string mcode (*:*) *
236 initialiser
237 | IComma of string mcode
238 | Idots of string mcode (* ... *) * initialiser option (* whencode *)
239 | OptIni of initialiser
240 | UniqueIni of initialiser
241
242 and designator =
243 DesignatorField of string mcode (* . *) * ident
244 | DesignatorIndex of string mcode (* [ *) * expression * string mcode (* ] *)
245 | DesignatorRange of
246 string mcode (* [ *) * expression * string mcode (* ... *) *
247 expression * string mcode (* ] *)
248
249 and initialiser = base_initialiser wrap
250
251 and initialiser_list = initialiser dots
252
253 (* --------------------------------------------------------------------- *)
254 (* Parameter *)
255
256 and base_parameterTypeDef =
257 VoidParam of typeC
258 | Param of typeC * ident option
259 | MetaParam of Ast_cocci.meta_name mcode * pure
260 | MetaParamList of Ast_cocci.meta_name mcode * listlen * pure
261 | PComma of string mcode
262 | Pdots of string mcode (* ... *)
263 | Pcircles of string mcode (* ooo *)
264 | OptParam of parameterTypeDef
265 | UniqueParam of parameterTypeDef
266
267 and parameterTypeDef = base_parameterTypeDef wrap
268
269 and parameter_list = parameterTypeDef dots
270
271 (* --------------------------------------------------------------------- *)
272 (* #define Parameters *)
273
274 and base_define_param =
275 DParam of ident
276 | DPComma of string mcode
277 | DPdots of string mcode (* ... *)
278 | DPcircles of string mcode (* ooo *)
279 | OptDParam of define_param
280 | UniqueDParam of define_param
281
282 and define_param = base_define_param wrap
283
284 and base_define_parameters =
285 NoParams
286 | DParams of string mcode(*( *) * define_param dots * string mcode(* )*)
287
288 and define_parameters = base_define_parameters wrap
289
290 (* --------------------------------------------------------------------- *)
291 (* Statement*)
292
293 and base_statement =
294 Decl of (info * mcodekind) (* before the decl *) * declaration
295 | Seq of string mcode (* { *) * statement dots *
296 string mcode (* } *)
297 | ExprStatement of expression option * string mcode (*;*)
298 | IfThen of string mcode (* if *) * string mcode (* ( *) *
299 expression * string mcode (* ) *) *
300 statement * (info * mcodekind)
301 | IfThenElse of string mcode (* if *) * string mcode (* ( *) *
302 expression * string mcode (* ) *) *
303 statement * string mcode (* else *) * statement *
304 (info * mcodekind)
305 | While of string mcode (* while *) * string mcode (* ( *) *
306 expression * string mcode (* ) *) *
307 statement * (info * mcodekind) (* after info *)
308 | Do of string mcode (* do *) * statement *
309 string mcode (* while *) * string mcode (* ( *) *
310 expression * string mcode (* ) *) *
311 string mcode (* ; *)
312 | For of string mcode (* for *) * string mcode (* ( *) *
313 expression option * string mcode (*;*) *
314 expression option * string mcode (*;*) *
315 expression option * string mcode (* ) *) * statement *
316 (info * mcodekind) (* after info *)
317 | Iterator of ident (* name *) * string mcode (* ( *) *
318 expression dots * string mcode (* ) *) *
319 statement * (info * mcodekind) (* after info *)
320 | Switch of string mcode (* switch *) * string mcode (* ( *) *
321 expression * string mcode (* ) *) * string mcode (* { *) *
322 statement (*decl*) dots *
323 case_line dots * string mcode (* } *)
324 | Break of string mcode (* break *) * string mcode (* ; *)
325 | Continue of string mcode (* continue *) * string mcode (* ; *)
326 | Label of ident * string mcode (* : *)
327 | Goto of string mcode (* goto *) * ident * string mcode (* ; *)
328 | Return of string mcode (* return *) * string mcode (* ; *)
329 | ReturnExpr of string mcode (* return *) * expression *
330 string mcode (* ; *)
331 | MetaStmt of Ast_cocci.meta_name mcode * pure
332 | MetaStmtList of Ast_cocci.meta_name mcode (*only in statement lists*) *
333 pure
334 | Exp of expression (* only in dotted statement lists *)
335 | TopExp of expression (* for macros body *)
336 | Ty of typeC (* only at top level *)
337 | TopInit of initialiser (* only at top level *)
338 | Disj of string mcode * statement dots list * string mcode list *
339 string mcode
340 | Nest of string mcode * statement dots * string mcode *
341 (statement dots,statement) whencode list * Ast_cocci.multi
342 | Dots of string mcode (* ... *) *
343 (statement dots,statement) whencode list
344 | Circles of string mcode (* ooo *) *
345 (statement dots,statement) whencode list
346 | Stars of string mcode (* *** *) *
347 (statement dots,statement) whencode list
348 | FunDecl of (info * mcodekind) (* before the function decl *) *
349 fninfo list * ident (* name *) *
350 string mcode (* ( *) * parameter_list * string mcode (* ) *) *
351 string mcode (* { *) * statement dots *
352 string mcode (* } *)
353 | Include of string mcode (* #include *) * Ast_cocci.inc_file mcode(* file *)
354 | Undef of string mcode (* #define *) * ident (* name *)
355 | Define of string mcode (* #define *) * ident (* name *) *
356 define_parameters (*params*) * statement dots
357 | OptStm of statement
358 | UniqueStm of statement
359
360 and fninfo =
361 FStorage of Ast_cocci.storage mcode
362 | FType of typeC
363 | FInline of string mcode
364 | FAttr of string mcode
365
366 and ('a,'b) whencode =
367 WhenNot of 'a
368 | WhenAlways of 'b
369 | WhenModifier of Ast_cocci.when_modifier
370 | WhenNotTrue of expression
371 | WhenNotFalse of expression
372
373 and statement = base_statement wrap
374
375 and base_case_line =
376 Default of string mcode (* default *) * string mcode (*:*) * statement dots
377 | Case of string mcode (* case *) * expression * string mcode (*:*) *
378 statement dots
379 | DisjCase of string mcode * case_line list *
380 string mcode list (* the |s *) * string mcode
381 | OptCase of case_line
382
383 and case_line = base_case_line wrap
384
385 (* --------------------------------------------------------------------- *)
386 (* Positions *)
387
388 and meta_pos =
389 MetaPos of Ast_cocci.meta_name mcode * Ast_cocci.meta_name list *
390 Ast_cocci.meta_collect
391
392 (* --------------------------------------------------------------------- *)
393 (* Top-level code *)
394
395 and base_top_level =
396 NONDECL of statement (* cannot match all of a top-level declaration *)
397 | TOPCODE of statement dots
398 | CODE of statement dots
399 | FILEINFO of string mcode (* old file *) * string mcode (* new file *)
400 | ERRORWORDS of expression list
401 | OTHER of statement (* temporary, disappears after top_level.ml *)
402
403 and top_level = base_top_level wrap
404 and rule = top_level list
405
406 and parsed_rule =
407 CocciRule of
408 (rule * Ast_cocci.metavar list *
409 (string list * string list * Ast_cocci.dependency * string *
410 Ast_cocci.exists)) *
411 (rule * Ast_cocci.metavar list) * Ast_cocci.ruletype
412 | ScriptRule of string (* name *) *
413 string * Ast_cocci.dependency *
414 (Ast_cocci.script_meta_name *
415 Ast_cocci.meta_name * Ast_cocci.metavar) list (*inherited vars*) *
416 Ast_cocci.meta_name list (*script vars*) *
417 string
418 | InitialScriptRule of string (* name *) *
419 string (*language*) * Ast_cocci.dependency * string (*code*)
420 | FinalScriptRule of string (* name *) *
421 string (*language*) * Ast_cocci.dependency * string (*code*)
422
423 (* --------------------------------------------------------------------- *)
424
425 and anything =
426 DotsExprTag of expression dots
427 | DotsInitTag of initialiser dots
428 | DotsParamTag of parameterTypeDef dots
429 | DotsStmtTag of statement dots
430 | DotsDeclTag of declaration dots
431 | DotsCaseTag of case_line dots
432 | IdentTag of ident
433 | ExprTag of expression
434 | ArgExprTag of expression (* for isos *)
435 | TestExprTag of expression (* for isos *)
436 | TypeCTag of typeC
437 | ParamTag of parameterTypeDef
438 | InitTag of initialiser
439 | DeclTag of declaration
440 | StmtTag of statement
441 | CaseLineTag of case_line
442 | TopTag of top_level
443 | IsoWhenTag of Ast_cocci.when_modifier (*only for when code, in iso phase*)
444 | IsoWhenTTag of expression(*only for when code, in iso phase*)
445 | IsoWhenFTag of expression(*only for when code, in iso phase*)
446 | MetaPosTag of meta_pos (* only in iso phase *)
447
448 val dotsExpr : expression dots -> anything
449 val dotsInit : initialiser dots -> anything
450 val dotsParam : parameterTypeDef dots -> anything
451 val dotsStmt : statement dots -> anything
452 val dotsDecl : declaration dots -> anything
453 val dotsCase : case_line dots -> anything
454 val ident : ident -> anything
455 val expr : expression -> anything
456 val typeC : typeC -> anything
457 val param : parameterTypeDef -> anything
458 val ini : initialiser -> anything
459 val decl : declaration -> anything
460 val stmt : statement -> anything
461 val case_line : case_line -> anything
462 val top : top_level -> anything
463
464 (* --------------------------------------------------------------------- *)
465
466 val undots : 'a dots -> 'a list
467
468 (* --------------------------------------------------------------------- *)
469 (* Avoid cluttering the parser. Calculated in compute_lines.ml. *)
470
471 val default_info : unit -> info
472 val default_befaft : unit -> mcodekind
473 val context_befaft : unit -> mcodekind
474 val wrap : 'a -> 'a wrap
475 val context_wrap : 'a -> 'a wrap
476 val unwrap : 'a wrap -> 'a
477 val unwrap_mcode : 'a mcode -> 'a
478 val rewrap : 'a wrap -> 'b -> 'b wrap
479 val rewrap_mcode : 'a mcode -> 'b -> 'b mcode
480 val copywrap : 'a wrap -> 'b -> 'b wrap
481 val get_pos : 'a mcode -> meta_pos list
482 val get_pos_ref : 'a mcode -> meta_pos list ref
483 val set_pos : meta_pos list -> 'a mcode -> 'a mcode
484 val get_info : 'a wrap -> info
485 val set_info : 'a wrap -> info -> 'a wrap
486 val get_index : 'a wrap -> int
487 val set_index : 'a wrap -> int -> unit
488 val get_line : 'a wrap -> int
489 val get_line_end : 'a wrap -> int
490 val get_mcodekind : 'a wrap -> mcodekind
491 val get_mcode_mcodekind : 'a mcode -> mcodekind
492 val get_mcodekind_ref : 'a wrap -> mcodekind ref
493 val set_mcodekind : 'a wrap -> mcodekind -> unit
494 val set_type : 'a wrap -> Type_cocci.typeC option -> unit
495 val get_type : 'a wrap -> Type_cocci.typeC option
496 val set_dots_bef_aft : statement -> dots_bef_aft -> statement
497 val get_dots_bef_aft : 'a wrap -> dots_bef_aft
498 val set_arg_exp : expression -> expression
499 val get_arg_exp : expression -> bool
500 val set_test_pos : expression -> expression
501 val get_test_pos : 'a wrap -> bool
502 val set_test_exp : expression -> expression
503 val get_test_exp : 'a wrap -> bool
504 val set_iso : 'a wrap -> (string*anything) list -> 'a wrap
505 val get_iso : 'a wrap -> (string*anything) list
506 val fresh_index : unit -> int
507 val set_mcode_data : 'a -> 'a mcode -> 'a mcode
508 val make_mcode : 'a -> 'a mcode
509 val make_mcode_info : 'a -> info -> 'a mcode
510 val make_minus_mcode : 'a -> 'a mcode
511
512 val ast0_type_to_type : typeC -> Type_cocci.typeC
513 val reverse_type : Type_cocci.typeC -> base_typeC
514 exception TyConv
515
516 val lub_pure : pure -> pure -> pure
517
518 (* --------------------------------------------------------------------- *)
519
520 val rule_name : string ref (* for the convenience of the parser *)