implement support for complex types
[cparser] / ast_t.h
1 /*
2  * This file is part of cparser.
3  * Copyright (C) 2007-2009 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 "entity_t.h"
31 #include "adt/obst.h"
32
33 /** The AST obstack contains all data that must stay in the AST. */
34 extern struct obstack ast_obstack;
35
36 /**
37  * Operator precedence classes
38  */
39 typedef enum precedence_t {
40         PREC_BOTTOM,
41         PREC_EXPRESSION,     /* ,                                  left to right */
42         PREC_ASSIGNMENT,     /* = += -= *= /= %= <<= >>= &= ^= |=  right to left */
43         PREC_CONDITIONAL,    /* ?:                                 right to left */
44         PREC_LOGICAL_OR,     /* ||                                 left to right */
45         PREC_LOGICAL_AND,    /* &&                                 left to right */
46         PREC_OR,             /* |                                  left to right */
47         PREC_XOR,            /* ^                                  left to right */
48         PREC_AND,            /* &                                  left to right */
49         PREC_EQUALITY,       /* == !=                              left to right */
50         PREC_RELATIONAL,     /* < <= > >=                          left to right */
51         PREC_SHIFT,          /* << >>                              left to right */
52         PREC_ADDITIVE,       /* + -                                left to right */
53         PREC_MULTIPLICATIVE, /* * / %                              left to right */
54         PREC_CAST,           /* (type)                             right to left */
55         PREC_UNARY,          /* ! ~ ++ -- + - * & sizeof           right to left */
56         PREC_POSTFIX,        /* () [] -> .                         left to right */
57         PREC_PRIMARY,
58         PREC_TOP
59 } precedence_t;
60
61 /**
62  * Expression kinds.
63  */
64 typedef enum expression_kind_t {
65         EXPR_ERROR = 1,
66         EXPR_REFERENCE,
67         EXPR_ENUM_CONSTANT,
68         EXPR_LITERAL_BOOLEAN,
69         EXPR_LITERAL_INTEGER,
70         EXPR_LITERAL_FLOATINGPOINT,
71         EXPR_LITERAL_CHARACTER,
72         EXPR_LITERAL_MS_NOOP, /**< MS __noop extension */
73         EXPR_STRING_LITERAL,
74         EXPR_COMPOUND_LITERAL,
75         EXPR_CALL,
76         EXPR_CONDITIONAL,
77         EXPR_SELECT,
78         EXPR_ARRAY_ACCESS,
79         EXPR_SIZEOF,
80         EXPR_CLASSIFY_TYPE,
81         EXPR_ALIGNOF,
82
83         EXPR_FUNCNAME,
84         EXPR_BUILTIN_CONSTANT_P,
85         EXPR_BUILTIN_TYPES_COMPATIBLE_P,
86         EXPR_OFFSETOF,
87         EXPR_VA_START,
88         EXPR_VA_ARG,
89         EXPR_VA_COPY,
90         EXPR_STATEMENT,
91         EXPR_LABEL_ADDRESS, /**< GCC extension &&label operator */
92
93         EXPR_UNARY_FIRST,
94         EXPR_UNARY_NEGATE = EXPR_UNARY_FIRST,
95         EXPR_UNARY_PLUS,
96         EXPR_UNARY_COMPLEMENT,
97         EXPR_UNARY_NOT,
98         EXPR_UNARY_DEREFERENCE,
99         EXPR_UNARY_TAKE_ADDRESS,
100         EXPR_UNARY_POSTFIX_INCREMENT,
101         EXPR_UNARY_POSTFIX_DECREMENT,
102         EXPR_UNARY_PREFIX_INCREMENT,
103         EXPR_UNARY_PREFIX_DECREMENT,
104         EXPR_UNARY_CAST,
105         EXPR_UNARY_ASSUME,        /**< MS __assume() */
106         EXPR_UNARY_DELETE,
107         EXPR_UNARY_DELETE_ARRAY,
108         EXPR_UNARY_THROW,
109         EXPR_UNARY_REAL,
110         EXPR_UNARY_IMAG,
111         EXPR_UNARY_LAST = EXPR_UNARY_IMAG,
112
113         EXPR_BINARY_FIRST,
114         EXPR_BINARY_ADD = EXPR_BINARY_FIRST,
115         EXPR_BINARY_SUB,
116         EXPR_BINARY_MUL,
117         EXPR_BINARY_DIV,
118         EXPR_BINARY_MOD,
119         EXPR_BINARY_EQUAL,
120         EXPR_BINARY_NOTEQUAL,
121         EXPR_BINARY_LESS,
122         EXPR_BINARY_LESSEQUAL,
123         EXPR_BINARY_GREATER,
124         EXPR_BINARY_GREATEREQUAL,
125         EXPR_BINARY_BITWISE_AND,
126         EXPR_BINARY_BITWISE_OR,
127         EXPR_BINARY_BITWISE_XOR,
128         EXPR_BINARY_LOGICAL_AND,
129         EXPR_BINARY_LOGICAL_OR,
130         EXPR_BINARY_SHIFTLEFT,
131         EXPR_BINARY_SHIFTRIGHT,
132         EXPR_BINARY_ASSIGN,
133         EXPR_BINARY_MUL_ASSIGN,
134         EXPR_BINARY_DIV_ASSIGN,
135         EXPR_BINARY_MOD_ASSIGN,
136         EXPR_BINARY_ADD_ASSIGN,
137         EXPR_BINARY_SUB_ASSIGN,
138         EXPR_BINARY_SHIFTLEFT_ASSIGN,
139         EXPR_BINARY_SHIFTRIGHT_ASSIGN,
140         EXPR_BINARY_BITWISE_AND_ASSIGN,
141         EXPR_BINARY_BITWISE_XOR_ASSIGN,
142         EXPR_BINARY_BITWISE_OR_ASSIGN,
143         EXPR_BINARY_COMMA,
144
145         EXPR_BINARY_ISGREATER,
146         EXPR_BINARY_ISGREATEREQUAL,
147         EXPR_BINARY_ISLESS,
148         EXPR_BINARY_ISLESSEQUAL,
149         EXPR_BINARY_ISLESSGREATER,
150         EXPR_BINARY_ISUNORDERED,
151         EXPR_BINARY_LAST = EXPR_BINARY_ISUNORDERED,
152 } expression_kind_t;
153
154 typedef enum funcname_kind_t {
155         FUNCNAME_FUNCTION,           /**< C99 __func__, older __FUNCTION__ */
156         FUNCNAME_PRETTY_FUNCTION,    /**< GNUC __PRETTY_FUNCTION__ */
157         FUNCNAME_FUNCSIG,            /**< MS __FUNCSIG__ */
158         FUNCNAME_FUNCDNAME           /**< MS __FUNCDNAME__ */
159 } funcname_kind_t;
160
161 /* convenience macros */
162 #define EXPR_BINARY_CASES              \
163              EXPR_BINARY_ADD:                \
164         case EXPR_BINARY_SUB:                \
165         case EXPR_BINARY_MUL:                \
166         case EXPR_BINARY_DIV:                \
167         case EXPR_BINARY_MOD:                \
168         case EXPR_BINARY_EQUAL:              \
169         case EXPR_BINARY_NOTEQUAL:           \
170         case EXPR_BINARY_LESS:               \
171         case EXPR_BINARY_LESSEQUAL:          \
172         case EXPR_BINARY_GREATER:            \
173         case EXPR_BINARY_GREATEREQUAL:       \
174         case EXPR_BINARY_BITWISE_AND:        \
175         case EXPR_BINARY_BITWISE_OR:         \
176         case EXPR_BINARY_BITWISE_XOR:        \
177         case EXPR_BINARY_LOGICAL_AND:        \
178         case EXPR_BINARY_LOGICAL_OR:         \
179         case EXPR_BINARY_SHIFTLEFT:          \
180         case EXPR_BINARY_SHIFTRIGHT:         \
181         case EXPR_BINARY_ASSIGN:             \
182         case EXPR_BINARY_MUL_ASSIGN:         \
183         case EXPR_BINARY_DIV_ASSIGN:         \
184         case EXPR_BINARY_MOD_ASSIGN:         \
185         case EXPR_BINARY_ADD_ASSIGN:         \
186         case EXPR_BINARY_SUB_ASSIGN:         \
187         case EXPR_BINARY_SHIFTLEFT_ASSIGN:   \
188         case EXPR_BINARY_SHIFTRIGHT_ASSIGN:  \
189         case EXPR_BINARY_BITWISE_AND_ASSIGN: \
190         case EXPR_BINARY_BITWISE_XOR_ASSIGN: \
191         case EXPR_BINARY_BITWISE_OR_ASSIGN:  \
192         case EXPR_BINARY_COMMA:              \
193         case EXPR_BINARY_ISGREATER:          \
194         case EXPR_BINARY_ISGREATEREQUAL:     \
195         case EXPR_BINARY_ISLESS:             \
196         case EXPR_BINARY_ISLESSEQUAL:        \
197         case EXPR_BINARY_ISLESSGREATER:      \
198         case EXPR_BINARY_ISUNORDERED
199
200 /**
201  * unary expression with mandatory operand
202  */
203 #define EXPR_UNARY_CASES_MANDATORY   \
204              EXPR_UNARY_NEGATE:            \
205         case EXPR_UNARY_PLUS:              \
206         case EXPR_UNARY_COMPLEMENT:        \
207         case EXPR_UNARY_NOT:               \
208         case EXPR_UNARY_DEREFERENCE:       \
209         case EXPR_UNARY_TAKE_ADDRESS:      \
210         case EXPR_UNARY_POSTFIX_INCREMENT: \
211         case EXPR_UNARY_POSTFIX_DECREMENT: \
212         case EXPR_UNARY_PREFIX_INCREMENT:  \
213         case EXPR_UNARY_PREFIX_DECREMENT:  \
214         case EXPR_UNARY_CAST:              \
215         case EXPR_UNARY_ASSUME:            \
216         case EXPR_UNARY_DELETE:            \
217         case EXPR_UNARY_DELETE_ARRAY:      \
218         case EXPR_UNARY_IMAG:              \
219         case EXPR_UNARY_REAL
220
221 /**
222  * unary expression with optional operand
223  */
224 #define EXPR_UNARY_CASES_OPTIONAL \
225         EXPR_UNARY_THROW
226
227 #define EXPR_UNARY_CASES           \
228              EXPR_UNARY_CASES_MANDATORY: \
229         case EXPR_UNARY_CASES_OPTIONAL
230
231 #define EXPR_LITERAL_CASES                     \
232              EXPR_LITERAL_BOOLEAN:                   \
233         case EXPR_LITERAL_INTEGER:                   \
234         case EXPR_LITERAL_FLOATINGPOINT:             \
235         case EXPR_LITERAL_MS_NOOP
236
237 /**
238  * The base class of every expression.
239  */
240 struct expression_base_t {
241         expression_kind_t kind;     /**< The expression kind. */
242         type_t           *type;     /**< The type of the expression. */
243         position_t        pos;      /**< The source position of this expression. */
244         bool              parenthesized : 1;
245 #ifndef NDEBUG
246         bool              transformed : 1; /**< Set if this expression was transformed. */
247 #endif
248         bool              implicit : 1;  /**< compiler generated expression.
249                                             Examples: select into anonymous structs,
250                                             implicit casts */
251 };
252
253 /**
254  * integer, float and boolean constants
255  */
256 struct literal_expression_t {
257         expression_base_t base;
258         string_t          value;
259         char const       *suffix; /**< Start of the suffix in value. */
260
261         /* ast2firm data */
262         ir_tarval        *target_value;
263 };
264
265 /**
266  * string and character literals
267  */
268 struct string_literal_expression_t {
269         expression_base_t base;
270         string_t          value;
271 };
272
273 struct funcname_expression_t {
274         expression_base_t  base;
275         funcname_kind_t    kind;
276 };
277
278 struct compound_literal_expression_t {
279         expression_base_t  base;
280         type_t            *type;
281         initializer_t     *initializer;
282         bool               global_scope;
283 };
284
285 struct builtin_constant_expression_t {
286         expression_base_t  base;
287         expression_t      *value;
288 };
289
290 struct builtin_types_compatible_expression_t {
291         expression_base_t  base;
292         type_t            *left;
293         type_t            *right;
294 };
295
296 struct reference_expression_t {
297         expression_base_t  base;
298         entity_t          *entity;
299 };
300
301 /**
302  * An argument of a call.
303  */
304 struct call_argument_t {
305         expression_t    *expression;  /**< The expression which value is transmitted. */
306         call_argument_t *next;        /**< Links to the next argument of this call. */
307 };
308
309
310 struct call_expression_t {
311         expression_base_t  base;
312         expression_t      *function;  /**< The address of the function to call. */
313         call_argument_t   *arguments; /**< List of arguments of this call. */
314 };
315
316
317 struct unary_expression_t {
318         expression_base_t  base;
319         expression_t      *value;     /**< The unary operand. */
320 };
321
322 struct binary_expression_t {
323         expression_base_t  base;
324         expression_t      *left;
325         expression_t      *right;
326 };
327
328 struct select_expression_t {
329         expression_base_t  base;
330         expression_t      *compound;
331         entity_t          *compound_entry;
332 };
333
334 struct array_access_expression_t {
335         expression_base_t  base;
336         expression_t      *array_ref; /**< the referenced array */
337         expression_t      *index;     /**< the index used */
338         bool               flipped;   /**< True if index/ref was written in a 5[a] way */
339 };
340
341 struct typeprop_expression_t {
342         expression_base_t  base;
343         type_t            *type;
344         expression_t      *tp_expression;
345 };
346
347 struct designator_t {
348         position_t    pos;
349         symbol_t     *symbol;      /**< the symbol if any */
350         expression_t *array_index; /**< the array index if any */
351         designator_t *next;
352 };
353
354 struct offsetof_expression_t {
355         expression_base_t  base;
356         type_t            *type;
357         designator_t      *designator;
358 };
359
360 struct va_start_expression_t {
361         expression_base_t  base;
362         expression_t      *ap;
363         expression_t      *parameter;
364 };
365
366 struct va_arg_expression_t {
367         expression_base_t  base;
368         expression_t      *ap;
369 };
370
371 struct va_copy_expression_t {
372         expression_base_t  base;
373         expression_t      *dst;    /**< destination argument */
374         expression_t      *src;    /**< source argument */
375 };
376
377 struct conditional_expression_t {
378         expression_base_t  base;
379         expression_t      *condition;
380         expression_t      *true_expression;
381         expression_t      *false_expression;
382 };
383
384 struct statement_expression_t {
385         expression_base_t  base;
386         statement_t       *statement;
387 };
388
389 struct classify_type_expression_t {
390         expression_base_t  base;
391         expression_t      *type_expression;
392 };
393
394 struct label_address_expression_t {
395         expression_base_t  base;
396         label_t           *label;
397 };
398
399 union expression_t {
400         expression_kind_t                     kind;
401         expression_base_t                     base;
402         literal_expression_t                  literal;
403         string_literal_expression_t           string_literal;
404         funcname_expression_t                 funcname;
405         compound_literal_expression_t         compound_literal;
406         builtin_constant_expression_t         builtin_constant;
407         builtin_types_compatible_expression_t builtin_types_compatible;
408         reference_expression_t                reference;
409         call_expression_t                     call;
410         unary_expression_t                    unary;
411         binary_expression_t                   binary;
412         select_expression_t                   select;
413         array_access_expression_t             array_access;
414         typeprop_expression_t                 typeprop;
415         offsetof_expression_t                 offsetofe;
416         va_start_expression_t                 va_starte;
417         va_arg_expression_t                   va_arge;
418         va_copy_expression_t                  va_copye;
419         conditional_expression_t              conditional;
420         statement_expression_t                statement;
421         classify_type_expression_t            classify_type;
422         label_address_expression_t            label_address;
423 };
424
425 typedef enum initializer_kind_t {
426         INITIALIZER_VALUE,
427         INITIALIZER_LIST,
428         INITIALIZER_STRING,
429         INITIALIZER_DESIGNATOR
430 } initializer_kind_t;
431
432 struct initializer_base_t {
433         initializer_kind_t kind;
434 };
435
436 struct initializer_value_t {
437         initializer_base_t  base;
438         expression_t       *value;
439 };
440
441 struct initializer_list_t {
442         initializer_base_t  base;
443         size_t              len;
444         initializer_t      *initializers[];
445 };
446
447 struct initializer_designator_t {
448         initializer_base_t  base;
449         designator_t       *designator;
450 };
451
452 union initializer_t {
453         initializer_kind_t        kind;
454         initializer_base_t        base;
455         initializer_value_t       value;
456         initializer_list_t        list;
457         initializer_designator_t  designator;
458 };
459
460 static inline string_literal_expression_t const *get_init_string(initializer_t const *const init)
461 {
462         assert(init->kind == INITIALIZER_STRING);
463         assert(init->value.value->kind == EXPR_STRING_LITERAL);
464         return &init->value.value->string_literal;
465 }
466
467 /**
468  * The statement kinds.
469  */
470 typedef enum statement_kind_t {
471         STATEMENT_ERROR = 1,
472         STATEMENT_EMPTY,
473         STATEMENT_COMPOUND,
474         STATEMENT_RETURN,
475         STATEMENT_DECLARATION,
476         STATEMENT_IF,
477         STATEMENT_SWITCH,
478         STATEMENT_EXPRESSION,
479         STATEMENT_CONTINUE,
480         STATEMENT_BREAK,
481         STATEMENT_COMPUTED_GOTO,
482         STATEMENT_GOTO,
483         STATEMENT_LABEL,
484         STATEMENT_CASE_LABEL,
485         STATEMENT_DO_WHILE,
486         STATEMENT_FOR,
487         STATEMENT_ASM,
488         STATEMENT_MS_TRY,          /**< MS __try/__finally or __try/__except */
489         STATEMENT_LEAVE            /**< MS __leave */
490 } statement_kind_t;
491
492 /**
493  * The base class of every statement.
494  */
495 struct statement_base_t {
496         statement_kind_t kind;
497         statement_t     *next;      /**< Point to the next statement in a compound statement. */
498         position_t       pos;
499         statement_t     *parent;    /**< The Parent statement that controls the execution. */
500         bool             reachable; /**< True, if this statement is reachable. */
501 #ifndef NDEBUG
502         bool             transformed;
503 #endif
504 };
505
506 struct return_statement_t {
507         statement_base_t  base;
508         expression_t     *value;    /**< The return value if any. */
509 };
510
511 struct compound_statement_t {
512         statement_base_t  base;
513         statement_t      *statements;
514         scope_t           scope;
515         bool              stmt_expr; /**< True if this compound statement is a statement expression. */
516 };
517
518 struct declaration_statement_t {
519         statement_base_t  base;
520         entity_t         *declarations_begin;
521         entity_t         *declarations_end;
522 };
523
524 struct if_statement_t {
525         statement_base_t  base;
526         scope_t           scope;
527         expression_t     *condition;
528         statement_t      *true_statement;
529         statement_t      *false_statement;
530 };
531
532 struct switch_statement_t {
533         statement_base_t        base;
534         scope_t                 scope;
535         expression_t           *expression;
536         statement_t            *body;
537         case_label_statement_t *first_case, *last_case; /**< List of all cases, including default. */
538         case_label_statement_t *default_label;          /**< The default label if existent. */
539 };
540
541 struct goto_statement_t {
542         statement_base_t  base;
543         label_t          *label;         /**< The destination label. */
544         goto_statement_t *next;          /**< links all goto statements of a function */
545 };
546
547 struct computed_goto_statement_t {
548         statement_base_t  base;
549         expression_t     *expression; /**< The expression for the computed goto. */
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         case_label_statement_t *next;          /**< link to the next case label in switch */
557         statement_t            *statement;
558         ir_tarval              *first_case;
559         ir_tarval              *last_case;
560         bool                   is_bad;         /**< If set marked as bad to suppress warnings. */
561         bool                   is_empty_range; /**< If set marked this as an empty range. */
562         long                   pn;
563 };
564
565 struct label_statement_t {
566         statement_base_t   base;
567         label_t           *label;
568         statement_t       *statement;
569         label_statement_t *next;    /**< links all label statements of a function */
570 };
571
572 struct expression_statement_t {
573         statement_base_t  base;
574         expression_t     *expression;
575 };
576
577 struct do_while_statement_t {
578         statement_base_t  base;
579         scope_t           scope;
580         expression_t     *condition;
581         statement_t      *body;
582 };
583
584 struct for_statement_t {
585         statement_base_t  base;
586         scope_t           scope;
587         expression_t     *initialisation;
588         expression_t     *condition;
589         expression_t     *step;
590         statement_t      *body;
591         bool              condition_reachable:1;
592         bool              step_reachable:1;
593 };
594
595 struct asm_argument_t {
596         string_t        constraints;
597         expression_t   *expression;
598         symbol_t       *symbol;
599         asm_argument_t *next;
600 };
601
602 struct asm_clobber_t {
603         string_t       clobber;
604         asm_clobber_t *next;
605 };
606
607 struct asm_label_t {
608         label_t     *label;
609         asm_label_t *next;
610 };
611
612 struct asm_statement_t {
613         statement_base_t base;
614         string_t         asm_text;
615         asm_argument_t  *inputs;
616         asm_argument_t  *outputs;
617         asm_clobber_t   *clobbers;
618         asm_label_t     *labels;
619         bool             is_volatile;
620 };
621
622 struct ms_try_statement_t {
623         statement_base_t  base;
624         statement_t      *try_statement;
625         expression_t     *except_expression; /**< non-null for except, NULL for finally */
626         statement_t      *final_statement;
627 };
628
629 struct leave_statement_t {
630         statement_base_t  base;
631 };
632
633 union statement_t {
634         statement_kind_t          kind;
635         statement_base_t          base;
636         return_statement_t        returns;
637         compound_statement_t      compound;
638         declaration_statement_t   declaration;
639         if_statement_t            ifs;
640         switch_statement_t        switchs;
641         computed_goto_statement_t computed_goto;
642         goto_statement_t          gotos;
643         case_label_statement_t    case_label;
644         label_statement_t         label;
645         expression_statement_t    expression;
646         do_while_statement_t      do_while;
647         for_statement_t           fors;
648         asm_statement_t           asms;
649         ms_try_statement_t        ms_try;
650         leave_statement_t         leave;
651 };
652
653 struct translation_unit_t {
654         scope_t      scope;
655         statement_t *global_asm;
656 };
657
658 /**
659  * Allocate an AST node with given size and
660  * initialize all fields with zero.
661  */
662 static inline void *allocate_ast_zero(size_t size)
663 {
664         return memset(obstack_alloc(&ast_obstack, size), 0, size);
665 }
666
667 /** If set, implicit casts are printed. */
668 extern bool print_implicit_casts;
669 /** If set parenthesis are printed to indicate operator precedence. */
670 extern bool print_parenthesis;
671
672 #endif