First iteration in adding wide string literal support. No input encoding handling...
[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_COUNT
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 union initializer_t {
278         initializer_type_t   type;
279         initializer_base_t   base;
280         initializer_value_t  value;
281         initializer_list_t   list;
282         initializer_string_t string;
283 };
284
285 struct declaration_t {
286         unsigned char       namespc;
287         unsigned char       storage_class;
288         unsigned int        address_taken : 1;
289         unsigned int        is_inline     : 1;
290         type_t             *type;
291         symbol_t           *symbol;
292         source_position_t   source_position;
293         union {
294                 bool            is_defined;
295                 statement_t    *statement;
296                 initializer_t  *initializer;
297                 expression_t   *enum_value;
298         } init;
299         context_t           context;
300         context_t          *parent_context;
301
302         /** next declaration in a context */
303         declaration_t      *next;
304         /** next declaration with same symbol */
305         declaration_t      *symbol_next;
306
307         unsigned char       declaration_type; /* used in ast2firm module */
308         union {
309                 unsigned int    value_number;     /* used in ast2firm module */
310                 ir_entity      *entity;           /* used in ast2firm module */
311                 ir_node        *block;            /* used in ast2firm module */
312                 tarval         *enum_val;         /* used in ast2firm module */
313         } v;
314 };
315
316 typedef enum {
317         STATEMENT_INVALID,
318         STATEMENT_COMPOUND,
319         STATEMENT_RETURN,
320         STATEMENT_DECLARATION,
321         STATEMENT_IF,
322         STATEMENT_SWITCH,
323         STATEMENT_EXPRESSION,
324         STATEMENT_CONTINUE,
325         STATEMENT_BREAK,
326         STATEMENT_GOTO,
327         STATEMENT_LABEL,
328         STATEMENT_CASE_LABEL,
329         STATEMENT_WHILE,
330         STATEMENT_DO_WHILE,
331         STATEMENT_FOR,
332         STATEMENT_ASM
333 } statement_type_t;
334
335 struct statement_base_t {
336         statement_type_t   type;
337         statement_t       *next;
338         source_position_t  source_position;
339 };
340
341 struct return_statement_t {
342         statement_base_t  statement;
343         expression_t     *return_value;
344 };
345
346 struct compound_statement_t {
347         statement_base_t  statement;
348         statement_t      *statements;
349         context_t         context;
350 };
351
352 struct declaration_statement_t {
353         statement_base_t  statement;
354         declaration_t    *declarations_begin;
355         declaration_t    *declarations_end;
356 };
357
358 struct if_statement_t {
359         statement_base_t  statement;
360         expression_t     *condition;
361         statement_t      *true_statement;
362         statement_t      *false_statement;
363 };
364
365 struct switch_statement_t {
366         statement_base_t  statement;
367         expression_t     *expression;
368         statement_t      *body;
369 };
370
371 struct goto_statement_t {
372         statement_base_t  statement;
373         declaration_t    *label;
374 };
375
376 struct case_label_statement_t {
377         statement_base_t  statement;
378         expression_t     *expression;
379         statement_t      *label_statement;
380 };
381
382 struct label_statement_t {
383         statement_base_t  statement;
384         declaration_t    *label;
385         statement_t      *label_statement;
386 };
387
388 struct expression_statement_t {
389         statement_base_t  statement;
390         expression_t     *expression;
391 };
392
393 struct while_statement_t {
394         statement_base_t  statement;
395         expression_t     *condition;
396         statement_t      *body;
397 };
398
399 struct do_while_statement_t {
400         statement_base_t  statement;
401         expression_t     *condition;
402         statement_t      *body;
403 };
404
405 struct for_statement_t {
406         statement_base_t  statement;
407         expression_t     *initialisation;
408         expression_t     *condition;
409         expression_t     *step;
410         statement_t      *body;
411         context_t         context;
412 };
413
414 struct asm_constraint_t {
415         const char       *constraints;
416         expression_t     *expression;
417         symbol_t         *symbol;
418         asm_constraint_t *next;
419 };
420
421 struct asm_clobber_t {
422         const char    *clobber;
423         asm_clobber_t *next;
424 };
425
426 struct asm_statement_t {
427         statement_base_t  statement;
428         const char       *asm_text;
429         asm_constraint_t *inputs;
430         asm_constraint_t *outputs;
431         asm_clobber_t    *clobbers;
432         bool              is_volatile;
433 };
434
435 union statement_t {
436         statement_type_t         type;
437         statement_base_t         base;
438         return_statement_t       returns;
439         compound_statement_t     compound;
440         declaration_statement_t  declaration;
441         if_statement_t           ifs;
442         switch_statement_t       switchs;
443         goto_statement_t         gotos;
444         case_label_statement_t   case_label;
445         label_statement_t        label;
446         expression_statement_t   expression;
447         while_statement_t        whiles;
448         do_while_statement_t     do_while;
449         for_statement_t          fors;
450         asm_statement_t          asms;
451 };
452
453 struct translation_unit_t {
454         context_t context;
455 };
456
457 static inline
458 void *_allocate_ast(size_t size)
459 {
460         return obstack_alloc(&ast_obstack, size);
461 }
462
463 #define allocate_ast(size)                 _allocate_ast(size)
464
465 #endif