5d54968c8c0fc7cd54a6aaa1ad0388dbb67c2563
[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 list list * 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 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 * pure
97 | MetaFunc of Ast_cocci.meta_name mcode * Ast_cocci.idconstraint * pure
98 | MetaLocalFunc of Ast_cocci.meta_name mcode * Ast_cocci.idconstraint * pure
99 | OptIdent of ident
100 | UniqueIdent of ident
101
102 and ident = base_ident wrap
103
104 (* --------------------------------------------------------------------- *)
105 (* Expression *)
106
107 and base_expression =
108 Ident of ident
109 | Constant of Ast_cocci.constant mcode
110 | FunCall of expression * string mcode (* ( *) *
111 expression dots * string mcode (* ) *)
112 | Assignment of expression * Ast_cocci.assignOp mcode * expression *
113 bool (* true if it can match an initialization *)
114 | CondExpr of expression * string mcode (* ? *) * expression option *
115 string mcode (* : *) * expression
116 | Postfix of expression * Ast_cocci.fixOp mcode
117 | Infix of expression * Ast_cocci.fixOp mcode
118 | Unary of expression * Ast_cocci.unaryOp mcode
119 | Binary of expression * Ast_cocci.binaryOp mcode * expression
120 | Nested of expression * Ast_cocci.binaryOp mcode * expression
121 | Paren of string mcode (* ( *) * expression *
122 string mcode (* ) *)
123 | ArrayAccess of expression * string mcode (* [ *) * expression *
124 string mcode (* ] *)
125 | RecordAccess of expression * string mcode (* . *) * ident
126 | RecordPtAccess of expression * string mcode (* -> *) * ident
127 | Cast of string mcode (* ( *) * typeC * string mcode (* ) *) *
128 expression
129 | SizeOfExpr of string mcode (* sizeof *) * expression
130 | SizeOfType of string mcode (* sizeof *) * string mcode (* ( *) *
131 typeC * string mcode (* ) *)
132 | TypeExp of typeC
133 | MetaErr of Ast_cocci.meta_name mcode * constraints * pure
134 | MetaExpr of Ast_cocci.meta_name mcode * constraints *
135 Type_cocci.typeC list option * Ast_cocci.form * pure
136 | MetaExprList of Ast_cocci.meta_name mcode (* only in arglists *) *
137 listlen * pure
138 | EComma of string mcode (* only in arglists *)
139 | DisjExpr of string mcode * expression list * string mcode list *
140 string mcode
141 | NestExpr of string mcode * expression dots * string mcode *
142 expression option * Ast_cocci.multi
143 | Edots of string mcode (* ... *) * expression option
144 | Ecircles of string mcode (* ooo *) * expression option
145 | Estars of string mcode (* *** *) * expression option
146 | OptExp of expression
147 | UniqueExp of expression
148
149 and expression = base_expression wrap
150
151 and constraints =
152 NoConstraint
153 | NotIdCstrt of Ast_cocci.reconstraint
154 | NotExpCstrt of expression list
155 | SubExpCstrt of Ast_cocci.meta_name list
156
157 and listlen =
158 MetaListLen of Ast_cocci.meta_name mcode
159 | CstListLen of int
160 | AnyListLen
161
162 (* --------------------------------------------------------------------- *)
163 (* Types *)
164
165 and base_typeC =
166 ConstVol of Ast_cocci.const_vol mcode * typeC
167 | BaseType of Ast_cocci.baseType * string mcode list
168 | Signed of Ast_cocci.sign mcode * typeC option
169 | Pointer of typeC * string mcode (* * *)
170 | FunctionPointer of typeC *
171 string mcode(* ( *)*string mcode(* * *)*string mcode(* ) *)*
172 string mcode (* ( *)*parameter_list*string mcode(* ) *)
173 | FunctionType of typeC option *
174 string mcode (* ( *) * parameter_list *
175 string mcode (* ) *)
176 | Array of typeC * string mcode (* [ *) *
177 expression option * string mcode (* ] *)
178 | EnumName of string mcode (*enum*) * ident option (* name *)
179 | EnumDef of typeC (* either StructUnionName or metavar *) *
180 string mcode (* { *) * expression dots * string mcode (* } *)
181 | StructUnionName of Ast_cocci.structUnion mcode * ident option (* name *)
182 | StructUnionDef of typeC (* either StructUnionName or metavar *) *
183 string mcode (* { *) * declaration dots * string mcode (* } *)
184 | TypeName of string mcode
185 | MetaType of Ast_cocci.meta_name mcode * pure
186 | DisjType of string mcode * typeC list * (* only after iso *)
187 string mcode list (* the |s *) * string mcode
188 | OptType of typeC
189 | UniqueType of typeC
190
191 and typeC = base_typeC wrap
192
193 (* --------------------------------------------------------------------- *)
194 (* Variable declaration *)
195 (* Even if the Cocci program specifies a list of declarations, they are
196 split out into multiple declarations of a single variable each. *)
197
198 and base_declaration =
199 MetaDecl of Ast_cocci.meta_name mcode * pure
200 | MetaField of Ast_cocci.meta_name mcode * pure (* structure fields *)
201 | Init of Ast_cocci.storage mcode option * typeC * ident *
202 string mcode (*=*) * initialiser * string mcode (*;*)
203 | UnInit of Ast_cocci.storage mcode option * typeC * ident *
204 string mcode (* ; *)
205 | TyDecl of typeC * string mcode (* ; *)
206 | MacroDecl of ident (* name *) * string mcode (* ( *) *
207 expression dots * string mcode (* ) *) * string mcode (* ; *)
208 | Typedef of string mcode (* typedef *) * typeC * typeC * string mcode (*;*)
209 | DisjDecl of string mcode * declaration list * string mcode list *
210 string mcode
211 | Ddots of string mcode (* ... *) * declaration option (* whencode *)
212 | OptDecl of declaration
213 | UniqueDecl of declaration
214
215 and declaration = base_declaration wrap
216
217 (* --------------------------------------------------------------------- *)
218 (* Initializers *)
219
220 and base_initialiser =
221 MetaInit of Ast_cocci.meta_name mcode * pure
222 | InitExpr of expression
223 | InitList of string mcode (*{*) * initialiser_list * string mcode (*}*) *
224 bool (* true if ordered, false if unordered *)
225 | InitGccExt of
226 designator list (* name *) * string mcode (*=*) *
227 initialiser (* gccext: *)
228 | InitGccName of ident (* name *) * string mcode (*:*) *
229 initialiser
230 | IComma of string mcode
231 | Idots of string mcode (* ... *) * initialiser option (* whencode *)
232 | OptIni of initialiser
233 | UniqueIni of initialiser
234
235 and designator =
236 DesignatorField of string mcode (* . *) * ident
237 | DesignatorIndex of string mcode (* [ *) * expression * string mcode (* ] *)
238 | DesignatorRange of
239 string mcode (* [ *) * expression * string mcode (* ... *) *
240 expression * string mcode (* ] *)
241
242 and initialiser = base_initialiser wrap
243
244 and initialiser_list = initialiser dots
245
246 (* --------------------------------------------------------------------- *)
247 (* Parameter *)
248
249 and base_parameterTypeDef =
250 VoidParam of typeC
251 | Param of typeC * ident option
252 | MetaParam of Ast_cocci.meta_name mcode * pure
253 | MetaParamList of Ast_cocci.meta_name mcode * listlen * pure
254 | PComma of string mcode
255 | Pdots of string mcode (* ... *)
256 | Pcircles of string mcode (* ooo *)
257 | OptParam of parameterTypeDef
258 | UniqueParam of parameterTypeDef
259
260 and parameterTypeDef = base_parameterTypeDef wrap
261
262 and parameter_list = parameterTypeDef dots
263
264 (* --------------------------------------------------------------------- *)
265 (* #define Parameters *)
266
267 and base_define_param =
268 DParam of ident
269 | DPComma of string mcode
270 | DPdots of string mcode (* ... *)
271 | DPcircles of string mcode (* ooo *)
272 | OptDParam of define_param
273 | UniqueDParam of define_param
274
275 and define_param = base_define_param wrap
276
277 and base_define_parameters =
278 NoParams
279 | DParams of string mcode(*( *) * define_param dots * string mcode(* )*)
280
281 and define_parameters = base_define_parameters wrap
282
283 (* --------------------------------------------------------------------- *)
284 (* Statement*)
285
286 and base_statement =
287 Decl of (info * mcodekind) (* before the decl *) * declaration
288 | Seq of string mcode (* { *) * statement dots *
289 string mcode (* } *)
290 | ExprStatement of expression * string mcode (*;*)
291 | IfThen of string mcode (* if *) * string mcode (* ( *) *
292 expression * string mcode (* ) *) *
293 statement * (info * mcodekind)
294 | IfThenElse of string mcode (* if *) * string mcode (* ( *) *
295 expression * string mcode (* ) *) *
296 statement * string mcode (* else *) * statement *
297 (info * mcodekind)
298 | While of string mcode (* while *) * string mcode (* ( *) *
299 expression * string mcode (* ) *) *
300 statement * (info * mcodekind) (* after info *)
301 | Do of string mcode (* do *) * statement *
302 string mcode (* while *) * string mcode (* ( *) *
303 expression * string mcode (* ) *) *
304 string mcode (* ; *)
305 | For of string mcode (* for *) * string mcode (* ( *) *
306 expression option * string mcode (*;*) *
307 expression option * string mcode (*;*) *
308 expression option * string mcode (* ) *) * statement *
309 (info * mcodekind) (* after info *)
310 | Iterator of ident (* name *) * string mcode (* ( *) *
311 expression dots * string mcode (* ) *) *
312 statement * (info * mcodekind) (* after info *)
313 | Switch of string mcode (* switch *) * string mcode (* ( *) *
314 expression * string mcode (* ) *) * string mcode (* { *) *
315 statement (*decl*) dots *
316 case_line dots * string mcode (* } *)
317 | Break of string mcode (* break *) * string mcode (* ; *)
318 | Continue of string mcode (* continue *) * string mcode (* ; *)
319 | Label of ident * string mcode (* : *)
320 | Goto of string mcode (* goto *) * ident * string mcode (* ; *)
321 | Return of string mcode (* return *) * string mcode (* ; *)
322 | ReturnExpr of string mcode (* return *) * expression *
323 string mcode (* ; *)
324 | MetaStmt of Ast_cocci.meta_name mcode * pure
325 | MetaStmtList of Ast_cocci.meta_name mcode (*only in statement lists*) *
326 pure
327 | Exp of expression (* only in dotted statement lists *)
328 | TopExp of expression (* for macros body *)
329 | Ty of typeC (* only at top level *)
330 | TopInit of initialiser (* only at top level *)
331 | Disj of string mcode * statement dots list * string mcode list *
332 string mcode
333 | Nest of string mcode * statement dots * string mcode *
334 (statement dots,statement) whencode list * Ast_cocci.multi
335 | Dots of string mcode (* ... *) *
336 (statement dots,statement) whencode list
337 | Circles of string mcode (* ooo *) *
338 (statement dots,statement) whencode list
339 | Stars of string mcode (* *** *) *
340 (statement dots,statement) whencode list
341 | FunDecl of (info * mcodekind) (* before the function decl *) *
342 fninfo list * ident (* name *) *
343 string mcode (* ( *) * parameter_list * string mcode (* ) *) *
344 string mcode (* { *) * statement dots *
345 string mcode (* } *)
346 | Include of string mcode (* #include *) * Ast_cocci.inc_file mcode(* file *)
347 | Define of string mcode (* #define *) * ident (* name *) *
348 define_parameters (*params*) * statement dots
349 | OptStm of statement
350 | UniqueStm of statement
351
352 and fninfo =
353 FStorage of Ast_cocci.storage mcode
354 | FType of typeC
355 | FInline of string mcode
356 | FAttr of string mcode
357
358 and ('a,'b) whencode =
359 WhenNot of 'a
360 | WhenAlways of 'b
361 | WhenModifier of Ast_cocci.when_modifier
362 | WhenNotTrue of expression
363 | WhenNotFalse of expression
364
365 and statement = base_statement wrap
366
367 and base_case_line =
368 Default of string mcode (* default *) * string mcode (*:*) * statement dots
369 | Case of string mcode (* case *) * expression * string mcode (*:*) *
370 statement dots
371 | DisjCase of string mcode * case_line list *
372 string mcode list (* the |s *) * string mcode
373 | OptCase of case_line
374
375 and case_line = base_case_line wrap
376
377 (* --------------------------------------------------------------------- *)
378 (* Positions *)
379
380 and meta_pos =
381 MetaPos of Ast_cocci.meta_name mcode * Ast_cocci.meta_name list *
382 Ast_cocci.meta_collect
383 | NoMetaPos
384
385 (* --------------------------------------------------------------------- *)
386 (* Top-level code *)
387
388 and base_top_level =
389 DECL of statement
390 | CODE of statement dots
391 | FILEINFO of string mcode (* old file *) * string mcode (* new file *)
392 | ERRORWORDS of expression list
393 | OTHER of statement (* temporary, disappears after top_level.ml *)
394
395 and top_level = base_top_level wrap
396 and rule = top_level list
397
398 and parsed_rule =
399 CocciRule of
400 (rule * Ast_cocci.metavar list *
401 (string list * string list * Ast_cocci.dependency * string *
402 Ast_cocci.exists)) *
403 (rule * Ast_cocci.metavar list) * Ast_cocci.ruletype
404 | ScriptRule of string (* name *) *
405 string * Ast_cocci.dependency *
406 (Ast_cocci.script_meta_name *
407 Ast_cocci.meta_name * Ast_cocci.metavar) list (*inherited vars*) *
408 Ast_cocci.meta_name list (*script vars*) *
409 string
410 | InitialScriptRule of string (* name *) *
411 string (*language*) * Ast_cocci.dependency * string (*code*)
412 | FinalScriptRule of string (* name *) *
413 string (*language*) * Ast_cocci.dependency * string (*code*)
414
415 (* --------------------------------------------------------------------- *)
416
417 and anything =
418 DotsExprTag of expression dots
419 | DotsInitTag of initialiser dots
420 | DotsParamTag of parameterTypeDef dots
421 | DotsStmtTag of statement dots
422 | DotsDeclTag of declaration dots
423 | DotsCaseTag of case_line dots
424 | IdentTag of ident
425 | ExprTag of expression
426 | ArgExprTag of expression (* for isos *)
427 | TestExprTag of expression (* for isos *)
428 | TypeCTag of typeC
429 | ParamTag of parameterTypeDef
430 | InitTag of initialiser
431 | DeclTag of declaration
432 | StmtTag of statement
433 | CaseLineTag of case_line
434 | TopTag of top_level
435 | IsoWhenTag of Ast_cocci.when_modifier (*only for when code, in iso phase*)
436 | IsoWhenTTag of expression(*only for when code, in iso phase*)
437 | IsoWhenFTag of expression(*only for when code, in iso phase*)
438 | MetaPosTag of meta_pos (* only in iso phase *)
439
440 val dotsExpr : expression dots -> anything
441 val dotsInit : initialiser dots -> anything
442 val dotsParam : parameterTypeDef dots -> anything
443 val dotsStmt : statement dots -> anything
444 val dotsDecl : declaration dots -> anything
445 val dotsCase : case_line dots -> anything
446 val ident : ident -> anything
447 val expr : expression -> anything
448 val typeC : typeC -> anything
449 val param : parameterTypeDef -> anything
450 val ini : initialiser -> anything
451 val decl : declaration -> anything
452 val stmt : statement -> anything
453 val case_line : case_line -> anything
454 val top : top_level -> anything
455
456 (* --------------------------------------------------------------------- *)
457
458 val undots : 'a dots -> 'a list
459
460 (* --------------------------------------------------------------------- *)
461 (* Avoid cluttering the parser. Calculated in compute_lines.ml. *)
462
463 val default_info : unit -> info
464 val default_befaft : unit -> mcodekind
465 val context_befaft : unit -> mcodekind
466 val wrap : 'a -> 'a wrap
467 val context_wrap : 'a -> 'a wrap
468 val unwrap : 'a wrap -> 'a
469 val unwrap_mcode : 'a mcode -> 'a
470 val rewrap : 'a wrap -> 'b -> 'b wrap
471 val rewrap_mcode : 'a mcode -> 'b -> 'b mcode
472 val copywrap : 'a wrap -> 'b -> 'b wrap
473 val get_pos : 'a mcode -> meta_pos
474 val get_pos_ref : 'a mcode -> meta_pos ref
475 val set_pos : meta_pos -> 'a mcode -> 'a mcode
476 val get_info : 'a wrap -> info
477 val set_info : 'a wrap -> info -> 'a wrap
478 val get_index : 'a wrap -> int
479 val set_index : 'a wrap -> int -> unit
480 val get_line : 'a wrap -> int
481 val get_line_end : 'a wrap -> int
482 val get_mcodekind : 'a wrap -> mcodekind
483 val get_mcode_mcodekind : 'a mcode -> mcodekind
484 val get_mcodekind_ref : 'a wrap -> mcodekind ref
485 val set_mcodekind : 'a wrap -> mcodekind -> unit
486 val set_type : 'a wrap -> Type_cocci.typeC option -> unit
487 val get_type : 'a wrap -> Type_cocci.typeC option
488 val set_dots_bef_aft : statement -> dots_bef_aft -> statement
489 val get_dots_bef_aft : 'a wrap -> dots_bef_aft
490 val set_arg_exp : expression -> expression
491 val get_arg_exp : expression -> bool
492 val set_test_pos : expression -> expression
493 val get_test_pos : 'a wrap -> bool
494 val set_test_exp : expression -> expression
495 val get_test_exp : 'a wrap -> bool
496 val set_iso : 'a wrap -> (string*anything) list -> 'a wrap
497 val get_iso : 'a wrap -> (string*anything) list
498 val fresh_index : unit -> int
499 val set_mcode_data : 'a -> 'a mcode -> 'a mcode
500 val make_mcode : 'a -> 'a mcode
501 val make_mcode_info : 'a -> info -> 'a mcode
502 val make_minus_mcode : 'a -> 'a mcode
503
504 val ast0_type_to_type : typeC -> Type_cocci.typeC
505 val reverse_type : Type_cocci.typeC -> base_typeC
506 exception TyConv
507
508 val lub_pure : pure -> pure -> pure
509
510 (* --------------------------------------------------------------------- *)
511
512 val rule_name : string ref (* for the convenience of the parser *)