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,
128 [EXPR_LABEL_ADDRESS] = PREC_PRIM,
130 [EXPR_UNARY_NEGATE] = PREC_UNARY,
131 [EXPR_UNARY_PLUS] = PREC_UNARY,
132 [EXPR_UNARY_BITWISE_NEGATE] = PREC_UNARY,
133 [EXPR_UNARY_NOT] = PREC_UNARY,
134 [EXPR_UNARY_DEREFERENCE] = PREC_UNARY,
135 [EXPR_UNARY_TAKE_ADDRESS] = PREC_UNARY,
136 [EXPR_UNARY_POSTFIX_INCREMENT] = PREC_UNARY,
137 [EXPR_UNARY_POSTFIX_DECREMENT] = PREC_UNARY,
138 [EXPR_UNARY_PREFIX_INCREMENT] = PREC_UNARY,
139 [EXPR_UNARY_PREFIX_DECREMENT] = PREC_UNARY,
140 [EXPR_UNARY_CAST] = PREC_UNARY,
141 [EXPR_UNARY_CAST_IMPLICIT] = PREC_UNARY,
142 [EXPR_UNARY_ASSUME] = PREC_PRIM,
144 [EXPR_BINARY_ADD] = PREC_PLUS,
145 [EXPR_BINARY_SUB] = PREC_PLUS,
146 [EXPR_BINARY_MUL] = PREC_MUL,
147 [EXPR_BINARY_DIV] = PREC_MUL,
148 [EXPR_BINARY_MOD] = PREC_MUL,
149 [EXPR_BINARY_EQUAL] = PREC_EQ,
150 [EXPR_BINARY_NOTEQUAL] = PREC_EQ,
151 [EXPR_BINARY_LESS] = PREC_CMP,
152 [EXPR_BINARY_LESSEQUAL] = PREC_CMP,
153 [EXPR_BINARY_GREATER] = PREC_CMP,
154 [EXPR_BINARY_GREATEREQUAL] = PREC_CMP,
155 [EXPR_BINARY_BITWISE_AND] = PREC_BIT_AND,
156 [EXPR_BINARY_BITWISE_OR] = PREC_BIT_OR,
157 [EXPR_BINARY_BITWISE_XOR] = PREC_BIT_XOR,
158 [EXPR_BINARY_LOGICAL_AND] = PREC_LOG_AND,
159 [EXPR_BINARY_LOGICAL_OR] = PREC_LOG_OR,
160 [EXPR_BINARY_SHIFTLEFT] = PREC_SHF,
161 [EXPR_BINARY_SHIFTRIGHT] = PREC_SHF,
162 [EXPR_BINARY_ASSIGN] = PREC_ASSIGN,
163 [EXPR_BINARY_MUL_ASSIGN] = PREC_ASSIGN,
164 [EXPR_BINARY_DIV_ASSIGN] = PREC_ASSIGN,
165 [EXPR_BINARY_MOD_ASSIGN] = PREC_ASSIGN,
166 [EXPR_BINARY_ADD_ASSIGN] = PREC_ASSIGN,
167 [EXPR_BINARY_SUB_ASSIGN] = PREC_ASSIGN,
168 [EXPR_BINARY_SHIFTLEFT_ASSIGN] = PREC_ASSIGN,
169 [EXPR_BINARY_SHIFTRIGHT_ASSIGN] = PREC_ASSIGN,
170 [EXPR_BINARY_BITWISE_AND_ASSIGN] = PREC_ASSIGN,
171 [EXPR_BINARY_BITWISE_XOR_ASSIGN] = PREC_ASSIGN,
172 [EXPR_BINARY_BITWISE_OR_ASSIGN] = PREC_ASSIGN,
173 [EXPR_BINARY_COMMA] = PREC_COMMA,
175 [EXPR_BINARY_BUILTIN_EXPECT] = PREC_PRIM,
176 [EXPR_BINARY_ISGREATER] = PREC_PRIM,
177 [EXPR_BINARY_ISGREATEREQUAL] = PREC_PRIM,
178 [EXPR_BINARY_ISLESS] = PREC_PRIM,
179 [EXPR_BINARY_ISLESSEQUAL] = PREC_PRIM,
180 [EXPR_BINARY_ISLESSGREATER] = PREC_PRIM,
181 [EXPR_BINARY_ISUNORDERED] = PREC_PRIM
183 assert((unsigned)kind < (sizeof(prec)/sizeof(prec[0])));
184 unsigned res = prec[kind];
186 assert(res != PREC_BOTTOM);
191 * Print a constant expression.
193 * @param cnst the constant expression
195 static void print_const(const const_expression_t *cnst)
197 if(cnst->base.type == NULL)
200 const type_t *const type = skip_typeref(cnst->base.type);
202 if (is_type_integer(type)) {
203 fprintf(out, "%lld", cnst->v.int_value);
204 } else if (is_type_float(type)) {
205 long double const val = cnst->v.float_value;
206 fprintf(out, "%.20Lg", val);
207 if (isfinite(val) && truncl(val) == val)
210 panic("unknown constant");
214 switch (type->atomic.akind) {
215 case ATOMIC_TYPE_UINT: suffix = "U"; break;
216 case ATOMIC_TYPE_LONG: suffix = "L"; break;
217 case ATOMIC_TYPE_ULONG: suffix = "UL"; break;
218 case ATOMIC_TYPE_LONGLONG: suffix = "LL"; break;
219 case ATOMIC_TYPE_ULONGLONG: suffix = "ULL"; break;
220 case ATOMIC_TYPE_FLOAT: suffix = "F"; break;
221 case ATOMIC_TYPE_LONG_DOUBLE: suffix = "L"; break;
223 default: suffix = NULL; break;
230 * Print a quoted string constant.
232 * @param string the string constant
233 * @param border the border char
234 * @param skip number of chars to skip at the end
236 static void print_quoted_string(const string_t *const string, char border, int skip)
239 const char *end = string->begin + string->size - skip;
240 for (const char *c = string->begin; c != end; ++c) {
245 case '\\': fputs("\\\\", out); break;
246 case '\a': fputs("\\a", out); break;
247 case '\b': fputs("\\b", out); break;
248 case '\f': fputs("\\f", out); break;
249 case '\n': fputs("\\n", out); break;
250 case '\r': fputs("\\r", out); break;
251 case '\t': fputs("\\t", out); break;
252 case '\v': fputs("\\v", out); break;
253 case '\?': fputs("\\?", out); break;
255 if (c_mode & _GNUC) {
256 fputs("\\e", out); break;
261 fprintf(out, "\\%03o", *c);
272 * Prints a wide string literal expression.
274 * @param wstr the wide string literal expression
275 * @param border the border char
276 * @param skip number of chars to skip at the end
278 static void print_quoted_wide_string(const wide_string_t *const wstr,
279 char border, int skip)
283 const wchar_rep_t *end = wstr->begin + wstr->size - skip;
284 for (const wchar_rep_t *c = wstr->begin; c != end; ++c) {
286 case L'\"': fputs("\\\"", out); break;
287 case L'\\': fputs("\\\\", out); break;
288 case L'\a': fputs("\\a", out); break;
289 case L'\b': fputs("\\b", out); break;
290 case L'\f': fputs("\\f", out); break;
291 case L'\n': fputs("\\n", out); break;
292 case L'\r': fputs("\\r", out); break;
293 case L'\t': fputs("\\t", out); break;
294 case L'\v': fputs("\\v", out); break;
295 case L'\?': fputs("\\?", out); break;
297 if (c_mode & _GNUC) {
298 fputs("\\e", out); break;
302 const unsigned tc = *c;
305 fprintf(out, "\\%03o", (char)*c);
309 } else if (tc < 0x800) {
310 fputc(0xC0 | (tc >> 6), out);
311 fputc(0x80 | (tc & 0x3F), out);
312 } else if (tc < 0x10000) {
313 fputc(0xE0 | ( tc >> 12), out);
314 fputc(0x80 | ((tc >> 6) & 0x3F), out);
315 fputc(0x80 | ( tc & 0x3F), out);
317 fputc(0xF0 | ( tc >> 18), out);
318 fputc(0x80 | ((tc >> 12) & 0x3F), out);
319 fputc(0x80 | ((tc >> 6) & 0x3F), out);
320 fputc(0x80 | ( tc & 0x3F), out);
329 * Print a constant character expression.
331 * @param cnst the constant character expression
333 static void print_character_constant(const const_expression_t *cnst)
335 print_quoted_string(&cnst->v.character, '\'', 0);
338 static void print_wide_character_constant(const const_expression_t *cnst)
340 print_quoted_wide_string(&cnst->v.wide_character, '\'', 0);
344 * Prints a string literal expression.
346 * @param string_literal the string literal expression
348 static void print_string_literal(
349 const string_literal_expression_t *string_literal)
351 print_quoted_string(&string_literal->value, '"', 1);
355 * Prints a predefined symbol.
357 static void print_funcname(
358 const funcname_expression_t *funcname)
361 switch(funcname->kind) {
362 case FUNCNAME_FUNCTION: s = (c_mode & _C99) ? "__func__" : "__FUNCTION__"; break;
363 case FUNCNAME_PRETTY_FUNCTION: s = "__PRETTY_FUNCTION__"; break;
364 case FUNCNAME_FUNCSIG: s = "__FUNCSIG__"; break;
365 case FUNCNAME_FUNCDNAME: s = "__FUNCDNAME__"; break;
372 static void print_wide_string_literal(
373 const wide_string_literal_expression_t *const wstr)
375 print_quoted_wide_string(&wstr->value, '"', 1);
378 static void print_compound_literal(
379 const compound_literal_expression_t *expression)
382 print_type(expression->type);
384 print_initializer(expression->initializer);
388 * Prints a call expression.
390 * @param call the call expression
392 static void print_call_expression(const call_expression_t *call)
394 unsigned prec = get_expression_precedence(call->base.kind);
395 print_expression_prec(call->function, prec);
397 call_argument_t *argument = call->arguments;
399 while(argument != NULL) {
405 print_expression_prec(argument->expression, PREC_COMMA + 1);
407 argument = argument->next;
413 * Prints a binary expression.
415 * @param binexpr the binary expression
417 static void print_binary_expression(const binary_expression_t *binexpr)
419 unsigned prec = get_expression_precedence(binexpr->base.kind);
420 int r2l = right_to_left(prec);
422 if(binexpr->base.kind == EXPR_BINARY_BUILTIN_EXPECT) {
423 fputs("__builtin_expect(", out);
424 print_expression_prec(binexpr->left, prec);
426 print_expression_prec(binexpr->right, prec);
431 print_expression_prec(binexpr->left, prec + r2l);
432 if (binexpr->base.kind != EXPR_BINARY_COMMA) {
435 switch (binexpr->base.kind) {
436 case EXPR_BINARY_COMMA: fputs(",", out); break;
437 case EXPR_BINARY_ASSIGN: fputs("=", out); break;
438 case EXPR_BINARY_ADD: fputs("+", out); break;
439 case EXPR_BINARY_SUB: fputs("-", out); break;
440 case EXPR_BINARY_MUL: fputs("*", out); break;
441 case EXPR_BINARY_MOD: fputs("%", out); break;
442 case EXPR_BINARY_DIV: fputs("/", out); break;
443 case EXPR_BINARY_BITWISE_OR: fputs("|", out); break;
444 case EXPR_BINARY_BITWISE_AND: fputs("&", out); break;
445 case EXPR_BINARY_BITWISE_XOR: fputs("^", out); break;
446 case EXPR_BINARY_LOGICAL_OR: fputs("||", out); break;
447 case EXPR_BINARY_LOGICAL_AND: fputs("&&", out); break;
448 case EXPR_BINARY_NOTEQUAL: fputs("!=", out); break;
449 case EXPR_BINARY_EQUAL: fputs("==", out); break;
450 case EXPR_BINARY_LESS: fputs("<", out); break;
451 case EXPR_BINARY_LESSEQUAL: fputs("<=", out); break;
452 case EXPR_BINARY_GREATER: fputs(">", out); break;
453 case EXPR_BINARY_GREATEREQUAL: fputs(">=", out); break;
454 case EXPR_BINARY_SHIFTLEFT: fputs("<<", out); break;
455 case EXPR_BINARY_SHIFTRIGHT: fputs(">>", out); break;
457 case EXPR_BINARY_ADD_ASSIGN: fputs("+=", out); break;
458 case EXPR_BINARY_SUB_ASSIGN: fputs("-=", out); break;
459 case EXPR_BINARY_MUL_ASSIGN: fputs("*=", out); break;
460 case EXPR_BINARY_MOD_ASSIGN: fputs("%=", out); break;
461 case EXPR_BINARY_DIV_ASSIGN: fputs("/=", out); break;
462 case EXPR_BINARY_BITWISE_OR_ASSIGN: fputs("|=", out); break;
463 case EXPR_BINARY_BITWISE_AND_ASSIGN: fputs("&=", out); break;
464 case EXPR_BINARY_BITWISE_XOR_ASSIGN: fputs("^=", out); break;
465 case EXPR_BINARY_SHIFTLEFT_ASSIGN: fputs("<<=", out); break;
466 case EXPR_BINARY_SHIFTRIGHT_ASSIGN: fputs(">>=", out); break;
467 default: panic("invalid binexpression found");
470 print_expression_prec(binexpr->right, prec - r2l);
474 * Prints an unary expression.
476 * @param unexpr the unary expression
478 static void print_unary_expression(const unary_expression_t *unexpr)
480 unsigned prec = get_expression_precedence(unexpr->base.kind);
481 switch(unexpr->base.kind) {
482 case EXPR_UNARY_NEGATE: fputc('-', out); break;
483 case EXPR_UNARY_PLUS: fputc('+', out); break;
484 case EXPR_UNARY_NOT: fputc('!', out); break;
485 case EXPR_UNARY_BITWISE_NEGATE: fputc('~', out); break;
486 case EXPR_UNARY_PREFIX_INCREMENT: fputs("++", out); break;
487 case EXPR_UNARY_PREFIX_DECREMENT: fputs("--", out); break;
488 case EXPR_UNARY_DEREFERENCE: fputc('*', out); break;
489 case EXPR_UNARY_TAKE_ADDRESS: fputc('&', out); break;
491 case EXPR_UNARY_POSTFIX_INCREMENT:
492 print_expression_prec(unexpr->value, prec);
495 case EXPR_UNARY_POSTFIX_DECREMENT:
496 print_expression_prec(unexpr->value, prec);
499 case EXPR_UNARY_CAST_IMPLICIT:
500 case EXPR_UNARY_CAST:
502 print_type(unexpr->base.type);
505 case EXPR_UNARY_ASSUME:
506 fputs("__assume(", out);
507 print_expression_prec(unexpr->value, PREC_COMMA + 1);
511 panic("invalid unary expression found");
513 print_expression_prec(unexpr->value, prec);
517 * Prints a reference expression.
519 * @param ref the reference expression
521 static void print_reference_expression(const reference_expression_t *ref)
523 fputs(ref->declaration->symbol->string, out);
527 * Prints a label address expression.
529 * @param ref the reference expression
531 static void print_label_address_expression(const label_address_expression_t *le)
533 fprintf(out, "&&%s", le->declaration->symbol->string);
537 * Prints an array expression.
539 * @param expression the array expression
541 static void print_array_expression(const array_access_expression_t *expression)
543 unsigned prec = get_expression_precedence(expression->base.kind);
544 if(!expression->flipped) {
545 print_expression_prec(expression->array_ref, prec);
547 print_expression_prec(expression->index, PREC_BOTTOM);
550 print_expression_prec(expression->index, prec);
552 print_expression_prec(expression->array_ref, PREC_BOTTOM);
558 * Prints a typeproperty expression (sizeof or __alignof__).
560 * @param expression the type property expression
562 static void print_typeprop_expression(const typeprop_expression_t *expression)
564 if (expression->base.kind == EXPR_SIZEOF) {
565 fputs("sizeof", out);
567 assert(expression->base.kind == EXPR_ALIGNOF);
568 fputs("__alignof__", out);
570 if(expression->tp_expression != NULL) {
571 /* always print the '()' here, sizeof x is right but unusual */
573 print_expression_prec(expression->tp_expression, PREC_ACCESS);
577 print_type(expression->type);
583 * Prints an builtin symbol.
585 * @param expression the builtin symbol expression
587 static void print_builtin_symbol(const builtin_symbol_expression_t *expression)
589 fputs(expression->symbol->string, out);
593 * Prints a builtin constant expression.
595 * @param expression the builtin constant expression
597 static void print_builtin_constant(const builtin_constant_expression_t *expression)
599 fputs("__builtin_constant_p(", out);
600 print_expression_prec(expression->value, PREC_COMMA + 1);
605 * Prints a builtin prefetch expression.
607 * @param expression the builtin prefetch expression
609 static void print_builtin_prefetch(const builtin_prefetch_expression_t *expression)
611 fputs("__builtin_prefetch(", out);
612 print_expression_prec(expression->adr, PREC_COMMA + 1);
613 if (expression->rw) {
615 print_expression_prec(expression->rw, PREC_COMMA + 1);
617 if (expression->locality) {
619 print_expression_prec(expression->locality, PREC_COMMA + 1);
625 * Prints a conditional expression.
627 * @param expression the conditional expression
629 static void print_conditional(const conditional_expression_t *expression)
631 print_expression_prec(expression->condition, PREC_LOG_OR);
633 if (expression->true_expression != NULL) {
634 print_expression_prec(expression->true_expression, PREC_EXPR);
639 print_expression_prec(expression->false_expression, PREC_COND);
643 * Prints a va_start expression.
645 * @param expression the va_start expression
647 static void print_va_start(const va_start_expression_t *const expression)
649 fputs("__builtin_va_start(", out);
650 print_expression_prec(expression->ap, PREC_COMMA + 1);
652 fputs(expression->parameter->symbol->string, out);
657 * Prints a va_arg expression.
659 * @param expression the va_arg expression
661 static void print_va_arg(const va_arg_expression_t *expression)
663 fputs("__builtin_va_arg(", out);
664 print_expression_prec(expression->ap, PREC_COMMA + 1);
666 print_type(expression->base.type);
671 * Prints a select expression (. or ->).
673 * @param expression the select expression
675 static void print_select(const select_expression_t *expression)
677 unsigned prec = get_expression_precedence(expression->base.kind);
678 print_expression_prec(expression->compound, prec);
679 if(is_type_pointer(skip_typeref(expression->compound->base.type))) {
684 fputs(expression->compound_entry->symbol->string, out);
688 * Prints a type classify expression.
690 * @param expr the type classify expression
692 static void print_classify_type_expression(
693 const classify_type_expression_t *const expr)
695 fputs("__builtin_classify_type(", out);
696 print_expression_prec(expr->type_expression, PREC_COMMA + 1);
701 * Prints a designator.
703 * @param designator the designator
705 static void print_designator(const designator_t *designator)
707 for ( ; designator != NULL; designator = designator->next) {
708 if (designator->symbol == NULL) {
710 print_expression_prec(designator->array_index, PREC_BOTTOM);
714 fputs(designator->symbol->string, out);
720 * Prints an offsetof expression.
722 * @param expression the offset expression
724 static void print_offsetof_expression(const offsetof_expression_t *expression)
726 fputs("__builtin_offsetof", out);
728 print_type(expression->type);
730 print_designator(expression->designator);
735 * Prints a statement expression.
737 * @param expression the statement expression
739 static void print_statement_expression(const statement_expression_t *expression)
742 print_statement(expression->statement);
747 * Prints an expression with parenthesis if needed.
749 * @param expression the expression to print
750 * @param top_prec the precedence of the user of this expression.
752 static void print_expression_prec(const expression_t *expression, unsigned top_prec)
754 if (expression->kind == EXPR_UNARY_CAST_IMPLICIT && !print_implicit_casts) {
755 expression = expression->unary.value;
757 unsigned prec = get_expression_precedence(expression->base.kind);
758 if (print_parenthesis && top_prec != PREC_BOTTOM)
762 switch(expression->kind) {
765 fputs("$invalid expression$", out);
767 case EXPR_CHARACTER_CONSTANT:
768 print_character_constant(&expression->conste);
770 case EXPR_WIDE_CHARACTER_CONSTANT:
771 print_wide_character_constant(&expression->conste);
774 print_const(&expression->conste);
777 print_funcname(&expression->funcname);
779 case EXPR_STRING_LITERAL:
780 print_string_literal(&expression->string);
782 case EXPR_WIDE_STRING_LITERAL:
783 print_wide_string_literal(&expression->wide_string);
785 case EXPR_COMPOUND_LITERAL:
786 print_compound_literal(&expression->compound_literal);
789 print_call_expression(&expression->call);
792 print_binary_expression(&expression->binary);
795 print_reference_expression(&expression->reference);
797 case EXPR_ARRAY_ACCESS:
798 print_array_expression(&expression->array_access);
800 case EXPR_LABEL_ADDRESS:
801 print_label_address_expression(&expression->label_address);
804 print_unary_expression(&expression->unary);
808 print_typeprop_expression(&expression->typeprop);
810 case EXPR_BUILTIN_SYMBOL:
811 print_builtin_symbol(&expression->builtin_symbol);
813 case EXPR_BUILTIN_CONSTANT_P:
814 print_builtin_constant(&expression->builtin_constant);
816 case EXPR_BUILTIN_PREFETCH:
817 print_builtin_prefetch(&expression->builtin_prefetch);
819 case EXPR_CONDITIONAL:
820 print_conditional(&expression->conditional);
823 print_va_start(&expression->va_starte);
826 print_va_arg(&expression->va_arge);
829 print_select(&expression->select);
831 case EXPR_CLASSIFY_TYPE:
832 print_classify_type_expression(&expression->classify_type);
835 print_offsetof_expression(&expression->offsetofe);
838 print_statement_expression(&expression->statement);
843 fprintf(out, "some expression of type %d", (int) expression->kind);
851 * Print an compound statement.
853 * @param block the compound statement
855 static void print_compound_statement(const compound_statement_t *block)
860 statement_t *statement = block->statements;
861 while(statement != NULL) {
862 if (statement->base.kind == STATEMENT_CASE_LABEL)
865 print_statement(statement);
867 statement = statement->base.next;
875 * Print a return statement.
877 * @param statement the return statement
879 static void print_return_statement(const return_statement_t *statement)
881 fputs("return ", out);
882 if(statement->value != NULL)
883 print_expression(statement->value);
888 * Print an expression statement.
890 * @param statement the expression statement
892 static void print_expression_statement(const expression_statement_t *statement)
894 print_expression(statement->expression);
899 * Print a goto statement.
901 * @param statement the goto statement
903 static void print_goto_statement(const goto_statement_t *statement)
906 if (statement->expression != NULL) {
908 print_expression(statement->expression);
910 fputs(statement->label->symbol->string, out);
916 * Print a label statement.
918 * @param statement the label statement
920 static void print_label_statement(const label_statement_t *statement)
922 fprintf(out, "%s:\n", statement->label->symbol->string);
923 print_statement(statement->statement);
927 * Print an if statement.
929 * @param statement the if statement
931 static void print_if_statement(const if_statement_t *statement)
934 print_expression(statement->condition);
936 print_statement(statement->true_statement);
938 if(statement->false_statement != NULL) {
941 print_statement(statement->false_statement);
946 * Print a switch statement.
948 * @param statement the switch statement
950 static void print_switch_statement(const switch_statement_t *statement)
952 fputs("switch (", out);
953 print_expression(statement->expression);
955 print_statement(statement->body);
959 * Print a case label (including the default label).
961 * @param statement the case label statement
963 static void print_case_label(const case_label_statement_t *statement)
965 if(statement->expression == NULL) {
966 fputs("default:\n", out);
969 print_expression(statement->expression);
970 if (statement->end_range != NULL) {
972 print_expression(statement->end_range);
977 if(statement->statement != NULL) {
978 if (statement->statement->base.kind == STATEMENT_CASE_LABEL) {
982 print_statement(statement->statement);
987 * Print a declaration statement.
989 * @param statement the statement
991 static void print_declaration_statement(
992 const declaration_statement_t *statement)
995 for (declaration_t *declaration = statement->declarations_begin;
996 declaration != statement->declarations_end->next;
997 declaration = declaration->next) {
998 if (declaration->storage_class == STORAGE_CLASS_ENUM_ENTRY)
1000 if (declaration->implicit)
1008 print_declaration(declaration);
1014 * Print a while statement.
1016 * @param statement the statement
1018 static void print_while_statement(const while_statement_t *statement)
1020 fputs("while (", out);
1021 print_expression(statement->condition);
1023 print_statement(statement->body);
1027 * Print a do-while statement.
1029 * @param statement the statement
1031 static void print_do_while_statement(const do_while_statement_t *statement)
1034 print_statement(statement->body);
1036 fputs("while (", out);
1037 print_expression(statement->condition);
1042 * Print a for statement.
1044 * @param statement the statement
1046 static void print_for_statement(const for_statement_t *statement)
1048 fputs("for (", out);
1049 declaration_t *decl = statement->scope.declarations;
1050 while (decl != NULL && decl->implicit)
1053 assert(statement->initialisation == NULL);
1054 print_declaration(decl);
1055 if (decl->next != NULL) {
1056 panic("multiple declarations in for statement not supported yet");
1060 if(statement->initialisation) {
1061 print_expression(statement->initialisation);
1065 if(statement->condition != NULL) {
1066 print_expression(statement->condition);
1069 if(statement->step != NULL) {
1070 print_expression(statement->step);
1073 print_statement(statement->body);
1077 * Print assembler arguments.
1079 * @param arguments the arguments
1081 static void print_asm_arguments(asm_argument_t *arguments)
1083 asm_argument_t *argument = arguments;
1084 for( ; argument != NULL; argument = argument->next) {
1085 if(argument != arguments)
1088 if(argument->symbol) {
1089 fprintf(out, "[%s] ", argument->symbol->string);
1091 print_quoted_string(&argument->constraints, '"', 1);
1093 print_expression(argument->expression);
1099 * Print assembler clobbers.
1101 * @param clobbers the clobbers
1103 static void print_asm_clobbers(asm_clobber_t *clobbers)
1105 asm_clobber_t *clobber = clobbers;
1106 for( ; clobber != NULL; clobber = clobber->next) {
1107 if(clobber != clobbers)
1110 print_quoted_string(&clobber->clobber, '"', 1);
1115 * Print an assembler statement.
1117 * @param statement the statement
1119 static void print_asm_statement(const asm_statement_t *statement)
1122 if(statement->is_volatile) {
1123 fputs("volatile ", out);
1126 print_quoted_string(&statement->asm_text, '"', 1);
1127 if(statement->inputs == NULL && statement->outputs == NULL
1128 && statement->clobbers == NULL)
1129 goto end_of_print_asm_statement;
1132 print_asm_arguments(statement->inputs);
1133 if(statement->outputs == NULL && statement->clobbers == NULL)
1134 goto end_of_print_asm_statement;
1137 print_asm_arguments(statement->outputs);
1138 if(statement->clobbers == NULL)
1139 goto end_of_print_asm_statement;
1142 print_asm_clobbers(statement->clobbers);
1144 end_of_print_asm_statement:
1149 * Print a microsoft __try statement.
1151 * @param statement the statement
1153 static void print_ms_try_statement(const ms_try_statement_t *statement)
1155 fputs("__try ", out);
1156 print_statement(statement->try_statement);
1158 if(statement->except_expression != NULL) {
1159 fputs("__except(", out);
1160 print_expression(statement->except_expression);
1163 fputs("__finally ", out);
1165 print_statement(statement->final_statement);
1169 * Print a microsoft __leave statement.
1171 * @param statement the statement
1173 static void print_leave_statement(const leave_statement_t *statement)
1176 fputs("__leave;\n", out);
1180 * Print a statement.
1182 * @param statement the statement
1184 void print_statement(const statement_t *statement)
1186 switch(statement->kind) {
1187 case STATEMENT_EMPTY:
1190 case STATEMENT_COMPOUND:
1191 print_compound_statement(&statement->compound);
1193 case STATEMENT_RETURN:
1194 print_return_statement(&statement->returns);
1196 case STATEMENT_EXPRESSION:
1197 print_expression_statement(&statement->expression);
1199 case STATEMENT_LABEL:
1200 print_label_statement(&statement->label);
1202 case STATEMENT_GOTO:
1203 print_goto_statement(&statement->gotos);
1205 case STATEMENT_CONTINUE:
1206 fputs("continue;\n", out);
1208 case STATEMENT_BREAK:
1209 fputs("break;\n", out);
1212 print_if_statement(&statement->ifs);
1214 case STATEMENT_SWITCH:
1215 print_switch_statement(&statement->switchs);
1217 case STATEMENT_CASE_LABEL:
1218 print_case_label(&statement->case_label);
1220 case STATEMENT_DECLARATION:
1221 print_declaration_statement(&statement->declaration);
1223 case STATEMENT_WHILE:
1224 print_while_statement(&statement->whiles);
1226 case STATEMENT_DO_WHILE:
1227 print_do_while_statement(&statement->do_while);
1230 print_for_statement(&statement->fors);
1233 print_asm_statement(&statement->asms);
1235 case STATEMENT_MS_TRY:
1236 print_ms_try_statement(&statement->ms_try);
1238 case STATEMENT_LEAVE:
1239 print_leave_statement(&statement->leave);
1241 case STATEMENT_INVALID:
1242 fputs("$invalid statement$", out);
1248 * Print a storage class.
1250 * @param storage_class the storage class
1252 static void print_storage_class(storage_class_tag_t storage_class)
1254 switch(storage_class) {
1255 case STORAGE_CLASS_ENUM_ENTRY:
1256 case STORAGE_CLASS_NONE:
1258 case STORAGE_CLASS_TYPEDEF: fputs("typedef ", out); break;
1259 case STORAGE_CLASS_EXTERN: fputs("extern ", out); break;
1260 case STORAGE_CLASS_STATIC: fputs("static ", out); break;
1261 case STORAGE_CLASS_AUTO: fputs("auto ", out); break;
1262 case STORAGE_CLASS_REGISTER: fputs("register ", out); break;
1263 case STORAGE_CLASS_THREAD: fputs("__thread", out); break;
1264 case STORAGE_CLASS_THREAD_EXTERN: fputs("extern __thread", out); break;
1265 case STORAGE_CLASS_THREAD_STATIC: fputs("static __thread", out); break;
1270 * Print an initializer.
1272 * @param initializer the initializer
1274 void print_initializer(const initializer_t *initializer)
1276 if(initializer == NULL) {
1281 switch(initializer->kind) {
1282 case INITIALIZER_VALUE: {
1283 const initializer_value_t *value = &initializer->value;
1284 print_expression(value->value);
1287 case INITIALIZER_LIST: {
1288 assert(initializer->kind == INITIALIZER_LIST);
1290 const initializer_list_t *list = &initializer->list;
1292 for(size_t i = 0 ; i < list->len; ++i) {
1293 const initializer_t *sub_init = list->initializers[i];
1294 print_initializer(list->initializers[i]);
1295 if(i < list->len-1) {
1296 if(sub_init == NULL || sub_init->kind != INITIALIZER_DESIGNATOR)
1303 case INITIALIZER_STRING:
1304 print_quoted_string(&initializer->string.string, '"', 1);
1306 case INITIALIZER_WIDE_STRING:
1307 print_quoted_wide_string(&initializer->wide_string.string, '"', 1);
1309 case INITIALIZER_DESIGNATOR:
1310 print_designator(initializer->designator.designator);
1315 panic("invalid initializer kind found");
1319 * Print microsoft extended declaration modifiers.
1321 static void print_ms_modifiers(const declaration_t *declaration) {
1322 if((c_mode & _MS) == 0)
1325 decl_modifiers_t modifiers = declaration->modifiers;
1327 /* DM_FORCEINLINE handled outside. */
1328 if ((modifiers & ~DM_FORCEINLINE) != 0 ||
1329 declaration->alignment != 0 ||
1330 declaration->get_property_sym != NULL ||
1331 declaration->put_property_sym != NULL) {
1334 fputs("__declspec", out);
1335 if(modifiers & DM_DLLIMPORT) {
1336 fputs(next, out); next = ", "; fputs("dllimport", out);
1338 if(modifiers & DM_DLLEXPORT) {
1339 fputs(next, out); next = ", "; fputs("dllexport", out);
1341 if(modifiers & DM_THREAD) {
1342 fputs(next, out); next = ", "; fputs("thread", out);
1344 if(modifiers & DM_NAKED) {
1345 fputs(next, out); next = ", "; fputs("naked", out);
1347 if(modifiers & DM_THREAD) {
1348 fputs(next, out); next = ", "; fputs("thread", out);
1350 if(modifiers & DM_SELECTANY) {
1351 fputs(next, out); next = ", "; fputs("selectany", out);
1353 if(modifiers & DM_NOTHROW) {
1354 fputs(next, out); next = ", "; fputs("nothrow", out);
1356 if(modifiers & DM_NORETURN) {
1357 fputs(next, out); next = ", "; fputs("noreturn", out);
1359 if(modifiers & DM_NOINLINE) {
1360 fputs(next, out); next = ", "; fputs("noinline", out);
1362 if (modifiers & DM_DEPRECATED) {
1363 fputs(next, out); next = ", "; fputs("deprecated", out);
1364 if(declaration->deprecated_string != NULL)
1365 fprintf(out, "(\"%s\")", declaration->deprecated_string);
1367 if(declaration->alignment != 0) {
1368 fputs(next, out); next = ", "; fprintf(out, "align(%u)", declaration->alignment);
1370 if(modifiers & DM_RESTRICT) {
1371 fputs(next, out); next = ", "; fputs("restrict", out);
1373 if(modifiers & DM_NOALIAS) {
1374 fputs(next, out); next = ", "; fputs("noalias", out);
1376 if(declaration->get_property_sym != NULL || declaration->put_property_sym != NULL) {
1378 fputs(next, out); next = ", "; fputs("property(", out);
1379 if(declaration->get_property_sym != NULL) {
1380 fprintf(out, "get=%s", declaration->get_property_sym->string);
1383 if(declaration->put_property_sym != NULL)
1384 fprintf(out, "%sput=%s", comma, declaration->put_property_sym->string);
1392 * Print a declaration in the NORMAL namespace.
1394 * @param declaration the declaration
1396 static void print_normal_declaration(const declaration_t *declaration)
1398 print_storage_class((storage_class_tag_t) declaration->declared_storage_class);
1399 if (declaration->is_inline) {
1400 if (declaration->modifiers & DM_FORCEINLINE) {
1401 fputs("__forceinline ", out);
1402 } else if (declaration->modifiers & DM_MICROSOFT_INLINE) {
1403 fputs("__inline ", out);
1405 fputs("inline ", out);
1408 print_ms_modifiers(declaration);
1409 print_type_ext(declaration->type, declaration->symbol,
1410 &declaration->scope);
1412 if(declaration->type->kind == TYPE_FUNCTION) {
1413 if(declaration->init.statement != NULL) {
1415 print_statement(declaration->init.statement);
1418 } else if(declaration->init.initializer != NULL) {
1420 print_initializer(declaration->init.initializer);
1426 * Prints an expression.
1428 * @param expression the expression
1430 void print_expression(const expression_t *expression) {
1431 print_expression_prec(expression, PREC_BOTTOM);
1435 * Print a declaration.
1437 * @param declaration the declaration
1439 void print_declaration(const declaration_t *declaration)
1441 if(declaration->namespc != NAMESPACE_NORMAL &&
1442 declaration->symbol == NULL)
1445 switch(declaration->namespc) {
1446 case NAMESPACE_NORMAL:
1447 print_normal_declaration(declaration);
1449 case NAMESPACE_STRUCT:
1450 fputs("struct ", out);
1451 fputs(declaration->symbol->string, out);
1453 print_compound_definition(declaration);
1456 case NAMESPACE_UNION:
1457 fputs("union ", out);
1458 fputs(declaration->symbol->string, out);
1460 print_compound_definition(declaration);
1463 case NAMESPACE_ENUM:
1464 fputs("enum ", out);
1465 fputs(declaration->symbol->string, out);
1467 print_enum_definition(declaration);
1474 * Print the AST of a translation unit.
1476 * @param unit the translation unit
1478 void print_ast(const translation_unit_t *unit)
1482 declaration_t *declaration = unit->scope.declarations;
1483 for( ; declaration != NULL; declaration = declaration->next) {
1484 if(declaration->storage_class == STORAGE_CLASS_ENUM_ENTRY)
1486 if(declaration->namespc != NAMESPACE_NORMAL &&
1487 declaration->symbol == NULL)
1489 if (declaration->implicit)
1493 print_declaration(declaration);
1498 bool is_constant_initializer(const initializer_t *initializer)
1500 switch(initializer->kind) {
1501 case INITIALIZER_STRING:
1502 case INITIALIZER_WIDE_STRING:
1503 case INITIALIZER_DESIGNATOR:
1506 case INITIALIZER_VALUE:
1507 return is_constant_expression(initializer->value.value);
1509 case INITIALIZER_LIST:
1510 for(size_t i = 0; i < initializer->list.len; ++i) {
1511 initializer_t *sub_initializer = initializer->list.initializers[i];
1512 if(!is_constant_initializer(sub_initializer))
1517 panic("invalid initializer kind found");
1520 static bool is_object_with_linker_constant_address(const expression_t *expression)
1522 switch(expression->kind) {
1523 case EXPR_UNARY_DEREFERENCE:
1524 return is_address_constant(expression->unary.value);
1527 type_t *base_type = skip_typeref(expression->select.compound->base.type);
1528 if(is_type_pointer(base_type)) {
1530 return is_address_constant(expression->select.compound);
1532 return is_object_with_linker_constant_address(expression->select.compound);
1536 case EXPR_ARRAY_ACCESS:
1537 return is_constant_expression(expression->array_access.index)
1538 && is_address_constant(expression->array_access.array_ref);
1540 case EXPR_REFERENCE: {
1541 declaration_t *declaration = expression->reference.declaration;
1542 switch((storage_class_tag_t) declaration->storage_class) {
1543 case STORAGE_CLASS_NONE:
1544 case STORAGE_CLASS_EXTERN:
1545 case STORAGE_CLASS_STATIC:
1557 bool is_address_constant(const expression_t *expression)
1559 switch(expression->kind) {
1560 case EXPR_UNARY_TAKE_ADDRESS:
1561 return is_object_with_linker_constant_address(expression->unary.value);
1563 case EXPR_UNARY_DEREFERENCE: {
1565 = revert_automatic_type_conversion(expression->unary.value);
1566 /* dereferencing a function is a NOP */
1567 if(is_type_function(real_type)) {
1568 return is_address_constant(expression->unary.value);
1574 case EXPR_UNARY_CAST: {
1575 type_t *dest = skip_typeref(expression->base.type);
1576 if (!is_type_pointer(dest) &&
1577 ! (dest->kind == TYPE_ATOMIC
1578 && (get_atomic_type_flags(dest->atomic.akind) & ATOMIC_TYPE_FLAG_INTEGER)
1579 && (get_atomic_type_size(dest->atomic.akind) >= get_atomic_type_size(get_intptr_kind()))))
1582 return (is_constant_expression(expression->unary.value)
1583 || is_address_constant(expression->unary.value));
1586 case EXPR_BINARY_ADD:
1587 case EXPR_BINARY_SUB: {
1588 expression_t *left = expression->binary.left;
1589 expression_t *right = expression->binary.right;
1591 if(is_type_pointer(skip_typeref(left->base.type))) {
1592 return is_address_constant(left) && is_constant_expression(right);
1593 } else if(is_type_pointer(skip_typeref(right->base.type))) {
1594 return is_constant_expression(left) && is_address_constant(right);
1600 case EXPR_REFERENCE: {
1601 declaration_t *declaration = expression->reference.declaration;
1602 type_t *type = skip_typeref(declaration->type);
1603 if(is_type_function(type))
1605 if(is_type_array(type)) {
1606 return is_object_with_linker_constant_address(expression);
1616 static bool is_builtin_const_call(const expression_t *expression)
1618 expression_t *function = expression->call.function;
1619 if (function->kind != EXPR_BUILTIN_SYMBOL) {
1623 symbol_t *symbol = function->builtin_symbol.symbol;
1625 switch (symbol->ID) {
1626 case T___builtin_huge_val:
1627 case T___builtin_nan:
1628 case T___builtin_nanf:
1629 case T___builtin_nand:
1636 static bool is_constant_pointer(const expression_t *expression)
1638 if (is_constant_expression(expression))
1641 switch (expression->kind) {
1643 return is_constant_pointer(expression->select.compound);
1644 case EXPR_UNARY_CAST:
1645 return is_constant_pointer(expression->unary.value);
1651 static bool is_object_with_constant_address(const expression_t *expression)
1653 switch(expression->kind) {
1655 expression_t *compound = expression->select.compound;
1656 type_t *compound_type = compound->base.type;
1657 compound_type = skip_typeref(compound_type);
1658 if(is_type_pointer(compound_type)) {
1659 return is_constant_pointer(compound);
1661 return is_object_with_constant_address(compound);
1664 case EXPR_ARRAY_ACCESS:
1665 return is_constant_pointer(expression->array_access.array_ref)
1666 && is_constant_expression(expression->array_access.index);
1667 case EXPR_UNARY_DEREFERENCE:
1668 return is_constant_pointer(expression->unary.value);
1674 bool is_constant_expression(const expression_t *expression)
1676 switch (expression->kind) {
1679 case EXPR_CHARACTER_CONSTANT:
1680 case EXPR_WIDE_CHARACTER_CONSTANT:
1681 case EXPR_STRING_LITERAL:
1682 case EXPR_WIDE_STRING_LITERAL:
1683 case EXPR_CLASSIFY_TYPE:
1687 case EXPR_BUILTIN_CONSTANT_P:
1688 case EXPR_LABEL_ADDRESS:
1692 type_t *type = expression->typeprop.type;
1694 type = expression->typeprop.tp_expression->base.type;
1696 type = skip_typeref(type);
1697 if (is_type_array(type) && type->array.is_vla)
1702 case EXPR_BUILTIN_SYMBOL:
1703 case EXPR_BUILTIN_PREFETCH:
1707 case EXPR_STATEMENT:
1708 case EXPR_UNARY_POSTFIX_INCREMENT:
1709 case EXPR_UNARY_POSTFIX_DECREMENT:
1710 case EXPR_UNARY_PREFIX_INCREMENT:
1711 case EXPR_UNARY_PREFIX_DECREMENT:
1712 case EXPR_UNARY_ASSUME: /* has VOID type */
1713 case EXPR_UNARY_DEREFERENCE:
1714 case EXPR_BINARY_ASSIGN:
1715 case EXPR_BINARY_MUL_ASSIGN:
1716 case EXPR_BINARY_DIV_ASSIGN:
1717 case EXPR_BINARY_MOD_ASSIGN:
1718 case EXPR_BINARY_ADD_ASSIGN:
1719 case EXPR_BINARY_SUB_ASSIGN:
1720 case EXPR_BINARY_SHIFTLEFT_ASSIGN:
1721 case EXPR_BINARY_SHIFTRIGHT_ASSIGN:
1722 case EXPR_BINARY_BITWISE_AND_ASSIGN:
1723 case EXPR_BINARY_BITWISE_XOR_ASSIGN:
1724 case EXPR_BINARY_BITWISE_OR_ASSIGN:
1725 case EXPR_BINARY_COMMA:
1728 case EXPR_UNARY_TAKE_ADDRESS:
1729 return is_object_with_constant_address(expression->unary.value);
1732 return is_builtin_const_call(expression);
1734 case EXPR_UNARY_NEGATE:
1735 case EXPR_UNARY_PLUS:
1736 case EXPR_UNARY_BITWISE_NEGATE:
1737 case EXPR_UNARY_NOT:
1738 return is_constant_expression(expression->unary.value);
1740 case EXPR_UNARY_CAST:
1741 case EXPR_UNARY_CAST_IMPLICIT:
1742 return is_type_arithmetic(skip_typeref(expression->base.type))
1743 && is_constant_expression(expression->unary.value);
1745 case EXPR_BINARY_ADD:
1746 case EXPR_BINARY_SUB:
1747 case EXPR_BINARY_MUL:
1748 case EXPR_BINARY_DIV:
1749 case EXPR_BINARY_MOD:
1750 case EXPR_BINARY_EQUAL:
1751 case EXPR_BINARY_NOTEQUAL:
1752 case EXPR_BINARY_LESS:
1753 case EXPR_BINARY_LESSEQUAL:
1754 case EXPR_BINARY_GREATER:
1755 case EXPR_BINARY_GREATEREQUAL:
1756 case EXPR_BINARY_BITWISE_AND:
1757 case EXPR_BINARY_BITWISE_OR:
1758 case EXPR_BINARY_BITWISE_XOR:
1759 case EXPR_BINARY_LOGICAL_AND:
1760 case EXPR_BINARY_LOGICAL_OR:
1761 case EXPR_BINARY_SHIFTLEFT:
1762 case EXPR_BINARY_SHIFTRIGHT:
1763 case EXPR_BINARY_BUILTIN_EXPECT:
1764 case EXPR_BINARY_ISGREATER:
1765 case EXPR_BINARY_ISGREATEREQUAL:
1766 case EXPR_BINARY_ISLESS:
1767 case EXPR_BINARY_ISLESSEQUAL:
1768 case EXPR_BINARY_ISLESSGREATER:
1769 case EXPR_BINARY_ISUNORDERED:
1770 return is_constant_expression(expression->binary.left)
1771 && is_constant_expression(expression->binary.right);
1773 case EXPR_COMPOUND_LITERAL:
1774 return is_constant_initializer(expression->compound_literal.initializer);
1776 case EXPR_CONDITIONAL: {
1777 expression_t *condition = expression->conditional.condition;
1778 if(!is_constant_expression(condition))
1781 long val = fold_constant(condition);
1783 return is_constant_expression(expression->conditional.true_expression);
1785 return is_constant_expression(expression->conditional.false_expression);
1788 case EXPR_ARRAY_ACCESS:
1789 return is_constant_expression(expression->array_access.array_ref)
1790 && is_constant_expression(expression->array_access.index);
1792 case EXPR_REFERENCE: {
1793 declaration_t *declaration = expression->reference.declaration;
1794 if(declaration->storage_class == STORAGE_CLASS_ENUM_ENTRY)
1806 panic("invalid expression found (is constant expression)");
1810 * Initialize the AST construction.
1814 obstack_init(&ast_obstack);
1822 obstack_free(&ast_obstack, NULL);
1826 * Set the output stream for the AST printer.
1828 * @param stream the output stream
1830 void ast_set_output(FILE *stream)
1833 type_set_output(stream);
1837 * Allocate an AST object of the given size.
1839 * @param size the size of the object to allocate
1841 * @return A new allocated object in the AST memeory space.
1843 void *(allocate_ast)(size_t size)
1845 return _allocate_ast(size);