Accept -Wno-all, -Wno-extra and -Wno-unused like GCC.
[cparser] / ast_t.h
1 #ifndef AST_T_H
2 #define AST_T_H
3
4 #include <libfirm/firm_types.h>
5 #include <assert.h>
6
7 #include "ast.h"
8 #include "symbol.h"
9 #include "token_t.h"
10 #include "type.h"
11 #include "adt/obst.h"
12
13 extern struct obstack ast_obstack;
14
15 typedef enum {
16         EXPR_UNKNOWN = 0,
17         EXPR_INVALID,
18         EXPR_REFERENCE,
19         EXPR_CONST,
20         EXPR_STRING_LITERAL,
21         EXPR_WIDE_STRING_LITERAL,
22         EXPR_CALL,
23         EXPR_CONDITIONAL,
24         EXPR_SELECT,
25         EXPR_ARRAY_ACCESS,
26         EXPR_SIZEOF,
27         EXPR_CLASSIFY_TYPE,
28         EXPR_ALIGNOF,
29
30         EXPR_FUNCTION,
31         EXPR_PRETTY_FUNCTION,
32         EXPR_BUILTIN_SYMBOL,
33         EXPR_BUILTIN_CONSTANT_P,
34         EXPR_BUILTIN_PREFETCH,
35         EXPR_OFFSETOF,
36         EXPR_VA_START,
37         EXPR_VA_ARG,
38         EXPR_STATEMENT,
39
40         EXPR_UNARY_FIRST,
41         EXPR_UNARY_NEGATE = EXPR_UNARY_FIRST,
42         EXPR_UNARY_PLUS,
43         EXPR_UNARY_BITWISE_NEGATE,
44         EXPR_UNARY_NOT,
45         EXPR_UNARY_DEREFERENCE,
46         EXPR_UNARY_TAKE_ADDRESS,
47         EXPR_UNARY_POSTFIX_INCREMENT,
48         EXPR_UNARY_POSTFIX_DECREMENT,
49         EXPR_UNARY_PREFIX_INCREMENT,
50         EXPR_UNARY_PREFIX_DECREMENT,
51         EXPR_UNARY_CAST,
52         EXPR_UNARY_CAST_IMPLICIT, /**< compiler generated cast */
53         EXPR_UNARY_ASSUME,        /**< MS __assume() */
54         EXPR_UNARY_BITFIELD_EXTRACT,
55         EXPR_UNARY_LAST = EXPR_UNARY_BITFIELD_EXTRACT,
56
57         EXPR_BINARY_FIRST,
58         EXPR_BINARY_ADD = EXPR_BINARY_FIRST,
59         EXPR_BINARY_SUB,
60         EXPR_BINARY_MUL,
61         EXPR_BINARY_DIV,
62         EXPR_BINARY_MOD,
63         EXPR_BINARY_EQUAL,
64         EXPR_BINARY_NOTEQUAL,
65         EXPR_BINARY_LESS,
66         EXPR_BINARY_LESSEQUAL,
67         EXPR_BINARY_GREATER,
68         EXPR_BINARY_GREATEREQUAL,
69         EXPR_BINARY_BITWISE_AND,
70         EXPR_BINARY_BITWISE_OR,
71         EXPR_BINARY_BITWISE_XOR,
72         EXPR_BINARY_LOGICAL_AND,
73         EXPR_BINARY_LOGICAL_OR,
74         EXPR_BINARY_SHIFTLEFT,
75         EXPR_BINARY_SHIFTRIGHT,
76         EXPR_BINARY_ASSIGN,
77         EXPR_BINARY_MUL_ASSIGN,
78         EXPR_BINARY_DIV_ASSIGN,
79         EXPR_BINARY_MOD_ASSIGN,
80         EXPR_BINARY_ADD_ASSIGN,
81         EXPR_BINARY_SUB_ASSIGN,
82         EXPR_BINARY_SHIFTLEFT_ASSIGN,
83         EXPR_BINARY_SHIFTRIGHT_ASSIGN,
84         EXPR_BINARY_BITWISE_AND_ASSIGN,
85         EXPR_BINARY_BITWISE_XOR_ASSIGN,
86         EXPR_BINARY_BITWISE_OR_ASSIGN,
87         EXPR_BINARY_COMMA,
88
89         EXPR_BINARY_BUILTIN_EXPECT,
90         EXPR_BINARY_ISGREATER,
91         EXPR_BINARY_ISGREATEREQUAL,
92         EXPR_BINARY_ISLESS,
93         EXPR_BINARY_ISLESSEQUAL,
94         EXPR_BINARY_ISLESSGREATER,
95         EXPR_BINARY_ISUNORDERED,
96         EXPR_BINARY_LAST = EXPR_BINARY_ISUNORDERED,
97 } expression_kind_t;
98
99 /* convenience macros */
100 #define EXPR_BINARY_CASES                  \
101         case EXPR_BINARY_ADD:                  \
102         case EXPR_BINARY_SUB:                  \
103         case EXPR_BINARY_MUL:                  \
104         case EXPR_BINARY_DIV:                  \
105         case EXPR_BINARY_MOD:                  \
106         case EXPR_BINARY_EQUAL:                \
107         case EXPR_BINARY_NOTEQUAL:             \
108         case EXPR_BINARY_LESS:                 \
109         case EXPR_BINARY_LESSEQUAL:            \
110         case EXPR_BINARY_GREATER:              \
111         case EXPR_BINARY_GREATEREQUAL:         \
112         case EXPR_BINARY_BITWISE_AND:          \
113         case EXPR_BINARY_BITWISE_OR:           \
114         case EXPR_BINARY_BITWISE_XOR:          \
115         case EXPR_BINARY_LOGICAL_AND:          \
116         case EXPR_BINARY_LOGICAL_OR:           \
117         case EXPR_BINARY_SHIFTLEFT:            \
118         case EXPR_BINARY_SHIFTRIGHT:           \
119         case EXPR_BINARY_ASSIGN:               \
120         case EXPR_BINARY_MUL_ASSIGN:           \
121         case EXPR_BINARY_DIV_ASSIGN:           \
122         case EXPR_BINARY_MOD_ASSIGN:           \
123         case EXPR_BINARY_ADD_ASSIGN:           \
124         case EXPR_BINARY_SUB_ASSIGN:           \
125         case EXPR_BINARY_SHIFTLEFT_ASSIGN:     \
126         case EXPR_BINARY_SHIFTRIGHT_ASSIGN:    \
127         case EXPR_BINARY_BITWISE_AND_ASSIGN:   \
128         case EXPR_BINARY_BITWISE_XOR_ASSIGN:   \
129         case EXPR_BINARY_BITWISE_OR_ASSIGN:    \
130         case EXPR_BINARY_COMMA:                \
131         case EXPR_BINARY_BUILTIN_EXPECT:       \
132         case EXPR_BINARY_ISGREATER:            \
133         case EXPR_BINARY_ISGREATEREQUAL:       \
134         case EXPR_BINARY_ISLESS:               \
135         case EXPR_BINARY_ISLESSEQUAL:          \
136         case EXPR_BINARY_ISLESSGREATER:        \
137         case EXPR_BINARY_ISUNORDERED:
138
139 #define EXPR_UNARY_CASES                   \
140         case EXPR_UNARY_NEGATE:                \
141         case EXPR_UNARY_PLUS:                  \
142         case EXPR_UNARY_BITWISE_NEGATE:        \
143         case EXPR_UNARY_NOT:                   \
144         case EXPR_UNARY_DEREFERENCE:           \
145         case EXPR_UNARY_TAKE_ADDRESS:          \
146         case EXPR_UNARY_POSTFIX_INCREMENT:     \
147         case EXPR_UNARY_POSTFIX_DECREMENT:     \
148         case EXPR_UNARY_PREFIX_INCREMENT:      \
149         case EXPR_UNARY_PREFIX_DECREMENT:      \
150         case EXPR_UNARY_CAST:                  \
151         case EXPR_UNARY_CAST_IMPLICIT:         \
152         case EXPR_UNARY_ASSUME:                \
153         case EXPR_UNARY_BITFIELD_EXTRACT:
154
155 struct scope_t {
156         declaration_t *declarations;  /**< List of declarations in this scope. */
157 };
158
159 struct expression_base_t {
160         expression_kind_t   kind;
161         type_t             *datatype;
162         source_position_t   source_position;
163 };
164
165 struct const_expression_t {
166         expression_base_t  expression;
167         union {
168                 long long   int_value;
169                 long double float_value;
170         } v;
171 };
172
173 struct string_literal_expression_t {
174         expression_base_t  expression;
175         string_t           value;
176 };
177
178 struct wide_string_literal_expression_t {
179         expression_base_t  expression;
180         wide_string_t      value;
181 };
182
183 struct builtin_symbol_expression_t {
184         expression_base_t  expression;
185         symbol_t          *symbol;
186 };
187
188 struct builtin_constant_expression_t {
189         expression_base_t  expression;
190         expression_t      *value;
191 };
192
193 struct builtin_prefetch_expression_t {
194         expression_base_t  expression;
195         expression_t      *adr;
196         expression_t      *rw;
197         expression_t      *locality;
198 };
199
200 struct reference_expression_t {
201         expression_base_t  expression;
202         symbol_t          *symbol;
203         declaration_t     *declaration;
204 };
205
206 struct call_argument_t {
207         expression_t    *expression;
208         call_argument_t *next;
209 };
210
211 struct call_expression_t {
212         expression_base_t  expression;
213         expression_t      *function;
214         call_argument_t   *arguments;
215 };
216
217 struct unary_expression_t {
218         expression_base_t  expression;
219         expression_t      *value;
220 };
221
222 struct binary_expression_t {
223         expression_base_t  expression;
224         expression_t      *left;
225         expression_t      *right;
226 };
227
228 struct select_expression_t {
229         expression_base_t  expression;
230         expression_t      *compound;
231         symbol_t          *symbol;
232
233         declaration_t     *compound_entry;
234 };
235
236 struct array_access_expression_t {
237         expression_base_t  expression;
238         expression_t      *array_ref;
239         expression_t      *index;
240         bool               flipped; /* index/ref was written in a 5[a] way */
241 };
242
243 struct sizeof_expression_t {
244         expression_base_t  expression;
245         type_t            *type;
246         expression_t      *size_expression;
247 };
248
249 struct alignof_expression_t {
250         expression_base_t  expression;
251         type_t            *type;
252 };
253
254 struct designator_t {
255         symbol_t     *symbol;
256         expression_t *array_access;
257         designator_t *next;
258 };
259
260 struct offsetof_expression_t {
261         expression_base_t  expression;
262         type_t            *type;
263         designator_t      *designator;
264 };
265
266 struct va_start_expression_t {
267         expression_base_t  expression;
268         expression_t      *ap;
269         declaration_t     *parameter;
270 };
271
272 struct va_arg_expression_t {
273         expression_base_t  expression;
274         expression_t      *ap;
275 };
276
277 struct conditional_expression_t {
278         expression_base_t  expression;
279         expression_t      *condition;
280         expression_t      *true_expression;
281         expression_t      *false_expression;
282 };
283
284 struct statement_expression_t {
285         expression_base_t  expression;
286         statement_t       *statement;
287 };
288
289 struct classify_type_expression_t {
290         expression_base_t  expression;
291         expression_t      *type_expression;
292 };
293
294 union expression_t {
295         expression_kind_t                kind;
296         expression_base_t                base;
297         const_expression_t               conste;
298         string_literal_expression_t      string;
299         wide_string_literal_expression_t wide_string;
300         builtin_symbol_expression_t      builtin_symbol;
301         builtin_constant_expression_t    builtin_constant;
302         builtin_prefetch_expression_t    builtin_prefetch;
303         reference_expression_t           reference;
304         call_expression_t                call;
305         unary_expression_t               unary;
306         binary_expression_t              binary;
307         select_expression_t              select;
308         array_access_expression_t        array_access;
309         sizeof_expression_t              sizeofe;
310         offsetof_expression_t            offsetofe;
311         va_start_expression_t            va_starte;
312         va_arg_expression_t              va_arge;
313         conditional_expression_t         conditional;
314         statement_expression_t           statement;
315         classify_type_expression_t       classify_type;
316         alignof_expression_t             alignofe;
317 };
318
319 typedef enum {
320         STORAGE_CLASS_NONE,
321         STORAGE_CLASS_TYPEDEF,
322         STORAGE_CLASS_EXTERN,
323         STORAGE_CLASS_STATIC,
324         STORAGE_CLASS_AUTO,
325         STORAGE_CLASS_REGISTER,
326         STORAGE_CLASS_ENUM_ENTRY,
327         STORAGE_CLASS_THREAD,
328         STORAGE_CLASS_THREAD_EXTERN,
329         STORAGE_CLASS_THREAD_STATIC
330 } storage_class_tag_t;
331
332 typedef enum {
333         NAMESPACE_NORMAL,
334         NAMESPACE_STRUCT,
335         NAMESPACE_UNION,
336         NAMESPACE_ENUM,
337         NAMESPACE_LABEL,
338 } namespace_t;
339
340 typedef enum {
341         INITIALIZER_VALUE,
342         INITIALIZER_LIST,
343         INITIALIZER_STRING,
344         INITIALIZER_WIDE_STRING
345 } initializer_kind_t;
346
347 struct initializer_base_t {
348         initializer_kind_t kind;
349 };
350
351 struct initializer_value_t {
352         initializer_base_t  initializer;
353         expression_t       *value;
354 };
355
356 struct initializer_list_t {
357         initializer_base_t  initializer;
358         size_t              len;
359         initializer_t      *initializers[];
360 };
361
362 struct initializer_string_t {
363         initializer_base_t initializer;
364         string_t           string;
365 };
366
367 struct initializer_wide_string_t {
368         initializer_base_t  initializer;
369         wide_string_t       string;
370 };
371
372 union initializer_t {
373         initializer_kind_t        kind;
374         initializer_base_t        base;
375         initializer_value_t       value;
376         initializer_list_t        list;
377         initializer_string_t      string;
378         initializer_wide_string_t wide_string;
379 };
380
381 typedef enum {
382         DM_DLLIMPORT   = (1 << 0),
383         DM_DLLEXPORT   = (1 << 1),
384         DM_THREAD      = (1 << 2),
385         DM_NAKED       = (1 << 3),
386         DM_FORCEINLINE = (1 << 4),
387         DM_NOTHROW     = (1 << 5),
388         DM_NORETURN    = (1 << 6),
389         DM_NOINLINE    = (1 << 7)
390 } decl_modifier_t;
391
392 typedef unsigned short decl_modifiers_t;
393
394 struct declaration_t {
395         unsigned char       namespc;
396         unsigned char       storage_class;
397         decl_modifiers_t    modifiers;
398         unsigned int        address_taken : 1;
399         unsigned int        is_inline     : 1;
400         unsigned int        used          : 1;  /**< Set if the declaration is used. */
401         type_t             *type;
402         symbol_t           *symbol;
403         source_position_t   source_position;
404         union {
405                 bool            is_defined;
406                 statement_t    *statement;
407                 initializer_t  *initializer;
408                 expression_t   *enum_value;
409         } init;
410         scope_t             scope;
411         scope_t            *parent_scope;
412
413         /** next declaration in a scope */
414         declaration_t      *next;
415         /** next declaration with same symbol */
416         declaration_t      *symbol_next;
417         /** next variable/parameter in function scope/global scope */
418         declaration_t      *next_var;
419
420         /* the following fields are used in ast2firm module */
421         unsigned char       declaration_kind;
422         union {
423                 unsigned int    value_number;
424                 ir_entity      *entity;
425                 ir_node        *block;
426                 tarval         *enum_val;
427         } v;
428 };
429
430 typedef enum {
431         STATEMENT_INVALID,
432         STATEMENT_COMPOUND,
433         STATEMENT_RETURN,
434         STATEMENT_DECLARATION,
435         STATEMENT_IF,
436         STATEMENT_SWITCH,
437         STATEMENT_EXPRESSION,
438         STATEMENT_CONTINUE,
439         STATEMENT_BREAK,
440         STATEMENT_GOTO,
441         STATEMENT_LABEL,
442         STATEMENT_CASE_LABEL,
443         STATEMENT_WHILE,
444         STATEMENT_DO_WHILE,
445         STATEMENT_FOR,
446         STATEMENT_ASM
447 } statement_kind_t;
448
449 struct statement_base_t {
450         statement_kind_t   kind;
451         statement_t       *next;
452         source_position_t  source_position;
453 };
454
455 struct return_statement_t {
456         statement_base_t  statement;
457         expression_t     *return_value;
458 };
459
460 struct compound_statement_t {
461         statement_base_t  statement;
462         statement_t      *statements;
463         scope_t           scope;
464 };
465
466 struct declaration_statement_t {
467         statement_base_t  statement;
468         declaration_t    *declarations_begin;
469         declaration_t    *declarations_end;
470 };
471
472 struct if_statement_t {
473         statement_base_t  statement;
474         expression_t     *condition;
475         statement_t      *true_statement;
476         statement_t      *false_statement;
477 };
478
479 struct switch_statement_t {
480         statement_base_t       statement;
481         expression_t           *expression;
482         statement_t            *body;
483         case_label_statement_t *first_case, *last_case;
484 };
485
486 struct goto_statement_t {
487         statement_base_t  statement;
488         declaration_t    *label;     /**< The destination label. */
489         goto_statement_t *next;      /**< links all goto statements of a function */
490 };
491
492 struct case_label_statement_t {
493         statement_base_t        statement;
494         expression_t           *expression;
495         statement_t            *label_statement;
496         case_label_statement_t *next; /**< link to the next case label in the switch */
497 };
498
499 struct label_statement_t {
500         statement_base_t   statement;
501         declaration_t     *label;
502         statement_t       *label_statement;
503         label_statement_t *next;            /**< links all label statements of a function */
504 };
505
506 struct expression_statement_t {
507         statement_base_t  statement;
508         expression_t     *expression;
509 };
510
511 struct while_statement_t {
512         statement_base_t  statement;
513         expression_t     *condition;
514         statement_t      *body;
515 };
516
517 struct do_while_statement_t {
518         statement_base_t  statement;
519         expression_t     *condition;
520         statement_t      *body;
521 };
522
523 struct for_statement_t {
524         statement_base_t  statement;
525         expression_t     *initialisation;
526         expression_t     *condition;
527         expression_t     *step;
528         statement_t      *body;
529         scope_t           scope;
530 };
531
532 struct asm_constraint_t {
533         string_t          constraints;
534         expression_t     *expression;
535         symbol_t         *symbol;
536         asm_constraint_t *next;
537 };
538
539 struct asm_clobber_t {
540         string_t       clobber;
541         asm_clobber_t *next;
542 };
543
544 struct asm_statement_t {
545         statement_base_t  statement;
546         string_t          asm_text;
547         asm_constraint_t *inputs;
548         asm_constraint_t *outputs;
549         asm_clobber_t    *clobbers;
550         bool              is_volatile;
551 };
552
553 union statement_t {
554         statement_kind_t         kind;
555         statement_base_t         base;
556         return_statement_t       returns;
557         compound_statement_t     compound;
558         declaration_statement_t  declaration;
559         if_statement_t           ifs;
560         switch_statement_t       switchs;
561         goto_statement_t         gotos;
562         case_label_statement_t   case_label;
563         label_statement_t        label;
564         expression_statement_t   expression;
565         while_statement_t        whiles;
566         do_while_statement_t     do_while;
567         for_statement_t          fors;
568         asm_statement_t          asms;
569 };
570
571 struct translation_unit_t {
572         scope_t scope;
573 };
574
575 static inline
576 void *_allocate_ast(size_t size)
577 {
578         return obstack_alloc(&ast_obstack, size);
579 }
580
581 #define allocate_ast(size)                 _allocate_ast(size)
582
583 #endif