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,
134 [EXPR_UNARY_DELETE] = PREC_UNARY,
135 [EXPR_UNARY_DELETE_ARRAY] = PREC_UNARY,
136 [EXPR_UNARY_THROW] = PREC_ASSIGNMENT,
138 [EXPR_BINARY_ADD] = PREC_ADDITIVE,
139 [EXPR_BINARY_SUB] = PREC_ADDITIVE,
140 [EXPR_BINARY_MUL] = PREC_MULTIPLICATIVE,
141 [EXPR_BINARY_DIV] = PREC_MULTIPLICATIVE,
142 [EXPR_BINARY_MOD] = PREC_MULTIPLICATIVE,
143 [EXPR_BINARY_EQUAL] = PREC_EQUALITY,
144 [EXPR_BINARY_NOTEQUAL] = PREC_EQUALITY,
145 [EXPR_BINARY_LESS] = PREC_RELATIONAL,
146 [EXPR_BINARY_LESSEQUAL] = PREC_RELATIONAL,
147 [EXPR_BINARY_GREATER] = PREC_RELATIONAL,
148 [EXPR_BINARY_GREATEREQUAL] = PREC_RELATIONAL,
149 [EXPR_BINARY_BITWISE_AND] = PREC_AND,
150 [EXPR_BINARY_BITWISE_OR] = PREC_OR,
151 [EXPR_BINARY_BITWISE_XOR] = PREC_XOR,
152 [EXPR_BINARY_LOGICAL_AND] = PREC_LOGICAL_AND,
153 [EXPR_BINARY_LOGICAL_OR] = PREC_LOGICAL_OR,
154 [EXPR_BINARY_SHIFTLEFT] = PREC_SHIFT,
155 [EXPR_BINARY_SHIFTRIGHT] = PREC_SHIFT,
156 [EXPR_BINARY_ASSIGN] = PREC_ASSIGNMENT,
157 [EXPR_BINARY_MUL_ASSIGN] = PREC_ASSIGNMENT,
158 [EXPR_BINARY_DIV_ASSIGN] = PREC_ASSIGNMENT,
159 [EXPR_BINARY_MOD_ASSIGN] = PREC_ASSIGNMENT,
160 [EXPR_BINARY_ADD_ASSIGN] = PREC_ASSIGNMENT,
161 [EXPR_BINARY_SUB_ASSIGN] = PREC_ASSIGNMENT,
162 [EXPR_BINARY_SHIFTLEFT_ASSIGN] = PREC_ASSIGNMENT,
163 [EXPR_BINARY_SHIFTRIGHT_ASSIGN] = PREC_ASSIGNMENT,
164 [EXPR_BINARY_BITWISE_AND_ASSIGN] = PREC_ASSIGNMENT,
165 [EXPR_BINARY_BITWISE_XOR_ASSIGN] = PREC_ASSIGNMENT,
166 [EXPR_BINARY_BITWISE_OR_ASSIGN] = PREC_ASSIGNMENT,
167 [EXPR_BINARY_COMMA] = PREC_EXPRESSION,
169 [EXPR_BINARY_BUILTIN_EXPECT] = PREC_PRIMARY,
170 [EXPR_BINARY_ISGREATER] = PREC_PRIMARY,
171 [EXPR_BINARY_ISGREATEREQUAL] = PREC_PRIMARY,
172 [EXPR_BINARY_ISLESS] = PREC_PRIMARY,
173 [EXPR_BINARY_ISLESSEQUAL] = PREC_PRIMARY,
174 [EXPR_BINARY_ISLESSGREATER] = PREC_PRIMARY,
175 [EXPR_BINARY_ISUNORDERED] = PREC_PRIMARY
177 assert((unsigned)kind < (sizeof(prec)/sizeof(prec[0])));
178 unsigned res = prec[kind];
180 assert(res != PREC_BOTTOM);
181 /* we need the lowest bit for right-to-left precedence */
186 * Print a constant expression.
188 * @param cnst the constant expression
190 static void print_const(const const_expression_t *cnst)
192 if(cnst->base.type == NULL)
195 const type_t *const type = skip_typeref(cnst->base.type);
197 if (is_type_integer(type)) {
198 fprintf(out, "%lld", cnst->v.int_value);
199 } else if (is_type_float(type)) {
200 long double const val = cnst->v.float_value;
202 /* ARG, no way to print long double */
203 fprintf(out, "%.20g", (double)val);
205 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", (unsigned)*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, 2 * PREC_ASSIGNMENT);
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);
433 switch (binexpr->base.kind) {
434 case EXPR_BINARY_COMMA: op = ", "; break;
435 case EXPR_BINARY_ASSIGN: op = " = "; break;
436 case EXPR_BINARY_ADD: op = " + "; break;
437 case EXPR_BINARY_SUB: op = " - "; break;
438 case EXPR_BINARY_MUL: op = " * "; break;
439 case EXPR_BINARY_MOD: op = " % "; break;
440 case EXPR_BINARY_DIV: op = " / "; break;
441 case EXPR_BINARY_BITWISE_OR: op = " | "; break;
442 case EXPR_BINARY_BITWISE_AND: op = " & "; break;
443 case EXPR_BINARY_BITWISE_XOR: op = " ^ "; break;
444 case EXPR_BINARY_LOGICAL_OR: op = " || "; break;
445 case EXPR_BINARY_LOGICAL_AND: op = " && "; break;
446 case EXPR_BINARY_NOTEQUAL: op = " != "; break;
447 case EXPR_BINARY_EQUAL: op = " == "; break;
448 case EXPR_BINARY_LESS: op = " < "; break;
449 case EXPR_BINARY_LESSEQUAL: op = " <= "; break;
450 case EXPR_BINARY_GREATER: op = " > "; break;
451 case EXPR_BINARY_GREATEREQUAL: op = " >= "; break;
452 case EXPR_BINARY_SHIFTLEFT: op = " << "; break;
453 case EXPR_BINARY_SHIFTRIGHT: op = " >> "; break;
455 case EXPR_BINARY_ADD_ASSIGN: op = " += "; break;
456 case EXPR_BINARY_SUB_ASSIGN: op = " -= "; break;
457 case EXPR_BINARY_MUL_ASSIGN: op = " *= "; break;
458 case EXPR_BINARY_MOD_ASSIGN: op = " %= "; break;
459 case EXPR_BINARY_DIV_ASSIGN: op = " /= "; break;
460 case EXPR_BINARY_BITWISE_OR_ASSIGN: op = " |= "; break;
461 case EXPR_BINARY_BITWISE_AND_ASSIGN: op = " &= "; break;
462 case EXPR_BINARY_BITWISE_XOR_ASSIGN: op = " ^= "; break;
463 case EXPR_BINARY_SHIFTLEFT_ASSIGN: op = " <<= "; break;
464 case EXPR_BINARY_SHIFTRIGHT_ASSIGN: op = " >>= "; break;
465 default: panic("invalid binexpression found");
468 print_expression_prec(binexpr->right, prec - r2l);
472 * Prints an unary expression.
474 * @param unexpr the unary expression
476 static void print_unary_expression(const unary_expression_t *unexpr)
478 unsigned prec = get_expression_precedence(unexpr->base.kind);
479 switch(unexpr->base.kind) {
480 case EXPR_UNARY_NEGATE: fputc('-', out); break;
481 case EXPR_UNARY_PLUS: fputc('+', out); break;
482 case EXPR_UNARY_NOT: fputc('!', out); break;
483 case EXPR_UNARY_BITWISE_NEGATE: fputc('~', out); break;
484 case EXPR_UNARY_PREFIX_INCREMENT: fputs("++", out); break;
485 case EXPR_UNARY_PREFIX_DECREMENT: fputs("--", out); break;
486 case EXPR_UNARY_DEREFERENCE: fputc('*', out); break;
487 case EXPR_UNARY_TAKE_ADDRESS: fputc('&', out); break;
488 case EXPR_UNARY_DELETE: fputs("delete ", out); break;
489 case EXPR_UNARY_DELETE_ARRAY: fputs("delete [] ", 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, 2 * PREC_ASSIGNMENT);
511 case EXPR_UNARY_THROW:
512 if (unexpr->value == NULL) {
516 fputs("throw ", out);
520 panic("invalid unary expression found");
522 print_expression_prec(unexpr->value, prec);
526 * Prints a reference expression.
528 * @param ref the reference expression
530 static void print_reference_expression(const reference_expression_t *ref)
532 fputs(ref->declaration->symbol->string, out);
536 * Prints a label address expression.
538 * @param ref the reference expression
540 static void print_label_address_expression(const label_address_expression_t *le)
542 fprintf(out, "&&%s", le->declaration->symbol->string);
546 * Prints an array expression.
548 * @param expression the array expression
550 static void print_array_expression(const array_access_expression_t *expression)
552 unsigned prec = get_expression_precedence(expression->base.kind);
553 if(!expression->flipped) {
554 print_expression_prec(expression->array_ref, prec);
556 print_expression(expression->index);
559 print_expression_prec(expression->index, prec);
561 print_expression(expression->array_ref);
567 * Prints a typeproperty expression (sizeof or __alignof__).
569 * @param expression the type property expression
571 static void print_typeprop_expression(const typeprop_expression_t *expression)
573 if (expression->base.kind == EXPR_SIZEOF) {
574 fputs("sizeof", out);
576 assert(expression->base.kind == EXPR_ALIGNOF);
577 fputs("__alignof__", out);
579 if(expression->tp_expression != NULL) {
580 /* always print the '()' here, sizeof x is right but unusual */
582 print_expression(expression->tp_expression);
586 print_type(expression->type);
592 * Prints an builtin symbol.
594 * @param expression the builtin symbol expression
596 static void print_builtin_symbol(const builtin_symbol_expression_t *expression)
598 fputs(expression->symbol->string, out);
602 * Prints a builtin constant expression.
604 * @param expression the builtin constant expression
606 static void print_builtin_constant(const builtin_constant_expression_t *expression)
608 fputs("__builtin_constant_p(", out);
609 print_expression_prec(expression->value, 2 * PREC_ASSIGNMENT);
614 * Prints a builtin prefetch expression.
616 * @param expression the builtin prefetch expression
618 static void print_builtin_prefetch(const builtin_prefetch_expression_t *expression)
620 fputs("__builtin_prefetch(", out);
621 print_expression_prec(expression->adr, 2 * PREC_ASSIGNMENT);
622 if (expression->rw) {
624 print_expression_prec(expression->rw, 2 * PREC_ASSIGNMENT);
626 if (expression->locality) {
628 print_expression_prec(expression->locality, 2 * PREC_ASSIGNMENT);
634 * Prints a conditional expression.
636 * @param expression the conditional expression
638 static void print_conditional(const conditional_expression_t *expression)
640 print_expression_prec(expression->condition, 2 * PREC_LOGICAL_OR);
642 if (expression->true_expression != NULL) {
643 print_expression_prec(expression->true_expression, 2 * PREC_EXPRESSION);
648 print_expression_prec(expression->false_expression, 2 * PREC_CONDITIONAL);
652 * Prints a va_start expression.
654 * @param expression the va_start expression
656 static void print_va_start(const va_start_expression_t *const expression)
658 fputs("__builtin_va_start(", out);
659 print_expression_prec(expression->ap, 2 * PREC_ASSIGNMENT);
661 fputs(expression->parameter->symbol->string, out);
666 * Prints a va_arg expression.
668 * @param expression the va_arg expression
670 static void print_va_arg(const va_arg_expression_t *expression)
672 fputs("__builtin_va_arg(", out);
673 print_expression_prec(expression->ap, 2 * PREC_ASSIGNMENT);
675 print_type(expression->base.type);
680 * Prints a select expression (. or ->).
682 * @param expression the select expression
684 static void print_select(const select_expression_t *expression)
686 unsigned prec = get_expression_precedence(expression->base.kind);
687 print_expression_prec(expression->compound, prec);
688 if(is_type_pointer(skip_typeref(expression->compound->base.type))) {
693 fputs(expression->compound_entry->symbol->string, out);
697 * Prints a type classify expression.
699 * @param expr the type classify expression
701 static void print_classify_type_expression(
702 const classify_type_expression_t *const expr)
704 fputs("__builtin_classify_type(", out);
705 print_expression_prec(expr->type_expression, 2 * PREC_ASSIGNMENT);
710 * Prints a designator.
712 * @param designator the designator
714 static void print_designator(const designator_t *designator)
716 for ( ; designator != NULL; designator = designator->next) {
717 if (designator->symbol == NULL) {
719 print_expression(designator->array_index);
723 fputs(designator->symbol->string, out);
729 * Prints an offsetof expression.
731 * @param expression the offset expression
733 static void print_offsetof_expression(const offsetof_expression_t *expression)
735 fputs("__builtin_offsetof", out);
737 print_type(expression->type);
739 print_designator(expression->designator);
744 * Prints a statement expression.
746 * @param expression the statement expression
748 static void print_statement_expression(const statement_expression_t *expression)
751 print_statement(expression->statement);
756 * Prints an expression with parenthesis if needed.
758 * @param expression the expression to print
759 * @param top_prec the precedence of the user of this expression.
761 static void print_expression_prec(const expression_t *expression, unsigned top_prec)
763 if (expression->kind == EXPR_UNARY_CAST_IMPLICIT && !print_implicit_casts) {
764 expression = expression->unary.value;
766 unsigned prec = get_expression_precedence(expression->base.kind);
767 if (print_parenthesis && top_prec != PREC_BOTTOM)
771 switch(expression->kind) {
774 fputs("$invalid expression$", out);
776 case EXPR_CHARACTER_CONSTANT:
777 print_character_constant(&expression->conste);
779 case EXPR_WIDE_CHARACTER_CONSTANT:
780 print_wide_character_constant(&expression->conste);
783 print_const(&expression->conste);
786 print_funcname(&expression->funcname);
788 case EXPR_STRING_LITERAL:
789 print_string_literal(&expression->string);
791 case EXPR_WIDE_STRING_LITERAL:
792 print_wide_string_literal(&expression->wide_string);
794 case EXPR_COMPOUND_LITERAL:
795 print_compound_literal(&expression->compound_literal);
798 print_call_expression(&expression->call);
801 print_binary_expression(&expression->binary);
804 print_reference_expression(&expression->reference);
806 case EXPR_ARRAY_ACCESS:
807 print_array_expression(&expression->array_access);
809 case EXPR_LABEL_ADDRESS:
810 print_label_address_expression(&expression->label_address);
813 print_unary_expression(&expression->unary);
817 print_typeprop_expression(&expression->typeprop);
819 case EXPR_BUILTIN_SYMBOL:
820 print_builtin_symbol(&expression->builtin_symbol);
822 case EXPR_BUILTIN_CONSTANT_P:
823 print_builtin_constant(&expression->builtin_constant);
825 case EXPR_BUILTIN_PREFETCH:
826 print_builtin_prefetch(&expression->builtin_prefetch);
828 case EXPR_CONDITIONAL:
829 print_conditional(&expression->conditional);
832 print_va_start(&expression->va_starte);
835 print_va_arg(&expression->va_arge);
838 print_select(&expression->select);
840 case EXPR_CLASSIFY_TYPE:
841 print_classify_type_expression(&expression->classify_type);
844 print_offsetof_expression(&expression->offsetofe);
847 print_statement_expression(&expression->statement);
852 fprintf(out, "some expression of type %d", (int) expression->kind);
860 * Print an compound statement.
862 * @param block the compound statement
864 static void print_compound_statement(const compound_statement_t *block)
869 statement_t *statement = block->statements;
870 while (statement != NULL) {
871 if (statement->base.kind == STATEMENT_CASE_LABEL)
873 if (statement->kind != STATEMENT_LABEL)
875 print_statement(statement);
877 statement = statement->base.next;
885 * Print a return statement.
887 * @param statement the return statement
889 static void print_return_statement(const return_statement_t *statement)
891 fputs("return ", out);
892 if(statement->value != NULL)
893 print_expression(statement->value);
898 * Print an expression statement.
900 * @param statement the expression statement
902 static void print_expression_statement(const expression_statement_t *statement)
904 print_expression(statement->expression);
909 * Print a goto statement.
911 * @param statement the goto statement
913 static void print_goto_statement(const goto_statement_t *statement)
916 if (statement->expression != NULL) {
918 print_expression(statement->expression);
920 fputs(statement->label->symbol->string, out);
926 * Print a label statement.
928 * @param statement the label statement
930 static void print_label_statement(const label_statement_t *statement)
932 fprintf(out, "%s:\n", statement->label->symbol->string);
934 print_statement(statement->statement);
938 * Print an if statement.
940 * @param statement the if statement
942 static void print_if_statement(const if_statement_t *statement)
945 print_expression(statement->condition);
947 print_statement(statement->true_statement);
949 if(statement->false_statement != NULL) {
952 print_statement(statement->false_statement);
957 * Print a switch statement.
959 * @param statement the switch statement
961 static void print_switch_statement(const switch_statement_t *statement)
963 fputs("switch (", out);
964 print_expression(statement->expression);
966 print_statement(statement->body);
970 * Print a case label (including the default label).
972 * @param statement the case label statement
974 static void print_case_label(const case_label_statement_t *statement)
976 if(statement->expression == NULL) {
977 fputs("default:\n", out);
980 print_expression(statement->expression);
981 if (statement->end_range != NULL) {
983 print_expression(statement->end_range);
988 if(statement->statement != NULL) {
989 if (statement->statement->base.kind == STATEMENT_CASE_LABEL) {
993 print_statement(statement->statement);
998 * Print a declaration statement.
1000 * @param statement the statement
1002 static void print_declaration_statement(
1003 const declaration_statement_t *statement)
1006 declaration_t *declaration = statement->declarations_begin;
1008 if (declaration->namespc == NAMESPACE_LOCAL_LABEL) {
1009 fputs("__label__ ", out);
1011 declaration != statement->declarations_end->next;
1012 declaration = declaration->next) {
1018 fputs(declaration->symbol->string, out);
1023 declaration != statement->declarations_end->next;
1024 declaration = declaration->next) {
1025 if (declaration->storage_class == STORAGE_CLASS_ENUM_ENTRY)
1027 if (declaration->implicit)
1035 print_declaration(declaration);
1042 * Print a while statement.
1044 * @param statement the statement
1046 static void print_while_statement(const while_statement_t *statement)
1048 fputs("while (", out);
1049 print_expression(statement->condition);
1051 print_statement(statement->body);
1055 * Print a do-while statement.
1057 * @param statement the statement
1059 static void print_do_while_statement(const do_while_statement_t *statement)
1062 print_statement(statement->body);
1064 fputs("while (", out);
1065 print_expression(statement->condition);
1070 * Print a for statement.
1072 * @param statement the statement
1074 static void print_for_statement(const for_statement_t *statement)
1076 fputs("for (", out);
1077 declaration_t *decl = statement->scope.declarations;
1078 while (decl != NULL && decl->implicit)
1081 assert(statement->initialisation == NULL);
1082 print_declaration(decl);
1083 if (decl->next != NULL) {
1084 panic("multiple declarations in for statement not supported yet");
1088 if(statement->initialisation) {
1089 print_expression(statement->initialisation);
1093 if(statement->condition != NULL) {
1094 print_expression(statement->condition);
1097 if(statement->step != NULL) {
1098 print_expression(statement->step);
1101 print_statement(statement->body);
1105 * Print assembler arguments.
1107 * @param arguments the arguments
1109 static void print_asm_arguments(asm_argument_t *arguments)
1111 asm_argument_t *argument = arguments;
1112 for( ; argument != NULL; argument = argument->next) {
1113 if(argument != arguments)
1116 if(argument->symbol) {
1117 fprintf(out, "[%s] ", argument->symbol->string);
1119 print_quoted_string(&argument->constraints, '"', 1);
1121 print_expression(argument->expression);
1127 * Print assembler clobbers.
1129 * @param clobbers the clobbers
1131 static void print_asm_clobbers(asm_clobber_t *clobbers)
1133 asm_clobber_t *clobber = clobbers;
1134 for( ; clobber != NULL; clobber = clobber->next) {
1135 if(clobber != clobbers)
1138 print_quoted_string(&clobber->clobber, '"', 1);
1143 * Print an assembler statement.
1145 * @param statement the statement
1147 static void print_asm_statement(const asm_statement_t *statement)
1150 if(statement->is_volatile) {
1151 fputs("volatile ", out);
1154 print_quoted_string(&statement->asm_text, '"', 1);
1155 if (statement->outputs == NULL &&
1156 statement->inputs == NULL &&
1157 statement->clobbers == NULL)
1158 goto end_of_print_asm_statement;
1161 print_asm_arguments(statement->outputs);
1162 if (statement->inputs == NULL && statement->clobbers == NULL)
1163 goto end_of_print_asm_statement;
1166 print_asm_arguments(statement->inputs);
1167 if (statement->clobbers == NULL)
1168 goto end_of_print_asm_statement;
1171 print_asm_clobbers(statement->clobbers);
1173 end_of_print_asm_statement:
1178 * Print a microsoft __try statement.
1180 * @param statement the statement
1182 static void print_ms_try_statement(const ms_try_statement_t *statement)
1184 fputs("__try ", out);
1185 print_statement(statement->try_statement);
1187 if(statement->except_expression != NULL) {
1188 fputs("__except(", out);
1189 print_expression(statement->except_expression);
1192 fputs("__finally ", out);
1194 print_statement(statement->final_statement);
1198 * Print a microsoft __leave statement.
1200 * @param statement the statement
1202 static void print_leave_statement(const leave_statement_t *statement)
1205 fputs("__leave;\n", out);
1209 * Print a statement.
1211 * @param statement the statement
1213 void print_statement(const statement_t *statement)
1215 switch (statement->kind) {
1216 case STATEMENT_EMPTY:
1219 case STATEMENT_COMPOUND:
1220 print_compound_statement(&statement->compound);
1222 case STATEMENT_RETURN:
1223 print_return_statement(&statement->returns);
1225 case STATEMENT_EXPRESSION:
1226 print_expression_statement(&statement->expression);
1228 case STATEMENT_LABEL:
1229 print_label_statement(&statement->label);
1231 case STATEMENT_GOTO:
1232 print_goto_statement(&statement->gotos);
1234 case STATEMENT_CONTINUE:
1235 fputs("continue;\n", out);
1237 case STATEMENT_BREAK:
1238 fputs("break;\n", out);
1241 print_if_statement(&statement->ifs);
1243 case STATEMENT_SWITCH:
1244 print_switch_statement(&statement->switchs);
1246 case STATEMENT_CASE_LABEL:
1247 print_case_label(&statement->case_label);
1249 case STATEMENT_DECLARATION:
1250 print_declaration_statement(&statement->declaration);
1252 case STATEMENT_WHILE:
1253 print_while_statement(&statement->whiles);
1255 case STATEMENT_DO_WHILE:
1256 print_do_while_statement(&statement->do_while);
1259 print_for_statement(&statement->fors);
1262 print_asm_statement(&statement->asms);
1264 case STATEMENT_MS_TRY:
1265 print_ms_try_statement(&statement->ms_try);
1267 case STATEMENT_LEAVE:
1268 print_leave_statement(&statement->leave);
1270 case STATEMENT_INVALID:
1271 fputs("$invalid statement$\n", out);
1277 * Print a storage class.
1279 * @param storage_class the storage class
1281 static void print_storage_class(storage_class_tag_t storage_class)
1283 switch(storage_class) {
1284 case STORAGE_CLASS_ENUM_ENTRY:
1285 case STORAGE_CLASS_NONE:
1287 case STORAGE_CLASS_TYPEDEF: fputs("typedef ", out); break;
1288 case STORAGE_CLASS_EXTERN: fputs("extern ", out); break;
1289 case STORAGE_CLASS_STATIC: fputs("static ", out); break;
1290 case STORAGE_CLASS_AUTO: fputs("auto ", out); break;
1291 case STORAGE_CLASS_REGISTER: fputs("register ", out); break;
1292 case STORAGE_CLASS_THREAD: fputs("__thread", out); break;
1293 case STORAGE_CLASS_THREAD_EXTERN: fputs("extern __thread", out); break;
1294 case STORAGE_CLASS_THREAD_STATIC: fputs("static __thread", out); break;
1299 * Print an initializer.
1301 * @param initializer the initializer
1303 void print_initializer(const initializer_t *initializer)
1305 if(initializer == NULL) {
1310 switch(initializer->kind) {
1311 case INITIALIZER_VALUE: {
1312 const initializer_value_t *value = &initializer->value;
1313 print_expression(value->value);
1316 case INITIALIZER_LIST: {
1317 assert(initializer->kind == INITIALIZER_LIST);
1319 const initializer_list_t *list = &initializer->list;
1321 for(size_t i = 0 ; i < list->len; ++i) {
1322 const initializer_t *sub_init = list->initializers[i];
1323 print_initializer(list->initializers[i]);
1324 if(i < list->len-1) {
1325 if(sub_init == NULL || sub_init->kind != INITIALIZER_DESIGNATOR)
1332 case INITIALIZER_STRING:
1333 print_quoted_string(&initializer->string.string, '"', 1);
1335 case INITIALIZER_WIDE_STRING:
1336 print_quoted_wide_string(&initializer->wide_string.string, '"', 1);
1338 case INITIALIZER_DESIGNATOR:
1339 print_designator(initializer->designator.designator);
1344 panic("invalid initializer kind found");
1348 * Print microsoft extended declaration modifiers.
1350 static void print_ms_modifiers(const declaration_t *declaration) {
1351 if((c_mode & _MS) == 0)
1354 decl_modifiers_t modifiers = declaration->modifiers;
1356 /* DM_FORCEINLINE handled outside. */
1357 if ((modifiers & ~DM_FORCEINLINE) != 0 ||
1358 declaration->alignment != 0 ||
1359 declaration->get_property_sym != NULL ||
1360 declaration->put_property_sym != NULL) {
1363 fputs("__declspec", out);
1364 if(modifiers & DM_DLLIMPORT) {
1365 fputs(next, out); next = ", "; fputs("dllimport", out);
1367 if(modifiers & DM_DLLEXPORT) {
1368 fputs(next, out); next = ", "; fputs("dllexport", out);
1370 if(modifiers & DM_THREAD) {
1371 fputs(next, out); next = ", "; fputs("thread", out);
1373 if(modifiers & DM_NAKED) {
1374 fputs(next, out); next = ", "; fputs("naked", out);
1376 if(modifiers & DM_THREAD) {
1377 fputs(next, out); next = ", "; fputs("thread", out);
1379 if(modifiers & DM_SELECTANY) {
1380 fputs(next, out); next = ", "; fputs("selectany", out);
1382 if(modifiers & DM_NOTHROW) {
1383 fputs(next, out); next = ", "; fputs("nothrow", out);
1385 if(modifiers & DM_NORETURN) {
1386 fputs(next, out); next = ", "; fputs("noreturn", out);
1388 if(modifiers & DM_NOINLINE) {
1389 fputs(next, out); next = ", "; fputs("noinline", out);
1391 if (modifiers & DM_DEPRECATED) {
1392 fputs(next, out); next = ", "; fputs("deprecated", out);
1393 if(declaration->deprecated_string != NULL)
1394 fprintf(out, "(\"%s\")", declaration->deprecated_string);
1396 if(declaration->alignment != 0) {
1397 fputs(next, out); next = ", "; fprintf(out, "align(%u)", declaration->alignment);
1399 if(modifiers & DM_RESTRICT) {
1400 fputs(next, out); next = ", "; fputs("restrict", out);
1402 if(modifiers & DM_NOALIAS) {
1403 fputs(next, out); next = ", "; fputs("noalias", out);
1405 if(declaration->get_property_sym != NULL || declaration->put_property_sym != NULL) {
1407 fputs(next, out); next = ", "; fputs("property(", out);
1408 if(declaration->get_property_sym != NULL) {
1409 fprintf(out, "get=%s", declaration->get_property_sym->string);
1412 if(declaration->put_property_sym != NULL)
1413 fprintf(out, "%sput=%s", comma, declaration->put_property_sym->string);
1421 * Print a declaration in the NORMAL namespace.
1423 * @param declaration the declaration
1425 static void print_normal_declaration(const declaration_t *declaration)
1427 print_storage_class((storage_class_tag_t) declaration->declared_storage_class);
1428 if (declaration->is_inline) {
1429 if (declaration->modifiers & DM_FORCEINLINE) {
1430 fputs("__forceinline ", out);
1431 } else if (declaration->modifiers & DM_MICROSOFT_INLINE) {
1432 fputs("__inline ", out);
1434 fputs("inline ", out);
1437 print_ms_modifiers(declaration);
1438 print_type_ext(declaration->type, declaration->symbol,
1439 &declaration->scope);
1441 if(declaration->type->kind == TYPE_FUNCTION) {
1442 if(declaration->init.statement != NULL) {
1445 print_statement(declaration->init.statement);
1448 } else if(declaration->init.initializer != NULL) {
1450 print_initializer(declaration->init.initializer);
1456 * Prints an expression.
1458 * @param expression the expression
1460 void print_expression(const expression_t *expression)
1462 print_expression_prec(expression, 2 * PREC_BOTTOM);
1466 * Print a declaration.
1468 * @param declaration the declaration
1470 void print_declaration(const declaration_t *declaration)
1472 if (declaration->namespc != NAMESPACE_NORMAL &&
1473 declaration->symbol == NULL)
1476 switch (declaration->namespc) {
1477 case NAMESPACE_NORMAL:
1478 print_normal_declaration(declaration);
1480 case NAMESPACE_STRUCT:
1481 fputs("struct ", out);
1482 fputs(declaration->symbol->string, out);
1483 if (declaration->init.complete) {
1485 print_compound_definition(declaration);
1489 case NAMESPACE_UNION:
1490 fputs("union ", out);
1491 fputs(declaration->symbol->string, out);
1492 if (declaration->init.complete) {
1494 print_compound_definition(declaration);
1498 case NAMESPACE_ENUM:
1499 fputs("enum ", out);
1500 fputs(declaration->symbol->string, out);
1501 if (declaration->init.complete) {
1503 print_enum_definition(declaration);
1511 * Print the AST of a translation unit.
1513 * @param unit the translation unit
1515 void print_ast(const translation_unit_t *unit)
1519 declaration_t *declaration = unit->scope.declarations;
1520 for( ; declaration != NULL; declaration = declaration->next) {
1521 if(declaration->storage_class == STORAGE_CLASS_ENUM_ENTRY)
1523 if(declaration->namespc != NAMESPACE_NORMAL &&
1524 declaration->symbol == NULL)
1526 if (declaration->implicit)
1530 print_declaration(declaration);
1535 bool is_constant_initializer(const initializer_t *initializer)
1537 switch(initializer->kind) {
1538 case INITIALIZER_STRING:
1539 case INITIALIZER_WIDE_STRING:
1540 case INITIALIZER_DESIGNATOR:
1543 case INITIALIZER_VALUE:
1544 return is_constant_expression(initializer->value.value);
1546 case INITIALIZER_LIST:
1547 for(size_t i = 0; i < initializer->list.len; ++i) {
1548 initializer_t *sub_initializer = initializer->list.initializers[i];
1549 if(!is_constant_initializer(sub_initializer))
1554 panic("invalid initializer kind found");
1557 static bool is_object_with_linker_constant_address(const expression_t *expression)
1559 switch(expression->kind) {
1560 case EXPR_UNARY_DEREFERENCE:
1561 return is_address_constant(expression->unary.value);
1564 type_t *base_type = skip_typeref(expression->select.compound->base.type);
1565 if(is_type_pointer(base_type)) {
1567 return is_address_constant(expression->select.compound);
1569 return is_object_with_linker_constant_address(expression->select.compound);
1573 case EXPR_ARRAY_ACCESS:
1574 return is_constant_expression(expression->array_access.index)
1575 && is_address_constant(expression->array_access.array_ref);
1577 case EXPR_REFERENCE: {
1578 declaration_t *declaration = expression->reference.declaration;
1579 switch((storage_class_tag_t) declaration->storage_class) {
1580 case STORAGE_CLASS_NONE:
1581 case STORAGE_CLASS_EXTERN:
1582 case STORAGE_CLASS_STATIC:
1594 bool is_address_constant(const expression_t *expression)
1596 switch(expression->kind) {
1597 case EXPR_UNARY_TAKE_ADDRESS:
1598 return is_object_with_linker_constant_address(expression->unary.value);
1600 case EXPR_UNARY_DEREFERENCE: {
1602 = revert_automatic_type_conversion(expression->unary.value);
1603 /* dereferencing a function is a NOP */
1604 if(is_type_function(real_type)) {
1605 return is_address_constant(expression->unary.value);
1611 case EXPR_UNARY_CAST: {
1612 type_t *dest = skip_typeref(expression->base.type);
1613 if (!is_type_pointer(dest) && (
1614 dest->kind != TYPE_ATOMIC ||
1615 !(get_atomic_type_flags(dest->atomic.akind) & ATOMIC_TYPE_FLAG_INTEGER) ||
1616 (get_atomic_type_size(dest->atomic.akind) < get_atomic_type_size(get_intptr_kind())
1620 return (is_constant_expression(expression->unary.value)
1621 || is_address_constant(expression->unary.value));
1624 case EXPR_BINARY_ADD:
1625 case EXPR_BINARY_SUB: {
1626 expression_t *left = expression->binary.left;
1627 expression_t *right = expression->binary.right;
1629 if(is_type_pointer(skip_typeref(left->base.type))) {
1630 return is_address_constant(left) && is_constant_expression(right);
1631 } else if(is_type_pointer(skip_typeref(right->base.type))) {
1632 return is_constant_expression(left) && is_address_constant(right);
1638 case EXPR_REFERENCE: {
1639 declaration_t *declaration = expression->reference.declaration;
1640 type_t *type = skip_typeref(declaration->type);
1641 if(is_type_function(type))
1643 if(is_type_array(type)) {
1644 return is_object_with_linker_constant_address(expression);
1646 /* Prevent stray errors */
1647 if (!is_type_valid(type))
1652 case EXPR_ARRAY_ACCESS: {
1653 type_t *const type =
1654 skip_typeref(revert_automatic_type_conversion(expression));
1656 is_type_array(type) &&
1657 is_constant_expression(expression->array_access.index) &&
1658 is_address_constant(expression->array_access.array_ref);
1666 static bool is_builtin_const_call(const expression_t *expression)
1668 expression_t *function = expression->call.function;
1669 if (function->kind != EXPR_BUILTIN_SYMBOL) {
1673 symbol_t *symbol = function->builtin_symbol.symbol;
1675 switch (symbol->ID) {
1676 case T___builtin_huge_val:
1677 case T___builtin_inf:
1678 case T___builtin_inff:
1679 case T___builtin_infl:
1680 case T___builtin_nan:
1681 case T___builtin_nanf:
1682 case T___builtin_nanl:
1689 static bool is_constant_pointer(const expression_t *expression)
1691 if (is_constant_expression(expression))
1694 switch (expression->kind) {
1695 case EXPR_UNARY_CAST:
1696 return is_constant_pointer(expression->unary.value);
1702 static bool is_object_with_constant_address(const expression_t *expression)
1704 switch(expression->kind) {
1706 expression_t *compound = expression->select.compound;
1707 type_t *compound_type = compound->base.type;
1708 compound_type = skip_typeref(compound_type);
1709 if(is_type_pointer(compound_type)) {
1710 return is_constant_pointer(compound);
1712 return is_object_with_constant_address(compound);
1716 case EXPR_ARRAY_ACCESS: {
1717 array_access_expression_t const* const array_access =
1718 &expression->array_access;
1720 is_constant_expression(array_access->index) && (
1721 is_object_with_constant_address(array_access->array_ref) ||
1722 is_constant_pointer(array_access->array_ref)
1726 case EXPR_UNARY_DEREFERENCE:
1727 return is_constant_pointer(expression->unary.value);
1733 bool is_constant_expression(const expression_t *expression)
1735 switch (expression->kind) {
1738 case EXPR_CHARACTER_CONSTANT:
1739 case EXPR_WIDE_CHARACTER_CONSTANT:
1740 case EXPR_STRING_LITERAL:
1741 case EXPR_WIDE_STRING_LITERAL:
1742 case EXPR_CLASSIFY_TYPE:
1746 case EXPR_BUILTIN_CONSTANT_P:
1747 case EXPR_LABEL_ADDRESS:
1751 type_t *type = expression->typeprop.type;
1753 type = expression->typeprop.tp_expression->base.type;
1755 type = skip_typeref(type);
1756 if (is_type_array(type) && type->array.is_vla)
1761 case EXPR_BUILTIN_SYMBOL:
1762 case EXPR_BUILTIN_PREFETCH:
1766 case EXPR_STATEMENT:
1767 case EXPR_UNARY_POSTFIX_INCREMENT:
1768 case EXPR_UNARY_POSTFIX_DECREMENT:
1769 case EXPR_UNARY_PREFIX_INCREMENT:
1770 case EXPR_UNARY_PREFIX_DECREMENT:
1771 case EXPR_UNARY_ASSUME: /* has VOID type */
1772 case EXPR_UNARY_DEREFERENCE:
1773 case EXPR_UNARY_DELETE:
1774 case EXPR_UNARY_DELETE_ARRAY:
1775 case EXPR_UNARY_THROW:
1776 case EXPR_BINARY_ASSIGN:
1777 case EXPR_BINARY_MUL_ASSIGN:
1778 case EXPR_BINARY_DIV_ASSIGN:
1779 case EXPR_BINARY_MOD_ASSIGN:
1780 case EXPR_BINARY_ADD_ASSIGN:
1781 case EXPR_BINARY_SUB_ASSIGN:
1782 case EXPR_BINARY_SHIFTLEFT_ASSIGN:
1783 case EXPR_BINARY_SHIFTRIGHT_ASSIGN:
1784 case EXPR_BINARY_BITWISE_AND_ASSIGN:
1785 case EXPR_BINARY_BITWISE_XOR_ASSIGN:
1786 case EXPR_BINARY_BITWISE_OR_ASSIGN:
1787 case EXPR_BINARY_COMMA:
1788 case EXPR_ARRAY_ACCESS:
1791 case EXPR_UNARY_TAKE_ADDRESS:
1792 return is_object_with_constant_address(expression->unary.value);
1795 return is_builtin_const_call(expression);
1797 case EXPR_UNARY_NEGATE:
1798 case EXPR_UNARY_PLUS:
1799 case EXPR_UNARY_BITWISE_NEGATE:
1800 case EXPR_UNARY_NOT:
1801 return is_constant_expression(expression->unary.value);
1803 case EXPR_UNARY_CAST:
1804 case EXPR_UNARY_CAST_IMPLICIT:
1805 return is_type_arithmetic(skip_typeref(expression->base.type))
1806 && is_constant_expression(expression->unary.value);
1808 case EXPR_BINARY_ADD:
1809 case EXPR_BINARY_SUB:
1810 case EXPR_BINARY_MUL:
1811 case EXPR_BINARY_DIV:
1812 case EXPR_BINARY_MOD:
1813 case EXPR_BINARY_EQUAL:
1814 case EXPR_BINARY_NOTEQUAL:
1815 case EXPR_BINARY_LESS:
1816 case EXPR_BINARY_LESSEQUAL:
1817 case EXPR_BINARY_GREATER:
1818 case EXPR_BINARY_GREATEREQUAL:
1819 case EXPR_BINARY_BITWISE_AND:
1820 case EXPR_BINARY_BITWISE_OR:
1821 case EXPR_BINARY_BITWISE_XOR:
1822 case EXPR_BINARY_LOGICAL_AND:
1823 case EXPR_BINARY_LOGICAL_OR:
1824 case EXPR_BINARY_SHIFTLEFT:
1825 case EXPR_BINARY_SHIFTRIGHT:
1826 case EXPR_BINARY_BUILTIN_EXPECT:
1827 case EXPR_BINARY_ISGREATER:
1828 case EXPR_BINARY_ISGREATEREQUAL:
1829 case EXPR_BINARY_ISLESS:
1830 case EXPR_BINARY_ISLESSEQUAL:
1831 case EXPR_BINARY_ISLESSGREATER:
1832 case EXPR_BINARY_ISUNORDERED:
1833 return is_constant_expression(expression->binary.left)
1834 && is_constant_expression(expression->binary.right);
1836 case EXPR_COMPOUND_LITERAL:
1837 return is_constant_initializer(expression->compound_literal.initializer);
1839 case EXPR_CONDITIONAL: {
1840 expression_t *condition = expression->conditional.condition;
1841 if(!is_constant_expression(condition))
1844 long val = fold_constant(condition);
1846 return is_constant_expression(expression->conditional.true_expression);
1848 return is_constant_expression(expression->conditional.false_expression);
1851 case EXPR_REFERENCE: {
1852 declaration_t *declaration = expression->reference.declaration;
1853 if(declaration->storage_class == STORAGE_CLASS_ENUM_ENTRY)
1865 panic("invalid expression found (is constant expression)");
1869 * Initialize the AST construction.
1873 obstack_init(&ast_obstack);
1881 obstack_free(&ast_obstack, NULL);
1885 * Set the output stream for the AST printer.
1887 * @param stream the output stream
1889 void ast_set_output(FILE *stream)
1892 type_set_output(stream);
1896 * Allocate an AST object of the given size.
1898 * @param size the size of the object to allocate
1900 * @return A new allocated object in the AST memeory space.
1902 void *(allocate_ast)(size_t size)
1904 return _allocate_ast(size);