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"
34 #ifdef __INTEL_COMPILER
40 #include "adt/error.h"
42 struct obstack ast_obstack;
47 /** If set, implicit casts are printed. */
48 bool print_implicit_casts = false;
50 /** If set parenthesis are printed to indicate operator precedence. */
51 bool print_parenthesis = false;
53 static void print_statement(const statement_t *statement);
54 static void print_expression_prec(const expression_t *expression, unsigned prec);
56 void change_indent(int delta)
62 void print_indent(void)
64 for (int i = 0; i < indent; ++i)
69 * Returns 1 if a given precedence level has right-to-left
70 * associativity, else 0.
72 * @param precedence the operator precedence
74 static int right_to_left(unsigned precedence)
77 case 2*PREC_ASSIGNMENT:
78 case 2*PREC_CONDITIONAL:
88 * Return the precedence of an expression given by its kind.
90 * @param kind the expression kind
92 static unsigned get_expression_precedence(expression_kind_t kind)
94 static const unsigned prec[] = {
95 [EXPR_UNKNOWN] = PREC_PRIMARY,
96 [EXPR_INVALID] = PREC_PRIMARY,
97 [EXPR_REFERENCE] = PREC_PRIMARY,
98 [EXPR_REFERENCE_ENUM_VALUE] = PREC_PRIMARY,
99 [EXPR_CHARACTER_CONSTANT] = PREC_PRIMARY,
100 [EXPR_WIDE_CHARACTER_CONSTANT] = PREC_PRIMARY,
101 [EXPR_CONST] = PREC_PRIMARY,
102 [EXPR_STRING_LITERAL] = PREC_PRIMARY,
103 [EXPR_WIDE_STRING_LITERAL] = PREC_PRIMARY,
104 [EXPR_COMPOUND_LITERAL] = PREC_UNARY,
105 [EXPR_CALL] = PREC_POSTFIX,
106 [EXPR_CONDITIONAL] = PREC_CONDITIONAL,
107 [EXPR_SELECT] = PREC_POSTFIX,
108 [EXPR_ARRAY_ACCESS] = PREC_POSTFIX,
109 [EXPR_SIZEOF] = PREC_UNARY,
110 [EXPR_CLASSIFY_TYPE] = PREC_UNARY,
111 [EXPR_ALIGNOF] = PREC_UNARY,
113 [EXPR_FUNCNAME] = PREC_PRIMARY,
114 [EXPR_BUILTIN_SYMBOL] = PREC_PRIMARY,
115 [EXPR_BUILTIN_CONSTANT_P] = PREC_PRIMARY,
116 [EXPR_BUILTIN_PREFETCH] = PREC_PRIMARY,
117 [EXPR_OFFSETOF] = PREC_PRIMARY,
118 [EXPR_VA_START] = PREC_PRIMARY,
119 [EXPR_VA_ARG] = PREC_PRIMARY,
120 [EXPR_STATEMENT] = PREC_PRIMARY,
121 [EXPR_LABEL_ADDRESS] = PREC_PRIMARY,
123 [EXPR_UNARY_NEGATE] = PREC_UNARY,
124 [EXPR_UNARY_PLUS] = PREC_UNARY,
125 [EXPR_UNARY_BITWISE_NEGATE] = PREC_UNARY,
126 [EXPR_UNARY_NOT] = PREC_UNARY,
127 [EXPR_UNARY_DEREFERENCE] = PREC_UNARY,
128 [EXPR_UNARY_TAKE_ADDRESS] = PREC_UNARY,
129 [EXPR_UNARY_POSTFIX_INCREMENT] = PREC_UNARY,
130 [EXPR_UNARY_POSTFIX_DECREMENT] = PREC_UNARY,
131 [EXPR_UNARY_PREFIX_INCREMENT] = PREC_UNARY,
132 [EXPR_UNARY_PREFIX_DECREMENT] = PREC_UNARY,
133 [EXPR_UNARY_CAST] = PREC_UNARY,
134 [EXPR_UNARY_CAST_IMPLICIT] = PREC_UNARY,
135 [EXPR_UNARY_ASSUME] = PREC_PRIMARY,
136 [EXPR_UNARY_DELETE] = PREC_UNARY,
137 [EXPR_UNARY_DELETE_ARRAY] = PREC_UNARY,
138 [EXPR_UNARY_THROW] = PREC_ASSIGNMENT,
140 [EXPR_BINARY_ADD] = PREC_ADDITIVE,
141 [EXPR_BINARY_SUB] = PREC_ADDITIVE,
142 [EXPR_BINARY_MUL] = PREC_MULTIPLICATIVE,
143 [EXPR_BINARY_DIV] = PREC_MULTIPLICATIVE,
144 [EXPR_BINARY_MOD] = PREC_MULTIPLICATIVE,
145 [EXPR_BINARY_EQUAL] = PREC_EQUALITY,
146 [EXPR_BINARY_NOTEQUAL] = PREC_EQUALITY,
147 [EXPR_BINARY_LESS] = PREC_RELATIONAL,
148 [EXPR_BINARY_LESSEQUAL] = PREC_RELATIONAL,
149 [EXPR_BINARY_GREATER] = PREC_RELATIONAL,
150 [EXPR_BINARY_GREATEREQUAL] = PREC_RELATIONAL,
151 [EXPR_BINARY_BITWISE_AND] = PREC_AND,
152 [EXPR_BINARY_BITWISE_OR] = PREC_OR,
153 [EXPR_BINARY_BITWISE_XOR] = PREC_XOR,
154 [EXPR_BINARY_LOGICAL_AND] = PREC_LOGICAL_AND,
155 [EXPR_BINARY_LOGICAL_OR] = PREC_LOGICAL_OR,
156 [EXPR_BINARY_SHIFTLEFT] = PREC_SHIFT,
157 [EXPR_BINARY_SHIFTRIGHT] = PREC_SHIFT,
158 [EXPR_BINARY_ASSIGN] = PREC_ASSIGNMENT,
159 [EXPR_BINARY_MUL_ASSIGN] = PREC_ASSIGNMENT,
160 [EXPR_BINARY_DIV_ASSIGN] = PREC_ASSIGNMENT,
161 [EXPR_BINARY_MOD_ASSIGN] = PREC_ASSIGNMENT,
162 [EXPR_BINARY_ADD_ASSIGN] = PREC_ASSIGNMENT,
163 [EXPR_BINARY_SUB_ASSIGN] = PREC_ASSIGNMENT,
164 [EXPR_BINARY_SHIFTLEFT_ASSIGN] = PREC_ASSIGNMENT,
165 [EXPR_BINARY_SHIFTRIGHT_ASSIGN] = PREC_ASSIGNMENT,
166 [EXPR_BINARY_BITWISE_AND_ASSIGN] = PREC_ASSIGNMENT,
167 [EXPR_BINARY_BITWISE_XOR_ASSIGN] = PREC_ASSIGNMENT,
168 [EXPR_BINARY_BITWISE_OR_ASSIGN] = PREC_ASSIGNMENT,
169 [EXPR_BINARY_COMMA] = PREC_EXPRESSION,
171 [EXPR_BINARY_BUILTIN_EXPECT] = PREC_PRIMARY,
172 [EXPR_BINARY_ISGREATER] = PREC_PRIMARY,
173 [EXPR_BINARY_ISGREATEREQUAL] = PREC_PRIMARY,
174 [EXPR_BINARY_ISLESS] = PREC_PRIMARY,
175 [EXPR_BINARY_ISLESSEQUAL] = PREC_PRIMARY,
176 [EXPR_BINARY_ISLESSGREATER] = PREC_PRIMARY,
177 [EXPR_BINARY_ISUNORDERED] = PREC_PRIMARY
179 assert((unsigned)kind < (sizeof(prec)/sizeof(prec[0])));
180 unsigned res = prec[kind];
182 assert(res != PREC_BOTTOM);
183 /* we need the lowest bit for right-to-left precedence */
188 * Print a constant expression.
190 * @param cnst the constant expression
192 static void print_const(const const_expression_t *cnst)
194 if(cnst->base.type == NULL)
197 const type_t *const type = skip_typeref(cnst->base.type);
199 if (is_type_integer(type)) {
200 fprintf(out, "%lld", cnst->v.int_value);
201 } else if (is_type_float(type)) {
202 long double const val = cnst->v.float_value;
204 /* ARG, no way to print long double */
205 fprintf(out, "%.20g", (double)val);
207 fprintf(out, "%.20Lg", val);
209 if (isfinite(val) && truncl(val) == val)
212 panic("unknown constant");
216 switch (type->atomic.akind) {
217 case ATOMIC_TYPE_UINT: suffix = "U"; break;
218 case ATOMIC_TYPE_LONG: suffix = "L"; break;
219 case ATOMIC_TYPE_ULONG: suffix = "UL"; break;
220 case ATOMIC_TYPE_LONGLONG: suffix = "LL"; break;
221 case ATOMIC_TYPE_ULONGLONG: suffix = "ULL"; break;
222 case ATOMIC_TYPE_FLOAT: suffix = "F"; break;
223 case ATOMIC_TYPE_LONG_DOUBLE: suffix = "L"; break;
225 default: suffix = NULL; break;
232 * Print a quoted string constant.
234 * @param string the string constant
235 * @param border the border char
236 * @param skip number of chars to skip at the end
238 static void print_quoted_string(const string_t *const string, char border, int skip)
241 const char *end = string->begin + string->size - skip;
242 for (const char *c = string->begin; c != end; ++c) {
247 case '\\': fputs("\\\\", out); break;
248 case '\a': fputs("\\a", out); break;
249 case '\b': fputs("\\b", out); break;
250 case '\f': fputs("\\f", out); break;
251 case '\n': fputs("\\n", out); break;
252 case '\r': fputs("\\r", out); break;
253 case '\t': fputs("\\t", out); break;
254 case '\v': fputs("\\v", out); break;
255 case '\?': fputs("\\?", out); break;
257 if (c_mode & _GNUC) {
258 fputs("\\e", out); break;
263 fprintf(out, "\\%03o", (unsigned)*c);
274 * Prints a wide string literal expression.
276 * @param wstr the wide string literal expression
277 * @param border the border char
278 * @param skip number of chars to skip at the end
280 static void print_quoted_wide_string(const wide_string_t *const wstr,
281 char border, int skip)
285 const wchar_rep_t *end = wstr->begin + wstr->size - skip;
286 for (const wchar_rep_t *c = wstr->begin; c != end; ++c) {
288 case L'\"': fputs("\\\"", out); break;
289 case L'\\': fputs("\\\\", out); break;
290 case L'\a': fputs("\\a", out); break;
291 case L'\b': fputs("\\b", out); break;
292 case L'\f': fputs("\\f", out); break;
293 case L'\n': fputs("\\n", out); break;
294 case L'\r': fputs("\\r", out); break;
295 case L'\t': fputs("\\t", out); break;
296 case L'\v': fputs("\\v", out); break;
297 case L'\?': fputs("\\?", out); break;
299 if (c_mode & _GNUC) {
300 fputs("\\e", out); break;
304 const unsigned tc = *c;
307 fprintf(out, "\\%03o", (char)*c);
311 } else if (tc < 0x800) {
312 fputc(0xC0 | (tc >> 6), out);
313 fputc(0x80 | (tc & 0x3F), out);
314 } else if (tc < 0x10000) {
315 fputc(0xE0 | ( tc >> 12), out);
316 fputc(0x80 | ((tc >> 6) & 0x3F), out);
317 fputc(0x80 | ( tc & 0x3F), out);
319 fputc(0xF0 | ( tc >> 18), out);
320 fputc(0x80 | ((tc >> 12) & 0x3F), out);
321 fputc(0x80 | ((tc >> 6) & 0x3F), out);
322 fputc(0x80 | ( tc & 0x3F), out);
331 * Print a constant character expression.
333 * @param cnst the constant character expression
335 static void print_character_constant(const const_expression_t *cnst)
337 print_quoted_string(&cnst->v.character, '\'', 0);
340 static void print_wide_character_constant(const const_expression_t *cnst)
342 print_quoted_wide_string(&cnst->v.wide_character, '\'', 0);
346 * Prints a string literal expression.
348 * @param string_literal the string literal expression
350 static void print_string_literal(
351 const string_literal_expression_t *string_literal)
353 print_quoted_string(&string_literal->value, '"', 1);
357 * Prints a predefined symbol.
359 static void print_funcname(
360 const funcname_expression_t *funcname)
363 switch (funcname->kind) {
364 case FUNCNAME_FUNCTION: s = (c_mode & _C99) ? "__func__" : "__FUNCTION__"; break;
365 case FUNCNAME_PRETTY_FUNCTION: s = "__PRETTY_FUNCTION__"; break;
366 case FUNCNAME_FUNCSIG: s = "__FUNCSIG__"; break;
367 case FUNCNAME_FUNCDNAME: s = "__FUNCDNAME__"; break;
374 static void print_wide_string_literal(
375 const wide_string_literal_expression_t *const wstr)
377 print_quoted_wide_string(&wstr->value, '"', 1);
380 static void print_compound_literal(
381 const compound_literal_expression_t *expression)
384 print_type(expression->type);
386 print_initializer(expression->initializer);
390 * Prints a call expression.
392 * @param call the call expression
394 static void print_call_expression(const call_expression_t *call)
396 unsigned prec = get_expression_precedence(call->base.kind);
397 print_expression_prec(call->function, prec);
399 call_argument_t *argument = call->arguments;
401 while(argument != NULL) {
407 print_expression_prec(argument->expression, 2 * PREC_ASSIGNMENT);
409 argument = argument->next;
415 * Prints a binary expression.
417 * @param binexpr the binary expression
419 static void print_binary_expression(const binary_expression_t *binexpr)
421 unsigned prec = get_expression_precedence(binexpr->base.kind);
422 int r2l = right_to_left(prec);
424 if(binexpr->base.kind == EXPR_BINARY_BUILTIN_EXPECT) {
425 fputs("__builtin_expect(", out);
426 print_expression_prec(binexpr->left, prec);
428 print_expression_prec(binexpr->right, prec);
433 print_expression_prec(binexpr->left, prec + r2l);
435 switch (binexpr->base.kind) {
436 case EXPR_BINARY_COMMA: op = ", "; break;
437 case EXPR_BINARY_ASSIGN: op = " = "; break;
438 case EXPR_BINARY_ADD: op = " + "; break;
439 case EXPR_BINARY_SUB: op = " - "; break;
440 case EXPR_BINARY_MUL: op = " * "; break;
441 case EXPR_BINARY_MOD: op = " % "; break;
442 case EXPR_BINARY_DIV: op = " / "; break;
443 case EXPR_BINARY_BITWISE_OR: op = " | "; break;
444 case EXPR_BINARY_BITWISE_AND: op = " & "; break;
445 case EXPR_BINARY_BITWISE_XOR: op = " ^ "; break;
446 case EXPR_BINARY_LOGICAL_OR: op = " || "; break;
447 case EXPR_BINARY_LOGICAL_AND: op = " && "; break;
448 case EXPR_BINARY_NOTEQUAL: op = " != "; break;
449 case EXPR_BINARY_EQUAL: op = " == "; break;
450 case EXPR_BINARY_LESS: op = " < "; break;
451 case EXPR_BINARY_LESSEQUAL: op = " <= "; break;
452 case EXPR_BINARY_GREATER: op = " > "; break;
453 case EXPR_BINARY_GREATEREQUAL: op = " >= "; break;
454 case EXPR_BINARY_SHIFTLEFT: op = " << "; break;
455 case EXPR_BINARY_SHIFTRIGHT: op = " >> "; break;
457 case EXPR_BINARY_ADD_ASSIGN: op = " += "; break;
458 case EXPR_BINARY_SUB_ASSIGN: op = " -= "; break;
459 case EXPR_BINARY_MUL_ASSIGN: op = " *= "; break;
460 case EXPR_BINARY_MOD_ASSIGN: op = " %= "; break;
461 case EXPR_BINARY_DIV_ASSIGN: op = " /= "; break;
462 case EXPR_BINARY_BITWISE_OR_ASSIGN: op = " |= "; break;
463 case EXPR_BINARY_BITWISE_AND_ASSIGN: op = " &= "; break;
464 case EXPR_BINARY_BITWISE_XOR_ASSIGN: op = " ^= "; break;
465 case EXPR_BINARY_SHIFTLEFT_ASSIGN: op = " <<= "; break;
466 case EXPR_BINARY_SHIFTRIGHT_ASSIGN: op = " >>= "; break;
467 default: panic("invalid binexpression found");
470 print_expression_prec(binexpr->right, prec - r2l);
474 * Prints an unary expression.
476 * @param unexpr the unary expression
478 static void print_unary_expression(const unary_expression_t *unexpr)
480 unsigned prec = get_expression_precedence(unexpr->base.kind);
481 switch (unexpr->base.kind) {
482 case EXPR_UNARY_NEGATE: fputc('-', out); break;
483 case EXPR_UNARY_PLUS: fputc('+', out); break;
484 case EXPR_UNARY_NOT: fputc('!', out); break;
485 case EXPR_UNARY_BITWISE_NEGATE: fputc('~', out); break;
486 case EXPR_UNARY_PREFIX_INCREMENT: fputs("++", out); break;
487 case EXPR_UNARY_PREFIX_DECREMENT: fputs("--", out); break;
488 case EXPR_UNARY_DEREFERENCE: fputc('*', out); break;
489 case EXPR_UNARY_TAKE_ADDRESS: fputc('&', out); break;
490 case EXPR_UNARY_DELETE: fputs("delete ", out); break;
491 case EXPR_UNARY_DELETE_ARRAY: fputs("delete [] ", out); break;
493 case EXPR_UNARY_POSTFIX_INCREMENT:
494 print_expression_prec(unexpr->value, prec);
497 case EXPR_UNARY_POSTFIX_DECREMENT:
498 print_expression_prec(unexpr->value, prec);
501 case EXPR_UNARY_CAST_IMPLICIT:
502 case EXPR_UNARY_CAST:
504 print_type(unexpr->base.type);
507 case EXPR_UNARY_ASSUME:
508 fputs("__assume(", out);
509 print_expression_prec(unexpr->value, 2 * PREC_ASSIGNMENT);
513 case EXPR_UNARY_THROW:
514 if (unexpr->value == NULL) {
518 fputs("throw ", out);
522 panic("invalid unary expression found");
524 print_expression_prec(unexpr->value, prec);
528 * Prints a reference expression.
530 * @param ref the reference expression
532 static void print_reference_expression(const reference_expression_t *ref)
534 fputs(ref->entity->base.symbol->string, out);
538 * Prints a label address expression.
540 * @param ref the reference expression
542 static void print_label_address_expression(const label_address_expression_t *le)
544 fprintf(out, "&&%s", le->label->base.symbol->string);
548 * Prints an array expression.
550 * @param expression the array expression
552 static void print_array_expression(const array_access_expression_t *expression)
554 unsigned prec = get_expression_precedence(expression->base.kind);
555 if(!expression->flipped) {
556 print_expression_prec(expression->array_ref, prec);
558 print_expression(expression->index);
561 print_expression_prec(expression->index, prec);
563 print_expression(expression->array_ref);
569 * Prints a typeproperty expression (sizeof or __alignof__).
571 * @param expression the type property expression
573 static void print_typeprop_expression(const typeprop_expression_t *expression)
575 if (expression->base.kind == EXPR_SIZEOF) {
576 fputs("sizeof", out);
578 assert(expression->base.kind == EXPR_ALIGNOF);
579 fputs("__alignof__", out);
581 if(expression->tp_expression != NULL) {
582 /* always print the '()' here, sizeof x is right but unusual */
584 print_expression(expression->tp_expression);
588 print_type(expression->type);
594 * Prints an builtin symbol.
596 * @param expression the builtin symbol expression
598 static void print_builtin_symbol(const builtin_symbol_expression_t *expression)
600 fputs(expression->symbol->string, out);
604 * Prints a builtin constant expression.
606 * @param expression the builtin constant expression
608 static void print_builtin_constant(const builtin_constant_expression_t *expression)
610 fputs("__builtin_constant_p(", out);
611 print_expression_prec(expression->value, 2 * PREC_ASSIGNMENT);
616 * Prints a builtin prefetch expression.
618 * @param expression the builtin prefetch expression
620 static void print_builtin_prefetch(const builtin_prefetch_expression_t *expression)
622 fputs("__builtin_prefetch(", out);
623 print_expression_prec(expression->adr, 2 * PREC_ASSIGNMENT);
624 if (expression->rw) {
626 print_expression_prec(expression->rw, 2 * PREC_ASSIGNMENT);
628 if (expression->locality) {
630 print_expression_prec(expression->locality, 2 * PREC_ASSIGNMENT);
636 * Prints a conditional expression.
638 * @param expression the conditional expression
640 static void print_conditional(const conditional_expression_t *expression)
642 print_expression_prec(expression->condition, 2 * PREC_LOGICAL_OR);
644 if (expression->true_expression != NULL) {
645 print_expression_prec(expression->true_expression, 2 * PREC_EXPRESSION);
650 print_expression_prec(expression->false_expression, 2 * PREC_CONDITIONAL);
654 * Prints a va_start expression.
656 * @param expression the va_start expression
658 static void print_va_start(const va_start_expression_t *const expression)
660 fputs("__builtin_va_start(", out);
661 print_expression_prec(expression->ap, 2 * PREC_ASSIGNMENT);
663 fputs(expression->parameter->base.base.symbol->string, out);
668 * Prints a va_arg expression.
670 * @param expression the va_arg expression
672 static void print_va_arg(const va_arg_expression_t *expression)
674 fputs("__builtin_va_arg(", out);
675 print_expression_prec(expression->ap, 2 * PREC_ASSIGNMENT);
677 print_type(expression->base.type);
682 * Prints a select expression (. or ->).
684 * @param expression the select expression
686 static void print_select(const select_expression_t *expression)
688 unsigned prec = get_expression_precedence(expression->base.kind);
689 print_expression_prec(expression->compound, prec);
690 if(is_type_pointer(skip_typeref(expression->compound->base.type))) {
695 fputs(expression->compound_entry->base.symbol->string, out);
699 * Prints a type classify expression.
701 * @param expr the type classify expression
703 static void print_classify_type_expression(
704 const classify_type_expression_t *const expr)
706 fputs("__builtin_classify_type(", out);
707 print_expression_prec(expr->type_expression, 2 * PREC_ASSIGNMENT);
712 * Prints a designator.
714 * @param designator the designator
716 static void print_designator(const designator_t *designator)
718 for ( ; designator != NULL; designator = designator->next) {
719 if (designator->symbol == NULL) {
721 print_expression(designator->array_index);
725 fputs(designator->symbol->string, out);
731 * Prints an offsetof expression.
733 * @param expression the offset expression
735 static void print_offsetof_expression(const offsetof_expression_t *expression)
737 fputs("__builtin_offsetof", out);
739 print_type(expression->type);
741 print_designator(expression->designator);
746 * Prints a statement expression.
748 * @param expression the statement expression
750 static void print_statement_expression(const statement_expression_t *expression)
753 print_statement(expression->statement);
758 * Prints an expression with parenthesis if needed.
760 * @param expression the expression to print
761 * @param top_prec the precedence of the user of this expression.
763 static void print_expression_prec(const expression_t *expression, unsigned top_prec)
765 if (expression->kind == EXPR_UNARY_CAST_IMPLICIT && !print_implicit_casts) {
766 expression = expression->unary.value;
768 unsigned prec = get_expression_precedence(expression->base.kind);
769 if (print_parenthesis && top_prec != PREC_BOTTOM)
773 switch (expression->kind) {
776 fputs("$invalid expression$", out);
778 case EXPR_CHARACTER_CONSTANT:
779 print_character_constant(&expression->conste);
781 case EXPR_WIDE_CHARACTER_CONSTANT:
782 print_wide_character_constant(&expression->conste);
785 print_const(&expression->conste);
788 print_funcname(&expression->funcname);
790 case EXPR_STRING_LITERAL:
791 print_string_literal(&expression->string);
793 case EXPR_WIDE_STRING_LITERAL:
794 print_wide_string_literal(&expression->wide_string);
796 case EXPR_COMPOUND_LITERAL:
797 print_compound_literal(&expression->compound_literal);
800 print_call_expression(&expression->call);
803 print_binary_expression(&expression->binary);
806 case EXPR_REFERENCE_ENUM_VALUE:
807 print_reference_expression(&expression->reference);
809 case EXPR_ARRAY_ACCESS:
810 print_array_expression(&expression->array_access);
812 case EXPR_LABEL_ADDRESS:
813 print_label_address_expression(&expression->label_address);
816 print_unary_expression(&expression->unary);
820 print_typeprop_expression(&expression->typeprop);
822 case EXPR_BUILTIN_SYMBOL:
823 print_builtin_symbol(&expression->builtin_symbol);
825 case EXPR_BUILTIN_CONSTANT_P:
826 print_builtin_constant(&expression->builtin_constant);
828 case EXPR_BUILTIN_PREFETCH:
829 print_builtin_prefetch(&expression->builtin_prefetch);
831 case EXPR_CONDITIONAL:
832 print_conditional(&expression->conditional);
835 print_va_start(&expression->va_starte);
838 print_va_arg(&expression->va_arge);
841 print_select(&expression->select);
843 case EXPR_CLASSIFY_TYPE:
844 print_classify_type_expression(&expression->classify_type);
847 print_offsetof_expression(&expression->offsetofe);
850 print_statement_expression(&expression->statement);
855 fprintf(out, "some expression of type %d", (int) expression->kind);
863 * Print an compound statement.
865 * @param block the compound statement
867 static void print_compound_statement(const compound_statement_t *block)
872 statement_t *statement = block->statements;
873 while (statement != NULL) {
874 if (statement->base.kind == STATEMENT_CASE_LABEL)
876 if (statement->kind != STATEMENT_LABEL)
878 print_statement(statement);
880 statement = statement->base.next;
888 * Print a return statement.
890 * @param statement the return statement
892 static void print_return_statement(const return_statement_t *statement)
894 fputs("return ", out);
895 if(statement->value != NULL)
896 print_expression(statement->value);
901 * Print an expression statement.
903 * @param statement the expression statement
905 static void print_expression_statement(const expression_statement_t *statement)
907 print_expression(statement->expression);
912 * Print a goto statement.
914 * @param statement the goto statement
916 static void print_goto_statement(const goto_statement_t *statement)
919 if (statement->expression != NULL) {
921 print_expression(statement->expression);
923 fputs(statement->label->base.symbol->string, out);
929 * Print a label statement.
931 * @param statement the label statement
933 static void print_label_statement(const label_statement_t *statement)
935 fprintf(out, "%s:\n", statement->label->base.symbol->string);
937 print_statement(statement->statement);
941 * Print an if statement.
943 * @param statement the if statement
945 static void print_if_statement(const if_statement_t *statement)
948 print_expression(statement->condition);
950 print_statement(statement->true_statement);
952 if(statement->false_statement != NULL) {
955 print_statement(statement->false_statement);
960 * Print a switch statement.
962 * @param statement the switch statement
964 static void print_switch_statement(const switch_statement_t *statement)
966 fputs("switch (", out);
967 print_expression(statement->expression);
969 print_statement(statement->body);
973 * Print a case label (including the default label).
975 * @param statement the case label statement
977 static void print_case_label(const case_label_statement_t *statement)
979 if(statement->expression == NULL) {
980 fputs("default:\n", out);
983 print_expression(statement->expression);
984 if (statement->end_range != NULL) {
986 print_expression(statement->end_range);
991 if(statement->statement != NULL) {
992 if (statement->statement->base.kind == STATEMENT_CASE_LABEL) {
996 print_statement(statement->statement);
1000 static void print_local_label(const local_label_statement_t *statement)
1002 fputs("__label__ ", out);
1005 entity_t *entity = statement->labels_begin;
1007 entity != statement->labels_end->base.next;
1008 entity = entity->base.next) {
1014 fputs(entity->base.symbol->string, out);
1019 static void print_typedef(const entity_t *entity)
1021 fputs("typedef ", out);
1022 print_type_ext(entity->typedefe.type, entity->base.symbol, NULL);
1027 * returns true if the entity is a compiler generated one and has no real
1028 * correspondenc in the source file
1030 static bool is_generated_entity(const entity_t *entity)
1032 if (entity->kind == ENTITY_TYPEDEF)
1033 return entity->typedefe.builtin;
1035 if (is_declaration(entity))
1036 return entity->declaration.implicit;
1042 * Print a declaration statement.
1044 * @param statement the statement
1046 static void print_declaration_statement(
1047 const declaration_statement_t *statement)
1050 entity_t *entity = statement->declarations_begin;
1052 entity != statement->declarations_end->base.next;
1053 entity = entity->base.next) {
1054 if (!is_declaration(entity) && entity->kind != ENTITY_TYPEDEF)
1056 if (is_generated_entity(entity))
1065 if (entity->kind == ENTITY_TYPEDEF) {
1066 print_typedef(entity);
1068 assert(is_declaration(entity));
1069 print_declaration(entity);
1077 * Print a while statement.
1079 * @param statement the statement
1081 static void print_while_statement(const while_statement_t *statement)
1083 fputs("while (", out);
1084 print_expression(statement->condition);
1086 print_statement(statement->body);
1090 * Print a do-while statement.
1092 * @param statement the statement
1094 static void print_do_while_statement(const do_while_statement_t *statement)
1097 print_statement(statement->body);
1099 fputs("while (", out);
1100 print_expression(statement->condition);
1105 * Print a for statement.
1107 * @param statement the statement
1109 static void print_for_statement(const for_statement_t *statement)
1111 fputs("for (", out);
1112 entity_t *entity = statement->scope.entities;
1113 while (entity != NULL && is_generated_entity(entity))
1114 entity = entity->base.next;
1116 if (entity != NULL) {
1117 assert(statement->initialisation == NULL);
1118 assert(is_declaration(entity));
1119 print_declaration(entity);
1120 if (entity->base.next != NULL) {
1121 panic("multiple declarations in for statement not supported yet");
1125 if(statement->initialisation) {
1126 print_expression(statement->initialisation);
1130 if(statement->condition != NULL) {
1131 print_expression(statement->condition);
1134 if(statement->step != NULL) {
1135 print_expression(statement->step);
1138 print_statement(statement->body);
1142 * Print assembler arguments.
1144 * @param arguments the arguments
1146 static void print_asm_arguments(asm_argument_t *arguments)
1148 asm_argument_t *argument = arguments;
1149 for( ; argument != NULL; argument = argument->next) {
1150 if(argument != arguments)
1153 if(argument->symbol) {
1154 fprintf(out, "[%s] ", argument->symbol->string);
1156 print_quoted_string(&argument->constraints, '"', 1);
1158 print_expression(argument->expression);
1164 * Print assembler clobbers.
1166 * @param clobbers the clobbers
1168 static void print_asm_clobbers(asm_clobber_t *clobbers)
1170 asm_clobber_t *clobber = clobbers;
1171 for( ; clobber != NULL; clobber = clobber->next) {
1172 if(clobber != clobbers)
1175 print_quoted_string(&clobber->clobber, '"', 1);
1180 * Print an assembler statement.
1182 * @param statement the statement
1184 static void print_asm_statement(const asm_statement_t *statement)
1187 if(statement->is_volatile) {
1188 fputs("volatile ", out);
1191 print_quoted_string(&statement->asm_text, '"', 1);
1192 if (statement->outputs == NULL &&
1193 statement->inputs == NULL &&
1194 statement->clobbers == NULL)
1195 goto end_of_print_asm_statement;
1198 print_asm_arguments(statement->outputs);
1199 if (statement->inputs == NULL && statement->clobbers == NULL)
1200 goto end_of_print_asm_statement;
1203 print_asm_arguments(statement->inputs);
1204 if (statement->clobbers == NULL)
1205 goto end_of_print_asm_statement;
1208 print_asm_clobbers(statement->clobbers);
1210 end_of_print_asm_statement:
1215 * Print a microsoft __try statement.
1217 * @param statement the statement
1219 static void print_ms_try_statement(const ms_try_statement_t *statement)
1221 fputs("__try ", out);
1222 print_statement(statement->try_statement);
1224 if(statement->except_expression != NULL) {
1225 fputs("__except(", out);
1226 print_expression(statement->except_expression);
1229 fputs("__finally ", out);
1231 print_statement(statement->final_statement);
1235 * Print a microsoft __leave statement.
1237 * @param statement the statement
1239 static void print_leave_statement(const leave_statement_t *statement)
1242 fputs("__leave;\n", out);
1246 * Print a statement.
1248 * @param statement the statement
1250 void print_statement(const statement_t *statement)
1252 switch (statement->kind) {
1253 case STATEMENT_EMPTY:
1256 case STATEMENT_COMPOUND:
1257 print_compound_statement(&statement->compound);
1259 case STATEMENT_RETURN:
1260 print_return_statement(&statement->returns);
1262 case STATEMENT_EXPRESSION:
1263 print_expression_statement(&statement->expression);
1265 case STATEMENT_LABEL:
1266 print_label_statement(&statement->label);
1268 case STATEMENT_LOCAL_LABEL:
1269 print_local_label(&statement->local_label);
1271 case STATEMENT_GOTO:
1272 print_goto_statement(&statement->gotos);
1274 case STATEMENT_CONTINUE:
1275 fputs("continue;\n", out);
1277 case STATEMENT_BREAK:
1278 fputs("break;\n", out);
1281 print_if_statement(&statement->ifs);
1283 case STATEMENT_SWITCH:
1284 print_switch_statement(&statement->switchs);
1286 case STATEMENT_CASE_LABEL:
1287 print_case_label(&statement->case_label);
1289 case STATEMENT_DECLARATION:
1290 print_declaration_statement(&statement->declaration);
1292 case STATEMENT_WHILE:
1293 print_while_statement(&statement->whiles);
1295 case STATEMENT_DO_WHILE:
1296 print_do_while_statement(&statement->do_while);
1299 print_for_statement(&statement->fors);
1302 print_asm_statement(&statement->asms);
1304 case STATEMENT_MS_TRY:
1305 print_ms_try_statement(&statement->ms_try);
1307 case STATEMENT_LEAVE:
1308 print_leave_statement(&statement->leave);
1310 case STATEMENT_INVALID:
1311 fputs("$invalid statement$\n", out);
1317 * Print a storage class.
1319 * @param storage_class the storage class
1321 static void print_storage_class(storage_class_tag_t storage_class)
1323 switch (storage_class) {
1324 case STORAGE_CLASS_NONE:
1326 case STORAGE_CLASS_TYPEDEF: fputs("typedef ", out); break;
1327 case STORAGE_CLASS_EXTERN: fputs("extern ", out); break;
1328 case STORAGE_CLASS_STATIC: fputs("static ", out); break;
1329 case STORAGE_CLASS_AUTO: fputs("auto ", out); break;
1330 case STORAGE_CLASS_REGISTER: fputs("register ", out); break;
1331 case STORAGE_CLASS_THREAD: fputs("__thread", out); break;
1332 case STORAGE_CLASS_THREAD_EXTERN: fputs("extern __thread", out); break;
1333 case STORAGE_CLASS_THREAD_STATIC: fputs("static __thread", out); break;
1338 * Print an initializer.
1340 * @param initializer the initializer
1342 void print_initializer(const initializer_t *initializer)
1344 if(initializer == NULL) {
1349 switch (initializer->kind) {
1350 case INITIALIZER_VALUE: {
1351 const initializer_value_t *value = &initializer->value;
1352 print_expression(value->value);
1355 case INITIALIZER_LIST: {
1356 assert(initializer->kind == INITIALIZER_LIST);
1358 const initializer_list_t *list = &initializer->list;
1360 for(size_t i = 0 ; i < list->len; ++i) {
1361 const initializer_t *sub_init = list->initializers[i];
1362 print_initializer(list->initializers[i]);
1363 if(i < list->len-1) {
1364 if(sub_init == NULL || sub_init->kind != INITIALIZER_DESIGNATOR)
1371 case INITIALIZER_STRING:
1372 print_quoted_string(&initializer->string.string, '"', 1);
1374 case INITIALIZER_WIDE_STRING:
1375 print_quoted_wide_string(&initializer->wide_string.string, '"', 1);
1377 case INITIALIZER_DESIGNATOR:
1378 print_designator(initializer->designator.designator);
1383 panic("invalid initializer kind found");
1387 * Print microsoft extended declaration modifiers.
1389 static void print_ms_modifiers(const declaration_t *declaration)
1391 if((c_mode & _MS) == 0)
1394 decl_modifiers_t modifiers = declaration->modifiers;
1396 bool ds_shown = false;
1397 const char *next = "(";
1399 if (declaration->base.kind == ENTITY_VARIABLE) {
1400 variable_t *variable = (variable_t*) declaration;
1401 if (variable->alignment != 0
1402 || variable->get_property_sym != NULL
1403 || variable->put_property_sym != NULL) {
1405 fputs("__declspec", out);
1409 if(variable->alignment != 0) {
1410 fputs(next, out); next = ", "; fprintf(out, "align(%u)", variable->alignment);
1412 if(variable->get_property_sym != NULL
1413 || variable->put_property_sym != NULL) {
1415 fputs(next, out); next = ", "; fputs("property(", out);
1416 if(variable->get_property_sym != NULL) {
1417 fprintf(out, "get=%s", variable->get_property_sym->string);
1420 if(variable->put_property_sym != NULL)
1421 fprintf(out, "%sput=%s", comma, variable->put_property_sym->string);
1427 /* DM_FORCEINLINE handled outside. */
1428 if ((modifiers & ~DM_FORCEINLINE) != 0) {
1430 fputs("__declspec", out);
1433 if(modifiers & DM_DLLIMPORT) {
1434 fputs(next, out); next = ", "; fputs("dllimport", out);
1436 if(modifiers & DM_DLLEXPORT) {
1437 fputs(next, out); next = ", "; fputs("dllexport", out);
1439 if(modifiers & DM_THREAD) {
1440 fputs(next, out); next = ", "; fputs("thread", out);
1442 if(modifiers & DM_NAKED) {
1443 fputs(next, out); next = ", "; fputs("naked", out);
1445 if(modifiers & DM_THREAD) {
1446 fputs(next, out); next = ", "; fputs("thread", out);
1448 if(modifiers & DM_SELECTANY) {
1449 fputs(next, out); next = ", "; fputs("selectany", out);
1451 if(modifiers & DM_NOTHROW) {
1452 fputs(next, out); next = ", "; fputs("nothrow", out);
1454 if(modifiers & DM_NORETURN) {
1455 fputs(next, out); next = ", "; fputs("noreturn", out);
1457 if(modifiers & DM_NOINLINE) {
1458 fputs(next, out); next = ", "; fputs("noinline", out);
1460 if (modifiers & DM_DEPRECATED) {
1461 fputs(next, out); next = ", "; fputs("deprecated", out);
1462 if(declaration->deprecated_string != NULL)
1463 fprintf(out, "(\"%s\")",
1464 declaration->deprecated_string);
1466 if(modifiers & DM_RESTRICT) {
1467 fputs(next, out); next = ", "; fputs("restrict", out);
1469 if(modifiers & DM_NOALIAS) {
1470 fputs(next, out); next = ", "; fputs("noalias", out);
1479 * Print a variable or function declaration
1481 void print_declaration(const entity_t *entity)
1483 assert(is_declaration(entity));
1484 const declaration_t *declaration = &entity->declaration;
1486 print_storage_class((storage_class_tag_t) declaration->declared_storage_class);
1487 if (entity->kind == ENTITY_FUNCTION) {
1488 function_t *function = (function_t*) declaration;
1489 if (function->is_inline) {
1490 if (declaration->modifiers & DM_FORCEINLINE) {
1491 fputs("__forceinline ", out);
1492 } else if (declaration->modifiers & DM_MICROSOFT_INLINE) {
1493 fputs("__inline ", out);
1495 fputs("inline ", out);
1499 print_ms_modifiers(declaration);
1500 if (entity->kind == ENTITY_FUNCTION) {
1501 print_type_ext(entity->declaration.type, entity->base.symbol,
1502 &entity->function.parameters);
1504 if (entity->function.statement != NULL) {
1507 print_statement(entity->function.statement);
1511 print_type_ext(declaration->type, declaration->base.symbol, NULL);
1513 if (entity->kind == ENTITY_VARIABLE
1514 && entity->variable.initializer != NULL) {
1516 print_initializer(entity->variable.initializer);
1523 * Prints an expression.
1525 * @param expression the expression
1527 void print_expression(const expression_t *expression)
1529 print_expression_prec(expression, 2 * PREC_BOTTOM);
1533 * Print a declaration.
1535 * @param declaration the declaration
1537 void print_entity(const entity_t *entity)
1539 if (entity->base.namespc != NAMESPACE_NORMAL && entity->base.symbol == NULL)
1542 switch ((entity_kind_tag_t) entity->kind) {
1543 case ENTITY_VARIABLE:
1544 case ENTITY_FUNCTION:
1545 case ENTITY_COMPOUND_MEMBER:
1546 print_declaration(entity);
1548 case ENTITY_TYPEDEF:
1549 print_typedef(entity);
1552 fputs("struct ", out);
1553 fputs(entity->base.symbol->string, out);
1554 if (entity->structe.complete) {
1556 print_compound_definition(&entity->structe);
1561 fputs("union ", out);
1562 fputs(entity->base.symbol->string, out);
1563 if (entity->unione.complete) {
1565 print_compound_definition(&entity->unione);
1570 fputs("enum ", out);
1571 fputs(entity->base.symbol->string, out);
1573 print_enum_definition(&entity->enume);
1577 case ENTITY_ENUM_VALUE:
1578 case ENTITY_LOCAL_LABEL:
1579 panic("print_entity used on unexpected entity type");
1580 case ENTITY_INVALID:
1583 panic("Invalid entity type encountered");
1587 * Print the AST of a translation unit.
1589 * @param unit the translation unit
1591 void print_ast(const translation_unit_t *unit)
1595 entity_t *entity = unit->scope.entities;
1596 for ( ; entity != NULL; entity = entity->base.next) {
1597 if (entity->kind == ENTITY_ENUM_VALUE)
1599 if (entity->base.namespc != NAMESPACE_NORMAL
1600 && entity->base.symbol == NULL)
1602 if (is_generated_entity(entity))
1606 print_entity(entity);
1611 bool is_constant_initializer(const initializer_t *initializer)
1613 switch (initializer->kind) {
1614 case INITIALIZER_STRING:
1615 case INITIALIZER_WIDE_STRING:
1616 case INITIALIZER_DESIGNATOR:
1619 case INITIALIZER_VALUE:
1620 return is_constant_expression(initializer->value.value);
1622 case INITIALIZER_LIST:
1623 for(size_t i = 0; i < initializer->list.len; ++i) {
1624 initializer_t *sub_initializer = initializer->list.initializers[i];
1625 if(!is_constant_initializer(sub_initializer))
1630 panic("invalid initializer kind found");
1633 static bool is_object_with_linker_constant_address(const expression_t *expression)
1635 switch (expression->kind) {
1636 case EXPR_UNARY_DEREFERENCE:
1637 return is_address_constant(expression->unary.value);
1640 type_t *base_type = skip_typeref(expression->select.compound->base.type);
1641 if(is_type_pointer(base_type)) {
1643 return is_address_constant(expression->select.compound);
1645 return is_object_with_linker_constant_address(expression->select.compound);
1649 case EXPR_ARRAY_ACCESS:
1650 return is_constant_expression(expression->array_access.index)
1651 && is_address_constant(expression->array_access.array_ref);
1653 case EXPR_REFERENCE: {
1654 entity_t *entity = expression->reference.entity;
1655 if (is_declaration(entity)) {
1656 switch ((storage_class_tag_t) entity->declaration.storage_class) {
1657 case STORAGE_CLASS_NONE:
1658 case STORAGE_CLASS_EXTERN:
1659 case STORAGE_CLASS_STATIC:
1662 case STORAGE_CLASS_REGISTER:
1663 case STORAGE_CLASS_TYPEDEF:
1664 case STORAGE_CLASS_AUTO:
1665 case STORAGE_CLASS_THREAD:
1666 case STORAGE_CLASS_THREAD_EXTERN:
1667 case STORAGE_CLASS_THREAD_STATIC:
1679 bool is_address_constant(const expression_t *expression)
1681 switch (expression->kind) {
1682 case EXPR_UNARY_TAKE_ADDRESS:
1683 return is_object_with_linker_constant_address(expression->unary.value);
1685 case EXPR_UNARY_DEREFERENCE: {
1687 = revert_automatic_type_conversion(expression->unary.value);
1688 /* dereferencing a function is a NOP */
1689 if(is_type_function(real_type)) {
1690 return is_address_constant(expression->unary.value);
1696 case EXPR_UNARY_CAST: {
1697 type_t *dest = skip_typeref(expression->base.type);
1698 if (!is_type_pointer(dest) && (
1699 dest->kind != TYPE_ATOMIC ||
1700 !(get_atomic_type_flags(dest->atomic.akind) & ATOMIC_TYPE_FLAG_INTEGER) ||
1701 (get_atomic_type_size(dest->atomic.akind) < get_atomic_type_size(get_intptr_kind())
1705 return (is_constant_expression(expression->unary.value)
1706 || is_address_constant(expression->unary.value));
1709 case EXPR_BINARY_ADD:
1710 case EXPR_BINARY_SUB: {
1711 expression_t *left = expression->binary.left;
1712 expression_t *right = expression->binary.right;
1714 if(is_type_pointer(skip_typeref(left->base.type))) {
1715 return is_address_constant(left) && is_constant_expression(right);
1716 } else if(is_type_pointer(skip_typeref(right->base.type))) {
1717 return is_constant_expression(left) && is_address_constant(right);
1723 case EXPR_REFERENCE: {
1724 entity_t *entity = expression->reference.entity;
1725 if (!is_declaration(entity))
1728 type_t *type = skip_typeref(entity->declaration.type);
1729 if(is_type_function(type))
1731 if(is_type_array(type)) {
1732 return is_object_with_linker_constant_address(expression);
1734 /* Prevent stray errors */
1735 if (!is_type_valid(type))
1740 case EXPR_ARRAY_ACCESS: {
1741 type_t *const type =
1742 skip_typeref(revert_automatic_type_conversion(expression));
1744 is_type_array(type) &&
1745 is_constant_expression(expression->array_access.index) &&
1746 is_address_constant(expression->array_access.array_ref);
1754 static bool is_builtin_const_call(const expression_t *expression)
1756 expression_t *function = expression->call.function;
1757 if (function->kind != EXPR_BUILTIN_SYMBOL) {
1761 symbol_t *symbol = function->builtin_symbol.symbol;
1763 switch (symbol->ID) {
1764 case T___builtin_huge_val:
1765 case T___builtin_inf:
1766 case T___builtin_inff:
1767 case T___builtin_infl:
1768 case T___builtin_nan:
1769 case T___builtin_nanf:
1770 case T___builtin_nanl:
1777 static bool is_constant_pointer(const expression_t *expression)
1779 if (is_constant_expression(expression))
1782 switch (expression->kind) {
1783 case EXPR_UNARY_CAST:
1784 return is_constant_pointer(expression->unary.value);
1790 static bool is_object_with_constant_address(const expression_t *expression)
1792 switch (expression->kind) {
1794 expression_t *compound = expression->select.compound;
1795 type_t *compound_type = compound->base.type;
1796 compound_type = skip_typeref(compound_type);
1797 if(is_type_pointer(compound_type)) {
1798 return is_constant_pointer(compound);
1800 return is_object_with_constant_address(compound);
1804 case EXPR_ARRAY_ACCESS: {
1805 array_access_expression_t const* const array_access =
1806 &expression->array_access;
1808 is_constant_expression(array_access->index) && (
1809 is_object_with_constant_address(array_access->array_ref) ||
1810 is_constant_pointer(array_access->array_ref)
1814 case EXPR_UNARY_DEREFERENCE:
1815 return is_constant_pointer(expression->unary.value);
1821 bool is_constant_expression(const expression_t *expression)
1823 switch (expression->kind) {
1826 case EXPR_CHARACTER_CONSTANT:
1827 case EXPR_WIDE_CHARACTER_CONSTANT:
1828 case EXPR_STRING_LITERAL:
1829 case EXPR_WIDE_STRING_LITERAL:
1830 case EXPR_CLASSIFY_TYPE:
1834 case EXPR_BUILTIN_CONSTANT_P:
1835 case EXPR_LABEL_ADDRESS:
1836 case EXPR_REFERENCE_ENUM_VALUE:
1840 type_t *type = expression->typeprop.type;
1842 type = expression->typeprop.tp_expression->base.type;
1844 type = skip_typeref(type);
1845 if (is_type_array(type) && type->array.is_vla)
1850 case EXPR_BUILTIN_SYMBOL:
1851 case EXPR_BUILTIN_PREFETCH:
1855 case EXPR_STATEMENT:
1856 case EXPR_REFERENCE:
1857 case EXPR_UNARY_POSTFIX_INCREMENT:
1858 case EXPR_UNARY_POSTFIX_DECREMENT:
1859 case EXPR_UNARY_PREFIX_INCREMENT:
1860 case EXPR_UNARY_PREFIX_DECREMENT:
1861 case EXPR_UNARY_ASSUME: /* has VOID type */
1862 case EXPR_UNARY_DEREFERENCE:
1863 case EXPR_UNARY_DELETE:
1864 case EXPR_UNARY_DELETE_ARRAY:
1865 case EXPR_UNARY_THROW:
1866 case EXPR_BINARY_ASSIGN:
1867 case EXPR_BINARY_MUL_ASSIGN:
1868 case EXPR_BINARY_DIV_ASSIGN:
1869 case EXPR_BINARY_MOD_ASSIGN:
1870 case EXPR_BINARY_ADD_ASSIGN:
1871 case EXPR_BINARY_SUB_ASSIGN:
1872 case EXPR_BINARY_SHIFTLEFT_ASSIGN:
1873 case EXPR_BINARY_SHIFTRIGHT_ASSIGN:
1874 case EXPR_BINARY_BITWISE_AND_ASSIGN:
1875 case EXPR_BINARY_BITWISE_XOR_ASSIGN:
1876 case EXPR_BINARY_BITWISE_OR_ASSIGN:
1877 case EXPR_BINARY_COMMA:
1878 case EXPR_ARRAY_ACCESS:
1881 case EXPR_UNARY_TAKE_ADDRESS:
1882 return is_object_with_constant_address(expression->unary.value);
1885 return is_builtin_const_call(expression);
1887 case EXPR_UNARY_NEGATE:
1888 case EXPR_UNARY_PLUS:
1889 case EXPR_UNARY_BITWISE_NEGATE:
1890 case EXPR_UNARY_NOT:
1891 return is_constant_expression(expression->unary.value);
1893 case EXPR_UNARY_CAST:
1894 case EXPR_UNARY_CAST_IMPLICIT:
1895 return is_type_arithmetic(skip_typeref(expression->base.type))
1896 && is_constant_expression(expression->unary.value);
1898 case EXPR_BINARY_ADD:
1899 case EXPR_BINARY_SUB:
1900 case EXPR_BINARY_MUL:
1901 case EXPR_BINARY_DIV:
1902 case EXPR_BINARY_MOD:
1903 case EXPR_BINARY_EQUAL:
1904 case EXPR_BINARY_NOTEQUAL:
1905 case EXPR_BINARY_LESS:
1906 case EXPR_BINARY_LESSEQUAL:
1907 case EXPR_BINARY_GREATER:
1908 case EXPR_BINARY_GREATEREQUAL:
1909 case EXPR_BINARY_BITWISE_AND:
1910 case EXPR_BINARY_BITWISE_OR:
1911 case EXPR_BINARY_BITWISE_XOR:
1912 case EXPR_BINARY_LOGICAL_AND:
1913 case EXPR_BINARY_LOGICAL_OR:
1914 case EXPR_BINARY_SHIFTLEFT:
1915 case EXPR_BINARY_SHIFTRIGHT:
1916 case EXPR_BINARY_BUILTIN_EXPECT:
1917 case EXPR_BINARY_ISGREATER:
1918 case EXPR_BINARY_ISGREATEREQUAL:
1919 case EXPR_BINARY_ISLESS:
1920 case EXPR_BINARY_ISLESSEQUAL:
1921 case EXPR_BINARY_ISLESSGREATER:
1922 case EXPR_BINARY_ISUNORDERED:
1923 return is_constant_expression(expression->binary.left)
1924 && is_constant_expression(expression->binary.right);
1926 case EXPR_COMPOUND_LITERAL:
1927 return is_constant_initializer(expression->compound_literal.initializer);
1929 case EXPR_CONDITIONAL: {
1930 expression_t *condition = expression->conditional.condition;
1931 if(!is_constant_expression(condition))
1934 long val = fold_constant(condition);
1936 return is_constant_expression(expression->conditional.true_expression);
1938 return is_constant_expression(expression->conditional.false_expression);
1947 panic("invalid expression found (is constant expression)");
1951 * Initialize the AST construction.
1955 obstack_init(&ast_obstack);
1963 obstack_free(&ast_obstack, NULL);
1967 * Set the output stream for the AST printer.
1969 * @param stream the output stream
1971 void ast_set_output(FILE *stream)
1974 type_set_output(stream);
1978 * Allocate an AST object of the given size.
1980 * @param size the size of the object to allocate
1982 * @return A new allocated object in the AST memeory space.
1984 void *(allocate_ast)(size_t size)
1986 return _allocate_ast(size);