change back union stuff and expriment with new union mode for initializers
[cparser] / ast_t.h
1 #ifndef AST_T_H
2 #define AST_T_H
3
4 #include <libfirm/firm_types.h>
5 #include <assert.h>
6
7 #include "ast.h"
8 #include "symbol.h"
9 #include "token_t.h"
10 #include "type.h"
11 #include "adt/obst.h"
12
13 extern struct obstack ast_obstack;
14
15 typedef enum {
16         EXPR_UNKNOWN = 0,
17         EXPR_INVALID,
18         EXPR_REFERENCE,
19         EXPR_CONST,
20         EXPR_STRING_LITERAL,
21         EXPR_CALL,
22         EXPR_UNARY,
23         EXPR_BINARY,
24         EXPR_CONDITIONAL,
25         EXPR_SELECT,
26         EXPR_ARRAY_ACCESS,
27         EXPR_SIZEOF,
28         EXPR_CLASSIFY_TYPE,
29
30         EXPR_FUNCTION,
31         EXPR_PRETTY_FUNCTION,
32         EXPR_BUILTIN_SYMBOL,
33         EXPR_OFFSETOF,
34         EXPR_VA_ARG,
35         EXPR_STATEMENT
36 } expresion_type_t;
37
38 struct context_t {
39         declaration_t   *declarations;
40 };
41
42 struct expression_t {
43         expresion_type_t   type;
44         type_t            *datatype;
45         source_position_t  source_position;
46 };
47
48 struct const_t {
49         expression_t  expression;
50         union {
51                 long long   int_value;
52                 long double float_value;
53         } v;
54 };
55
56 struct string_literal_t {
57         expression_t  expression;
58         const char   *value;
59 };
60
61 struct builtin_symbol_expression_t {
62         expression_t  expression;
63         symbol_t     *symbol;
64 };
65
66 struct reference_expression_t {
67         expression_t   expression;
68         symbol_t      *symbol;
69         declaration_t *declaration;
70 };
71
72 struct call_argument_t {
73         expression_t    *expression;
74         call_argument_t *next;
75 };
76
77 struct call_expression_t {
78         expression_t     expression;
79         expression_t    *function;
80         call_argument_t *arguments;
81 };
82
83 typedef enum {
84         UNEXPR_INVALID = 0,
85         UNEXPR_NEGATE,
86         UNEXPR_PLUS,
87         UNEXPR_BITWISE_NEGATE,
88         UNEXPR_NOT,
89         UNEXPR_DEREFERENCE,
90         UNEXPR_TAKE_ADDRESS,
91         UNEXPR_POSTFIX_INCREMENT,
92         UNEXPR_POSTFIX_DECREMENT,
93         UNEXPR_PREFIX_INCREMENT,
94         UNEXPR_PREFIX_DECREMENT,
95         UNEXPR_CAST
96 } unary_expression_type_t;
97
98 struct unary_expression_t {
99         expression_t             expression;
100         unary_expression_type_t  type;
101         expression_t            *value;
102 };
103
104 typedef enum {
105         BINEXPR_INVALID = 0,
106         BINEXPR_ADD,
107         BINEXPR_SUB,
108         BINEXPR_MUL,
109         BINEXPR_DIV,
110         BINEXPR_MOD,
111         BINEXPR_EQUAL,
112         BINEXPR_NOTEQUAL,
113         BINEXPR_LESS,
114         BINEXPR_LESSEQUAL,
115         BINEXPR_GREATER,
116         BINEXPR_GREATEREQUAL,
117         BINEXPR_BITWISE_AND,
118         BINEXPR_BITWISE_OR,
119         BINEXPR_BITWISE_XOR,
120         BINEXPR_LOGICAL_AND,
121         BINEXPR_LOGICAL_OR,
122         BINEXPR_SHIFTLEFT,
123         BINEXPR_SHIFTRIGHT,
124         BINEXPR_ASSIGN,
125         BINEXPR_MUL_ASSIGN,
126         BINEXPR_DIV_ASSIGN,
127         BINEXPR_MOD_ASSIGN,
128         BINEXPR_ADD_ASSIGN,
129         BINEXPR_SUB_ASSIGN,
130         BINEXPR_SHIFTLEFT_ASSIGN,
131         BINEXPR_SHIFTRIGHT_ASSIGN,
132         BINEXPR_BITWISE_AND_ASSIGN,
133         BINEXPR_BITWISE_XOR_ASSIGN,
134         BINEXPR_BITWISE_OR_ASSIGN,
135         BINEXPR_COMMA
136 } binary_expression_type_t;
137
138 struct binary_expression_t {
139         expression_t              expression;
140         binary_expression_type_t  type;
141         expression_t             *left;
142         expression_t             *right;
143 };
144
145 struct select_expression_t {
146         expression_t   expression;
147         expression_t  *compound;
148         symbol_t      *symbol;
149
150         declaration_t *compound_entry;
151 };
152
153 struct array_access_expression_t {
154         expression_t  expression;
155         expression_t *array_ref;
156         expression_t *index;
157 };
158
159 struct sizeof_expression_t {
160         expression_t  expression;
161         type_t       *type;
162         expression_t *size_expression;
163 };
164
165 struct designator_t {
166         symbol_t     *symbol;
167         expression_t *array_access;
168         designator_t *next;
169 };
170
171 struct offsetof_expression_t {
172         expression_t  expression;
173         type_t       *type;
174         designator_t *designator;
175 };
176
177 struct va_arg_expression_t {
178         expression_t  expression;
179         expression_t *arg;
180         type_t       *type;
181 };
182
183 struct conditional_expression_t {
184         expression_t  expression;
185         expression_t *condition;
186         expression_t *true_expression;
187         expression_t *false_expression;
188 };
189
190 struct statement_expression_t {
191         expression_t  expression;
192         statement_t  *statement;
193 };
194
195 struct classify_type_expression_t {
196         expression_t  expression;
197         expression_t *type_expression;
198 };
199
200 typedef enum {
201         STORAGE_CLASS_NONE,
202         STORAGE_CLASS_TYPEDEF,
203         STORAGE_CLASS_EXTERN,
204         STORAGE_CLASS_STATIC,
205         STORAGE_CLASS_AUTO,
206         STORAGE_CLASS_REGISTER,
207         STORAGE_CLASS_ENUM_ENTRY
208 } storage_class_t;
209
210 typedef enum {
211         NAMESPACE_NORMAL,
212         NAMESPACE_STRUCT,
213         NAMESPACE_UNION,
214         NAMESPACE_ENUM,
215         NAMESPACE_LABEL
216 } namespace_t;
217
218 typedef enum {
219         INITIALIZER_VALUE,
220         INITIALIZER_LIST,
221         INITIALIZER_STRING,
222         INITIALIZER_COUNT
223 } initializer_type_t;
224
225 struct initializer_base_t {
226         initializer_type_t type;
227 };
228
229 struct initializer_value_t {
230         initializer_base_t  initializer;
231         expression_t       *value;
232 };
233
234 struct initializer_list_t {
235         initializer_base_t  initializer;
236         size_t              len;
237         initializer_t      *initializers[];
238 };
239
240 struct initializer_string_t {
241         initializer_base_t  initializer;
242         const char         *string;
243 };
244
245 union initializer_t {
246         initializer_type_t   type;
247         initializer_base_t   base;
248         initializer_value_t  value;
249         initializer_list_t   list;
250         initializer_string_t string;
251 };
252
253 struct declaration_t {
254         unsigned char       namespc;
255         unsigned char       storage_class;
256         unsigned int        address_taken : 1;
257         unsigned int        is_inline     : 1;
258         type_t             *type;
259         symbol_t           *symbol;
260         source_position_t   source_position;
261         union {
262                 bool            is_defined;
263                 statement_t    *statement;
264                 initializer_t  *initializer;
265                 expression_t   *enum_value;
266         } init;
267         context_t           context;
268         context_t          *parent_context;
269
270         /** next declaration in a context */
271         declaration_t      *next;
272         /** next declaration with same symbol */
273         declaration_t      *symbol_next;
274
275         unsigned char       declaration_type; /* used in ast2firm module */
276         union {
277                 unsigned int    value_number;     /* used in ast2firm module */
278                 ir_entity      *entity;           /* used in ast2firm module */
279                 ir_node        *block;            /* used in ast2firm module */
280                 tarval         *enum_val;         /* used in ast2firm module */
281         } v;
282 };
283
284 typedef enum {
285         STATEMENT_INVALID,
286         STATEMENT_COMPOUND,
287         STATEMENT_RETURN,
288         STATEMENT_DECLARATION,
289         STATEMENT_IF,
290         STATEMENT_SWITCH,
291         STATEMENT_EXPRESSION,
292         STATEMENT_CONTINUE,
293         STATEMENT_BREAK,
294         STATEMENT_GOTO,
295         STATEMENT_LABEL,
296         STATEMENT_CASE_LABEL,
297         STATEMENT_WHILE,
298         STATEMENT_DO_WHILE,
299         STATEMENT_FOR
300 } statement_type_t;
301
302 struct statement_t {
303         statement_type_t   type;
304         statement_t       *next;
305         source_position_t  source_position;
306 };
307
308 struct return_statement_t {
309         statement_t   statement;
310         expression_t *return_value;
311 };
312
313 struct compound_statement_t {
314         statement_t  statement;
315         statement_t *statements;
316         context_t    context;
317 };
318
319 struct declaration_statement_t {
320         statement_t    statement;
321         declaration_t *declarations_begin;
322         declaration_t *declarations_end;
323 };
324
325 struct if_statement_t {
326         statement_t   statement;
327         expression_t *condition;
328         statement_t  *true_statement;
329         statement_t  *false_statement;
330 };
331
332 struct switch_statement_t {
333         statement_t   statement;
334         expression_t *expression;
335         statement_t  *body;
336 };
337
338 struct goto_statement_t {
339         statement_t    statement;
340         declaration_t *label;
341 };
342
343 struct case_label_statement_t {
344         statement_t   statement;
345         expression_t *expression;
346         statement_t  *label_statement;
347 };
348
349 struct label_statement_t {
350         statement_t    statement;
351         declaration_t *label;
352         statement_t   *label_statement;
353 };
354
355 struct expression_statement_t {
356         statement_t   statement;
357         expression_t *expression;
358 };
359
360 struct while_statement_t {
361         statement_t   statement;
362         expression_t *condition;
363         statement_t  *body;
364 };
365
366 struct do_while_statement_t {
367         statement_t   statement;
368         expression_t *condition;
369         statement_t  *body;
370 };
371
372 struct for_statement_t {
373         statement_t   statement;
374         expression_t  *initialisation;
375         expression_t  *condition;
376         expression_t  *step;
377         statement_t   *body;
378         context_t      context;
379 };
380
381 struct translation_unit_t {
382         context_t context;
383 };
384
385 static inline
386 void *_allocate_ast(size_t size)
387 {
388         return obstack_alloc(&ast_obstack, size);
389 }
390
391 #define allocate_ast(size)                 _allocate_ast(size)
392
393 #endif