Do no include -Wswitch-default in -Wall.
[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 typeprop_expression_t {
244         expression_base_t  expression;
245         type_t            *type;
246         expression_t      *tp_expression;
247 };
248
249 struct designator_t {
250         symbol_t     *symbol;
251         expression_t *array_access;
252         designator_t *next;
253 };
254
255 struct offsetof_expression_t {
256         expression_base_t  expression;
257         type_t            *type;
258         designator_t      *designator;
259 };
260
261 struct va_start_expression_t {
262         expression_base_t  expression;
263         expression_t      *ap;
264         declaration_t     *parameter;
265 };
266
267 struct va_arg_expression_t {
268         expression_base_t  expression;
269         expression_t      *ap;
270 };
271
272 struct conditional_expression_t {
273         expression_base_t  expression;
274         expression_t      *condition;
275         expression_t      *true_expression;
276         expression_t      *false_expression;
277 };
278
279 struct statement_expression_t {
280         expression_base_t  expression;
281         statement_t       *statement;
282 };
283
284 struct classify_type_expression_t {
285         expression_base_t  expression;
286         expression_t      *type_expression;
287 };
288
289 union expression_t {
290         expression_kind_t                kind;
291         expression_base_t                base;
292         const_expression_t               conste;
293         string_literal_expression_t      string;
294         wide_string_literal_expression_t wide_string;
295         builtin_symbol_expression_t      builtin_symbol;
296         builtin_constant_expression_t    builtin_constant;
297         builtin_prefetch_expression_t    builtin_prefetch;
298         reference_expression_t           reference;
299         call_expression_t                call;
300         unary_expression_t               unary;
301         binary_expression_t              binary;
302         select_expression_t              select;
303         array_access_expression_t        array_access;
304         typeprop_expression_t            typeprop;
305         offsetof_expression_t            offsetofe;
306         va_start_expression_t            va_starte;
307         va_arg_expression_t              va_arge;
308         conditional_expression_t         conditional;
309         statement_expression_t           statement;
310         classify_type_expression_t       classify_type;
311 };
312
313 typedef enum {
314         STORAGE_CLASS_NONE,
315         STORAGE_CLASS_TYPEDEF,
316         STORAGE_CLASS_EXTERN,
317         STORAGE_CLASS_STATIC,
318         STORAGE_CLASS_AUTO,
319         STORAGE_CLASS_REGISTER,
320         STORAGE_CLASS_ENUM_ENTRY,
321         STORAGE_CLASS_THREAD,
322         STORAGE_CLASS_THREAD_EXTERN,
323         STORAGE_CLASS_THREAD_STATIC
324 } storage_class_tag_t;
325
326 typedef enum {
327         NAMESPACE_NORMAL,
328         NAMESPACE_STRUCT,
329         NAMESPACE_UNION,
330         NAMESPACE_ENUM,
331         NAMESPACE_LABEL,
332 } namespace_t;
333
334 typedef enum {
335         INITIALIZER_VALUE,
336         INITIALIZER_LIST,
337         INITIALIZER_STRING,
338         INITIALIZER_WIDE_STRING
339 } initializer_kind_t;
340
341 struct initializer_base_t {
342         initializer_kind_t kind;
343 };
344
345 struct initializer_value_t {
346         initializer_base_t  initializer;
347         expression_t       *value;
348 };
349
350 struct initializer_list_t {
351         initializer_base_t  initializer;
352         size_t              len;
353         initializer_t      *initializers[];
354 };
355
356 struct initializer_string_t {
357         initializer_base_t initializer;
358         string_t           string;
359 };
360
361 struct initializer_wide_string_t {
362         initializer_base_t  initializer;
363         wide_string_t       string;
364 };
365
366 union initializer_t {
367         initializer_kind_t        kind;
368         initializer_base_t        base;
369         initializer_value_t       value;
370         initializer_list_t        list;
371         initializer_string_t      string;
372         initializer_wide_string_t wide_string;
373 };
374
375 typedef enum {
376         DM_DLLIMPORT   = (1 << 0),
377         DM_DLLEXPORT   = (1 << 1),
378         DM_THREAD      = (1 << 2),
379         DM_NAKED       = (1 << 3),
380         DM_FORCEINLINE = (1 << 4),
381         DM_NOTHROW     = (1 << 5),
382         DM_NORETURN    = (1 << 6),
383         DM_NOINLINE    = (1 << 7)
384 } decl_modifier_t;
385
386 typedef unsigned short decl_modifiers_t;
387
388 struct declaration_t {
389         unsigned char       namespc;
390         unsigned char       storage_class;
391         decl_modifiers_t    modifiers;
392         unsigned int        address_taken : 1;
393         unsigned int        is_inline     : 1;
394         unsigned int        used          : 1;  /**< Set if the declaration is used. */
395         type_t             *type;
396         symbol_t           *symbol;
397         source_position_t   source_position;
398         union {
399                 bool            is_defined;
400                 statement_t    *statement;
401                 initializer_t  *initializer;
402                 expression_t   *enum_value;
403         } init;
404         scope_t             scope;
405         scope_t            *parent_scope;
406
407         /** next declaration in a scope */
408         declaration_t      *next;
409         /** next declaration with same symbol */
410         declaration_t      *symbol_next;
411         /** next variable/parameter in function scope/global scope */
412         declaration_t      *next_var;
413
414         /* the following fields are used in ast2firm module */
415         unsigned char       declaration_kind;
416         union {
417                 unsigned int    value_number;
418                 ir_entity      *entity;
419                 ir_node        *block;
420                 tarval         *enum_val;
421         } v;
422 };
423
424 typedef enum {
425         STATEMENT_INVALID,
426         STATEMENT_COMPOUND,
427         STATEMENT_RETURN,
428         STATEMENT_DECLARATION,
429         STATEMENT_IF,
430         STATEMENT_SWITCH,
431         STATEMENT_EXPRESSION,
432         STATEMENT_CONTINUE,
433         STATEMENT_BREAK,
434         STATEMENT_GOTO,
435         STATEMENT_LABEL,
436         STATEMENT_CASE_LABEL,
437         STATEMENT_WHILE,
438         STATEMENT_DO_WHILE,
439         STATEMENT_FOR,
440         STATEMENT_ASM
441 } statement_kind_t;
442
443 struct statement_base_t {
444         statement_kind_t   kind;
445         statement_t       *next;
446         source_position_t  source_position;
447 };
448
449 struct return_statement_t {
450         statement_base_t  statement;
451         expression_t     *return_value;
452 };
453
454 struct compound_statement_t {
455         statement_base_t  statement;
456         statement_t      *statements;
457         scope_t           scope;
458 };
459
460 struct declaration_statement_t {
461         statement_base_t  statement;
462         declaration_t    *declarations_begin;
463         declaration_t    *declarations_end;
464 };
465
466 struct if_statement_t {
467         statement_base_t  statement;
468         expression_t     *condition;
469         statement_t      *true_statement;
470         statement_t      *false_statement;
471 };
472
473 struct switch_statement_t {
474         statement_base_t       statement;
475         expression_t           *expression;
476         statement_t            *body;
477         case_label_statement_t *first_case, *last_case;
478 };
479
480 struct goto_statement_t {
481         statement_base_t  statement;
482         declaration_t    *label;     /**< The destination label. */
483         goto_statement_t *next;      /**< links all goto statements of a function */
484 };
485
486 struct case_label_statement_t {
487         statement_base_t        statement;
488         expression_t           *expression;
489         statement_t            *label_statement;
490         case_label_statement_t *next; /**< link to the next case label in the switch */
491 };
492
493 struct label_statement_t {
494         statement_base_t   statement;
495         declaration_t     *label;
496         statement_t       *label_statement;
497         label_statement_t *next;            /**< links all label statements of a function */
498 };
499
500 struct expression_statement_t {
501         statement_base_t  statement;
502         expression_t     *expression;
503 };
504
505 struct while_statement_t {
506         statement_base_t  statement;
507         expression_t     *condition;
508         statement_t      *body;
509 };
510
511 struct do_while_statement_t {
512         statement_base_t  statement;
513         expression_t     *condition;
514         statement_t      *body;
515 };
516
517 struct for_statement_t {
518         statement_base_t  statement;
519         expression_t     *initialisation;
520         expression_t     *condition;
521         expression_t     *step;
522         statement_t      *body;
523         scope_t           scope;
524 };
525
526 struct asm_constraint_t {
527         string_t          constraints;
528         expression_t     *expression;
529         symbol_t         *symbol;
530         asm_constraint_t *next;
531 };
532
533 struct asm_clobber_t {
534         string_t       clobber;
535         asm_clobber_t *next;
536 };
537
538 struct asm_statement_t {
539         statement_base_t  statement;
540         string_t          asm_text;
541         asm_constraint_t *inputs;
542         asm_constraint_t *outputs;
543         asm_clobber_t    *clobbers;
544         bool              is_volatile;
545 };
546
547 union statement_t {
548         statement_kind_t         kind;
549         statement_base_t         base;
550         return_statement_t       returns;
551         compound_statement_t     compound;
552         declaration_statement_t  declaration;
553         if_statement_t           ifs;
554         switch_statement_t       switchs;
555         goto_statement_t         gotos;
556         case_label_statement_t   case_label;
557         label_statement_t        label;
558         expression_statement_t   expression;
559         while_statement_t        whiles;
560         do_while_statement_t     do_while;
561         for_statement_t          fors;
562         asm_statement_t          asms;
563 };
564
565 struct translation_unit_t {
566         scope_t scope;
567 };
568
569 static inline
570 void *_allocate_ast(size_t size)
571 {
572         return obstack_alloc(&ast_obstack, size);
573 }
574
575 #define allocate_ast(size)                 _allocate_ast(size)
576
577 #endif