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 #if defined(__INTEL_COMPILER)
36 #elif defined(__CYGWIN__)
37 #include "win32/cygwin_math_ext.h"
42 #include "adt/error.h"
44 struct obstack ast_obstack;
49 /** If set, implicit casts are printed. */
50 bool print_implicit_casts = false;
52 /** If set parenthesis are printed to indicate operator precedence. */
53 bool print_parenthesis = false;
55 static void print_statement(const statement_t *statement);
56 static void print_expression_prec(const expression_t *expression, unsigned prec);
58 void change_indent(int delta)
64 void print_indent(void)
66 for (int i = 0; i < indent; ++i)
71 * Returns 1 if a given precedence level has right-to-left
72 * associativity, else 0.
74 * @param precedence the operator precedence
76 static int right_to_left(unsigned precedence)
80 case PREC_CONDITIONAL:
90 * Return the precedence of an expression given by its kind.
92 * @param kind the expression kind
94 static unsigned get_expression_precedence(expression_kind_t kind)
96 static const unsigned prec[] = {
97 [EXPR_UNKNOWN] = PREC_PRIMARY,
98 [EXPR_INVALID] = PREC_PRIMARY,
99 [EXPR_REFERENCE] = PREC_PRIMARY,
100 [EXPR_REFERENCE_ENUM_VALUE] = PREC_PRIMARY,
101 [EXPR_CHARACTER_CONSTANT] = PREC_PRIMARY,
102 [EXPR_WIDE_CHARACTER_CONSTANT] = PREC_PRIMARY,
103 [EXPR_CONST] = PREC_PRIMARY,
104 [EXPR_STRING_LITERAL] = PREC_PRIMARY,
105 [EXPR_WIDE_STRING_LITERAL] = PREC_PRIMARY,
106 [EXPR_COMPOUND_LITERAL] = PREC_UNARY,
107 [EXPR_CALL] = PREC_POSTFIX,
108 [EXPR_CONDITIONAL] = PREC_CONDITIONAL,
109 [EXPR_SELECT] = PREC_POSTFIX,
110 [EXPR_ARRAY_ACCESS] = PREC_POSTFIX,
111 [EXPR_SIZEOF] = PREC_UNARY,
112 [EXPR_CLASSIFY_TYPE] = PREC_UNARY,
113 [EXPR_ALIGNOF] = PREC_UNARY,
115 [EXPR_FUNCNAME] = PREC_PRIMARY,
116 [EXPR_BUILTIN_SYMBOL] = PREC_PRIMARY,
117 [EXPR_BUILTIN_CONSTANT_P] = PREC_PRIMARY,
118 [EXPR_BUILTIN_PREFETCH] = PREC_PRIMARY,
119 [EXPR_OFFSETOF] = PREC_PRIMARY,
120 [EXPR_VA_START] = PREC_PRIMARY,
121 [EXPR_VA_ARG] = PREC_PRIMARY,
122 [EXPR_STATEMENT] = PREC_PRIMARY,
123 [EXPR_LABEL_ADDRESS] = PREC_PRIMARY,
125 [EXPR_UNARY_NEGATE] = PREC_UNARY,
126 [EXPR_UNARY_PLUS] = PREC_UNARY,
127 [EXPR_UNARY_BITWISE_NEGATE] = PREC_UNARY,
128 [EXPR_UNARY_NOT] = PREC_UNARY,
129 [EXPR_UNARY_DEREFERENCE] = PREC_UNARY,
130 [EXPR_UNARY_TAKE_ADDRESS] = PREC_UNARY,
131 [EXPR_UNARY_POSTFIX_INCREMENT] = PREC_POSTFIX,
132 [EXPR_UNARY_POSTFIX_DECREMENT] = PREC_POSTFIX,
133 [EXPR_UNARY_PREFIX_INCREMENT] = PREC_UNARY,
134 [EXPR_UNARY_PREFIX_DECREMENT] = PREC_UNARY,
135 [EXPR_UNARY_CAST] = PREC_UNARY,
136 [EXPR_UNARY_CAST_IMPLICIT] = PREC_UNARY,
137 [EXPR_UNARY_ASSUME] = PREC_PRIMARY,
138 [EXPR_UNARY_DELETE] = PREC_UNARY,
139 [EXPR_UNARY_DELETE_ARRAY] = PREC_UNARY,
140 [EXPR_UNARY_THROW] = PREC_ASSIGNMENT,
142 [EXPR_BINARY_ADD] = PREC_ADDITIVE,
143 [EXPR_BINARY_SUB] = PREC_ADDITIVE,
144 [EXPR_BINARY_MUL] = PREC_MULTIPLICATIVE,
145 [EXPR_BINARY_DIV] = PREC_MULTIPLICATIVE,
146 [EXPR_BINARY_MOD] = PREC_MULTIPLICATIVE,
147 [EXPR_BINARY_EQUAL] = PREC_EQUALITY,
148 [EXPR_BINARY_NOTEQUAL] = PREC_EQUALITY,
149 [EXPR_BINARY_LESS] = PREC_RELATIONAL,
150 [EXPR_BINARY_LESSEQUAL] = PREC_RELATIONAL,
151 [EXPR_BINARY_GREATER] = PREC_RELATIONAL,
152 [EXPR_BINARY_GREATEREQUAL] = PREC_RELATIONAL,
153 [EXPR_BINARY_BITWISE_AND] = PREC_AND,
154 [EXPR_BINARY_BITWISE_OR] = PREC_OR,
155 [EXPR_BINARY_BITWISE_XOR] = PREC_XOR,
156 [EXPR_BINARY_LOGICAL_AND] = PREC_LOGICAL_AND,
157 [EXPR_BINARY_LOGICAL_OR] = PREC_LOGICAL_OR,
158 [EXPR_BINARY_SHIFTLEFT] = PREC_SHIFT,
159 [EXPR_BINARY_SHIFTRIGHT] = PREC_SHIFT,
160 [EXPR_BINARY_ASSIGN] = PREC_ASSIGNMENT,
161 [EXPR_BINARY_MUL_ASSIGN] = PREC_ASSIGNMENT,
162 [EXPR_BINARY_DIV_ASSIGN] = PREC_ASSIGNMENT,
163 [EXPR_BINARY_MOD_ASSIGN] = PREC_ASSIGNMENT,
164 [EXPR_BINARY_ADD_ASSIGN] = PREC_ASSIGNMENT,
165 [EXPR_BINARY_SUB_ASSIGN] = PREC_ASSIGNMENT,
166 [EXPR_BINARY_SHIFTLEFT_ASSIGN] = PREC_ASSIGNMENT,
167 [EXPR_BINARY_SHIFTRIGHT_ASSIGN] = PREC_ASSIGNMENT,
168 [EXPR_BINARY_BITWISE_AND_ASSIGN] = PREC_ASSIGNMENT,
169 [EXPR_BINARY_BITWISE_XOR_ASSIGN] = PREC_ASSIGNMENT,
170 [EXPR_BINARY_BITWISE_OR_ASSIGN] = PREC_ASSIGNMENT,
171 [EXPR_BINARY_COMMA] = PREC_EXPRESSION,
173 [EXPR_BINARY_ISGREATER] = PREC_PRIMARY,
174 [EXPR_BINARY_ISGREATEREQUAL] = PREC_PRIMARY,
175 [EXPR_BINARY_ISLESS] = PREC_PRIMARY,
176 [EXPR_BINARY_ISLESSEQUAL] = PREC_PRIMARY,
177 [EXPR_BINARY_ISLESSGREATER] = PREC_PRIMARY,
178 [EXPR_BINARY_ISUNORDERED] = PREC_PRIMARY
180 assert((unsigned)kind < (sizeof(prec)/sizeof(prec[0])));
181 unsigned res = prec[kind];
183 assert(res != PREC_BOTTOM);
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_atomic(type, ATOMIC_TYPE_BOOL)) {
200 fputs(cnst->v.int_value ? "true" : "false", out);
201 } else if (is_type_integer(type)) {
202 fprintf(out, "%lld", cnst->v.int_value);
203 } else if (is_type_float(type)) {
204 long double const val = cnst->v.float_value;
206 /* ARG, no way to print long double */
207 fprintf(out, "%.20g", (double)val);
209 fprintf(out, "%.20Lg", val);
211 if (isfinite(val) && truncl(val) == val)
214 panic("unknown constant");
218 switch (type->atomic.akind) {
219 case ATOMIC_TYPE_UINT: suffix = "U"; break;
220 case ATOMIC_TYPE_LONG: suffix = "L"; break;
221 case ATOMIC_TYPE_ULONG: suffix = "UL"; break;
222 case ATOMIC_TYPE_LONGLONG: suffix = "LL"; break;
223 case ATOMIC_TYPE_ULONGLONG: suffix = "ULL"; break;
224 case ATOMIC_TYPE_FLOAT: suffix = "F"; break;
225 case ATOMIC_TYPE_LONG_DOUBLE: suffix = "L"; break;
233 * Print a quoted string constant.
235 * @param string the string constant
236 * @param border the border char
237 * @param skip number of chars to skip at the end
239 static void print_quoted_string(const string_t *const string, char border, int skip)
242 const char *end = string->begin + string->size - skip;
243 for (const char *c = string->begin; c != end; ++c) {
244 unsigned char const tc = *c;
249 case '\\': fputs("\\\\", out); break;
250 case '\a': fputs("\\a", out); break;
251 case '\b': fputs("\\b", out); break;
252 case '\f': fputs("\\f", out); break;
253 case '\n': fputs("\\n", out); break;
254 case '\r': fputs("\\r", out); break;
255 case '\t': fputs("\\t", out); break;
256 case '\v': fputs("\\v", out); break;
257 case '\?': fputs("\\?", out); break;
259 if (c_mode & _GNUC) {
260 fputs("\\e", out); break;
264 if (tc < 0x80 && !isprint(tc)) {
265 fprintf(out, "\\%03o", (unsigned)tc);
276 * Prints a wide string literal expression.
278 * @param wstr the wide string literal expression
279 * @param border the border char
280 * @param skip number of chars to skip at the end
282 static void print_quoted_wide_string(const wide_string_t *const wstr,
283 char border, int skip)
287 const wchar_rep_t *end = wstr->begin + wstr->size - skip;
288 for (const wchar_rep_t *c = wstr->begin; c != end; ++c) {
290 case L'\"': fputs("\\\"", out); break;
291 case L'\\': fputs("\\\\", out); break;
292 case L'\a': fputs("\\a", out); break;
293 case L'\b': fputs("\\b", out); break;
294 case L'\f': fputs("\\f", out); break;
295 case L'\n': fputs("\\n", out); break;
296 case L'\r': fputs("\\r", out); break;
297 case L'\t': fputs("\\t", out); break;
298 case L'\v': fputs("\\v", out); break;
299 case L'\?': fputs("\\?", out); break;
301 if (c_mode & _GNUC) {
302 fputs("\\e", out); break;
306 const unsigned tc = *c;
311 fprintf(out, "\\%03o", tc);
313 } else if (tc < 0x800) {
314 fputc(0xC0 | (tc >> 6), out);
315 fputc(0x80 | (tc & 0x3F), out);
316 } else if (tc < 0x10000) {
317 fputc(0xE0 | ( tc >> 12), out);
318 fputc(0x80 | ((tc >> 6) & 0x3F), out);
319 fputc(0x80 | ( tc & 0x3F), out);
321 fputc(0xF0 | ( tc >> 18), out);
322 fputc(0x80 | ((tc >> 12) & 0x3F), out);
323 fputc(0x80 | ((tc >> 6) & 0x3F), out);
324 fputc(0x80 | ( tc & 0x3F), out);
333 * Print a constant character expression.
335 * @param cnst the constant character expression
337 static void print_character_constant(const const_expression_t *cnst)
339 print_quoted_string(&cnst->v.character, '\'', 0);
342 static void print_wide_character_constant(const const_expression_t *cnst)
344 print_quoted_wide_string(&cnst->v.wide_character, '\'', 0);
348 * Prints a string literal expression.
350 * @param string_literal the string literal expression
352 static void print_string_literal(
353 const string_literal_expression_t *string_literal)
355 print_quoted_string(&string_literal->value, '"', 1);
359 * Prints a predefined symbol.
361 static void print_funcname(const funcname_expression_t *funcname)
364 switch (funcname->kind) {
365 case FUNCNAME_FUNCTION: s = (c_mode & _C99) ? "__func__" : "__FUNCTION__"; break;
366 case FUNCNAME_PRETTY_FUNCTION: s = "__PRETTY_FUNCTION__"; break;
367 case FUNCNAME_FUNCSIG: s = "__FUNCSIG__"; break;
368 case FUNCNAME_FUNCDNAME: s = "__FUNCDNAME__"; break;
373 static void print_wide_string_literal(
374 const wide_string_literal_expression_t *const wstr)
376 print_quoted_wide_string(&wstr->value, '"', 1);
379 static void print_compound_literal(
380 const compound_literal_expression_t *expression)
383 print_type(expression->type);
385 print_initializer(expression->initializer);
388 static void print_assignment_expression(const expression_t *const expr)
390 print_expression_prec(expr, PREC_ASSIGNMENT);
394 * Prints a call expression.
396 * @param call the call expression
398 static void print_call_expression(const call_expression_t *call)
400 unsigned prec = get_expression_precedence(call->base.kind);
401 print_expression_prec(call->function, prec);
403 call_argument_t *argument = call->arguments;
405 while (argument != NULL) {
411 print_assignment_expression(argument->expression);
413 argument = argument->next;
419 * Prints a binary expression.
421 * @param binexpr the binary expression
423 static void print_binary_expression(const binary_expression_t *binexpr)
425 unsigned prec = get_expression_precedence(binexpr->base.kind);
426 int r2l = right_to_left(prec);
428 print_expression_prec(binexpr->left, prec + r2l);
430 switch (binexpr->base.kind) {
431 case EXPR_BINARY_COMMA: op = ", "; break;
432 case EXPR_BINARY_ASSIGN: op = " = "; break;
433 case EXPR_BINARY_ADD: op = " + "; break;
434 case EXPR_BINARY_SUB: op = " - "; break;
435 case EXPR_BINARY_MUL: op = " * "; break;
436 case EXPR_BINARY_MOD: op = " % "; break;
437 case EXPR_BINARY_DIV: op = " / "; break;
438 case EXPR_BINARY_BITWISE_OR: op = " | "; break;
439 case EXPR_BINARY_BITWISE_AND: op = " & "; break;
440 case EXPR_BINARY_BITWISE_XOR: op = " ^ "; break;
441 case EXPR_BINARY_LOGICAL_OR: op = " || "; break;
442 case EXPR_BINARY_LOGICAL_AND: op = " && "; break;
443 case EXPR_BINARY_NOTEQUAL: op = " != "; break;
444 case EXPR_BINARY_EQUAL: op = " == "; break;
445 case EXPR_BINARY_LESS: op = " < "; break;
446 case EXPR_BINARY_LESSEQUAL: op = " <= "; break;
447 case EXPR_BINARY_GREATER: op = " > "; break;
448 case EXPR_BINARY_GREATEREQUAL: op = " >= "; break;
449 case EXPR_BINARY_SHIFTLEFT: op = " << "; break;
450 case EXPR_BINARY_SHIFTRIGHT: op = " >> "; break;
452 case EXPR_BINARY_ADD_ASSIGN: op = " += "; break;
453 case EXPR_BINARY_SUB_ASSIGN: op = " -= "; break;
454 case EXPR_BINARY_MUL_ASSIGN: op = " *= "; break;
455 case EXPR_BINARY_MOD_ASSIGN: op = " %= "; break;
456 case EXPR_BINARY_DIV_ASSIGN: op = " /= "; break;
457 case EXPR_BINARY_BITWISE_OR_ASSIGN: op = " |= "; break;
458 case EXPR_BINARY_BITWISE_AND_ASSIGN: op = " &= "; break;
459 case EXPR_BINARY_BITWISE_XOR_ASSIGN: op = " ^= "; break;
460 case EXPR_BINARY_SHIFTLEFT_ASSIGN: op = " <<= "; break;
461 case EXPR_BINARY_SHIFTRIGHT_ASSIGN: op = " >>= "; break;
462 default: panic("invalid binexpression found");
465 print_expression_prec(binexpr->right, prec + 1 - r2l);
469 * Prints an unary expression.
471 * @param unexpr the unary expression
473 static void print_unary_expression(const unary_expression_t *unexpr)
475 unsigned prec = get_expression_precedence(unexpr->base.kind);
476 switch (unexpr->base.kind) {
477 case EXPR_UNARY_NEGATE: fputc('-', out); break;
478 case EXPR_UNARY_PLUS: fputc('+', out); break;
479 case EXPR_UNARY_NOT: fputc('!', out); break;
480 case EXPR_UNARY_BITWISE_NEGATE: fputc('~', out); break;
481 case EXPR_UNARY_PREFIX_INCREMENT: fputs("++", out); break;
482 case EXPR_UNARY_PREFIX_DECREMENT: fputs("--", out); break;
483 case EXPR_UNARY_DEREFERENCE: fputc('*', out); break;
484 case EXPR_UNARY_TAKE_ADDRESS: fputc('&', out); break;
485 case EXPR_UNARY_DELETE: fputs("delete ", out); break;
486 case EXPR_UNARY_DELETE_ARRAY: fputs("delete [] ", out); break;
488 case EXPR_UNARY_POSTFIX_INCREMENT:
489 print_expression_prec(unexpr->value, prec);
492 case EXPR_UNARY_POSTFIX_DECREMENT:
493 print_expression_prec(unexpr->value, prec);
496 case EXPR_UNARY_CAST_IMPLICIT:
497 case EXPR_UNARY_CAST:
499 print_type(unexpr->base.type);
502 case EXPR_UNARY_ASSUME:
503 fputs("__assume(", out);
504 print_assignment_expression(unexpr->value);
508 case EXPR_UNARY_THROW:
509 if (unexpr->value == NULL) {
513 fputs("throw ", out);
517 panic("invalid unary expression found");
519 print_expression_prec(unexpr->value, prec);
523 * Prints a reference expression.
525 * @param ref the reference expression
527 static void print_reference_expression(const reference_expression_t *ref)
529 fputs(ref->entity->base.symbol->string, out);
533 * Prints a label address expression.
535 * @param ref the reference expression
537 static void print_label_address_expression(const label_address_expression_t *le)
539 fprintf(out, "&&%s", le->label->base.symbol->string);
543 * Prints an array expression.
545 * @param expression the array expression
547 static void print_array_expression(const array_access_expression_t *expression)
549 unsigned prec = get_expression_precedence(expression->base.kind);
550 if (!expression->flipped) {
551 print_expression_prec(expression->array_ref, prec);
553 print_expression(expression->index);
556 print_expression_prec(expression->index, prec);
558 print_expression(expression->array_ref);
564 * Prints a typeproperty expression (sizeof or __alignof__).
566 * @param expression the type property expression
568 static void print_typeprop_expression(const typeprop_expression_t *expression)
570 if (expression->base.kind == EXPR_SIZEOF) {
571 fputs("sizeof", out);
573 assert(expression->base.kind == EXPR_ALIGNOF);
574 fputs("__alignof__", out);
576 if (expression->tp_expression != NULL) {
577 /* always print the '()' here, sizeof x is right but unusual */
579 print_expression(expression->tp_expression);
583 print_type(expression->type);
589 * Prints an builtin symbol.
591 * @param expression the builtin symbol expression
593 static void print_builtin_symbol(const builtin_symbol_expression_t *expression)
595 fputs(expression->symbol->string, out);
599 * Prints a builtin constant expression.
601 * @param expression the builtin constant expression
603 static void print_builtin_constant(const builtin_constant_expression_t *expression)
605 fputs("__builtin_constant_p(", out);
606 print_assignment_expression(expression->value);
611 * Prints a builtin prefetch expression.
613 * @param expression the builtin prefetch expression
615 static void print_builtin_prefetch(const builtin_prefetch_expression_t *expression)
617 fputs("__builtin_prefetch(", out);
618 print_assignment_expression(expression->adr);
619 if (expression->rw) {
621 print_assignment_expression(expression->rw);
623 if (expression->locality) {
625 print_assignment_expression(expression->locality);
631 * Prints a conditional expression.
633 * @param expression the conditional expression
635 static void print_conditional(const conditional_expression_t *expression)
637 print_expression_prec(expression->condition, PREC_LOGICAL_OR);
639 if (expression->true_expression != NULL) {
640 print_expression_prec(expression->true_expression, PREC_EXPRESSION);
645 precedence_t prec = c_mode & _CXX ? PREC_ASSIGNMENT : PREC_CONDITIONAL;
646 print_expression_prec(expression->false_expression, prec);
650 * Prints a va_start expression.
652 * @param expression the va_start expression
654 static void print_va_start(const va_start_expression_t *const expression)
656 fputs("__builtin_va_start(", out);
657 print_assignment_expression(expression->ap);
659 fputs(expression->parameter->base.base.symbol->string, out);
664 * Prints a va_arg expression.
666 * @param expression the va_arg expression
668 static void print_va_arg(const va_arg_expression_t *expression)
670 fputs("__builtin_va_arg(", out);
671 print_assignment_expression(expression->ap);
673 print_type(expression->base.type);
678 * Prints a select expression (. or ->).
680 * @param expression the select expression
682 static void print_select(const select_expression_t *expression)
684 unsigned prec = get_expression_precedence(expression->base.kind);
685 print_expression_prec(expression->compound, prec);
686 if (is_type_pointer(skip_typeref(expression->compound->base.type))) {
691 fputs(expression->compound_entry->base.symbol->string, out);
695 * Prints a type classify expression.
697 * @param expr the type classify expression
699 static void print_classify_type_expression(
700 const classify_type_expression_t *const expr)
702 fputs("__builtin_classify_type(", out);
703 print_assignment_expression(expr->type_expression);
708 * Prints a designator.
710 * @param designator the designator
712 static void print_designator(const designator_t *designator)
714 for ( ; designator != NULL; designator = designator->next) {
715 if (designator->symbol == NULL) {
717 print_expression(designator->array_index);
721 fputs(designator->symbol->string, out);
727 * Prints an offsetof expression.
729 * @param expression the offset expression
731 static void print_offsetof_expression(const offsetof_expression_t *expression)
733 fputs("__builtin_offsetof", out);
735 print_type(expression->type);
737 print_designator(expression->designator);
742 * Prints a statement expression.
744 * @param expression the statement expression
746 static void print_statement_expression(const statement_expression_t *expression)
749 print_statement(expression->statement);
754 * Prints an expression with parenthesis if needed.
756 * @param expression the expression to print
757 * @param top_prec the precedence of the user of this expression.
759 static void print_expression_prec(const expression_t *expression, unsigned top_prec)
761 if (expression->kind == EXPR_UNARY_CAST_IMPLICIT && !print_implicit_casts) {
762 expression = expression->unary.value;
766 expression->base.parenthesized ||
767 (print_parenthesis && top_prec != PREC_BOTTOM) ||
768 top_prec > get_expression_precedence(expression->base.kind);
772 switch (expression->kind) {
775 fputs("$invalid expression$", out);
777 case EXPR_CHARACTER_CONSTANT:
778 print_character_constant(&expression->conste);
780 case EXPR_WIDE_CHARACTER_CONSTANT:
781 print_wide_character_constant(&expression->conste);
784 print_const(&expression->conste);
787 print_funcname(&expression->funcname);
789 case EXPR_STRING_LITERAL:
790 print_string_literal(&expression->string);
792 case EXPR_WIDE_STRING_LITERAL:
793 print_wide_string_literal(&expression->wide_string);
795 case EXPR_COMPOUND_LITERAL:
796 print_compound_literal(&expression->compound_literal);
799 print_call_expression(&expression->call);
802 print_binary_expression(&expression->binary);
805 case EXPR_REFERENCE_ENUM_VALUE:
806 print_reference_expression(&expression->reference);
808 case EXPR_ARRAY_ACCESS:
809 print_array_expression(&expression->array_access);
811 case EXPR_LABEL_ADDRESS:
812 print_label_address_expression(&expression->label_address);
815 print_unary_expression(&expression->unary);
819 print_typeprop_expression(&expression->typeprop);
821 case EXPR_BUILTIN_SYMBOL:
822 print_builtin_symbol(&expression->builtin_symbol);
824 case EXPR_BUILTIN_CONSTANT_P:
825 print_builtin_constant(&expression->builtin_constant);
827 case EXPR_BUILTIN_PREFETCH:
828 print_builtin_prefetch(&expression->builtin_prefetch);
830 case EXPR_CONDITIONAL:
831 print_conditional(&expression->conditional);
834 print_va_start(&expression->va_starte);
837 print_va_arg(&expression->va_arge);
840 print_select(&expression->select);
842 case EXPR_CLASSIFY_TYPE:
843 print_classify_type_expression(&expression->classify_type);
846 print_offsetof_expression(&expression->offsetofe);
849 print_statement_expression(&expression->statement);
854 fprintf(out, "some expression of type %d", (int)expression->kind);
862 * Print an compound statement.
864 * @param block the compound statement
866 static void print_compound_statement(const compound_statement_t *block)
871 statement_t *statement = block->statements;
872 while (statement != NULL) {
873 if (statement->base.kind == STATEMENT_CASE_LABEL)
875 if (statement->kind != STATEMENT_LABEL)
877 print_statement(statement);
879 statement = statement->base.next;
883 fputs(block->stmt_expr ? "}" : "}\n", out);
887 * Print a return statement.
889 * @param statement the return statement
891 static void print_return_statement(const return_statement_t *statement)
893 expression_t const *const val = statement->value;
895 fputs("return ", out);
896 print_expression(val);
899 fputs("return;\n", out);
904 * Print an expression statement.
906 * @param statement the expression statement
908 static void print_expression_statement(const expression_statement_t *statement)
910 print_expression(statement->expression);
915 * Print a goto statement.
917 * @param statement the goto statement
919 static void print_goto_statement(const goto_statement_t *statement)
922 if (statement->expression != NULL) {
924 print_expression(statement->expression);
926 fputs(statement->label->base.symbol->string, out);
932 * Print a label statement.
934 * @param statement the label statement
936 static void print_label_statement(const label_statement_t *statement)
938 fprintf(out, "%s:\n", statement->label->base.symbol->string);
940 print_statement(statement->statement);
944 * Print an if statement.
946 * @param statement the if statement
948 static void print_if_statement(const if_statement_t *statement)
951 print_expression(statement->condition);
953 print_statement(statement->true_statement);
955 if (statement->false_statement != NULL) {
958 print_statement(statement->false_statement);
963 * Print a switch statement.
965 * @param statement the switch statement
967 static void print_switch_statement(const switch_statement_t *statement)
969 fputs("switch (", out);
970 print_expression(statement->expression);
972 print_statement(statement->body);
976 * Print a case label (including the default label).
978 * @param statement the case label statement
980 static void print_case_label(const case_label_statement_t *statement)
982 if (statement->expression == NULL) {
983 fputs("default:\n", out);
986 print_expression(statement->expression);
987 if (statement->end_range != NULL) {
989 print_expression(statement->end_range);
994 if (statement->statement != NULL) {
995 if (statement->statement->base.kind == STATEMENT_CASE_LABEL) {
999 print_statement(statement->statement);
1003 static void print_typedef(const entity_t *entity)
1005 fputs("typedef ", out);
1006 print_type_ext(entity->typedefe.type, entity->base.symbol, NULL);
1011 * returns true if the entity is a compiler generated one and has no real
1012 * correspondenc in the source file
1014 static bool is_generated_entity(const entity_t *entity)
1016 if (entity->kind == ENTITY_TYPEDEF)
1017 return entity->typedefe.builtin;
1019 if (is_declaration(entity))
1020 return entity->declaration.implicit;
1026 * Print a declaration statement.
1028 * @param statement the statement
1030 static void print_declaration_statement(
1031 const declaration_statement_t *statement)
1034 entity_t *entity = statement->declarations_begin;
1035 if (entity == NULL) {
1036 fputs("/* empty declaration statement */\n", out);
1040 entity_t *const end = statement->declarations_end->base.next;
1041 for (; entity != end; entity = entity->base.next) {
1042 if (entity->kind == ENTITY_ENUM_VALUE)
1044 if (is_generated_entity(entity))
1053 print_entity(entity);
1059 * Print a while statement.
1061 * @param statement the statement
1063 static void print_while_statement(const while_statement_t *statement)
1065 fputs("while (", out);
1066 print_expression(statement->condition);
1068 print_statement(statement->body);
1072 * Print a do-while statement.
1074 * @param statement the statement
1076 static void print_do_while_statement(const do_while_statement_t *statement)
1079 print_statement(statement->body);
1081 fputs("while (", out);
1082 print_expression(statement->condition);
1087 * Print a for statement.
1089 * @param statement the statement
1091 static void print_for_statement(const for_statement_t *statement)
1093 fputs("for (", out);
1094 entity_t *entity = statement->scope.entities;
1095 while (entity != NULL && is_generated_entity(entity))
1096 entity = entity->base.next;
1098 if (entity != NULL) {
1099 assert(statement->initialisation == NULL);
1100 assert(is_declaration(entity));
1101 print_declaration(entity);
1102 if (entity->base.next != NULL) {
1103 panic("multiple declarations in for statement not supported yet");
1106 if (statement->initialisation) {
1107 print_expression(statement->initialisation);
1111 if (statement->condition != NULL) {
1113 print_expression(statement->condition);
1116 if (statement->step != NULL) {
1118 print_expression(statement->step);
1121 print_statement(statement->body);
1125 * Print assembler arguments.
1127 * @param arguments the arguments
1129 static void print_asm_arguments(asm_argument_t *arguments)
1131 asm_argument_t *argument = arguments;
1132 for (; argument != NULL; argument = argument->next) {
1133 if (argument != arguments)
1136 if (argument->symbol) {
1137 fprintf(out, "[%s] ", argument->symbol->string);
1139 print_quoted_string(&argument->constraints, '"', 1);
1141 print_expression(argument->expression);
1147 * Print assembler clobbers.
1149 * @param clobbers the clobbers
1151 static void print_asm_clobbers(asm_clobber_t *clobbers)
1153 asm_clobber_t *clobber = clobbers;
1154 for (; clobber != NULL; clobber = clobber->next) {
1155 if (clobber != clobbers)
1158 print_quoted_string(&clobber->clobber, '"', 1);
1163 * Print an assembler statement.
1165 * @param statement the statement
1167 static void print_asm_statement(const asm_statement_t *statement)
1170 if (statement->is_volatile) {
1171 fputs("volatile ", out);
1174 print_quoted_string(&statement->asm_text, '"', 1);
1175 if (statement->outputs == NULL &&
1176 statement->inputs == NULL &&
1177 statement->clobbers == NULL)
1178 goto end_of_print_asm_statement;
1181 print_asm_arguments(statement->outputs);
1182 if (statement->inputs == NULL && statement->clobbers == NULL)
1183 goto end_of_print_asm_statement;
1186 print_asm_arguments(statement->inputs);
1187 if (statement->clobbers == NULL)
1188 goto end_of_print_asm_statement;
1191 print_asm_clobbers(statement->clobbers);
1193 end_of_print_asm_statement:
1198 * Print a microsoft __try statement.
1200 * @param statement the statement
1202 static void print_ms_try_statement(const ms_try_statement_t *statement)
1204 fputs("__try ", out);
1205 print_statement(statement->try_statement);
1207 if (statement->except_expression != NULL) {
1208 fputs("__except(", out);
1209 print_expression(statement->except_expression);
1212 fputs("__finally ", out);
1214 print_statement(statement->final_statement);
1218 * Print a microsoft __leave statement.
1220 * @param statement the statement
1222 static void print_leave_statement(const leave_statement_t *statement)
1225 fputs("__leave;\n", out);
1229 * Print a statement.
1231 * @param statement the statement
1233 void print_statement(const statement_t *statement)
1235 switch (statement->kind) {
1236 case STATEMENT_EMPTY:
1239 case STATEMENT_COMPOUND:
1240 print_compound_statement(&statement->compound);
1242 case STATEMENT_RETURN:
1243 print_return_statement(&statement->returns);
1245 case STATEMENT_EXPRESSION:
1246 print_expression_statement(&statement->expression);
1248 case STATEMENT_LABEL:
1249 print_label_statement(&statement->label);
1251 case STATEMENT_GOTO:
1252 print_goto_statement(&statement->gotos);
1254 case STATEMENT_CONTINUE:
1255 fputs("continue;\n", out);
1257 case STATEMENT_BREAK:
1258 fputs("break;\n", out);
1261 print_if_statement(&statement->ifs);
1263 case STATEMENT_SWITCH:
1264 print_switch_statement(&statement->switchs);
1266 case STATEMENT_CASE_LABEL:
1267 print_case_label(&statement->case_label);
1269 case STATEMENT_DECLARATION:
1270 print_declaration_statement(&statement->declaration);
1272 case STATEMENT_WHILE:
1273 print_while_statement(&statement->whiles);
1275 case STATEMENT_DO_WHILE:
1276 print_do_while_statement(&statement->do_while);
1279 print_for_statement(&statement->fors);
1282 print_asm_statement(&statement->asms);
1284 case STATEMENT_MS_TRY:
1285 print_ms_try_statement(&statement->ms_try);
1287 case STATEMENT_LEAVE:
1288 print_leave_statement(&statement->leave);
1290 case STATEMENT_INVALID:
1291 fputs("$invalid statement$\n", out);
1297 * Print a storage class.
1299 * @param storage_class the storage class
1301 static void print_storage_class(storage_class_tag_t storage_class)
1303 switch (storage_class) {
1304 case STORAGE_CLASS_NONE: return;
1305 case STORAGE_CLASS_TYPEDEF: fputs("typedef ", out); return;
1306 case STORAGE_CLASS_EXTERN: fputs("extern ", out); return;
1307 case STORAGE_CLASS_STATIC: fputs("static ", out); return;
1308 case STORAGE_CLASS_AUTO: fputs("auto ", out); return;
1309 case STORAGE_CLASS_REGISTER: fputs("register ", out); return;
1311 panic("invalid storage class");
1315 * Print an initializer.
1317 * @param initializer the initializer
1319 void print_initializer(const initializer_t *initializer)
1321 if (initializer == NULL) {
1326 switch (initializer->kind) {
1327 case INITIALIZER_VALUE: {
1328 const initializer_value_t *value = &initializer->value;
1329 print_assignment_expression(value->value);
1332 case INITIALIZER_LIST: {
1333 assert(initializer->kind == INITIALIZER_LIST);
1335 const initializer_list_t *list = &initializer->list;
1337 for (size_t i = 0 ; i < list->len; ++i) {
1338 const initializer_t *sub_init = list->initializers[i];
1339 print_initializer(list->initializers[i]);
1340 if (i < list->len-1) {
1341 if (sub_init == NULL || sub_init->kind != INITIALIZER_DESIGNATOR)
1348 case INITIALIZER_STRING:
1349 print_quoted_string(&initializer->string.string, '"', 1);
1351 case INITIALIZER_WIDE_STRING:
1352 print_quoted_wide_string(&initializer->wide_string.string, '"', 1);
1354 case INITIALIZER_DESIGNATOR:
1355 print_designator(initializer->designator.designator);
1360 panic("invalid initializer kind found");
1364 * Print microsoft extended declaration modifiers.
1366 static void print_ms_modifiers(const declaration_t *declaration)
1368 if ((c_mode & _MS) == 0)
1371 decl_modifiers_t modifiers = declaration->modifiers;
1373 bool ds_shown = false;
1374 const char *next = "(";
1376 if (declaration->base.kind == ENTITY_VARIABLE) {
1377 variable_t *variable = (variable_t*)declaration;
1378 if (variable->alignment != 0
1379 || variable->get_property_sym != NULL
1380 || variable->put_property_sym != NULL) {
1382 fputs("__declspec", out);
1386 if (variable->alignment != 0) {
1387 fputs(next, out); next = ", "; fprintf(out, "align(%u)", variable->alignment);
1389 if (variable->get_property_sym != NULL
1390 || variable->put_property_sym != NULL) {
1392 fputs(next, out); next = ", "; fputs("property(", out);
1393 if (variable->get_property_sym != NULL) {
1394 fprintf(out, "get=%s", variable->get_property_sym->string);
1397 if (variable->put_property_sym != NULL)
1398 fprintf(out, "%sput=%s", comma, variable->put_property_sym->string);
1404 /* DM_FORCEINLINE handled outside. */
1405 if ((modifiers & ~DM_FORCEINLINE) != 0) {
1407 fputs("__declspec", out);
1410 if (modifiers & DM_DLLIMPORT) {
1411 fputs(next, out); next = ", "; fputs("dllimport", out);
1413 if (modifiers & DM_DLLEXPORT) {
1414 fputs(next, out); next = ", "; fputs("dllexport", out);
1416 if (modifiers & DM_THREAD) {
1417 fputs(next, out); next = ", "; fputs("thread", out);
1419 if (modifiers & DM_NAKED) {
1420 fputs(next, out); next = ", "; fputs("naked", out);
1422 if (modifiers & DM_THREAD) {
1423 fputs(next, out); next = ", "; fputs("thread", out);
1425 if (modifiers & DM_SELECTANY) {
1426 fputs(next, out); next = ", "; fputs("selectany", out);
1428 if (modifiers & DM_NOTHROW) {
1429 fputs(next, out); next = ", "; fputs("nothrow", out);
1431 if (modifiers & DM_NORETURN) {
1432 fputs(next, out); next = ", "; fputs("noreturn", out);
1434 if (modifiers & DM_NOINLINE) {
1435 fputs(next, out); next = ", "; fputs("noinline", out);
1437 if (modifiers & DM_DEPRECATED) {
1438 fputs(next, out); next = ", "; fputs("deprecated", out);
1439 if (declaration->deprecated_string != NULL)
1440 fprintf(out, "(\"%s\")",
1441 declaration->deprecated_string);
1443 if (modifiers & DM_RESTRICT) {
1444 fputs(next, out); next = ", "; fputs("restrict", out);
1446 if (modifiers & DM_NOALIAS) {
1447 fputs(next, out); next = ", "; fputs("noalias", out);
1455 static void print_scope(const scope_t *scope)
1457 const entity_t *entity = scope->entities;
1458 for ( ; entity != NULL; entity = entity->base.next) {
1460 print_entity(entity);
1465 static void print_namespace(const namespace_t *namespace)
1467 fputs("namespace ", out);
1468 if (namespace->base.symbol != NULL) {
1469 fputs(namespace->base.symbol->string, out);
1476 print_scope(&namespace->members);
1484 * Print a variable or function declaration
1486 void print_declaration(const entity_t *entity)
1488 assert(is_declaration(entity));
1489 const declaration_t *declaration = &entity->declaration;
1491 print_storage_class((storage_class_tag_t)declaration->declared_storage_class);
1492 if (entity->kind == ENTITY_FUNCTION) {
1493 function_t *function = (function_t*)declaration;
1494 if (function->is_inline) {
1495 if (declaration->modifiers & DM_FORCEINLINE) {
1496 fputs("__forceinline ", out);
1497 } else if (declaration->modifiers & DM_MICROSOFT_INLINE) {
1498 fputs("__inline ", out);
1500 fputs("inline ", out);
1504 print_ms_modifiers(declaration);
1505 switch (entity->kind) {
1506 case ENTITY_FUNCTION:
1507 print_type_ext(entity->declaration.type, entity->base.symbol,
1508 &entity->function.parameters);
1510 if (entity->function.statement != NULL) {
1513 print_statement(entity->function.statement);
1518 case ENTITY_VARIABLE:
1519 if (entity->variable.thread_local)
1520 fputs("__thread ", out);
1521 print_type_ext(declaration->type, declaration->base.symbol, NULL);
1522 if (entity->variable.initializer != NULL) {
1524 print_initializer(entity->variable.initializer);
1529 print_type_ext(declaration->type, declaration->base.symbol, NULL);
1536 * Prints an expression.
1538 * @param expression the expression
1540 void print_expression(const expression_t *expression)
1542 print_expression_prec(expression, PREC_BOTTOM);
1546 * Print a declaration.
1548 * @param declaration the declaration
1550 void print_entity(const entity_t *entity)
1552 if (entity->base.namespc != NAMESPACE_NORMAL && entity->base.symbol == NULL)
1555 switch ((entity_kind_tag_t)entity->kind) {
1556 case ENTITY_VARIABLE:
1557 case ENTITY_PARAMETER:
1558 case ENTITY_COMPOUND_MEMBER:
1559 print_declaration(entity);
1561 case ENTITY_FUNCTION:
1562 print_declaration(entity);
1564 case ENTITY_TYPEDEF:
1565 print_typedef(entity);
1568 fputs("struct ", out);
1569 fputs(entity->base.symbol->string, out);
1570 if (entity->structe.complete) {
1572 print_compound_definition(&entity->structe);
1577 fputs("union ", out);
1578 fputs(entity->base.symbol->string, out);
1579 if (entity->unione.complete) {
1581 print_compound_definition(&entity->unione);
1586 fputs("enum ", out);
1587 fputs(entity->base.symbol->string, out);
1589 print_enum_definition(&entity->enume);
1592 case ENTITY_NAMESPACE:
1593 print_namespace(&entity->namespacee);
1595 case ENTITY_LOCAL_LABEL:
1596 fprintf(out, "__label__ %s;", entity->base.symbol->string);
1599 case ENTITY_ENUM_VALUE:
1600 panic("print_entity used on unexpected entity type");
1601 case ENTITY_INVALID:
1604 panic("Invalid entity type encountered");
1608 * Print the AST of a translation unit.
1610 * @param unit the translation unit
1612 void print_ast(const translation_unit_t *unit)
1616 entity_t *entity = unit->scope.entities;
1617 for ( ; entity != NULL; entity = entity->base.next) {
1618 if (entity->kind == ENTITY_ENUM_VALUE)
1620 if (entity->base.namespc != NAMESPACE_NORMAL
1621 && entity->base.symbol == NULL)
1623 if (is_generated_entity(entity))
1627 print_entity(entity);
1632 bool is_constant_initializer(const initializer_t *initializer)
1634 switch (initializer->kind) {
1635 case INITIALIZER_STRING:
1636 case INITIALIZER_WIDE_STRING:
1637 case INITIALIZER_DESIGNATOR:
1640 case INITIALIZER_VALUE:
1641 return is_constant_expression(initializer->value.value);
1643 case INITIALIZER_LIST:
1644 for (size_t i = 0; i < initializer->list.len; ++i) {
1645 initializer_t *sub_initializer = initializer->list.initializers[i];
1646 if (!is_constant_initializer(sub_initializer))
1651 panic("invalid initializer kind found");
1654 static bool is_object_with_linker_constant_address(const expression_t *expression)
1656 switch (expression->kind) {
1657 case EXPR_UNARY_DEREFERENCE:
1658 return is_address_constant(expression->unary.value);
1661 type_t *base_type = skip_typeref(expression->select.compound->base.type);
1662 if (is_type_pointer(base_type)) {
1664 return is_address_constant(expression->select.compound);
1666 return is_object_with_linker_constant_address(expression->select.compound);
1670 case EXPR_ARRAY_ACCESS:
1671 return is_constant_expression(expression->array_access.index)
1672 && is_address_constant(expression->array_access.array_ref);
1674 case EXPR_REFERENCE: {
1675 entity_t *entity = expression->reference.entity;
1676 if (is_declaration(entity)) {
1677 switch ((storage_class_tag_t)entity->declaration.storage_class) {
1678 case STORAGE_CLASS_NONE:
1679 case STORAGE_CLASS_EXTERN:
1680 case STORAGE_CLASS_STATIC:
1682 entity->kind != ENTITY_VARIABLE ||
1683 !entity->variable.thread_local;
1685 case STORAGE_CLASS_REGISTER:
1686 case STORAGE_CLASS_TYPEDEF:
1687 case STORAGE_CLASS_AUTO:
1699 bool is_address_constant(const expression_t *expression)
1701 switch (expression->kind) {
1702 case EXPR_UNARY_TAKE_ADDRESS:
1703 return is_object_with_linker_constant_address(expression->unary.value);
1705 case EXPR_UNARY_DEREFERENCE: {
1707 = revert_automatic_type_conversion(expression->unary.value);
1708 /* dereferencing a function is a NOP */
1709 if (is_type_function(real_type)) {
1710 return is_address_constant(expression->unary.value);
1715 case EXPR_UNARY_CAST: {
1716 type_t *dest = skip_typeref(expression->base.type);
1717 if (!is_type_pointer(dest) && (
1718 dest->kind != TYPE_ATOMIC ||
1719 !(get_atomic_type_flags(dest->atomic.akind) & ATOMIC_TYPE_FLAG_INTEGER) ||
1720 get_atomic_type_size(dest->atomic.akind) < get_atomic_type_size(get_intptr_kind())
1724 return (is_constant_expression(expression->unary.value)
1725 || is_address_constant(expression->unary.value));
1728 case EXPR_BINARY_ADD:
1729 case EXPR_BINARY_SUB: {
1730 expression_t *left = expression->binary.left;
1731 expression_t *right = expression->binary.right;
1733 if (is_type_pointer(skip_typeref(left->base.type))) {
1734 return is_address_constant(left) && is_constant_expression(right);
1735 } else if (is_type_pointer(skip_typeref(right->base.type))) {
1736 return is_constant_expression(left) && is_address_constant(right);
1742 case EXPR_REFERENCE: {
1743 entity_t *entity = expression->reference.entity;
1744 if (!is_declaration(entity))
1747 type_t *type = skip_typeref(entity->declaration.type);
1748 if (is_type_function(type))
1750 if (is_type_array(type)) {
1751 return is_object_with_linker_constant_address(expression);
1753 /* Prevent stray errors */
1754 if (!is_type_valid(type))
1759 case EXPR_ARRAY_ACCESS: {
1760 type_t *const type =
1761 skip_typeref(revert_automatic_type_conversion(expression));
1763 is_type_array(type) &&
1764 is_constant_expression(expression->array_access.index) &&
1765 is_address_constant(expression->array_access.array_ref);
1773 static bool is_builtin_const_call(const expression_t *expression)
1775 expression_t *function = expression->call.function;
1776 if (function->kind != EXPR_BUILTIN_SYMBOL) {
1780 symbol_t *symbol = function->builtin_symbol.symbol;
1782 switch (symbol->ID) {
1783 case T___builtin_huge_val:
1784 case T___builtin_inf:
1785 case T___builtin_inff:
1786 case T___builtin_infl:
1787 case T___builtin_nan:
1788 case T___builtin_nanf:
1789 case T___builtin_nanl:
1796 static bool is_constant_pointer(const expression_t *expression)
1798 if (is_constant_expression(expression))
1801 switch (expression->kind) {
1802 case EXPR_UNARY_CAST:
1803 return is_constant_pointer(expression->unary.value);
1809 static bool is_object_with_constant_address(const expression_t *expression)
1811 switch (expression->kind) {
1813 expression_t *compound = expression->select.compound;
1814 type_t *compound_type = compound->base.type;
1815 compound_type = skip_typeref(compound_type);
1816 if (is_type_pointer(compound_type)) {
1817 return is_constant_pointer(compound);
1819 return is_object_with_constant_address(compound);
1823 case EXPR_ARRAY_ACCESS: {
1824 array_access_expression_t const* const array_access =
1825 &expression->array_access;
1827 is_constant_expression(array_access->index) && (
1828 is_object_with_constant_address(array_access->array_ref) ||
1829 is_constant_pointer(array_access->array_ref)
1833 case EXPR_UNARY_DEREFERENCE:
1834 return is_constant_pointer(expression->unary.value);
1840 bool is_constant_expression(const expression_t *expression)
1842 switch (expression->kind) {
1845 case EXPR_CHARACTER_CONSTANT:
1846 case EXPR_WIDE_CHARACTER_CONSTANT:
1847 case EXPR_STRING_LITERAL:
1848 case EXPR_WIDE_STRING_LITERAL:
1849 case EXPR_CLASSIFY_TYPE:
1853 case EXPR_BUILTIN_CONSTANT_P:
1854 case EXPR_LABEL_ADDRESS:
1855 case EXPR_REFERENCE_ENUM_VALUE:
1859 type_t *type = expression->typeprop.type;
1861 type = expression->typeprop.tp_expression->base.type;
1863 type = skip_typeref(type);
1864 if (is_type_array(type) && type->array.is_vla)
1869 case EXPR_BUILTIN_SYMBOL:
1870 case EXPR_BUILTIN_PREFETCH:
1874 case EXPR_STATEMENT:
1875 case EXPR_REFERENCE:
1876 case EXPR_UNARY_POSTFIX_INCREMENT:
1877 case EXPR_UNARY_POSTFIX_DECREMENT:
1878 case EXPR_UNARY_PREFIX_INCREMENT:
1879 case EXPR_UNARY_PREFIX_DECREMENT:
1880 case EXPR_UNARY_ASSUME: /* has VOID type */
1881 case EXPR_UNARY_DEREFERENCE:
1882 case EXPR_UNARY_DELETE:
1883 case EXPR_UNARY_DELETE_ARRAY:
1884 case EXPR_UNARY_THROW:
1885 case EXPR_BINARY_ASSIGN:
1886 case EXPR_BINARY_MUL_ASSIGN:
1887 case EXPR_BINARY_DIV_ASSIGN:
1888 case EXPR_BINARY_MOD_ASSIGN:
1889 case EXPR_BINARY_ADD_ASSIGN:
1890 case EXPR_BINARY_SUB_ASSIGN:
1891 case EXPR_BINARY_SHIFTLEFT_ASSIGN:
1892 case EXPR_BINARY_SHIFTRIGHT_ASSIGN:
1893 case EXPR_BINARY_BITWISE_AND_ASSIGN:
1894 case EXPR_BINARY_BITWISE_XOR_ASSIGN:
1895 case EXPR_BINARY_BITWISE_OR_ASSIGN:
1896 case EXPR_BINARY_COMMA:
1897 case EXPR_ARRAY_ACCESS:
1900 case EXPR_UNARY_TAKE_ADDRESS:
1901 return is_object_with_constant_address(expression->unary.value);
1904 return is_builtin_const_call(expression);
1906 case EXPR_UNARY_NEGATE:
1907 case EXPR_UNARY_PLUS:
1908 case EXPR_UNARY_BITWISE_NEGATE:
1909 case EXPR_UNARY_NOT:
1910 return is_constant_expression(expression->unary.value);
1912 case EXPR_UNARY_CAST:
1913 case EXPR_UNARY_CAST_IMPLICIT:
1914 return is_type_arithmetic(skip_typeref(expression->base.type))
1915 && is_constant_expression(expression->unary.value);
1917 case EXPR_BINARY_ADD:
1918 case EXPR_BINARY_SUB:
1919 case EXPR_BINARY_MUL:
1920 case EXPR_BINARY_DIV:
1921 case EXPR_BINARY_MOD:
1922 case EXPR_BINARY_EQUAL:
1923 case EXPR_BINARY_NOTEQUAL:
1924 case EXPR_BINARY_LESS:
1925 case EXPR_BINARY_LESSEQUAL:
1926 case EXPR_BINARY_GREATER:
1927 case EXPR_BINARY_GREATEREQUAL:
1928 case EXPR_BINARY_BITWISE_AND:
1929 case EXPR_BINARY_BITWISE_OR:
1930 case EXPR_BINARY_BITWISE_XOR:
1931 case EXPR_BINARY_SHIFTLEFT:
1932 case EXPR_BINARY_SHIFTRIGHT:
1933 case EXPR_BINARY_ISGREATER:
1934 case EXPR_BINARY_ISGREATEREQUAL:
1935 case EXPR_BINARY_ISLESS:
1936 case EXPR_BINARY_ISLESSEQUAL:
1937 case EXPR_BINARY_ISLESSGREATER:
1938 case EXPR_BINARY_ISUNORDERED:
1939 return is_constant_expression(expression->binary.left)
1940 && is_constant_expression(expression->binary.right);
1942 case EXPR_BINARY_LOGICAL_AND: {
1943 expression_t const *const left = expression->binary.left;
1944 if (!is_constant_expression(left))
1946 if (fold_constant(left) == 0)
1948 return is_constant_expression(expression->binary.right);
1951 case EXPR_BINARY_LOGICAL_OR: {
1952 expression_t const *const left = expression->binary.left;
1953 if (!is_constant_expression(left))
1955 if (fold_constant(left) != 0)
1957 return is_constant_expression(expression->binary.right);
1960 case EXPR_COMPOUND_LITERAL:
1961 return is_constant_initializer(expression->compound_literal.initializer);
1963 case EXPR_CONDITIONAL: {
1964 expression_t *condition = expression->conditional.condition;
1965 if (!is_constant_expression(condition))
1968 long val = fold_constant(condition);
1970 return is_constant_expression(expression->conditional.true_expression);
1972 return is_constant_expression(expression->conditional.false_expression);
1981 panic("invalid expression found (is constant expression)");
1985 * Initialize the AST construction.
1989 obstack_init(&ast_obstack);
1997 obstack_free(&ast_obstack, NULL);
2001 * Set the output stream for the AST printer.
2003 * @param stream the output stream
2005 void ast_set_output(FILE *stream)
2008 type_set_output(stream);
2012 * Allocate an AST object of the given size.
2014 * @param size the size of the object to allocate
2016 * @return A new allocated object in the AST memeory space.
2018 void *(allocate_ast)(size_t size)
2020 return _allocate_ast(size);