Release coccinelle-0.2.4rc6
[bpt/coccinelle.git] / parsing_c / ast_c.ml
CommitLineData
0708f913 1(* Yoann Padioleau
ae4735db
C
2 *
3 * Copyright (C) 2010, University of Copenhagen DIKU and INRIA.
0708f913 4 * Copyright (C) 2002, 2006, 2007, 2008, 2009 Yoann Padioleau
34e49164
C
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License (GPL)
8 * version 2 as published by the Free Software Foundation.
ae4735db 9 *
34e49164
C
10 * This program 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 * file license.txt for more details.
14 *)
15open Common
16
17(*****************************************************************************)
18(* The AST C related types *)
19(*****************************************************************************)
b1b2de81
C
20(*
21 * Some stuff are tagged semantic: which means that they are computed
ae4735db
C
22 * after parsing.
23 *
24 * This means that some elements in this AST are present only if
b1b2de81
C
25 * some annotation/transformation has been done on the original AST returned
26 * by the parser. Cf type_annotater, comment_annotater, cpp_ast_c, etc.
27 *)
28
29
30(* ------------------------------------------------------------------------- *)
31(* Token/info *)
32(* ------------------------------------------------------------------------- *)
34e49164 33
ae4735db
C
34(* To allow some transformations over the AST, we keep as much information
35 * as possible in the AST such as the tokens content and their locations.
485bce71
C
36 * Those info are called 'info' (how original) and can be tagged.
37 * For instance one tag may say that the unparser should remove this token.
ae4735db 38 *
485bce71 39 * Update: Now I use a ref! in those 'info' so take care.
0708f913 40 * That means that modifications of the info of tokens can have
ae4735db 41 * an effect on the info stored in the ast (which is sometimes
0708f913 42 * convenient, cf unparse_c.ml or comment_annotater_c.ml)
ae4735db
C
43 *
44 * convention: I often use 'ii' for the name of a list of info.
45 *
46 * Sometimes we want to add someting at the beginning or at the end
485bce71
C
47 * of a construct. For 'function' and 'decl' we want to add something
48 * to their left and for 'if' 'while' et 'for' and so on at their right.
49 * We want some kinds of "virtual placeholders" that represent the start or
50 * end of a construct. We use fakeInfo for that purpose.
51 * To identify those cases I have added a fakestart/fakeend comment.
ae4735db 52 *
485bce71 53 * cocci: Each token will be decorated in the future by the mcodekind
34e49164
C
54 * of cocci. It is the job of the pretty printer to look at this
55 * information and decide to print or not the token (and also the
56 * pending '+' associated sometimes with the token).
ae4735db 57 *
34e49164
C
58 * The first time that we parse the original C file, the mcodekind is
59 * empty, or more precisely all is tagged as a CONTEXT with NOTHING
60 * associated. This is what I call a "clean" expr/statement/....
ae4735db 61 *
34e49164
C
62 * Each token will also be decorated in the future with an environment,
63 * because the pending '+' may contain metavariables that refer to some
64 * C code.
ae4735db 65 *
34e49164
C
66 *)
67
68(* forunparser: *)
69
91eba41f 70type posl = int * int (* line-col, for MetaPosValList, for position variables *)
708f4980 71 (* with sexp *)
485bce71
C
72
73(* the virtual position is set in Parsing_hacks.insert_virtual_positions *)
34e49164 74type virtual_position = Common.parse_info * int (* character offset *)
708f4980 75 (* with sexp *)
485bce71 76
ae4735db 77type parse_info =
34e49164
C
78 (* Present both in ast and list of tokens *)
79 | OriginTok of Common.parse_info
80 (* Present only in ast and generated after parsing. Used mainly
81 * by Julia, to add stuff at virtual places, beginning of func or decl *)
82 | FakeTok of string * virtual_position
83 (* Present both in ast and list of tokens. *)
84 | ExpandedTok of Common.parse_info * virtual_position
0708f913 85
34e49164
C
86 (* Present neither in ast nor in list of tokens
87 * but only in the '+' of the mcode of some tokens. Those kind of tokens
88 * are used to be able to use '=' to compare big ast portions.
89 *)
90 | AbstractLineTok of Common.parse_info (* local to the abstracted thing *)
708f4980 91 (* with sexp *)
34e49164 92
ae4735db 93type info = {
34e49164 94 pinfo : parse_info;
b1b2de81
C
95
96 (* this cocci_tag can be changed, which is how we can express some program
ae4735db 97 * transformations by tagging the tokens involved in this transformation.
485bce71 98 *)
951c7801 99 cocci_tag: (Ast_cocci.mcodekind * metavars_binding list) option ref;
0708f913 100 (* set in comment_annotater_c.ml *)
485bce71 101 comments_tag: comments_around ref;
b1b2de81 102
34e49164
C
103 (* todo? token_info : sometimes useful to know what token it was *)
104 }
105and il = info list
106
107(* wrap2 is like wrap, except that I use it often for separator such
108 * as ','. In that case the info is associated to the argument that
ae4735db
C
109 * follows, so in 'a,b' I will have in the list [(a,[]); (b,[','])].
110 *
111 * wrap3 is like wrap, except that I use it in case sometimes it
708f4980
C
112 * will be empty because the info will be included in a nested
113 * entity (e.g. for Ident in expr because it's inlined in the name)
114 * so user should never assume List.length wrap3 > 0.
115 *)
34e49164
C
116and 'a wrap = 'a * il
117and 'a wrap2 = 'a * il
708f4980 118and 'a wrap3 = 'a * il (* * evotype*)
34e49164 119
b1b2de81
C
120(* ------------------------------------------------------------------------- *)
121(* Name *)
122(* ------------------------------------------------------------------------- *)
123
124(* was called 'ident' before, but 'name' is I think better
951c7801 125 * as concatenated strings can be used not only for identifiers and for
b1b2de81 126 * declarators, but also for fields, for labels, etc.
951c7801 127 *
708f4980
C
128 * Note: because now the info is embeded in the name, the info for
129 * expression like Ident, or types like Typename, are not anymore
130 * stored in the expression or type. Hence if you assume this,
131 * which was true before, you are now wrong. So never write code like
132 * let (unwrape,_), ii = e and use 'ii' believing it contains
133 * the local ii to e. If you want to do that, use the appropiate
134 * wrapper get_local_ii_of_expr_inlining_ii_of_name.
b1b2de81 135 *)
951c7801 136and name =
b1b2de81
C
137 | RegularName of string wrap
138 | CppConcatenatedName of (string wrap) wrap2 (* the ## separators *) list
139 (* normally only used inside list of things, as in parameters or arguments
140 * in which case, cf cpp-manual, it has a special meaning *)
141 | CppVariadicName of string wrap (* ## s *)
951c7801 142 | CppIdentBuilder of string wrap (* s ( ) *) *
b1b2de81
C
143 ((string wrap) wrap2 list) (* arguments *)
144
145
34e49164
C
146(* ------------------------------------------------------------------------- *)
147(* C Type *)
148(* ------------------------------------------------------------------------- *)
149(* Could have more precise type in fullType, in expression, etc, but
150 * it requires to do too much things in parsing such as checking no
151 * conflicting structname, computing value, etc. Better to separate
91eba41f 152 * concern. So I put '=>' to mean what we would really like. In fact
34e49164
C
153 * what we really like is defining another fullType, expression, etc
154 * from scratch, because many stuff are just sugar.
ae4735db 155 *
34e49164
C
156 * invariant: Array and FunctionType have also typeQualifier but they
157 * dont have sense. I put this to factorise some code. If you look in
91eba41f 158 * the grammar, you see that we can never specify const for the array
ae4735db 159 * himself (but we can do it for pointer) or function, we always
91eba41f 160 * have in the action rule of the grammar a { (nQ, FunctionType ...) }.
ae4735db
C
161 *
162 *
34e49164
C
163 * Because of ExprStatement, we can have more 'new scope' events, but
164 * rare I think. For instance with 'array of constExpression' there can
165 * have an exprStatement and a new (local) struct defined. Same for
166 * Constructor.
ae4735db 167 *
b1b2de81 168 *)
34e49164
C
169
170
171and fullType = typeQualifier * typeC
708f4980 172 and typeC = typeCbis wrap (* todo reput wrap3 *)
34e49164 173
b1b2de81 174 and typeCbis =
34e49164
C
175 | BaseType of baseType
176
177 | Pointer of fullType
178 | Array of constExpression option * fullType
179 | FunctionType of functionType
180
ae4735db 181 | Enum of string option * enumType
34e49164
C
182 | StructUnion of structUnion * string option * structType (* new scope *)
183
184 | EnumName of string
ae4735db 185 | StructUnionName of structUnion * string
34e49164 186
b1b2de81 187 | TypeName of name * fullType option (* semantic: filled later *)
ae4735db 188
34e49164
C
189 | ParenType of fullType (* forunparser: *)
190
ae4735db
C
191 (* gccext: TypeOfType below may seems useless; Why declare a
192 * __typeof__(int) x; ?
b1b2de81 193 * When used with macros, it allows to fix a problem of C which
34e49164 194 * is that type declaration can be spread around the ident. Indeed it
ae4735db
C
195 * may be difficult to have a macro such as
196 * '#define macro(type, ident) type ident;'
197 * because when you want to do a
198 * macro(char[256], x),
199 * then it will generate invalid code, but with a
200 * '#define macro(type, ident) __typeof(type) ident;'
201 * it will work.
b1b2de81 202 *)
ae4735db
C
203 | TypeOfExpr of expression
204 | TypeOfType of fullType
485bce71
C
205
206 (* cppext: IfdefType TODO *)
ae4735db
C
207
208(* -------------------------------------- *)
209 and baseType = Void
210 | IntType of intType
34e49164 211 | FloatType of floatType
1eddfd50
C
212 | SizeType
213 | SSizeType
214 | PtrDiffType
34e49164 215
ae4735db 216 (* stdC: type section
34e49164 217 * add a | SizeT ?
ae4735db 218 * note: char and signed char are semantically different!!
34e49164
C
219 *)
220 and intType = CChar (* obsolete? | CWchar *)
221 | Si of signed
222
223 and signed = sign * base
224 and base = CChar2 | CShort | CInt | CLong | CLongLong (* gccext: *)
225 and sign = Signed | UnSigned
226
227 and floatType = CFloat | CDouble | CLongDouble
228
229
ae4735db 230 (* -------------------------------------- *)
34e49164 231 and structUnion = Struct | Union
ae4735db
C
232 and structType = field list
233 and field =
485bce71 234 | DeclarationField of field_declaration
b1b2de81 235 (* gccext: *)
708f4980 236 | EmptyField of info
b1b2de81 237
485bce71 238 (* cppext: *)
ae4735db 239 | MacroDeclField of (string * argument wrap2 list)
708f4980 240 wrap (* optional ';'*)
485bce71
C
241
242 (* cppext: *)
243 | CppDirectiveStruct of cpp_directive
244 | IfdefStruct of ifdef_directive (* * field list list *)
245
34e49164
C
246
247 (* before unparser, I didn't have a FieldDeclList but just a Field. *)
ae4735db 248 and field_declaration =
485bce71 249 | FieldDeclList of fieldkind wrap2 list (* , *) wrap (* ; *)
34e49164
C
250
251 (* At first I thought that a bitfield could be only Signed/Unsigned.
252 * But it seems that gcc allow char i:4. C rule must say that you
ae4735db 253 * can cast into int so enum too, ...
34e49164 254 *)
ae4735db 255 and fieldkind =
b1b2de81 256 | Simple of name option * fullType
ae4735db 257 | BitField of name option * fullType *
b1b2de81 258 info (* : *) * constExpression
ae4735db 259 (* fullType => BitFieldInt | BitFieldUnsigned *)
34e49164
C
260
261
ae4735db 262 (* -------------------------------------- *)
c491d8ee 263 and enumType = oneEnumType wrap2 (* , *) list
34e49164
C
264 (* => string * int list *)
265
c491d8ee 266 and oneEnumType = name * (info (* = *) * constExpression) option
34e49164 267
ae4735db 268 (* -------------------------------------- *)
34e49164
C
269 (* return * (params * has "...") *)
270 and functionType = fullType * (parameterType wrap2 list * bool wrap)
ae4735db 271 and parameterType =
b1b2de81
C
272 { p_namei: name option;
273 p_register: bool wrap;
274 p_type: fullType;
275 }
276 (* => (bool (register) * fullType) list * bool *)
34e49164
C
277
278
ae4735db 279and typeQualifier = typeQualifierbis wrap
34e49164
C
280and typeQualifierbis = {const: bool; volatile: bool}
281
485bce71
C
282(* gccext: cppext: *)
283and attribute = attributebis wrap
284 and attributebis =
951c7801 285 | Attribute of string
34e49164
C
286
287(* ------------------------------------------------------------------------- *)
288(* C expression *)
289(* ------------------------------------------------------------------------- *)
708f4980 290and expression = (expressionbis * exp_info ref (* semantic: *)) wrap3
485bce71 291 and exp_info = exp_type option * test
0708f913 292 and exp_type = fullType (* Type_c.completed_and_simplified *) * local
485bce71
C
293 and local = LocalVar of parse_info | NotLocalVar (* cocci: *)
294 and test = Test | NotTest (* cocci: *)
295
951c7801 296 and expressionbis =
34e49164
C
297
298 (* Ident can be a enumeration constant, a simple variable, a name of a func.
299 * With cppext, Ident can also be the name of a macro. Sparse says
b1b2de81
C
300 * "an identifier with a meaning is a symbol" *)
301 | Ident of name (* todo? more semantic info such as LocalFunc *)
302
951c7801 303 | Constant of constant
34e49164 304 | FunCall of expression * argument wrap2 (* , *) list
b1b2de81 305 (* gccext: x ? /* empty */ : y <=> x ? x : y; hence the 'option' below *)
34e49164
C
306 | CondExpr of expression * expression option * expression
307
308 (* should be considered as statements, bad C langage *)
faf9a90c
C
309 | Sequence of expression * expression
310 | Assignment of expression * assignOp * expression
34e49164 311
91eba41f
C
312
313 | Postfix of expression * fixOp
314 | Infix of expression * fixOp
315
951c7801
C
316 | Unary of expression * unaryOp
317 | Binary of expression * binaryOp * expression
34e49164 318
91eba41f
C
319 | ArrayAccess of expression * expression
320
321 (* field ident access *)
b1b2de81
C
322 | RecordAccess of expression * name
323 | RecordPtAccess of expression * name
34e49164
C
324 (* redundant normally, could replace it by DeRef RecordAcces *)
325
ae4735db
C
326 | SizeOfExpr of expression
327 | SizeOfType of fullType
328 | Cast of fullType * expression
34e49164 329
ae4735db
C
330 (* gccext: *)
331 | StatementExpr of compound wrap (* ( ) new scope *)
332 | Constructor of fullType * initialiser wrap2 (* , *) list
34e49164
C
333
334 (* forunparser: *)
ae4735db 335 | ParenExpr of expression
34e49164 336
485bce71
C
337 (* cppext: IfdefExpr TODO *)
338
34e49164 339 (* cppext: normmally just expression *)
708f4980 340 and argument = (expression, weird_argument) Common.either
ae4735db 341 and weird_argument =
34e49164
C
342 | ArgType of parameterType
343 | ArgAction of action_macro
ae4735db 344 and action_macro =
485bce71 345 (* todo: ArgStatement of statement, possibly have ghost token *)
ae4735db 346 | ActMisc of il
34e49164
C
347
348
349 (* I put string for Int and Float because int would not be enough because
350 * OCaml int are 31 bits. So simpler to do string. Same reason to have
351 * string instead of int list for the String case.
ae4735db 352 *
b1b2de81 353 * note: -2 is not a constant, it is the unary operator '-'
34e49164
C
354 * applied to constant 2. So the string must represent a positive
355 * integer only. *)
356
ae4735db 357 and constant =
0708f913
C
358 | String of (string * isWchar)
359 | MultiString of string list (* can contain MacroString, todo: more info *)
34e49164 360 | Char of (string * isWchar) (* normally it is equivalent to Int *)
708f4980 361 | Int of (string * intType)
34e49164
C
362 | Float of (string * floatType)
363
364 and isWchar = IsWchar | IsChar
365
ae4735db
C
366
367 and unaryOp = GetRef | DeRef | UnPlus | UnMinus | Tilde | Not
485bce71 368 | GetRefLabel (* gccext: GetRefLabel, via &&label notation *)
34e49164
C
369 and assignOp = SimpleAssign | OpAssign of arithOp
370 and fixOp = Dec | Inc
371
372 and binaryOp = Arith of arithOp | Logical of logicalOp
373
ae4735db 374 and arithOp =
34e49164 375 | Plus | Minus | Mul | Div | Mod
ae4735db 376 | DecLeft | DecRight
34e49164
C
377 | And | Or | Xor
378
ae4735db
C
379 and logicalOp =
380 | Inf | Sup | InfEq | SupEq
381 | Eq | NotEq
34e49164
C
382 | AndLog | OrLog
383
384 and constExpression = expression (* => int *)
385
386
387(* ------------------------------------------------------------------------- *)
388(* C statement *)
389(* ------------------------------------------------------------------------- *)
390(* note: that assignement is not a statement but an expression;
391 * wonderful C langage.
ae4735db 392 *
34e49164 393 * note: I use 'and' for type definition cos gccext allow statement as
ae4735db
C
394 * expression, so need mutual recursive type definition.
395 *
b1b2de81 396 *)
34e49164 397
708f4980 398and statement = statementbis wrap3
ae4735db 399 and statementbis =
34e49164
C
400 | Labeled of labeled
401 | Compound of compound (* new scope *)
402 | ExprStatement of exprStatement
403 | Selection of selection (* have fakeend *)
404 | Iteration of iteration (* have fakeend *)
405 | Jump of jump
406
407 (* simplify cocci: only at the beginning of a compound normally *)
ae4735db 408 | Decl of declaration
34e49164
C
409
410 (* gccext: *)
411 | Asm of asmbody
412 | NestedFunc of definition
413
414 (* cppext: *)
415 | MacroStmt
ae4735db 416
34e49164
C
417
418
b1b2de81 419 and labeled = Label of name * statement
ae4735db 420 | Case of expression * statement
34e49164
C
421 | CaseRange of expression * expression * statement (* gccext: *)
422 | Default of statement
423
ae4735db
C
424 (* cppext:
425 * old: compound = (declaration list * statement list)
426 * old: (declaration, statement) either list
34e49164 427 * Simplify cocci to just have statement list, by integrating Decl in stmt.
ae4735db 428 *
485bce71 429 * update: now introduce also the _sequencable to allow ifdef in the middle.
b1b2de81
C
430 * Indeed, I now allow ifdefs in the ast but they must be only between
431 * "sequencable" elements. They can be put in a type only if this type
ae4735db
C
432 * is used in a list, like at the toplevel, used in a 'toplevel list',
433 * or inside a compound, used in a 'statement list'. I must not allow
434 * ifdef anywhere. For instance I can not make ifdef a statement
b1b2de81
C
435 * cos some instruction like If accept only one statement and the
436 * ifdef directive must not take the place of a legitimate instruction.
437 * We had a similar phenomena in SmPL where we have the notion
ae4735db 438 * of statement and sequencable statement too. Once you have
b1b2de81
C
439 * such a type of sequencable thing, then s/xx list/xx_sequencable list/
440 * and introduce the ifdef.
ae4735db 441 *
b1b2de81
C
442 * update: those ifdefs are either passed, or present in the AST but in
443 * a flat form. To structure those flat ifdefs you have to run
444 * a transformation that will put in a tree the statements inside
445 * ifdefs branches. Cf cpp_ast_c.ml. This is for instance the difference
446 * between a IfdefStmt (flat) and IfdefStmt2 (tree structured).
ae4735db 447 *
34e49164 448 *)
ae4735db 449 and compound = statement_sequencable list
485bce71
C
450
451 (* cppext: easier to put at statement_list level than statement level *)
ae4735db 452 and statement_sequencable =
485bce71 453 | StmtElem of statement
b1b2de81 454
ae4735db 455 (* cppext: *)
485bce71 456 | CppDirectiveStmt of cpp_directive
ae4735db 457 | IfdefStmt of ifdef_directive
485bce71
C
458
459 (* this will be build in cpp_ast_c from the previous flat IfdefStmt *)
460 | IfdefStmt2 of ifdef_directive list * (statement_sequencable list) list
34e49164
C
461
462 and exprStatement = expression option
463
ae4735db 464 (* for Switch, need check that all elements in the compound start
34e49164
C
465 * with a case:, otherwise unreachable code.
466 *)
ae4735db 467 and selection =
34e49164 468 | If of expression * statement * statement
ae4735db 469 | Switch of expression * statement
485bce71 470
34e49164 471
ae4735db 472 and iteration =
34e49164
C
473 | While of expression * statement
474 | DoWhile of statement * expression
475 | For of exprStatement wrap * exprStatement wrap * exprStatement wrap *
476 statement
485bce71 477 (* cppext: *)
34e49164
C
478 | MacroIteration of string * argument wrap2 list * statement
479
b1b2de81 480 and jump = Goto of name
ae4735db 481 | Continue | Break
34e49164
C
482 | Return | ReturnExpr of expression
483 | GotoComputed of expression (* gccext: goto *exp ';' *)
484
485
486 (* gccext: *)
487 and asmbody = il (* string list *) * colon wrap (* : *) list
488 and colon = Colon of colon_option wrap2 list
489 and colon_option = colon_option_bis wrap
490 and colon_option_bis = ColonMisc | ColonExpr of expression
491
492
493(* ------------------------------------------------------------------------- *)
494(* Declaration *)
495(* ------------------------------------------------------------------------- *)
ae4735db 496(* (string * ...) option cos can have empty declaration or struct tag
34e49164 497 * declaration.
ae4735db
C
498 *
499 * Before I had a Typedef constructor, but why make this special case and not
500 * have StructDef, EnumDef, ... so that 'struct t {...} v' will generate 2
485bce71 501 * declarations ? So I try to generalise and not have Typedef either. This
34e49164 502 * requires more work in parsing. Better to separate concern.
ae4735db 503 *
34e49164
C
504 * Before the need for unparser, I didn't have a DeclList but just a Decl.
505 *
506 * I am not sure what it means to declare a prototype inline, but gcc
ae4735db 507 * accepts it.
34e49164
C
508 *)
509
ae4735db 510and declaration =
34e49164
C
511 | DeclList of onedecl wrap2 (* , *) list wrap (* ; fakestart sto *)
512 (* cppext: *)
708f4980 513 | MacroDecl of (string * argument wrap2 list) wrap (* fakestart *)
34e49164 514
ae4735db 515 and onedecl =
b1b2de81 516 { v_namei: (name * (info (* = *) * initialiser) option) option;
485bce71 517 v_type: fullType;
ae4735db 518 (* semantic: set in type annotated and used in cocci_vs_c
978fd7e5
C
519 * when we transform some initialisation into affectation
520 *)
521 v_type_bis: fullType (* Type_c.completed_and_simplified *) option ref;
485bce71
C
522 v_storage: storage;
523 v_local: local_decl; (* cocci: *)
524 v_attr: attribute list; (* gccext: *)
525 }
526 and storage = storagebis * bool (* gccext: inline or not *)
34e49164
C
527 and storagebis = NoSto | StoTypedef | Sto of storageClass
528 and storageClass = Auto | Static | Register | Extern
529
b1b2de81
C
530 and local_decl = LocalDecl | NotLocalDecl
531
978fd7e5
C
532 (* fullType is the type used if the type should be converted to
533 an assignment. It can be adjusted in the type annotation
534 phase when typedef information is availalble *)
34e49164 535 and initialiser = initialiserbis wrap
ae4735db
C
536 and initialiserbis =
537 | InitExpr of expression
538 | InitList of initialiser wrap2 (* , *) list
34e49164
C
539 (* gccext: *)
540 | InitDesignators of designator list * initialiser
541 | InitFieldOld of string * initialiser
542 | InitIndexOld of expression * initialiser
543
544 (* ex: [2].y = x, or .y[2] or .y.x. They can be nested *)
ae4735db
C
545 and designator = designatorbis wrap
546 and designatorbis =
547 | DesignatorField of string
34e49164
C
548 | DesignatorIndex of expression
549 | DesignatorRange of expression * expression
ae4735db 550
34e49164
C
551(* ------------------------------------------------------------------------- *)
552(* Function definition *)
553(* ------------------------------------------------------------------------- *)
ae4735db
C
554(* Normally we should define another type functionType2 because there
555 * are more restrictions on what can define a function than a pointer
34e49164 556 * function. For instance a function declaration can omit the name of the
b1b2de81 557 * parameter whereas a function definition can not. But, in some cases such
ae4735db 558 * as 'f(void) {', there is no name too, so I simplified and reused the
34e49164 559 * same functionType type for both declaration and function definition.
ae4735db 560 *
b1b2de81
C
561 * Also old style C does not have type in the parameter, so again simpler
562 * to abuse the functionType and allow missing type.
34e49164 563 *)
b1b2de81 564and definition = definitionbis wrap (* ( ) { } fakestart sto *)
ae4735db 565 and definitionbis =
b1b2de81 566 { f_name: name;
708f4980 567 f_type: functionType; (* less? a functionType2 ? *)
485bce71
C
568 f_storage: storage;
569 f_body: compound;
570 f_attr: attribute list; (* gccext: *)
91eba41f 571 f_old_c_style: declaration list option;
485bce71
C
572 }
573 (* cppext: IfdefFunHeader TODO *)
34e49164
C
574
575(* ------------------------------------------------------------------------- *)
485bce71 576(* cppext: cpp directives, #ifdef, #define and #include body *)
34e49164 577(* ------------------------------------------------------------------------- *)
485bce71 578and cpp_directive =
ae4735db
C
579 | Define of define
580 | Include of includ
485bce71 581 | Undef of string wrap
ae4735db 582 | PragmaAndCo of il
b1b2de81 583(*| Ifdef ? no, ifdefs are handled differently, cf ifdef_directive below *)
485bce71 584
708f4980 585and define = string wrap (* #define s eol *) * (define_kind * define_val)
34e49164
C
586 and define_kind =
587 | DefineVar
485bce71 588 | DefineFunc of ((string wrap) wrap2 list) wrap (* () *)
ae4735db 589 and define_val =
b1b2de81 590 (* most common case; e.g. to define int constant *)
ae4735db 591 | DefineExpr of expression
91eba41f 592
34e49164
C
593 | DefineStmt of statement
594 | DefineType of fullType
485bce71 595 | DefineDoWhileZero of (statement * expression) wrap (* do { } while(0) *)
91eba41f 596
34e49164 597 | DefineFunction of definition
485bce71 598 | DefineInit of initialiser (* in practice only { } with possible ',' *)
b1b2de81 599
485bce71
C
600 (* TODO DefineMulti of define_val list *)
601
34e49164
C
602 | DefineText of string wrap
603 | DefineEmpty
604
485bce71 605 | DefineTodo
34e49164
C
606
607
485bce71 608
ae4735db 609and includ =
485bce71
C
610 { i_include: inc_file wrap; (* #include s *)
611 (* cocci: computed in ? *)
612 i_rel_pos: include_rel_pos option ref;
613 (* cocci: cf -test incl *)
ae4735db 614 i_is_in_ifdef: bool;
485bce71
C
615 (* cf cpp_ast_c.ml. set to None at parsing time. *)
616 i_content: (Common.filename (* full path *) * program) option;
617 }
ae4735db 618 and inc_file =
34e49164
C
619 | Local of inc_elem list
620 | NonLocal of inc_elem list
0708f913 621 | Weird of string (* ex: #include SYSTEM_H *)
34e49164
C
622 and inc_elem = string
623
485bce71 624 (* cocci: to tag the first of #include <xx/> and last of #include <yy/>
ae4735db 625 *
485bce71
C
626 * The first_of and last_of store the list of prefixes that was
627 * introduced by the include. On #include <a/b/x>, if the include was
628 * the first in the file, it would give in first_of the following
ae4735db
C
629 * prefixes a/b/c; a/b/; a/ ; <empty>
630 *
485bce71
C
631 * This is set after parsing, in cocci.ml, in update_rel_pos.
632 *)
ae4735db 633 and include_rel_pos = {
485bce71
C
634 first_of : string list list;
635 last_of : string list list;
34e49164
C
636 }
637
485bce71
C
638
639
b1b2de81
C
640(* todo? to specialize if someone need more info *)
641and ifdef_directive = (* or and 'a ifdefed = 'a list wrap *)
642 | IfdefDirective of (ifdefkind * matching_tag) wrap
ae4735db 643 and ifdefkind =
b1b2de81
C
644 | Ifdef (* todo? of string ? of formula_cpp ? *)
645 | IfdefElseif (* same *)
646 | IfdefElse (* same *)
ae4735db
C
647 | IfdefEndif
648 (* set in Parsing_hacks.set_ifdef_parenthize_info. It internally use
b1b2de81 649 * a global so it means if you parse the same file twice you may get
ae4735db 650 * different id. I try now to avoid this pb by resetting it each
b1b2de81
C
651 * time I parse a file.
652 *)
ae4735db 653 and matching_tag =
b1b2de81
C
654 IfdefTag of (int (* tag *) * int (* total with this tag *))
655
656
485bce71
C
657
658
659
34e49164
C
660(* ------------------------------------------------------------------------- *)
661(* The toplevels elements *)
662(* ------------------------------------------------------------------------- *)
663and toplevel =
664 | Declaration of declaration
665 | Definition of definition
ae4735db 666
34e49164 667 (* cppext: *)
485bce71
C
668 | CppTop of cpp_directive
669 | IfdefTop of ifdef_directive (* * toplevel list *)
670
34e49164 671 (* cppext: *)
ae4735db
C
672 | MacroTop of string * argument wrap2 list * il
673
34e49164
C
674 | EmptyDef of il (* gccext: allow redundant ';' *)
675 | NotParsedCorrectly of il
676
34e49164
C
677 | FinalDef of info (* EOF *)
678
679(* ------------------------------------------------------------------------- *)
680and program = toplevel list
681
34e49164
C
682(*****************************************************************************)
683(* Cocci Bindings *)
684(*****************************************************************************)
ae4735db
C
685(* Was previously in pattern.ml, but because of the transformer,
686 * we need to decorate each token with some cocci code AND the environment
34e49164
C
687 * for this cocci code.
688 *)
689and metavars_binding = (Ast_cocci.meta_name, metavar_binding_kind) assoc
ae4735db 690 and metavar_binding_kind =
5636bb2c
C
691 | MetaIdVal of string *
692 Ast_cocci.meta_name list (* negative constraints *)
34e49164
C
693 | MetaFuncVal of string
694 | MetaLocalFuncVal of string
695
5636bb2c
C
696 | MetaExprVal of expression (* a "clean expr" *) *
697 (*subterm constraints, currently exprs*)
698 Ast_cocci.meta_name list
34e49164
C
699 | MetaExprListVal of argument wrap2 list
700 | MetaParamVal of parameterType
701 | MetaParamListVal of parameterType wrap2 list
702
703 | MetaTypeVal of fullType
113803cf 704 | MetaInitVal of initialiser
413ffc02
C
705 | MetaDeclVal of declaration
706 | MetaFieldVal of field
34e49164
C
707 | MetaStmtVal of statement
708
709 (* Could also be in Lib_engine.metavars_binding2 with the ParenVal,
710 * because don't need to have the value for a position in the env of
711 * a '+'. But ParenVal or LabelVal are used only by CTL, they are not
712 * variables accessible via SmPL whereas the position can be one day
713 * so I think it's better to put MetaPosVal here *)
714 | MetaPosVal of (Ast_cocci.fixpos * Ast_cocci.fixpos) (* max, min *)
485bce71
C
715 | MetaPosValList of
716 (Common.filename * string (*element*) * posl * posl) list (* min, max *)
34e49164
C
717 | MetaListlenVal of int
718
719
720(*****************************************************************************)
721(* C comments *)
722(*****************************************************************************)
723
ae4735db 724(* convention: I often use "m" for comments as I can not use "c"
485bce71 725 * (already use for c stuff) and "com" is too long.
34e49164
C
726 *)
727
0708f913
C
728(* this type will be associated to each token.
729 *)
34e49164 730and comments_around = {
0708f913
C
731 mbefore: Token_c.comment_like_token list;
732 mafter: Token_c.comment_like_token list;
708f4980
C
733
734 (* less: could remove ? do something simpler than CComment for
735 * coccinelle, cf above. *)
736 mbefore2: comment_and_relative_pos list;
737 mafter2: comment_and_relative_pos list;
738 }
34e49164
C
739 and comment_and_relative_pos = {
740
741 minfo: Common.parse_info;
742 (* the int represent the number of lines of difference between the
743 * current token and the comment. When on same line, this number is 0.
744 * When previous line, -1. In some way the after/before in previous
745 * record is useless because the sign of the integer can helps
746 * do the difference too, but I keep it that way.
747 *)
748 mpos: int;
749 (* todo?
ae4735db 750 * cppbetween: bool; touse? if false positive
34e49164
C
751 * is_alone_in_line: bool; (*for labels, to avoid false positive*)
752 *)
708f4980 753 }
34e49164
C
754
755and comment = Common.parse_info
756and com = comment list ref
34e49164 757
708f4980 758 (* with sexp *)
34e49164
C
759
760
761(*****************************************************************************)
762(* Some constructors *)
763(*****************************************************************************)
764let nullQualif = ({const=false; volatile= false}, [])
ae4735db 765let nQ = nullQualif
34e49164
C
766
767let defaultInt = (BaseType (IntType (Si (Signed, CInt))))
768
769let noType () = ref (None,NotTest)
770let noInstr = (ExprStatement (None), [])
771let noTypedefDef () = None
772
ae4735db 773let emptyMetavarsBinding =
34e49164
C
774 ([]: metavars_binding)
775
708f4980 776let emptyAnnotCocci =
34e49164 777 (Ast_cocci.CONTEXT (Ast_cocci.NoPos,Ast_cocci.NOTHING),
951c7801 778 ([] : metavars_binding list))
34e49164 779
ae4735db 780let emptyAnnot =
951c7801 781 (None: (Ast_cocci.mcodekind * metavars_binding list) option)
708f4980
C
782
783(* compatibility mode *)
ae4735db 784let mcode_and_env_of_cocciref aref =
708f4980
C
785 match !aref with
786 | Some x -> x
787 | None -> emptyAnnotCocci
788
789
34e49164
C
790let emptyComments= {
791 mbefore = [];
792 mafter = [];
708f4980
C
793 mbefore2 = [];
794 mafter2 = [];
34e49164
C
795}
796
797
798(* for include, some meta information needed by cocci *)
ae4735db 799let noRelPos () =
34e49164 800 ref (None: include_rel_pos option)
ae4735db 801let noInIfdef () =
34e49164
C
802 ref false
803
804
ae4735db 805(* When want add some info in ast that does not correspond to
34e49164
C
806 * an existing C element.
807 * old: or when don't want 'synchronize' on it in unparse_c.ml
808 * (now have other mark for tha matter).
809 *)
810let no_virt_pos = ({str="";charpos=0;line=0;column=0;file=""},-1)
811
ae4735db 812let fakeInfo pi =
34e49164
C
813 { pinfo = FakeTok ("",no_virt_pos);
814 cocci_tag = ref emptyAnnot;
815 comments_tag = ref emptyComments;
816 }
817
485bce71
C
818let noii = []
819let noattr = []
820let noi_content = (None: ((Common.filename * program) option))
34e49164
C
821
822(*****************************************************************************)
823(* Wrappers *)
824(*****************************************************************************)
825let unwrap = fst
826
113803cf 827let unwrap2 = fst
34e49164
C
828
829let unwrap_expr ((unwrap_e, typ), iie) = unwrap_e
830let rewrap_expr ((_old_unwrap_e, typ), iie) newe = ((newe, typ), iie)
831
708f4980
C
832let unwrap_typeC (qu, (typeC, ii)) = typeC
833let rewrap_typeC (qu, (typeC, ii)) newtypeC = (qu, (newtypeC, ii))
834
835let unwrap_typeCbis (typeC, ii) = typeC
836
837let unwrap_st (unwrap_st, ii) = unwrap_st
838
839(* ------------------------------------------------------------------------- *)
840let mk_e unwrap_e ii = (unwrap_e, noType()), ii
841let mk_e_bis unwrap_e ty ii = (unwrap_e, ty), ii
842
843let mk_ty typeC ii = nQ, (typeC, ii)
844let mk_tybis typeC ii = (typeC, ii)
845
846let mk_st unwrap_st ii = (unwrap_st, ii)
847
848(* ------------------------------------------------------------------------- *)
849let get_ii_typeC_take_care (typeC, ii) = ii
850let get_ii_st_take_care (st, ii) = ii
851let get_ii_expr_take_care (e, ii) = ii
852
853let get_st_and_ii (st, ii) = st, ii
854let get_ty_and_ii (qu, (typeC, ii)) = qu, (typeC, ii)
855let get_e_and_ii (e, ii) = e, ii
856
857
858(* ------------------------------------------------------------------------- *)
34e49164
C
859let get_type_expr ((unwrap_e, typ), iie) = !typ
860let set_type_expr ((unwrap_e, oldtyp), iie) newtyp =
861 oldtyp := newtyp
862 (* old: (unwrap_e, newtyp), iie *)
863
ae4735db 864let get_onlytype_expr ((unwrap_e, typ), iie) =
91eba41f
C
865 match !typ with
866 | Some (ft,_local), _test -> Some ft
867 | None, _ -> None
868
ae4735db 869let get_onlylocal_expr ((unwrap_e, typ), iie) =
0708f913
C
870 match !typ with
871 | Some (ft,local), _test -> Some local
872 | None, _ -> None
873
91eba41f 874(* ------------------------------------------------------------------------- *)
ae4735db 875let rewrap_str s ii =
34e49164
C
876 {ii with pinfo =
877 (match ii.pinfo with
878 OriginTok pi -> OriginTok { pi with Common.str = s;}
879 | ExpandedTok (pi,vpi) -> ExpandedTok ({ pi with Common.str = s;},vpi)
880 | FakeTok (_,vpi) -> FakeTok (s,vpi)
881 | AbstractLineTok pi -> OriginTok { pi with Common.str = s;})}
882
ae4735db 883let rewrap_pinfo pi ii =
34e49164
C
884 {ii with pinfo = pi}
885
708f4980
C
886
887
34e49164
C
888(* info about the current location *)
889let get_pi = function
890 OriginTok pi -> pi
891 | ExpandedTok (_,(pi,_)) -> pi
892 | FakeTok (_,(pi,_)) -> pi
893 | AbstractLineTok pi -> pi
894
895(* original info *)
896let get_opi = function
897 OriginTok pi -> pi
708f4980 898 | ExpandedTok (pi,_) -> pi (* diff with get_pi *)
34e49164
C
899 | FakeTok (_,_) -> failwith "no position information"
900 | AbstractLineTok pi -> pi
901
34e49164
C
902let str_of_info ii =
903 match ii.pinfo with
904 OriginTok pi -> pi.Common.str
905 | ExpandedTok (pi,_) -> pi.Common.str
906 | FakeTok (s,_) -> s
907 | AbstractLineTok pi -> pi.Common.str
908
909let get_info f ii =
910 match ii.pinfo with
911 OriginTok pi -> f pi
912 | ExpandedTok (_,(pi,_)) -> f pi
913 | FakeTok (_,(pi,_)) -> f pi
914 | AbstractLineTok pi -> f pi
915
916let get_orig_info f ii =
917 match ii.pinfo with
918 OriginTok pi -> f pi
708f4980 919 | ExpandedTok (pi,_) -> f pi (* diff with get_info *)
34e49164
C
920 | FakeTok (_,(pi,_)) -> f pi
921 | AbstractLineTok pi -> f pi
922
923let make_expanded ii =
924 {ii with pinfo = ExpandedTok (get_opi ii.pinfo,no_virt_pos)}
925
926let pos_of_info ii = get_info (function x -> x.Common.charpos) ii
927let opos_of_info ii = get_orig_info (function x -> x.Common.charpos) ii
928let line_of_info ii = get_orig_info (function x -> x.Common.line) ii
929let col_of_info ii = get_orig_info (function x -> x.Common.column) ii
930let file_of_info ii = get_orig_info (function x -> x.Common.file) ii
708f4980 931let mcode_of_info ii = fst (mcode_and_env_of_cocciref ii.cocci_tag)
34e49164
C
932let pinfo_of_info ii = ii.pinfo
933let parse_info_of_info ii = get_pi ii.pinfo
934
ae4735db 935let strloc_of_info ii =
978fd7e5
C
936 spf "%s:%d" (file_of_info ii) (line_of_info ii)
937
485bce71
C
938let is_fake ii =
939 match ii.pinfo with
940 FakeTok (_,_) -> true
941 | _ -> false
942
ae4735db 943let is_origintok ii =
485bce71
C
944 match ii.pinfo with
945 | OriginTok pi -> true
946 | _ -> false
947
91eba41f 948(* ------------------------------------------------------------------------- *)
34e49164 949type posrv = Real of Common.parse_info | Virt of virtual_position
485bce71 950
34e49164
C
951let compare_pos ii1 ii2 =
952 let get_pos = function
953 OriginTok pi -> Real pi
954 | FakeTok (s,vpi) -> Virt vpi
955 | ExpandedTok (pi,vpi) -> Virt vpi
956 | AbstractLineTok pi -> Real pi in (* used for printing *)
957 let pos1 = get_pos (pinfo_of_info ii1) in
958 let pos2 = get_pos (pinfo_of_info ii2) in
959 match (pos1,pos2) with
faf9a90c
C
960 (Real p1, Real p2) ->
961 compare p1.Common.charpos p2.Common.charpos
34e49164 962 | (Virt (p1,_), Real p2) ->
b1b2de81 963 if (compare p1.Common.charpos p2.Common.charpos) =|= (-1) then (-1) else 1
34e49164 964 | (Real p1, Virt (p2,_)) ->
b1b2de81 965 if (compare p1.Common.charpos p2.Common.charpos) =|= 1 then 1 else (-1)
34e49164
C
966 | (Virt (p1,o1), Virt (p2,o2)) ->
967 let poi1 = p1.Common.charpos in
968 let poi2 = p2.Common.charpos in
969 match compare poi1 poi2 with
970 -1 -> -1
971 | 0 -> compare o1 o2
972 | x -> x
973
ae4735db 974let equal_posl (l1,c1) (l2,c2) =
34e49164
C
975 (l1 =|= l2) && (c1 =|= c2)
976
977let info_to_fixpos ii =
978 match pinfo_of_info ii with
979 OriginTok pi -> Ast_cocci.Real pi.Common.charpos
980 | ExpandedTok (_,(pi,offset)) ->
981 Ast_cocci.Virt (pi.Common.charpos,offset)
982 | FakeTok (_,(pi,offset)) ->
983 Ast_cocci.Virt (pi.Common.charpos,offset)
984 | AbstractLineTok pi -> failwith "unexpected abstract"
985
485bce71 986(* cocci: *)
34e49164 987let is_test (e : expression) =
708f4980 988 let (_,info), _ = e in
34e49164 989 let (_,test) = !info in
b1b2de81 990 test =*= Test
34e49164
C
991
992(*****************************************************************************)
993(* Abstract line *)
994(*****************************************************************************)
995
996(* When we have extended the C Ast to add some info to the tokens,
997 * such as its line number in the file, we can not use anymore the
998 * ocaml '=' to compare Ast elements. To overcome this problem, to be
999 * able to use again '=', we just have to get rid of all those extra
1000 * information, to "abstract those line" (al) information.
ae4735db 1001 *
91eba41f
C
1002 * Julia then modifies it a little to have a tokenindex, so the original
1003 * true al_info is in fact real_al_info.
34e49164
C
1004 *)
1005
ae4735db 1006let al_info tokenindex x =
34e49164
C
1007 { pinfo =
1008 (AbstractLineTok
1009 {charpos = tokenindex;
1010 line = tokenindex;
1011 column = tokenindex;
1012 file = "";
1013 str = str_of_info x});
1014 cocci_tag = ref emptyAnnot;
1015 comments_tag = ref emptyComments;
1016 }
1017
ae4735db 1018let semi_al_info x =
34e49164
C
1019 { x with
1020 cocci_tag = ref emptyAnnot;
1021 comments_tag = ref emptyComments;
1022 }
1023
ae4735db 1024let magic_real_number = -10
91eba41f 1025
ae4735db 1026let real_al_info x =
91eba41f
C
1027 { pinfo =
1028 (AbstractLineTok
1029 {charpos = magic_real_number;
1030 line = magic_real_number;
1031 column = magic_real_number;
1032 file = "";
1033 str = str_of_info x});
1034 cocci_tag = ref emptyAnnot;
1035 comments_tag = ref emptyComments;
1036 }
1037
b1b2de81
C
1038let al_comments x =
1039 let keep_cpp l =
1040 List.filter (function (Token_c.TCommentCpp _,_) -> true | _ -> false) l in
1041 let al_com (x,i) =
1042 (x,{i with Common.charpos = magic_real_number;
1043 Common.line = magic_real_number;
1044 Common.column = magic_real_number}) in
1045 {mbefore = []; (* duplicates mafter of the previous token *)
708f4980
C
1046 mafter = List.map al_com (keep_cpp x.mafter);
1047 mbefore2=[];
1048 mafter2=[];
1049 }
b1b2de81 1050
ae4735db 1051let al_info_cpp tokenindex x =
b1b2de81
C
1052 { pinfo =
1053 (AbstractLineTok
1054 {charpos = tokenindex;
1055 line = tokenindex;
1056 column = tokenindex;
1057 file = "";
1058 str = str_of_info x});
1059 cocci_tag = ref emptyAnnot;
1060 comments_tag = ref (al_comments !(x.comments_tag));
1061 }
1062
ae4735db 1063let semi_al_info_cpp x =
b1b2de81
C
1064 { x with
1065 cocci_tag = ref emptyAnnot;
1066 comments_tag = ref (al_comments !(x.comments_tag));
1067 }
1068
ae4735db 1069let real_al_info_cpp x =
b1b2de81
C
1070 { pinfo =
1071 (AbstractLineTok
1072 {charpos = magic_real_number;
1073 line = magic_real_number;
1074 column = magic_real_number;
1075 file = "";
1076 str = str_of_info x});
1077 cocci_tag = ref emptyAnnot;
1078 comments_tag = ref (al_comments !(x.comments_tag));
1079 }
1080
91eba41f 1081
34e49164
C
1082(*****************************************************************************)
1083(* Views *)
1084(*****************************************************************************)
1085
1086(* Transform a list of arguments (or parameters) where the commas are
1087 * represented via the wrap2 and associated with an element, with
1088 * a list where the comma are on their own. f(1,2,2) was
1089 * [(1,[]); (2,[,]); (2,[,])] and become [1;',';2;',';2].
ae4735db 1090 *
34e49164
C
1091 * Used in cocci_vs_c.ml, to have a more direct correspondance between
1092 * the ast_cocci of julia and ast_c.
1093 *)
ae4735db 1094let rec (split_comma: 'a wrap2 list -> ('a, il) either list) =
34e49164
C
1095 function
1096 | [] -> []
ae4735db
C
1097 | (e, ii)::xs ->
1098 if null ii
34e49164
C
1099 then (Left e)::split_comma xs
1100 else Right ii::Left e::split_comma xs
1101
ae4735db 1102let rec (unsplit_comma: ('a, il) either list -> 'a wrap2 list) =
34e49164
C
1103 function
1104 | [] -> []
ae4735db 1105 | Right ii::Left e::xs ->
34e49164 1106 (e, ii)::unsplit_comma xs
ae4735db 1107 | Left e::xs ->
34e49164
C
1108 let empty_ii = [] in
1109 (e, empty_ii)::unsplit_comma xs
ae4735db 1110 | Right ii::_ ->
34e49164
C
1111 raise Impossible
1112
1113
1114
1115
485bce71
C
1116(*****************************************************************************)
1117(* Helpers, could also be put in lib_parsing_c.ml instead *)
1118(*****************************************************************************)
1119
91eba41f
C
1120(* should maybe be in pretty_print_c ? *)
1121
ae4735db 1122let s_of_inc_file inc_file =
485bce71
C
1123 match inc_file with
1124 | Local xs -> xs +> Common.join "/"
1125 | NonLocal xs -> xs +> Common.join "/"
0708f913 1126 | Weird s -> s
485bce71 1127
ae4735db 1128let s_of_inc_file_bis inc_file =
485bce71
C
1129 match inc_file with
1130 | Local xs -> "\"" ^ xs +> Common.join "/" ^ "\""
1131 | NonLocal xs -> "<" ^ xs +> Common.join "/" ^ ">"
0708f913 1132 | Weird s -> s
485bce71 1133
ae4735db 1134let fieldname_of_fieldkind fieldkind =
b1b2de81 1135 match fieldkind with
485bce71 1136 | Simple (sopt, ft) -> sopt
b1b2de81 1137 | BitField (sopt, ft, info, expr) -> sopt
485bce71 1138
91eba41f 1139
ae4735db 1140let s_of_attr attr =
91eba41f
C
1141 attr
1142 +> List.map (fun (Attribute s, ii) -> s)
1143 +> Common.join ","
113803cf 1144
708f4980
C
1145
1146(* ------------------------------------------------------------------------- *)
ae4735db 1147let str_of_name ident =
b1b2de81
C
1148 match ident with
1149 | RegularName (s,ii) -> s
ae4735db 1150 | CppConcatenatedName xs ->
b1b2de81
C
1151 xs +> List.map (fun (x,iiop) -> unwrap x) +> Common.join "##"
1152 | CppVariadicName (s, ii) -> "##" ^ s
ae4735db
C
1153 | CppIdentBuilder ((s,iis), xs) ->
1154 s ^ "(" ^
b1b2de81
C
1155 (xs +> List.map (fun ((x,iix), iicomma) -> x) +> Common.join ",") ^
1156 ")"
1157
ae4735db 1158let get_s_and_ii_of_name name =
708f4980 1159 match name with
ae4735db 1160 | RegularName (s, iis) -> s, iis
708f4980 1161 | CppIdentBuilder ((s, iis), xs) -> s, iis
ae4735db 1162 | CppVariadicName (s,iis) ->
708f4980
C
1163 let (iop, iis) = Common.tuple_of_list2 iis in
1164 s, [iis]
ae4735db 1165 | CppConcatenatedName xs ->
b1b2de81
C
1166 (match xs with
1167 | [] -> raise Impossible
ae4735db 1168 | ((s,iis),noiiop)::xs ->
708f4980 1169 s, iis
b1b2de81 1170 )
b1b2de81 1171
ae4735db 1172let get_s_and_info_of_name name =
708f4980
C
1173 let (s,ii) = get_s_and_ii_of_name name in
1174 s, List.hd ii
1175
ae4735db 1176let info_of_name name =
708f4980
C
1177 let (s,ii) = get_s_and_ii_of_name name in
1178 List.hd ii
1179
ae4735db 1180let ii_of_name name =
708f4980
C
1181 let (s,ii) = get_s_and_ii_of_name name in
1182 ii
1183
ae4735db 1184let get_local_ii_of_expr_inlining_ii_of_name e =
708f4980
C
1185 let (ebis,_),ii = e in
1186 match ebis, ii with
ae4735db 1187 | Ident name, noii ->
708f4980
C
1188 assert(null noii);
1189 ii_of_name name
ae4735db 1190 | RecordAccess (e, name), ii ->
708f4980 1191 ii @ ii_of_name name
ae4735db 1192 | RecordPtAccess (e, name), ii ->
708f4980
C
1193 ii @ ii_of_name name
1194 | _, ii -> ii
1195
1196
1197let get_local_ii_of_tybis_inlining_ii_of_name ty =
1198 match ty with
1199 | TypeName (name, _typ), [] -> ii_of_name name
1200 | _, ii -> ii
1201
978fd7e5 1202(* the following is used to obtain the argument to LocalVar *)
ae4735db 1203let info_of_type ft =
978fd7e5
C
1204 let (qu, ty) = ft in
1205 (* bugfix: because of string->name, the ii can be deeper *)
1206 let ii = get_local_ii_of_tybis_inlining_ii_of_name ty in
1207 match ii with
1208 | ii::_ -> ii.pinfo
1209 | [] -> failwith "type has no text; need to think again"
1210
708f4980
C
1211(* only Label and Goto have name *)
1212let get_local_ii_of_st_inlining_ii_of_name st =
1213 match st with
1214 | Labeled (Label (name, st)), ii -> ii_of_name name @ ii
ae4735db 1215 | Jump (Goto name), ii ->
708f4980
C
1216 let (i1, i3) = Common.tuple_of_list2 ii in
1217 [i1] @ ii_of_name name @ [i3]
1218 | _, ii -> ii
1219
ae4735db 1220
708f4980
C
1221
1222(* ------------------------------------------------------------------------- *)
ae4735db 1223let name_of_parameter param =
b1b2de81
C
1224 param.p_namei +> Common.map_option (str_of_name)
1225