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