2 * This file is part of cparser.
3 * Copyright (C) 2012 Matthias Braun <matze@braunis.de>
8 #include <libfirm/firm_types.h>
17 /** The AST obstack contains all data that must stay in the AST. */
18 extern struct obstack ast_obstack;
21 * Operator precedence classes
23 typedef enum precedence_t {
25 PREC_EXPRESSION, /* , left to right */
26 PREC_ASSIGNMENT, /* = += -= *= /= %= <<= >>= &= ^= |= right to left */
27 PREC_CONDITIONAL, /* ?: right to left */
28 PREC_LOGICAL_OR, /* || left to right */
29 PREC_LOGICAL_AND, /* && left to right */
30 PREC_OR, /* | left to right */
31 PREC_XOR, /* ^ left to right */
32 PREC_AND, /* & left to right */
33 PREC_EQUALITY, /* == != left to right */
34 PREC_RELATIONAL, /* < <= > >= left to right */
35 PREC_SHIFT, /* << >> left to right */
36 PREC_ADDITIVE, /* + - left to right */
37 PREC_MULTIPLICATIVE, /* * / % left to right */
38 PREC_CAST, /* (type) right to left */
39 PREC_UNARY, /* ! ~ ++ -- + - * & sizeof right to left */
40 PREC_POSTFIX, /* () [] -> . left to right */
48 typedef enum expression_kind_t {
54 EXPR_LITERAL_FLOATINGPOINT,
55 EXPR_LITERAL_CHARACTER,
56 EXPR_LITERAL_MS_NOOP, /**< MS __noop extension */
58 EXPR_COMPOUND_LITERAL,
68 EXPR_BUILTIN_CONSTANT_P,
69 EXPR_BUILTIN_TYPES_COMPATIBLE_P,
75 EXPR_LABEL_ADDRESS, /**< GCC extension &&label operator */
78 EXPR_UNARY_NEGATE = EXPR_UNARY_FIRST,
80 EXPR_UNARY_COMPLEMENT,
82 EXPR_UNARY_DEREFERENCE,
83 EXPR_UNARY_TAKE_ADDRESS,
84 EXPR_UNARY_POSTFIX_INCREMENT,
85 EXPR_UNARY_POSTFIX_DECREMENT,
86 EXPR_UNARY_PREFIX_INCREMENT,
87 EXPR_UNARY_PREFIX_DECREMENT,
89 EXPR_UNARY_ASSUME, /**< MS __assume() */
91 EXPR_UNARY_DELETE_ARRAY,
95 EXPR_UNARY_LAST = EXPR_UNARY_IMAG,
98 EXPR_BINARY_ADD = EXPR_BINARY_FIRST,
104 EXPR_BINARY_NOTEQUAL,
106 EXPR_BINARY_LESSEQUAL,
108 EXPR_BINARY_GREATEREQUAL,
109 EXPR_BINARY_BITWISE_AND,
110 EXPR_BINARY_BITWISE_OR,
111 EXPR_BINARY_BITWISE_XOR,
112 EXPR_BINARY_LOGICAL_AND,
113 EXPR_BINARY_LOGICAL_OR,
114 EXPR_BINARY_SHIFTLEFT,
115 EXPR_BINARY_SHIFTRIGHT,
117 EXPR_BINARY_MUL_ASSIGN,
118 EXPR_BINARY_DIV_ASSIGN,
119 EXPR_BINARY_MOD_ASSIGN,
120 EXPR_BINARY_ADD_ASSIGN,
121 EXPR_BINARY_SUB_ASSIGN,
122 EXPR_BINARY_SHIFTLEFT_ASSIGN,
123 EXPR_BINARY_SHIFTRIGHT_ASSIGN,
124 EXPR_BINARY_BITWISE_AND_ASSIGN,
125 EXPR_BINARY_BITWISE_XOR_ASSIGN,
126 EXPR_BINARY_BITWISE_OR_ASSIGN,
129 EXPR_BINARY_ISGREATER,
130 EXPR_BINARY_ISGREATEREQUAL,
132 EXPR_BINARY_ISLESSEQUAL,
133 EXPR_BINARY_ISLESSGREATER,
134 EXPR_BINARY_ISUNORDERED,
135 EXPR_BINARY_LAST = EXPR_BINARY_ISUNORDERED,
138 typedef enum funcname_kind_t {
139 FUNCNAME_FUNCTION, /**< C99 __func__, older __FUNCTION__ */
140 FUNCNAME_PRETTY_FUNCTION, /**< GNUC __PRETTY_FUNCTION__ */
141 FUNCNAME_FUNCSIG, /**< MS __FUNCSIG__ */
142 FUNCNAME_FUNCDNAME /**< MS __FUNCDNAME__ */
145 /* convenience macros */
146 #define EXPR_BINARY_CASES \
148 case EXPR_BINARY_SUB: \
149 case EXPR_BINARY_MUL: \
150 case EXPR_BINARY_DIV: \
151 case EXPR_BINARY_MOD: \
152 case EXPR_BINARY_EQUAL: \
153 case EXPR_BINARY_NOTEQUAL: \
154 case EXPR_BINARY_LESS: \
155 case EXPR_BINARY_LESSEQUAL: \
156 case EXPR_BINARY_GREATER: \
157 case EXPR_BINARY_GREATEREQUAL: \
158 case EXPR_BINARY_BITWISE_AND: \
159 case EXPR_BINARY_BITWISE_OR: \
160 case EXPR_BINARY_BITWISE_XOR: \
161 case EXPR_BINARY_LOGICAL_AND: \
162 case EXPR_BINARY_LOGICAL_OR: \
163 case EXPR_BINARY_SHIFTLEFT: \
164 case EXPR_BINARY_SHIFTRIGHT: \
165 case EXPR_BINARY_ASSIGN: \
166 case EXPR_BINARY_MUL_ASSIGN: \
167 case EXPR_BINARY_DIV_ASSIGN: \
168 case EXPR_BINARY_MOD_ASSIGN: \
169 case EXPR_BINARY_ADD_ASSIGN: \
170 case EXPR_BINARY_SUB_ASSIGN: \
171 case EXPR_BINARY_SHIFTLEFT_ASSIGN: \
172 case EXPR_BINARY_SHIFTRIGHT_ASSIGN: \
173 case EXPR_BINARY_BITWISE_AND_ASSIGN: \
174 case EXPR_BINARY_BITWISE_XOR_ASSIGN: \
175 case EXPR_BINARY_BITWISE_OR_ASSIGN: \
176 case EXPR_BINARY_COMMA: \
177 case EXPR_BINARY_ISGREATER: \
178 case EXPR_BINARY_ISGREATEREQUAL: \
179 case EXPR_BINARY_ISLESS: \
180 case EXPR_BINARY_ISLESSEQUAL: \
181 case EXPR_BINARY_ISLESSGREATER: \
182 case EXPR_BINARY_ISUNORDERED
185 * unary expression with mandatory operand
187 #define EXPR_UNARY_CASES_MANDATORY \
189 case EXPR_UNARY_PLUS: \
190 case EXPR_UNARY_COMPLEMENT: \
191 case EXPR_UNARY_NOT: \
192 case EXPR_UNARY_DEREFERENCE: \
193 case EXPR_UNARY_TAKE_ADDRESS: \
194 case EXPR_UNARY_POSTFIX_INCREMENT: \
195 case EXPR_UNARY_POSTFIX_DECREMENT: \
196 case EXPR_UNARY_PREFIX_INCREMENT: \
197 case EXPR_UNARY_PREFIX_DECREMENT: \
198 case EXPR_UNARY_CAST: \
199 case EXPR_UNARY_ASSUME: \
200 case EXPR_UNARY_DELETE: \
201 case EXPR_UNARY_DELETE_ARRAY: \
202 case EXPR_UNARY_IMAG: \
206 * unary expression with optional operand
208 #define EXPR_UNARY_CASES_OPTIONAL \
211 #define EXPR_UNARY_CASES \
212 EXPR_UNARY_CASES_MANDATORY: \
213 case EXPR_UNARY_CASES_OPTIONAL
215 #define EXPR_LITERAL_CASES \
216 EXPR_LITERAL_BOOLEAN: \
217 case EXPR_LITERAL_INTEGER: \
218 case EXPR_LITERAL_FLOATINGPOINT: \
219 case EXPR_LITERAL_MS_NOOP
222 * The base class of every expression.
224 struct expression_base_t {
225 expression_kind_t kind; /**< The expression kind. */
226 type_t *type; /**< The type of the expression. */
227 position_t pos; /**< The source position of this expression. */
228 bool parenthesized : 1;
230 bool transformed : 1; /**< Set if this expression was transformed. */
232 bool implicit : 1; /**< compiler generated expression.
233 Examples: select into anonymous structs,
238 * integer, float and boolean constants
240 struct literal_expression_t {
241 expression_base_t base;
243 char const *suffix; /**< Start of the suffix in value. */
246 ir_tarval *target_value;
250 * string and character literals
252 struct string_literal_expression_t {
253 expression_base_t base;
257 struct funcname_expression_t {
258 expression_base_t base;
259 funcname_kind_t kind;
262 struct compound_literal_expression_t {
263 expression_base_t base;
265 initializer_t *initializer;
269 struct builtin_constant_expression_t {
270 expression_base_t base;
274 struct builtin_types_compatible_expression_t {
275 expression_base_t base;
280 struct reference_expression_t {
281 expression_base_t base;
286 * An argument of a call.
288 struct call_argument_t {
289 expression_t *expression; /**< The expression which value is transmitted. */
290 call_argument_t *next; /**< Links to the next argument of this call. */
294 struct call_expression_t {
295 expression_base_t base;
296 expression_t *function; /**< The address of the function to call. */
297 call_argument_t *arguments; /**< List of arguments of this call. */
301 struct unary_expression_t {
302 expression_base_t base;
303 expression_t *value; /**< The unary operand. */
306 struct binary_expression_t {
307 expression_base_t base;
312 struct select_expression_t {
313 expression_base_t base;
314 expression_t *compound;
315 entity_t *compound_entry;
318 struct array_access_expression_t {
319 expression_base_t base;
320 expression_t *array_ref; /**< the referenced array */
321 expression_t *index; /**< the index used */
322 bool flipped; /**< True if index/ref was written in a 5[a] way */
325 struct typeprop_expression_t {
326 expression_base_t base;
328 expression_t *tp_expression;
331 struct designator_t {
333 symbol_t *symbol; /**< the symbol if any */
334 expression_t *array_index; /**< the array index if any */
335 expression_t *range_last; /**< last index of a range initializer, if any */
339 struct offsetof_expression_t {
340 expression_base_t base;
342 designator_t *designator;
345 struct va_start_expression_t {
346 expression_base_t base;
348 expression_t *parameter;
351 struct va_arg_expression_t {
352 expression_base_t base;
356 struct va_copy_expression_t {
357 expression_base_t base;
358 expression_t *dst; /**< destination argument */
359 expression_t *src; /**< source argument */
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 literal_expression_t literal;
388 string_literal_expression_t string_literal;
389 funcname_expression_t funcname;
390 compound_literal_expression_t compound_literal;
391 builtin_constant_expression_t builtin_constant;
392 builtin_types_compatible_expression_t builtin_types_compatible;
393 reference_expression_t reference;
394 call_expression_t call;
395 unary_expression_t unary;
396 binary_expression_t binary;
397 select_expression_t select;
398 array_access_expression_t array_access;
399 typeprop_expression_t typeprop;
400 offsetof_expression_t offsetofe;
401 va_start_expression_t va_starte;
402 va_arg_expression_t va_arge;
403 va_copy_expression_t va_copye;
404 conditional_expression_t conditional;
405 statement_expression_t statement;
406 classify_type_expression_t classify_type;
407 label_address_expression_t label_address;
410 typedef enum initializer_kind_t {
414 INITIALIZER_DESIGNATOR
415 } initializer_kind_t;
417 struct initializer_base_t {
418 initializer_kind_t kind;
421 struct initializer_value_t {
422 initializer_base_t base;
426 struct initializer_list_t {
427 initializer_base_t base;
429 initializer_t *initializers[];
432 struct initializer_designator_t {
433 initializer_base_t base;
434 designator_t *designator;
437 union initializer_t {
438 initializer_kind_t kind;
439 initializer_base_t base;
440 initializer_value_t value;
441 initializer_list_t list;
442 initializer_designator_t designator;
445 static inline string_literal_expression_t const *get_init_string(initializer_t const *const init)
447 assert(init->kind == INITIALIZER_STRING);
448 assert(init->value.value->kind == EXPR_STRING_LITERAL);
449 return &init->value.value->string_literal;
453 * The statement kinds.
455 typedef enum statement_kind_t {
460 STATEMENT_DECLARATION,
463 STATEMENT_EXPRESSION,
466 STATEMENT_COMPUTED_GOTO,
469 STATEMENT_CASE_LABEL,
473 STATEMENT_MS_TRY, /**< MS __try/__finally or __try/__except */
474 STATEMENT_LEAVE /**< MS __leave */
478 * The base class of every statement.
480 struct statement_base_t {
481 statement_kind_t kind;
482 statement_t *next; /**< Point to the next statement in a compound statement. */
484 statement_t *parent; /**< The Parent statement that controls the execution. */
485 bool reachable; /**< True, if this statement is reachable. */
491 struct return_statement_t {
492 statement_base_t base;
493 expression_t *value; /**< The return value if any. */
496 struct compound_statement_t {
497 statement_base_t base;
498 statement_t *statements;
500 bool stmt_expr; /**< True if this compound statement is a statement expression. */
503 struct declaration_statement_t {
504 statement_base_t base;
505 entity_t *declarations_begin;
506 entity_t *declarations_end;
509 struct if_statement_t {
510 statement_base_t base;
512 expression_t *condition;
513 statement_t *true_statement;
514 statement_t *false_statement;
517 struct switch_statement_t {
518 statement_base_t base;
520 expression_t *expression;
522 case_label_statement_t *first_case, *last_case; /**< List of all cases, including default. */
523 case_label_statement_t *default_label; /**< The default label if existent. */
526 struct goto_statement_t {
527 statement_base_t base;
528 label_t *label; /**< The destination label. */
529 goto_statement_t *next; /**< links all goto statements of a function */
532 struct computed_goto_statement_t {
533 statement_base_t base;
534 expression_t *expression; /**< The expression for the computed goto. */
537 struct case_label_statement_t {
538 statement_base_t base;
539 expression_t *expression; /**< The case label expression, NULL for default label. */
540 expression_t *end_range; /**< For GNUC case a .. b: the end range expression, NULL else. */
541 case_label_statement_t *next; /**< link to the next case label in switch */
542 statement_t *statement;
543 ir_tarval *first_case;
544 ir_tarval *last_case;
545 bool is_bad; /**< If set marked as bad to suppress warnings. */
546 bool is_empty_range; /**< If set marked this as an empty range. */
550 struct label_statement_t {
551 statement_base_t base;
553 statement_t *statement;
554 label_statement_t *next; /**< links all label statements of a function */
557 struct expression_statement_t {
558 statement_base_t base;
559 expression_t *expression;
562 struct do_while_statement_t {
563 statement_base_t base;
565 expression_t *condition;
569 struct for_statement_t {
570 statement_base_t base;
572 expression_t *initialisation;
573 expression_t *condition;
576 bool condition_reachable:1;
577 bool step_reachable:1;
580 struct asm_argument_t {
581 string_t constraints;
582 expression_t *expression;
584 asm_argument_t *next;
587 struct asm_clobber_t {
597 struct asm_statement_t {
598 statement_base_t base;
600 asm_argument_t *inputs;
601 asm_argument_t *outputs;
602 asm_clobber_t *clobbers;
607 struct ms_try_statement_t {
608 statement_base_t base;
609 statement_t *try_statement;
610 expression_t *except_expression; /**< non-null for except, NULL for finally */
611 statement_t *final_statement;
614 struct leave_statement_t {
615 statement_base_t base;
619 statement_kind_t kind;
620 statement_base_t base;
621 return_statement_t returns;
622 compound_statement_t compound;
623 declaration_statement_t declaration;
625 switch_statement_t switchs;
626 computed_goto_statement_t computed_goto;
627 goto_statement_t gotos;
628 case_label_statement_t case_label;
629 label_statement_t label;
630 expression_statement_t expression;
631 do_while_statement_t do_while;
632 for_statement_t fors;
633 asm_statement_t asms;
634 ms_try_statement_t ms_try;
635 leave_statement_t leave;
638 struct translation_unit_t {
640 statement_t *global_asm;
644 * Allocate an AST node with given size and
645 * initialize all fields with zero.
647 static inline void *allocate_ast_zero(size_t size)
649 return memset(obstack_alloc(&ast_obstack, size), 0, size);
652 /** If set, implicit casts are printed. */
653 extern bool print_implicit_casts;
654 /** If set parenthesis are printed to indicate operator precedence. */
655 extern bool print_parenthesis;