add a printing abstraction layer so we can print type/ast to files and memory buffers
[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_UNKNOWN = 0,
66         EXPR_INVALID,
67         EXPR_REFERENCE,
68         EXPR_REFERENCE_ENUM_VALUE,
69         EXPR_CONST,
70         EXPR_CHARACTER_CONSTANT,
71         EXPR_WIDE_CHARACTER_CONSTANT,
72         EXPR_STRING_LITERAL,
73         EXPR_WIDE_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_CAST_IMPLICIT, /**< compiler generated cast */
106         EXPR_UNARY_ASSUME,        /**< MS __assume() */
107         EXPR_UNARY_DELETE,
108         EXPR_UNARY_DELETE_ARRAY,
109         EXPR_UNARY_THROW,
110         EXPR_UNARY_LAST = EXPR_UNARY_THROW,
111
112         EXPR_BINARY_FIRST,
113         EXPR_BINARY_ADD = EXPR_BINARY_FIRST,
114         EXPR_BINARY_SUB,
115         EXPR_BINARY_MUL,
116         EXPR_BINARY_DIV,
117         EXPR_BINARY_MOD,
118         EXPR_BINARY_EQUAL,
119         EXPR_BINARY_NOTEQUAL,
120         EXPR_BINARY_LESS,
121         EXPR_BINARY_LESSEQUAL,
122         EXPR_BINARY_GREATER,
123         EXPR_BINARY_GREATEREQUAL,
124         EXPR_BINARY_BITWISE_AND,
125         EXPR_BINARY_BITWISE_OR,
126         EXPR_BINARY_BITWISE_XOR,
127         EXPR_BINARY_LOGICAL_AND,
128         EXPR_BINARY_LOGICAL_OR,
129         EXPR_BINARY_SHIFTLEFT,
130         EXPR_BINARY_SHIFTRIGHT,
131         EXPR_BINARY_ASSIGN,
132         EXPR_BINARY_MUL_ASSIGN,
133         EXPR_BINARY_DIV_ASSIGN,
134         EXPR_BINARY_MOD_ASSIGN,
135         EXPR_BINARY_ADD_ASSIGN,
136         EXPR_BINARY_SUB_ASSIGN,
137         EXPR_BINARY_SHIFTLEFT_ASSIGN,
138         EXPR_BINARY_SHIFTRIGHT_ASSIGN,
139         EXPR_BINARY_BITWISE_AND_ASSIGN,
140         EXPR_BINARY_BITWISE_XOR_ASSIGN,
141         EXPR_BINARY_BITWISE_OR_ASSIGN,
142         EXPR_BINARY_COMMA,
143
144         EXPR_BINARY_ISGREATER,
145         EXPR_BINARY_ISGREATEREQUAL,
146         EXPR_BINARY_ISLESS,
147         EXPR_BINARY_ISLESSEQUAL,
148         EXPR_BINARY_ISLESSGREATER,
149         EXPR_BINARY_ISUNORDERED,
150         EXPR_BINARY_LAST = EXPR_BINARY_ISUNORDERED,
151 } expression_kind_t;
152
153 typedef enum funcname_kind_t {
154         FUNCNAME_FUNCTION,           /**< C99 __func__, older __FUNCTION__ */
155         FUNCNAME_PRETTY_FUNCTION,    /**< GNUC __PRETTY_FUNCTION__ */
156         FUNCNAME_FUNCSIG,            /**< MS __FUNCSIG__ */
157         FUNCNAME_FUNCDNAME           /**< MS __FUNCDNAME__ */
158 } funcname_kind_t;
159
160 /* convenience macros */
161 #define EXPR_BINARY_CASES                  \
162         case EXPR_BINARY_ADD:                  \
163         case EXPR_BINARY_SUB:                  \
164         case EXPR_BINARY_MUL:                  \
165         case EXPR_BINARY_DIV:                  \
166         case EXPR_BINARY_MOD:                  \
167         case EXPR_BINARY_EQUAL:                \
168         case EXPR_BINARY_NOTEQUAL:             \
169         case EXPR_BINARY_LESS:                 \
170         case EXPR_BINARY_LESSEQUAL:            \
171         case EXPR_BINARY_GREATER:              \
172         case EXPR_BINARY_GREATEREQUAL:         \
173         case EXPR_BINARY_BITWISE_AND:          \
174         case EXPR_BINARY_BITWISE_OR:           \
175         case EXPR_BINARY_BITWISE_XOR:          \
176         case EXPR_BINARY_LOGICAL_AND:          \
177         case EXPR_BINARY_LOGICAL_OR:           \
178         case EXPR_BINARY_SHIFTLEFT:            \
179         case EXPR_BINARY_SHIFTRIGHT:           \
180         case EXPR_BINARY_ASSIGN:               \
181         case EXPR_BINARY_MUL_ASSIGN:           \
182         case EXPR_BINARY_DIV_ASSIGN:           \
183         case EXPR_BINARY_MOD_ASSIGN:           \
184         case EXPR_BINARY_ADD_ASSIGN:           \
185         case EXPR_BINARY_SUB_ASSIGN:           \
186         case EXPR_BINARY_SHIFTLEFT_ASSIGN:     \
187         case EXPR_BINARY_SHIFTRIGHT_ASSIGN:    \
188         case EXPR_BINARY_BITWISE_AND_ASSIGN:   \
189         case EXPR_BINARY_BITWISE_XOR_ASSIGN:   \
190         case EXPR_BINARY_BITWISE_OR_ASSIGN:    \
191         case EXPR_BINARY_COMMA:                \
192         case EXPR_BINARY_ISGREATER:            \
193         case EXPR_BINARY_ISGREATEREQUAL:       \
194         case EXPR_BINARY_ISLESS:               \
195         case EXPR_BINARY_ISLESSEQUAL:          \
196         case EXPR_BINARY_ISLESSGREATER:        \
197         case EXPR_BINARY_ISUNORDERED:
198
199 /**
200  * unary expression with mandatory operand
201  */
202 #define EXPR_UNARY_CASES_MANDATORY         \
203         case EXPR_UNARY_NEGATE:                \
204         case EXPR_UNARY_PLUS:                  \
205         case EXPR_UNARY_BITWISE_NEGATE:        \
206         case EXPR_UNARY_NOT:                   \
207         case EXPR_UNARY_DEREFERENCE:           \
208         case EXPR_UNARY_TAKE_ADDRESS:          \
209         case EXPR_UNARY_POSTFIX_INCREMENT:     \
210         case EXPR_UNARY_POSTFIX_DECREMENT:     \
211         case EXPR_UNARY_PREFIX_INCREMENT:      \
212         case EXPR_UNARY_PREFIX_DECREMENT:      \
213         case EXPR_UNARY_CAST:                  \
214         case EXPR_UNARY_CAST_IMPLICIT:         \
215         case EXPR_UNARY_ASSUME:                \
216         case EXPR_UNARY_DELETE:                \
217         case EXPR_UNARY_DELETE_ARRAY:
218
219 /**
220  * unary expression with optional operand
221  */
222 #define EXPR_UNARY_CASES_OPTIONAL \
223         case EXPR_UNARY_THROW:        \
224
225 #define EXPR_UNARY_CASES       \
226         EXPR_UNARY_CASES_MANDATORY \
227         EXPR_UNARY_CASES_OPTIONAL
228
229 /**
230  * The base class of every expression.
231  */
232 struct expression_base_t {
233         expression_kind_t   kind;            /**< The expression kind. */
234         type_t             *type;            /**< The type of the expression. */
235         source_position_t   source_position; /**< The source position of this expression. */
236         bool                parenthesized : 1;
237 #ifndef NDEBUG
238         bool                transformed : 1;     /**< Set if this expression was transformed. */
239 #endif
240 };
241
242 /**
243  * A constant.
244  */
245 struct const_expression_t {
246         expression_base_t  base;
247         union {
248                 long long      int_value;
249                 long double    float_value;
250                 string_t       character;
251                 wide_string_t  wide_character;
252         } v;
253         bool               is_ms_noop;  /**< True, if this constant is the result
254                                              of an microsoft __noop operator */
255 };
256
257 struct string_literal_expression_t {
258         expression_base_t  base;
259         string_t           value;
260 };
261
262 struct funcname_expression_t {
263         expression_base_t  base;
264         funcname_kind_t    kind;
265         string_t           value;     /**< the value once assigned. */
266 };
267
268 struct wide_string_literal_expression_t {
269         expression_base_t  base;
270         wide_string_t      value;
271 };
272
273 struct compound_literal_expression_t {
274         expression_base_t  base;
275         type_t            *type;
276         initializer_t     *initializer;
277 };
278
279 struct builtin_constant_expression_t {
280         expression_base_t  base;
281         expression_t      *value;
282 };
283
284 struct builtin_types_compatible_expression_t {
285         expression_base_t  base;
286         type_t            *left;
287         type_t            *right;
288 };
289
290 struct reference_expression_t {
291         expression_base_t  base;
292         entity_t          *entity;
293 };
294
295 /**
296  * An argument of a call.
297  */
298 struct call_argument_t {
299         expression_t    *expression;  /**< The expression which value is transmitted. */
300         call_argument_t *next;        /**< Links to the next argument of this call. */
301 };
302
303
304 struct call_expression_t {
305         expression_base_t  base;
306         expression_t      *function;  /**< The address of the function to call. */
307         call_argument_t   *arguments; /**< List of arguments of this call. */
308 };
309
310
311 struct unary_expression_t {
312         expression_base_t  base;
313         expression_t      *value;     /**< The unary operand. */
314 };
315
316 struct binary_expression_t {
317         expression_base_t  base;
318         expression_t      *left;
319         expression_t      *right;
320 };
321
322 struct select_expression_t {
323         expression_base_t  base;
324         expression_t      *compound;
325         entity_t          *compound_entry;
326         bool               implicit : 1; /**< compiler generated select
327                                               (for anonymous struct/union) */
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         source_position_t  source_position;
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         variable_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         const_expression_t                    conste;
399         funcname_expression_t                 funcname;
400         string_literal_expression_t           string;
401         wide_string_literal_expression_t      wide_string;
402         compound_literal_expression_t         compound_literal;
403         builtin_constant_expression_t         builtin_constant;
404         builtin_types_compatible_expression_t builtin_types_compatible;
405         reference_expression_t                reference;
406         call_expression_t                     call;
407         unary_expression_t                    unary;
408         binary_expression_t                   binary;
409         select_expression_t                   select;
410         array_access_expression_t             array_access;
411         typeprop_expression_t                 typeprop;
412         offsetof_expression_t                 offsetofe;
413         va_start_expression_t                 va_starte;
414         va_arg_expression_t                   va_arge;
415         va_copy_expression_t                  va_copye;
416         conditional_expression_t              conditional;
417         statement_expression_t                statement;
418         classify_type_expression_t            classify_type;
419         label_address_expression_t            label_address;
420 };
421
422 typedef enum initializer_kind_t {
423         INITIALIZER_VALUE,
424         INITIALIZER_LIST,
425         INITIALIZER_STRING,
426         INITIALIZER_WIDE_STRING,
427         INITIALIZER_DESIGNATOR
428 } initializer_kind_t;
429
430 struct initializer_base_t {
431         initializer_kind_t kind;
432 };
433
434 struct initializer_value_t {
435         initializer_base_t  base;
436         expression_t       *value;
437 };
438
439 struct initializer_list_t {
440         initializer_base_t  base;
441         size_t              len;
442         initializer_t      *initializers[];
443 };
444
445 struct initializer_string_t {
446         initializer_base_t base;
447         string_t           string;
448 };
449
450 struct initializer_wide_string_t {
451         initializer_base_t  base;
452         wide_string_t       string;
453 };
454
455 struct initializer_designator_t {
456         initializer_base_t  base;
457         designator_t       *designator;
458 };
459
460 union initializer_t {
461         initializer_kind_t        kind;
462         initializer_base_t        base;
463         initializer_value_t       value;
464         initializer_list_t        list;
465         initializer_string_t      string;
466         initializer_wide_string_t wide_string;
467         initializer_designator_t  designator;
468 };
469
470 /**
471  * The statement kinds.
472  */
473 typedef enum statement_kind_t {
474         STATEMENT_INVALID,
475         STATEMENT_EMPTY,
476         STATEMENT_COMPOUND,
477         STATEMENT_RETURN,
478         STATEMENT_DECLARATION,
479         STATEMENT_IF,
480         STATEMENT_SWITCH,
481         STATEMENT_EXPRESSION,
482         STATEMENT_CONTINUE,
483         STATEMENT_BREAK,
484         STATEMENT_GOTO,
485         STATEMENT_LABEL,
486         STATEMENT_CASE_LABEL,
487         STATEMENT_WHILE,
488         STATEMENT_DO_WHILE,
489         STATEMENT_FOR,
490         STATEMENT_ASM,
491         STATEMENT_MS_TRY,          /**< MS __try/__finally or __try/__except */
492         STATEMENT_LEAVE            /**< MS __leave */
493 } statement_kind_t;
494
495 /**
496  * The base class of every statement.
497  */
498 struct statement_base_t {
499         statement_kind_t   kind;
500         statement_t       *next;         /**< Point to the next statement in a compound statement. */
501         source_position_t  source_position;
502         statement_t       *parent;       /**< The Parent statement that controls the execution. */
503         bool               reachable;    /**< True, if this statement is reachable. */
504 #ifndef NDEBUG
505         bool               transformed;
506 #endif
507 };
508
509 struct invalid_statement_t {
510         statement_base_t  base;
511 };
512
513 struct empty_statement_t {
514         statement_base_t  base;
515 };
516
517 struct return_statement_t {
518         statement_base_t  base;
519         expression_t     *value;    /**< The return value if any. */
520 };
521
522 struct compound_statement_t {
523         statement_base_t  base;
524         statement_t      *statements;
525         scope_t           scope;
526         bool              stmt_expr; /**< True if this compound statement is a statement expression. */
527 };
528
529 struct declaration_statement_t {
530         statement_base_t  base;
531         entity_t         *declarations_begin;
532         entity_t         *declarations_end;
533 };
534
535 struct if_statement_t {
536         statement_base_t  base;
537         expression_t     *condition;
538         statement_t      *true_statement;
539         statement_t      *false_statement;
540 };
541
542 struct switch_statement_t {
543         statement_base_t        base;
544         expression_t           *expression;
545         statement_t            *body;
546         case_label_statement_t *first_case, *last_case;  /**< List of all cases, including default. */
547         case_label_statement_t *default_label;           /**< The default label if existent. */
548         unsigned long           default_proj_nr;         /**< The Proj-number for the default Proj. */
549 };
550
551 struct goto_statement_t {
552         statement_base_t  base;
553         label_t          *label;         /**< The destination label. */
554         expression_t     *expression;    /**< The expression for an assigned goto. */
555         goto_statement_t *next;          /**< links all goto statements of a function */
556 };
557
558 struct case_label_statement_t {
559         statement_base_t        base;
560         expression_t           *expression;    /**< The case label expression, NULL for default label. */
561         expression_t           *end_range;     /**< For GNUC case a .. b: the end range expression, NULL else. */
562         case_label_statement_t *next;          /**< link to the next case label in switch */
563         statement_t            *statement;
564         long                   first_case;     /**< The folded value of expression. */
565         long                   last_case;      /**< The folded value of end_range. */
566         bool                   is_bad;         /**< If set marked as bad to suppress warnings. */
567         bool                   is_empty_range; /**< If set marked this as an empty range. */
568 };
569
570 struct label_statement_t {
571         statement_base_t   base;
572         label_t           *label;
573         statement_t       *statement;
574         label_statement_t *next;    /**< links all label statements of a function */
575 };
576
577 struct expression_statement_t {
578         statement_base_t  base;
579         expression_t     *expression;
580 };
581
582 struct while_statement_t {
583         statement_base_t  base;
584         expression_t     *condition;
585         statement_t      *body;
586 };
587
588 struct do_while_statement_t {
589         statement_base_t  base;
590         expression_t     *condition;
591         statement_t      *body;
592 };
593
594 struct for_statement_t {
595         statement_base_t  base;
596         expression_t     *initialisation;
597         expression_t     *condition;
598         expression_t     *step;
599         statement_t      *body;
600         scope_t           scope;
601         bool              condition_reachable:1;
602         bool              step_reachable:1;
603 };
604
605 struct asm_argument_t {
606         string_t        constraints;
607         expression_t   *expression;
608         symbol_t       *symbol;
609         asm_argument_t *next;
610 };
611
612 struct asm_clobber_t {
613         string_t       clobber;
614         asm_clobber_t *next;
615 };
616
617 struct asm_statement_t {
618         statement_base_t base;
619         string_t         asm_text;
620         asm_argument_t  *inputs;
621         asm_argument_t  *outputs;
622         asm_clobber_t   *clobbers;
623         bool             is_volatile;
624 };
625
626 struct ms_try_statement_t {
627         statement_base_t  base;
628         statement_t      *try_statement;
629         expression_t     *except_expression; /**< non-null for except, NULL for finally */
630         statement_t      *final_statement;
631 };
632
633 struct leave_statement_t {
634         statement_base_t  base;
635 };
636
637 union statement_t {
638         statement_kind_t         kind;
639         statement_base_t         base;
640         return_statement_t       returns;
641         compound_statement_t     compound;
642         declaration_statement_t  declaration;
643         if_statement_t           ifs;
644         switch_statement_t       switchs;
645         goto_statement_t         gotos;
646         case_label_statement_t   case_label;
647         label_statement_t        label;
648         expression_statement_t   expression;
649         while_statement_t        whiles;
650         do_while_statement_t     do_while;
651         for_statement_t          fors;
652         asm_statement_t          asms;
653         ms_try_statement_t       ms_try;
654         leave_statement_t        leave;
655 };
656
657 struct translation_unit_t {
658         scope_t      scope;
659         statement_t *global_asm;
660 };
661
662 static inline void *_allocate_ast(size_t size)
663 {
664         return obstack_alloc(&ast_obstack, size);
665 }
666
667 static inline bool is_invalid_expression(expression_t *expression)
668 {
669         return expression->base.kind == EXPR_INVALID;
670 }
671
672 static inline bool is_invalid_statement(statement_t *statement)
673 {
674         return statement->base.kind == STATEMENT_INVALID;
675 }
676
677 #define allocate_ast(size)                 _allocate_ast(size)
678
679 #endif