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)
78 case 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_POSTFIX,
130 [EXPR_UNARY_POSTFIX_DECREMENT] = PREC_POSTFIX,
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_ISGREATER] = PREC_PRIMARY,
172 [EXPR_BINARY_ISGREATEREQUAL] = PREC_PRIMARY,
173 [EXPR_BINARY_ISLESS] = PREC_PRIMARY,
174 [EXPR_BINARY_ISLESSEQUAL] = PREC_PRIMARY,
175 [EXPR_BINARY_ISLESSGREATER] = PREC_PRIMARY,
176 [EXPR_BINARY_ISUNORDERED] = PREC_PRIMARY
178 assert((unsigned)kind < (sizeof(prec)/sizeof(prec[0])));
179 unsigned res = prec[kind];
181 assert(res != PREC_BOTTOM);
186 * Print a constant expression.
188 * @param cnst the constant expression
190 static void print_const(const const_expression_t *cnst)
192 if (cnst->base.type == NULL)
195 const type_t *const type = skip_typeref(cnst->base.type);
197 if (is_type_atomic(type, ATOMIC_TYPE_BOOL)) {
198 fputs(cnst->v.int_value ? "true" : "false", out);
199 } else 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;
231 * Print a quoted string constant.
233 * @param string the string constant
234 * @param border the border char
235 * @param skip number of chars to skip at the end
237 static void print_quoted_string(const string_t *const string, char border, int skip)
240 const char *end = string->begin + string->size - skip;
241 for (const char *c = string->begin; c != end; ++c) {
246 case '\\': fputs("\\\\", out); break;
247 case '\a': fputs("\\a", out); break;
248 case '\b': fputs("\\b", out); break;
249 case '\f': fputs("\\f", out); break;
250 case '\n': fputs("\\n", out); break;
251 case '\r': fputs("\\r", out); break;
252 case '\t': fputs("\\t", out); break;
253 case '\v': fputs("\\v", out); break;
254 case '\?': fputs("\\?", out); break;
256 if (c_mode & _GNUC) {
257 fputs("\\e", out); break;
262 fprintf(out, "\\%03o", (unsigned)*c);
273 * Prints a wide string literal expression.
275 * @param wstr the wide string literal expression
276 * @param border the border char
277 * @param skip number of chars to skip at the end
279 static void print_quoted_wide_string(const wide_string_t *const wstr,
280 char border, int skip)
284 const wchar_rep_t *end = wstr->begin + wstr->size - skip;
285 for (const wchar_rep_t *c = wstr->begin; c != end; ++c) {
287 case L'\"': fputs("\\\"", out); break;
288 case L'\\': fputs("\\\\", out); break;
289 case L'\a': fputs("\\a", out); break;
290 case L'\b': fputs("\\b", out); break;
291 case L'\f': fputs("\\f", out); break;
292 case L'\n': fputs("\\n", out); break;
293 case L'\r': fputs("\\r", out); break;
294 case L'\t': fputs("\\t", out); break;
295 case L'\v': fputs("\\v", out); break;
296 case L'\?': fputs("\\?", out); break;
298 if (c_mode & _GNUC) {
299 fputs("\\e", out); break;
303 const unsigned tc = *c;
306 fprintf(out, "\\%03o", (char)*c);
310 } else if (tc < 0x800) {
311 fputc(0xC0 | (tc >> 6), out);
312 fputc(0x80 | (tc & 0x3F), out);
313 } else if (tc < 0x10000) {
314 fputc(0xE0 | ( tc >> 12), out);
315 fputc(0x80 | ((tc >> 6) & 0x3F), out);
316 fputc(0x80 | ( tc & 0x3F), out);
318 fputc(0xF0 | ( tc >> 18), out);
319 fputc(0x80 | ((tc >> 12) & 0x3F), out);
320 fputc(0x80 | ((tc >> 6) & 0x3F), out);
321 fputc(0x80 | ( tc & 0x3F), out);
330 * Print a constant character expression.
332 * @param cnst the constant character expression
334 static void print_character_constant(const const_expression_t *cnst)
336 print_quoted_string(&cnst->v.character, '\'', 0);
339 static void print_wide_character_constant(const const_expression_t *cnst)
341 print_quoted_wide_string(&cnst->v.wide_character, '\'', 0);
345 * Prints a string literal expression.
347 * @param string_literal the string literal expression
349 static void print_string_literal(
350 const string_literal_expression_t *string_literal)
352 print_quoted_string(&string_literal->value, '"', 1);
356 * Prints a predefined symbol.
358 static void print_funcname(const funcname_expression_t *funcname)
361 switch (funcname->kind) {
362 case FUNCNAME_FUNCTION: s = (c_mode & _C99) ? "__func__" : "__FUNCTION__"; break;
363 case FUNCNAME_PRETTY_FUNCTION: s = "__PRETTY_FUNCTION__"; break;
364 case FUNCNAME_FUNCSIG: s = "__FUNCSIG__"; break;
365 case FUNCNAME_FUNCDNAME: s = "__FUNCDNAME__"; break;
370 static void print_wide_string_literal(
371 const wide_string_literal_expression_t *const wstr)
373 print_quoted_wide_string(&wstr->value, '"', 1);
376 static void print_compound_literal(
377 const compound_literal_expression_t *expression)
380 print_type(expression->type);
382 print_initializer(expression->initializer);
386 * Prints a call expression.
388 * @param call the call expression
390 static void print_call_expression(const call_expression_t *call)
392 unsigned prec = get_expression_precedence(call->base.kind);
393 print_expression_prec(call->function, prec);
395 call_argument_t *argument = call->arguments;
397 while (argument != NULL) {
403 print_expression_prec(argument->expression, PREC_ASSIGNMENT);
405 argument = argument->next;
411 * Prints a binary expression.
413 * @param binexpr the binary expression
415 static void print_binary_expression(const binary_expression_t *binexpr)
417 unsigned prec = get_expression_precedence(binexpr->base.kind);
418 int r2l = right_to_left(prec);
420 print_expression_prec(binexpr->left, prec + r2l);
422 switch (binexpr->base.kind) {
423 case EXPR_BINARY_COMMA: op = ", "; break;
424 case EXPR_BINARY_ASSIGN: op = " = "; break;
425 case EXPR_BINARY_ADD: op = " + "; break;
426 case EXPR_BINARY_SUB: op = " - "; break;
427 case EXPR_BINARY_MUL: op = " * "; break;
428 case EXPR_BINARY_MOD: op = " % "; break;
429 case EXPR_BINARY_DIV: op = " / "; break;
430 case EXPR_BINARY_BITWISE_OR: op = " | "; break;
431 case EXPR_BINARY_BITWISE_AND: op = " & "; break;
432 case EXPR_BINARY_BITWISE_XOR: op = " ^ "; break;
433 case EXPR_BINARY_LOGICAL_OR: op = " || "; break;
434 case EXPR_BINARY_LOGICAL_AND: op = " && "; break;
435 case EXPR_BINARY_NOTEQUAL: op = " != "; break;
436 case EXPR_BINARY_EQUAL: op = " == "; break;
437 case EXPR_BINARY_LESS: op = " < "; break;
438 case EXPR_BINARY_LESSEQUAL: op = " <= "; break;
439 case EXPR_BINARY_GREATER: op = " > "; break;
440 case EXPR_BINARY_GREATEREQUAL: op = " >= "; break;
441 case EXPR_BINARY_SHIFTLEFT: op = " << "; break;
442 case EXPR_BINARY_SHIFTRIGHT: op = " >> "; break;
444 case EXPR_BINARY_ADD_ASSIGN: op = " += "; break;
445 case EXPR_BINARY_SUB_ASSIGN: op = " -= "; break;
446 case EXPR_BINARY_MUL_ASSIGN: op = " *= "; break;
447 case EXPR_BINARY_MOD_ASSIGN: op = " %= "; break;
448 case EXPR_BINARY_DIV_ASSIGN: op = " /= "; break;
449 case EXPR_BINARY_BITWISE_OR_ASSIGN: op = " |= "; break;
450 case EXPR_BINARY_BITWISE_AND_ASSIGN: op = " &= "; break;
451 case EXPR_BINARY_BITWISE_XOR_ASSIGN: op = " ^= "; break;
452 case EXPR_BINARY_SHIFTLEFT_ASSIGN: op = " <<= "; break;
453 case EXPR_BINARY_SHIFTRIGHT_ASSIGN: op = " >>= "; break;
454 default: panic("invalid binexpression found");
457 print_expression_prec(binexpr->right, prec + 1 - r2l);
461 * Prints an unary expression.
463 * @param unexpr the unary expression
465 static void print_unary_expression(const unary_expression_t *unexpr)
467 unsigned prec = get_expression_precedence(unexpr->base.kind);
468 switch (unexpr->base.kind) {
469 case EXPR_UNARY_NEGATE: fputc('-', out); break;
470 case EXPR_UNARY_PLUS: fputc('+', out); break;
471 case EXPR_UNARY_NOT: fputc('!', out); break;
472 case EXPR_UNARY_BITWISE_NEGATE: fputc('~', out); break;
473 case EXPR_UNARY_PREFIX_INCREMENT: fputs("++", out); break;
474 case EXPR_UNARY_PREFIX_DECREMENT: fputs("--", out); break;
475 case EXPR_UNARY_DEREFERENCE: fputc('*', out); break;
476 case EXPR_UNARY_TAKE_ADDRESS: fputc('&', out); break;
477 case EXPR_UNARY_DELETE: fputs("delete ", out); break;
478 case EXPR_UNARY_DELETE_ARRAY: fputs("delete [] ", out); break;
480 case EXPR_UNARY_POSTFIX_INCREMENT:
481 print_expression_prec(unexpr->value, prec);
484 case EXPR_UNARY_POSTFIX_DECREMENT:
485 print_expression_prec(unexpr->value, prec);
488 case EXPR_UNARY_CAST_IMPLICIT:
489 case EXPR_UNARY_CAST:
491 print_type(unexpr->base.type);
494 case EXPR_UNARY_ASSUME:
495 fputs("__assume(", out);
496 print_expression_prec(unexpr->value, PREC_ASSIGNMENT);
500 case EXPR_UNARY_THROW:
501 if (unexpr->value == NULL) {
505 fputs("throw ", out);
509 panic("invalid unary expression found");
511 print_expression_prec(unexpr->value, prec);
515 * Prints a reference expression.
517 * @param ref the reference expression
519 static void print_reference_expression(const reference_expression_t *ref)
521 fputs(ref->entity->base.symbol->string, out);
525 * Prints a label address expression.
527 * @param ref the reference expression
529 static void print_label_address_expression(const label_address_expression_t *le)
531 fprintf(out, "&&%s", le->label->base.symbol->string);
535 * Prints an array expression.
537 * @param expression the array expression
539 static void print_array_expression(const array_access_expression_t *expression)
541 unsigned prec = get_expression_precedence(expression->base.kind);
542 if (!expression->flipped) {
543 print_expression_prec(expression->array_ref, prec);
545 print_expression(expression->index);
548 print_expression_prec(expression->index, prec);
550 print_expression(expression->array_ref);
556 * Prints a typeproperty expression (sizeof or __alignof__).
558 * @param expression the type property expression
560 static void print_typeprop_expression(const typeprop_expression_t *expression)
562 if (expression->base.kind == EXPR_SIZEOF) {
563 fputs("sizeof", out);
565 assert(expression->base.kind == EXPR_ALIGNOF);
566 fputs("__alignof__", out);
568 if (expression->tp_expression != NULL) {
569 /* always print the '()' here, sizeof x is right but unusual */
571 print_expression(expression->tp_expression);
575 print_type(expression->type);
581 * Prints an builtin symbol.
583 * @param expression the builtin symbol expression
585 static void print_builtin_symbol(const builtin_symbol_expression_t *expression)
587 fputs(expression->symbol->string, out);
591 * Prints a builtin constant expression.
593 * @param expression the builtin constant expression
595 static void print_builtin_constant(const builtin_constant_expression_t *expression)
597 fputs("__builtin_constant_p(", out);
598 print_expression_prec(expression->value, PREC_ASSIGNMENT);
603 * Prints a builtin prefetch expression.
605 * @param expression the builtin prefetch expression
607 static void print_builtin_prefetch(const builtin_prefetch_expression_t *expression)
609 fputs("__builtin_prefetch(", out);
610 print_expression_prec(expression->adr, PREC_ASSIGNMENT);
611 if (expression->rw) {
613 print_expression_prec(expression->rw, PREC_ASSIGNMENT);
615 if (expression->locality) {
617 print_expression_prec(expression->locality, PREC_ASSIGNMENT);
623 * Prints a conditional expression.
625 * @param expression the conditional expression
627 static void print_conditional(const conditional_expression_t *expression)
629 print_expression_prec(expression->condition, PREC_LOGICAL_OR);
631 if (expression->true_expression != NULL) {
632 print_expression_prec(expression->true_expression, PREC_EXPRESSION);
637 precedence_t prec = c_mode & _CXX ? PREC_ASSIGNMENT : PREC_CONDITIONAL;
638 print_expression_prec(expression->false_expression, prec);
642 * Prints a va_start expression.
644 * @param expression the va_start expression
646 static void print_va_start(const va_start_expression_t *const expression)
648 fputs("__builtin_va_start(", out);
649 print_expression_prec(expression->ap, PREC_ASSIGNMENT);
651 fputs(expression->parameter->base.base.symbol->string, out);
656 * Prints a va_arg expression.
658 * @param expression the va_arg expression
660 static void print_va_arg(const va_arg_expression_t *expression)
662 fputs("__builtin_va_arg(", out);
663 print_expression_prec(expression->ap, PREC_ASSIGNMENT);
665 print_type(expression->base.type);
670 * Prints a select expression (. or ->).
672 * @param expression the select expression
674 static void print_select(const select_expression_t *expression)
676 unsigned prec = get_expression_precedence(expression->base.kind);
677 print_expression_prec(expression->compound, prec);
678 if (is_type_pointer(skip_typeref(expression->compound->base.type))) {
683 fputs(expression->compound_entry->base.symbol->string, out);
687 * Prints a type classify expression.
689 * @param expr the type classify expression
691 static void print_classify_type_expression(
692 const classify_type_expression_t *const expr)
694 fputs("__builtin_classify_type(", out);
695 print_expression_prec(expr->type_expression, PREC_ASSIGNMENT);
700 * Prints a designator.
702 * @param designator the designator
704 static void print_designator(const designator_t *designator)
706 for ( ; designator != NULL; designator = designator->next) {
707 if (designator->symbol == NULL) {
709 print_expression(designator->array_index);
713 fputs(designator->symbol->string, out);
719 * Prints an offsetof expression.
721 * @param expression the offset expression
723 static void print_offsetof_expression(const offsetof_expression_t *expression)
725 fputs("__builtin_offsetof", out);
727 print_type(expression->type);
729 print_designator(expression->designator);
734 * Prints a statement expression.
736 * @param expression the statement expression
738 static void print_statement_expression(const statement_expression_t *expression)
741 print_statement(expression->statement);
746 * Prints an expression with parenthesis if needed.
748 * @param expression the expression to print
749 * @param top_prec the precedence of the user of this expression.
751 static void print_expression_prec(const expression_t *expression, unsigned top_prec)
753 if (expression->kind == EXPR_UNARY_CAST_IMPLICIT && !print_implicit_casts) {
754 expression = expression->unary.value;
756 unsigned prec = get_expression_precedence(expression->base.kind);
757 if (print_parenthesis && top_prec != PREC_BOTTOM)
761 switch (expression->kind) {
764 fputs("$invalid expression$", out);
766 case EXPR_CHARACTER_CONSTANT:
767 print_character_constant(&expression->conste);
769 case EXPR_WIDE_CHARACTER_CONSTANT:
770 print_wide_character_constant(&expression->conste);
773 print_const(&expression->conste);
776 print_funcname(&expression->funcname);
778 case EXPR_STRING_LITERAL:
779 print_string_literal(&expression->string);
781 case EXPR_WIDE_STRING_LITERAL:
782 print_wide_string_literal(&expression->wide_string);
784 case EXPR_COMPOUND_LITERAL:
785 print_compound_literal(&expression->compound_literal);
788 print_call_expression(&expression->call);
791 print_binary_expression(&expression->binary);
794 case EXPR_REFERENCE_ENUM_VALUE:
795 print_reference_expression(&expression->reference);
797 case EXPR_ARRAY_ACCESS:
798 print_array_expression(&expression->array_access);
800 case EXPR_LABEL_ADDRESS:
801 print_label_address_expression(&expression->label_address);
804 print_unary_expression(&expression->unary);
808 print_typeprop_expression(&expression->typeprop);
810 case EXPR_BUILTIN_SYMBOL:
811 print_builtin_symbol(&expression->builtin_symbol);
813 case EXPR_BUILTIN_CONSTANT_P:
814 print_builtin_constant(&expression->builtin_constant);
816 case EXPR_BUILTIN_PREFETCH:
817 print_builtin_prefetch(&expression->builtin_prefetch);
819 case EXPR_CONDITIONAL:
820 print_conditional(&expression->conditional);
823 print_va_start(&expression->va_starte);
826 print_va_arg(&expression->va_arge);
829 print_select(&expression->select);
831 case EXPR_CLASSIFY_TYPE:
832 print_classify_type_expression(&expression->classify_type);
835 print_offsetof_expression(&expression->offsetofe);
838 print_statement_expression(&expression->statement);
843 fprintf(out, "some expression of type %d", (int)expression->kind);
851 * Print an compound statement.
853 * @param block the compound statement
855 static void print_compound_statement(const compound_statement_t *block)
860 statement_t *statement = block->statements;
861 while (statement != NULL) {
862 if (statement->base.kind == STATEMENT_CASE_LABEL)
864 if (statement->kind != STATEMENT_LABEL)
866 print_statement(statement);
868 statement = statement->base.next;
876 * Print a return statement.
878 * @param statement the return statement
880 static void print_return_statement(const return_statement_t *statement)
882 fputs("return ", out);
883 if (statement->value != NULL)
884 print_expression(statement->value);
889 * Print an expression statement.
891 * @param statement the expression statement
893 static void print_expression_statement(const expression_statement_t *statement)
895 print_expression(statement->expression);
900 * Print a goto statement.
902 * @param statement the goto statement
904 static void print_goto_statement(const goto_statement_t *statement)
907 if (statement->expression != NULL) {
909 print_expression(statement->expression);
911 fputs(statement->label->base.symbol->string, out);
917 * Print a label statement.
919 * @param statement the label statement
921 static void print_label_statement(const label_statement_t *statement)
923 fprintf(out, "%s:\n", statement->label->base.symbol->string);
925 print_statement(statement->statement);
929 * Print an if statement.
931 * @param statement the if statement
933 static void print_if_statement(const if_statement_t *statement)
936 print_expression(statement->condition);
938 print_statement(statement->true_statement);
940 if (statement->false_statement != NULL) {
943 print_statement(statement->false_statement);
948 * Print a switch statement.
950 * @param statement the switch statement
952 static void print_switch_statement(const switch_statement_t *statement)
954 fputs("switch (", out);
955 print_expression(statement->expression);
957 print_statement(statement->body);
961 * Print a case label (including the default label).
963 * @param statement the case label statement
965 static void print_case_label(const case_label_statement_t *statement)
967 if (statement->expression == NULL) {
968 fputs("default:\n", out);
971 print_expression(statement->expression);
972 if (statement->end_range != NULL) {
974 print_expression(statement->end_range);
979 if (statement->statement != NULL) {
980 if (statement->statement->base.kind == STATEMENT_CASE_LABEL) {
984 print_statement(statement->statement);
988 static void print_local_label(const local_label_statement_t *statement)
990 fputs("__label__ ", out);
993 entity_t *entity = statement->labels_begin;
995 entity != statement->labels_end->base.next;
996 entity = entity->base.next) {
1002 fputs(entity->base.symbol->string, out);
1007 static void print_typedef(const entity_t *entity)
1009 fputs("typedef ", out);
1010 print_type_ext(entity->typedefe.type, entity->base.symbol, NULL);
1015 * returns true if the entity is a compiler generated one and has no real
1016 * correspondenc in the source file
1018 static bool is_generated_entity(const entity_t *entity)
1020 if (entity->kind == ENTITY_TYPEDEF)
1021 return entity->typedefe.builtin;
1023 if (is_declaration(entity))
1024 return entity->declaration.implicit;
1030 * Print a declaration statement.
1032 * @param statement the statement
1034 static void print_declaration_statement(
1035 const declaration_statement_t *statement)
1038 entity_t *entity = statement->declarations_begin;
1039 if (entity == NULL) {
1040 fputs("/* empty declaration statement */\n", out);
1044 entity_t *const end = statement->declarations_end->base.next;
1045 for (; entity != end; entity = entity->base.next) {
1046 if (!is_declaration(entity) && entity->kind != ENTITY_TYPEDEF)
1048 if (is_generated_entity(entity))
1057 if (entity->kind == ENTITY_TYPEDEF) {
1058 print_typedef(entity);
1060 assert(is_declaration(entity));
1061 print_declaration(entity);
1069 * Print a while statement.
1071 * @param statement the statement
1073 static void print_while_statement(const while_statement_t *statement)
1075 fputs("while (", out);
1076 print_expression(statement->condition);
1078 print_statement(statement->body);
1082 * Print a do-while statement.
1084 * @param statement the statement
1086 static void print_do_while_statement(const do_while_statement_t *statement)
1089 print_statement(statement->body);
1091 fputs("while (", out);
1092 print_expression(statement->condition);
1097 * Print a for statement.
1099 * @param statement the statement
1101 static void print_for_statement(const for_statement_t *statement)
1103 fputs("for (", out);
1104 entity_t *entity = statement->scope.entities;
1105 while (entity != NULL && is_generated_entity(entity))
1106 entity = entity->base.next;
1108 if (entity != NULL) {
1109 assert(statement->initialisation == NULL);
1110 assert(is_declaration(entity));
1111 print_declaration(entity);
1112 if (entity->base.next != NULL) {
1113 panic("multiple declarations in for statement not supported yet");
1117 if (statement->initialisation) {
1118 print_expression(statement->initialisation);
1122 if (statement->condition != NULL) {
1123 print_expression(statement->condition);
1126 if (statement->step != NULL) {
1127 print_expression(statement->step);
1130 print_statement(statement->body);
1134 * Print assembler arguments.
1136 * @param arguments the arguments
1138 static void print_asm_arguments(asm_argument_t *arguments)
1140 asm_argument_t *argument = arguments;
1141 for (; argument != NULL; argument = argument->next) {
1142 if (argument != arguments)
1145 if (argument->symbol) {
1146 fprintf(out, "[%s] ", argument->symbol->string);
1148 print_quoted_string(&argument->constraints, '"', 1);
1150 print_expression(argument->expression);
1156 * Print assembler clobbers.
1158 * @param clobbers the clobbers
1160 static void print_asm_clobbers(asm_clobber_t *clobbers)
1162 asm_clobber_t *clobber = clobbers;
1163 for (; clobber != NULL; clobber = clobber->next) {
1164 if (clobber != clobbers)
1167 print_quoted_string(&clobber->clobber, '"', 1);
1172 * Print an assembler statement.
1174 * @param statement the statement
1176 static void print_asm_statement(const asm_statement_t *statement)
1179 if (statement->is_volatile) {
1180 fputs("volatile ", out);
1183 print_quoted_string(&statement->asm_text, '"', 1);
1184 if (statement->outputs == NULL &&
1185 statement->inputs == NULL &&
1186 statement->clobbers == NULL)
1187 goto end_of_print_asm_statement;
1190 print_asm_arguments(statement->outputs);
1191 if (statement->inputs == NULL && statement->clobbers == NULL)
1192 goto end_of_print_asm_statement;
1195 print_asm_arguments(statement->inputs);
1196 if (statement->clobbers == NULL)
1197 goto end_of_print_asm_statement;
1200 print_asm_clobbers(statement->clobbers);
1202 end_of_print_asm_statement:
1207 * Print a microsoft __try statement.
1209 * @param statement the statement
1211 static void print_ms_try_statement(const ms_try_statement_t *statement)
1213 fputs("__try ", out);
1214 print_statement(statement->try_statement);
1216 if (statement->except_expression != NULL) {
1217 fputs("__except(", out);
1218 print_expression(statement->except_expression);
1221 fputs("__finally ", out);
1223 print_statement(statement->final_statement);
1227 * Print a microsoft __leave statement.
1229 * @param statement the statement
1231 static void print_leave_statement(const leave_statement_t *statement)
1234 fputs("__leave;\n", out);
1238 * Print a statement.
1240 * @param statement the statement
1242 void print_statement(const statement_t *statement)
1244 switch (statement->kind) {
1245 case STATEMENT_EMPTY:
1248 case STATEMENT_COMPOUND:
1249 print_compound_statement(&statement->compound);
1251 case STATEMENT_RETURN:
1252 print_return_statement(&statement->returns);
1254 case STATEMENT_EXPRESSION:
1255 print_expression_statement(&statement->expression);
1257 case STATEMENT_LABEL:
1258 print_label_statement(&statement->label);
1260 case STATEMENT_LOCAL_LABEL:
1261 print_local_label(&statement->local_label);
1263 case STATEMENT_GOTO:
1264 print_goto_statement(&statement->gotos);
1266 case STATEMENT_CONTINUE:
1267 fputs("continue;\n", out);
1269 case STATEMENT_BREAK:
1270 fputs("break;\n", out);
1273 print_if_statement(&statement->ifs);
1275 case STATEMENT_SWITCH:
1276 print_switch_statement(&statement->switchs);
1278 case STATEMENT_CASE_LABEL:
1279 print_case_label(&statement->case_label);
1281 case STATEMENT_DECLARATION:
1282 print_declaration_statement(&statement->declaration);
1284 case STATEMENT_WHILE:
1285 print_while_statement(&statement->whiles);
1287 case STATEMENT_DO_WHILE:
1288 print_do_while_statement(&statement->do_while);
1291 print_for_statement(&statement->fors);
1294 print_asm_statement(&statement->asms);
1296 case STATEMENT_MS_TRY:
1297 print_ms_try_statement(&statement->ms_try);
1299 case STATEMENT_LEAVE:
1300 print_leave_statement(&statement->leave);
1302 case STATEMENT_INVALID:
1303 fputs("$invalid statement$\n", out);
1309 * Print a storage class.
1311 * @param storage_class the storage class
1313 static void print_storage_class(storage_class_tag_t storage_class)
1316 switch (storage_class) {
1317 case STORAGE_CLASS_NONE: return;
1318 case STORAGE_CLASS_TYPEDEF: text = "typedef "; break;
1319 case STORAGE_CLASS_EXTERN: text = "extern "; break;
1320 case STORAGE_CLASS_STATIC: text = "static "; break;
1321 case STORAGE_CLASS_AUTO: text = "auto "; break;
1322 case STORAGE_CLASS_REGISTER: text = "register "; break;
1328 * Print an initializer.
1330 * @param initializer the initializer
1332 void print_initializer(const initializer_t *initializer)
1334 if (initializer == NULL) {
1339 switch (initializer->kind) {
1340 case INITIALIZER_VALUE: {
1341 const initializer_value_t *value = &initializer->value;
1342 print_expression(value->value);
1345 case INITIALIZER_LIST: {
1346 assert(initializer->kind == INITIALIZER_LIST);
1348 const initializer_list_t *list = &initializer->list;
1350 for (size_t i = 0 ; i < list->len; ++i) {
1351 const initializer_t *sub_init = list->initializers[i];
1352 print_initializer(list->initializers[i]);
1353 if (i < list->len-1) {
1354 if (sub_init == NULL || sub_init->kind != INITIALIZER_DESIGNATOR)
1361 case INITIALIZER_STRING:
1362 print_quoted_string(&initializer->string.string, '"', 1);
1364 case INITIALIZER_WIDE_STRING:
1365 print_quoted_wide_string(&initializer->wide_string.string, '"', 1);
1367 case INITIALIZER_DESIGNATOR:
1368 print_designator(initializer->designator.designator);
1373 panic("invalid initializer kind found");
1377 * Print microsoft extended declaration modifiers.
1379 static void print_ms_modifiers(const declaration_t *declaration)
1381 if ((c_mode & _MS) == 0)
1384 decl_modifiers_t modifiers = declaration->modifiers;
1386 bool ds_shown = false;
1387 const char *next = "(";
1389 if (declaration->base.kind == ENTITY_VARIABLE) {
1390 variable_t *variable = (variable_t*)declaration;
1391 if (variable->alignment != 0
1392 || variable->get_property_sym != NULL
1393 || variable->put_property_sym != NULL) {
1395 fputs("__declspec", out);
1399 if (variable->alignment != 0) {
1400 fputs(next, out); next = ", "; fprintf(out, "align(%u)", variable->alignment);
1402 if (variable->get_property_sym != NULL
1403 || variable->put_property_sym != NULL) {
1405 fputs(next, out); next = ", "; fputs("property(", out);
1406 if (variable->get_property_sym != NULL) {
1407 fprintf(out, "get=%s", variable->get_property_sym->string);
1410 if (variable->put_property_sym != NULL)
1411 fprintf(out, "%sput=%s", comma, variable->put_property_sym->string);
1417 /* DM_FORCEINLINE handled outside. */
1418 if ((modifiers & ~DM_FORCEINLINE) != 0) {
1420 fputs("__declspec", out);
1423 if (modifiers & DM_DLLIMPORT) {
1424 fputs(next, out); next = ", "; fputs("dllimport", out);
1426 if (modifiers & DM_DLLEXPORT) {
1427 fputs(next, out); next = ", "; fputs("dllexport", out);
1429 if (modifiers & DM_THREAD) {
1430 fputs(next, out); next = ", "; fputs("thread", out);
1432 if (modifiers & DM_NAKED) {
1433 fputs(next, out); next = ", "; fputs("naked", out);
1435 if (modifiers & DM_THREAD) {
1436 fputs(next, out); next = ", "; fputs("thread", out);
1438 if (modifiers & DM_SELECTANY) {
1439 fputs(next, out); next = ", "; fputs("selectany", out);
1441 if (modifiers & DM_NOTHROW) {
1442 fputs(next, out); next = ", "; fputs("nothrow", out);
1444 if (modifiers & DM_NORETURN) {
1445 fputs(next, out); next = ", "; fputs("noreturn", out);
1447 if (modifiers & DM_NOINLINE) {
1448 fputs(next, out); next = ", "; fputs("noinline", out);
1450 if (modifiers & DM_DEPRECATED) {
1451 fputs(next, out); next = ", "; fputs("deprecated", out);
1452 if (declaration->deprecated_string != NULL)
1453 fprintf(out, "(\"%s\")",
1454 declaration->deprecated_string);
1456 if (modifiers & DM_RESTRICT) {
1457 fputs(next, out); next = ", "; fputs("restrict", out);
1459 if (modifiers & DM_NOALIAS) {
1460 fputs(next, out); next = ", "; fputs("noalias", out);
1468 static void print_scope(const scope_t *scope)
1470 const entity_t *entity = scope->entities;
1471 for ( ; entity != NULL; entity = entity->base.next) {
1473 print_entity(entity);
1478 static void print_namespace(const namespace_t *namespace)
1480 fputs("namespace ", out);
1481 if (namespace->base.symbol != NULL) {
1482 fputs(namespace->base.symbol->string, out);
1489 print_scope(&namespace->members);
1497 * Print a variable or function declaration
1499 void print_declaration(const entity_t *entity)
1501 assert(is_declaration(entity));
1502 const declaration_t *declaration = &entity->declaration;
1504 print_storage_class((storage_class_tag_t)declaration->declared_storage_class);
1505 if (entity->kind == ENTITY_FUNCTION) {
1506 function_t *function = (function_t*)declaration;
1507 if (function->is_inline) {
1508 if (declaration->modifiers & DM_FORCEINLINE) {
1509 fputs("__forceinline ", out);
1510 } else if (declaration->modifiers & DM_MICROSOFT_INLINE) {
1511 fputs("__inline ", out);
1513 fputs("inline ", out);
1517 print_ms_modifiers(declaration);
1518 switch (entity->kind) {
1519 case ENTITY_FUNCTION:
1520 print_type_ext(entity->declaration.type, entity->base.symbol,
1521 &entity->function.parameters);
1523 if (entity->function.statement != NULL) {
1526 print_statement(entity->function.statement);
1531 case ENTITY_VARIABLE:
1532 if (entity->variable.thread_local)
1533 fputs("__thread ", out);
1534 print_type_ext(declaration->type, declaration->base.symbol, NULL);
1535 if (entity->variable.initializer != NULL) {
1537 print_initializer(entity->variable.initializer);
1542 print_type_ext(declaration->type, declaration->base.symbol, NULL);
1549 * Prints an expression.
1551 * @param expression the expression
1553 void print_expression(const expression_t *expression)
1555 print_expression_prec(expression, PREC_BOTTOM);
1559 * Print a declaration.
1561 * @param declaration the declaration
1563 void print_entity(const entity_t *entity)
1565 if (entity->base.namespc != NAMESPACE_NORMAL && entity->base.symbol == NULL)
1568 switch ((entity_kind_tag_t)entity->kind) {
1569 case ENTITY_VARIABLE:
1570 case ENTITY_PARAMETER:
1571 case ENTITY_COMPOUND_MEMBER:
1572 print_declaration(entity);
1574 case ENTITY_FUNCTION:
1575 print_declaration(entity);
1577 case ENTITY_TYPEDEF:
1578 print_typedef(entity);
1581 fputs("struct ", out);
1582 fputs(entity->base.symbol->string, out);
1583 if (entity->structe.complete) {
1585 print_compound_definition(&entity->structe);
1590 fputs("union ", out);
1591 fputs(entity->base.symbol->string, out);
1592 if (entity->unione.complete) {
1594 print_compound_definition(&entity->unione);
1599 fputs("enum ", out);
1600 fputs(entity->base.symbol->string, out);
1602 print_enum_definition(&entity->enume);
1605 case ENTITY_NAMESPACE:
1606 print_namespace(&entity->namespacee);
1609 case ENTITY_ENUM_VALUE:
1610 case ENTITY_LOCAL_LABEL:
1611 panic("print_entity used on unexpected entity type");
1612 case ENTITY_INVALID:
1615 panic("Invalid entity type encountered");
1619 * Print the AST of a translation unit.
1621 * @param unit the translation unit
1623 void print_ast(const translation_unit_t *unit)
1627 entity_t *entity = unit->scope.entities;
1628 for ( ; entity != NULL; entity = entity->base.next) {
1629 if (entity->kind == ENTITY_ENUM_VALUE)
1631 if (entity->base.namespc != NAMESPACE_NORMAL
1632 && entity->base.symbol == NULL)
1634 if (is_generated_entity(entity))
1638 print_entity(entity);
1643 bool is_constant_initializer(const initializer_t *initializer)
1645 switch (initializer->kind) {
1646 case INITIALIZER_STRING:
1647 case INITIALIZER_WIDE_STRING:
1648 case INITIALIZER_DESIGNATOR:
1651 case INITIALIZER_VALUE:
1652 return is_constant_expression(initializer->value.value);
1654 case INITIALIZER_LIST:
1655 for (size_t i = 0; i < initializer->list.len; ++i) {
1656 initializer_t *sub_initializer = initializer->list.initializers[i];
1657 if (!is_constant_initializer(sub_initializer))
1662 panic("invalid initializer kind found");
1665 static bool is_object_with_linker_constant_address(const expression_t *expression)
1667 switch (expression->kind) {
1668 case EXPR_UNARY_DEREFERENCE:
1669 return is_address_constant(expression->unary.value);
1672 type_t *base_type = skip_typeref(expression->select.compound->base.type);
1673 if (is_type_pointer(base_type)) {
1675 return is_address_constant(expression->select.compound);
1677 return is_object_with_linker_constant_address(expression->select.compound);
1681 case EXPR_ARRAY_ACCESS:
1682 return is_constant_expression(expression->array_access.index)
1683 && is_address_constant(expression->array_access.array_ref);
1685 case EXPR_REFERENCE: {
1686 entity_t *entity = expression->reference.entity;
1687 if (is_declaration(entity)) {
1688 switch ((storage_class_tag_t)entity->declaration.storage_class) {
1689 case STORAGE_CLASS_NONE:
1690 case STORAGE_CLASS_EXTERN:
1691 case STORAGE_CLASS_STATIC:
1693 entity->kind != ENTITY_VARIABLE ||
1694 !entity->variable.thread_local;
1696 case STORAGE_CLASS_REGISTER:
1697 case STORAGE_CLASS_TYPEDEF:
1698 case STORAGE_CLASS_AUTO:
1710 bool is_address_constant(const expression_t *expression)
1712 switch (expression->kind) {
1713 case EXPR_UNARY_TAKE_ADDRESS:
1714 return is_object_with_linker_constant_address(expression->unary.value);
1716 case EXPR_UNARY_DEREFERENCE: {
1718 = revert_automatic_type_conversion(expression->unary.value);
1719 /* dereferencing a function is a NOP */
1720 if (is_type_function(real_type)) {
1721 return is_address_constant(expression->unary.value);
1726 case EXPR_UNARY_CAST: {
1727 type_t *dest = skip_typeref(expression->base.type);
1728 if (!is_type_pointer(dest) && (
1729 dest->kind != TYPE_ATOMIC ||
1730 !(get_atomic_type_flags(dest->atomic.akind) & ATOMIC_TYPE_FLAG_INTEGER) ||
1731 get_atomic_type_size(dest->atomic.akind) < get_atomic_type_size(get_intptr_kind())
1735 return (is_constant_expression(expression->unary.value)
1736 || is_address_constant(expression->unary.value));
1739 case EXPR_BINARY_ADD:
1740 case EXPR_BINARY_SUB: {
1741 expression_t *left = expression->binary.left;
1742 expression_t *right = expression->binary.right;
1744 if (is_type_pointer(skip_typeref(left->base.type))) {
1745 return is_address_constant(left) && is_constant_expression(right);
1746 } else if (is_type_pointer(skip_typeref(right->base.type))) {
1747 return is_constant_expression(left) && is_address_constant(right);
1753 case EXPR_REFERENCE: {
1754 entity_t *entity = expression->reference.entity;
1755 if (!is_declaration(entity))
1758 type_t *type = skip_typeref(entity->declaration.type);
1759 if (is_type_function(type))
1761 if (is_type_array(type)) {
1762 return is_object_with_linker_constant_address(expression);
1764 /* Prevent stray errors */
1765 if (!is_type_valid(type))
1770 case EXPR_ARRAY_ACCESS: {
1771 type_t *const type =
1772 skip_typeref(revert_automatic_type_conversion(expression));
1774 is_type_array(type) &&
1775 is_constant_expression(expression->array_access.index) &&
1776 is_address_constant(expression->array_access.array_ref);
1784 static bool is_builtin_const_call(const expression_t *expression)
1786 expression_t *function = expression->call.function;
1787 if (function->kind != EXPR_BUILTIN_SYMBOL) {
1791 symbol_t *symbol = function->builtin_symbol.symbol;
1793 switch (symbol->ID) {
1794 case T___builtin_huge_val:
1795 case T___builtin_inf:
1796 case T___builtin_inff:
1797 case T___builtin_infl:
1798 case T___builtin_nan:
1799 case T___builtin_nanf:
1800 case T___builtin_nanl:
1807 static bool is_constant_pointer(const expression_t *expression)
1809 if (is_constant_expression(expression))
1812 switch (expression->kind) {
1813 case EXPR_UNARY_CAST:
1814 return is_constant_pointer(expression->unary.value);
1820 static bool is_object_with_constant_address(const expression_t *expression)
1822 switch (expression->kind) {
1824 expression_t *compound = expression->select.compound;
1825 type_t *compound_type = compound->base.type;
1826 compound_type = skip_typeref(compound_type);
1827 if (is_type_pointer(compound_type)) {
1828 return is_constant_pointer(compound);
1830 return is_object_with_constant_address(compound);
1834 case EXPR_ARRAY_ACCESS: {
1835 array_access_expression_t const* const array_access =
1836 &expression->array_access;
1838 is_constant_expression(array_access->index) && (
1839 is_object_with_constant_address(array_access->array_ref) ||
1840 is_constant_pointer(array_access->array_ref)
1844 case EXPR_UNARY_DEREFERENCE:
1845 return is_constant_pointer(expression->unary.value);
1851 bool is_constant_expression(const expression_t *expression)
1853 switch (expression->kind) {
1856 case EXPR_CHARACTER_CONSTANT:
1857 case EXPR_WIDE_CHARACTER_CONSTANT:
1858 case EXPR_STRING_LITERAL:
1859 case EXPR_WIDE_STRING_LITERAL:
1860 case EXPR_CLASSIFY_TYPE:
1864 case EXPR_BUILTIN_CONSTANT_P:
1865 case EXPR_LABEL_ADDRESS:
1866 case EXPR_REFERENCE_ENUM_VALUE:
1870 type_t *type = expression->typeprop.type;
1872 type = expression->typeprop.tp_expression->base.type;
1874 type = skip_typeref(type);
1875 if (is_type_array(type) && type->array.is_vla)
1880 case EXPR_BUILTIN_SYMBOL:
1881 case EXPR_BUILTIN_PREFETCH:
1885 case EXPR_STATEMENT:
1886 case EXPR_REFERENCE:
1887 case EXPR_UNARY_POSTFIX_INCREMENT:
1888 case EXPR_UNARY_POSTFIX_DECREMENT:
1889 case EXPR_UNARY_PREFIX_INCREMENT:
1890 case EXPR_UNARY_PREFIX_DECREMENT:
1891 case EXPR_UNARY_ASSUME: /* has VOID type */
1892 case EXPR_UNARY_DEREFERENCE:
1893 case EXPR_UNARY_DELETE:
1894 case EXPR_UNARY_DELETE_ARRAY:
1895 case EXPR_UNARY_THROW:
1896 case EXPR_BINARY_ASSIGN:
1897 case EXPR_BINARY_MUL_ASSIGN:
1898 case EXPR_BINARY_DIV_ASSIGN:
1899 case EXPR_BINARY_MOD_ASSIGN:
1900 case EXPR_BINARY_ADD_ASSIGN:
1901 case EXPR_BINARY_SUB_ASSIGN:
1902 case EXPR_BINARY_SHIFTLEFT_ASSIGN:
1903 case EXPR_BINARY_SHIFTRIGHT_ASSIGN:
1904 case EXPR_BINARY_BITWISE_AND_ASSIGN:
1905 case EXPR_BINARY_BITWISE_XOR_ASSIGN:
1906 case EXPR_BINARY_BITWISE_OR_ASSIGN:
1907 case EXPR_BINARY_COMMA:
1908 case EXPR_ARRAY_ACCESS:
1911 case EXPR_UNARY_TAKE_ADDRESS:
1912 return is_object_with_constant_address(expression->unary.value);
1915 return is_builtin_const_call(expression);
1917 case EXPR_UNARY_NEGATE:
1918 case EXPR_UNARY_PLUS:
1919 case EXPR_UNARY_BITWISE_NEGATE:
1920 case EXPR_UNARY_NOT:
1921 return is_constant_expression(expression->unary.value);
1923 case EXPR_UNARY_CAST:
1924 case EXPR_UNARY_CAST_IMPLICIT:
1925 return is_type_arithmetic(skip_typeref(expression->base.type))
1926 && is_constant_expression(expression->unary.value);
1928 case EXPR_BINARY_ADD:
1929 case EXPR_BINARY_SUB:
1930 case EXPR_BINARY_MUL:
1931 case EXPR_BINARY_DIV:
1932 case EXPR_BINARY_MOD:
1933 case EXPR_BINARY_EQUAL:
1934 case EXPR_BINARY_NOTEQUAL:
1935 case EXPR_BINARY_LESS:
1936 case EXPR_BINARY_LESSEQUAL:
1937 case EXPR_BINARY_GREATER:
1938 case EXPR_BINARY_GREATEREQUAL:
1939 case EXPR_BINARY_BITWISE_AND:
1940 case EXPR_BINARY_BITWISE_OR:
1941 case EXPR_BINARY_BITWISE_XOR:
1942 case EXPR_BINARY_LOGICAL_AND:
1943 case EXPR_BINARY_LOGICAL_OR:
1944 case EXPR_BINARY_SHIFTLEFT:
1945 case EXPR_BINARY_SHIFTRIGHT:
1946 case EXPR_BINARY_ISGREATER:
1947 case EXPR_BINARY_ISGREATEREQUAL:
1948 case EXPR_BINARY_ISLESS:
1949 case EXPR_BINARY_ISLESSEQUAL:
1950 case EXPR_BINARY_ISLESSGREATER:
1951 case EXPR_BINARY_ISUNORDERED:
1952 return is_constant_expression(expression->binary.left)
1953 && is_constant_expression(expression->binary.right);
1955 case EXPR_COMPOUND_LITERAL:
1956 return is_constant_initializer(expression->compound_literal.initializer);
1958 case EXPR_CONDITIONAL: {
1959 expression_t *condition = expression->conditional.condition;
1960 if (!is_constant_expression(condition))
1963 long val = fold_constant(condition);
1965 return is_constant_expression(expression->conditional.true_expression);
1967 return is_constant_expression(expression->conditional.false_expression);
1976 panic("invalid expression found (is constant expression)");
1980 * Initialize the AST construction.
1984 obstack_init(&ast_obstack);
1992 obstack_free(&ast_obstack, NULL);
1996 * Set the output stream for the AST printer.
1998 * @param stream the output stream
2000 void ast_set_output(FILE *stream)
2003 type_set_output(stream);
2007 * Allocate an AST object of the given size.
2009 * @param size the size of the object to allocate
2011 * @return A new allocated object in the AST memeory space.
2013 void *(allocate_ast)(size_t size)
2015 return _allocate_ast(size);