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"
33 #ifdef __INTEL_COMPILER
39 #include "adt/error.h"
41 struct obstack ast_obstack;
46 /** If set, implicit casts are printed. */
47 bool print_implicit_casts = false;
49 /** If set parenthesis are printed to indicate operator precedence. */
50 bool print_parenthesis = false;
52 static void print_statement(const statement_t *statement);
53 static void print_expression_prec(const expression_t *expression, unsigned prec);
55 void change_indent(int delta)
61 void print_indent(void)
63 for (int i = 0; i < indent; ++i)
70 PREC_COMMA = 4, /* , left to right */
71 PREC_ASSIGN = 6, /* = += -= *= /= %= <<= >>= &= ^= |= right to left */
72 PREC_COND = 8, /* ?: right to left */
73 PREC_LOG_OR = 10, /* || left to right */
74 PREC_LOG_AND = 12, /* && left to right */
75 PREC_BIT_OR = 14, /* | left to right */
76 PREC_BIT_XOR = 16, /* ^ left to right */
77 PREC_BIT_AND = 18, /* & left to right */
78 PREC_EQ = 20, /* == != left to right */
79 PREC_CMP = 22, /* < <= > >= left to right */
80 PREC_SHF = 24, /* << >> left to right */
81 PREC_PLUS = 26, /* + - left to right */
82 PREC_MUL = 28, /* * / % left to right */
83 PREC_UNARY = 30, /* ! ~ ++ -- + - (type) * & sizeof right to left */
84 PREC_ACCESS = 32, /* () [] -> . left to right */
85 PREC_PRIM = 34, /* primary */
90 * Returns 1 if a given precedence level has right-to-left
91 * associativity, else -1.
93 * @param precedence the operator precedence
95 static int right_to_left(unsigned precedence) {
96 return (precedence == PREC_ASSIGN || precedence == PREC_COND ||
97 precedence == PREC_UNARY) ? 1 : -1;
101 * Return the precedence of an expression given by its kind.
103 * @param kind the expression kind
105 static unsigned get_expression_precedence(expression_kind_t kind)
107 static const unsigned prec[] = {
108 [EXPR_UNKNOWN] = PREC_PRIM,
109 [EXPR_INVALID] = PREC_PRIM,
110 [EXPR_REFERENCE] = PREC_PRIM,
111 [EXPR_CHARACTER_CONSTANT] = PREC_PRIM,
112 [EXPR_WIDE_CHARACTER_CONSTANT] = PREC_PRIM,
113 [EXPR_CONST] = PREC_PRIM,
114 [EXPR_STRING_LITERAL] = PREC_PRIM,
115 [EXPR_WIDE_STRING_LITERAL] = PREC_PRIM,
116 [EXPR_COMPOUND_LITERAL] = PREC_UNARY,
117 [EXPR_CALL] = PREC_ACCESS,
118 [EXPR_CONDITIONAL] = PREC_COND,
119 [EXPR_SELECT] = PREC_ACCESS,
120 [EXPR_ARRAY_ACCESS] = PREC_ACCESS,
121 [EXPR_SIZEOF] = PREC_UNARY,
122 [EXPR_CLASSIFY_TYPE] = PREC_UNARY,
123 [EXPR_ALIGNOF] = PREC_UNARY,
125 [EXPR_FUNCNAME] = PREC_PRIM,
126 [EXPR_BUILTIN_SYMBOL] = PREC_PRIM,
127 [EXPR_BUILTIN_CONSTANT_P] = PREC_PRIM,
128 [EXPR_BUILTIN_PREFETCH] = PREC_PRIM,
129 [EXPR_OFFSETOF] = PREC_PRIM,
130 [EXPR_VA_START] = PREC_PRIM,
131 [EXPR_VA_ARG] = PREC_PRIM,
132 [EXPR_STATEMENT] = PREC_ACCESS,
133 [EXPR_LABEL_ADDRESS] = PREC_PRIM,
135 [EXPR_UNARY_NEGATE] = PREC_UNARY,
136 [EXPR_UNARY_PLUS] = PREC_UNARY,
137 [EXPR_UNARY_BITWISE_NEGATE] = PREC_UNARY,
138 [EXPR_UNARY_NOT] = PREC_UNARY,
139 [EXPR_UNARY_DEREFERENCE] = PREC_UNARY,
140 [EXPR_UNARY_TAKE_ADDRESS] = PREC_UNARY,
141 [EXPR_UNARY_POSTFIX_INCREMENT] = PREC_UNARY,
142 [EXPR_UNARY_POSTFIX_DECREMENT] = PREC_UNARY,
143 [EXPR_UNARY_PREFIX_INCREMENT] = PREC_UNARY,
144 [EXPR_UNARY_PREFIX_DECREMENT] = PREC_UNARY,
145 [EXPR_UNARY_CAST] = PREC_UNARY,
146 [EXPR_UNARY_CAST_IMPLICIT] = PREC_UNARY,
147 [EXPR_UNARY_ASSUME] = PREC_PRIM,
149 [EXPR_BINARY_ADD] = PREC_PLUS,
150 [EXPR_BINARY_SUB] = PREC_PLUS,
151 [EXPR_BINARY_MUL] = PREC_MUL,
152 [EXPR_BINARY_DIV] = PREC_MUL,
153 [EXPR_BINARY_MOD] = PREC_MUL,
154 [EXPR_BINARY_EQUAL] = PREC_EQ,
155 [EXPR_BINARY_NOTEQUAL] = PREC_EQ,
156 [EXPR_BINARY_LESS] = PREC_CMP,
157 [EXPR_BINARY_LESSEQUAL] = PREC_CMP,
158 [EXPR_BINARY_GREATER] = PREC_CMP,
159 [EXPR_BINARY_GREATEREQUAL] = PREC_CMP,
160 [EXPR_BINARY_BITWISE_AND] = PREC_BIT_AND,
161 [EXPR_BINARY_BITWISE_OR] = PREC_BIT_OR,
162 [EXPR_BINARY_BITWISE_XOR] = PREC_BIT_XOR,
163 [EXPR_BINARY_LOGICAL_AND] = PREC_LOG_AND,
164 [EXPR_BINARY_LOGICAL_OR] = PREC_LOG_OR,
165 [EXPR_BINARY_SHIFTLEFT] = PREC_SHF,
166 [EXPR_BINARY_SHIFTRIGHT] = PREC_SHF,
167 [EXPR_BINARY_ASSIGN] = PREC_ASSIGN,
168 [EXPR_BINARY_MUL_ASSIGN] = PREC_ASSIGN,
169 [EXPR_BINARY_DIV_ASSIGN] = PREC_ASSIGN,
170 [EXPR_BINARY_MOD_ASSIGN] = PREC_ASSIGN,
171 [EXPR_BINARY_ADD_ASSIGN] = PREC_ASSIGN,
172 [EXPR_BINARY_SUB_ASSIGN] = PREC_ASSIGN,
173 [EXPR_BINARY_SHIFTLEFT_ASSIGN] = PREC_ASSIGN,
174 [EXPR_BINARY_SHIFTRIGHT_ASSIGN] = PREC_ASSIGN,
175 [EXPR_BINARY_BITWISE_AND_ASSIGN] = PREC_ASSIGN,
176 [EXPR_BINARY_BITWISE_XOR_ASSIGN] = PREC_ASSIGN,
177 [EXPR_BINARY_BITWISE_OR_ASSIGN] = PREC_ASSIGN,
178 [EXPR_BINARY_COMMA] = PREC_COMMA,
180 [EXPR_BINARY_BUILTIN_EXPECT] = PREC_PRIM,
181 [EXPR_BINARY_ISGREATER] = PREC_PRIM,
182 [EXPR_BINARY_ISGREATEREQUAL] = PREC_PRIM,
183 [EXPR_BINARY_ISLESS] = PREC_PRIM,
184 [EXPR_BINARY_ISLESSEQUAL] = PREC_PRIM,
185 [EXPR_BINARY_ISLESSGREATER] = PREC_PRIM,
186 [EXPR_BINARY_ISUNORDERED] = PREC_PRIM
188 assert((unsigned)kind < (sizeof(prec)/sizeof(prec[0])));
189 unsigned res = prec[kind];
191 assert(res != PREC_BOTTOM);
196 * Print a constant expression.
198 * @param cnst the constant expression
200 static void print_const(const const_expression_t *cnst)
202 if(cnst->base.type == NULL)
205 const type_t *const type = skip_typeref(cnst->base.type);
207 if (is_type_integer(type)) {
208 fprintf(out, "%lld", cnst->v.int_value);
209 } else if (is_type_float(type)) {
210 long double const val = cnst->v.float_value;
211 fprintf(out, "%.20Lg", val);
212 if (isfinite(val) && truncl(val) == val)
215 panic("unknown constant");
219 switch (type->atomic.akind) {
220 case ATOMIC_TYPE_UINT: suffix = "U"; break;
221 case ATOMIC_TYPE_LONG: suffix = "L"; break;
222 case ATOMIC_TYPE_ULONG: suffix = "UL"; break;
223 case ATOMIC_TYPE_LONGLONG: suffix = "LL"; break;
224 case ATOMIC_TYPE_ULONGLONG: suffix = "ULL"; break;
225 case ATOMIC_TYPE_FLOAT: suffix = "F"; break;
226 case ATOMIC_TYPE_LONG_DOUBLE: suffix = "L"; break;
228 default: suffix = NULL; break;
235 * Print a quoted string constant.
237 * @param string the string constant
238 * @param border the border char
239 * @param skip number of chars to skip at the end
241 static void print_quoted_string(const string_t *const string, char border, int skip)
244 const char *end = string->begin + string->size - skip;
245 for (const char *c = string->begin; c != end; ++c) {
250 case '\\': fputs("\\\\", out); break;
251 case '\a': fputs("\\a", out); break;
252 case '\b': fputs("\\b", out); break;
253 case '\f': fputs("\\f", out); break;
254 case '\n': fputs("\\n", out); break;
255 case '\r': fputs("\\r", out); break;
256 case '\t': fputs("\\t", out); break;
257 case '\v': fputs("\\v", out); break;
258 case '\?': fputs("\\?", out); break;
260 if (c_mode & _GNUC) {
261 fputs("\\e", out); break;
266 fprintf(out, "\\%03o", *c);
277 * Prints a wide string literal expression.
279 * @param wstr the wide string literal expression
280 * @param border the border char
281 * @param skip number of chars to skip at the end
283 static void print_quoted_wide_string(const wide_string_t *const wstr,
284 char border, int skip)
288 const wchar_rep_t *end = wstr->begin + wstr->size - skip;
289 for (const wchar_rep_t *c = wstr->begin; c != end; ++c) {
291 case L'\"': fputs("\\\"", out); break;
292 case L'\\': fputs("\\\\", out); break;
293 case L'\a': fputs("\\a", out); break;
294 case L'\b': fputs("\\b", out); break;
295 case L'\f': fputs("\\f", out); break;
296 case L'\n': fputs("\\n", out); break;
297 case L'\r': fputs("\\r", out); break;
298 case L'\t': fputs("\\t", out); break;
299 case L'\v': fputs("\\v", out); break;
300 case L'\?': fputs("\\?", out); break;
302 if (c_mode & _GNUC) {
303 fputs("\\e", out); break;
307 const unsigned tc = *c;
310 fprintf(out, "\\%03o", (char)*c);
314 } else if (tc < 0x800) {
315 fputc(0xC0 | (tc >> 6), out);
316 fputc(0x80 | (tc & 0x3F), out);
317 } else if (tc < 0x10000) {
318 fputc(0xE0 | ( tc >> 12), out);
319 fputc(0x80 | ((tc >> 6) & 0x3F), out);
320 fputc(0x80 | ( tc & 0x3F), out);
322 fputc(0xF0 | ( tc >> 18), out);
323 fputc(0x80 | ((tc >> 12) & 0x3F), out);
324 fputc(0x80 | ((tc >> 6) & 0x3F), out);
325 fputc(0x80 | ( tc & 0x3F), out);
334 * Print a constant character expression.
336 * @param cnst the constant character expression
338 static void print_character_constant(const const_expression_t *cnst)
340 print_quoted_string(&cnst->v.character, '\'', 0);
343 static void print_wide_character_constant(const const_expression_t *cnst)
345 print_quoted_wide_string(&cnst->v.wide_character, '\'', 0);
349 * Prints a string literal expression.
351 * @param string_literal the string literal expression
353 static void print_string_literal(
354 const string_literal_expression_t *string_literal)
356 print_quoted_string(&string_literal->value, '"', 1);
360 * Prints a predefined symbol.
362 static void print_funcname(
363 const funcname_expression_t *funcname)
366 switch(funcname->kind) {
367 case FUNCNAME_FUNCTION: s = (c_mode & _C99) ? "__func__" : "__FUNCTION__"; break;
368 case FUNCNAME_PRETTY_FUNCTION: s = "__PRETTY_FUNCTION__"; break;
369 case FUNCNAME_FUNCSIG: s = "__FUNCSIG__"; break;
370 case FUNCNAME_FUNCDNAME: s = "__FUNCDNAME__"; break;
377 static void print_wide_string_literal(
378 const wide_string_literal_expression_t *const wstr)
380 print_quoted_wide_string(&wstr->value, '"', 1);
383 static void print_compound_literal(
384 const compound_literal_expression_t *expression)
387 print_type(expression->type);
389 print_initializer(expression->initializer);
393 * Prints a call expression.
395 * @param call the call expression
397 static void print_call_expression(const call_expression_t *call)
399 unsigned prec = get_expression_precedence(call->base.kind);
400 print_expression_prec(call->function, prec);
402 call_argument_t *argument = call->arguments;
404 while(argument != NULL) {
410 print_expression_prec(argument->expression, PREC_COMMA + 1);
412 argument = argument->next;
418 * Prints a binary expression.
420 * @param binexpr the binary expression
422 static void print_binary_expression(const binary_expression_t *binexpr)
424 unsigned prec = get_expression_precedence(binexpr->base.kind);
425 int r2l = right_to_left(prec);
427 if(binexpr->base.kind == EXPR_BINARY_BUILTIN_EXPECT) {
428 fputs("__builtin_expect(", out);
429 print_expression_prec(binexpr->left, prec);
431 print_expression_prec(binexpr->right, prec);
436 print_expression_prec(binexpr->left, prec + r2l);
437 if (binexpr->base.kind != EXPR_BINARY_COMMA) {
440 switch (binexpr->base.kind) {
441 case EXPR_BINARY_COMMA: fputs(",", out); break;
442 case EXPR_BINARY_ASSIGN: fputs("=", out); break;
443 case EXPR_BINARY_ADD: fputs("+", out); break;
444 case EXPR_BINARY_SUB: fputs("-", out); break;
445 case EXPR_BINARY_MUL: fputs("*", out); break;
446 case EXPR_BINARY_MOD: fputs("%", out); break;
447 case EXPR_BINARY_DIV: fputs("/", out); break;
448 case EXPR_BINARY_BITWISE_OR: fputs("|", out); break;
449 case EXPR_BINARY_BITWISE_AND: fputs("&", out); break;
450 case EXPR_BINARY_BITWISE_XOR: fputs("^", out); break;
451 case EXPR_BINARY_LOGICAL_OR: fputs("||", out); break;
452 case EXPR_BINARY_LOGICAL_AND: fputs("&&", out); break;
453 case EXPR_BINARY_NOTEQUAL: fputs("!=", out); break;
454 case EXPR_BINARY_EQUAL: fputs("==", out); break;
455 case EXPR_BINARY_LESS: fputs("<", out); break;
456 case EXPR_BINARY_LESSEQUAL: fputs("<=", out); break;
457 case EXPR_BINARY_GREATER: fputs(">", out); break;
458 case EXPR_BINARY_GREATEREQUAL: fputs(">=", out); break;
459 case EXPR_BINARY_SHIFTLEFT: fputs("<<", out); break;
460 case EXPR_BINARY_SHIFTRIGHT: fputs(">>", out); break;
462 case EXPR_BINARY_ADD_ASSIGN: fputs("+=", out); break;
463 case EXPR_BINARY_SUB_ASSIGN: fputs("-=", out); break;
464 case EXPR_BINARY_MUL_ASSIGN: fputs("*=", out); break;
465 case EXPR_BINARY_MOD_ASSIGN: fputs("%=", out); break;
466 case EXPR_BINARY_DIV_ASSIGN: fputs("/=", out); break;
467 case EXPR_BINARY_BITWISE_OR_ASSIGN: fputs("|=", out); break;
468 case EXPR_BINARY_BITWISE_AND_ASSIGN: fputs("&=", out); break;
469 case EXPR_BINARY_BITWISE_XOR_ASSIGN: fputs("^=", out); break;
470 case EXPR_BINARY_SHIFTLEFT_ASSIGN: fputs("<<=", out); break;
471 case EXPR_BINARY_SHIFTRIGHT_ASSIGN: fputs(">>=", out); break;
472 default: panic("invalid binexpression found");
475 print_expression_prec(binexpr->right, prec - r2l);
479 * Prints an unary expression.
481 * @param unexpr the unary expression
483 static void print_unary_expression(const unary_expression_t *unexpr)
485 unsigned prec = get_expression_precedence(unexpr->base.kind);
486 switch(unexpr->base.kind) {
487 case EXPR_UNARY_NEGATE: fputc('-', out); break;
488 case EXPR_UNARY_PLUS: fputc('+', out); break;
489 case EXPR_UNARY_NOT: fputc('!', out); break;
490 case EXPR_UNARY_BITWISE_NEGATE: fputc('~', out); break;
491 case EXPR_UNARY_PREFIX_INCREMENT: fputs("++", out); break;
492 case EXPR_UNARY_PREFIX_DECREMENT: fputs("--", out); break;
493 case EXPR_UNARY_DEREFERENCE: fputc('*', out); break;
494 case EXPR_UNARY_TAKE_ADDRESS: fputc('&', out); break;
496 case EXPR_UNARY_POSTFIX_INCREMENT:
497 print_expression_prec(unexpr->value, prec);
500 case EXPR_UNARY_POSTFIX_DECREMENT:
501 print_expression_prec(unexpr->value, prec);
504 case EXPR_UNARY_CAST_IMPLICIT:
505 case EXPR_UNARY_CAST:
507 print_type(unexpr->base.type);
510 case EXPR_UNARY_ASSUME:
511 fputs("__assume(", out);
512 print_expression_prec(unexpr->value, PREC_COMMA + 1);
516 panic("invalid unary expression found");
518 print_expression_prec(unexpr->value, prec);
522 * Prints a reference expression.
524 * @param ref the reference expression
526 static void print_reference_expression(const reference_expression_t *ref)
528 fputs(ref->declaration->symbol->string, out);
532 * Prints a label address expression.
534 * @param ref the reference expression
536 static void print_label_address_expression(const label_address_expression_t *le)
538 fprintf(out, "&&%s", le->declaration->symbol->string);
542 * Prints an array expression.
544 * @param expression the array expression
546 static void print_array_expression(const array_access_expression_t *expression)
548 unsigned prec = get_expression_precedence(expression->base.kind);
549 if(!expression->flipped) {
550 print_expression_prec(expression->array_ref, prec);
552 print_expression_prec(expression->index, PREC_BOTTOM);
555 print_expression_prec(expression->index, prec);
557 print_expression_prec(expression->array_ref, PREC_BOTTOM);
563 * Prints a typeproperty expression (sizeof or __alignof__).
565 * @param expression the type property expression
567 static void print_typeprop_expression(const typeprop_expression_t *expression)
569 if (expression->base.kind == EXPR_SIZEOF) {
570 fputs("sizeof", out);
572 assert(expression->base.kind == EXPR_ALIGNOF);
573 fputs("__alignof__", out);
575 if(expression->tp_expression != NULL) {
576 /* always print the '()' here, sizeof x is right but unusual */
578 print_expression_prec(expression->tp_expression, PREC_ACCESS);
582 print_type(expression->type);
588 * Prints an builtin symbol.
590 * @param expression the builtin symbol expression
592 static void print_builtin_symbol(const builtin_symbol_expression_t *expression)
594 fputs(expression->symbol->string, out);
598 * Prints a builtin constant expression.
600 * @param expression the builtin constant expression
602 static void print_builtin_constant(const builtin_constant_expression_t *expression)
604 fputs("__builtin_constant_p(", out);
605 print_expression_prec(expression->value, PREC_COMMA + 1);
610 * Prints a builtin prefetch expression.
612 * @param expression the builtin prefetch expression
614 static void print_builtin_prefetch(const builtin_prefetch_expression_t *expression)
616 fputs("__builtin_prefetch(", out);
617 print_expression_prec(expression->adr, PREC_COMMA + 1);
618 if (expression->rw) {
620 print_expression_prec(expression->rw, PREC_COMMA + 1);
622 if (expression->locality) {
624 print_expression_prec(expression->locality, PREC_COMMA + 1);
630 * Prints a conditional expression.
632 * @param expression the conditional expression
634 static void print_conditional(const conditional_expression_t *expression)
636 print_expression_prec(expression->condition, PREC_LOG_OR);
638 if (expression->true_expression != NULL) {
639 print_expression_prec(expression->true_expression, PREC_EXPR);
644 print_expression_prec(expression->false_expression, PREC_COND);
648 * Prints a va_start expression.
650 * @param expression the va_start expression
652 static void print_va_start(const va_start_expression_t *const expression)
654 fputs("__builtin_va_start(", out);
655 print_expression_prec(expression->ap, PREC_COMMA + 1);
657 fputs(expression->parameter->symbol->string, out);
662 * Prints a va_arg expression.
664 * @param expression the va_arg expression
666 static void print_va_arg(const va_arg_expression_t *expression)
668 fputs("__builtin_va_arg(", out);
669 print_expression_prec(expression->ap, PREC_COMMA + 1);
671 print_type(expression->base.type);
676 * Prints a select expression (. or ->).
678 * @param expression the select expression
680 static void print_select(const select_expression_t *expression)
682 unsigned prec = get_expression_precedence(expression->base.kind);
683 print_expression_prec(expression->compound, prec);
684 if(is_type_pointer(skip_typeref(expression->compound->base.type))) {
689 fputs(expression->compound_entry->symbol->string, out);
693 * Prints a type classify expression.
695 * @param expr the type classify expression
697 static void print_classify_type_expression(
698 const classify_type_expression_t *const expr)
700 fputs("__builtin_classify_type(", out);
701 print_expression_prec(expr->type_expression, PREC_COMMA + 1);
706 * Prints a designator.
708 * @param designator the designator
710 static void print_designator(const designator_t *designator)
712 for ( ; designator != NULL; designator = designator->next) {
713 if (designator->symbol == NULL) {
715 print_expression_prec(designator->array_index, PREC_BOTTOM);
719 fputs(designator->symbol->string, out);
725 * Prints an offsetof expression.
727 * @param expression the offset expression
729 static void print_offsetof_expression(const offsetof_expression_t *expression)
731 fputs("__builtin_offsetof", out);
733 print_type(expression->type);
735 print_designator(expression->designator);
740 * Prints a statement expression.
742 * @param expression the statement expression
744 static void print_statement_expression(const statement_expression_t *expression)
747 print_statement(expression->statement);
752 * Prints an expression with parenthesis if needed.
754 * @param expression the expression to print
755 * @param top_prec the precedence of the user of this expression.
757 static void print_expression_prec(const expression_t *expression, unsigned top_prec)
759 if (expression->kind == EXPR_UNARY_CAST_IMPLICIT && !print_implicit_casts) {
760 expression = expression->unary.value;
762 unsigned prec = get_expression_precedence(expression->base.kind);
763 if (print_parenthesis && top_prec != PREC_BOTTOM)
767 switch(expression->kind) {
770 fputs("$invalid expression$", out);
772 case EXPR_CHARACTER_CONSTANT:
773 print_character_constant(&expression->conste);
775 case EXPR_WIDE_CHARACTER_CONSTANT:
776 print_wide_character_constant(&expression->conste);
779 print_const(&expression->conste);
782 print_funcname(&expression->funcname);
784 case EXPR_STRING_LITERAL:
785 print_string_literal(&expression->string);
787 case EXPR_WIDE_STRING_LITERAL:
788 print_wide_string_literal(&expression->wide_string);
790 case EXPR_COMPOUND_LITERAL:
791 print_compound_literal(&expression->compound_literal);
794 print_call_expression(&expression->call);
797 print_binary_expression(&expression->binary);
800 print_reference_expression(&expression->reference);
802 case EXPR_ARRAY_ACCESS:
803 print_array_expression(&expression->array_access);
805 case EXPR_LABEL_ADDRESS:
806 print_label_address_expression(&expression->label_address);
809 print_unary_expression(&expression->unary);
813 print_typeprop_expression(&expression->typeprop);
815 case EXPR_BUILTIN_SYMBOL:
816 print_builtin_symbol(&expression->builtin_symbol);
818 case EXPR_BUILTIN_CONSTANT_P:
819 print_builtin_constant(&expression->builtin_constant);
821 case EXPR_BUILTIN_PREFETCH:
822 print_builtin_prefetch(&expression->builtin_prefetch);
824 case EXPR_CONDITIONAL:
825 print_conditional(&expression->conditional);
828 print_va_start(&expression->va_starte);
831 print_va_arg(&expression->va_arge);
834 print_select(&expression->select);
836 case EXPR_CLASSIFY_TYPE:
837 print_classify_type_expression(&expression->classify_type);
840 print_offsetof_expression(&expression->offsetofe);
843 print_statement_expression(&expression->statement);
848 fprintf(out, "some expression of type %d", (int) expression->kind);
856 * Print an compound statement.
858 * @param block the compound statement
860 static void print_compound_statement(const compound_statement_t *block)
865 statement_t *statement = block->statements;
866 while(statement != NULL) {
867 if (statement->base.kind == STATEMENT_CASE_LABEL)
870 print_statement(statement);
872 statement = statement->base.next;
880 * Print a return statement.
882 * @param statement the return statement
884 static void print_return_statement(const return_statement_t *statement)
886 fputs("return ", out);
887 if(statement->value != NULL)
888 print_expression(statement->value);
893 * Print an expression statement.
895 * @param statement the expression statement
897 static void print_expression_statement(const expression_statement_t *statement)
899 print_expression(statement->expression);
904 * Print a goto statement.
906 * @param statement the goto statement
908 static void print_goto_statement(const goto_statement_t *statement)
911 if (statement->expression != NULL) {
913 print_expression(statement->expression);
915 fputs(statement->label->symbol->string, out);
921 * Print a label statement.
923 * @param statement the label statement
925 static void print_label_statement(const label_statement_t *statement)
927 fprintf(out, "%s:\n", statement->label->symbol->string);
928 print_statement(statement->statement);
932 * Print an if statement.
934 * @param statement the if statement
936 static void print_if_statement(const if_statement_t *statement)
939 print_expression(statement->condition);
941 print_statement(statement->true_statement);
943 if(statement->false_statement != NULL) {
946 print_statement(statement->false_statement);
951 * Print a switch statement.
953 * @param statement the switch statement
955 static void print_switch_statement(const switch_statement_t *statement)
957 fputs("switch (", out);
958 print_expression(statement->expression);
960 print_statement(statement->body);
964 * Print a case label (including the default label).
966 * @param statement the case label statement
968 static void print_case_label(const case_label_statement_t *statement)
970 if(statement->expression == NULL) {
971 fputs("default:\n", out);
974 print_expression(statement->expression);
975 if (statement->end_range != NULL) {
977 print_expression(statement->end_range);
982 if(statement->statement != NULL) {
983 if (statement->statement->base.kind == STATEMENT_CASE_LABEL) {
987 print_statement(statement->statement);
992 * Print a declaration statement.
994 * @param statement the statement
996 static void print_declaration_statement(
997 const declaration_statement_t *statement)
1000 for (declaration_t *declaration = statement->declarations_begin;
1001 declaration != statement->declarations_end->next;
1002 declaration = declaration->next) {
1003 if (declaration->storage_class == STORAGE_CLASS_ENUM_ENTRY)
1005 if (declaration->implicit)
1013 print_declaration(declaration);
1019 * Print a while statement.
1021 * @param statement the statement
1023 static void print_while_statement(const while_statement_t *statement)
1025 fputs("while (", out);
1026 print_expression(statement->condition);
1028 print_statement(statement->body);
1032 * Print a do-while statement.
1034 * @param statement the statement
1036 static void print_do_while_statement(const do_while_statement_t *statement)
1039 print_statement(statement->body);
1041 fputs("while (", out);
1042 print_expression(statement->condition);
1047 * Print a for statement.
1049 * @param statement the statement
1051 static void print_for_statement(const for_statement_t *statement)
1053 fputs("for (", out);
1054 declaration_t *decl = statement->scope.declarations;
1055 while (decl != NULL && decl->implicit)
1058 assert(statement->initialisation == NULL);
1059 print_declaration(decl);
1060 if (decl->next != NULL) {
1061 panic("multiple declarations in for statement not supported yet");
1065 if(statement->initialisation) {
1066 print_expression(statement->initialisation);
1070 if(statement->condition != NULL) {
1071 print_expression(statement->condition);
1074 if(statement->step != NULL) {
1075 print_expression(statement->step);
1078 print_statement(statement->body);
1082 * Print assembler arguments.
1084 * @param arguments the arguments
1086 static void print_asm_arguments(asm_argument_t *arguments)
1088 asm_argument_t *argument = arguments;
1089 for( ; argument != NULL; argument = argument->next) {
1090 if(argument != arguments)
1093 if(argument->symbol) {
1094 fprintf(out, "[%s] ", argument->symbol->string);
1096 print_quoted_string(&argument->constraints, '"', 1);
1098 print_expression(argument->expression);
1104 * Print assembler clobbers.
1106 * @param clobbers the clobbers
1108 static void print_asm_clobbers(asm_clobber_t *clobbers)
1110 asm_clobber_t *clobber = clobbers;
1111 for( ; clobber != NULL; clobber = clobber->next) {
1112 if(clobber != clobbers)
1115 print_quoted_string(&clobber->clobber, '"', 1);
1120 * Print an assembler statement.
1122 * @param statement the statement
1124 static void print_asm_statement(const asm_statement_t *statement)
1127 if(statement->is_volatile) {
1128 fputs("volatile ", out);
1131 print_quoted_string(&statement->asm_text, '"', 1);
1132 if(statement->inputs == NULL && statement->outputs == NULL
1133 && statement->clobbers == NULL)
1134 goto end_of_print_asm_statement;
1137 print_asm_arguments(statement->inputs);
1138 if(statement->outputs == NULL && statement->clobbers == NULL)
1139 goto end_of_print_asm_statement;
1142 print_asm_arguments(statement->outputs);
1143 if(statement->clobbers == NULL)
1144 goto end_of_print_asm_statement;
1147 print_asm_clobbers(statement->clobbers);
1149 end_of_print_asm_statement:
1154 * Print a microsoft __try statement.
1156 * @param statement the statement
1158 static void print_ms_try_statement(const ms_try_statement_t *statement)
1160 fputs("__try ", out);
1161 print_statement(statement->try_statement);
1163 if(statement->except_expression != NULL) {
1164 fputs("__except(", out);
1165 print_expression(statement->except_expression);
1168 fputs("__finally ", out);
1170 print_statement(statement->final_statement);
1174 * Print a microsoft __leave statement.
1176 * @param statement the statement
1178 static void print_leave_statement(const leave_statement_t *statement)
1181 fputs("__leave;\n", out);
1185 * Print a statement.
1187 * @param statement the statement
1189 void print_statement(const statement_t *statement)
1191 switch(statement->kind) {
1192 case STATEMENT_EMPTY:
1195 case STATEMENT_COMPOUND:
1196 print_compound_statement(&statement->compound);
1198 case STATEMENT_RETURN:
1199 print_return_statement(&statement->returns);
1201 case STATEMENT_EXPRESSION:
1202 print_expression_statement(&statement->expression);
1204 case STATEMENT_LABEL:
1205 print_label_statement(&statement->label);
1207 case STATEMENT_GOTO:
1208 print_goto_statement(&statement->gotos);
1210 case STATEMENT_CONTINUE:
1211 fputs("continue;\n", out);
1213 case STATEMENT_BREAK:
1214 fputs("break;\n", out);
1217 print_if_statement(&statement->ifs);
1219 case STATEMENT_SWITCH:
1220 print_switch_statement(&statement->switchs);
1222 case STATEMENT_CASE_LABEL:
1223 print_case_label(&statement->case_label);
1225 case STATEMENT_DECLARATION:
1226 print_declaration_statement(&statement->declaration);
1228 case STATEMENT_WHILE:
1229 print_while_statement(&statement->whiles);
1231 case STATEMENT_DO_WHILE:
1232 print_do_while_statement(&statement->do_while);
1235 print_for_statement(&statement->fors);
1238 print_asm_statement(&statement->asms);
1240 case STATEMENT_MS_TRY:
1241 print_ms_try_statement(&statement->ms_try);
1243 case STATEMENT_LEAVE:
1244 print_leave_statement(&statement->leave);
1246 case STATEMENT_INVALID:
1247 fputs("$invalid statement$", out);
1253 * Print a storage class.
1255 * @param storage_class the storage class
1257 static void print_storage_class(storage_class_tag_t storage_class)
1259 switch(storage_class) {
1260 case STORAGE_CLASS_ENUM_ENTRY:
1261 case STORAGE_CLASS_NONE:
1263 case STORAGE_CLASS_TYPEDEF: fputs("typedef ", out); break;
1264 case STORAGE_CLASS_EXTERN: fputs("extern ", out); break;
1265 case STORAGE_CLASS_STATIC: fputs("static ", out); break;
1266 case STORAGE_CLASS_AUTO: fputs("auto ", out); break;
1267 case STORAGE_CLASS_REGISTER: fputs("register ", out); break;
1268 case STORAGE_CLASS_THREAD: fputs("__thread", out); break;
1269 case STORAGE_CLASS_THREAD_EXTERN: fputs("extern __thread", out); break;
1270 case STORAGE_CLASS_THREAD_STATIC: fputs("static __thread", out); break;
1275 * Print an initializer.
1277 * @param initializer the initializer
1279 void print_initializer(const initializer_t *initializer)
1281 if(initializer == NULL) {
1286 switch(initializer->kind) {
1287 case INITIALIZER_VALUE: {
1288 const initializer_value_t *value = &initializer->value;
1289 print_expression(value->value);
1292 case INITIALIZER_LIST: {
1293 assert(initializer->kind == INITIALIZER_LIST);
1295 const initializer_list_t *list = &initializer->list;
1297 for(size_t i = 0 ; i < list->len; ++i) {
1298 const initializer_t *sub_init = list->initializers[i];
1299 print_initializer(list->initializers[i]);
1300 if(i < list->len-1) {
1301 if(sub_init == NULL || sub_init->kind != INITIALIZER_DESIGNATOR)
1308 case INITIALIZER_STRING:
1309 print_quoted_string(&initializer->string.string, '"', 1);
1311 case INITIALIZER_WIDE_STRING:
1312 print_quoted_wide_string(&initializer->wide_string.string, '"', 1);
1314 case INITIALIZER_DESIGNATOR:
1315 print_designator(initializer->designator.designator);
1320 panic("invalid initializer kind found");
1324 * Print microsoft extended declaration modifiers.
1326 static void print_ms_modifiers(const declaration_t *declaration) {
1327 if((c_mode & _MS) == 0)
1330 decl_modifiers_t modifiers = declaration->modifiers;
1332 /* DM_FORCEINLINE handled outside. */
1333 if ((modifiers & ~DM_FORCEINLINE) != 0 ||
1334 declaration->alignment != 0 ||
1335 declaration->get_property_sym != NULL ||
1336 declaration->put_property_sym != NULL) {
1339 fputs("__declspec", out);
1340 if(modifiers & DM_DLLIMPORT) {
1341 fputs(next, out); next = ", "; fputs("dllimport", out);
1343 if(modifiers & DM_DLLEXPORT) {
1344 fputs(next, out); next = ", "; fputs("dllexport", out);
1346 if(modifiers & DM_THREAD) {
1347 fputs(next, out); next = ", "; fputs("thread", out);
1349 if(modifiers & DM_NAKED) {
1350 fputs(next, out); next = ", "; fputs("naked", out);
1352 if(modifiers & DM_THREAD) {
1353 fputs(next, out); next = ", "; fputs("thread", out);
1355 if(modifiers & DM_SELECTANY) {
1356 fputs(next, out); next = ", "; fputs("selectany", out);
1358 if(modifiers & DM_NOTHROW) {
1359 fputs(next, out); next = ", "; fputs("nothrow", out);
1361 if(modifiers & DM_NORETURN) {
1362 fputs(next, out); next = ", "; fputs("noreturn", out);
1364 if(modifiers & DM_NOINLINE) {
1365 fputs(next, out); next = ", "; fputs("noinline", out);
1367 if (modifiers & DM_DEPRECATED) {
1368 fputs(next, out); next = ", "; fputs("deprecated", out);
1369 if(declaration->deprecated_string != NULL)
1370 fprintf(out, "(\"%s\")", declaration->deprecated_string);
1372 if(declaration->alignment != 0) {
1373 fputs(next, out); next = ", "; fprintf(out, "align(%u)", declaration->alignment);
1375 if(modifiers & DM_RESTRICT) {
1376 fputs(next, out); next = ", "; fputs("restrict", out);
1378 if(modifiers & DM_NOALIAS) {
1379 fputs(next, out); next = ", "; fputs("noalias", out);
1381 if(declaration->get_property_sym != NULL || declaration->put_property_sym != NULL) {
1383 fputs(next, out); next = ", "; fputs("property(", out);
1384 if(declaration->get_property_sym != NULL) {
1385 fprintf(out, "get=%s", declaration->get_property_sym->string);
1388 if(declaration->put_property_sym != NULL)
1389 fprintf(out, "%sput=%s", comma, declaration->put_property_sym->string);
1397 * Print a declaration in the NORMAL namespace.
1399 * @param declaration the declaration
1401 static void print_normal_declaration(const declaration_t *declaration)
1403 print_storage_class((storage_class_tag_t) declaration->declared_storage_class);
1404 if (declaration->is_inline) {
1405 if (declaration->modifiers & DM_FORCEINLINE) {
1406 fputs("__forceinline ", out);
1407 } else if (declaration->modifiers & DM_MICROSOFT_INLINE) {
1408 fputs("__inline ", out);
1410 fputs("inline ", out);
1413 print_ms_modifiers(declaration);
1414 print_type_ext(declaration->type, declaration->symbol,
1415 &declaration->scope);
1417 if(declaration->type->kind == TYPE_FUNCTION) {
1418 if(declaration->init.statement != NULL) {
1420 print_statement(declaration->init.statement);
1423 } else if(declaration->init.initializer != NULL) {
1425 print_initializer(declaration->init.initializer);
1431 * Prints an expression.
1433 * @param expression the expression
1435 void print_expression(const expression_t *expression) {
1436 print_expression_prec(expression, PREC_BOTTOM);
1440 * Print a declaration.
1442 * @param declaration the declaration
1444 void print_declaration(const declaration_t *declaration)
1446 if(declaration->namespc != NAMESPACE_NORMAL &&
1447 declaration->symbol == NULL)
1450 switch(declaration->namespc) {
1451 case NAMESPACE_NORMAL:
1452 print_normal_declaration(declaration);
1454 case NAMESPACE_STRUCT:
1455 fputs("struct ", out);
1456 fputs(declaration->symbol->string, out);
1458 print_compound_definition(declaration);
1461 case NAMESPACE_UNION:
1462 fputs("union ", out);
1463 fputs(declaration->symbol->string, out);
1465 print_compound_definition(declaration);
1468 case NAMESPACE_ENUM:
1469 fputs("enum ", out);
1470 fputs(declaration->symbol->string, out);
1472 print_enum_definition(declaration);
1479 * Print the AST of a translation unit.
1481 * @param unit the translation unit
1483 void print_ast(const translation_unit_t *unit)
1487 declaration_t *declaration = unit->scope.declarations;
1488 for( ; declaration != NULL; declaration = declaration->next) {
1489 if(declaration->storage_class == STORAGE_CLASS_ENUM_ENTRY)
1491 if(declaration->namespc != NAMESPACE_NORMAL &&
1492 declaration->symbol == NULL)
1494 if (declaration->implicit)
1498 print_declaration(declaration);
1503 bool is_constant_initializer(const initializer_t *initializer)
1505 switch(initializer->kind) {
1506 case INITIALIZER_STRING:
1507 case INITIALIZER_WIDE_STRING:
1508 case INITIALIZER_DESIGNATOR:
1511 case INITIALIZER_VALUE:
1512 return is_constant_expression(initializer->value.value);
1514 case INITIALIZER_LIST:
1515 for(size_t i = 0; i < initializer->list.len; ++i) {
1516 initializer_t *sub_initializer = initializer->list.initializers[i];
1517 if(!is_constant_initializer(sub_initializer))
1522 panic("invalid initializer kind found");
1525 static bool is_object_with_linker_constant_address(const expression_t *expression)
1527 switch(expression->kind) {
1528 case EXPR_UNARY_DEREFERENCE:
1529 return is_address_constant(expression->unary.value);
1532 type_t *base_type = skip_typeref(expression->select.compound->base.type);
1533 if(is_type_pointer(base_type)) {
1535 return is_address_constant(expression->select.compound);
1537 return is_object_with_linker_constant_address(expression->select.compound);
1541 case EXPR_ARRAY_ACCESS:
1542 return is_constant_expression(expression->array_access.index)
1543 && is_address_constant(expression->array_access.array_ref);
1545 case EXPR_REFERENCE: {
1546 declaration_t *declaration = expression->reference.declaration;
1547 switch((storage_class_tag_t) declaration->storage_class) {
1548 case STORAGE_CLASS_NONE:
1549 case STORAGE_CLASS_EXTERN:
1550 case STORAGE_CLASS_STATIC:
1562 bool is_address_constant(const expression_t *expression)
1564 switch(expression->kind) {
1565 case EXPR_UNARY_TAKE_ADDRESS:
1566 return is_object_with_linker_constant_address(expression->unary.value);
1568 case EXPR_UNARY_DEREFERENCE: {
1570 = revert_automatic_type_conversion(expression->unary.value);
1571 /* dereferencing a function is a NOP */
1572 if(is_type_function(real_type)) {
1573 return is_address_constant(expression->unary.value);
1579 case EXPR_UNARY_CAST: {
1580 type_t *dest = skip_typeref(expression->base.type);
1581 if (!is_type_pointer(dest) &&
1582 ! (dest->kind == TYPE_ATOMIC
1583 && (get_atomic_type_flags(dest->atomic.akind) & ATOMIC_TYPE_FLAG_INTEGER)
1584 && (get_atomic_type_size(dest->atomic.akind) >= get_atomic_type_size(get_intptr_kind()))))
1587 return (is_constant_expression(expression->unary.value)
1588 || is_address_constant(expression->unary.value));
1591 case EXPR_BINARY_ADD:
1592 case EXPR_BINARY_SUB: {
1593 expression_t *left = expression->binary.left;
1594 expression_t *right = expression->binary.right;
1596 if(is_type_pointer(skip_typeref(left->base.type))) {
1597 return is_address_constant(left) && is_constant_expression(right);
1598 } else if(is_type_pointer(skip_typeref(right->base.type))) {
1599 return is_constant_expression(left) && is_address_constant(right);
1605 case EXPR_REFERENCE: {
1606 declaration_t *declaration = expression->reference.declaration;
1607 type_t *type = skip_typeref(declaration->type);
1608 if(is_type_function(type))
1610 if(is_type_array(type)) {
1611 return is_object_with_linker_constant_address(expression);
1621 static bool is_builtin_const_call(const expression_t *expression)
1623 expression_t *function = expression->call.function;
1624 if (function->kind != EXPR_BUILTIN_SYMBOL) {
1628 symbol_t *symbol = function->builtin_symbol.symbol;
1630 switch (symbol->ID) {
1631 case T___builtin_huge_val:
1632 case T___builtin_nan:
1633 case T___builtin_nanf:
1634 case T___builtin_nand:
1641 static bool is_constant_pointer(const expression_t *expression)
1643 if (is_constant_expression(expression))
1646 switch (expression->kind) {
1648 return is_constant_pointer(expression->select.compound);
1649 case EXPR_UNARY_CAST:
1650 return is_constant_pointer(expression->unary.value);
1656 static bool is_object_with_constant_address(const expression_t *expression)
1658 switch(expression->kind) {
1660 expression_t *compound = expression->select.compound;
1661 type_t *compound_type = compound->base.type;
1662 compound_type = skip_typeref(compound_type);
1663 if(is_type_pointer(compound_type)) {
1664 return is_constant_pointer(compound);
1666 return is_object_with_constant_address(compound);
1669 case EXPR_ARRAY_ACCESS:
1670 return is_constant_pointer(expression->array_access.array_ref)
1671 && is_constant_expression(expression->array_access.index);
1672 case EXPR_UNARY_DEREFERENCE:
1673 return is_constant_pointer(expression->unary.value);
1679 bool is_constant_expression(const expression_t *expression)
1681 switch (expression->kind) {
1684 case EXPR_CHARACTER_CONSTANT:
1685 case EXPR_WIDE_CHARACTER_CONSTANT:
1686 case EXPR_STRING_LITERAL:
1687 case EXPR_WIDE_STRING_LITERAL:
1688 case EXPR_CLASSIFY_TYPE:
1692 case EXPR_BUILTIN_CONSTANT_P:
1693 case EXPR_LABEL_ADDRESS:
1697 type_t *type = expression->typeprop.type;
1699 type = expression->typeprop.tp_expression->base.type;
1701 type = skip_typeref(type);
1702 if (is_type_array(type) && type->array.is_vla)
1707 case EXPR_BUILTIN_SYMBOL:
1708 case EXPR_BUILTIN_PREFETCH:
1712 case EXPR_STATEMENT:
1713 case EXPR_UNARY_POSTFIX_INCREMENT:
1714 case EXPR_UNARY_POSTFIX_DECREMENT:
1715 case EXPR_UNARY_PREFIX_INCREMENT:
1716 case EXPR_UNARY_PREFIX_DECREMENT:
1717 case EXPR_UNARY_ASSUME: /* has VOID type */
1718 case EXPR_UNARY_DEREFERENCE:
1719 case EXPR_BINARY_ASSIGN:
1720 case EXPR_BINARY_MUL_ASSIGN:
1721 case EXPR_BINARY_DIV_ASSIGN:
1722 case EXPR_BINARY_MOD_ASSIGN:
1723 case EXPR_BINARY_ADD_ASSIGN:
1724 case EXPR_BINARY_SUB_ASSIGN:
1725 case EXPR_BINARY_SHIFTLEFT_ASSIGN:
1726 case EXPR_BINARY_SHIFTRIGHT_ASSIGN:
1727 case EXPR_BINARY_BITWISE_AND_ASSIGN:
1728 case EXPR_BINARY_BITWISE_XOR_ASSIGN:
1729 case EXPR_BINARY_BITWISE_OR_ASSIGN:
1730 case EXPR_BINARY_COMMA:
1733 case EXPR_UNARY_TAKE_ADDRESS:
1734 return is_object_with_constant_address(expression->unary.value);
1737 return is_builtin_const_call(expression);
1739 case EXPR_UNARY_NEGATE:
1740 case EXPR_UNARY_PLUS:
1741 case EXPR_UNARY_BITWISE_NEGATE:
1742 case EXPR_UNARY_NOT:
1743 return is_constant_expression(expression->unary.value);
1745 case EXPR_UNARY_CAST:
1746 case EXPR_UNARY_CAST_IMPLICIT:
1747 return is_type_arithmetic(skip_typeref(expression->base.type))
1748 && is_constant_expression(expression->unary.value);
1750 case EXPR_BINARY_ADD:
1751 case EXPR_BINARY_SUB:
1752 case EXPR_BINARY_MUL:
1753 case EXPR_BINARY_DIV:
1754 case EXPR_BINARY_MOD:
1755 case EXPR_BINARY_EQUAL:
1756 case EXPR_BINARY_NOTEQUAL:
1757 case EXPR_BINARY_LESS:
1758 case EXPR_BINARY_LESSEQUAL:
1759 case EXPR_BINARY_GREATER:
1760 case EXPR_BINARY_GREATEREQUAL:
1761 case EXPR_BINARY_BITWISE_AND:
1762 case EXPR_BINARY_BITWISE_OR:
1763 case EXPR_BINARY_BITWISE_XOR:
1764 case EXPR_BINARY_LOGICAL_AND:
1765 case EXPR_BINARY_LOGICAL_OR:
1766 case EXPR_BINARY_SHIFTLEFT:
1767 case EXPR_BINARY_SHIFTRIGHT:
1768 case EXPR_BINARY_BUILTIN_EXPECT:
1769 case EXPR_BINARY_ISGREATER:
1770 case EXPR_BINARY_ISGREATEREQUAL:
1771 case EXPR_BINARY_ISLESS:
1772 case EXPR_BINARY_ISLESSEQUAL:
1773 case EXPR_BINARY_ISLESSGREATER:
1774 case EXPR_BINARY_ISUNORDERED:
1775 return is_constant_expression(expression->binary.left)
1776 && is_constant_expression(expression->binary.right);
1778 case EXPR_COMPOUND_LITERAL:
1779 return is_constant_initializer(expression->compound_literal.initializer);
1781 case EXPR_CONDITIONAL: {
1782 expression_t *condition = expression->conditional.condition;
1783 if(!is_constant_expression(condition))
1786 long val = fold_constant(condition);
1788 return is_constant_expression(expression->conditional.true_expression);
1790 return is_constant_expression(expression->conditional.false_expression);
1793 case EXPR_ARRAY_ACCESS:
1794 return is_constant_expression(expression->array_access.array_ref)
1795 && is_constant_expression(expression->array_access.index);
1797 case EXPR_REFERENCE: {
1798 declaration_t *declaration = expression->reference.declaration;
1799 if(declaration->storage_class == STORAGE_CLASS_ENUM_ENTRY)
1811 panic("invalid expression found (is constant expression)");
1815 * Initialize the AST construction.
1819 obstack_init(&ast_obstack);
1827 obstack_free(&ast_obstack, NULL);
1831 * Set the output stream for the AST printer.
1833 * @param stream the output stream
1835 void ast_set_output(FILE *stream)
1838 type_set_output(stream);
1842 * Allocate an AST object of the given size.
1844 * @param size the size of the object to allocate
1846 * @return A new allocated object in the AST memeory space.
1848 void *(allocate_ast)(size_t size)
1850 return _allocate_ast(size);