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