2 * This file is part of cparser.
3 * Copyright (C) 2007-2009 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"
35 #if defined(__INTEL_COMPILER)
37 #elif defined(__CYGWIN__)
38 #include "win32/cygwin_math_ext.h"
43 #include "adt/error.h"
46 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_CONSTANT_P] = PREC_PRIMARY,
118 [EXPR_BUILTIN_TYPES_COMPATIBLE_P] = PREC_PRIMARY,
119 [EXPR_OFFSETOF] = PREC_PRIMARY,
120 [EXPR_VA_START] = PREC_PRIMARY,
121 [EXPR_VA_ARG] = PREC_PRIMARY,
122 [EXPR_VA_COPY] = PREC_PRIMARY,
123 [EXPR_STATEMENT] = PREC_PRIMARY,
124 [EXPR_LABEL_ADDRESS] = PREC_PRIMARY,
126 [EXPR_UNARY_NEGATE] = PREC_UNARY,
127 [EXPR_UNARY_PLUS] = PREC_UNARY,
128 [EXPR_UNARY_BITWISE_NEGATE] = PREC_UNARY,
129 [EXPR_UNARY_NOT] = PREC_UNARY,
130 [EXPR_UNARY_DEREFERENCE] = PREC_UNARY,
131 [EXPR_UNARY_TAKE_ADDRESS] = PREC_UNARY,
132 [EXPR_UNARY_POSTFIX_INCREMENT] = PREC_POSTFIX,
133 [EXPR_UNARY_POSTFIX_DECREMENT] = PREC_POSTFIX,
134 [EXPR_UNARY_PREFIX_INCREMENT] = PREC_UNARY,
135 [EXPR_UNARY_PREFIX_DECREMENT] = PREC_UNARY,
136 [EXPR_UNARY_CAST] = PREC_UNARY,
137 [EXPR_UNARY_CAST_IMPLICIT] = PREC_UNARY,
138 [EXPR_UNARY_ASSUME] = PREC_PRIMARY,
139 [EXPR_UNARY_DELETE] = PREC_UNARY,
140 [EXPR_UNARY_DELETE_ARRAY] = PREC_UNARY,
141 [EXPR_UNARY_THROW] = PREC_ASSIGNMENT,
143 [EXPR_BINARY_ADD] = PREC_ADDITIVE,
144 [EXPR_BINARY_SUB] = PREC_ADDITIVE,
145 [EXPR_BINARY_MUL] = PREC_MULTIPLICATIVE,
146 [EXPR_BINARY_DIV] = PREC_MULTIPLICATIVE,
147 [EXPR_BINARY_MOD] = PREC_MULTIPLICATIVE,
148 [EXPR_BINARY_EQUAL] = PREC_EQUALITY,
149 [EXPR_BINARY_NOTEQUAL] = PREC_EQUALITY,
150 [EXPR_BINARY_LESS] = PREC_RELATIONAL,
151 [EXPR_BINARY_LESSEQUAL] = PREC_RELATIONAL,
152 [EXPR_BINARY_GREATER] = PREC_RELATIONAL,
153 [EXPR_BINARY_GREATEREQUAL] = PREC_RELATIONAL,
154 [EXPR_BINARY_BITWISE_AND] = PREC_AND,
155 [EXPR_BINARY_BITWISE_OR] = PREC_OR,
156 [EXPR_BINARY_BITWISE_XOR] = PREC_XOR,
157 [EXPR_BINARY_LOGICAL_AND] = PREC_LOGICAL_AND,
158 [EXPR_BINARY_LOGICAL_OR] = PREC_LOGICAL_OR,
159 [EXPR_BINARY_SHIFTLEFT] = PREC_SHIFT,
160 [EXPR_BINARY_SHIFTRIGHT] = PREC_SHIFT,
161 [EXPR_BINARY_ASSIGN] = PREC_ASSIGNMENT,
162 [EXPR_BINARY_MUL_ASSIGN] = PREC_ASSIGNMENT,
163 [EXPR_BINARY_DIV_ASSIGN] = PREC_ASSIGNMENT,
164 [EXPR_BINARY_MOD_ASSIGN] = PREC_ASSIGNMENT,
165 [EXPR_BINARY_ADD_ASSIGN] = PREC_ASSIGNMENT,
166 [EXPR_BINARY_SUB_ASSIGN] = PREC_ASSIGNMENT,
167 [EXPR_BINARY_SHIFTLEFT_ASSIGN] = PREC_ASSIGNMENT,
168 [EXPR_BINARY_SHIFTRIGHT_ASSIGN] = PREC_ASSIGNMENT,
169 [EXPR_BINARY_BITWISE_AND_ASSIGN] = PREC_ASSIGNMENT,
170 [EXPR_BINARY_BITWISE_XOR_ASSIGN] = PREC_ASSIGNMENT,
171 [EXPR_BINARY_BITWISE_OR_ASSIGN] = PREC_ASSIGNMENT,
172 [EXPR_BINARY_COMMA] = PREC_EXPRESSION,
174 [EXPR_BINARY_ISGREATER] = PREC_PRIMARY,
175 [EXPR_BINARY_ISGREATEREQUAL] = PREC_PRIMARY,
176 [EXPR_BINARY_ISLESS] = PREC_PRIMARY,
177 [EXPR_BINARY_ISLESSEQUAL] = PREC_PRIMARY,
178 [EXPR_BINARY_ISLESSGREATER] = PREC_PRIMARY,
179 [EXPR_BINARY_ISUNORDERED] = PREC_PRIMARY
181 assert((size_t)kind < lengthof(prec));
182 unsigned res = prec[kind];
184 assert(res != PREC_BOTTOM);
189 * Print a constant expression.
191 * @param cnst the constant expression
193 static void print_const(const const_expression_t *cnst)
195 if (cnst->base.type == NULL)
198 const type_t *const type = skip_typeref(cnst->base.type);
200 if (is_type_atomic(type, ATOMIC_TYPE_BOOL)) {
201 print_string(cnst->v.int_value ? "true" : "false");
202 } else if (is_type_integer(type)) {
203 print_format("%lld", cnst->v.int_value);
204 } else if (is_type_float(type)) {
205 long double const val = cnst->v.float_value;
207 /* ARG, no way to print long double */
208 print_format("%.20g", (double)val);
210 print_format("%.20Lg", val);
212 if (isfinite(val) && truncl(val) == val)
215 panic("unknown constant");
219 switch (type->atomic.akind) {
220 case ATOMIC_TYPE_UINT: suffix = "U"; break;
221 case ATOMIC_TYPE_LONG: suffix = "L"; break;
222 case ATOMIC_TYPE_ULONG: suffix = "UL"; break;
223 case ATOMIC_TYPE_LONGLONG: suffix = "LL"; break;
224 case ATOMIC_TYPE_ULONGLONG: suffix = "ULL"; break;
225 case ATOMIC_TYPE_FLOAT: suffix = "F"; break;
226 case ATOMIC_TYPE_LONG_DOUBLE: suffix = "L"; break;
230 print_string(suffix);
234 * Print a quoted string constant.
236 * @param string the string constant
237 * @param border the border char
238 * @param skip number of chars to skip at the end
240 static void print_quoted_string(const string_t *const string, char border, int skip)
243 const char *end = string->begin + string->size - skip;
244 for (const char *c = string->begin; c != end; ++c) {
245 unsigned char const tc = *c;
250 case '\\': print_string("\\\\"); break;
251 case '\a': print_string("\\a"); break;
252 case '\b': print_string("\\b"); break;
253 case '\f': print_string("\\f"); break;
254 case '\n': print_string("\\n"); break;
255 case '\r': print_string("\\r"); break;
256 case '\t': print_string("\\t"); break;
257 case '\v': print_string("\\v"); break;
258 case '\?': print_string("\\?"); break;
260 if (c_mode & _GNUC) {
261 print_string("\\e"); break;
265 if (tc < 0x80 && !isprint(tc)) {
266 print_format("\\%03o", (unsigned)tc);
277 * Prints a wide string literal expression.
279 * @param wstr the wide string literal expression
280 * @param border the border char
281 * @param skip number of chars to skip at the end
283 static void print_quoted_wide_string(const wide_string_t *const wstr,
284 char border, int skip)
288 const wchar_rep_t *end = wstr->begin + wstr->size - skip;
289 for (const wchar_rep_t *c = wstr->begin; c != end; ++c) {
291 case L'\"': print_string("\\\""); break;
292 case L'\\': print_string("\\\\"); break;
293 case L'\a': print_string("\\a"); break;
294 case L'\b': print_string("\\b"); break;
295 case L'\f': print_string("\\f"); break;
296 case L'\n': print_string("\\n"); break;
297 case L'\r': print_string("\\r"); break;
298 case L'\t': print_string("\\t"); break;
299 case L'\v': print_string("\\v"); break;
300 case L'\?': print_string("\\?"); break;
302 if (c_mode & _GNUC) {
303 print_string("\\e"); break;
307 const unsigned tc = *c;
312 print_format("\\%03o", tc);
324 * Print a constant character expression.
326 * @param cnst the constant character expression
328 static void print_character_constant(const const_expression_t *cnst)
330 print_quoted_string(&cnst->v.character, '\'', 0);
333 static void print_wide_character_constant(const const_expression_t *cnst)
335 print_quoted_wide_string(&cnst->v.wide_character, '\'', 0);
339 * Prints a string literal expression.
341 * @param string_literal the string literal expression
343 static void print_string_literal(
344 const string_literal_expression_t *string_literal)
346 print_quoted_string(&string_literal->value, '"', 1);
350 * Prints a predefined symbol.
352 static void print_funcname(const funcname_expression_t *funcname)
355 switch (funcname->kind) {
356 case FUNCNAME_FUNCTION: s = (c_mode & _C99) ? "__func__" : "__FUNCTION__"; break;
357 case FUNCNAME_PRETTY_FUNCTION: s = "__PRETTY_FUNCTION__"; break;
358 case FUNCNAME_FUNCSIG: s = "__FUNCSIG__"; break;
359 case FUNCNAME_FUNCDNAME: s = "__FUNCDNAME__"; break;
364 static void print_wide_string_literal(
365 const wide_string_literal_expression_t *const wstr)
367 print_quoted_wide_string(&wstr->value, '"', 1);
370 static void print_compound_literal(
371 const compound_literal_expression_t *expression)
374 print_type(expression->type);
376 print_initializer(expression->initializer);
379 static void print_assignment_expression(const expression_t *const expr)
381 print_expression_prec(expr, PREC_ASSIGNMENT);
385 * Prints a call expression.
387 * @param call the call expression
389 static void print_call_expression(const call_expression_t *call)
391 print_expression_prec(call->function, PREC_POSTFIX);
393 call_argument_t *argument = call->arguments;
395 while (argument != NULL) {
401 print_assignment_expression(argument->expression);
403 argument = argument->next;
409 * Prints a binary expression.
411 * @param binexpr the binary expression
413 static void print_binary_expression(const binary_expression_t *binexpr)
415 unsigned prec = get_expression_precedence(binexpr->base.kind);
416 int r2l = right_to_left(prec);
418 print_expression_prec(binexpr->left, prec + r2l);
420 switch (binexpr->base.kind) {
421 case EXPR_BINARY_COMMA: op = ", "; break;
422 case EXPR_BINARY_ASSIGN: op = " = "; break;
423 case EXPR_BINARY_ADD: op = " + "; break;
424 case EXPR_BINARY_SUB: op = " - "; break;
425 case EXPR_BINARY_MUL: op = " * "; break;
426 case EXPR_BINARY_MOD: op = " % "; break;
427 case EXPR_BINARY_DIV: op = " / "; break;
428 case EXPR_BINARY_BITWISE_OR: op = " | "; break;
429 case EXPR_BINARY_BITWISE_AND: op = " & "; break;
430 case EXPR_BINARY_BITWISE_XOR: op = " ^ "; break;
431 case EXPR_BINARY_LOGICAL_OR: op = " || "; break;
432 case EXPR_BINARY_LOGICAL_AND: op = " && "; break;
433 case EXPR_BINARY_NOTEQUAL: op = " != "; break;
434 case EXPR_BINARY_EQUAL: op = " == "; break;
435 case EXPR_BINARY_LESS: op = " < "; break;
436 case EXPR_BINARY_LESSEQUAL: op = " <= "; break;
437 case EXPR_BINARY_GREATER: op = " > "; break;
438 case EXPR_BINARY_GREATEREQUAL: op = " >= "; break;
439 case EXPR_BINARY_SHIFTLEFT: op = " << "; break;
440 case EXPR_BINARY_SHIFTRIGHT: op = " >> "; break;
442 case EXPR_BINARY_ADD_ASSIGN: op = " += "; break;
443 case EXPR_BINARY_SUB_ASSIGN: op = " -= "; break;
444 case EXPR_BINARY_MUL_ASSIGN: op = " *= "; break;
445 case EXPR_BINARY_MOD_ASSIGN: op = " %= "; break;
446 case EXPR_BINARY_DIV_ASSIGN: op = " /= "; break;
447 case EXPR_BINARY_BITWISE_OR_ASSIGN: op = " |= "; break;
448 case EXPR_BINARY_BITWISE_AND_ASSIGN: op = " &= "; break;
449 case EXPR_BINARY_BITWISE_XOR_ASSIGN: op = " ^= "; break;
450 case EXPR_BINARY_SHIFTLEFT_ASSIGN: op = " <<= "; break;
451 case EXPR_BINARY_SHIFTRIGHT_ASSIGN: op = " >>= "; break;
452 default: panic("invalid binexpression found");
455 print_expression_prec(binexpr->right, prec + 1 - r2l);
459 * Prints an unary expression.
461 * @param unexpr the unary expression
463 static void print_unary_expression(const unary_expression_t *unexpr)
465 unsigned prec = get_expression_precedence(unexpr->base.kind);
466 switch (unexpr->base.kind) {
467 case EXPR_UNARY_NEGATE: print_string("-"); break;
468 case EXPR_UNARY_PLUS: print_string("+"); break;
469 case EXPR_UNARY_NOT: print_string("!"); break;
470 case EXPR_UNARY_BITWISE_NEGATE: print_string("~"); break;
471 case EXPR_UNARY_PREFIX_INCREMENT: print_string("++"); break;
472 case EXPR_UNARY_PREFIX_DECREMENT: print_string("--"); break;
473 case EXPR_UNARY_DEREFERENCE: print_string("*"); break;
474 case EXPR_UNARY_TAKE_ADDRESS: print_string("&"); break;
475 case EXPR_UNARY_DELETE: print_string("delete "); break;
476 case EXPR_UNARY_DELETE_ARRAY: print_string("delete [] "); break;
478 case EXPR_UNARY_POSTFIX_INCREMENT:
479 print_expression_prec(unexpr->value, prec);
482 case EXPR_UNARY_POSTFIX_DECREMENT:
483 print_expression_prec(unexpr->value, prec);
486 case EXPR_UNARY_CAST_IMPLICIT:
487 case EXPR_UNARY_CAST:
489 print_type(unexpr->base.type);
492 case EXPR_UNARY_ASSUME:
493 print_string("__assume(");
494 print_assignment_expression(unexpr->value);
498 case EXPR_UNARY_THROW:
499 if (unexpr->value == NULL) {
500 print_string("throw");
503 print_string("throw ");
507 panic("invalid unary expression found");
509 print_expression_prec(unexpr->value, prec);
513 * Prints a reference expression.
515 * @param ref the reference expression
517 static void print_reference_expression(const reference_expression_t *ref)
519 print_string(ref->entity->base.symbol->string);
523 * Prints a label address expression.
525 * @param ref the reference expression
527 static void print_label_address_expression(const label_address_expression_t *le)
529 print_format("&&%s", le->label->base.symbol->string);
533 * Prints an array expression.
535 * @param expression the array expression
537 static void print_array_expression(const array_access_expression_t *expression)
539 if (!expression->flipped) {
540 print_expression_prec(expression->array_ref, PREC_POSTFIX);
542 print_expression(expression->index);
545 print_expression_prec(expression->index, PREC_POSTFIX);
547 print_expression(expression->array_ref);
553 * Prints a typeproperty expression (sizeof or __alignof__).
555 * @param expression the type property expression
557 static void print_typeprop_expression(const typeprop_expression_t *expression)
559 if (expression->base.kind == EXPR_SIZEOF) {
560 print_string("sizeof");
562 assert(expression->base.kind == EXPR_ALIGNOF);
563 print_string("__alignof__");
565 if (expression->tp_expression != NULL) {
566 /* PREC_TOP: always print the '()' here, sizeof x is right but unusual */
567 print_expression_prec(expression->tp_expression, PREC_TOP);
570 print_type(expression->type);
576 * Prints a builtin constant expression.
578 * @param expression the builtin constant expression
580 static void print_builtin_constant(const builtin_constant_expression_t *expression)
582 print_string("__builtin_constant_p(");
583 print_assignment_expression(expression->value);
588 * Prints a builtin types compatible expression.
590 * @param expression the builtin types compatible expression
592 static void print_builtin_types_compatible(
593 const builtin_types_compatible_expression_t *expression)
595 print_string("__builtin_types_compatible_p(");
596 print_type(expression->left);
598 print_type(expression->right);
603 * Prints a conditional expression.
605 * @param expression the conditional expression
607 static void print_conditional(const conditional_expression_t *expression)
609 print_expression_prec(expression->condition, PREC_LOGICAL_OR);
610 if (expression->true_expression != NULL) {
612 print_expression_prec(expression->true_expression, PREC_EXPRESSION);
615 print_string(" ?: ");
617 precedence_t prec = c_mode & _CXX ? PREC_ASSIGNMENT : PREC_CONDITIONAL;
618 print_expression_prec(expression->false_expression, prec);
622 * Prints a va_start expression.
624 * @param expression the va_start expression
626 static void print_va_start(const va_start_expression_t *const expression)
628 print_string("__builtin_va_start(");
629 print_assignment_expression(expression->ap);
631 print_string(expression->parameter->base.base.symbol->string);
636 * Prints a va_arg expression.
638 * @param expression the va_arg expression
640 static void print_va_arg(const va_arg_expression_t *expression)
642 print_string("__builtin_va_arg(");
643 print_assignment_expression(expression->ap);
645 print_type(expression->base.type);
650 * Prints a va_copy expression.
652 * @param expression the va_copy expression
654 static void print_va_copy(const va_copy_expression_t *expression)
656 print_string("__builtin_va_copy(");
657 print_assignment_expression(expression->dst);
659 print_assignment_expression(expression->src);
664 * Prints a select expression (. or ->).
666 * @param expression the select expression
668 static void print_select(const select_expression_t *expression)
670 print_expression_prec(expression->compound, PREC_POSTFIX);
671 if (is_type_pointer(skip_typeref(expression->compound->base.type))) {
676 print_string(expression->compound_entry->base.symbol->string);
680 * Prints a type classify expression.
682 * @param expr the type classify expression
684 static void print_classify_type_expression(
685 const classify_type_expression_t *const expr)
687 print_string("__builtin_classify_type(");
688 print_assignment_expression(expr->type_expression);
693 * Prints a designator.
695 * @param designator the designator
697 static void print_designator(const designator_t *designator)
699 for ( ; designator != NULL; designator = designator->next) {
700 if (designator->symbol == NULL) {
702 print_expression(designator->array_index);
706 print_string(designator->symbol->string);
712 * Prints an offsetof expression.
714 * @param expression the offset expression
716 static void print_offsetof_expression(const offsetof_expression_t *expression)
718 print_string("__builtin_offsetof(");
719 print_type(expression->type);
721 print_designator(expression->designator);
726 * Prints a statement expression.
728 * @param expression the statement expression
730 static void print_statement_expression(const statement_expression_t *expression)
733 print_statement(expression->statement);
738 * Prints an expression with parenthesis if needed.
740 * @param expression the expression to print
741 * @param top_prec the precedence of the user of this expression.
743 static void print_expression_prec(const expression_t *expression, unsigned top_prec)
745 if (expression->kind == EXPR_UNARY_CAST_IMPLICIT && !print_implicit_casts) {
746 expression = expression->unary.value;
750 expression->base.parenthesized ||
751 (print_parenthesis && top_prec != PREC_BOTTOM) ||
752 top_prec > get_expression_precedence(expression->base.kind);
756 switch (expression->kind) {
759 print_string("$invalid expression$");
761 case EXPR_CHARACTER_CONSTANT:
762 print_character_constant(&expression->conste);
764 case EXPR_WIDE_CHARACTER_CONSTANT:
765 print_wide_character_constant(&expression->conste);
768 print_const(&expression->conste);
771 print_funcname(&expression->funcname);
773 case EXPR_STRING_LITERAL:
774 print_string_literal(&expression->string);
776 case EXPR_WIDE_STRING_LITERAL:
777 print_wide_string_literal(&expression->wide_string);
779 case EXPR_COMPOUND_LITERAL:
780 print_compound_literal(&expression->compound_literal);
783 print_call_expression(&expression->call);
786 print_binary_expression(&expression->binary);
789 case EXPR_REFERENCE_ENUM_VALUE:
790 print_reference_expression(&expression->reference);
792 case EXPR_ARRAY_ACCESS:
793 print_array_expression(&expression->array_access);
795 case EXPR_LABEL_ADDRESS:
796 print_label_address_expression(&expression->label_address);
799 print_unary_expression(&expression->unary);
803 print_typeprop_expression(&expression->typeprop);
805 case EXPR_BUILTIN_CONSTANT_P:
806 print_builtin_constant(&expression->builtin_constant);
808 case EXPR_BUILTIN_TYPES_COMPATIBLE_P:
809 print_builtin_types_compatible(&expression->builtin_types_compatible);
811 case EXPR_CONDITIONAL:
812 print_conditional(&expression->conditional);
815 print_va_start(&expression->va_starte);
818 print_va_arg(&expression->va_arge);
821 print_va_copy(&expression->va_copye);
824 print_select(&expression->select);
826 case EXPR_CLASSIFY_TYPE:
827 print_classify_type_expression(&expression->classify_type);
830 print_offsetof_expression(&expression->offsetofe);
833 print_statement_expression(&expression->statement);
838 print_format("some expression of type %d", (int)expression->kind);
846 * Print an compound statement.
848 * @param block the compound statement
850 static void print_compound_statement(const compound_statement_t *block)
855 statement_t *statement = block->statements;
856 while (statement != NULL) {
857 if (statement->base.kind == STATEMENT_CASE_LABEL)
859 if (statement->kind != STATEMENT_LABEL)
861 print_statement(statement);
863 statement = statement->base.next;
867 print_string(block->stmt_expr ? "}" : "}\n");
871 * Print a return statement.
873 * @param statement the return statement
875 static void print_return_statement(const return_statement_t *statement)
877 expression_t const *const val = statement->value;
879 print_string("return ");
880 print_expression(val);
883 print_string("return;\n");
888 * Print an expression statement.
890 * @param statement the expression statement
892 static void print_expression_statement(const expression_statement_t *statement)
894 print_expression(statement->expression);
899 * Print a goto statement.
901 * @param statement the goto statement
903 static void print_goto_statement(const goto_statement_t *statement)
905 print_string("goto ");
906 if (statement->expression != NULL) {
908 print_expression(statement->expression);
910 print_string(statement->label->base.symbol->string);
916 * Print a label statement.
918 * @param statement the label statement
920 static void print_label_statement(const label_statement_t *statement)
922 print_format("%s:\n", statement->label->base.symbol->string);
924 print_statement(statement->statement);
928 * Print an if statement.
930 * @param statement the if statement
932 static void print_if_statement(const if_statement_t *statement)
934 print_string("if (");
935 print_expression(statement->condition);
937 print_statement(statement->true_statement);
939 if (statement->false_statement != NULL) {
941 print_string("else ");
942 print_statement(statement->false_statement);
947 * Print a switch statement.
949 * @param statement the switch statement
951 static void print_switch_statement(const switch_statement_t *statement)
953 print_string("switch (");
954 print_expression(statement->expression);
956 print_statement(statement->body);
960 * Print a case label (including the default label).
962 * @param statement the case label statement
964 static void print_case_label(const case_label_statement_t *statement)
966 if (statement->expression == NULL) {
967 print_string("default:\n");
969 print_string("case ");
970 print_expression(statement->expression);
971 if (statement->end_range != NULL) {
972 print_string(" ... ");
973 print_expression(statement->end_range);
978 if (statement->statement != NULL) {
979 if (statement->statement->base.kind == STATEMENT_CASE_LABEL) {
983 print_statement(statement->statement);
987 static void print_typedef(const entity_t *entity)
989 print_string("typedef ");
990 print_type_ext(entity->typedefe.type, entity->base.symbol, NULL);
995 * returns true if the entity is a compiler generated one and has no real
996 * correspondenc in the source file
998 static bool is_generated_entity(const entity_t *entity)
1000 if (entity->kind == ENTITY_TYPEDEF)
1001 return entity->typedefe.builtin;
1003 if (is_declaration(entity))
1004 return entity->declaration.implicit;
1010 * Print a declaration statement.
1012 * @param statement the statement
1014 static void print_declaration_statement(
1015 const declaration_statement_t *statement)
1018 entity_t *entity = statement->declarations_begin;
1019 if (entity == NULL) {
1020 print_string("/* empty declaration statement */\n");
1024 entity_t *const end = statement->declarations_end->base.next;
1025 for (; entity != end; entity = entity->base.next) {
1026 if (entity->kind == ENTITY_ENUM_VALUE)
1028 if (is_generated_entity(entity))
1037 print_entity(entity);
1043 * Print a while statement.
1045 * @param statement the statement
1047 static void print_while_statement(const while_statement_t *statement)
1049 print_string("while (");
1050 print_expression(statement->condition);
1052 print_statement(statement->body);
1056 * Print a do-while statement.
1058 * @param statement the statement
1060 static void print_do_while_statement(const do_while_statement_t *statement)
1062 print_string("do ");
1063 print_statement(statement->body);
1065 print_string("while (");
1066 print_expression(statement->condition);
1067 print_string(");\n");
1071 * Print a for statement.
1073 * @param statement the statement
1075 static void print_for_statement(const for_statement_t *statement)
1077 print_string("for (");
1078 if (statement->initialisation != NULL) {
1079 print_expression(statement->initialisation);
1082 entity_t const *entity = statement->scope.entities;
1083 for (; entity != NULL; entity = entity->base.next) {
1084 if (is_generated_entity(entity))
1086 /* FIXME display of multiple declarations is wrong */
1087 print_declaration(entity);
1090 if (statement->condition != NULL) {
1092 print_expression(statement->condition);
1095 if (statement->step != NULL) {
1097 print_expression(statement->step);
1100 print_statement(statement->body);
1104 * Print assembler arguments.
1106 * @param arguments the arguments
1108 static void print_asm_arguments(asm_argument_t *arguments)
1110 asm_argument_t *argument = arguments;
1111 for (; argument != NULL; argument = argument->next) {
1112 if (argument != arguments)
1115 if (argument->symbol) {
1116 print_format("[%s] ", argument->symbol->string);
1118 print_quoted_string(&argument->constraints, '"', 1);
1120 print_expression(argument->expression);
1126 * Print assembler clobbers.
1128 * @param clobbers the clobbers
1130 static void print_asm_clobbers(asm_clobber_t *clobbers)
1132 asm_clobber_t *clobber = clobbers;
1133 for (; clobber != NULL; clobber = clobber->next) {
1134 if (clobber != clobbers)
1137 print_quoted_string(&clobber->clobber, '"', 1);
1142 * Print an assembler statement.
1144 * @param statement the statement
1146 static void print_asm_statement(const asm_statement_t *statement)
1148 print_string("asm ");
1149 if (statement->is_volatile) {
1150 print_string("volatile ");
1153 print_quoted_string(&statement->asm_text, '"', 1);
1154 if (statement->outputs == NULL &&
1155 statement->inputs == NULL &&
1156 statement->clobbers == NULL)
1157 goto end_of_print_asm_statement;
1159 print_string(" : ");
1160 print_asm_arguments(statement->outputs);
1161 if (statement->inputs == NULL && statement->clobbers == NULL)
1162 goto end_of_print_asm_statement;
1164 print_string(" : ");
1165 print_asm_arguments(statement->inputs);
1166 if (statement->clobbers == NULL)
1167 goto end_of_print_asm_statement;
1169 print_string(" : ");
1170 print_asm_clobbers(statement->clobbers);
1172 end_of_print_asm_statement:
1173 print_string(");\n");
1177 * Print a microsoft __try statement.
1179 * @param statement the statement
1181 static void print_ms_try_statement(const ms_try_statement_t *statement)
1183 print_string("__try ");
1184 print_statement(statement->try_statement);
1186 if (statement->except_expression != NULL) {
1187 print_string("__except(");
1188 print_expression(statement->except_expression);
1191 print_string("__finally ");
1193 print_statement(statement->final_statement);
1197 * Print a microsoft __leave statement.
1199 * @param statement the statement
1201 static void print_leave_statement(const leave_statement_t *statement)
1204 print_string("__leave;\n");
1208 * Print a statement.
1210 * @param statement the statement
1212 void print_statement(const statement_t *statement)
1214 switch (statement->kind) {
1215 case STATEMENT_EMPTY:
1216 print_string(";\n");
1218 case STATEMENT_COMPOUND:
1219 print_compound_statement(&statement->compound);
1221 case STATEMENT_RETURN:
1222 print_return_statement(&statement->returns);
1224 case STATEMENT_EXPRESSION:
1225 print_expression_statement(&statement->expression);
1227 case STATEMENT_LABEL:
1228 print_label_statement(&statement->label);
1230 case STATEMENT_GOTO:
1231 print_goto_statement(&statement->gotos);
1233 case STATEMENT_CONTINUE:
1234 print_string("continue;\n");
1236 case STATEMENT_BREAK:
1237 print_string("break;\n");
1240 print_if_statement(&statement->ifs);
1242 case STATEMENT_SWITCH:
1243 print_switch_statement(&statement->switchs);
1245 case STATEMENT_CASE_LABEL:
1246 print_case_label(&statement->case_label);
1248 case STATEMENT_DECLARATION:
1249 print_declaration_statement(&statement->declaration);
1251 case STATEMENT_WHILE:
1252 print_while_statement(&statement->whiles);
1254 case STATEMENT_DO_WHILE:
1255 print_do_while_statement(&statement->do_while);
1258 print_for_statement(&statement->fors);
1261 print_asm_statement(&statement->asms);
1263 case STATEMENT_MS_TRY:
1264 print_ms_try_statement(&statement->ms_try);
1266 case STATEMENT_LEAVE:
1267 print_leave_statement(&statement->leave);
1269 case STATEMENT_INVALID:
1270 print_string("$invalid statement$\n");
1276 * Print a storage class.
1278 * @param storage_class the storage class
1280 static void print_storage_class(storage_class_tag_t storage_class)
1282 switch (storage_class) {
1283 case STORAGE_CLASS_NONE: return;
1284 case STORAGE_CLASS_TYPEDEF: print_string("typedef "); return;
1285 case STORAGE_CLASS_EXTERN: print_string("extern "); return;
1286 case STORAGE_CLASS_STATIC: print_string("static "); return;
1287 case STORAGE_CLASS_AUTO: print_string("auto "); return;
1288 case STORAGE_CLASS_REGISTER: print_string("register "); return;
1290 panic("invalid storage class");
1294 * Print an initializer.
1296 * @param initializer the initializer
1298 void print_initializer(const initializer_t *initializer)
1300 if (initializer == NULL) {
1305 switch (initializer->kind) {
1306 case INITIALIZER_VALUE: {
1307 const initializer_value_t *value = &initializer->value;
1308 print_assignment_expression(value->value);
1311 case INITIALIZER_LIST: {
1312 assert(initializer->kind == INITIALIZER_LIST);
1314 const initializer_list_t *list = &initializer->list;
1316 for (size_t i = 0 ; i < list->len; ++i) {
1317 const initializer_t *sub_init = list->initializers[i];
1318 print_initializer(list->initializers[i]);
1319 if (i < list->len-1) {
1320 if (sub_init == NULL || sub_init->kind != INITIALIZER_DESIGNATOR)
1327 case INITIALIZER_STRING:
1328 print_quoted_string(&initializer->string.string, '"', 1);
1330 case INITIALIZER_WIDE_STRING:
1331 print_quoted_wide_string(&initializer->wide_string.string, '"', 1);
1333 case INITIALIZER_DESIGNATOR:
1334 print_designator(initializer->designator.designator);
1335 print_string(" = ");
1339 panic("invalid initializer kind found");
1344 * Print microsoft extended declaration modifiers.
1346 static void print_ms_modifiers(const declaration_t *declaration)
1348 if ((c_mode & _MS) == 0)
1351 decl_modifiers_t modifiers = declaration->modifiers;
1353 bool ds_shown = false;
1354 const char *next = "(";
1356 if (declaration->base.kind == ENTITY_VARIABLE) {
1357 variable_t *variable = (variable_t*)declaration;
1358 if (variable->alignment != 0
1359 || variable->get_property_sym != NULL
1360 || variable->put_property_sym != NULL) {
1362 print_string("__declspec");
1366 if (variable->alignment != 0) {
1367 print_string(next); next = ", "; print_format("align(%u)", variable->alignment);
1369 if (variable->get_property_sym != NULL
1370 || variable->put_property_sym != NULL) {
1372 print_string(next); next = ", "; print_string("property(");
1373 if (variable->get_property_sym != NULL) {
1374 print_format("get=%s", variable->get_property_sym->string);
1377 if (variable->put_property_sym != NULL)
1378 print_format("%sput=%s", comma, variable->put_property_sym->string);
1384 /* DM_FORCEINLINE handled outside. */
1385 if ((modifiers & ~DM_FORCEINLINE) != 0) {
1387 print_string("__declspec");
1390 if (modifiers & DM_DLLIMPORT) {
1391 print_string(next); next = ", "; print_string("dllimport");
1393 if (modifiers & DM_DLLEXPORT) {
1394 print_string(next); next = ", "; print_string("dllexport");
1396 if (modifiers & DM_THREAD) {
1397 print_string(next); next = ", "; print_string("thread");
1399 if (modifiers & DM_NAKED) {
1400 print_string(next); next = ", "; print_string("naked");
1402 if (modifiers & DM_THREAD) {
1403 print_string(next); next = ", "; print_string("thread");
1405 if (modifiers & DM_SELECTANY) {
1406 print_string(next); next = ", "; print_string("selectany");
1408 if (modifiers & DM_NOTHROW) {
1409 print_string(next); next = ", "; print_string("nothrow");
1411 if (modifiers & DM_NORETURN) {
1412 print_string(next); next = ", "; print_string("noreturn");
1414 if (modifiers & DM_NOINLINE) {
1415 print_string(next); next = ", "; print_string("noinline");
1417 if (modifiers & DM_DEPRECATED) {
1418 print_string(next); next = ", "; print_string("deprecated");
1419 if (declaration->deprecated_string != NULL)
1420 print_format("(\"%s\")",
1421 declaration->deprecated_string);
1423 if (modifiers & DM_RESTRICT) {
1424 print_string(next); next = ", "; print_string("restrict");
1426 if (modifiers & DM_NOALIAS) {
1427 print_string(next); next = ", "; print_string("noalias");
1436 static void print_scope(const scope_t *scope)
1438 const entity_t *entity = scope->entities;
1439 for ( ; entity != NULL; entity = entity->base.next) {
1441 print_entity(entity);
1446 static void print_namespace(const namespace_t *namespace)
1448 print_string("namespace ");
1449 if (namespace->base.symbol != NULL) {
1450 print_string(namespace->base.symbol->string);
1454 print_string("{\n");
1457 print_scope(&namespace->members);
1461 print_string("}\n");
1465 * Print a variable or function declaration
1467 void print_declaration(const entity_t *entity)
1469 assert(is_declaration(entity));
1470 const declaration_t *declaration = &entity->declaration;
1472 print_storage_class((storage_class_tag_t)declaration->declared_storage_class);
1473 if (entity->kind == ENTITY_FUNCTION) {
1474 function_t *function = (function_t*)declaration;
1475 if (function->is_inline) {
1476 if (declaration->modifiers & DM_FORCEINLINE) {
1477 print_string("__forceinline ");
1478 } else if (declaration->modifiers & DM_MICROSOFT_INLINE) {
1479 print_string("__inline ");
1481 print_string("inline ");
1485 //print_ms_modifiers(declaration);
1486 switch (entity->kind) {
1487 case ENTITY_FUNCTION:
1488 print_type_ext(entity->declaration.type, entity->base.symbol,
1489 &entity->function.parameters);
1491 if (entity->function.statement != NULL) {
1494 print_statement(entity->function.statement);
1499 case ENTITY_VARIABLE:
1500 if (entity->variable.thread_local)
1501 print_string("__thread ");
1502 print_type_ext(declaration->type, declaration->base.symbol, NULL);
1503 if (entity->variable.initializer != NULL) {
1504 print_string(" = ");
1505 print_initializer(entity->variable.initializer);
1510 print_type_ext(declaration->type, declaration->base.symbol, NULL);
1517 * Prints an expression.
1519 * @param expression the expression
1521 void print_expression(const expression_t *expression)
1523 print_expression_prec(expression, PREC_BOTTOM);
1527 * Print a declaration.
1529 * @param declaration the declaration
1531 void print_entity(const entity_t *entity)
1533 if (entity->base.namespc != NAMESPACE_NORMAL && entity->base.symbol == NULL)
1536 switch ((entity_kind_tag_t)entity->kind) {
1537 case ENTITY_VARIABLE:
1538 case ENTITY_PARAMETER:
1539 case ENTITY_COMPOUND_MEMBER:
1540 case ENTITY_FUNCTION:
1541 print_declaration(entity);
1543 case ENTITY_TYPEDEF:
1544 print_typedef(entity);
1548 print_string("class ");
1549 print_string(entity->base.symbol->string);
1550 print_string("; /* TODO */\n");
1553 print_string("struct ");
1554 print_string(entity->base.symbol->string);
1555 if (entity->structe.complete) {
1557 print_compound_definition(&entity->structe);
1562 print_string("union ");
1563 print_string(entity->base.symbol->string);
1564 if (entity->unione.complete) {
1566 print_compound_definition(&entity->unione);
1571 print_string("enum ");
1572 print_string(entity->base.symbol->string);
1574 print_enum_definition(&entity->enume);
1577 case ENTITY_NAMESPACE:
1578 print_namespace(&entity->namespacee);
1580 case ENTITY_LOCAL_LABEL:
1581 print_string("__label__ ");
1582 print_string(entity->base.symbol->string);
1586 case ENTITY_ENUM_VALUE:
1587 panic("print_entity used on unexpected entity type");
1588 case ENTITY_INVALID:
1591 panic("Invalid entity type encountered");
1595 * Print the AST of a translation unit.
1597 * @param unit the translation unit
1599 void print_ast(const translation_unit_t *unit)
1601 entity_t *entity = unit->scope.entities;
1602 for ( ; entity != NULL; entity = entity->base.next) {
1603 if (entity->kind == ENTITY_ENUM_VALUE)
1605 if (entity->base.namespc != NAMESPACE_NORMAL
1606 && entity->base.symbol == NULL)
1608 if (is_generated_entity(entity))
1612 print_entity(entity);
1617 bool is_constant_initializer(const initializer_t *initializer)
1619 switch (initializer->kind) {
1620 case INITIALIZER_STRING:
1621 case INITIALIZER_WIDE_STRING:
1622 case INITIALIZER_DESIGNATOR:
1625 case INITIALIZER_VALUE:
1626 return is_constant_expression(initializer->value.value);
1628 case INITIALIZER_LIST:
1629 for (size_t i = 0; i < initializer->list.len; ++i) {
1630 initializer_t *sub_initializer = initializer->list.initializers[i];
1631 if (!is_constant_initializer(sub_initializer))
1636 panic("invalid initializer kind found");
1639 static bool is_object_with_linker_constant_address(const expression_t *expression)
1641 switch (expression->kind) {
1642 case EXPR_UNARY_DEREFERENCE:
1643 return is_address_constant(expression->unary.value);
1646 type_t *base_type = skip_typeref(expression->select.compound->base.type);
1647 if (is_type_pointer(base_type)) {
1649 return is_address_constant(expression->select.compound);
1651 return is_object_with_linker_constant_address(expression->select.compound);
1655 case EXPR_ARRAY_ACCESS:
1656 return is_constant_expression(expression->array_access.index)
1657 && is_address_constant(expression->array_access.array_ref);
1659 case EXPR_REFERENCE: {
1660 entity_t *entity = expression->reference.entity;
1661 if (is_declaration(entity)) {
1662 switch ((storage_class_tag_t)entity->declaration.storage_class) {
1663 case STORAGE_CLASS_NONE:
1664 case STORAGE_CLASS_EXTERN:
1665 case STORAGE_CLASS_STATIC:
1667 entity->kind != ENTITY_VARIABLE ||
1668 !entity->variable.thread_local;
1670 case STORAGE_CLASS_REGISTER:
1671 case STORAGE_CLASS_TYPEDEF:
1672 case STORAGE_CLASS_AUTO:
1684 bool is_address_constant(const expression_t *expression)
1686 switch (expression->kind) {
1687 case EXPR_STRING_LITERAL:
1688 case EXPR_WIDE_STRING_LITERAL:
1690 case EXPR_LABEL_ADDRESS:
1693 case EXPR_UNARY_TAKE_ADDRESS:
1694 return is_object_with_linker_constant_address(expression->unary.value);
1696 case EXPR_UNARY_DEREFERENCE: {
1698 = revert_automatic_type_conversion(expression->unary.value);
1699 /* dereferencing a function is a NOP */
1700 if (is_type_function(real_type)) {
1701 return is_address_constant(expression->unary.value);
1706 case EXPR_UNARY_CAST: {
1707 type_t *dest = skip_typeref(expression->base.type);
1708 if (!is_type_pointer(dest) && (
1709 dest->kind != TYPE_ATOMIC ||
1710 !(get_atomic_type_flags(dest->atomic.akind) & ATOMIC_TYPE_FLAG_INTEGER) ||
1711 get_atomic_type_size(dest->atomic.akind) < get_atomic_type_size(get_intptr_kind())
1715 return (is_constant_expression(expression->unary.value)
1716 || is_address_constant(expression->unary.value));
1719 case EXPR_BINARY_ADD:
1720 case EXPR_BINARY_SUB: {
1721 expression_t *left = expression->binary.left;
1722 expression_t *right = expression->binary.right;
1724 if (is_type_pointer(skip_typeref(left->base.type))) {
1725 return is_address_constant(left) && is_constant_expression(right);
1726 } else if (is_type_pointer(skip_typeref(right->base.type))) {
1727 return is_constant_expression(left) && is_address_constant(right);
1733 case EXPR_REFERENCE: {
1734 entity_t *entity = expression->reference.entity;
1735 if (!is_declaration(entity))
1738 type_t *type = skip_typeref(entity->declaration.type);
1739 if (is_type_function(type))
1741 if (is_type_array(type)) {
1742 return is_object_with_linker_constant_address(expression);
1744 /* Prevent stray errors */
1745 if (!is_type_valid(type))
1750 case EXPR_ARRAY_ACCESS: {
1751 type_t *const type =
1752 skip_typeref(revert_automatic_type_conversion(expression));
1754 is_type_array(type) &&
1755 is_constant_expression(expression->array_access.index) &&
1756 is_address_constant(expression->array_access.array_ref);
1759 case EXPR_CONDITIONAL: {
1760 expression_t *const c = expression->conditional.condition;
1761 if (!is_constant_expression(c))
1764 if (fold_constant_to_bool(c)) {
1765 expression_t const *const t = expression->conditional.true_expression;
1766 return is_address_constant(t != NULL ? t : c);
1768 return is_address_constant(expression->conditional.false_expression);
1778 * Check if the given expression is a call to a builtin function
1779 * returning a constant result.
1781 static bool is_builtin_const_call(const expression_t *expression)
1783 expression_t *function = expression->call.function;
1784 if (function->kind != EXPR_REFERENCE)
1786 reference_expression_t *ref = &function->reference;
1787 if (ref->entity->kind != ENTITY_FUNCTION)
1790 switch (ref->entity->function.btk) {
1791 case bk_gnu_builtin_huge_val:
1792 case bk_gnu_builtin_huge_valf:
1793 case bk_gnu_builtin_huge_vall:
1794 case bk_gnu_builtin_inf:
1795 case bk_gnu_builtin_inff:
1796 case bk_gnu_builtin_infl:
1797 case bk_gnu_builtin_nan:
1798 case bk_gnu_builtin_nanf:
1799 case bk_gnu_builtin_nanl:
1807 static bool is_constant_pointer(const expression_t *expression)
1809 if (is_constant_expression(expression))
1812 switch (expression->kind) {
1813 case EXPR_UNARY_CAST:
1814 return is_constant_pointer(expression->unary.value);
1820 static bool is_object_with_constant_address(const expression_t *expression)
1822 switch (expression->kind) {
1824 expression_t *compound = expression->select.compound;
1825 type_t *compound_type = compound->base.type;
1826 compound_type = skip_typeref(compound_type);
1827 if (is_type_pointer(compound_type)) {
1828 return is_constant_pointer(compound);
1830 return is_object_with_constant_address(compound);
1834 case EXPR_ARRAY_ACCESS: {
1835 array_access_expression_t const* const array_access =
1836 &expression->array_access;
1838 is_constant_expression(array_access->index) && (
1839 is_object_with_constant_address(array_access->array_ref) ||
1840 is_constant_pointer(array_access->array_ref)
1844 case EXPR_UNARY_DEREFERENCE:
1845 return is_constant_pointer(expression->unary.value);
1851 bool is_constant_expression(const expression_t *expression)
1853 switch (expression->kind) {
1856 case EXPR_CHARACTER_CONSTANT:
1857 case EXPR_WIDE_CHARACTER_CONSTANT:
1858 case EXPR_CLASSIFY_TYPE:
1861 case EXPR_BUILTIN_CONSTANT_P:
1862 case EXPR_BUILTIN_TYPES_COMPATIBLE_P:
1863 case EXPR_REFERENCE_ENUM_VALUE:
1867 type_t *const type = skip_typeref(expression->typeprop.type);
1868 return !is_type_array(type) || !type->array.is_vla;
1871 case EXPR_STRING_LITERAL:
1872 case EXPR_WIDE_STRING_LITERAL:
1874 case EXPR_LABEL_ADDRESS:
1879 case EXPR_STATEMENT:
1880 case EXPR_REFERENCE:
1881 case EXPR_UNARY_POSTFIX_INCREMENT:
1882 case EXPR_UNARY_POSTFIX_DECREMENT:
1883 case EXPR_UNARY_PREFIX_INCREMENT:
1884 case EXPR_UNARY_PREFIX_DECREMENT:
1885 case EXPR_UNARY_ASSUME: /* has VOID type */
1886 case EXPR_UNARY_DEREFERENCE:
1887 case EXPR_UNARY_DELETE:
1888 case EXPR_UNARY_DELETE_ARRAY:
1889 case EXPR_UNARY_THROW:
1890 case EXPR_BINARY_ASSIGN:
1891 case EXPR_BINARY_MUL_ASSIGN:
1892 case EXPR_BINARY_DIV_ASSIGN:
1893 case EXPR_BINARY_MOD_ASSIGN:
1894 case EXPR_BINARY_ADD_ASSIGN:
1895 case EXPR_BINARY_SUB_ASSIGN:
1896 case EXPR_BINARY_SHIFTLEFT_ASSIGN:
1897 case EXPR_BINARY_SHIFTRIGHT_ASSIGN:
1898 case EXPR_BINARY_BITWISE_AND_ASSIGN:
1899 case EXPR_BINARY_BITWISE_XOR_ASSIGN:
1900 case EXPR_BINARY_BITWISE_OR_ASSIGN:
1901 case EXPR_BINARY_COMMA:
1902 case EXPR_ARRAY_ACCESS:
1905 case EXPR_UNARY_TAKE_ADDRESS:
1906 return is_object_with_constant_address(expression->unary.value);
1909 return is_builtin_const_call(expression);
1911 case EXPR_UNARY_NEGATE:
1912 case EXPR_UNARY_PLUS:
1913 case EXPR_UNARY_BITWISE_NEGATE:
1914 case EXPR_UNARY_NOT:
1915 return is_constant_expression(expression->unary.value);
1917 case EXPR_UNARY_CAST:
1918 case EXPR_UNARY_CAST_IMPLICIT:
1919 return is_type_arithmetic(skip_typeref(expression->base.type))
1920 && is_constant_expression(expression->unary.value);
1922 case EXPR_BINARY_ADD:
1923 case EXPR_BINARY_SUB:
1924 case EXPR_BINARY_MUL:
1925 case EXPR_BINARY_DIV:
1926 case EXPR_BINARY_MOD:
1927 case EXPR_BINARY_EQUAL:
1928 case EXPR_BINARY_NOTEQUAL:
1929 case EXPR_BINARY_LESS:
1930 case EXPR_BINARY_LESSEQUAL:
1931 case EXPR_BINARY_GREATER:
1932 case EXPR_BINARY_GREATEREQUAL:
1933 case EXPR_BINARY_BITWISE_AND:
1934 case EXPR_BINARY_BITWISE_OR:
1935 case EXPR_BINARY_BITWISE_XOR:
1936 case EXPR_BINARY_SHIFTLEFT:
1937 case EXPR_BINARY_SHIFTRIGHT:
1938 case EXPR_BINARY_ISGREATER:
1939 case EXPR_BINARY_ISGREATEREQUAL:
1940 case EXPR_BINARY_ISLESS:
1941 case EXPR_BINARY_ISLESSEQUAL:
1942 case EXPR_BINARY_ISLESSGREATER:
1943 case EXPR_BINARY_ISUNORDERED:
1944 return is_constant_expression(expression->binary.left)
1945 && is_constant_expression(expression->binary.right);
1947 case EXPR_BINARY_LOGICAL_AND: {
1948 expression_t const *const left = expression->binary.left;
1949 if (!is_constant_expression(left))
1951 if (fold_constant_to_bool(left) == false)
1953 return is_constant_expression(expression->binary.right);
1956 case EXPR_BINARY_LOGICAL_OR: {
1957 expression_t const *const left = expression->binary.left;
1958 if (!is_constant_expression(left))
1960 if (fold_constant_to_bool(left) == true)
1962 return is_constant_expression(expression->binary.right);
1965 case EXPR_COMPOUND_LITERAL:
1966 return is_constant_initializer(expression->compound_literal.initializer);
1968 case EXPR_CONDITIONAL: {
1969 expression_t *condition = expression->conditional.condition;
1970 if (!is_constant_expression(condition))
1973 if (fold_constant_to_bool(condition) == true) {
1974 expression_t const *const t = expression->conditional.true_expression;
1975 return t == NULL || is_constant_expression(t);
1977 return is_constant_expression(expression->conditional.false_expression);
1987 panic("invalid expression found (is constant expression)");
1991 * Initialize the AST construction.
1995 obstack_init(&ast_obstack);
2003 obstack_free(&ast_obstack, NULL);
2007 * Allocate an AST object of the given size.
2009 * @param size the size of the object to allocate
2011 * @return A new allocated object in the AST memeory space.
2013 void *(allocate_ast)(size_t size)
2015 return _allocate_ast(size);