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