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>
32 /** The AST obstack contains all data that must stay in the AST. */
33 extern struct obstack ast_obstack;
36 * Operator precedence classes
38 typedef enum precedence_t {
40 PREC_EXPRESSION, /* , left to right */
41 PREC_ASSIGNMENT, /* = += -= *= /= %= <<= >>= &= ^= |= right to left */
42 PREC_CONDITIONAL, /* ?: right to left */
43 PREC_LOGICAL_OR, /* || left to right */
44 PREC_LOGICAL_AND, /* && left to right */
45 PREC_OR, /* | left to right */
46 PREC_XOR, /* ^ left to right */
47 PREC_AND, /* & left to right */
48 PREC_EQUALITY, /* == != left to right */
49 PREC_RELATIONAL, /* < <= > >= left to right */
50 PREC_SHIFT, /* << >> left to right */
51 PREC_ADDITIVE, /* + - left to right */
52 PREC_MULTIPLICATIVE, /* * / % left to right */
53 PREC_CAST, /* (type) right to left */
54 PREC_UNARY, /* ! ~ ++ -- + - * & sizeof right to left */
55 PREC_POSTFIX, /* () [] -> . left to right */
63 typedef enum expression_kind_t {
68 EXPR_CHARACTER_CONSTANT,
69 EXPR_WIDE_CHARACTER_CONSTANT,
71 EXPR_WIDE_STRING_LITERAL,
72 EXPR_COMPOUND_LITERAL,
83 EXPR_BUILTIN_CONSTANT_P,
84 EXPR_BUILTIN_PREFETCH,
89 EXPR_LABEL_ADDRESS, /**< GCC extension &&label operator */
92 EXPR_UNARY_NEGATE = EXPR_UNARY_FIRST,
94 EXPR_UNARY_BITWISE_NEGATE,
96 EXPR_UNARY_DEREFERENCE,
97 EXPR_UNARY_TAKE_ADDRESS,
98 EXPR_UNARY_POSTFIX_INCREMENT,
99 EXPR_UNARY_POSTFIX_DECREMENT,
100 EXPR_UNARY_PREFIX_INCREMENT,
101 EXPR_UNARY_PREFIX_DECREMENT,
103 EXPR_UNARY_CAST_IMPLICIT, /**< compiler generated cast */
104 EXPR_UNARY_ASSUME, /**< MS __assume() */
106 EXPR_UNARY_DELETE_ARRAY,
108 EXPR_UNARY_LAST = EXPR_UNARY_THROW,
111 EXPR_BINARY_ADD = EXPR_BINARY_FIRST,
117 EXPR_BINARY_NOTEQUAL,
119 EXPR_BINARY_LESSEQUAL,
121 EXPR_BINARY_GREATEREQUAL,
122 EXPR_BINARY_BITWISE_AND,
123 EXPR_BINARY_BITWISE_OR,
124 EXPR_BINARY_BITWISE_XOR,
125 EXPR_BINARY_LOGICAL_AND,
126 EXPR_BINARY_LOGICAL_OR,
127 EXPR_BINARY_SHIFTLEFT,
128 EXPR_BINARY_SHIFTRIGHT,
130 EXPR_BINARY_MUL_ASSIGN,
131 EXPR_BINARY_DIV_ASSIGN,
132 EXPR_BINARY_MOD_ASSIGN,
133 EXPR_BINARY_ADD_ASSIGN,
134 EXPR_BINARY_SUB_ASSIGN,
135 EXPR_BINARY_SHIFTLEFT_ASSIGN,
136 EXPR_BINARY_SHIFTRIGHT_ASSIGN,
137 EXPR_BINARY_BITWISE_AND_ASSIGN,
138 EXPR_BINARY_BITWISE_XOR_ASSIGN,
139 EXPR_BINARY_BITWISE_OR_ASSIGN,
142 EXPR_BINARY_BUILTIN_EXPECT,
143 EXPR_BINARY_ISGREATER,
144 EXPR_BINARY_ISGREATEREQUAL,
146 EXPR_BINARY_ISLESSEQUAL,
147 EXPR_BINARY_ISLESSGREATER,
148 EXPR_BINARY_ISUNORDERED,
149 EXPR_BINARY_LAST = EXPR_BINARY_ISUNORDERED,
152 typedef enum funcname_kind_t {
153 FUNCNAME_FUNCTION, /**< C99 __func__, older __FUNCTION__ */
154 FUNCNAME_PRETTY_FUNCTION, /**< GNUC __PRETTY_FUNCTION__ */
155 FUNCNAME_FUNCSIG, /**< MS __FUNCSIG__ */
156 FUNCNAME_FUNCDNAME /**< MS __FUNCDNAME__ */
159 /* convenience macros */
160 #define EXPR_BINARY_CASES \
161 case EXPR_BINARY_ADD: \
162 case EXPR_BINARY_SUB: \
163 case EXPR_BINARY_MUL: \
164 case EXPR_BINARY_DIV: \
165 case EXPR_BINARY_MOD: \
166 case EXPR_BINARY_EQUAL: \
167 case EXPR_BINARY_NOTEQUAL: \
168 case EXPR_BINARY_LESS: \
169 case EXPR_BINARY_LESSEQUAL: \
170 case EXPR_BINARY_GREATER: \
171 case EXPR_BINARY_GREATEREQUAL: \
172 case EXPR_BINARY_BITWISE_AND: \
173 case EXPR_BINARY_BITWISE_OR: \
174 case EXPR_BINARY_BITWISE_XOR: \
175 case EXPR_BINARY_LOGICAL_AND: \
176 case EXPR_BINARY_LOGICAL_OR: \
177 case EXPR_BINARY_SHIFTLEFT: \
178 case EXPR_BINARY_SHIFTRIGHT: \
179 case EXPR_BINARY_ASSIGN: \
180 case EXPR_BINARY_MUL_ASSIGN: \
181 case EXPR_BINARY_DIV_ASSIGN: \
182 case EXPR_BINARY_MOD_ASSIGN: \
183 case EXPR_BINARY_ADD_ASSIGN: \
184 case EXPR_BINARY_SUB_ASSIGN: \
185 case EXPR_BINARY_SHIFTLEFT_ASSIGN: \
186 case EXPR_BINARY_SHIFTRIGHT_ASSIGN: \
187 case EXPR_BINARY_BITWISE_AND_ASSIGN: \
188 case EXPR_BINARY_BITWISE_XOR_ASSIGN: \
189 case EXPR_BINARY_BITWISE_OR_ASSIGN: \
190 case EXPR_BINARY_COMMA: \
191 case EXPR_BINARY_BUILTIN_EXPECT: \
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 optinal 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 * A scope containing declarations.
233 declaration_t *declarations; /**< List of declarations in this scope. */
234 declaration_t *last_declaration; /**< last declaration in this scope. */
235 scope_t *parent; /**< points to the parent scope. */
236 unsigned depth; /**< while parsing, the depth of this scope in the scope stack. */
239 struct expression_base_t {
240 expression_kind_t kind;
242 source_position_t source_position;
248 struct const_expression_t {
249 expression_base_t base;
252 long double float_value;
254 wide_string_t wide_character;
256 bool is_ms_noop; /**< True, if this constant is the result
257 of an microsoft __noop operator */
260 struct string_literal_expression_t {
261 expression_base_t base;
265 struct funcname_expression_t {
266 expression_base_t base;
267 funcname_kind_t kind;
268 string_t value; /**< the value once assigned. */
271 struct wide_string_literal_expression_t {
272 expression_base_t base;
276 struct compound_literal_expression_t {
277 expression_base_t base;
279 initializer_t *initializer;
282 struct builtin_symbol_expression_t {
283 expression_base_t base;
287 struct builtin_constant_expression_t {
288 expression_base_t base;
292 struct builtin_prefetch_expression_t {
293 expression_base_t base;
296 expression_t *locality;
299 struct reference_expression_t {
300 expression_base_t base;
301 declaration_t *declaration;
304 struct call_argument_t {
305 expression_t *expression;
306 call_argument_t *next;
309 struct call_expression_t {
310 expression_base_t base;
311 expression_t *function;
312 call_argument_t *arguments;
315 struct unary_expression_t {
316 expression_base_t base;
320 struct binary_expression_t {
321 expression_base_t base;
326 struct select_expression_t {
327 expression_base_t base;
328 expression_t *compound;
329 declaration_t *compound_entry;
332 struct array_access_expression_t {
333 expression_base_t base;
334 expression_t *array_ref;
336 bool flipped; /**< index/ref was written in a 5[a] way */
339 struct typeprop_expression_t {
340 expression_base_t base;
342 expression_t *tp_expression;
345 struct designator_t {
346 source_position_t source_position;
348 expression_t *array_index;
352 struct offsetof_expression_t {
353 expression_base_t base;
355 designator_t *designator;
358 struct va_start_expression_t {
359 expression_base_t base;
361 declaration_t *parameter;
364 struct va_arg_expression_t {
365 expression_base_t base;
369 struct conditional_expression_t {
370 expression_base_t base;
371 expression_t *condition;
372 expression_t *true_expression;
373 expression_t *false_expression;
376 struct statement_expression_t {
377 expression_base_t base;
378 statement_t *statement;
381 struct classify_type_expression_t {
382 expression_base_t base;
383 expression_t *type_expression;
386 struct label_address_expression_t {
387 expression_base_t base;
388 declaration_t *declaration;
392 expression_kind_t kind;
393 expression_base_t base;
394 const_expression_t conste;
395 funcname_expression_t funcname;
396 string_literal_expression_t string;
397 wide_string_literal_expression_t wide_string;
398 compound_literal_expression_t compound_literal;
399 builtin_symbol_expression_t builtin_symbol;
400 builtin_constant_expression_t builtin_constant;
401 builtin_prefetch_expression_t builtin_prefetch;
402 reference_expression_t reference;
403 call_expression_t call;
404 unary_expression_t unary;
405 binary_expression_t binary;
406 select_expression_t select;
407 array_access_expression_t array_access;
408 typeprop_expression_t typeprop;
409 offsetof_expression_t offsetofe;
410 va_start_expression_t va_starte;
411 va_arg_expression_t va_arge;
412 conditional_expression_t conditional;
413 statement_expression_t statement;
414 classify_type_expression_t classify_type;
415 label_address_expression_t label_address;
418 typedef enum storage_class_tag_t {
420 STORAGE_CLASS_EXTERN,
421 STORAGE_CLASS_STATIC,
422 STORAGE_CLASS_TYPEDEF,
424 STORAGE_CLASS_REGISTER,
425 STORAGE_CLASS_ENUM_ENTRY,
426 STORAGE_CLASS_THREAD,
427 STORAGE_CLASS_THREAD_EXTERN,
428 STORAGE_CLASS_THREAD_STATIC,
429 } storage_class_tag_t;
431 typedef enum namespace_t {
437 NAMESPACE_LOCAL_LABEL
440 typedef enum initializer_kind_t {
444 INITIALIZER_WIDE_STRING,
445 INITIALIZER_DESIGNATOR
446 } initializer_kind_t;
448 struct initializer_base_t {
449 initializer_kind_t kind;
452 struct initializer_value_t {
453 initializer_base_t base;
457 struct initializer_list_t {
458 initializer_base_t base;
460 initializer_t *initializers[];
463 struct initializer_string_t {
464 initializer_base_t base;
468 struct initializer_wide_string_t {
469 initializer_base_t base;
470 wide_string_t string;
473 struct initializer_designator_t {
474 initializer_base_t base;
475 designator_t *designator;
478 union initializer_t {
479 initializer_kind_t kind;
480 initializer_base_t base;
481 initializer_value_t value;
482 initializer_list_t list;
483 initializer_string_t string;
484 initializer_wide_string_t wide_string;
485 initializer_designator_t designator;
491 typedef enum gnu_attribute_kind_t {
502 GNU_AK_ALWAYS_INLINE,
508 GNU_AK_TRANSPARENT_UNION,
516 GNU_AK_NO_INSTRUMENT_FUNCTION,
517 GNU_AK_WARN_UNUSED_RESULT,
522 GNU_AK_FUNCTION_VECTOR,
524 GNU_AK_INTERRUPT_HANDLER,
530 GNU_AK_EIGTHBIT_DATA,
535 GNU_AK_EXTERNALLY_VISIBLE,
558 } gnu_attribute_kind_t;
561 * Extended microsoft modifier.
563 typedef enum decl_modifier_t {
564 DM_DLLIMPORT = 1 << 0,
565 DM_DLLEXPORT = 1 << 1,
568 DM_MICROSOFT_INLINE = 1 << 4,
569 DM_FORCEINLINE = 1 << 5,
570 DM_SELECTANY = 1 << 6,
572 DM_NOVTABLE = 1 << 8,
573 DM_NORETURN = 1 << 9,
574 DM_NOINLINE = 1 << 10,
575 DM_RESTRICT = 1 << 11,
576 DM_NOALIAS = 1 << 12,
578 DM_TRANSPARENT_UNION = 1 << 14,
581 DM_CONSTRUCTOR = 1 << 17,
582 DM_DESTRUCTOR = 1 << 18,
586 DM_FASTCALL = 1 << 22,
587 DM_STDCALL = 1 << 23,
588 DM_THISCALL = 1 << 24,
589 DM_DEPRECATED = 1 << 25
592 typedef unsigned decl_modifiers_t;
594 struct declaration_t {
595 unsigned char namespc;
596 unsigned char declared_storage_class;
597 unsigned char storage_class;
598 unsigned char alignment; /**< Alignment of the declaration, 0 for default. */
599 decl_modifiers_t modifiers; /**< modifiers. */
600 const char *deprecated_string; /**< MS deprecated string if any. */
601 symbol_t *get_property_sym; /**< MS get property. */
602 symbol_t *put_property_sym; /**< MS put property. */
603 unsigned int address_taken : 1; /**< Set if the address of this declaration was taken. */
604 unsigned int is_inline : 1;
605 unsigned int used : 1; /**< Set if the declaration is used. */
606 unsigned int read : 1;
607 unsigned int implicit : 1; /**< Set for implicit (not found in source code) declarations. */
608 unsigned int need_closure : 1; /**< Inner function needs closure. */
609 unsigned int goto_to_outer : 1; /**< Inner function has goto to outer function. */
611 il_size_t offset; /**< The offset of this member inside a compound. */
613 source_position_t source_position;
615 bool complete; /**< used to indicate whether struct/union types are already defined or if just the name is declared */
616 statement_t *statement;
617 initializer_t *initializer;
618 expression_t *enum_value;
620 scope_t scope; /**< The scope that this declaration opens. */
621 scope_t *parent_scope; /**< The parent scope where this declaration lives. */
623 /** next declaration in a scope */
625 /** next declaration with same symbol */
626 declaration_t *symbol_next;
628 /* the following fields are used in ast2firm module */
629 unsigned char declaration_kind;
631 unsigned int value_number;
640 typedef enum statement_kind_t {
645 STATEMENT_DECLARATION,
648 STATEMENT_EXPRESSION,
653 STATEMENT_CASE_LABEL,
658 STATEMENT_MS_TRY, /**< MS __try/__finally or __try/__except */
659 STATEMENT_LEAVE /**< MS __leave */
662 struct statement_base_t {
663 statement_kind_t kind;
665 source_position_t source_position;
673 struct invalid_statement_t {
674 statement_base_t base;
677 struct empty_statement_t {
678 statement_base_t base;
681 struct return_statement_t {
682 statement_base_t base;
686 struct compound_statement_t {
687 statement_base_t base;
688 statement_t *statements;
692 struct declaration_statement_t {
693 statement_base_t base;
694 declaration_t *declarations_begin;
695 declaration_t *declarations_end;
698 struct if_statement_t {
699 statement_base_t base;
700 expression_t *condition;
701 statement_t *true_statement;
702 statement_t *false_statement;
705 struct switch_statement_t {
706 statement_base_t base;
707 expression_t *expression;
709 case_label_statement_t *first_case, *last_case; /**< List of all cases, including default. */
710 case_label_statement_t *default_label; /**< The default label if existent. */
711 unsigned long default_proj_nr; /**< The Proj-number for the default Proj. */
714 struct goto_statement_t {
715 statement_base_t base;
716 declaration_t *label; /**< The destination label. */
717 expression_t *expression; /**< The expression for an assigned goto. */
718 goto_statement_t *next; /**< links all goto statements of a function */
721 struct case_label_statement_t {
722 statement_base_t base;
723 expression_t *expression; /**< The case label expression, NULL for default label. */
724 expression_t *end_range; /**< For GNUC case a .. b: the end range expression, NULL else. */
725 case_label_statement_t *next; /**< link to the next case label in switch */
726 statement_t *statement;
727 long first_case; /**< The folded value of expression. */
728 long last_case; /**< The folded value of end_range. */
729 bool is_bad; /**< If set marked as bad to suppress warnings. */
730 bool is_empty_range; /**< If set marked this as an empty range. */
733 struct label_statement_t {
734 statement_base_t base;
735 declaration_t *label;
736 statement_t *statement;
737 label_statement_t *next; /**< links all label statements of a function */
740 struct expression_statement_t {
741 statement_base_t base;
742 expression_t *expression;
745 struct while_statement_t {
746 statement_base_t base;
747 expression_t *condition;
751 struct do_while_statement_t {
752 statement_base_t base;
753 expression_t *condition;
757 struct for_statement_t {
758 statement_base_t base;
759 expression_t *initialisation;
760 expression_t *condition;
764 bool condition_reachable:1;
765 bool step_reachable:1;
768 struct asm_argument_t {
769 string_t constraints;
770 expression_t *expression;
772 asm_argument_t *next;
775 struct asm_clobber_t {
780 struct asm_statement_t {
781 statement_base_t base;
783 asm_argument_t *inputs;
784 asm_argument_t *outputs;
785 asm_clobber_t *clobbers;
789 struct ms_try_statement_t {
790 statement_base_t base;
791 statement_t *try_statement;
792 expression_t *except_expression; /**< non-null for except, NULL for finally */
793 statement_t *final_statement;
796 struct leave_statement_t {
797 statement_base_t base;
801 statement_kind_t kind;
802 statement_base_t base;
803 return_statement_t returns;
804 compound_statement_t compound;
805 declaration_statement_t declaration;
807 switch_statement_t switchs;
808 goto_statement_t gotos;
809 case_label_statement_t case_label;
810 label_statement_t label;
811 expression_statement_t expression;
812 while_statement_t whiles;
813 do_while_statement_t do_while;
814 for_statement_t fors;
815 asm_statement_t asms;
816 ms_try_statement_t ms_try;
817 leave_statement_t leave;
820 struct translation_unit_t {
822 statement_t *global_asm;
826 void *_allocate_ast(size_t size)
828 return obstack_alloc(&ast_obstack, size);
832 bool is_invalid_expression(expression_t *expression)
834 return expression->base.kind == EXPR_INVALID;
838 bool is_invalid_statement(statement_t *statement)
840 return statement->base.kind == STATEMENT_INVALID;
844 #define allocate_ast(size) _allocate_ast(size)