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