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