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) {
248 case '\\': fputs("\\\\", out); break;
249 case '\a': fputs("\\a", out); break;
250 case '\b': fputs("\\b", out); break;
251 case '\f': fputs("\\f", out); break;
252 case '\n': fputs("\\n", out); break;
253 case '\r': fputs("\\r", out); break;
254 case '\t': fputs("\\t", out); break;
255 case '\v': fputs("\\v", out); break;
256 case '\?': fputs("\\?", out); break;
258 if (c_mode & _GNUC) {
259 fputs("\\e", out); break;
264 fprintf(out, "\\%03o", (unsigned)(unsigned char)*c);
275 * Prints a wide string literal expression.
277 * @param wstr the wide string literal expression
278 * @param border the border char
279 * @param skip number of chars to skip at the end
281 static void print_quoted_wide_string(const wide_string_t *const wstr,
282 char border, int skip)
286 const wchar_rep_t *end = wstr->begin + wstr->size - skip;
287 for (const wchar_rep_t *c = wstr->begin; c != end; ++c) {
289 case L'\"': fputs("\\\"", out); break;
290 case L'\\': fputs("\\\\", out); break;
291 case L'\a': fputs("\\a", out); break;
292 case L'\b': fputs("\\b", out); break;
293 case L'\f': fputs("\\f", out); break;
294 case L'\n': fputs("\\n", out); break;
295 case L'\r': fputs("\\r", out); break;
296 case L'\t': fputs("\\t", out); break;
297 case L'\v': fputs("\\v", out); break;
298 case L'\?': fputs("\\?", out); break;
300 if (c_mode & _GNUC) {
301 fputs("\\e", out); break;
305 const unsigned tc = *c;
310 fprintf(out, "\\%03o", tc);
312 } else if (tc < 0x800) {
313 fputc(0xC0 | (tc >> 6), out);
314 fputc(0x80 | (tc & 0x3F), out);
315 } else if (tc < 0x10000) {
316 fputc(0xE0 | ( tc >> 12), out);
317 fputc(0x80 | ((tc >> 6) & 0x3F), out);
318 fputc(0x80 | ( tc & 0x3F), out);
320 fputc(0xF0 | ( tc >> 18), out);
321 fputc(0x80 | ((tc >> 12) & 0x3F), out);
322 fputc(0x80 | ((tc >> 6) & 0x3F), out);
323 fputc(0x80 | ( tc & 0x3F), out);
332 * Print a constant character expression.
334 * @param cnst the constant character expression
336 static void print_character_constant(const const_expression_t *cnst)
338 print_quoted_string(&cnst->v.character, '\'', 0);
341 static void print_wide_character_constant(const const_expression_t *cnst)
343 print_quoted_wide_string(&cnst->v.wide_character, '\'', 0);
347 * Prints a string literal expression.
349 * @param string_literal the string literal expression
351 static void print_string_literal(
352 const string_literal_expression_t *string_literal)
354 print_quoted_string(&string_literal->value, '"', 1);
358 * Prints a predefined symbol.
360 static void print_funcname(const funcname_expression_t *funcname)
363 switch (funcname->kind) {
364 case FUNCNAME_FUNCTION: s = (c_mode & _C99) ? "__func__" : "__FUNCTION__"; break;
365 case FUNCNAME_PRETTY_FUNCTION: s = "__PRETTY_FUNCTION__"; break;
366 case FUNCNAME_FUNCSIG: s = "__FUNCSIG__"; break;
367 case FUNCNAME_FUNCDNAME: s = "__FUNCDNAME__"; break;
372 static void print_wide_string_literal(
373 const wide_string_literal_expression_t *const wstr)
375 print_quoted_wide_string(&wstr->value, '"', 1);
378 static void print_compound_literal(
379 const compound_literal_expression_t *expression)
382 print_type(expression->type);
384 print_initializer(expression->initializer);
387 static void print_assignment_expression(const expression_t *const expr)
389 print_expression_prec(expr, PREC_ASSIGNMENT);
393 * Prints a call expression.
395 * @param call the call expression
397 static void print_call_expression(const call_expression_t *call)
399 unsigned prec = get_expression_precedence(call->base.kind);
400 print_expression_prec(call->function, prec);
402 call_argument_t *argument = call->arguments;
404 while (argument != NULL) {
410 print_assignment_expression(argument->expression);
412 argument = argument->next;
418 * Prints a binary expression.
420 * @param binexpr the binary expression
422 static void print_binary_expression(const binary_expression_t *binexpr)
424 unsigned prec = get_expression_precedence(binexpr->base.kind);
425 int r2l = right_to_left(prec);
427 print_expression_prec(binexpr->left, prec + r2l);
429 switch (binexpr->base.kind) {
430 case EXPR_BINARY_COMMA: op = ", "; break;
431 case EXPR_BINARY_ASSIGN: op = " = "; break;
432 case EXPR_BINARY_ADD: op = " + "; break;
433 case EXPR_BINARY_SUB: op = " - "; break;
434 case EXPR_BINARY_MUL: op = " * "; break;
435 case EXPR_BINARY_MOD: op = " % "; break;
436 case EXPR_BINARY_DIV: op = " / "; break;
437 case EXPR_BINARY_BITWISE_OR: op = " | "; break;
438 case EXPR_BINARY_BITWISE_AND: op = " & "; break;
439 case EXPR_BINARY_BITWISE_XOR: op = " ^ "; break;
440 case EXPR_BINARY_LOGICAL_OR: op = " || "; break;
441 case EXPR_BINARY_LOGICAL_AND: op = " && "; break;
442 case EXPR_BINARY_NOTEQUAL: op = " != "; break;
443 case EXPR_BINARY_EQUAL: op = " == "; break;
444 case EXPR_BINARY_LESS: op = " < "; break;
445 case EXPR_BINARY_LESSEQUAL: op = " <= "; break;
446 case EXPR_BINARY_GREATER: op = " > "; break;
447 case EXPR_BINARY_GREATEREQUAL: op = " >= "; break;
448 case EXPR_BINARY_SHIFTLEFT: op = " << "; break;
449 case EXPR_BINARY_SHIFTRIGHT: op = " >> "; break;
451 case EXPR_BINARY_ADD_ASSIGN: op = " += "; break;
452 case EXPR_BINARY_SUB_ASSIGN: op = " -= "; break;
453 case EXPR_BINARY_MUL_ASSIGN: op = " *= "; break;
454 case EXPR_BINARY_MOD_ASSIGN: op = " %= "; break;
455 case EXPR_BINARY_DIV_ASSIGN: op = " /= "; break;
456 case EXPR_BINARY_BITWISE_OR_ASSIGN: op = " |= "; break;
457 case EXPR_BINARY_BITWISE_AND_ASSIGN: op = " &= "; break;
458 case EXPR_BINARY_BITWISE_XOR_ASSIGN: op = " ^= "; break;
459 case EXPR_BINARY_SHIFTLEFT_ASSIGN: op = " <<= "; break;
460 case EXPR_BINARY_SHIFTRIGHT_ASSIGN: op = " >>= "; break;
461 default: panic("invalid binexpression found");
464 print_expression_prec(binexpr->right, prec + 1 - r2l);
468 * Prints an unary expression.
470 * @param unexpr the unary expression
472 static void print_unary_expression(const unary_expression_t *unexpr)
474 unsigned prec = get_expression_precedence(unexpr->base.kind);
475 switch (unexpr->base.kind) {
476 case EXPR_UNARY_NEGATE: fputc('-', out); break;
477 case EXPR_UNARY_PLUS: fputc('+', out); break;
478 case EXPR_UNARY_NOT: fputc('!', out); break;
479 case EXPR_UNARY_BITWISE_NEGATE: fputc('~', out); break;
480 case EXPR_UNARY_PREFIX_INCREMENT: fputs("++", out); break;
481 case EXPR_UNARY_PREFIX_DECREMENT: fputs("--", out); break;
482 case EXPR_UNARY_DEREFERENCE: fputc('*', out); break;
483 case EXPR_UNARY_TAKE_ADDRESS: fputc('&', out); break;
484 case EXPR_UNARY_DELETE: fputs("delete ", out); break;
485 case EXPR_UNARY_DELETE_ARRAY: fputs("delete [] ", out); break;
487 case EXPR_UNARY_POSTFIX_INCREMENT:
488 print_expression_prec(unexpr->value, prec);
491 case EXPR_UNARY_POSTFIX_DECREMENT:
492 print_expression_prec(unexpr->value, prec);
495 case EXPR_UNARY_CAST_IMPLICIT:
496 case EXPR_UNARY_CAST:
498 print_type(unexpr->base.type);
501 case EXPR_UNARY_ASSUME:
502 fputs("__assume(", out);
503 print_assignment_expression(unexpr->value);
507 case EXPR_UNARY_THROW:
508 if (unexpr->value == NULL) {
512 fputs("throw ", out);
516 panic("invalid unary expression found");
518 print_expression_prec(unexpr->value, prec);
522 * Prints a reference expression.
524 * @param ref the reference expression
526 static void print_reference_expression(const reference_expression_t *ref)
528 fputs(ref->entity->base.symbol->string, out);
532 * Prints a label address expression.
534 * @param ref the reference expression
536 static void print_label_address_expression(const label_address_expression_t *le)
538 fprintf(out, "&&%s", le->label->base.symbol->string);
542 * Prints an array expression.
544 * @param expression the array expression
546 static void print_array_expression(const array_access_expression_t *expression)
548 unsigned prec = get_expression_precedence(expression->base.kind);
549 if (!expression->flipped) {
550 print_expression_prec(expression->array_ref, prec);
552 print_expression(expression->index);
555 print_expression_prec(expression->index, prec);
557 print_expression(expression->array_ref);
563 * Prints a typeproperty expression (sizeof or __alignof__).
565 * @param expression the type property expression
567 static void print_typeprop_expression(const typeprop_expression_t *expression)
569 if (expression->base.kind == EXPR_SIZEOF) {
570 fputs("sizeof", out);
572 assert(expression->base.kind == EXPR_ALIGNOF);
573 fputs("__alignof__", out);
575 if (expression->tp_expression != NULL) {
576 /* always print the '()' here, sizeof x is right but unusual */
578 print_expression(expression->tp_expression);
582 print_type(expression->type);
588 * Prints an builtin symbol.
590 * @param expression the builtin symbol expression
592 static void print_builtin_symbol(const builtin_symbol_expression_t *expression)
594 fputs(expression->symbol->string, out);
598 * Prints a builtin constant expression.
600 * @param expression the builtin constant expression
602 static void print_builtin_constant(const builtin_constant_expression_t *expression)
604 fputs("__builtin_constant_p(", out);
605 print_assignment_expression(expression->value);
610 * Prints a builtin prefetch expression.
612 * @param expression the builtin prefetch expression
614 static void print_builtin_prefetch(const builtin_prefetch_expression_t *expression)
616 fputs("__builtin_prefetch(", out);
617 print_assignment_expression(expression->adr);
618 if (expression->rw) {
620 print_assignment_expression(expression->rw);
622 if (expression->locality) {
624 print_assignment_expression(expression->locality);
630 * Prints a conditional expression.
632 * @param expression the conditional expression
634 static void print_conditional(const conditional_expression_t *expression)
636 print_expression_prec(expression->condition, PREC_LOGICAL_OR);
638 if (expression->true_expression != NULL) {
639 print_expression_prec(expression->true_expression, PREC_EXPRESSION);
644 precedence_t prec = c_mode & _CXX ? PREC_ASSIGNMENT : PREC_CONDITIONAL;
645 print_expression_prec(expression->false_expression, prec);
649 * Prints a va_start expression.
651 * @param expression the va_start expression
653 static void print_va_start(const va_start_expression_t *const expression)
655 fputs("__builtin_va_start(", out);
656 print_assignment_expression(expression->ap);
658 fputs(expression->parameter->base.base.symbol->string, out);
663 * Prints a va_arg expression.
665 * @param expression the va_arg expression
667 static void print_va_arg(const va_arg_expression_t *expression)
669 fputs("__builtin_va_arg(", out);
670 print_assignment_expression(expression->ap);
672 print_type(expression->base.type);
677 * Prints a select expression (. or ->).
679 * @param expression the select expression
681 static void print_select(const select_expression_t *expression)
683 unsigned prec = get_expression_precedence(expression->base.kind);
684 print_expression_prec(expression->compound, prec);
685 if (is_type_pointer(skip_typeref(expression->compound->base.type))) {
690 fputs(expression->compound_entry->base.symbol->string, out);
694 * Prints a type classify expression.
696 * @param expr the type classify expression
698 static void print_classify_type_expression(
699 const classify_type_expression_t *const expr)
701 fputs("__builtin_classify_type(", out);
702 print_assignment_expression(expr->type_expression);
707 * Prints a designator.
709 * @param designator the designator
711 static void print_designator(const designator_t *designator)
713 for ( ; designator != NULL; designator = designator->next) {
714 if (designator->symbol == NULL) {
716 print_expression(designator->array_index);
720 fputs(designator->symbol->string, out);
726 * Prints an offsetof expression.
728 * @param expression the offset expression
730 static void print_offsetof_expression(const offsetof_expression_t *expression)
732 fputs("__builtin_offsetof", out);
734 print_type(expression->type);
736 print_designator(expression->designator);
741 * Prints a statement expression.
743 * @param expression the statement expression
745 static void print_statement_expression(const statement_expression_t *expression)
748 print_statement(expression->statement);
753 * Prints an expression with parenthesis if needed.
755 * @param expression the expression to print
756 * @param top_prec the precedence of the user of this expression.
758 static void print_expression_prec(const expression_t *expression, unsigned top_prec)
760 if (expression->kind == EXPR_UNARY_CAST_IMPLICIT && !print_implicit_casts) {
761 expression = expression->unary.value;
765 expression->base.parenthesized ||
766 (print_parenthesis && top_prec != PREC_BOTTOM) ||
767 top_prec > get_expression_precedence(expression->base.kind);
771 switch (expression->kind) {
774 fputs("$invalid expression$", out);
776 case EXPR_CHARACTER_CONSTANT:
777 print_character_constant(&expression->conste);
779 case EXPR_WIDE_CHARACTER_CONSTANT:
780 print_wide_character_constant(&expression->conste);
783 print_const(&expression->conste);
786 print_funcname(&expression->funcname);
788 case EXPR_STRING_LITERAL:
789 print_string_literal(&expression->string);
791 case EXPR_WIDE_STRING_LITERAL:
792 print_wide_string_literal(&expression->wide_string);
794 case EXPR_COMPOUND_LITERAL:
795 print_compound_literal(&expression->compound_literal);
798 print_call_expression(&expression->call);
801 print_binary_expression(&expression->binary);
804 case EXPR_REFERENCE_ENUM_VALUE:
805 print_reference_expression(&expression->reference);
807 case EXPR_ARRAY_ACCESS:
808 print_array_expression(&expression->array_access);
810 case EXPR_LABEL_ADDRESS:
811 print_label_address_expression(&expression->label_address);
814 print_unary_expression(&expression->unary);
818 print_typeprop_expression(&expression->typeprop);
820 case EXPR_BUILTIN_SYMBOL:
821 print_builtin_symbol(&expression->builtin_symbol);
823 case EXPR_BUILTIN_CONSTANT_P:
824 print_builtin_constant(&expression->builtin_constant);
826 case EXPR_BUILTIN_PREFETCH:
827 print_builtin_prefetch(&expression->builtin_prefetch);
829 case EXPR_CONDITIONAL:
830 print_conditional(&expression->conditional);
833 print_va_start(&expression->va_starte);
836 print_va_arg(&expression->va_arge);
839 print_select(&expression->select);
841 case EXPR_CLASSIFY_TYPE:
842 print_classify_type_expression(&expression->classify_type);
845 print_offsetof_expression(&expression->offsetofe);
848 print_statement_expression(&expression->statement);
853 fprintf(out, "some expression of type %d", (int)expression->kind);
861 * Print an compound statement.
863 * @param block the compound statement
865 static void print_compound_statement(const compound_statement_t *block)
870 statement_t *statement = block->statements;
871 while (statement != NULL) {
872 if (statement->base.kind == STATEMENT_CASE_LABEL)
874 if (statement->kind != STATEMENT_LABEL)
876 print_statement(statement);
878 statement = statement->base.next;
882 fputs(block->stmt_expr ? "}" : "}\n", out);
886 * Print a return statement.
888 * @param statement the return statement
890 static void print_return_statement(const return_statement_t *statement)
892 expression_t const *const val = statement->value;
894 fputs("return ", out);
895 print_expression(val);
898 fputs("return;\n", out);
903 * Print an expression statement.
905 * @param statement the expression statement
907 static void print_expression_statement(const expression_statement_t *statement)
909 print_expression(statement->expression);
914 * Print a goto statement.
916 * @param statement the goto statement
918 static void print_goto_statement(const goto_statement_t *statement)
921 if (statement->expression != NULL) {
923 print_expression(statement->expression);
925 fputs(statement->label->base.symbol->string, out);
931 * Print a label statement.
933 * @param statement the label statement
935 static void print_label_statement(const label_statement_t *statement)
937 fprintf(out, "%s:\n", statement->label->base.symbol->string);
939 print_statement(statement->statement);
943 * Print an if statement.
945 * @param statement the if statement
947 static void print_if_statement(const if_statement_t *statement)
950 print_expression(statement->condition);
952 print_statement(statement->true_statement);
954 if (statement->false_statement != NULL) {
957 print_statement(statement->false_statement);
962 * Print a switch statement.
964 * @param statement the switch statement
966 static void print_switch_statement(const switch_statement_t *statement)
968 fputs("switch (", out);
969 print_expression(statement->expression);
971 print_statement(statement->body);
975 * Print a case label (including the default label).
977 * @param statement the case label statement
979 static void print_case_label(const case_label_statement_t *statement)
981 if (statement->expression == NULL) {
982 fputs("default:\n", out);
985 print_expression(statement->expression);
986 if (statement->end_range != NULL) {
988 print_expression(statement->end_range);
993 if (statement->statement != NULL) {
994 if (statement->statement->base.kind == STATEMENT_CASE_LABEL) {
998 print_statement(statement->statement);
1002 static void print_typedef(const entity_t *entity)
1004 fputs("typedef ", out);
1005 print_type_ext(entity->typedefe.type, entity->base.symbol, NULL);
1010 * returns true if the entity is a compiler generated one and has no real
1011 * correspondenc in the source file
1013 static bool is_generated_entity(const entity_t *entity)
1015 if (entity->kind == ENTITY_TYPEDEF)
1016 return entity->typedefe.builtin;
1018 if (is_declaration(entity))
1019 return entity->declaration.implicit;
1025 * Print a declaration statement.
1027 * @param statement the statement
1029 static void print_declaration_statement(
1030 const declaration_statement_t *statement)
1033 entity_t *entity = statement->declarations_begin;
1034 if (entity == NULL) {
1035 fputs("/* empty declaration statement */\n", out);
1039 entity_t *const end = statement->declarations_end->base.next;
1040 for (; entity != end; entity = entity->base.next) {
1041 if (entity->kind == ENTITY_ENUM_VALUE)
1043 if (is_generated_entity(entity))
1052 print_entity(entity);
1058 * Print a while statement.
1060 * @param statement the statement
1062 static void print_while_statement(const while_statement_t *statement)
1064 fputs("while (", out);
1065 print_expression(statement->condition);
1067 print_statement(statement->body);
1071 * Print a do-while statement.
1073 * @param statement the statement
1075 static void print_do_while_statement(const do_while_statement_t *statement)
1078 print_statement(statement->body);
1080 fputs("while (", out);
1081 print_expression(statement->condition);
1086 * Print a for statement.
1088 * @param statement the statement
1090 static void print_for_statement(const for_statement_t *statement)
1092 fputs("for (", out);
1093 entity_t *entity = statement->scope.entities;
1094 while (entity != NULL && is_generated_entity(entity))
1095 entity = entity->base.next;
1097 if (entity != NULL) {
1098 assert(statement->initialisation == NULL);
1099 assert(is_declaration(entity));
1100 print_declaration(entity);
1101 if (entity->base.next != NULL) {
1102 panic("multiple declarations in for statement not supported yet");
1105 if (statement->initialisation) {
1106 print_expression(statement->initialisation);
1110 if (statement->condition != NULL) {
1112 print_expression(statement->condition);
1115 if (statement->step != NULL) {
1117 print_expression(statement->step);
1120 print_statement(statement->body);
1124 * Print assembler arguments.
1126 * @param arguments the arguments
1128 static void print_asm_arguments(asm_argument_t *arguments)
1130 asm_argument_t *argument = arguments;
1131 for (; argument != NULL; argument = argument->next) {
1132 if (argument != arguments)
1135 if (argument->symbol) {
1136 fprintf(out, "[%s] ", argument->symbol->string);
1138 print_quoted_string(&argument->constraints, '"', 1);
1140 print_expression(argument->expression);
1146 * Print assembler clobbers.
1148 * @param clobbers the clobbers
1150 static void print_asm_clobbers(asm_clobber_t *clobbers)
1152 asm_clobber_t *clobber = clobbers;
1153 for (; clobber != NULL; clobber = clobber->next) {
1154 if (clobber != clobbers)
1157 print_quoted_string(&clobber->clobber, '"', 1);
1162 * Print an assembler statement.
1164 * @param statement the statement
1166 static void print_asm_statement(const asm_statement_t *statement)
1169 if (statement->is_volatile) {
1170 fputs("volatile ", out);
1173 print_quoted_string(&statement->asm_text, '"', 1);
1174 if (statement->outputs == NULL &&
1175 statement->inputs == NULL &&
1176 statement->clobbers == NULL)
1177 goto end_of_print_asm_statement;
1180 print_asm_arguments(statement->outputs);
1181 if (statement->inputs == NULL && statement->clobbers == NULL)
1182 goto end_of_print_asm_statement;
1185 print_asm_arguments(statement->inputs);
1186 if (statement->clobbers == NULL)
1187 goto end_of_print_asm_statement;
1190 print_asm_clobbers(statement->clobbers);
1192 end_of_print_asm_statement:
1197 * Print a microsoft __try statement.
1199 * @param statement the statement
1201 static void print_ms_try_statement(const ms_try_statement_t *statement)
1203 fputs("__try ", out);
1204 print_statement(statement->try_statement);
1206 if (statement->except_expression != NULL) {
1207 fputs("__except(", out);
1208 print_expression(statement->except_expression);
1211 fputs("__finally ", out);
1213 print_statement(statement->final_statement);
1217 * Print a microsoft __leave statement.
1219 * @param statement the statement
1221 static void print_leave_statement(const leave_statement_t *statement)
1224 fputs("__leave;\n", out);
1228 * Print a statement.
1230 * @param statement the statement
1232 void print_statement(const statement_t *statement)
1234 switch (statement->kind) {
1235 case STATEMENT_EMPTY:
1238 case STATEMENT_COMPOUND:
1239 print_compound_statement(&statement->compound);
1241 case STATEMENT_RETURN:
1242 print_return_statement(&statement->returns);
1244 case STATEMENT_EXPRESSION:
1245 print_expression_statement(&statement->expression);
1247 case STATEMENT_LABEL:
1248 print_label_statement(&statement->label);
1250 case STATEMENT_GOTO:
1251 print_goto_statement(&statement->gotos);
1253 case STATEMENT_CONTINUE:
1254 fputs("continue;\n", out);
1256 case STATEMENT_BREAK:
1257 fputs("break;\n", out);
1260 print_if_statement(&statement->ifs);
1262 case STATEMENT_SWITCH:
1263 print_switch_statement(&statement->switchs);
1265 case STATEMENT_CASE_LABEL:
1266 print_case_label(&statement->case_label);
1268 case STATEMENT_DECLARATION:
1269 print_declaration_statement(&statement->declaration);
1271 case STATEMENT_WHILE:
1272 print_while_statement(&statement->whiles);
1274 case STATEMENT_DO_WHILE:
1275 print_do_while_statement(&statement->do_while);
1278 print_for_statement(&statement->fors);
1281 print_asm_statement(&statement->asms);
1283 case STATEMENT_MS_TRY:
1284 print_ms_try_statement(&statement->ms_try);
1286 case STATEMENT_LEAVE:
1287 print_leave_statement(&statement->leave);
1289 case STATEMENT_INVALID:
1290 fputs("$invalid statement$\n", out);
1296 * Print a storage class.
1298 * @param storage_class the storage class
1300 static void print_storage_class(storage_class_tag_t storage_class)
1302 switch (storage_class) {
1303 case STORAGE_CLASS_NONE: return;
1304 case STORAGE_CLASS_TYPEDEF: fputs("typedef ", out); return;
1305 case STORAGE_CLASS_EXTERN: fputs("extern ", out); return;
1306 case STORAGE_CLASS_STATIC: fputs("static ", out); return;
1307 case STORAGE_CLASS_AUTO: fputs("auto ", out); return;
1308 case STORAGE_CLASS_REGISTER: fputs("register ", out); return;
1310 panic("invalid storage class");
1314 * Print an initializer.
1316 * @param initializer the initializer
1318 void print_initializer(const initializer_t *initializer)
1320 if (initializer == NULL) {
1325 switch (initializer->kind) {
1326 case INITIALIZER_VALUE: {
1327 const initializer_value_t *value = &initializer->value;
1328 print_assignment_expression(value->value);
1331 case INITIALIZER_LIST: {
1332 assert(initializer->kind == INITIALIZER_LIST);
1334 const initializer_list_t *list = &initializer->list;
1336 for (size_t i = 0 ; i < list->len; ++i) {
1337 const initializer_t *sub_init = list->initializers[i];
1338 print_initializer(list->initializers[i]);
1339 if (i < list->len-1) {
1340 if (sub_init == NULL || sub_init->kind != INITIALIZER_DESIGNATOR)
1347 case INITIALIZER_STRING:
1348 print_quoted_string(&initializer->string.string, '"', 1);
1350 case INITIALIZER_WIDE_STRING:
1351 print_quoted_wide_string(&initializer->wide_string.string, '"', 1);
1353 case INITIALIZER_DESIGNATOR:
1354 print_designator(initializer->designator.designator);
1359 panic("invalid initializer kind found");
1363 * Print microsoft extended declaration modifiers.
1365 static void print_ms_modifiers(const declaration_t *declaration)
1367 if ((c_mode & _MS) == 0)
1370 decl_modifiers_t modifiers = declaration->modifiers;
1372 bool ds_shown = false;
1373 const char *next = "(";
1375 if (declaration->base.kind == ENTITY_VARIABLE) {
1376 variable_t *variable = (variable_t*)declaration;
1377 if (variable->alignment != 0
1378 || variable->get_property_sym != NULL
1379 || variable->put_property_sym != NULL) {
1381 fputs("__declspec", out);
1385 if (variable->alignment != 0) {
1386 fputs(next, out); next = ", "; fprintf(out, "align(%u)", variable->alignment);
1388 if (variable->get_property_sym != NULL
1389 || variable->put_property_sym != NULL) {
1391 fputs(next, out); next = ", "; fputs("property(", out);
1392 if (variable->get_property_sym != NULL) {
1393 fprintf(out, "get=%s", variable->get_property_sym->string);
1396 if (variable->put_property_sym != NULL)
1397 fprintf(out, "%sput=%s", comma, variable->put_property_sym->string);
1403 /* DM_FORCEINLINE handled outside. */
1404 if ((modifiers & ~DM_FORCEINLINE) != 0) {
1406 fputs("__declspec", out);
1409 if (modifiers & DM_DLLIMPORT) {
1410 fputs(next, out); next = ", "; fputs("dllimport", out);
1412 if (modifiers & DM_DLLEXPORT) {
1413 fputs(next, out); next = ", "; fputs("dllexport", out);
1415 if (modifiers & DM_THREAD) {
1416 fputs(next, out); next = ", "; fputs("thread", out);
1418 if (modifiers & DM_NAKED) {
1419 fputs(next, out); next = ", "; fputs("naked", out);
1421 if (modifiers & DM_THREAD) {
1422 fputs(next, out); next = ", "; fputs("thread", out);
1424 if (modifiers & DM_SELECTANY) {
1425 fputs(next, out); next = ", "; fputs("selectany", out);
1427 if (modifiers & DM_NOTHROW) {
1428 fputs(next, out); next = ", "; fputs("nothrow", out);
1430 if (modifiers & DM_NORETURN) {
1431 fputs(next, out); next = ", "; fputs("noreturn", out);
1433 if (modifiers & DM_NOINLINE) {
1434 fputs(next, out); next = ", "; fputs("noinline", out);
1436 if (modifiers & DM_DEPRECATED) {
1437 fputs(next, out); next = ", "; fputs("deprecated", out);
1438 if (declaration->deprecated_string != NULL)
1439 fprintf(out, "(\"%s\")",
1440 declaration->deprecated_string);
1442 if (modifiers & DM_RESTRICT) {
1443 fputs(next, out); next = ", "; fputs("restrict", out);
1445 if (modifiers & DM_NOALIAS) {
1446 fputs(next, out); next = ", "; fputs("noalias", out);
1454 static void print_scope(const scope_t *scope)
1456 const entity_t *entity = scope->entities;
1457 for ( ; entity != NULL; entity = entity->base.next) {
1459 print_entity(entity);
1464 static void print_namespace(const namespace_t *namespace)
1466 fputs("namespace ", out);
1467 if (namespace->base.symbol != NULL) {
1468 fputs(namespace->base.symbol->string, out);
1475 print_scope(&namespace->members);
1483 * Print a variable or function declaration
1485 void print_declaration(const entity_t *entity)
1487 assert(is_declaration(entity));
1488 const declaration_t *declaration = &entity->declaration;
1490 print_storage_class((storage_class_tag_t)declaration->declared_storage_class);
1491 if (entity->kind == ENTITY_FUNCTION) {
1492 function_t *function = (function_t*)declaration;
1493 if (function->is_inline) {
1494 if (declaration->modifiers & DM_FORCEINLINE) {
1495 fputs("__forceinline ", out);
1496 } else if (declaration->modifiers & DM_MICROSOFT_INLINE) {
1497 fputs("__inline ", out);
1499 fputs("inline ", out);
1503 print_ms_modifiers(declaration);
1504 switch (entity->kind) {
1505 case ENTITY_FUNCTION:
1506 print_type_ext(entity->declaration.type, entity->base.symbol,
1507 &entity->function.parameters);
1509 if (entity->function.statement != NULL) {
1512 print_statement(entity->function.statement);
1517 case ENTITY_VARIABLE:
1518 if (entity->variable.thread_local)
1519 fputs("__thread ", out);
1520 print_type_ext(declaration->type, declaration->base.symbol, NULL);
1521 if (entity->variable.initializer != NULL) {
1523 print_initializer(entity->variable.initializer);
1528 print_type_ext(declaration->type, declaration->base.symbol, NULL);
1535 * Prints an expression.
1537 * @param expression the expression
1539 void print_expression(const expression_t *expression)
1541 print_expression_prec(expression, PREC_BOTTOM);
1545 * Print a declaration.
1547 * @param declaration the declaration
1549 void print_entity(const entity_t *entity)
1551 if (entity->base.namespc != NAMESPACE_NORMAL && entity->base.symbol == NULL)
1554 switch ((entity_kind_tag_t)entity->kind) {
1555 case ENTITY_VARIABLE:
1556 case ENTITY_PARAMETER:
1557 case ENTITY_COMPOUND_MEMBER:
1558 print_declaration(entity);
1560 case ENTITY_FUNCTION:
1561 print_declaration(entity);
1563 case ENTITY_TYPEDEF:
1564 print_typedef(entity);
1567 fputs("struct ", out);
1568 fputs(entity->base.symbol->string, out);
1569 if (entity->structe.complete) {
1571 print_compound_definition(&entity->structe);
1576 fputs("union ", out);
1577 fputs(entity->base.symbol->string, out);
1578 if (entity->unione.complete) {
1580 print_compound_definition(&entity->unione);
1585 fputs("enum ", out);
1586 fputs(entity->base.symbol->string, out);
1588 print_enum_definition(&entity->enume);
1591 case ENTITY_NAMESPACE:
1592 print_namespace(&entity->namespacee);
1594 case ENTITY_LOCAL_LABEL:
1595 fprintf(out, "__label__ %s;", entity->base.symbol->string);
1598 case ENTITY_ENUM_VALUE:
1599 panic("print_entity used on unexpected entity type");
1600 case ENTITY_INVALID:
1603 panic("Invalid entity type encountered");
1607 * Print the AST of a translation unit.
1609 * @param unit the translation unit
1611 void print_ast(const translation_unit_t *unit)
1615 entity_t *entity = unit->scope.entities;
1616 for ( ; entity != NULL; entity = entity->base.next) {
1617 if (entity->kind == ENTITY_ENUM_VALUE)
1619 if (entity->base.namespc != NAMESPACE_NORMAL
1620 && entity->base.symbol == NULL)
1622 if (is_generated_entity(entity))
1626 print_entity(entity);
1631 bool is_constant_initializer(const initializer_t *initializer)
1633 switch (initializer->kind) {
1634 case INITIALIZER_STRING:
1635 case INITIALIZER_WIDE_STRING:
1636 case INITIALIZER_DESIGNATOR:
1639 case INITIALIZER_VALUE:
1640 return is_constant_expression(initializer->value.value);
1642 case INITIALIZER_LIST:
1643 for (size_t i = 0; i < initializer->list.len; ++i) {
1644 initializer_t *sub_initializer = initializer->list.initializers[i];
1645 if (!is_constant_initializer(sub_initializer))
1650 panic("invalid initializer kind found");
1653 static bool is_object_with_linker_constant_address(const expression_t *expression)
1655 switch (expression->kind) {
1656 case EXPR_UNARY_DEREFERENCE:
1657 return is_address_constant(expression->unary.value);
1660 type_t *base_type = skip_typeref(expression->select.compound->base.type);
1661 if (is_type_pointer(base_type)) {
1663 return is_address_constant(expression->select.compound);
1665 return is_object_with_linker_constant_address(expression->select.compound);
1669 case EXPR_ARRAY_ACCESS:
1670 return is_constant_expression(expression->array_access.index)
1671 && is_address_constant(expression->array_access.array_ref);
1673 case EXPR_REFERENCE: {
1674 entity_t *entity = expression->reference.entity;
1675 if (is_declaration(entity)) {
1676 switch ((storage_class_tag_t)entity->declaration.storage_class) {
1677 case STORAGE_CLASS_NONE:
1678 case STORAGE_CLASS_EXTERN:
1679 case STORAGE_CLASS_STATIC:
1681 entity->kind != ENTITY_VARIABLE ||
1682 !entity->variable.thread_local;
1684 case STORAGE_CLASS_REGISTER:
1685 case STORAGE_CLASS_TYPEDEF:
1686 case STORAGE_CLASS_AUTO:
1698 bool is_address_constant(const expression_t *expression)
1700 switch (expression->kind) {
1701 case EXPR_UNARY_TAKE_ADDRESS:
1702 return is_object_with_linker_constant_address(expression->unary.value);
1704 case EXPR_UNARY_DEREFERENCE: {
1706 = revert_automatic_type_conversion(expression->unary.value);
1707 /* dereferencing a function is a NOP */
1708 if (is_type_function(real_type)) {
1709 return is_address_constant(expression->unary.value);
1714 case EXPR_UNARY_CAST: {
1715 type_t *dest = skip_typeref(expression->base.type);
1716 if (!is_type_pointer(dest) && (
1717 dest->kind != TYPE_ATOMIC ||
1718 !(get_atomic_type_flags(dest->atomic.akind) & ATOMIC_TYPE_FLAG_INTEGER) ||
1719 get_atomic_type_size(dest->atomic.akind) < get_atomic_type_size(get_intptr_kind())
1723 return (is_constant_expression(expression->unary.value)
1724 || is_address_constant(expression->unary.value));
1727 case EXPR_BINARY_ADD:
1728 case EXPR_BINARY_SUB: {
1729 expression_t *left = expression->binary.left;
1730 expression_t *right = expression->binary.right;
1732 if (is_type_pointer(skip_typeref(left->base.type))) {
1733 return is_address_constant(left) && is_constant_expression(right);
1734 } else if (is_type_pointer(skip_typeref(right->base.type))) {
1735 return is_constant_expression(left) && is_address_constant(right);
1741 case EXPR_REFERENCE: {
1742 entity_t *entity = expression->reference.entity;
1743 if (!is_declaration(entity))
1746 type_t *type = skip_typeref(entity->declaration.type);
1747 if (is_type_function(type))
1749 if (is_type_array(type)) {
1750 return is_object_with_linker_constant_address(expression);
1752 /* Prevent stray errors */
1753 if (!is_type_valid(type))
1758 case EXPR_ARRAY_ACCESS: {
1759 type_t *const type =
1760 skip_typeref(revert_automatic_type_conversion(expression));
1762 is_type_array(type) &&
1763 is_constant_expression(expression->array_access.index) &&
1764 is_address_constant(expression->array_access.array_ref);
1772 static bool is_builtin_const_call(const expression_t *expression)
1774 expression_t *function = expression->call.function;
1775 if (function->kind != EXPR_BUILTIN_SYMBOL) {
1779 symbol_t *symbol = function->builtin_symbol.symbol;
1781 switch (symbol->ID) {
1782 case T___builtin_huge_val:
1783 case T___builtin_inf:
1784 case T___builtin_inff:
1785 case T___builtin_infl:
1786 case T___builtin_nan:
1787 case T___builtin_nanf:
1788 case T___builtin_nanl:
1795 static bool is_constant_pointer(const expression_t *expression)
1797 if (is_constant_expression(expression))
1800 switch (expression->kind) {
1801 case EXPR_UNARY_CAST:
1802 return is_constant_pointer(expression->unary.value);
1808 static bool is_object_with_constant_address(const expression_t *expression)
1810 switch (expression->kind) {
1812 expression_t *compound = expression->select.compound;
1813 type_t *compound_type = compound->base.type;
1814 compound_type = skip_typeref(compound_type);
1815 if (is_type_pointer(compound_type)) {
1816 return is_constant_pointer(compound);
1818 return is_object_with_constant_address(compound);
1822 case EXPR_ARRAY_ACCESS: {
1823 array_access_expression_t const* const array_access =
1824 &expression->array_access;
1826 is_constant_expression(array_access->index) && (
1827 is_object_with_constant_address(array_access->array_ref) ||
1828 is_constant_pointer(array_access->array_ref)
1832 case EXPR_UNARY_DEREFERENCE:
1833 return is_constant_pointer(expression->unary.value);
1839 bool is_constant_expression(const expression_t *expression)
1841 switch (expression->kind) {
1844 case EXPR_CHARACTER_CONSTANT:
1845 case EXPR_WIDE_CHARACTER_CONSTANT:
1846 case EXPR_STRING_LITERAL:
1847 case EXPR_WIDE_STRING_LITERAL:
1848 case EXPR_CLASSIFY_TYPE:
1852 case EXPR_BUILTIN_CONSTANT_P:
1853 case EXPR_LABEL_ADDRESS:
1854 case EXPR_REFERENCE_ENUM_VALUE:
1858 type_t *type = expression->typeprop.type;
1860 type = expression->typeprop.tp_expression->base.type;
1862 type = skip_typeref(type);
1863 if (is_type_array(type) && type->array.is_vla)
1868 case EXPR_BUILTIN_SYMBOL:
1869 case EXPR_BUILTIN_PREFETCH:
1873 case EXPR_STATEMENT:
1874 case EXPR_REFERENCE:
1875 case EXPR_UNARY_POSTFIX_INCREMENT:
1876 case EXPR_UNARY_POSTFIX_DECREMENT:
1877 case EXPR_UNARY_PREFIX_INCREMENT:
1878 case EXPR_UNARY_PREFIX_DECREMENT:
1879 case EXPR_UNARY_ASSUME: /* has VOID type */
1880 case EXPR_UNARY_DEREFERENCE:
1881 case EXPR_UNARY_DELETE:
1882 case EXPR_UNARY_DELETE_ARRAY:
1883 case EXPR_UNARY_THROW:
1884 case EXPR_BINARY_ASSIGN:
1885 case EXPR_BINARY_MUL_ASSIGN:
1886 case EXPR_BINARY_DIV_ASSIGN:
1887 case EXPR_BINARY_MOD_ASSIGN:
1888 case EXPR_BINARY_ADD_ASSIGN:
1889 case EXPR_BINARY_SUB_ASSIGN:
1890 case EXPR_BINARY_SHIFTLEFT_ASSIGN:
1891 case EXPR_BINARY_SHIFTRIGHT_ASSIGN:
1892 case EXPR_BINARY_BITWISE_AND_ASSIGN:
1893 case EXPR_BINARY_BITWISE_XOR_ASSIGN:
1894 case EXPR_BINARY_BITWISE_OR_ASSIGN:
1895 case EXPR_BINARY_COMMA:
1896 case EXPR_ARRAY_ACCESS:
1899 case EXPR_UNARY_TAKE_ADDRESS:
1900 return is_object_with_constant_address(expression->unary.value);
1903 return is_builtin_const_call(expression);
1905 case EXPR_UNARY_NEGATE:
1906 case EXPR_UNARY_PLUS:
1907 case EXPR_UNARY_BITWISE_NEGATE:
1908 case EXPR_UNARY_NOT:
1909 return is_constant_expression(expression->unary.value);
1911 case EXPR_UNARY_CAST:
1912 case EXPR_UNARY_CAST_IMPLICIT:
1913 return is_type_arithmetic(skip_typeref(expression->base.type))
1914 && is_constant_expression(expression->unary.value);
1916 case EXPR_BINARY_ADD:
1917 case EXPR_BINARY_SUB:
1918 case EXPR_BINARY_MUL:
1919 case EXPR_BINARY_DIV:
1920 case EXPR_BINARY_MOD:
1921 case EXPR_BINARY_EQUAL:
1922 case EXPR_BINARY_NOTEQUAL:
1923 case EXPR_BINARY_LESS:
1924 case EXPR_BINARY_LESSEQUAL:
1925 case EXPR_BINARY_GREATER:
1926 case EXPR_BINARY_GREATEREQUAL:
1927 case EXPR_BINARY_BITWISE_AND:
1928 case EXPR_BINARY_BITWISE_OR:
1929 case EXPR_BINARY_BITWISE_XOR:
1930 case EXPR_BINARY_SHIFTLEFT:
1931 case EXPR_BINARY_SHIFTRIGHT:
1932 case EXPR_BINARY_ISGREATER:
1933 case EXPR_BINARY_ISGREATEREQUAL:
1934 case EXPR_BINARY_ISLESS:
1935 case EXPR_BINARY_ISLESSEQUAL:
1936 case EXPR_BINARY_ISLESSGREATER:
1937 case EXPR_BINARY_ISUNORDERED:
1938 return is_constant_expression(expression->binary.left)
1939 && is_constant_expression(expression->binary.right);
1941 case EXPR_BINARY_LOGICAL_AND: {
1942 expression_t const *const left = expression->binary.left;
1943 if (!is_constant_expression(left))
1945 if (fold_constant(left) == 0)
1947 return is_constant_expression(expression->binary.right);
1950 case EXPR_BINARY_LOGICAL_OR: {
1951 expression_t const *const left = expression->binary.left;
1952 if (!is_constant_expression(left))
1954 if (fold_constant(left) != 0)
1956 return is_constant_expression(expression->binary.right);
1959 case EXPR_COMPOUND_LITERAL:
1960 return is_constant_initializer(expression->compound_literal.initializer);
1962 case EXPR_CONDITIONAL: {
1963 expression_t *condition = expression->conditional.condition;
1964 if (!is_constant_expression(condition))
1967 long val = fold_constant(condition);
1969 return is_constant_expression(expression->conditional.true_expression);
1971 return is_constant_expression(expression->conditional.false_expression);
1980 panic("invalid expression found (is constant expression)");
1984 * Initialize the AST construction.
1988 obstack_init(&ast_obstack);
1996 obstack_free(&ast_obstack, NULL);
2000 * Set the output stream for the AST printer.
2002 * @param stream the output stream
2004 void ast_set_output(FILE *stream)
2007 type_set_output(stream);
2011 * Allocate an AST object of the given size.
2013 * @param size the size of the object to allocate
2015 * @return A new allocated object in the AST memeory space.
2017 void *(allocate_ast)(size_t size)
2019 return _allocate_ast(size);