Multi-line character constants implemented
[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 };
159
160 struct expression_base_t {
161         expression_kind_t   kind;
162         type_t             *type;
163         source_position_t   source_position;
164 };
165
166 struct const_expression_t {
167         expression_base_t  base;
168         union {
169                 long long    int_value;
170                 long double  float_value;
171                 string_t     chars;
172         } v;
173 };
174
175 struct string_literal_expression_t {
176         expression_base_t  base;
177         string_t           value;
178 };
179
180 struct wide_string_literal_expression_t {
181         expression_base_t  base;
182         wide_string_t      value;
183 };
184
185 struct builtin_symbol_expression_t {
186         expression_base_t  base;
187         symbol_t          *symbol;
188 };
189
190 struct builtin_constant_expression_t {
191         expression_base_t  base;
192         expression_t      *value;
193 };
194
195 struct builtin_prefetch_expression_t {
196         expression_base_t  base;
197         expression_t      *adr;
198         expression_t      *rw;
199         expression_t      *locality;
200 };
201
202 struct reference_expression_t {
203         expression_base_t  base;
204         symbol_t          *symbol;
205         declaration_t     *declaration;
206 };
207
208 struct call_argument_t {
209         expression_t    *expression;
210         call_argument_t *next;
211 };
212
213 struct call_expression_t {
214         expression_base_t  base;
215         expression_t      *function;
216         call_argument_t   *arguments;
217 };
218
219 struct unary_expression_t {
220         expression_base_t  base;
221         expression_t      *value;
222 };
223
224 struct binary_expression_t {
225         expression_base_t  base;
226         expression_t      *left;
227         expression_t      *right;
228 };
229
230 struct select_expression_t {
231         expression_base_t  base;
232         expression_t      *compound;
233         symbol_t          *symbol;
234
235         declaration_t     *compound_entry;
236 };
237
238 struct array_access_expression_t {
239         expression_base_t  base;
240         expression_t      *array_ref;
241         expression_t      *index;
242         bool               flipped; /* index/ref was written in a 5[a] way */
243 };
244
245 struct typeprop_expression_t {
246         expression_base_t  base;
247         type_t            *type;
248         expression_t      *tp_expression;
249 };
250
251 struct designator_t {
252         symbol_t     *symbol;
253         expression_t *array_access;
254         designator_t *next;
255 };
256
257 struct offsetof_expression_t {
258         expression_base_t  base;
259         type_t            *type;
260         designator_t      *designator;
261 };
262
263 struct va_start_expression_t {
264         expression_base_t  base;
265         expression_t      *ap;
266         declaration_t     *parameter;
267 };
268
269 struct va_arg_expression_t {
270         expression_base_t  base;
271         expression_t      *ap;
272 };
273
274 struct conditional_expression_t {
275         expression_base_t  base;
276         expression_t      *condition;
277         expression_t      *true_expression;
278         expression_t      *false_expression;
279 };
280
281 struct statement_expression_t {
282         expression_base_t  base;
283         statement_t       *statement;
284 };
285
286 struct classify_type_expression_t {
287         expression_base_t  base;
288         expression_t      *type_expression;
289 };
290
291 union expression_t {
292         expression_kind_t                kind;
293         expression_base_t                base;
294         const_expression_t               conste;
295         string_literal_expression_t      string;
296         wide_string_literal_expression_t wide_string;
297         builtin_symbol_expression_t      builtin_symbol;
298         builtin_constant_expression_t    builtin_constant;
299         builtin_prefetch_expression_t    builtin_prefetch;
300         reference_expression_t           reference;
301         call_expression_t                call;
302         unary_expression_t               unary;
303         binary_expression_t              binary;
304         select_expression_t              select;
305         array_access_expression_t        array_access;
306         typeprop_expression_t            typeprop;
307         offsetof_expression_t            offsetofe;
308         va_start_expression_t            va_starte;
309         va_arg_expression_t              va_arge;
310         conditional_expression_t         conditional;
311         statement_expression_t           statement;
312         classify_type_expression_t       classify_type;
313 };
314
315 typedef enum {
316         STORAGE_CLASS_NONE,
317         STORAGE_CLASS_TYPEDEF,
318         STORAGE_CLASS_EXTERN,
319         STORAGE_CLASS_STATIC,
320         STORAGE_CLASS_AUTO,
321         STORAGE_CLASS_REGISTER,
322         STORAGE_CLASS_ENUM_ENTRY,
323         STORAGE_CLASS_THREAD,
324         STORAGE_CLASS_THREAD_EXTERN,
325         STORAGE_CLASS_THREAD_STATIC
326 } storage_class_tag_t;
327
328 typedef enum {
329         NAMESPACE_NORMAL,
330         NAMESPACE_STRUCT,
331         NAMESPACE_UNION,
332         NAMESPACE_ENUM,
333         NAMESPACE_LABEL,
334 } namespace_t;
335
336 typedef enum {
337         INITIALIZER_VALUE,
338         INITIALIZER_LIST,
339         INITIALIZER_STRING,
340         INITIALIZER_WIDE_STRING
341 } initializer_kind_t;
342
343 struct initializer_base_t {
344         initializer_kind_t kind;
345 };
346
347 struct initializer_value_t {
348         initializer_base_t  initializer;
349         expression_t       *value;
350 };
351
352 struct initializer_list_t {
353         initializer_base_t  initializer;
354         size_t              len;
355         initializer_t      *initializers[];
356 };
357
358 struct initializer_string_t {
359         initializer_base_t initializer;
360         string_t           string;
361 };
362
363 struct initializer_wide_string_t {
364         initializer_base_t  initializer;
365         wide_string_t       string;
366 };
367
368 union initializer_t {
369         initializer_kind_t        kind;
370         initializer_base_t        base;
371         initializer_value_t       value;
372         initializer_list_t        list;
373         initializer_string_t      string;
374         initializer_wide_string_t wide_string;
375 };
376
377 typedef enum {
378         DM_DLLIMPORT   = (1 << 0),
379         DM_DLLEXPORT   = (1 << 1),
380         DM_THREAD      = (1 << 2),
381         DM_NAKED       = (1 << 3),
382         DM_FORCEINLINE = (1 << 4),
383         DM_NOTHROW     = (1 << 5),
384         DM_NORETURN    = (1 << 6),
385         DM_NOINLINE    = (1 << 7)
386 } decl_modifier_t;
387
388 typedef unsigned short decl_modifiers_t;
389
390 struct declaration_t {
391         unsigned char       namespc;
392         unsigned char       storage_class;
393         decl_modifiers_t    modifiers;
394         unsigned int        address_taken : 1;
395         unsigned int        is_inline     : 1;
396         unsigned int        used          : 1;  /**< Set if the declaration is used. */
397         type_t             *type;
398         symbol_t           *symbol;
399         source_position_t   source_position;
400         union {
401                 bool            is_defined;
402                 statement_t    *statement;
403                 initializer_t  *initializer;
404                 expression_t   *enum_value;
405         } init;
406         scope_t             scope;
407         scope_t            *parent_scope;
408
409         /** next declaration in a scope */
410         declaration_t      *next;
411         /** next declaration with same symbol */
412         declaration_t      *symbol_next;
413
414         /* the following fields are used in ast2firm module */
415         unsigned char       declaration_kind;
416         union {
417                 unsigned int    value_number;
418                 ir_entity      *entity;
419                 ir_node        *block;
420                 tarval         *enum_val;
421                 ir_type        *irtype;
422         } v;
423 };
424
425 typedef enum {
426         STATEMENT_INVALID,
427         STATEMENT_COMPOUND,
428         STATEMENT_RETURN,
429         STATEMENT_DECLARATION,
430         STATEMENT_IF,
431         STATEMENT_SWITCH,
432         STATEMENT_EXPRESSION,
433         STATEMENT_CONTINUE,
434         STATEMENT_BREAK,
435         STATEMENT_GOTO,
436         STATEMENT_LABEL,
437         STATEMENT_CASE_LABEL,
438         STATEMENT_WHILE,
439         STATEMENT_DO_WHILE,
440         STATEMENT_FOR,
441         STATEMENT_ASM
442 } statement_kind_t;
443
444 struct statement_base_t {
445         statement_kind_t   kind;
446         statement_t       *next;
447         source_position_t  source_position;
448 };
449
450 struct return_statement_t {
451         statement_base_t  base;
452         expression_t     *value;
453 };
454
455 struct compound_statement_t {
456         statement_base_t  base;
457         statement_t      *statements;
458         scope_t           scope;
459 };
460
461 struct declaration_statement_t {
462         statement_base_t  base;
463         declaration_t    *declarations_begin;
464         declaration_t    *declarations_end;
465 };
466
467 struct if_statement_t {
468         statement_base_t  base;
469         expression_t     *condition;
470         statement_t      *true_statement;
471         statement_t      *false_statement;
472 };
473
474 struct switch_statement_t {
475         statement_base_t       base;
476         expression_t           *expression;
477         statement_t            *body;
478         case_label_statement_t *first_case, *last_case;
479 };
480
481 struct goto_statement_t {
482         statement_base_t  base;
483         declaration_t    *label;     /**< The destination label. */
484         goto_statement_t *next;      /**< links all goto statements of a function */
485 };
486
487 struct case_label_statement_t {
488         statement_base_t        base;
489         expression_t           *expression;  /**< The case label expression, NULL for default label. */
490         expression_t           *end_range;   /**< For GNUC case a .. b: the end range expression, NULL else. */
491         statement_t            *statement;
492         case_label_statement_t *next; /**< link to the next case label in switch */
493 };
494
495 struct label_statement_t {
496         statement_base_t   base;
497         declaration_t     *label;
498         statement_t       *statement;
499         label_statement_t *next;    /**< links all label statements of a function */
500 };
501
502 struct expression_statement_t {
503         statement_base_t  base;
504         expression_t     *expression;
505 };
506
507 struct while_statement_t {
508         statement_base_t  base;
509         expression_t     *condition;
510         statement_t      *body;
511 };
512
513 struct do_while_statement_t {
514         statement_base_t  base;
515         expression_t     *condition;
516         statement_t      *body;
517 };
518
519 struct for_statement_t {
520         statement_base_t  base;
521         expression_t     *initialisation;
522         expression_t     *condition;
523         expression_t     *step;
524         statement_t      *body;
525         scope_t           scope;
526 };
527
528 struct asm_constraint_t {
529         string_t          constraints;
530         expression_t     *expression;
531         symbol_t         *symbol;
532         asm_constraint_t *next;
533 };
534
535 struct asm_clobber_t {
536         string_t       clobber;
537         asm_clobber_t *next;
538 };
539
540 struct asm_statement_t {
541         statement_base_t  base;
542         string_t          asm_text;
543         asm_constraint_t *inputs;
544         asm_constraint_t *outputs;
545         asm_clobber_t    *clobbers;
546         bool              is_volatile;
547 };
548
549 union statement_t {
550         statement_kind_t         kind;
551         statement_base_t         base;
552         return_statement_t       returns;
553         compound_statement_t     compound;
554         declaration_statement_t  declaration;
555         if_statement_t           ifs;
556         switch_statement_t       switchs;
557         goto_statement_t         gotos;
558         case_label_statement_t   case_label;
559         label_statement_t        label;
560         expression_statement_t   expression;
561         while_statement_t        whiles;
562         do_while_statement_t     do_while;
563         for_statement_t          fors;
564         asm_statement_t          asms;
565 };
566
567 struct translation_unit_t {
568         scope_t scope;
569 };
570
571 static inline
572 void *_allocate_ast(size_t size)
573 {
574         return obstack_alloc(&ast_obstack, size);
575 }
576
577 #define allocate_ast(size)                 _allocate_ast(size)
578
579 #endif