2 * This file is part of cparser.
3 * Copyright (C) 2007-2008 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,
70 EXPR_CHARACTER_CONSTANT,
71 EXPR_WIDE_CHARACTER_CONSTANT,
73 EXPR_WIDE_STRING_LITERAL,
74 EXPR_COMPOUND_LITERAL,
84 EXPR_BUILTIN_CONSTANT_P,
85 EXPR_BUILTIN_TYPES_COMPATIBLE_P,
90 EXPR_LABEL_ADDRESS, /**< GCC extension &&label operator */
93 EXPR_UNARY_NEGATE = EXPR_UNARY_FIRST,
95 EXPR_UNARY_BITWISE_NEGATE,
97 EXPR_UNARY_DEREFERENCE,
98 EXPR_UNARY_TAKE_ADDRESS,
99 EXPR_UNARY_POSTFIX_INCREMENT,
100 EXPR_UNARY_POSTFIX_DECREMENT,
101 EXPR_UNARY_PREFIX_INCREMENT,
102 EXPR_UNARY_PREFIX_DECREMENT,
104 EXPR_UNARY_CAST_IMPLICIT, /**< compiler generated cast */
105 EXPR_UNARY_ASSUME, /**< MS __assume() */
107 EXPR_UNARY_DELETE_ARRAY,
109 EXPR_UNARY_LAST = EXPR_UNARY_THROW,
112 EXPR_BINARY_ADD = EXPR_BINARY_FIRST,
118 EXPR_BINARY_NOTEQUAL,
120 EXPR_BINARY_LESSEQUAL,
122 EXPR_BINARY_GREATEREQUAL,
123 EXPR_BINARY_BITWISE_AND,
124 EXPR_BINARY_BITWISE_OR,
125 EXPR_BINARY_BITWISE_XOR,
126 EXPR_BINARY_LOGICAL_AND,
127 EXPR_BINARY_LOGICAL_OR,
128 EXPR_BINARY_SHIFTLEFT,
129 EXPR_BINARY_SHIFTRIGHT,
131 EXPR_BINARY_MUL_ASSIGN,
132 EXPR_BINARY_DIV_ASSIGN,
133 EXPR_BINARY_MOD_ASSIGN,
134 EXPR_BINARY_ADD_ASSIGN,
135 EXPR_BINARY_SUB_ASSIGN,
136 EXPR_BINARY_SHIFTLEFT_ASSIGN,
137 EXPR_BINARY_SHIFTRIGHT_ASSIGN,
138 EXPR_BINARY_BITWISE_AND_ASSIGN,
139 EXPR_BINARY_BITWISE_XOR_ASSIGN,
140 EXPR_BINARY_BITWISE_OR_ASSIGN,
143 EXPR_BINARY_ISGREATER,
144 EXPR_BINARY_ISGREATEREQUAL,
146 EXPR_BINARY_ISLESSEQUAL,
147 EXPR_BINARY_ISLESSGREATER,
148 EXPR_BINARY_ISUNORDERED,
149 EXPR_BINARY_LAST = EXPR_BINARY_ISUNORDERED,
152 typedef enum funcname_kind_t {
153 FUNCNAME_FUNCTION, /**< C99 __func__, older __FUNCTION__ */
154 FUNCNAME_PRETTY_FUNCTION, /**< GNUC __PRETTY_FUNCTION__ */
155 FUNCNAME_FUNCSIG, /**< MS __FUNCSIG__ */
156 FUNCNAME_FUNCDNAME /**< MS __FUNCDNAME__ */
159 /* convenience macros */
160 #define EXPR_BINARY_CASES \
161 case EXPR_BINARY_ADD: \
162 case EXPR_BINARY_SUB: \
163 case EXPR_BINARY_MUL: \
164 case EXPR_BINARY_DIV: \
165 case EXPR_BINARY_MOD: \
166 case EXPR_BINARY_EQUAL: \
167 case EXPR_BINARY_NOTEQUAL: \
168 case EXPR_BINARY_LESS: \
169 case EXPR_BINARY_LESSEQUAL: \
170 case EXPR_BINARY_GREATER: \
171 case EXPR_BINARY_GREATEREQUAL: \
172 case EXPR_BINARY_BITWISE_AND: \
173 case EXPR_BINARY_BITWISE_OR: \
174 case EXPR_BINARY_BITWISE_XOR: \
175 case EXPR_BINARY_LOGICAL_AND: \
176 case EXPR_BINARY_LOGICAL_OR: \
177 case EXPR_BINARY_SHIFTLEFT: \
178 case EXPR_BINARY_SHIFTRIGHT: \
179 case EXPR_BINARY_ASSIGN: \
180 case EXPR_BINARY_MUL_ASSIGN: \
181 case EXPR_BINARY_DIV_ASSIGN: \
182 case EXPR_BINARY_MOD_ASSIGN: \
183 case EXPR_BINARY_ADD_ASSIGN: \
184 case EXPR_BINARY_SUB_ASSIGN: \
185 case EXPR_BINARY_SHIFTLEFT_ASSIGN: \
186 case EXPR_BINARY_SHIFTRIGHT_ASSIGN: \
187 case EXPR_BINARY_BITWISE_AND_ASSIGN: \
188 case EXPR_BINARY_BITWISE_XOR_ASSIGN: \
189 case EXPR_BINARY_BITWISE_OR_ASSIGN: \
190 case EXPR_BINARY_COMMA: \
191 case EXPR_BINARY_ISGREATER: \
192 case EXPR_BINARY_ISGREATEREQUAL: \
193 case EXPR_BINARY_ISLESS: \
194 case EXPR_BINARY_ISLESSEQUAL: \
195 case EXPR_BINARY_ISLESSGREATER: \
196 case EXPR_BINARY_ISUNORDERED:
199 * unary expression with mandatory operand
201 #define EXPR_UNARY_CASES_MANDATORY \
202 case EXPR_UNARY_NEGATE: \
203 case EXPR_UNARY_PLUS: \
204 case EXPR_UNARY_BITWISE_NEGATE: \
205 case EXPR_UNARY_NOT: \
206 case EXPR_UNARY_DEREFERENCE: \
207 case EXPR_UNARY_TAKE_ADDRESS: \
208 case EXPR_UNARY_POSTFIX_INCREMENT: \
209 case EXPR_UNARY_POSTFIX_DECREMENT: \
210 case EXPR_UNARY_PREFIX_INCREMENT: \
211 case EXPR_UNARY_PREFIX_DECREMENT: \
212 case EXPR_UNARY_CAST: \
213 case EXPR_UNARY_CAST_IMPLICIT: \
214 case EXPR_UNARY_ASSUME: \
215 case EXPR_UNARY_DELETE: \
216 case EXPR_UNARY_DELETE_ARRAY:
219 * unary expression with optional operand
221 #define EXPR_UNARY_CASES_OPTIONAL \
222 case EXPR_UNARY_THROW: \
224 #define EXPR_UNARY_CASES \
225 EXPR_UNARY_CASES_MANDATORY \
226 EXPR_UNARY_CASES_OPTIONAL
229 * The base class of every expression.
231 struct expression_base_t {
232 expression_kind_t kind; /**< The expression kind. */
233 type_t *type; /**< The type of the expression. */
234 source_position_t source_position; /**< The source position of this expression. */
237 bool transformed; /**< Set if this expression was transformed. */
244 struct const_expression_t {
245 expression_base_t base;
248 long double float_value;
250 wide_string_t wide_character;
252 bool is_ms_noop; /**< True, if this constant is the result
253 of an microsoft __noop operator */
256 struct string_literal_expression_t {
257 expression_base_t base;
261 struct funcname_expression_t {
262 expression_base_t base;
263 funcname_kind_t kind;
264 string_t value; /**< the value once assigned. */
267 struct wide_string_literal_expression_t {
268 expression_base_t base;
272 struct compound_literal_expression_t {
273 expression_base_t base;
275 initializer_t *initializer;
278 struct builtin_constant_expression_t {
279 expression_base_t base;
283 struct builtin_types_compatible_expression_t {
284 expression_base_t base;
289 struct reference_expression_t {
290 expression_base_t base;
295 * An argument of a call.
297 struct call_argument_t {
298 expression_t *expression; /**< The expression which value is transmitted. */
299 call_argument_t *next; /**< Links to the next argument of this call. */
303 struct call_expression_t {
304 expression_base_t base;
305 expression_t *function; /**< The address of the function to call. */
306 call_argument_t *arguments; /**< List of arguments of this call. */
310 struct unary_expression_t {
311 expression_base_t base;
312 expression_t *value; /**< The unary operand. */
315 struct binary_expression_t {
316 expression_base_t base;
321 struct select_expression_t {
322 expression_base_t base;
323 expression_t *compound;
324 entity_t *compound_entry;
327 struct array_access_expression_t {
328 expression_base_t base;
329 expression_t *array_ref; /**< the referenced array */
330 expression_t *index; /**< the index used */
331 bool flipped; /**< True if index/ref was written in a 5[a] way */
334 struct typeprop_expression_t {
335 expression_base_t base;
337 expression_t *tp_expression;
340 struct designator_t {
341 source_position_t source_position;
342 symbol_t *symbol; /**< the symbol if any */
343 expression_t *array_index; /**< the array index if any */
347 struct offsetof_expression_t {
348 expression_base_t base;
350 designator_t *designator;
353 struct va_start_expression_t {
354 expression_base_t base;
356 variable_t *parameter;
359 struct va_arg_expression_t {
360 expression_base_t base;
364 struct conditional_expression_t {
365 expression_base_t base;
366 expression_t *condition;
367 expression_t *true_expression;
368 expression_t *false_expression;
371 struct statement_expression_t {
372 expression_base_t base;
373 statement_t *statement;
376 struct classify_type_expression_t {
377 expression_base_t base;
378 expression_t *type_expression;
381 struct label_address_expression_t {
382 expression_base_t base;
387 expression_kind_t kind;
388 expression_base_t base;
389 const_expression_t conste;
390 funcname_expression_t funcname;
391 string_literal_expression_t string;
392 wide_string_literal_expression_t wide_string;
393 compound_literal_expression_t compound_literal;
394 builtin_constant_expression_t builtin_constant;
395 builtin_types_compatible_expression_t builtin_types_compatible;
396 reference_expression_t reference;
397 call_expression_t call;
398 unary_expression_t unary;
399 binary_expression_t binary;
400 select_expression_t select;
401 array_access_expression_t array_access;
402 typeprop_expression_t typeprop;
403 offsetof_expression_t offsetofe;
404 va_start_expression_t va_starte;
405 va_arg_expression_t va_arge;
406 conditional_expression_t conditional;
407 statement_expression_t statement;
408 classify_type_expression_t classify_type;
409 label_address_expression_t label_address;
412 typedef enum initializer_kind_t {
416 INITIALIZER_WIDE_STRING,
417 INITIALIZER_DESIGNATOR
418 } initializer_kind_t;
420 struct initializer_base_t {
421 initializer_kind_t kind;
424 struct initializer_value_t {
425 initializer_base_t base;
429 struct initializer_list_t {
430 initializer_base_t base;
432 initializer_t *initializers[];
435 struct initializer_string_t {
436 initializer_base_t base;
440 struct initializer_wide_string_t {
441 initializer_base_t base;
442 wide_string_t string;
445 struct initializer_designator_t {
446 initializer_base_t base;
447 designator_t *designator;
450 union initializer_t {
451 initializer_kind_t kind;
452 initializer_base_t base;
453 initializer_value_t value;
454 initializer_list_t list;
455 initializer_string_t string;
456 initializer_wide_string_t wide_string;
457 initializer_designator_t designator;
463 typedef enum gnu_attribute_kind_t {
471 GNU_AK_RETURNS_TWICE,
475 GNU_AK_ALWAYS_INLINE,
481 GNU_AK_TRANSPARENT_UNION,
489 GNU_AK_NO_INSTRUMENT_FUNCTION,
490 GNU_AK_WARN_UNUSED_RESULT,
495 GNU_AK_FUNCTION_VECTOR,
497 GNU_AK_INTERRUPT_HANDLER,
503 GNU_AK_EIGTHBIT_DATA,
508 GNU_AK_EXTERNALLY_VISIBLE,
531 } gnu_attribute_kind_t;
534 * The statement kinds.
536 typedef enum statement_kind_t {
541 STATEMENT_DECLARATION,
544 STATEMENT_EXPRESSION,
549 STATEMENT_CASE_LABEL,
554 STATEMENT_MS_TRY, /**< MS __try/__finally or __try/__except */
555 STATEMENT_LEAVE /**< MS __leave */
559 * The base class of every statement.
561 struct statement_base_t {
562 statement_kind_t kind;
563 statement_t *next; /**< Point to the next statement in a compound statement. */
564 source_position_t source_position;
565 statement_t *parent; /**< The Parent statement that controls the execution. */
566 bool reachable; /**< True, if this statement is reachable. */
572 struct invalid_statement_t {
573 statement_base_t base;
576 struct empty_statement_t {
577 statement_base_t base;
580 struct return_statement_t {
581 statement_base_t base;
582 expression_t *value; /**< The return value if any. */
585 struct compound_statement_t {
586 statement_base_t base;
587 statement_t *statements;
589 bool stmt_expr; /**< True if this compound statement is a statement expression. */
592 struct declaration_statement_t {
593 statement_base_t base;
594 entity_t *declarations_begin;
595 entity_t *declarations_end;
598 struct if_statement_t {
599 statement_base_t base;
600 expression_t *condition;
601 statement_t *true_statement;
602 statement_t *false_statement;
605 struct switch_statement_t {
606 statement_base_t base;
607 expression_t *expression;
609 case_label_statement_t *first_case, *last_case; /**< List of all cases, including default. */
610 case_label_statement_t *default_label; /**< The default label if existent. */
611 unsigned long default_proj_nr; /**< The Proj-number for the default Proj. */
614 struct goto_statement_t {
615 statement_base_t base;
616 label_t *label; /**< The destination label. */
617 expression_t *expression; /**< The expression for an assigned goto. */
618 goto_statement_t *next; /**< links all goto statements of a function */
621 struct case_label_statement_t {
622 statement_base_t base;
623 expression_t *expression; /**< The case label expression, NULL for default label. */
624 expression_t *end_range; /**< For GNUC case a .. b: the end range expression, NULL else. */
625 case_label_statement_t *next; /**< link to the next case label in switch */
626 statement_t *statement;
627 long first_case; /**< The folded value of expression. */
628 long last_case; /**< The folded value of end_range. */
629 bool is_bad; /**< If set marked as bad to suppress warnings. */
630 bool is_empty_range; /**< If set marked this as an empty range. */
633 struct label_statement_t {
634 statement_base_t base;
636 statement_t *statement;
637 label_statement_t *next; /**< links all label statements of a function */
640 struct expression_statement_t {
641 statement_base_t base;
642 expression_t *expression;
645 struct while_statement_t {
646 statement_base_t base;
647 expression_t *condition;
651 struct do_while_statement_t {
652 statement_base_t base;
653 expression_t *condition;
657 struct for_statement_t {
658 statement_base_t base;
659 expression_t *initialisation;
660 expression_t *condition;
664 bool condition_reachable:1;
665 bool step_reachable:1;
668 struct asm_argument_t {
669 string_t constraints;
670 expression_t *expression;
672 asm_argument_t *next;
675 struct asm_clobber_t {
680 struct asm_statement_t {
681 statement_base_t base;
683 asm_argument_t *inputs;
684 asm_argument_t *outputs;
685 asm_clobber_t *clobbers;
689 struct ms_try_statement_t {
690 statement_base_t base;
691 statement_t *try_statement;
692 expression_t *except_expression; /**< non-null for except, NULL for finally */
693 statement_t *final_statement;
696 struct leave_statement_t {
697 statement_base_t base;
701 statement_kind_t kind;
702 statement_base_t base;
703 return_statement_t returns;
704 compound_statement_t compound;
705 declaration_statement_t declaration;
707 switch_statement_t switchs;
708 goto_statement_t gotos;
709 case_label_statement_t case_label;
710 label_statement_t label;
711 expression_statement_t expression;
712 while_statement_t whiles;
713 do_while_statement_t do_while;
714 for_statement_t fors;
715 asm_statement_t asms;
716 ms_try_statement_t ms_try;
717 leave_statement_t leave;
720 struct translation_unit_t {
722 statement_t *global_asm;
726 void *_allocate_ast(size_t size)
728 return obstack_alloc(&ast_obstack, size);
732 bool is_invalid_expression(expression_t *expression)
734 return expression->base.kind == EXPR_INVALID;
738 bool is_invalid_statement(statement_t *statement)
740 return statement->base.kind == STATEMENT_INVALID;
744 #define allocate_ast(size) _allocate_ast(size)