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 extern struct obstack ast_obstack;
39 EXPR_CHARACTER_CONSTANT,
40 EXPR_WIDE_CHARACTER_CONSTANT,
42 EXPR_WIDE_STRING_LITERAL,
43 EXPR_COMPOUND_LITERAL,
54 EXPR_BUILTIN_CONSTANT_P,
55 EXPR_BUILTIN_PREFETCH,
62 EXPR_UNARY_NEGATE = EXPR_UNARY_FIRST,
64 EXPR_UNARY_BITWISE_NEGATE,
66 EXPR_UNARY_DEREFERENCE,
67 EXPR_UNARY_TAKE_ADDRESS,
68 EXPR_UNARY_POSTFIX_INCREMENT,
69 EXPR_UNARY_POSTFIX_DECREMENT,
70 EXPR_UNARY_PREFIX_INCREMENT,
71 EXPR_UNARY_PREFIX_DECREMENT,
73 EXPR_UNARY_CAST_IMPLICIT, /**< compiler generated cast */
74 EXPR_UNARY_ASSUME, /**< MS __assume() */
75 EXPR_UNARY_BITFIELD_EXTRACT,
76 EXPR_UNARY_LAST = EXPR_UNARY_BITFIELD_EXTRACT,
79 EXPR_BINARY_ADD = EXPR_BINARY_FIRST,
87 EXPR_BINARY_LESSEQUAL,
89 EXPR_BINARY_GREATEREQUAL,
90 EXPR_BINARY_BITWISE_AND,
91 EXPR_BINARY_BITWISE_OR,
92 EXPR_BINARY_BITWISE_XOR,
93 EXPR_BINARY_LOGICAL_AND,
94 EXPR_BINARY_LOGICAL_OR,
95 EXPR_BINARY_SHIFTLEFT,
96 EXPR_BINARY_SHIFTRIGHT,
98 EXPR_BINARY_MUL_ASSIGN,
99 EXPR_BINARY_DIV_ASSIGN,
100 EXPR_BINARY_MOD_ASSIGN,
101 EXPR_BINARY_ADD_ASSIGN,
102 EXPR_BINARY_SUB_ASSIGN,
103 EXPR_BINARY_SHIFTLEFT_ASSIGN,
104 EXPR_BINARY_SHIFTRIGHT_ASSIGN,
105 EXPR_BINARY_BITWISE_AND_ASSIGN,
106 EXPR_BINARY_BITWISE_XOR_ASSIGN,
107 EXPR_BINARY_BITWISE_OR_ASSIGN,
110 EXPR_BINARY_BUILTIN_EXPECT,
111 EXPR_BINARY_ISGREATER,
112 EXPR_BINARY_ISGREATEREQUAL,
114 EXPR_BINARY_ISLESSEQUAL,
115 EXPR_BINARY_ISLESSGREATER,
116 EXPR_BINARY_ISUNORDERED,
117 EXPR_BINARY_LAST = EXPR_BINARY_ISUNORDERED,
121 FUNCNAME_FUNCTION, /**< C99 __func__, older __FUNCTION__ */
122 FUNCNAME_PRETTY_FUNCTION, /**< GNUC __PRETTY_FUNCTION__ */
123 FUNCNAME_FUNCSIG, /**< MS __FUNCSIG__ */
124 FUNCNAME_FUNCDNAME /**< MS __FUNCDNAME__ */
127 /* convenience macros */
128 #define EXPR_BINARY_CASES \
129 case EXPR_BINARY_ADD: \
130 case EXPR_BINARY_SUB: \
131 case EXPR_BINARY_MUL: \
132 case EXPR_BINARY_DIV: \
133 case EXPR_BINARY_MOD: \
134 case EXPR_BINARY_EQUAL: \
135 case EXPR_BINARY_NOTEQUAL: \
136 case EXPR_BINARY_LESS: \
137 case EXPR_BINARY_LESSEQUAL: \
138 case EXPR_BINARY_GREATER: \
139 case EXPR_BINARY_GREATEREQUAL: \
140 case EXPR_BINARY_BITWISE_AND: \
141 case EXPR_BINARY_BITWISE_OR: \
142 case EXPR_BINARY_BITWISE_XOR: \
143 case EXPR_BINARY_LOGICAL_AND: \
144 case EXPR_BINARY_LOGICAL_OR: \
145 case EXPR_BINARY_SHIFTLEFT: \
146 case EXPR_BINARY_SHIFTRIGHT: \
147 case EXPR_BINARY_ASSIGN: \
148 case EXPR_BINARY_MUL_ASSIGN: \
149 case EXPR_BINARY_DIV_ASSIGN: \
150 case EXPR_BINARY_MOD_ASSIGN: \
151 case EXPR_BINARY_ADD_ASSIGN: \
152 case EXPR_BINARY_SUB_ASSIGN: \
153 case EXPR_BINARY_SHIFTLEFT_ASSIGN: \
154 case EXPR_BINARY_SHIFTRIGHT_ASSIGN: \
155 case EXPR_BINARY_BITWISE_AND_ASSIGN: \
156 case EXPR_BINARY_BITWISE_XOR_ASSIGN: \
157 case EXPR_BINARY_BITWISE_OR_ASSIGN: \
158 case EXPR_BINARY_COMMA: \
159 case EXPR_BINARY_BUILTIN_EXPECT: \
160 case EXPR_BINARY_ISGREATER: \
161 case EXPR_BINARY_ISGREATEREQUAL: \
162 case EXPR_BINARY_ISLESS: \
163 case EXPR_BINARY_ISLESSEQUAL: \
164 case EXPR_BINARY_ISLESSGREATER: \
165 case EXPR_BINARY_ISUNORDERED:
167 #define EXPR_UNARY_CASES \
168 case EXPR_UNARY_NEGATE: \
169 case EXPR_UNARY_PLUS: \
170 case EXPR_UNARY_BITWISE_NEGATE: \
171 case EXPR_UNARY_NOT: \
172 case EXPR_UNARY_DEREFERENCE: \
173 case EXPR_UNARY_TAKE_ADDRESS: \
174 case EXPR_UNARY_POSTFIX_INCREMENT: \
175 case EXPR_UNARY_POSTFIX_DECREMENT: \
176 case EXPR_UNARY_PREFIX_INCREMENT: \
177 case EXPR_UNARY_PREFIX_DECREMENT: \
178 case EXPR_UNARY_CAST: \
179 case EXPR_UNARY_CAST_IMPLICIT: \
180 case EXPR_UNARY_ASSUME: \
181 case EXPR_UNARY_BITFIELD_EXTRACT:
184 * A scope containing declarations.
187 declaration_t *declarations; /**< List of declarations in this scope. */
188 declaration_t *last_declaration; /**< last declaration in this scope. */
191 struct expression_base_t {
192 expression_kind_t kind;
194 source_position_t source_position;
197 struct const_expression_t {
198 expression_base_t base;
201 long double float_value;
203 wide_string_t wide_character;
205 bool is_ms_noop; /**< True, if this constant is the result
206 of an microsoft __noop operator */
209 struct string_literal_expression_t {
210 expression_base_t base;
214 struct funcname_expression_t {
215 expression_base_t base;
216 funcname_kind_t kind;
217 string_t value; /**< the value once assigned. */
220 struct wide_string_literal_expression_t {
221 expression_base_t base;
225 struct compound_literal_expression_t {
226 expression_base_t base;
228 initializer_t *initializer;
231 struct builtin_symbol_expression_t {
232 expression_base_t base;
236 struct builtin_constant_expression_t {
237 expression_base_t base;
241 struct builtin_prefetch_expression_t {
242 expression_base_t base;
245 expression_t *locality;
248 struct reference_expression_t {
249 expression_base_t base;
251 declaration_t *declaration;
254 struct call_argument_t {
255 expression_t *expression;
256 call_argument_t *next;
259 struct call_expression_t {
260 expression_base_t base;
261 expression_t *function;
262 call_argument_t *arguments;
265 struct unary_expression_t {
266 expression_base_t base;
270 struct binary_expression_t {
271 expression_base_t base;
276 struct select_expression_t {
277 expression_base_t base;
278 expression_t *compound;
281 declaration_t *compound_entry;
284 struct array_access_expression_t {
285 expression_base_t base;
286 expression_t *array_ref;
288 bool flipped; /* index/ref was written in a 5[a] way */
291 struct typeprop_expression_t {
292 expression_base_t base;
294 expression_t *tp_expression;
297 struct designator_t {
298 source_position_t source_position;
300 expression_t *array_index;
304 struct offsetof_expression_t {
305 expression_base_t base;
307 designator_t *designator;
310 struct va_start_expression_t {
311 expression_base_t base;
313 declaration_t *parameter;
316 struct va_arg_expression_t {
317 expression_base_t base;
321 struct conditional_expression_t {
322 expression_base_t base;
323 expression_t *condition;
324 expression_t *true_expression;
325 expression_t *false_expression;
328 struct statement_expression_t {
329 expression_base_t base;
330 statement_t *statement;
333 struct classify_type_expression_t {
334 expression_base_t base;
335 expression_t *type_expression;
339 expression_kind_t kind;
340 expression_base_t base;
341 const_expression_t conste;
342 funcname_expression_t funcname;
343 string_literal_expression_t string;
344 wide_string_literal_expression_t wide_string;
345 compound_literal_expression_t compound_literal;
346 builtin_symbol_expression_t builtin_symbol;
347 builtin_constant_expression_t builtin_constant;
348 builtin_prefetch_expression_t builtin_prefetch;
349 reference_expression_t reference;
350 call_expression_t call;
351 unary_expression_t unary;
352 binary_expression_t binary;
353 select_expression_t select;
354 array_access_expression_t array_access;
355 typeprop_expression_t typeprop;
356 offsetof_expression_t offsetofe;
357 va_start_expression_t va_starte;
358 va_arg_expression_t va_arge;
359 conditional_expression_t conditional;
360 statement_expression_t statement;
361 classify_type_expression_t classify_type;
366 STORAGE_CLASS_EXTERN,
367 STORAGE_CLASS_STATIC,
368 STORAGE_CLASS_TYPEDEF,
370 STORAGE_CLASS_REGISTER,
371 STORAGE_CLASS_ENUM_ENTRY,
372 STORAGE_CLASS_THREAD,
373 STORAGE_CLASS_THREAD_EXTERN,
374 STORAGE_CLASS_THREAD_STATIC,
375 } storage_class_tag_t;
389 INITIALIZER_WIDE_STRING,
390 INITIALIZER_DESIGNATOR
391 } initializer_kind_t;
393 struct initializer_base_t {
394 initializer_kind_t kind;
397 struct initializer_value_t {
398 initializer_base_t base;
402 struct initializer_list_t {
403 initializer_base_t base;
405 initializer_t *initializers[];
408 struct initializer_string_t {
409 initializer_base_t base;
413 struct initializer_wide_string_t {
414 initializer_base_t base;
415 wide_string_t string;
418 struct initializer_designator_t {
419 initializer_base_t base;
420 designator_t *designator;
423 union initializer_t {
424 initializer_kind_t kind;
425 initializer_base_t base;
426 initializer_value_t value;
427 initializer_list_t list;
428 initializer_string_t string;
429 initializer_wide_string_t wide_string;
430 initializer_designator_t designator;
434 * Extended microsoft modifier.
437 DM_DLLIMPORT = (1 << 0),
438 DM_DLLEXPORT = (1 << 1),
439 DM_THREAD = (1 << 2),
441 DM_MICROSOFT_INLINE = (1 << 4),
442 DM_FORCEINLINE = (1 << 5),
443 DM_SELECTANY = (1 << 6),
444 DM_NOTHROW = (1 << 7),
445 DM_NOVTABLE = (1 << 8),
446 DM_NORETURN = (1 << 9),
447 DM_NOINLINE = (1 << 10),
448 DM_RESTRICT = (1 << 11),
449 DM_NOALIAS = (1 << 12)
452 typedef unsigned short decl_modifiers_t;
454 struct declaration_t {
455 unsigned char namespc;
456 unsigned char declared_storage_class;
457 unsigned char storage_class;
458 unsigned char alignment; /**< Alignment of the declaration, 0 for default. */
459 decl_modifiers_t modifiers; /**< MS __declspec modifiers. */
460 const char *deprecated_string; /**< MS deprecated string if any. */
461 symbol_t *get_property_sym; /**< MS get property. */
462 symbol_t *put_property_sym; /**< MS put property. */
463 unsigned int address_taken : 1;
464 unsigned int is_inline : 1;
465 unsigned int used : 1; /**< Set if the declaration is used. */
466 unsigned int deprecated : 1; /**< Microsoft of GNU deprecated attribute. */
469 source_position_t source_position;
472 statement_t *statement;
473 initializer_t *initializer;
474 expression_t *enum_value;
476 scope_t scope; /**< The scope that this declaration opens. */
477 scope_t *parent_scope; /**< The parant scope where this declaration lives. */
479 /** next declaration in a scope */
481 /** next declaration with same symbol */
482 declaration_t *symbol_next;
484 /* the following fields are used in ast2firm module */
485 unsigned char declaration_kind;
487 unsigned int value_number;
501 STATEMENT_DECLARATION,
504 STATEMENT_EXPRESSION,
509 STATEMENT_CASE_LABEL,
516 struct statement_base_t {
517 statement_kind_t kind;
519 source_position_t source_position;
522 struct invalid_statement_t {
523 statement_base_t base;
526 struct empty_statement_t {
527 statement_base_t base;
530 struct return_statement_t {
531 statement_base_t base;
535 struct compound_statement_t {
536 statement_base_t base;
537 statement_t *statements;
541 struct declaration_statement_t {
542 statement_base_t base;
543 declaration_t *declarations_begin;
544 declaration_t *declarations_end;
547 struct if_statement_t {
548 statement_base_t base;
549 expression_t *condition;
550 statement_t *true_statement;
551 statement_t *false_statement;
554 struct switch_statement_t {
555 statement_base_t base;
556 expression_t *expression;
558 case_label_statement_t *first_case, *last_case;
561 struct goto_statement_t {
562 statement_base_t base;
563 declaration_t *label; /**< The destination label. */
564 goto_statement_t *next; /**< links all goto statements of a function */
567 struct case_label_statement_t {
568 statement_base_t base;
569 expression_t *expression; /**< The case label expression, NULL for default label. */
570 expression_t *end_range; /**< For GNUC case a .. b: the end range expression, NULL else. */
571 statement_t *statement;
572 case_label_statement_t *next; /**< link to the next case label in switch */
575 struct label_statement_t {
576 statement_base_t base;
577 declaration_t *label;
578 statement_t *statement;
579 label_statement_t *next; /**< links all label statements of a function */
582 struct expression_statement_t {
583 statement_base_t base;
584 expression_t *expression;
587 struct while_statement_t {
588 statement_base_t base;
589 expression_t *condition;
593 struct do_while_statement_t {
594 statement_base_t base;
595 expression_t *condition;
599 struct for_statement_t {
600 statement_base_t base;
601 expression_t *initialisation;
602 expression_t *condition;
608 struct asm_constraint_t {
609 string_t constraints;
610 expression_t *expression;
612 asm_constraint_t *next;
615 struct asm_clobber_t {
620 struct asm_statement_t {
621 statement_base_t base;
623 asm_constraint_t *inputs;
624 asm_constraint_t *outputs;
625 asm_clobber_t *clobbers;
630 statement_kind_t kind;
631 statement_base_t base;
632 return_statement_t returns;
633 compound_statement_t compound;
634 declaration_statement_t declaration;
636 switch_statement_t switchs;
637 goto_statement_t gotos;
638 case_label_statement_t case_label;
639 label_statement_t label;
640 expression_statement_t expression;
641 while_statement_t whiles;
642 do_while_statement_t do_while;
643 for_statement_t fors;
644 asm_statement_t asms;
647 struct translation_unit_t {
652 void *_allocate_ast(size_t size)
654 return obstack_alloc(&ast_obstack, size);
658 bool is_invalid_expression(expression_t *expression)
660 return expression->base.kind == EXPR_INVALID;
664 bool is_invalid_statement(statement_t *statement)
666 return statement->base.kind == STATEMENT_INVALID;
670 #define allocate_ast(size) _allocate_ast(size)