Unify the precedence lists of the parser and the AST printer.
[cparser] / ast_t.h
1 /*
2  * This file is part of cparser.
3  * Copyright (C) 2007-2008 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 "adt/obst.h"
31
32 /** The AST obstack contains all data that must stay in the AST. */
33 extern struct obstack ast_obstack;
34
35 /**
36  * Operator precedence classes
37  */
38 typedef enum precedence_t {
39         PREC_BOTTOM,
40         PREC_EXPRESSION,     /* ,                                  left to right */
41         PREC_ASSIGNMENT,     /* = += -= *= /= %= <<= >>= &= ^= |=  right to left */
42         PREC_CONDITIONAL,    /* ?:                                 right to left */
43         PREC_LOGICAL_OR,     /* ||                                 left to right */
44         PREC_LOGICAL_AND,    /* &&                                 left to right */
45         PREC_OR,             /* |                                  left to right */
46         PREC_XOR,            /* ^                                  left to right */
47         PREC_AND,            /* &                                  left to right */
48         PREC_EQUALITY,       /* == !=                              left to right */
49         PREC_RELATIONAL,     /* < <= > >=                          left to right */
50         PREC_SHIFT,          /* << >>                              left to right */
51         PREC_ADDITIVE,       /* + -                                left to right */
52         PREC_MULTIPLICATIVE, /* * / %                              left to right */
53         PREC_CAST,           /* (type)                             right to left */
54         PREC_UNARY,          /* ! ~ ++ -- + - * & sizeof           right to left */
55         PREC_POSTFIX,        /* () [] -> .                         left to right */
56         PREC_PRIMARY,
57         PREC_TOP
58 } precedence_t;
59
60 /**
61  * Expression kinds.
62  */
63 typedef enum expression_kind_t {
64         EXPR_UNKNOWN = 0,
65         EXPR_INVALID,
66         EXPR_REFERENCE,
67         EXPR_CONST,
68         EXPR_CHARACTER_CONSTANT,
69         EXPR_WIDE_CHARACTER_CONSTANT,
70         EXPR_STRING_LITERAL,
71         EXPR_WIDE_STRING_LITERAL,
72         EXPR_COMPOUND_LITERAL,
73         EXPR_CALL,
74         EXPR_CONDITIONAL,
75         EXPR_SELECT,
76         EXPR_ARRAY_ACCESS,
77         EXPR_SIZEOF,
78         EXPR_CLASSIFY_TYPE,
79         EXPR_ALIGNOF,
80
81         EXPR_FUNCNAME,
82         EXPR_BUILTIN_SYMBOL,
83         EXPR_BUILTIN_CONSTANT_P,
84         EXPR_BUILTIN_PREFETCH,
85         EXPR_OFFSETOF,
86         EXPR_VA_START,
87         EXPR_VA_ARG,
88         EXPR_STATEMENT,
89         EXPR_LABEL_ADDRESS, /**< GCC extension &&label operator */
90
91         EXPR_UNARY_FIRST,
92         EXPR_UNARY_NEGATE = EXPR_UNARY_FIRST,
93         EXPR_UNARY_PLUS,
94         EXPR_UNARY_BITWISE_NEGATE,
95         EXPR_UNARY_NOT,
96         EXPR_UNARY_DEREFERENCE,
97         EXPR_UNARY_TAKE_ADDRESS,
98         EXPR_UNARY_POSTFIX_INCREMENT,
99         EXPR_UNARY_POSTFIX_DECREMENT,
100         EXPR_UNARY_PREFIX_INCREMENT,
101         EXPR_UNARY_PREFIX_DECREMENT,
102         EXPR_UNARY_CAST,
103         EXPR_UNARY_CAST_IMPLICIT, /**< compiler generated cast */
104         EXPR_UNARY_ASSUME,        /**< MS __assume() */
105         EXPR_UNARY_LAST = EXPR_UNARY_ASSUME,
106
107         EXPR_BINARY_FIRST,
108         EXPR_BINARY_ADD = EXPR_BINARY_FIRST,
109         EXPR_BINARY_SUB,
110         EXPR_BINARY_MUL,
111         EXPR_BINARY_DIV,
112         EXPR_BINARY_MOD,
113         EXPR_BINARY_EQUAL,
114         EXPR_BINARY_NOTEQUAL,
115         EXPR_BINARY_LESS,
116         EXPR_BINARY_LESSEQUAL,
117         EXPR_BINARY_GREATER,
118         EXPR_BINARY_GREATEREQUAL,
119         EXPR_BINARY_BITWISE_AND,
120         EXPR_BINARY_BITWISE_OR,
121         EXPR_BINARY_BITWISE_XOR,
122         EXPR_BINARY_LOGICAL_AND,
123         EXPR_BINARY_LOGICAL_OR,
124         EXPR_BINARY_SHIFTLEFT,
125         EXPR_BINARY_SHIFTRIGHT,
126         EXPR_BINARY_ASSIGN,
127         EXPR_BINARY_MUL_ASSIGN,
128         EXPR_BINARY_DIV_ASSIGN,
129         EXPR_BINARY_MOD_ASSIGN,
130         EXPR_BINARY_ADD_ASSIGN,
131         EXPR_BINARY_SUB_ASSIGN,
132         EXPR_BINARY_SHIFTLEFT_ASSIGN,
133         EXPR_BINARY_SHIFTRIGHT_ASSIGN,
134         EXPR_BINARY_BITWISE_AND_ASSIGN,
135         EXPR_BINARY_BITWISE_XOR_ASSIGN,
136         EXPR_BINARY_BITWISE_OR_ASSIGN,
137         EXPR_BINARY_COMMA,
138
139         EXPR_BINARY_BUILTIN_EXPECT,
140         EXPR_BINARY_ISGREATER,
141         EXPR_BINARY_ISGREATEREQUAL,
142         EXPR_BINARY_ISLESS,
143         EXPR_BINARY_ISLESSEQUAL,
144         EXPR_BINARY_ISLESSGREATER,
145         EXPR_BINARY_ISUNORDERED,
146         EXPR_BINARY_LAST = EXPR_BINARY_ISUNORDERED,
147 } expression_kind_t;
148
149 typedef enum funcname_kind_t {
150         FUNCNAME_FUNCTION,           /**< C99 __func__, older __FUNCTION__ */
151         FUNCNAME_PRETTY_FUNCTION,    /**< GNUC __PRETTY_FUNCTION__ */
152         FUNCNAME_FUNCSIG,            /**< MS __FUNCSIG__ */
153         FUNCNAME_FUNCDNAME           /**< MS __FUNCDNAME__ */
154 } funcname_kind_t;
155
156 /* convenience macros */
157 #define EXPR_BINARY_CASES                  \
158         case EXPR_BINARY_ADD:                  \
159         case EXPR_BINARY_SUB:                  \
160         case EXPR_BINARY_MUL:                  \
161         case EXPR_BINARY_DIV:                  \
162         case EXPR_BINARY_MOD:                  \
163         case EXPR_BINARY_EQUAL:                \
164         case EXPR_BINARY_NOTEQUAL:             \
165         case EXPR_BINARY_LESS:                 \
166         case EXPR_BINARY_LESSEQUAL:            \
167         case EXPR_BINARY_GREATER:              \
168         case EXPR_BINARY_GREATEREQUAL:         \
169         case EXPR_BINARY_BITWISE_AND:          \
170         case EXPR_BINARY_BITWISE_OR:           \
171         case EXPR_BINARY_BITWISE_XOR:          \
172         case EXPR_BINARY_LOGICAL_AND:          \
173         case EXPR_BINARY_LOGICAL_OR:           \
174         case EXPR_BINARY_SHIFTLEFT:            \
175         case EXPR_BINARY_SHIFTRIGHT:           \
176         case EXPR_BINARY_ASSIGN:               \
177         case EXPR_BINARY_MUL_ASSIGN:           \
178         case EXPR_BINARY_DIV_ASSIGN:           \
179         case EXPR_BINARY_MOD_ASSIGN:           \
180         case EXPR_BINARY_ADD_ASSIGN:           \
181         case EXPR_BINARY_SUB_ASSIGN:           \
182         case EXPR_BINARY_SHIFTLEFT_ASSIGN:     \
183         case EXPR_BINARY_SHIFTRIGHT_ASSIGN:    \
184         case EXPR_BINARY_BITWISE_AND_ASSIGN:   \
185         case EXPR_BINARY_BITWISE_XOR_ASSIGN:   \
186         case EXPR_BINARY_BITWISE_OR_ASSIGN:    \
187         case EXPR_BINARY_COMMA:                \
188         case EXPR_BINARY_BUILTIN_EXPECT:       \
189         case EXPR_BINARY_ISGREATER:            \
190         case EXPR_BINARY_ISGREATEREQUAL:       \
191         case EXPR_BINARY_ISLESS:               \
192         case EXPR_BINARY_ISLESSEQUAL:          \
193         case EXPR_BINARY_ISLESSGREATER:        \
194         case EXPR_BINARY_ISUNORDERED:
195
196 #define EXPR_UNARY_CASES                   \
197         case EXPR_UNARY_NEGATE:                \
198         case EXPR_UNARY_PLUS:                  \
199         case EXPR_UNARY_BITWISE_NEGATE:        \
200         case EXPR_UNARY_NOT:                   \
201         case EXPR_UNARY_DEREFERENCE:           \
202         case EXPR_UNARY_TAKE_ADDRESS:          \
203         case EXPR_UNARY_POSTFIX_INCREMENT:     \
204         case EXPR_UNARY_POSTFIX_DECREMENT:     \
205         case EXPR_UNARY_PREFIX_INCREMENT:      \
206         case EXPR_UNARY_PREFIX_DECREMENT:      \
207         case EXPR_UNARY_CAST:                  \
208         case EXPR_UNARY_CAST_IMPLICIT:         \
209         case EXPR_UNARY_ASSUME:
210
211 /**
212  * A scope containing declarations.
213  */
214 struct scope_t {
215         declaration_t *declarations;      /**< List of declarations in this scope. */
216         declaration_t *last_declaration;  /**< last declaration in this scope. */
217         scope_t       *parent;            /**< points to the parent scope. */
218         unsigned      depth;              /**< while parsing, the depth of this scope in the scope stack. */
219 };
220
221 struct expression_base_t {
222         expression_kind_t   kind;
223         type_t             *type;
224         source_position_t   source_position;
225 #ifndef NDEBUG
226         bool                transformed;
227 #endif
228 };
229
230 struct const_expression_t {
231         expression_base_t  base;
232         union {
233                 long long      int_value;
234                 long double    float_value;
235                 string_t       character;
236                 wide_string_t  wide_character;
237         } v;
238         bool               is_ms_noop;  /**< True, if this constant is the result
239                                              of an microsoft __noop operator */
240 };
241
242 struct string_literal_expression_t {
243         expression_base_t  base;
244         string_t           value;
245 };
246
247 struct funcname_expression_t {
248         expression_base_t  base;
249         funcname_kind_t    kind;
250         string_t           value;     /**< the value once assigned. */
251 };
252
253 struct wide_string_literal_expression_t {
254         expression_base_t  base;
255         wide_string_t      value;
256 };
257
258 struct compound_literal_expression_t {
259         expression_base_t  base;
260         type_t            *type;
261         initializer_t     *initializer;
262 };
263
264 struct builtin_symbol_expression_t {
265         expression_base_t  base;
266         symbol_t          *symbol;
267 };
268
269 struct builtin_constant_expression_t {
270         expression_base_t  base;
271         expression_t      *value;
272 };
273
274 struct builtin_prefetch_expression_t {
275         expression_base_t  base;
276         expression_t      *adr;
277         expression_t      *rw;
278         expression_t      *locality;
279 };
280
281 struct reference_expression_t {
282         expression_base_t  base;
283         declaration_t     *declaration;
284 };
285
286 struct call_argument_t {
287         expression_t    *expression;
288         call_argument_t *next;
289 };
290
291 struct call_expression_t {
292         expression_base_t  base;
293         expression_t      *function;
294         call_argument_t   *arguments;
295 };
296
297 struct unary_expression_t {
298         expression_base_t  base;
299         expression_t      *value;
300 };
301
302 struct binary_expression_t {
303         expression_base_t  base;
304         expression_t      *left;
305         expression_t      *right;
306 };
307
308 struct select_expression_t {
309         expression_base_t  base;
310         expression_t      *compound;
311         declaration_t     *compound_entry;
312 };
313
314 struct array_access_expression_t {
315         expression_base_t  base;
316         expression_t      *array_ref;
317         expression_t      *index;
318         bool               flipped; /**< index/ref was written in a 5[a] way */
319 };
320
321 struct typeprop_expression_t {
322         expression_base_t  base;
323         type_t            *type;
324         expression_t      *tp_expression;
325 };
326
327 struct designator_t {
328         source_position_t  source_position;
329         symbol_t          *symbol;
330         expression_t      *array_index;
331         designator_t      *next;
332 };
333
334 struct offsetof_expression_t {
335         expression_base_t  base;
336         type_t            *type;
337         designator_t      *designator;
338 };
339
340 struct va_start_expression_t {
341         expression_base_t  base;
342         expression_t      *ap;
343         declaration_t     *parameter;
344 };
345
346 struct va_arg_expression_t {
347         expression_base_t  base;
348         expression_t      *ap;
349 };
350
351 struct conditional_expression_t {
352         expression_base_t  base;
353         expression_t      *condition;
354         expression_t      *true_expression;
355         expression_t      *false_expression;
356 };
357
358 struct statement_expression_t {
359         expression_base_t  base;
360         statement_t       *statement;
361 };
362
363 struct classify_type_expression_t {
364         expression_base_t  base;
365         expression_t      *type_expression;
366 };
367
368 struct label_address_expression_t {
369         expression_base_t  base;
370         declaration_t     *declaration;
371 };
372
373 union expression_t {
374         expression_kind_t                kind;
375         expression_base_t                base;
376         const_expression_t               conste;
377         funcname_expression_t            funcname;
378         string_literal_expression_t      string;
379         wide_string_literal_expression_t wide_string;
380         compound_literal_expression_t    compound_literal;
381         builtin_symbol_expression_t      builtin_symbol;
382         builtin_constant_expression_t    builtin_constant;
383         builtin_prefetch_expression_t    builtin_prefetch;
384         reference_expression_t           reference;
385         call_expression_t                call;
386         unary_expression_t               unary;
387         binary_expression_t              binary;
388         select_expression_t              select;
389         array_access_expression_t        array_access;
390         typeprop_expression_t            typeprop;
391         offsetof_expression_t            offsetofe;
392         va_start_expression_t            va_starte;
393         va_arg_expression_t              va_arge;
394         conditional_expression_t         conditional;
395         statement_expression_t           statement;
396         classify_type_expression_t       classify_type;
397         label_address_expression_t       label_address;
398 };
399
400 typedef enum storage_class_tag_t {
401         STORAGE_CLASS_NONE,
402         STORAGE_CLASS_EXTERN,
403         STORAGE_CLASS_STATIC,
404         STORAGE_CLASS_TYPEDEF,
405         STORAGE_CLASS_AUTO,
406         STORAGE_CLASS_REGISTER,
407         STORAGE_CLASS_ENUM_ENTRY,
408         STORAGE_CLASS_THREAD,
409         STORAGE_CLASS_THREAD_EXTERN,
410         STORAGE_CLASS_THREAD_STATIC,
411 } storage_class_tag_t;
412
413 typedef enum namespace_t {
414         NAMESPACE_NORMAL,
415         NAMESPACE_STRUCT,
416         NAMESPACE_UNION,
417         NAMESPACE_ENUM,
418         NAMESPACE_LABEL,
419         NAMESPACE_LOCAL_LABEL
420 } namespace_t;
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  * GNU attributes.
472  */
473 typedef enum gnu_attribute_kind_t {
474         GNU_AK_CONST,
475         GNU_AK_VOLATILE,
476         GNU_AK_CDECL,
477         GNU_AK_STDCALL,
478         GNU_AK_FASTCALL,
479         GNU_AK_DEPRECATED,
480         GNU_AK_NOINLINE,
481         GNU_AK_NORETURN,
482         GNU_AK_NAKED,
483         GNU_AK_PURE,
484         GNU_AK_ALWAYS_INLINE,
485         GNU_AK_MALLOC,
486         GNU_AK_WEAK,
487         GNU_AK_CONSTRUCTOR,
488         GNU_AK_DESTRUCTOR,
489         GNU_AK_NOTHROW,
490         GNU_AK_TRANSPARENT_UNION,
491         GNU_AK_COMMON,
492         GNU_AK_NOCOMMON,
493         GNU_AK_PACKED,
494         GNU_AK_SHARED,
495         GNU_AK_NOTSHARED,
496         GNU_AK_USED,
497         GNU_AK_UNUSED,
498         GNU_AK_NO_INSTRUMENT_FUNCTION,
499         GNU_AK_WARN_UNUSED_RESULT,
500         GNU_AK_LONGCALL,
501         GNU_AK_SHORTCALL,
502         GNU_AK_LONG_CALL,
503         GNU_AK_SHORT_CALL,
504         GNU_AK_FUNCTION_VECTOR,
505         GNU_AK_INTERRUPT,
506         GNU_AK_INTERRUPT_HANDLER,
507         GNU_AK_NMI_HANDLER,
508         GNU_AK_NESTING,
509         GNU_AK_NEAR,
510         GNU_AK_FAR,
511         GNU_AK_SIGNAL,
512         GNU_AK_EIGTHBIT_DATA,
513         GNU_AK_TINY_DATA,
514         GNU_AK_SAVEALL,
515         GNU_AK_FLATTEN,
516         GNU_AK_SSEREGPARM,
517         GNU_AK_EXTERNALLY_VISIBLE,
518         GNU_AK_RETURN_TWICE,
519         GNU_AK_MAY_ALIAS,
520         GNU_AK_MS_STRUCT,
521         GNU_AK_GCC_STRUCT,
522         GNU_AK_DLLIMPORT,
523         GNU_AK_DLLEXPORT,
524         GNU_AK_ALIGNED,
525         GNU_AK_ALIAS,
526         GNU_AK_SECTION,
527         GNU_AK_FORMAT,
528         GNU_AK_FORMAT_ARG,
529         GNU_AK_WEAKREF,
530         GNU_AK_NONNULL,
531         GNU_AK_TLS_MODEL,
532         GNU_AK_VISIBILITY,
533         GNU_AK_REGPARM,
534         GNU_AK_MODEL,
535         GNU_AK_MODE,
536         GNU_AK_TRAP_EXIT,
537         GNU_AK_SP_SWITCH,
538         GNU_AK_SENTINEL,
539         GNU_AK_LAST
540 } gnu_attribute_kind_t;
541
542 /**
543  * Extended microsoft modifier.
544  */
545 typedef enum decl_modifier_t {
546         DM_DLLIMPORT         = 1 <<  0,
547         DM_DLLEXPORT         = 1 <<  1,
548         DM_THREAD            = 1 <<  2,
549         DM_NAKED             = 1 <<  3,
550         DM_MICROSOFT_INLINE  = 1 <<  4,
551         DM_FORCEINLINE       = 1 <<  5,
552         DM_SELECTANY         = 1 <<  6,
553         DM_NOTHROW           = 1 <<  7,
554         DM_NOVTABLE          = 1 <<  8,
555         DM_NORETURN          = 1 <<  9,
556         DM_NOINLINE          = 1 << 10,
557         DM_RESTRICT          = 1 << 11,
558         DM_NOALIAS           = 1 << 12,
559         DM_PACKED            = 1 << 13,
560         DM_TRANSPARENT_UNION = 1 << 14,
561         DM_CONST             = 1 << 15,
562         DM_PURE              = 1 << 16,
563         DM_CONSTRUCTOR       = 1 << 17,
564         DM_DESTRUCTOR        = 1 << 18,
565         DM_UNUSED            = 1 << 19,
566         DM_USED              = 1 << 20,
567         DM_CDECL             = 1 << 21,
568         DM_FASTCALL          = 1 << 22,
569         DM_STDCALL           = 1 << 23,
570         DM_THISCALL          = 1 << 24,
571         DM_DEPRECATED        = 1 << 25
572 } decl_modifier_t;
573
574 typedef unsigned decl_modifiers_t;
575
576 struct declaration_t {
577         unsigned char       namespc;
578         unsigned char       declared_storage_class;
579         unsigned char       storage_class;
580         unsigned char       alignment;          /**< Alignment of the declaration, 0 for default. */
581         decl_modifiers_t    modifiers;          /**< modifiers. */
582         const char         *deprecated_string;  /**< MS deprecated string if any. */
583         symbol_t           *get_property_sym;   /**< MS get property. */
584         symbol_t           *put_property_sym;   /**< MS put property. */
585         unsigned int        address_taken : 1;  /**< Set if the address of this declaration was taken. */
586         unsigned int        is_inline     : 1;
587         unsigned int        used          : 1;  /**< Set if the declaration is used. */
588         unsigned int        read          : 1;
589         unsigned int        implicit      : 1;  /**< Set for implicit (not found in source code) declarations. */
590         unsigned int        need_closure  : 1;  /**< Inner function needs closure. */
591         unsigned int        goto_to_outer : 1;  /**< Inner function has goto to outer function. */
592         type_t             *type;
593         il_size_t           offset;             /**< The offset of this member inside a compound. */
594         symbol_t           *symbol;
595         source_position_t   source_position;
596         union {
597                 bool            complete;           /**< used to indicate whether struct/union types are already defined or if just the name is declared */
598                 statement_t    *statement;
599                 initializer_t  *initializer;
600                 expression_t   *enum_value;
601         } init;
602         scope_t             scope;              /**< The scope that this declaration opens. */
603         scope_t            *parent_scope;       /**< The parent scope where this declaration lives. */
604
605         /** next declaration in a scope */
606         declaration_t      *next;
607         /** next declaration with same symbol */
608         declaration_t      *symbol_next;
609
610         /* the following fields are used in ast2firm module */
611         unsigned char       declaration_kind;
612         union {
613                 unsigned int  value_number;
614                 ir_entity    *entity;
615                 ir_node      *block;
616                 ir_node      *vla_base;
617                 tarval       *enum_val;
618                 ir_type      *irtype;
619         } v;
620 };
621
622 typedef enum statement_kind_t {
623         STATEMENT_INVALID,
624         STATEMENT_EMPTY,
625         STATEMENT_COMPOUND,
626         STATEMENT_RETURN,
627         STATEMENT_DECLARATION,
628         STATEMENT_IF,
629         STATEMENT_SWITCH,
630         STATEMENT_EXPRESSION,
631         STATEMENT_CONTINUE,
632         STATEMENT_BREAK,
633         STATEMENT_GOTO,
634         STATEMENT_LABEL,
635         STATEMENT_CASE_LABEL,
636         STATEMENT_WHILE,
637         STATEMENT_DO_WHILE,
638         STATEMENT_FOR,
639         STATEMENT_ASM,
640         STATEMENT_MS_TRY,          /**< MS __try/__finally or __try/__except */
641         STATEMENT_LEAVE            /**< MS __leave */
642 } statement_kind_t;
643
644 struct statement_base_t {
645         statement_kind_t   kind;
646         statement_t       *next;
647         source_position_t  source_position;
648         statement_t       *parent;
649         bool               reachable;
650 #ifndef NDEBUG
651         bool               transformed;
652 #endif
653 };
654
655 struct invalid_statement_t {
656         statement_base_t  base;
657 };
658
659 struct empty_statement_t {
660         statement_base_t  base;
661 };
662
663 struct return_statement_t {
664         statement_base_t  base;
665         expression_t     *value;
666 };
667
668 struct compound_statement_t {
669         statement_base_t  base;
670         statement_t      *statements;
671         scope_t           scope;
672 };
673
674 struct declaration_statement_t {
675         statement_base_t  base;
676         declaration_t    *declarations_begin;
677         declaration_t    *declarations_end;
678 };
679
680 struct if_statement_t {
681         statement_base_t  base;
682         expression_t     *condition;
683         statement_t      *true_statement;
684         statement_t      *false_statement;
685 };
686
687 struct switch_statement_t {
688         statement_base_t        base;
689         expression_t           *expression;
690         statement_t            *body;
691         case_label_statement_t *first_case, *last_case;  /**< List of all cases, including default. */
692         case_label_statement_t *default_label;           /**< The default label if existent. */
693         unsigned long           default_proj_nr;         /**< The Proj-number for the default Proj. */
694 };
695
696 struct goto_statement_t {
697         statement_base_t  base;
698         declaration_t    *label;         /**< The destination label. */
699         expression_t     *expression;    /**< The expression for an assigned goto. */
700         goto_statement_t *next;          /**< links all goto statements of a function */
701 };
702
703 struct case_label_statement_t {
704         statement_base_t        base;
705         expression_t           *expression;    /**< The case label expression, NULL for default label. */
706         expression_t           *end_range;     /**< For GNUC case a .. b: the end range expression, NULL else. */
707         case_label_statement_t *next;          /**< link to the next case label in switch */
708         statement_t            *statement;
709         long                   first_case;     /**< The folded value of expression. */
710         long                   last_case;      /**< The folded value of end_range. */
711         bool                   is_bad;         /**< If set marked as bad to suppress warnings. */
712         bool                   is_empty_range; /**< If set marked this as an empty range. */
713 };
714
715 struct label_statement_t {
716         statement_base_t   base;
717         declaration_t     *label;
718         statement_t       *statement;
719         label_statement_t *next;    /**< links all label statements of a function */
720 };
721
722 struct expression_statement_t {
723         statement_base_t  base;
724         expression_t     *expression;
725 };
726
727 struct while_statement_t {
728         statement_base_t  base;
729         expression_t     *condition;
730         statement_t      *body;
731 };
732
733 struct do_while_statement_t {
734         statement_base_t  base;
735         expression_t     *condition;
736         statement_t      *body;
737 };
738
739 struct for_statement_t {
740         statement_base_t  base;
741         expression_t     *initialisation;
742         expression_t     *condition;
743         expression_t     *step;
744         statement_t      *body;
745         scope_t           scope;
746         bool              condition_reachable:1;
747         bool              step_reachable:1;
748 };
749
750 struct asm_argument_t {
751         string_t        constraints;
752         expression_t   *expression;
753         symbol_t       *symbol;
754         asm_argument_t *next;
755 };
756
757 struct asm_clobber_t {
758         string_t       clobber;
759         asm_clobber_t *next;
760 };
761
762 struct asm_statement_t {
763         statement_base_t base;
764         string_t         asm_text;
765         asm_argument_t  *inputs;
766         asm_argument_t  *outputs;
767         asm_clobber_t   *clobbers;
768         bool             is_volatile;
769 };
770
771 struct ms_try_statement_t {
772         statement_base_t  base;
773         statement_t      *try_statement;
774         expression_t     *except_expression; /**< non-null for except, NULL for finally */
775         statement_t      *final_statement;
776 };
777
778 struct leave_statement_t {
779         statement_base_t  base;
780 };
781
782 union statement_t {
783         statement_kind_t         kind;
784         statement_base_t         base;
785         return_statement_t       returns;
786         compound_statement_t     compound;
787         declaration_statement_t  declaration;
788         if_statement_t           ifs;
789         switch_statement_t       switchs;
790         goto_statement_t         gotos;
791         case_label_statement_t   case_label;
792         label_statement_t        label;
793         expression_statement_t   expression;
794         while_statement_t        whiles;
795         do_while_statement_t     do_while;
796         for_statement_t          fors;
797         asm_statement_t          asms;
798         ms_try_statement_t       ms_try;
799         leave_statement_t        leave;
800 };
801
802 struct translation_unit_t {
803         scope_t      scope;
804         statement_t *global_asm;
805 };
806
807 static inline
808 void *_allocate_ast(size_t size)
809 {
810         return obstack_alloc(&ast_obstack, size);
811 }
812
813 static inline
814 bool is_invalid_expression(expression_t *expression)
815 {
816         return expression->base.kind == EXPR_INVALID;
817 }
818
819 static inline
820 bool is_invalid_statement(statement_t *statement)
821 {
822         return statement->base.kind == STATEMENT_INVALID;
823 }
824
825
826 #define allocate_ast(size)                 _allocate_ast(size)
827
828 #endif