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_LAST = EXPR_UNARY_THROW,
109 EXPR_BINARY_ADD = EXPR_BINARY_FIRST,
115 EXPR_BINARY_NOTEQUAL,
117 EXPR_BINARY_LESSEQUAL,
119 EXPR_BINARY_GREATEREQUAL,
120 EXPR_BINARY_BITWISE_AND,
121 EXPR_BINARY_BITWISE_OR,
122 EXPR_BINARY_BITWISE_XOR,
123 EXPR_BINARY_LOGICAL_AND,
124 EXPR_BINARY_LOGICAL_OR,
125 EXPR_BINARY_SHIFTLEFT,
126 EXPR_BINARY_SHIFTRIGHT,
128 EXPR_BINARY_MUL_ASSIGN,
129 EXPR_BINARY_DIV_ASSIGN,
130 EXPR_BINARY_MOD_ASSIGN,
131 EXPR_BINARY_ADD_ASSIGN,
132 EXPR_BINARY_SUB_ASSIGN,
133 EXPR_BINARY_SHIFTLEFT_ASSIGN,
134 EXPR_BINARY_SHIFTRIGHT_ASSIGN,
135 EXPR_BINARY_BITWISE_AND_ASSIGN,
136 EXPR_BINARY_BITWISE_XOR_ASSIGN,
137 EXPR_BINARY_BITWISE_OR_ASSIGN,
140 EXPR_BINARY_BUILTIN_EXPECT,
141 EXPR_BINARY_ISGREATER,
142 EXPR_BINARY_ISGREATEREQUAL,
144 EXPR_BINARY_ISLESSEQUAL,
145 EXPR_BINARY_ISLESSGREATER,
146 EXPR_BINARY_ISUNORDERED,
147 EXPR_BINARY_LAST = EXPR_BINARY_ISUNORDERED,
150 typedef enum funcname_kind_t {
151 FUNCNAME_FUNCTION, /**< C99 __func__, older __FUNCTION__ */
152 FUNCNAME_PRETTY_FUNCTION, /**< GNUC __PRETTY_FUNCTION__ */
153 FUNCNAME_FUNCSIG, /**< MS __FUNCSIG__ */
154 FUNCNAME_FUNCDNAME /**< MS __FUNCDNAME__ */
157 /* convenience macros */
158 #define EXPR_BINARY_CASES \
159 case EXPR_BINARY_ADD: \
160 case EXPR_BINARY_SUB: \
161 case EXPR_BINARY_MUL: \
162 case EXPR_BINARY_DIV: \
163 case EXPR_BINARY_MOD: \
164 case EXPR_BINARY_EQUAL: \
165 case EXPR_BINARY_NOTEQUAL: \
166 case EXPR_BINARY_LESS: \
167 case EXPR_BINARY_LESSEQUAL: \
168 case EXPR_BINARY_GREATER: \
169 case EXPR_BINARY_GREATEREQUAL: \
170 case EXPR_BINARY_BITWISE_AND: \
171 case EXPR_BINARY_BITWISE_OR: \
172 case EXPR_BINARY_BITWISE_XOR: \
173 case EXPR_BINARY_LOGICAL_AND: \
174 case EXPR_BINARY_LOGICAL_OR: \
175 case EXPR_BINARY_SHIFTLEFT: \
176 case EXPR_BINARY_SHIFTRIGHT: \
177 case EXPR_BINARY_ASSIGN: \
178 case EXPR_BINARY_MUL_ASSIGN: \
179 case EXPR_BINARY_DIV_ASSIGN: \
180 case EXPR_BINARY_MOD_ASSIGN: \
181 case EXPR_BINARY_ADD_ASSIGN: \
182 case EXPR_BINARY_SUB_ASSIGN: \
183 case EXPR_BINARY_SHIFTLEFT_ASSIGN: \
184 case EXPR_BINARY_SHIFTRIGHT_ASSIGN: \
185 case EXPR_BINARY_BITWISE_AND_ASSIGN: \
186 case EXPR_BINARY_BITWISE_XOR_ASSIGN: \
187 case EXPR_BINARY_BITWISE_OR_ASSIGN: \
188 case EXPR_BINARY_COMMA: \
189 case EXPR_BINARY_BUILTIN_EXPECT: \
190 case EXPR_BINARY_ISGREATER: \
191 case EXPR_BINARY_ISGREATEREQUAL: \
192 case EXPR_BINARY_ISLESS: \
193 case EXPR_BINARY_ISLESSEQUAL: \
194 case EXPR_BINARY_ISLESSGREATER: \
195 case EXPR_BINARY_ISUNORDERED:
198 * unary expression with mandatory operand
200 #define EXPR_UNARY_CASES_MANDATORY \
201 case EXPR_UNARY_NEGATE: \
202 case EXPR_UNARY_PLUS: \
203 case EXPR_UNARY_BITWISE_NEGATE: \
204 case EXPR_UNARY_NOT: \
205 case EXPR_UNARY_DEREFERENCE: \
206 case EXPR_UNARY_TAKE_ADDRESS: \
207 case EXPR_UNARY_POSTFIX_INCREMENT: \
208 case EXPR_UNARY_POSTFIX_DECREMENT: \
209 case EXPR_UNARY_PREFIX_INCREMENT: \
210 case EXPR_UNARY_PREFIX_DECREMENT: \
211 case EXPR_UNARY_CAST: \
212 case EXPR_UNARY_CAST_IMPLICIT: \
213 case EXPR_UNARY_ASSUME:
216 * unary expression with optinal operand
218 #define EXPR_UNARY_CASES_OPTIONAL \
219 case EXPR_UNARY_THROW: \
221 #define EXPR_UNARY_CASES \
222 EXPR_UNARY_CASES_MANDATORY \
223 EXPR_UNARY_CASES_OPTIONAL
226 * A scope containing declarations.
229 declaration_t *declarations; /**< List of declarations in this scope. */
230 declaration_t *last_declaration; /**< last declaration in this scope. */
231 scope_t *parent; /**< points to the parent scope. */
232 unsigned depth; /**< while parsing, the depth of this scope in the scope stack. */
235 struct expression_base_t {
236 expression_kind_t kind;
238 source_position_t source_position;
244 struct const_expression_t {
245 expression_base_t base;
248 long double float_value;
250 wide_string_t wide_character;
252 bool is_ms_noop; /**< True, if this constant is the result
253 of an microsoft __noop operator */
256 struct string_literal_expression_t {
257 expression_base_t base;
261 struct funcname_expression_t {
262 expression_base_t base;
263 funcname_kind_t kind;
264 string_t value; /**< the value once assigned. */
267 struct wide_string_literal_expression_t {
268 expression_base_t base;
272 struct compound_literal_expression_t {
273 expression_base_t base;
275 initializer_t *initializer;
278 struct builtin_symbol_expression_t {
279 expression_base_t base;
283 struct builtin_constant_expression_t {
284 expression_base_t base;
288 struct builtin_prefetch_expression_t {
289 expression_base_t base;
292 expression_t *locality;
295 struct reference_expression_t {
296 expression_base_t base;
297 declaration_t *declaration;
300 struct call_argument_t {
301 expression_t *expression;
302 call_argument_t *next;
305 struct call_expression_t {
306 expression_base_t base;
307 expression_t *function;
308 call_argument_t *arguments;
311 struct unary_expression_t {
312 expression_base_t base;
316 struct binary_expression_t {
317 expression_base_t base;
322 struct select_expression_t {
323 expression_base_t base;
324 expression_t *compound;
325 declaration_t *compound_entry;
328 struct array_access_expression_t {
329 expression_base_t base;
330 expression_t *array_ref;
332 bool flipped; /**< index/ref was written in a 5[a] way */
335 struct typeprop_expression_t {
336 expression_base_t base;
338 expression_t *tp_expression;
341 struct designator_t {
342 source_position_t source_position;
344 expression_t *array_index;
348 struct offsetof_expression_t {
349 expression_base_t base;
351 designator_t *designator;
354 struct va_start_expression_t {
355 expression_base_t base;
357 declaration_t *parameter;
360 struct va_arg_expression_t {
361 expression_base_t base;
365 struct conditional_expression_t {
366 expression_base_t base;
367 expression_t *condition;
368 expression_t *true_expression;
369 expression_t *false_expression;
372 struct statement_expression_t {
373 expression_base_t base;
374 statement_t *statement;
377 struct classify_type_expression_t {
378 expression_base_t base;
379 expression_t *type_expression;
382 struct label_address_expression_t {
383 expression_base_t base;
384 declaration_t *declaration;
388 expression_kind_t kind;
389 expression_base_t base;
390 const_expression_t conste;
391 funcname_expression_t funcname;
392 string_literal_expression_t string;
393 wide_string_literal_expression_t wide_string;
394 compound_literal_expression_t compound_literal;
395 builtin_symbol_expression_t builtin_symbol;
396 builtin_constant_expression_t builtin_constant;
397 builtin_prefetch_expression_t builtin_prefetch;
398 reference_expression_t reference;
399 call_expression_t call;
400 unary_expression_t unary;
401 binary_expression_t binary;
402 select_expression_t select;
403 array_access_expression_t array_access;
404 typeprop_expression_t typeprop;
405 offsetof_expression_t offsetofe;
406 va_start_expression_t va_starte;
407 va_arg_expression_t va_arge;
408 conditional_expression_t conditional;
409 statement_expression_t statement;
410 classify_type_expression_t classify_type;
411 label_address_expression_t label_address;
414 typedef enum storage_class_tag_t {
416 STORAGE_CLASS_EXTERN,
417 STORAGE_CLASS_STATIC,
418 STORAGE_CLASS_TYPEDEF,
420 STORAGE_CLASS_REGISTER,
421 STORAGE_CLASS_ENUM_ENTRY,
422 STORAGE_CLASS_THREAD,
423 STORAGE_CLASS_THREAD_EXTERN,
424 STORAGE_CLASS_THREAD_STATIC,
425 } storage_class_tag_t;
427 typedef enum namespace_t {
433 NAMESPACE_LOCAL_LABEL
436 typedef enum initializer_kind_t {
440 INITIALIZER_WIDE_STRING,
441 INITIALIZER_DESIGNATOR
442 } initializer_kind_t;
444 struct initializer_base_t {
445 initializer_kind_t kind;
448 struct initializer_value_t {
449 initializer_base_t base;
453 struct initializer_list_t {
454 initializer_base_t base;
456 initializer_t *initializers[];
459 struct initializer_string_t {
460 initializer_base_t base;
464 struct initializer_wide_string_t {
465 initializer_base_t base;
466 wide_string_t string;
469 struct initializer_designator_t {
470 initializer_base_t base;
471 designator_t *designator;
474 union initializer_t {
475 initializer_kind_t kind;
476 initializer_base_t base;
477 initializer_value_t value;
478 initializer_list_t list;
479 initializer_string_t string;
480 initializer_wide_string_t wide_string;
481 initializer_designator_t designator;
487 typedef enum gnu_attribute_kind_t {
498 GNU_AK_ALWAYS_INLINE,
504 GNU_AK_TRANSPARENT_UNION,
512 GNU_AK_NO_INSTRUMENT_FUNCTION,
513 GNU_AK_WARN_UNUSED_RESULT,
518 GNU_AK_FUNCTION_VECTOR,
520 GNU_AK_INTERRUPT_HANDLER,
526 GNU_AK_EIGTHBIT_DATA,
531 GNU_AK_EXTERNALLY_VISIBLE,
554 } gnu_attribute_kind_t;
557 * Extended microsoft modifier.
559 typedef enum decl_modifier_t {
560 DM_DLLIMPORT = 1 << 0,
561 DM_DLLEXPORT = 1 << 1,
564 DM_MICROSOFT_INLINE = 1 << 4,
565 DM_FORCEINLINE = 1 << 5,
566 DM_SELECTANY = 1 << 6,
568 DM_NOVTABLE = 1 << 8,
569 DM_NORETURN = 1 << 9,
570 DM_NOINLINE = 1 << 10,
571 DM_RESTRICT = 1 << 11,
572 DM_NOALIAS = 1 << 12,
574 DM_TRANSPARENT_UNION = 1 << 14,
577 DM_CONSTRUCTOR = 1 << 17,
578 DM_DESTRUCTOR = 1 << 18,
582 DM_FASTCALL = 1 << 22,
583 DM_STDCALL = 1 << 23,
584 DM_THISCALL = 1 << 24,
585 DM_DEPRECATED = 1 << 25
588 typedef unsigned decl_modifiers_t;
590 struct declaration_t {
591 unsigned char namespc;
592 unsigned char declared_storage_class;
593 unsigned char storage_class;
594 unsigned char alignment; /**< Alignment of the declaration, 0 for default. */
595 decl_modifiers_t modifiers; /**< modifiers. */
596 const char *deprecated_string; /**< MS deprecated string if any. */
597 symbol_t *get_property_sym; /**< MS get property. */
598 symbol_t *put_property_sym; /**< MS put property. */
599 unsigned int address_taken : 1; /**< Set if the address of this declaration was taken. */
600 unsigned int is_inline : 1;
601 unsigned int used : 1; /**< Set if the declaration is used. */
602 unsigned int read : 1;
603 unsigned int implicit : 1; /**< Set for implicit (not found in source code) declarations. */
604 unsigned int need_closure : 1; /**< Inner function needs closure. */
605 unsigned int goto_to_outer : 1; /**< Inner function has goto to outer function. */
607 il_size_t offset; /**< The offset of this member inside a compound. */
609 source_position_t source_position;
611 bool complete; /**< used to indicate whether struct/union types are already defined or if just the name is declared */
612 statement_t *statement;
613 initializer_t *initializer;
614 expression_t *enum_value;
616 scope_t scope; /**< The scope that this declaration opens. */
617 scope_t *parent_scope; /**< The parent scope where this declaration lives. */
619 /** next declaration in a scope */
621 /** next declaration with same symbol */
622 declaration_t *symbol_next;
624 /* the following fields are used in ast2firm module */
625 unsigned char declaration_kind;
627 unsigned int value_number;
636 typedef enum statement_kind_t {
641 STATEMENT_DECLARATION,
644 STATEMENT_EXPRESSION,
649 STATEMENT_CASE_LABEL,
654 STATEMENT_MS_TRY, /**< MS __try/__finally or __try/__except */
655 STATEMENT_LEAVE /**< MS __leave */
658 struct statement_base_t {
659 statement_kind_t kind;
661 source_position_t source_position;
669 struct invalid_statement_t {
670 statement_base_t base;
673 struct empty_statement_t {
674 statement_base_t base;
677 struct return_statement_t {
678 statement_base_t base;
682 struct compound_statement_t {
683 statement_base_t base;
684 statement_t *statements;
688 struct declaration_statement_t {
689 statement_base_t base;
690 declaration_t *declarations_begin;
691 declaration_t *declarations_end;
694 struct if_statement_t {
695 statement_base_t base;
696 expression_t *condition;
697 statement_t *true_statement;
698 statement_t *false_statement;
701 struct switch_statement_t {
702 statement_base_t base;
703 expression_t *expression;
705 case_label_statement_t *first_case, *last_case; /**< List of all cases, including default. */
706 case_label_statement_t *default_label; /**< The default label if existent. */
707 unsigned long default_proj_nr; /**< The Proj-number for the default Proj. */
710 struct goto_statement_t {
711 statement_base_t base;
712 declaration_t *label; /**< The destination label. */
713 expression_t *expression; /**< The expression for an assigned goto. */
714 goto_statement_t *next; /**< links all goto statements of a function */
717 struct case_label_statement_t {
718 statement_base_t base;
719 expression_t *expression; /**< The case label expression, NULL for default label. */
720 expression_t *end_range; /**< For GNUC case a .. b: the end range expression, NULL else. */
721 case_label_statement_t *next; /**< link to the next case label in switch */
722 statement_t *statement;
723 long first_case; /**< The folded value of expression. */
724 long last_case; /**< The folded value of end_range. */
725 bool is_bad; /**< If set marked as bad to suppress warnings. */
726 bool is_empty_range; /**< If set marked this as an empty range. */
729 struct label_statement_t {
730 statement_base_t base;
731 declaration_t *label;
732 statement_t *statement;
733 label_statement_t *next; /**< links all label statements of a function */
736 struct expression_statement_t {
737 statement_base_t base;
738 expression_t *expression;
741 struct while_statement_t {
742 statement_base_t base;
743 expression_t *condition;
747 struct do_while_statement_t {
748 statement_base_t base;
749 expression_t *condition;
753 struct for_statement_t {
754 statement_base_t base;
755 expression_t *initialisation;
756 expression_t *condition;
760 bool condition_reachable:1;
761 bool step_reachable:1;
764 struct asm_argument_t {
765 string_t constraints;
766 expression_t *expression;
768 asm_argument_t *next;
771 struct asm_clobber_t {
776 struct asm_statement_t {
777 statement_base_t base;
779 asm_argument_t *inputs;
780 asm_argument_t *outputs;
781 asm_clobber_t *clobbers;
785 struct ms_try_statement_t {
786 statement_base_t base;
787 statement_t *try_statement;
788 expression_t *except_expression; /**< non-null for except, NULL for finally */
789 statement_t *final_statement;
792 struct leave_statement_t {
793 statement_base_t base;
797 statement_kind_t kind;
798 statement_base_t base;
799 return_statement_t returns;
800 compound_statement_t compound;
801 declaration_statement_t declaration;
803 switch_statement_t switchs;
804 goto_statement_t gotos;
805 case_label_statement_t case_label;
806 label_statement_t label;
807 expression_statement_t expression;
808 while_statement_t whiles;
809 do_while_statement_t do_while;
810 for_statement_t fors;
811 asm_statement_t asms;
812 ms_try_statement_t ms_try;
813 leave_statement_t leave;
816 struct translation_unit_t {
818 statement_t *global_asm;
822 void *_allocate_ast(size_t size)
824 return obstack_alloc(&ast_obstack, size);
828 bool is_invalid_expression(expression_t *expression)
830 return expression->base.kind == EXPR_INVALID;
834 bool is_invalid_statement(statement_t *statement)
836 return statement->base.kind == STATEMENT_INVALID;
840 #define allocate_ast(size) _allocate_ast(size)