some refactoring in preparation for a preprocessor
[cparser] / ast_t.h
1 /*
2  * This file is part of cparser.
3  * Copyright (C) 2007-2008 Matthias Braun <matze@braunis.de>
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License
7  * as published by the Free Software Foundation; either version 2
8  * of the License, or (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
18  * 02111-1307, USA.
19  */
20 #ifndef AST_T_H
21 #define AST_T_H
22
23 #include <libfirm/firm_types.h>
24 #include <assert.h>
25
26 #include "ast.h"
27 #include "symbol.h"
28 #include "token_t.h"
29 #include "type.h"
30 #include "adt/obst.h"
31
32 extern struct obstack ast_obstack;
33
34 typedef enum {
35         EXPR_UNKNOWN = 0,
36         EXPR_INVALID,
37         EXPR_REFERENCE,
38         EXPR_CONST,
39         EXPR_CHARACTER_CONSTANT,
40         EXPR_WIDE_CHARACTER_CONSTANT,
41         EXPR_STRING_LITERAL,
42         EXPR_WIDE_STRING_LITERAL,
43         EXPR_COMPOUND_LITERAL,
44         EXPR_CALL,
45         EXPR_CONDITIONAL,
46         EXPR_SELECT,
47         EXPR_ARRAY_ACCESS,
48         EXPR_SIZEOF,
49         EXPR_CLASSIFY_TYPE,
50         EXPR_ALIGNOF,
51
52         EXPR_FUNCTION,
53         EXPR_PRETTY_FUNCTION,
54         EXPR_BUILTIN_SYMBOL,
55         EXPR_BUILTIN_CONSTANT_P,
56         EXPR_BUILTIN_PREFETCH,
57         EXPR_OFFSETOF,
58         EXPR_VA_START,
59         EXPR_VA_ARG,
60         EXPR_STATEMENT,
61
62         EXPR_UNARY_FIRST,
63         EXPR_UNARY_NEGATE = EXPR_UNARY_FIRST,
64         EXPR_UNARY_PLUS,
65         EXPR_UNARY_BITWISE_NEGATE,
66         EXPR_UNARY_NOT,
67         EXPR_UNARY_DEREFERENCE,
68         EXPR_UNARY_TAKE_ADDRESS,
69         EXPR_UNARY_POSTFIX_INCREMENT,
70         EXPR_UNARY_POSTFIX_DECREMENT,
71         EXPR_UNARY_PREFIX_INCREMENT,
72         EXPR_UNARY_PREFIX_DECREMENT,
73         EXPR_UNARY_CAST,
74         EXPR_UNARY_CAST_IMPLICIT, /**< compiler generated cast */
75         EXPR_UNARY_ASSUME,        /**< MS __assume() */
76         EXPR_UNARY_BITFIELD_EXTRACT,
77         EXPR_UNARY_LAST = EXPR_UNARY_BITFIELD_EXTRACT,
78
79         EXPR_BINARY_FIRST,
80         EXPR_BINARY_ADD = EXPR_BINARY_FIRST,
81         EXPR_BINARY_SUB,
82         EXPR_BINARY_MUL,
83         EXPR_BINARY_DIV,
84         EXPR_BINARY_MOD,
85         EXPR_BINARY_EQUAL,
86         EXPR_BINARY_NOTEQUAL,
87         EXPR_BINARY_LESS,
88         EXPR_BINARY_LESSEQUAL,
89         EXPR_BINARY_GREATER,
90         EXPR_BINARY_GREATEREQUAL,
91         EXPR_BINARY_BITWISE_AND,
92         EXPR_BINARY_BITWISE_OR,
93         EXPR_BINARY_BITWISE_XOR,
94         EXPR_BINARY_LOGICAL_AND,
95         EXPR_BINARY_LOGICAL_OR,
96         EXPR_BINARY_SHIFTLEFT,
97         EXPR_BINARY_SHIFTRIGHT,
98         EXPR_BINARY_ASSIGN,
99         EXPR_BINARY_MUL_ASSIGN,
100         EXPR_BINARY_DIV_ASSIGN,
101         EXPR_BINARY_MOD_ASSIGN,
102         EXPR_BINARY_ADD_ASSIGN,
103         EXPR_BINARY_SUB_ASSIGN,
104         EXPR_BINARY_SHIFTLEFT_ASSIGN,
105         EXPR_BINARY_SHIFTRIGHT_ASSIGN,
106         EXPR_BINARY_BITWISE_AND_ASSIGN,
107         EXPR_BINARY_BITWISE_XOR_ASSIGN,
108         EXPR_BINARY_BITWISE_OR_ASSIGN,
109         EXPR_BINARY_COMMA,
110
111         EXPR_BINARY_BUILTIN_EXPECT,
112         EXPR_BINARY_ISGREATER,
113         EXPR_BINARY_ISGREATEREQUAL,
114         EXPR_BINARY_ISLESS,
115         EXPR_BINARY_ISLESSEQUAL,
116         EXPR_BINARY_ISLESSGREATER,
117         EXPR_BINARY_ISUNORDERED,
118         EXPR_BINARY_LAST = EXPR_BINARY_ISUNORDERED,
119 } expression_kind_t;
120
121 /* convenience macros */
122 #define EXPR_BINARY_CASES                  \
123         case EXPR_BINARY_ADD:                  \
124         case EXPR_BINARY_SUB:                  \
125         case EXPR_BINARY_MUL:                  \
126         case EXPR_BINARY_DIV:                  \
127         case EXPR_BINARY_MOD:                  \
128         case EXPR_BINARY_EQUAL:                \
129         case EXPR_BINARY_NOTEQUAL:             \
130         case EXPR_BINARY_LESS:                 \
131         case EXPR_BINARY_LESSEQUAL:            \
132         case EXPR_BINARY_GREATER:              \
133         case EXPR_BINARY_GREATEREQUAL:         \
134         case EXPR_BINARY_BITWISE_AND:          \
135         case EXPR_BINARY_BITWISE_OR:           \
136         case EXPR_BINARY_BITWISE_XOR:          \
137         case EXPR_BINARY_LOGICAL_AND:          \
138         case EXPR_BINARY_LOGICAL_OR:           \
139         case EXPR_BINARY_SHIFTLEFT:            \
140         case EXPR_BINARY_SHIFTRIGHT:           \
141         case EXPR_BINARY_ASSIGN:               \
142         case EXPR_BINARY_MUL_ASSIGN:           \
143         case EXPR_BINARY_DIV_ASSIGN:           \
144         case EXPR_BINARY_MOD_ASSIGN:           \
145         case EXPR_BINARY_ADD_ASSIGN:           \
146         case EXPR_BINARY_SUB_ASSIGN:           \
147         case EXPR_BINARY_SHIFTLEFT_ASSIGN:     \
148         case EXPR_BINARY_SHIFTRIGHT_ASSIGN:    \
149         case EXPR_BINARY_BITWISE_AND_ASSIGN:   \
150         case EXPR_BINARY_BITWISE_XOR_ASSIGN:   \
151         case EXPR_BINARY_BITWISE_OR_ASSIGN:    \
152         case EXPR_BINARY_COMMA:                \
153         case EXPR_BINARY_BUILTIN_EXPECT:       \
154         case EXPR_BINARY_ISGREATER:            \
155         case EXPR_BINARY_ISGREATEREQUAL:       \
156         case EXPR_BINARY_ISLESS:               \
157         case EXPR_BINARY_ISLESSEQUAL:          \
158         case EXPR_BINARY_ISLESSGREATER:        \
159         case EXPR_BINARY_ISUNORDERED:
160
161 #define EXPR_UNARY_CASES                   \
162         case EXPR_UNARY_NEGATE:                \
163         case EXPR_UNARY_PLUS:                  \
164         case EXPR_UNARY_BITWISE_NEGATE:        \
165         case EXPR_UNARY_NOT:                   \
166         case EXPR_UNARY_DEREFERENCE:           \
167         case EXPR_UNARY_TAKE_ADDRESS:          \
168         case EXPR_UNARY_POSTFIX_INCREMENT:     \
169         case EXPR_UNARY_POSTFIX_DECREMENT:     \
170         case EXPR_UNARY_PREFIX_INCREMENT:      \
171         case EXPR_UNARY_PREFIX_DECREMENT:      \
172         case EXPR_UNARY_CAST:                  \
173         case EXPR_UNARY_CAST_IMPLICIT:         \
174         case EXPR_UNARY_ASSUME:                \
175         case EXPR_UNARY_BITFIELD_EXTRACT:
176
177 struct scope_t {
178         declaration_t *declarations;      /**< List of declarations in this scope. */
179         declaration_t *last_declaration;
180 };
181
182 struct expression_base_t {
183         expression_kind_t   kind;
184         type_t             *type;
185         source_position_t   source_position;
186 };
187
188 struct const_expression_t {
189         expression_base_t  base;
190         union {
191                 long long     int_value;
192                 long double   float_value;
193                 string_t      character;
194                 wide_string_t wide_character;
195         } v;
196 };
197
198 struct string_literal_expression_t {
199         expression_base_t  base;
200         string_t           value;
201 };
202
203 struct wide_string_literal_expression_t {
204         expression_base_t  base;
205         wide_string_t      value;
206 };
207
208 struct compound_literal_expression_t {
209         expression_base_t  base;
210         type_t            *type;
211         initializer_t     *initializer;
212 };
213
214 struct builtin_symbol_expression_t {
215         expression_base_t  base;
216         symbol_t          *symbol;
217 };
218
219 struct builtin_constant_expression_t {
220         expression_base_t  base;
221         expression_t      *value;
222 };
223
224 struct builtin_prefetch_expression_t {
225         expression_base_t  base;
226         expression_t      *adr;
227         expression_t      *rw;
228         expression_t      *locality;
229 };
230
231 struct reference_expression_t {
232         expression_base_t  base;
233         symbol_t          *symbol;
234         declaration_t     *declaration;
235 };
236
237 struct call_argument_t {
238         expression_t    *expression;
239         call_argument_t *next;
240 };
241
242 struct call_expression_t {
243         expression_base_t  base;
244         expression_t      *function;
245         call_argument_t   *arguments;
246 };
247
248 struct unary_expression_t {
249         expression_base_t  base;
250         expression_t      *value;
251 };
252
253 struct binary_expression_t {
254         expression_base_t  base;
255         expression_t      *left;
256         expression_t      *right;
257 };
258
259 struct select_expression_t {
260         expression_base_t  base;
261         expression_t      *compound;
262         symbol_t          *symbol;
263
264         declaration_t     *compound_entry;
265 };
266
267 struct array_access_expression_t {
268         expression_base_t  base;
269         expression_t      *array_ref;
270         expression_t      *index;
271         bool               flipped; /* index/ref was written in a 5[a] way */
272 };
273
274 struct typeprop_expression_t {
275         expression_base_t  base;
276         type_t            *type;
277         expression_t      *tp_expression;
278 };
279
280 struct designator_t {
281         source_position_t  source_position;
282         symbol_t          *symbol;
283         expression_t      *array_index;
284         designator_t      *next;
285 };
286
287 struct offsetof_expression_t {
288         expression_base_t  base;
289         type_t            *type;
290         designator_t      *designator;
291 };
292
293 struct va_start_expression_t {
294         expression_base_t  base;
295         expression_t      *ap;
296         declaration_t     *parameter;
297 };
298
299 struct va_arg_expression_t {
300         expression_base_t  base;
301         expression_t      *ap;
302 };
303
304 struct conditional_expression_t {
305         expression_base_t  base;
306         expression_t      *condition;
307         expression_t      *true_expression;
308         expression_t      *false_expression;
309 };
310
311 struct statement_expression_t {
312         expression_base_t  base;
313         statement_t       *statement;
314 };
315
316 struct classify_type_expression_t {
317         expression_base_t  base;
318         expression_t      *type_expression;
319 };
320
321 union expression_t {
322         expression_kind_t                kind;
323         expression_base_t                base;
324         const_expression_t               conste;
325         string_literal_expression_t      string;
326         wide_string_literal_expression_t wide_string;
327         compound_literal_expression_t    compound_literal;
328         builtin_symbol_expression_t      builtin_symbol;
329         builtin_constant_expression_t    builtin_constant;
330         builtin_prefetch_expression_t    builtin_prefetch;
331         reference_expression_t           reference;
332         call_expression_t                call;
333         unary_expression_t               unary;
334         binary_expression_t              binary;
335         select_expression_t              select;
336         array_access_expression_t        array_access;
337         typeprop_expression_t            typeprop;
338         offsetof_expression_t            offsetofe;
339         va_start_expression_t            va_starte;
340         va_arg_expression_t              va_arge;
341         conditional_expression_t         conditional;
342         statement_expression_t           statement;
343         classify_type_expression_t       classify_type;
344 };
345
346 typedef enum {
347         STORAGE_CLASS_NONE,
348         STORAGE_CLASS_EXTERN,
349         STORAGE_CLASS_STATIC,
350         STORAGE_CLASS_TYPEDEF,
351         STORAGE_CLASS_AUTO,
352         STORAGE_CLASS_REGISTER,
353         STORAGE_CLASS_ENUM_ENTRY,
354         STORAGE_CLASS_THREAD,
355         STORAGE_CLASS_THREAD_EXTERN,
356         STORAGE_CLASS_THREAD_STATIC,
357 } storage_class_tag_t;
358
359 typedef enum {
360         NAMESPACE_NORMAL,
361         NAMESPACE_STRUCT,
362         NAMESPACE_UNION,
363         NAMESPACE_ENUM,
364         NAMESPACE_LABEL,
365 } namespace_t;
366
367 typedef enum {
368         INITIALIZER_VALUE,
369         INITIALIZER_LIST,
370         INITIALIZER_STRING,
371         INITIALIZER_WIDE_STRING,
372         INITIALIZER_DESIGNATOR
373 } initializer_kind_t;
374
375 struct initializer_base_t {
376         initializer_kind_t kind;
377 };
378
379 struct initializer_value_t {
380         initializer_base_t  base;
381         expression_t       *value;
382 };
383
384 struct initializer_list_t {
385         initializer_base_t  base;
386         size_t              len;
387         initializer_t      *initializers[];
388 };
389
390 struct initializer_string_t {
391         initializer_base_t base;
392         string_t           string;
393 };
394
395 struct initializer_wide_string_t {
396         initializer_base_t  base;
397         wide_string_t       string;
398 };
399
400 struct initializer_designator_t {
401         initializer_base_t  base;
402         designator_t       *designator;
403 };
404
405 union initializer_t {
406         initializer_kind_t        kind;
407         initializer_base_t        base;
408         initializer_value_t       value;
409         initializer_list_t        list;
410         initializer_string_t      string;
411         initializer_wide_string_t wide_string;
412         initializer_designator_t  designator;
413 };
414
415 typedef enum {
416         DM_DLLIMPORT   = (1 << 0),
417         DM_DLLEXPORT   = (1 << 1),
418         DM_THREAD      = (1 << 2),
419         DM_NAKED       = (1 << 3),
420         DM_FORCEINLINE = (1 << 4),
421         DM_NOTHROW     = (1 << 5),
422         DM_NORETURN    = (1 << 6),
423         DM_NOINLINE    = (1 << 7)
424 } decl_modifier_t;
425
426 typedef unsigned short decl_modifiers_t;
427
428 struct declaration_t {
429         unsigned char       namespc;
430         unsigned char       declared_storage_class;
431         unsigned char       storage_class;
432         decl_modifiers_t    modifiers;
433         unsigned int        address_taken : 1;
434         unsigned int        is_inline     : 1;
435         unsigned int        used          : 1;  /**< Set if the declaration is used. */
436         type_t             *type;
437         symbol_t           *symbol;
438         source_position_t   source_position;
439         union {
440                 bool            is_defined;
441                 statement_t    *statement;
442                 initializer_t  *initializer;
443                 expression_t   *enum_value;
444         } init;
445         scope_t             scope;
446         scope_t            *parent_scope;
447
448         /** next declaration in a scope */
449         declaration_t      *next;
450         /** next declaration with same symbol */
451         declaration_t      *symbol_next;
452
453         /* the following fields are used in ast2firm module */
454         unsigned char       declaration_kind;
455         union {
456                 unsigned int  value_number;
457                 ir_entity    *entity;
458                 ir_node      *block;
459                 ir_node      *vla_base;
460                 tarval       *enum_val;
461                 ir_type      *irtype;
462         } v;
463 };
464
465 typedef enum {
466         STATEMENT_INVALID,
467         STATEMENT_COMPOUND,
468         STATEMENT_RETURN,
469         STATEMENT_DECLARATION,
470         STATEMENT_IF,
471         STATEMENT_SWITCH,
472         STATEMENT_EXPRESSION,
473         STATEMENT_CONTINUE,
474         STATEMENT_BREAK,
475         STATEMENT_GOTO,
476         STATEMENT_LABEL,
477         STATEMENT_CASE_LABEL,
478         STATEMENT_WHILE,
479         STATEMENT_DO_WHILE,
480         STATEMENT_FOR,
481         STATEMENT_ASM
482 } statement_kind_t;
483
484 struct statement_base_t {
485         statement_kind_t   kind;
486         statement_t       *next;
487         source_position_t  source_position;
488 };
489
490 struct return_statement_t {
491         statement_base_t  base;
492         expression_t     *value;
493 };
494
495 struct compound_statement_t {
496         statement_base_t  base;
497         statement_t      *statements;
498         scope_t           scope;
499 };
500
501 struct declaration_statement_t {
502         statement_base_t  base;
503         declaration_t    *declarations_begin;
504         declaration_t    *declarations_end;
505 };
506
507 struct if_statement_t {
508         statement_base_t  base;
509         expression_t     *condition;
510         statement_t      *true_statement;
511         statement_t      *false_statement;
512 };
513
514 struct switch_statement_t {
515         statement_base_t       base;
516         expression_t           *expression;
517         statement_t            *body;
518         case_label_statement_t *first_case, *last_case;
519 };
520
521 struct goto_statement_t {
522         statement_base_t  base;
523         declaration_t    *label;     /**< The destination label. */
524         goto_statement_t *next;      /**< links all goto statements of a function */
525 };
526
527 struct case_label_statement_t {
528         statement_base_t        base;
529         expression_t           *expression;  /**< The case label expression, NULL for default label. */
530         expression_t           *end_range;   /**< For GNUC case a .. b: the end range expression, NULL else. */
531         statement_t            *statement;
532         case_label_statement_t *next; /**< link to the next case label in switch */
533 };
534
535 struct label_statement_t {
536         statement_base_t   base;
537         declaration_t     *label;
538         statement_t       *statement;
539         label_statement_t *next;    /**< links all label statements of a function */
540 };
541
542 struct expression_statement_t {
543         statement_base_t  base;
544         expression_t     *expression;
545 };
546
547 struct while_statement_t {
548         statement_base_t  base;
549         expression_t     *condition;
550         statement_t      *body;
551 };
552
553 struct do_while_statement_t {
554         statement_base_t  base;
555         expression_t     *condition;
556         statement_t      *body;
557 };
558
559 struct for_statement_t {
560         statement_base_t  base;
561         expression_t     *initialisation;
562         expression_t     *condition;
563         expression_t     *step;
564         statement_t      *body;
565         scope_t           scope;
566 };
567
568 struct asm_constraint_t {
569         string_t          constraints;
570         expression_t     *expression;
571         symbol_t         *symbol;
572         asm_constraint_t *next;
573 };
574
575 struct asm_clobber_t {
576         string_t       clobber;
577         asm_clobber_t *next;
578 };
579
580 struct asm_statement_t {
581         statement_base_t  base;
582         string_t          asm_text;
583         asm_constraint_t *inputs;
584         asm_constraint_t *outputs;
585         asm_clobber_t    *clobbers;
586         bool              is_volatile;
587 };
588
589 union statement_t {
590         statement_kind_t         kind;
591         statement_base_t         base;
592         return_statement_t       returns;
593         compound_statement_t     compound;
594         declaration_statement_t  declaration;
595         if_statement_t           ifs;
596         switch_statement_t       switchs;
597         goto_statement_t         gotos;
598         case_label_statement_t   case_label;
599         label_statement_t        label;
600         expression_statement_t   expression;
601         while_statement_t        whiles;
602         do_while_statement_t     do_while;
603         for_statement_t          fors;
604         asm_statement_t          asms;
605 };
606
607 struct translation_unit_t {
608         scope_t scope;
609 };
610
611 static inline
612 void *_allocate_ast(size_t size)
613 {
614         return obstack_alloc(&ast_obstack, size);
615 }
616
617 #define allocate_ast(size)                 _allocate_ast(size)
618
619 #endif