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