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