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