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
26 #include "lang_features.h"
34 #include "adt/error.h"
36 struct obstack ast_obstack;
41 /** If set, implicit casts are printed. */
42 bool print_implicit_casts = false;
44 /** If set parenthesis are printed to indicate operator precedence. */
45 bool print_parenthesis = false;
47 static void print_statement(const statement_t *statement);
48 static void print_expression_prec(const expression_t *expression, unsigned prec);
50 void change_indent(int delta)
56 void print_indent(void)
58 for(int i = 0; i < indent; ++i)
65 PREC_COMMA = 4, /* , left to right */
66 PREC_ASSIGN = 6, /* = += -= *= /= %= <<= >>= &= ^= |= right to left */
67 PREC_COND = 8, /* ?: right to left */
68 PREC_LOG_OR = 10, /* || left to right */
69 PREC_LOG_AND = 12, /* && left to right */
70 PREC_BIT_OR = 14, /* | left to right */
71 PREC_BIT_XOR = 16, /* ^ left to right */
72 PREC_BIT_AND = 18, /* & left to right */
73 PREC_EQ = 20, /* == != left to right */
74 PREC_CMP = 22, /* < <= > >= left to right */
75 PREC_SHF = 24, /* << >> left to right */
76 PREC_PLUS = 26, /* + - left to right */
77 PREC_MUL = 28, /* * / % left to right */
78 PREC_UNARY = 30, /* ! ~ ++ -- + - (type) * & sizeof right to left */
79 PREC_ACCESS = 32, /* () [] -> . left to right */
80 PREC_PRIM = 34, /* primary */
85 * Returns 1 if a given precedence level has right-to-left
86 * associativity, else -1.
88 * @param precedence the operator precedence
90 static int right_to_left(unsigned precedence) {
91 return (precedence == PREC_ASSIGN || precedence == PREC_COND ||
92 precedence == PREC_UNARY) ? 1 : -1;
96 * Return the precedence of an expression given by its kind.
98 * @param kind the expression kind
100 static unsigned get_expression_precedence(expression_kind_t kind)
102 static const unsigned prec[] = {
103 [EXPR_UNKNOWN] = PREC_PRIM,
104 [EXPR_INVALID] = PREC_PRIM,
105 [EXPR_REFERENCE] = PREC_PRIM,
106 [EXPR_CHARACTER_CONSTANT] = PREC_PRIM,
107 [EXPR_WIDE_CHARACTER_CONSTANT] = PREC_PRIM,
108 [EXPR_CONST] = PREC_PRIM,
109 [EXPR_STRING_LITERAL] = PREC_PRIM,
110 [EXPR_WIDE_STRING_LITERAL] = PREC_PRIM,
111 [EXPR_COMPOUND_LITERAL] = PREC_UNARY,
112 [EXPR_CALL] = PREC_ACCESS,
113 [EXPR_CONDITIONAL] = PREC_COND,
114 [EXPR_SELECT] = PREC_ACCESS,
115 [EXPR_ARRAY_ACCESS] = PREC_ACCESS,
116 [EXPR_SIZEOF] = PREC_UNARY,
117 [EXPR_CLASSIFY_TYPE] = PREC_UNARY,
118 [EXPR_ALIGNOF] = PREC_UNARY,
120 [EXPR_FUNCNAME] = PREC_PRIM,
121 [EXPR_BUILTIN_SYMBOL] = PREC_PRIM,
122 [EXPR_BUILTIN_CONSTANT_P] = PREC_PRIM,
123 [EXPR_BUILTIN_PREFETCH] = PREC_PRIM,
124 [EXPR_OFFSETOF] = PREC_PRIM,
125 [EXPR_VA_START] = PREC_PRIM,
126 [EXPR_VA_ARG] = PREC_PRIM,
127 [EXPR_STATEMENT] = PREC_ACCESS,
129 [EXPR_UNARY_NEGATE] = PREC_UNARY,
130 [EXPR_UNARY_PLUS] = PREC_UNARY,
131 [EXPR_UNARY_BITWISE_NEGATE] = PREC_UNARY,
132 [EXPR_UNARY_NOT] = PREC_UNARY,
133 [EXPR_UNARY_DEREFERENCE] = PREC_UNARY,
134 [EXPR_UNARY_TAKE_ADDRESS] = PREC_UNARY,
135 [EXPR_UNARY_POSTFIX_INCREMENT] = PREC_UNARY,
136 [EXPR_UNARY_POSTFIX_DECREMENT] = PREC_UNARY,
137 [EXPR_UNARY_PREFIX_INCREMENT] = PREC_UNARY,
138 [EXPR_UNARY_PREFIX_DECREMENT] = PREC_UNARY,
139 [EXPR_UNARY_CAST] = PREC_UNARY,
140 [EXPR_UNARY_CAST_IMPLICIT] = PREC_UNARY,
141 [EXPR_UNARY_ASSUME] = PREC_PRIM,
143 [EXPR_BINARY_ADD] = PREC_PLUS,
144 [EXPR_BINARY_SUB] = PREC_PLUS,
145 [EXPR_BINARY_MUL] = PREC_MUL,
146 [EXPR_BINARY_DIV] = PREC_MUL,
147 [EXPR_BINARY_MOD] = PREC_MUL,
148 [EXPR_BINARY_EQUAL] = PREC_EQ,
149 [EXPR_BINARY_NOTEQUAL] = PREC_EQ,
150 [EXPR_BINARY_LESS] = PREC_CMP,
151 [EXPR_BINARY_LESSEQUAL] = PREC_CMP,
152 [EXPR_BINARY_GREATER] = PREC_CMP,
153 [EXPR_BINARY_GREATEREQUAL] = PREC_CMP,
154 [EXPR_BINARY_BITWISE_AND] = PREC_BIT_AND,
155 [EXPR_BINARY_BITWISE_OR] = PREC_BIT_OR,
156 [EXPR_BINARY_BITWISE_XOR] = PREC_BIT_XOR,
157 [EXPR_BINARY_LOGICAL_AND] = PREC_LOG_AND,
158 [EXPR_BINARY_LOGICAL_OR] = PREC_LOG_OR,
159 [EXPR_BINARY_SHIFTLEFT] = PREC_SHF,
160 [EXPR_BINARY_SHIFTRIGHT] = PREC_SHF,
161 [EXPR_BINARY_ASSIGN] = PREC_ASSIGN,
162 [EXPR_BINARY_MUL_ASSIGN] = PREC_ASSIGN,
163 [EXPR_BINARY_DIV_ASSIGN] = PREC_ASSIGN,
164 [EXPR_BINARY_MOD_ASSIGN] = PREC_ASSIGN,
165 [EXPR_BINARY_ADD_ASSIGN] = PREC_ASSIGN,
166 [EXPR_BINARY_SUB_ASSIGN] = PREC_ASSIGN,
167 [EXPR_BINARY_SHIFTLEFT_ASSIGN] = PREC_ASSIGN,
168 [EXPR_BINARY_SHIFTRIGHT_ASSIGN] = PREC_ASSIGN,
169 [EXPR_BINARY_BITWISE_AND_ASSIGN] = PREC_ASSIGN,
170 [EXPR_BINARY_BITWISE_XOR_ASSIGN] = PREC_ASSIGN,
171 [EXPR_BINARY_BITWISE_OR_ASSIGN] = PREC_ASSIGN,
172 [EXPR_BINARY_COMMA] = PREC_COMMA,
174 [EXPR_BINARY_BUILTIN_EXPECT] = PREC_PRIM,
175 [EXPR_BINARY_ISGREATER] = PREC_PRIM,
176 [EXPR_BINARY_ISGREATEREQUAL] = PREC_PRIM,
177 [EXPR_BINARY_ISLESS] = PREC_PRIM,
178 [EXPR_BINARY_ISLESSEQUAL] = PREC_PRIM,
179 [EXPR_BINARY_ISLESSGREATER] = PREC_PRIM,
180 [EXPR_BINARY_ISUNORDERED] = PREC_PRIM
182 assert((unsigned)kind < (sizeof(prec)/sizeof(prec[0])));
183 unsigned res = prec[kind];
185 assert(res != PREC_BOTTOM);
190 * Print a constant expression.
192 * @param cnst the constant expression
194 static void print_const(const const_expression_t *cnst)
196 if(cnst->base.type == NULL)
199 const type_t *const type = skip_typeref(cnst->base.type);
201 if (is_type_integer(type)) {
202 fprintf(out, "%lld", cnst->v.int_value);
203 } else if (is_type_float(type)) {
204 long double const val = cnst->v.float_value;
205 fprintf(out, "%.20Lg", val);
206 if (isfinite(val) && truncl(val) == val)
209 panic("unknown constant");
213 switch (type->atomic.akind) {
214 case ATOMIC_TYPE_UINT: suffix = "U"; break;
215 case ATOMIC_TYPE_LONG: suffix = "L"; break;
216 case ATOMIC_TYPE_ULONG: suffix = "UL"; break;
217 case ATOMIC_TYPE_LONGLONG: suffix = "LL"; break;
218 case ATOMIC_TYPE_ULONGLONG: suffix = "ULL"; break;
219 case ATOMIC_TYPE_FLOAT: suffix = "F"; break;
220 case ATOMIC_TYPE_LONG_DOUBLE: suffix = "L"; break;
222 default: suffix = NULL; break;
229 * Print a quoted string constant.
231 * @param string the string constant
232 * @param border the border char
233 * @param skip number of chars to skip at the end
235 static void print_quoted_string(const string_t *const string, char border, int skip)
238 const char *end = string->begin + string->size - skip;
239 for (const char *c = string->begin; c != end; ++c) {
244 case '\\': fputs("\\\\", out); break;
245 case '\a': fputs("\\a", out); break;
246 case '\b': fputs("\\b", out); break;
247 case '\f': fputs("\\f", out); break;
248 case '\n': fputs("\\n", out); break;
249 case '\r': fputs("\\r", out); break;
250 case '\t': fputs("\\t", out); break;
251 case '\v': fputs("\\v", out); break;
252 case '\?': fputs("\\?", out); break;
254 if (c_mode & _GNUC) {
255 fputs("\\e", out); break;
260 fprintf(out, "\\%03o", *c);
271 * Prints a wide string literal expression.
273 * @param wstr the wide string literal expression
274 * @param border the border char
275 * @param skip number of chars to skip at the end
277 static void print_quoted_wide_string(const wide_string_t *const wstr,
278 char border, int skip)
282 const wchar_rep_t *end = wstr->begin + wstr->size - skip;
283 for (const wchar_rep_t *c = wstr->begin; c != end; ++c) {
285 case L'\"': fputs("\\\"", out); break;
286 case L'\\': fputs("\\\\", out); break;
287 case L'\a': fputs("\\a", out); break;
288 case L'\b': fputs("\\b", out); break;
289 case L'\f': fputs("\\f", out); break;
290 case L'\n': fputs("\\n", out); break;
291 case L'\r': fputs("\\r", out); break;
292 case L'\t': fputs("\\t", out); break;
293 case L'\v': fputs("\\v", out); break;
294 case L'\?': fputs("\\?", out); break;
296 if (c_mode & _GNUC) {
297 fputs("\\e", out); break;
301 const unsigned tc = *c;
304 fprintf(out, "\\%03o", (char)*c);
308 } else if (tc < 0x800) {
309 fputc(0xC0 | (tc >> 6), out);
310 fputc(0x80 | (tc & 0x3F), out);
311 } else if (tc < 0x10000) {
312 fputc(0xE0 | ( tc >> 12), out);
313 fputc(0x80 | ((tc >> 6) & 0x3F), out);
314 fputc(0x80 | ( tc & 0x3F), out);
316 fputc(0xF0 | ( tc >> 18), out);
317 fputc(0x80 | ((tc >> 12) & 0x3F), out);
318 fputc(0x80 | ((tc >> 6) & 0x3F), out);
319 fputc(0x80 | ( tc & 0x3F), out);
328 * Print a constant character expression.
330 * @param cnst the constant character expression
332 static void print_character_constant(const const_expression_t *cnst)
334 print_quoted_string(&cnst->v.character, '\'', 0);
337 static void print_wide_character_constant(const const_expression_t *cnst)
339 print_quoted_wide_string(&cnst->v.wide_character, '\'', 0);
343 * Prints a string literal expression.
345 * @param string_literal the string literal expression
347 static void print_string_literal(
348 const string_literal_expression_t *string_literal)
350 print_quoted_string(&string_literal->value, '"', 1);
354 * Prints a predefined symbol.
356 static void print_funcname(
357 const funcname_expression_t *funcname)
360 switch(funcname->kind) {
361 case FUNCNAME_FUNCTION: s = (c_mode & _C99) ? "__func__" : "__FUNCTION__"; break;
362 case FUNCNAME_PRETTY_FUNCTION: s = "__PRETTY_FUNCTION__"; break;
363 case FUNCNAME_FUNCSIG: s = "__FUNCSIG__"; break;
364 case FUNCNAME_FUNCDNAME: s = "__FUNCDNAME__"; break;
371 static void print_wide_string_literal(
372 const wide_string_literal_expression_t *const wstr)
374 print_quoted_wide_string(&wstr->value, '"', 1);
377 static void print_compound_literal(
378 const compound_literal_expression_t *expression)
381 print_type(expression->type);
383 print_initializer(expression->initializer);
387 * Prints a call expression.
389 * @param call the call expression
391 static void print_call_expression(const call_expression_t *call)
393 unsigned prec = get_expression_precedence(call->base.kind);
394 print_expression_prec(call->function, prec);
396 call_argument_t *argument = call->arguments;
398 while(argument != NULL) {
404 print_expression_prec(argument->expression, PREC_COMMA + 1);
406 argument = argument->next;
412 * Prints a binary expression.
414 * @param binexpr the binary expression
416 static void print_binary_expression(const binary_expression_t *binexpr)
418 unsigned prec = get_expression_precedence(binexpr->base.kind);
419 int r2l = right_to_left(prec);
421 if(binexpr->base.kind == EXPR_BINARY_BUILTIN_EXPECT) {
422 fputs("__builtin_expect(", out);
423 print_expression_prec(binexpr->left, prec);
425 print_expression_prec(binexpr->right, prec);
430 print_expression_prec(binexpr->left, prec + r2l);
431 if (binexpr->base.kind != EXPR_BINARY_COMMA) {
434 switch (binexpr->base.kind) {
435 case EXPR_BINARY_COMMA: fputs(",", out); break;
436 case EXPR_BINARY_ASSIGN: fputs("=", out); break;
437 case EXPR_BINARY_ADD: fputs("+", out); break;
438 case EXPR_BINARY_SUB: fputs("-", out); break;
439 case EXPR_BINARY_MUL: fputs("*", out); break;
440 case EXPR_BINARY_MOD: fputs("%", out); break;
441 case EXPR_BINARY_DIV: fputs("/", out); break;
442 case EXPR_BINARY_BITWISE_OR: fputs("|", out); break;
443 case EXPR_BINARY_BITWISE_AND: fputs("&", out); break;
444 case EXPR_BINARY_BITWISE_XOR: fputs("^", out); break;
445 case EXPR_BINARY_LOGICAL_OR: fputs("||", out); break;
446 case EXPR_BINARY_LOGICAL_AND: fputs("&&", out); break;
447 case EXPR_BINARY_NOTEQUAL: fputs("!=", out); break;
448 case EXPR_BINARY_EQUAL: fputs("==", out); break;
449 case EXPR_BINARY_LESS: fputs("<", out); break;
450 case EXPR_BINARY_LESSEQUAL: fputs("<=", out); break;
451 case EXPR_BINARY_GREATER: fputs(">", out); break;
452 case EXPR_BINARY_GREATEREQUAL: fputs(">=", out); break;
453 case EXPR_BINARY_SHIFTLEFT: fputs("<<", out); break;
454 case EXPR_BINARY_SHIFTRIGHT: fputs(">>", out); break;
456 case EXPR_BINARY_ADD_ASSIGN: fputs("+=", out); break;
457 case EXPR_BINARY_SUB_ASSIGN: fputs("-=", out); break;
458 case EXPR_BINARY_MUL_ASSIGN: fputs("*=", out); break;
459 case EXPR_BINARY_MOD_ASSIGN: fputs("%=", out); break;
460 case EXPR_BINARY_DIV_ASSIGN: fputs("/=", out); break;
461 case EXPR_BINARY_BITWISE_OR_ASSIGN: fputs("|=", out); break;
462 case EXPR_BINARY_BITWISE_AND_ASSIGN: fputs("&=", out); break;
463 case EXPR_BINARY_BITWISE_XOR_ASSIGN: fputs("^=", out); break;
464 case EXPR_BINARY_SHIFTLEFT_ASSIGN: fputs("<<=", out); break;
465 case EXPR_BINARY_SHIFTRIGHT_ASSIGN: fputs(">>=", out); break;
466 default: panic("invalid binexpression found");
469 print_expression_prec(binexpr->right, prec - r2l);
473 * Prints an unary expression.
475 * @param unexpr the unary expression
477 static void print_unary_expression(const unary_expression_t *unexpr)
479 unsigned prec = get_expression_precedence(unexpr->base.kind);
480 switch(unexpr->base.kind) {
481 case EXPR_UNARY_NEGATE: fputs("-", out); break;
482 case EXPR_UNARY_PLUS: fputs("+", out); break;
483 case EXPR_UNARY_NOT: fputs("!", out); break;
484 case EXPR_UNARY_BITWISE_NEGATE: fputs("~", out); break;
485 case EXPR_UNARY_PREFIX_INCREMENT: fputs("++", out); break;
486 case EXPR_UNARY_PREFIX_DECREMENT: fputs("--", out); break;
487 case EXPR_UNARY_DEREFERENCE: fputs("*", out); break;
488 case EXPR_UNARY_TAKE_ADDRESS: fputs("&", out); break;
490 case EXPR_UNARY_POSTFIX_INCREMENT:
491 print_expression_prec(unexpr->value, prec);
494 case EXPR_UNARY_POSTFIX_DECREMENT:
495 print_expression_prec(unexpr->value, prec);
498 case EXPR_UNARY_CAST_IMPLICIT:
499 case EXPR_UNARY_CAST:
501 print_type(unexpr->base.type);
504 case EXPR_UNARY_ASSUME:
505 fputs("__assume(", out);
506 print_expression_prec(unexpr->value, PREC_COMMA + 1);
510 panic("invalid unary expression found");
512 print_expression_prec(unexpr->value, prec);
516 * Prints a reference expression.
518 * @param ref the reference expression
520 static void print_reference_expression(const reference_expression_t *ref)
522 fprintf(out, "%s", ref->declaration->symbol->string);
526 * Prints an array expression.
528 * @param expression the array expression
530 static void print_array_expression(const array_access_expression_t *expression)
532 unsigned prec = get_expression_precedence(expression->base.kind);
533 if(!expression->flipped) {
534 print_expression_prec(expression->array_ref, prec);
536 print_expression_prec(expression->index, PREC_BOTTOM);
539 print_expression_prec(expression->index, prec);
541 print_expression_prec(expression->array_ref, PREC_BOTTOM);
547 * Prints a typeproperty expression (sizeof or __alignof__).
549 * @param expression the type property expression
551 static void print_typeprop_expression(const typeprop_expression_t *expression)
553 if (expression->base.kind == EXPR_SIZEOF) {
554 fputs("sizeof", out);
556 assert(expression->base.kind == EXPR_ALIGNOF);
557 fputs("__alignof__", out);
559 if(expression->tp_expression != NULL) {
560 /* always print the '()' here, sizeof x is right but unusual */
562 print_expression_prec(expression->tp_expression, PREC_ACCESS);
566 print_type(expression->type);
572 * Prints an builtin symbol.
574 * @param expression the builtin symbol expression
576 static void print_builtin_symbol(const builtin_symbol_expression_t *expression)
578 fputs(expression->symbol->string, out);
582 * Prints a builtin constant expression.
584 * @param expression the builtin constant expression
586 static void print_builtin_constant(const builtin_constant_expression_t *expression)
588 fputs("__builtin_constant_p(", out);
589 print_expression_prec(expression->value, PREC_COMMA + 1);
594 * Prints a builtin prefetch expression.
596 * @param expression the builtin prefetch expression
598 static void print_builtin_prefetch(const builtin_prefetch_expression_t *expression)
600 fputs("__builtin_prefetch(", out);
601 print_expression_prec(expression->adr, PREC_COMMA + 1);
602 if (expression->rw) {
604 print_expression_prec(expression->rw, PREC_COMMA + 1);
606 if (expression->locality) {
608 print_expression_prec(expression->locality, PREC_COMMA + 1);
614 * Prints a conditional expression.
616 * @param expression the conditional expression
618 static void print_conditional(const conditional_expression_t *expression)
620 print_expression_prec(expression->condition, PREC_LOG_OR);
622 if (expression->true_expression != NULL) {
623 print_expression_prec(expression->true_expression, PREC_EXPR);
628 print_expression_prec(expression->false_expression, PREC_COND);
632 * Prints a va_start expression.
634 * @param expression the va_start expression
636 static void print_va_start(const va_start_expression_t *const expression)
638 fputs("__builtin_va_start(", out);
639 print_expression_prec(expression->ap, PREC_COMMA + 1);
641 fputs(expression->parameter->symbol->string, out);
646 * Prints a va_arg expression.
648 * @param expression the va_arg expression
650 static void print_va_arg(const va_arg_expression_t *expression)
652 fputs("__builtin_va_arg(", out);
653 print_expression_prec(expression->ap, PREC_COMMA + 1);
655 print_type(expression->base.type);
660 * Prints a select expression (. or ->).
662 * @param expression the select expression
664 static void print_select(const select_expression_t *expression)
666 unsigned prec = get_expression_precedence(expression->base.kind);
667 print_expression_prec(expression->compound, prec);
668 if(is_type_pointer(skip_typeref(expression->compound->base.type))) {
673 fputs(expression->compound_entry->symbol->string, out);
677 * Prints a type classify expression.
679 * @param expr the type classify expression
681 static void print_classify_type_expression(
682 const classify_type_expression_t *const expr)
684 fputs("__builtin_classify_type(", out);
685 print_expression_prec(expr->type_expression, PREC_COMMA + 1);
690 * Prints a designator.
692 * @param designator the designator
694 static void print_designator(const designator_t *designator)
696 for ( ; designator != NULL; designator = designator->next) {
697 if (designator->symbol == NULL) {
699 print_expression_prec(designator->array_index, PREC_BOTTOM);
703 fputs(designator->symbol->string, out);
709 * Prints an offsetof expression.
711 * @param expression the offset expression
713 static void print_offsetof_expression(const offsetof_expression_t *expression)
715 fputs("__builtin_offsetof", out);
717 print_type(expression->type);
719 print_designator(expression->designator);
724 * Prints a statement expression.
726 * @param expression the statement expression
728 static void print_statement_expression(const statement_expression_t *expression)
731 print_statement(expression->statement);
736 * Prints an expression with parenthesis if needed.
738 * @param expression the expression to print
739 * @param top_prec the precedence of the user of this expression.
741 static void print_expression_prec(const expression_t *expression, unsigned top_prec)
743 if (expression->kind == EXPR_UNARY_CAST_IMPLICIT && !print_implicit_casts) {
744 expression = expression->unary.value;
746 unsigned prec = get_expression_precedence(expression->base.kind);
747 if (print_parenthesis && top_prec != PREC_BOTTOM)
751 switch(expression->kind) {
754 fprintf(out, "$invalid expression$");
756 case EXPR_CHARACTER_CONSTANT:
757 print_character_constant(&expression->conste);
759 case EXPR_WIDE_CHARACTER_CONSTANT:
760 print_wide_character_constant(&expression->conste);
763 print_const(&expression->conste);
766 print_funcname(&expression->funcname);
768 case EXPR_STRING_LITERAL:
769 print_string_literal(&expression->string);
771 case EXPR_WIDE_STRING_LITERAL:
772 print_wide_string_literal(&expression->wide_string);
774 case EXPR_COMPOUND_LITERAL:
775 print_compound_literal(&expression->compound_literal);
778 print_call_expression(&expression->call);
781 print_binary_expression(&expression->binary);
784 print_reference_expression(&expression->reference);
786 case EXPR_ARRAY_ACCESS:
787 print_array_expression(&expression->array_access);
790 print_unary_expression(&expression->unary);
794 print_typeprop_expression(&expression->typeprop);
796 case EXPR_BUILTIN_SYMBOL:
797 print_builtin_symbol(&expression->builtin_symbol);
799 case EXPR_BUILTIN_CONSTANT_P:
800 print_builtin_constant(&expression->builtin_constant);
802 case EXPR_BUILTIN_PREFETCH:
803 print_builtin_prefetch(&expression->builtin_prefetch);
805 case EXPR_CONDITIONAL:
806 print_conditional(&expression->conditional);
809 print_va_start(&expression->va_starte);
812 print_va_arg(&expression->va_arge);
815 print_select(&expression->select);
817 case EXPR_CLASSIFY_TYPE:
818 print_classify_type_expression(&expression->classify_type);
821 print_offsetof_expression(&expression->offsetofe);
824 print_statement_expression(&expression->statement);
829 fprintf(out, "some expression of type %d", (int) expression->kind);
837 * Print an compound statement.
839 * @param block the compound statement
841 static void print_compound_statement(const compound_statement_t *block)
846 statement_t *statement = block->statements;
847 while(statement != NULL) {
848 if (statement->base.kind == STATEMENT_CASE_LABEL)
851 print_statement(statement);
853 statement = statement->base.next;
861 * Print a return statement.
863 * @param statement the return statement
865 static void print_return_statement(const return_statement_t *statement)
867 fprintf(out, "return ");
868 if(statement->value != NULL)
869 print_expression(statement->value);
874 * Print an expression statement.
876 * @param statement the expression statement
878 static void print_expression_statement(const expression_statement_t *statement)
880 print_expression(statement->expression);
885 * Print a goto statement.
887 * @param statement the goto statement
889 static void print_goto_statement(const goto_statement_t *statement)
891 fprintf(out, "goto ");
892 if (statement->expression != NULL) {
894 print_expression(statement->expression);
896 fputs(statement->label->symbol->string, out);
902 * Print a label statement.
904 * @param statement the label statement
906 static void print_label_statement(const label_statement_t *statement)
908 fprintf(out, "%s:\n", statement->label->symbol->string);
909 print_statement(statement->statement);
913 * Print an if statement.
915 * @param statement the if statement
917 static void print_if_statement(const if_statement_t *statement)
920 print_expression(statement->condition);
922 print_statement(statement->true_statement);
924 if(statement->false_statement != NULL) {
927 print_statement(statement->false_statement);
932 * Print a switch statement.
934 * @param statement the switch statement
936 static void print_switch_statement(const switch_statement_t *statement)
938 fputs("switch (", out);
939 print_expression(statement->expression);
941 print_statement(statement->body);
945 * Print a case label (including the default label).
947 * @param statement the case label statement
949 static void print_case_label(const case_label_statement_t *statement)
951 if(statement->expression == NULL) {
952 fputs("default:\n", out);
955 print_expression(statement->expression);
956 if (statement->end_range != NULL) {
958 print_expression(statement->end_range);
963 if(statement->statement != NULL) {
964 if (statement->statement->base.kind == STATEMENT_CASE_LABEL) {
968 print_statement(statement->statement);
973 * Print a declaration statement.
975 * @param statement the statement
977 static void print_declaration_statement(
978 const declaration_statement_t *statement)
981 for (declaration_t *declaration = statement->declarations_begin;
982 declaration != statement->declarations_end->next;
983 declaration = declaration->next) {
984 if (declaration->storage_class == STORAGE_CLASS_ENUM_ENTRY)
986 if (declaration->implicit)
994 print_declaration(declaration);
1000 * Print a while statement.
1002 * @param statement the statement
1004 static void print_while_statement(const while_statement_t *statement)
1006 fputs("while (", out);
1007 print_expression(statement->condition);
1009 print_statement(statement->body);
1013 * Print a do-while statement.
1015 * @param statement the statement
1017 static void print_do_while_statement(const do_while_statement_t *statement)
1020 print_statement(statement->body);
1022 fputs("while (", out);
1023 print_expression(statement->condition);
1028 * Print a for statement.
1030 * @param statement the statement
1032 static void print_for_statement(const for_statement_t *statement)
1034 fputs("for (", out);
1035 declaration_t *decl = statement->scope.declarations;
1036 while (decl != NULL && decl->implicit)
1039 assert(statement->initialisation == NULL);
1040 print_declaration(decl);
1041 if (decl->next != NULL) {
1042 panic("multiple declarations in for statement not supported yet");
1046 if(statement->initialisation) {
1047 print_expression(statement->initialisation);
1051 if(statement->condition != NULL) {
1052 print_expression(statement->condition);
1055 if(statement->step != NULL) {
1056 print_expression(statement->step);
1059 print_statement(statement->body);
1063 * Print assembler arguments.
1065 * @param arguments the arguments
1067 static void print_asm_arguments(asm_argument_t *arguments)
1069 asm_argument_t *argument = arguments;
1070 for( ; argument != NULL; argument = argument->next) {
1071 if(argument != arguments)
1074 if(argument->symbol) {
1075 fprintf(out, "[%s] ", argument->symbol->string);
1077 print_quoted_string(&argument->constraints, '"', 1);
1079 print_expression(argument->expression);
1085 * Print assembler clobbers.
1087 * @param clobbers the clobbers
1089 static void print_asm_clobbers(asm_clobber_t *clobbers)
1091 asm_clobber_t *clobber = clobbers;
1092 for( ; clobber != NULL; clobber = clobber->next) {
1093 if(clobber != clobbers)
1096 print_quoted_string(&clobber->clobber, '"', 1);
1101 * Print an assembler statement.
1103 * @param statement the statement
1105 static void print_asm_statement(const asm_statement_t *statement)
1108 if(statement->is_volatile) {
1109 fputs("volatile ", out);
1112 print_quoted_string(&statement->asm_text, '"', 1);
1113 if(statement->inputs == NULL && statement->outputs == NULL
1114 && statement->clobbers == NULL)
1115 goto end_of_print_asm_statement;
1118 print_asm_arguments(statement->inputs);
1119 if(statement->outputs == NULL && statement->clobbers == NULL)
1120 goto end_of_print_asm_statement;
1123 print_asm_arguments(statement->outputs);
1124 if(statement->clobbers == NULL)
1125 goto end_of_print_asm_statement;
1128 print_asm_clobbers(statement->clobbers);
1130 end_of_print_asm_statement:
1135 * Print a microsoft __try statement.
1137 * @param statement the statement
1139 static void print_ms_try_statement(const ms_try_statement_t *statement)
1141 fputs("__try ", out);
1142 print_statement(statement->try_statement);
1144 if(statement->except_expression != NULL) {
1145 fputs("__except(", out);
1146 print_expression(statement->except_expression);
1149 fputs("__finally ", out);
1151 print_statement(statement->final_statement);
1155 * Print a microsoft __leave statement.
1157 * @param statement the statement
1159 static void print_leave_statement(const leave_statement_t *statement)
1162 fputs("__leave;\n", out);
1166 * Print a statement.
1168 * @param statement the statement
1170 void print_statement(const statement_t *statement)
1172 switch(statement->kind) {
1173 case STATEMENT_EMPTY:
1176 case STATEMENT_COMPOUND:
1177 print_compound_statement(&statement->compound);
1179 case STATEMENT_RETURN:
1180 print_return_statement(&statement->returns);
1182 case STATEMENT_EXPRESSION:
1183 print_expression_statement(&statement->expression);
1185 case STATEMENT_LABEL:
1186 print_label_statement(&statement->label);
1188 case STATEMENT_GOTO:
1189 print_goto_statement(&statement->gotos);
1191 case STATEMENT_CONTINUE:
1192 fputs("continue;\n", out);
1194 case STATEMENT_BREAK:
1195 fputs("break;\n", out);
1198 print_if_statement(&statement->ifs);
1200 case STATEMENT_SWITCH:
1201 print_switch_statement(&statement->switchs);
1203 case STATEMENT_CASE_LABEL:
1204 print_case_label(&statement->case_label);
1206 case STATEMENT_DECLARATION:
1207 print_declaration_statement(&statement->declaration);
1209 case STATEMENT_WHILE:
1210 print_while_statement(&statement->whiles);
1212 case STATEMENT_DO_WHILE:
1213 print_do_while_statement(&statement->do_while);
1216 print_for_statement(&statement->fors);
1219 print_asm_statement(&statement->asms);
1221 case STATEMENT_MS_TRY:
1222 print_ms_try_statement(&statement->ms_try);
1224 case STATEMENT_LEAVE:
1225 print_leave_statement(&statement->leave);
1227 case STATEMENT_INVALID:
1228 fprintf(out, "$invalid statement$");
1234 * Print a storage class.
1236 * @param storage_class the storage class
1238 static void print_storage_class(storage_class_tag_t storage_class)
1240 switch(storage_class) {
1241 case STORAGE_CLASS_ENUM_ENTRY:
1242 case STORAGE_CLASS_NONE:
1244 case STORAGE_CLASS_TYPEDEF: fputs("typedef ", out); break;
1245 case STORAGE_CLASS_EXTERN: fputs("extern ", out); break;
1246 case STORAGE_CLASS_STATIC: fputs("static ", out); break;
1247 case STORAGE_CLASS_AUTO: fputs("auto ", out); break;
1248 case STORAGE_CLASS_REGISTER: fputs("register ", out); break;
1249 case STORAGE_CLASS_THREAD: fputs("__thread", out); break;
1250 case STORAGE_CLASS_THREAD_EXTERN: fputs("extern __thread", out); break;
1251 case STORAGE_CLASS_THREAD_STATIC: fputs("static __thread", out); break;
1256 * Print an initializer.
1258 * @param initializer the initializer
1260 void print_initializer(const initializer_t *initializer)
1262 if(initializer == NULL) {
1267 switch(initializer->kind) {
1268 case INITIALIZER_VALUE: {
1269 const initializer_value_t *value = &initializer->value;
1270 print_expression(value->value);
1273 case INITIALIZER_LIST: {
1274 assert(initializer->kind == INITIALIZER_LIST);
1276 const initializer_list_t *list = &initializer->list;
1278 for(size_t i = 0 ; i < list->len; ++i) {
1279 const initializer_t *sub_init = list->initializers[i];
1280 print_initializer(list->initializers[i]);
1281 if(i < list->len-1) {
1282 if(sub_init == NULL || sub_init->kind != INITIALIZER_DESIGNATOR)
1289 case INITIALIZER_STRING:
1290 print_quoted_string(&initializer->string.string, '"', 1);
1292 case INITIALIZER_WIDE_STRING:
1293 print_quoted_wide_string(&initializer->wide_string.string, '"', 1);
1295 case INITIALIZER_DESIGNATOR:
1296 print_designator(initializer->designator.designator);
1301 panic("invalid initializer kind found");
1305 * Print microsoft extended declaration modifiers.
1307 static void print_ms_modifiers(const declaration_t *declaration) {
1308 if((c_mode & _MS) == 0)
1311 decl_modifiers_t modifiers = declaration->modifiers;
1313 /* DM_FORCEINLINE handled outside. */
1314 if ((modifiers & ~DM_FORCEINLINE) != 0 ||
1315 declaration->alignment != 0 ||
1316 declaration->get_property_sym != NULL ||
1317 declaration->put_property_sym != NULL) {
1320 fputs("__declspec", out);
1321 if(modifiers & DM_DLLIMPORT) {
1322 fputs(next, out); next = ", "; fputs("dllimport", out);
1324 if(modifiers & DM_DLLEXPORT) {
1325 fputs(next, out); next = ", "; fputs("dllexport", out);
1327 if(modifiers & DM_THREAD) {
1328 fputs(next, out); next = ", "; fputs("thread", out);
1330 if(modifiers & DM_NAKED) {
1331 fputs(next, out); next = ", "; fputs("naked", out);
1333 if(modifiers & DM_THREAD) {
1334 fputs(next, out); next = ", "; fputs("thread", out);
1336 if(modifiers & DM_SELECTANY) {
1337 fputs(next, out); next = ", "; fputs("selectany", out);
1339 if(modifiers & DM_NOTHROW) {
1340 fputs(next, out); next = ", "; fputs("nothrow", out);
1342 if(modifiers & DM_NORETURN) {
1343 fputs(next, out); next = ", "; fputs("noreturn", out);
1345 if(modifiers & DM_NOINLINE) {
1346 fputs(next, out); next = ", "; fputs("noinline", out);
1348 if (modifiers & DM_DEPRECATED) {
1349 fputs(next, out); next = ", "; fputs("deprecated", out);
1350 if(declaration->deprecated_string != NULL)
1351 fprintf(out, "(\"%s\")", declaration->deprecated_string);
1353 if(declaration->alignment != 0) {
1354 fputs(next, out); next = ", "; fprintf(out, "align(%u)", declaration->alignment);
1356 if(modifiers & DM_RESTRICT) {
1357 fputs(next, out); next = ", "; fputs("restrict", out);
1359 if(modifiers & DM_NOALIAS) {
1360 fputs(next, out); next = ", "; fputs("noalias", out);
1362 if(declaration->get_property_sym != NULL || declaration->put_property_sym != NULL) {
1364 fputs(next, out); next = ", "; fprintf(out, "property(");
1365 if(declaration->get_property_sym != NULL) {
1366 fprintf(out, "get=%s", declaration->get_property_sym->string);
1369 if(declaration->put_property_sym != NULL)
1370 fprintf(out, "%sput=%s", comma, declaration->put_property_sym->string);
1378 * Print a declaration in the NORMAL namespace.
1380 * @param declaration the declaration
1382 static void print_normal_declaration(const declaration_t *declaration)
1384 print_storage_class((storage_class_tag_t) declaration->declared_storage_class);
1385 if (declaration->is_inline) {
1386 if (declaration->modifiers & DM_FORCEINLINE) {
1387 fputs("__forceinline ", out);
1388 } else if (declaration->modifiers & DM_MICROSOFT_INLINE) {
1389 fputs("__inline ", out);
1391 fputs("inline ", out);
1394 print_ms_modifiers(declaration);
1395 print_type_ext(declaration->type, declaration->symbol,
1396 &declaration->scope);
1398 if(declaration->type->kind == TYPE_FUNCTION) {
1399 if(declaration->init.statement != NULL) {
1401 print_statement(declaration->init.statement);
1404 } else if(declaration->init.initializer != NULL) {
1406 print_initializer(declaration->init.initializer);
1412 * Prints an expression.
1414 * @param expression the expression
1416 void print_expression(const expression_t *expression) {
1417 print_expression_prec(expression, PREC_BOTTOM);
1421 * Print a declaration.
1423 * @param declaration the declaration
1425 void print_declaration(const declaration_t *declaration)
1427 if(declaration->namespc != NAMESPACE_NORMAL &&
1428 declaration->symbol == NULL)
1431 switch(declaration->namespc) {
1432 case NAMESPACE_NORMAL:
1433 print_normal_declaration(declaration);
1435 case NAMESPACE_STRUCT:
1436 fputs("struct ", out);
1437 fputs(declaration->symbol->string, out);
1439 print_compound_definition(declaration);
1442 case NAMESPACE_UNION:
1443 fputs("union ", out);
1444 fputs(declaration->symbol->string, out);
1446 print_compound_definition(declaration);
1449 case NAMESPACE_ENUM:
1450 fputs("enum ", out);
1451 fputs(declaration->symbol->string, out);
1453 print_enum_definition(declaration);
1460 * Print the AST of a translation unit.
1462 * @param unit the translation unit
1464 void print_ast(const translation_unit_t *unit)
1468 declaration_t *declaration = unit->scope.declarations;
1469 for( ; declaration != NULL; declaration = declaration->next) {
1470 if(declaration->storage_class == STORAGE_CLASS_ENUM_ENTRY)
1472 if(declaration->namespc != NAMESPACE_NORMAL &&
1473 declaration->symbol == NULL)
1475 if (declaration->implicit)
1479 print_declaration(declaration);
1484 bool is_constant_initializer(const initializer_t *initializer)
1486 switch(initializer->kind) {
1487 case INITIALIZER_STRING:
1488 case INITIALIZER_WIDE_STRING:
1489 case INITIALIZER_DESIGNATOR:
1492 case INITIALIZER_VALUE:
1493 return is_constant_expression(initializer->value.value);
1495 case INITIALIZER_LIST:
1496 for(size_t i = 0; i < initializer->list.len; ++i) {
1497 initializer_t *sub_initializer = initializer->list.initializers[i];
1498 if(!is_constant_initializer(sub_initializer))
1503 panic("invalid initializer kind found");
1506 static bool is_object_with_linker_constant_address(const expression_t *expression)
1508 switch(expression->kind) {
1509 case EXPR_UNARY_DEREFERENCE:
1510 return is_address_constant(expression->unary.value);
1513 type_t *base_type = skip_typeref(expression->select.compound->base.type);
1514 if(is_type_pointer(base_type)) {
1516 return is_address_constant(expression->select.compound);
1518 return is_object_with_linker_constant_address(expression->select.compound);
1522 case EXPR_ARRAY_ACCESS:
1523 return is_constant_expression(expression->array_access.index)
1524 && is_address_constant(expression->array_access.array_ref);
1526 case EXPR_REFERENCE: {
1527 declaration_t *declaration = expression->reference.declaration;
1528 switch((storage_class_tag_t) declaration->storage_class) {
1529 case STORAGE_CLASS_NONE:
1530 case STORAGE_CLASS_EXTERN:
1531 case STORAGE_CLASS_STATIC:
1543 bool is_address_constant(const expression_t *expression)
1545 switch(expression->kind) {
1546 case EXPR_UNARY_TAKE_ADDRESS:
1547 return is_object_with_linker_constant_address(expression->unary.value);
1549 case EXPR_UNARY_DEREFERENCE: {
1551 = revert_automatic_type_conversion(expression->unary.value);
1552 /* dereferencing a function is a NOP */
1553 if(is_type_function(real_type)) {
1554 return is_address_constant(expression->unary.value);
1560 case EXPR_UNARY_CAST: {
1561 type_t *dest = skip_typeref(expression->base.type);
1562 if (!is_type_pointer(dest) &&
1563 ! (dest->kind == TYPE_ATOMIC
1564 && (get_atomic_type_flags(dest->atomic.akind) & ATOMIC_TYPE_FLAG_INTEGER)
1565 && (get_atomic_type_size(dest->atomic.akind) >= get_atomic_type_size(get_intptr_kind()))))
1568 return (is_constant_expression(expression->unary.value)
1569 || is_address_constant(expression->unary.value));
1572 case EXPR_BINARY_ADD:
1573 case EXPR_BINARY_SUB: {
1574 expression_t *left = expression->binary.left;
1575 expression_t *right = expression->binary.right;
1577 if(is_type_pointer(skip_typeref(left->base.type))) {
1578 return is_address_constant(left) && is_constant_expression(right);
1579 } else if(is_type_pointer(skip_typeref(right->base.type))) {
1580 return is_constant_expression(left) && is_address_constant(right);
1586 case EXPR_REFERENCE: {
1587 declaration_t *declaration = expression->reference.declaration;
1588 type_t *type = skip_typeref(declaration->type);
1589 if(is_type_function(type))
1591 if(is_type_array(type)) {
1592 return is_object_with_linker_constant_address(expression);
1602 static bool is_builtin_const_call(const expression_t *expression)
1604 expression_t *function = expression->call.function;
1605 if (function->kind != EXPR_BUILTIN_SYMBOL) {
1609 symbol_t *symbol = function->builtin_symbol.symbol;
1611 switch (symbol->ID) {
1612 case T___builtin_huge_val:
1613 case T___builtin_nan:
1614 case T___builtin_nanf:
1615 case T___builtin_nand:
1622 static bool is_constant_pointer(const expression_t *expression)
1624 if (is_constant_expression(expression))
1627 switch (expression->kind) {
1629 return is_constant_pointer(expression->select.compound);
1630 case EXPR_UNARY_CAST:
1631 return is_constant_pointer(expression->unary.value);
1637 static bool is_object_with_constant_address(const expression_t *expression)
1639 switch(expression->kind) {
1641 expression_t *compound = expression->select.compound;
1642 type_t *compound_type = compound->base.type;
1643 compound_type = skip_typeref(compound_type);
1644 if(is_type_pointer(compound_type)) {
1645 return is_constant_pointer(compound);
1647 return is_object_with_constant_address(compound);
1650 case EXPR_ARRAY_ACCESS:
1651 return is_constant_pointer(expression->array_access.array_ref)
1652 && is_constant_expression(expression->array_access.index);
1653 case EXPR_UNARY_DEREFERENCE:
1654 return is_constant_pointer(expression->unary.value);
1660 bool is_constant_expression(const expression_t *expression)
1662 switch(expression->kind) {
1665 case EXPR_CHARACTER_CONSTANT:
1666 case EXPR_WIDE_CHARACTER_CONSTANT:
1667 case EXPR_STRING_LITERAL:
1668 case EXPR_WIDE_STRING_LITERAL:
1669 case EXPR_CLASSIFY_TYPE:
1673 case EXPR_BUILTIN_CONSTANT_P:
1677 type_t *type = expression->typeprop.type;
1679 type = expression->typeprop.tp_expression->base.type;
1681 type = skip_typeref(type);
1682 if (is_type_array(type) && type->array.is_vla)
1687 case EXPR_BUILTIN_SYMBOL:
1688 case EXPR_BUILTIN_PREFETCH:
1692 case EXPR_STATEMENT:
1693 case EXPR_UNARY_POSTFIX_INCREMENT:
1694 case EXPR_UNARY_POSTFIX_DECREMENT:
1695 case EXPR_UNARY_PREFIX_INCREMENT:
1696 case EXPR_UNARY_PREFIX_DECREMENT:
1697 case EXPR_UNARY_ASSUME: /* has VOID type */
1698 case EXPR_UNARY_DEREFERENCE:
1699 case EXPR_BINARY_ASSIGN:
1700 case EXPR_BINARY_MUL_ASSIGN:
1701 case EXPR_BINARY_DIV_ASSIGN:
1702 case EXPR_BINARY_MOD_ASSIGN:
1703 case EXPR_BINARY_ADD_ASSIGN:
1704 case EXPR_BINARY_SUB_ASSIGN:
1705 case EXPR_BINARY_SHIFTLEFT_ASSIGN:
1706 case EXPR_BINARY_SHIFTRIGHT_ASSIGN:
1707 case EXPR_BINARY_BITWISE_AND_ASSIGN:
1708 case EXPR_BINARY_BITWISE_XOR_ASSIGN:
1709 case EXPR_BINARY_BITWISE_OR_ASSIGN:
1710 case EXPR_BINARY_COMMA:
1713 case EXPR_UNARY_TAKE_ADDRESS:
1714 return is_object_with_constant_address(expression->unary.value);
1717 return is_builtin_const_call(expression);
1719 case EXPR_UNARY_NEGATE:
1720 case EXPR_UNARY_PLUS:
1721 case EXPR_UNARY_BITWISE_NEGATE:
1722 case EXPR_UNARY_NOT:
1723 return is_constant_expression(expression->unary.value);
1725 case EXPR_UNARY_CAST:
1726 case EXPR_UNARY_CAST_IMPLICIT:
1727 return is_type_arithmetic(skip_typeref(expression->base.type))
1728 && is_constant_expression(expression->unary.value);
1730 case EXPR_BINARY_ADD:
1731 case EXPR_BINARY_SUB:
1732 case EXPR_BINARY_MUL:
1733 case EXPR_BINARY_DIV:
1734 case EXPR_BINARY_MOD:
1735 case EXPR_BINARY_EQUAL:
1736 case EXPR_BINARY_NOTEQUAL:
1737 case EXPR_BINARY_LESS:
1738 case EXPR_BINARY_LESSEQUAL:
1739 case EXPR_BINARY_GREATER:
1740 case EXPR_BINARY_GREATEREQUAL:
1741 case EXPR_BINARY_BITWISE_AND:
1742 case EXPR_BINARY_BITWISE_OR:
1743 case EXPR_BINARY_BITWISE_XOR:
1744 case EXPR_BINARY_LOGICAL_AND:
1745 case EXPR_BINARY_LOGICAL_OR:
1746 case EXPR_BINARY_SHIFTLEFT:
1747 case EXPR_BINARY_SHIFTRIGHT:
1748 case EXPR_BINARY_BUILTIN_EXPECT:
1749 case EXPR_BINARY_ISGREATER:
1750 case EXPR_BINARY_ISGREATEREQUAL:
1751 case EXPR_BINARY_ISLESS:
1752 case EXPR_BINARY_ISLESSEQUAL:
1753 case EXPR_BINARY_ISLESSGREATER:
1754 case EXPR_BINARY_ISUNORDERED:
1755 return is_constant_expression(expression->binary.left)
1756 && is_constant_expression(expression->binary.right);
1758 case EXPR_COMPOUND_LITERAL:
1759 return is_constant_initializer(expression->compound_literal.initializer);
1761 case EXPR_CONDITIONAL: {
1762 expression_t *condition = expression->conditional.condition;
1763 if(!is_constant_expression(condition))
1766 long val = fold_constant(condition);
1768 return is_constant_expression(expression->conditional.true_expression);
1770 return is_constant_expression(expression->conditional.false_expression);
1773 case EXPR_ARRAY_ACCESS:
1774 return is_constant_expression(expression->array_access.array_ref)
1775 && is_constant_expression(expression->array_access.index);
1777 case EXPR_REFERENCE: {
1778 declaration_t *declaration = expression->reference.declaration;
1779 if(declaration->storage_class == STORAGE_CLASS_ENUM_ENTRY)
1791 panic("invalid expression found (is constant expression)");
1795 * Initialize the AST construction.
1799 obstack_init(&ast_obstack);
1807 obstack_free(&ast_obstack, NULL);
1811 * Set the output stream for the AST printer.
1813 * @param stream the output stream
1815 void ast_set_output(FILE *stream)
1818 type_set_output(stream);
1822 * Allocate an AST object of the given size.
1824 * @param size the size of the object to allocate
1826 * @return A new allocated object in the AST memeory space.
1828 void *(allocate_ast)(size_t size)
1830 return _allocate_ast(size);