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