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