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"
29 #include "separator_t.h"
37 #if defined(__INTEL_COMPILER)
39 #elif defined(__CYGWIN__)
40 #include "win32/cygwin_math_ext.h"
45 #include "adt/error.h"
48 struct obstack ast_obstack;
51 static int case_indent;
53 bool print_implicit_casts = false;
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_ERROR] = PREC_PRIMARY,
99 [EXPR_REFERENCE] = PREC_PRIMARY,
100 [EXPR_ENUM_CONSTANT] = PREC_PRIMARY,
101 [EXPR_LITERAL_INTEGER] = PREC_PRIMARY,
102 [EXPR_LITERAL_FLOATINGPOINT] = PREC_PRIMARY,
103 [EXPR_LITERAL_CHARACTER] = PREC_PRIMARY,
104 [EXPR_LITERAL_MS_NOOP] = PREC_PRIMARY,
105 [EXPR_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_CONSTANT_P] = PREC_PRIMARY,
117 [EXPR_BUILTIN_TYPES_COMPATIBLE_P] = PREC_PRIMARY,
118 [EXPR_OFFSETOF] = PREC_PRIMARY,
119 [EXPR_VA_START] = PREC_PRIMARY,
120 [EXPR_VA_ARG] = PREC_PRIMARY,
121 [EXPR_VA_COPY] = 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_COMPLEMENT] = 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_ASSUME] = PREC_PRIMARY,
137 [EXPR_UNARY_DELETE] = PREC_UNARY,
138 [EXPR_UNARY_DELETE_ARRAY] = PREC_UNARY,
139 [EXPR_UNARY_THROW] = PREC_ASSIGNMENT,
140 [EXPR_UNARY_IMAG] = PREC_UNARY,
141 [EXPR_UNARY_REAL] = PREC_UNARY,
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 quoted string constant.
191 * @param string the string constant
192 * @param border the border char
194 static void print_quoted_string(const string_t *const string, char border)
196 print_string(get_string_encoding_prefix(string->encoding));
199 const char *end = string->begin + string->size;
200 for (const char *c = string->begin; c != end; ++c) {
206 case '\\': print_string("\\\\"); break;
207 case '\a': print_string("\\a"); break;
208 case '\b': print_string("\\b"); break;
209 case '\f': print_string("\\f"); break;
210 case '\n': print_string("\\n"); break;
211 case '\r': print_string("\\r"); break;
212 case '\t': print_string("\\t"); break;
213 case '\v': print_string("\\v"); break;
214 case '\?': print_string("\\?"); break;
216 if (c_mode & _GNUC) {
217 print_string("\\e"); break;
221 if ((unsigned)tc < 0x80 && !isprint(tc)) {
222 print_format("\\%03o", (unsigned)tc);
232 static void print_string_literal(string_literal_expression_t const *const literal, char const delimiter)
234 print_quoted_string(&literal->value, delimiter);
237 static void print_literal(const literal_expression_t *literal)
239 switch (literal->base.kind) {
240 case EXPR_LITERAL_MS_NOOP:
241 print_string("__noop");
244 case EXPR_LITERAL_BOOLEAN:
245 case EXPR_LITERAL_FLOATINGPOINT:
246 case EXPR_LITERAL_INTEGER:
247 print_string(literal->value.begin);
253 print_string("INVALID LITERAL KIND");
257 * Prints a predefined symbol.
259 static void print_funcname(const funcname_expression_t *funcname)
262 switch (funcname->kind) {
263 case FUNCNAME_FUNCTION: s = (c_mode & _C99) ? "__func__" : "__FUNCTION__"; break;
264 case FUNCNAME_PRETTY_FUNCTION: s = "__PRETTY_FUNCTION__"; break;
265 case FUNCNAME_FUNCSIG: s = "__FUNCSIG__"; break;
266 case FUNCNAME_FUNCDNAME: s = "__FUNCDNAME__"; break;
271 static void print_compound_literal(
272 const compound_literal_expression_t *expression)
275 print_type(expression->type);
277 print_initializer(expression->initializer);
280 static void print_assignment_expression(const expression_t *const expr)
282 print_expression_prec(expr, PREC_ASSIGNMENT);
286 * Prints a call expression.
288 * @param call the call expression
290 static void print_call_expression(const call_expression_t *call)
292 print_expression_prec(call->function, PREC_POSTFIX);
294 separator_t sep = { "", ", " };
295 for (call_argument_t const *arg = call->arguments; arg; arg = arg->next) {
296 print_string(sep_next(&sep));
297 print_assignment_expression(arg->expression);
303 * Prints a binary expression.
305 * @param binexpr the binary expression
307 static void print_binary_expression(const binary_expression_t *binexpr)
309 unsigned prec = get_expression_precedence(binexpr->base.kind);
310 int r2l = right_to_left(prec);
312 print_expression_prec(binexpr->left, prec + r2l);
314 switch (binexpr->base.kind) {
315 case EXPR_BINARY_COMMA: op = ", "; break;
316 case EXPR_BINARY_ASSIGN: op = " = "; break;
317 case EXPR_BINARY_ADD: op = " + "; break;
318 case EXPR_BINARY_SUB: op = " - "; break;
319 case EXPR_BINARY_MUL: op = " * "; break;
320 case EXPR_BINARY_MOD: op = " % "; break;
321 case EXPR_BINARY_DIV: op = " / "; break;
322 case EXPR_BINARY_BITWISE_OR: op = " | "; break;
323 case EXPR_BINARY_BITWISE_AND: op = " & "; break;
324 case EXPR_BINARY_BITWISE_XOR: op = " ^ "; break;
325 case EXPR_BINARY_LOGICAL_OR: op = " || "; break;
326 case EXPR_BINARY_LOGICAL_AND: op = " && "; break;
327 case EXPR_BINARY_NOTEQUAL: op = " != "; break;
328 case EXPR_BINARY_EQUAL: op = " == "; break;
329 case EXPR_BINARY_LESS: op = " < "; break;
330 case EXPR_BINARY_LESSEQUAL: op = " <= "; break;
331 case EXPR_BINARY_GREATER: op = " > "; break;
332 case EXPR_BINARY_GREATEREQUAL: op = " >= "; break;
333 case EXPR_BINARY_SHIFTLEFT: op = " << "; break;
334 case EXPR_BINARY_SHIFTRIGHT: op = " >> "; break;
336 case EXPR_BINARY_ADD_ASSIGN: op = " += "; break;
337 case EXPR_BINARY_SUB_ASSIGN: op = " -= "; break;
338 case EXPR_BINARY_MUL_ASSIGN: op = " *= "; break;
339 case EXPR_BINARY_MOD_ASSIGN: op = " %= "; break;
340 case EXPR_BINARY_DIV_ASSIGN: op = " /= "; break;
341 case EXPR_BINARY_BITWISE_OR_ASSIGN: op = " |= "; break;
342 case EXPR_BINARY_BITWISE_AND_ASSIGN: op = " &= "; break;
343 case EXPR_BINARY_BITWISE_XOR_ASSIGN: op = " ^= "; break;
344 case EXPR_BINARY_SHIFTLEFT_ASSIGN: op = " <<= "; break;
345 case EXPR_BINARY_SHIFTRIGHT_ASSIGN: op = " >>= "; break;
346 default: panic("invalid binexpression found");
349 print_expression_prec(binexpr->right, prec + 1 - r2l);
353 * Prints an unary expression.
355 * @param unexpr the unary expression
357 static void print_unary_expression(const unary_expression_t *unexpr)
359 unsigned prec = get_expression_precedence(unexpr->base.kind);
360 switch (unexpr->base.kind) {
361 case EXPR_UNARY_NEGATE: print_char ('-' ); break;
362 case EXPR_UNARY_PLUS: print_char ('+' ); break;
363 case EXPR_UNARY_NOT: print_char ('!' ); break;
364 case EXPR_UNARY_COMPLEMENT: print_char ('~' ); break;
365 case EXPR_UNARY_PREFIX_INCREMENT: print_string("++"); break;
366 case EXPR_UNARY_PREFIX_DECREMENT: print_string("--"); break;
367 case EXPR_UNARY_DEREFERENCE: print_char ('*' ); break;
368 case EXPR_UNARY_TAKE_ADDRESS: print_char ('&' ); break;
369 case EXPR_UNARY_DELETE: print_string("delete "); break;
370 case EXPR_UNARY_DELETE_ARRAY: print_string("delete [] "); break;
371 case EXPR_UNARY_REAL: print_string("__real__ "); break;
372 case EXPR_UNARY_IMAG: print_string("__imag__ "); break;
374 case EXPR_UNARY_POSTFIX_INCREMENT:
375 print_expression_prec(unexpr->value, prec);
378 case EXPR_UNARY_POSTFIX_DECREMENT:
379 print_expression_prec(unexpr->value, prec);
382 case EXPR_UNARY_CAST:
384 print_type(unexpr->base.type);
387 case EXPR_UNARY_ASSUME:
388 print_string("__assume(");
389 print_assignment_expression(unexpr->value);
392 case EXPR_UNARY_THROW:
393 if (unexpr->value == NULL) {
394 print_string("throw");
397 print_string("throw ");
401 panic("invalid unary expression found");
403 print_expression_prec(unexpr->value, prec);
407 * Prints a reference expression.
409 * @param ref the reference expression
411 static void print_reference_expression(const reference_expression_t *ref)
413 print_string(ref->entity->base.symbol->string);
417 * Prints a label address expression.
419 * @param ref the reference expression
421 static void print_label_address_expression(const label_address_expression_t *le)
423 print_format("&&%s", le->label->base.symbol->string);
427 * Prints an array expression.
429 * @param expression the array expression
431 static void print_array_expression(const array_access_expression_t *expression)
433 if (!expression->flipped) {
434 print_expression_prec(expression->array_ref, PREC_POSTFIX);
436 print_expression(expression->index);
439 print_expression_prec(expression->index, PREC_POSTFIX);
441 print_expression(expression->array_ref);
447 * Prints a typeproperty expression (sizeof or __alignof__).
449 * @param expression the type property expression
451 static void print_typeprop_expression(const typeprop_expression_t *expression)
453 switch (expression->base.kind) {
454 case EXPR_SIZEOF: print_string("sizeof"); break;
455 case EXPR_ALIGNOF: print_string(c_mode & _C11 ? "_Alignof" : "__alignof__"); break;
456 default: panic("invalid typeprop kind");
458 if (expression->tp_expression != NULL) {
459 /* PREC_TOP: always print the '()' here, sizeof x is right but unusual */
460 print_expression_prec(expression->tp_expression, PREC_TOP);
463 print_type(expression->type);
469 * Prints a builtin constant expression.
471 * @param expression the builtin constant expression
473 static void print_builtin_constant(const builtin_constant_expression_t *expression)
475 print_string("__builtin_constant_p(");
476 print_assignment_expression(expression->value);
481 * Prints a builtin types compatible expression.
483 * @param expression the builtin types compatible expression
485 static void print_builtin_types_compatible(
486 const builtin_types_compatible_expression_t *expression)
488 print_string("__builtin_types_compatible_p(");
489 print_type(expression->left);
491 print_type(expression->right);
496 * Prints a conditional expression.
498 * @param expression the conditional expression
500 static void print_conditional(const conditional_expression_t *expression)
502 print_expression_prec(expression->condition, PREC_LOGICAL_OR);
503 if (expression->true_expression != NULL) {
505 print_expression_prec(expression->true_expression, PREC_EXPRESSION);
508 print_string(" ?: ");
510 precedence_t prec = c_mode & _CXX ? PREC_ASSIGNMENT : PREC_CONDITIONAL;
511 print_expression_prec(expression->false_expression, prec);
515 * Prints a va_start expression.
517 * @param expression the va_start expression
519 static void print_va_start(const va_start_expression_t *const expression)
521 print_string("__builtin_va_start(");
522 print_assignment_expression(expression->ap);
524 print_assignment_expression(expression->parameter);
529 * Prints a va_arg expression.
531 * @param expression the va_arg expression
533 static void print_va_arg(const va_arg_expression_t *expression)
535 print_string("__builtin_va_arg(");
536 print_assignment_expression(expression->ap);
538 print_type(expression->base.type);
543 * Prints a va_copy expression.
545 * @param expression the va_copy expression
547 static void print_va_copy(const va_copy_expression_t *expression)
549 print_string("__builtin_va_copy(");
550 print_assignment_expression(expression->dst);
552 print_assignment_expression(expression->src);
557 * Prints a select expression (. or ->).
559 * @param expression the select expression
561 static void print_select(const select_expression_t *expression)
563 print_expression_prec(expression->compound, PREC_POSTFIX);
564 /* do not print anything for anonymous struct/union selects
565 * FIXME: if the anonymous select was a '->' this will print '.'
567 if (expression->compound_entry->base.symbol == NULL)
570 if (is_type_pointer(skip_typeref(expression->compound->base.type))) {
575 print_string(expression->compound_entry->base.symbol->string);
579 * Prints a type classify expression.
581 * @param expr the type classify expression
583 static void print_classify_type_expression(
584 const classify_type_expression_t *const expr)
586 print_string("__builtin_classify_type(");
587 print_assignment_expression(expr->type_expression);
592 * Prints a designator.
594 * @param designator the designator
596 static void print_designator(const designator_t *designator)
598 for ( ; designator != NULL; designator = designator->next) {
599 if (designator->symbol == NULL) {
601 print_expression(designator->array_index);
605 print_string(designator->symbol->string);
611 * Prints an offsetof expression.
613 * @param expression the offset expression
615 static void print_offsetof_expression(const offsetof_expression_t *expression)
617 print_string("__builtin_offsetof(");
618 print_type(expression->type);
620 print_designator(expression->designator);
625 * Prints a statement expression.
627 * @param expression the statement expression
629 static void print_statement_expression(const statement_expression_t *expression)
632 print_statement(expression->statement);
636 static bool needs_parentheses(expression_t const *const expr, unsigned const top_prec)
638 if (expr->base.parenthesized)
641 if (top_prec > get_expression_precedence(expr->base.kind))
644 if (print_parenthesis && top_prec != PREC_BOTTOM) {
645 switch (expr->kind) {
646 case EXPR_ENUM_CONSTANT:
648 case EXPR_LITERAL_CASES:
649 case EXPR_LITERAL_CHARACTER:
651 case EXPR_STRING_LITERAL:
652 /* Do not print () around subexpressions consisting of a single token. */
664 * Prints an expression with parenthesis if needed.
666 * @param expression the expression to print
667 * @param top_prec the precedence of the user of this expression.
669 static void print_expression_prec(expression_t const *expr, unsigned const top_prec)
671 if (expr->kind == EXPR_UNARY_CAST && expr->base.implicit && !print_implicit_casts) {
672 expr = expr->unary.value;
675 bool const parenthesized = needs_parentheses(expr, top_prec);
679 switch (expr->kind) {
681 case EXPR_SIZEOF: print_typeprop_expression( &expr->typeprop); break;
682 case EXPR_ARRAY_ACCESS: print_array_expression( &expr->array_access); break;
683 case EXPR_BINARY_CASES: print_binary_expression( &expr->binary); break;
684 case EXPR_BUILTIN_CONSTANT_P: print_builtin_constant( &expr->builtin_constant); break;
685 case EXPR_BUILTIN_TYPES_COMPATIBLE_P: print_builtin_types_compatible(&expr->builtin_types_compatible); break;
686 case EXPR_CALL: print_call_expression( &expr->call); break;
687 case EXPR_CLASSIFY_TYPE: print_classify_type_expression(&expr->classify_type); break;
688 case EXPR_COMPOUND_LITERAL: print_compound_literal( &expr->compound_literal); break;
689 case EXPR_CONDITIONAL: print_conditional( &expr->conditional); break;
690 case EXPR_ERROR: print_string("$error$"); break;
691 case EXPR_FUNCNAME: print_funcname( &expr->funcname); break;
692 case EXPR_LABEL_ADDRESS: print_label_address_expression(&expr->label_address); break;
693 case EXPR_LITERAL_CASES: print_literal( &expr->literal); break;
694 case EXPR_LITERAL_CHARACTER: print_string_literal( &expr->string_literal, '\''); break;
695 case EXPR_OFFSETOF: print_offsetof_expression( &expr->offsetofe); break;
697 case EXPR_ENUM_CONSTANT: print_reference_expression( &expr->reference); break;
698 case EXPR_SELECT: print_select( &expr->select); break;
699 case EXPR_STATEMENT: print_statement_expression( &expr->statement); break;
700 case EXPR_STRING_LITERAL: print_string_literal( &expr->string_literal, '"'); break;
701 case EXPR_UNARY_CASES: print_unary_expression( &expr->unary); break;
702 case EXPR_VA_ARG: print_va_arg( &expr->va_arge); break;
703 case EXPR_VA_COPY: print_va_copy( &expr->va_copye); break;
704 case EXPR_VA_START: print_va_start( &expr->va_starte); break;
710 static void print_indented_statement(statement_t const *const stmt)
712 switch (stmt->kind) {
713 case STATEMENT_LABEL:
716 case STATEMENT_CASE_LABEL:
717 for (int i = 0; i != case_indent; ++i)
725 print_statement(stmt);
729 * Print an compound statement.
731 * @param block the compound statement
733 static void print_compound_statement(const compound_statement_t *block)
738 for (statement_t const *stmt = block->statements; stmt; stmt = stmt->base.next) {
739 print_indented_statement(stmt);
749 * Print a return statement.
751 * @param statement the return statement
753 static void print_return_statement(const return_statement_t *statement)
755 expression_t const *const val = statement->value;
757 print_string("return ");
758 print_expression(val);
761 print_string("return;");
766 * Print an expression statement.
768 * @param statement the expression statement
770 static void print_expression_statement(const expression_statement_t *statement)
772 print_expression(statement->expression);
777 * Print a computed goto statement.
779 * @param statement the computed goto statement
781 static void print_computed_goto_statement(computed_goto_statement_t const *const stmt)
783 print_string("goto *");
784 print_expression(stmt->expression);
789 * Print a goto statement.
791 * @param statement the goto statement
793 static void print_goto_statement(const goto_statement_t *statement)
795 print_string("goto ");
796 print_string(statement->label->base.symbol->string);
801 * Print a label statement.
803 * @param statement the label statement
805 static void print_label_statement(const label_statement_t *statement)
807 print_format("%s:\n", statement->label->base.symbol->string);
808 print_indented_statement(statement->statement);
811 static void print_inner_statement(statement_t const *const stmt)
813 if (stmt->kind == STATEMENT_COMPOUND) {
815 print_compound_statement(&stmt->compound);
819 print_indented_statement(stmt);
824 static void print_after_inner_statement(statement_t const *const stmt)
826 if (stmt->kind == STATEMENT_COMPOUND) {
835 * Print an if statement.
837 * @param statement the if statement
839 static void print_if_statement(const if_statement_t *statement)
841 print_string("if (");
842 print_expression(statement->condition);
844 print_inner_statement(statement->true_statement);
846 statement_t const *const f = statement->false_statement;
848 print_after_inner_statement(statement->true_statement);
849 print_string("else");
850 if (f->kind == STATEMENT_IF) {
852 print_if_statement(&f->ifs);
854 print_inner_statement(f);
860 * Print a switch statement.
862 * @param statement the switch statement
864 static void print_switch_statement(const switch_statement_t *statement)
866 int const old_case_indent = case_indent;
867 case_indent = indent;
869 print_string("switch (");
870 print_expression(statement->expression);
872 print_inner_statement(statement->body);
874 case_indent = old_case_indent;
878 * Print a case label (including the default label).
880 * @param statement the case label statement
882 static void print_case_label(const case_label_statement_t *statement)
884 if (statement->expression == NULL) {
885 print_string("default:\n");
887 print_string("case ");
888 print_expression(statement->expression);
889 if (statement->end_range != NULL) {
890 print_string(" ... ");
891 print_expression(statement->end_range);
895 print_indented_statement(statement->statement);
898 static void print_typedef(const entity_t *entity)
900 print_string("typedef ");
901 print_type_ext(entity->typedefe.type, entity->base.symbol, NULL);
906 * returns true if the entity is a compiler generated one and has no real
907 * correspondenc in the source file
909 static bool is_generated_entity(const entity_t *entity)
911 if (entity->kind == ENTITY_TYPEDEF)
912 return entity->typedefe.builtin;
914 if (is_declaration(entity))
915 return entity->declaration.implicit;
921 * Print a declaration statement.
923 * @param statement the statement
925 static void print_declaration_statement(
926 const declaration_statement_t *statement)
929 entity_t *entity = statement->declarations_begin;
930 if (entity == NULL) {
931 print_string("/* empty declaration statement */");
935 entity_t *const end = statement->declarations_end->base.next;
936 for (; entity != end; entity = entity->base.next) {
937 if (entity->kind == ENTITY_ENUM_VALUE)
939 if (is_generated_entity(entity))
949 print_entity(entity);
954 * Print a do-while statement.
956 * @param statement the statement
958 static void print_do_while_statement(const do_while_statement_t *statement)
961 print_inner_statement(statement->body);
962 print_after_inner_statement(statement->body);
963 print_string("while (");
964 print_expression(statement->condition);
969 * Print a for statement.
971 * @param statement the statement
973 static void print_for_statement(const for_statement_t *statement)
975 if (statement->initialisation || statement->scope.entities || !statement->condition || statement->step) {
976 print_string("for (");
977 if (statement->initialisation != NULL) {
978 print_expression(statement->initialisation);
981 entity_t const *entity = statement->scope.entities;
982 for (; entity != NULL; entity = entity->base.next) {
983 if (is_generated_entity(entity))
985 /* FIXME display of multiple declarations is wrong */
986 print_declaration(entity);
989 if (statement->condition != NULL) {
991 print_expression(statement->condition);
994 if (statement->step != NULL) {
996 print_expression(statement->step);
999 print_string("while (");
1000 print_expression(statement->condition);
1003 print_inner_statement(statement->body);
1007 * Print assembler arguments.
1009 * @param arguments the arguments
1011 static void print_asm_arguments(asm_argument_t const *const arguments)
1014 separator_t sep = { " ", ", " };
1015 for (asm_argument_t const *i = arguments; i; i = i->next) {
1016 print_string(sep_next(&sep));
1018 print_format("[%s] ", i->symbol->string);
1019 print_quoted_string(&i->constraints, '"');
1021 print_expression(i->expression);
1027 * Print assembler clobbers.
1029 * @param clobbers the clobbers
1031 static void print_asm_clobbers(asm_clobber_t const *const clobbers)
1034 separator_t sep = { " ", ", " };
1035 for (asm_clobber_t const *i = clobbers; i; i = i->next) {
1036 print_string(sep_next(&sep));
1037 print_quoted_string(&i->clobber, '"');
1041 static void print_asm_labels(asm_label_t const *const labels)
1044 separator_t sep = { " ", ", " };
1045 for (asm_label_t const *i = labels; i; i = i->next) {
1046 print_string(sep_next(&sep));
1047 print_string(i->label->base.symbol->string);
1052 * Print an assembler statement.
1054 * @param stmt the statement
1056 static void print_asm_statement(asm_statement_t const *const stmt)
1058 print_string("asm");
1059 if (stmt->is_volatile) print_string(" volatile");
1060 if (stmt->labels) print_string(" goto");
1062 print_quoted_string(&stmt->asm_text, '"');
1066 stmt->clobbers ? 3 :
1070 if (n >= 1) print_asm_arguments(stmt->outputs);
1071 if (n >= 2) print_asm_arguments(stmt->inputs);
1072 if (n >= 3) print_asm_clobbers( stmt->clobbers);
1073 if (n >= 4) print_asm_labels( stmt->labels);
1079 * Print a microsoft __try statement.
1081 * @param statement the statement
1083 static void print_ms_try_statement(const ms_try_statement_t *statement)
1085 print_string("__try");
1086 print_inner_statement(statement->try_statement);
1087 print_after_inner_statement(statement->try_statement);
1088 if (statement->except_expression != NULL) {
1089 print_string("__except(");
1090 print_expression(statement->except_expression);
1093 print_string("__finally");
1095 print_inner_statement(statement->final_statement);
1099 * Print a microsoft __leave statement.
1101 * @param statement the statement
1103 static void print_leave_statement(const leave_statement_t *statement)
1106 print_string("__leave;");
1110 * Print a statement.
1112 * @param statement the statement
1114 void print_statement(statement_t const *const stmt)
1116 switch (stmt->kind) {
1117 case STATEMENT_ASM: print_asm_statement( &stmt->asms); break;
1118 case STATEMENT_BREAK: print_string("break;"); break;
1119 case STATEMENT_CASE_LABEL: print_case_label( &stmt->case_label); break;
1120 case STATEMENT_COMPOUND: print_compound_statement( &stmt->compound); break;
1121 case STATEMENT_COMPUTED_GOTO: print_computed_goto_statement(&stmt->computed_goto); break;
1122 case STATEMENT_CONTINUE: print_string("continue;"); break;
1123 case STATEMENT_DECLARATION: print_declaration_statement( &stmt->declaration); break;
1124 case STATEMENT_DO_WHILE: print_do_while_statement( &stmt->do_while); break;
1125 case STATEMENT_EMPTY: print_char(';'); break;
1126 case STATEMENT_ERROR: print_string("$error statement$"); break;
1127 case STATEMENT_EXPRESSION: print_expression_statement( &stmt->expression); break;
1128 case STATEMENT_FOR: print_for_statement( &stmt->fors); break;
1129 case STATEMENT_GOTO: print_goto_statement( &stmt->gotos); break;
1130 case STATEMENT_IF: print_if_statement( &stmt->ifs); break;
1131 case STATEMENT_LABEL: print_label_statement( &stmt->label); break;
1132 case STATEMENT_LEAVE: print_leave_statement( &stmt->leave); break;
1133 case STATEMENT_MS_TRY: print_ms_try_statement( &stmt->ms_try); break;
1134 case STATEMENT_RETURN: print_return_statement( &stmt->returns); break;
1135 case STATEMENT_SWITCH: print_switch_statement( &stmt->switchs); break;
1140 * Print a storage class.
1142 * @param storage_class the storage class
1144 static void print_storage_class(storage_class_tag_t storage_class)
1146 switch (storage_class) {
1147 case STORAGE_CLASS_NONE: return;
1148 case STORAGE_CLASS_TYPEDEF: print_string("typedef "); return;
1149 case STORAGE_CLASS_EXTERN: print_string("extern "); return;
1150 case STORAGE_CLASS_STATIC: print_string("static "); return;
1151 case STORAGE_CLASS_AUTO: print_string("auto "); return;
1152 case STORAGE_CLASS_REGISTER: print_string("register "); return;
1154 panic("invalid storage class");
1158 * Print an initializer.
1160 * @param initializer the initializer
1162 void print_initializer(const initializer_t *initializer)
1164 if (initializer == NULL) {
1169 switch (initializer->kind) {
1170 case INITIALIZER_STRING:
1171 case INITIALIZER_VALUE:
1172 print_assignment_expression(initializer->value.value);
1175 case INITIALIZER_LIST: {
1177 const initializer_list_t *list = &initializer->list;
1179 for (size_t i = 0 ; i < list->len; ++i) {
1180 const initializer_t *sub_init = list->initializers[i];
1181 print_initializer(list->initializers[i]);
1182 if (i < list->len-1) {
1183 if (sub_init == NULL || sub_init->kind != INITIALIZER_DESIGNATOR)
1191 case INITIALIZER_DESIGNATOR:
1192 print_designator(initializer->designator.designator);
1193 print_string(" = ");
1197 panic("invalid initializer kind found");
1202 * Print microsoft extended declaration modifiers.
1204 static void print_ms_modifiers(const declaration_t *declaration)
1206 if ((c_mode & _MS) == 0)
1209 decl_modifiers_t modifiers = declaration->modifiers;
1211 separator_t sep = { "__declspec(", ", " };
1213 if (declaration->base.kind == ENTITY_VARIABLE) {
1214 variable_t *variable = (variable_t*)declaration;
1215 if (variable->alignment != 0
1216 || variable->get_property_sym != NULL
1217 || variable->put_property_sym != NULL) {
1218 if (variable->alignment != 0) {
1219 print_format("%salign(%u)", sep_next(&sep), variable->alignment);
1221 if (variable->get_property_sym != NULL
1222 || variable->put_property_sym != NULL) {
1224 print_format("%sproperty(", sep_next(&sep));
1225 if (variable->get_property_sym != NULL) {
1226 print_format("get=%s", variable->get_property_sym->string);
1229 if (variable->put_property_sym != NULL)
1230 print_format("%sput=%s", comma, variable->put_property_sym->string);
1236 /* DM_FORCEINLINE handled outside. */
1237 if ((modifiers & ~DM_FORCEINLINE) != 0) {
1238 if (modifiers & DM_DLLIMPORT) {
1239 print_format("%sdllimport", sep_next(&sep));
1241 if (modifiers & DM_DLLEXPORT) {
1242 print_format("%sdllexport", sep_next(&sep));
1244 if (modifiers & DM_THREAD) {
1245 print_format("%sthread", sep_next(&sep));
1247 if (modifiers & DM_NAKED) {
1248 print_format("%snaked", sep_next(&sep));
1250 if (modifiers & DM_THREAD) {
1251 print_format("%sthread", sep_next(&sep));
1253 if (modifiers & DM_SELECTANY) {
1254 print_format("%sselectany", sep_next(&sep));
1256 if (modifiers & DM_NOTHROW) {
1257 print_format("%snothrow", sep_next(&sep));
1259 if (modifiers & DM_NORETURN) {
1260 print_format("%snoreturn", sep_next(&sep));
1262 if (modifiers & DM_NOINLINE) {
1263 print_format("%snoinline", sep_next(&sep));
1265 if (modifiers & DM_DEPRECATED) {
1266 print_format("%sdeprecated", sep_next(&sep));
1267 if (declaration->deprecated_string != NULL)
1268 print_format("(\"%s\")",
1269 declaration->deprecated_string);
1271 if (modifiers & DM_RESTRICT) {
1272 print_format("%srestrict", sep_next(&sep));
1274 if (modifiers & DM_NOALIAS) {
1275 print_format("%snoalias", sep_next(&sep));
1279 if (!sep_at_first(&sep))
1284 static void print_scope(const scope_t *scope)
1286 const entity_t *entity = scope->entities;
1287 for ( ; entity != NULL; entity = entity->base.next) {
1289 print_entity(entity);
1294 static void print_namespace(const namespace_t *namespace)
1296 print_string("namespace ");
1297 if (namespace->base.symbol != NULL) {
1298 print_string(namespace->base.symbol->string);
1302 print_string("{\n");
1305 print_scope(&namespace->members);
1309 print_string("}\n");
1313 * Print a variable or function declaration
1315 void print_declaration(const entity_t *entity)
1317 assert(is_declaration(entity));
1318 const declaration_t *declaration = &entity->declaration;
1320 print_storage_class((storage_class_tag_t)declaration->declared_storage_class);
1321 if (entity->kind == ENTITY_FUNCTION) {
1322 function_t *function = (function_t*)declaration;
1323 if (function->is_inline) {
1324 if (declaration->modifiers & DM_FORCEINLINE) {
1325 print_string("__forceinline ");
1326 } else if (declaration->modifiers & DM_MICROSOFT_INLINE) {
1327 print_string("__inline ");
1329 print_string("inline ");
1333 //print_ms_modifiers(declaration);
1334 switch (entity->kind) {
1335 case ENTITY_FUNCTION:
1336 print_type_ext(entity->declaration.type, entity->base.symbol,
1337 &entity->function.parameters);
1339 if (entity->function.body != NULL) {
1341 print_indented_statement(entity->function.body);
1347 case ENTITY_VARIABLE:
1348 if (entity->variable.thread_local)
1349 print_string(c_mode & _C11 ? "_Thread_local " : "__thread ");
1350 print_type_ext(declaration->type, declaration->base.symbol, NULL);
1351 if (entity->variable.initializer != NULL) {
1352 print_string(" = ");
1353 print_initializer(entity->variable.initializer);
1357 case ENTITY_COMPOUND_MEMBER:
1358 print_type_ext(declaration->type, declaration->base.symbol, NULL);
1359 if (entity->compound_member.bitfield) {
1360 print_format(" : %u", entity->compound_member.bit_size);
1365 print_type_ext(declaration->type, declaration->base.symbol, NULL);
1372 * Prints an expression.
1374 * @param expression the expression
1376 void print_expression(const expression_t *expression)
1378 print_expression_prec(expression, PREC_BOTTOM);
1382 * Print a declaration.
1384 * @param declaration the declaration
1386 void print_entity(const entity_t *entity)
1388 if (entity->base.namespc != NAMESPACE_NORMAL && entity->base.symbol == NULL)
1391 switch ((entity_kind_tag_t)entity->kind) {
1392 case ENTITY_VARIABLE:
1393 case ENTITY_PARAMETER:
1394 case ENTITY_COMPOUND_MEMBER:
1395 case ENTITY_FUNCTION:
1396 print_declaration(entity);
1398 case ENTITY_TYPEDEF:
1399 print_typedef(entity);
1403 print_string("class ");
1404 print_string(entity->base.symbol->string);
1405 print_string("; /* TODO */\n");
1408 print_string("struct ");
1409 goto print_compound;
1411 print_string("union ");
1413 print_string(entity->base.symbol->string);
1414 if (entity->compound.complete) {
1416 print_compound_definition(&entity->compound);
1421 print_string("enum ");
1422 print_string(entity->base.symbol->string);
1424 print_enum_definition(&entity->enume);
1427 case ENTITY_NAMESPACE:
1428 print_namespace(&entity->namespacee);
1430 case ENTITY_LOCAL_LABEL:
1431 print_string("__label__ ");
1432 print_string(entity->base.symbol->string);
1436 case ENTITY_ENUM_VALUE:
1437 panic("print_entity used on unexpected entity type");
1439 panic("Invalid entity type encountered");
1443 * Print the AST of a translation unit.
1445 * @param unit the translation unit
1447 void print_ast(const translation_unit_t *unit)
1449 entity_t *entity = unit->scope.entities;
1450 for ( ; entity != NULL; entity = entity->base.next) {
1451 if (entity->kind == ENTITY_ENUM_VALUE)
1453 if (entity->base.namespc != NAMESPACE_NORMAL
1454 && entity->base.symbol == NULL)
1456 if (is_generated_entity(entity))
1460 print_entity(entity);
1465 expression_classification_t is_constant_initializer(const initializer_t *initializer)
1467 switch (initializer->kind) {
1468 case INITIALIZER_STRING:
1469 case INITIALIZER_DESIGNATOR:
1470 return EXPR_CLASS_CONSTANT;
1472 case INITIALIZER_VALUE:
1473 return is_linker_constant(initializer->value.value);
1475 case INITIALIZER_LIST: {
1476 expression_classification_t all = EXPR_CLASS_CONSTANT;
1477 for (size_t i = 0; i < initializer->list.len; ++i) {
1478 initializer_t *sub_initializer = initializer->list.initializers[i];
1479 expression_classification_t const cur = is_constant_initializer(sub_initializer);
1487 panic("invalid initializer kind found");
1491 * Checks if an expression references an object with a constant/known location
1492 * to the linker. Example:
1493 * - "x", "*&x" with x being a global variable. The value of x need not be
1494 * constant but the address of x is.
1495 * - "a.b.c" when a has a constant/known location to the linker
1497 static expression_classification_t is_object_with_linker_constant_address(
1498 const expression_t *expression)
1500 switch (expression->kind) {
1501 case EXPR_UNARY_DEREFERENCE:
1502 return is_linker_constant(expression->unary.value);
1504 case EXPR_COMPOUND_LITERAL: {
1505 const compound_literal_expression_t *literal
1506 = &expression->compound_literal;
1507 return literal->global_scope ||
1508 ((literal->type->base.qualifiers & TYPE_QUALIFIER_CONST)
1509 && is_constant_initializer(literal->initializer));
1513 type_t *base_type = skip_typeref(expression->select.compound->base.type);
1514 if (is_type_pointer(base_type)) {
1516 return is_linker_constant(expression->select.compound);
1518 return is_object_with_linker_constant_address(expression->select.compound);
1522 case EXPR_ARRAY_ACCESS: {
1523 expression_classification_t const ref = is_linker_constant(expression->array_access.array_ref);
1524 expression_classification_t const idx = is_constant_expression(expression->array_access.index);
1525 return ref < idx ? ref : idx;
1528 case EXPR_REFERENCE: {
1529 entity_t *entity = expression->reference.entity;
1530 if (!is_declaration(entity))
1531 return EXPR_CLASS_VARIABLE;
1533 switch ((storage_class_tag_t)entity->declaration.storage_class) {
1534 case STORAGE_CLASS_NONE:
1535 case STORAGE_CLASS_EXTERN:
1536 case STORAGE_CLASS_STATIC:
1538 entity->kind != ENTITY_VARIABLE ||
1539 !entity->variable.thread_local ? EXPR_CLASS_CONSTANT :
1540 EXPR_CLASS_VARIABLE;
1542 case STORAGE_CLASS_REGISTER:
1543 case STORAGE_CLASS_TYPEDEF:
1544 case STORAGE_CLASS_AUTO:
1547 return EXPR_CLASS_VARIABLE;
1551 return EXPR_CLASS_ERROR;
1554 return EXPR_CLASS_VARIABLE;
1558 expression_classification_t is_linker_constant(const expression_t *expression)
1560 switch (expression->kind) {
1561 case EXPR_STRING_LITERAL:
1563 case EXPR_LABEL_ADDRESS:
1564 return EXPR_CLASS_CONSTANT;
1566 case EXPR_COMPOUND_LITERAL:
1567 return is_constant_initializer(expression->compound_literal.initializer);
1569 case EXPR_UNARY_TAKE_ADDRESS:
1570 return is_object_with_linker_constant_address(expression->unary.value);
1572 case EXPR_UNARY_DEREFERENCE: {
1574 = revert_automatic_type_conversion(expression->unary.value);
1575 /* dereferencing a function is a NOP */
1576 if (is_type_function(real_type)) {
1577 return is_linker_constant(expression->unary.value);
1582 case EXPR_UNARY_CAST: {
1583 type_t *dest = skip_typeref(expression->base.type);
1584 if (!is_type_pointer(dest) && (
1585 dest->kind != TYPE_ATOMIC ||
1586 !(get_atomic_type_flags(dest->atomic.akind) & ATOMIC_TYPE_FLAG_INTEGER) ||
1587 get_atomic_type_size(dest->atomic.akind) < get_type_size(type_void_ptr)
1589 return is_constant_expression(expression);
1591 return is_linker_constant(expression->unary.value);
1594 case EXPR_BINARY_ADD:
1595 case EXPR_BINARY_SUB: {
1596 expression_t *const left = expression->binary.left;
1597 expression_t *const right = expression->binary.right;
1598 type_t *const ltype = skip_typeref(left->base.type);
1599 type_t *const rtype = skip_typeref(right->base.type);
1601 if (is_type_pointer(ltype)) {
1602 expression_classification_t const l = is_linker_constant(left);
1603 expression_classification_t const r = is_constant_expression(right);
1604 return l < r ? l : r;
1605 } else if (is_type_pointer(rtype)) {
1606 expression_classification_t const l = is_constant_expression(left);
1607 expression_classification_t const r = is_linker_constant(right);
1608 return l < r ? l : r;
1609 } else if (!is_type_valid(ltype) || !is_type_valid(rtype)) {
1610 return EXPR_CLASS_ERROR;
1612 return is_constant_expression(expression);
1616 case EXPR_REFERENCE: {
1617 entity_t *entity = expression->reference.entity;
1618 if (!is_declaration(entity))
1619 return EXPR_CLASS_VARIABLE;
1621 type_t *type = skip_typeref(entity->declaration.type);
1622 if (is_type_function(type))
1623 return EXPR_CLASS_CONSTANT;
1624 if (is_type_array(type)) {
1625 return is_object_with_linker_constant_address(expression);
1627 /* Prevent stray errors */
1628 if (!is_type_valid(type))
1629 return EXPR_CLASS_ERROR;
1630 return EXPR_CLASS_VARIABLE;
1633 case EXPR_ARRAY_ACCESS: {
1634 type_t *const type =
1635 skip_typeref(revert_automatic_type_conversion(expression));
1636 if (!is_type_array(type))
1637 return EXPR_CLASS_VARIABLE;
1638 return is_linker_constant(expression->array_access.array_ref);
1641 case EXPR_CONDITIONAL: {
1642 expression_t *const c = expression->conditional.condition;
1643 expression_classification_t const cclass = is_constant_expression(c);
1644 if (cclass != EXPR_CLASS_CONSTANT)
1647 if (fold_constant_to_bool(c)) {
1648 expression_t const *const t = expression->conditional.true_expression;
1649 return is_linker_constant(t != NULL ? t : c);
1651 return is_linker_constant(expression->conditional.false_expression);
1656 entity_t *entity = expression->select.compound_entry;
1657 if (!is_declaration(entity))
1658 return EXPR_CLASS_VARIABLE;
1659 type_t *type = skip_typeref(entity->declaration.type);
1660 if (is_type_array(type)) {
1661 /* arrays automatically convert to their address */
1662 expression_t *compound = expression->select.compound;
1663 type_t *base_type = skip_typeref(compound->base.type);
1664 if (is_type_pointer(base_type)) {
1666 return is_linker_constant(compound);
1668 return is_object_with_linker_constant_address(compound);
1671 return EXPR_CLASS_VARIABLE;
1675 return is_constant_expression(expression);
1680 * Check if the given expression is a call to a builtin function
1681 * returning a constant result.
1683 static expression_classification_t is_builtin_const_call(const expression_t *expression)
1685 expression_t *function = expression->call.function;
1686 if (function->kind != EXPR_REFERENCE)
1687 return EXPR_CLASS_VARIABLE;
1688 reference_expression_t *ref = &function->reference;
1689 if (ref->entity->kind != ENTITY_FUNCTION)
1690 return EXPR_CLASS_VARIABLE;
1692 switch (ref->entity->function.btk) {
1695 return EXPR_CLASS_CONSTANT;
1697 return EXPR_CLASS_VARIABLE;
1702 static expression_classification_t is_constant_pointer(const expression_t *expression)
1704 expression_classification_t const expr_class = is_constant_expression(expression);
1705 if (expr_class != EXPR_CLASS_VARIABLE)
1708 switch (expression->kind) {
1709 case EXPR_UNARY_CAST:
1710 return is_constant_pointer(expression->unary.value);
1712 return EXPR_CLASS_VARIABLE;
1716 static expression_classification_t is_object_with_constant_address(const expression_t *expression)
1718 switch (expression->kind) {
1720 expression_t *compound = expression->select.compound;
1721 type_t *compound_type = compound->base.type;
1722 compound_type = skip_typeref(compound_type);
1723 if (is_type_pointer(compound_type)) {
1724 return is_constant_pointer(compound);
1726 return is_object_with_constant_address(compound);
1730 case EXPR_ARRAY_ACCESS: {
1731 array_access_expression_t const* const array_access =
1732 &expression->array_access;
1733 expression_classification_t const idx_class = is_constant_expression(array_access->index);
1734 if (idx_class != EXPR_CLASS_CONSTANT)
1736 expression_classification_t const ref_addr = is_object_with_constant_address(array_access->array_ref);
1737 expression_classification_t const ref_ptr = is_constant_pointer(array_access->array_ref);
1738 return ref_addr > ref_ptr ? ref_addr : ref_ptr;
1741 case EXPR_UNARY_DEREFERENCE:
1742 return is_constant_pointer(expression->unary.value);
1745 return EXPR_CLASS_ERROR;
1748 return EXPR_CLASS_VARIABLE;
1752 expression_classification_t is_constant_expression(const expression_t *expression)
1754 switch (expression->kind) {
1755 case EXPR_LITERAL_CHARACTER:
1756 case EXPR_BUILTIN_TYPES_COMPATIBLE_P:
1757 case EXPR_ENUM_CONSTANT:
1758 case EXPR_LITERAL_BOOLEAN:
1759 case EXPR_LITERAL_MS_NOOP:
1760 return EXPR_CLASS_CONSTANT;
1765 type = skip_typeref(expression->typeprop.type);
1768 case EXPR_CLASSIFY_TYPE:
1769 type = skip_typeref(expression->classify_type.type_expression->base.type);
1772 case EXPR_LITERAL_INTEGER:
1773 case EXPR_LITERAL_FLOATINGPOINT:
1774 type = skip_typeref(expression->base.type);
1778 type = skip_typeref(expression->offsetofe.type);
1782 type = skip_typeref(expression->typeprop.type);
1783 if (is_type_array(type) && type->array.is_vla)
1784 return EXPR_CLASS_VARIABLE;
1788 return is_type_valid(type) ? EXPR_CLASS_CONSTANT : EXPR_CLASS_ERROR;
1791 case EXPR_BUILTIN_CONSTANT_P: {
1792 expression_classification_t const c = is_constant_expression(expression->builtin_constant.value);
1793 return c != EXPR_CLASS_ERROR ? EXPR_CLASS_CONSTANT : EXPR_CLASS_ERROR;
1796 case EXPR_STRING_LITERAL:
1798 case EXPR_LABEL_ADDRESS:
1803 case EXPR_STATEMENT:
1804 case EXPR_UNARY_POSTFIX_INCREMENT:
1805 case EXPR_UNARY_POSTFIX_DECREMENT:
1806 case EXPR_UNARY_PREFIX_INCREMENT:
1807 case EXPR_UNARY_PREFIX_DECREMENT:
1808 case EXPR_UNARY_ASSUME: /* has VOID type */
1809 case EXPR_UNARY_DEREFERENCE:
1810 case EXPR_UNARY_DELETE:
1811 case EXPR_UNARY_DELETE_ARRAY:
1812 case EXPR_UNARY_THROW:
1813 case EXPR_BINARY_ASSIGN:
1814 case EXPR_BINARY_MUL_ASSIGN:
1815 case EXPR_BINARY_DIV_ASSIGN:
1816 case EXPR_BINARY_MOD_ASSIGN:
1817 case EXPR_BINARY_ADD_ASSIGN:
1818 case EXPR_BINARY_SUB_ASSIGN:
1819 case EXPR_BINARY_SHIFTLEFT_ASSIGN:
1820 case EXPR_BINARY_SHIFTRIGHT_ASSIGN:
1821 case EXPR_BINARY_BITWISE_AND_ASSIGN:
1822 case EXPR_BINARY_BITWISE_XOR_ASSIGN:
1823 case EXPR_BINARY_BITWISE_OR_ASSIGN:
1824 case EXPR_BINARY_COMMA:
1825 case EXPR_ARRAY_ACCESS:
1826 return EXPR_CLASS_VARIABLE;
1828 case EXPR_REFERENCE: {
1829 type_t *const type = skip_typeref(expression->base.type);
1830 return is_type_valid(type) ? EXPR_CLASS_VARIABLE : EXPR_CLASS_ERROR;
1833 case EXPR_UNARY_TAKE_ADDRESS:
1834 return is_object_with_constant_address(expression->unary.value);
1837 return is_builtin_const_call(expression);
1839 case EXPR_UNARY_NEGATE:
1840 case EXPR_UNARY_PLUS:
1841 case EXPR_UNARY_COMPLEMENT:
1842 case EXPR_UNARY_NOT:
1843 return is_constant_expression(expression->unary.value);
1845 case EXPR_UNARY_IMAG:
1846 case EXPR_UNARY_REAL: {
1847 type_t *type = skip_typeref(expression->base.type);
1848 if (!is_type_valid(type))
1849 return EXPR_CLASS_ERROR;
1850 return is_constant_expression(expression->unary.value);
1854 case EXPR_UNARY_CAST: {
1855 type_t *const type = skip_typeref(expression->base.type);
1856 if (is_type_scalar(type) || is_type_complex(type))
1857 return is_constant_expression(expression->unary.value);
1858 if (!is_type_valid(type))
1859 return EXPR_CLASS_ERROR;
1860 return EXPR_CLASS_VARIABLE;
1863 case EXPR_BINARY_ADD:
1864 case EXPR_BINARY_SUB:
1865 case EXPR_BINARY_MUL:
1866 case EXPR_BINARY_DIV:
1867 case EXPR_BINARY_MOD:
1868 case EXPR_BINARY_EQUAL:
1869 case EXPR_BINARY_NOTEQUAL:
1870 case EXPR_BINARY_LESS:
1871 case EXPR_BINARY_LESSEQUAL:
1872 case EXPR_BINARY_GREATER:
1873 case EXPR_BINARY_GREATEREQUAL:
1874 case EXPR_BINARY_BITWISE_AND:
1875 case EXPR_BINARY_BITWISE_OR:
1876 case EXPR_BINARY_BITWISE_XOR:
1877 case EXPR_BINARY_SHIFTLEFT:
1878 case EXPR_BINARY_SHIFTRIGHT:
1879 case EXPR_BINARY_ISGREATER:
1880 case EXPR_BINARY_ISGREATEREQUAL:
1881 case EXPR_BINARY_ISLESS:
1882 case EXPR_BINARY_ISLESSEQUAL:
1883 case EXPR_BINARY_ISLESSGREATER:
1884 case EXPR_BINARY_ISUNORDERED: {
1885 expression_classification_t const l = is_constant_expression(expression->binary.left);
1886 expression_classification_t const r = is_constant_expression(expression->binary.right);
1887 return l < r ? l : r;
1890 case EXPR_BINARY_LOGICAL_AND: {
1891 expression_t const *const left = expression->binary.left;
1892 expression_classification_t const lclass = is_constant_expression(left);
1893 if (lclass != EXPR_CLASS_CONSTANT)
1895 if (!fold_constant_to_bool(left))
1896 return EXPR_CLASS_CONSTANT;
1897 return is_constant_expression(expression->binary.right);
1900 case EXPR_BINARY_LOGICAL_OR: {
1901 expression_t const *const left = expression->binary.left;
1902 expression_classification_t const lclass = is_constant_expression(left);
1903 if (lclass != EXPR_CLASS_CONSTANT)
1905 if (fold_constant_to_bool(left))
1906 return EXPR_CLASS_CONSTANT;
1907 return is_constant_expression(expression->binary.right);
1910 case EXPR_COMPOUND_LITERAL:
1911 return is_constant_initializer(expression->compound_literal.initializer);
1913 case EXPR_CONDITIONAL: {
1914 expression_t *const condition = expression->conditional.condition;
1915 expression_classification_t const cclass = is_constant_expression(condition);
1916 if (cclass != EXPR_CLASS_CONSTANT)
1919 if (fold_constant_to_bool(condition)) {
1920 expression_t const *const t = expression->conditional.true_expression;
1921 return t == NULL ? EXPR_CLASS_CONSTANT : is_constant_expression(t);
1923 return is_constant_expression(expression->conditional.false_expression);
1928 return EXPR_CLASS_ERROR;
1930 panic("invalid expression");
1935 obstack_init(&ast_obstack);
1940 obstack_free(&ast_obstack, NULL);