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