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>
33 /** The AST obstack contains all data that must stay in the AST. */
34 extern struct obstack ast_obstack;
37 * Operator precedence classes
39 typedef enum precedence_t {
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 */
64 typedef enum expression_kind_t {
68 EXPR_REFERENCE_ENUM_VALUE,
71 EXPR_LITERAL_INTEGER_OCTAL,
72 EXPR_LITERAL_INTEGER_HEXADECIMAL,
73 EXPR_LITERAL_FLOATINGPOINT,
74 EXPR_LITERAL_FLOATINGPOINT_HEXADECIMAL,
75 EXPR_LITERAL_CHARACTER,
76 EXPR_LITERAL_WIDE_CHARACTER,
77 EXPR_LITERAL_MS_NOOP, /**< MS __noop extension */
79 EXPR_WIDE_STRING_LITERAL,
80 EXPR_COMPOUND_LITERAL,
90 EXPR_BUILTIN_CONSTANT_P,
91 EXPR_BUILTIN_TYPES_COMPATIBLE_P,
97 EXPR_LABEL_ADDRESS, /**< GCC extension &&label operator */
100 EXPR_UNARY_NEGATE = EXPR_UNARY_FIRST,
102 EXPR_UNARY_BITWISE_NEGATE,
104 EXPR_UNARY_DEREFERENCE,
105 EXPR_UNARY_TAKE_ADDRESS,
106 EXPR_UNARY_POSTFIX_INCREMENT,
107 EXPR_UNARY_POSTFIX_DECREMENT,
108 EXPR_UNARY_PREFIX_INCREMENT,
109 EXPR_UNARY_PREFIX_DECREMENT,
111 EXPR_UNARY_ASSUME, /**< MS __assume() */
113 EXPR_UNARY_DELETE_ARRAY,
115 EXPR_UNARY_LAST = EXPR_UNARY_THROW,
118 EXPR_BINARY_ADD = EXPR_BINARY_FIRST,
124 EXPR_BINARY_NOTEQUAL,
126 EXPR_BINARY_LESSEQUAL,
128 EXPR_BINARY_GREATEREQUAL,
129 EXPR_BINARY_BITWISE_AND,
130 EXPR_BINARY_BITWISE_OR,
131 EXPR_BINARY_BITWISE_XOR,
132 EXPR_BINARY_LOGICAL_AND,
133 EXPR_BINARY_LOGICAL_OR,
134 EXPR_BINARY_SHIFTLEFT,
135 EXPR_BINARY_SHIFTRIGHT,
137 EXPR_BINARY_MUL_ASSIGN,
138 EXPR_BINARY_DIV_ASSIGN,
139 EXPR_BINARY_MOD_ASSIGN,
140 EXPR_BINARY_ADD_ASSIGN,
141 EXPR_BINARY_SUB_ASSIGN,
142 EXPR_BINARY_SHIFTLEFT_ASSIGN,
143 EXPR_BINARY_SHIFTRIGHT_ASSIGN,
144 EXPR_BINARY_BITWISE_AND_ASSIGN,
145 EXPR_BINARY_BITWISE_XOR_ASSIGN,
146 EXPR_BINARY_BITWISE_OR_ASSIGN,
149 EXPR_BINARY_ISGREATER,
150 EXPR_BINARY_ISGREATEREQUAL,
152 EXPR_BINARY_ISLESSEQUAL,
153 EXPR_BINARY_ISLESSGREATER,
154 EXPR_BINARY_ISUNORDERED,
155 EXPR_BINARY_LAST = EXPR_BINARY_ISUNORDERED,
158 typedef enum funcname_kind_t {
159 FUNCNAME_FUNCTION, /**< C99 __func__, older __FUNCTION__ */
160 FUNCNAME_PRETTY_FUNCTION, /**< GNUC __PRETTY_FUNCTION__ */
161 FUNCNAME_FUNCSIG, /**< MS __FUNCSIG__ */
162 FUNCNAME_FUNCDNAME /**< MS __FUNCDNAME__ */
165 /* convenience macros */
166 #define EXPR_BINARY_CASES \
167 case EXPR_BINARY_ADD: \
168 case EXPR_BINARY_SUB: \
169 case EXPR_BINARY_MUL: \
170 case EXPR_BINARY_DIV: \
171 case EXPR_BINARY_MOD: \
172 case EXPR_BINARY_EQUAL: \
173 case EXPR_BINARY_NOTEQUAL: \
174 case EXPR_BINARY_LESS: \
175 case EXPR_BINARY_LESSEQUAL: \
176 case EXPR_BINARY_GREATER: \
177 case EXPR_BINARY_GREATEREQUAL: \
178 case EXPR_BINARY_BITWISE_AND: \
179 case EXPR_BINARY_BITWISE_OR: \
180 case EXPR_BINARY_BITWISE_XOR: \
181 case EXPR_BINARY_LOGICAL_AND: \
182 case EXPR_BINARY_LOGICAL_OR: \
183 case EXPR_BINARY_SHIFTLEFT: \
184 case EXPR_BINARY_SHIFTRIGHT: \
185 case EXPR_BINARY_ASSIGN: \
186 case EXPR_BINARY_MUL_ASSIGN: \
187 case EXPR_BINARY_DIV_ASSIGN: \
188 case EXPR_BINARY_MOD_ASSIGN: \
189 case EXPR_BINARY_ADD_ASSIGN: \
190 case EXPR_BINARY_SUB_ASSIGN: \
191 case EXPR_BINARY_SHIFTLEFT_ASSIGN: \
192 case EXPR_BINARY_SHIFTRIGHT_ASSIGN: \
193 case EXPR_BINARY_BITWISE_AND_ASSIGN: \
194 case EXPR_BINARY_BITWISE_XOR_ASSIGN: \
195 case EXPR_BINARY_BITWISE_OR_ASSIGN: \
196 case EXPR_BINARY_COMMA: \
197 case EXPR_BINARY_ISGREATER: \
198 case EXPR_BINARY_ISGREATEREQUAL: \
199 case EXPR_BINARY_ISLESS: \
200 case EXPR_BINARY_ISLESSEQUAL: \
201 case EXPR_BINARY_ISLESSGREATER: \
202 case EXPR_BINARY_ISUNORDERED:
205 * unary expression with mandatory operand
207 #define EXPR_UNARY_CASES_MANDATORY \
208 case EXPR_UNARY_NEGATE: \
209 case EXPR_UNARY_PLUS: \
210 case EXPR_UNARY_BITWISE_NEGATE: \
211 case EXPR_UNARY_NOT: \
212 case EXPR_UNARY_DEREFERENCE: \
213 case EXPR_UNARY_TAKE_ADDRESS: \
214 case EXPR_UNARY_POSTFIX_INCREMENT: \
215 case EXPR_UNARY_POSTFIX_DECREMENT: \
216 case EXPR_UNARY_PREFIX_INCREMENT: \
217 case EXPR_UNARY_PREFIX_DECREMENT: \
218 case EXPR_UNARY_CAST: \
219 case EXPR_UNARY_ASSUME: \
220 case EXPR_UNARY_DELETE: \
221 case EXPR_UNARY_DELETE_ARRAY:
224 * unary expression with optional operand
226 #define EXPR_UNARY_CASES_OPTIONAL \
227 case EXPR_UNARY_THROW: \
229 #define EXPR_UNARY_CASES \
230 EXPR_UNARY_CASES_MANDATORY \
231 EXPR_UNARY_CASES_OPTIONAL
233 #define EXPR_LITERAL_CASES \
234 case EXPR_LITERAL_BOOLEAN: \
235 case EXPR_LITERAL_INTEGER: \
236 case EXPR_LITERAL_INTEGER_OCTAL: \
237 case EXPR_LITERAL_INTEGER_HEXADECIMAL: \
238 case EXPR_LITERAL_FLOATINGPOINT: \
239 case EXPR_LITERAL_FLOATINGPOINT_HEXADECIMAL: \
240 case EXPR_LITERAL_CHARACTER: \
241 case EXPR_LITERAL_WIDE_CHARACTER: \
242 case EXPR_LITERAL_MS_NOOP:
245 * The base class of every expression.
247 struct expression_base_t {
248 expression_kind_t kind; /**< The expression kind. */
249 type_t *type; /**< The type of the expression. */
250 source_position_t source_position; /**< The source position of this expression. */
251 bool parenthesized : 1;
253 bool transformed : 1; /**< Set if this expression was transformed. */
255 bool implicit : 1; /**< compiler generated expression.
257 select into anonymous structs
263 * integer/float constants, character and string literals
265 struct literal_expression_t {
266 expression_base_t base;
271 ir_tarval *target_value;
274 struct string_literal_expression_t {
275 expression_base_t base;
279 struct funcname_expression_t {
280 expression_base_t base;
281 funcname_kind_t kind;
282 string_t value; /**< the value once assigned. */
285 struct compound_literal_expression_t {
286 expression_base_t base;
288 initializer_t *initializer;
291 struct builtin_constant_expression_t {
292 expression_base_t base;
296 struct builtin_types_compatible_expression_t {
297 expression_base_t base;
302 struct reference_expression_t {
303 expression_base_t base;
308 * An argument of a call.
310 struct call_argument_t {
311 expression_t *expression; /**< The expression which value is transmitted. */
312 call_argument_t *next; /**< Links to the next argument of this call. */
316 struct call_expression_t {
317 expression_base_t base;
318 expression_t *function; /**< The address of the function to call. */
319 call_argument_t *arguments; /**< List of arguments of this call. */
323 struct unary_expression_t {
324 expression_base_t base;
325 expression_t *value; /**< The unary operand. */
328 struct binary_expression_t {
329 expression_base_t base;
334 struct select_expression_t {
335 expression_base_t base;
336 expression_t *compound;
337 entity_t *compound_entry;
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 bool is_invalid_expression(expression_t *expression)
673 return expression->base.kind == EXPR_INVALID;
676 static inline bool is_invalid_statement(statement_t *statement)
678 return statement->base.kind == STATEMENT_INVALID;
682 * Allocate an AST node with given size and
683 * initialize all fields with zero.
685 static inline void *allocate_ast_zero(size_t size)
687 return memset(obstack_alloc(&ast_obstack, size), 0, size);
690 /** If set, implicit casts are printed. */
691 extern bool print_implicit_casts;
692 /** If set parenthesis are printed to indicate operator precedence. */
693 extern bool print_parenthesis;