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 {
70 EXPR_LITERAL_FLOATINGPOINT,
71 EXPR_LITERAL_FLOATINGPOINT_HEXADECIMAL,
72 EXPR_LITERAL_CHARACTER,
73 EXPR_LITERAL_WIDE_CHARACTER,
74 EXPR_LITERAL_MS_NOOP, /**< MS __noop extension */
76 EXPR_WIDE_STRING_LITERAL,
77 EXPR_COMPOUND_LITERAL,
87 EXPR_BUILTIN_CONSTANT_P,
88 EXPR_BUILTIN_TYPES_COMPATIBLE_P,
94 EXPR_LABEL_ADDRESS, /**< GCC extension &&label operator */
97 EXPR_UNARY_NEGATE = EXPR_UNARY_FIRST,
99 EXPR_UNARY_BITWISE_NEGATE,
101 EXPR_UNARY_DEREFERENCE,
102 EXPR_UNARY_TAKE_ADDRESS,
103 EXPR_UNARY_POSTFIX_INCREMENT,
104 EXPR_UNARY_POSTFIX_DECREMENT,
105 EXPR_UNARY_PREFIX_INCREMENT,
106 EXPR_UNARY_PREFIX_DECREMENT,
108 EXPR_UNARY_ASSUME, /**< MS __assume() */
110 EXPR_UNARY_DELETE_ARRAY,
112 EXPR_UNARY_LAST = EXPR_UNARY_THROW,
115 EXPR_BINARY_ADD = EXPR_BINARY_FIRST,
121 EXPR_BINARY_NOTEQUAL,
123 EXPR_BINARY_LESSEQUAL,
125 EXPR_BINARY_GREATEREQUAL,
126 EXPR_BINARY_BITWISE_AND,
127 EXPR_BINARY_BITWISE_OR,
128 EXPR_BINARY_BITWISE_XOR,
129 EXPR_BINARY_LOGICAL_AND,
130 EXPR_BINARY_LOGICAL_OR,
131 EXPR_BINARY_SHIFTLEFT,
132 EXPR_BINARY_SHIFTRIGHT,
134 EXPR_BINARY_MUL_ASSIGN,
135 EXPR_BINARY_DIV_ASSIGN,
136 EXPR_BINARY_MOD_ASSIGN,
137 EXPR_BINARY_ADD_ASSIGN,
138 EXPR_BINARY_SUB_ASSIGN,
139 EXPR_BINARY_SHIFTLEFT_ASSIGN,
140 EXPR_BINARY_SHIFTRIGHT_ASSIGN,
141 EXPR_BINARY_BITWISE_AND_ASSIGN,
142 EXPR_BINARY_BITWISE_XOR_ASSIGN,
143 EXPR_BINARY_BITWISE_OR_ASSIGN,
146 EXPR_BINARY_ISGREATER,
147 EXPR_BINARY_ISGREATEREQUAL,
149 EXPR_BINARY_ISLESSEQUAL,
150 EXPR_BINARY_ISLESSGREATER,
151 EXPR_BINARY_ISUNORDERED,
152 EXPR_BINARY_LAST = EXPR_BINARY_ISUNORDERED,
155 typedef enum funcname_kind_t {
156 FUNCNAME_FUNCTION, /**< C99 __func__, older __FUNCTION__ */
157 FUNCNAME_PRETTY_FUNCTION, /**< GNUC __PRETTY_FUNCTION__ */
158 FUNCNAME_FUNCSIG, /**< MS __FUNCSIG__ */
159 FUNCNAME_FUNCDNAME /**< MS __FUNCDNAME__ */
162 /* convenience macros */
163 #define EXPR_BINARY_CASES \
165 case EXPR_BINARY_SUB: \
166 case EXPR_BINARY_MUL: \
167 case EXPR_BINARY_DIV: \
168 case EXPR_BINARY_MOD: \
169 case EXPR_BINARY_EQUAL: \
170 case EXPR_BINARY_NOTEQUAL: \
171 case EXPR_BINARY_LESS: \
172 case EXPR_BINARY_LESSEQUAL: \
173 case EXPR_BINARY_GREATER: \
174 case EXPR_BINARY_GREATEREQUAL: \
175 case EXPR_BINARY_BITWISE_AND: \
176 case EXPR_BINARY_BITWISE_OR: \
177 case EXPR_BINARY_BITWISE_XOR: \
178 case EXPR_BINARY_LOGICAL_AND: \
179 case EXPR_BINARY_LOGICAL_OR: \
180 case EXPR_BINARY_SHIFTLEFT: \
181 case EXPR_BINARY_SHIFTRIGHT: \
182 case EXPR_BINARY_ASSIGN: \
183 case EXPR_BINARY_MUL_ASSIGN: \
184 case EXPR_BINARY_DIV_ASSIGN: \
185 case EXPR_BINARY_MOD_ASSIGN: \
186 case EXPR_BINARY_ADD_ASSIGN: \
187 case EXPR_BINARY_SUB_ASSIGN: \
188 case EXPR_BINARY_SHIFTLEFT_ASSIGN: \
189 case EXPR_BINARY_SHIFTRIGHT_ASSIGN: \
190 case EXPR_BINARY_BITWISE_AND_ASSIGN: \
191 case EXPR_BINARY_BITWISE_XOR_ASSIGN: \
192 case EXPR_BINARY_BITWISE_OR_ASSIGN: \
193 case EXPR_BINARY_COMMA: \
194 case EXPR_BINARY_ISGREATER: \
195 case EXPR_BINARY_ISGREATEREQUAL: \
196 case EXPR_BINARY_ISLESS: \
197 case EXPR_BINARY_ISLESSEQUAL: \
198 case EXPR_BINARY_ISLESSGREATER: \
199 case EXPR_BINARY_ISUNORDERED
202 * unary expression with mandatory operand
204 #define EXPR_UNARY_CASES_MANDATORY \
206 case EXPR_UNARY_PLUS: \
207 case EXPR_UNARY_BITWISE_NEGATE: \
208 case EXPR_UNARY_NOT: \
209 case EXPR_UNARY_DEREFERENCE: \
210 case EXPR_UNARY_TAKE_ADDRESS: \
211 case EXPR_UNARY_POSTFIX_INCREMENT: \
212 case EXPR_UNARY_POSTFIX_DECREMENT: \
213 case EXPR_UNARY_PREFIX_INCREMENT: \
214 case EXPR_UNARY_PREFIX_DECREMENT: \
215 case EXPR_UNARY_CAST: \
216 case EXPR_UNARY_ASSUME: \
217 case EXPR_UNARY_DELETE: \
218 case EXPR_UNARY_DELETE_ARRAY
221 * unary expression with optional operand
223 #define EXPR_UNARY_CASES_OPTIONAL \
226 #define EXPR_UNARY_CASES \
227 EXPR_UNARY_CASES_MANDATORY: \
228 case EXPR_UNARY_CASES_OPTIONAL
230 #define EXPR_LITERAL_CASES \
231 EXPR_LITERAL_BOOLEAN: \
232 case EXPR_LITERAL_INTEGER: \
233 case EXPR_LITERAL_FLOATINGPOINT: \
234 case EXPR_LITERAL_FLOATINGPOINT_HEXADECIMAL: \
235 case EXPR_LITERAL_CHARACTER: \
236 case EXPR_LITERAL_WIDE_CHARACTER: \
237 case EXPR_LITERAL_MS_NOOP
240 * The base class of every expression.
242 struct expression_base_t {
243 expression_kind_t kind; /**< The expression kind. */
244 type_t *type; /**< The type of the expression. */
245 source_position_t source_position; /**< The source position of this expression. */
246 bool parenthesized : 1;
248 bool transformed : 1; /**< Set if this expression was transformed. */
250 bool implicit : 1; /**< compiler generated expression.
252 select into anonymous structs
258 * integer/float constants, character and string literals
260 struct literal_expression_t {
261 expression_base_t base;
266 ir_tarval *target_value;
269 struct string_literal_expression_t {
270 expression_base_t base;
274 struct funcname_expression_t {
275 expression_base_t base;
276 funcname_kind_t kind;
277 string_t value; /**< the value once assigned. */
280 struct compound_literal_expression_t {
281 expression_base_t base;
283 initializer_t *initializer;
286 struct builtin_constant_expression_t {
287 expression_base_t base;
291 struct builtin_types_compatible_expression_t {
292 expression_base_t base;
297 struct reference_expression_t {
298 expression_base_t base;
303 * An argument of a call.
305 struct call_argument_t {
306 expression_t *expression; /**< The expression which value is transmitted. */
307 call_argument_t *next; /**< Links to the next argument of this call. */
311 struct call_expression_t {
312 expression_base_t base;
313 expression_t *function; /**< The address of the function to call. */
314 call_argument_t *arguments; /**< List of arguments of this call. */
318 struct unary_expression_t {
319 expression_base_t base;
320 expression_t *value; /**< The unary operand. */
323 struct binary_expression_t {
324 expression_base_t base;
329 struct select_expression_t {
330 expression_base_t base;
331 expression_t *compound;
332 entity_t *compound_entry;
335 struct array_access_expression_t {
336 expression_base_t base;
337 expression_t *array_ref; /**< the referenced array */
338 expression_t *index; /**< the index used */
339 bool flipped; /**< True if index/ref was written in a 5[a] way */
342 struct typeprop_expression_t {
343 expression_base_t base;
345 expression_t *tp_expression;
348 struct designator_t {
349 source_position_t source_position;
350 symbol_t *symbol; /**< the symbol if any */
351 expression_t *array_index; /**< the array index if any */
355 struct offsetof_expression_t {
356 expression_base_t base;
358 designator_t *designator;
361 struct va_start_expression_t {
362 expression_base_t base;
364 variable_t *parameter;
367 struct va_arg_expression_t {
368 expression_base_t base;
372 struct va_copy_expression_t {
373 expression_base_t base;
374 expression_t *dst; /**< destination argument */
375 expression_t *src; /**< source argument */
378 struct conditional_expression_t {
379 expression_base_t base;
380 expression_t *condition;
381 expression_t *true_expression;
382 expression_t *false_expression;
385 struct statement_expression_t {
386 expression_base_t base;
387 statement_t *statement;
390 struct classify_type_expression_t {
391 expression_base_t base;
392 expression_t *type_expression;
395 struct label_address_expression_t {
396 expression_base_t base;
401 expression_kind_t kind;
402 expression_base_t base;
403 literal_expression_t literal;
404 string_literal_expression_t string_literal;
405 funcname_expression_t funcname;
406 compound_literal_expression_t compound_literal;
407 builtin_constant_expression_t builtin_constant;
408 builtin_types_compatible_expression_t builtin_types_compatible;
409 reference_expression_t reference;
410 call_expression_t call;
411 unary_expression_t unary;
412 binary_expression_t binary;
413 select_expression_t select;
414 array_access_expression_t array_access;
415 typeprop_expression_t typeprop;
416 offsetof_expression_t offsetofe;
417 va_start_expression_t va_starte;
418 va_arg_expression_t va_arge;
419 va_copy_expression_t va_copye;
420 conditional_expression_t conditional;
421 statement_expression_t statement;
422 classify_type_expression_t classify_type;
423 label_address_expression_t label_address;
426 typedef enum initializer_kind_t {
430 INITIALIZER_WIDE_STRING,
431 INITIALIZER_DESIGNATOR
432 } initializer_kind_t;
434 struct initializer_base_t {
435 initializer_kind_t kind;
438 struct initializer_value_t {
439 initializer_base_t base;
443 struct initializer_list_t {
444 initializer_base_t base;
446 initializer_t *initializers[];
449 struct initializer_string_t {
450 initializer_base_t base;
454 struct initializer_wide_string_t {
455 initializer_base_t base;
459 struct initializer_designator_t {
460 initializer_base_t base;
461 designator_t *designator;
464 union initializer_t {
465 initializer_kind_t kind;
466 initializer_base_t base;
467 initializer_value_t value;
468 initializer_list_t list;
469 initializer_string_t string;
470 initializer_wide_string_t wide_string;
471 initializer_designator_t designator;
475 * The statement kinds.
477 typedef enum statement_kind_t {
482 STATEMENT_DECLARATION,
485 STATEMENT_EXPRESSION,
488 STATEMENT_COMPUTED_GOTO,
491 STATEMENT_CASE_LABEL,
496 STATEMENT_MS_TRY, /**< MS __try/__finally or __try/__except */
497 STATEMENT_LEAVE /**< MS __leave */
501 * The base class of every statement.
503 struct statement_base_t {
504 statement_kind_t kind;
505 statement_t *next; /**< Point to the next statement in a compound statement. */
506 source_position_t source_position;
507 statement_t *parent; /**< The Parent statement that controls the execution. */
508 bool reachable; /**< True, if this statement is reachable. */
514 struct return_statement_t {
515 statement_base_t base;
516 expression_t *value; /**< The return value if any. */
519 struct compound_statement_t {
520 statement_base_t base;
521 statement_t *statements;
523 bool stmt_expr; /**< True if this compound statement is a statement expression. */
526 struct declaration_statement_t {
527 statement_base_t base;
528 entity_t *declarations_begin;
529 entity_t *declarations_end;
532 struct if_statement_t {
533 statement_base_t base;
535 expression_t *condition;
536 statement_t *true_statement;
537 statement_t *false_statement;
540 struct switch_statement_t {
541 statement_base_t base;
543 expression_t *expression;
545 case_label_statement_t *first_case, *last_case; /**< List of all cases, including default. */
546 case_label_statement_t *default_label; /**< The default label if existent. */
549 struct goto_statement_t {
550 statement_base_t base;
551 label_t *label; /**< The destination label. */
552 goto_statement_t *next; /**< links all goto statements of a function */
555 struct computed_goto_statement_t {
556 statement_base_t base;
557 expression_t *expression; /**< The expression for the computed goto. */
560 struct case_label_statement_t {
561 statement_base_t base;
562 expression_t *expression; /**< The case label expression, NULL for default label. */
563 expression_t *end_range; /**< For GNUC case a .. b: the end range expression, NULL else. */
564 case_label_statement_t *next; /**< link to the next case label in switch */
565 statement_t *statement;
566 long first_case; /**< The folded value of expression. */
567 long last_case; /**< The folded value of end_range. */
568 bool is_bad; /**< If set marked as bad to suppress warnings. */
569 bool is_empty_range; /**< If set marked this as an empty range. */
573 struct label_statement_t {
574 statement_base_t base;
576 statement_t *statement;
577 label_statement_t *next; /**< links all label statements of a function */
580 struct expression_statement_t {
581 statement_base_t base;
582 expression_t *expression;
585 struct while_statement_t {
586 statement_base_t base;
588 expression_t *condition;
592 struct do_while_statement_t {
593 statement_base_t base;
595 expression_t *condition;
599 struct for_statement_t {
600 statement_base_t base;
602 expression_t *initialisation;
603 expression_t *condition;
606 bool condition_reachable:1;
607 bool step_reachable:1;
610 struct asm_argument_t {
611 string_t constraints;
612 expression_t *expression;
614 asm_argument_t *next;
617 struct asm_clobber_t {
622 struct asm_statement_t {
623 statement_base_t base;
625 asm_argument_t *inputs;
626 asm_argument_t *outputs;
627 asm_clobber_t *clobbers;
631 struct ms_try_statement_t {
632 statement_base_t base;
633 statement_t *try_statement;
634 expression_t *except_expression; /**< non-null for except, NULL for finally */
635 statement_t *final_statement;
638 struct leave_statement_t {
639 statement_base_t base;
643 statement_kind_t kind;
644 statement_base_t base;
645 return_statement_t returns;
646 compound_statement_t compound;
647 declaration_statement_t declaration;
649 switch_statement_t switchs;
650 computed_goto_statement_t computed_goto;
651 goto_statement_t gotos;
652 case_label_statement_t case_label;
653 label_statement_t label;
654 expression_statement_t expression;
655 while_statement_t whiles;
656 do_while_statement_t do_while;
657 for_statement_t fors;
658 asm_statement_t asms;
659 ms_try_statement_t ms_try;
660 leave_statement_t leave;
663 struct translation_unit_t {
665 statement_t *global_asm;
669 * Allocate an AST node with given size and
670 * initialize all fields with zero.
672 static inline void *allocate_ast_zero(size_t size)
674 return memset(obstack_alloc(&ast_obstack, size), 0, size);
677 /** If set, implicit casts are printed. */
678 extern bool print_implicit_casts;
679 /** If set parenthesis are printed to indicate operator precedence. */
680 extern bool print_parenthesis;