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