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