2 * This file is part of cparser.
3 * Copyright (C) 2007-2009 Matthias Braun <matze@braunis.de>
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.
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.
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
23 #include <libfirm/firm_types.h>
32 #include "target_value.h"
34 /** The AST obstack contains all data that must stay in the AST. */
35 extern struct obstack ast_obstack;
38 * Operator precedence classes
40 typedef enum precedence_t {
42 PREC_EXPRESSION, /* , left to right */
43 PREC_ASSIGNMENT, /* = += -= *= /= %= <<= >>= &= ^= |= right to left */
44 PREC_CONDITIONAL, /* ?: right to left */
45 PREC_LOGICAL_OR, /* || left to right */
46 PREC_LOGICAL_AND, /* && left to right */
47 PREC_OR, /* | left to right */
48 PREC_XOR, /* ^ left to right */
49 PREC_AND, /* & left to right */
50 PREC_EQUALITY, /* == != left to right */
51 PREC_RELATIONAL, /* < <= > >= left to right */
52 PREC_SHIFT, /* << >> left to right */
53 PREC_ADDITIVE, /* + - left to right */
54 PREC_MULTIPLICATIVE, /* * / % left to right */
55 PREC_CAST, /* (type) right to left */
56 PREC_UNARY, /* ! ~ ++ -- + - * & sizeof right to left */
57 PREC_POSTFIX, /* () [] -> . left to right */
65 typedef enum expression_kind_t {
69 EXPR_REFERENCE_ENUM_VALUE,
72 EXPR_LITERAL_INTEGER_OCTAL,
73 EXPR_LITERAL_INTEGER_HEXADECIMAL,
74 EXPR_LITERAL_FLOATINGPOINT,
75 EXPR_LITERAL_FLOATINGPOINT_HEXADECIMAL,
76 EXPR_LITERAL_CHARACTER,
77 EXPR_LITERAL_WIDE_CHARACTER,
78 EXPR_LITERAL_MS_NOOP, /**< MS __noop extension */
80 EXPR_WIDE_STRING_LITERAL,
81 EXPR_COMPOUND_LITERAL,
91 EXPR_BUILTIN_CONSTANT_P,
92 EXPR_BUILTIN_TYPES_COMPATIBLE_P,
98 EXPR_LABEL_ADDRESS, /**< GCC extension &&label operator */
101 EXPR_UNARY_NEGATE = EXPR_UNARY_FIRST,
103 EXPR_UNARY_BITWISE_NEGATE,
105 EXPR_UNARY_DEREFERENCE,
106 EXPR_UNARY_TAKE_ADDRESS,
107 EXPR_UNARY_POSTFIX_INCREMENT,
108 EXPR_UNARY_POSTFIX_DECREMENT,
109 EXPR_UNARY_PREFIX_INCREMENT,
110 EXPR_UNARY_PREFIX_DECREMENT,
112 EXPR_UNARY_CAST_IMPLICIT, /**< compiler generated cast */
113 EXPR_UNARY_ASSUME, /**< MS __assume() */
115 EXPR_UNARY_DELETE_ARRAY,
117 EXPR_UNARY_LAST = EXPR_UNARY_THROW,
120 EXPR_BINARY_ADD = EXPR_BINARY_FIRST,
126 EXPR_BINARY_NOTEQUAL,
128 EXPR_BINARY_LESSEQUAL,
130 EXPR_BINARY_GREATEREQUAL,
131 EXPR_BINARY_BITWISE_AND,
132 EXPR_BINARY_BITWISE_OR,
133 EXPR_BINARY_BITWISE_XOR,
134 EXPR_BINARY_LOGICAL_AND,
135 EXPR_BINARY_LOGICAL_OR,
136 EXPR_BINARY_SHIFTLEFT,
137 EXPR_BINARY_SHIFTRIGHT,
139 EXPR_BINARY_MUL_ASSIGN,
140 EXPR_BINARY_DIV_ASSIGN,
141 EXPR_BINARY_MOD_ASSIGN,
142 EXPR_BINARY_ADD_ASSIGN,
143 EXPR_BINARY_SUB_ASSIGN,
144 EXPR_BINARY_SHIFTLEFT_ASSIGN,
145 EXPR_BINARY_SHIFTRIGHT_ASSIGN,
146 EXPR_BINARY_BITWISE_AND_ASSIGN,
147 EXPR_BINARY_BITWISE_XOR_ASSIGN,
148 EXPR_BINARY_BITWISE_OR_ASSIGN,
151 EXPR_BINARY_ISGREATER,
152 EXPR_BINARY_ISGREATEREQUAL,
154 EXPR_BINARY_ISLESSEQUAL,
155 EXPR_BINARY_ISLESSGREATER,
156 EXPR_BINARY_ISUNORDERED,
157 EXPR_BINARY_LAST = EXPR_BINARY_ISUNORDERED,
160 typedef enum funcname_kind_t {
161 FUNCNAME_FUNCTION, /**< C99 __func__, older __FUNCTION__ */
162 FUNCNAME_PRETTY_FUNCTION, /**< GNUC __PRETTY_FUNCTION__ */
163 FUNCNAME_FUNCSIG, /**< MS __FUNCSIG__ */
164 FUNCNAME_FUNCDNAME /**< MS __FUNCDNAME__ */
167 /* convenience macros */
168 #define EXPR_BINARY_CASES \
169 case EXPR_BINARY_ADD: \
170 case EXPR_BINARY_SUB: \
171 case EXPR_BINARY_MUL: \
172 case EXPR_BINARY_DIV: \
173 case EXPR_BINARY_MOD: \
174 case EXPR_BINARY_EQUAL: \
175 case EXPR_BINARY_NOTEQUAL: \
176 case EXPR_BINARY_LESS: \
177 case EXPR_BINARY_LESSEQUAL: \
178 case EXPR_BINARY_GREATER: \
179 case EXPR_BINARY_GREATEREQUAL: \
180 case EXPR_BINARY_BITWISE_AND: \
181 case EXPR_BINARY_BITWISE_OR: \
182 case EXPR_BINARY_BITWISE_XOR: \
183 case EXPR_BINARY_LOGICAL_AND: \
184 case EXPR_BINARY_LOGICAL_OR: \
185 case EXPR_BINARY_SHIFTLEFT: \
186 case EXPR_BINARY_SHIFTRIGHT: \
187 case EXPR_BINARY_ASSIGN: \
188 case EXPR_BINARY_MUL_ASSIGN: \
189 case EXPR_BINARY_DIV_ASSIGN: \
190 case EXPR_BINARY_MOD_ASSIGN: \
191 case EXPR_BINARY_ADD_ASSIGN: \
192 case EXPR_BINARY_SUB_ASSIGN: \
193 case EXPR_BINARY_SHIFTLEFT_ASSIGN: \
194 case EXPR_BINARY_SHIFTRIGHT_ASSIGN: \
195 case EXPR_BINARY_BITWISE_AND_ASSIGN: \
196 case EXPR_BINARY_BITWISE_XOR_ASSIGN: \
197 case EXPR_BINARY_BITWISE_OR_ASSIGN: \
198 case EXPR_BINARY_COMMA: \
199 case EXPR_BINARY_ISGREATER: \
200 case EXPR_BINARY_ISGREATEREQUAL: \
201 case EXPR_BINARY_ISLESS: \
202 case EXPR_BINARY_ISLESSEQUAL: \
203 case EXPR_BINARY_ISLESSGREATER: \
204 case EXPR_BINARY_ISUNORDERED:
207 * unary expression with mandatory operand
209 #define EXPR_UNARY_CASES_MANDATORY \
210 case EXPR_UNARY_NEGATE: \
211 case EXPR_UNARY_PLUS: \
212 case EXPR_UNARY_BITWISE_NEGATE: \
213 case EXPR_UNARY_NOT: \
214 case EXPR_UNARY_DEREFERENCE: \
215 case EXPR_UNARY_TAKE_ADDRESS: \
216 case EXPR_UNARY_POSTFIX_INCREMENT: \
217 case EXPR_UNARY_POSTFIX_DECREMENT: \
218 case EXPR_UNARY_PREFIX_INCREMENT: \
219 case EXPR_UNARY_PREFIX_DECREMENT: \
220 case EXPR_UNARY_CAST: \
221 case EXPR_UNARY_CAST_IMPLICIT: \
222 case EXPR_UNARY_ASSUME: \
223 case EXPR_UNARY_DELETE: \
224 case EXPR_UNARY_DELETE_ARRAY:
227 * unary expression with optional operand
229 #define EXPR_UNARY_CASES_OPTIONAL \
230 case EXPR_UNARY_THROW: \
232 #define EXPR_UNARY_CASES \
233 EXPR_UNARY_CASES_MANDATORY \
234 EXPR_UNARY_CASES_OPTIONAL
236 #define EXPR_LITERAL_CASES \
237 case EXPR_LITERAL_BOOLEAN: \
238 case EXPR_LITERAL_INTEGER: \
239 case EXPR_LITERAL_INTEGER_OCTAL: \
240 case EXPR_LITERAL_INTEGER_HEXADECIMAL: \
241 case EXPR_LITERAL_FLOATINGPOINT: \
242 case EXPR_LITERAL_FLOATINGPOINT_HEXADECIMAL: \
243 case EXPR_LITERAL_CHARACTER: \
244 case EXPR_LITERAL_WIDE_CHARACTER: \
245 case EXPR_LITERAL_MS_NOOP:
248 * The base class of every expression.
250 struct expression_base_t {
251 expression_kind_t kind; /**< The expression kind. */
252 type_t *type; /**< The type of the expression. */
253 source_position_t source_position; /**< The source position of this expression. */
254 bool parenthesized : 1;
256 bool transformed : 1; /**< Set if this expression was transformed. */
261 * integer/float constants, character and string literals
263 struct literal_expression_t {
264 expression_base_t base;
269 tarval *target_value;
272 struct string_literal_expression_t {
273 expression_base_t base;
277 struct funcname_expression_t {
278 expression_base_t base;
279 funcname_kind_t kind;
280 string_t value; /**< the value once assigned. */
283 struct compound_literal_expression_t {
284 expression_base_t base;
286 initializer_t *initializer;
289 struct builtin_constant_expression_t {
290 expression_base_t base;
294 struct builtin_types_compatible_expression_t {
295 expression_base_t base;
300 struct reference_expression_t {
301 expression_base_t base;
306 * An argument of a call.
308 struct call_argument_t {
309 expression_t *expression; /**< The expression which value is transmitted. */
310 call_argument_t *next; /**< Links to the next argument of this call. */
314 struct call_expression_t {
315 expression_base_t base;
316 expression_t *function; /**< The address of the function to call. */
317 call_argument_t *arguments; /**< List of arguments of this call. */
321 struct unary_expression_t {
322 expression_base_t base;
323 expression_t *value; /**< The unary operand. */
326 struct binary_expression_t {
327 expression_base_t base;
332 struct select_expression_t {
333 expression_base_t base;
334 expression_t *compound;
335 entity_t *compound_entry;
336 bool implicit : 1; /**< compiler generated select
337 (for anonymous struct/union) */
340 struct array_access_expression_t {
341 expression_base_t base;
342 expression_t *array_ref; /**< the referenced array */
343 expression_t *index; /**< the index used */
344 bool flipped; /**< True if index/ref was written in a 5[a] way */
347 struct typeprop_expression_t {
348 expression_base_t base;
350 expression_t *tp_expression;
353 struct designator_t {
354 source_position_t source_position;
355 symbol_t *symbol; /**< the symbol if any */
356 expression_t *array_index; /**< the array index if any */
360 struct offsetof_expression_t {
361 expression_base_t base;
363 designator_t *designator;
366 struct va_start_expression_t {
367 expression_base_t base;
369 variable_t *parameter;
372 struct va_arg_expression_t {
373 expression_base_t base;
377 struct va_copy_expression_t {
378 expression_base_t base;
379 expression_t *dst; /**< destination argument */
380 expression_t *src; /**< source argument */
383 struct conditional_expression_t {
384 expression_base_t base;
385 expression_t *condition;
386 expression_t *true_expression;
387 expression_t *false_expression;
390 struct statement_expression_t {
391 expression_base_t base;
392 statement_t *statement;
395 struct classify_type_expression_t {
396 expression_base_t base;
397 expression_t *type_expression;
400 struct label_address_expression_t {
401 expression_base_t base;
406 expression_kind_t kind;
407 expression_base_t base;
408 literal_expression_t literal;
409 string_literal_expression_t string_literal;
410 funcname_expression_t funcname;
411 compound_literal_expression_t compound_literal;
412 builtin_constant_expression_t builtin_constant;
413 builtin_types_compatible_expression_t builtin_types_compatible;
414 reference_expression_t reference;
415 call_expression_t call;
416 unary_expression_t unary;
417 binary_expression_t binary;
418 select_expression_t select;
419 array_access_expression_t array_access;
420 typeprop_expression_t typeprop;
421 offsetof_expression_t offsetofe;
422 va_start_expression_t va_starte;
423 va_arg_expression_t va_arge;
424 va_copy_expression_t va_copye;
425 conditional_expression_t conditional;
426 statement_expression_t statement;
427 classify_type_expression_t classify_type;
428 label_address_expression_t label_address;
431 typedef enum initializer_kind_t {
435 INITIALIZER_WIDE_STRING,
436 INITIALIZER_DESIGNATOR
437 } initializer_kind_t;
439 struct initializer_base_t {
440 initializer_kind_t kind;
443 struct initializer_value_t {
444 initializer_base_t base;
448 struct initializer_list_t {
449 initializer_base_t base;
451 initializer_t *initializers[];
454 struct initializer_string_t {
455 initializer_base_t base;
459 struct initializer_wide_string_t {
460 initializer_base_t base;
464 struct initializer_designator_t {
465 initializer_base_t base;
466 designator_t *designator;
469 union initializer_t {
470 initializer_kind_t kind;
471 initializer_base_t base;
472 initializer_value_t value;
473 initializer_list_t list;
474 initializer_string_t string;
475 initializer_wide_string_t wide_string;
476 initializer_designator_t designator;
480 * The statement kinds.
482 typedef enum statement_kind_t {
487 STATEMENT_DECLARATION,
490 STATEMENT_EXPRESSION,
495 STATEMENT_CASE_LABEL,
500 STATEMENT_MS_TRY, /**< MS __try/__finally or __try/__except */
501 STATEMENT_LEAVE /**< MS __leave */
505 * The base class of every statement.
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. */
518 struct invalid_statement_t {
519 statement_base_t base;
522 struct empty_statement_t {
523 statement_base_t base;
526 struct return_statement_t {
527 statement_base_t base;
528 expression_t *value; /**< The return value if any. */
531 struct compound_statement_t {
532 statement_base_t base;
533 statement_t *statements;
535 bool stmt_expr; /**< True if this compound statement is a statement expression. */
538 struct declaration_statement_t {
539 statement_base_t base;
540 entity_t *declarations_begin;
541 entity_t *declarations_end;
544 struct if_statement_t {
545 statement_base_t base;
546 expression_t *condition;
547 statement_t *true_statement;
548 statement_t *false_statement;
551 struct switch_statement_t {
552 statement_base_t base;
553 expression_t *expression;
555 case_label_statement_t *first_case, *last_case; /**< List of all cases, including default. */
556 case_label_statement_t *default_label; /**< The default label if existent. */
557 unsigned long default_proj_nr; /**< The Proj-number for the default Proj. */
560 struct goto_statement_t {
561 statement_base_t base;
562 label_t *label; /**< The destination label. */
563 expression_t *expression; /**< The expression for an assigned goto. */
564 goto_statement_t *next; /**< links all goto statements of a function */
567 struct case_label_statement_t {
568 statement_base_t base;
569 expression_t *expression; /**< The case label expression, NULL for default label. */
570 expression_t *end_range; /**< For GNUC case a .. b: the end range expression, NULL else. */
571 case_label_statement_t *next; /**< link to the next case label in switch */
572 statement_t *statement;
573 long first_case; /**< The folded value of expression. */
574 long last_case; /**< The folded value of end_range. */
575 bool is_bad; /**< If set marked as bad to suppress warnings. */
576 bool is_empty_range; /**< If set marked this as an empty range. */
579 struct label_statement_t {
580 statement_base_t base;
582 statement_t *statement;
583 label_statement_t *next; /**< links all label statements of a function */
586 struct expression_statement_t {
587 statement_base_t base;
588 expression_t *expression;
591 struct while_statement_t {
592 statement_base_t base;
593 expression_t *condition;
597 struct do_while_statement_t {
598 statement_base_t base;
599 expression_t *condition;
603 struct for_statement_t {
604 statement_base_t base;
605 expression_t *initialisation;
606 expression_t *condition;
610 bool condition_reachable:1;
611 bool step_reachable:1;
614 struct asm_argument_t {
615 string_t constraints;
616 expression_t *expression;
618 asm_argument_t *next;
621 struct asm_clobber_t {
626 struct asm_statement_t {
627 statement_base_t base;
629 asm_argument_t *inputs;
630 asm_argument_t *outputs;
631 asm_clobber_t *clobbers;
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;
642 struct leave_statement_t {
643 statement_base_t base;
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;
653 switch_statement_t switchs;
654 goto_statement_t gotos;
655 case_label_statement_t case_label;
656 label_statement_t label;
657 expression_statement_t expression;
658 while_statement_t whiles;
659 do_while_statement_t do_while;
660 for_statement_t fors;
661 asm_statement_t asms;
662 ms_try_statement_t ms_try;
663 leave_statement_t leave;
666 struct translation_unit_t {
668 statement_t *global_asm;
671 static inline void *_allocate_ast(size_t size)
673 return obstack_alloc(&ast_obstack, size);
676 static inline bool is_invalid_expression(expression_t *expression)
678 return expression->base.kind == EXPR_INVALID;
681 static inline bool is_invalid_statement(statement_t *statement)
683 return statement->base.kind == STATEMENT_INVALID;
686 #define allocate_ast(size) _allocate_ast(size)
689 * Allocate an AST node with given size and
690 * initialize all fields with zero.
692 static inline void *allocate_ast_zero(size_t size)
694 void *res = allocate_ast(size);
695 memset(res, 0, size);