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);
178 /* we need the lowest bit for right-to-left precedence */
183 * Print a constant expression.
185 * @param cnst the constant expression
187 static void print_const(const const_expression_t *cnst)
189 if(cnst->base.type == NULL)
192 const type_t *const type = skip_typeref(cnst->base.type);
194 if (is_type_integer(type)) {
195 fprintf(out, "%lld", cnst->v.int_value);
196 } else if (is_type_float(type)) {
197 long double const val = cnst->v.float_value;
199 /* ARG, no way to print long double */
200 fprintf(out, "%.20g", (double)val);
202 fprintf(out, "%.20Lg", val);
204 if (isfinite(val) && truncl(val) == val)
207 panic("unknown constant");
211 switch (type->atomic.akind) {
212 case ATOMIC_TYPE_UINT: suffix = "U"; break;
213 case ATOMIC_TYPE_LONG: suffix = "L"; break;
214 case ATOMIC_TYPE_ULONG: suffix = "UL"; break;
215 case ATOMIC_TYPE_LONGLONG: suffix = "LL"; break;
216 case ATOMIC_TYPE_ULONGLONG: suffix = "ULL"; break;
217 case ATOMIC_TYPE_FLOAT: suffix = "F"; break;
218 case ATOMIC_TYPE_LONG_DOUBLE: suffix = "L"; break;
220 default: suffix = NULL; break;
227 * Print a quoted string constant.
229 * @param string the string constant
230 * @param border the border char
231 * @param skip number of chars to skip at the end
233 static void print_quoted_string(const string_t *const string, char border, int skip)
236 const char *end = string->begin + string->size - skip;
237 for (const char *c = string->begin; c != end; ++c) {
242 case '\\': fputs("\\\\", out); break;
243 case '\a': fputs("\\a", out); break;
244 case '\b': fputs("\\b", out); break;
245 case '\f': fputs("\\f", out); break;
246 case '\n': fputs("\\n", out); break;
247 case '\r': fputs("\\r", out); break;
248 case '\t': fputs("\\t", out); break;
249 case '\v': fputs("\\v", out); break;
250 case '\?': fputs("\\?", out); break;
252 if (c_mode & _GNUC) {
253 fputs("\\e", out); break;
258 fprintf(out, "\\%03o", (unsigned)*c);
269 * Prints a wide string literal expression.
271 * @param wstr the wide string literal expression
272 * @param border the border char
273 * @param skip number of chars to skip at the end
275 static void print_quoted_wide_string(const wide_string_t *const wstr,
276 char border, int skip)
280 const wchar_rep_t *end = wstr->begin + wstr->size - skip;
281 for (const wchar_rep_t *c = wstr->begin; c != end; ++c) {
283 case L'\"': fputs("\\\"", out); break;
284 case L'\\': fputs("\\\\", out); break;
285 case L'\a': fputs("\\a", out); break;
286 case L'\b': fputs("\\b", out); break;
287 case L'\f': fputs("\\f", out); break;
288 case L'\n': fputs("\\n", out); break;
289 case L'\r': fputs("\\r", out); break;
290 case L'\t': fputs("\\t", out); break;
291 case L'\v': fputs("\\v", out); break;
292 case L'\?': fputs("\\?", out); break;
294 if (c_mode & _GNUC) {
295 fputs("\\e", out); break;
299 const unsigned tc = *c;
302 fprintf(out, "\\%03o", (char)*c);
306 } else if (tc < 0x800) {
307 fputc(0xC0 | (tc >> 6), out);
308 fputc(0x80 | (tc & 0x3F), out);
309 } else if (tc < 0x10000) {
310 fputc(0xE0 | ( tc >> 12), out);
311 fputc(0x80 | ((tc >> 6) & 0x3F), out);
312 fputc(0x80 | ( tc & 0x3F), out);
314 fputc(0xF0 | ( tc >> 18), out);
315 fputc(0x80 | ((tc >> 12) & 0x3F), out);
316 fputc(0x80 | ((tc >> 6) & 0x3F), out);
317 fputc(0x80 | ( tc & 0x3F), out);
326 * Print a constant character expression.
328 * @param cnst the constant character expression
330 static void print_character_constant(const const_expression_t *cnst)
332 print_quoted_string(&cnst->v.character, '\'', 0);
335 static void print_wide_character_constant(const const_expression_t *cnst)
337 print_quoted_wide_string(&cnst->v.wide_character, '\'', 0);
341 * Prints a string literal expression.
343 * @param string_literal the string literal expression
345 static void print_string_literal(
346 const string_literal_expression_t *string_literal)
348 print_quoted_string(&string_literal->value, '"', 1);
352 * Prints a predefined symbol.
354 static void print_funcname(
355 const funcname_expression_t *funcname)
358 switch(funcname->kind) {
359 case FUNCNAME_FUNCTION: s = (c_mode & _C99) ? "__func__" : "__FUNCTION__"; break;
360 case FUNCNAME_PRETTY_FUNCTION: s = "__PRETTY_FUNCTION__"; break;
361 case FUNCNAME_FUNCSIG: s = "__FUNCSIG__"; break;
362 case FUNCNAME_FUNCDNAME: s = "__FUNCDNAME__"; break;
369 static void print_wide_string_literal(
370 const wide_string_literal_expression_t *const wstr)
372 print_quoted_wide_string(&wstr->value, '"', 1);
375 static void print_compound_literal(
376 const compound_literal_expression_t *expression)
379 print_type(expression->type);
381 print_initializer(expression->initializer);
385 * Prints a call expression.
387 * @param call the call expression
389 static void print_call_expression(const call_expression_t *call)
391 unsigned prec = get_expression_precedence(call->base.kind);
392 print_expression_prec(call->function, prec);
394 call_argument_t *argument = call->arguments;
396 while(argument != NULL) {
402 print_expression_prec(argument->expression, 2 * PREC_ASSIGNMENT);
404 argument = argument->next;
410 * Prints a binary expression.
412 * @param binexpr the binary expression
414 static void print_binary_expression(const binary_expression_t *binexpr)
416 unsigned prec = get_expression_precedence(binexpr->base.kind);
417 int r2l = right_to_left(prec);
419 if(binexpr->base.kind == EXPR_BINARY_BUILTIN_EXPECT) {
420 fputs("__builtin_expect(", out);
421 print_expression_prec(binexpr->left, prec);
423 print_expression_prec(binexpr->right, prec);
428 print_expression_prec(binexpr->left, prec + r2l);
430 switch (binexpr->base.kind) {
431 case EXPR_BINARY_COMMA: op = ", "; break;
432 case EXPR_BINARY_ASSIGN: op = " = "; break;
433 case EXPR_BINARY_ADD: op = " + "; break;
434 case EXPR_BINARY_SUB: op = " - "; break;
435 case EXPR_BINARY_MUL: op = " * "; break;
436 case EXPR_BINARY_MOD: op = " % "; break;
437 case EXPR_BINARY_DIV: op = " / "; break;
438 case EXPR_BINARY_BITWISE_OR: op = " | "; break;
439 case EXPR_BINARY_BITWISE_AND: op = " & "; break;
440 case EXPR_BINARY_BITWISE_XOR: op = " ^ "; break;
441 case EXPR_BINARY_LOGICAL_OR: op = " || "; break;
442 case EXPR_BINARY_LOGICAL_AND: op = " && "; break;
443 case EXPR_BINARY_NOTEQUAL: op = " != "; break;
444 case EXPR_BINARY_EQUAL: op = " == "; break;
445 case EXPR_BINARY_LESS: op = " < "; break;
446 case EXPR_BINARY_LESSEQUAL: op = " <= "; break;
447 case EXPR_BINARY_GREATER: op = " > "; break;
448 case EXPR_BINARY_GREATEREQUAL: op = " >= "; break;
449 case EXPR_BINARY_SHIFTLEFT: op = " << "; break;
450 case EXPR_BINARY_SHIFTRIGHT: op = " >> "; break;
452 case EXPR_BINARY_ADD_ASSIGN: op = " += "; break;
453 case EXPR_BINARY_SUB_ASSIGN: op = " -= "; break;
454 case EXPR_BINARY_MUL_ASSIGN: op = " *= "; break;
455 case EXPR_BINARY_MOD_ASSIGN: op = " %= "; break;
456 case EXPR_BINARY_DIV_ASSIGN: op = " /= "; break;
457 case EXPR_BINARY_BITWISE_OR_ASSIGN: op = " |= "; break;
458 case EXPR_BINARY_BITWISE_AND_ASSIGN: op = " &= "; break;
459 case EXPR_BINARY_BITWISE_XOR_ASSIGN: op = " ^= "; break;
460 case EXPR_BINARY_SHIFTLEFT_ASSIGN: op = " <<= "; break;
461 case EXPR_BINARY_SHIFTRIGHT_ASSIGN: op = " >>= "; break;
462 default: panic("invalid binexpression found");
465 print_expression_prec(binexpr->right, prec - r2l);
469 * Prints an unary expression.
471 * @param unexpr the unary expression
473 static void print_unary_expression(const unary_expression_t *unexpr)
475 unsigned prec = get_expression_precedence(unexpr->base.kind);
476 switch(unexpr->base.kind) {
477 case EXPR_UNARY_NEGATE: fputc('-', out); break;
478 case EXPR_UNARY_PLUS: fputc('+', out); break;
479 case EXPR_UNARY_NOT: fputc('!', out); break;
480 case EXPR_UNARY_BITWISE_NEGATE: fputc('~', out); break;
481 case EXPR_UNARY_PREFIX_INCREMENT: fputs("++", out); break;
482 case EXPR_UNARY_PREFIX_DECREMENT: fputs("--", out); break;
483 case EXPR_UNARY_DEREFERENCE: fputc('*', out); break;
484 case EXPR_UNARY_TAKE_ADDRESS: fputc('&', out); break;
486 case EXPR_UNARY_POSTFIX_INCREMENT:
487 print_expression_prec(unexpr->value, prec);
490 case EXPR_UNARY_POSTFIX_DECREMENT:
491 print_expression_prec(unexpr->value, prec);
494 case EXPR_UNARY_CAST_IMPLICIT:
495 case EXPR_UNARY_CAST:
497 print_type(unexpr->base.type);
500 case EXPR_UNARY_ASSUME:
501 fputs("__assume(", out);
502 print_expression_prec(unexpr->value, 2 * PREC_ASSIGNMENT);
506 panic("invalid unary expression found");
508 print_expression_prec(unexpr->value, prec);
512 * Prints a reference expression.
514 * @param ref the reference expression
516 static void print_reference_expression(const reference_expression_t *ref)
518 fputs(ref->declaration->symbol->string, out);
522 * Prints a label address expression.
524 * @param ref the reference expression
526 static void print_label_address_expression(const label_address_expression_t *le)
528 fprintf(out, "&&%s", le->declaration->symbol->string);
532 * Prints an array expression.
534 * @param expression the array expression
536 static void print_array_expression(const array_access_expression_t *expression)
538 unsigned prec = get_expression_precedence(expression->base.kind);
539 if(!expression->flipped) {
540 print_expression_prec(expression->array_ref, prec);
542 print_expression(expression->index);
545 print_expression_prec(expression->index, prec);
547 print_expression(expression->array_ref);
553 * Prints a typeproperty expression (sizeof or __alignof__).
555 * @param expression the type property expression
557 static void print_typeprop_expression(const typeprop_expression_t *expression)
559 if (expression->base.kind == EXPR_SIZEOF) {
560 fputs("sizeof", out);
562 assert(expression->base.kind == EXPR_ALIGNOF);
563 fputs("__alignof__", out);
565 if(expression->tp_expression != NULL) {
566 /* always print the '()' here, sizeof x is right but unusual */
568 print_expression(expression->tp_expression);
572 print_type(expression->type);
578 * Prints an builtin symbol.
580 * @param expression the builtin symbol expression
582 static void print_builtin_symbol(const builtin_symbol_expression_t *expression)
584 fputs(expression->symbol->string, out);
588 * Prints a builtin constant expression.
590 * @param expression the builtin constant expression
592 static void print_builtin_constant(const builtin_constant_expression_t *expression)
594 fputs("__builtin_constant_p(", out);
595 print_expression_prec(expression->value, 2 * PREC_ASSIGNMENT);
600 * Prints a builtin prefetch expression.
602 * @param expression the builtin prefetch expression
604 static void print_builtin_prefetch(const builtin_prefetch_expression_t *expression)
606 fputs("__builtin_prefetch(", out);
607 print_expression_prec(expression->adr, 2 * PREC_ASSIGNMENT);
608 if (expression->rw) {
610 print_expression_prec(expression->rw, 2 * PREC_ASSIGNMENT);
612 if (expression->locality) {
614 print_expression_prec(expression->locality, 2 * PREC_ASSIGNMENT);
620 * Prints a conditional expression.
622 * @param expression the conditional expression
624 static void print_conditional(const conditional_expression_t *expression)
626 print_expression_prec(expression->condition, 2 * PREC_LOGICAL_OR);
628 if (expression->true_expression != NULL) {
629 print_expression_prec(expression->true_expression, 2 * PREC_EXPRESSION);
634 print_expression_prec(expression->false_expression, 2 * PREC_CONDITIONAL);
638 * Prints a va_start expression.
640 * @param expression the va_start expression
642 static void print_va_start(const va_start_expression_t *const expression)
644 fputs("__builtin_va_start(", out);
645 print_expression_prec(expression->ap, 2 * PREC_ASSIGNMENT);
647 fputs(expression->parameter->symbol->string, out);
652 * Prints a va_arg expression.
654 * @param expression the va_arg expression
656 static void print_va_arg(const va_arg_expression_t *expression)
658 fputs("__builtin_va_arg(", out);
659 print_expression_prec(expression->ap, 2 * PREC_ASSIGNMENT);
661 print_type(expression->base.type);
666 * Prints a select expression (. or ->).
668 * @param expression the select expression
670 static void print_select(const select_expression_t *expression)
672 unsigned prec = get_expression_precedence(expression->base.kind);
673 print_expression_prec(expression->compound, prec);
674 if(is_type_pointer(skip_typeref(expression->compound->base.type))) {
679 fputs(expression->compound_entry->symbol->string, out);
683 * Prints a type classify expression.
685 * @param expr the type classify expression
687 static void print_classify_type_expression(
688 const classify_type_expression_t *const expr)
690 fputs("__builtin_classify_type(", out);
691 print_expression_prec(expr->type_expression, 2 * PREC_ASSIGNMENT);
696 * Prints a designator.
698 * @param designator the designator
700 static void print_designator(const designator_t *designator)
702 for ( ; designator != NULL; designator = designator->next) {
703 if (designator->symbol == NULL) {
705 print_expression(designator->array_index);
709 fputs(designator->symbol->string, out);
715 * Prints an offsetof expression.
717 * @param expression the offset expression
719 static void print_offsetof_expression(const offsetof_expression_t *expression)
721 fputs("__builtin_offsetof", out);
723 print_type(expression->type);
725 print_designator(expression->designator);
730 * Prints a statement expression.
732 * @param expression the statement expression
734 static void print_statement_expression(const statement_expression_t *expression)
737 print_statement(expression->statement);
742 * Prints an expression with parenthesis if needed.
744 * @param expression the expression to print
745 * @param top_prec the precedence of the user of this expression.
747 static void print_expression_prec(const expression_t *expression, unsigned top_prec)
749 if (expression->kind == EXPR_UNARY_CAST_IMPLICIT && !print_implicit_casts) {
750 expression = expression->unary.value;
752 unsigned prec = get_expression_precedence(expression->base.kind);
753 if (print_parenthesis && top_prec != PREC_BOTTOM)
757 switch(expression->kind) {
760 fputs("$invalid expression$", out);
762 case EXPR_CHARACTER_CONSTANT:
763 print_character_constant(&expression->conste);
765 case EXPR_WIDE_CHARACTER_CONSTANT:
766 print_wide_character_constant(&expression->conste);
769 print_const(&expression->conste);
772 print_funcname(&expression->funcname);
774 case EXPR_STRING_LITERAL:
775 print_string_literal(&expression->string);
777 case EXPR_WIDE_STRING_LITERAL:
778 print_wide_string_literal(&expression->wide_string);
780 case EXPR_COMPOUND_LITERAL:
781 print_compound_literal(&expression->compound_literal);
784 print_call_expression(&expression->call);
787 print_binary_expression(&expression->binary);
790 print_reference_expression(&expression->reference);
792 case EXPR_ARRAY_ACCESS:
793 print_array_expression(&expression->array_access);
795 case EXPR_LABEL_ADDRESS:
796 print_label_address_expression(&expression->label_address);
799 print_unary_expression(&expression->unary);
803 print_typeprop_expression(&expression->typeprop);
805 case EXPR_BUILTIN_SYMBOL:
806 print_builtin_symbol(&expression->builtin_symbol);
808 case EXPR_BUILTIN_CONSTANT_P:
809 print_builtin_constant(&expression->builtin_constant);
811 case EXPR_BUILTIN_PREFETCH:
812 print_builtin_prefetch(&expression->builtin_prefetch);
814 case EXPR_CONDITIONAL:
815 print_conditional(&expression->conditional);
818 print_va_start(&expression->va_starte);
821 print_va_arg(&expression->va_arge);
824 print_select(&expression->select);
826 case EXPR_CLASSIFY_TYPE:
827 print_classify_type_expression(&expression->classify_type);
830 print_offsetof_expression(&expression->offsetofe);
833 print_statement_expression(&expression->statement);
838 fprintf(out, "some expression of type %d", (int) expression->kind);
846 * Print an compound statement.
848 * @param block the compound statement
850 static void print_compound_statement(const compound_statement_t *block)
855 statement_t *statement = block->statements;
856 while (statement != NULL) {
857 if (statement->base.kind == STATEMENT_CASE_LABEL)
859 if (statement->kind != STATEMENT_LABEL)
861 print_statement(statement);
863 statement = statement->base.next;
871 * Print a return statement.
873 * @param statement the return statement
875 static void print_return_statement(const return_statement_t *statement)
877 fputs("return ", out);
878 if(statement->value != NULL)
879 print_expression(statement->value);
884 * Print an expression statement.
886 * @param statement the expression statement
888 static void print_expression_statement(const expression_statement_t *statement)
890 print_expression(statement->expression);
895 * Print a goto statement.
897 * @param statement the goto statement
899 static void print_goto_statement(const goto_statement_t *statement)
902 if (statement->expression != NULL) {
904 print_expression(statement->expression);
906 fputs(statement->label->symbol->string, out);
912 * Print a label statement.
914 * @param statement the label statement
916 static void print_label_statement(const label_statement_t *statement)
918 fprintf(out, "%s:\n", statement->label->symbol->string);
920 print_statement(statement->statement);
924 * Print an if statement.
926 * @param statement the if statement
928 static void print_if_statement(const if_statement_t *statement)
931 print_expression(statement->condition);
933 print_statement(statement->true_statement);
935 if(statement->false_statement != NULL) {
938 print_statement(statement->false_statement);
943 * Print a switch statement.
945 * @param statement the switch statement
947 static void print_switch_statement(const switch_statement_t *statement)
949 fputs("switch (", out);
950 print_expression(statement->expression);
952 print_statement(statement->body);
956 * Print a case label (including the default label).
958 * @param statement the case label statement
960 static void print_case_label(const case_label_statement_t *statement)
962 if(statement->expression == NULL) {
963 fputs("default:\n", out);
966 print_expression(statement->expression);
967 if (statement->end_range != NULL) {
969 print_expression(statement->end_range);
974 if(statement->statement != NULL) {
975 if (statement->statement->base.kind == STATEMENT_CASE_LABEL) {
979 print_statement(statement->statement);
984 * Print a declaration statement.
986 * @param statement the statement
988 static void print_declaration_statement(
989 const declaration_statement_t *statement)
992 declaration_t *declaration = statement->declarations_begin;
994 if (declaration->namespc == NAMESPACE_LOCAL_LABEL) {
995 fputs("__label__ ", out);
997 declaration != statement->declarations_end->next;
998 declaration = declaration->next) {
1004 fputs(declaration->symbol->string, out);
1009 declaration != statement->declarations_end->next;
1010 declaration = declaration->next) {
1011 if (declaration->storage_class == STORAGE_CLASS_ENUM_ENTRY)
1013 if (declaration->implicit)
1021 print_declaration(declaration);
1028 * Print a while statement.
1030 * @param statement the statement
1032 static void print_while_statement(const while_statement_t *statement)
1034 fputs("while (", out);
1035 print_expression(statement->condition);
1037 print_statement(statement->body);
1041 * Print a do-while statement.
1043 * @param statement the statement
1045 static void print_do_while_statement(const do_while_statement_t *statement)
1048 print_statement(statement->body);
1050 fputs("while (", out);
1051 print_expression(statement->condition);
1056 * Print a for statement.
1058 * @param statement the statement
1060 static void print_for_statement(const for_statement_t *statement)
1062 fputs("for (", out);
1063 declaration_t *decl = statement->scope.declarations;
1064 while (decl != NULL && decl->implicit)
1067 assert(statement->initialisation == NULL);
1068 print_declaration(decl);
1069 if (decl->next != NULL) {
1070 panic("multiple declarations in for statement not supported yet");
1074 if(statement->initialisation) {
1075 print_expression(statement->initialisation);
1079 if(statement->condition != NULL) {
1080 print_expression(statement->condition);
1083 if(statement->step != NULL) {
1084 print_expression(statement->step);
1087 print_statement(statement->body);
1091 * Print assembler arguments.
1093 * @param arguments the arguments
1095 static void print_asm_arguments(asm_argument_t *arguments)
1097 asm_argument_t *argument = arguments;
1098 for( ; argument != NULL; argument = argument->next) {
1099 if(argument != arguments)
1102 if(argument->symbol) {
1103 fprintf(out, "[%s] ", argument->symbol->string);
1105 print_quoted_string(&argument->constraints, '"', 1);
1107 print_expression(argument->expression);
1113 * Print assembler clobbers.
1115 * @param clobbers the clobbers
1117 static void print_asm_clobbers(asm_clobber_t *clobbers)
1119 asm_clobber_t *clobber = clobbers;
1120 for( ; clobber != NULL; clobber = clobber->next) {
1121 if(clobber != clobbers)
1124 print_quoted_string(&clobber->clobber, '"', 1);
1129 * Print an assembler statement.
1131 * @param statement the statement
1133 static void print_asm_statement(const asm_statement_t *statement)
1136 if(statement->is_volatile) {
1137 fputs("volatile ", out);
1140 print_quoted_string(&statement->asm_text, '"', 1);
1141 if (statement->outputs == NULL &&
1142 statement->inputs == NULL &&
1143 statement->clobbers == NULL)
1144 goto end_of_print_asm_statement;
1147 print_asm_arguments(statement->outputs);
1148 if (statement->inputs == NULL && statement->clobbers == NULL)
1149 goto end_of_print_asm_statement;
1152 print_asm_arguments(statement->inputs);
1153 if (statement->clobbers == NULL)
1154 goto end_of_print_asm_statement;
1157 print_asm_clobbers(statement->clobbers);
1159 end_of_print_asm_statement:
1164 * Print a microsoft __try statement.
1166 * @param statement the statement
1168 static void print_ms_try_statement(const ms_try_statement_t *statement)
1170 fputs("__try ", out);
1171 print_statement(statement->try_statement);
1173 if(statement->except_expression != NULL) {
1174 fputs("__except(", out);
1175 print_expression(statement->except_expression);
1178 fputs("__finally ", out);
1180 print_statement(statement->final_statement);
1184 * Print a microsoft __leave statement.
1186 * @param statement the statement
1188 static void print_leave_statement(const leave_statement_t *statement)
1191 fputs("__leave;\n", out);
1195 * Print a statement.
1197 * @param statement the statement
1199 void print_statement(const statement_t *statement)
1201 switch (statement->kind) {
1202 case STATEMENT_EMPTY:
1205 case STATEMENT_COMPOUND:
1206 print_compound_statement(&statement->compound);
1208 case STATEMENT_RETURN:
1209 print_return_statement(&statement->returns);
1211 case STATEMENT_EXPRESSION:
1212 print_expression_statement(&statement->expression);
1214 case STATEMENT_LABEL:
1215 print_label_statement(&statement->label);
1217 case STATEMENT_GOTO:
1218 print_goto_statement(&statement->gotos);
1220 case STATEMENT_CONTINUE:
1221 fputs("continue;\n", out);
1223 case STATEMENT_BREAK:
1224 fputs("break;\n", out);
1227 print_if_statement(&statement->ifs);
1229 case STATEMENT_SWITCH:
1230 print_switch_statement(&statement->switchs);
1232 case STATEMENT_CASE_LABEL:
1233 print_case_label(&statement->case_label);
1235 case STATEMENT_DECLARATION:
1236 print_declaration_statement(&statement->declaration);
1238 case STATEMENT_WHILE:
1239 print_while_statement(&statement->whiles);
1241 case STATEMENT_DO_WHILE:
1242 print_do_while_statement(&statement->do_while);
1245 print_for_statement(&statement->fors);
1248 print_asm_statement(&statement->asms);
1250 case STATEMENT_MS_TRY:
1251 print_ms_try_statement(&statement->ms_try);
1253 case STATEMENT_LEAVE:
1254 print_leave_statement(&statement->leave);
1256 case STATEMENT_INVALID:
1257 fputs("$invalid statement$\n", out);
1263 * Print a storage class.
1265 * @param storage_class the storage class
1267 static void print_storage_class(storage_class_tag_t storage_class)
1269 switch(storage_class) {
1270 case STORAGE_CLASS_ENUM_ENTRY:
1271 case STORAGE_CLASS_NONE:
1273 case STORAGE_CLASS_TYPEDEF: fputs("typedef ", out); break;
1274 case STORAGE_CLASS_EXTERN: fputs("extern ", out); break;
1275 case STORAGE_CLASS_STATIC: fputs("static ", out); break;
1276 case STORAGE_CLASS_AUTO: fputs("auto ", out); break;
1277 case STORAGE_CLASS_REGISTER: fputs("register ", out); break;
1278 case STORAGE_CLASS_THREAD: fputs("__thread", out); break;
1279 case STORAGE_CLASS_THREAD_EXTERN: fputs("extern __thread", out); break;
1280 case STORAGE_CLASS_THREAD_STATIC: fputs("static __thread", out); break;
1285 * Print an initializer.
1287 * @param initializer the initializer
1289 void print_initializer(const initializer_t *initializer)
1291 if(initializer == NULL) {
1296 switch(initializer->kind) {
1297 case INITIALIZER_VALUE: {
1298 const initializer_value_t *value = &initializer->value;
1299 print_expression(value->value);
1302 case INITIALIZER_LIST: {
1303 assert(initializer->kind == INITIALIZER_LIST);
1305 const initializer_list_t *list = &initializer->list;
1307 for(size_t i = 0 ; i < list->len; ++i) {
1308 const initializer_t *sub_init = list->initializers[i];
1309 print_initializer(list->initializers[i]);
1310 if(i < list->len-1) {
1311 if(sub_init == NULL || sub_init->kind != INITIALIZER_DESIGNATOR)
1318 case INITIALIZER_STRING:
1319 print_quoted_string(&initializer->string.string, '"', 1);
1321 case INITIALIZER_WIDE_STRING:
1322 print_quoted_wide_string(&initializer->wide_string.string, '"', 1);
1324 case INITIALIZER_DESIGNATOR:
1325 print_designator(initializer->designator.designator);
1330 panic("invalid initializer kind found");
1334 * Print microsoft extended declaration modifiers.
1336 static void print_ms_modifiers(const declaration_t *declaration) {
1337 if((c_mode & _MS) == 0)
1340 decl_modifiers_t modifiers = declaration->modifiers;
1342 /* DM_FORCEINLINE handled outside. */
1343 if ((modifiers & ~DM_FORCEINLINE) != 0 ||
1344 declaration->alignment != 0 ||
1345 declaration->get_property_sym != NULL ||
1346 declaration->put_property_sym != NULL) {
1349 fputs("__declspec", out);
1350 if(modifiers & DM_DLLIMPORT) {
1351 fputs(next, out); next = ", "; fputs("dllimport", out);
1353 if(modifiers & DM_DLLEXPORT) {
1354 fputs(next, out); next = ", "; fputs("dllexport", out);
1356 if(modifiers & DM_THREAD) {
1357 fputs(next, out); next = ", "; fputs("thread", out);
1359 if(modifiers & DM_NAKED) {
1360 fputs(next, out); next = ", "; fputs("naked", out);
1362 if(modifiers & DM_THREAD) {
1363 fputs(next, out); next = ", "; fputs("thread", out);
1365 if(modifiers & DM_SELECTANY) {
1366 fputs(next, out); next = ", "; fputs("selectany", out);
1368 if(modifiers & DM_NOTHROW) {
1369 fputs(next, out); next = ", "; fputs("nothrow", out);
1371 if(modifiers & DM_NORETURN) {
1372 fputs(next, out); next = ", "; fputs("noreturn", out);
1374 if(modifiers & DM_NOINLINE) {
1375 fputs(next, out); next = ", "; fputs("noinline", out);
1377 if (modifiers & DM_DEPRECATED) {
1378 fputs(next, out); next = ", "; fputs("deprecated", out);
1379 if(declaration->deprecated_string != NULL)
1380 fprintf(out, "(\"%s\")", declaration->deprecated_string);
1382 if(declaration->alignment != 0) {
1383 fputs(next, out); next = ", "; fprintf(out, "align(%u)", declaration->alignment);
1385 if(modifiers & DM_RESTRICT) {
1386 fputs(next, out); next = ", "; fputs("restrict", out);
1388 if(modifiers & DM_NOALIAS) {
1389 fputs(next, out); next = ", "; fputs("noalias", out);
1391 if(declaration->get_property_sym != NULL || declaration->put_property_sym != NULL) {
1393 fputs(next, out); next = ", "; fputs("property(", out);
1394 if(declaration->get_property_sym != NULL) {
1395 fprintf(out, "get=%s", declaration->get_property_sym->string);
1398 if(declaration->put_property_sym != NULL)
1399 fprintf(out, "%sput=%s", comma, declaration->put_property_sym->string);
1407 * Print a declaration in the NORMAL namespace.
1409 * @param declaration the declaration
1411 static void print_normal_declaration(const declaration_t *declaration)
1413 print_storage_class((storage_class_tag_t) declaration->declared_storage_class);
1414 if (declaration->is_inline) {
1415 if (declaration->modifiers & DM_FORCEINLINE) {
1416 fputs("__forceinline ", out);
1417 } else if (declaration->modifiers & DM_MICROSOFT_INLINE) {
1418 fputs("__inline ", out);
1420 fputs("inline ", out);
1423 print_ms_modifiers(declaration);
1424 print_type_ext(declaration->type, declaration->symbol,
1425 &declaration->scope);
1427 if(declaration->type->kind == TYPE_FUNCTION) {
1428 if(declaration->init.statement != NULL) {
1431 print_statement(declaration->init.statement);
1434 } else if(declaration->init.initializer != NULL) {
1436 print_initializer(declaration->init.initializer);
1442 * Prints an expression.
1444 * @param expression the expression
1446 void print_expression(const expression_t *expression)
1448 print_expression_prec(expression, 2 * PREC_BOTTOM);
1452 * Print a declaration.
1454 * @param declaration the declaration
1456 void print_declaration(const declaration_t *declaration)
1458 if (declaration->namespc != NAMESPACE_NORMAL &&
1459 declaration->symbol == NULL)
1462 switch (declaration->namespc) {
1463 case NAMESPACE_NORMAL:
1464 print_normal_declaration(declaration);
1466 case NAMESPACE_STRUCT:
1467 fputs("struct ", out);
1468 fputs(declaration->symbol->string, out);
1469 if (declaration->init.complete) {
1471 print_compound_definition(declaration);
1475 case NAMESPACE_UNION:
1476 fputs("union ", out);
1477 fputs(declaration->symbol->string, out);
1478 if (declaration->init.complete) {
1480 print_compound_definition(declaration);
1484 case NAMESPACE_ENUM:
1485 fputs("enum ", out);
1486 fputs(declaration->symbol->string, out);
1487 if (declaration->init.complete) {
1489 print_enum_definition(declaration);
1497 * Print the AST of a translation unit.
1499 * @param unit the translation unit
1501 void print_ast(const translation_unit_t *unit)
1505 declaration_t *declaration = unit->scope.declarations;
1506 for( ; declaration != NULL; declaration = declaration->next) {
1507 if(declaration->storage_class == STORAGE_CLASS_ENUM_ENTRY)
1509 if(declaration->namespc != NAMESPACE_NORMAL &&
1510 declaration->symbol == NULL)
1512 if (declaration->implicit)
1516 print_declaration(declaration);
1521 bool is_constant_initializer(const initializer_t *initializer)
1523 switch(initializer->kind) {
1524 case INITIALIZER_STRING:
1525 case INITIALIZER_WIDE_STRING:
1526 case INITIALIZER_DESIGNATOR:
1529 case INITIALIZER_VALUE:
1530 return is_constant_expression(initializer->value.value);
1532 case INITIALIZER_LIST:
1533 for(size_t i = 0; i < initializer->list.len; ++i) {
1534 initializer_t *sub_initializer = initializer->list.initializers[i];
1535 if(!is_constant_initializer(sub_initializer))
1540 panic("invalid initializer kind found");
1543 static bool is_object_with_linker_constant_address(const expression_t *expression)
1545 switch(expression->kind) {
1546 case EXPR_UNARY_DEREFERENCE:
1547 return is_address_constant(expression->unary.value);
1550 type_t *base_type = skip_typeref(expression->select.compound->base.type);
1551 if(is_type_pointer(base_type)) {
1553 return is_address_constant(expression->select.compound);
1555 return is_object_with_linker_constant_address(expression->select.compound);
1559 case EXPR_ARRAY_ACCESS:
1560 return is_constant_expression(expression->array_access.index)
1561 && is_address_constant(expression->array_access.array_ref);
1563 case EXPR_REFERENCE: {
1564 declaration_t *declaration = expression->reference.declaration;
1565 switch((storage_class_tag_t) declaration->storage_class) {
1566 case STORAGE_CLASS_NONE:
1567 case STORAGE_CLASS_EXTERN:
1568 case STORAGE_CLASS_STATIC:
1580 bool is_address_constant(const expression_t *expression)
1582 switch(expression->kind) {
1583 case EXPR_UNARY_TAKE_ADDRESS:
1584 return is_object_with_linker_constant_address(expression->unary.value);
1586 case EXPR_UNARY_DEREFERENCE: {
1588 = revert_automatic_type_conversion(expression->unary.value);
1589 /* dereferencing a function is a NOP */
1590 if(is_type_function(real_type)) {
1591 return is_address_constant(expression->unary.value);
1597 case EXPR_UNARY_CAST: {
1598 type_t *dest = skip_typeref(expression->base.type);
1599 if (!is_type_pointer(dest) && (
1600 dest->kind != TYPE_ATOMIC ||
1601 !(get_atomic_type_flags(dest->atomic.akind) & ATOMIC_TYPE_FLAG_INTEGER) ||
1602 (get_atomic_type_size(dest->atomic.akind) < get_atomic_type_size(get_intptr_kind())
1606 return (is_constant_expression(expression->unary.value)
1607 || is_address_constant(expression->unary.value));
1610 case EXPR_BINARY_ADD:
1611 case EXPR_BINARY_SUB: {
1612 expression_t *left = expression->binary.left;
1613 expression_t *right = expression->binary.right;
1615 if(is_type_pointer(skip_typeref(left->base.type))) {
1616 return is_address_constant(left) && is_constant_expression(right);
1617 } else if(is_type_pointer(skip_typeref(right->base.type))) {
1618 return is_constant_expression(left) && is_address_constant(right);
1624 case EXPR_REFERENCE: {
1625 declaration_t *declaration = expression->reference.declaration;
1626 type_t *type = skip_typeref(declaration->type);
1627 if(is_type_function(type))
1629 if(is_type_array(type)) {
1630 return is_object_with_linker_constant_address(expression);
1632 /* Prevent stray errors */
1633 if (!is_type_valid(type))
1638 case EXPR_ARRAY_ACCESS: {
1639 type_t *const type =
1640 skip_typeref(revert_automatic_type_conversion(expression));
1642 is_type_array(type) &&
1643 is_constant_expression(expression->array_access.index) &&
1644 is_address_constant(expression->array_access.array_ref);
1652 static bool is_builtin_const_call(const expression_t *expression)
1654 expression_t *function = expression->call.function;
1655 if (function->kind != EXPR_BUILTIN_SYMBOL) {
1659 symbol_t *symbol = function->builtin_symbol.symbol;
1661 switch (symbol->ID) {
1662 case T___builtin_huge_val:
1663 case T___builtin_inf:
1664 case T___builtin_inff:
1665 case T___builtin_infl:
1666 case T___builtin_nan:
1667 case T___builtin_nanf:
1668 case T___builtin_nanl:
1675 static bool is_constant_pointer(const expression_t *expression)
1677 if (is_constant_expression(expression))
1680 switch (expression->kind) {
1681 case EXPR_UNARY_CAST:
1682 return is_constant_pointer(expression->unary.value);
1688 static bool is_object_with_constant_address(const expression_t *expression)
1690 switch(expression->kind) {
1692 expression_t *compound = expression->select.compound;
1693 type_t *compound_type = compound->base.type;
1694 compound_type = skip_typeref(compound_type);
1695 if(is_type_pointer(compound_type)) {
1696 return is_constant_pointer(compound);
1698 return is_object_with_constant_address(compound);
1702 case EXPR_ARRAY_ACCESS: {
1703 array_access_expression_t const* const array_access =
1704 &expression->array_access;
1706 is_constant_expression(array_access->index) && (
1707 is_object_with_constant_address(array_access->array_ref) ||
1708 is_constant_pointer(array_access->array_ref)
1712 case EXPR_UNARY_DEREFERENCE:
1713 return is_constant_pointer(expression->unary.value);
1719 bool is_constant_expression(const expression_t *expression)
1721 switch (expression->kind) {
1724 case EXPR_CHARACTER_CONSTANT:
1725 case EXPR_WIDE_CHARACTER_CONSTANT:
1726 case EXPR_STRING_LITERAL:
1727 case EXPR_WIDE_STRING_LITERAL:
1728 case EXPR_CLASSIFY_TYPE:
1732 case EXPR_BUILTIN_CONSTANT_P:
1733 case EXPR_LABEL_ADDRESS:
1737 type_t *type = expression->typeprop.type;
1739 type = expression->typeprop.tp_expression->base.type;
1741 type = skip_typeref(type);
1742 if (is_type_array(type) && type->array.is_vla)
1747 case EXPR_BUILTIN_SYMBOL:
1748 case EXPR_BUILTIN_PREFETCH:
1752 case EXPR_STATEMENT:
1753 case EXPR_UNARY_POSTFIX_INCREMENT:
1754 case EXPR_UNARY_POSTFIX_DECREMENT:
1755 case EXPR_UNARY_PREFIX_INCREMENT:
1756 case EXPR_UNARY_PREFIX_DECREMENT:
1757 case EXPR_UNARY_ASSUME: /* has VOID type */
1758 case EXPR_UNARY_DEREFERENCE:
1759 case EXPR_BINARY_ASSIGN:
1760 case EXPR_BINARY_MUL_ASSIGN:
1761 case EXPR_BINARY_DIV_ASSIGN:
1762 case EXPR_BINARY_MOD_ASSIGN:
1763 case EXPR_BINARY_ADD_ASSIGN:
1764 case EXPR_BINARY_SUB_ASSIGN:
1765 case EXPR_BINARY_SHIFTLEFT_ASSIGN:
1766 case EXPR_BINARY_SHIFTRIGHT_ASSIGN:
1767 case EXPR_BINARY_BITWISE_AND_ASSIGN:
1768 case EXPR_BINARY_BITWISE_XOR_ASSIGN:
1769 case EXPR_BINARY_BITWISE_OR_ASSIGN:
1770 case EXPR_BINARY_COMMA:
1771 case EXPR_ARRAY_ACCESS:
1774 case EXPR_UNARY_TAKE_ADDRESS:
1775 return is_object_with_constant_address(expression->unary.value);
1778 return is_builtin_const_call(expression);
1780 case EXPR_UNARY_NEGATE:
1781 case EXPR_UNARY_PLUS:
1782 case EXPR_UNARY_BITWISE_NEGATE:
1783 case EXPR_UNARY_NOT:
1784 return is_constant_expression(expression->unary.value);
1786 case EXPR_UNARY_CAST:
1787 case EXPR_UNARY_CAST_IMPLICIT:
1788 return is_type_arithmetic(skip_typeref(expression->base.type))
1789 && is_constant_expression(expression->unary.value);
1791 case EXPR_BINARY_ADD:
1792 case EXPR_BINARY_SUB:
1793 case EXPR_BINARY_MUL:
1794 case EXPR_BINARY_DIV:
1795 case EXPR_BINARY_MOD:
1796 case EXPR_BINARY_EQUAL:
1797 case EXPR_BINARY_NOTEQUAL:
1798 case EXPR_BINARY_LESS:
1799 case EXPR_BINARY_LESSEQUAL:
1800 case EXPR_BINARY_GREATER:
1801 case EXPR_BINARY_GREATEREQUAL:
1802 case EXPR_BINARY_BITWISE_AND:
1803 case EXPR_BINARY_BITWISE_OR:
1804 case EXPR_BINARY_BITWISE_XOR:
1805 case EXPR_BINARY_LOGICAL_AND:
1806 case EXPR_BINARY_LOGICAL_OR:
1807 case EXPR_BINARY_SHIFTLEFT:
1808 case EXPR_BINARY_SHIFTRIGHT:
1809 case EXPR_BINARY_BUILTIN_EXPECT:
1810 case EXPR_BINARY_ISGREATER:
1811 case EXPR_BINARY_ISGREATEREQUAL:
1812 case EXPR_BINARY_ISLESS:
1813 case EXPR_BINARY_ISLESSEQUAL:
1814 case EXPR_BINARY_ISLESSGREATER:
1815 case EXPR_BINARY_ISUNORDERED:
1816 return is_constant_expression(expression->binary.left)
1817 && is_constant_expression(expression->binary.right);
1819 case EXPR_COMPOUND_LITERAL:
1820 return is_constant_initializer(expression->compound_literal.initializer);
1822 case EXPR_CONDITIONAL: {
1823 expression_t *condition = expression->conditional.condition;
1824 if(!is_constant_expression(condition))
1827 long val = fold_constant(condition);
1829 return is_constant_expression(expression->conditional.true_expression);
1831 return is_constant_expression(expression->conditional.false_expression);
1834 case EXPR_REFERENCE: {
1835 declaration_t *declaration = expression->reference.declaration;
1836 if(declaration->storage_class == STORAGE_CLASS_ENUM_ENTRY)
1848 panic("invalid expression found (is constant expression)");
1852 * Initialize the AST construction.
1856 obstack_init(&ast_obstack);
1864 obstack_free(&ast_obstack, NULL);
1868 * Set the output stream for the AST printer.
1870 * @param stream the output stream
1872 void ast_set_output(FILE *stream)
1875 type_set_output(stream);
1879 * Allocate an AST object of the given size.
1881 * @param size the size of the object to allocate
1883 * @return A new allocated object in the AST memeory space.
1885 void *(allocate_ast)(size_t size)
1887 return _allocate_ast(size);