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