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)
70 PREC_COMMA = 4, /* , left to right */
71 PREC_ASSIGN = 6, /* = += -= *= /= %= <<= >>= &= ^= |= right to left */
72 PREC_COND = 8, /* ?: right to left */
73 PREC_LOG_OR = 10, /* || left to right */
74 PREC_LOG_AND = 12, /* && left to right */
75 PREC_BIT_OR = 14, /* | left to right */
76 PREC_BIT_XOR = 16, /* ^ left to right */
77 PREC_BIT_AND = 18, /* & left to right */
78 PREC_EQ = 20, /* == != left to right */
79 PREC_CMP = 22, /* < <= > >= left to right */
80 PREC_SHF = 24, /* << >> left to right */
81 PREC_PLUS = 26, /* + - left to right */
82 PREC_MUL = 28, /* * / % left to right */
83 PREC_UNARY = 30, /* ! ~ ++ -- + - (type) * & sizeof right to left */
84 PREC_ACCESS = 32, /* () [] -> . left to right */
85 PREC_PRIM = 34, /* primary */
90 * Returns 1 if a given precedence level has right-to-left
91 * associativity, else -1.
93 * @param precedence the operator precedence
95 static int right_to_left(unsigned precedence) {
96 return (precedence == PREC_ASSIGN || precedence == PREC_COND ||
97 precedence == PREC_UNARY) ? 1 : -1;
101 * Return the precedence of an expression given by its kind.
103 * @param kind the expression kind
105 static unsigned get_expression_precedence(expression_kind_t kind)
107 static const unsigned prec[] = {
108 [EXPR_UNKNOWN] = PREC_PRIM,
109 [EXPR_INVALID] = PREC_PRIM,
110 [EXPR_REFERENCE] = PREC_PRIM,
111 [EXPR_CHARACTER_CONSTANT] = PREC_PRIM,
112 [EXPR_WIDE_CHARACTER_CONSTANT] = PREC_PRIM,
113 [EXPR_CONST] = PREC_PRIM,
114 [EXPR_STRING_LITERAL] = PREC_PRIM,
115 [EXPR_WIDE_STRING_LITERAL] = PREC_PRIM,
116 [EXPR_COMPOUND_LITERAL] = PREC_UNARY,
117 [EXPR_CALL] = PREC_ACCESS,
118 [EXPR_CONDITIONAL] = PREC_COND,
119 [EXPR_SELECT] = PREC_ACCESS,
120 [EXPR_ARRAY_ACCESS] = PREC_ACCESS,
121 [EXPR_SIZEOF] = PREC_UNARY,
122 [EXPR_CLASSIFY_TYPE] = PREC_UNARY,
123 [EXPR_ALIGNOF] = PREC_UNARY,
125 [EXPR_FUNCNAME] = PREC_PRIM,
126 [EXPR_BUILTIN_SYMBOL] = PREC_PRIM,
127 [EXPR_BUILTIN_CONSTANT_P] = PREC_PRIM,
128 [EXPR_BUILTIN_PREFETCH] = PREC_PRIM,
129 [EXPR_OFFSETOF] = PREC_PRIM,
130 [EXPR_VA_START] = PREC_PRIM,
131 [EXPR_VA_ARG] = PREC_PRIM,
132 [EXPR_STATEMENT] = PREC_ACCESS,
133 [EXPR_LABEL_ADDRESS] = PREC_PRIM,
135 [EXPR_UNARY_NEGATE] = PREC_UNARY,
136 [EXPR_UNARY_PLUS] = PREC_UNARY,
137 [EXPR_UNARY_BITWISE_NEGATE] = PREC_UNARY,
138 [EXPR_UNARY_NOT] = PREC_UNARY,
139 [EXPR_UNARY_DEREFERENCE] = PREC_UNARY,
140 [EXPR_UNARY_TAKE_ADDRESS] = PREC_UNARY,
141 [EXPR_UNARY_POSTFIX_INCREMENT] = PREC_UNARY,
142 [EXPR_UNARY_POSTFIX_DECREMENT] = PREC_UNARY,
143 [EXPR_UNARY_PREFIX_INCREMENT] = PREC_UNARY,
144 [EXPR_UNARY_PREFIX_DECREMENT] = PREC_UNARY,
145 [EXPR_UNARY_CAST] = PREC_UNARY,
146 [EXPR_UNARY_CAST_IMPLICIT] = PREC_UNARY,
147 [EXPR_UNARY_ASSUME] = PREC_PRIM,
149 [EXPR_BINARY_ADD] = PREC_PLUS,
150 [EXPR_BINARY_SUB] = PREC_PLUS,
151 [EXPR_BINARY_MUL] = PREC_MUL,
152 [EXPR_BINARY_DIV] = PREC_MUL,
153 [EXPR_BINARY_MOD] = PREC_MUL,
154 [EXPR_BINARY_EQUAL] = PREC_EQ,
155 [EXPR_BINARY_NOTEQUAL] = PREC_EQ,
156 [EXPR_BINARY_LESS] = PREC_CMP,
157 [EXPR_BINARY_LESSEQUAL] = PREC_CMP,
158 [EXPR_BINARY_GREATER] = PREC_CMP,
159 [EXPR_BINARY_GREATEREQUAL] = PREC_CMP,
160 [EXPR_BINARY_BITWISE_AND] = PREC_BIT_AND,
161 [EXPR_BINARY_BITWISE_OR] = PREC_BIT_OR,
162 [EXPR_BINARY_BITWISE_XOR] = PREC_BIT_XOR,
163 [EXPR_BINARY_LOGICAL_AND] = PREC_LOG_AND,
164 [EXPR_BINARY_LOGICAL_OR] = PREC_LOG_OR,
165 [EXPR_BINARY_SHIFTLEFT] = PREC_SHF,
166 [EXPR_BINARY_SHIFTRIGHT] = PREC_SHF,
167 [EXPR_BINARY_ASSIGN] = PREC_ASSIGN,
168 [EXPR_BINARY_MUL_ASSIGN] = PREC_ASSIGN,
169 [EXPR_BINARY_DIV_ASSIGN] = PREC_ASSIGN,
170 [EXPR_BINARY_MOD_ASSIGN] = PREC_ASSIGN,
171 [EXPR_BINARY_ADD_ASSIGN] = PREC_ASSIGN,
172 [EXPR_BINARY_SUB_ASSIGN] = PREC_ASSIGN,
173 [EXPR_BINARY_SHIFTLEFT_ASSIGN] = PREC_ASSIGN,
174 [EXPR_BINARY_SHIFTRIGHT_ASSIGN] = PREC_ASSIGN,
175 [EXPR_BINARY_BITWISE_AND_ASSIGN] = PREC_ASSIGN,
176 [EXPR_BINARY_BITWISE_XOR_ASSIGN] = PREC_ASSIGN,
177 [EXPR_BINARY_BITWISE_OR_ASSIGN] = PREC_ASSIGN,
178 [EXPR_BINARY_COMMA] = PREC_COMMA,
180 [EXPR_BINARY_BUILTIN_EXPECT] = PREC_PRIM,
181 [EXPR_BINARY_ISGREATER] = PREC_PRIM,
182 [EXPR_BINARY_ISGREATEREQUAL] = PREC_PRIM,
183 [EXPR_BINARY_ISLESS] = PREC_PRIM,
184 [EXPR_BINARY_ISLESSEQUAL] = PREC_PRIM,
185 [EXPR_BINARY_ISLESSGREATER] = PREC_PRIM,
186 [EXPR_BINARY_ISUNORDERED] = PREC_PRIM
188 assert((unsigned)kind < (sizeof(prec)/sizeof(prec[0])));
189 unsigned res = prec[kind];
191 assert(res != PREC_BOTTOM);
196 * Print a constant expression.
198 * @param cnst the constant expression
200 static void print_const(const const_expression_t *cnst)
202 if(cnst->base.type == NULL)
205 const type_t *const type = skip_typeref(cnst->base.type);
207 if (is_type_integer(type)) {
208 fprintf(out, "%lld", cnst->v.int_value);
209 } else if (is_type_float(type)) {
210 long double const val = cnst->v.float_value;
212 /* ARG, no way to print long double */
213 fprintf(out, "%.20g", (double)val);
215 fprintf(out, "%.20Lg", val);
217 if (isfinite(val) && truncl(val) == val)
220 panic("unknown constant");
224 switch (type->atomic.akind) {
225 case ATOMIC_TYPE_UINT: suffix = "U"; break;
226 case ATOMIC_TYPE_LONG: suffix = "L"; break;
227 case ATOMIC_TYPE_ULONG: suffix = "UL"; break;
228 case ATOMIC_TYPE_LONGLONG: suffix = "LL"; break;
229 case ATOMIC_TYPE_ULONGLONG: suffix = "ULL"; break;
230 case ATOMIC_TYPE_FLOAT: suffix = "F"; break;
231 case ATOMIC_TYPE_LONG_DOUBLE: suffix = "L"; break;
233 default: suffix = NULL; break;
240 * Print a quoted string constant.
242 * @param string the string constant
243 * @param border the border char
244 * @param skip number of chars to skip at the end
246 static void print_quoted_string(const string_t *const string, char border, int skip)
249 const char *end = string->begin + string->size - skip;
250 for (const char *c = string->begin; c != end; ++c) {
255 case '\\': fputs("\\\\", out); break;
256 case '\a': fputs("\\a", out); break;
257 case '\b': fputs("\\b", out); break;
258 case '\f': fputs("\\f", out); break;
259 case '\n': fputs("\\n", out); break;
260 case '\r': fputs("\\r", out); break;
261 case '\t': fputs("\\t", out); break;
262 case '\v': fputs("\\v", out); break;
263 case '\?': fputs("\\?", out); break;
265 if (c_mode & _GNUC) {
266 fputs("\\e", out); break;
271 fprintf(out, "\\%03o", *c);
282 * Prints a wide string literal expression.
284 * @param wstr the wide string literal expression
285 * @param border the border char
286 * @param skip number of chars to skip at the end
288 static void print_quoted_wide_string(const wide_string_t *const wstr,
289 char border, int skip)
293 const wchar_rep_t *end = wstr->begin + wstr->size - skip;
294 for (const wchar_rep_t *c = wstr->begin; c != end; ++c) {
296 case L'\"': fputs("\\\"", out); break;
297 case L'\\': fputs("\\\\", out); break;
298 case L'\a': fputs("\\a", out); break;
299 case L'\b': fputs("\\b", out); break;
300 case L'\f': fputs("\\f", out); break;
301 case L'\n': fputs("\\n", out); break;
302 case L'\r': fputs("\\r", out); break;
303 case L'\t': fputs("\\t", out); break;
304 case L'\v': fputs("\\v", out); break;
305 case L'\?': fputs("\\?", out); break;
307 if (c_mode & _GNUC) {
308 fputs("\\e", out); break;
312 const unsigned tc = *c;
315 fprintf(out, "\\%03o", (char)*c);
319 } else if (tc < 0x800) {
320 fputc(0xC0 | (tc >> 6), out);
321 fputc(0x80 | (tc & 0x3F), out);
322 } else if (tc < 0x10000) {
323 fputc(0xE0 | ( tc >> 12), out);
324 fputc(0x80 | ((tc >> 6) & 0x3F), out);
325 fputc(0x80 | ( tc & 0x3F), out);
327 fputc(0xF0 | ( tc >> 18), out);
328 fputc(0x80 | ((tc >> 12) & 0x3F), out);
329 fputc(0x80 | ((tc >> 6) & 0x3F), out);
330 fputc(0x80 | ( tc & 0x3F), out);
339 * Print a constant character expression.
341 * @param cnst the constant character expression
343 static void print_character_constant(const const_expression_t *cnst)
345 print_quoted_string(&cnst->v.character, '\'', 0);
348 static void print_wide_character_constant(const const_expression_t *cnst)
350 print_quoted_wide_string(&cnst->v.wide_character, '\'', 0);
354 * Prints a string literal expression.
356 * @param string_literal the string literal expression
358 static void print_string_literal(
359 const string_literal_expression_t *string_literal)
361 print_quoted_string(&string_literal->value, '"', 1);
365 * Prints a predefined symbol.
367 static void print_funcname(
368 const funcname_expression_t *funcname)
371 switch(funcname->kind) {
372 case FUNCNAME_FUNCTION: s = (c_mode & _C99) ? "__func__" : "__FUNCTION__"; break;
373 case FUNCNAME_PRETTY_FUNCTION: s = "__PRETTY_FUNCTION__"; break;
374 case FUNCNAME_FUNCSIG: s = "__FUNCSIG__"; break;
375 case FUNCNAME_FUNCDNAME: s = "__FUNCDNAME__"; break;
382 static void print_wide_string_literal(
383 const wide_string_literal_expression_t *const wstr)
385 print_quoted_wide_string(&wstr->value, '"', 1);
388 static void print_compound_literal(
389 const compound_literal_expression_t *expression)
392 print_type(expression->type);
394 print_initializer(expression->initializer);
398 * Prints a call expression.
400 * @param call the call expression
402 static void print_call_expression(const call_expression_t *call)
404 unsigned prec = get_expression_precedence(call->base.kind);
405 print_expression_prec(call->function, prec);
407 call_argument_t *argument = call->arguments;
409 while(argument != NULL) {
415 print_expression_prec(argument->expression, PREC_COMMA + 1);
417 argument = argument->next;
423 * Prints a binary expression.
425 * @param binexpr the binary expression
427 static void print_binary_expression(const binary_expression_t *binexpr)
429 unsigned prec = get_expression_precedence(binexpr->base.kind);
430 int r2l = right_to_left(prec);
432 if(binexpr->base.kind == EXPR_BINARY_BUILTIN_EXPECT) {
433 fputs("__builtin_expect(", out);
434 print_expression_prec(binexpr->left, prec);
436 print_expression_prec(binexpr->right, prec);
441 print_expression_prec(binexpr->left, prec + r2l);
442 if (binexpr->base.kind != EXPR_BINARY_COMMA) {
445 switch (binexpr->base.kind) {
446 case EXPR_BINARY_COMMA: fputs(",", out); break;
447 case EXPR_BINARY_ASSIGN: fputs("=", out); break;
448 case EXPR_BINARY_ADD: fputs("+", out); break;
449 case EXPR_BINARY_SUB: fputs("-", out); break;
450 case EXPR_BINARY_MUL: fputs("*", out); break;
451 case EXPR_BINARY_MOD: fputs("%", out); break;
452 case EXPR_BINARY_DIV: fputs("/", out); break;
453 case EXPR_BINARY_BITWISE_OR: fputs("|", out); break;
454 case EXPR_BINARY_BITWISE_AND: fputs("&", out); break;
455 case EXPR_BINARY_BITWISE_XOR: fputs("^", out); break;
456 case EXPR_BINARY_LOGICAL_OR: fputs("||", out); break;
457 case EXPR_BINARY_LOGICAL_AND: fputs("&&", out); break;
458 case EXPR_BINARY_NOTEQUAL: fputs("!=", out); break;
459 case EXPR_BINARY_EQUAL: fputs("==", out); break;
460 case EXPR_BINARY_LESS: fputs("<", out); break;
461 case EXPR_BINARY_LESSEQUAL: fputs("<=", out); break;
462 case EXPR_BINARY_GREATER: fputs(">", out); break;
463 case EXPR_BINARY_GREATEREQUAL: fputs(">=", out); break;
464 case EXPR_BINARY_SHIFTLEFT: fputs("<<", out); break;
465 case EXPR_BINARY_SHIFTRIGHT: fputs(">>", out); break;
467 case EXPR_BINARY_ADD_ASSIGN: fputs("+=", out); break;
468 case EXPR_BINARY_SUB_ASSIGN: fputs("-=", out); break;
469 case EXPR_BINARY_MUL_ASSIGN: fputs("*=", out); break;
470 case EXPR_BINARY_MOD_ASSIGN: fputs("%=", out); break;
471 case EXPR_BINARY_DIV_ASSIGN: fputs("/=", out); break;
472 case EXPR_BINARY_BITWISE_OR_ASSIGN: fputs("|=", out); break;
473 case EXPR_BINARY_BITWISE_AND_ASSIGN: fputs("&=", out); break;
474 case EXPR_BINARY_BITWISE_XOR_ASSIGN: fputs("^=", out); break;
475 case EXPR_BINARY_SHIFTLEFT_ASSIGN: fputs("<<=", out); break;
476 case EXPR_BINARY_SHIFTRIGHT_ASSIGN: fputs(">>=", out); break;
477 default: panic("invalid binexpression found");
480 print_expression_prec(binexpr->right, prec - r2l);
484 * Prints an unary expression.
486 * @param unexpr the unary expression
488 static void print_unary_expression(const unary_expression_t *unexpr)
490 unsigned prec = get_expression_precedence(unexpr->base.kind);
491 switch(unexpr->base.kind) {
492 case EXPR_UNARY_NEGATE: fputc('-', out); break;
493 case EXPR_UNARY_PLUS: fputc('+', out); break;
494 case EXPR_UNARY_NOT: fputc('!', out); break;
495 case EXPR_UNARY_BITWISE_NEGATE: fputc('~', out); break;
496 case EXPR_UNARY_PREFIX_INCREMENT: fputs("++", out); break;
497 case EXPR_UNARY_PREFIX_DECREMENT: fputs("--", out); break;
498 case EXPR_UNARY_DEREFERENCE: fputc('*', out); break;
499 case EXPR_UNARY_TAKE_ADDRESS: fputc('&', out); break;
501 case EXPR_UNARY_POSTFIX_INCREMENT:
502 print_expression_prec(unexpr->value, prec);
505 case EXPR_UNARY_POSTFIX_DECREMENT:
506 print_expression_prec(unexpr->value, prec);
509 case EXPR_UNARY_CAST_IMPLICIT:
510 case EXPR_UNARY_CAST:
512 print_type(unexpr->base.type);
515 case EXPR_UNARY_ASSUME:
516 fputs("__assume(", out);
517 print_expression_prec(unexpr->value, PREC_COMMA + 1);
521 panic("invalid unary expression found");
523 print_expression_prec(unexpr->value, prec);
527 * Prints a reference expression.
529 * @param ref the reference expression
531 static void print_reference_expression(const reference_expression_t *ref)
533 fputs(ref->declaration->symbol->string, out);
537 * Prints a label address expression.
539 * @param ref the reference expression
541 static void print_label_address_expression(const label_address_expression_t *le)
543 fprintf(out, "&&%s", le->declaration->symbol->string);
547 * Prints an array expression.
549 * @param expression the array expression
551 static void print_array_expression(const array_access_expression_t *expression)
553 unsigned prec = get_expression_precedence(expression->base.kind);
554 if(!expression->flipped) {
555 print_expression_prec(expression->array_ref, prec);
557 print_expression_prec(expression->index, PREC_BOTTOM);
560 print_expression_prec(expression->index, prec);
562 print_expression_prec(expression->array_ref, PREC_BOTTOM);
568 * Prints a typeproperty expression (sizeof or __alignof__).
570 * @param expression the type property expression
572 static void print_typeprop_expression(const typeprop_expression_t *expression)
574 if (expression->base.kind == EXPR_SIZEOF) {
575 fputs("sizeof", out);
577 assert(expression->base.kind == EXPR_ALIGNOF);
578 fputs("__alignof__", out);
580 if(expression->tp_expression != NULL) {
581 /* always print the '()' here, sizeof x is right but unusual */
583 print_expression_prec(expression->tp_expression, PREC_ACCESS);
587 print_type(expression->type);
593 * Prints an builtin symbol.
595 * @param expression the builtin symbol expression
597 static void print_builtin_symbol(const builtin_symbol_expression_t *expression)
599 fputs(expression->symbol->string, out);
603 * Prints a builtin constant expression.
605 * @param expression the builtin constant expression
607 static void print_builtin_constant(const builtin_constant_expression_t *expression)
609 fputs("__builtin_constant_p(", out);
610 print_expression_prec(expression->value, PREC_COMMA + 1);
615 * Prints a builtin prefetch expression.
617 * @param expression the builtin prefetch expression
619 static void print_builtin_prefetch(const builtin_prefetch_expression_t *expression)
621 fputs("__builtin_prefetch(", out);
622 print_expression_prec(expression->adr, PREC_COMMA + 1);
623 if (expression->rw) {
625 print_expression_prec(expression->rw, PREC_COMMA + 1);
627 if (expression->locality) {
629 print_expression_prec(expression->locality, PREC_COMMA + 1);
635 * Prints a conditional expression.
637 * @param expression the conditional expression
639 static void print_conditional(const conditional_expression_t *expression)
641 print_expression_prec(expression->condition, PREC_LOG_OR);
643 if (expression->true_expression != NULL) {
644 print_expression_prec(expression->true_expression, PREC_EXPR);
649 print_expression_prec(expression->false_expression, PREC_COND);
653 * Prints a va_start expression.
655 * @param expression the va_start expression
657 static void print_va_start(const va_start_expression_t *const expression)
659 fputs("__builtin_va_start(", out);
660 print_expression_prec(expression->ap, PREC_COMMA + 1);
662 fputs(expression->parameter->symbol->string, out);
667 * Prints a va_arg expression.
669 * @param expression the va_arg expression
671 static void print_va_arg(const va_arg_expression_t *expression)
673 fputs("__builtin_va_arg(", out);
674 print_expression_prec(expression->ap, PREC_COMMA + 1);
676 print_type(expression->base.type);
681 * Prints a select expression (. or ->).
683 * @param expression the select expression
685 static void print_select(const select_expression_t *expression)
687 unsigned prec = get_expression_precedence(expression->base.kind);
688 print_expression_prec(expression->compound, prec);
689 if(is_type_pointer(skip_typeref(expression->compound->base.type))) {
694 fputs(expression->compound_entry->symbol->string, out);
698 * Prints a type classify expression.
700 * @param expr the type classify expression
702 static void print_classify_type_expression(
703 const classify_type_expression_t *const expr)
705 fputs("__builtin_classify_type(", out);
706 print_expression_prec(expr->type_expression, PREC_COMMA + 1);
711 * Prints a designator.
713 * @param designator the designator
715 static void print_designator(const designator_t *designator)
717 for ( ; designator != NULL; designator = designator->next) {
718 if (designator->symbol == NULL) {
720 print_expression_prec(designator->array_index, PREC_BOTTOM);
724 fputs(designator->symbol->string, out);
730 * Prints an offsetof expression.
732 * @param expression the offset expression
734 static void print_offsetof_expression(const offsetof_expression_t *expression)
736 fputs("__builtin_offsetof", out);
738 print_type(expression->type);
740 print_designator(expression->designator);
745 * Prints a statement expression.
747 * @param expression the statement expression
749 static void print_statement_expression(const statement_expression_t *expression)
752 print_statement(expression->statement);
757 * Prints an expression with parenthesis if needed.
759 * @param expression the expression to print
760 * @param top_prec the precedence of the user of this expression.
762 static void print_expression_prec(const expression_t *expression, unsigned top_prec)
764 if (expression->kind == EXPR_UNARY_CAST_IMPLICIT && !print_implicit_casts) {
765 expression = expression->unary.value;
767 unsigned prec = get_expression_precedence(expression->base.kind);
768 if (print_parenthesis && top_prec != PREC_BOTTOM)
772 switch(expression->kind) {
775 fputs("$invalid expression$", out);
777 case EXPR_CHARACTER_CONSTANT:
778 print_character_constant(&expression->conste);
780 case EXPR_WIDE_CHARACTER_CONSTANT:
781 print_wide_character_constant(&expression->conste);
784 print_const(&expression->conste);
787 print_funcname(&expression->funcname);
789 case EXPR_STRING_LITERAL:
790 print_string_literal(&expression->string);
792 case EXPR_WIDE_STRING_LITERAL:
793 print_wide_string_literal(&expression->wide_string);
795 case EXPR_COMPOUND_LITERAL:
796 print_compound_literal(&expression->compound_literal);
799 print_call_expression(&expression->call);
802 print_binary_expression(&expression->binary);
805 print_reference_expression(&expression->reference);
807 case EXPR_ARRAY_ACCESS:
808 print_array_expression(&expression->array_access);
810 case EXPR_LABEL_ADDRESS:
811 print_label_address_expression(&expression->label_address);
814 print_unary_expression(&expression->unary);
818 print_typeprop_expression(&expression->typeprop);
820 case EXPR_BUILTIN_SYMBOL:
821 print_builtin_symbol(&expression->builtin_symbol);
823 case EXPR_BUILTIN_CONSTANT_P:
824 print_builtin_constant(&expression->builtin_constant);
826 case EXPR_BUILTIN_PREFETCH:
827 print_builtin_prefetch(&expression->builtin_prefetch);
829 case EXPR_CONDITIONAL:
830 print_conditional(&expression->conditional);
833 print_va_start(&expression->va_starte);
836 print_va_arg(&expression->va_arge);
839 print_select(&expression->select);
841 case EXPR_CLASSIFY_TYPE:
842 print_classify_type_expression(&expression->classify_type);
845 print_offsetof_expression(&expression->offsetofe);
848 print_statement_expression(&expression->statement);
853 fprintf(out, "some expression of type %d", (int) expression->kind);
861 * Print an compound statement.
863 * @param block the compound statement
865 static void print_compound_statement(const compound_statement_t *block)
870 statement_t *statement = block->statements;
871 while (statement != NULL) {
872 if (statement->base.kind == STATEMENT_CASE_LABEL)
874 if (statement->kind != STATEMENT_LABEL)
876 print_statement(statement);
878 statement = statement->base.next;
886 * Print a return statement.
888 * @param statement the return statement
890 static void print_return_statement(const return_statement_t *statement)
892 fputs("return ", out);
893 if(statement->value != NULL)
894 print_expression(statement->value);
899 * Print an expression statement.
901 * @param statement the expression statement
903 static void print_expression_statement(const expression_statement_t *statement)
905 print_expression(statement->expression);
910 * Print a goto statement.
912 * @param statement the goto statement
914 static void print_goto_statement(const goto_statement_t *statement)
917 if (statement->expression != NULL) {
919 print_expression(statement->expression);
921 fputs(statement->label->symbol->string, out);
927 * Print a label statement.
929 * @param statement the label statement
931 static void print_label_statement(const label_statement_t *statement)
933 fprintf(out, "%s:\n", statement->label->symbol->string);
935 print_statement(statement->statement);
939 * Print an if statement.
941 * @param statement the if statement
943 static void print_if_statement(const if_statement_t *statement)
946 print_expression(statement->condition);
948 print_statement(statement->true_statement);
950 if(statement->false_statement != NULL) {
953 print_statement(statement->false_statement);
958 * Print a switch statement.
960 * @param statement the switch statement
962 static void print_switch_statement(const switch_statement_t *statement)
964 fputs("switch (", out);
965 print_expression(statement->expression);
967 print_statement(statement->body);
971 * Print a case label (including the default label).
973 * @param statement the case label statement
975 static void print_case_label(const case_label_statement_t *statement)
977 if(statement->expression == NULL) {
978 fputs("default:\n", out);
981 print_expression(statement->expression);
982 if (statement->end_range != NULL) {
984 print_expression(statement->end_range);
989 if(statement->statement != NULL) {
990 if (statement->statement->base.kind == STATEMENT_CASE_LABEL) {
994 print_statement(statement->statement);
999 * Print a declaration statement.
1001 * @param statement the statement
1003 static void print_declaration_statement(
1004 const declaration_statement_t *statement)
1007 declaration_t *declaration = statement->declarations_begin;
1009 if (declaration->namespc == NAMESPACE_LOCAL_LABEL) {
1010 fputs("__label__ ", out);
1012 declaration != statement->declarations_end->next;
1013 declaration = declaration->next) {
1019 fputs(declaration->symbol->string, out);
1024 declaration != statement->declarations_end->next;
1025 declaration = declaration->next) {
1026 if (declaration->storage_class == STORAGE_CLASS_ENUM_ENTRY)
1028 if (declaration->implicit)
1036 print_declaration(declaration);
1043 * Print a while statement.
1045 * @param statement the statement
1047 static void print_while_statement(const while_statement_t *statement)
1049 fputs("while (", out);
1050 print_expression(statement->condition);
1052 print_statement(statement->body);
1056 * Print a do-while statement.
1058 * @param statement the statement
1060 static void print_do_while_statement(const do_while_statement_t *statement)
1063 print_statement(statement->body);
1065 fputs("while (", out);
1066 print_expression(statement->condition);
1071 * Print a for statement.
1073 * @param statement the statement
1075 static void print_for_statement(const for_statement_t *statement)
1077 fputs("for (", out);
1078 declaration_t *decl = statement->scope.declarations;
1079 while (decl != NULL && decl->implicit)
1082 assert(statement->initialisation == NULL);
1083 print_declaration(decl);
1084 if (decl->next != NULL) {
1085 panic("multiple declarations in for statement not supported yet");
1089 if(statement->initialisation) {
1090 print_expression(statement->initialisation);
1094 if(statement->condition != NULL) {
1095 print_expression(statement->condition);
1098 if(statement->step != NULL) {
1099 print_expression(statement->step);
1102 print_statement(statement->body);
1106 * Print assembler arguments.
1108 * @param arguments the arguments
1110 static void print_asm_arguments(asm_argument_t *arguments)
1112 asm_argument_t *argument = arguments;
1113 for( ; argument != NULL; argument = argument->next) {
1114 if(argument != arguments)
1117 if(argument->symbol) {
1118 fprintf(out, "[%s] ", argument->symbol->string);
1120 print_quoted_string(&argument->constraints, '"', 1);
1122 print_expression(argument->expression);
1128 * Print assembler clobbers.
1130 * @param clobbers the clobbers
1132 static void print_asm_clobbers(asm_clobber_t *clobbers)
1134 asm_clobber_t *clobber = clobbers;
1135 for( ; clobber != NULL; clobber = clobber->next) {
1136 if(clobber != clobbers)
1139 print_quoted_string(&clobber->clobber, '"', 1);
1144 * Print an assembler statement.
1146 * @param statement the statement
1148 static void print_asm_statement(const asm_statement_t *statement)
1151 if(statement->is_volatile) {
1152 fputs("volatile ", out);
1155 print_quoted_string(&statement->asm_text, '"', 1);
1156 if (statement->outputs == NULL &&
1157 statement->inputs == NULL &&
1158 statement->clobbers == NULL)
1159 goto end_of_print_asm_statement;
1162 print_asm_arguments(statement->outputs);
1163 if (statement->inputs == NULL && statement->clobbers == NULL)
1164 goto end_of_print_asm_statement;
1167 print_asm_arguments(statement->inputs);
1168 if (statement->clobbers == NULL)
1169 goto end_of_print_asm_statement;
1172 print_asm_clobbers(statement->clobbers);
1174 end_of_print_asm_statement:
1179 * Print a microsoft __try statement.
1181 * @param statement the statement
1183 static void print_ms_try_statement(const ms_try_statement_t *statement)
1185 fputs("__try ", out);
1186 print_statement(statement->try_statement);
1188 if(statement->except_expression != NULL) {
1189 fputs("__except(", out);
1190 print_expression(statement->except_expression);
1193 fputs("__finally ", out);
1195 print_statement(statement->final_statement);
1199 * Print a microsoft __leave statement.
1201 * @param statement the statement
1203 static void print_leave_statement(const leave_statement_t *statement)
1206 fputs("__leave;\n", out);
1210 * Print a statement.
1212 * @param statement the statement
1214 void print_statement(const statement_t *statement)
1216 switch (statement->kind) {
1217 case STATEMENT_EMPTY:
1220 case STATEMENT_COMPOUND:
1221 print_compound_statement(&statement->compound);
1223 case STATEMENT_RETURN:
1224 print_return_statement(&statement->returns);
1226 case STATEMENT_EXPRESSION:
1227 print_expression_statement(&statement->expression);
1229 case STATEMENT_LABEL:
1230 print_label_statement(&statement->label);
1232 case STATEMENT_GOTO:
1233 print_goto_statement(&statement->gotos);
1235 case STATEMENT_CONTINUE:
1236 fputs("continue;\n", out);
1238 case STATEMENT_BREAK:
1239 fputs("break;\n", out);
1242 print_if_statement(&statement->ifs);
1244 case STATEMENT_SWITCH:
1245 print_switch_statement(&statement->switchs);
1247 case STATEMENT_CASE_LABEL:
1248 print_case_label(&statement->case_label);
1250 case STATEMENT_DECLARATION:
1251 print_declaration_statement(&statement->declaration);
1253 case STATEMENT_WHILE:
1254 print_while_statement(&statement->whiles);
1256 case STATEMENT_DO_WHILE:
1257 print_do_while_statement(&statement->do_while);
1260 print_for_statement(&statement->fors);
1263 print_asm_statement(&statement->asms);
1265 case STATEMENT_MS_TRY:
1266 print_ms_try_statement(&statement->ms_try);
1268 case STATEMENT_LEAVE:
1269 print_leave_statement(&statement->leave);
1271 case STATEMENT_INVALID:
1272 fputs("$invalid statement$\n", out);
1278 * Print a storage class.
1280 * @param storage_class the storage class
1282 static void print_storage_class(storage_class_tag_t storage_class)
1284 switch(storage_class) {
1285 case STORAGE_CLASS_ENUM_ENTRY:
1286 case STORAGE_CLASS_NONE:
1288 case STORAGE_CLASS_TYPEDEF: fputs("typedef ", out); break;
1289 case STORAGE_CLASS_EXTERN: fputs("extern ", out); break;
1290 case STORAGE_CLASS_STATIC: fputs("static ", out); break;
1291 case STORAGE_CLASS_AUTO: fputs("auto ", out); break;
1292 case STORAGE_CLASS_REGISTER: fputs("register ", out); break;
1293 case STORAGE_CLASS_THREAD: fputs("__thread", out); break;
1294 case STORAGE_CLASS_THREAD_EXTERN: fputs("extern __thread", out); break;
1295 case STORAGE_CLASS_THREAD_STATIC: fputs("static __thread", out); break;
1300 * Print an initializer.
1302 * @param initializer the initializer
1304 void print_initializer(const initializer_t *initializer)
1306 if(initializer == NULL) {
1311 switch(initializer->kind) {
1312 case INITIALIZER_VALUE: {
1313 const initializer_value_t *value = &initializer->value;
1314 print_expression(value->value);
1317 case INITIALIZER_LIST: {
1318 assert(initializer->kind == INITIALIZER_LIST);
1320 const initializer_list_t *list = &initializer->list;
1322 for(size_t i = 0 ; i < list->len; ++i) {
1323 const initializer_t *sub_init = list->initializers[i];
1324 print_initializer(list->initializers[i]);
1325 if(i < list->len-1) {
1326 if(sub_init == NULL || sub_init->kind != INITIALIZER_DESIGNATOR)
1333 case INITIALIZER_STRING:
1334 print_quoted_string(&initializer->string.string, '"', 1);
1336 case INITIALIZER_WIDE_STRING:
1337 print_quoted_wide_string(&initializer->wide_string.string, '"', 1);
1339 case INITIALIZER_DESIGNATOR:
1340 print_designator(initializer->designator.designator);
1345 panic("invalid initializer kind found");
1349 * Print microsoft extended declaration modifiers.
1351 static void print_ms_modifiers(const declaration_t *declaration) {
1352 if((c_mode & _MS) == 0)
1355 decl_modifiers_t modifiers = declaration->modifiers;
1357 /* DM_FORCEINLINE handled outside. */
1358 if ((modifiers & ~DM_FORCEINLINE) != 0 ||
1359 declaration->alignment != 0 ||
1360 declaration->get_property_sym != NULL ||
1361 declaration->put_property_sym != NULL) {
1364 fputs("__declspec", out);
1365 if(modifiers & DM_DLLIMPORT) {
1366 fputs(next, out); next = ", "; fputs("dllimport", out);
1368 if(modifiers & DM_DLLEXPORT) {
1369 fputs(next, out); next = ", "; fputs("dllexport", out);
1371 if(modifiers & DM_THREAD) {
1372 fputs(next, out); next = ", "; fputs("thread", out);
1374 if(modifiers & DM_NAKED) {
1375 fputs(next, out); next = ", "; fputs("naked", out);
1377 if(modifiers & DM_THREAD) {
1378 fputs(next, out); next = ", "; fputs("thread", out);
1380 if(modifiers & DM_SELECTANY) {
1381 fputs(next, out); next = ", "; fputs("selectany", out);
1383 if(modifiers & DM_NOTHROW) {
1384 fputs(next, out); next = ", "; fputs("nothrow", out);
1386 if(modifiers & DM_NORETURN) {
1387 fputs(next, out); next = ", "; fputs("noreturn", out);
1389 if(modifiers & DM_NOINLINE) {
1390 fputs(next, out); next = ", "; fputs("noinline", out);
1392 if (modifiers & DM_DEPRECATED) {
1393 fputs(next, out); next = ", "; fputs("deprecated", out);
1394 if(declaration->deprecated_string != NULL)
1395 fprintf(out, "(\"%s\")", declaration->deprecated_string);
1397 if(declaration->alignment != 0) {
1398 fputs(next, out); next = ", "; fprintf(out, "align(%u)", declaration->alignment);
1400 if(modifiers & DM_RESTRICT) {
1401 fputs(next, out); next = ", "; fputs("restrict", out);
1403 if(modifiers & DM_NOALIAS) {
1404 fputs(next, out); next = ", "; fputs("noalias", out);
1406 if(declaration->get_property_sym != NULL || declaration->put_property_sym != NULL) {
1408 fputs(next, out); next = ", "; fputs("property(", out);
1409 if(declaration->get_property_sym != NULL) {
1410 fprintf(out, "get=%s", declaration->get_property_sym->string);
1413 if(declaration->put_property_sym != NULL)
1414 fprintf(out, "%sput=%s", comma, declaration->put_property_sym->string);
1422 * Print a declaration in the NORMAL namespace.
1424 * @param declaration the declaration
1426 static void print_normal_declaration(const declaration_t *declaration)
1428 print_storage_class((storage_class_tag_t) declaration->declared_storage_class);
1429 if (declaration->is_inline) {
1430 if (declaration->modifiers & DM_FORCEINLINE) {
1431 fputs("__forceinline ", out);
1432 } else if (declaration->modifiers & DM_MICROSOFT_INLINE) {
1433 fputs("__inline ", out);
1435 fputs("inline ", out);
1438 print_ms_modifiers(declaration);
1439 print_type_ext(declaration->type, declaration->symbol,
1440 &declaration->scope);
1442 if(declaration->type->kind == TYPE_FUNCTION) {
1443 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) {
1461 print_expression_prec(expression, PREC_BOTTOM);
1465 * Print a declaration.
1467 * @param declaration the declaration
1469 void print_declaration(const declaration_t *declaration)
1471 if (declaration->namespc != NAMESPACE_NORMAL &&
1472 declaration->symbol == NULL)
1475 switch (declaration->namespc) {
1476 case NAMESPACE_NORMAL:
1477 print_normal_declaration(declaration);
1479 case NAMESPACE_STRUCT:
1480 fputs("struct ", out);
1481 fputs(declaration->symbol->string, out);
1482 if (declaration->init.complete) {
1484 print_compound_definition(declaration);
1488 case NAMESPACE_UNION:
1489 fputs("union ", out);
1490 fputs(declaration->symbol->string, out);
1491 if (declaration->init.complete) {
1493 print_compound_definition(declaration);
1497 case NAMESPACE_ENUM:
1498 fputs("enum ", out);
1499 fputs(declaration->symbol->string, out);
1500 if (declaration->init.complete) {
1502 print_enum_definition(declaration);
1510 * Print the AST of a translation unit.
1512 * @param unit the translation unit
1514 void print_ast(const translation_unit_t *unit)
1518 declaration_t *declaration = unit->scope.declarations;
1519 for( ; declaration != NULL; declaration = declaration->next) {
1520 if(declaration->storage_class == STORAGE_CLASS_ENUM_ENTRY)
1522 if(declaration->namespc != NAMESPACE_NORMAL &&
1523 declaration->symbol == NULL)
1525 if (declaration->implicit)
1529 print_declaration(declaration);
1534 bool is_constant_initializer(const initializer_t *initializer)
1536 switch(initializer->kind) {
1537 case INITIALIZER_STRING:
1538 case INITIALIZER_WIDE_STRING:
1539 case INITIALIZER_DESIGNATOR:
1542 case INITIALIZER_VALUE:
1543 return is_constant_expression(initializer->value.value);
1545 case INITIALIZER_LIST:
1546 for(size_t i = 0; i < initializer->list.len; ++i) {
1547 initializer_t *sub_initializer = initializer->list.initializers[i];
1548 if(!is_constant_initializer(sub_initializer))
1553 panic("invalid initializer kind found");
1556 static bool is_object_with_linker_constant_address(const expression_t *expression)
1558 switch(expression->kind) {
1559 case EXPR_UNARY_DEREFERENCE:
1560 return is_address_constant(expression->unary.value);
1563 type_t *base_type = skip_typeref(expression->select.compound->base.type);
1564 if(is_type_pointer(base_type)) {
1566 return is_address_constant(expression->select.compound);
1568 return is_object_with_linker_constant_address(expression->select.compound);
1572 case EXPR_ARRAY_ACCESS:
1573 return is_constant_expression(expression->array_access.index)
1574 && is_address_constant(expression->array_access.array_ref);
1576 case EXPR_REFERENCE: {
1577 declaration_t *declaration = expression->reference.declaration;
1578 switch((storage_class_tag_t) declaration->storage_class) {
1579 case STORAGE_CLASS_NONE:
1580 case STORAGE_CLASS_EXTERN:
1581 case STORAGE_CLASS_STATIC:
1593 bool is_address_constant(const expression_t *expression)
1595 switch(expression->kind) {
1596 case EXPR_UNARY_TAKE_ADDRESS:
1597 return is_object_with_linker_constant_address(expression->unary.value);
1599 case EXPR_UNARY_DEREFERENCE: {
1601 = revert_automatic_type_conversion(expression->unary.value);
1602 /* dereferencing a function is a NOP */
1603 if(is_type_function(real_type)) {
1604 return is_address_constant(expression->unary.value);
1610 case EXPR_UNARY_CAST: {
1611 type_t *dest = skip_typeref(expression->base.type);
1612 if (!is_type_pointer(dest) && (
1613 dest->kind != TYPE_ATOMIC ||
1614 !(get_atomic_type_flags(dest->atomic.akind) & ATOMIC_TYPE_FLAG_INTEGER) ||
1615 (get_atomic_type_size(dest->atomic.akind) < get_atomic_type_size(get_intptr_kind())
1619 return (is_constant_expression(expression->unary.value)
1620 || is_address_constant(expression->unary.value));
1623 case EXPR_BINARY_ADD:
1624 case EXPR_BINARY_SUB: {
1625 expression_t *left = expression->binary.left;
1626 expression_t *right = expression->binary.right;
1628 if(is_type_pointer(skip_typeref(left->base.type))) {
1629 return is_address_constant(left) && is_constant_expression(right);
1630 } else if(is_type_pointer(skip_typeref(right->base.type))) {
1631 return is_constant_expression(left) && is_address_constant(right);
1637 case EXPR_REFERENCE: {
1638 declaration_t *declaration = expression->reference.declaration;
1639 type_t *type = skip_typeref(declaration->type);
1640 if(is_type_function(type))
1642 if(is_type_array(type)) {
1643 return is_object_with_linker_constant_address(expression);
1645 /* Prevent stray errors */
1646 if (!is_type_valid(type))
1651 case EXPR_ARRAY_ACCESS: {
1652 type_t *const type =
1653 skip_typeref(revert_automatic_type_conversion(expression));
1655 is_type_array(type) &&
1656 is_constant_expression(expression->array_access.index) &&
1657 is_address_constant(expression->array_access.array_ref);
1665 static bool is_builtin_const_call(const expression_t *expression)
1667 expression_t *function = expression->call.function;
1668 if (function->kind != EXPR_BUILTIN_SYMBOL) {
1672 symbol_t *symbol = function->builtin_symbol.symbol;
1674 switch (symbol->ID) {
1675 case T___builtin_huge_val:
1676 case T___builtin_nan:
1677 case T___builtin_nanf:
1678 case T___builtin_nand:
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_BINARY_ASSIGN:
1770 case EXPR_BINARY_MUL_ASSIGN:
1771 case EXPR_BINARY_DIV_ASSIGN:
1772 case EXPR_BINARY_MOD_ASSIGN:
1773 case EXPR_BINARY_ADD_ASSIGN:
1774 case EXPR_BINARY_SUB_ASSIGN:
1775 case EXPR_BINARY_SHIFTLEFT_ASSIGN:
1776 case EXPR_BINARY_SHIFTRIGHT_ASSIGN:
1777 case EXPR_BINARY_BITWISE_AND_ASSIGN:
1778 case EXPR_BINARY_BITWISE_XOR_ASSIGN:
1779 case EXPR_BINARY_BITWISE_OR_ASSIGN:
1780 case EXPR_BINARY_COMMA:
1781 case EXPR_ARRAY_ACCESS:
1784 case EXPR_UNARY_TAKE_ADDRESS:
1785 return is_object_with_constant_address(expression->unary.value);
1788 return is_builtin_const_call(expression);
1790 case EXPR_UNARY_NEGATE:
1791 case EXPR_UNARY_PLUS:
1792 case EXPR_UNARY_BITWISE_NEGATE:
1793 case EXPR_UNARY_NOT:
1794 return is_constant_expression(expression->unary.value);
1796 case EXPR_UNARY_CAST:
1797 case EXPR_UNARY_CAST_IMPLICIT:
1798 return is_type_arithmetic(skip_typeref(expression->base.type))
1799 && is_constant_expression(expression->unary.value);
1801 case EXPR_BINARY_ADD:
1802 case EXPR_BINARY_SUB:
1803 case EXPR_BINARY_MUL:
1804 case EXPR_BINARY_DIV:
1805 case EXPR_BINARY_MOD:
1806 case EXPR_BINARY_EQUAL:
1807 case EXPR_BINARY_NOTEQUAL:
1808 case EXPR_BINARY_LESS:
1809 case EXPR_BINARY_LESSEQUAL:
1810 case EXPR_BINARY_GREATER:
1811 case EXPR_BINARY_GREATEREQUAL:
1812 case EXPR_BINARY_BITWISE_AND:
1813 case EXPR_BINARY_BITWISE_OR:
1814 case EXPR_BINARY_BITWISE_XOR:
1815 case EXPR_BINARY_LOGICAL_AND:
1816 case EXPR_BINARY_LOGICAL_OR:
1817 case EXPR_BINARY_SHIFTLEFT:
1818 case EXPR_BINARY_SHIFTRIGHT:
1819 case EXPR_BINARY_BUILTIN_EXPECT:
1820 case EXPR_BINARY_ISGREATER:
1821 case EXPR_BINARY_ISGREATEREQUAL:
1822 case EXPR_BINARY_ISLESS:
1823 case EXPR_BINARY_ISLESSEQUAL:
1824 case EXPR_BINARY_ISLESSGREATER:
1825 case EXPR_BINARY_ISUNORDERED:
1826 return is_constant_expression(expression->binary.left)
1827 && is_constant_expression(expression->binary.right);
1829 case EXPR_COMPOUND_LITERAL:
1830 return is_constant_initializer(expression->compound_literal.initializer);
1832 case EXPR_CONDITIONAL: {
1833 expression_t *condition = expression->conditional.condition;
1834 if(!is_constant_expression(condition))
1837 long val = fold_constant(condition);
1839 return is_constant_expression(expression->conditional.true_expression);
1841 return is_constant_expression(expression->conditional.false_expression);
1844 case EXPR_REFERENCE: {
1845 declaration_t *declaration = expression->reference.declaration;
1846 if(declaration->storage_class == STORAGE_CLASS_ENUM_ENTRY)
1858 panic("invalid expression found (is constant expression)");
1862 * Initialize the AST construction.
1866 obstack_init(&ast_obstack);
1874 obstack_free(&ast_obstack, NULL);
1878 * Set the output stream for the AST printer.
1880 * @param stream the output stream
1882 void ast_set_output(FILE *stream)
1885 type_set_output(stream);
1889 * Allocate an AST object of the given size.
1891 * @param size the size of the object to allocate
1893 * @return A new allocated object in the AST memeory space.
1895 void *(allocate_ast)(size_t size)
1897 return _allocate_ast(size);