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