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