- implemented -Wunused-label
[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
418         /* the following fields are used in ast2firm module */
419         unsigned char       declaration_kind;
420         union {
421                 unsigned int    value_number;
422                 ir_entity      *entity;
423                 ir_node        *block;
424                 tarval         *enum_val;
425         } v;
426 };
427
428 typedef enum {
429         STATEMENT_INVALID,
430         STATEMENT_COMPOUND,
431         STATEMENT_RETURN,
432         STATEMENT_DECLARATION,
433         STATEMENT_IF,
434         STATEMENT_SWITCH,
435         STATEMENT_EXPRESSION,
436         STATEMENT_CONTINUE,
437         STATEMENT_BREAK,
438         STATEMENT_GOTO,
439         STATEMENT_LABEL,
440         STATEMENT_CASE_LABEL,
441         STATEMENT_WHILE,
442         STATEMENT_DO_WHILE,
443         STATEMENT_FOR,
444         STATEMENT_ASM
445 } statement_kind_t;
446
447 struct statement_base_t {
448         statement_kind_t   kind;
449         statement_t       *next;
450         source_position_t  source_position;
451 };
452
453 struct return_statement_t {
454         statement_base_t  statement;
455         expression_t     *return_value;
456 };
457
458 struct compound_statement_t {
459         statement_base_t  statement;
460         statement_t      *statements;
461         scope_t           scope;
462 };
463
464 struct declaration_statement_t {
465         statement_base_t  statement;
466         declaration_t    *declarations_begin;
467         declaration_t    *declarations_end;
468 };
469
470 struct if_statement_t {
471         statement_base_t  statement;
472         expression_t     *condition;
473         statement_t      *true_statement;
474         statement_t      *false_statement;
475 };
476
477 struct switch_statement_t {
478         statement_base_t       statement;
479         expression_t           *expression;
480         statement_t            *body;
481         case_label_statement_t *first_case, *last_case;
482 };
483
484 struct goto_statement_t {
485         statement_base_t  statement;
486         declaration_t    *label;     /**< The destination label. */
487         goto_statement_t *next;      /**< links all goto statements of a function */
488 };
489
490 struct case_label_statement_t {
491         statement_base_t        statement;
492         expression_t           *expression;
493         statement_t            *label_statement;
494         case_label_statement_t *next; /**< link to the next case label in the switch */
495 };
496
497 struct label_statement_t {
498         statement_base_t   statement;
499         declaration_t     *label;
500         statement_t       *label_statement;
501         label_statement_t *next;            /**< links all label statements of a function */
502 };
503
504 struct expression_statement_t {
505         statement_base_t  statement;
506         expression_t     *expression;
507 };
508
509 struct while_statement_t {
510         statement_base_t  statement;
511         expression_t     *condition;
512         statement_t      *body;
513 };
514
515 struct do_while_statement_t {
516         statement_base_t  statement;
517         expression_t     *condition;
518         statement_t      *body;
519 };
520
521 struct for_statement_t {
522         statement_base_t  statement;
523         expression_t     *initialisation;
524         expression_t     *condition;
525         expression_t     *step;
526         statement_t      *body;
527         scope_t           scope;
528 };
529
530 struct asm_constraint_t {
531         string_t          constraints;
532         expression_t     *expression;
533         symbol_t         *symbol;
534         asm_constraint_t *next;
535 };
536
537 struct asm_clobber_t {
538         string_t       clobber;
539         asm_clobber_t *next;
540 };
541
542 struct asm_statement_t {
543         statement_base_t  statement;
544         string_t          asm_text;
545         asm_constraint_t *inputs;
546         asm_constraint_t *outputs;
547         asm_clobber_t    *clobbers;
548         bool              is_volatile;
549 };
550
551 union statement_t {
552         statement_kind_t         kind;
553         statement_base_t         base;
554         return_statement_t       returns;
555         compound_statement_t     compound;
556         declaration_statement_t  declaration;
557         if_statement_t           ifs;
558         switch_statement_t       switchs;
559         goto_statement_t         gotos;
560         case_label_statement_t   case_label;
561         label_statement_t        label;
562         expression_statement_t   expression;
563         while_statement_t        whiles;
564         do_while_statement_t     do_while;
565         for_statement_t          fors;
566         asm_statement_t          asms;
567 };
568
569 struct translation_unit_t {
570         scope_t scope;
571 };
572
573 static inline
574 void *_allocate_ast(size_t size)
575 {
576         return obstack_alloc(&ast_obstack, size);
577 }
578
579 #define allocate_ast(size)                 _allocate_ast(size)
580
581 #endif