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;
38 typedef enum expression_kind_t {
43 EXPR_CHARACTER_CONSTANT,
44 EXPR_WIDE_CHARACTER_CONSTANT,
46 EXPR_WIDE_STRING_LITERAL,
47 EXPR_COMPOUND_LITERAL,
58 EXPR_BUILTIN_CONSTANT_P,
59 EXPR_BUILTIN_PREFETCH,
64 EXPR_LABEL_ADDRESS, /**< GCC extension &&label operator */
67 EXPR_UNARY_NEGATE = EXPR_UNARY_FIRST,
69 EXPR_UNARY_BITWISE_NEGATE,
71 EXPR_UNARY_DEREFERENCE,
72 EXPR_UNARY_TAKE_ADDRESS,
73 EXPR_UNARY_POSTFIX_INCREMENT,
74 EXPR_UNARY_POSTFIX_DECREMENT,
75 EXPR_UNARY_PREFIX_INCREMENT,
76 EXPR_UNARY_PREFIX_DECREMENT,
78 EXPR_UNARY_CAST_IMPLICIT, /**< compiler generated cast */
79 EXPR_UNARY_ASSUME, /**< MS __assume() */
80 EXPR_UNARY_LAST = EXPR_UNARY_ASSUME,
83 EXPR_BINARY_ADD = EXPR_BINARY_FIRST,
91 EXPR_BINARY_LESSEQUAL,
93 EXPR_BINARY_GREATEREQUAL,
94 EXPR_BINARY_BITWISE_AND,
95 EXPR_BINARY_BITWISE_OR,
96 EXPR_BINARY_BITWISE_XOR,
97 EXPR_BINARY_LOGICAL_AND,
98 EXPR_BINARY_LOGICAL_OR,
99 EXPR_BINARY_SHIFTLEFT,
100 EXPR_BINARY_SHIFTRIGHT,
102 EXPR_BINARY_MUL_ASSIGN,
103 EXPR_BINARY_DIV_ASSIGN,
104 EXPR_BINARY_MOD_ASSIGN,
105 EXPR_BINARY_ADD_ASSIGN,
106 EXPR_BINARY_SUB_ASSIGN,
107 EXPR_BINARY_SHIFTLEFT_ASSIGN,
108 EXPR_BINARY_SHIFTRIGHT_ASSIGN,
109 EXPR_BINARY_BITWISE_AND_ASSIGN,
110 EXPR_BINARY_BITWISE_XOR_ASSIGN,
111 EXPR_BINARY_BITWISE_OR_ASSIGN,
114 EXPR_BINARY_BUILTIN_EXPECT,
115 EXPR_BINARY_ISGREATER,
116 EXPR_BINARY_ISGREATEREQUAL,
118 EXPR_BINARY_ISLESSEQUAL,
119 EXPR_BINARY_ISLESSGREATER,
120 EXPR_BINARY_ISUNORDERED,
121 EXPR_BINARY_LAST = EXPR_BINARY_ISUNORDERED,
124 typedef enum funcname_kind_t {
125 FUNCNAME_FUNCTION, /**< C99 __func__, older __FUNCTION__ */
126 FUNCNAME_PRETTY_FUNCTION, /**< GNUC __PRETTY_FUNCTION__ */
127 FUNCNAME_FUNCSIG, /**< MS __FUNCSIG__ */
128 FUNCNAME_FUNCDNAME /**< MS __FUNCDNAME__ */
131 /* convenience macros */
132 #define EXPR_BINARY_CASES \
133 case EXPR_BINARY_ADD: \
134 case EXPR_BINARY_SUB: \
135 case EXPR_BINARY_MUL: \
136 case EXPR_BINARY_DIV: \
137 case EXPR_BINARY_MOD: \
138 case EXPR_BINARY_EQUAL: \
139 case EXPR_BINARY_NOTEQUAL: \
140 case EXPR_BINARY_LESS: \
141 case EXPR_BINARY_LESSEQUAL: \
142 case EXPR_BINARY_GREATER: \
143 case EXPR_BINARY_GREATEREQUAL: \
144 case EXPR_BINARY_BITWISE_AND: \
145 case EXPR_BINARY_BITWISE_OR: \
146 case EXPR_BINARY_BITWISE_XOR: \
147 case EXPR_BINARY_LOGICAL_AND: \
148 case EXPR_BINARY_LOGICAL_OR: \
149 case EXPR_BINARY_SHIFTLEFT: \
150 case EXPR_BINARY_SHIFTRIGHT: \
151 case EXPR_BINARY_ASSIGN: \
152 case EXPR_BINARY_MUL_ASSIGN: \
153 case EXPR_BINARY_DIV_ASSIGN: \
154 case EXPR_BINARY_MOD_ASSIGN: \
155 case EXPR_BINARY_ADD_ASSIGN: \
156 case EXPR_BINARY_SUB_ASSIGN: \
157 case EXPR_BINARY_SHIFTLEFT_ASSIGN: \
158 case EXPR_BINARY_SHIFTRIGHT_ASSIGN: \
159 case EXPR_BINARY_BITWISE_AND_ASSIGN: \
160 case EXPR_BINARY_BITWISE_XOR_ASSIGN: \
161 case EXPR_BINARY_BITWISE_OR_ASSIGN: \
162 case EXPR_BINARY_COMMA: \
163 case EXPR_BINARY_BUILTIN_EXPECT: \
164 case EXPR_BINARY_ISGREATER: \
165 case EXPR_BINARY_ISGREATEREQUAL: \
166 case EXPR_BINARY_ISLESS: \
167 case EXPR_BINARY_ISLESSEQUAL: \
168 case EXPR_BINARY_ISLESSGREATER: \
169 case EXPR_BINARY_ISUNORDERED:
171 #define EXPR_UNARY_CASES \
172 case EXPR_UNARY_NEGATE: \
173 case EXPR_UNARY_PLUS: \
174 case EXPR_UNARY_BITWISE_NEGATE: \
175 case EXPR_UNARY_NOT: \
176 case EXPR_UNARY_DEREFERENCE: \
177 case EXPR_UNARY_TAKE_ADDRESS: \
178 case EXPR_UNARY_POSTFIX_INCREMENT: \
179 case EXPR_UNARY_POSTFIX_DECREMENT: \
180 case EXPR_UNARY_PREFIX_INCREMENT: \
181 case EXPR_UNARY_PREFIX_DECREMENT: \
182 case EXPR_UNARY_CAST: \
183 case EXPR_UNARY_CAST_IMPLICIT: \
184 case EXPR_UNARY_ASSUME:
187 * A scope containing declarations.
190 declaration_t *declarations; /**< List of declarations in this scope. */
191 declaration_t *last_declaration; /**< last declaration in this scope. */
194 struct expression_base_t {
195 expression_kind_t kind;
197 source_position_t source_position;
203 struct const_expression_t {
204 expression_base_t base;
207 long double float_value;
209 wide_string_t wide_character;
211 bool is_ms_noop; /**< True, if this constant is the result
212 of an microsoft __noop operator */
215 struct string_literal_expression_t {
216 expression_base_t base;
220 struct funcname_expression_t {
221 expression_base_t base;
222 funcname_kind_t kind;
223 string_t value; /**< the value once assigned. */
226 struct wide_string_literal_expression_t {
227 expression_base_t base;
231 struct compound_literal_expression_t {
232 expression_base_t base;
234 initializer_t *initializer;
237 struct builtin_symbol_expression_t {
238 expression_base_t base;
242 struct builtin_constant_expression_t {
243 expression_base_t base;
247 struct builtin_prefetch_expression_t {
248 expression_base_t base;
251 expression_t *locality;
254 struct reference_expression_t {
255 expression_base_t base;
256 declaration_t *declaration;
259 struct call_argument_t {
260 expression_t *expression;
261 call_argument_t *next;
264 struct call_expression_t {
265 expression_base_t base;
266 expression_t *function;
267 call_argument_t *arguments;
270 struct unary_expression_t {
271 expression_base_t base;
275 struct binary_expression_t {
276 expression_base_t base;
281 struct select_expression_t {
282 expression_base_t base;
283 expression_t *compound;
284 declaration_t *compound_entry;
287 struct array_access_expression_t {
288 expression_base_t base;
289 expression_t *array_ref;
291 bool flipped; /**< index/ref was written in a 5[a] way */
294 struct typeprop_expression_t {
295 expression_base_t base;
297 expression_t *tp_expression;
300 struct designator_t {
301 source_position_t source_position;
303 expression_t *array_index;
307 struct offsetof_expression_t {
308 expression_base_t base;
310 designator_t *designator;
313 struct va_start_expression_t {
314 expression_base_t base;
316 declaration_t *parameter;
319 struct va_arg_expression_t {
320 expression_base_t base;
324 struct conditional_expression_t {
325 expression_base_t base;
326 expression_t *condition;
327 expression_t *true_expression;
328 expression_t *false_expression;
331 struct statement_expression_t {
332 expression_base_t base;
333 statement_t *statement;
336 struct classify_type_expression_t {
337 expression_base_t base;
338 expression_t *type_expression;
341 struct label_address_expression_t {
342 expression_base_t base;
343 declaration_t *declaration;
347 expression_kind_t kind;
348 expression_base_t base;
349 const_expression_t conste;
350 funcname_expression_t funcname;
351 string_literal_expression_t string;
352 wide_string_literal_expression_t wide_string;
353 compound_literal_expression_t compound_literal;
354 builtin_symbol_expression_t builtin_symbol;
355 builtin_constant_expression_t builtin_constant;
356 builtin_prefetch_expression_t builtin_prefetch;
357 reference_expression_t reference;
358 call_expression_t call;
359 unary_expression_t unary;
360 binary_expression_t binary;
361 select_expression_t select;
362 array_access_expression_t array_access;
363 typeprop_expression_t typeprop;
364 offsetof_expression_t offsetofe;
365 va_start_expression_t va_starte;
366 va_arg_expression_t va_arge;
367 conditional_expression_t conditional;
368 statement_expression_t statement;
369 classify_type_expression_t classify_type;
370 label_address_expression_t label_address;
373 typedef enum storage_class_tag_t {
375 STORAGE_CLASS_EXTERN,
376 STORAGE_CLASS_STATIC,
377 STORAGE_CLASS_TYPEDEF,
379 STORAGE_CLASS_REGISTER,
380 STORAGE_CLASS_ENUM_ENTRY,
381 STORAGE_CLASS_THREAD,
382 STORAGE_CLASS_THREAD_EXTERN,
383 STORAGE_CLASS_THREAD_STATIC,
384 } storage_class_tag_t;
386 typedef enum namespace_t {
392 NAMESPACE_LOCAL_LABEL
395 typedef enum initializer_kind_t {
399 INITIALIZER_WIDE_STRING,
400 INITIALIZER_DESIGNATOR
401 } initializer_kind_t;
403 struct initializer_base_t {
404 initializer_kind_t kind;
407 struct initializer_value_t {
408 initializer_base_t base;
412 struct initializer_list_t {
413 initializer_base_t base;
415 initializer_t *initializers[];
418 struct initializer_string_t {
419 initializer_base_t base;
423 struct initializer_wide_string_t {
424 initializer_base_t base;
425 wide_string_t string;
428 struct initializer_designator_t {
429 initializer_base_t base;
430 designator_t *designator;
433 union initializer_t {
434 initializer_kind_t kind;
435 initializer_base_t base;
436 initializer_value_t value;
437 initializer_list_t list;
438 initializer_string_t string;
439 initializer_wide_string_t wide_string;
440 initializer_designator_t designator;
446 typedef enum gnu_attribute_kind_t {
457 GNU_AK_ALWAYS_INLINE,
463 GNU_AK_TRANSPARENT_UNION,
471 GNU_AK_NO_INSTRUMENT_FUNCTION,
472 GNU_AK_WARN_UNUSED_RESULT,
477 GNU_AK_FUNCTION_VECTOR,
479 GNU_AK_INTERRUPT_HANDLER,
485 GNU_AK_EIGTHBIT_DATA,
490 GNU_AK_EXTERNALLY_VISIBLE,
513 } gnu_attribute_kind_t;
516 * Extended microsoft modifier.
518 typedef enum decl_modifier_t {
519 DM_DLLIMPORT = 1 << 0,
520 DM_DLLEXPORT = 1 << 1,
523 DM_MICROSOFT_INLINE = 1 << 4,
524 DM_FORCEINLINE = 1 << 5,
525 DM_SELECTANY = 1 << 6,
527 DM_NOVTABLE = 1 << 8,
528 DM_NORETURN = 1 << 9,
529 DM_NOINLINE = 1 << 10,
530 DM_RESTRICT = 1 << 11,
531 DM_NOALIAS = 1 << 12,
533 DM_TRANSPARENT_UNION = 1 << 14,
535 DM_CONSTRUCTOR = 1 << 16,
536 DM_DESTRUCTOR = 1 << 17,
540 DM_FASTCALL = 1 << 21,
541 DM_STDCALL = 1 << 22,
542 DM_THISCALL = 1 << 23,
543 DM_DEPRECATED = 1 << 24
546 typedef unsigned decl_modifiers_t;
548 struct declaration_t {
549 unsigned char namespc;
550 unsigned char declared_storage_class;
551 unsigned char storage_class;
552 unsigned char alignment; /**< Alignment of the declaration, 0 for default. */
553 decl_modifiers_t modifiers; /**< modifiers. */
554 const char *deprecated_string; /**< MS deprecated string if any. */
555 symbol_t *get_property_sym; /**< MS get property. */
556 symbol_t *put_property_sym; /**< MS put property. */
557 unsigned int address_taken : 1; /**< Set if the address of this declaration was taken. */
558 unsigned int is_inline : 1;
559 unsigned int used : 1; /**< Set if the declaration is used. */
560 unsigned int implicit : 1; /**< Set for implicit (not found in source code) declarations. */
562 il_size_t offset; /**< The offset of this member inside a compound. */
564 string_t *asm_name; /**< GCC extension: ASM label. */
565 source_position_t source_position;
567 bool complete; /**< used to indicate whether struct/union types are already defined or if just the name is declared */
568 statement_t *statement;
569 initializer_t *initializer;
570 expression_t *enum_value;
572 scope_t scope; /**< The scope that this declaration opens. */
573 scope_t *parent_scope; /**< The parent scope where this declaration lives. */
575 /** next declaration in a scope */
577 /** next declaration with same symbol */
578 declaration_t *symbol_next;
580 /* the following fields are used in ast2firm module */
581 unsigned char declaration_kind;
583 unsigned int value_number;
592 typedef enum statement_kind_t {
597 STATEMENT_DECLARATION,
600 STATEMENT_EXPRESSION,
605 STATEMENT_CASE_LABEL,
610 STATEMENT_MS_TRY, /**< MS __try/__finally or __try/__except */
611 STATEMENT_LEAVE /**< MS __leave */
614 struct statement_base_t {
615 statement_kind_t kind;
617 source_position_t source_position;
625 struct invalid_statement_t {
626 statement_base_t base;
629 struct empty_statement_t {
630 statement_base_t base;
633 struct return_statement_t {
634 statement_base_t base;
638 struct compound_statement_t {
639 statement_base_t base;
640 statement_t *statements;
644 struct declaration_statement_t {
645 statement_base_t base;
646 declaration_t *declarations_begin;
647 declaration_t *declarations_end;
650 struct if_statement_t {
651 statement_base_t base;
652 expression_t *condition;
653 statement_t *true_statement;
654 statement_t *false_statement;
657 struct switch_statement_t {
658 statement_base_t base;
659 expression_t *expression;
661 case_label_statement_t *first_case, *last_case; /**< List of all cases, including default. */
662 case_label_statement_t *default_label; /**< The default label if existent. */
663 unsigned long default_proj_nr; /**< The Proj-number for the default Proj. */
666 struct goto_statement_t {
667 statement_base_t base;
668 declaration_t *label; /**< The destination label. */
669 expression_t *expression; /**< The expression for an assigned goto. */
670 goto_statement_t *next; /**< links all goto statements of a function */
673 struct case_label_statement_t {
674 statement_base_t base;
675 expression_t *expression; /**< The case label expression, NULL for default label. */
676 expression_t *end_range; /**< For GNUC case a .. b: the end range expression, NULL else. */
677 case_label_statement_t *next; /**< link to the next case label in switch */
678 statement_t *statement;
679 long first_case; /**< The folded value of expression. */
680 long last_case; /**< The folded value of end_range. */
681 bool is_bad; /**< If set marked as bad to suppress warnings. */
682 bool is_empty_range; /**< If set marked this as an empty range. */
685 struct label_statement_t {
686 statement_base_t base;
687 declaration_t *label;
688 statement_t *statement;
689 label_statement_t *next; /**< links all label statements of a function */
692 struct expression_statement_t {
693 statement_base_t base;
694 expression_t *expression;
697 struct while_statement_t {
698 statement_base_t base;
699 expression_t *condition;
703 struct do_while_statement_t {
704 statement_base_t base;
705 expression_t *condition;
709 struct for_statement_t {
710 statement_base_t base;
711 expression_t *initialisation;
712 expression_t *condition;
716 bool condition_reachable:1;
717 bool step_reachable:1;
720 struct asm_argument_t {
721 string_t constraints;
722 expression_t *expression;
724 asm_argument_t *next;
727 struct asm_clobber_t {
732 struct asm_statement_t {
733 statement_base_t base;
735 asm_argument_t *inputs;
736 asm_argument_t *outputs;
737 asm_clobber_t *clobbers;
741 struct ms_try_statement_t {
742 statement_base_t base;
743 statement_t *try_statement;
744 expression_t *except_expression; /**< non-null for except, NULL for finally */
745 statement_t *final_statement;
748 struct leave_statement_t {
749 statement_base_t base;
753 statement_kind_t kind;
754 statement_base_t base;
755 return_statement_t returns;
756 compound_statement_t compound;
757 declaration_statement_t declaration;
759 switch_statement_t switchs;
760 goto_statement_t gotos;
761 case_label_statement_t case_label;
762 label_statement_t label;
763 expression_statement_t expression;
764 while_statement_t whiles;
765 do_while_statement_t do_while;
766 for_statement_t fors;
767 asm_statement_t asms;
768 ms_try_statement_t ms_try;
769 leave_statement_t leave;
772 struct translation_unit_t {
774 statement_t *global_asm;
778 void *_allocate_ast(size_t size)
780 return obstack_alloc(&ast_obstack, size);
784 bool is_invalid_expression(expression_t *expression)
786 return expression->base.kind == EXPR_INVALID;
790 bool is_invalid_statement(statement_t *statement)
792 return statement->base.kind == STATEMENT_INVALID;
796 #define allocate_ast(size) _allocate_ast(size)