handle automatic type conversion of array and function types like described in the...
[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         bool          flipped; /* index/ref was written in a 5[a] way */
158 };
159
160 struct sizeof_expression_t {
161         expression_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_t  expression;
174         type_t       *type;
175         designator_t *designator;
176 };
177
178 struct va_arg_expression_t {
179         expression_t  expression;
180         expression_t *arg;
181         type_t       *type;
182 };
183
184 struct conditional_expression_t {
185         expression_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_t  expression;
193         statement_t  *statement;
194 };
195
196 struct classify_type_expression_t {
197         expression_t  expression;
198         expression_t *type_expression;
199 };
200
201 typedef enum {
202         STORAGE_CLASS_NONE,
203         STORAGE_CLASS_TYPEDEF,
204         STORAGE_CLASS_EXTERN,
205         STORAGE_CLASS_STATIC,
206         STORAGE_CLASS_AUTO,
207         STORAGE_CLASS_REGISTER,
208         STORAGE_CLASS_ENUM_ENTRY
209 } storage_class_t;
210
211 typedef enum {
212         NAMESPACE_NORMAL,
213         NAMESPACE_STRUCT,
214         NAMESPACE_UNION,
215         NAMESPACE_ENUM,
216         NAMESPACE_LABEL
217 } namespace_t;
218
219 typedef enum {
220         INITIALIZER_VALUE,
221         INITIALIZER_LIST,
222         INITIALIZER_STRING,
223         INITIALIZER_COUNT
224 } initializer_type_t;
225
226 struct initializer_base_t {
227         initializer_type_t type;
228 };
229
230 struct initializer_value_t {
231         initializer_base_t  initializer;
232         expression_t       *value;
233 };
234
235 struct initializer_list_t {
236         initializer_base_t  initializer;
237         size_t              len;
238         initializer_t      *initializers[];
239 };
240
241 struct initializer_string_t {
242         initializer_base_t  initializer;
243         const char         *string;
244 };
245
246 union initializer_t {
247         initializer_type_t   type;
248         initializer_base_t   base;
249         initializer_value_t  value;
250         initializer_list_t   list;
251         initializer_string_t string;
252 };
253
254 struct declaration_t {
255         unsigned char       namespc;
256         unsigned char       storage_class;
257         unsigned int        address_taken : 1;
258         unsigned int        is_inline     : 1;
259         type_t             *type;
260         symbol_t           *symbol;
261         source_position_t   source_position;
262         union {
263                 bool            is_defined;
264                 statement_t    *statement;
265                 initializer_t  *initializer;
266                 expression_t   *enum_value;
267         } init;
268         context_t           context;
269         context_t          *parent_context;
270
271         /** next declaration in a context */
272         declaration_t      *next;
273         /** next declaration with same symbol */
274         declaration_t      *symbol_next;
275
276         unsigned char       declaration_type; /* used in ast2firm module */
277         union {
278                 unsigned int    value_number;     /* used in ast2firm module */
279                 ir_entity      *entity;           /* used in ast2firm module */
280                 ir_node        *block;            /* used in ast2firm module */
281                 tarval         *enum_val;         /* used in ast2firm module */
282         } v;
283 };
284
285 typedef enum {
286         STATEMENT_INVALID,
287         STATEMENT_COMPOUND,
288         STATEMENT_RETURN,
289         STATEMENT_DECLARATION,
290         STATEMENT_IF,
291         STATEMENT_SWITCH,
292         STATEMENT_EXPRESSION,
293         STATEMENT_CONTINUE,
294         STATEMENT_BREAK,
295         STATEMENT_GOTO,
296         STATEMENT_LABEL,
297         STATEMENT_CASE_LABEL,
298         STATEMENT_WHILE,
299         STATEMENT_DO_WHILE,
300         STATEMENT_FOR
301 } statement_type_t;
302
303 struct statement_t {
304         statement_type_t   type;
305         statement_t       *next;
306         source_position_t  source_position;
307 };
308
309 struct return_statement_t {
310         statement_t   statement;
311         expression_t *return_value;
312 };
313
314 struct compound_statement_t {
315         statement_t  statement;
316         statement_t *statements;
317         context_t    context;
318 };
319
320 struct declaration_statement_t {
321         statement_t    statement;
322         declaration_t *declarations_begin;
323         declaration_t *declarations_end;
324 };
325
326 struct if_statement_t {
327         statement_t   statement;
328         expression_t *condition;
329         statement_t  *true_statement;
330         statement_t  *false_statement;
331 };
332
333 struct switch_statement_t {
334         statement_t   statement;
335         expression_t *expression;
336         statement_t  *body;
337 };
338
339 struct goto_statement_t {
340         statement_t    statement;
341         declaration_t *label;
342 };
343
344 struct case_label_statement_t {
345         statement_t   statement;
346         expression_t *expression;
347         statement_t  *label_statement;
348 };
349
350 struct label_statement_t {
351         statement_t    statement;
352         declaration_t *label;
353         statement_t   *label_statement;
354 };
355
356 struct expression_statement_t {
357         statement_t   statement;
358         expression_t *expression;
359 };
360
361 struct while_statement_t {
362         statement_t   statement;
363         expression_t *condition;
364         statement_t  *body;
365 };
366
367 struct do_while_statement_t {
368         statement_t   statement;
369         expression_t *condition;
370         statement_t  *body;
371 };
372
373 struct for_statement_t {
374         statement_t   statement;
375         expression_t  *initialisation;
376         expression_t  *condition;
377         expression_t  *step;
378         statement_t   *body;
379         context_t      context;
380 };
381
382 struct translation_unit_t {
383         context_t context;
384 };
385
386 static inline
387 void *_allocate_ast(size_t size)
388 {
389         return obstack_alloc(&ast_obstack, size);
390 }
391
392 #define allocate_ast(size)                 _allocate_ast(size)
393
394 #endif