type: Make an assert()ion independent of the last entry of an enum.
[cparser] / ast_t.h
1 /*
2  * This file is part of cparser.
3  * Copyright (C) 2012 Matthias Braun <matze@braunis.de>
4  */
5 #ifndef AST_T_H
6 #define AST_T_H
7
8 #include <libfirm/firm_types.h>
9 #include <assert.h>
10
11 #include "ast.h"
12 #include "symbol.h"
13 #include "token_t.h"
14 #include "type.h"
15 #include "entity_t.h"
16 #include "adt/obst.h"
17
18 /** The AST obstack contains all data that must stay in the AST. */
19 extern struct obstack ast_obstack;
20
21 /**
22  * Operator precedence classes
23  */
24 typedef enum precedence_t {
25         PREC_BOTTOM,
26         PREC_EXPRESSION,     /* ,                                  left to right */
27         PREC_ASSIGNMENT,     /* = += -= *= /= %= <<= >>= &= ^= |=  right to left */
28         PREC_CONDITIONAL,    /* ?:                                 right to left */
29         PREC_LOGICAL_OR,     /* ||                                 left to right */
30         PREC_LOGICAL_AND,    /* &&                                 left to right */
31         PREC_OR,             /* |                                  left to right */
32         PREC_XOR,            /* ^                                  left to right */
33         PREC_AND,            /* &                                  left to right */
34         PREC_EQUALITY,       /* == !=                              left to right */
35         PREC_RELATIONAL,     /* < <= > >=                          left to right */
36         PREC_SHIFT,          /* << >>                              left to right */
37         PREC_ADDITIVE,       /* + -                                left to right */
38         PREC_MULTIPLICATIVE, /* * / %                              left to right */
39         PREC_CAST,           /* (type)                             right to left */
40         PREC_UNARY,          /* ! ~ ++ -- + - * & sizeof           right to left */
41         PREC_POSTFIX,        /* () [] -> .                         left to right */
42         PREC_PRIMARY,
43         PREC_TOP
44 } precedence_t;
45
46 /**
47  * Expression kinds.
48  */
49 typedef enum expression_kind_t {
50         EXPR_ERROR = 1,
51         EXPR_REFERENCE,
52         EXPR_ENUM_CONSTANT,
53         EXPR_LITERAL_BOOLEAN,
54         EXPR_LITERAL_INTEGER,
55         EXPR_LITERAL_FLOATINGPOINT,
56         EXPR_LITERAL_CHARACTER,
57         EXPR_LITERAL_MS_NOOP, /**< MS __noop extension */
58         EXPR_STRING_LITERAL,
59         EXPR_COMPOUND_LITERAL,
60         EXPR_CALL,
61         EXPR_CONDITIONAL,
62         EXPR_SELECT,
63         EXPR_ARRAY_ACCESS,
64         EXPR_SIZEOF,
65         EXPR_CLASSIFY_TYPE,
66         EXPR_ALIGNOF,
67
68         EXPR_FUNCNAME,
69         EXPR_BUILTIN_CONSTANT_P,
70         EXPR_BUILTIN_TYPES_COMPATIBLE_P,
71         EXPR_OFFSETOF,
72         EXPR_VA_START,
73         EXPR_VA_ARG,
74         EXPR_VA_COPY,
75         EXPR_STATEMENT,
76         EXPR_LABEL_ADDRESS, /**< GCC extension &&label operator */
77
78         EXPR_UNARY_FIRST,
79         EXPR_UNARY_NEGATE = EXPR_UNARY_FIRST,
80         EXPR_UNARY_PLUS,
81         EXPR_UNARY_COMPLEMENT,
82         EXPR_UNARY_NOT,
83         EXPR_UNARY_DEREFERENCE,
84         EXPR_UNARY_TAKE_ADDRESS,
85         EXPR_UNARY_POSTFIX_INCREMENT,
86         EXPR_UNARY_POSTFIX_DECREMENT,
87         EXPR_UNARY_PREFIX_INCREMENT,
88         EXPR_UNARY_PREFIX_DECREMENT,
89         EXPR_UNARY_CAST,
90         EXPR_UNARY_ASSUME,        /**< MS __assume() */
91         EXPR_UNARY_DELETE,
92         EXPR_UNARY_DELETE_ARRAY,
93         EXPR_UNARY_THROW,
94         EXPR_UNARY_REAL,
95         EXPR_UNARY_IMAG,
96         EXPR_UNARY_LAST = EXPR_UNARY_IMAG,
97
98         EXPR_BINARY_FIRST,
99         EXPR_BINARY_ADD = EXPR_BINARY_FIRST,
100         EXPR_BINARY_SUB,
101         EXPR_BINARY_MUL,
102         EXPR_BINARY_DIV,
103         EXPR_BINARY_MOD,
104         EXPR_BINARY_EQUAL,
105         EXPR_BINARY_NOTEQUAL,
106         EXPR_BINARY_LESS,
107         EXPR_BINARY_LESSEQUAL,
108         EXPR_BINARY_GREATER,
109         EXPR_BINARY_GREATEREQUAL,
110         EXPR_BINARY_BITWISE_AND,
111         EXPR_BINARY_BITWISE_OR,
112         EXPR_BINARY_BITWISE_XOR,
113         EXPR_BINARY_LOGICAL_AND,
114         EXPR_BINARY_LOGICAL_OR,
115         EXPR_BINARY_SHIFTLEFT,
116         EXPR_BINARY_SHIFTRIGHT,
117         EXPR_BINARY_ASSIGN,
118         EXPR_BINARY_MUL_ASSIGN,
119         EXPR_BINARY_DIV_ASSIGN,
120         EXPR_BINARY_MOD_ASSIGN,
121         EXPR_BINARY_ADD_ASSIGN,
122         EXPR_BINARY_SUB_ASSIGN,
123         EXPR_BINARY_SHIFTLEFT_ASSIGN,
124         EXPR_BINARY_SHIFTRIGHT_ASSIGN,
125         EXPR_BINARY_BITWISE_AND_ASSIGN,
126         EXPR_BINARY_BITWISE_XOR_ASSIGN,
127         EXPR_BINARY_BITWISE_OR_ASSIGN,
128         EXPR_BINARY_COMMA,
129
130         EXPR_BINARY_ISGREATER,
131         EXPR_BINARY_ISGREATEREQUAL,
132         EXPR_BINARY_ISLESS,
133         EXPR_BINARY_ISLESSEQUAL,
134         EXPR_BINARY_ISLESSGREATER,
135         EXPR_BINARY_ISUNORDERED,
136         EXPR_BINARY_LAST = EXPR_BINARY_ISUNORDERED,
137 } expression_kind_t;
138
139 typedef enum funcname_kind_t {
140         FUNCNAME_FUNCTION,           /**< C99 __func__, older __FUNCTION__ */
141         FUNCNAME_PRETTY_FUNCTION,    /**< GNUC __PRETTY_FUNCTION__ */
142         FUNCNAME_FUNCSIG,            /**< MS __FUNCSIG__ */
143         FUNCNAME_FUNCDNAME           /**< MS __FUNCDNAME__ */
144 } funcname_kind_t;
145
146 /* convenience macros */
147 #define EXPR_BINARY_CASES              \
148              EXPR_BINARY_ADD:                \
149         case EXPR_BINARY_SUB:                \
150         case EXPR_BINARY_MUL:                \
151         case EXPR_BINARY_DIV:                \
152         case EXPR_BINARY_MOD:                \
153         case EXPR_BINARY_EQUAL:              \
154         case EXPR_BINARY_NOTEQUAL:           \
155         case EXPR_BINARY_LESS:               \
156         case EXPR_BINARY_LESSEQUAL:          \
157         case EXPR_BINARY_GREATER:            \
158         case EXPR_BINARY_GREATEREQUAL:       \
159         case EXPR_BINARY_BITWISE_AND:        \
160         case EXPR_BINARY_BITWISE_OR:         \
161         case EXPR_BINARY_BITWISE_XOR:        \
162         case EXPR_BINARY_LOGICAL_AND:        \
163         case EXPR_BINARY_LOGICAL_OR:         \
164         case EXPR_BINARY_SHIFTLEFT:          \
165         case EXPR_BINARY_SHIFTRIGHT:         \
166         case EXPR_BINARY_ASSIGN:             \
167         case EXPR_BINARY_MUL_ASSIGN:         \
168         case EXPR_BINARY_DIV_ASSIGN:         \
169         case EXPR_BINARY_MOD_ASSIGN:         \
170         case EXPR_BINARY_ADD_ASSIGN:         \
171         case EXPR_BINARY_SUB_ASSIGN:         \
172         case EXPR_BINARY_SHIFTLEFT_ASSIGN:   \
173         case EXPR_BINARY_SHIFTRIGHT_ASSIGN:  \
174         case EXPR_BINARY_BITWISE_AND_ASSIGN: \
175         case EXPR_BINARY_BITWISE_XOR_ASSIGN: \
176         case EXPR_BINARY_BITWISE_OR_ASSIGN:  \
177         case EXPR_BINARY_COMMA:              \
178         case EXPR_BINARY_ISGREATER:          \
179         case EXPR_BINARY_ISGREATEREQUAL:     \
180         case EXPR_BINARY_ISLESS:             \
181         case EXPR_BINARY_ISLESSEQUAL:        \
182         case EXPR_BINARY_ISLESSGREATER:      \
183         case EXPR_BINARY_ISUNORDERED
184
185 /**
186  * unary expression with mandatory operand
187  */
188 #define EXPR_UNARY_CASES_MANDATORY   \
189              EXPR_UNARY_NEGATE:            \
190         case EXPR_UNARY_PLUS:              \
191         case EXPR_UNARY_COMPLEMENT:        \
192         case EXPR_UNARY_NOT:               \
193         case EXPR_UNARY_DEREFERENCE:       \
194         case EXPR_UNARY_TAKE_ADDRESS:      \
195         case EXPR_UNARY_POSTFIX_INCREMENT: \
196         case EXPR_UNARY_POSTFIX_DECREMENT: \
197         case EXPR_UNARY_PREFIX_INCREMENT:  \
198         case EXPR_UNARY_PREFIX_DECREMENT:  \
199         case EXPR_UNARY_CAST:              \
200         case EXPR_UNARY_ASSUME:            \
201         case EXPR_UNARY_DELETE:            \
202         case EXPR_UNARY_DELETE_ARRAY:      \
203         case EXPR_UNARY_IMAG:              \
204         case EXPR_UNARY_REAL
205
206 /**
207  * unary expression with optional operand
208  */
209 #define EXPR_UNARY_CASES_OPTIONAL \
210         EXPR_UNARY_THROW
211
212 #define EXPR_UNARY_CASES           \
213              EXPR_UNARY_CASES_MANDATORY: \
214         case EXPR_UNARY_CASES_OPTIONAL
215
216 #define EXPR_LITERAL_CASES                     \
217              EXPR_LITERAL_BOOLEAN:                   \
218         case EXPR_LITERAL_INTEGER:                   \
219         case EXPR_LITERAL_FLOATINGPOINT:             \
220         case EXPR_LITERAL_MS_NOOP
221
222 /**
223  * The base class of every expression.
224  */
225 struct expression_base_t {
226         expression_kind_t kind;     /**< The expression kind. */
227         type_t           *type;     /**< The type of the expression. */
228         position_t        pos;      /**< The source position of this expression. */
229         bool              parenthesized : 1;
230 #ifndef NDEBUG
231         bool              transformed : 1; /**< Set if this expression was transformed. */
232 #endif
233         bool              implicit : 1;  /**< compiler generated expression.
234                                             Examples: select into anonymous structs,
235                                             implicit casts */
236 };
237
238 /**
239  * integer, float and boolean constants
240  */
241 struct literal_expression_t {
242         expression_base_t base;
243         string_t          value;
244         char const       *suffix; /**< Start of the suffix in value. */
245
246         /* ast2firm data */
247         ir_tarval        *target_value;
248 };
249
250 /**
251  * string and character literals
252  */
253 struct string_literal_expression_t {
254         expression_base_t base;
255         string_t          value;
256 };
257
258 struct funcname_expression_t {
259         expression_base_t  base;
260         funcname_kind_t    kind;
261 };
262
263 struct compound_literal_expression_t {
264         expression_base_t  base;
265         type_t            *type;
266         initializer_t     *initializer;
267         bool               global_scope;
268 };
269
270 struct builtin_constant_expression_t {
271         expression_base_t  base;
272         expression_t      *value;
273 };
274
275 struct builtin_types_compatible_expression_t {
276         expression_base_t  base;
277         type_t            *left;
278         type_t            *right;
279 };
280
281 struct reference_expression_t {
282         expression_base_t  base;
283         entity_t          *entity;
284 };
285
286 /**
287  * An argument of a call.
288  */
289 struct call_argument_t {
290         expression_t    *expression;  /**< The expression which value is transmitted. */
291         call_argument_t *next;        /**< Links to the next argument of this call. */
292 };
293
294
295 struct call_expression_t {
296         expression_base_t  base;
297         expression_t      *function;  /**< The address of the function to call. */
298         call_argument_t   *arguments; /**< List of arguments of this call. */
299 };
300
301
302 struct unary_expression_t {
303         expression_base_t  base;
304         expression_t      *value;     /**< The unary operand. */
305 };
306
307 struct binary_expression_t {
308         expression_base_t  base;
309         expression_t      *left;
310         expression_t      *right;
311 };
312
313 struct select_expression_t {
314         expression_base_t  base;
315         expression_t      *compound;
316         entity_t          *compound_entry;
317 };
318
319 struct array_access_expression_t {
320         expression_base_t  base;
321         expression_t      *array_ref; /**< the referenced array */
322         expression_t      *index;     /**< the index used */
323         bool               flipped;   /**< True if index/ref was written in a 5[a] way */
324 };
325
326 struct typeprop_expression_t {
327         expression_base_t  base;
328         type_t            *type;
329         expression_t      *tp_expression;
330 };
331
332 struct designator_t {
333         position_t    pos;
334         symbol_t     *symbol;      /**< the symbol if any */
335         expression_t *array_index; /**< the array index if any */
336         expression_t *range_last;  /**< last index of a range initializer, if any */
337         designator_t *next;
338 };
339
340 struct offsetof_expression_t {
341         expression_base_t  base;
342         type_t            *type;
343         designator_t      *designator;
344 };
345
346 struct va_start_expression_t {
347         expression_base_t  base;
348         expression_t      *ap;
349         expression_t      *parameter;
350 };
351
352 struct va_arg_expression_t {
353         expression_base_t  base;
354         expression_t      *ap;
355 };
356
357 struct va_copy_expression_t {
358         expression_base_t  base;
359         expression_t      *dst;    /**< destination argument */
360         expression_t      *src;    /**< source argument */
361 };
362
363 struct conditional_expression_t {
364         expression_base_t  base;
365         expression_t      *condition;
366         expression_t      *true_expression;
367         expression_t      *false_expression;
368 };
369
370 struct statement_expression_t {
371         expression_base_t  base;
372         statement_t       *statement;
373 };
374
375 struct classify_type_expression_t {
376         expression_base_t  base;
377         expression_t      *type_expression;
378 };
379
380 struct label_address_expression_t {
381         expression_base_t  base;
382         label_t           *label;
383 };
384
385 union expression_t {
386         expression_kind_t                     kind;
387         expression_base_t                     base;
388         literal_expression_t                  literal;
389         string_literal_expression_t           string_literal;
390         funcname_expression_t                 funcname;
391         compound_literal_expression_t         compound_literal;
392         builtin_constant_expression_t         builtin_constant;
393         builtin_types_compatible_expression_t builtin_types_compatible;
394         reference_expression_t                reference;
395         call_expression_t                     call;
396         unary_expression_t                    unary;
397         binary_expression_t                   binary;
398         select_expression_t                   select;
399         array_access_expression_t             array_access;
400         typeprop_expression_t                 typeprop;
401         offsetof_expression_t                 offsetofe;
402         va_start_expression_t                 va_starte;
403         va_arg_expression_t                   va_arge;
404         va_copy_expression_t                  va_copye;
405         conditional_expression_t              conditional;
406         statement_expression_t                statement;
407         classify_type_expression_t            classify_type;
408         label_address_expression_t            label_address;
409 };
410
411 typedef enum initializer_kind_t {
412         INITIALIZER_VALUE,
413         INITIALIZER_LIST,
414         INITIALIZER_STRING,
415         INITIALIZER_DESIGNATOR
416 } initializer_kind_t;
417
418 struct initializer_base_t {
419         initializer_kind_t kind;
420 };
421
422 struct initializer_value_t {
423         initializer_base_t  base;
424         expression_t       *value;
425 };
426
427 struct initializer_list_t {
428         initializer_base_t  base;
429         size_t              len;
430         initializer_t      *initializers[];
431 };
432
433 struct initializer_designator_t {
434         initializer_base_t  base;
435         designator_t       *designator;
436 };
437
438 union initializer_t {
439         initializer_kind_t        kind;
440         initializer_base_t        base;
441         initializer_value_t       value;
442         initializer_list_t        list;
443         initializer_designator_t  designator;
444 };
445
446 static inline string_literal_expression_t const *get_init_string(initializer_t const *const init)
447 {
448         assert(init->kind == INITIALIZER_STRING);
449         assert(init->value.value->kind == EXPR_STRING_LITERAL);
450         return &init->value.value->string_literal;
451 }
452
453 /**
454  * The statement kinds.
455  */
456 typedef enum statement_kind_t {
457         STATEMENT_ERROR = 1,
458         STATEMENT_EMPTY,
459         STATEMENT_COMPOUND,
460         STATEMENT_RETURN,
461         STATEMENT_DECLARATION,
462         STATEMENT_IF,
463         STATEMENT_SWITCH,
464         STATEMENT_EXPRESSION,
465         STATEMENT_CONTINUE,
466         STATEMENT_BREAK,
467         STATEMENT_COMPUTED_GOTO,
468         STATEMENT_GOTO,
469         STATEMENT_LABEL,
470         STATEMENT_CASE_LABEL,
471         STATEMENT_DO_WHILE,
472         STATEMENT_FOR,
473         STATEMENT_ASM,
474         STATEMENT_MS_TRY,          /**< MS __try/__finally or __try/__except */
475         STATEMENT_LEAVE            /**< MS __leave */
476 } statement_kind_t;
477
478 /**
479  * The base class of every statement.
480  */
481 struct statement_base_t {
482         statement_kind_t kind;
483         statement_t     *next;      /**< Point to the next statement in a compound statement. */
484         position_t       pos;
485         statement_t     *parent;    /**< The Parent statement that controls the execution. */
486         bool             reachable; /**< True, if this statement is reachable. */
487 #ifndef NDEBUG
488         bool             transformed;
489 #endif
490 };
491
492 struct return_statement_t {
493         statement_base_t  base;
494         expression_t     *value;    /**< The return value if any. */
495 };
496
497 struct compound_statement_t {
498         statement_base_t  base;
499         statement_t      *statements;
500         scope_t           scope;
501         bool              stmt_expr; /**< True if this compound statement is a statement expression. */
502 };
503
504 struct declaration_statement_t {
505         statement_base_t  base;
506         entity_t         *declarations_begin;
507         entity_t         *declarations_end;
508 };
509
510 struct if_statement_t {
511         statement_base_t  base;
512         scope_t           scope;
513         expression_t     *condition;
514         statement_t      *true_statement;
515         statement_t      *false_statement;
516 };
517
518 struct switch_statement_t {
519         statement_base_t        base;
520         scope_t                 scope;
521         expression_t           *expression;
522         statement_t            *body;
523         case_label_statement_t *first_case, *last_case; /**< List of all cases, including default. */
524         case_label_statement_t *default_label;          /**< The default label if existent. */
525 };
526
527 struct goto_statement_t {
528         statement_base_t  base;
529         label_t          *label;         /**< The destination label. */
530         goto_statement_t *next;          /**< links all goto statements of a function */
531 };
532
533 struct computed_goto_statement_t {
534         statement_base_t  base;
535         expression_t     *expression; /**< The expression for the computed goto. */
536 };
537
538 struct case_label_statement_t {
539         statement_base_t        base;
540         expression_t           *expression;    /**< The case label expression, NULL for default label. */
541         expression_t           *end_range;     /**< For GNUC case a .. b: the end range expression, NULL else. */
542         case_label_statement_t *next;          /**< link to the next case label in switch */
543         statement_t            *statement;
544         ir_tarval              *first_case;
545         ir_tarval              *last_case;
546         bool                   is_bad;         /**< If set marked as bad to suppress warnings. */
547         bool                   is_empty_range; /**< If set marked this as an empty range. */
548         long                   pn;
549 };
550
551 struct label_statement_t {
552         statement_base_t   base;
553         label_t           *label;
554         statement_t       *statement;
555         label_statement_t *next;    /**< links all label statements of a function */
556 };
557
558 struct expression_statement_t {
559         statement_base_t  base;
560         expression_t     *expression;
561 };
562
563 struct do_while_statement_t {
564         statement_base_t  base;
565         scope_t           scope;
566         expression_t     *condition;
567         statement_t      *body;
568 };
569
570 struct for_statement_t {
571         statement_base_t  base;
572         scope_t           scope;
573         expression_t     *initialisation;
574         expression_t     *condition;
575         expression_t     *step;
576         statement_t      *body;
577         bool              condition_reachable:1;
578         bool              step_reachable:1;
579 };
580
581 struct asm_argument_t {
582         string_t        constraints;
583         expression_t   *expression;
584         symbol_t       *symbol;
585         asm_argument_t *next;
586 };
587
588 struct asm_clobber_t {
589         string_t       clobber;
590         asm_clobber_t *next;
591 };
592
593 struct asm_label_t {
594         label_t     *label;
595         asm_label_t *next;
596 };
597
598 struct asm_statement_t {
599         statement_base_t base;
600         string_t         asm_text;
601         asm_argument_t  *inputs;
602         asm_argument_t  *outputs;
603         asm_clobber_t   *clobbers;
604         asm_label_t     *labels;
605         bool             is_volatile;
606 };
607
608 struct ms_try_statement_t {
609         statement_base_t  base;
610         statement_t      *try_statement;
611         expression_t     *except_expression; /**< non-null for except, NULL for finally */
612         statement_t      *final_statement;
613 };
614
615 struct leave_statement_t {
616         statement_base_t  base;
617 };
618
619 union statement_t {
620         statement_kind_t          kind;
621         statement_base_t          base;
622         return_statement_t        returns;
623         compound_statement_t      compound;
624         declaration_statement_t   declaration;
625         if_statement_t            ifs;
626         switch_statement_t        switchs;
627         computed_goto_statement_t computed_goto;
628         goto_statement_t          gotos;
629         case_label_statement_t    case_label;
630         label_statement_t         label;
631         expression_statement_t    expression;
632         do_while_statement_t      do_while;
633         for_statement_t           fors;
634         asm_statement_t           asms;
635         ms_try_statement_t        ms_try;
636         leave_statement_t         leave;
637 };
638
639 struct translation_unit_t {
640         scope_t      scope;
641         statement_t *global_asm;
642 };
643
644 /**
645  * Allocate an AST node with given size and
646  * initialize all fields with zero.
647  */
648 static inline void *allocate_ast_zero(size_t size)
649 {
650         return memset(obstack_alloc(&ast_obstack, size), 0, size);
651 }
652
653 /** If set, implicit casts are printed. */
654 extern bool print_implicit_casts;
655 /** If set parenthesis are printed to indicate operator precedence. */
656 extern bool print_parenthesis;
657
658 #endif