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_THROW] = PREC_ASSIGNMENT,
136 [EXPR_BINARY_ADD] = PREC_ADDITIVE,
137 [EXPR_BINARY_SUB] = PREC_ADDITIVE,
138 [EXPR_BINARY_MUL] = PREC_MULTIPLICATIVE,
139 [EXPR_BINARY_DIV] = PREC_MULTIPLICATIVE,
140 [EXPR_BINARY_MOD] = PREC_MULTIPLICATIVE,
141 [EXPR_BINARY_EQUAL] = PREC_EQUALITY,
142 [EXPR_BINARY_NOTEQUAL] = PREC_EQUALITY,
143 [EXPR_BINARY_LESS] = PREC_RELATIONAL,
144 [EXPR_BINARY_LESSEQUAL] = PREC_RELATIONAL,
145 [EXPR_BINARY_GREATER] = PREC_RELATIONAL,
146 [EXPR_BINARY_GREATEREQUAL] = PREC_RELATIONAL,
147 [EXPR_BINARY_BITWISE_AND] = PREC_AND,
148 [EXPR_BINARY_BITWISE_OR] = PREC_OR,
149 [EXPR_BINARY_BITWISE_XOR] = PREC_XOR,
150 [EXPR_BINARY_LOGICAL_AND] = PREC_LOGICAL_AND,
151 [EXPR_BINARY_LOGICAL_OR] = PREC_LOGICAL_OR,
152 [EXPR_BINARY_SHIFTLEFT] = PREC_SHIFT,
153 [EXPR_BINARY_SHIFTRIGHT] = PREC_SHIFT,
154 [EXPR_BINARY_ASSIGN] = PREC_ASSIGNMENT,
155 [EXPR_BINARY_MUL_ASSIGN] = PREC_ASSIGNMENT,
156 [EXPR_BINARY_DIV_ASSIGN] = PREC_ASSIGNMENT,
157 [EXPR_BINARY_MOD_ASSIGN] = PREC_ASSIGNMENT,
158 [EXPR_BINARY_ADD_ASSIGN] = PREC_ASSIGNMENT,
159 [EXPR_BINARY_SUB_ASSIGN] = PREC_ASSIGNMENT,
160 [EXPR_BINARY_SHIFTLEFT_ASSIGN] = PREC_ASSIGNMENT,
161 [EXPR_BINARY_SHIFTRIGHT_ASSIGN] = PREC_ASSIGNMENT,
162 [EXPR_BINARY_BITWISE_AND_ASSIGN] = PREC_ASSIGNMENT,
163 [EXPR_BINARY_BITWISE_XOR_ASSIGN] = PREC_ASSIGNMENT,
164 [EXPR_BINARY_BITWISE_OR_ASSIGN] = PREC_ASSIGNMENT,
165 [EXPR_BINARY_COMMA] = PREC_EXPRESSION,
167 [EXPR_BINARY_BUILTIN_EXPECT] = PREC_PRIMARY,
168 [EXPR_BINARY_ISGREATER] = PREC_PRIMARY,
169 [EXPR_BINARY_ISGREATEREQUAL] = PREC_PRIMARY,
170 [EXPR_BINARY_ISLESS] = PREC_PRIMARY,
171 [EXPR_BINARY_ISLESSEQUAL] = PREC_PRIMARY,
172 [EXPR_BINARY_ISLESSGREATER] = PREC_PRIMARY,
173 [EXPR_BINARY_ISUNORDERED] = PREC_PRIMARY
175 assert((unsigned)kind < (sizeof(prec)/sizeof(prec[0])));
176 unsigned res = prec[kind];
178 assert(res != PREC_BOTTOM);
179 /* we need the lowest bit for right-to-left precedence */
184 * Print a constant expression.
186 * @param cnst the constant expression
188 static void print_const(const const_expression_t *cnst)
190 if(cnst->base.type == NULL)
193 const type_t *const type = skip_typeref(cnst->base.type);
195 if (is_type_integer(type)) {
196 fprintf(out, "%lld", cnst->v.int_value);
197 } else if (is_type_float(type)) {
198 long double const val = cnst->v.float_value;
200 /* ARG, no way to print long double */
201 fprintf(out, "%.20g", (double)val);
203 fprintf(out, "%.20Lg", val);
205 if (isfinite(val) && truncl(val) == val)
208 panic("unknown constant");
212 switch (type->atomic.akind) {
213 case ATOMIC_TYPE_UINT: suffix = "U"; break;
214 case ATOMIC_TYPE_LONG: suffix = "L"; break;
215 case ATOMIC_TYPE_ULONG: suffix = "UL"; break;
216 case ATOMIC_TYPE_LONGLONG: suffix = "LL"; break;
217 case ATOMIC_TYPE_ULONGLONG: suffix = "ULL"; break;
218 case ATOMIC_TYPE_FLOAT: suffix = "F"; break;
219 case ATOMIC_TYPE_LONG_DOUBLE: suffix = "L"; break;
221 default: suffix = NULL; break;
228 * Print a quoted string constant.
230 * @param string the string constant
231 * @param border the border char
232 * @param skip number of chars to skip at the end
234 static void print_quoted_string(const string_t *const string, char border, int skip)
237 const char *end = string->begin + string->size - skip;
238 for (const char *c = string->begin; c != end; ++c) {
243 case '\\': fputs("\\\\", out); break;
244 case '\a': fputs("\\a", out); break;
245 case '\b': fputs("\\b", out); break;
246 case '\f': fputs("\\f", out); break;
247 case '\n': fputs("\\n", out); break;
248 case '\r': fputs("\\r", out); break;
249 case '\t': fputs("\\t", out); break;
250 case '\v': fputs("\\v", out); break;
251 case '\?': fputs("\\?", out); break;
253 if (c_mode & _GNUC) {
254 fputs("\\e", out); break;
259 fprintf(out, "\\%03o", (unsigned)*c);
270 * Prints a wide string literal expression.
272 * @param wstr the wide string literal expression
273 * @param border the border char
274 * @param skip number of chars to skip at the end
276 static void print_quoted_wide_string(const wide_string_t *const wstr,
277 char border, int skip)
281 const wchar_rep_t *end = wstr->begin + wstr->size - skip;
282 for (const wchar_rep_t *c = wstr->begin; c != end; ++c) {
284 case L'\"': fputs("\\\"", out); break;
285 case L'\\': fputs("\\\\", out); break;
286 case L'\a': fputs("\\a", out); break;
287 case L'\b': fputs("\\b", out); break;
288 case L'\f': fputs("\\f", out); break;
289 case L'\n': fputs("\\n", out); break;
290 case L'\r': fputs("\\r", out); break;
291 case L'\t': fputs("\\t", out); break;
292 case L'\v': fputs("\\v", out); break;
293 case L'\?': fputs("\\?", out); break;
295 if (c_mode & _GNUC) {
296 fputs("\\e", out); break;
300 const unsigned tc = *c;
303 fprintf(out, "\\%03o", (char)*c);
307 } else if (tc < 0x800) {
308 fputc(0xC0 | (tc >> 6), out);
309 fputc(0x80 | (tc & 0x3F), out);
310 } else if (tc < 0x10000) {
311 fputc(0xE0 | ( tc >> 12), out);
312 fputc(0x80 | ((tc >> 6) & 0x3F), out);
313 fputc(0x80 | ( tc & 0x3F), out);
315 fputc(0xF0 | ( tc >> 18), out);
316 fputc(0x80 | ((tc >> 12) & 0x3F), out);
317 fputc(0x80 | ((tc >> 6) & 0x3F), out);
318 fputc(0x80 | ( tc & 0x3F), out);
327 * Print a constant character expression.
329 * @param cnst the constant character expression
331 static void print_character_constant(const const_expression_t *cnst)
333 print_quoted_string(&cnst->v.character, '\'', 0);
336 static void print_wide_character_constant(const const_expression_t *cnst)
338 print_quoted_wide_string(&cnst->v.wide_character, '\'', 0);
342 * Prints a string literal expression.
344 * @param string_literal the string literal expression
346 static void print_string_literal(
347 const string_literal_expression_t *string_literal)
349 print_quoted_string(&string_literal->value, '"', 1);
353 * Prints a predefined symbol.
355 static void print_funcname(
356 const funcname_expression_t *funcname)
359 switch(funcname->kind) {
360 case FUNCNAME_FUNCTION: s = (c_mode & _C99) ? "__func__" : "__FUNCTION__"; break;
361 case FUNCNAME_PRETTY_FUNCTION: s = "__PRETTY_FUNCTION__"; break;
362 case FUNCNAME_FUNCSIG: s = "__FUNCSIG__"; break;
363 case FUNCNAME_FUNCDNAME: s = "__FUNCDNAME__"; break;
370 static void print_wide_string_literal(
371 const wide_string_literal_expression_t *const wstr)
373 print_quoted_wide_string(&wstr->value, '"', 1);
376 static void print_compound_literal(
377 const compound_literal_expression_t *expression)
380 print_type(expression->type);
382 print_initializer(expression->initializer);
386 * Prints a call expression.
388 * @param call the call expression
390 static void print_call_expression(const call_expression_t *call)
392 unsigned prec = get_expression_precedence(call->base.kind);
393 print_expression_prec(call->function, prec);
395 call_argument_t *argument = call->arguments;
397 while(argument != NULL) {
403 print_expression_prec(argument->expression, 2 * PREC_ASSIGNMENT);
405 argument = argument->next;
411 * Prints a binary expression.
413 * @param binexpr the binary expression
415 static void print_binary_expression(const binary_expression_t *binexpr)
417 unsigned prec = get_expression_precedence(binexpr->base.kind);
418 int r2l = right_to_left(prec);
420 if(binexpr->base.kind == EXPR_BINARY_BUILTIN_EXPECT) {
421 fputs("__builtin_expect(", out);
422 print_expression_prec(binexpr->left, prec);
424 print_expression_prec(binexpr->right, prec);
429 print_expression_prec(binexpr->left, prec + r2l);
431 switch (binexpr->base.kind) {
432 case EXPR_BINARY_COMMA: op = ", "; break;
433 case EXPR_BINARY_ASSIGN: op = " = "; break;
434 case EXPR_BINARY_ADD: op = " + "; break;
435 case EXPR_BINARY_SUB: op = " - "; break;
436 case EXPR_BINARY_MUL: op = " * "; break;
437 case EXPR_BINARY_MOD: op = " % "; break;
438 case EXPR_BINARY_DIV: op = " / "; break;
439 case EXPR_BINARY_BITWISE_OR: op = " | "; break;
440 case EXPR_BINARY_BITWISE_AND: op = " & "; break;
441 case EXPR_BINARY_BITWISE_XOR: op = " ^ "; break;
442 case EXPR_BINARY_LOGICAL_OR: op = " || "; break;
443 case EXPR_BINARY_LOGICAL_AND: op = " && "; break;
444 case EXPR_BINARY_NOTEQUAL: op = " != "; break;
445 case EXPR_BINARY_EQUAL: op = " == "; break;
446 case EXPR_BINARY_LESS: op = " < "; break;
447 case EXPR_BINARY_LESSEQUAL: op = " <= "; break;
448 case EXPR_BINARY_GREATER: op = " > "; break;
449 case EXPR_BINARY_GREATEREQUAL: op = " >= "; break;
450 case EXPR_BINARY_SHIFTLEFT: op = " << "; break;
451 case EXPR_BINARY_SHIFTRIGHT: op = " >> "; break;
453 case EXPR_BINARY_ADD_ASSIGN: op = " += "; break;
454 case EXPR_BINARY_SUB_ASSIGN: op = " -= "; break;
455 case EXPR_BINARY_MUL_ASSIGN: op = " *= "; break;
456 case EXPR_BINARY_MOD_ASSIGN: op = " %= "; break;
457 case EXPR_BINARY_DIV_ASSIGN: op = " /= "; break;
458 case EXPR_BINARY_BITWISE_OR_ASSIGN: op = " |= "; break;
459 case EXPR_BINARY_BITWISE_AND_ASSIGN: op = " &= "; break;
460 case EXPR_BINARY_BITWISE_XOR_ASSIGN: op = " ^= "; break;
461 case EXPR_BINARY_SHIFTLEFT_ASSIGN: op = " <<= "; break;
462 case EXPR_BINARY_SHIFTRIGHT_ASSIGN: op = " >>= "; break;
463 default: panic("invalid binexpression found");
466 print_expression_prec(binexpr->right, prec - r2l);
470 * Prints an unary expression.
472 * @param unexpr the unary expression
474 static void print_unary_expression(const unary_expression_t *unexpr)
476 unsigned prec = get_expression_precedence(unexpr->base.kind);
477 switch(unexpr->base.kind) {
478 case EXPR_UNARY_NEGATE: fputc('-', out); break;
479 case EXPR_UNARY_PLUS: fputc('+', out); break;
480 case EXPR_UNARY_NOT: fputc('!', out); break;
481 case EXPR_UNARY_BITWISE_NEGATE: fputc('~', out); break;
482 case EXPR_UNARY_PREFIX_INCREMENT: fputs("++", out); break;
483 case EXPR_UNARY_PREFIX_DECREMENT: fputs("--", out); break;
484 case EXPR_UNARY_DEREFERENCE: fputc('*', out); break;
485 case EXPR_UNARY_TAKE_ADDRESS: fputc('&', out); break;
487 case EXPR_UNARY_POSTFIX_INCREMENT:
488 print_expression_prec(unexpr->value, prec);
491 case EXPR_UNARY_POSTFIX_DECREMENT:
492 print_expression_prec(unexpr->value, prec);
495 case EXPR_UNARY_CAST_IMPLICIT:
496 case EXPR_UNARY_CAST:
498 print_type(unexpr->base.type);
501 case EXPR_UNARY_ASSUME:
502 fputs("__assume(", out);
503 print_expression_prec(unexpr->value, 2 * PREC_ASSIGNMENT);
507 case EXPR_UNARY_THROW:
508 if (unexpr->value == NULL) {
512 fputs("throw ", out);
516 panic("invalid unary expression found");
518 print_expression_prec(unexpr->value, prec);
522 * Prints a reference expression.
524 * @param ref the reference expression
526 static void print_reference_expression(const reference_expression_t *ref)
528 fputs(ref->declaration->symbol->string, out);
532 * Prints a label address expression.
534 * @param ref the reference expression
536 static void print_label_address_expression(const label_address_expression_t *le)
538 fprintf(out, "&&%s", le->declaration->symbol->string);
542 * Prints an array expression.
544 * @param expression the array expression
546 static void print_array_expression(const array_access_expression_t *expression)
548 unsigned prec = get_expression_precedence(expression->base.kind);
549 if(!expression->flipped) {
550 print_expression_prec(expression->array_ref, prec);
552 print_expression(expression->index);
555 print_expression_prec(expression->index, prec);
557 print_expression(expression->array_ref);
563 * Prints a typeproperty expression (sizeof or __alignof__).
565 * @param expression the type property expression
567 static void print_typeprop_expression(const typeprop_expression_t *expression)
569 if (expression->base.kind == EXPR_SIZEOF) {
570 fputs("sizeof", out);
572 assert(expression->base.kind == EXPR_ALIGNOF);
573 fputs("__alignof__", out);
575 if(expression->tp_expression != NULL) {
576 /* always print the '()' here, sizeof x is right but unusual */
578 print_expression(expression->tp_expression);
582 print_type(expression->type);
588 * Prints an builtin symbol.
590 * @param expression the builtin symbol expression
592 static void print_builtin_symbol(const builtin_symbol_expression_t *expression)
594 fputs(expression->symbol->string, out);
598 * Prints a builtin constant expression.
600 * @param expression the builtin constant expression
602 static void print_builtin_constant(const builtin_constant_expression_t *expression)
604 fputs("__builtin_constant_p(", out);
605 print_expression_prec(expression->value, 2 * PREC_ASSIGNMENT);
610 * Prints a builtin prefetch expression.
612 * @param expression the builtin prefetch expression
614 static void print_builtin_prefetch(const builtin_prefetch_expression_t *expression)
616 fputs("__builtin_prefetch(", out);
617 print_expression_prec(expression->adr, 2 * PREC_ASSIGNMENT);
618 if (expression->rw) {
620 print_expression_prec(expression->rw, 2 * PREC_ASSIGNMENT);
622 if (expression->locality) {
624 print_expression_prec(expression->locality, 2 * PREC_ASSIGNMENT);
630 * Prints a conditional expression.
632 * @param expression the conditional expression
634 static void print_conditional(const conditional_expression_t *expression)
636 print_expression_prec(expression->condition, 2 * PREC_LOGICAL_OR);
638 if (expression->true_expression != NULL) {
639 print_expression_prec(expression->true_expression, 2 * PREC_EXPRESSION);
644 print_expression_prec(expression->false_expression, 2 * PREC_CONDITIONAL);
648 * Prints a va_start expression.
650 * @param expression the va_start expression
652 static void print_va_start(const va_start_expression_t *const expression)
654 fputs("__builtin_va_start(", out);
655 print_expression_prec(expression->ap, 2 * PREC_ASSIGNMENT);
657 fputs(expression->parameter->symbol->string, out);
662 * Prints a va_arg expression.
664 * @param expression the va_arg expression
666 static void print_va_arg(const va_arg_expression_t *expression)
668 fputs("__builtin_va_arg(", out);
669 print_expression_prec(expression->ap, 2 * PREC_ASSIGNMENT);
671 print_type(expression->base.type);
676 * Prints a select expression (. or ->).
678 * @param expression the select expression
680 static void print_select(const select_expression_t *expression)
682 unsigned prec = get_expression_precedence(expression->base.kind);
683 print_expression_prec(expression->compound, prec);
684 if(is_type_pointer(skip_typeref(expression->compound->base.type))) {
689 fputs(expression->compound_entry->symbol->string, out);
693 * Prints a type classify expression.
695 * @param expr the type classify expression
697 static void print_classify_type_expression(
698 const classify_type_expression_t *const expr)
700 fputs("__builtin_classify_type(", out);
701 print_expression_prec(expr->type_expression, 2 * PREC_ASSIGNMENT);
706 * Prints a designator.
708 * @param designator the designator
710 static void print_designator(const designator_t *designator)
712 for ( ; designator != NULL; designator = designator->next) {
713 if (designator->symbol == NULL) {
715 print_expression(designator->array_index);
719 fputs(designator->symbol->string, out);
725 * Prints an offsetof expression.
727 * @param expression the offset expression
729 static void print_offsetof_expression(const offsetof_expression_t *expression)
731 fputs("__builtin_offsetof", out);
733 print_type(expression->type);
735 print_designator(expression->designator);
740 * Prints a statement expression.
742 * @param expression the statement expression
744 static void print_statement_expression(const statement_expression_t *expression)
747 print_statement(expression->statement);
752 * Prints an expression with parenthesis if needed.
754 * @param expression the expression to print
755 * @param top_prec the precedence of the user of this expression.
757 static void print_expression_prec(const expression_t *expression, unsigned top_prec)
759 if (expression->kind == EXPR_UNARY_CAST_IMPLICIT && !print_implicit_casts) {
760 expression = expression->unary.value;
762 unsigned prec = get_expression_precedence(expression->base.kind);
763 if (print_parenthesis && top_prec != PREC_BOTTOM)
767 switch(expression->kind) {
770 fputs("$invalid expression$", out);
772 case EXPR_CHARACTER_CONSTANT:
773 print_character_constant(&expression->conste);
775 case EXPR_WIDE_CHARACTER_CONSTANT:
776 print_wide_character_constant(&expression->conste);
779 print_const(&expression->conste);
782 print_funcname(&expression->funcname);
784 case EXPR_STRING_LITERAL:
785 print_string_literal(&expression->string);
787 case EXPR_WIDE_STRING_LITERAL:
788 print_wide_string_literal(&expression->wide_string);
790 case EXPR_COMPOUND_LITERAL:
791 print_compound_literal(&expression->compound_literal);
794 print_call_expression(&expression->call);
797 print_binary_expression(&expression->binary);
800 print_reference_expression(&expression->reference);
802 case EXPR_ARRAY_ACCESS:
803 print_array_expression(&expression->array_access);
805 case EXPR_LABEL_ADDRESS:
806 print_label_address_expression(&expression->label_address);
809 print_unary_expression(&expression->unary);
813 print_typeprop_expression(&expression->typeprop);
815 case EXPR_BUILTIN_SYMBOL:
816 print_builtin_symbol(&expression->builtin_symbol);
818 case EXPR_BUILTIN_CONSTANT_P:
819 print_builtin_constant(&expression->builtin_constant);
821 case EXPR_BUILTIN_PREFETCH:
822 print_builtin_prefetch(&expression->builtin_prefetch);
824 case EXPR_CONDITIONAL:
825 print_conditional(&expression->conditional);
828 print_va_start(&expression->va_starte);
831 print_va_arg(&expression->va_arge);
834 print_select(&expression->select);
836 case EXPR_CLASSIFY_TYPE:
837 print_classify_type_expression(&expression->classify_type);
840 print_offsetof_expression(&expression->offsetofe);
843 print_statement_expression(&expression->statement);
848 fprintf(out, "some expression of type %d", (int) expression->kind);
856 * Print an compound statement.
858 * @param block the compound statement
860 static void print_compound_statement(const compound_statement_t *block)
865 statement_t *statement = block->statements;
866 while (statement != NULL) {
867 if (statement->base.kind == STATEMENT_CASE_LABEL)
869 if (statement->kind != STATEMENT_LABEL)
871 print_statement(statement);
873 statement = statement->base.next;
881 * Print a return statement.
883 * @param statement the return statement
885 static void print_return_statement(const return_statement_t *statement)
887 fputs("return ", out);
888 if(statement->value != NULL)
889 print_expression(statement->value);
894 * Print an expression statement.
896 * @param statement the expression statement
898 static void print_expression_statement(const expression_statement_t *statement)
900 print_expression(statement->expression);
905 * Print a goto statement.
907 * @param statement the goto statement
909 static void print_goto_statement(const goto_statement_t *statement)
912 if (statement->expression != NULL) {
914 print_expression(statement->expression);
916 fputs(statement->label->symbol->string, out);
922 * Print a label statement.
924 * @param statement the label statement
926 static void print_label_statement(const label_statement_t *statement)
928 fprintf(out, "%s:\n", statement->label->symbol->string);
930 print_statement(statement->statement);
934 * Print an if statement.
936 * @param statement the if statement
938 static void print_if_statement(const if_statement_t *statement)
941 print_expression(statement->condition);
943 print_statement(statement->true_statement);
945 if(statement->false_statement != NULL) {
948 print_statement(statement->false_statement);
953 * Print a switch statement.
955 * @param statement the switch statement
957 static void print_switch_statement(const switch_statement_t *statement)
959 fputs("switch (", out);
960 print_expression(statement->expression);
962 print_statement(statement->body);
966 * Print a case label (including the default label).
968 * @param statement the case label statement
970 static void print_case_label(const case_label_statement_t *statement)
972 if(statement->expression == NULL) {
973 fputs("default:\n", out);
976 print_expression(statement->expression);
977 if (statement->end_range != NULL) {
979 print_expression(statement->end_range);
984 if(statement->statement != NULL) {
985 if (statement->statement->base.kind == STATEMENT_CASE_LABEL) {
989 print_statement(statement->statement);
994 * Print a declaration statement.
996 * @param statement the statement
998 static void print_declaration_statement(
999 const declaration_statement_t *statement)
1002 declaration_t *declaration = statement->declarations_begin;
1004 if (declaration->namespc == NAMESPACE_LOCAL_LABEL) {
1005 fputs("__label__ ", out);
1007 declaration != statement->declarations_end->next;
1008 declaration = declaration->next) {
1014 fputs(declaration->symbol->string, out);
1019 declaration != statement->declarations_end->next;
1020 declaration = declaration->next) {
1021 if (declaration->storage_class == STORAGE_CLASS_ENUM_ENTRY)
1023 if (declaration->implicit)
1031 print_declaration(declaration);
1038 * Print a while statement.
1040 * @param statement the statement
1042 static void print_while_statement(const while_statement_t *statement)
1044 fputs("while (", out);
1045 print_expression(statement->condition);
1047 print_statement(statement->body);
1051 * Print a do-while statement.
1053 * @param statement the statement
1055 static void print_do_while_statement(const do_while_statement_t *statement)
1058 print_statement(statement->body);
1060 fputs("while (", out);
1061 print_expression(statement->condition);
1066 * Print a for statement.
1068 * @param statement the statement
1070 static void print_for_statement(const for_statement_t *statement)
1072 fputs("for (", out);
1073 declaration_t *decl = statement->scope.declarations;
1074 while (decl != NULL && decl->implicit)
1077 assert(statement->initialisation == NULL);
1078 print_declaration(decl);
1079 if (decl->next != NULL) {
1080 panic("multiple declarations in for statement not supported yet");
1084 if(statement->initialisation) {
1085 print_expression(statement->initialisation);
1089 if(statement->condition != NULL) {
1090 print_expression(statement->condition);
1093 if(statement->step != NULL) {
1094 print_expression(statement->step);
1097 print_statement(statement->body);
1101 * Print assembler arguments.
1103 * @param arguments the arguments
1105 static void print_asm_arguments(asm_argument_t *arguments)
1107 asm_argument_t *argument = arguments;
1108 for( ; argument != NULL; argument = argument->next) {
1109 if(argument != arguments)
1112 if(argument->symbol) {
1113 fprintf(out, "[%s] ", argument->symbol->string);
1115 print_quoted_string(&argument->constraints, '"', 1);
1117 print_expression(argument->expression);
1123 * Print assembler clobbers.
1125 * @param clobbers the clobbers
1127 static void print_asm_clobbers(asm_clobber_t *clobbers)
1129 asm_clobber_t *clobber = clobbers;
1130 for( ; clobber != NULL; clobber = clobber->next) {
1131 if(clobber != clobbers)
1134 print_quoted_string(&clobber->clobber, '"', 1);
1139 * Print an assembler statement.
1141 * @param statement the statement
1143 static void print_asm_statement(const asm_statement_t *statement)
1146 if(statement->is_volatile) {
1147 fputs("volatile ", out);
1150 print_quoted_string(&statement->asm_text, '"', 1);
1151 if (statement->outputs == NULL &&
1152 statement->inputs == NULL &&
1153 statement->clobbers == NULL)
1154 goto end_of_print_asm_statement;
1157 print_asm_arguments(statement->outputs);
1158 if (statement->inputs == NULL && statement->clobbers == NULL)
1159 goto end_of_print_asm_statement;
1162 print_asm_arguments(statement->inputs);
1163 if (statement->clobbers == NULL)
1164 goto end_of_print_asm_statement;
1167 print_asm_clobbers(statement->clobbers);
1169 end_of_print_asm_statement:
1174 * Print a microsoft __try statement.
1176 * @param statement the statement
1178 static void print_ms_try_statement(const ms_try_statement_t *statement)
1180 fputs("__try ", out);
1181 print_statement(statement->try_statement);
1183 if(statement->except_expression != NULL) {
1184 fputs("__except(", out);
1185 print_expression(statement->except_expression);
1188 fputs("__finally ", out);
1190 print_statement(statement->final_statement);
1194 * Print a microsoft __leave statement.
1196 * @param statement the statement
1198 static void print_leave_statement(const leave_statement_t *statement)
1201 fputs("__leave;\n", out);
1205 * Print a statement.
1207 * @param statement the statement
1209 void print_statement(const statement_t *statement)
1211 switch (statement->kind) {
1212 case STATEMENT_EMPTY:
1215 case STATEMENT_COMPOUND:
1216 print_compound_statement(&statement->compound);
1218 case STATEMENT_RETURN:
1219 print_return_statement(&statement->returns);
1221 case STATEMENT_EXPRESSION:
1222 print_expression_statement(&statement->expression);
1224 case STATEMENT_LABEL:
1225 print_label_statement(&statement->label);
1227 case STATEMENT_GOTO:
1228 print_goto_statement(&statement->gotos);
1230 case STATEMENT_CONTINUE:
1231 fputs("continue;\n", out);
1233 case STATEMENT_BREAK:
1234 fputs("break;\n", out);
1237 print_if_statement(&statement->ifs);
1239 case STATEMENT_SWITCH:
1240 print_switch_statement(&statement->switchs);
1242 case STATEMENT_CASE_LABEL:
1243 print_case_label(&statement->case_label);
1245 case STATEMENT_DECLARATION:
1246 print_declaration_statement(&statement->declaration);
1248 case STATEMENT_WHILE:
1249 print_while_statement(&statement->whiles);
1251 case STATEMENT_DO_WHILE:
1252 print_do_while_statement(&statement->do_while);
1255 print_for_statement(&statement->fors);
1258 print_asm_statement(&statement->asms);
1260 case STATEMENT_MS_TRY:
1261 print_ms_try_statement(&statement->ms_try);
1263 case STATEMENT_LEAVE:
1264 print_leave_statement(&statement->leave);
1266 case STATEMENT_INVALID:
1267 fputs("$invalid statement$\n", out);
1273 * Print a storage class.
1275 * @param storage_class the storage class
1277 static void print_storage_class(storage_class_tag_t storage_class)
1279 switch(storage_class) {
1280 case STORAGE_CLASS_ENUM_ENTRY:
1281 case STORAGE_CLASS_NONE:
1283 case STORAGE_CLASS_TYPEDEF: fputs("typedef ", out); break;
1284 case STORAGE_CLASS_EXTERN: fputs("extern ", out); break;
1285 case STORAGE_CLASS_STATIC: fputs("static ", out); break;
1286 case STORAGE_CLASS_AUTO: fputs("auto ", out); break;
1287 case STORAGE_CLASS_REGISTER: fputs("register ", out); break;
1288 case STORAGE_CLASS_THREAD: fputs("__thread", out); break;
1289 case STORAGE_CLASS_THREAD_EXTERN: fputs("extern __thread", out); break;
1290 case STORAGE_CLASS_THREAD_STATIC: fputs("static __thread", out); break;
1295 * Print an initializer.
1297 * @param initializer the initializer
1299 void print_initializer(const initializer_t *initializer)
1301 if(initializer == NULL) {
1306 switch(initializer->kind) {
1307 case INITIALIZER_VALUE: {
1308 const initializer_value_t *value = &initializer->value;
1309 print_expression(value->value);
1312 case INITIALIZER_LIST: {
1313 assert(initializer->kind == INITIALIZER_LIST);
1315 const initializer_list_t *list = &initializer->list;
1317 for(size_t i = 0 ; i < list->len; ++i) {
1318 const initializer_t *sub_init = list->initializers[i];
1319 print_initializer(list->initializers[i]);
1320 if(i < list->len-1) {
1321 if(sub_init == NULL || sub_init->kind != INITIALIZER_DESIGNATOR)
1328 case INITIALIZER_STRING:
1329 print_quoted_string(&initializer->string.string, '"', 1);
1331 case INITIALIZER_WIDE_STRING:
1332 print_quoted_wide_string(&initializer->wide_string.string, '"', 1);
1334 case INITIALIZER_DESIGNATOR:
1335 print_designator(initializer->designator.designator);
1340 panic("invalid initializer kind found");
1344 * Print microsoft extended declaration modifiers.
1346 static void print_ms_modifiers(const declaration_t *declaration) {
1347 if((c_mode & _MS) == 0)
1350 decl_modifiers_t modifiers = declaration->modifiers;
1352 /* DM_FORCEINLINE handled outside. */
1353 if ((modifiers & ~DM_FORCEINLINE) != 0 ||
1354 declaration->alignment != 0 ||
1355 declaration->get_property_sym != NULL ||
1356 declaration->put_property_sym != NULL) {
1359 fputs("__declspec", out);
1360 if(modifiers & DM_DLLIMPORT) {
1361 fputs(next, out); next = ", "; fputs("dllimport", out);
1363 if(modifiers & DM_DLLEXPORT) {
1364 fputs(next, out); next = ", "; fputs("dllexport", out);
1366 if(modifiers & DM_THREAD) {
1367 fputs(next, out); next = ", "; fputs("thread", out);
1369 if(modifiers & DM_NAKED) {
1370 fputs(next, out); next = ", "; fputs("naked", out);
1372 if(modifiers & DM_THREAD) {
1373 fputs(next, out); next = ", "; fputs("thread", out);
1375 if(modifiers & DM_SELECTANY) {
1376 fputs(next, out); next = ", "; fputs("selectany", out);
1378 if(modifiers & DM_NOTHROW) {
1379 fputs(next, out); next = ", "; fputs("nothrow", out);
1381 if(modifiers & DM_NORETURN) {
1382 fputs(next, out); next = ", "; fputs("noreturn", out);
1384 if(modifiers & DM_NOINLINE) {
1385 fputs(next, out); next = ", "; fputs("noinline", out);
1387 if (modifiers & DM_DEPRECATED) {
1388 fputs(next, out); next = ", "; fputs("deprecated", out);
1389 if(declaration->deprecated_string != NULL)
1390 fprintf(out, "(\"%s\")", declaration->deprecated_string);
1392 if(declaration->alignment != 0) {
1393 fputs(next, out); next = ", "; fprintf(out, "align(%u)", declaration->alignment);
1395 if(modifiers & DM_RESTRICT) {
1396 fputs(next, out); next = ", "; fputs("restrict", out);
1398 if(modifiers & DM_NOALIAS) {
1399 fputs(next, out); next = ", "; fputs("noalias", out);
1401 if(declaration->get_property_sym != NULL || declaration->put_property_sym != NULL) {
1403 fputs(next, out); next = ", "; fputs("property(", out);
1404 if(declaration->get_property_sym != NULL) {
1405 fprintf(out, "get=%s", declaration->get_property_sym->string);
1408 if(declaration->put_property_sym != NULL)
1409 fprintf(out, "%sput=%s", comma, declaration->put_property_sym->string);
1417 * Print a declaration in the NORMAL namespace.
1419 * @param declaration the declaration
1421 static void print_normal_declaration(const declaration_t *declaration)
1423 print_storage_class((storage_class_tag_t) declaration->declared_storage_class);
1424 if (declaration->is_inline) {
1425 if (declaration->modifiers & DM_FORCEINLINE) {
1426 fputs("__forceinline ", out);
1427 } else if (declaration->modifiers & DM_MICROSOFT_INLINE) {
1428 fputs("__inline ", out);
1430 fputs("inline ", out);
1433 print_ms_modifiers(declaration);
1434 print_type_ext(declaration->type, declaration->symbol,
1435 &declaration->scope);
1437 if(declaration->type->kind == TYPE_FUNCTION) {
1438 if(declaration->init.statement != NULL) {
1441 print_statement(declaration->init.statement);
1444 } else if(declaration->init.initializer != NULL) {
1446 print_initializer(declaration->init.initializer);
1452 * Prints an expression.
1454 * @param expression the expression
1456 void print_expression(const expression_t *expression)
1458 print_expression_prec(expression, 2 * PREC_BOTTOM);
1462 * Print a declaration.
1464 * @param declaration the declaration
1466 void print_declaration(const declaration_t *declaration)
1468 if (declaration->namespc != NAMESPACE_NORMAL &&
1469 declaration->symbol == NULL)
1472 switch (declaration->namespc) {
1473 case NAMESPACE_NORMAL:
1474 print_normal_declaration(declaration);
1476 case NAMESPACE_STRUCT:
1477 fputs("struct ", out);
1478 fputs(declaration->symbol->string, out);
1479 if (declaration->init.complete) {
1481 print_compound_definition(declaration);
1485 case NAMESPACE_UNION:
1486 fputs("union ", out);
1487 fputs(declaration->symbol->string, out);
1488 if (declaration->init.complete) {
1490 print_compound_definition(declaration);
1494 case NAMESPACE_ENUM:
1495 fputs("enum ", out);
1496 fputs(declaration->symbol->string, out);
1497 if (declaration->init.complete) {
1499 print_enum_definition(declaration);
1507 * Print the AST of a translation unit.
1509 * @param unit the translation unit
1511 void print_ast(const translation_unit_t *unit)
1515 declaration_t *declaration = unit->scope.declarations;
1516 for( ; declaration != NULL; declaration = declaration->next) {
1517 if(declaration->storage_class == STORAGE_CLASS_ENUM_ENTRY)
1519 if(declaration->namespc != NAMESPACE_NORMAL &&
1520 declaration->symbol == NULL)
1522 if (declaration->implicit)
1526 print_declaration(declaration);
1531 bool is_constant_initializer(const initializer_t *initializer)
1533 switch(initializer->kind) {
1534 case INITIALIZER_STRING:
1535 case INITIALIZER_WIDE_STRING:
1536 case INITIALIZER_DESIGNATOR:
1539 case INITIALIZER_VALUE:
1540 return is_constant_expression(initializer->value.value);
1542 case INITIALIZER_LIST:
1543 for(size_t i = 0; i < initializer->list.len; ++i) {
1544 initializer_t *sub_initializer = initializer->list.initializers[i];
1545 if(!is_constant_initializer(sub_initializer))
1550 panic("invalid initializer kind found");
1553 static bool is_object_with_linker_constant_address(const expression_t *expression)
1555 switch(expression->kind) {
1556 case EXPR_UNARY_DEREFERENCE:
1557 return is_address_constant(expression->unary.value);
1560 type_t *base_type = skip_typeref(expression->select.compound->base.type);
1561 if(is_type_pointer(base_type)) {
1563 return is_address_constant(expression->select.compound);
1565 return is_object_with_linker_constant_address(expression->select.compound);
1569 case EXPR_ARRAY_ACCESS:
1570 return is_constant_expression(expression->array_access.index)
1571 && is_address_constant(expression->array_access.array_ref);
1573 case EXPR_REFERENCE: {
1574 declaration_t *declaration = expression->reference.declaration;
1575 switch((storage_class_tag_t) declaration->storage_class) {
1576 case STORAGE_CLASS_NONE:
1577 case STORAGE_CLASS_EXTERN:
1578 case STORAGE_CLASS_STATIC:
1590 bool is_address_constant(const expression_t *expression)
1592 switch(expression->kind) {
1593 case EXPR_UNARY_TAKE_ADDRESS:
1594 return is_object_with_linker_constant_address(expression->unary.value);
1596 case EXPR_UNARY_DEREFERENCE: {
1598 = revert_automatic_type_conversion(expression->unary.value);
1599 /* dereferencing a function is a NOP */
1600 if(is_type_function(real_type)) {
1601 return is_address_constant(expression->unary.value);
1607 case EXPR_UNARY_CAST: {
1608 type_t *dest = skip_typeref(expression->base.type);
1609 if (!is_type_pointer(dest) && (
1610 dest->kind != TYPE_ATOMIC ||
1611 !(get_atomic_type_flags(dest->atomic.akind) & ATOMIC_TYPE_FLAG_INTEGER) ||
1612 (get_atomic_type_size(dest->atomic.akind) < get_atomic_type_size(get_intptr_kind())
1616 return (is_constant_expression(expression->unary.value)
1617 || is_address_constant(expression->unary.value));
1620 case EXPR_BINARY_ADD:
1621 case EXPR_BINARY_SUB: {
1622 expression_t *left = expression->binary.left;
1623 expression_t *right = expression->binary.right;
1625 if(is_type_pointer(skip_typeref(left->base.type))) {
1626 return is_address_constant(left) && is_constant_expression(right);
1627 } else if(is_type_pointer(skip_typeref(right->base.type))) {
1628 return is_constant_expression(left) && is_address_constant(right);
1634 case EXPR_REFERENCE: {
1635 declaration_t *declaration = expression->reference.declaration;
1636 type_t *type = skip_typeref(declaration->type);
1637 if(is_type_function(type))
1639 if(is_type_array(type)) {
1640 return is_object_with_linker_constant_address(expression);
1642 /* Prevent stray errors */
1643 if (!is_type_valid(type))
1648 case EXPR_ARRAY_ACCESS: {
1649 type_t *const type =
1650 skip_typeref(revert_automatic_type_conversion(expression));
1652 is_type_array(type) &&
1653 is_constant_expression(expression->array_access.index) &&
1654 is_address_constant(expression->array_access.array_ref);
1662 static bool is_builtin_const_call(const expression_t *expression)
1664 expression_t *function = expression->call.function;
1665 if (function->kind != EXPR_BUILTIN_SYMBOL) {
1669 symbol_t *symbol = function->builtin_symbol.symbol;
1671 switch (symbol->ID) {
1672 case T___builtin_huge_val:
1673 case T___builtin_inf:
1674 case T___builtin_inff:
1675 case T___builtin_infl:
1676 case T___builtin_nan:
1677 case T___builtin_nanf:
1678 case T___builtin_nanl:
1685 static bool is_constant_pointer(const expression_t *expression)
1687 if (is_constant_expression(expression))
1690 switch (expression->kind) {
1691 case EXPR_UNARY_CAST:
1692 return is_constant_pointer(expression->unary.value);
1698 static bool is_object_with_constant_address(const expression_t *expression)
1700 switch(expression->kind) {
1702 expression_t *compound = expression->select.compound;
1703 type_t *compound_type = compound->base.type;
1704 compound_type = skip_typeref(compound_type);
1705 if(is_type_pointer(compound_type)) {
1706 return is_constant_pointer(compound);
1708 return is_object_with_constant_address(compound);
1712 case EXPR_ARRAY_ACCESS: {
1713 array_access_expression_t const* const array_access =
1714 &expression->array_access;
1716 is_constant_expression(array_access->index) && (
1717 is_object_with_constant_address(array_access->array_ref) ||
1718 is_constant_pointer(array_access->array_ref)
1722 case EXPR_UNARY_DEREFERENCE:
1723 return is_constant_pointer(expression->unary.value);
1729 bool is_constant_expression(const expression_t *expression)
1731 switch (expression->kind) {
1734 case EXPR_CHARACTER_CONSTANT:
1735 case EXPR_WIDE_CHARACTER_CONSTANT:
1736 case EXPR_STRING_LITERAL:
1737 case EXPR_WIDE_STRING_LITERAL:
1738 case EXPR_CLASSIFY_TYPE:
1742 case EXPR_BUILTIN_CONSTANT_P:
1743 case EXPR_LABEL_ADDRESS:
1747 type_t *type = expression->typeprop.type;
1749 type = expression->typeprop.tp_expression->base.type;
1751 type = skip_typeref(type);
1752 if (is_type_array(type) && type->array.is_vla)
1757 case EXPR_BUILTIN_SYMBOL:
1758 case EXPR_BUILTIN_PREFETCH:
1762 case EXPR_STATEMENT:
1763 case EXPR_UNARY_POSTFIX_INCREMENT:
1764 case EXPR_UNARY_POSTFIX_DECREMENT:
1765 case EXPR_UNARY_PREFIX_INCREMENT:
1766 case EXPR_UNARY_PREFIX_DECREMENT:
1767 case EXPR_UNARY_ASSUME: /* has VOID type */
1768 case EXPR_UNARY_DEREFERENCE:
1769 case EXPR_UNARY_THROW:
1770 case EXPR_BINARY_ASSIGN:
1771 case EXPR_BINARY_MUL_ASSIGN:
1772 case EXPR_BINARY_DIV_ASSIGN:
1773 case EXPR_BINARY_MOD_ASSIGN:
1774 case EXPR_BINARY_ADD_ASSIGN:
1775 case EXPR_BINARY_SUB_ASSIGN:
1776 case EXPR_BINARY_SHIFTLEFT_ASSIGN:
1777 case EXPR_BINARY_SHIFTRIGHT_ASSIGN:
1778 case EXPR_BINARY_BITWISE_AND_ASSIGN:
1779 case EXPR_BINARY_BITWISE_XOR_ASSIGN:
1780 case EXPR_BINARY_BITWISE_OR_ASSIGN:
1781 case EXPR_BINARY_COMMA:
1782 case EXPR_ARRAY_ACCESS:
1785 case EXPR_UNARY_TAKE_ADDRESS:
1786 return is_object_with_constant_address(expression->unary.value);
1789 return is_builtin_const_call(expression);
1791 case EXPR_UNARY_NEGATE:
1792 case EXPR_UNARY_PLUS:
1793 case EXPR_UNARY_BITWISE_NEGATE:
1794 case EXPR_UNARY_NOT:
1795 return is_constant_expression(expression->unary.value);
1797 case EXPR_UNARY_CAST:
1798 case EXPR_UNARY_CAST_IMPLICIT:
1799 return is_type_arithmetic(skip_typeref(expression->base.type))
1800 && is_constant_expression(expression->unary.value);
1802 case EXPR_BINARY_ADD:
1803 case EXPR_BINARY_SUB:
1804 case EXPR_BINARY_MUL:
1805 case EXPR_BINARY_DIV:
1806 case EXPR_BINARY_MOD:
1807 case EXPR_BINARY_EQUAL:
1808 case EXPR_BINARY_NOTEQUAL:
1809 case EXPR_BINARY_LESS:
1810 case EXPR_BINARY_LESSEQUAL:
1811 case EXPR_BINARY_GREATER:
1812 case EXPR_BINARY_GREATEREQUAL:
1813 case EXPR_BINARY_BITWISE_AND:
1814 case EXPR_BINARY_BITWISE_OR:
1815 case EXPR_BINARY_BITWISE_XOR:
1816 case EXPR_BINARY_LOGICAL_AND:
1817 case EXPR_BINARY_LOGICAL_OR:
1818 case EXPR_BINARY_SHIFTLEFT:
1819 case EXPR_BINARY_SHIFTRIGHT:
1820 case EXPR_BINARY_BUILTIN_EXPECT:
1821 case EXPR_BINARY_ISGREATER:
1822 case EXPR_BINARY_ISGREATEREQUAL:
1823 case EXPR_BINARY_ISLESS:
1824 case EXPR_BINARY_ISLESSEQUAL:
1825 case EXPR_BINARY_ISLESSGREATER:
1826 case EXPR_BINARY_ISUNORDERED:
1827 return is_constant_expression(expression->binary.left)
1828 && is_constant_expression(expression->binary.right);
1830 case EXPR_COMPOUND_LITERAL:
1831 return is_constant_initializer(expression->compound_literal.initializer);
1833 case EXPR_CONDITIONAL: {
1834 expression_t *condition = expression->conditional.condition;
1835 if(!is_constant_expression(condition))
1838 long val = fold_constant(condition);
1840 return is_constant_expression(expression->conditional.true_expression);
1842 return is_constant_expression(expression->conditional.false_expression);
1845 case EXPR_REFERENCE: {
1846 declaration_t *declaration = expression->reference.declaration;
1847 if(declaration->storage_class == STORAGE_CLASS_ENUM_ENTRY)
1859 panic("invalid expression found (is constant expression)");
1863 * Initialize the AST construction.
1867 obstack_init(&ast_obstack);
1875 obstack_free(&ast_obstack, NULL);
1879 * Set the output stream for the AST printer.
1881 * @param stream the output stream
1883 void ast_set_output(FILE *stream)
1886 type_set_output(stream);
1890 * Allocate an AST object of the given size.
1892 * @param size the size of the object to allocate
1894 * @return A new allocated object in the AST memeory space.
1896 void *(allocate_ast)(size_t size)
1898 return _allocate_ast(size);