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,
85 EXPR_BUILTIN_CONSTANT_P,
86 EXPR_BUILTIN_TYPES_COMPATIBLE_P,
91 EXPR_LABEL_ADDRESS, /**< GCC extension &&label operator */
94 EXPR_UNARY_NEGATE = EXPR_UNARY_FIRST,
96 EXPR_UNARY_BITWISE_NEGATE,
98 EXPR_UNARY_DEREFERENCE,
99 EXPR_UNARY_TAKE_ADDRESS,
100 EXPR_UNARY_POSTFIX_INCREMENT,
101 EXPR_UNARY_POSTFIX_DECREMENT,
102 EXPR_UNARY_PREFIX_INCREMENT,
103 EXPR_UNARY_PREFIX_DECREMENT,
105 EXPR_UNARY_CAST_IMPLICIT, /**< compiler generated cast */
106 EXPR_UNARY_ASSUME, /**< MS __assume() */
108 EXPR_UNARY_DELETE_ARRAY,
110 EXPR_UNARY_LAST = EXPR_UNARY_THROW,
113 EXPR_BINARY_ADD = EXPR_BINARY_FIRST,
119 EXPR_BINARY_NOTEQUAL,
121 EXPR_BINARY_LESSEQUAL,
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,
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,
144 EXPR_BINARY_ISGREATER,
145 EXPR_BINARY_ISGREATEREQUAL,
147 EXPR_BINARY_ISLESSEQUAL,
148 EXPR_BINARY_ISLESSGREATER,
149 EXPR_BINARY_ISUNORDERED,
150 EXPR_BINARY_LAST = EXPR_BINARY_ISUNORDERED,
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__ */
160 /* convenience macros */
161 #define EXPR_BINARY_CASES \
162 case 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:
200 * unary expression with mandatory operand
202 #define EXPR_UNARY_CASES_MANDATORY \
203 case 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_CAST_IMPLICIT: \
215 case EXPR_UNARY_ASSUME: \
216 case EXPR_UNARY_DELETE: \
217 case EXPR_UNARY_DELETE_ARRAY:
220 * unary expression with optional operand
222 #define EXPR_UNARY_CASES_OPTIONAL \
223 case EXPR_UNARY_THROW: \
225 #define EXPR_UNARY_CASES \
226 EXPR_UNARY_CASES_MANDATORY \
227 EXPR_UNARY_CASES_OPTIONAL
230 * The base class of every expression.
232 struct expression_base_t {
233 expression_kind_t kind; /**< The expression kind. */
234 type_t *type; /**< The type of the expression. */
235 source_position_t source_position; /**< The source position of this expression. */
238 bool transformed; /**< Set if this expression was transformed. */
242 struct const_expression_t {
243 expression_base_t base;
246 long double float_value;
248 wide_string_t wide_character;
250 bool is_ms_noop; /**< True, if this constant is the result
251 of an microsoft __noop operator */
254 struct string_literal_expression_t {
255 expression_base_t base;
259 struct funcname_expression_t {
260 expression_base_t base;
261 funcname_kind_t kind;
262 string_t value; /**< the value once assigned. */
265 struct wide_string_literal_expression_t {
266 expression_base_t base;
270 struct compound_literal_expression_t {
271 expression_base_t base;
273 initializer_t *initializer;
276 struct builtin_symbol_expression_t {
277 expression_base_t base;
281 struct builtin_constant_expression_t {
282 expression_base_t base;
286 struct builtin_types_compatible_expression_t {
287 expression_base_t base;
292 struct reference_expression_t {
293 expression_base_t base;
297 struct call_argument_t {
298 expression_t *expression;
299 call_argument_t *next;
302 struct call_expression_t {
303 expression_base_t base;
304 expression_t *function;
305 call_argument_t *arguments;
308 struct unary_expression_t {
309 expression_base_t base;
313 struct binary_expression_t {
314 expression_base_t base;
319 struct select_expression_t {
320 expression_base_t base;
321 expression_t *compound;
322 entity_t *compound_entry;
325 struct array_access_expression_t {
326 expression_base_t base;
327 expression_t *array_ref;
329 bool flipped; /**< index/ref was written in a 5[a] way */
332 struct typeprop_expression_t {
333 expression_base_t base;
335 expression_t *tp_expression;
338 struct designator_t {
339 source_position_t source_position;
341 expression_t *array_index;
345 struct offsetof_expression_t {
346 expression_base_t base;
348 designator_t *designator;
351 struct va_start_expression_t {
352 expression_base_t base;
354 variable_t *parameter;
357 struct va_arg_expression_t {
358 expression_base_t base;
362 struct conditional_expression_t {
363 expression_base_t base;
364 expression_t *condition;
365 expression_t *true_expression;
366 expression_t *false_expression;
369 struct statement_expression_t {
370 expression_base_t base;
371 statement_t *statement;
374 struct classify_type_expression_t {
375 expression_base_t base;
376 expression_t *type_expression;
379 struct label_address_expression_t {
380 expression_base_t base;
385 expression_kind_t kind;
386 expression_base_t base;
387 const_expression_t conste;
388 funcname_expression_t funcname;
389 string_literal_expression_t string;
390 wide_string_literal_expression_t wide_string;
391 compound_literal_expression_t compound_literal;
392 builtin_symbol_expression_t builtin_symbol;
393 builtin_constant_expression_t builtin_constant;
394 builtin_types_compatible_expression_t builtin_types_compatible;
395 reference_expression_t reference;
396 call_expression_t call;
397 unary_expression_t unary;
398 binary_expression_t binary;
399 select_expression_t select;
400 array_access_expression_t array_access;
401 typeprop_expression_t typeprop;
402 offsetof_expression_t offsetofe;
403 va_start_expression_t va_starte;
404 va_arg_expression_t va_arge;
405 conditional_expression_t conditional;
406 statement_expression_t statement;
407 classify_type_expression_t classify_type;
408 label_address_expression_t label_address;
411 typedef enum initializer_kind_t {
415 INITIALIZER_WIDE_STRING,
416 INITIALIZER_DESIGNATOR
417 } initializer_kind_t;
419 struct initializer_base_t {
420 initializer_kind_t kind;
423 struct initializer_value_t {
424 initializer_base_t base;
428 struct initializer_list_t {
429 initializer_base_t base;
431 initializer_t *initializers[];
434 struct initializer_string_t {
435 initializer_base_t base;
439 struct initializer_wide_string_t {
440 initializer_base_t base;
441 wide_string_t string;
444 struct initializer_designator_t {
445 initializer_base_t base;
446 designator_t *designator;
449 union initializer_t {
450 initializer_kind_t kind;
451 initializer_base_t base;
452 initializer_value_t value;
453 initializer_list_t list;
454 initializer_string_t string;
455 initializer_wide_string_t wide_string;
456 initializer_designator_t designator;
462 typedef enum gnu_attribute_kind_t {
470 GNU_AK_RETURNS_TWICE,
474 GNU_AK_ALWAYS_INLINE,
480 GNU_AK_TRANSPARENT_UNION,
488 GNU_AK_NO_INSTRUMENT_FUNCTION,
489 GNU_AK_WARN_UNUSED_RESULT,
494 GNU_AK_FUNCTION_VECTOR,
496 GNU_AK_INTERRUPT_HANDLER,
502 GNU_AK_EIGTHBIT_DATA,
507 GNU_AK_EXTERNALLY_VISIBLE,
530 } gnu_attribute_kind_t;
533 * The statement kinds.
535 typedef enum statement_kind_t {
540 STATEMENT_DECLARATION,
543 STATEMENT_EXPRESSION,
548 STATEMENT_CASE_LABEL,
553 STATEMENT_MS_TRY, /**< MS __try/__finally or __try/__except */
554 STATEMENT_LEAVE /**< MS __leave */
558 * The base class of every statement.
560 struct statement_base_t {
561 statement_kind_t kind;
563 source_position_t source_position;
571 struct invalid_statement_t {
572 statement_base_t base;
575 struct empty_statement_t {
576 statement_base_t base;
579 struct return_statement_t {
580 statement_base_t base;
584 struct compound_statement_t {
585 statement_base_t base;
586 statement_t *statements;
588 bool stmt_expr; /* The compound statement is a statement expression */
591 struct declaration_statement_t {
592 statement_base_t base;
593 entity_t *declarations_begin;
594 entity_t *declarations_end;
597 struct if_statement_t {
598 statement_base_t base;
599 expression_t *condition;
600 statement_t *true_statement;
601 statement_t *false_statement;
604 struct switch_statement_t {
605 statement_base_t base;
606 expression_t *expression;
608 case_label_statement_t *first_case, *last_case; /**< List of all cases, including default. */
609 case_label_statement_t *default_label; /**< The default label if existent. */
610 unsigned long default_proj_nr; /**< The Proj-number for the default Proj. */
613 struct goto_statement_t {
614 statement_base_t base;
615 label_t *label; /**< The destination label. */
616 expression_t *expression; /**< The expression for an assigned goto. */
617 goto_statement_t *next; /**< links all goto statements of a function */
620 struct case_label_statement_t {
621 statement_base_t base;
622 expression_t *expression; /**< The case label expression, NULL for default label. */
623 expression_t *end_range; /**< For GNUC case a .. b: the end range expression, NULL else. */
624 case_label_statement_t *next; /**< link to the next case label in switch */
625 statement_t *statement;
626 long first_case; /**< The folded value of expression. */
627 long last_case; /**< The folded value of end_range. */
628 bool is_bad; /**< If set marked as bad to suppress warnings. */
629 bool is_empty_range; /**< If set marked this as an empty range. */
632 struct label_statement_t {
633 statement_base_t base;
635 statement_t *statement;
636 label_statement_t *next; /**< links all label statements of a function */
639 struct expression_statement_t {
640 statement_base_t base;
641 expression_t *expression;
644 struct while_statement_t {
645 statement_base_t base;
646 expression_t *condition;
650 struct do_while_statement_t {
651 statement_base_t base;
652 expression_t *condition;
656 struct for_statement_t {
657 statement_base_t base;
658 expression_t *initialisation;
659 expression_t *condition;
663 bool condition_reachable:1;
664 bool step_reachable:1;
667 struct asm_argument_t {
668 string_t constraints;
669 expression_t *expression;
671 asm_argument_t *next;
674 struct asm_clobber_t {
679 struct asm_statement_t {
680 statement_base_t base;
682 asm_argument_t *inputs;
683 asm_argument_t *outputs;
684 asm_clobber_t *clobbers;
688 struct ms_try_statement_t {
689 statement_base_t base;
690 statement_t *try_statement;
691 expression_t *except_expression; /**< non-null for except, NULL for finally */
692 statement_t *final_statement;
695 struct leave_statement_t {
696 statement_base_t base;
700 statement_kind_t kind;
701 statement_base_t base;
702 return_statement_t returns;
703 compound_statement_t compound;
704 declaration_statement_t declaration;
706 switch_statement_t switchs;
707 goto_statement_t gotos;
708 case_label_statement_t case_label;
709 label_statement_t label;
710 expression_statement_t expression;
711 while_statement_t whiles;
712 do_while_statement_t do_while;
713 for_statement_t fors;
714 asm_statement_t asms;
715 ms_try_statement_t ms_try;
716 leave_statement_t leave;
719 struct translation_unit_t {
721 statement_t *global_asm;
725 void *_allocate_ast(size_t size)
727 return obstack_alloc(&ast_obstack, size);
731 bool is_invalid_expression(expression_t *expression)
733 return expression->base.kind == EXPR_INVALID;
737 bool is_invalid_statement(statement_t *statement)
739 return statement->base.kind == STATEMENT_INVALID;
743 #define allocate_ast(size) _allocate_ast(size)