3314400b6bf18fa2a3c21c91b5b2135dad69ca75
[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 } expression_type_t;
37
38 struct context_t {
39         declaration_t   *declarations;
40 };
41
42 struct expression_base_t {
43         expression_type_t   type;
44         type_t             *datatype;
45         source_position_t   source_position;
46 };
47
48 struct const_expression_t {
49         expression_base_t  expression;
50         union {
51                 long long   int_value;
52                 long double float_value;
53         } v;
54 };
55
56 struct string_literal_expression_t {
57         expression_base_t  expression;
58         const char        *value;
59 };
60
61 struct builtin_symbol_expression_t {
62         expression_base_t  expression;
63         symbol_t          *symbol;
64 };
65
66 struct reference_expression_t {
67         expression_base_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_base_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_base_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_base_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_base_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_base_t  expression;
155         expression_t      *array_ref;
156         expression_t      *index;
157         bool               flipped; /* index/ref was written in a 5[a] way */
158 };
159
160 struct sizeof_expression_t {
161         expression_base_t  expression;
162         type_t            *type;
163         expression_t      *size_expression;
164 };
165
166 struct designator_t {
167         symbol_t     *symbol;
168         expression_t *array_access;
169         designator_t *next;
170 };
171
172 struct offsetof_expression_t {
173         expression_base_t  expression;
174         type_t            *type;
175         designator_t      *designator;
176 };
177
178 struct va_arg_expression_t {
179         expression_base_t  expression;
180         expression_t      *arg;
181         type_t            *type;
182 };
183
184 struct conditional_expression_t {
185         expression_base_t  expression;
186         expression_t      *condition;
187         expression_t      *true_expression;
188         expression_t      *false_expression;
189 };
190
191 struct statement_expression_t {
192         expression_base_t  expression;
193         statement_t       *statement;
194 };
195
196 struct classify_type_expression_t {
197         expression_base_t  expression;
198         expression_t      *type_expression;
199 };
200
201 union expression_t {
202         expression_type_t            type;
203         expression_base_t            base;
204         const_expression_t           conste;
205         string_literal_expression_t  string;
206         builtin_symbol_expression_t  builtin_symbol;
207         reference_expression_t       reference;
208         call_expression_t            call;
209         unary_expression_t           unary;
210         binary_expression_t          binary;
211         select_expression_t          select;
212         array_access_expression_t    array_access;
213         sizeof_expression_t          sizeofe;
214         offsetof_expression_t        offsetofe;
215         va_arg_expression_t          va_arge;
216         conditional_expression_t     conditional;
217         statement_expression_t       statement;
218         classify_type_expression_t   classify_type;
219 };
220
221 typedef enum {
222         STORAGE_CLASS_NONE,
223         STORAGE_CLASS_TYPEDEF,
224         STORAGE_CLASS_EXTERN,
225         STORAGE_CLASS_STATIC,
226         STORAGE_CLASS_AUTO,
227         STORAGE_CLASS_REGISTER,
228         STORAGE_CLASS_ENUM_ENTRY,
229         STORAGE_CLASS_THREAD,
230         STORAGE_CLASS_THREAD_EXTERN,
231         STORAGE_CLASS_THREAD_STATIC
232 } storage_class_tag_t;
233
234 typedef enum {
235         NAMESPACE_NORMAL,
236         NAMESPACE_STRUCT,
237         NAMESPACE_UNION,
238         NAMESPACE_ENUM,
239         NAMESPACE_LABEL,
240 } namespace_t;
241
242 typedef enum {
243         INITIALIZER_VALUE,
244         INITIALIZER_LIST,
245         INITIALIZER_STRING,
246         INITIALIZER_COUNT
247 } initializer_type_t;
248
249 struct initializer_base_t {
250         initializer_type_t type;
251 };
252
253 struct initializer_value_t {
254         initializer_base_t  initializer;
255         expression_t       *value;
256 };
257
258 struct initializer_list_t {
259         initializer_base_t  initializer;
260         size_t              len;
261         initializer_t      *initializers[];
262 };
263
264 struct initializer_string_t {
265         initializer_base_t  initializer;
266         const char         *string;
267 };
268
269 union initializer_t {
270         initializer_type_t   type;
271         initializer_base_t   base;
272         initializer_value_t  value;
273         initializer_list_t   list;
274         initializer_string_t string;
275 };
276
277 struct declaration_t {
278         unsigned char       namespc;
279         unsigned char       storage_class;
280         unsigned int        address_taken : 1;
281         unsigned int        is_inline     : 1;
282         type_t             *type;
283         symbol_t           *symbol;
284         source_position_t   source_position;
285         union {
286                 bool            is_defined;
287                 statement_t    *statement;
288                 initializer_t  *initializer;
289                 expression_t   *enum_value;
290         } init;
291         context_t           context;
292         context_t          *parent_context;
293
294         /** next declaration in a context */
295         declaration_t      *next;
296         /** next declaration with same symbol */
297         declaration_t      *symbol_next;
298
299         unsigned char       declaration_type; /* used in ast2firm module */
300         union {
301                 unsigned int    value_number;     /* used in ast2firm module */
302                 ir_entity      *entity;           /* used in ast2firm module */
303                 ir_node        *block;            /* used in ast2firm module */
304                 tarval         *enum_val;         /* used in ast2firm module */
305         } v;
306 };
307
308 typedef enum {
309         STATEMENT_INVALID,
310         STATEMENT_COMPOUND,
311         STATEMENT_RETURN,
312         STATEMENT_DECLARATION,
313         STATEMENT_IF,
314         STATEMENT_SWITCH,
315         STATEMENT_EXPRESSION,
316         STATEMENT_CONTINUE,
317         STATEMENT_BREAK,
318         STATEMENT_GOTO,
319         STATEMENT_LABEL,
320         STATEMENT_CASE_LABEL,
321         STATEMENT_WHILE,
322         STATEMENT_DO_WHILE,
323         STATEMENT_FOR,
324         STATEMENT_ASM
325 } statement_type_t;
326
327 struct statement_base_t {
328         statement_type_t   type;
329         statement_t       *next;
330         source_position_t  source_position;
331 };
332
333 struct return_statement_t {
334         statement_base_t  statement;
335         expression_t     *return_value;
336 };
337
338 struct compound_statement_t {
339         statement_base_t  statement;
340         statement_t      *statements;
341         context_t         context;
342 };
343
344 struct declaration_statement_t {
345         statement_base_t  statement;
346         declaration_t    *declarations_begin;
347         declaration_t    *declarations_end;
348 };
349
350 struct if_statement_t {
351         statement_base_t  statement;
352         expression_t     *condition;
353         statement_t      *true_statement;
354         statement_t      *false_statement;
355 };
356
357 struct switch_statement_t {
358         statement_base_t  statement;
359         expression_t     *expression;
360         statement_t      *body;
361 };
362
363 struct goto_statement_t {
364         statement_base_t  statement;
365         declaration_t    *label;
366 };
367
368 struct case_label_statement_t {
369         statement_base_t  statement;
370         expression_t     *expression;
371         statement_t      *label_statement;
372 };
373
374 struct label_statement_t {
375         statement_base_t  statement;
376         declaration_t    *label;
377         statement_t      *label_statement;
378 };
379
380 struct expression_statement_t {
381         statement_base_t  statement;
382         expression_t     *expression;
383 };
384
385 struct while_statement_t {
386         statement_base_t  statement;
387         expression_t     *condition;
388         statement_t      *body;
389 };
390
391 struct do_while_statement_t {
392         statement_base_t  statement;
393         expression_t     *condition;
394         statement_t      *body;
395 };
396
397 struct for_statement_t {
398         statement_base_t  statement;
399         expression_t     *initialisation;
400         expression_t     *condition;
401         expression_t     *step;
402         statement_t      *body;
403         context_t         context;
404 };
405
406 struct asm_constraint_t {
407         const char       *constraints;
408         expression_t     *expression;
409         symbol_t         *symbol;
410         asm_constraint_t *next;
411 };
412
413 struct asm_clobber_t {
414         const char    *clobber;
415         asm_clobber_t *next;
416 };
417
418 struct asm_statement_t {
419         statement_base_t  statement;
420         const char       *asm_text;
421         asm_constraint_t *inputs;
422         asm_constraint_t *outputs;
423         asm_clobber_t    *clobbers;
424         bool              is_volatile;
425 };
426
427 union statement_t {
428         statement_type_t         type;
429         statement_base_t         base;
430         return_statement_t       returns;
431         compound_statement_t     compound;
432         declaration_statement_t  declaration;
433         if_statement_t           ifs;
434         switch_statement_t       switchs;
435         goto_statement_t         gotos;
436         case_label_statement_t   case_label;
437         label_statement_t        label;
438         expression_statement_t   expression;
439         while_statement_t        whiles;
440         do_while_statement_t     do_while;
441         for_statement_t          fors;
442         asm_statement_t          asms;
443 };
444
445 struct translation_unit_t {
446         context_t context;
447 };
448
449 static inline
450 void *_allocate_ast(size_t size)
451 {
452         return obstack_alloc(&ast_obstack, size);
453 }
454
455 #define allocate_ast(size)                 _allocate_ast(size)
456
457 #endif