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,
55 EXPR_BUILTIN_CONSTANT_P,
56 EXPR_BUILTIN_PREFETCH,
63 EXPR_UNARY_NEGATE = EXPR_UNARY_FIRST,
65 EXPR_UNARY_BITWISE_NEGATE,
67 EXPR_UNARY_DEREFERENCE,
68 EXPR_UNARY_TAKE_ADDRESS,
69 EXPR_UNARY_POSTFIX_INCREMENT,
70 EXPR_UNARY_POSTFIX_DECREMENT,
71 EXPR_UNARY_PREFIX_INCREMENT,
72 EXPR_UNARY_PREFIX_DECREMENT,
74 EXPR_UNARY_CAST_IMPLICIT, /**< compiler generated cast */
75 EXPR_UNARY_ASSUME, /**< MS __assume() */
76 EXPR_UNARY_BITFIELD_EXTRACT,
77 EXPR_UNARY_LAST = EXPR_UNARY_BITFIELD_EXTRACT,
80 EXPR_BINARY_ADD = EXPR_BINARY_FIRST,
88 EXPR_BINARY_LESSEQUAL,
90 EXPR_BINARY_GREATEREQUAL,
91 EXPR_BINARY_BITWISE_AND,
92 EXPR_BINARY_BITWISE_OR,
93 EXPR_BINARY_BITWISE_XOR,
94 EXPR_BINARY_LOGICAL_AND,
95 EXPR_BINARY_LOGICAL_OR,
96 EXPR_BINARY_SHIFTLEFT,
97 EXPR_BINARY_SHIFTRIGHT,
99 EXPR_BINARY_MUL_ASSIGN,
100 EXPR_BINARY_DIV_ASSIGN,
101 EXPR_BINARY_MOD_ASSIGN,
102 EXPR_BINARY_ADD_ASSIGN,
103 EXPR_BINARY_SUB_ASSIGN,
104 EXPR_BINARY_SHIFTLEFT_ASSIGN,
105 EXPR_BINARY_SHIFTRIGHT_ASSIGN,
106 EXPR_BINARY_BITWISE_AND_ASSIGN,
107 EXPR_BINARY_BITWISE_XOR_ASSIGN,
108 EXPR_BINARY_BITWISE_OR_ASSIGN,
111 EXPR_BINARY_BUILTIN_EXPECT,
112 EXPR_BINARY_ISGREATER,
113 EXPR_BINARY_ISGREATEREQUAL,
115 EXPR_BINARY_ISLESSEQUAL,
116 EXPR_BINARY_ISLESSGREATER,
117 EXPR_BINARY_ISUNORDERED,
118 EXPR_BINARY_LAST = EXPR_BINARY_ISUNORDERED,
121 /* convenience macros */
122 #define EXPR_BINARY_CASES \
123 case EXPR_BINARY_ADD: \
124 case EXPR_BINARY_SUB: \
125 case EXPR_BINARY_MUL: \
126 case EXPR_BINARY_DIV: \
127 case EXPR_BINARY_MOD: \
128 case EXPR_BINARY_EQUAL: \
129 case EXPR_BINARY_NOTEQUAL: \
130 case EXPR_BINARY_LESS: \
131 case EXPR_BINARY_LESSEQUAL: \
132 case EXPR_BINARY_GREATER: \
133 case EXPR_BINARY_GREATEREQUAL: \
134 case EXPR_BINARY_BITWISE_AND: \
135 case EXPR_BINARY_BITWISE_OR: \
136 case EXPR_BINARY_BITWISE_XOR: \
137 case EXPR_BINARY_LOGICAL_AND: \
138 case EXPR_BINARY_LOGICAL_OR: \
139 case EXPR_BINARY_SHIFTLEFT: \
140 case EXPR_BINARY_SHIFTRIGHT: \
141 case EXPR_BINARY_ASSIGN: \
142 case EXPR_BINARY_MUL_ASSIGN: \
143 case EXPR_BINARY_DIV_ASSIGN: \
144 case EXPR_BINARY_MOD_ASSIGN: \
145 case EXPR_BINARY_ADD_ASSIGN: \
146 case EXPR_BINARY_SUB_ASSIGN: \
147 case EXPR_BINARY_SHIFTLEFT_ASSIGN: \
148 case EXPR_BINARY_SHIFTRIGHT_ASSIGN: \
149 case EXPR_BINARY_BITWISE_AND_ASSIGN: \
150 case EXPR_BINARY_BITWISE_XOR_ASSIGN: \
151 case EXPR_BINARY_BITWISE_OR_ASSIGN: \
152 case EXPR_BINARY_COMMA: \
153 case EXPR_BINARY_BUILTIN_EXPECT: \
154 case EXPR_BINARY_ISGREATER: \
155 case EXPR_BINARY_ISGREATEREQUAL: \
156 case EXPR_BINARY_ISLESS: \
157 case EXPR_BINARY_ISLESSEQUAL: \
158 case EXPR_BINARY_ISLESSGREATER: \
159 case EXPR_BINARY_ISUNORDERED:
161 #define EXPR_UNARY_CASES \
162 case EXPR_UNARY_NEGATE: \
163 case EXPR_UNARY_PLUS: \
164 case EXPR_UNARY_BITWISE_NEGATE: \
165 case EXPR_UNARY_NOT: \
166 case EXPR_UNARY_DEREFERENCE: \
167 case EXPR_UNARY_TAKE_ADDRESS: \
168 case EXPR_UNARY_POSTFIX_INCREMENT: \
169 case EXPR_UNARY_POSTFIX_DECREMENT: \
170 case EXPR_UNARY_PREFIX_INCREMENT: \
171 case EXPR_UNARY_PREFIX_DECREMENT: \
172 case EXPR_UNARY_CAST: \
173 case EXPR_UNARY_CAST_IMPLICIT: \
174 case EXPR_UNARY_ASSUME: \
175 case EXPR_UNARY_BITFIELD_EXTRACT:
178 * A scope containing declarations.
181 declaration_t *declarations; /**< List of declarations in this scope. */
182 declaration_t *last_declaration; /**< last declaration in this scope. */
185 struct expression_base_t {
186 expression_kind_t kind;
188 source_position_t source_position;
191 struct const_expression_t {
192 expression_base_t base;
195 long double float_value;
197 wide_string_t wide_character;
201 struct string_literal_expression_t {
202 expression_base_t base;
206 struct wide_string_literal_expression_t {
207 expression_base_t base;
211 struct compound_literal_expression_t {
212 expression_base_t base;
214 initializer_t *initializer;
217 struct builtin_symbol_expression_t {
218 expression_base_t base;
222 struct builtin_constant_expression_t {
223 expression_base_t base;
227 struct builtin_prefetch_expression_t {
228 expression_base_t base;
231 expression_t *locality;
234 struct reference_expression_t {
235 expression_base_t base;
237 declaration_t *declaration;
240 struct call_argument_t {
241 expression_t *expression;
242 call_argument_t *next;
245 struct call_expression_t {
246 expression_base_t base;
247 expression_t *function;
248 call_argument_t *arguments;
251 struct unary_expression_t {
252 expression_base_t base;
256 struct binary_expression_t {
257 expression_base_t base;
262 struct select_expression_t {
263 expression_base_t base;
264 expression_t *compound;
267 declaration_t *compound_entry;
270 struct array_access_expression_t {
271 expression_base_t base;
272 expression_t *array_ref;
274 bool flipped; /* index/ref was written in a 5[a] way */
277 struct typeprop_expression_t {
278 expression_base_t base;
280 expression_t *tp_expression;
283 struct designator_t {
284 source_position_t source_position;
286 expression_t *array_index;
290 struct offsetof_expression_t {
291 expression_base_t base;
293 designator_t *designator;
296 struct va_start_expression_t {
297 expression_base_t base;
299 declaration_t *parameter;
302 struct va_arg_expression_t {
303 expression_base_t base;
307 struct conditional_expression_t {
308 expression_base_t base;
309 expression_t *condition;
310 expression_t *true_expression;
311 expression_t *false_expression;
314 struct statement_expression_t {
315 expression_base_t base;
316 statement_t *statement;
319 struct classify_type_expression_t {
320 expression_base_t base;
321 expression_t *type_expression;
325 expression_kind_t kind;
326 expression_base_t base;
327 const_expression_t conste;
328 string_literal_expression_t string;
329 wide_string_literal_expression_t wide_string;
330 compound_literal_expression_t compound_literal;
331 builtin_symbol_expression_t builtin_symbol;
332 builtin_constant_expression_t builtin_constant;
333 builtin_prefetch_expression_t builtin_prefetch;
334 reference_expression_t reference;
335 call_expression_t call;
336 unary_expression_t unary;
337 binary_expression_t binary;
338 select_expression_t select;
339 array_access_expression_t array_access;
340 typeprop_expression_t typeprop;
341 offsetof_expression_t offsetofe;
342 va_start_expression_t va_starte;
343 va_arg_expression_t va_arge;
344 conditional_expression_t conditional;
345 statement_expression_t statement;
346 classify_type_expression_t classify_type;
351 STORAGE_CLASS_EXTERN,
352 STORAGE_CLASS_STATIC,
353 STORAGE_CLASS_TYPEDEF,
355 STORAGE_CLASS_REGISTER,
356 STORAGE_CLASS_ENUM_ENTRY,
357 STORAGE_CLASS_THREAD,
358 STORAGE_CLASS_THREAD_EXTERN,
359 STORAGE_CLASS_THREAD_STATIC,
360 } storage_class_tag_t;
374 INITIALIZER_WIDE_STRING,
375 INITIALIZER_DESIGNATOR
376 } initializer_kind_t;
378 struct initializer_base_t {
379 initializer_kind_t kind;
382 struct initializer_value_t {
383 initializer_base_t base;
387 struct initializer_list_t {
388 initializer_base_t base;
390 initializer_t *initializers[];
393 struct initializer_string_t {
394 initializer_base_t base;
398 struct initializer_wide_string_t {
399 initializer_base_t base;
400 wide_string_t string;
403 struct initializer_designator_t {
404 initializer_base_t base;
405 designator_t *designator;
408 union initializer_t {
409 initializer_kind_t kind;
410 initializer_base_t base;
411 initializer_value_t value;
412 initializer_list_t list;
413 initializer_string_t string;
414 initializer_wide_string_t wide_string;
415 initializer_designator_t designator;
419 * Extended microsoft modifier.
422 DM_DLLIMPORT = (1 << 0),
423 DM_DLLEXPORT = (1 << 1),
424 DM_THREAD = (1 << 2),
426 DM_MICROSOFT_INLINE = (1 << 4),
427 DM_FORCEINLINE = (1 << 5),
428 DM_SELECTANY = (1 << 6),
429 DM_NOTHROW = (1 << 7),
430 DM_NOVTABLE = (1 << 8),
431 DM_NORETURN = (1 << 9),
432 DM_NOINLINE = (1 << 10)
435 typedef unsigned short decl_modifiers_t;
437 struct declaration_t {
438 unsigned char namespc;
439 unsigned char declared_storage_class;
440 unsigned char storage_class;
441 decl_modifiers_t modifiers;
442 unsigned int address_taken : 1;
443 unsigned int is_inline : 1;
444 unsigned int used : 1; /**< Set if the declaration is used. */
447 source_position_t source_position;
450 statement_t *statement;
451 initializer_t *initializer;
452 expression_t *enum_value;
454 scope_t scope; /**< The scope that this declaration opens. */
455 scope_t *parent_scope; /**< The parant scope where this declaration lives. */
457 /** next declaration in a scope */
459 /** next declaration with same symbol */
460 declaration_t *symbol_next;
462 /* the following fields are used in ast2firm module */
463 unsigned char declaration_kind;
465 unsigned int value_number;
478 STATEMENT_DECLARATION,
481 STATEMENT_EXPRESSION,
486 STATEMENT_CASE_LABEL,
493 struct statement_base_t {
494 statement_kind_t kind;
496 source_position_t source_position;
499 struct return_statement_t {
500 statement_base_t base;
504 struct compound_statement_t {
505 statement_base_t base;
506 statement_t *statements;
510 struct declaration_statement_t {
511 statement_base_t base;
512 declaration_t *declarations_begin;
513 declaration_t *declarations_end;
516 struct if_statement_t {
517 statement_base_t base;
518 expression_t *condition;
519 statement_t *true_statement;
520 statement_t *false_statement;
523 struct switch_statement_t {
524 statement_base_t base;
525 expression_t *expression;
527 case_label_statement_t *first_case, *last_case;
530 struct goto_statement_t {
531 statement_base_t base;
532 declaration_t *label; /**< The destination label. */
533 goto_statement_t *next; /**< links all goto statements of a function */
536 struct case_label_statement_t {
537 statement_base_t base;
538 expression_t *expression; /**< The case label expression, NULL for default label. */
539 expression_t *end_range; /**< For GNUC case a .. b: the end range expression, NULL else. */
540 statement_t *statement;
541 case_label_statement_t *next; /**< link to the next case label in switch */
544 struct label_statement_t {
545 statement_base_t base;
546 declaration_t *label;
547 statement_t *statement;
548 label_statement_t *next; /**< links all label statements of a function */
551 struct expression_statement_t {
552 statement_base_t base;
553 expression_t *expression;
556 struct while_statement_t {
557 statement_base_t base;
558 expression_t *condition;
562 struct do_while_statement_t {
563 statement_base_t base;
564 expression_t *condition;
568 struct for_statement_t {
569 statement_base_t base;
570 expression_t *initialisation;
571 expression_t *condition;
577 struct asm_constraint_t {
578 string_t constraints;
579 expression_t *expression;
581 asm_constraint_t *next;
584 struct asm_clobber_t {
589 struct asm_statement_t {
590 statement_base_t base;
592 asm_constraint_t *inputs;
593 asm_constraint_t *outputs;
594 asm_clobber_t *clobbers;
599 statement_kind_t kind;
600 statement_base_t base;
601 return_statement_t returns;
602 compound_statement_t compound;
603 declaration_statement_t declaration;
605 switch_statement_t switchs;
606 goto_statement_t gotos;
607 case_label_statement_t case_label;
608 label_statement_t label;
609 expression_statement_t expression;
610 while_statement_t whiles;
611 do_while_statement_t do_while;
612 for_statement_t fors;
613 asm_statement_t asms;
616 struct translation_unit_t {
621 void *_allocate_ast(size_t size)
623 return obstack_alloc(&ast_obstack, size);
626 #define allocate_ast(size) _allocate_ast(size)