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