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;
41 EXPR_WIDE_STRING_LITERAL,
42 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,
120 /* convenience macros */
121 #define EXPR_BINARY_CASES \
122 case EXPR_BINARY_ADD: \
123 case EXPR_BINARY_SUB: \
124 case EXPR_BINARY_MUL: \
125 case EXPR_BINARY_DIV: \
126 case EXPR_BINARY_MOD: \
127 case EXPR_BINARY_EQUAL: \
128 case EXPR_BINARY_NOTEQUAL: \
129 case EXPR_BINARY_LESS: \
130 case EXPR_BINARY_LESSEQUAL: \
131 case EXPR_BINARY_GREATER: \
132 case EXPR_BINARY_GREATEREQUAL: \
133 case EXPR_BINARY_BITWISE_AND: \
134 case EXPR_BINARY_BITWISE_OR: \
135 case EXPR_BINARY_BITWISE_XOR: \
136 case EXPR_BINARY_LOGICAL_AND: \
137 case EXPR_BINARY_LOGICAL_OR: \
138 case EXPR_BINARY_SHIFTLEFT: \
139 case EXPR_BINARY_SHIFTRIGHT: \
140 case EXPR_BINARY_ASSIGN: \
141 case EXPR_BINARY_MUL_ASSIGN: \
142 case EXPR_BINARY_DIV_ASSIGN: \
143 case EXPR_BINARY_MOD_ASSIGN: \
144 case EXPR_BINARY_ADD_ASSIGN: \
145 case EXPR_BINARY_SUB_ASSIGN: \
146 case EXPR_BINARY_SHIFTLEFT_ASSIGN: \
147 case EXPR_BINARY_SHIFTRIGHT_ASSIGN: \
148 case EXPR_BINARY_BITWISE_AND_ASSIGN: \
149 case EXPR_BINARY_BITWISE_XOR_ASSIGN: \
150 case EXPR_BINARY_BITWISE_OR_ASSIGN: \
151 case EXPR_BINARY_COMMA: \
152 case EXPR_BINARY_BUILTIN_EXPECT: \
153 case EXPR_BINARY_ISGREATER: \
154 case EXPR_BINARY_ISGREATEREQUAL: \
155 case EXPR_BINARY_ISLESS: \
156 case EXPR_BINARY_ISLESSEQUAL: \
157 case EXPR_BINARY_ISLESSGREATER: \
158 case EXPR_BINARY_ISUNORDERED:
160 #define EXPR_UNARY_CASES \
161 case EXPR_UNARY_NEGATE: \
162 case EXPR_UNARY_PLUS: \
163 case EXPR_UNARY_BITWISE_NEGATE: \
164 case EXPR_UNARY_NOT: \
165 case EXPR_UNARY_DEREFERENCE: \
166 case EXPR_UNARY_TAKE_ADDRESS: \
167 case EXPR_UNARY_POSTFIX_INCREMENT: \
168 case EXPR_UNARY_POSTFIX_DECREMENT: \
169 case EXPR_UNARY_PREFIX_INCREMENT: \
170 case EXPR_UNARY_PREFIX_DECREMENT: \
171 case EXPR_UNARY_CAST: \
172 case EXPR_UNARY_CAST_IMPLICIT: \
173 case EXPR_UNARY_ASSUME: \
174 case EXPR_UNARY_BITFIELD_EXTRACT:
177 declaration_t *declarations; /**< List of declarations in this scope. */
178 declaration_t *last_declaration;
181 struct expression_base_t {
182 expression_kind_t kind;
184 source_position_t source_position;
187 struct const_expression_t {
188 expression_base_t base;
191 long double float_value;
196 struct string_literal_expression_t {
197 expression_base_t base;
201 struct wide_string_literal_expression_t {
202 expression_base_t base;
206 struct compound_literal_expression_t {
207 expression_base_t base;
209 initializer_t *initializer;
212 struct builtin_symbol_expression_t {
213 expression_base_t base;
217 struct builtin_constant_expression_t {
218 expression_base_t base;
222 struct builtin_prefetch_expression_t {
223 expression_base_t base;
226 expression_t *locality;
229 struct reference_expression_t {
230 expression_base_t base;
232 declaration_t *declaration;
235 struct call_argument_t {
236 expression_t *expression;
237 call_argument_t *next;
240 struct call_expression_t {
241 expression_base_t base;
242 expression_t *function;
243 call_argument_t *arguments;
246 struct unary_expression_t {
247 expression_base_t base;
251 struct binary_expression_t {
252 expression_base_t base;
257 struct select_expression_t {
258 expression_base_t base;
259 expression_t *compound;
262 declaration_t *compound_entry;
265 struct array_access_expression_t {
266 expression_base_t base;
267 expression_t *array_ref;
269 bool flipped; /* index/ref was written in a 5[a] way */
272 struct typeprop_expression_t {
273 expression_base_t base;
275 expression_t *tp_expression;
278 struct designator_t {
279 source_position_t source_position;
281 expression_t *array_index;
285 struct offsetof_expression_t {
286 expression_base_t base;
288 designator_t *designator;
291 struct va_start_expression_t {
292 expression_base_t base;
294 declaration_t *parameter;
297 struct va_arg_expression_t {
298 expression_base_t base;
302 struct conditional_expression_t {
303 expression_base_t base;
304 expression_t *condition;
305 expression_t *true_expression;
306 expression_t *false_expression;
309 struct statement_expression_t {
310 expression_base_t base;
311 statement_t *statement;
314 struct classify_type_expression_t {
315 expression_base_t base;
316 expression_t *type_expression;
320 expression_kind_t kind;
321 expression_base_t base;
322 const_expression_t conste;
323 string_literal_expression_t string;
324 wide_string_literal_expression_t wide_string;
325 compound_literal_expression_t compound_literal;
326 builtin_symbol_expression_t builtin_symbol;
327 builtin_constant_expression_t builtin_constant;
328 builtin_prefetch_expression_t builtin_prefetch;
329 reference_expression_t reference;
330 call_expression_t call;
331 unary_expression_t unary;
332 binary_expression_t binary;
333 select_expression_t select;
334 array_access_expression_t array_access;
335 typeprop_expression_t typeprop;
336 offsetof_expression_t offsetofe;
337 va_start_expression_t va_starte;
338 va_arg_expression_t va_arge;
339 conditional_expression_t conditional;
340 statement_expression_t statement;
341 classify_type_expression_t classify_type;
346 STORAGE_CLASS_EXTERN,
347 STORAGE_CLASS_STATIC,
348 STORAGE_CLASS_TYPEDEF,
350 STORAGE_CLASS_REGISTER,
351 STORAGE_CLASS_ENUM_ENTRY,
352 STORAGE_CLASS_THREAD,
353 STORAGE_CLASS_THREAD_EXTERN,
354 STORAGE_CLASS_THREAD_STATIC,
355 } storage_class_tag_t;
369 INITIALIZER_WIDE_STRING,
370 INITIALIZER_DESIGNATOR
371 } initializer_kind_t;
373 struct initializer_base_t {
374 initializer_kind_t kind;
377 struct initializer_value_t {
378 initializer_base_t base;
382 struct initializer_list_t {
383 initializer_base_t base;
385 initializer_t *initializers[];
388 struct initializer_string_t {
389 initializer_base_t base;
393 struct initializer_wide_string_t {
394 initializer_base_t base;
395 wide_string_t string;
398 struct initializer_designator_t {
399 initializer_base_t base;
400 designator_t *designator;
403 union initializer_t {
404 initializer_kind_t kind;
405 initializer_base_t base;
406 initializer_value_t value;
407 initializer_list_t list;
408 initializer_string_t string;
409 initializer_wide_string_t wide_string;
410 initializer_designator_t designator;
414 DM_DLLIMPORT = (1 << 0),
415 DM_DLLEXPORT = (1 << 1),
416 DM_THREAD = (1 << 2),
418 DM_FORCEINLINE = (1 << 4),
419 DM_NOTHROW = (1 << 5),
420 DM_NORETURN = (1 << 6),
421 DM_NOINLINE = (1 << 7)
424 typedef unsigned short decl_modifiers_t;
426 struct declaration_t {
427 unsigned char namespc;
428 unsigned char declared_storage_class;
429 unsigned char storage_class;
430 decl_modifiers_t modifiers;
431 unsigned int address_taken : 1;
432 unsigned int is_inline : 1;
433 unsigned int used : 1; /**< Set if the declaration is used. */
436 source_position_t source_position;
439 statement_t *statement;
440 initializer_t *initializer;
441 expression_t *enum_value;
444 scope_t *parent_scope;
446 /** next declaration in a scope */
448 /** next declaration with same symbol */
449 declaration_t *symbol_next;
451 /* the following fields are used in ast2firm module */
452 unsigned char declaration_kind;
454 unsigned int value_number;
467 STATEMENT_DECLARATION,
470 STATEMENT_EXPRESSION,
475 STATEMENT_CASE_LABEL,
482 struct statement_base_t {
483 statement_kind_t kind;
485 source_position_t source_position;
488 struct return_statement_t {
489 statement_base_t base;
493 struct compound_statement_t {
494 statement_base_t base;
495 statement_t *statements;
499 struct declaration_statement_t {
500 statement_base_t base;
501 declaration_t *declarations_begin;
502 declaration_t *declarations_end;
505 struct if_statement_t {
506 statement_base_t base;
507 expression_t *condition;
508 statement_t *true_statement;
509 statement_t *false_statement;
512 struct switch_statement_t {
513 statement_base_t base;
514 expression_t *expression;
516 case_label_statement_t *first_case, *last_case;
519 struct goto_statement_t {
520 statement_base_t base;
521 declaration_t *label; /**< The destination label. */
522 goto_statement_t *next; /**< links all goto statements of a function */
525 struct case_label_statement_t {
526 statement_base_t base;
527 expression_t *expression; /**< The case label expression, NULL for default label. */
528 expression_t *end_range; /**< For GNUC case a .. b: the end range expression, NULL else. */
529 statement_t *statement;
530 case_label_statement_t *next; /**< link to the next case label in switch */
533 struct label_statement_t {
534 statement_base_t base;
535 declaration_t *label;
536 statement_t *statement;
537 label_statement_t *next; /**< links all label statements of a function */
540 struct expression_statement_t {
541 statement_base_t base;
542 expression_t *expression;
545 struct while_statement_t {
546 statement_base_t base;
547 expression_t *condition;
551 struct do_while_statement_t {
552 statement_base_t base;
553 expression_t *condition;
557 struct for_statement_t {
558 statement_base_t base;
559 expression_t *initialisation;
560 expression_t *condition;
566 struct asm_constraint_t {
567 string_t constraints;
568 expression_t *expression;
570 asm_constraint_t *next;
573 struct asm_clobber_t {
578 struct asm_statement_t {
579 statement_base_t base;
581 asm_constraint_t *inputs;
582 asm_constraint_t *outputs;
583 asm_clobber_t *clobbers;
588 statement_kind_t kind;
589 statement_base_t base;
590 return_statement_t returns;
591 compound_statement_t compound;
592 declaration_statement_t declaration;
594 switch_statement_t switchs;
595 goto_statement_t gotos;
596 case_label_statement_t case_label;
597 label_statement_t label;
598 expression_statement_t expression;
599 while_statement_t whiles;
600 do_while_statement_t do_while;
601 for_statement_t fors;
602 asm_statement_t asms;
605 struct translation_unit_t {
610 void *_allocate_ast(size_t size)
612 return obstack_alloc(&ast_obstack, size);
615 #define allocate_ast(size) _allocate_ast(size)