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;
207 struct string_literal_expression_t {
208 expression_base_t base;
212 struct funcname_expression_t {
213 expression_base_t base;
214 funcname_kind_t kind;
215 string_t value; /**< the value once assigned. */
218 struct wide_string_literal_expression_t {
219 expression_base_t base;
223 struct compound_literal_expression_t {
224 expression_base_t base;
226 initializer_t *initializer;
229 struct builtin_symbol_expression_t {
230 expression_base_t base;
234 struct builtin_constant_expression_t {
235 expression_base_t base;
239 struct builtin_prefetch_expression_t {
240 expression_base_t base;
243 expression_t *locality;
246 struct reference_expression_t {
247 expression_base_t base;
249 declaration_t *declaration;
252 struct call_argument_t {
253 expression_t *expression;
254 call_argument_t *next;
257 struct call_expression_t {
258 expression_base_t base;
259 expression_t *function;
260 call_argument_t *arguments;
263 struct unary_expression_t {
264 expression_base_t base;
268 struct binary_expression_t {
269 expression_base_t base;
274 struct select_expression_t {
275 expression_base_t base;
276 expression_t *compound;
279 declaration_t *compound_entry;
282 struct array_access_expression_t {
283 expression_base_t base;
284 expression_t *array_ref;
286 bool flipped; /* index/ref was written in a 5[a] way */
289 struct typeprop_expression_t {
290 expression_base_t base;
292 expression_t *tp_expression;
295 struct designator_t {
296 source_position_t source_position;
298 expression_t *array_index;
302 struct offsetof_expression_t {
303 expression_base_t base;
305 designator_t *designator;
308 struct va_start_expression_t {
309 expression_base_t base;
311 declaration_t *parameter;
314 struct va_arg_expression_t {
315 expression_base_t base;
319 struct conditional_expression_t {
320 expression_base_t base;
321 expression_t *condition;
322 expression_t *true_expression;
323 expression_t *false_expression;
326 struct statement_expression_t {
327 expression_base_t base;
328 statement_t *statement;
331 struct classify_type_expression_t {
332 expression_base_t base;
333 expression_t *type_expression;
337 expression_kind_t kind;
338 expression_base_t base;
339 const_expression_t conste;
340 funcname_expression_t funcname;
341 string_literal_expression_t string;
342 wide_string_literal_expression_t wide_string;
343 compound_literal_expression_t compound_literal;
344 builtin_symbol_expression_t builtin_symbol;
345 builtin_constant_expression_t builtin_constant;
346 builtin_prefetch_expression_t builtin_prefetch;
347 reference_expression_t reference;
348 call_expression_t call;
349 unary_expression_t unary;
350 binary_expression_t binary;
351 select_expression_t select;
352 array_access_expression_t array_access;
353 typeprop_expression_t typeprop;
354 offsetof_expression_t offsetofe;
355 va_start_expression_t va_starte;
356 va_arg_expression_t va_arge;
357 conditional_expression_t conditional;
358 statement_expression_t statement;
359 classify_type_expression_t classify_type;
364 STORAGE_CLASS_EXTERN,
365 STORAGE_CLASS_STATIC,
366 STORAGE_CLASS_TYPEDEF,
368 STORAGE_CLASS_REGISTER,
369 STORAGE_CLASS_ENUM_ENTRY,
370 STORAGE_CLASS_THREAD,
371 STORAGE_CLASS_THREAD_EXTERN,
372 STORAGE_CLASS_THREAD_STATIC,
373 } storage_class_tag_t;
387 INITIALIZER_WIDE_STRING,
388 INITIALIZER_DESIGNATOR
389 } initializer_kind_t;
391 struct initializer_base_t {
392 initializer_kind_t kind;
395 struct initializer_value_t {
396 initializer_base_t base;
400 struct initializer_list_t {
401 initializer_base_t base;
403 initializer_t *initializers[];
406 struct initializer_string_t {
407 initializer_base_t base;
411 struct initializer_wide_string_t {
412 initializer_base_t base;
413 wide_string_t string;
416 struct initializer_designator_t {
417 initializer_base_t base;
418 designator_t *designator;
421 union initializer_t {
422 initializer_kind_t kind;
423 initializer_base_t base;
424 initializer_value_t value;
425 initializer_list_t list;
426 initializer_string_t string;
427 initializer_wide_string_t wide_string;
428 initializer_designator_t designator;
432 * Extended microsoft modifier.
435 DM_DLLIMPORT = (1 << 0),
436 DM_DLLEXPORT = (1 << 1),
437 DM_THREAD = (1 << 2),
439 DM_MICROSOFT_INLINE = (1 << 4),
440 DM_FORCEINLINE = (1 << 5),
441 DM_SELECTANY = (1 << 6),
442 DM_NOTHROW = (1 << 7),
443 DM_NOVTABLE = (1 << 8),
444 DM_NORETURN = (1 << 9),
445 DM_NOINLINE = (1 << 10),
446 DM_RESTRICT = (1 << 11),
447 DM_NOALIAS = (1 << 12)
450 typedef unsigned short decl_modifiers_t;
452 struct declaration_t {
453 unsigned char namespc;
454 unsigned char declared_storage_class;
455 unsigned char storage_class;
456 unsigned char alignment; /**< Alignment of the declaration, 0 for default. */
457 decl_modifiers_t modifiers; /**< MS __declspec modifiers. */
458 const char *deprecated_string; /**< MS deprecated string if any. */
459 symbol_t *get_property_sym; /**< MS get property. */
460 symbol_t *put_property_sym; /**< MS put property. */
461 unsigned int address_taken : 1;
462 unsigned int is_inline : 1;
463 unsigned int used : 1; /**< Set if the declaration is used. */
464 unsigned int deprecated : 1; /**< Microsoft of GNU deprecated attribute. */
467 source_position_t source_position;
470 statement_t *statement;
471 initializer_t *initializer;
472 expression_t *enum_value;
474 scope_t scope; /**< The scope that this declaration opens. */
475 scope_t *parent_scope; /**< The parant scope where this declaration lives. */
477 /** next declaration in a scope */
479 /** next declaration with same symbol */
480 declaration_t *symbol_next;
482 /* the following fields are used in ast2firm module */
483 unsigned char declaration_kind;
485 unsigned int value_number;
499 STATEMENT_DECLARATION,
502 STATEMENT_EXPRESSION,
507 STATEMENT_CASE_LABEL,
514 struct statement_base_t {
515 statement_kind_t kind;
517 source_position_t source_position;
520 struct invalid_statement_t {
521 statement_base_t base;
524 struct empty_statement_t {
525 statement_base_t base;
528 struct return_statement_t {
529 statement_base_t base;
533 struct compound_statement_t {
534 statement_base_t base;
535 statement_t *statements;
539 struct declaration_statement_t {
540 statement_base_t base;
541 declaration_t *declarations_begin;
542 declaration_t *declarations_end;
545 struct if_statement_t {
546 statement_base_t base;
547 expression_t *condition;
548 statement_t *true_statement;
549 statement_t *false_statement;
552 struct switch_statement_t {
553 statement_base_t base;
554 expression_t *expression;
556 case_label_statement_t *first_case, *last_case;
559 struct goto_statement_t {
560 statement_base_t base;
561 declaration_t *label; /**< The destination label. */
562 goto_statement_t *next; /**< links all goto statements of a function */
565 struct case_label_statement_t {
566 statement_base_t base;
567 expression_t *expression; /**< The case label expression, NULL for default label. */
568 expression_t *end_range; /**< For GNUC case a .. b: the end range expression, NULL else. */
569 statement_t *statement;
570 case_label_statement_t *next; /**< link to the next case label in switch */
573 struct label_statement_t {
574 statement_base_t base;
575 declaration_t *label;
576 statement_t *statement;
577 label_statement_t *next; /**< links all label statements of a function */
580 struct expression_statement_t {
581 statement_base_t base;
582 expression_t *expression;
585 struct while_statement_t {
586 statement_base_t base;
587 expression_t *condition;
591 struct do_while_statement_t {
592 statement_base_t base;
593 expression_t *condition;
597 struct for_statement_t {
598 statement_base_t base;
599 expression_t *initialisation;
600 expression_t *condition;
606 struct asm_constraint_t {
607 string_t constraints;
608 expression_t *expression;
610 asm_constraint_t *next;
613 struct asm_clobber_t {
618 struct asm_statement_t {
619 statement_base_t base;
621 asm_constraint_t *inputs;
622 asm_constraint_t *outputs;
623 asm_clobber_t *clobbers;
628 statement_kind_t kind;
629 statement_base_t base;
630 return_statement_t returns;
631 compound_statement_t compound;
632 declaration_statement_t declaration;
634 switch_statement_t switchs;
635 goto_statement_t gotos;
636 case_label_statement_t case_label;
637 label_statement_t label;
638 expression_statement_t expression;
639 while_statement_t whiles;
640 do_while_statement_t do_while;
641 for_statement_t fors;
642 asm_statement_t asms;
645 struct translation_unit_t {
650 void *_allocate_ast(size_t size)
652 return obstack_alloc(&ast_obstack, size);
656 bool is_invalid_expression(expression_t *expression)
658 return expression->base.kind == EXPR_INVALID;
662 bool is_invalid_statement(statement_t *statement)
664 return statement->base.kind == STATEMENT_INVALID;
668 #define allocate_ast(size) _allocate_ast(size)