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"
45 struct obstack ast_obstack;
50 /** If set, implicit casts are printed. */
51 bool print_implicit_casts = false;
53 /** If set parenthesis are printed to indicate operator precedence. */
54 bool print_parenthesis = false;
56 static void print_statement(const statement_t *statement);
57 static void print_expression_prec(const expression_t *expression, unsigned prec);
59 void change_indent(int delta)
65 void print_indent(void)
67 for (int i = 0; i < indent; ++i)
72 * Returns 1 if a given precedence level has right-to-left
73 * associativity, else 0.
75 * @param precedence the operator precedence
77 static int right_to_left(unsigned precedence)
81 case PREC_CONDITIONAL:
91 * Return the precedence of an expression given by its kind.
93 * @param kind the expression kind
95 static unsigned get_expression_precedence(expression_kind_t kind)
97 static const unsigned prec[] = {
98 [EXPR_UNKNOWN] = PREC_PRIMARY,
99 [EXPR_INVALID] = PREC_PRIMARY,
100 [EXPR_REFERENCE] = PREC_PRIMARY,
101 [EXPR_REFERENCE_ENUM_VALUE] = PREC_PRIMARY,
102 [EXPR_CHARACTER_CONSTANT] = PREC_PRIMARY,
103 [EXPR_WIDE_CHARACTER_CONSTANT] = PREC_PRIMARY,
104 [EXPR_CONST] = PREC_PRIMARY,
105 [EXPR_STRING_LITERAL] = PREC_PRIMARY,
106 [EXPR_WIDE_STRING_LITERAL] = PREC_PRIMARY,
107 [EXPR_COMPOUND_LITERAL] = PREC_UNARY,
108 [EXPR_CALL] = PREC_POSTFIX,
109 [EXPR_CONDITIONAL] = PREC_CONDITIONAL,
110 [EXPR_SELECT] = PREC_POSTFIX,
111 [EXPR_ARRAY_ACCESS] = PREC_POSTFIX,
112 [EXPR_SIZEOF] = PREC_UNARY,
113 [EXPR_CLASSIFY_TYPE] = PREC_UNARY,
114 [EXPR_ALIGNOF] = PREC_UNARY,
116 [EXPR_FUNCNAME] = PREC_PRIMARY,
117 [EXPR_BUILTIN_SYMBOL] = PREC_PRIMARY,
118 [EXPR_BUILTIN_CONSTANT_P] = PREC_PRIMARY,
119 [EXPR_BUILTIN_TYPES_COMPATIBLE_P] = PREC_PRIMARY,
120 [EXPR_BUILTIN_PREFETCH] = PREC_PRIMARY,
121 [EXPR_OFFSETOF] = PREC_PRIMARY,
122 [EXPR_VA_START] = PREC_PRIMARY,
123 [EXPR_VA_ARG] = PREC_PRIMARY,
124 [EXPR_STATEMENT] = PREC_PRIMARY,
125 [EXPR_LABEL_ADDRESS] = PREC_PRIMARY,
127 [EXPR_UNARY_NEGATE] = PREC_UNARY,
128 [EXPR_UNARY_PLUS] = PREC_UNARY,
129 [EXPR_UNARY_BITWISE_NEGATE] = PREC_UNARY,
130 [EXPR_UNARY_NOT] = PREC_UNARY,
131 [EXPR_UNARY_DEREFERENCE] = PREC_UNARY,
132 [EXPR_UNARY_TAKE_ADDRESS] = PREC_UNARY,
133 [EXPR_UNARY_POSTFIX_INCREMENT] = PREC_POSTFIX,
134 [EXPR_UNARY_POSTFIX_DECREMENT] = PREC_POSTFIX,
135 [EXPR_UNARY_PREFIX_INCREMENT] = PREC_UNARY,
136 [EXPR_UNARY_PREFIX_DECREMENT] = PREC_UNARY,
137 [EXPR_UNARY_CAST] = PREC_UNARY,
138 [EXPR_UNARY_CAST_IMPLICIT] = PREC_UNARY,
139 [EXPR_UNARY_ASSUME] = PREC_PRIMARY,
140 [EXPR_UNARY_DELETE] = PREC_UNARY,
141 [EXPR_UNARY_DELETE_ARRAY] = PREC_UNARY,
142 [EXPR_UNARY_THROW] = PREC_ASSIGNMENT,
144 [EXPR_BINARY_ADD] = PREC_ADDITIVE,
145 [EXPR_BINARY_SUB] = PREC_ADDITIVE,
146 [EXPR_BINARY_MUL] = PREC_MULTIPLICATIVE,
147 [EXPR_BINARY_DIV] = PREC_MULTIPLICATIVE,
148 [EXPR_BINARY_MOD] = PREC_MULTIPLICATIVE,
149 [EXPR_BINARY_EQUAL] = PREC_EQUALITY,
150 [EXPR_BINARY_NOTEQUAL] = PREC_EQUALITY,
151 [EXPR_BINARY_LESS] = PREC_RELATIONAL,
152 [EXPR_BINARY_LESSEQUAL] = PREC_RELATIONAL,
153 [EXPR_BINARY_GREATER] = PREC_RELATIONAL,
154 [EXPR_BINARY_GREATEREQUAL] = PREC_RELATIONAL,
155 [EXPR_BINARY_BITWISE_AND] = PREC_AND,
156 [EXPR_BINARY_BITWISE_OR] = PREC_OR,
157 [EXPR_BINARY_BITWISE_XOR] = PREC_XOR,
158 [EXPR_BINARY_LOGICAL_AND] = PREC_LOGICAL_AND,
159 [EXPR_BINARY_LOGICAL_OR] = PREC_LOGICAL_OR,
160 [EXPR_BINARY_SHIFTLEFT] = PREC_SHIFT,
161 [EXPR_BINARY_SHIFTRIGHT] = PREC_SHIFT,
162 [EXPR_BINARY_ASSIGN] = PREC_ASSIGNMENT,
163 [EXPR_BINARY_MUL_ASSIGN] = PREC_ASSIGNMENT,
164 [EXPR_BINARY_DIV_ASSIGN] = PREC_ASSIGNMENT,
165 [EXPR_BINARY_MOD_ASSIGN] = PREC_ASSIGNMENT,
166 [EXPR_BINARY_ADD_ASSIGN] = PREC_ASSIGNMENT,
167 [EXPR_BINARY_SUB_ASSIGN] = PREC_ASSIGNMENT,
168 [EXPR_BINARY_SHIFTLEFT_ASSIGN] = PREC_ASSIGNMENT,
169 [EXPR_BINARY_SHIFTRIGHT_ASSIGN] = PREC_ASSIGNMENT,
170 [EXPR_BINARY_BITWISE_AND_ASSIGN] = PREC_ASSIGNMENT,
171 [EXPR_BINARY_BITWISE_XOR_ASSIGN] = PREC_ASSIGNMENT,
172 [EXPR_BINARY_BITWISE_OR_ASSIGN] = PREC_ASSIGNMENT,
173 [EXPR_BINARY_COMMA] = PREC_EXPRESSION,
175 [EXPR_BINARY_ISGREATER] = PREC_PRIMARY,
176 [EXPR_BINARY_ISGREATEREQUAL] = PREC_PRIMARY,
177 [EXPR_BINARY_ISLESS] = PREC_PRIMARY,
178 [EXPR_BINARY_ISLESSEQUAL] = PREC_PRIMARY,
179 [EXPR_BINARY_ISLESSGREATER] = PREC_PRIMARY,
180 [EXPR_BINARY_ISUNORDERED] = PREC_PRIMARY
182 assert((size_t)kind < lengthof(prec));
183 unsigned res = prec[kind];
185 assert(res != PREC_BOTTOM);
190 * Print a constant expression.
192 * @param cnst the constant expression
194 static void print_const(const const_expression_t *cnst)
196 if (cnst->base.type == NULL)
199 const type_t *const type = skip_typeref(cnst->base.type);
201 if (is_type_atomic(type, ATOMIC_TYPE_BOOL)) {
202 fputs(cnst->v.int_value ? "true" : "false", out);
203 } else if (is_type_integer(type)) {
204 fprintf(out, "%lld", cnst->v.int_value);
205 } else if (is_type_float(type)) {
206 long double const val = cnst->v.float_value;
208 /* ARG, no way to print long double */
209 fprintf(out, "%.20g", (double)val);
211 fprintf(out, "%.20Lg", val);
213 if (isfinite(val) && truncl(val) == val)
216 panic("unknown constant");
220 switch (type->atomic.akind) {
221 case ATOMIC_TYPE_UINT: suffix = "U"; break;
222 case ATOMIC_TYPE_LONG: suffix = "L"; break;
223 case ATOMIC_TYPE_ULONG: suffix = "UL"; break;
224 case ATOMIC_TYPE_LONGLONG: suffix = "LL"; break;
225 case ATOMIC_TYPE_ULONGLONG: suffix = "ULL"; break;
226 case ATOMIC_TYPE_FLOAT: suffix = "F"; break;
227 case ATOMIC_TYPE_LONG_DOUBLE: suffix = "L"; break;
235 * Print a quoted string constant.
237 * @param string the string constant
238 * @param border the border char
239 * @param skip number of chars to skip at the end
241 static void print_quoted_string(const string_t *const string, char border, int skip)
244 const char *end = string->begin + string->size - skip;
245 for (const char *c = string->begin; c != end; ++c) {
246 unsigned char const tc = *c;
251 case '\\': fputs("\\\\", out); break;
252 case '\a': fputs("\\a", out); break;
253 case '\b': fputs("\\b", out); break;
254 case '\f': fputs("\\f", out); break;
255 case '\n': fputs("\\n", out); break;
256 case '\r': fputs("\\r", out); break;
257 case '\t': fputs("\\t", out); break;
258 case '\v': fputs("\\v", out); break;
259 case '\?': fputs("\\?", out); break;
261 if (c_mode & _GNUC) {
262 fputs("\\e", out); break;
266 if (tc < 0x80 && !isprint(tc)) {
267 fprintf(out, "\\%03o", (unsigned)tc);
278 * Prints a wide string literal expression.
280 * @param wstr the wide string literal expression
281 * @param border the border char
282 * @param skip number of chars to skip at the end
284 static void print_quoted_wide_string(const wide_string_t *const wstr,
285 char border, int skip)
289 const wchar_rep_t *end = wstr->begin + wstr->size - skip;
290 for (const wchar_rep_t *c = wstr->begin; c != end; ++c) {
292 case L'\"': fputs("\\\"", out); break;
293 case L'\\': fputs("\\\\", out); break;
294 case L'\a': fputs("\\a", out); break;
295 case L'\b': fputs("\\b", out); break;
296 case L'\f': fputs("\\f", out); break;
297 case L'\n': fputs("\\n", out); break;
298 case L'\r': fputs("\\r", out); break;
299 case L'\t': fputs("\\t", out); break;
300 case L'\v': fputs("\\v", out); break;
301 case L'\?': fputs("\\?", out); break;
303 if (c_mode & _GNUC) {
304 fputs("\\e", out); break;
308 const unsigned tc = *c;
313 fprintf(out, "\\%03o", tc);
315 } else if (tc < 0x800) {
316 fputc(0xC0 | (tc >> 6), out);
317 fputc(0x80 | (tc & 0x3F), out);
318 } else if (tc < 0x10000) {
319 fputc(0xE0 | ( tc >> 12), out);
320 fputc(0x80 | ((tc >> 6) & 0x3F), out);
321 fputc(0x80 | ( tc & 0x3F), out);
323 fputc(0xF0 | ( tc >> 18), out);
324 fputc(0x80 | ((tc >> 12) & 0x3F), out);
325 fputc(0x80 | ((tc >> 6) & 0x3F), out);
326 fputc(0x80 | ( tc & 0x3F), out);
335 * Print a constant character expression.
337 * @param cnst the constant character expression
339 static void print_character_constant(const const_expression_t *cnst)
341 print_quoted_string(&cnst->v.character, '\'', 0);
344 static void print_wide_character_constant(const const_expression_t *cnst)
346 print_quoted_wide_string(&cnst->v.wide_character, '\'', 0);
350 * Prints a string literal expression.
352 * @param string_literal the string literal expression
354 static void print_string_literal(
355 const string_literal_expression_t *string_literal)
357 print_quoted_string(&string_literal->value, '"', 1);
361 * Prints a predefined symbol.
363 static void print_funcname(const funcname_expression_t *funcname)
366 switch (funcname->kind) {
367 case FUNCNAME_FUNCTION: s = (c_mode & _C99) ? "__func__" : "__FUNCTION__"; break;
368 case FUNCNAME_PRETTY_FUNCTION: s = "__PRETTY_FUNCTION__"; break;
369 case FUNCNAME_FUNCSIG: s = "__FUNCSIG__"; break;
370 case FUNCNAME_FUNCDNAME: s = "__FUNCDNAME__"; break;
375 static void print_wide_string_literal(
376 const wide_string_literal_expression_t *const wstr)
378 print_quoted_wide_string(&wstr->value, '"', 1);
381 static void print_compound_literal(
382 const compound_literal_expression_t *expression)
385 print_type(expression->type);
387 print_initializer(expression->initializer);
390 static void print_assignment_expression(const expression_t *const expr)
392 print_expression_prec(expr, PREC_ASSIGNMENT);
396 * Prints a call expression.
398 * @param call the call expression
400 static void print_call_expression(const call_expression_t *call)
402 unsigned prec = get_expression_precedence(call->base.kind);
403 print_expression_prec(call->function, prec);
405 call_argument_t *argument = call->arguments;
407 while (argument != NULL) {
413 print_assignment_expression(argument->expression);
415 argument = argument->next;
421 * Prints a binary expression.
423 * @param binexpr the binary expression
425 static void print_binary_expression(const binary_expression_t *binexpr)
427 unsigned prec = get_expression_precedence(binexpr->base.kind);
428 int r2l = right_to_left(prec);
430 print_expression_prec(binexpr->left, prec + r2l);
432 switch (binexpr->base.kind) {
433 case EXPR_BINARY_COMMA: op = ", "; break;
434 case EXPR_BINARY_ASSIGN: op = " = "; break;
435 case EXPR_BINARY_ADD: op = " + "; break;
436 case EXPR_BINARY_SUB: op = " - "; break;
437 case EXPR_BINARY_MUL: op = " * "; break;
438 case EXPR_BINARY_MOD: op = " % "; break;
439 case EXPR_BINARY_DIV: op = " / "; break;
440 case EXPR_BINARY_BITWISE_OR: op = " | "; break;
441 case EXPR_BINARY_BITWISE_AND: op = " & "; break;
442 case EXPR_BINARY_BITWISE_XOR: op = " ^ "; break;
443 case EXPR_BINARY_LOGICAL_OR: op = " || "; break;
444 case EXPR_BINARY_LOGICAL_AND: op = " && "; break;
445 case EXPR_BINARY_NOTEQUAL: op = " != "; break;
446 case EXPR_BINARY_EQUAL: op = " == "; break;
447 case EXPR_BINARY_LESS: op = " < "; break;
448 case EXPR_BINARY_LESSEQUAL: op = " <= "; break;
449 case EXPR_BINARY_GREATER: op = " > "; break;
450 case EXPR_BINARY_GREATEREQUAL: op = " >= "; break;
451 case EXPR_BINARY_SHIFTLEFT: op = " << "; break;
452 case EXPR_BINARY_SHIFTRIGHT: op = " >> "; break;
454 case EXPR_BINARY_ADD_ASSIGN: op = " += "; break;
455 case EXPR_BINARY_SUB_ASSIGN: op = " -= "; break;
456 case EXPR_BINARY_MUL_ASSIGN: op = " *= "; break;
457 case EXPR_BINARY_MOD_ASSIGN: op = " %= "; break;
458 case EXPR_BINARY_DIV_ASSIGN: op = " /= "; break;
459 case EXPR_BINARY_BITWISE_OR_ASSIGN: op = " |= "; break;
460 case EXPR_BINARY_BITWISE_AND_ASSIGN: op = " &= "; break;
461 case EXPR_BINARY_BITWISE_XOR_ASSIGN: op = " ^= "; break;
462 case EXPR_BINARY_SHIFTLEFT_ASSIGN: op = " <<= "; break;
463 case EXPR_BINARY_SHIFTRIGHT_ASSIGN: op = " >>= "; break;
464 default: panic("invalid binexpression found");
467 print_expression_prec(binexpr->right, prec + 1 - r2l);
471 * Prints an unary expression.
473 * @param unexpr the unary expression
475 static void print_unary_expression(const unary_expression_t *unexpr)
477 unsigned prec = get_expression_precedence(unexpr->base.kind);
478 switch (unexpr->base.kind) {
479 case EXPR_UNARY_NEGATE: fputc('-', out); break;
480 case EXPR_UNARY_PLUS: fputc('+', out); break;
481 case EXPR_UNARY_NOT: fputc('!', out); break;
482 case EXPR_UNARY_BITWISE_NEGATE: fputc('~', out); break;
483 case EXPR_UNARY_PREFIX_INCREMENT: fputs("++", out); break;
484 case EXPR_UNARY_PREFIX_DECREMENT: fputs("--", out); break;
485 case EXPR_UNARY_DEREFERENCE: fputc('*', out); break;
486 case EXPR_UNARY_TAKE_ADDRESS: fputc('&', out); break;
487 case EXPR_UNARY_DELETE: fputs("delete ", out); break;
488 case EXPR_UNARY_DELETE_ARRAY: fputs("delete [] ", out); break;
490 case EXPR_UNARY_POSTFIX_INCREMENT:
491 print_expression_prec(unexpr->value, prec);
494 case EXPR_UNARY_POSTFIX_DECREMENT:
495 print_expression_prec(unexpr->value, prec);
498 case EXPR_UNARY_CAST_IMPLICIT:
499 case EXPR_UNARY_CAST:
501 print_type(unexpr->base.type);
504 case EXPR_UNARY_ASSUME:
505 fputs("__assume(", out);
506 print_assignment_expression(unexpr->value);
510 case EXPR_UNARY_THROW:
511 if (unexpr->value == NULL) {
515 fputs("throw ", out);
519 panic("invalid unary expression found");
521 print_expression_prec(unexpr->value, prec);
525 * Prints a reference expression.
527 * @param ref the reference expression
529 static void print_reference_expression(const reference_expression_t *ref)
531 fputs(ref->entity->base.symbol->string, out);
535 * Prints a label address expression.
537 * @param ref the reference expression
539 static void print_label_address_expression(const label_address_expression_t *le)
541 fprintf(out, "&&%s", le->label->base.symbol->string);
545 * Prints an array expression.
547 * @param expression the array expression
549 static void print_array_expression(const array_access_expression_t *expression)
551 unsigned prec = get_expression_precedence(expression->base.kind);
552 if (!expression->flipped) {
553 print_expression_prec(expression->array_ref, prec);
555 print_expression(expression->index);
558 print_expression_prec(expression->index, prec);
560 print_expression(expression->array_ref);
566 * Prints a typeproperty expression (sizeof or __alignof__).
568 * @param expression the type property expression
570 static void print_typeprop_expression(const typeprop_expression_t *expression)
572 if (expression->base.kind == EXPR_SIZEOF) {
573 fputs("sizeof", out);
575 assert(expression->base.kind == EXPR_ALIGNOF);
576 fputs("__alignof__", out);
578 if (expression->tp_expression != NULL) {
579 /* PREC_TOP: always print the '()' here, sizeof x is right but unusual */
580 print_expression_prec(expression->tp_expression, PREC_TOP);
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 types compatible expression.
613 * @param expression the builtin types compatible expression
615 static void print_builtin_types_compatible(
616 const builtin_types_compatible_expression_t *expression)
618 fputs("__builtin_types_compatible_p(", out);
619 print_type(expression->left);
621 print_type(expression->right);
626 * Prints a builtin prefetch expression.
628 * @param expression the builtin prefetch expression
630 static void print_builtin_prefetch(const builtin_prefetch_expression_t *expression)
632 fputs("__builtin_prefetch(", out);
633 print_assignment_expression(expression->adr);
634 if (expression->rw) {
636 print_assignment_expression(expression->rw);
638 if (expression->locality) {
640 print_assignment_expression(expression->locality);
646 * Prints a conditional expression.
648 * @param expression the conditional expression
650 static void print_conditional(const conditional_expression_t *expression)
652 print_expression_prec(expression->condition, PREC_LOGICAL_OR);
654 if (expression->true_expression != NULL) {
655 print_expression_prec(expression->true_expression, PREC_EXPRESSION);
660 precedence_t prec = c_mode & _CXX ? PREC_ASSIGNMENT : PREC_CONDITIONAL;
661 print_expression_prec(expression->false_expression, prec);
665 * Prints a va_start expression.
667 * @param expression the va_start expression
669 static void print_va_start(const va_start_expression_t *const expression)
671 fputs("__builtin_va_start(", out);
672 print_assignment_expression(expression->ap);
674 fputs(expression->parameter->base.base.symbol->string, out);
679 * Prints a va_arg expression.
681 * @param expression the va_arg expression
683 static void print_va_arg(const va_arg_expression_t *expression)
685 fputs("__builtin_va_arg(", out);
686 print_assignment_expression(expression->ap);
688 print_type(expression->base.type);
693 * Prints a select expression (. or ->).
695 * @param expression the select expression
697 static void print_select(const select_expression_t *expression)
699 unsigned prec = get_expression_precedence(expression->base.kind);
700 print_expression_prec(expression->compound, prec);
701 if (is_type_pointer(skip_typeref(expression->compound->base.type))) {
706 fputs(expression->compound_entry->base.symbol->string, out);
710 * Prints a type classify expression.
712 * @param expr the type classify expression
714 static void print_classify_type_expression(
715 const classify_type_expression_t *const expr)
717 fputs("__builtin_classify_type(", out);
718 print_assignment_expression(expr->type_expression);
723 * Prints a designator.
725 * @param designator the designator
727 static void print_designator(const designator_t *designator)
729 for ( ; designator != NULL; designator = designator->next) {
730 if (designator->symbol == NULL) {
732 print_expression(designator->array_index);
736 fputs(designator->symbol->string, out);
742 * Prints an offsetof expression.
744 * @param expression the offset expression
746 static void print_offsetof_expression(const offsetof_expression_t *expression)
748 fputs("__builtin_offsetof", out);
750 print_type(expression->type);
752 print_designator(expression->designator);
757 * Prints a statement expression.
759 * @param expression the statement expression
761 static void print_statement_expression(const statement_expression_t *expression)
764 print_statement(expression->statement);
769 * Prints an expression with parenthesis if needed.
771 * @param expression the expression to print
772 * @param top_prec the precedence of the user of this expression.
774 static void print_expression_prec(const expression_t *expression, unsigned top_prec)
776 if (expression->kind == EXPR_UNARY_CAST_IMPLICIT && !print_implicit_casts) {
777 expression = expression->unary.value;
781 expression->base.parenthesized ||
782 (print_parenthesis && top_prec != PREC_BOTTOM) ||
783 top_prec > get_expression_precedence(expression->base.kind);
787 switch (expression->kind) {
790 fputs("$invalid expression$", out);
792 case EXPR_CHARACTER_CONSTANT:
793 print_character_constant(&expression->conste);
795 case EXPR_WIDE_CHARACTER_CONSTANT:
796 print_wide_character_constant(&expression->conste);
799 print_const(&expression->conste);
802 print_funcname(&expression->funcname);
804 case EXPR_STRING_LITERAL:
805 print_string_literal(&expression->string);
807 case EXPR_WIDE_STRING_LITERAL:
808 print_wide_string_literal(&expression->wide_string);
810 case EXPR_COMPOUND_LITERAL:
811 print_compound_literal(&expression->compound_literal);
814 print_call_expression(&expression->call);
817 print_binary_expression(&expression->binary);
820 case EXPR_REFERENCE_ENUM_VALUE:
821 print_reference_expression(&expression->reference);
823 case EXPR_ARRAY_ACCESS:
824 print_array_expression(&expression->array_access);
826 case EXPR_LABEL_ADDRESS:
827 print_label_address_expression(&expression->label_address);
830 print_unary_expression(&expression->unary);
834 print_typeprop_expression(&expression->typeprop);
836 case EXPR_BUILTIN_SYMBOL:
837 print_builtin_symbol(&expression->builtin_symbol);
839 case EXPR_BUILTIN_CONSTANT_P:
840 print_builtin_constant(&expression->builtin_constant);
842 case EXPR_BUILTIN_TYPES_COMPATIBLE_P:
843 print_builtin_types_compatible(&expression->builtin_types_compatible);
845 case EXPR_BUILTIN_PREFETCH:
846 print_builtin_prefetch(&expression->builtin_prefetch);
848 case EXPR_CONDITIONAL:
849 print_conditional(&expression->conditional);
852 print_va_start(&expression->va_starte);
855 print_va_arg(&expression->va_arge);
858 print_select(&expression->select);
860 case EXPR_CLASSIFY_TYPE:
861 print_classify_type_expression(&expression->classify_type);
864 print_offsetof_expression(&expression->offsetofe);
867 print_statement_expression(&expression->statement);
872 fprintf(out, "some expression of type %d", (int)expression->kind);
880 * Print an compound statement.
882 * @param block the compound statement
884 static void print_compound_statement(const compound_statement_t *block)
889 statement_t *statement = block->statements;
890 while (statement != NULL) {
891 if (statement->base.kind == STATEMENT_CASE_LABEL)
893 if (statement->kind != STATEMENT_LABEL)
895 print_statement(statement);
897 statement = statement->base.next;
901 fputs(block->stmt_expr ? "}" : "}\n", out);
905 * Print a return statement.
907 * @param statement the return statement
909 static void print_return_statement(const return_statement_t *statement)
911 expression_t const *const val = statement->value;
913 fputs("return ", out);
914 print_expression(val);
917 fputs("return;\n", out);
922 * Print an expression statement.
924 * @param statement the expression statement
926 static void print_expression_statement(const expression_statement_t *statement)
928 print_expression(statement->expression);
933 * Print a goto statement.
935 * @param statement the goto statement
937 static void print_goto_statement(const goto_statement_t *statement)
940 if (statement->expression != NULL) {
942 print_expression(statement->expression);
944 fputs(statement->label->base.symbol->string, out);
950 * Print a label statement.
952 * @param statement the label statement
954 static void print_label_statement(const label_statement_t *statement)
956 fprintf(out, "%s:\n", statement->label->base.symbol->string);
958 print_statement(statement->statement);
962 * Print an if statement.
964 * @param statement the if statement
966 static void print_if_statement(const if_statement_t *statement)
969 print_expression(statement->condition);
971 print_statement(statement->true_statement);
973 if (statement->false_statement != NULL) {
976 print_statement(statement->false_statement);
981 * Print a switch statement.
983 * @param statement the switch statement
985 static void print_switch_statement(const switch_statement_t *statement)
987 fputs("switch (", out);
988 print_expression(statement->expression);
990 print_statement(statement->body);
994 * Print a case label (including the default label).
996 * @param statement the case label statement
998 static void print_case_label(const case_label_statement_t *statement)
1000 if (statement->expression == NULL) {
1001 fputs("default:\n", out);
1003 fputs("case ", out);
1004 print_expression(statement->expression);
1005 if (statement->end_range != NULL) {
1006 fputs(" ... ", out);
1007 print_expression(statement->end_range);
1012 if (statement->statement != NULL) {
1013 if (statement->statement->base.kind == STATEMENT_CASE_LABEL) {
1017 print_statement(statement->statement);
1021 static void print_typedef(const entity_t *entity)
1023 fputs("typedef ", out);
1024 print_type_ext(entity->typedefe.type, entity->base.symbol, NULL);
1029 * returns true if the entity is a compiler generated one and has no real
1030 * correspondenc in the source file
1032 static bool is_generated_entity(const entity_t *entity)
1034 if (entity->kind == ENTITY_TYPEDEF)
1035 return entity->typedefe.builtin;
1037 if (is_declaration(entity))
1038 return entity->declaration.implicit;
1044 * Print a declaration statement.
1046 * @param statement the statement
1048 static void print_declaration_statement(
1049 const declaration_statement_t *statement)
1052 entity_t *entity = statement->declarations_begin;
1053 if (entity == NULL) {
1054 fputs("/* empty declaration statement */\n", out);
1058 entity_t *const end = statement->declarations_end->base.next;
1059 for (; entity != end; entity = entity->base.next) {
1060 if (entity->kind == ENTITY_ENUM_VALUE)
1062 if (is_generated_entity(entity))
1071 print_entity(entity);
1077 * Print a while statement.
1079 * @param statement the statement
1081 static void print_while_statement(const while_statement_t *statement)
1083 fputs("while (", out);
1084 print_expression(statement->condition);
1086 print_statement(statement->body);
1090 * Print a do-while statement.
1092 * @param statement the statement
1094 static void print_do_while_statement(const do_while_statement_t *statement)
1097 print_statement(statement->body);
1099 fputs("while (", out);
1100 print_expression(statement->condition);
1105 * Print a for statement.
1107 * @param statement the statement
1109 static void print_for_statement(const for_statement_t *statement)
1111 fputs("for (", out);
1112 entity_t *entity = statement->scope.entities;
1113 while (entity != NULL && is_generated_entity(entity))
1114 entity = entity->base.next;
1116 if (entity != NULL) {
1117 assert(statement->initialisation == NULL);
1118 assert(is_declaration(entity));
1119 print_declaration(entity);
1120 if (entity->base.next != NULL) {
1121 panic("multiple declarations in for statement not supported yet");
1124 if (statement->initialisation) {
1125 print_expression(statement->initialisation);
1129 if (statement->condition != NULL) {
1131 print_expression(statement->condition);
1134 if (statement->step != NULL) {
1136 print_expression(statement->step);
1139 print_statement(statement->body);
1143 * Print assembler arguments.
1145 * @param arguments the arguments
1147 static void print_asm_arguments(asm_argument_t *arguments)
1149 asm_argument_t *argument = arguments;
1150 for (; argument != NULL; argument = argument->next) {
1151 if (argument != arguments)
1154 if (argument->symbol) {
1155 fprintf(out, "[%s] ", argument->symbol->string);
1157 print_quoted_string(&argument->constraints, '"', 1);
1159 print_expression(argument->expression);
1165 * Print assembler clobbers.
1167 * @param clobbers the clobbers
1169 static void print_asm_clobbers(asm_clobber_t *clobbers)
1171 asm_clobber_t *clobber = clobbers;
1172 for (; clobber != NULL; clobber = clobber->next) {
1173 if (clobber != clobbers)
1176 print_quoted_string(&clobber->clobber, '"', 1);
1181 * Print an assembler statement.
1183 * @param statement the statement
1185 static void print_asm_statement(const asm_statement_t *statement)
1188 if (statement->is_volatile) {
1189 fputs("volatile ", out);
1192 print_quoted_string(&statement->asm_text, '"', 1);
1193 if (statement->outputs == NULL &&
1194 statement->inputs == NULL &&
1195 statement->clobbers == NULL)
1196 goto end_of_print_asm_statement;
1199 print_asm_arguments(statement->outputs);
1200 if (statement->inputs == NULL && statement->clobbers == NULL)
1201 goto end_of_print_asm_statement;
1204 print_asm_arguments(statement->inputs);
1205 if (statement->clobbers == NULL)
1206 goto end_of_print_asm_statement;
1209 print_asm_clobbers(statement->clobbers);
1211 end_of_print_asm_statement:
1216 * Print a microsoft __try statement.
1218 * @param statement the statement
1220 static void print_ms_try_statement(const ms_try_statement_t *statement)
1222 fputs("__try ", out);
1223 print_statement(statement->try_statement);
1225 if (statement->except_expression != NULL) {
1226 fputs("__except(", out);
1227 print_expression(statement->except_expression);
1230 fputs("__finally ", out);
1232 print_statement(statement->final_statement);
1236 * Print a microsoft __leave statement.
1238 * @param statement the statement
1240 static void print_leave_statement(const leave_statement_t *statement)
1243 fputs("__leave;\n", out);
1247 * Print a statement.
1249 * @param statement the statement
1251 void print_statement(const statement_t *statement)
1253 switch (statement->kind) {
1254 case STATEMENT_EMPTY:
1257 case STATEMENT_COMPOUND:
1258 print_compound_statement(&statement->compound);
1260 case STATEMENT_RETURN:
1261 print_return_statement(&statement->returns);
1263 case STATEMENT_EXPRESSION:
1264 print_expression_statement(&statement->expression);
1266 case STATEMENT_LABEL:
1267 print_label_statement(&statement->label);
1269 case STATEMENT_GOTO:
1270 print_goto_statement(&statement->gotos);
1272 case STATEMENT_CONTINUE:
1273 fputs("continue;\n", out);
1275 case STATEMENT_BREAK:
1276 fputs("break;\n", out);
1279 print_if_statement(&statement->ifs);
1281 case STATEMENT_SWITCH:
1282 print_switch_statement(&statement->switchs);
1284 case STATEMENT_CASE_LABEL:
1285 print_case_label(&statement->case_label);
1287 case STATEMENT_DECLARATION:
1288 print_declaration_statement(&statement->declaration);
1290 case STATEMENT_WHILE:
1291 print_while_statement(&statement->whiles);
1293 case STATEMENT_DO_WHILE:
1294 print_do_while_statement(&statement->do_while);
1297 print_for_statement(&statement->fors);
1300 print_asm_statement(&statement->asms);
1302 case STATEMENT_MS_TRY:
1303 print_ms_try_statement(&statement->ms_try);
1305 case STATEMENT_LEAVE:
1306 print_leave_statement(&statement->leave);
1308 case STATEMENT_INVALID:
1309 fputs("$invalid statement$\n", out);
1315 * Print a storage class.
1317 * @param storage_class the storage class
1319 static void print_storage_class(storage_class_tag_t storage_class)
1321 switch (storage_class) {
1322 case STORAGE_CLASS_NONE: return;
1323 case STORAGE_CLASS_TYPEDEF: fputs("typedef ", out); return;
1324 case STORAGE_CLASS_EXTERN: fputs("extern ", out); return;
1325 case STORAGE_CLASS_STATIC: fputs("static ", out); return;
1326 case STORAGE_CLASS_AUTO: fputs("auto ", out); return;
1327 case STORAGE_CLASS_REGISTER: fputs("register ", out); return;
1329 panic("invalid storage class");
1333 * Print an initializer.
1335 * @param initializer the initializer
1337 void print_initializer(const initializer_t *initializer)
1339 if (initializer == NULL) {
1344 switch (initializer->kind) {
1345 case INITIALIZER_VALUE: {
1346 const initializer_value_t *value = &initializer->value;
1347 print_assignment_expression(value->value);
1350 case INITIALIZER_LIST: {
1351 assert(initializer->kind == INITIALIZER_LIST);
1353 const initializer_list_t *list = &initializer->list;
1355 for (size_t i = 0 ; i < list->len; ++i) {
1356 const initializer_t *sub_init = list->initializers[i];
1357 print_initializer(list->initializers[i]);
1358 if (i < list->len-1) {
1359 if (sub_init == NULL || sub_init->kind != INITIALIZER_DESIGNATOR)
1366 case INITIALIZER_STRING:
1367 print_quoted_string(&initializer->string.string, '"', 1);
1369 case INITIALIZER_WIDE_STRING:
1370 print_quoted_wide_string(&initializer->wide_string.string, '"', 1);
1372 case INITIALIZER_DESIGNATOR:
1373 print_designator(initializer->designator.designator);
1378 panic("invalid initializer kind found");
1382 * Print microsoft extended declaration modifiers.
1384 static void print_ms_modifiers(const declaration_t *declaration)
1386 if ((c_mode & _MS) == 0)
1389 decl_modifiers_t modifiers = declaration->modifiers;
1391 bool ds_shown = false;
1392 const char *next = "(";
1394 if (declaration->base.kind == ENTITY_VARIABLE) {
1395 variable_t *variable = (variable_t*)declaration;
1396 if (variable->alignment != 0
1397 || variable->get_property_sym != NULL
1398 || variable->put_property_sym != NULL) {
1400 fputs("__declspec", out);
1404 if (variable->alignment != 0) {
1405 fputs(next, out); next = ", "; fprintf(out, "align(%u)", variable->alignment);
1407 if (variable->get_property_sym != NULL
1408 || variable->put_property_sym != NULL) {
1410 fputs(next, out); next = ", "; fputs("property(", out);
1411 if (variable->get_property_sym != NULL) {
1412 fprintf(out, "get=%s", variable->get_property_sym->string);
1415 if (variable->put_property_sym != NULL)
1416 fprintf(out, "%sput=%s", comma, variable->put_property_sym->string);
1422 /* DM_FORCEINLINE handled outside. */
1423 if ((modifiers & ~DM_FORCEINLINE) != 0) {
1425 fputs("__declspec", out);
1428 if (modifiers & DM_DLLIMPORT) {
1429 fputs(next, out); next = ", "; fputs("dllimport", out);
1431 if (modifiers & DM_DLLEXPORT) {
1432 fputs(next, out); next = ", "; fputs("dllexport", out);
1434 if (modifiers & DM_THREAD) {
1435 fputs(next, out); next = ", "; fputs("thread", out);
1437 if (modifiers & DM_NAKED) {
1438 fputs(next, out); next = ", "; fputs("naked", out);
1440 if (modifiers & DM_THREAD) {
1441 fputs(next, out); next = ", "; fputs("thread", out);
1443 if (modifiers & DM_SELECTANY) {
1444 fputs(next, out); next = ", "; fputs("selectany", out);
1446 if (modifiers & DM_NOTHROW) {
1447 fputs(next, out); next = ", "; fputs("nothrow", out);
1449 if (modifiers & DM_NORETURN) {
1450 fputs(next, out); next = ", "; fputs("noreturn", out);
1452 if (modifiers & DM_NOINLINE) {
1453 fputs(next, out); next = ", "; fputs("noinline", out);
1455 if (modifiers & DM_DEPRECATED) {
1456 fputs(next, out); next = ", "; fputs("deprecated", out);
1457 if (declaration->deprecated_string != NULL)
1458 fprintf(out, "(\"%s\")",
1459 declaration->deprecated_string);
1461 if (modifiers & DM_RESTRICT) {
1462 fputs(next, out); next = ", "; fputs("restrict", out);
1464 if (modifiers & DM_NOALIAS) {
1465 fputs(next, out); next = ", "; fputs("noalias", out);
1473 static void print_scope(const scope_t *scope)
1475 const entity_t *entity = scope->entities;
1476 for ( ; entity != NULL; entity = entity->base.next) {
1478 print_entity(entity);
1483 static void print_namespace(const namespace_t *namespace)
1485 fputs("namespace ", out);
1486 if (namespace->base.symbol != NULL) {
1487 fputs(namespace->base.symbol->string, out);
1494 print_scope(&namespace->members);
1502 * Print a variable or function declaration
1504 void print_declaration(const entity_t *entity)
1506 assert(is_declaration(entity));
1507 const declaration_t *declaration = &entity->declaration;
1509 print_storage_class((storage_class_tag_t)declaration->declared_storage_class);
1510 if (entity->kind == ENTITY_FUNCTION) {
1511 function_t *function = (function_t*)declaration;
1512 if (function->is_inline) {
1513 if (declaration->modifiers & DM_FORCEINLINE) {
1514 fputs("__forceinline ", out);
1515 } else if (declaration->modifiers & DM_MICROSOFT_INLINE) {
1516 fputs("__inline ", out);
1518 fputs("inline ", out);
1522 print_ms_modifiers(declaration);
1523 switch (entity->kind) {
1524 case ENTITY_FUNCTION:
1525 print_type_ext(entity->declaration.type, entity->base.symbol,
1526 &entity->function.parameters);
1528 if (entity->function.statement != NULL) {
1531 print_statement(entity->function.statement);
1536 case ENTITY_VARIABLE:
1537 if (entity->variable.thread_local)
1538 fputs("__thread ", out);
1539 print_type_ext(declaration->type, declaration->base.symbol, NULL);
1540 if (entity->variable.initializer != NULL) {
1542 print_initializer(entity->variable.initializer);
1547 print_type_ext(declaration->type, declaration->base.symbol, NULL);
1554 * Prints an expression.
1556 * @param expression the expression
1558 void print_expression(const expression_t *expression)
1560 print_expression_prec(expression, PREC_BOTTOM);
1564 * Print a declaration.
1566 * @param declaration the declaration
1568 void print_entity(const entity_t *entity)
1570 if (entity->base.namespc != NAMESPACE_NORMAL && entity->base.symbol == NULL)
1573 switch ((entity_kind_tag_t)entity->kind) {
1574 case ENTITY_VARIABLE:
1575 case ENTITY_PARAMETER:
1576 case ENTITY_COMPOUND_MEMBER:
1577 case ENTITY_FUNCTION:
1578 print_declaration(entity);
1580 case ENTITY_TYPEDEF:
1581 print_typedef(entity);
1584 fputs("struct ", out);
1585 fputs(entity->base.symbol->string, out);
1586 if (entity->structe.complete) {
1588 print_compound_definition(&entity->structe);
1593 fputs("union ", out);
1594 fputs(entity->base.symbol->string, out);
1595 if (entity->unione.complete) {
1597 print_compound_definition(&entity->unione);
1602 fputs("enum ", out);
1603 fputs(entity->base.symbol->string, out);
1605 print_enum_definition(&entity->enume);
1608 case ENTITY_NAMESPACE:
1609 print_namespace(&entity->namespacee);
1611 case ENTITY_LOCAL_LABEL:
1612 fprintf(out, "__label__ %s;", entity->base.symbol->string);
1615 case ENTITY_ENUM_VALUE:
1616 panic("print_entity used on unexpected entity type");
1617 case ENTITY_INVALID:
1620 panic("Invalid entity type encountered");
1624 * Print the AST of a translation unit.
1626 * @param unit the translation unit
1628 void print_ast(const translation_unit_t *unit)
1632 entity_t *entity = unit->scope.entities;
1633 for ( ; entity != NULL; entity = entity->base.next) {
1634 if (entity->kind == ENTITY_ENUM_VALUE)
1636 if (entity->base.namespc != NAMESPACE_NORMAL
1637 && entity->base.symbol == NULL)
1639 if (is_generated_entity(entity))
1643 print_entity(entity);
1648 bool is_constant_initializer(const initializer_t *initializer)
1650 switch (initializer->kind) {
1651 case INITIALIZER_STRING:
1652 case INITIALIZER_WIDE_STRING:
1653 case INITIALIZER_DESIGNATOR:
1656 case INITIALIZER_VALUE:
1657 return is_constant_expression(initializer->value.value);
1659 case INITIALIZER_LIST:
1660 for (size_t i = 0; i < initializer->list.len; ++i) {
1661 initializer_t *sub_initializer = initializer->list.initializers[i];
1662 if (!is_constant_initializer(sub_initializer))
1667 panic("invalid initializer kind found");
1670 static bool is_object_with_linker_constant_address(const expression_t *expression)
1672 switch (expression->kind) {
1673 case EXPR_UNARY_DEREFERENCE:
1674 return is_address_constant(expression->unary.value);
1677 type_t *base_type = skip_typeref(expression->select.compound->base.type);
1678 if (is_type_pointer(base_type)) {
1680 return is_address_constant(expression->select.compound);
1682 return is_object_with_linker_constant_address(expression->select.compound);
1686 case EXPR_ARRAY_ACCESS:
1687 return is_constant_expression(expression->array_access.index)
1688 && is_address_constant(expression->array_access.array_ref);
1690 case EXPR_REFERENCE: {
1691 entity_t *entity = expression->reference.entity;
1692 if (is_declaration(entity)) {
1693 switch ((storage_class_tag_t)entity->declaration.storage_class) {
1694 case STORAGE_CLASS_NONE:
1695 case STORAGE_CLASS_EXTERN:
1696 case STORAGE_CLASS_STATIC:
1698 entity->kind != ENTITY_VARIABLE ||
1699 !entity->variable.thread_local;
1701 case STORAGE_CLASS_REGISTER:
1702 case STORAGE_CLASS_TYPEDEF:
1703 case STORAGE_CLASS_AUTO:
1715 bool is_address_constant(const expression_t *expression)
1717 switch (expression->kind) {
1718 case EXPR_UNARY_TAKE_ADDRESS:
1719 return is_object_with_linker_constant_address(expression->unary.value);
1721 case EXPR_UNARY_DEREFERENCE: {
1723 = revert_automatic_type_conversion(expression->unary.value);
1724 /* dereferencing a function is a NOP */
1725 if (is_type_function(real_type)) {
1726 return is_address_constant(expression->unary.value);
1731 case EXPR_UNARY_CAST: {
1732 type_t *dest = skip_typeref(expression->base.type);
1733 if (!is_type_pointer(dest) && (
1734 dest->kind != TYPE_ATOMIC ||
1735 !(get_atomic_type_flags(dest->atomic.akind) & ATOMIC_TYPE_FLAG_INTEGER) ||
1736 get_atomic_type_size(dest->atomic.akind) < get_atomic_type_size(get_intptr_kind())
1740 return (is_constant_expression(expression->unary.value)
1741 || is_address_constant(expression->unary.value));
1744 case EXPR_BINARY_ADD:
1745 case EXPR_BINARY_SUB: {
1746 expression_t *left = expression->binary.left;
1747 expression_t *right = expression->binary.right;
1749 if (is_type_pointer(skip_typeref(left->base.type))) {
1750 return is_address_constant(left) && is_constant_expression(right);
1751 } else if (is_type_pointer(skip_typeref(right->base.type))) {
1752 return is_constant_expression(left) && is_address_constant(right);
1758 case EXPR_REFERENCE: {
1759 entity_t *entity = expression->reference.entity;
1760 if (!is_declaration(entity))
1763 type_t *type = skip_typeref(entity->declaration.type);
1764 if (is_type_function(type))
1766 if (is_type_array(type)) {
1767 return is_object_with_linker_constant_address(expression);
1769 /* Prevent stray errors */
1770 if (!is_type_valid(type))
1775 case EXPR_ARRAY_ACCESS: {
1776 type_t *const type =
1777 skip_typeref(revert_automatic_type_conversion(expression));
1779 is_type_array(type) &&
1780 is_constant_expression(expression->array_access.index) &&
1781 is_address_constant(expression->array_access.array_ref);
1789 static bool is_builtin_const_call(const expression_t *expression)
1791 expression_t *function = expression->call.function;
1792 if (function->kind != EXPR_BUILTIN_SYMBOL) {
1796 symbol_t *symbol = function->builtin_symbol.symbol;
1798 switch (symbol->ID) {
1799 case T___builtin_huge_val:
1800 case T___builtin_inf:
1801 case T___builtin_inff:
1802 case T___builtin_infl:
1803 case T___builtin_nan:
1804 case T___builtin_nanf:
1805 case T___builtin_nanl:
1812 static bool is_constant_pointer(const expression_t *expression)
1814 if (is_constant_expression(expression))
1817 switch (expression->kind) {
1818 case EXPR_UNARY_CAST:
1819 return is_constant_pointer(expression->unary.value);
1825 static bool is_object_with_constant_address(const expression_t *expression)
1827 switch (expression->kind) {
1829 expression_t *compound = expression->select.compound;
1830 type_t *compound_type = compound->base.type;
1831 compound_type = skip_typeref(compound_type);
1832 if (is_type_pointer(compound_type)) {
1833 return is_constant_pointer(compound);
1835 return is_object_with_constant_address(compound);
1839 case EXPR_ARRAY_ACCESS: {
1840 array_access_expression_t const* const array_access =
1841 &expression->array_access;
1843 is_constant_expression(array_access->index) && (
1844 is_object_with_constant_address(array_access->array_ref) ||
1845 is_constant_pointer(array_access->array_ref)
1849 case EXPR_UNARY_DEREFERENCE:
1850 return is_constant_pointer(expression->unary.value);
1856 bool is_constant_expression(const expression_t *expression)
1858 switch (expression->kind) {
1861 case EXPR_CHARACTER_CONSTANT:
1862 case EXPR_WIDE_CHARACTER_CONSTANT:
1863 case EXPR_STRING_LITERAL:
1864 case EXPR_WIDE_STRING_LITERAL:
1865 case EXPR_CLASSIFY_TYPE:
1869 case EXPR_BUILTIN_CONSTANT_P:
1870 case EXPR_BUILTIN_TYPES_COMPATIBLE_P:
1871 case EXPR_LABEL_ADDRESS:
1872 case EXPR_REFERENCE_ENUM_VALUE:
1876 type_t *type = expression->typeprop.type;
1878 type = expression->typeprop.tp_expression->base.type;
1880 type = skip_typeref(type);
1881 if (is_type_array(type) && type->array.is_vla)
1886 case EXPR_BUILTIN_SYMBOL:
1887 case EXPR_BUILTIN_PREFETCH:
1891 case EXPR_STATEMENT:
1892 case EXPR_REFERENCE:
1893 case EXPR_UNARY_POSTFIX_INCREMENT:
1894 case EXPR_UNARY_POSTFIX_DECREMENT:
1895 case EXPR_UNARY_PREFIX_INCREMENT:
1896 case EXPR_UNARY_PREFIX_DECREMENT:
1897 case EXPR_UNARY_ASSUME: /* has VOID type */
1898 case EXPR_UNARY_DEREFERENCE:
1899 case EXPR_UNARY_DELETE:
1900 case EXPR_UNARY_DELETE_ARRAY:
1901 case EXPR_UNARY_THROW:
1902 case EXPR_BINARY_ASSIGN:
1903 case EXPR_BINARY_MUL_ASSIGN:
1904 case EXPR_BINARY_DIV_ASSIGN:
1905 case EXPR_BINARY_MOD_ASSIGN:
1906 case EXPR_BINARY_ADD_ASSIGN:
1907 case EXPR_BINARY_SUB_ASSIGN:
1908 case EXPR_BINARY_SHIFTLEFT_ASSIGN:
1909 case EXPR_BINARY_SHIFTRIGHT_ASSIGN:
1910 case EXPR_BINARY_BITWISE_AND_ASSIGN:
1911 case EXPR_BINARY_BITWISE_XOR_ASSIGN:
1912 case EXPR_BINARY_BITWISE_OR_ASSIGN:
1913 case EXPR_BINARY_COMMA:
1914 case EXPR_ARRAY_ACCESS:
1917 case EXPR_UNARY_TAKE_ADDRESS:
1918 return is_object_with_constant_address(expression->unary.value);
1921 return is_builtin_const_call(expression);
1923 case EXPR_UNARY_NEGATE:
1924 case EXPR_UNARY_PLUS:
1925 case EXPR_UNARY_BITWISE_NEGATE:
1926 case EXPR_UNARY_NOT:
1927 return is_constant_expression(expression->unary.value);
1929 case EXPR_UNARY_CAST:
1930 case EXPR_UNARY_CAST_IMPLICIT:
1931 return is_type_arithmetic(skip_typeref(expression->base.type))
1932 && is_constant_expression(expression->unary.value);
1934 case EXPR_BINARY_ADD:
1935 case EXPR_BINARY_SUB:
1936 case EXPR_BINARY_MUL:
1937 case EXPR_BINARY_DIV:
1938 case EXPR_BINARY_MOD:
1939 case EXPR_BINARY_EQUAL:
1940 case EXPR_BINARY_NOTEQUAL:
1941 case EXPR_BINARY_LESS:
1942 case EXPR_BINARY_LESSEQUAL:
1943 case EXPR_BINARY_GREATER:
1944 case EXPR_BINARY_GREATEREQUAL:
1945 case EXPR_BINARY_BITWISE_AND:
1946 case EXPR_BINARY_BITWISE_OR:
1947 case EXPR_BINARY_BITWISE_XOR:
1948 case EXPR_BINARY_SHIFTLEFT:
1949 case EXPR_BINARY_SHIFTRIGHT:
1950 case EXPR_BINARY_ISGREATER:
1951 case EXPR_BINARY_ISGREATEREQUAL:
1952 case EXPR_BINARY_ISLESS:
1953 case EXPR_BINARY_ISLESSEQUAL:
1954 case EXPR_BINARY_ISLESSGREATER:
1955 case EXPR_BINARY_ISUNORDERED:
1956 return is_constant_expression(expression->binary.left)
1957 && is_constant_expression(expression->binary.right);
1959 case EXPR_BINARY_LOGICAL_AND: {
1960 expression_t const *const left = expression->binary.left;
1961 if (!is_constant_expression(left))
1963 if (fold_constant(left) == 0)
1965 return is_constant_expression(expression->binary.right);
1968 case EXPR_BINARY_LOGICAL_OR: {
1969 expression_t const *const left = expression->binary.left;
1970 if (!is_constant_expression(left))
1972 if (fold_constant(left) != 0)
1974 return is_constant_expression(expression->binary.right);
1977 case EXPR_COMPOUND_LITERAL:
1978 return is_constant_initializer(expression->compound_literal.initializer);
1980 case EXPR_CONDITIONAL: {
1981 expression_t *condition = expression->conditional.condition;
1982 if (!is_constant_expression(condition))
1985 long val = fold_constant(condition);
1987 expression_t const *const t = expression->conditional.true_expression;
1988 return t == NULL || is_constant_expression(t);
1990 return is_constant_expression(expression->conditional.false_expression);
2000 panic("invalid expression found (is constant expression)");
2004 * Initialize the AST construction.
2008 obstack_init(&ast_obstack);
2016 obstack_free(&ast_obstack, NULL);
2020 * Set the output stream for the AST printer.
2022 * @param stream the output stream
2024 void ast_set_output(FILE *stream)
2027 type_set_output(stream);
2031 * Allocate an AST object of the given size.
2033 * @param size the size of the object to allocate
2035 * @return A new allocated object in the AST memeory space.
2037 void *(allocate_ast)(size_t size)
2039 return _allocate_ast(size);