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)
68 * Returns 1 if a given precedence level has right-to-left
69 * associativity, else 0.
71 * @param precedence the operator precedence
73 static int right_to_left(unsigned precedence)
77 case PREC_CONDITIONAL:
87 * Return the precedence of an expression given by its kind.
89 * @param kind the expression kind
91 static unsigned get_expression_precedence(expression_kind_t kind)
93 static const unsigned prec[] = {
94 [EXPR_UNKNOWN] = PREC_PRIMARY,
95 [EXPR_INVALID] = PREC_PRIMARY,
96 [EXPR_REFERENCE] = PREC_PRIMARY,
97 [EXPR_CHARACTER_CONSTANT] = PREC_PRIMARY,
98 [EXPR_WIDE_CHARACTER_CONSTANT] = PREC_PRIMARY,
99 [EXPR_CONST] = PREC_PRIMARY,
100 [EXPR_STRING_LITERAL] = PREC_PRIMARY,
101 [EXPR_WIDE_STRING_LITERAL] = PREC_PRIMARY,
102 [EXPR_COMPOUND_LITERAL] = PREC_UNARY,
103 [EXPR_CALL] = PREC_POSTFIX,
104 [EXPR_CONDITIONAL] = PREC_CONDITIONAL,
105 [EXPR_SELECT] = PREC_POSTFIX,
106 [EXPR_ARRAY_ACCESS] = PREC_POSTFIX,
107 [EXPR_SIZEOF] = PREC_UNARY,
108 [EXPR_CLASSIFY_TYPE] = PREC_UNARY,
109 [EXPR_ALIGNOF] = PREC_UNARY,
111 [EXPR_FUNCNAME] = PREC_PRIMARY,
112 [EXPR_BUILTIN_SYMBOL] = PREC_PRIMARY,
113 [EXPR_BUILTIN_CONSTANT_P] = PREC_PRIMARY,
114 [EXPR_BUILTIN_PREFETCH] = PREC_PRIMARY,
115 [EXPR_OFFSETOF] = PREC_PRIMARY,
116 [EXPR_VA_START] = PREC_PRIMARY,
117 [EXPR_VA_ARG] = PREC_PRIMARY,
118 [EXPR_STATEMENT] = PREC_PRIMARY,
119 [EXPR_LABEL_ADDRESS] = PREC_PRIMARY,
121 [EXPR_UNARY_NEGATE] = PREC_UNARY,
122 [EXPR_UNARY_PLUS] = PREC_UNARY,
123 [EXPR_UNARY_BITWISE_NEGATE] = PREC_UNARY,
124 [EXPR_UNARY_NOT] = PREC_UNARY,
125 [EXPR_UNARY_DEREFERENCE] = PREC_UNARY,
126 [EXPR_UNARY_TAKE_ADDRESS] = PREC_UNARY,
127 [EXPR_UNARY_POSTFIX_INCREMENT] = PREC_UNARY,
128 [EXPR_UNARY_POSTFIX_DECREMENT] = PREC_UNARY,
129 [EXPR_UNARY_PREFIX_INCREMENT] = PREC_UNARY,
130 [EXPR_UNARY_PREFIX_DECREMENT] = PREC_UNARY,
131 [EXPR_UNARY_CAST] = PREC_UNARY,
132 [EXPR_UNARY_CAST_IMPLICIT] = PREC_UNARY,
133 [EXPR_UNARY_ASSUME] = PREC_PRIMARY,
135 [EXPR_BINARY_ADD] = PREC_ADDITIVE,
136 [EXPR_BINARY_SUB] = PREC_ADDITIVE,
137 [EXPR_BINARY_MUL] = PREC_MULTIPLICATIVE,
138 [EXPR_BINARY_DIV] = PREC_MULTIPLICATIVE,
139 [EXPR_BINARY_MOD] = PREC_MULTIPLICATIVE,
140 [EXPR_BINARY_EQUAL] = PREC_EQUALITY,
141 [EXPR_BINARY_NOTEQUAL] = PREC_EQUALITY,
142 [EXPR_BINARY_LESS] = PREC_RELATIONAL,
143 [EXPR_BINARY_LESSEQUAL] = PREC_RELATIONAL,
144 [EXPR_BINARY_GREATER] = PREC_RELATIONAL,
145 [EXPR_BINARY_GREATEREQUAL] = PREC_RELATIONAL,
146 [EXPR_BINARY_BITWISE_AND] = PREC_AND,
147 [EXPR_BINARY_BITWISE_OR] = PREC_OR,
148 [EXPR_BINARY_BITWISE_XOR] = PREC_XOR,
149 [EXPR_BINARY_LOGICAL_AND] = PREC_LOGICAL_AND,
150 [EXPR_BINARY_LOGICAL_OR] = PREC_LOGICAL_OR,
151 [EXPR_BINARY_SHIFTLEFT] = PREC_SHIFT,
152 [EXPR_BINARY_SHIFTRIGHT] = PREC_SHIFT,
153 [EXPR_BINARY_ASSIGN] = PREC_ASSIGNMENT,
154 [EXPR_BINARY_MUL_ASSIGN] = PREC_ASSIGNMENT,
155 [EXPR_BINARY_DIV_ASSIGN] = PREC_ASSIGNMENT,
156 [EXPR_BINARY_MOD_ASSIGN] = PREC_ASSIGNMENT,
157 [EXPR_BINARY_ADD_ASSIGN] = PREC_ASSIGNMENT,
158 [EXPR_BINARY_SUB_ASSIGN] = PREC_ASSIGNMENT,
159 [EXPR_BINARY_SHIFTLEFT_ASSIGN] = PREC_ASSIGNMENT,
160 [EXPR_BINARY_SHIFTRIGHT_ASSIGN] = PREC_ASSIGNMENT,
161 [EXPR_BINARY_BITWISE_AND_ASSIGN] = PREC_ASSIGNMENT,
162 [EXPR_BINARY_BITWISE_XOR_ASSIGN] = PREC_ASSIGNMENT,
163 [EXPR_BINARY_BITWISE_OR_ASSIGN] = PREC_ASSIGNMENT,
164 [EXPR_BINARY_COMMA] = PREC_EXPRESSION,
166 [EXPR_BINARY_BUILTIN_EXPECT] = PREC_PRIMARY,
167 [EXPR_BINARY_ISGREATER] = PREC_PRIMARY,
168 [EXPR_BINARY_ISGREATEREQUAL] = PREC_PRIMARY,
169 [EXPR_BINARY_ISLESS] = PREC_PRIMARY,
170 [EXPR_BINARY_ISLESSEQUAL] = PREC_PRIMARY,
171 [EXPR_BINARY_ISLESSGREATER] = PREC_PRIMARY,
172 [EXPR_BINARY_ISUNORDERED] = PREC_PRIMARY
174 assert((unsigned)kind < (sizeof(prec)/sizeof(prec[0])));
175 unsigned res = prec[kind];
177 assert(res != PREC_BOTTOM);
182 * Print a constant expression.
184 * @param cnst the constant expression
186 static void print_const(const const_expression_t *cnst)
188 if(cnst->base.type == NULL)
191 const type_t *const type = skip_typeref(cnst->base.type);
193 if (is_type_integer(type)) {
194 fprintf(out, "%lld", cnst->v.int_value);
195 } else if (is_type_float(type)) {
196 long double const val = cnst->v.float_value;
198 /* ARG, no way to print long double */
199 fprintf(out, "%.20g", (double)val);
201 fprintf(out, "%.20Lg", val);
203 if (isfinite(val) && truncl(val) == val)
206 panic("unknown constant");
210 switch (type->atomic.akind) {
211 case ATOMIC_TYPE_UINT: suffix = "U"; break;
212 case ATOMIC_TYPE_LONG: suffix = "L"; break;
213 case ATOMIC_TYPE_ULONG: suffix = "UL"; break;
214 case ATOMIC_TYPE_LONGLONG: suffix = "LL"; break;
215 case ATOMIC_TYPE_ULONGLONG: suffix = "ULL"; break;
216 case ATOMIC_TYPE_FLOAT: suffix = "F"; break;
217 case ATOMIC_TYPE_LONG_DOUBLE: suffix = "L"; break;
219 default: suffix = NULL; break;
226 * Print a quoted string constant.
228 * @param string the string constant
229 * @param border the border char
230 * @param skip number of chars to skip at the end
232 static void print_quoted_string(const string_t *const string, char border, int skip)
235 const char *end = string->begin + string->size - skip;
236 for (const char *c = string->begin; c != end; ++c) {
241 case '\\': fputs("\\\\", out); break;
242 case '\a': fputs("\\a", out); break;
243 case '\b': fputs("\\b", out); break;
244 case '\f': fputs("\\f", out); break;
245 case '\n': fputs("\\n", out); break;
246 case '\r': fputs("\\r", out); break;
247 case '\t': fputs("\\t", out); break;
248 case '\v': fputs("\\v", out); break;
249 case '\?': fputs("\\?", out); break;
251 if (c_mode & _GNUC) {
252 fputs("\\e", out); break;
257 fprintf(out, "\\%03o", (unsigned)*c);
268 * Prints a wide string literal expression.
270 * @param wstr the wide string literal expression
271 * @param border the border char
272 * @param skip number of chars to skip at the end
274 static void print_quoted_wide_string(const wide_string_t *const wstr,
275 char border, int skip)
279 const wchar_rep_t *end = wstr->begin + wstr->size - skip;
280 for (const wchar_rep_t *c = wstr->begin; c != end; ++c) {
282 case L'\"': fputs("\\\"", out); break;
283 case L'\\': fputs("\\\\", out); break;
284 case L'\a': fputs("\\a", out); break;
285 case L'\b': fputs("\\b", out); break;
286 case L'\f': fputs("\\f", out); break;
287 case L'\n': fputs("\\n", out); break;
288 case L'\r': fputs("\\r", out); break;
289 case L'\t': fputs("\\t", out); break;
290 case L'\v': fputs("\\v", out); break;
291 case L'\?': fputs("\\?", out); break;
293 if (c_mode & _GNUC) {
294 fputs("\\e", out); break;
298 const unsigned tc = *c;
301 fprintf(out, "\\%03o", (char)*c);
305 } else if (tc < 0x800) {
306 fputc(0xC0 | (tc >> 6), out);
307 fputc(0x80 | (tc & 0x3F), out);
308 } else if (tc < 0x10000) {
309 fputc(0xE0 | ( tc >> 12), out);
310 fputc(0x80 | ((tc >> 6) & 0x3F), out);
311 fputc(0x80 | ( tc & 0x3F), out);
313 fputc(0xF0 | ( tc >> 18), out);
314 fputc(0x80 | ((tc >> 12) & 0x3F), out);
315 fputc(0x80 | ((tc >> 6) & 0x3F), out);
316 fputc(0x80 | ( tc & 0x3F), out);
325 * Print a constant character expression.
327 * @param cnst the constant character expression
329 static void print_character_constant(const const_expression_t *cnst)
331 print_quoted_string(&cnst->v.character, '\'', 0);
334 static void print_wide_character_constant(const const_expression_t *cnst)
336 print_quoted_wide_string(&cnst->v.wide_character, '\'', 0);
340 * Prints a string literal expression.
342 * @param string_literal the string literal expression
344 static void print_string_literal(
345 const string_literal_expression_t *string_literal)
347 print_quoted_string(&string_literal->value, '"', 1);
351 * Prints a predefined symbol.
353 static void print_funcname(
354 const funcname_expression_t *funcname)
357 switch(funcname->kind) {
358 case FUNCNAME_FUNCTION: s = (c_mode & _C99) ? "__func__" : "__FUNCTION__"; break;
359 case FUNCNAME_PRETTY_FUNCTION: s = "__PRETTY_FUNCTION__"; break;
360 case FUNCNAME_FUNCSIG: s = "__FUNCSIG__"; break;
361 case FUNCNAME_FUNCDNAME: s = "__FUNCDNAME__"; break;
368 static void print_wide_string_literal(
369 const wide_string_literal_expression_t *const wstr)
371 print_quoted_wide_string(&wstr->value, '"', 1);
374 static void print_compound_literal(
375 const compound_literal_expression_t *expression)
378 print_type(expression->type);
380 print_initializer(expression->initializer);
384 * Prints a call expression.
386 * @param call the call expression
388 static void print_call_expression(const call_expression_t *call)
390 unsigned prec = get_expression_precedence(call->base.kind);
391 print_expression_prec(call->function, prec);
393 call_argument_t *argument = call->arguments;
395 while(argument != NULL) {
401 print_expression_prec(argument->expression, PREC_ASSIGNMENT);
403 argument = argument->next;
409 * Prints a binary expression.
411 * @param binexpr the binary expression
413 static void print_binary_expression(const binary_expression_t *binexpr)
415 unsigned prec = get_expression_precedence(binexpr->base.kind);
416 int r2l = right_to_left(prec);
418 if(binexpr->base.kind == EXPR_BINARY_BUILTIN_EXPECT) {
419 fputs("__builtin_expect(", out);
420 print_expression_prec(binexpr->left, prec);
422 print_expression_prec(binexpr->right, prec);
427 print_expression_prec(binexpr->left, prec + r2l);
429 switch (binexpr->base.kind) {
430 case EXPR_BINARY_COMMA: op = ", "; break;
431 case EXPR_BINARY_ASSIGN: op = " = "; break;
432 case EXPR_BINARY_ADD: op = " + "; break;
433 case EXPR_BINARY_SUB: op = " - "; break;
434 case EXPR_BINARY_MUL: op = " * "; break;
435 case EXPR_BINARY_MOD: op = " % "; break;
436 case EXPR_BINARY_DIV: op = " / "; break;
437 case EXPR_BINARY_BITWISE_OR: op = " | "; break;
438 case EXPR_BINARY_BITWISE_AND: op = " & "; break;
439 case EXPR_BINARY_BITWISE_XOR: op = " ^ "; break;
440 case EXPR_BINARY_LOGICAL_OR: op = " || "; break;
441 case EXPR_BINARY_LOGICAL_AND: op = " && "; break;
442 case EXPR_BINARY_NOTEQUAL: op = " != "; break;
443 case EXPR_BINARY_EQUAL: op = " == "; break;
444 case EXPR_BINARY_LESS: op = " < "; break;
445 case EXPR_BINARY_LESSEQUAL: op = " <= "; break;
446 case EXPR_BINARY_GREATER: op = " > "; break;
447 case EXPR_BINARY_GREATEREQUAL: op = " >= "; break;
448 case EXPR_BINARY_SHIFTLEFT: op = " << "; break;
449 case EXPR_BINARY_SHIFTRIGHT: op = " >> "; break;
451 case EXPR_BINARY_ADD_ASSIGN: op = " += "; break;
452 case EXPR_BINARY_SUB_ASSIGN: op = " -= "; break;
453 case EXPR_BINARY_MUL_ASSIGN: op = " *= "; break;
454 case EXPR_BINARY_MOD_ASSIGN: op = " %= "; break;
455 case EXPR_BINARY_DIV_ASSIGN: op = " /= "; break;
456 case EXPR_BINARY_BITWISE_OR_ASSIGN: op = " |= "; break;
457 case EXPR_BINARY_BITWISE_AND_ASSIGN: op = " &= "; break;
458 case EXPR_BINARY_BITWISE_XOR_ASSIGN: op = " ^= "; break;
459 case EXPR_BINARY_SHIFTLEFT_ASSIGN: op = " <<= "; break;
460 case EXPR_BINARY_SHIFTRIGHT_ASSIGN: op = " >>= "; break;
461 default: panic("invalid binexpression found");
464 print_expression_prec(binexpr->right, prec - r2l);
468 * Prints an unary expression.
470 * @param unexpr the unary expression
472 static void print_unary_expression(const unary_expression_t *unexpr)
474 unsigned prec = get_expression_precedence(unexpr->base.kind);
475 switch(unexpr->base.kind) {
476 case EXPR_UNARY_NEGATE: fputc('-', out); break;
477 case EXPR_UNARY_PLUS: fputc('+', out); break;
478 case EXPR_UNARY_NOT: fputc('!', out); break;
479 case EXPR_UNARY_BITWISE_NEGATE: fputc('~', out); break;
480 case EXPR_UNARY_PREFIX_INCREMENT: fputs("++", out); break;
481 case EXPR_UNARY_PREFIX_DECREMENT: fputs("--", out); break;
482 case EXPR_UNARY_DEREFERENCE: fputc('*', out); break;
483 case EXPR_UNARY_TAKE_ADDRESS: fputc('&', out); break;
485 case EXPR_UNARY_POSTFIX_INCREMENT:
486 print_expression_prec(unexpr->value, prec);
489 case EXPR_UNARY_POSTFIX_DECREMENT:
490 print_expression_prec(unexpr->value, prec);
493 case EXPR_UNARY_CAST_IMPLICIT:
494 case EXPR_UNARY_CAST:
496 print_type(unexpr->base.type);
499 case EXPR_UNARY_ASSUME:
500 fputs("__assume(", out);
501 print_expression_prec(unexpr->value, PREC_ASSIGNMENT);
505 panic("invalid unary expression found");
507 print_expression_prec(unexpr->value, prec);
511 * Prints a reference expression.
513 * @param ref the reference expression
515 static void print_reference_expression(const reference_expression_t *ref)
517 fputs(ref->declaration->symbol->string, out);
521 * Prints a label address expression.
523 * @param ref the reference expression
525 static void print_label_address_expression(const label_address_expression_t *le)
527 fprintf(out, "&&%s", le->declaration->symbol->string);
531 * Prints an array expression.
533 * @param expression the array expression
535 static void print_array_expression(const array_access_expression_t *expression)
537 unsigned prec = get_expression_precedence(expression->base.kind);
538 if(!expression->flipped) {
539 print_expression_prec(expression->array_ref, prec);
541 print_expression(expression->index);
544 print_expression_prec(expression->index, prec);
546 print_expression(expression->array_ref);
552 * Prints a typeproperty expression (sizeof or __alignof__).
554 * @param expression the type property expression
556 static void print_typeprop_expression(const typeprop_expression_t *expression)
558 if (expression->base.kind == EXPR_SIZEOF) {
559 fputs("sizeof", out);
561 assert(expression->base.kind == EXPR_ALIGNOF);
562 fputs("__alignof__", out);
564 if(expression->tp_expression != NULL) {
565 /* always print the '()' here, sizeof x is right but unusual */
567 print_expression(expression->tp_expression);
571 print_type(expression->type);
577 * Prints an builtin symbol.
579 * @param expression the builtin symbol expression
581 static void print_builtin_symbol(const builtin_symbol_expression_t *expression)
583 fputs(expression->symbol->string, out);
587 * Prints a builtin constant expression.
589 * @param expression the builtin constant expression
591 static void print_builtin_constant(const builtin_constant_expression_t *expression)
593 fputs("__builtin_constant_p(", out);
594 print_expression_prec(expression->value, PREC_ASSIGNMENT);
599 * Prints a builtin prefetch expression.
601 * @param expression the builtin prefetch expression
603 static void print_builtin_prefetch(const builtin_prefetch_expression_t *expression)
605 fputs("__builtin_prefetch(", out);
606 print_expression_prec(expression->adr, PREC_ASSIGNMENT);
607 if (expression->rw) {
609 print_expression_prec(expression->rw, PREC_ASSIGNMENT);
611 if (expression->locality) {
613 print_expression_prec(expression->locality, PREC_ASSIGNMENT);
619 * Prints a conditional expression.
621 * @param expression the conditional expression
623 static void print_conditional(const conditional_expression_t *expression)
625 print_expression_prec(expression->condition, PREC_LOGICAL_OR);
627 if (expression->true_expression != NULL) {
628 print_expression_prec(expression->true_expression, PREC_EXPRESSION);
633 print_expression_prec(expression->false_expression, PREC_CONDITIONAL);
637 * Prints a va_start expression.
639 * @param expression the va_start expression
641 static void print_va_start(const va_start_expression_t *const expression)
643 fputs("__builtin_va_start(", out);
644 print_expression_prec(expression->ap, PREC_ASSIGNMENT);
646 fputs(expression->parameter->symbol->string, out);
651 * Prints a va_arg expression.
653 * @param expression the va_arg expression
655 static void print_va_arg(const va_arg_expression_t *expression)
657 fputs("__builtin_va_arg(", out);
658 print_expression_prec(expression->ap, PREC_ASSIGNMENT);
660 print_type(expression->base.type);
665 * Prints a select expression (. or ->).
667 * @param expression the select expression
669 static void print_select(const select_expression_t *expression)
671 unsigned prec = get_expression_precedence(expression->base.kind);
672 print_expression_prec(expression->compound, prec);
673 if(is_type_pointer(skip_typeref(expression->compound->base.type))) {
678 fputs(expression->compound_entry->symbol->string, out);
682 * Prints a type classify expression.
684 * @param expr the type classify expression
686 static void print_classify_type_expression(
687 const classify_type_expression_t *const expr)
689 fputs("__builtin_classify_type(", out);
690 print_expression_prec(expr->type_expression, PREC_ASSIGNMENT);
695 * Prints a designator.
697 * @param designator the designator
699 static void print_designator(const designator_t *designator)
701 for ( ; designator != NULL; designator = designator->next) {
702 if (designator->symbol == NULL) {
704 print_expression(designator->array_index);
708 fputs(designator->symbol->string, out);
714 * Prints an offsetof expression.
716 * @param expression the offset expression
718 static void print_offsetof_expression(const offsetof_expression_t *expression)
720 fputs("__builtin_offsetof", out);
722 print_type(expression->type);
724 print_designator(expression->designator);
729 * Prints a statement expression.
731 * @param expression the statement expression
733 static void print_statement_expression(const statement_expression_t *expression)
736 print_statement(expression->statement);
741 * Prints an expression with parenthesis if needed.
743 * @param expression the expression to print
744 * @param top_prec the precedence of the user of this expression.
746 static void print_expression_prec(const expression_t *expression, unsigned top_prec)
748 if (expression->kind == EXPR_UNARY_CAST_IMPLICIT && !print_implicit_casts) {
749 expression = expression->unary.value;
751 unsigned prec = get_expression_precedence(expression->base.kind);
752 if (print_parenthesis && top_prec != PREC_BOTTOM)
756 switch(expression->kind) {
759 fputs("$invalid expression$", out);
761 case EXPR_CHARACTER_CONSTANT:
762 print_character_constant(&expression->conste);
764 case EXPR_WIDE_CHARACTER_CONSTANT:
765 print_wide_character_constant(&expression->conste);
768 print_const(&expression->conste);
771 print_funcname(&expression->funcname);
773 case EXPR_STRING_LITERAL:
774 print_string_literal(&expression->string);
776 case EXPR_WIDE_STRING_LITERAL:
777 print_wide_string_literal(&expression->wide_string);
779 case EXPR_COMPOUND_LITERAL:
780 print_compound_literal(&expression->compound_literal);
783 print_call_expression(&expression->call);
786 print_binary_expression(&expression->binary);
789 print_reference_expression(&expression->reference);
791 case EXPR_ARRAY_ACCESS:
792 print_array_expression(&expression->array_access);
794 case EXPR_LABEL_ADDRESS:
795 print_label_address_expression(&expression->label_address);
798 print_unary_expression(&expression->unary);
802 print_typeprop_expression(&expression->typeprop);
804 case EXPR_BUILTIN_SYMBOL:
805 print_builtin_symbol(&expression->builtin_symbol);
807 case EXPR_BUILTIN_CONSTANT_P:
808 print_builtin_constant(&expression->builtin_constant);
810 case EXPR_BUILTIN_PREFETCH:
811 print_builtin_prefetch(&expression->builtin_prefetch);
813 case EXPR_CONDITIONAL:
814 print_conditional(&expression->conditional);
817 print_va_start(&expression->va_starte);
820 print_va_arg(&expression->va_arge);
823 print_select(&expression->select);
825 case EXPR_CLASSIFY_TYPE:
826 print_classify_type_expression(&expression->classify_type);
829 print_offsetof_expression(&expression->offsetofe);
832 print_statement_expression(&expression->statement);
837 fprintf(out, "some expression of type %d", (int) expression->kind);
845 * Print an compound statement.
847 * @param block the compound statement
849 static void print_compound_statement(const compound_statement_t *block)
854 statement_t *statement = block->statements;
855 while (statement != NULL) {
856 if (statement->base.kind == STATEMENT_CASE_LABEL)
858 if (statement->kind != STATEMENT_LABEL)
860 print_statement(statement);
862 statement = statement->base.next;
870 * Print a return statement.
872 * @param statement the return statement
874 static void print_return_statement(const return_statement_t *statement)
876 fputs("return ", out);
877 if(statement->value != NULL)
878 print_expression(statement->value);
883 * Print an expression statement.
885 * @param statement the expression statement
887 static void print_expression_statement(const expression_statement_t *statement)
889 print_expression(statement->expression);
894 * Print a goto statement.
896 * @param statement the goto statement
898 static void print_goto_statement(const goto_statement_t *statement)
901 if (statement->expression != NULL) {
903 print_expression(statement->expression);
905 fputs(statement->label->symbol->string, out);
911 * Print a label statement.
913 * @param statement the label statement
915 static void print_label_statement(const label_statement_t *statement)
917 fprintf(out, "%s:\n", statement->label->symbol->string);
919 print_statement(statement->statement);
923 * Print an if statement.
925 * @param statement the if statement
927 static void print_if_statement(const if_statement_t *statement)
930 print_expression(statement->condition);
932 print_statement(statement->true_statement);
934 if(statement->false_statement != NULL) {
937 print_statement(statement->false_statement);
942 * Print a switch statement.
944 * @param statement the switch statement
946 static void print_switch_statement(const switch_statement_t *statement)
948 fputs("switch (", out);
949 print_expression(statement->expression);
951 print_statement(statement->body);
955 * Print a case label (including the default label).
957 * @param statement the case label statement
959 static void print_case_label(const case_label_statement_t *statement)
961 if(statement->expression == NULL) {
962 fputs("default:\n", out);
965 print_expression(statement->expression);
966 if (statement->end_range != NULL) {
968 print_expression(statement->end_range);
973 if(statement->statement != NULL) {
974 if (statement->statement->base.kind == STATEMENT_CASE_LABEL) {
978 print_statement(statement->statement);
983 * Print a declaration statement.
985 * @param statement the statement
987 static void print_declaration_statement(
988 const declaration_statement_t *statement)
991 declaration_t *declaration = statement->declarations_begin;
993 if (declaration->namespc == NAMESPACE_LOCAL_LABEL) {
994 fputs("__label__ ", out);
996 declaration != statement->declarations_end->next;
997 declaration = declaration->next) {
1003 fputs(declaration->symbol->string, out);
1008 declaration != statement->declarations_end->next;
1009 declaration = declaration->next) {
1010 if (declaration->storage_class == STORAGE_CLASS_ENUM_ENTRY)
1012 if (declaration->implicit)
1020 print_declaration(declaration);
1027 * Print a while statement.
1029 * @param statement the statement
1031 static void print_while_statement(const while_statement_t *statement)
1033 fputs("while (", out);
1034 print_expression(statement->condition);
1036 print_statement(statement->body);
1040 * Print a do-while statement.
1042 * @param statement the statement
1044 static void print_do_while_statement(const do_while_statement_t *statement)
1047 print_statement(statement->body);
1049 fputs("while (", out);
1050 print_expression(statement->condition);
1055 * Print a for statement.
1057 * @param statement the statement
1059 static void print_for_statement(const for_statement_t *statement)
1061 fputs("for (", out);
1062 declaration_t *decl = statement->scope.declarations;
1063 while (decl != NULL && decl->implicit)
1066 assert(statement->initialisation == NULL);
1067 print_declaration(decl);
1068 if (decl->next != NULL) {
1069 panic("multiple declarations in for statement not supported yet");
1073 if(statement->initialisation) {
1074 print_expression(statement->initialisation);
1078 if(statement->condition != NULL) {
1079 print_expression(statement->condition);
1082 if(statement->step != NULL) {
1083 print_expression(statement->step);
1086 print_statement(statement->body);
1090 * Print assembler arguments.
1092 * @param arguments the arguments
1094 static void print_asm_arguments(asm_argument_t *arguments)
1096 asm_argument_t *argument = arguments;
1097 for( ; argument != NULL; argument = argument->next) {
1098 if(argument != arguments)
1101 if(argument->symbol) {
1102 fprintf(out, "[%s] ", argument->symbol->string);
1104 print_quoted_string(&argument->constraints, '"', 1);
1106 print_expression(argument->expression);
1112 * Print assembler clobbers.
1114 * @param clobbers the clobbers
1116 static void print_asm_clobbers(asm_clobber_t *clobbers)
1118 asm_clobber_t *clobber = clobbers;
1119 for( ; clobber != NULL; clobber = clobber->next) {
1120 if(clobber != clobbers)
1123 print_quoted_string(&clobber->clobber, '"', 1);
1128 * Print an assembler statement.
1130 * @param statement the statement
1132 static void print_asm_statement(const asm_statement_t *statement)
1135 if(statement->is_volatile) {
1136 fputs("volatile ", out);
1139 print_quoted_string(&statement->asm_text, '"', 1);
1140 if (statement->outputs == NULL &&
1141 statement->inputs == NULL &&
1142 statement->clobbers == NULL)
1143 goto end_of_print_asm_statement;
1146 print_asm_arguments(statement->outputs);
1147 if (statement->inputs == NULL && statement->clobbers == NULL)
1148 goto end_of_print_asm_statement;
1151 print_asm_arguments(statement->inputs);
1152 if (statement->clobbers == NULL)
1153 goto end_of_print_asm_statement;
1156 print_asm_clobbers(statement->clobbers);
1158 end_of_print_asm_statement:
1163 * Print a microsoft __try statement.
1165 * @param statement the statement
1167 static void print_ms_try_statement(const ms_try_statement_t *statement)
1169 fputs("__try ", out);
1170 print_statement(statement->try_statement);
1172 if(statement->except_expression != NULL) {
1173 fputs("__except(", out);
1174 print_expression(statement->except_expression);
1177 fputs("__finally ", out);
1179 print_statement(statement->final_statement);
1183 * Print a microsoft __leave statement.
1185 * @param statement the statement
1187 static void print_leave_statement(const leave_statement_t *statement)
1190 fputs("__leave;\n", out);
1194 * Print a statement.
1196 * @param statement the statement
1198 void print_statement(const statement_t *statement)
1200 switch (statement->kind) {
1201 case STATEMENT_EMPTY:
1204 case STATEMENT_COMPOUND:
1205 print_compound_statement(&statement->compound);
1207 case STATEMENT_RETURN:
1208 print_return_statement(&statement->returns);
1210 case STATEMENT_EXPRESSION:
1211 print_expression_statement(&statement->expression);
1213 case STATEMENT_LABEL:
1214 print_label_statement(&statement->label);
1216 case STATEMENT_GOTO:
1217 print_goto_statement(&statement->gotos);
1219 case STATEMENT_CONTINUE:
1220 fputs("continue;\n", out);
1222 case STATEMENT_BREAK:
1223 fputs("break;\n", out);
1226 print_if_statement(&statement->ifs);
1228 case STATEMENT_SWITCH:
1229 print_switch_statement(&statement->switchs);
1231 case STATEMENT_CASE_LABEL:
1232 print_case_label(&statement->case_label);
1234 case STATEMENT_DECLARATION:
1235 print_declaration_statement(&statement->declaration);
1237 case STATEMENT_WHILE:
1238 print_while_statement(&statement->whiles);
1240 case STATEMENT_DO_WHILE:
1241 print_do_while_statement(&statement->do_while);
1244 print_for_statement(&statement->fors);
1247 print_asm_statement(&statement->asms);
1249 case STATEMENT_MS_TRY:
1250 print_ms_try_statement(&statement->ms_try);
1252 case STATEMENT_LEAVE:
1253 print_leave_statement(&statement->leave);
1255 case STATEMENT_INVALID:
1256 fputs("$invalid statement$\n", out);
1262 * Print a storage class.
1264 * @param storage_class the storage class
1266 static void print_storage_class(storage_class_tag_t storage_class)
1268 switch(storage_class) {
1269 case STORAGE_CLASS_ENUM_ENTRY:
1270 case STORAGE_CLASS_NONE:
1272 case STORAGE_CLASS_TYPEDEF: fputs("typedef ", out); break;
1273 case STORAGE_CLASS_EXTERN: fputs("extern ", out); break;
1274 case STORAGE_CLASS_STATIC: fputs("static ", out); break;
1275 case STORAGE_CLASS_AUTO: fputs("auto ", out); break;
1276 case STORAGE_CLASS_REGISTER: fputs("register ", out); break;
1277 case STORAGE_CLASS_THREAD: fputs("__thread", out); break;
1278 case STORAGE_CLASS_THREAD_EXTERN: fputs("extern __thread", out); break;
1279 case STORAGE_CLASS_THREAD_STATIC: fputs("static __thread", out); break;
1284 * Print an initializer.
1286 * @param initializer the initializer
1288 void print_initializer(const initializer_t *initializer)
1290 if(initializer == NULL) {
1295 switch(initializer->kind) {
1296 case INITIALIZER_VALUE: {
1297 const initializer_value_t *value = &initializer->value;
1298 print_expression(value->value);
1301 case INITIALIZER_LIST: {
1302 assert(initializer->kind == INITIALIZER_LIST);
1304 const initializer_list_t *list = &initializer->list;
1306 for(size_t i = 0 ; i < list->len; ++i) {
1307 const initializer_t *sub_init = list->initializers[i];
1308 print_initializer(list->initializers[i]);
1309 if(i < list->len-1) {
1310 if(sub_init == NULL || sub_init->kind != INITIALIZER_DESIGNATOR)
1317 case INITIALIZER_STRING:
1318 print_quoted_string(&initializer->string.string, '"', 1);
1320 case INITIALIZER_WIDE_STRING:
1321 print_quoted_wide_string(&initializer->wide_string.string, '"', 1);
1323 case INITIALIZER_DESIGNATOR:
1324 print_designator(initializer->designator.designator);
1329 panic("invalid initializer kind found");
1333 * Print microsoft extended declaration modifiers.
1335 static void print_ms_modifiers(const declaration_t *declaration) {
1336 if((c_mode & _MS) == 0)
1339 decl_modifiers_t modifiers = declaration->modifiers;
1341 /* DM_FORCEINLINE handled outside. */
1342 if ((modifiers & ~DM_FORCEINLINE) != 0 ||
1343 declaration->alignment != 0 ||
1344 declaration->get_property_sym != NULL ||
1345 declaration->put_property_sym != NULL) {
1348 fputs("__declspec", out);
1349 if(modifiers & DM_DLLIMPORT) {
1350 fputs(next, out); next = ", "; fputs("dllimport", out);
1352 if(modifiers & DM_DLLEXPORT) {
1353 fputs(next, out); next = ", "; fputs("dllexport", out);
1355 if(modifiers & DM_THREAD) {
1356 fputs(next, out); next = ", "; fputs("thread", out);
1358 if(modifiers & DM_NAKED) {
1359 fputs(next, out); next = ", "; fputs("naked", out);
1361 if(modifiers & DM_THREAD) {
1362 fputs(next, out); next = ", "; fputs("thread", out);
1364 if(modifiers & DM_SELECTANY) {
1365 fputs(next, out); next = ", "; fputs("selectany", out);
1367 if(modifiers & DM_NOTHROW) {
1368 fputs(next, out); next = ", "; fputs("nothrow", out);
1370 if(modifiers & DM_NORETURN) {
1371 fputs(next, out); next = ", "; fputs("noreturn", out);
1373 if(modifiers & DM_NOINLINE) {
1374 fputs(next, out); next = ", "; fputs("noinline", out);
1376 if (modifiers & DM_DEPRECATED) {
1377 fputs(next, out); next = ", "; fputs("deprecated", out);
1378 if(declaration->deprecated_string != NULL)
1379 fprintf(out, "(\"%s\")", declaration->deprecated_string);
1381 if(declaration->alignment != 0) {
1382 fputs(next, out); next = ", "; fprintf(out, "align(%u)", declaration->alignment);
1384 if(modifiers & DM_RESTRICT) {
1385 fputs(next, out); next = ", "; fputs("restrict", out);
1387 if(modifiers & DM_NOALIAS) {
1388 fputs(next, out); next = ", "; fputs("noalias", out);
1390 if(declaration->get_property_sym != NULL || declaration->put_property_sym != NULL) {
1392 fputs(next, out); next = ", "; fputs("property(", out);
1393 if(declaration->get_property_sym != NULL) {
1394 fprintf(out, "get=%s", declaration->get_property_sym->string);
1397 if(declaration->put_property_sym != NULL)
1398 fprintf(out, "%sput=%s", comma, declaration->put_property_sym->string);
1406 * Print a declaration in the NORMAL namespace.
1408 * @param declaration the declaration
1410 static void print_normal_declaration(const declaration_t *declaration)
1412 print_storage_class((storage_class_tag_t) declaration->declared_storage_class);
1413 if (declaration->is_inline) {
1414 if (declaration->modifiers & DM_FORCEINLINE) {
1415 fputs("__forceinline ", out);
1416 } else if (declaration->modifiers & DM_MICROSOFT_INLINE) {
1417 fputs("__inline ", out);
1419 fputs("inline ", out);
1422 print_ms_modifiers(declaration);
1423 print_type_ext(declaration->type, declaration->symbol,
1424 &declaration->scope);
1426 if(declaration->type->kind == TYPE_FUNCTION) {
1427 if(declaration->init.statement != NULL) {
1430 print_statement(declaration->init.statement);
1433 } else if(declaration->init.initializer != NULL) {
1435 print_initializer(declaration->init.initializer);
1441 * Prints an expression.
1443 * @param expression the expression
1445 void print_expression(const expression_t *expression)
1447 print_expression_prec(expression, PREC_BOTTOM);
1451 * Print a declaration.
1453 * @param declaration the declaration
1455 void print_declaration(const declaration_t *declaration)
1457 if (declaration->namespc != NAMESPACE_NORMAL &&
1458 declaration->symbol == NULL)
1461 switch (declaration->namespc) {
1462 case NAMESPACE_NORMAL:
1463 print_normal_declaration(declaration);
1465 case NAMESPACE_STRUCT:
1466 fputs("struct ", out);
1467 fputs(declaration->symbol->string, out);
1468 if (declaration->init.complete) {
1470 print_compound_definition(declaration);
1474 case NAMESPACE_UNION:
1475 fputs("union ", out);
1476 fputs(declaration->symbol->string, out);
1477 if (declaration->init.complete) {
1479 print_compound_definition(declaration);
1483 case NAMESPACE_ENUM:
1484 fputs("enum ", out);
1485 fputs(declaration->symbol->string, out);
1486 if (declaration->init.complete) {
1488 print_enum_definition(declaration);
1496 * Print the AST of a translation unit.
1498 * @param unit the translation unit
1500 void print_ast(const translation_unit_t *unit)
1504 declaration_t *declaration = unit->scope.declarations;
1505 for( ; declaration != NULL; declaration = declaration->next) {
1506 if(declaration->storage_class == STORAGE_CLASS_ENUM_ENTRY)
1508 if(declaration->namespc != NAMESPACE_NORMAL &&
1509 declaration->symbol == NULL)
1511 if (declaration->implicit)
1515 print_declaration(declaration);
1520 bool is_constant_initializer(const initializer_t *initializer)
1522 switch(initializer->kind) {
1523 case INITIALIZER_STRING:
1524 case INITIALIZER_WIDE_STRING:
1525 case INITIALIZER_DESIGNATOR:
1528 case INITIALIZER_VALUE:
1529 return is_constant_expression(initializer->value.value);
1531 case INITIALIZER_LIST:
1532 for(size_t i = 0; i < initializer->list.len; ++i) {
1533 initializer_t *sub_initializer = initializer->list.initializers[i];
1534 if(!is_constant_initializer(sub_initializer))
1539 panic("invalid initializer kind found");
1542 static bool is_object_with_linker_constant_address(const expression_t *expression)
1544 switch(expression->kind) {
1545 case EXPR_UNARY_DEREFERENCE:
1546 return is_address_constant(expression->unary.value);
1549 type_t *base_type = skip_typeref(expression->select.compound->base.type);
1550 if(is_type_pointer(base_type)) {
1552 return is_address_constant(expression->select.compound);
1554 return is_object_with_linker_constant_address(expression->select.compound);
1558 case EXPR_ARRAY_ACCESS:
1559 return is_constant_expression(expression->array_access.index)
1560 && is_address_constant(expression->array_access.array_ref);
1562 case EXPR_REFERENCE: {
1563 declaration_t *declaration = expression->reference.declaration;
1564 switch((storage_class_tag_t) declaration->storage_class) {
1565 case STORAGE_CLASS_NONE:
1566 case STORAGE_CLASS_EXTERN:
1567 case STORAGE_CLASS_STATIC:
1579 bool is_address_constant(const expression_t *expression)
1581 switch(expression->kind) {
1582 case EXPR_UNARY_TAKE_ADDRESS:
1583 return is_object_with_linker_constant_address(expression->unary.value);
1585 case EXPR_UNARY_DEREFERENCE: {
1587 = revert_automatic_type_conversion(expression->unary.value);
1588 /* dereferencing a function is a NOP */
1589 if(is_type_function(real_type)) {
1590 return is_address_constant(expression->unary.value);
1596 case EXPR_UNARY_CAST: {
1597 type_t *dest = skip_typeref(expression->base.type);
1598 if (!is_type_pointer(dest) && (
1599 dest->kind != TYPE_ATOMIC ||
1600 !(get_atomic_type_flags(dest->atomic.akind) & ATOMIC_TYPE_FLAG_INTEGER) ||
1601 (get_atomic_type_size(dest->atomic.akind) < get_atomic_type_size(get_intptr_kind())
1605 return (is_constant_expression(expression->unary.value)
1606 || is_address_constant(expression->unary.value));
1609 case EXPR_BINARY_ADD:
1610 case EXPR_BINARY_SUB: {
1611 expression_t *left = expression->binary.left;
1612 expression_t *right = expression->binary.right;
1614 if(is_type_pointer(skip_typeref(left->base.type))) {
1615 return is_address_constant(left) && is_constant_expression(right);
1616 } else if(is_type_pointer(skip_typeref(right->base.type))) {
1617 return is_constant_expression(left) && is_address_constant(right);
1623 case EXPR_REFERENCE: {
1624 declaration_t *declaration = expression->reference.declaration;
1625 type_t *type = skip_typeref(declaration->type);
1626 if(is_type_function(type))
1628 if(is_type_array(type)) {
1629 return is_object_with_linker_constant_address(expression);
1631 /* Prevent stray errors */
1632 if (!is_type_valid(type))
1637 case EXPR_ARRAY_ACCESS: {
1638 type_t *const type =
1639 skip_typeref(revert_automatic_type_conversion(expression));
1641 is_type_array(type) &&
1642 is_constant_expression(expression->array_access.index) &&
1643 is_address_constant(expression->array_access.array_ref);
1651 static bool is_builtin_const_call(const expression_t *expression)
1653 expression_t *function = expression->call.function;
1654 if (function->kind != EXPR_BUILTIN_SYMBOL) {
1658 symbol_t *symbol = function->builtin_symbol.symbol;
1660 switch (symbol->ID) {
1661 case T___builtin_huge_val:
1662 case T___builtin_inf:
1663 case T___builtin_inff:
1664 case T___builtin_infl:
1665 case T___builtin_nan:
1666 case T___builtin_nanf:
1667 case T___builtin_nanl:
1674 static bool is_constant_pointer(const expression_t *expression)
1676 if (is_constant_expression(expression))
1679 switch (expression->kind) {
1680 case EXPR_UNARY_CAST:
1681 return is_constant_pointer(expression->unary.value);
1687 static bool is_object_with_constant_address(const expression_t *expression)
1689 switch(expression->kind) {
1691 expression_t *compound = expression->select.compound;
1692 type_t *compound_type = compound->base.type;
1693 compound_type = skip_typeref(compound_type);
1694 if(is_type_pointer(compound_type)) {
1695 return is_constant_pointer(compound);
1697 return is_object_with_constant_address(compound);
1701 case EXPR_ARRAY_ACCESS: {
1702 array_access_expression_t const* const array_access =
1703 &expression->array_access;
1705 is_constant_expression(array_access->index) && (
1706 is_object_with_constant_address(array_access->array_ref) ||
1707 is_constant_pointer(array_access->array_ref)
1711 case EXPR_UNARY_DEREFERENCE:
1712 return is_constant_pointer(expression->unary.value);
1718 bool is_constant_expression(const expression_t *expression)
1720 switch (expression->kind) {
1723 case EXPR_CHARACTER_CONSTANT:
1724 case EXPR_WIDE_CHARACTER_CONSTANT:
1725 case EXPR_STRING_LITERAL:
1726 case EXPR_WIDE_STRING_LITERAL:
1727 case EXPR_CLASSIFY_TYPE:
1731 case EXPR_BUILTIN_CONSTANT_P:
1732 case EXPR_LABEL_ADDRESS:
1736 type_t *type = expression->typeprop.type;
1738 type = expression->typeprop.tp_expression->base.type;
1740 type = skip_typeref(type);
1741 if (is_type_array(type) && type->array.is_vla)
1746 case EXPR_BUILTIN_SYMBOL:
1747 case EXPR_BUILTIN_PREFETCH:
1751 case EXPR_STATEMENT:
1752 case EXPR_UNARY_POSTFIX_INCREMENT:
1753 case EXPR_UNARY_POSTFIX_DECREMENT:
1754 case EXPR_UNARY_PREFIX_INCREMENT:
1755 case EXPR_UNARY_PREFIX_DECREMENT:
1756 case EXPR_UNARY_ASSUME: /* has VOID type */
1757 case EXPR_UNARY_DEREFERENCE:
1758 case EXPR_BINARY_ASSIGN:
1759 case EXPR_BINARY_MUL_ASSIGN:
1760 case EXPR_BINARY_DIV_ASSIGN:
1761 case EXPR_BINARY_MOD_ASSIGN:
1762 case EXPR_BINARY_ADD_ASSIGN:
1763 case EXPR_BINARY_SUB_ASSIGN:
1764 case EXPR_BINARY_SHIFTLEFT_ASSIGN:
1765 case EXPR_BINARY_SHIFTRIGHT_ASSIGN:
1766 case EXPR_BINARY_BITWISE_AND_ASSIGN:
1767 case EXPR_BINARY_BITWISE_XOR_ASSIGN:
1768 case EXPR_BINARY_BITWISE_OR_ASSIGN:
1769 case EXPR_BINARY_COMMA:
1770 case EXPR_ARRAY_ACCESS:
1773 case EXPR_UNARY_TAKE_ADDRESS:
1774 return is_object_with_constant_address(expression->unary.value);
1777 return is_builtin_const_call(expression);
1779 case EXPR_UNARY_NEGATE:
1780 case EXPR_UNARY_PLUS:
1781 case EXPR_UNARY_BITWISE_NEGATE:
1782 case EXPR_UNARY_NOT:
1783 return is_constant_expression(expression->unary.value);
1785 case EXPR_UNARY_CAST:
1786 case EXPR_UNARY_CAST_IMPLICIT:
1787 return is_type_arithmetic(skip_typeref(expression->base.type))
1788 && is_constant_expression(expression->unary.value);
1790 case EXPR_BINARY_ADD:
1791 case EXPR_BINARY_SUB:
1792 case EXPR_BINARY_MUL:
1793 case EXPR_BINARY_DIV:
1794 case EXPR_BINARY_MOD:
1795 case EXPR_BINARY_EQUAL:
1796 case EXPR_BINARY_NOTEQUAL:
1797 case EXPR_BINARY_LESS:
1798 case EXPR_BINARY_LESSEQUAL:
1799 case EXPR_BINARY_GREATER:
1800 case EXPR_BINARY_GREATEREQUAL:
1801 case EXPR_BINARY_BITWISE_AND:
1802 case EXPR_BINARY_BITWISE_OR:
1803 case EXPR_BINARY_BITWISE_XOR:
1804 case EXPR_BINARY_LOGICAL_AND:
1805 case EXPR_BINARY_LOGICAL_OR:
1806 case EXPR_BINARY_SHIFTLEFT:
1807 case EXPR_BINARY_SHIFTRIGHT:
1808 case EXPR_BINARY_BUILTIN_EXPECT:
1809 case EXPR_BINARY_ISGREATER:
1810 case EXPR_BINARY_ISGREATEREQUAL:
1811 case EXPR_BINARY_ISLESS:
1812 case EXPR_BINARY_ISLESSEQUAL:
1813 case EXPR_BINARY_ISLESSGREATER:
1814 case EXPR_BINARY_ISUNORDERED:
1815 return is_constant_expression(expression->binary.left)
1816 && is_constant_expression(expression->binary.right);
1818 case EXPR_COMPOUND_LITERAL:
1819 return is_constant_initializer(expression->compound_literal.initializer);
1821 case EXPR_CONDITIONAL: {
1822 expression_t *condition = expression->conditional.condition;
1823 if(!is_constant_expression(condition))
1826 long val = fold_constant(condition);
1828 return is_constant_expression(expression->conditional.true_expression);
1830 return is_constant_expression(expression->conditional.false_expression);
1833 case EXPR_REFERENCE: {
1834 declaration_t *declaration = expression->reference.declaration;
1835 if(declaration->storage_class == STORAGE_CLASS_ENUM_ENTRY)
1847 panic("invalid expression found (is constant expression)");
1851 * Initialize the AST construction.
1855 obstack_init(&ast_obstack);
1863 obstack_free(&ast_obstack, NULL);
1867 * Set the output stream for the AST printer.
1869 * @param stream the output stream
1871 void ast_set_output(FILE *stream)
1874 type_set_output(stream);
1878 * Allocate an AST object of the given size.
1880 * @param size the size of the object to allocate
1882 * @return A new allocated object in the AST memeory space.
1884 void *(allocate_ast)(size_t size)
1886 return _allocate_ast(size);