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