Use struct string_literal_expression_t instead of struct literal_expression_t for...
[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_WIDE_CHARACTER,
73         EXPR_LITERAL_MS_NOOP, /**< MS __noop extension */
74         EXPR_STRING_LITERAL,
75         EXPR_COMPOUND_LITERAL,
76         EXPR_CALL,
77         EXPR_CONDITIONAL,
78         EXPR_SELECT,
79         EXPR_ARRAY_ACCESS,
80         EXPR_SIZEOF,
81         EXPR_CLASSIFY_TYPE,
82         EXPR_ALIGNOF,
83
84         EXPR_FUNCNAME,
85         EXPR_BUILTIN_CONSTANT_P,
86         EXPR_BUILTIN_TYPES_COMPATIBLE_P,
87         EXPR_OFFSETOF,
88         EXPR_VA_START,
89         EXPR_VA_ARG,
90         EXPR_VA_COPY,
91         EXPR_STATEMENT,
92         EXPR_LABEL_ADDRESS, /**< GCC extension &&label operator */
93
94         EXPR_UNARY_FIRST,
95         EXPR_UNARY_NEGATE = EXPR_UNARY_FIRST,
96         EXPR_UNARY_PLUS,
97         EXPR_UNARY_BITWISE_NEGATE,
98         EXPR_UNARY_NOT,
99         EXPR_UNARY_DEREFERENCE,
100         EXPR_UNARY_TAKE_ADDRESS,
101         EXPR_UNARY_POSTFIX_INCREMENT,
102         EXPR_UNARY_POSTFIX_DECREMENT,
103         EXPR_UNARY_PREFIX_INCREMENT,
104         EXPR_UNARY_PREFIX_DECREMENT,
105         EXPR_UNARY_CAST,
106         EXPR_UNARY_ASSUME,        /**< MS __assume() */
107         EXPR_UNARY_DELETE,
108         EXPR_UNARY_DELETE_ARRAY,
109         EXPR_UNARY_THROW,
110         EXPR_UNARY_LAST = EXPR_UNARY_THROW,
111
112         EXPR_BINARY_FIRST,
113         EXPR_BINARY_ADD = EXPR_BINARY_FIRST,
114         EXPR_BINARY_SUB,
115         EXPR_BINARY_MUL,
116         EXPR_BINARY_DIV,
117         EXPR_BINARY_MOD,
118         EXPR_BINARY_EQUAL,
119         EXPR_BINARY_NOTEQUAL,
120         EXPR_BINARY_LESS,
121         EXPR_BINARY_LESSEQUAL,
122         EXPR_BINARY_GREATER,
123         EXPR_BINARY_GREATEREQUAL,
124         EXPR_BINARY_BITWISE_AND,
125         EXPR_BINARY_BITWISE_OR,
126         EXPR_BINARY_BITWISE_XOR,
127         EXPR_BINARY_LOGICAL_AND,
128         EXPR_BINARY_LOGICAL_OR,
129         EXPR_BINARY_SHIFTLEFT,
130         EXPR_BINARY_SHIFTRIGHT,
131         EXPR_BINARY_ASSIGN,
132         EXPR_BINARY_MUL_ASSIGN,
133         EXPR_BINARY_DIV_ASSIGN,
134         EXPR_BINARY_MOD_ASSIGN,
135         EXPR_BINARY_ADD_ASSIGN,
136         EXPR_BINARY_SUB_ASSIGN,
137         EXPR_BINARY_SHIFTLEFT_ASSIGN,
138         EXPR_BINARY_SHIFTRIGHT_ASSIGN,
139         EXPR_BINARY_BITWISE_AND_ASSIGN,
140         EXPR_BINARY_BITWISE_XOR_ASSIGN,
141         EXPR_BINARY_BITWISE_OR_ASSIGN,
142         EXPR_BINARY_COMMA,
143
144         EXPR_BINARY_ISGREATER,
145         EXPR_BINARY_ISGREATEREQUAL,
146         EXPR_BINARY_ISLESS,
147         EXPR_BINARY_ISLESSEQUAL,
148         EXPR_BINARY_ISLESSGREATER,
149         EXPR_BINARY_ISUNORDERED,
150         EXPR_BINARY_LAST = EXPR_BINARY_ISUNORDERED,
151 } expression_kind_t;
152
153 typedef enum funcname_kind_t {
154         FUNCNAME_FUNCTION,           /**< C99 __func__, older __FUNCTION__ */
155         FUNCNAME_PRETTY_FUNCTION,    /**< GNUC __PRETTY_FUNCTION__ */
156         FUNCNAME_FUNCSIG,            /**< MS __FUNCSIG__ */
157         FUNCNAME_FUNCDNAME           /**< MS __FUNCDNAME__ */
158 } funcname_kind_t;
159
160 /* convenience macros */
161 #define EXPR_BINARY_CASES              \
162              EXPR_BINARY_ADD:                \
163         case EXPR_BINARY_SUB:                \
164         case EXPR_BINARY_MUL:                \
165         case EXPR_BINARY_DIV:                \
166         case EXPR_BINARY_MOD:                \
167         case EXPR_BINARY_EQUAL:              \
168         case EXPR_BINARY_NOTEQUAL:           \
169         case EXPR_BINARY_LESS:               \
170         case EXPR_BINARY_LESSEQUAL:          \
171         case EXPR_BINARY_GREATER:            \
172         case EXPR_BINARY_GREATEREQUAL:       \
173         case EXPR_BINARY_BITWISE_AND:        \
174         case EXPR_BINARY_BITWISE_OR:         \
175         case EXPR_BINARY_BITWISE_XOR:        \
176         case EXPR_BINARY_LOGICAL_AND:        \
177         case EXPR_BINARY_LOGICAL_OR:         \
178         case EXPR_BINARY_SHIFTLEFT:          \
179         case EXPR_BINARY_SHIFTRIGHT:         \
180         case EXPR_BINARY_ASSIGN:             \
181         case EXPR_BINARY_MUL_ASSIGN:         \
182         case EXPR_BINARY_DIV_ASSIGN:         \
183         case EXPR_BINARY_MOD_ASSIGN:         \
184         case EXPR_BINARY_ADD_ASSIGN:         \
185         case EXPR_BINARY_SUB_ASSIGN:         \
186         case EXPR_BINARY_SHIFTLEFT_ASSIGN:   \
187         case EXPR_BINARY_SHIFTRIGHT_ASSIGN:  \
188         case EXPR_BINARY_BITWISE_AND_ASSIGN: \
189         case EXPR_BINARY_BITWISE_XOR_ASSIGN: \
190         case EXPR_BINARY_BITWISE_OR_ASSIGN:  \
191         case EXPR_BINARY_COMMA:              \
192         case EXPR_BINARY_ISGREATER:          \
193         case EXPR_BINARY_ISGREATEREQUAL:     \
194         case EXPR_BINARY_ISLESS:             \
195         case EXPR_BINARY_ISLESSEQUAL:        \
196         case EXPR_BINARY_ISLESSGREATER:      \
197         case EXPR_BINARY_ISUNORDERED
198
199 /**
200  * unary expression with mandatory operand
201  */
202 #define EXPR_UNARY_CASES_MANDATORY   \
203              EXPR_UNARY_NEGATE:            \
204         case EXPR_UNARY_PLUS:              \
205         case EXPR_UNARY_BITWISE_NEGATE:    \
206         case EXPR_UNARY_NOT:               \
207         case EXPR_UNARY_DEREFERENCE:       \
208         case EXPR_UNARY_TAKE_ADDRESS:      \
209         case EXPR_UNARY_POSTFIX_INCREMENT: \
210         case EXPR_UNARY_POSTFIX_DECREMENT: \
211         case EXPR_UNARY_PREFIX_INCREMENT:  \
212         case EXPR_UNARY_PREFIX_DECREMENT:  \
213         case EXPR_UNARY_CAST:              \
214         case EXPR_UNARY_ASSUME:            \
215         case EXPR_UNARY_DELETE:            \
216         case EXPR_UNARY_DELETE_ARRAY
217
218 /**
219  * unary expression with optional operand
220  */
221 #define EXPR_UNARY_CASES_OPTIONAL \
222         EXPR_UNARY_THROW
223
224 #define EXPR_UNARY_CASES           \
225              EXPR_UNARY_CASES_MANDATORY: \
226         case EXPR_UNARY_CASES_OPTIONAL
227
228 #define EXPR_LITERAL_CASES                     \
229              EXPR_LITERAL_BOOLEAN:                   \
230         case EXPR_LITERAL_INTEGER:                   \
231         case EXPR_LITERAL_FLOATINGPOINT:             \
232         case EXPR_LITERAL_MS_NOOP
233
234 /**
235  * The base class of every expression.
236  */
237 struct expression_base_t {
238         expression_kind_t   kind;            /**< The expression kind. */
239         type_t             *type;            /**< The type of the expression. */
240         source_position_t   source_position; /**< The source position of this expression. */
241         bool                parenthesized : 1;
242 #ifndef NDEBUG
243         bool                transformed : 1;     /**< Set if this expression was transformed. */
244 #endif
245         bool                implicit : 1;    /**< compiler generated expression.
246                                                   Examples:
247                                                      select into anonymous structs
248                                                      implicit casts
249                                               */
250 };
251
252 /**
253  * integer, float and boolean constants
254  */
255 struct literal_expression_t {
256         expression_base_t  base;
257         string_t           value;
258         string_t           suffix;
259
260         /* ast2firm data */
261         ir_tarval         *target_value;
262 };
263
264 /**
265  * string and character literals
266  */
267 struct string_literal_expression_t {
268         expression_base_t  base;
269         string_encoding_t  encoding;
270         string_t           value;
271 };
272
273 struct funcname_expression_t {
274         expression_base_t  base;
275         funcname_kind_t    kind;
276         string_t           value;     /**< the value once assigned. */
277 };
278
279 struct compound_literal_expression_t {
280         expression_base_t  base;
281         type_t            *type;
282         initializer_t     *initializer;
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         source_position_t  source_position;
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_WIDE_STRING,
430         INITIALIZER_DESIGNATOR
431 } initializer_kind_t;
432
433 struct initializer_base_t {
434         initializer_kind_t kind;
435 };
436
437 struct initializer_value_t {
438         initializer_base_t  base;
439         expression_t       *value;
440 };
441
442 struct initializer_list_t {
443         initializer_base_t  base;
444         size_t              len;
445         initializer_t      *initializers[];
446 };
447
448 struct initializer_string_t {
449         initializer_base_t base;
450         string_t           string;
451 };
452
453 struct initializer_wide_string_t {
454         initializer_base_t  base;
455         string_t            string;
456 };
457
458 struct initializer_designator_t {
459         initializer_base_t  base;
460         designator_t       *designator;
461 };
462
463 union initializer_t {
464         initializer_kind_t        kind;
465         initializer_base_t        base;
466         initializer_value_t       value;
467         initializer_list_t        list;
468         initializer_string_t      string;
469         initializer_wide_string_t wide_string;
470         initializer_designator_t  designator;
471 };
472
473 /**
474  * The statement kinds.
475  */
476 typedef enum statement_kind_t {
477         STATEMENT_ERROR = 1,
478         STATEMENT_EMPTY,
479         STATEMENT_COMPOUND,
480         STATEMENT_RETURN,
481         STATEMENT_DECLARATION,
482         STATEMENT_IF,
483         STATEMENT_SWITCH,
484         STATEMENT_EXPRESSION,
485         STATEMENT_CONTINUE,
486         STATEMENT_BREAK,
487         STATEMENT_COMPUTED_GOTO,
488         STATEMENT_GOTO,
489         STATEMENT_LABEL,
490         STATEMENT_CASE_LABEL,
491         STATEMENT_WHILE,
492         STATEMENT_DO_WHILE,
493         STATEMENT_FOR,
494         STATEMENT_ASM,
495         STATEMENT_MS_TRY,          /**< MS __try/__finally or __try/__except */
496         STATEMENT_LEAVE            /**< MS __leave */
497 } statement_kind_t;
498
499 /**
500  * The base class of every statement.
501  */
502 struct statement_base_t {
503         statement_kind_t   kind;
504         statement_t       *next;         /**< Point to the next statement in a compound statement. */
505         source_position_t  source_position;
506         statement_t       *parent;       /**< The Parent statement that controls the execution. */
507         bool               reachable;    /**< True, if this statement is reachable. */
508 #ifndef NDEBUG
509         bool               transformed;
510 #endif
511 };
512
513 struct return_statement_t {
514         statement_base_t  base;
515         expression_t     *value;    /**< The return value if any. */
516 };
517
518 struct compound_statement_t {
519         statement_base_t  base;
520         statement_t      *statements;
521         scope_t           scope;
522         bool              stmt_expr; /**< True if this compound statement is a statement expression. */
523 };
524
525 struct declaration_statement_t {
526         statement_base_t  base;
527         entity_t         *declarations_begin;
528         entity_t         *declarations_end;
529 };
530
531 struct if_statement_t {
532         statement_base_t  base;
533         scope_t           scope;
534         expression_t     *condition;
535         statement_t      *true_statement;
536         statement_t      *false_statement;
537 };
538
539 struct switch_statement_t {
540         statement_base_t        base;
541         scope_t                 scope;
542         expression_t           *expression;
543         statement_t            *body;
544         case_label_statement_t *first_case, *last_case; /**< List of all cases, including default. */
545         case_label_statement_t *default_label;          /**< The default label if existent. */
546 };
547
548 struct goto_statement_t {
549         statement_base_t  base;
550         label_t          *label;         /**< The destination label. */
551         goto_statement_t *next;          /**< links all goto statements of a function */
552 };
553
554 struct computed_goto_statement_t {
555         statement_base_t  base;
556         expression_t     *expression; /**< The expression for the computed goto. */
557 };
558
559 struct case_label_statement_t {
560         statement_base_t        base;
561         expression_t           *expression;    /**< The case label expression, NULL for default label. */
562         expression_t           *end_range;     /**< For GNUC case a .. b: the end range expression, NULL else. */
563         case_label_statement_t *next;          /**< link to the next case label in switch */
564         statement_t            *statement;
565         long                   first_case;     /**< The folded value of expression. */
566         long                   last_case;      /**< The folded value of end_range. */
567         bool                   is_bad;         /**< If set marked as bad to suppress warnings. */
568         bool                   is_empty_range; /**< If set marked this as an empty range. */
569         long                   pn;
570 };
571
572 struct label_statement_t {
573         statement_base_t   base;
574         label_t           *label;
575         statement_t       *statement;
576         label_statement_t *next;    /**< links all label statements of a function */
577 };
578
579 struct expression_statement_t {
580         statement_base_t  base;
581         expression_t     *expression;
582 };
583
584 struct while_statement_t {
585         statement_base_t  base;
586         scope_t           scope;
587         expression_t     *condition;
588         statement_t      *body;
589 };
590
591 struct do_while_statement_t {
592         statement_base_t  base;
593         scope_t           scope;
594         expression_t     *condition;
595         statement_t      *body;
596 };
597
598 struct for_statement_t {
599         statement_base_t  base;
600         scope_t           scope;
601         expression_t     *initialisation;
602         expression_t     *condition;
603         expression_t     *step;
604         statement_t      *body;
605         bool              condition_reachable:1;
606         bool              step_reachable:1;
607 };
608
609 struct asm_argument_t {
610         string_t        constraints;
611         expression_t   *expression;
612         symbol_t       *symbol;
613         asm_argument_t *next;
614 };
615
616 struct asm_clobber_t {
617         string_t       clobber;
618         asm_clobber_t *next;
619 };
620
621 struct asm_statement_t {
622         statement_base_t base;
623         string_t         asm_text;
624         asm_argument_t  *inputs;
625         asm_argument_t  *outputs;
626         asm_clobber_t   *clobbers;
627         bool             is_volatile;
628 };
629
630 struct ms_try_statement_t {
631         statement_base_t  base;
632         statement_t      *try_statement;
633         expression_t     *except_expression; /**< non-null for except, NULL for finally */
634         statement_t      *final_statement;
635 };
636
637 struct leave_statement_t {
638         statement_base_t  base;
639 };
640
641 union statement_t {
642         statement_kind_t          kind;
643         statement_base_t          base;
644         return_statement_t        returns;
645         compound_statement_t      compound;
646         declaration_statement_t   declaration;
647         if_statement_t            ifs;
648         switch_statement_t        switchs;
649         computed_goto_statement_t computed_goto;
650         goto_statement_t          gotos;
651         case_label_statement_t    case_label;
652         label_statement_t         label;
653         expression_statement_t    expression;
654         while_statement_t         whiles;
655         do_while_statement_t      do_while;
656         for_statement_t           fors;
657         asm_statement_t           asms;
658         ms_try_statement_t        ms_try;
659         leave_statement_t         leave;
660 };
661
662 struct translation_unit_t {
663         scope_t      scope;
664         statement_t *global_asm;
665 };
666
667 /**
668  * Allocate an AST node with given size and
669  * initialize all fields with zero.
670  */
671 static inline void *allocate_ast_zero(size_t size)
672 {
673         return memset(obstack_alloc(&ast_obstack, size), 0, size);
674 }
675
676 /** If set, implicit casts are printed. */
677 extern bool print_implicit_casts;
678 /** If set parenthesis are printed to indicate operator precedence. */
679 extern bool print_parenthesis;
680
681 #endif