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 {
68 EXPR_REFERENCE_ENUM_VALUE,
71 EXPR_LITERAL_INTEGER_OCTAL,
72 EXPR_LITERAL_INTEGER_HEXADECIMAL,
73 EXPR_LITERAL_FLOATINGPOINT,
74 EXPR_LITERAL_FLOATINGPOINT_HEXADECIMAL,
75 EXPR_LITERAL_CHARACTER,
76 EXPR_LITERAL_WIDE_CHARACTER,
77 EXPR_LITERAL_MS_NOOP, /**< MS __noop extension */
79 EXPR_WIDE_STRING_LITERAL,
80 EXPR_COMPOUND_LITERAL,
90 EXPR_BUILTIN_CONSTANT_P,
91 EXPR_BUILTIN_TYPES_COMPATIBLE_P,
97 EXPR_LABEL_ADDRESS, /**< GCC extension &&label operator */
100 EXPR_UNARY_NEGATE = EXPR_UNARY_FIRST,
102 EXPR_UNARY_BITWISE_NEGATE,
104 EXPR_UNARY_DEREFERENCE,
105 EXPR_UNARY_TAKE_ADDRESS,
106 EXPR_UNARY_POSTFIX_INCREMENT,
107 EXPR_UNARY_POSTFIX_DECREMENT,
108 EXPR_UNARY_PREFIX_INCREMENT,
109 EXPR_UNARY_PREFIX_DECREMENT,
111 EXPR_UNARY_CAST_IMPLICIT, /**< compiler generated cast */
112 EXPR_UNARY_ASSUME, /**< MS __assume() */
114 EXPR_UNARY_DELETE_ARRAY,
116 EXPR_UNARY_LAST = EXPR_UNARY_THROW,
119 EXPR_BINARY_ADD = EXPR_BINARY_FIRST,
125 EXPR_BINARY_NOTEQUAL,
127 EXPR_BINARY_LESSEQUAL,
129 EXPR_BINARY_GREATEREQUAL,
130 EXPR_BINARY_BITWISE_AND,
131 EXPR_BINARY_BITWISE_OR,
132 EXPR_BINARY_BITWISE_XOR,
133 EXPR_BINARY_LOGICAL_AND,
134 EXPR_BINARY_LOGICAL_OR,
135 EXPR_BINARY_SHIFTLEFT,
136 EXPR_BINARY_SHIFTRIGHT,
138 EXPR_BINARY_MUL_ASSIGN,
139 EXPR_BINARY_DIV_ASSIGN,
140 EXPR_BINARY_MOD_ASSIGN,
141 EXPR_BINARY_ADD_ASSIGN,
142 EXPR_BINARY_SUB_ASSIGN,
143 EXPR_BINARY_SHIFTLEFT_ASSIGN,
144 EXPR_BINARY_SHIFTRIGHT_ASSIGN,
145 EXPR_BINARY_BITWISE_AND_ASSIGN,
146 EXPR_BINARY_BITWISE_XOR_ASSIGN,
147 EXPR_BINARY_BITWISE_OR_ASSIGN,
150 EXPR_BINARY_ISGREATER,
151 EXPR_BINARY_ISGREATEREQUAL,
153 EXPR_BINARY_ISLESSEQUAL,
154 EXPR_BINARY_ISLESSGREATER,
155 EXPR_BINARY_ISUNORDERED,
156 EXPR_BINARY_LAST = EXPR_BINARY_ISUNORDERED,
159 typedef enum funcname_kind_t {
160 FUNCNAME_FUNCTION, /**< C99 __func__, older __FUNCTION__ */
161 FUNCNAME_PRETTY_FUNCTION, /**< GNUC __PRETTY_FUNCTION__ */
162 FUNCNAME_FUNCSIG, /**< MS __FUNCSIG__ */
163 FUNCNAME_FUNCDNAME /**< MS __FUNCDNAME__ */
166 /* convenience macros */
167 #define EXPR_BINARY_CASES \
168 case EXPR_BINARY_ADD: \
169 case EXPR_BINARY_SUB: \
170 case EXPR_BINARY_MUL: \
171 case EXPR_BINARY_DIV: \
172 case EXPR_BINARY_MOD: \
173 case EXPR_BINARY_EQUAL: \
174 case EXPR_BINARY_NOTEQUAL: \
175 case EXPR_BINARY_LESS: \
176 case EXPR_BINARY_LESSEQUAL: \
177 case EXPR_BINARY_GREATER: \
178 case EXPR_BINARY_GREATEREQUAL: \
179 case EXPR_BINARY_BITWISE_AND: \
180 case EXPR_BINARY_BITWISE_OR: \
181 case EXPR_BINARY_BITWISE_XOR: \
182 case EXPR_BINARY_LOGICAL_AND: \
183 case EXPR_BINARY_LOGICAL_OR: \
184 case EXPR_BINARY_SHIFTLEFT: \
185 case EXPR_BINARY_SHIFTRIGHT: \
186 case EXPR_BINARY_ASSIGN: \
187 case EXPR_BINARY_MUL_ASSIGN: \
188 case EXPR_BINARY_DIV_ASSIGN: \
189 case EXPR_BINARY_MOD_ASSIGN: \
190 case EXPR_BINARY_ADD_ASSIGN: \
191 case EXPR_BINARY_SUB_ASSIGN: \
192 case EXPR_BINARY_SHIFTLEFT_ASSIGN: \
193 case EXPR_BINARY_SHIFTRIGHT_ASSIGN: \
194 case EXPR_BINARY_BITWISE_AND_ASSIGN: \
195 case EXPR_BINARY_BITWISE_XOR_ASSIGN: \
196 case EXPR_BINARY_BITWISE_OR_ASSIGN: \
197 case EXPR_BINARY_COMMA: \
198 case EXPR_BINARY_ISGREATER: \
199 case EXPR_BINARY_ISGREATEREQUAL: \
200 case EXPR_BINARY_ISLESS: \
201 case EXPR_BINARY_ISLESSEQUAL: \
202 case EXPR_BINARY_ISLESSGREATER: \
203 case EXPR_BINARY_ISUNORDERED:
206 * unary expression with mandatory operand
208 #define EXPR_UNARY_CASES_MANDATORY \
209 case EXPR_UNARY_NEGATE: \
210 case EXPR_UNARY_PLUS: \
211 case EXPR_UNARY_BITWISE_NEGATE: \
212 case EXPR_UNARY_NOT: \
213 case EXPR_UNARY_DEREFERENCE: \
214 case EXPR_UNARY_TAKE_ADDRESS: \
215 case EXPR_UNARY_POSTFIX_INCREMENT: \
216 case EXPR_UNARY_POSTFIX_DECREMENT: \
217 case EXPR_UNARY_PREFIX_INCREMENT: \
218 case EXPR_UNARY_PREFIX_DECREMENT: \
219 case EXPR_UNARY_CAST: \
220 case EXPR_UNARY_CAST_IMPLICIT: \
221 case EXPR_UNARY_ASSUME: \
222 case EXPR_UNARY_DELETE: \
223 case EXPR_UNARY_DELETE_ARRAY:
226 * unary expression with optional operand
228 #define EXPR_UNARY_CASES_OPTIONAL \
229 case EXPR_UNARY_THROW: \
231 #define EXPR_UNARY_CASES \
232 EXPR_UNARY_CASES_MANDATORY \
233 EXPR_UNARY_CASES_OPTIONAL
235 #define EXPR_LITERAL_CASES \
236 case EXPR_LITERAL_BOOLEAN: \
237 case EXPR_LITERAL_INTEGER: \
238 case EXPR_LITERAL_INTEGER_OCTAL: \
239 case EXPR_LITERAL_INTEGER_HEXADECIMAL: \
240 case EXPR_LITERAL_FLOATINGPOINT: \
241 case EXPR_LITERAL_FLOATINGPOINT_HEXADECIMAL: \
242 case EXPR_LITERAL_CHARACTER: \
243 case EXPR_LITERAL_WIDE_CHARACTER: \
244 case EXPR_LITERAL_MS_NOOP:
247 * The base class of every expression.
249 struct expression_base_t {
250 expression_kind_t kind; /**< The expression kind. */
251 type_t *type; /**< The type of the expression. */
252 source_position_t source_position; /**< The source position of this expression. */
253 bool parenthesized : 1;
255 bool transformed : 1; /**< Set if this expression was transformed. */
260 * integer/float constants, character and string literals
262 struct literal_expression_t {
263 expression_base_t base;
268 tarval *target_value;
271 struct string_literal_expression_t {
272 expression_base_t base;
276 struct funcname_expression_t {
277 expression_base_t base;
278 funcname_kind_t kind;
279 string_t value; /**< the value once assigned. */
282 struct compound_literal_expression_t {
283 expression_base_t base;
285 initializer_t *initializer;
288 struct builtin_constant_expression_t {
289 expression_base_t base;
293 struct builtin_types_compatible_expression_t {
294 expression_base_t base;
299 struct reference_expression_t {
300 expression_base_t base;
305 * An argument of a call.
307 struct call_argument_t {
308 expression_t *expression; /**< The expression which value is transmitted. */
309 call_argument_t *next; /**< Links to the next argument of this call. */
313 struct call_expression_t {
314 expression_base_t base;
315 expression_t *function; /**< The address of the function to call. */
316 call_argument_t *arguments; /**< List of arguments of this call. */
320 struct unary_expression_t {
321 expression_base_t base;
322 expression_t *value; /**< The unary operand. */
325 struct binary_expression_t {
326 expression_base_t base;
331 struct select_expression_t {
332 expression_base_t base;
333 expression_t *compound;
334 entity_t *compound_entry;
335 bool implicit : 1; /**< compiler generated select
336 (for anonymous struct/union) */
339 struct array_access_expression_t {
340 expression_base_t base;
341 expression_t *array_ref; /**< the referenced array */
342 expression_t *index; /**< the index used */
343 bool flipped; /**< True if index/ref was written in a 5[a] way */
346 struct typeprop_expression_t {
347 expression_base_t base;
349 expression_t *tp_expression;
352 struct designator_t {
353 source_position_t source_position;
354 symbol_t *symbol; /**< the symbol if any */
355 expression_t *array_index; /**< the array index if any */
359 struct offsetof_expression_t {
360 expression_base_t base;
362 designator_t *designator;
365 struct va_start_expression_t {
366 expression_base_t base;
368 variable_t *parameter;
371 struct va_arg_expression_t {
372 expression_base_t base;
376 struct va_copy_expression_t {
377 expression_base_t base;
378 expression_t *dst; /**< destination argument */
379 expression_t *src; /**< source argument */
382 struct conditional_expression_t {
383 expression_base_t base;
384 expression_t *condition;
385 expression_t *true_expression;
386 expression_t *false_expression;
389 struct statement_expression_t {
390 expression_base_t base;
391 statement_t *statement;
394 struct classify_type_expression_t {
395 expression_base_t base;
396 expression_t *type_expression;
399 struct label_address_expression_t {
400 expression_base_t base;
405 expression_kind_t kind;
406 expression_base_t base;
407 literal_expression_t literal;
408 string_literal_expression_t string_literal;
409 funcname_expression_t funcname;
410 compound_literal_expression_t compound_literal;
411 builtin_constant_expression_t builtin_constant;
412 builtin_types_compatible_expression_t builtin_types_compatible;
413 reference_expression_t reference;
414 call_expression_t call;
415 unary_expression_t unary;
416 binary_expression_t binary;
417 select_expression_t select;
418 array_access_expression_t array_access;
419 typeprop_expression_t typeprop;
420 offsetof_expression_t offsetofe;
421 va_start_expression_t va_starte;
422 va_arg_expression_t va_arge;
423 va_copy_expression_t va_copye;
424 conditional_expression_t conditional;
425 statement_expression_t statement;
426 classify_type_expression_t classify_type;
427 label_address_expression_t label_address;
430 typedef enum initializer_kind_t {
434 INITIALIZER_WIDE_STRING,
435 INITIALIZER_DESIGNATOR
436 } initializer_kind_t;
438 struct initializer_base_t {
439 initializer_kind_t kind;
442 struct initializer_value_t {
443 initializer_base_t base;
447 struct initializer_list_t {
448 initializer_base_t base;
450 initializer_t *initializers[];
453 struct initializer_string_t {
454 initializer_base_t base;
458 struct initializer_wide_string_t {
459 initializer_base_t base;
463 struct initializer_designator_t {
464 initializer_base_t base;
465 designator_t *designator;
468 union initializer_t {
469 initializer_kind_t kind;
470 initializer_base_t base;
471 initializer_value_t value;
472 initializer_list_t list;
473 initializer_string_t string;
474 initializer_wide_string_t wide_string;
475 initializer_designator_t designator;
479 * The statement kinds.
481 typedef enum statement_kind_t {
486 STATEMENT_DECLARATION,
489 STATEMENT_EXPRESSION,
494 STATEMENT_CASE_LABEL,
499 STATEMENT_MS_TRY, /**< MS __try/__finally or __try/__except */
500 STATEMENT_LEAVE /**< MS __leave */
504 * The base class of every statement.
506 struct statement_base_t {
507 statement_kind_t kind;
508 statement_t *next; /**< Point to the next statement in a compound statement. */
509 source_position_t source_position;
510 statement_t *parent; /**< The Parent statement that controls the execution. */
511 bool reachable; /**< True, if this statement is reachable. */
517 struct invalid_statement_t {
518 statement_base_t base;
521 struct empty_statement_t {
522 statement_base_t base;
525 struct return_statement_t {
526 statement_base_t base;
527 expression_t *value; /**< The return value if any. */
530 struct compound_statement_t {
531 statement_base_t base;
532 statement_t *statements;
534 bool stmt_expr; /**< True if this compound statement is a statement expression. */
537 struct declaration_statement_t {
538 statement_base_t base;
539 entity_t *declarations_begin;
540 entity_t *declarations_end;
543 struct if_statement_t {
544 statement_base_t base;
545 expression_t *condition;
546 statement_t *true_statement;
547 statement_t *false_statement;
550 struct switch_statement_t {
551 statement_base_t base;
552 expression_t *expression;
554 case_label_statement_t *first_case, *last_case; /**< List of all cases, including default. */
555 case_label_statement_t *default_label; /**< The default label if existent. */
556 unsigned long default_proj_nr; /**< The Proj-number for the default Proj. */
559 struct goto_statement_t {
560 statement_base_t base;
561 label_t *label; /**< The destination label. */
562 expression_t *expression; /**< The expression for an assigned goto. */
563 goto_statement_t *next; /**< links all goto statements of a function */
566 struct case_label_statement_t {
567 statement_base_t base;
568 expression_t *expression; /**< The case label expression, NULL for default label. */
569 expression_t *end_range; /**< For GNUC case a .. b: the end range expression, NULL else. */
570 case_label_statement_t *next; /**< link to the next case label in switch */
571 statement_t *statement;
572 long first_case; /**< The folded value of expression. */
573 long last_case; /**< The folded value of end_range. */
574 bool is_bad; /**< If set marked as bad to suppress warnings. */
575 bool is_empty_range; /**< If set marked this as an empty range. */
578 struct label_statement_t {
579 statement_base_t base;
581 statement_t *statement;
582 label_statement_t *next; /**< links all label statements of a function */
585 struct expression_statement_t {
586 statement_base_t base;
587 expression_t *expression;
590 struct while_statement_t {
591 statement_base_t base;
592 expression_t *condition;
596 struct do_while_statement_t {
597 statement_base_t base;
598 expression_t *condition;
602 struct for_statement_t {
603 statement_base_t base;
604 expression_t *initialisation;
605 expression_t *condition;
609 bool condition_reachable:1;
610 bool step_reachable:1;
613 struct asm_argument_t {
614 string_t constraints;
615 expression_t *expression;
617 asm_argument_t *next;
620 struct asm_clobber_t {
625 struct asm_statement_t {
626 statement_base_t base;
628 asm_argument_t *inputs;
629 asm_argument_t *outputs;
630 asm_clobber_t *clobbers;
634 struct ms_try_statement_t {
635 statement_base_t base;
636 statement_t *try_statement;
637 expression_t *except_expression; /**< non-null for except, NULL for finally */
638 statement_t *final_statement;
641 struct leave_statement_t {
642 statement_base_t base;
646 statement_kind_t kind;
647 statement_base_t base;
648 return_statement_t returns;
649 compound_statement_t compound;
650 declaration_statement_t declaration;
652 switch_statement_t switchs;
653 goto_statement_t gotos;
654 case_label_statement_t case_label;
655 label_statement_t label;
656 expression_statement_t expression;
657 while_statement_t whiles;
658 do_while_statement_t do_while;
659 for_statement_t fors;
660 asm_statement_t asms;
661 ms_try_statement_t ms_try;
662 leave_statement_t leave;
665 struct translation_unit_t {
667 statement_t *global_asm;
670 static inline void *_allocate_ast(size_t size)
672 return obstack_alloc(&ast_obstack, size);
675 static inline bool is_invalid_expression(expression_t *expression)
677 return expression->base.kind == EXPR_INVALID;
680 static inline bool is_invalid_statement(statement_t *statement)
682 return statement->base.kind == STATEMENT_INVALID;
685 #define allocate_ast(size) _allocate_ast(size)
688 * Allocate an AST node with given size and
689 * initialize all fields with zero.
691 static inline void *allocate_ast_zero(size_t size)
693 void *res = allocate_ast(size);
694 memset(res, 0, size);