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_FUNCSIG, /**< MS function signature, aka ld_name */
55 EXPR_FUNCDNAME, /**< MS decorated name of a function */
57 EXPR_BUILTIN_CONSTANT_P,
58 EXPR_BUILTIN_PREFETCH,
65 EXPR_UNARY_NEGATE = EXPR_UNARY_FIRST,
67 EXPR_UNARY_BITWISE_NEGATE,
69 EXPR_UNARY_DEREFERENCE,
70 EXPR_UNARY_TAKE_ADDRESS,
71 EXPR_UNARY_POSTFIX_INCREMENT,
72 EXPR_UNARY_POSTFIX_DECREMENT,
73 EXPR_UNARY_PREFIX_INCREMENT,
74 EXPR_UNARY_PREFIX_DECREMENT,
76 EXPR_UNARY_CAST_IMPLICIT, /**< compiler generated cast */
77 EXPR_UNARY_ASSUME, /**< MS __assume() */
78 EXPR_UNARY_BITFIELD_EXTRACT,
79 EXPR_UNARY_LAST = EXPR_UNARY_BITFIELD_EXTRACT,
82 EXPR_BINARY_ADD = EXPR_BINARY_FIRST,
90 EXPR_BINARY_LESSEQUAL,
92 EXPR_BINARY_GREATEREQUAL,
93 EXPR_BINARY_BITWISE_AND,
94 EXPR_BINARY_BITWISE_OR,
95 EXPR_BINARY_BITWISE_XOR,
96 EXPR_BINARY_LOGICAL_AND,
97 EXPR_BINARY_LOGICAL_OR,
98 EXPR_BINARY_SHIFTLEFT,
99 EXPR_BINARY_SHIFTRIGHT,
101 EXPR_BINARY_MUL_ASSIGN,
102 EXPR_BINARY_DIV_ASSIGN,
103 EXPR_BINARY_MOD_ASSIGN,
104 EXPR_BINARY_ADD_ASSIGN,
105 EXPR_BINARY_SUB_ASSIGN,
106 EXPR_BINARY_SHIFTLEFT_ASSIGN,
107 EXPR_BINARY_SHIFTRIGHT_ASSIGN,
108 EXPR_BINARY_BITWISE_AND_ASSIGN,
109 EXPR_BINARY_BITWISE_XOR_ASSIGN,
110 EXPR_BINARY_BITWISE_OR_ASSIGN,
113 EXPR_BINARY_BUILTIN_EXPECT,
114 EXPR_BINARY_ISGREATER,
115 EXPR_BINARY_ISGREATEREQUAL,
117 EXPR_BINARY_ISLESSEQUAL,
118 EXPR_BINARY_ISLESSGREATER,
119 EXPR_BINARY_ISUNORDERED,
120 EXPR_BINARY_LAST = EXPR_BINARY_ISUNORDERED,
123 /* convenience macros */
124 #define EXPR_BINARY_CASES \
125 case EXPR_BINARY_ADD: \
126 case EXPR_BINARY_SUB: \
127 case EXPR_BINARY_MUL: \
128 case EXPR_BINARY_DIV: \
129 case EXPR_BINARY_MOD: \
130 case EXPR_BINARY_EQUAL: \
131 case EXPR_BINARY_NOTEQUAL: \
132 case EXPR_BINARY_LESS: \
133 case EXPR_BINARY_LESSEQUAL: \
134 case EXPR_BINARY_GREATER: \
135 case EXPR_BINARY_GREATEREQUAL: \
136 case EXPR_BINARY_BITWISE_AND: \
137 case EXPR_BINARY_BITWISE_OR: \
138 case EXPR_BINARY_BITWISE_XOR: \
139 case EXPR_BINARY_LOGICAL_AND: \
140 case EXPR_BINARY_LOGICAL_OR: \
141 case EXPR_BINARY_SHIFTLEFT: \
142 case EXPR_BINARY_SHIFTRIGHT: \
143 case EXPR_BINARY_ASSIGN: \
144 case EXPR_BINARY_MUL_ASSIGN: \
145 case EXPR_BINARY_DIV_ASSIGN: \
146 case EXPR_BINARY_MOD_ASSIGN: \
147 case EXPR_BINARY_ADD_ASSIGN: \
148 case EXPR_BINARY_SUB_ASSIGN: \
149 case EXPR_BINARY_SHIFTLEFT_ASSIGN: \
150 case EXPR_BINARY_SHIFTRIGHT_ASSIGN: \
151 case EXPR_BINARY_BITWISE_AND_ASSIGN: \
152 case EXPR_BINARY_BITWISE_XOR_ASSIGN: \
153 case EXPR_BINARY_BITWISE_OR_ASSIGN: \
154 case EXPR_BINARY_COMMA: \
155 case EXPR_BINARY_BUILTIN_EXPECT: \
156 case EXPR_BINARY_ISGREATER: \
157 case EXPR_BINARY_ISGREATEREQUAL: \
158 case EXPR_BINARY_ISLESS: \
159 case EXPR_BINARY_ISLESSEQUAL: \
160 case EXPR_BINARY_ISLESSGREATER: \
161 case EXPR_BINARY_ISUNORDERED:
163 #define EXPR_UNARY_CASES \
164 case EXPR_UNARY_NEGATE: \
165 case EXPR_UNARY_PLUS: \
166 case EXPR_UNARY_BITWISE_NEGATE: \
167 case EXPR_UNARY_NOT: \
168 case EXPR_UNARY_DEREFERENCE: \
169 case EXPR_UNARY_TAKE_ADDRESS: \
170 case EXPR_UNARY_POSTFIX_INCREMENT: \
171 case EXPR_UNARY_POSTFIX_DECREMENT: \
172 case EXPR_UNARY_PREFIX_INCREMENT: \
173 case EXPR_UNARY_PREFIX_DECREMENT: \
174 case EXPR_UNARY_CAST: \
175 case EXPR_UNARY_CAST_IMPLICIT: \
176 case EXPR_UNARY_ASSUME: \
177 case EXPR_UNARY_BITFIELD_EXTRACT:
180 * A scope containing declarations.
183 declaration_t *declarations; /**< List of declarations in this scope. */
184 declaration_t *last_declaration; /**< last declaration in this scope. */
187 struct expression_base_t {
188 expression_kind_t kind;
190 source_position_t source_position;
193 struct const_expression_t {
194 expression_base_t base;
197 long double float_value;
199 wide_string_t wide_character;
203 struct string_literal_expression_t {
204 expression_base_t base;
208 struct wide_string_literal_expression_t {
209 expression_base_t base;
213 struct compound_literal_expression_t {
214 expression_base_t base;
216 initializer_t *initializer;
219 struct builtin_symbol_expression_t {
220 expression_base_t base;
224 struct builtin_constant_expression_t {
225 expression_base_t base;
229 struct builtin_prefetch_expression_t {
230 expression_base_t base;
233 expression_t *locality;
236 struct reference_expression_t {
237 expression_base_t base;
239 declaration_t *declaration;
242 struct call_argument_t {
243 expression_t *expression;
244 call_argument_t *next;
247 struct call_expression_t {
248 expression_base_t base;
249 expression_t *function;
250 call_argument_t *arguments;
253 struct unary_expression_t {
254 expression_base_t base;
258 struct binary_expression_t {
259 expression_base_t base;
264 struct select_expression_t {
265 expression_base_t base;
266 expression_t *compound;
269 declaration_t *compound_entry;
272 struct array_access_expression_t {
273 expression_base_t base;
274 expression_t *array_ref;
276 bool flipped; /* index/ref was written in a 5[a] way */
279 struct typeprop_expression_t {
280 expression_base_t base;
282 expression_t *tp_expression;
285 struct designator_t {
286 source_position_t source_position;
288 expression_t *array_index;
292 struct offsetof_expression_t {
293 expression_base_t base;
295 designator_t *designator;
298 struct va_start_expression_t {
299 expression_base_t base;
301 declaration_t *parameter;
304 struct va_arg_expression_t {
305 expression_base_t base;
309 struct conditional_expression_t {
310 expression_base_t base;
311 expression_t *condition;
312 expression_t *true_expression;
313 expression_t *false_expression;
316 struct statement_expression_t {
317 expression_base_t base;
318 statement_t *statement;
321 struct classify_type_expression_t {
322 expression_base_t base;
323 expression_t *type_expression;
327 expression_kind_t kind;
328 expression_base_t base;
329 const_expression_t conste;
330 string_literal_expression_t string;
331 wide_string_literal_expression_t wide_string;
332 compound_literal_expression_t compound_literal;
333 builtin_symbol_expression_t builtin_symbol;
334 builtin_constant_expression_t builtin_constant;
335 builtin_prefetch_expression_t builtin_prefetch;
336 reference_expression_t reference;
337 call_expression_t call;
338 unary_expression_t unary;
339 binary_expression_t binary;
340 select_expression_t select;
341 array_access_expression_t array_access;
342 typeprop_expression_t typeprop;
343 offsetof_expression_t offsetofe;
344 va_start_expression_t va_starte;
345 va_arg_expression_t va_arge;
346 conditional_expression_t conditional;
347 statement_expression_t statement;
348 classify_type_expression_t classify_type;
353 STORAGE_CLASS_EXTERN,
354 STORAGE_CLASS_STATIC,
355 STORAGE_CLASS_TYPEDEF,
357 STORAGE_CLASS_REGISTER,
358 STORAGE_CLASS_ENUM_ENTRY,
359 STORAGE_CLASS_THREAD,
360 STORAGE_CLASS_THREAD_EXTERN,
361 STORAGE_CLASS_THREAD_STATIC,
362 } storage_class_tag_t;
376 INITIALIZER_WIDE_STRING,
377 INITIALIZER_DESIGNATOR
378 } initializer_kind_t;
380 struct initializer_base_t {
381 initializer_kind_t kind;
384 struct initializer_value_t {
385 initializer_base_t base;
389 struct initializer_list_t {
390 initializer_base_t base;
392 initializer_t *initializers[];
395 struct initializer_string_t {
396 initializer_base_t base;
400 struct initializer_wide_string_t {
401 initializer_base_t base;
402 wide_string_t string;
405 struct initializer_designator_t {
406 initializer_base_t base;
407 designator_t *designator;
410 union initializer_t {
411 initializer_kind_t kind;
412 initializer_base_t base;
413 initializer_value_t value;
414 initializer_list_t list;
415 initializer_string_t string;
416 initializer_wide_string_t wide_string;
417 initializer_designator_t designator;
421 * Extended microsoft modifier.
424 DM_DLLIMPORT = (1 << 0),
425 DM_DLLEXPORT = (1 << 1),
426 DM_THREAD = (1 << 2),
428 DM_MICROSOFT_INLINE = (1 << 4),
429 DM_FORCEINLINE = (1 << 5),
430 DM_SELECTANY = (1 << 6),
431 DM_NOTHROW = (1 << 7),
432 DM_NOVTABLE = (1 << 8),
433 DM_NORETURN = (1 << 9),
434 DM_NOINLINE = (1 << 10),
435 DM_RESTRICT = (1 << 11),
436 DM_NOALIAS = (1 << 12)
439 typedef unsigned short decl_modifiers_t;
441 struct declaration_t {
442 unsigned char namespc;
443 unsigned char declared_storage_class;
444 unsigned char storage_class;
445 unsigned char alignment; /**< Alignment of the declaration, 0 for default. */
446 decl_modifiers_t modifiers; /**< MS __declspec modifiers. */
447 const char *deprecated_string; /**< MS deprecated string if any. */
448 symbol_t *get_property_sym; /**< MS get property. */
449 symbol_t *put_property_sym; /**< MS put property. */
450 unsigned int address_taken : 1;
451 unsigned int is_inline : 1;
452 unsigned int used : 1; /**< Set if the declaration is used. */
453 unsigned int deprecated : 1; /**< Microsoft of GNU deprecated attribute. */
456 source_position_t source_position;
459 statement_t *statement;
460 initializer_t *initializer;
461 expression_t *enum_value;
463 scope_t scope; /**< The scope that this declaration opens. */
464 scope_t *parent_scope; /**< The parant scope where this declaration lives. */
466 /** next declaration in a scope */
468 /** next declaration with same symbol */
469 declaration_t *symbol_next;
471 /* the following fields are used in ast2firm module */
472 unsigned char declaration_kind;
474 unsigned int value_number;
488 STATEMENT_DECLARATION,
491 STATEMENT_EXPRESSION,
496 STATEMENT_CASE_LABEL,
503 struct statement_base_t {
504 statement_kind_t kind;
506 source_position_t source_position;
509 struct invalid_statement_t {
510 statement_base_t base;
513 struct empty_statement_t {
514 statement_base_t base;
517 struct return_statement_t {
518 statement_base_t base;
522 struct compound_statement_t {
523 statement_base_t base;
524 statement_t *statements;
528 struct declaration_statement_t {
529 statement_base_t base;
530 declaration_t *declarations_begin;
531 declaration_t *declarations_end;
534 struct if_statement_t {
535 statement_base_t base;
536 expression_t *condition;
537 statement_t *true_statement;
538 statement_t *false_statement;
541 struct switch_statement_t {
542 statement_base_t base;
543 expression_t *expression;
545 case_label_statement_t *first_case, *last_case;
548 struct goto_statement_t {
549 statement_base_t base;
550 declaration_t *label; /**< The destination label. */
551 goto_statement_t *next; /**< links all goto statements of a function */
554 struct case_label_statement_t {
555 statement_base_t base;
556 expression_t *expression; /**< The case label expression, NULL for default label. */
557 expression_t *end_range; /**< For GNUC case a .. b: the end range expression, NULL else. */
558 statement_t *statement;
559 case_label_statement_t *next; /**< link to the next case label in switch */
562 struct label_statement_t {
563 statement_base_t base;
564 declaration_t *label;
565 statement_t *statement;
566 label_statement_t *next; /**< links all label statements of a function */
569 struct expression_statement_t {
570 statement_base_t base;
571 expression_t *expression;
574 struct while_statement_t {
575 statement_base_t base;
576 expression_t *condition;
580 struct do_while_statement_t {
581 statement_base_t base;
582 expression_t *condition;
586 struct for_statement_t {
587 statement_base_t base;
588 expression_t *initialisation;
589 expression_t *condition;
595 struct asm_constraint_t {
596 string_t constraints;
597 expression_t *expression;
599 asm_constraint_t *next;
602 struct asm_clobber_t {
607 struct asm_statement_t {
608 statement_base_t base;
610 asm_constraint_t *inputs;
611 asm_constraint_t *outputs;
612 asm_clobber_t *clobbers;
617 statement_kind_t kind;
618 statement_base_t base;
619 return_statement_t returns;
620 compound_statement_t compound;
621 declaration_statement_t declaration;
623 switch_statement_t switchs;
624 goto_statement_t gotos;
625 case_label_statement_t case_label;
626 label_statement_t label;
627 expression_statement_t expression;
628 while_statement_t whiles;
629 do_while_statement_t do_while;
630 for_statement_t fors;
631 asm_statement_t asms;
634 struct translation_unit_t {
639 void *_allocate_ast(size_t size)
641 return obstack_alloc(&ast_obstack, size);
645 bool is_invalid_expression(expression_t *expression)
647 return expression->base.kind == EXPR_INVALID;
651 bool is_invalid_statement(statement_t *statement)
653 return statement->base.kind == STATEMENT_INVALID;
657 #define allocate_ast(size) _allocate_ast(size)