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;
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,
66 EXPR_UNARY_NEGATE = EXPR_UNARY_FIRST,
68 EXPR_UNARY_BITWISE_NEGATE,
70 EXPR_UNARY_DEREFERENCE,
71 EXPR_UNARY_TAKE_ADDRESS,
72 EXPR_UNARY_POSTFIX_INCREMENT,
73 EXPR_UNARY_POSTFIX_DECREMENT,
74 EXPR_UNARY_PREFIX_INCREMENT,
75 EXPR_UNARY_PREFIX_DECREMENT,
77 EXPR_UNARY_CAST_IMPLICIT, /**< compiler generated cast */
78 EXPR_UNARY_ASSUME, /**< MS __assume() */
79 EXPR_UNARY_BITFIELD_EXTRACT,
80 EXPR_UNARY_LAST = EXPR_UNARY_BITFIELD_EXTRACT,
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,
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: \
185 case EXPR_UNARY_BITFIELD_EXTRACT:
188 * A scope containing declarations.
191 declaration_t *declarations; /**< List of declarations in this scope. */
192 declaration_t *last_declaration; /**< last declaration in this scope. */
195 struct expression_base_t {
196 expression_kind_t kind;
198 source_position_t source_position;
201 struct const_expression_t {
202 expression_base_t base;
205 long double float_value;
207 wide_string_t wide_character;
209 bool is_ms_noop; /**< True, if this constant is the result
210 of an microsoft __noop operator */
213 struct string_literal_expression_t {
214 expression_base_t base;
218 struct funcname_expression_t {
219 expression_base_t base;
220 funcname_kind_t kind;
221 string_t value; /**< the value once assigned. */
224 struct wide_string_literal_expression_t {
225 expression_base_t base;
229 struct compound_literal_expression_t {
230 expression_base_t base;
232 initializer_t *initializer;
235 struct builtin_symbol_expression_t {
236 expression_base_t base;
240 struct builtin_constant_expression_t {
241 expression_base_t base;
245 struct builtin_prefetch_expression_t {
246 expression_base_t base;
249 expression_t *locality;
252 struct reference_expression_t {
253 expression_base_t base;
255 declaration_t *declaration;
258 struct call_argument_t {
259 expression_t *expression;
260 call_argument_t *next;
263 struct call_expression_t {
264 expression_base_t base;
265 expression_t *function;
266 call_argument_t *arguments;
269 struct unary_expression_t {
270 expression_base_t base;
274 struct binary_expression_t {
275 expression_base_t base;
280 struct select_expression_t {
281 expression_base_t base;
282 expression_t *compound;
285 declaration_t *compound_entry;
288 struct array_access_expression_t {
289 expression_base_t base;
290 expression_t *array_ref;
292 bool flipped; /* index/ref was written in a 5[a] way */
295 struct typeprop_expression_t {
296 expression_base_t base;
298 expression_t *tp_expression;
301 struct designator_t {
302 source_position_t source_position;
304 expression_t *array_index;
308 struct offsetof_expression_t {
309 expression_base_t base;
311 designator_t *designator;
314 struct va_start_expression_t {
315 expression_base_t base;
317 declaration_t *parameter;
320 struct va_arg_expression_t {
321 expression_base_t base;
325 struct conditional_expression_t {
326 expression_base_t base;
327 expression_t *condition;
328 expression_t *true_expression;
329 expression_t *false_expression;
332 struct statement_expression_t {
333 expression_base_t base;
334 statement_t *statement;
337 struct classify_type_expression_t {
338 expression_base_t base;
339 expression_t *type_expression;
343 expression_kind_t kind;
344 expression_base_t base;
345 const_expression_t conste;
346 funcname_expression_t funcname;
347 string_literal_expression_t string;
348 wide_string_literal_expression_t wide_string;
349 compound_literal_expression_t compound_literal;
350 builtin_symbol_expression_t builtin_symbol;
351 builtin_constant_expression_t builtin_constant;
352 builtin_prefetch_expression_t builtin_prefetch;
353 reference_expression_t reference;
354 call_expression_t call;
355 unary_expression_t unary;
356 binary_expression_t binary;
357 select_expression_t select;
358 array_access_expression_t array_access;
359 typeprop_expression_t typeprop;
360 offsetof_expression_t offsetofe;
361 va_start_expression_t va_starte;
362 va_arg_expression_t va_arge;
363 conditional_expression_t conditional;
364 statement_expression_t statement;
365 classify_type_expression_t classify_type;
370 STORAGE_CLASS_EXTERN,
371 STORAGE_CLASS_STATIC,
372 STORAGE_CLASS_TYPEDEF,
374 STORAGE_CLASS_REGISTER,
375 STORAGE_CLASS_ENUM_ENTRY,
376 STORAGE_CLASS_THREAD,
377 STORAGE_CLASS_THREAD_EXTERN,
378 STORAGE_CLASS_THREAD_STATIC,
379 } storage_class_tag_t;
393 INITIALIZER_WIDE_STRING,
394 INITIALIZER_DESIGNATOR
395 } initializer_kind_t;
397 struct initializer_base_t {
398 initializer_kind_t kind;
401 struct initializer_value_t {
402 initializer_base_t base;
406 struct initializer_list_t {
407 initializer_base_t base;
409 initializer_t *initializers[];
412 struct initializer_string_t {
413 initializer_base_t base;
417 struct initializer_wide_string_t {
418 initializer_base_t base;
419 wide_string_t string;
422 struct initializer_designator_t {
423 initializer_base_t base;
424 designator_t *designator;
427 union initializer_t {
428 initializer_kind_t kind;
429 initializer_base_t base;
430 initializer_value_t value;
431 initializer_list_t list;
432 initializer_string_t string;
433 initializer_wide_string_t wide_string;
434 initializer_designator_t designator;
440 typedef enum gnu_attribute_kind_t {
451 GNU_AK_ALWAYS_INLINE,
457 GNU_AK_TRANSPARENT_UNION,
465 GNU_AK_NO_INSTRUMENT_FUNCTION,
466 GNU_AK_WARN_UNUSED_RESULT,
471 GNU_AK_FUNCTION_VECTOR,
473 GNU_AK_INTERRUPT_HANDLER,
479 GNU_AK_EIGTHBIT_DATA,
484 GNU_AK_EXTERNALLY_VISIBLE,
504 } gnu_attribute_kind_t;
507 * Extended microsoft modifier.
510 DM_DLLIMPORT = (1 << 0),
511 DM_DLLEXPORT = (1 << 1),
512 DM_THREAD = (1 << 2),
514 DM_MICROSOFT_INLINE = (1 << 4),
515 DM_FORCEINLINE = (1 << 5),
516 DM_SELECTANY = (1 << 6),
517 DM_NOTHROW = (1 << 7),
518 DM_NOVTABLE = (1 << 8),
519 DM_NORETURN = (1 << 9),
520 DM_NOINLINE = (1 << 10),
521 DM_RESTRICT = (1 << 11),
522 DM_NOALIAS = (1 << 12)
525 typedef unsigned short decl_modifiers_t;
527 struct declaration_t {
528 unsigned char namespc;
529 unsigned char declared_storage_class;
530 unsigned char storage_class;
531 unsigned char alignment; /**< Alignment of the declaration, 0 for default. */
532 decl_modifiers_t modifiers; /**< MS __declspec modifiers. */
533 const char *deprecated_string; /**< MS deprecated string if any. */
534 symbol_t *get_property_sym; /**< MS get property. */
535 symbol_t *put_property_sym; /**< MS put property. */
536 unsigned int address_taken : 1;
537 unsigned int is_inline : 1;
538 unsigned int used : 1; /**< Set if the declaration is used. */
539 unsigned int deprecated : 1; /**< Microsoft of GNU deprecated attribute. */
542 source_position_t source_position;
545 statement_t *statement;
546 initializer_t *initializer;
547 expression_t *enum_value;
549 scope_t scope; /**< The scope that this declaration opens. */
550 scope_t *parent_scope; /**< The parant scope where this declaration lives. */
552 /** next declaration in a scope */
554 /** next declaration with same symbol */
555 declaration_t *symbol_next;
557 /* the following fields are used in ast2firm module */
558 unsigned char declaration_kind;
560 unsigned int value_number;
574 STATEMENT_DECLARATION,
577 STATEMENT_EXPRESSION,
582 STATEMENT_CASE_LABEL,
589 struct statement_base_t {
590 statement_kind_t kind;
592 source_position_t source_position;
595 struct invalid_statement_t {
596 statement_base_t base;
599 struct empty_statement_t {
600 statement_base_t base;
603 struct return_statement_t {
604 statement_base_t base;
608 struct compound_statement_t {
609 statement_base_t base;
610 statement_t *statements;
614 struct declaration_statement_t {
615 statement_base_t base;
616 declaration_t *declarations_begin;
617 declaration_t *declarations_end;
620 struct if_statement_t {
621 statement_base_t base;
622 expression_t *condition;
623 statement_t *true_statement;
624 statement_t *false_statement;
627 struct switch_statement_t {
628 statement_base_t base;
629 expression_t *expression;
631 case_label_statement_t *first_case, *last_case;
634 struct goto_statement_t {
635 statement_base_t base;
636 declaration_t *label; /**< The destination label. */
637 goto_statement_t *next; /**< links all goto statements of a function */
640 struct case_label_statement_t {
641 statement_base_t base;
642 expression_t *expression; /**< The case label expression, NULL for default label. */
643 expression_t *end_range; /**< For GNUC case a .. b: the end range expression, NULL else. */
644 statement_t *statement;
645 case_label_statement_t *next; /**< link to the next case label in switch */
648 struct label_statement_t {
649 statement_base_t base;
650 declaration_t *label;
651 statement_t *statement;
652 label_statement_t *next; /**< links all label statements of a function */
655 struct expression_statement_t {
656 statement_base_t base;
657 expression_t *expression;
660 struct while_statement_t {
661 statement_base_t base;
662 expression_t *condition;
666 struct do_while_statement_t {
667 statement_base_t base;
668 expression_t *condition;
672 struct for_statement_t {
673 statement_base_t base;
674 expression_t *initialisation;
675 expression_t *condition;
681 struct asm_constraint_t {
682 string_t constraints;
683 expression_t *expression;
685 asm_constraint_t *next;
688 struct asm_clobber_t {
693 struct asm_statement_t {
694 statement_base_t base;
696 asm_constraint_t *inputs;
697 asm_constraint_t *outputs;
698 asm_clobber_t *clobbers;
703 statement_kind_t kind;
704 statement_base_t base;
705 return_statement_t returns;
706 compound_statement_t compound;
707 declaration_statement_t declaration;
709 switch_statement_t switchs;
710 goto_statement_t gotos;
711 case_label_statement_t case_label;
712 label_statement_t label;
713 expression_statement_t expression;
714 while_statement_t whiles;
715 do_while_statement_t do_while;
716 for_statement_t fors;
717 asm_statement_t asms;
720 struct translation_unit_t {
725 void *_allocate_ast(size_t size)
727 return obstack_alloc(&ast_obstack, size);
731 bool is_invalid_expression(expression_t *expression)
733 return expression->base.kind == EXPR_INVALID;
737 bool is_invalid_statement(statement_t *statement)
739 return statement->base.kind == STATEMENT_INVALID;
743 #define allocate_ast(size) _allocate_ast(size)