Record the encoding in string_literal_expression_t and merge EXPR_WIDE_STRING_LITERAL...
[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_CHARACTER:                 \
233         case EXPR_LITERAL_WIDE_CHARACTER:            \
234         case EXPR_LITERAL_MS_NOOP
235
236 /**
237  * The base class of every expression.
238  */
239 struct expression_base_t {
240         expression_kind_t   kind;            /**< The expression kind. */
241         type_t             *type;            /**< The type of the expression. */
242         source_position_t   source_position; /**< The source position of this expression. */
243         bool                parenthesized : 1;
244 #ifndef NDEBUG
245         bool                transformed : 1;     /**< Set if this expression was transformed. */
246 #endif
247         bool                implicit : 1;    /**< compiler generated expression.
248                                                   Examples:
249                                                      select into anonymous structs
250                                                      implicit casts
251                                               */
252 };
253
254 /**
255  * integer/float constants, character and string literals
256  */
257 struct literal_expression_t {
258         expression_base_t  base;
259         string_t           value;
260         string_t           suffix;
261
262         /* ast2firm data */
263         ir_tarval         *target_value;
264 };
265
266 struct string_literal_expression_t {
267         expression_base_t  base;
268         string_encoding_t  encoding;
269         string_t           value;
270 };
271
272 struct funcname_expression_t {
273         expression_base_t  base;
274         funcname_kind_t    kind;
275         string_t           value;     /**< the value once assigned. */
276 };
277
278 struct compound_literal_expression_t {
279         expression_base_t  base;
280         type_t            *type;
281         initializer_t     *initializer;
282 };
283
284 struct builtin_constant_expression_t {
285         expression_base_t  base;
286         expression_t      *value;
287 };
288
289 struct builtin_types_compatible_expression_t {
290         expression_base_t  base;
291         type_t            *left;
292         type_t            *right;
293 };
294
295 struct reference_expression_t {
296         expression_base_t  base;
297         entity_t          *entity;
298 };
299
300 /**
301  * An argument of a call.
302  */
303 struct call_argument_t {
304         expression_t    *expression;  /**< The expression which value is transmitted. */
305         call_argument_t *next;        /**< Links to the next argument of this call. */
306 };
307
308
309 struct call_expression_t {
310         expression_base_t  base;
311         expression_t      *function;  /**< The address of the function to call. */
312         call_argument_t   *arguments; /**< List of arguments of this call. */
313 };
314
315
316 struct unary_expression_t {
317         expression_base_t  base;
318         expression_t      *value;     /**< The unary operand. */
319 };
320
321 struct binary_expression_t {
322         expression_base_t  base;
323         expression_t      *left;
324         expression_t      *right;
325 };
326
327 struct select_expression_t {
328         expression_base_t  base;
329         expression_t      *compound;
330         entity_t          *compound_entry;
331 };
332
333 struct array_access_expression_t {
334         expression_base_t  base;
335         expression_t      *array_ref; /**< the referenced array */
336         expression_t      *index;     /**< the index used */
337         bool               flipped;   /**< True if index/ref was written in a 5[a] way */
338 };
339
340 struct typeprop_expression_t {
341         expression_base_t  base;
342         type_t            *type;
343         expression_t      *tp_expression;
344 };
345
346 struct designator_t {
347         source_position_t  source_position;
348         symbol_t          *symbol;      /**< the symbol if any */
349         expression_t      *array_index; /**< the array index if any */
350         designator_t      *next;
351 };
352
353 struct offsetof_expression_t {
354         expression_base_t  base;
355         type_t            *type;
356         designator_t      *designator;
357 };
358
359 struct va_start_expression_t {
360         expression_base_t  base;
361         expression_t      *ap;
362         expression_t      *parameter;
363 };
364
365 struct va_arg_expression_t {
366         expression_base_t  base;
367         expression_t      *ap;
368 };
369
370 struct va_copy_expression_t {
371         expression_base_t  base;
372         expression_t      *dst;    /**< destination argument */
373         expression_t      *src;    /**< source argument */
374 };
375
376 struct conditional_expression_t {
377         expression_base_t  base;
378         expression_t      *condition;
379         expression_t      *true_expression;
380         expression_t      *false_expression;
381 };
382
383 struct statement_expression_t {
384         expression_base_t  base;
385         statement_t       *statement;
386 };
387
388 struct classify_type_expression_t {
389         expression_base_t  base;
390         expression_t      *type_expression;
391 };
392
393 struct label_address_expression_t {
394         expression_base_t  base;
395         label_t           *label;
396 };
397
398 union expression_t {
399         expression_kind_t                     kind;
400         expression_base_t                     base;
401         literal_expression_t                  literal;
402         string_literal_expression_t           string_literal;
403         funcname_expression_t                 funcname;
404         compound_literal_expression_t         compound_literal;
405         builtin_constant_expression_t         builtin_constant;
406         builtin_types_compatible_expression_t builtin_types_compatible;
407         reference_expression_t                reference;
408         call_expression_t                     call;
409         unary_expression_t                    unary;
410         binary_expression_t                   binary;
411         select_expression_t                   select;
412         array_access_expression_t             array_access;
413         typeprop_expression_t                 typeprop;
414         offsetof_expression_t                 offsetofe;
415         va_start_expression_t                 va_starte;
416         va_arg_expression_t                   va_arge;
417         va_copy_expression_t                  va_copye;
418         conditional_expression_t              conditional;
419         statement_expression_t                statement;
420         classify_type_expression_t            classify_type;
421         label_address_expression_t            label_address;
422 };
423
424 typedef enum initializer_kind_t {
425         INITIALIZER_VALUE,
426         INITIALIZER_LIST,
427         INITIALIZER_STRING,
428         INITIALIZER_WIDE_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_string_t {
448         initializer_base_t base;
449         string_t           string;
450 };
451
452 struct initializer_wide_string_t {
453         initializer_base_t  base;
454         string_t            string;
455 };
456
457 struct initializer_designator_t {
458         initializer_base_t  base;
459         designator_t       *designator;
460 };
461
462 union initializer_t {
463         initializer_kind_t        kind;
464         initializer_base_t        base;
465         initializer_value_t       value;
466         initializer_list_t        list;
467         initializer_string_t      string;
468         initializer_wide_string_t wide_string;
469         initializer_designator_t  designator;
470 };
471
472 /**
473  * The statement kinds.
474  */
475 typedef enum statement_kind_t {
476         STATEMENT_ERROR = 1,
477         STATEMENT_EMPTY,
478         STATEMENT_COMPOUND,
479         STATEMENT_RETURN,
480         STATEMENT_DECLARATION,
481         STATEMENT_IF,
482         STATEMENT_SWITCH,
483         STATEMENT_EXPRESSION,
484         STATEMENT_CONTINUE,
485         STATEMENT_BREAK,
486         STATEMENT_COMPUTED_GOTO,
487         STATEMENT_GOTO,
488         STATEMENT_LABEL,
489         STATEMENT_CASE_LABEL,
490         STATEMENT_WHILE,
491         STATEMENT_DO_WHILE,
492         STATEMENT_FOR,
493         STATEMENT_ASM,
494         STATEMENT_MS_TRY,          /**< MS __try/__finally or __try/__except */
495         STATEMENT_LEAVE            /**< MS __leave */
496 } statement_kind_t;
497
498 /**
499  * The base class of every statement.
500  */
501 struct statement_base_t {
502         statement_kind_t   kind;
503         statement_t       *next;         /**< Point to the next statement in a compound statement. */
504         source_position_t  source_position;
505         statement_t       *parent;       /**< The Parent statement that controls the execution. */
506         bool               reachable;    /**< True, if this statement is reachable. */
507 #ifndef NDEBUG
508         bool               transformed;
509 #endif
510 };
511
512 struct return_statement_t {
513         statement_base_t  base;
514         expression_t     *value;    /**< The return value if any. */
515 };
516
517 struct compound_statement_t {
518         statement_base_t  base;
519         statement_t      *statements;
520         scope_t           scope;
521         bool              stmt_expr; /**< True if this compound statement is a statement expression. */
522 };
523
524 struct declaration_statement_t {
525         statement_base_t  base;
526         entity_t         *declarations_begin;
527         entity_t         *declarations_end;
528 };
529
530 struct if_statement_t {
531         statement_base_t  base;
532         scope_t           scope;
533         expression_t     *condition;
534         statement_t      *true_statement;
535         statement_t      *false_statement;
536 };
537
538 struct switch_statement_t {
539         statement_base_t        base;
540         scope_t                 scope;
541         expression_t           *expression;
542         statement_t            *body;
543         case_label_statement_t *first_case, *last_case; /**< List of all cases, including default. */
544         case_label_statement_t *default_label;          /**< The default label if existent. */
545 };
546
547 struct goto_statement_t {
548         statement_base_t  base;
549         label_t          *label;         /**< The destination label. */
550         goto_statement_t *next;          /**< links all goto statements of a function */
551 };
552
553 struct computed_goto_statement_t {
554         statement_base_t  base;
555         expression_t     *expression; /**< The expression for the computed goto. */
556 };
557
558 struct case_label_statement_t {
559         statement_base_t        base;
560         expression_t           *expression;    /**< The case label expression, NULL for default label. */
561         expression_t           *end_range;     /**< For GNUC case a .. b: the end range expression, NULL else. */
562         case_label_statement_t *next;          /**< link to the next case label in switch */
563         statement_t            *statement;
564         long                   first_case;     /**< The folded value of expression. */
565         long                   last_case;      /**< The folded value of end_range. */
566         bool                   is_bad;         /**< If set marked as bad to suppress warnings. */
567         bool                   is_empty_range; /**< If set marked this as an empty range. */
568         long                   pn;
569 };
570
571 struct label_statement_t {
572         statement_base_t   base;
573         label_t           *label;
574         statement_t       *statement;
575         label_statement_t *next;    /**< links all label statements of a function */
576 };
577
578 struct expression_statement_t {
579         statement_base_t  base;
580         expression_t     *expression;
581 };
582
583 struct while_statement_t {
584         statement_base_t  base;
585         scope_t           scope;
586         expression_t     *condition;
587         statement_t      *body;
588 };
589
590 struct do_while_statement_t {
591         statement_base_t  base;
592         scope_t           scope;
593         expression_t     *condition;
594         statement_t      *body;
595 };
596
597 struct for_statement_t {
598         statement_base_t  base;
599         scope_t           scope;
600         expression_t     *initialisation;
601         expression_t     *condition;
602         expression_t     *step;
603         statement_t      *body;
604         bool              condition_reachable:1;
605         bool              step_reachable:1;
606 };
607
608 struct asm_argument_t {
609         string_t        constraints;
610         expression_t   *expression;
611         symbol_t       *symbol;
612         asm_argument_t *next;
613 };
614
615 struct asm_clobber_t {
616         string_t       clobber;
617         asm_clobber_t *next;
618 };
619
620 struct asm_statement_t {
621         statement_base_t base;
622         string_t         asm_text;
623         asm_argument_t  *inputs;
624         asm_argument_t  *outputs;
625         asm_clobber_t   *clobbers;
626         bool             is_volatile;
627 };
628
629 struct ms_try_statement_t {
630         statement_base_t  base;
631         statement_t      *try_statement;
632         expression_t     *except_expression; /**< non-null for except, NULL for finally */
633         statement_t      *final_statement;
634 };
635
636 struct leave_statement_t {
637         statement_base_t  base;
638 };
639
640 union statement_t {
641         statement_kind_t          kind;
642         statement_base_t          base;
643         return_statement_t        returns;
644         compound_statement_t      compound;
645         declaration_statement_t   declaration;
646         if_statement_t            ifs;
647         switch_statement_t        switchs;
648         computed_goto_statement_t computed_goto;
649         goto_statement_t          gotos;
650         case_label_statement_t    case_label;
651         label_statement_t         label;
652         expression_statement_t    expression;
653         while_statement_t         whiles;
654         do_while_statement_t      do_while;
655         for_statement_t           fors;
656         asm_statement_t           asms;
657         ms_try_statement_t        ms_try;
658         leave_statement_t         leave;
659 };
660
661 struct translation_unit_t {
662         scope_t      scope;
663         statement_t *global_asm;
664 };
665
666 /**
667  * Allocate an AST node with given size and
668  * initialize all fields with zero.
669  */
670 static inline void *allocate_ast_zero(size_t size)
671 {
672         return memset(obstack_alloc(&ast_obstack, size), 0, size);
673 }
674
675 /** If set, implicit casts are printed. */
676 extern bool print_implicit_casts;
677 /** If set parenthesis are printed to indicate operator precedence. */
678 extern bool print_parenthesis;
679
680 #endif