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() */
105 EXPR_UNARY_LAST = EXPR_UNARY_ASSUME,
108 EXPR_BINARY_ADD = EXPR_BINARY_FIRST,
114 EXPR_BINARY_NOTEQUAL,
116 EXPR_BINARY_LESSEQUAL,
118 EXPR_BINARY_GREATEREQUAL,
119 EXPR_BINARY_BITWISE_AND,
120 EXPR_BINARY_BITWISE_OR,
121 EXPR_BINARY_BITWISE_XOR,
122 EXPR_BINARY_LOGICAL_AND,
123 EXPR_BINARY_LOGICAL_OR,
124 EXPR_BINARY_SHIFTLEFT,
125 EXPR_BINARY_SHIFTRIGHT,
127 EXPR_BINARY_MUL_ASSIGN,
128 EXPR_BINARY_DIV_ASSIGN,
129 EXPR_BINARY_MOD_ASSIGN,
130 EXPR_BINARY_ADD_ASSIGN,
131 EXPR_BINARY_SUB_ASSIGN,
132 EXPR_BINARY_SHIFTLEFT_ASSIGN,
133 EXPR_BINARY_SHIFTRIGHT_ASSIGN,
134 EXPR_BINARY_BITWISE_AND_ASSIGN,
135 EXPR_BINARY_BITWISE_XOR_ASSIGN,
136 EXPR_BINARY_BITWISE_OR_ASSIGN,
139 EXPR_BINARY_BUILTIN_EXPECT,
140 EXPR_BINARY_ISGREATER,
141 EXPR_BINARY_ISGREATEREQUAL,
143 EXPR_BINARY_ISLESSEQUAL,
144 EXPR_BINARY_ISLESSGREATER,
145 EXPR_BINARY_ISUNORDERED,
146 EXPR_BINARY_LAST = EXPR_BINARY_ISUNORDERED,
149 typedef enum funcname_kind_t {
150 FUNCNAME_FUNCTION, /**< C99 __func__, older __FUNCTION__ */
151 FUNCNAME_PRETTY_FUNCTION, /**< GNUC __PRETTY_FUNCTION__ */
152 FUNCNAME_FUNCSIG, /**< MS __FUNCSIG__ */
153 FUNCNAME_FUNCDNAME /**< MS __FUNCDNAME__ */
156 /* convenience macros */
157 #define EXPR_BINARY_CASES \
158 case EXPR_BINARY_ADD: \
159 case EXPR_BINARY_SUB: \
160 case EXPR_BINARY_MUL: \
161 case EXPR_BINARY_DIV: \
162 case EXPR_BINARY_MOD: \
163 case EXPR_BINARY_EQUAL: \
164 case EXPR_BINARY_NOTEQUAL: \
165 case EXPR_BINARY_LESS: \
166 case EXPR_BINARY_LESSEQUAL: \
167 case EXPR_BINARY_GREATER: \
168 case EXPR_BINARY_GREATEREQUAL: \
169 case EXPR_BINARY_BITWISE_AND: \
170 case EXPR_BINARY_BITWISE_OR: \
171 case EXPR_BINARY_BITWISE_XOR: \
172 case EXPR_BINARY_LOGICAL_AND: \
173 case EXPR_BINARY_LOGICAL_OR: \
174 case EXPR_BINARY_SHIFTLEFT: \
175 case EXPR_BINARY_SHIFTRIGHT: \
176 case EXPR_BINARY_ASSIGN: \
177 case EXPR_BINARY_MUL_ASSIGN: \
178 case EXPR_BINARY_DIV_ASSIGN: \
179 case EXPR_BINARY_MOD_ASSIGN: \
180 case EXPR_BINARY_ADD_ASSIGN: \
181 case EXPR_BINARY_SUB_ASSIGN: \
182 case EXPR_BINARY_SHIFTLEFT_ASSIGN: \
183 case EXPR_BINARY_SHIFTRIGHT_ASSIGN: \
184 case EXPR_BINARY_BITWISE_AND_ASSIGN: \
185 case EXPR_BINARY_BITWISE_XOR_ASSIGN: \
186 case EXPR_BINARY_BITWISE_OR_ASSIGN: \
187 case EXPR_BINARY_COMMA: \
188 case EXPR_BINARY_BUILTIN_EXPECT: \
189 case EXPR_BINARY_ISGREATER: \
190 case EXPR_BINARY_ISGREATEREQUAL: \
191 case EXPR_BINARY_ISLESS: \
192 case EXPR_BINARY_ISLESSEQUAL: \
193 case EXPR_BINARY_ISLESSGREATER: \
194 case EXPR_BINARY_ISUNORDERED:
196 #define EXPR_UNARY_CASES \
197 case EXPR_UNARY_NEGATE: \
198 case EXPR_UNARY_PLUS: \
199 case EXPR_UNARY_BITWISE_NEGATE: \
200 case EXPR_UNARY_NOT: \
201 case EXPR_UNARY_DEREFERENCE: \
202 case EXPR_UNARY_TAKE_ADDRESS: \
203 case EXPR_UNARY_POSTFIX_INCREMENT: \
204 case EXPR_UNARY_POSTFIX_DECREMENT: \
205 case EXPR_UNARY_PREFIX_INCREMENT: \
206 case EXPR_UNARY_PREFIX_DECREMENT: \
207 case EXPR_UNARY_CAST: \
208 case EXPR_UNARY_CAST_IMPLICIT: \
209 case EXPR_UNARY_ASSUME:
212 * A scope containing declarations.
215 declaration_t *declarations; /**< List of declarations in this scope. */
216 declaration_t *last_declaration; /**< last declaration in this scope. */
217 scope_t *parent; /**< points to the parent scope. */
218 unsigned depth; /**< while parsing, the depth of this scope in the scope stack. */
221 struct expression_base_t {
222 expression_kind_t kind;
224 source_position_t source_position;
230 struct const_expression_t {
231 expression_base_t base;
234 long double float_value;
236 wide_string_t wide_character;
238 bool is_ms_noop; /**< True, if this constant is the result
239 of an microsoft __noop operator */
242 struct string_literal_expression_t {
243 expression_base_t base;
247 struct funcname_expression_t {
248 expression_base_t base;
249 funcname_kind_t kind;
250 string_t value; /**< the value once assigned. */
253 struct wide_string_literal_expression_t {
254 expression_base_t base;
258 struct compound_literal_expression_t {
259 expression_base_t base;
261 initializer_t *initializer;
264 struct builtin_symbol_expression_t {
265 expression_base_t base;
269 struct builtin_constant_expression_t {
270 expression_base_t base;
274 struct builtin_prefetch_expression_t {
275 expression_base_t base;
278 expression_t *locality;
281 struct reference_expression_t {
282 expression_base_t base;
283 declaration_t *declaration;
286 struct call_argument_t {
287 expression_t *expression;
288 call_argument_t *next;
291 struct call_expression_t {
292 expression_base_t base;
293 expression_t *function;
294 call_argument_t *arguments;
297 struct unary_expression_t {
298 expression_base_t base;
302 struct binary_expression_t {
303 expression_base_t base;
308 struct select_expression_t {
309 expression_base_t base;
310 expression_t *compound;
311 declaration_t *compound_entry;
314 struct array_access_expression_t {
315 expression_base_t base;
316 expression_t *array_ref;
318 bool flipped; /**< index/ref was written in a 5[a] way */
321 struct typeprop_expression_t {
322 expression_base_t base;
324 expression_t *tp_expression;
327 struct designator_t {
328 source_position_t source_position;
330 expression_t *array_index;
334 struct offsetof_expression_t {
335 expression_base_t base;
337 designator_t *designator;
340 struct va_start_expression_t {
341 expression_base_t base;
343 declaration_t *parameter;
346 struct va_arg_expression_t {
347 expression_base_t base;
351 struct conditional_expression_t {
352 expression_base_t base;
353 expression_t *condition;
354 expression_t *true_expression;
355 expression_t *false_expression;
358 struct statement_expression_t {
359 expression_base_t base;
360 statement_t *statement;
363 struct classify_type_expression_t {
364 expression_base_t base;
365 expression_t *type_expression;
368 struct label_address_expression_t {
369 expression_base_t base;
370 declaration_t *declaration;
374 expression_kind_t kind;
375 expression_base_t base;
376 const_expression_t conste;
377 funcname_expression_t funcname;
378 string_literal_expression_t string;
379 wide_string_literal_expression_t wide_string;
380 compound_literal_expression_t compound_literal;
381 builtin_symbol_expression_t builtin_symbol;
382 builtin_constant_expression_t builtin_constant;
383 builtin_prefetch_expression_t builtin_prefetch;
384 reference_expression_t reference;
385 call_expression_t call;
386 unary_expression_t unary;
387 binary_expression_t binary;
388 select_expression_t select;
389 array_access_expression_t array_access;
390 typeprop_expression_t typeprop;
391 offsetof_expression_t offsetofe;
392 va_start_expression_t va_starte;
393 va_arg_expression_t va_arge;
394 conditional_expression_t conditional;
395 statement_expression_t statement;
396 classify_type_expression_t classify_type;
397 label_address_expression_t label_address;
400 typedef enum storage_class_tag_t {
402 STORAGE_CLASS_EXTERN,
403 STORAGE_CLASS_STATIC,
404 STORAGE_CLASS_TYPEDEF,
406 STORAGE_CLASS_REGISTER,
407 STORAGE_CLASS_ENUM_ENTRY,
408 STORAGE_CLASS_THREAD,
409 STORAGE_CLASS_THREAD_EXTERN,
410 STORAGE_CLASS_THREAD_STATIC,
411 } storage_class_tag_t;
413 typedef enum namespace_t {
419 NAMESPACE_LOCAL_LABEL
422 typedef enum initializer_kind_t {
426 INITIALIZER_WIDE_STRING,
427 INITIALIZER_DESIGNATOR
428 } initializer_kind_t;
430 struct initializer_base_t {
431 initializer_kind_t kind;
434 struct initializer_value_t {
435 initializer_base_t base;
439 struct initializer_list_t {
440 initializer_base_t base;
442 initializer_t *initializers[];
445 struct initializer_string_t {
446 initializer_base_t base;
450 struct initializer_wide_string_t {
451 initializer_base_t base;
452 wide_string_t string;
455 struct initializer_designator_t {
456 initializer_base_t base;
457 designator_t *designator;
460 union initializer_t {
461 initializer_kind_t kind;
462 initializer_base_t base;
463 initializer_value_t value;
464 initializer_list_t list;
465 initializer_string_t string;
466 initializer_wide_string_t wide_string;
467 initializer_designator_t designator;
473 typedef enum gnu_attribute_kind_t {
484 GNU_AK_ALWAYS_INLINE,
490 GNU_AK_TRANSPARENT_UNION,
498 GNU_AK_NO_INSTRUMENT_FUNCTION,
499 GNU_AK_WARN_UNUSED_RESULT,
504 GNU_AK_FUNCTION_VECTOR,
506 GNU_AK_INTERRUPT_HANDLER,
512 GNU_AK_EIGTHBIT_DATA,
517 GNU_AK_EXTERNALLY_VISIBLE,
540 } gnu_attribute_kind_t;
543 * Extended microsoft modifier.
545 typedef enum decl_modifier_t {
546 DM_DLLIMPORT = 1 << 0,
547 DM_DLLEXPORT = 1 << 1,
550 DM_MICROSOFT_INLINE = 1 << 4,
551 DM_FORCEINLINE = 1 << 5,
552 DM_SELECTANY = 1 << 6,
554 DM_NOVTABLE = 1 << 8,
555 DM_NORETURN = 1 << 9,
556 DM_NOINLINE = 1 << 10,
557 DM_RESTRICT = 1 << 11,
558 DM_NOALIAS = 1 << 12,
560 DM_TRANSPARENT_UNION = 1 << 14,
563 DM_CONSTRUCTOR = 1 << 17,
564 DM_DESTRUCTOR = 1 << 18,
568 DM_FASTCALL = 1 << 22,
569 DM_STDCALL = 1 << 23,
570 DM_THISCALL = 1 << 24,
571 DM_DEPRECATED = 1 << 25
574 typedef unsigned decl_modifiers_t;
576 struct declaration_t {
577 unsigned char namespc;
578 unsigned char declared_storage_class;
579 unsigned char storage_class;
580 unsigned char alignment; /**< Alignment of the declaration, 0 for default. */
581 decl_modifiers_t modifiers; /**< modifiers. */
582 const char *deprecated_string; /**< MS deprecated string if any. */
583 symbol_t *get_property_sym; /**< MS get property. */
584 symbol_t *put_property_sym; /**< MS put property. */
585 unsigned int address_taken : 1; /**< Set if the address of this declaration was taken. */
586 unsigned int is_inline : 1;
587 unsigned int used : 1; /**< Set if the declaration is used. */
588 unsigned int read : 1;
589 unsigned int implicit : 1; /**< Set for implicit (not found in source code) declarations. */
590 unsigned int need_closure : 1; /**< Inner function needs closure. */
591 unsigned int goto_to_outer : 1; /**< Inner function has goto to outer function. */
593 il_size_t offset; /**< The offset of this member inside a compound. */
595 source_position_t source_position;
597 bool complete; /**< used to indicate whether struct/union types are already defined or if just the name is declared */
598 statement_t *statement;
599 initializer_t *initializer;
600 expression_t *enum_value;
602 scope_t scope; /**< The scope that this declaration opens. */
603 scope_t *parent_scope; /**< The parent scope where this declaration lives. */
605 /** next declaration in a scope */
607 /** next declaration with same symbol */
608 declaration_t *symbol_next;
610 /* the following fields are used in ast2firm module */
611 unsigned char declaration_kind;
613 unsigned int value_number;
622 typedef enum statement_kind_t {
627 STATEMENT_DECLARATION,
630 STATEMENT_EXPRESSION,
635 STATEMENT_CASE_LABEL,
640 STATEMENT_MS_TRY, /**< MS __try/__finally or __try/__except */
641 STATEMENT_LEAVE /**< MS __leave */
644 struct statement_base_t {
645 statement_kind_t kind;
647 source_position_t source_position;
655 struct invalid_statement_t {
656 statement_base_t base;
659 struct empty_statement_t {
660 statement_base_t base;
663 struct return_statement_t {
664 statement_base_t base;
668 struct compound_statement_t {
669 statement_base_t base;
670 statement_t *statements;
674 struct declaration_statement_t {
675 statement_base_t base;
676 declaration_t *declarations_begin;
677 declaration_t *declarations_end;
680 struct if_statement_t {
681 statement_base_t base;
682 expression_t *condition;
683 statement_t *true_statement;
684 statement_t *false_statement;
687 struct switch_statement_t {
688 statement_base_t base;
689 expression_t *expression;
691 case_label_statement_t *first_case, *last_case; /**< List of all cases, including default. */
692 case_label_statement_t *default_label; /**< The default label if existent. */
693 unsigned long default_proj_nr; /**< The Proj-number for the default Proj. */
696 struct goto_statement_t {
697 statement_base_t base;
698 declaration_t *label; /**< The destination label. */
699 expression_t *expression; /**< The expression for an assigned goto. */
700 goto_statement_t *next; /**< links all goto statements of a function */
703 struct case_label_statement_t {
704 statement_base_t base;
705 expression_t *expression; /**< The case label expression, NULL for default label. */
706 expression_t *end_range; /**< For GNUC case a .. b: the end range expression, NULL else. */
707 case_label_statement_t *next; /**< link to the next case label in switch */
708 statement_t *statement;
709 long first_case; /**< The folded value of expression. */
710 long last_case; /**< The folded value of end_range. */
711 bool is_bad; /**< If set marked as bad to suppress warnings. */
712 bool is_empty_range; /**< If set marked this as an empty range. */
715 struct label_statement_t {
716 statement_base_t base;
717 declaration_t *label;
718 statement_t *statement;
719 label_statement_t *next; /**< links all label statements of a function */
722 struct expression_statement_t {
723 statement_base_t base;
724 expression_t *expression;
727 struct while_statement_t {
728 statement_base_t base;
729 expression_t *condition;
733 struct do_while_statement_t {
734 statement_base_t base;
735 expression_t *condition;
739 struct for_statement_t {
740 statement_base_t base;
741 expression_t *initialisation;
742 expression_t *condition;
746 bool condition_reachable:1;
747 bool step_reachable:1;
750 struct asm_argument_t {
751 string_t constraints;
752 expression_t *expression;
754 asm_argument_t *next;
757 struct asm_clobber_t {
762 struct asm_statement_t {
763 statement_base_t base;
765 asm_argument_t *inputs;
766 asm_argument_t *outputs;
767 asm_clobber_t *clobbers;
771 struct ms_try_statement_t {
772 statement_base_t base;
773 statement_t *try_statement;
774 expression_t *except_expression; /**< non-null for except, NULL for finally */
775 statement_t *final_statement;
778 struct leave_statement_t {
779 statement_base_t base;
783 statement_kind_t kind;
784 statement_base_t base;
785 return_statement_t returns;
786 compound_statement_t compound;
787 declaration_statement_t declaration;
789 switch_statement_t switchs;
790 goto_statement_t gotos;
791 case_label_statement_t case_label;
792 label_statement_t label;
793 expression_statement_t expression;
794 while_statement_t whiles;
795 do_while_statement_t do_while;
796 for_statement_t fors;
797 asm_statement_t asms;
798 ms_try_statement_t ms_try;
799 leave_statement_t leave;
802 struct translation_unit_t {
804 statement_t *global_asm;
808 void *_allocate_ast(size_t size)
810 return obstack_alloc(&ast_obstack, size);
814 bool is_invalid_expression(expression_t *expression)
816 return expression->base.kind == EXPR_INVALID;
820 bool is_invalid_statement(statement_t *statement)
822 return statement->base.kind == STATEMENT_INVALID;
826 #define allocate_ast(size) _allocate_ast(size)