2 * This file is part of cparser.
3 * Copyright (C) 2012 Matthias Braun <matze@braunis.de>
8 #include <libfirm/firm_types.h>
18 /** The AST obstack contains all data that must stay in the AST. */
19 extern struct obstack ast_obstack;
22 * Operator precedence classes
24 typedef enum precedence_t {
26 PREC_EXPRESSION, /* , left to right */
27 PREC_ASSIGNMENT, /* = += -= *= /= %= <<= >>= &= ^= |= right to left */
28 PREC_CONDITIONAL, /* ?: right to left */
29 PREC_LOGICAL_OR, /* || left to right */
30 PREC_LOGICAL_AND, /* && left to right */
31 PREC_OR, /* | left to right */
32 PREC_XOR, /* ^ left to right */
33 PREC_AND, /* & left to right */
34 PREC_EQUALITY, /* == != left to right */
35 PREC_RELATIONAL, /* < <= > >= left to right */
36 PREC_SHIFT, /* << >> left to right */
37 PREC_ADDITIVE, /* + - left to right */
38 PREC_MULTIPLICATIVE, /* * / % left to right */
39 PREC_CAST, /* (type) right to left */
40 PREC_UNARY, /* ! ~ ++ -- + - * & sizeof right to left */
41 PREC_POSTFIX, /* () [] -> . left to right */
49 typedef enum expression_kind_t {
55 EXPR_LITERAL_FLOATINGPOINT,
56 EXPR_LITERAL_CHARACTER,
57 EXPR_LITERAL_MS_NOOP, /**< MS __noop extension */
59 EXPR_COMPOUND_LITERAL,
69 EXPR_BUILTIN_CONSTANT_P,
70 EXPR_BUILTIN_TYPES_COMPATIBLE_P,
76 EXPR_LABEL_ADDRESS, /**< GCC extension &&label operator */
79 EXPR_UNARY_NEGATE = EXPR_UNARY_FIRST,
81 EXPR_UNARY_COMPLEMENT,
83 EXPR_UNARY_DEREFERENCE,
84 EXPR_UNARY_TAKE_ADDRESS,
85 EXPR_UNARY_POSTFIX_INCREMENT,
86 EXPR_UNARY_POSTFIX_DECREMENT,
87 EXPR_UNARY_PREFIX_INCREMENT,
88 EXPR_UNARY_PREFIX_DECREMENT,
90 EXPR_UNARY_ASSUME, /**< MS __assume() */
92 EXPR_UNARY_DELETE_ARRAY,
96 EXPR_UNARY_LAST = EXPR_UNARY_IMAG,
99 EXPR_BINARY_ADD = EXPR_BINARY_FIRST,
105 EXPR_BINARY_NOTEQUAL,
107 EXPR_BINARY_LESSEQUAL,
109 EXPR_BINARY_GREATEREQUAL,
110 EXPR_BINARY_BITWISE_AND,
111 EXPR_BINARY_BITWISE_OR,
112 EXPR_BINARY_BITWISE_XOR,
113 EXPR_BINARY_LOGICAL_AND,
114 EXPR_BINARY_LOGICAL_OR,
115 EXPR_BINARY_SHIFTLEFT,
116 EXPR_BINARY_SHIFTRIGHT,
118 EXPR_BINARY_MUL_ASSIGN,
119 EXPR_BINARY_DIV_ASSIGN,
120 EXPR_BINARY_MOD_ASSIGN,
121 EXPR_BINARY_ADD_ASSIGN,
122 EXPR_BINARY_SUB_ASSIGN,
123 EXPR_BINARY_SHIFTLEFT_ASSIGN,
124 EXPR_BINARY_SHIFTRIGHT_ASSIGN,
125 EXPR_BINARY_BITWISE_AND_ASSIGN,
126 EXPR_BINARY_BITWISE_XOR_ASSIGN,
127 EXPR_BINARY_BITWISE_OR_ASSIGN,
130 EXPR_BINARY_ISGREATER,
131 EXPR_BINARY_ISGREATEREQUAL,
133 EXPR_BINARY_ISLESSEQUAL,
134 EXPR_BINARY_ISLESSGREATER,
135 EXPR_BINARY_ISUNORDERED,
136 EXPR_BINARY_LAST = EXPR_BINARY_ISUNORDERED,
139 typedef enum funcname_kind_t {
140 FUNCNAME_FUNCTION, /**< C99 __func__, older __FUNCTION__ */
141 FUNCNAME_PRETTY_FUNCTION, /**< GNUC __PRETTY_FUNCTION__ */
142 FUNCNAME_FUNCSIG, /**< MS __FUNCSIG__ */
143 FUNCNAME_FUNCDNAME /**< MS __FUNCDNAME__ */
146 /* convenience macros */
147 #define EXPR_BINARY_CASES \
149 case EXPR_BINARY_SUB: \
150 case EXPR_BINARY_MUL: \
151 case EXPR_BINARY_DIV: \
152 case EXPR_BINARY_MOD: \
153 case EXPR_BINARY_EQUAL: \
154 case EXPR_BINARY_NOTEQUAL: \
155 case EXPR_BINARY_LESS: \
156 case EXPR_BINARY_LESSEQUAL: \
157 case EXPR_BINARY_GREATER: \
158 case EXPR_BINARY_GREATEREQUAL: \
159 case EXPR_BINARY_BITWISE_AND: \
160 case EXPR_BINARY_BITWISE_OR: \
161 case EXPR_BINARY_BITWISE_XOR: \
162 case EXPR_BINARY_LOGICAL_AND: \
163 case EXPR_BINARY_LOGICAL_OR: \
164 case EXPR_BINARY_SHIFTLEFT: \
165 case EXPR_BINARY_SHIFTRIGHT: \
166 case EXPR_BINARY_ASSIGN: \
167 case EXPR_BINARY_MUL_ASSIGN: \
168 case EXPR_BINARY_DIV_ASSIGN: \
169 case EXPR_BINARY_MOD_ASSIGN: \
170 case EXPR_BINARY_ADD_ASSIGN: \
171 case EXPR_BINARY_SUB_ASSIGN: \
172 case EXPR_BINARY_SHIFTLEFT_ASSIGN: \
173 case EXPR_BINARY_SHIFTRIGHT_ASSIGN: \
174 case EXPR_BINARY_BITWISE_AND_ASSIGN: \
175 case EXPR_BINARY_BITWISE_XOR_ASSIGN: \
176 case EXPR_BINARY_BITWISE_OR_ASSIGN: \
177 case EXPR_BINARY_COMMA: \
178 case EXPR_BINARY_ISGREATER: \
179 case EXPR_BINARY_ISGREATEREQUAL: \
180 case EXPR_BINARY_ISLESS: \
181 case EXPR_BINARY_ISLESSEQUAL: \
182 case EXPR_BINARY_ISLESSGREATER: \
183 case EXPR_BINARY_ISUNORDERED
186 * unary expression with mandatory operand
188 #define EXPR_UNARY_CASES_MANDATORY \
190 case EXPR_UNARY_PLUS: \
191 case EXPR_UNARY_COMPLEMENT: \
192 case EXPR_UNARY_NOT: \
193 case EXPR_UNARY_DEREFERENCE: \
194 case EXPR_UNARY_TAKE_ADDRESS: \
195 case EXPR_UNARY_POSTFIX_INCREMENT: \
196 case EXPR_UNARY_POSTFIX_DECREMENT: \
197 case EXPR_UNARY_PREFIX_INCREMENT: \
198 case EXPR_UNARY_PREFIX_DECREMENT: \
199 case EXPR_UNARY_CAST: \
200 case EXPR_UNARY_ASSUME: \
201 case EXPR_UNARY_DELETE: \
202 case EXPR_UNARY_DELETE_ARRAY: \
203 case EXPR_UNARY_IMAG: \
207 * unary expression with optional operand
209 #define EXPR_UNARY_CASES_OPTIONAL \
212 #define EXPR_UNARY_CASES \
213 EXPR_UNARY_CASES_MANDATORY: \
214 case EXPR_UNARY_CASES_OPTIONAL
216 #define EXPR_LITERAL_CASES \
217 EXPR_LITERAL_BOOLEAN: \
218 case EXPR_LITERAL_INTEGER: \
219 case EXPR_LITERAL_FLOATINGPOINT: \
220 case EXPR_LITERAL_MS_NOOP
223 * The base class of every expression.
225 struct expression_base_t {
226 expression_kind_t kind; /**< The expression kind. */
227 type_t *type; /**< The type of the expression. */
228 position_t pos; /**< The source position of this expression. */
229 bool parenthesized : 1;
231 bool transformed : 1; /**< Set if this expression was transformed. */
233 bool implicit : 1; /**< compiler generated expression.
234 Examples: select into anonymous structs,
239 * integer, float and boolean constants
241 struct literal_expression_t {
242 expression_base_t base;
244 char const *suffix; /**< Start of the suffix in value. */
247 ir_tarval *target_value;
251 * string and character literals
253 struct string_literal_expression_t {
254 expression_base_t base;
258 struct funcname_expression_t {
259 expression_base_t base;
260 funcname_kind_t kind;
263 struct compound_literal_expression_t {
264 expression_base_t base;
266 initializer_t *initializer;
270 struct builtin_constant_expression_t {
271 expression_base_t base;
275 struct builtin_types_compatible_expression_t {
276 expression_base_t base;
281 struct reference_expression_t {
282 expression_base_t base;
287 * An argument of a call.
289 struct call_argument_t {
290 expression_t *expression; /**< The expression which value is transmitted. */
291 call_argument_t *next; /**< Links to the next argument of this call. */
295 struct call_expression_t {
296 expression_base_t base;
297 expression_t *function; /**< The address of the function to call. */
298 call_argument_t *arguments; /**< List of arguments of this call. */
302 struct unary_expression_t {
303 expression_base_t base;
304 expression_t *value; /**< The unary operand. */
307 struct binary_expression_t {
308 expression_base_t base;
313 struct select_expression_t {
314 expression_base_t base;
315 expression_t *compound;
316 entity_t *compound_entry;
319 struct array_access_expression_t {
320 expression_base_t base;
321 expression_t *array_ref; /**< the referenced array */
322 expression_t *index; /**< the index used */
323 bool flipped; /**< True if index/ref was written in a 5[a] way */
326 struct typeprop_expression_t {
327 expression_base_t base;
329 expression_t *tp_expression;
332 struct designator_t {
334 symbol_t *symbol; /**< the symbol if any */
335 expression_t *array_index; /**< the array index if any */
336 expression_t *range_last; /**< last index of a range initializer, if any */
340 struct offsetof_expression_t {
341 expression_base_t base;
343 designator_t *designator;
346 struct va_start_expression_t {
347 expression_base_t base;
349 expression_t *parameter;
352 struct va_arg_expression_t {
353 expression_base_t base;
357 struct va_copy_expression_t {
358 expression_base_t base;
359 expression_t *dst; /**< destination argument */
360 expression_t *src; /**< source argument */
363 struct conditional_expression_t {
364 expression_base_t base;
365 expression_t *condition;
366 expression_t *true_expression;
367 expression_t *false_expression;
370 struct statement_expression_t {
371 expression_base_t base;
372 statement_t *statement;
375 struct classify_type_expression_t {
376 expression_base_t base;
377 expression_t *type_expression;
380 struct label_address_expression_t {
381 expression_base_t base;
386 expression_kind_t kind;
387 expression_base_t base;
388 literal_expression_t literal;
389 string_literal_expression_t string_literal;
390 funcname_expression_t funcname;
391 compound_literal_expression_t compound_literal;
392 builtin_constant_expression_t builtin_constant;
393 builtin_types_compatible_expression_t builtin_types_compatible;
394 reference_expression_t reference;
395 call_expression_t call;
396 unary_expression_t unary;
397 binary_expression_t binary;
398 select_expression_t select;
399 array_access_expression_t array_access;
400 typeprop_expression_t typeprop;
401 offsetof_expression_t offsetofe;
402 va_start_expression_t va_starte;
403 va_arg_expression_t va_arge;
404 va_copy_expression_t va_copye;
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_DESIGNATOR
416 } initializer_kind_t;
418 struct initializer_base_t {
419 initializer_kind_t kind;
422 struct initializer_value_t {
423 initializer_base_t base;
427 struct initializer_list_t {
428 initializer_base_t base;
430 initializer_t *initializers[];
433 struct initializer_designator_t {
434 initializer_base_t base;
435 designator_t *designator;
438 union initializer_t {
439 initializer_kind_t kind;
440 initializer_base_t base;
441 initializer_value_t value;
442 initializer_list_t list;
443 initializer_designator_t designator;
446 static inline string_literal_expression_t const *get_init_string(initializer_t const *const init)
448 assert(init->kind == INITIALIZER_STRING);
449 assert(init->value.value->kind == EXPR_STRING_LITERAL);
450 return &init->value.value->string_literal;
454 * The statement kinds.
456 typedef enum statement_kind_t {
461 STATEMENT_DECLARATION,
464 STATEMENT_EXPRESSION,
467 STATEMENT_COMPUTED_GOTO,
470 STATEMENT_CASE_LABEL,
474 STATEMENT_MS_TRY, /**< MS __try/__finally or __try/__except */
475 STATEMENT_LEAVE /**< MS __leave */
479 * The base class of every statement.
481 struct statement_base_t {
482 statement_kind_t kind;
483 statement_t *next; /**< Point to the next statement in a compound statement. */
485 statement_t *parent; /**< The Parent statement that controls the execution. */
486 bool reachable; /**< True, if this statement is reachable. */
492 struct return_statement_t {
493 statement_base_t base;
494 expression_t *value; /**< The return value if any. */
497 struct compound_statement_t {
498 statement_base_t base;
499 statement_t *statements;
501 bool stmt_expr; /**< True if this compound statement is a statement expression. */
504 struct declaration_statement_t {
505 statement_base_t base;
506 entity_t *declarations_begin;
507 entity_t *declarations_end;
510 struct if_statement_t {
511 statement_base_t base;
513 expression_t *condition;
514 statement_t *true_statement;
515 statement_t *false_statement;
518 struct switch_statement_t {
519 statement_base_t base;
521 expression_t *expression;
523 case_label_statement_t *first_case, *last_case; /**< List of all cases, including default. */
524 case_label_statement_t *default_label; /**< The default label if existent. */
527 struct goto_statement_t {
528 statement_base_t base;
529 label_t *label; /**< The destination label. */
530 goto_statement_t *next; /**< links all goto statements of a function */
533 struct computed_goto_statement_t {
534 statement_base_t base;
535 expression_t *expression; /**< The expression for the computed goto. */
538 struct case_label_statement_t {
539 statement_base_t base;
540 expression_t *expression; /**< The case label expression, NULL for default label. */
541 expression_t *end_range; /**< For GNUC case a .. b: the end range expression, NULL else. */
542 case_label_statement_t *next; /**< link to the next case label in switch */
543 statement_t *statement;
544 ir_tarval *first_case;
545 ir_tarval *last_case;
546 bool is_bad; /**< If set marked as bad to suppress warnings. */
547 bool is_empty_range; /**< If set marked this as an empty range. */
551 struct label_statement_t {
552 statement_base_t base;
554 statement_t *statement;
555 label_statement_t *next; /**< links all label statements of a function */
558 struct expression_statement_t {
559 statement_base_t base;
560 expression_t *expression;
563 struct do_while_statement_t {
564 statement_base_t base;
566 expression_t *condition;
570 struct for_statement_t {
571 statement_base_t base;
573 expression_t *initialisation;
574 expression_t *condition;
577 bool condition_reachable:1;
578 bool step_reachable:1;
581 struct asm_argument_t {
582 string_t constraints;
583 expression_t *expression;
585 asm_argument_t *next;
588 struct asm_clobber_t {
598 struct asm_statement_t {
599 statement_base_t base;
601 asm_argument_t *inputs;
602 asm_argument_t *outputs;
603 asm_clobber_t *clobbers;
608 struct ms_try_statement_t {
609 statement_base_t base;
610 statement_t *try_statement;
611 expression_t *except_expression; /**< non-null for except, NULL for finally */
612 statement_t *final_statement;
615 struct leave_statement_t {
616 statement_base_t base;
620 statement_kind_t kind;
621 statement_base_t base;
622 return_statement_t returns;
623 compound_statement_t compound;
624 declaration_statement_t declaration;
626 switch_statement_t switchs;
627 computed_goto_statement_t computed_goto;
628 goto_statement_t gotos;
629 case_label_statement_t case_label;
630 label_statement_t label;
631 expression_statement_t expression;
632 do_while_statement_t do_while;
633 for_statement_t fors;
634 asm_statement_t asms;
635 ms_try_statement_t ms_try;
636 leave_statement_t leave;
639 struct translation_unit_t {
641 statement_t *global_asm;
645 * Allocate an AST node with given size and
646 * initialize all fields with zero.
648 static inline void *allocate_ast_zero(size_t size)
650 return memset(obstack_alloc(&ast_obstack, size), 0, size);
653 /** If set, implicit casts are printed. */
654 extern bool print_implicit_casts;
655 /** If set parenthesis are printed to indicate operator precedence. */
656 extern bool print_parenthesis;