3 * Copyright (C) 2010, University of Copenhagen DIKU and INRIA.
4 * Copyright (C) 2002, 2006, 2007, 2008, 2009 Yoann Padioleau
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.
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.
17 (*****************************************************************************)
18 (* The AST C related types *)
19 (*****************************************************************************)
21 * Some stuff are tagged semantic: which means that they are computed
24 * This means that some elements in this AST are present only if
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.
30 (* ------------------------------------------------------------------------- *)
32 (* ------------------------------------------------------------------------- *)
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.
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.
39 * Update: Now I use a ref! in those 'info' so take care.
40 * That means that modifications of the info of tokens can have
41 * an effect on the info stored in the ast (which is sometimes
42 * convenient, cf unparse_c.ml or comment_annotater_c.ml)
44 * convention: I often use 'ii' for the name of a list of info.
46 * Sometimes we want to add someting at the beginning or at the end
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.
53 * cocci: Each token will be decorated in the future by the mcodekind
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).
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/....
62 * Each token will also be decorated in the future with an environment,
63 * because the pending '+' may contain metavariables that refer to some
70 type posl
= int * int (* line-col, for MetaPosValList, for position variables *)
73 (* the virtual position is set in Parsing_hacks.insert_virtual_positions *)
74 type virtual_position
= Common.parse_info
* int (* character offset *)
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
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.
90 | AbstractLineTok
of Common.parse_info
(* local to the abstracted thing *)
96 (* this cocci_tag can be changed, which is how we can express some program
97 * transformations by tagging the tokens involved in this transformation.
99 cocci_tag
: (Ast_cocci.mcodekind
* metavars_binding list
) option ref;
100 (* set in comment_annotater_c.ml *)
101 comments_tag
: comments_around
ref;
103 (* todo? token_info : sometimes useful to know what token it was *)
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
109 * follows, so in 'a,b' I will have in the list [(a,[]); (b,[','])].
111 * wrap3 is like wrap, except that I use it in case sometimes it
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.
116 and 'a wrap
= 'a
* il
117 and 'a wrap2
= 'a
* il
118 and 'a wrap3
= 'a
* il
(* * evotype*)
120 (* ------------------------------------------------------------------------- *)
122 (* ------------------------------------------------------------------------- *)
124 (* was called 'ident' before, but 'name' is I think better
125 * as concatenated strings can be used not only for identifiers and for
126 * declarators, but also for fields, for labels, etc.
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.
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 *)
142 | CppIdentBuilder
of string wrap
(* s ( ) *) *
143 ((string wrap
) wrap2 list
) (* arguments *)
146 (* ------------------------------------------------------------------------- *)
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
152 * concern. So I put '=>' to mean what we would really like. In fact
153 * what we really like is defining another fullType, expression, etc
154 * from scratch, because many stuff are just sugar.
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
158 * the grammar, you see that we can never specify const for the array
159 * himself (but we can do it for pointer) or function, we always
160 * have in the action rule of the grammar a { (nQ, FunctionType ...) }.
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
171 and fullType
= typeQualifier
* typeC
172 and typeC
= typeCbis wrap
(* todo reput wrap3 *)
175 NoType
(* for c++ only *)
176 | BaseType
of baseType
178 | Pointer
of fullType
179 | Array
of constExpression
option * fullType
180 | FunctionType
of functionType
182 | Enum
of string option * enumType
183 | StructUnion
of structUnion
* string option * structType
(* new scope *)
186 | StructUnionName
of structUnion
* string
188 | TypeName
of name
* fullType
option (* semantic: filled later *)
190 | ParenType
of fullType
(* for unparser: *)
192 (* gccext: TypeOfType below may seems useless; Why declare a
193 * __typeof__(int) x; ?
194 * When used with macros, it allows to fix a problem of C which
195 * is that type declaration can be spread around the ident. Indeed it
196 * may be difficult to have a macro such as
197 * '#define macro(type, ident) type ident;'
198 * because when you want to do a
199 * macro(char[256], x),
200 * then it will generate invalid code, but with a
201 * '#define macro(type, ident) __typeof(type) ident;'
204 | TypeOfExpr
of expression
205 | TypeOfType
of fullType
207 (* cppext: IfdefType TODO *)
209 (* -------------------------------------- *)
212 | FloatType
of floatType
217 (* stdC: type section
219 * note: char and signed char are semantically different!!
221 and intType
= CChar
(* obsolete? | CWchar *)
224 and signed
= sign
* base
225 and base
= CChar2
| CShort
| CInt
| CLong
| CLongLong
(* gccext: *)
226 and sign
= Signed
| UnSigned
228 and floatType
= CFloat
| CDouble
| CLongDouble
231 (* -------------------------------------- *)
232 and structUnion
= Struct
| Union
233 and structType
= field list
235 | DeclarationField
of field_declaration
240 | MacroDeclField
of (string * argument wrap2 list
)
241 wrap
(* optional ';'*)
244 | CppDirectiveStruct
of cpp_directive
245 | IfdefStruct
of ifdef_directive
(* * field list list *)
248 (* before unparser, I didn't have a FieldDeclList but just a Field. *)
249 and field_declaration
=
250 | FieldDeclList
of fieldkind wrap2 list
(* , *) wrap
(* ; *)
252 (* At first I thought that a bitfield could be only Signed/Unsigned.
253 * But it seems that gcc allow char i:4. C rule must say that you
254 * can cast into int so enum too, ...
257 | Simple
of name
option * fullType
258 | BitField
of name
option * fullType
*
259 info
(* : *) * constExpression
260 (* fullType => BitFieldInt | BitFieldUnsigned *)
263 (* -------------------------------------- *)
264 and enumType
= oneEnumType wrap2
(* , *) list
265 (* => string * int list *)
267 and oneEnumType
= name
* (info
(* = *) * constExpression
) option
269 (* -------------------------------------- *)
270 (* return * (params * has "...") *)
271 and functionType
= fullType
* (parameterType wrap2 list
* bool wrap
)
273 { p_namei
: name
option;
274 p_register
: bool wrap
;
277 (* => (bool (register) * fullType) list * bool *)
280 and typeQualifier
= typeQualifierbis wrap
281 and typeQualifierbis
= {const
: bool; volatile
: bool}
283 (* gccext: cppext: *)
284 and attribute
= attributebis wrap
286 | Attribute
of string
288 (* ------------------------------------------------------------------------- *)
290 (* ------------------------------------------------------------------------- *)
291 and expression
= (expressionbis
* exp_info
ref (* semantic: *)) wrap3
292 and exp_info
= exp_type
option * test
293 and exp_type
= fullType
(* Type_c.completed_and_simplified *) * local
294 and local
= LocalVar
of parse_info
| NotLocalVar
(* cocci: *)
295 and test
= Test
| NotTest
(* cocci: *)
299 (* Ident can be a enumeration constant, a simple variable, a name of a func.
300 * With cppext, Ident can also be the name of a macro. Sparse says
301 * "an identifier with a meaning is a symbol" *)
302 | Ident
of name
(* todo? more semantic info such as LocalFunc *)
304 | Constant
of constant
305 | FunCall
of expression
* argument wrap2
(* , *) list
306 (* gccext: x ? /* empty */ : y <=> x ? x : y; hence the 'option' below *)
307 | CondExpr
of expression
* expression
option * expression
309 (* should be considered as statements, bad C langage *)
310 | Sequence
of expression
* expression
311 | Assignment
of expression
* assignOp
* expression
314 | Postfix
of expression
* fixOp
315 | Infix
of expression
* fixOp
317 | Unary
of expression
* unaryOp
318 | Binary
of expression
* binaryOp
* expression
320 | ArrayAccess
of expression
* expression
322 (* field ident access *)
323 | RecordAccess
of expression
* name
324 | RecordPtAccess
of expression
* name
325 (* redundant normally, could replace it by DeRef RecordAcces *)
327 | SizeOfExpr
of expression
328 | SizeOfType
of fullType
329 | Cast
of fullType
* expression
332 | StatementExpr
of compound wrap
(* ( ) new scope *)
333 | Constructor
of fullType
* initialiser wrap2
(* , *) list
336 | ParenExpr
of expression
340 | Delete
of expression
342 (* cppext: IfdefExpr TODO *)
344 (* cppext: normally just expression *)
345 and argument
= (expression
, weird_argument
) Common.either
347 | ArgType
of parameterType
348 | ArgAction
of action_macro
350 (* todo: ArgStatement of statement, possibly have ghost token *)
354 (* I put string for Int and Float because int would not be enough because
355 * OCaml int are 31 bits. So simpler to do string. Same reason to have
356 * string instead of int list for the String case.
358 * note: -2 is not a constant, it is the unary operator '-'
359 * applied to constant 2. So the string must represent a positive
363 | String
of (string * isWchar
)
364 | MultiString
of string list
(* can contain MacroString, todo: more info *)
365 | Char
of (string * isWchar
) (* normally it is equivalent to Int *)
366 | Int
of (string * intType
)
367 | Float
of (string * floatType
)
369 and isWchar
= IsWchar
| IsChar
372 and unaryOp
= GetRef
| DeRef
| UnPlus
| UnMinus
| Tilde
| Not
373 | GetRefLabel
(* gccext: GetRefLabel, via &&label notation *)
374 and assignOp
= SimpleAssign
| OpAssign
of arithOp
375 and fixOp
= Dec
| Inc
377 and binaryOp
= Arith
of arithOp
| Logical
of logicalOp
380 | Plus
| Minus
| Mul
| Div
| Mod
385 | Inf
| Sup
| InfEq
| SupEq
389 and constExpression
= expression
(* => int *)
391 (* ------------------------------------------------------------------------- *)
393 (* ------------------------------------------------------------------------- *)
394 (* note: that assignement is not a statement but an expression;
395 * wonderful C langage.
397 * note: I use 'and' for type definition cos gccext allow statement as
398 * expression, so need mutual recursive type definition.
402 and statement
= statementbis wrap3
405 | Compound
of compound
(* new scope *)
406 | ExprStatement
of exprStatement
407 | Selection
of selection
(* have fakeend *)
408 | Iteration
of iteration
(* have fakeend *)
411 (* simplify cocci: only at the beginning of a compound normally *)
412 | Decl
of declaration
416 | NestedFunc
of definition
423 and labeled
= Label
of name
* statement
424 | Case
of expression
* statement
425 | CaseRange
of expression
* expression
* statement
(* gccext: *)
426 | Default
of statement
429 * old: compound = (declaration list * statement list)
430 * old: (declaration, statement) either list
431 * Simplify cocci to just have statement list, by integrating Decl in stmt.
433 * update: now introduce also the _sequencable to allow ifdef in the middle.
434 * Indeed, I now allow ifdefs in the ast but they must be only between
435 * "sequencable" elements. They can be put in a type only if this type
436 * is used in a list, like at the toplevel, used in a 'toplevel list',
437 * or inside a compound, used in a 'statement list'. I must not allow
438 * ifdef anywhere. For instance I can not make ifdef a statement
439 * cos some instruction like If accept only one statement and the
440 * ifdef directive must not take the place of a legitimate instruction.
441 * We had a similar phenomena in SmPL where we have the notion
442 * of statement and sequencable statement too. Once you have
443 * such a type of sequencable thing, then s/xx list/xx_sequencable list/
444 * and introduce the ifdef.
446 * update: those ifdefs are either passed, or present in the AST but in
447 * a flat form. To structure those flat ifdefs you have to run
448 * a transformation that will put in a tree the statements inside
449 * ifdefs branches. Cf cpp_ast_c.ml. This is for instance the difference
450 * between a IfdefStmt (flat) and IfdefStmt2 (tree structured).
453 and compound
= statement_sequencable list
455 (* cppext: easier to put at statement_list level than statement level *)
456 and statement_sequencable
=
457 | StmtElem
of statement
460 | CppDirectiveStmt
of cpp_directive
461 | IfdefStmt
of ifdef_directive
463 (* this will be build in cpp_ast_c from the previous flat IfdefStmt *)
464 | IfdefStmt2
of ifdef_directive list
* (statement_sequencable list
) list
466 and exprStatement
= expression
option
468 (* for Switch, need check that all elements in the compound start
469 * with a case:, otherwise unreachable code.
472 | If
of expression
* statement
* statement
473 | Switch
of expression
* statement
477 | While
of expression
* statement
478 | DoWhile
of statement
* expression
479 | For
of exprStatement wrap
* exprStatement wrap
* exprStatement wrap
*
482 | MacroIteration
of string * argument wrap2 list
* statement
484 and jump
= Goto
of name
486 | Return
| ReturnExpr
of expression
487 | GotoComputed
of expression
(* gccext: goto *exp ';' *)
491 and asmbody
= il
(* string list *) * colon wrap
(* : *) list
492 and colon
= Colon
of colon_option wrap2 list
493 and colon_option
= colon_option_bis wrap
494 and colon_option_bis
= ColonMisc
| ColonExpr
of expression
497 (* ------------------------------------------------------------------------- *)
499 (* ------------------------------------------------------------------------- *)
500 (* (string * ...) option cos can have empty declaration or struct tag
503 * Before I had a Typedef constructor, but why make this special case and not
504 * have StructDef, EnumDef, ... so that 'struct t {...} v' will generate 2
505 * declarations ? So I try to generalise and not have Typedef either. This
506 * requires more work in parsing. Better to separate concern.
508 * Before the need for unparser, I didn't have a DeclList but just a Decl.
510 * I am not sure what it means to declare a prototype inline, but gcc
515 | DeclList
of onedecl wrap2
(* , *) list wrap
(* ; fakestart sto *)
517 | MacroDecl
of (string * argument wrap2 list
) wrap
(* fakestart *)
520 { v_namei
: (name
* v_init
) option;
522 (* semantic: set in type annotated and used in cocci_vs_c
523 * when we transform some initialisation into affectation
525 v_type_bis
: fullType
(* Type_c.completed_and_simplified *) option ref;
527 v_local
: local_decl
; (* cocci: *)
528 v_attr
: attribute list
; (* gccext: *)
531 NoInit
| ValInit
of info
* initialiser
532 | ConstrInit
of argument wrap2
(* , *) list wrap
533 and storage
= storagebis
* bool (* gccext: inline or not *)
534 and storagebis
= NoSto
| StoTypedef
| Sto
of storageClass
535 and storageClass
= Auto
| Static
| Register
| Extern
537 and local_decl
= LocalDecl
| NotLocalDecl
539 (* fullType is the type used if the type should be converted to
540 an assignment. It can be adjusted in the type annotation
541 phase when typedef information is availalble *)
542 and initialiser
= initialiserbis wrap
544 | InitExpr
of expression
545 | InitList
of initialiser wrap2
(* , *) list
547 | InitDesignators
of designator list
* initialiser
548 | InitFieldOld
of string * initialiser
549 | InitIndexOld
of expression
* initialiser
551 (* ex: [2].y = x, or .y[2] or .y.x. They can be nested *)
552 and designator
= designatorbis wrap
554 | DesignatorField
of string
555 | DesignatorIndex
of expression
556 | DesignatorRange
of expression
* expression
558 (* ------------------------------------------------------------------------- *)
559 (* Function definition *)
560 (* ------------------------------------------------------------------------- *)
561 (* Normally we should define another type functionType2 because there
562 * are more restrictions on what can define a function than a pointer
563 * function. For instance a function declaration can omit the name of the
564 * parameter whereas a function definition can not. But, in some cases such
565 * as 'f(void) {', there is no name too, so I simplified and reused the
566 * same functionType type for both declaration and function definition.
568 * Also old style C does not have type in the parameter, so again simpler
569 * to abuse the functionType and allow missing type.
571 and definition
= definitionbis wrap
(* ( ) { } fakestart sto *)
574 f_type
: functionType
; (* less? a functionType2 ? *)
577 f_attr
: attribute list
; (* gccext: *)
578 f_old_c_style
: declaration list
option;
580 (* cppext: IfdefFunHeader TODO *)
582 (* ------------------------------------------------------------------------- *)
583 (* cppext: cpp directives, #ifdef, #define and #include body *)
584 (* ------------------------------------------------------------------------- *)
589 (*| Ifdef ? no, ifdefs are handled differently, cf ifdef_directive below *)
591 and define
= string wrap
(* #define s eol *) * (define_kind
* define_val
)
594 | DefineFunc
of ((string wrap
) wrap2 list
) wrap
(* () *)
597 (* most common case; e.g. to define int constant *)
598 | DefineExpr
of expression
600 | DefineStmt
of statement
601 | DefineType
of fullType
602 | DefineDoWhileZero
of (statement
* expression
) wrap
(* do { } while(0) *)
604 | DefineFunction
of definition
605 | DefineInit
of initialiser
(* in practice only { } with possible ',' *)
607 (* TODO DefineMulti of define_val list *)
609 | DefineText
of string wrap
617 { i_include
: inc_file wrap
; (* #include s *)
618 (* cocci: computed in ? *)
619 i_rel_pos
: include_rel_pos
option ref;
620 (* cocci: cf -test incl *)
622 (* cf cpp_ast_c.ml. set to None at parsing time. *)
623 i_content
: (Common.filename
(* full path *) * program
) option;
626 | Local
of inc_elem list
627 | NonLocal
of inc_elem list
628 | Weird
of string (* ex: #include SYSTEM_H *)
629 and inc_elem
= string
631 (* cocci: to tag the first of #include <xx/> and last of #include <yy/>
633 * The first_of and last_of store the list of prefixes that was
634 * introduced by the include. On #include <a/b/x>, if the include was
635 * the first in the file, it would give in first_of the following
636 * prefixes a/b/c; a/b/; a/ ; <empty>
638 * This is set after parsing, in cocci.ml, in update_rel_pos.
640 and include_rel_pos
= {
641 first_of
: string list list
;
642 last_of
: string list list
;
647 (* todo? to specialize if someone need more info *)
648 and ifdef_directive
= (* or and 'a ifdefed = 'a list wrap *)
649 | IfdefDirective
of (ifdefkind
* matching_tag
) wrap
651 | Ifdef
(* todo? of string ? of formula_cpp ? *)
652 | IfdefElseif
(* same *)
653 | IfdefElse
(* same *)
655 (* set in Parsing_hacks.set_ifdef_parenthize_info. It internally use
656 * a global so it means if you parse the same file twice you may get
657 * different id. I try now to avoid this pb by resetting it each
658 * time I parse a file.
661 IfdefTag
of (int (* tag *) * int (* total with this tag *))
667 (* ------------------------------------------------------------------------- *)
668 (* The toplevels elements *)
669 (* ------------------------------------------------------------------------- *)
671 | Declaration
of declaration
672 | Definition
of definition
675 | CppTop
of cpp_directive
676 | IfdefTop
of ifdef_directive
(* * toplevel list *)
679 | MacroTop
of string * argument wrap2 list
* il
681 | EmptyDef
of il
(* gccext: allow redundant ';' *)
682 | NotParsedCorrectly
of il
684 | FinalDef
of info
(* EOF *)
686 (* ------------------------------------------------------------------------- *)
687 and program
= toplevel list
689 (*****************************************************************************)
691 (*****************************************************************************)
692 (* Was previously in pattern.ml, but because of the transformer,
693 * we need to decorate each token with some cocci code AND the environment
694 * for this cocci code.
696 and metavars_binding
= (Ast_cocci.meta_name
, metavar_binding_kind
) assoc
697 and metavar_binding_kind
=
698 | MetaIdVal
of string *
699 Ast_cocci.meta_name list
(* negative constraints *)
700 | MetaFuncVal
of string
701 | MetaLocalFuncVal
of string
703 | MetaExprVal
of expression
(* a "clean expr" *) *
704 (*subterm constraints, currently exprs*)
705 Ast_cocci.meta_name list
706 | MetaExprListVal
of argument wrap2 list
707 | MetaParamVal
of parameterType
708 | MetaParamListVal
of parameterType wrap2 list
710 | MetaTypeVal
of fullType
711 | MetaInitVal
of initialiser
712 | MetaDeclVal
of declaration
713 | MetaFieldVal
of field
714 | MetaStmtVal
of statement
716 (* Could also be in Lib_engine.metavars_binding2 with the ParenVal,
717 * because don't need to have the value for a position in the env of
718 * a '+'. But ParenVal or LabelVal are used only by CTL, they are not
719 * variables accessible via SmPL whereas the position can be one day
720 * so I think it's better to put MetaPosVal here *)
721 | MetaPosVal
of (Ast_cocci.fixpos
* Ast_cocci.fixpos
) (* max, min *)
723 (Common.filename
* string (*element*) * posl
* posl
) list
(* min, max *)
724 | MetaListlenVal
of int
727 (*****************************************************************************)
729 (*****************************************************************************)
731 (* convention: I often use "m" for comments as I can not use "c"
732 * (already use for c stuff) and "com" is too long.
735 (* this type will be associated to each token.
737 and comments_around
= {
738 mbefore
: Token_c.comment_like_token list
;
739 mafter
: Token_c.comment_like_token list
;
741 (* less: could remove ? do something simpler than CComment for
742 * coccinelle, cf above. *)
743 mbefore2
: comment_and_relative_pos list
;
744 mafter2
: comment_and_relative_pos list
;
746 and comment_and_relative_pos
= {
748 minfo
: Common.parse_info
;
749 (* the int represent the number of lines of difference between the
750 * current token and the comment. When on same line, this number is 0.
751 * When previous line, -1. In some way the after/before in previous
752 * record is useless because the sign of the integer can helps
753 * do the difference too, but I keep it that way.
757 * cppbetween: bool; touse? if false positive
758 * is_alone_in_line: bool; (*for labels, to avoid false positive*)
762 and comment
= Common.parse_info
763 and com
= comment list
ref
768 (*****************************************************************************)
769 (* Some constructors *)
770 (*****************************************************************************)
771 let nullQualif = ({const
=false; volatile
= false}, [])
774 let defaultInt = (BaseType
(IntType
(Si
(Signed
, CInt
))))
776 let noType () = ref (None
,NotTest
)
777 let noInstr = (ExprStatement
(None
), [])
778 let noTypedefDef () = None
780 let emptyMetavarsBinding =
781 ([]: metavars_binding
)
783 let emptyAnnotCocci =
784 (Ast_cocci.CONTEXT
(Ast_cocci.NoPos
,Ast_cocci.NOTHING
),
785 ([] : metavars_binding list
))
788 (None
: (Ast_cocci.mcodekind
* metavars_binding list
) option)
790 (* compatibility mode *)
791 let mcode_and_env_of_cocciref aref
=
794 | None
-> emptyAnnotCocci
805 (* for include, some meta information needed by cocci *)
807 ref (None
: include_rel_pos
option)
812 (* When want add some info in ast that does not correspond to
813 * an existing C element.
814 * old: or when don't want 'synchronize' on it in unparse_c.ml
815 * (now have other mark for tha matter).
817 let no_virt_pos = ({str
="";charpos
=0;line
=0;column
=0;file
=""},-1)
820 { pinfo
= FakeTok
("",no_virt_pos);
821 cocci_tag
= ref emptyAnnot;
822 comments_tag
= ref emptyComments;
827 let noi_content = (None
: ((Common.filename
* program
) option))
829 (*****************************************************************************)
831 (*****************************************************************************)
836 let unwrap_expr ((unwrap_e
, typ
), iie
) = unwrap_e
837 let rewrap_expr ((_old_unwrap_e
, typ
), iie
) newe
= ((newe
, typ
), iie
)
839 let unwrap_typeC (qu
, (typeC
, ii
)) = typeC
840 let rewrap_typeC (qu
, (typeC
, ii
)) newtypeC
= (qu
, (newtypeC
, ii
))
842 let unwrap_typeCbis (typeC
, ii
) = typeC
844 let unwrap_st (unwrap_st, ii
) = unwrap_st
846 (* ------------------------------------------------------------------------- *)
847 let mk_e unwrap_e ii
= (unwrap_e
, noType()), ii
848 let mk_e_bis unwrap_e ty ii
= (unwrap_e
, ty
), ii
850 let mk_ty typeC ii
= nQ, (typeC
, ii
)
851 let mk_tybis typeC ii
= (typeC
, ii
)
853 let mk_st unwrap_st ii
= (unwrap_st, ii
)
855 (* ------------------------------------------------------------------------- *)
856 let get_ii_typeC_take_care (typeC
, ii
) = ii
857 let get_ii_st_take_care (st
, ii
) = ii
858 let get_ii_expr_take_care (e
, ii
) = ii
860 let get_st_and_ii (st
, ii
) = st
, ii
861 let get_ty_and_ii (qu
, (typeC
, ii
)) = qu
, (typeC
, ii
)
862 let get_e_and_ii (e
, ii
) = e
, ii
865 (* ------------------------------------------------------------------------- *)
866 let get_type_expr ((unwrap_e
, typ
), iie
) = !typ
867 let set_type_expr ((unwrap_e
, oldtyp
), iie
) newtyp
=
869 (* old: (unwrap_e, newtyp), iie *)
871 let get_onlytype_expr ((unwrap_e
, typ
), iie
) =
873 | Some
(ft
,_local
), _test
-> Some ft
876 let get_onlylocal_expr ((unwrap_e
, typ
), iie
) =
878 | Some
(ft
,local
), _test
-> Some local
881 (* ------------------------------------------------------------------------- *)
882 let rewrap_str s ii
=
885 OriginTok pi
-> OriginTok
{ pi
with Common.str
= s
;}
886 | ExpandedTok
(pi
,vpi
) -> ExpandedTok
({ pi
with Common.str
= s
;},vpi
)
887 | FakeTok
(_
,vpi
) -> FakeTok
(s
,vpi
)
888 | AbstractLineTok pi
-> OriginTok
{ pi
with Common.str
= s
;})}
890 let rewrap_pinfo pi ii
=
895 (* info about the current location *)
896 let get_pi = function
898 | ExpandedTok
(_
,(pi
,_
)) -> pi
899 | FakeTok
(_
,(pi
,_
)) -> pi
900 | AbstractLineTok pi
-> pi
903 let get_opi = function
905 | ExpandedTok
(pi
,_
) -> pi
(* diff with get_pi *)
906 | FakeTok
(_
,_
) -> failwith
"no position information"
907 | AbstractLineTok pi
-> pi
911 OriginTok pi
-> pi
.Common.str
912 | ExpandedTok
(pi
,_
) -> pi
.Common.str
914 | AbstractLineTok pi
-> pi
.Common.str
919 | ExpandedTok
(_
,(pi
,_
)) -> f pi
920 | FakeTok
(_
,(pi
,_
)) -> f pi
921 | AbstractLineTok pi
-> f pi
923 let get_orig_info f ii
=
926 | ExpandedTok
(pi
,_
) -> f pi
(* diff with get_info *)
927 | FakeTok
(_
,(pi
,_
)) -> f pi
928 | AbstractLineTok pi
-> f pi
930 let make_expanded ii
=
931 {ii
with pinfo
= ExpandedTok
(get_opi ii
.pinfo
,no_virt_pos)}
933 let pos_of_info ii
= get_info (function x
-> x
.Common.charpos
) ii
934 let opos_of_info ii
= get_orig_info (function x
-> x
.Common.charpos
) ii
935 let line_of_info ii
= get_orig_info (function x
-> x
.Common.line
) ii
936 let col_of_info ii
= get_orig_info (function x
-> x
.Common.column
) ii
937 let file_of_info ii
= get_orig_info (function x
-> x
.Common.file
) ii
938 let mcode_of_info ii
= fst
(mcode_and_env_of_cocciref ii
.cocci_tag
)
939 let pinfo_of_info ii
= ii
.pinfo
940 let parse_info_of_info ii
= get_pi ii
.pinfo
942 let strloc_of_info ii
=
943 spf
"%s:%d" (file_of_info ii
) (line_of_info ii
)
947 FakeTok
(_
,_
) -> true
950 let is_origintok ii
=
952 | OriginTok pi
-> true
955 (* ------------------------------------------------------------------------- *)
956 type posrv
= Real
of Common.parse_info
| Virt
of virtual_position
958 let compare_pos ii1 ii2
=
959 let get_pos = function
960 OriginTok pi
-> Real pi
961 | FakeTok
(s
,vpi
) -> Virt vpi
962 | ExpandedTok
(pi
,vpi
) -> Virt vpi
963 | AbstractLineTok pi
-> Real pi
in (* used for printing *)
964 let pos1 = get_pos (pinfo_of_info ii1
) in
965 let pos2 = get_pos (pinfo_of_info ii2
) in
966 match (pos1,pos2) with
967 (Real p1
, Real p2
) ->
968 compare p1
.Common.charpos p2
.Common.charpos
969 | (Virt
(p1
,_
), Real p2
) ->
970 if (compare p1
.Common.charpos p2
.Common.charpos
) =|= (-1) then (-1) else 1
971 | (Real p1
, Virt
(p2
,_
)) ->
972 if (compare p1
.Common.charpos p2
.Common.charpos
) =|= 1 then 1 else (-1)
973 | (Virt
(p1
,o1
), Virt
(p2
,o2
)) ->
974 let poi1 = p1
.Common.charpos
in
975 let poi2 = p2
.Common.charpos
in
976 match compare
poi1 poi2 with
981 let equal_posl (l1
,c1
) (l2
,c2
) =
982 (l1
=|= l2
) && (c1
=|= c2
)
984 let info_to_fixpos ii
=
985 match pinfo_of_info ii
with
986 OriginTok pi
-> Ast_cocci.Real pi
.Common.charpos
987 | ExpandedTok
(_
,(pi
,offset
)) ->
988 Ast_cocci.Virt
(pi
.Common.charpos
,offset
)
989 | FakeTok
(_
,(pi
,offset
)) ->
990 Ast_cocci.Virt
(pi
.Common.charpos
,offset
)
991 | AbstractLineTok pi
-> failwith
"unexpected abstract"
994 let is_test (e
: expression
) =
995 let (_
,info
), _
= e
in
996 let (_
,test
) = !info
in
999 (*****************************************************************************)
1001 (*****************************************************************************)
1003 (* When we have extended the C Ast to add some info to the tokens,
1004 * such as its line number in the file, we can not use anymore the
1005 * ocaml '=' to compare Ast elements. To overcome this problem, to be
1006 * able to use again '=', we just have to get rid of all those extra
1007 * information, to "abstract those line" (al) information.
1009 * Julia then modifies it a little to have a tokenindex, so the original
1010 * true al_info is in fact real_al_info.
1013 let al_info tokenindex x
=
1016 {charpos
= tokenindex
;
1018 column
= tokenindex
;
1020 str
= str_of_info x
});
1021 cocci_tag
= ref emptyAnnot;
1022 comments_tag
= ref emptyComments;
1025 let semi_al_info x
=
1027 cocci_tag
= ref emptyAnnot;
1028 comments_tag
= ref emptyComments;
1031 let magic_real_number = -10
1033 let real_al_info x
=
1036 {charpos
= magic_real_number;
1037 line
= magic_real_number;
1038 column
= magic_real_number;
1040 str
= str_of_info x
});
1041 cocci_tag
= ref emptyAnnot;
1042 comments_tag
= ref emptyComments;
1047 List.filter
(function (Token_c.TCommentCpp _
,_
) -> true | _
-> false) l
in
1049 (x
,{i
with Common.charpos
= magic_real_number;
1050 Common.line
= magic_real_number;
1051 Common.column
= magic_real_number}) in
1052 {mbefore
= []; (* duplicates mafter of the previous token *)
1053 mafter
= List.map
al_com (keep_cpp x
.mafter
);
1058 let al_info_cpp tokenindex x
=
1061 {charpos
= tokenindex
;
1063 column
= tokenindex
;
1065 str
= str_of_info x
});
1066 cocci_tag
= ref emptyAnnot;
1067 comments_tag
= ref (al_comments !(x
.comments_tag
));
1070 let semi_al_info_cpp x
=
1072 cocci_tag
= ref emptyAnnot;
1073 comments_tag
= ref (al_comments !(x
.comments_tag
));
1076 let real_al_info_cpp x
=
1079 {charpos
= magic_real_number;
1080 line
= magic_real_number;
1081 column
= magic_real_number;
1083 str
= str_of_info x
});
1084 cocci_tag
= ref emptyAnnot;
1085 comments_tag
= ref (al_comments !(x
.comments_tag
));
1089 (*****************************************************************************)
1091 (*****************************************************************************)
1093 (* Transform a list of arguments (or parameters) where the commas are
1094 * represented via the wrap2 and associated with an element, with
1095 * a list where the comma are on their own. f(1,2,2) was
1096 * [(1,[]); (2,[,]); (2,[,])] and become [1;',';2;',';2].
1098 * Used in cocci_vs_c.ml, to have a more direct correspondance between
1099 * the ast_cocci of julia and ast_c.
1101 let rec (split_comma
: 'a wrap2 list
-> ('a
, il
) either list
) =
1106 then (Left e
)::split_comma xs
1107 else Right ii
::Left e
::split_comma xs
1109 let rec (unsplit_comma
: ('a
, il
) either list
-> 'a wrap2 list
) =
1112 | Right ii
::Left e
::xs
->
1113 (e
, ii
)::unsplit_comma xs
1115 let empty_ii = [] in
1116 (e
, empty_ii)::unsplit_comma xs
1123 (*****************************************************************************)
1124 (* Helpers, could also be put in lib_parsing_c.ml instead *)
1125 (*****************************************************************************)
1127 (* should maybe be in pretty_print_c ? *)
1129 let s_of_inc_file inc_file
=
1131 | Local xs
-> xs
+> Common.join
"/"
1132 | NonLocal xs
-> xs
+> Common.join
"/"
1135 let s_of_inc_file_bis inc_file
=
1137 | Local xs
-> "\"" ^ xs
+> Common.join
"/" ^
"\""
1138 | NonLocal xs
-> "<" ^ xs
+> Common.join
"/" ^
">"
1141 let fieldname_of_fieldkind fieldkind
=
1142 match fieldkind
with
1143 | Simple
(sopt
, ft
) -> sopt
1144 | BitField
(sopt
, ft
, info
, expr
) -> sopt
1147 let s_of_attr attr
=
1149 +> List.map
(fun (Attribute s
, ii
) -> s
)
1153 (* ------------------------------------------------------------------------- *)
1154 let str_of_name ident
=
1156 | RegularName
(s
,ii
) -> s
1157 | CppConcatenatedName xs
->
1158 xs
+> List.map
(fun (x
,iiop
) -> unwrap x
) +> Common.join
"##"
1159 | CppVariadicName
(s
, ii
) -> "##" ^ s
1160 | CppIdentBuilder
((s
,iis
), xs
) ->
1162 (xs
+> List.map
(fun ((x
,iix
), iicomma
) -> x
) +> Common.join
",") ^
1165 let get_s_and_ii_of_name name
=
1167 | RegularName
(s
, iis
) -> s
, iis
1168 | CppIdentBuilder
((s
, iis
), xs
) -> s
, iis
1169 | CppVariadicName
(s
,iis
) ->
1170 let (iop
, iis
) = Common.tuple_of_list2 iis
in
1172 | CppConcatenatedName xs
->
1174 | [] -> raise Impossible
1175 | ((s
,iis
),noiiop
)::xs
->
1179 let get_s_and_info_of_name name
=
1180 let (s
,ii
) = get_s_and_ii_of_name name
in
1183 let info_of_name name
=
1184 let (s
,ii
) = get_s_and_ii_of_name name
in
1187 let ii_of_name name
=
1188 let (s
,ii
) = get_s_and_ii_of_name name
in
1191 let get_local_ii_of_expr_inlining_ii_of_name e
=
1192 let (ebis
,_
),ii
= e
in
1194 | Ident name
, noii ->
1197 | RecordAccess
(e
, name
), ii
->
1198 ii
@ ii_of_name name
1199 | RecordPtAccess
(e
, name
), ii
->
1200 ii
@ ii_of_name name
1204 let get_local_ii_of_tybis_inlining_ii_of_name ty
=
1206 | TypeName
(name
, _typ
), [] -> ii_of_name name
1209 (* the following is used to obtain the argument to LocalVar *)
1210 let info_of_type ft
=
1211 let (qu
, ty
) = ft
in
1212 (* bugfix: because of string->name, the ii can be deeper *)
1213 let ii = get_local_ii_of_tybis_inlining_ii_of_name ty
in
1216 | [] -> failwith
"type has no text; need to think again"
1218 (* only Label and Goto have name *)
1219 let get_local_ii_of_st_inlining_ii_of_name st
=
1221 | Labeled
(Label
(name
, st
)), ii -> ii_of_name name
@ ii
1222 | Jump
(Goto name
), ii ->
1223 let (i1
, i3
) = Common.tuple_of_list2
ii in
1224 [i1
] @ ii_of_name name
@ [i3
]
1229 (* ------------------------------------------------------------------------- *)
1230 let name_of_parameter param
=
1231 param
.p_namei
+> Common.map_option
(str_of_name)