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