Add support for variadic arguments.
[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_WIDE_STRING_LITERAL,
22         EXPR_CALL,
23         EXPR_UNARY,
24         EXPR_BINARY,
25         EXPR_CONDITIONAL,
26         EXPR_SELECT,
27         EXPR_ARRAY_ACCESS,
28         EXPR_SIZEOF,
29         EXPR_CLASSIFY_TYPE,
30
31         EXPR_FUNCTION,
32         EXPR_PRETTY_FUNCTION,
33         EXPR_BUILTIN_SYMBOL,
34         EXPR_OFFSETOF,
35         EXPR_VA_START,
36         EXPR_VA_ARG,
37         EXPR_STATEMENT
38 } expression_type_t;
39
40 struct context_t {
41         declaration_t   *declarations;
42 };
43
44 struct expression_base_t {
45         expression_type_t   type;
46         type_t             *datatype;
47         source_position_t   source_position;
48 };
49
50 struct const_expression_t {
51         expression_base_t  expression;
52         union {
53                 long long   int_value;
54                 long double float_value;
55         } v;
56 };
57
58 struct string_literal_expression_t {
59         expression_base_t  expression;
60         const char        *value;
61 };
62
63 struct wide_string_literal_expression_t {
64         expression_base_t  expression;
65         wide_string_t      value;
66 };
67
68 struct builtin_symbol_expression_t {
69         expression_base_t  expression;
70         symbol_t          *symbol;
71 };
72
73 struct reference_expression_t {
74         expression_base_t  expression;
75         symbol_t          *symbol;
76         declaration_t     *declaration;
77 };
78
79 struct call_argument_t {
80         expression_t    *expression;
81         call_argument_t *next;
82 };
83
84 struct call_expression_t {
85         expression_base_t  expression;
86         expression_t      *function;
87         call_argument_t   *arguments;
88 };
89
90 typedef enum {
91         UNEXPR_INVALID = 0,
92         UNEXPR_NEGATE,
93         UNEXPR_PLUS,
94         UNEXPR_BITWISE_NEGATE,
95         UNEXPR_NOT,
96         UNEXPR_DEREFERENCE,
97         UNEXPR_TAKE_ADDRESS,
98         UNEXPR_POSTFIX_INCREMENT,
99         UNEXPR_POSTFIX_DECREMENT,
100         UNEXPR_PREFIX_INCREMENT,
101         UNEXPR_PREFIX_DECREMENT,
102         UNEXPR_CAST,
103         UNEXPR_CAST_IMPLICIT /* compiler generated cast */
104 } unary_expression_type_t;
105
106 struct unary_expression_t {
107         expression_base_t        expression;
108         unary_expression_type_t  type;
109         expression_t            *value;
110 };
111
112 typedef enum {
113         BINEXPR_INVALID = 0,
114         BINEXPR_ADD,
115         BINEXPR_SUB,
116         BINEXPR_MUL,
117         BINEXPR_DIV,
118         BINEXPR_MOD,
119         BINEXPR_EQUAL,
120         BINEXPR_NOTEQUAL,
121         BINEXPR_LESS,
122         BINEXPR_LESSEQUAL,
123         BINEXPR_GREATER,
124         BINEXPR_GREATEREQUAL,
125         BINEXPR_BITWISE_AND,
126         BINEXPR_BITWISE_OR,
127         BINEXPR_BITWISE_XOR,
128         BINEXPR_LOGICAL_AND,
129         BINEXPR_LOGICAL_OR,
130         BINEXPR_SHIFTLEFT,
131         BINEXPR_SHIFTRIGHT,
132         BINEXPR_ASSIGN,
133         BINEXPR_MUL_ASSIGN,
134         BINEXPR_DIV_ASSIGN,
135         BINEXPR_MOD_ASSIGN,
136         BINEXPR_ADD_ASSIGN,
137         BINEXPR_SUB_ASSIGN,
138         BINEXPR_SHIFTLEFT_ASSIGN,
139         BINEXPR_SHIFTRIGHT_ASSIGN,
140         BINEXPR_BITWISE_AND_ASSIGN,
141         BINEXPR_BITWISE_XOR_ASSIGN,
142         BINEXPR_BITWISE_OR_ASSIGN,
143         BINEXPR_COMMA
144 } binary_expression_type_t;
145
146 struct binary_expression_t {
147         expression_base_t         expression;
148         binary_expression_type_t  type;
149         expression_t             *left;
150         expression_t             *right;
151 };
152
153 struct select_expression_t {
154         expression_base_t  expression;
155         expression_t      *compound;
156         symbol_t          *symbol;
157
158         declaration_t     *compound_entry;
159 };
160
161 struct array_access_expression_t {
162         expression_base_t  expression;
163         expression_t      *array_ref;
164         expression_t      *index;
165         bool               flipped; /* index/ref was written in a 5[a] way */
166 };
167
168 struct sizeof_expression_t {
169         expression_base_t  expression;
170         type_t            *type;
171         expression_t      *size_expression;
172 };
173
174 struct designator_t {
175         symbol_t     *symbol;
176         expression_t *array_access;
177         designator_t *next;
178 };
179
180 struct offsetof_expression_t {
181         expression_base_t  expression;
182         type_t            *type;
183         designator_t      *designator;
184 };
185
186 struct va_start_expression_t {
187         expression_base_t  expression;
188         expression_t      *ap;
189         declaration_t     *parameter;
190 };
191
192 struct va_arg_expression_t {
193         expression_base_t  expression;
194         expression_t      *ap;
195 };
196
197 struct conditional_expression_t {
198         expression_base_t  expression;
199         expression_t      *condition;
200         expression_t      *true_expression;
201         expression_t      *false_expression;
202 };
203
204 struct statement_expression_t {
205         expression_base_t  expression;
206         statement_t       *statement;
207 };
208
209 struct classify_type_expression_t {
210         expression_base_t  expression;
211         expression_t      *type_expression;
212 };
213
214 union expression_t {
215         expression_type_t                type;
216         expression_base_t                base;
217         const_expression_t               conste;
218         string_literal_expression_t      string;
219         wide_string_literal_expression_t wide_string;
220         builtin_symbol_expression_t      builtin_symbol;
221         reference_expression_t           reference;
222         call_expression_t                call;
223         unary_expression_t               unary;
224         binary_expression_t              binary;
225         select_expression_t              select;
226         array_access_expression_t        array_access;
227         sizeof_expression_t              sizeofe;
228         offsetof_expression_t            offsetofe;
229         va_start_expression_t            va_starte;
230         va_arg_expression_t              va_arge;
231         conditional_expression_t         conditional;
232         statement_expression_t           statement;
233         classify_type_expression_t       classify_type;
234 };
235
236 typedef enum {
237         STORAGE_CLASS_NONE,
238         STORAGE_CLASS_TYPEDEF,
239         STORAGE_CLASS_EXTERN,
240         STORAGE_CLASS_STATIC,
241         STORAGE_CLASS_AUTO,
242         STORAGE_CLASS_REGISTER,
243         STORAGE_CLASS_ENUM_ENTRY,
244         STORAGE_CLASS_THREAD,
245         STORAGE_CLASS_THREAD_EXTERN,
246         STORAGE_CLASS_THREAD_STATIC
247 } storage_class_tag_t;
248
249 typedef enum {
250         NAMESPACE_NORMAL,
251         NAMESPACE_STRUCT,
252         NAMESPACE_UNION,
253         NAMESPACE_ENUM,
254         NAMESPACE_LABEL,
255 } namespace_t;
256
257 typedef enum {
258         INITIALIZER_VALUE,
259         INITIALIZER_LIST,
260         INITIALIZER_STRING,
261         INITIALIZER_WIDE_STRING
262 } initializer_type_t;
263
264 struct initializer_base_t {
265         initializer_type_t type;
266 };
267
268 struct initializer_value_t {
269         initializer_base_t  initializer;
270         expression_t       *value;
271 };
272
273 struct initializer_list_t {
274         initializer_base_t  initializer;
275         size_t              len;
276         initializer_t      *initializers[];
277 };
278
279 struct initializer_string_t {
280         initializer_base_t  initializer;
281         const char         *string;
282 };
283
284 struct initializer_wide_string_t {
285         initializer_base_t  initializer;
286         wide_string_t       string;
287 };
288
289 union initializer_t {
290         initializer_type_t        type;
291         initializer_base_t        base;
292         initializer_value_t       value;
293         initializer_list_t        list;
294         initializer_string_t      string;
295         initializer_wide_string_t wide_string;
296 };
297
298 struct declaration_t {
299         unsigned char       namespc;
300         unsigned char       storage_class;
301         unsigned int        address_taken : 1;
302         unsigned int        is_inline     : 1;
303         type_t             *type;
304         symbol_t           *symbol;
305         source_position_t   source_position;
306         union {
307                 bool            is_defined;
308                 statement_t    *statement;
309                 initializer_t  *initializer;
310                 expression_t   *enum_value;
311         } init;
312         context_t           context;
313         context_t          *parent_context;
314
315         /** next declaration in a context */
316         declaration_t      *next;
317         /** next declaration with same symbol */
318         declaration_t      *symbol_next;
319
320         unsigned char       declaration_type; /* used in ast2firm module */
321         union {
322                 unsigned int    value_number;     /* used in ast2firm module */
323                 ir_entity      *entity;           /* used in ast2firm module */
324                 ir_node        *block;            /* used in ast2firm module */
325                 tarval         *enum_val;         /* used in ast2firm module */
326         } v;
327 };
328
329 typedef enum {
330         STATEMENT_INVALID,
331         STATEMENT_COMPOUND,
332         STATEMENT_RETURN,
333         STATEMENT_DECLARATION,
334         STATEMENT_IF,
335         STATEMENT_SWITCH,
336         STATEMENT_EXPRESSION,
337         STATEMENT_CONTINUE,
338         STATEMENT_BREAK,
339         STATEMENT_GOTO,
340         STATEMENT_LABEL,
341         STATEMENT_CASE_LABEL,
342         STATEMENT_WHILE,
343         STATEMENT_DO_WHILE,
344         STATEMENT_FOR,
345         STATEMENT_ASM
346 } statement_type_t;
347
348 struct statement_base_t {
349         statement_type_t   type;
350         statement_t       *next;
351         source_position_t  source_position;
352 };
353
354 struct return_statement_t {
355         statement_base_t  statement;
356         expression_t     *return_value;
357 };
358
359 struct compound_statement_t {
360         statement_base_t  statement;
361         statement_t      *statements;
362         context_t         context;
363 };
364
365 struct declaration_statement_t {
366         statement_base_t  statement;
367         declaration_t    *declarations_begin;
368         declaration_t    *declarations_end;
369 };
370
371 struct if_statement_t {
372         statement_base_t  statement;
373         expression_t     *condition;
374         statement_t      *true_statement;
375         statement_t      *false_statement;
376 };
377
378 struct switch_statement_t {
379         statement_base_t  statement;
380         expression_t     *expression;
381         statement_t      *body;
382 };
383
384 struct goto_statement_t {
385         statement_base_t  statement;
386         declaration_t    *label;
387 };
388
389 struct case_label_statement_t {
390         statement_base_t  statement;
391         expression_t     *expression;
392         statement_t      *label_statement;
393 };
394
395 struct label_statement_t {
396         statement_base_t  statement;
397         declaration_t    *label;
398         statement_t      *label_statement;
399 };
400
401 struct expression_statement_t {
402         statement_base_t  statement;
403         expression_t     *expression;
404 };
405
406 struct while_statement_t {
407         statement_base_t  statement;
408         expression_t     *condition;
409         statement_t      *body;
410 };
411
412 struct do_while_statement_t {
413         statement_base_t  statement;
414         expression_t     *condition;
415         statement_t      *body;
416 };
417
418 struct for_statement_t {
419         statement_base_t  statement;
420         expression_t     *initialisation;
421         expression_t     *condition;
422         expression_t     *step;
423         statement_t      *body;
424         context_t         context;
425 };
426
427 struct asm_constraint_t {
428         const char       *constraints;
429         expression_t     *expression;
430         symbol_t         *symbol;
431         asm_constraint_t *next;
432 };
433
434 struct asm_clobber_t {
435         const char    *clobber;
436         asm_clobber_t *next;
437 };
438
439 struct asm_statement_t {
440         statement_base_t  statement;
441         const char       *asm_text;
442         asm_constraint_t *inputs;
443         asm_constraint_t *outputs;
444         asm_clobber_t    *clobbers;
445         bool              is_volatile;
446 };
447
448 union statement_t {
449         statement_type_t         type;
450         statement_base_t         base;
451         return_statement_t       returns;
452         compound_statement_t     compound;
453         declaration_statement_t  declaration;
454         if_statement_t           ifs;
455         switch_statement_t       switchs;
456         goto_statement_t         gotos;
457         case_label_statement_t   case_label;
458         label_statement_t        label;
459         expression_statement_t   expression;
460         while_statement_t        whiles;
461         do_while_statement_t     do_while;
462         for_statement_t          fors;
463         asm_statement_t          asms;
464 };
465
466 struct translation_unit_t {
467         context_t context;
468 };
469
470 static inline
471 void *_allocate_ast(size_t size)
472 {
473         return obstack_alloc(&ast_obstack, size);
474 }
475
476 #define allocate_ast(size)                 _allocate_ast(size)
477
478 #endif