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