6b6b94ea4a000fdb423bc87b3aa73631ae727c3c
[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_FUNCNAME,
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 typedef enum {
121         FUNCNAME_FUNCTION,           /**< C99 __func__, older __FUNCTION__ */
122         FUNCNAME_PRETTY_FUNCTION,    /**< GNUC __PRETTY_FUNCTION__ */
123         FUNCNAME_FUNCSIG,            /**< MS __FUNCSIG__ */
124         FUNCNAME_FUNCDNAME           /**< MS __FUNCDNAME__ */
125 } funcname_kind_t;
126
127 /* convenience macros */
128 #define EXPR_BINARY_CASES                  \
129         case EXPR_BINARY_ADD:                  \
130         case EXPR_BINARY_SUB:                  \
131         case EXPR_BINARY_MUL:                  \
132         case EXPR_BINARY_DIV:                  \
133         case EXPR_BINARY_MOD:                  \
134         case EXPR_BINARY_EQUAL:                \
135         case EXPR_BINARY_NOTEQUAL:             \
136         case EXPR_BINARY_LESS:                 \
137         case EXPR_BINARY_LESSEQUAL:            \
138         case EXPR_BINARY_GREATER:              \
139         case EXPR_BINARY_GREATEREQUAL:         \
140         case EXPR_BINARY_BITWISE_AND:          \
141         case EXPR_BINARY_BITWISE_OR:           \
142         case EXPR_BINARY_BITWISE_XOR:          \
143         case EXPR_BINARY_LOGICAL_AND:          \
144         case EXPR_BINARY_LOGICAL_OR:           \
145         case EXPR_BINARY_SHIFTLEFT:            \
146         case EXPR_BINARY_SHIFTRIGHT:           \
147         case EXPR_BINARY_ASSIGN:               \
148         case EXPR_BINARY_MUL_ASSIGN:           \
149         case EXPR_BINARY_DIV_ASSIGN:           \
150         case EXPR_BINARY_MOD_ASSIGN:           \
151         case EXPR_BINARY_ADD_ASSIGN:           \
152         case EXPR_BINARY_SUB_ASSIGN:           \
153         case EXPR_BINARY_SHIFTLEFT_ASSIGN:     \
154         case EXPR_BINARY_SHIFTRIGHT_ASSIGN:    \
155         case EXPR_BINARY_BITWISE_AND_ASSIGN:   \
156         case EXPR_BINARY_BITWISE_XOR_ASSIGN:   \
157         case EXPR_BINARY_BITWISE_OR_ASSIGN:    \
158         case EXPR_BINARY_COMMA:                \
159         case EXPR_BINARY_BUILTIN_EXPECT:       \
160         case EXPR_BINARY_ISGREATER:            \
161         case EXPR_BINARY_ISGREATEREQUAL:       \
162         case EXPR_BINARY_ISLESS:               \
163         case EXPR_BINARY_ISLESSEQUAL:          \
164         case EXPR_BINARY_ISLESSGREATER:        \
165         case EXPR_BINARY_ISUNORDERED:
166
167 #define EXPR_UNARY_CASES                   \
168         case EXPR_UNARY_NEGATE:                \
169         case EXPR_UNARY_PLUS:                  \
170         case EXPR_UNARY_BITWISE_NEGATE:        \
171         case EXPR_UNARY_NOT:                   \
172         case EXPR_UNARY_DEREFERENCE:           \
173         case EXPR_UNARY_TAKE_ADDRESS:          \
174         case EXPR_UNARY_POSTFIX_INCREMENT:     \
175         case EXPR_UNARY_POSTFIX_DECREMENT:     \
176         case EXPR_UNARY_PREFIX_INCREMENT:      \
177         case EXPR_UNARY_PREFIX_DECREMENT:      \
178         case EXPR_UNARY_CAST:                  \
179         case EXPR_UNARY_CAST_IMPLICIT:         \
180         case EXPR_UNARY_ASSUME:                \
181         case EXPR_UNARY_BITFIELD_EXTRACT:
182
183 /**
184  * A scope containing declarations.
185  */
186 struct scope_t {
187         declaration_t *declarations;      /**< List of declarations in this scope. */
188         declaration_t *last_declaration;  /**< last declaration in this scope. */
189 };
190
191 struct expression_base_t {
192         expression_kind_t   kind;
193         type_t             *type;
194         source_position_t   source_position;
195 };
196
197 struct const_expression_t {
198         expression_base_t  base;
199         union {
200                 long long      int_value;
201                 long double    float_value;
202                 string_t       character;
203                 wide_string_t  wide_character;
204         } v;
205         bool               is_ms_noop;  /**< True, if this constant is the result
206                                              of an microsoft __noop operator */
207 };
208
209 struct string_literal_expression_t {
210         expression_base_t  base;
211         string_t           value;
212 };
213
214 struct funcname_expression_t {
215         expression_base_t  base;
216         funcname_kind_t    kind;
217         string_t           value;     /**< the value once assigned. */
218 };
219
220 struct wide_string_literal_expression_t {
221         expression_base_t  base;
222         wide_string_t      value;
223 };
224
225 struct compound_literal_expression_t {
226         expression_base_t  base;
227         type_t            *type;
228         initializer_t     *initializer;
229 };
230
231 struct builtin_symbol_expression_t {
232         expression_base_t  base;
233         symbol_t          *symbol;
234 };
235
236 struct builtin_constant_expression_t {
237         expression_base_t  base;
238         expression_t      *value;
239 };
240
241 struct builtin_prefetch_expression_t {
242         expression_base_t  base;
243         expression_t      *adr;
244         expression_t      *rw;
245         expression_t      *locality;
246 };
247
248 struct reference_expression_t {
249         expression_base_t  base;
250         symbol_t          *symbol;
251         declaration_t     *declaration;
252 };
253
254 struct call_argument_t {
255         expression_t    *expression;
256         call_argument_t *next;
257 };
258
259 struct call_expression_t {
260         expression_base_t  base;
261         expression_t      *function;
262         call_argument_t   *arguments;
263 };
264
265 struct unary_expression_t {
266         expression_base_t  base;
267         expression_t      *value;
268 };
269
270 struct binary_expression_t {
271         expression_base_t  base;
272         expression_t      *left;
273         expression_t      *right;
274 };
275
276 struct select_expression_t {
277         expression_base_t  base;
278         expression_t      *compound;
279         symbol_t          *symbol;
280
281         declaration_t     *compound_entry;
282 };
283
284 struct array_access_expression_t {
285         expression_base_t  base;
286         expression_t      *array_ref;
287         expression_t      *index;
288         bool               flipped; /* index/ref was written in a 5[a] way */
289 };
290
291 struct typeprop_expression_t {
292         expression_base_t  base;
293         type_t            *type;
294         expression_t      *tp_expression;
295 };
296
297 struct designator_t {
298         source_position_t  source_position;
299         symbol_t          *symbol;
300         expression_t      *array_index;
301         designator_t      *next;
302 };
303
304 struct offsetof_expression_t {
305         expression_base_t  base;
306         type_t            *type;
307         designator_t      *designator;
308 };
309
310 struct va_start_expression_t {
311         expression_base_t  base;
312         expression_t      *ap;
313         declaration_t     *parameter;
314 };
315
316 struct va_arg_expression_t {
317         expression_base_t  base;
318         expression_t      *ap;
319 };
320
321 struct conditional_expression_t {
322         expression_base_t  base;
323         expression_t      *condition;
324         expression_t      *true_expression;
325         expression_t      *false_expression;
326 };
327
328 struct statement_expression_t {
329         expression_base_t  base;
330         statement_t       *statement;
331 };
332
333 struct classify_type_expression_t {
334         expression_base_t  base;
335         expression_t      *type_expression;
336 };
337
338 union expression_t {
339         expression_kind_t                kind;
340         expression_base_t                base;
341         const_expression_t               conste;
342         funcname_expression_t            funcname;
343         string_literal_expression_t      string;
344         wide_string_literal_expression_t wide_string;
345         compound_literal_expression_t    compound_literal;
346         builtin_symbol_expression_t      builtin_symbol;
347         builtin_constant_expression_t    builtin_constant;
348         builtin_prefetch_expression_t    builtin_prefetch;
349         reference_expression_t           reference;
350         call_expression_t                call;
351         unary_expression_t               unary;
352         binary_expression_t              binary;
353         select_expression_t              select;
354         array_access_expression_t        array_access;
355         typeprop_expression_t            typeprop;
356         offsetof_expression_t            offsetofe;
357         va_start_expression_t            va_starte;
358         va_arg_expression_t              va_arge;
359         conditional_expression_t         conditional;
360         statement_expression_t           statement;
361         classify_type_expression_t       classify_type;
362 };
363
364 typedef enum {
365         STORAGE_CLASS_NONE,
366         STORAGE_CLASS_EXTERN,
367         STORAGE_CLASS_STATIC,
368         STORAGE_CLASS_TYPEDEF,
369         STORAGE_CLASS_AUTO,
370         STORAGE_CLASS_REGISTER,
371         STORAGE_CLASS_ENUM_ENTRY,
372         STORAGE_CLASS_THREAD,
373         STORAGE_CLASS_THREAD_EXTERN,
374         STORAGE_CLASS_THREAD_STATIC,
375 } storage_class_tag_t;
376
377 typedef enum {
378         NAMESPACE_NORMAL,
379         NAMESPACE_STRUCT,
380         NAMESPACE_UNION,
381         NAMESPACE_ENUM,
382         NAMESPACE_LABEL,
383 } namespace_t;
384
385 typedef enum {
386         INITIALIZER_VALUE,
387         INITIALIZER_LIST,
388         INITIALIZER_STRING,
389         INITIALIZER_WIDE_STRING,
390         INITIALIZER_DESIGNATOR
391 } initializer_kind_t;
392
393 struct initializer_base_t {
394         initializer_kind_t kind;
395 };
396
397 struct initializer_value_t {
398         initializer_base_t  base;
399         expression_t       *value;
400 };
401
402 struct initializer_list_t {
403         initializer_base_t  base;
404         size_t              len;
405         initializer_t      *initializers[];
406 };
407
408 struct initializer_string_t {
409         initializer_base_t base;
410         string_t           string;
411 };
412
413 struct initializer_wide_string_t {
414         initializer_base_t  base;
415         wide_string_t       string;
416 };
417
418 struct initializer_designator_t {
419         initializer_base_t  base;
420         designator_t       *designator;
421 };
422
423 union initializer_t {
424         initializer_kind_t        kind;
425         initializer_base_t        base;
426         initializer_value_t       value;
427         initializer_list_t        list;
428         initializer_string_t      string;
429         initializer_wide_string_t wide_string;
430         initializer_designator_t  designator;
431 };
432
433 /**
434  * Extended microsoft modifier.
435  */
436 typedef enum {
437         DM_DLLIMPORT        = (1 <<  0),
438         DM_DLLEXPORT        = (1 <<  1),
439         DM_THREAD           = (1 <<  2),
440         DM_NAKED            = (1 <<  3),
441         DM_MICROSOFT_INLINE = (1 <<  4),
442         DM_FORCEINLINE      = (1 <<  5),
443         DM_SELECTANY        = (1 <<  6),
444         DM_NOTHROW          = (1 <<  7),
445         DM_NOVTABLE         = (1 <<  8),
446         DM_NORETURN         = (1 <<  9),
447         DM_NOINLINE         = (1 << 10),
448         DM_RESTRICT         = (1 << 11),
449         DM_NOALIAS          = (1 << 12)
450 } decl_modifier_t;
451
452 typedef unsigned short decl_modifiers_t;
453
454 struct declaration_t {
455         unsigned char       namespc;
456         unsigned char       declared_storage_class;
457         unsigned char       storage_class;
458         unsigned char       alignment;          /**< Alignment of the declaration, 0 for default. */
459         decl_modifiers_t    modifiers;          /**< MS __declspec modifiers. */
460         const char         *deprecated_string;  /**< MS deprecated string if any. */
461         symbol_t           *get_property_sym;   /**< MS get property. */
462         symbol_t           *put_property_sym;   /**< MS put property. */
463         unsigned int        address_taken : 1;
464         unsigned int        is_inline     : 1;
465         unsigned int        used          : 1;  /**< Set if the declaration is used. */
466         unsigned int        deprecated    : 1;  /**< Microsoft of GNU deprecated attribute. */
467         type_t             *type;
468         symbol_t           *symbol;
469         source_position_t   source_position;
470         union {
471                 bool            is_defined;
472                 statement_t    *statement;
473                 initializer_t  *initializer;
474                 expression_t   *enum_value;
475         } init;
476         scope_t             scope;              /**< The scope that this declaration opens. */
477         scope_t            *parent_scope;       /**< The parant scope where this declaration lives. */
478
479         /** next declaration in a scope */
480         declaration_t      *next;
481         /** next declaration with same symbol */
482         declaration_t      *symbol_next;
483
484         /* the following fields are used in ast2firm module */
485         unsigned char       declaration_kind;
486         union {
487                 unsigned int  value_number;
488                 ir_entity    *entity;
489                 ir_node      *block;
490                 ir_node      *vla_base;
491                 tarval       *enum_val;
492                 ir_type      *irtype;
493         } v;
494 };
495
496 typedef enum {
497         STATEMENT_INVALID,
498         STATEMENT_EMPTY,
499         STATEMENT_COMPOUND,
500         STATEMENT_RETURN,
501         STATEMENT_DECLARATION,
502         STATEMENT_IF,
503         STATEMENT_SWITCH,
504         STATEMENT_EXPRESSION,
505         STATEMENT_CONTINUE,
506         STATEMENT_BREAK,
507         STATEMENT_GOTO,
508         STATEMENT_LABEL,
509         STATEMENT_CASE_LABEL,
510         STATEMENT_WHILE,
511         STATEMENT_DO_WHILE,
512         STATEMENT_FOR,
513         STATEMENT_ASM
514 } statement_kind_t;
515
516 struct statement_base_t {
517         statement_kind_t   kind;
518         statement_t       *next;
519         source_position_t  source_position;
520 };
521
522 struct invalid_statement_t {
523         statement_base_t  base;
524 };
525
526 struct empty_statement_t {
527         statement_base_t  base;
528 };
529
530 struct return_statement_t {
531         statement_base_t  base;
532         expression_t     *value;
533 };
534
535 struct compound_statement_t {
536         statement_base_t  base;
537         statement_t      *statements;
538         scope_t           scope;
539 };
540
541 struct declaration_statement_t {
542         statement_base_t  base;
543         declaration_t    *declarations_begin;
544         declaration_t    *declarations_end;
545 };
546
547 struct if_statement_t {
548         statement_base_t  base;
549         expression_t     *condition;
550         statement_t      *true_statement;
551         statement_t      *false_statement;
552 };
553
554 struct switch_statement_t {
555         statement_base_t       base;
556         expression_t           *expression;
557         statement_t            *body;
558         case_label_statement_t *first_case, *last_case;
559 };
560
561 struct goto_statement_t {
562         statement_base_t  base;
563         declaration_t    *label;     /**< The destination label. */
564         goto_statement_t *next;      /**< links all goto statements of a function */
565 };
566
567 struct case_label_statement_t {
568         statement_base_t        base;
569         expression_t           *expression;  /**< The case label expression, NULL for default label. */
570         expression_t           *end_range;   /**< For GNUC case a .. b: the end range expression, NULL else. */
571         statement_t            *statement;
572         case_label_statement_t *next; /**< link to the next case label in switch */
573 };
574
575 struct label_statement_t {
576         statement_base_t   base;
577         declaration_t     *label;
578         statement_t       *statement;
579         label_statement_t *next;    /**< links all label statements of a function */
580 };
581
582 struct expression_statement_t {
583         statement_base_t  base;
584         expression_t     *expression;
585 };
586
587 struct while_statement_t {
588         statement_base_t  base;
589         expression_t     *condition;
590         statement_t      *body;
591 };
592
593 struct do_while_statement_t {
594         statement_base_t  base;
595         expression_t     *condition;
596         statement_t      *body;
597 };
598
599 struct for_statement_t {
600         statement_base_t  base;
601         expression_t     *initialisation;
602         expression_t     *condition;
603         expression_t     *step;
604         statement_t      *body;
605         scope_t           scope;
606 };
607
608 struct asm_constraint_t {
609         string_t          constraints;
610         expression_t     *expression;
611         symbol_t         *symbol;
612         asm_constraint_t *next;
613 };
614
615 struct asm_clobber_t {
616         string_t       clobber;
617         asm_clobber_t *next;
618 };
619
620 struct asm_statement_t {
621         statement_base_t  base;
622         string_t          asm_text;
623         asm_constraint_t *inputs;
624         asm_constraint_t *outputs;
625         asm_clobber_t    *clobbers;
626         bool              is_volatile;
627 };
628
629 union statement_t {
630         statement_kind_t         kind;
631         statement_base_t         base;
632         return_statement_t       returns;
633         compound_statement_t     compound;
634         declaration_statement_t  declaration;
635         if_statement_t           ifs;
636         switch_statement_t       switchs;
637         goto_statement_t         gotos;
638         case_label_statement_t   case_label;
639         label_statement_t        label;
640         expression_statement_t   expression;
641         while_statement_t        whiles;
642         do_while_statement_t     do_while;
643         for_statement_t          fors;
644         asm_statement_t          asms;
645 };
646
647 struct translation_unit_t {
648         scope_t scope;
649 };
650
651 static inline
652 void *_allocate_ast(size_t size)
653 {
654         return obstack_alloc(&ast_obstack, size);
655 }
656
657 static inline
658 bool is_invalid_expression(expression_t *expression)
659 {
660         return expression->base.kind == EXPR_INVALID;
661 }
662
663 static inline
664 bool is_invalid_statement(statement_t *statement)
665 {
666         return statement->base.kind == STATEMENT_INVALID;
667 }
668
669
670 #define allocate_ast(size)                 _allocate_ast(size)
671
672 #endif