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