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"
36 #if defined(__INTEL_COMPILER)
38 #elif defined(__CYGWIN__)
39 #include "win32/cygwin_math_ext.h"
44 #include "adt/error.h"
47 struct obstack ast_obstack;
50 static int case_indent;
52 bool print_implicit_casts = false;
53 bool print_parenthesis = false;
55 static void print_statement(const statement_t *statement);
56 static void print_expression_prec(const expression_t *expression, unsigned prec);
58 void change_indent(int delta)
64 void print_indent(void)
66 for (int i = 0; i < indent; ++i)
70 static void print_stringrep(const string_t *string)
72 for (size_t i = 0; i < string->size; ++i) {
73 print_char(string->begin[i]);
78 * Returns 1 if a given precedence level has right-to-left
79 * associativity, else 0.
81 * @param precedence the operator precedence
83 static int right_to_left(unsigned precedence)
87 case PREC_CONDITIONAL:
97 * Return the precedence of an expression given by its kind.
99 * @param kind the expression kind
101 static unsigned get_expression_precedence(expression_kind_t kind)
103 static const unsigned prec[] = {
104 [EXPR_ERROR] = PREC_PRIMARY,
105 [EXPR_REFERENCE] = PREC_PRIMARY,
106 [EXPR_ENUM_CONSTANT] = PREC_PRIMARY,
107 [EXPR_LITERAL_INTEGER] = PREC_PRIMARY,
108 [EXPR_LITERAL_FLOATINGPOINT] = PREC_PRIMARY,
109 [EXPR_LITERAL_FLOATINGPOINT_HEXADECIMAL] = PREC_PRIMARY,
110 [EXPR_LITERAL_CHARACTER] = PREC_PRIMARY,
111 [EXPR_LITERAL_WIDE_CHARACTER] = PREC_PRIMARY,
112 [EXPR_LITERAL_MS_NOOP] = PREC_PRIMARY,
113 [EXPR_STRING_LITERAL] = PREC_PRIMARY,
114 [EXPR_WIDE_STRING_LITERAL] = PREC_PRIMARY,
115 [EXPR_COMPOUND_LITERAL] = PREC_UNARY,
116 [EXPR_CALL] = PREC_POSTFIX,
117 [EXPR_CONDITIONAL] = PREC_CONDITIONAL,
118 [EXPR_SELECT] = PREC_POSTFIX,
119 [EXPR_ARRAY_ACCESS] = PREC_POSTFIX,
120 [EXPR_SIZEOF] = PREC_UNARY,
121 [EXPR_CLASSIFY_TYPE] = PREC_UNARY,
122 [EXPR_ALIGNOF] = PREC_UNARY,
124 [EXPR_FUNCNAME] = PREC_PRIMARY,
125 [EXPR_BUILTIN_CONSTANT_P] = PREC_PRIMARY,
126 [EXPR_BUILTIN_TYPES_COMPATIBLE_P] = PREC_PRIMARY,
127 [EXPR_OFFSETOF] = PREC_PRIMARY,
128 [EXPR_VA_START] = PREC_PRIMARY,
129 [EXPR_VA_ARG] = PREC_PRIMARY,
130 [EXPR_VA_COPY] = PREC_PRIMARY,
131 [EXPR_STATEMENT] = PREC_PRIMARY,
132 [EXPR_LABEL_ADDRESS] = PREC_PRIMARY,
134 [EXPR_UNARY_NEGATE] = PREC_UNARY,
135 [EXPR_UNARY_PLUS] = PREC_UNARY,
136 [EXPR_UNARY_BITWISE_NEGATE] = PREC_UNARY,
137 [EXPR_UNARY_NOT] = PREC_UNARY,
138 [EXPR_UNARY_DEREFERENCE] = PREC_UNARY,
139 [EXPR_UNARY_TAKE_ADDRESS] = PREC_UNARY,
140 [EXPR_UNARY_POSTFIX_INCREMENT] = PREC_POSTFIX,
141 [EXPR_UNARY_POSTFIX_DECREMENT] = PREC_POSTFIX,
142 [EXPR_UNARY_PREFIX_INCREMENT] = PREC_UNARY,
143 [EXPR_UNARY_PREFIX_DECREMENT] = PREC_UNARY,
144 [EXPR_UNARY_CAST] = PREC_UNARY,
145 [EXPR_UNARY_ASSUME] = PREC_PRIMARY,
146 [EXPR_UNARY_DELETE] = PREC_UNARY,
147 [EXPR_UNARY_DELETE_ARRAY] = PREC_UNARY,
148 [EXPR_UNARY_THROW] = PREC_ASSIGNMENT,
150 [EXPR_BINARY_ADD] = PREC_ADDITIVE,
151 [EXPR_BINARY_SUB] = PREC_ADDITIVE,
152 [EXPR_BINARY_MUL] = PREC_MULTIPLICATIVE,
153 [EXPR_BINARY_DIV] = PREC_MULTIPLICATIVE,
154 [EXPR_BINARY_MOD] = PREC_MULTIPLICATIVE,
155 [EXPR_BINARY_EQUAL] = PREC_EQUALITY,
156 [EXPR_BINARY_NOTEQUAL] = PREC_EQUALITY,
157 [EXPR_BINARY_LESS] = PREC_RELATIONAL,
158 [EXPR_BINARY_LESSEQUAL] = PREC_RELATIONAL,
159 [EXPR_BINARY_GREATER] = PREC_RELATIONAL,
160 [EXPR_BINARY_GREATEREQUAL] = PREC_RELATIONAL,
161 [EXPR_BINARY_BITWISE_AND] = PREC_AND,
162 [EXPR_BINARY_BITWISE_OR] = PREC_OR,
163 [EXPR_BINARY_BITWISE_XOR] = PREC_XOR,
164 [EXPR_BINARY_LOGICAL_AND] = PREC_LOGICAL_AND,
165 [EXPR_BINARY_LOGICAL_OR] = PREC_LOGICAL_OR,
166 [EXPR_BINARY_SHIFTLEFT] = PREC_SHIFT,
167 [EXPR_BINARY_SHIFTRIGHT] = PREC_SHIFT,
168 [EXPR_BINARY_ASSIGN] = PREC_ASSIGNMENT,
169 [EXPR_BINARY_MUL_ASSIGN] = PREC_ASSIGNMENT,
170 [EXPR_BINARY_DIV_ASSIGN] = PREC_ASSIGNMENT,
171 [EXPR_BINARY_MOD_ASSIGN] = PREC_ASSIGNMENT,
172 [EXPR_BINARY_ADD_ASSIGN] = PREC_ASSIGNMENT,
173 [EXPR_BINARY_SUB_ASSIGN] = PREC_ASSIGNMENT,
174 [EXPR_BINARY_SHIFTLEFT_ASSIGN] = PREC_ASSIGNMENT,
175 [EXPR_BINARY_SHIFTRIGHT_ASSIGN] = PREC_ASSIGNMENT,
176 [EXPR_BINARY_BITWISE_AND_ASSIGN] = PREC_ASSIGNMENT,
177 [EXPR_BINARY_BITWISE_XOR_ASSIGN] = PREC_ASSIGNMENT,
178 [EXPR_BINARY_BITWISE_OR_ASSIGN] = PREC_ASSIGNMENT,
179 [EXPR_BINARY_COMMA] = PREC_EXPRESSION,
181 [EXPR_BINARY_ISGREATER] = PREC_PRIMARY,
182 [EXPR_BINARY_ISGREATEREQUAL] = PREC_PRIMARY,
183 [EXPR_BINARY_ISLESS] = PREC_PRIMARY,
184 [EXPR_BINARY_ISLESSEQUAL] = PREC_PRIMARY,
185 [EXPR_BINARY_ISLESSGREATER] = PREC_PRIMARY,
186 [EXPR_BINARY_ISUNORDERED] = PREC_PRIMARY
188 assert((size_t)kind < lengthof(prec));
189 unsigned res = prec[kind];
191 assert(res != PREC_BOTTOM);
196 * Print a quoted string constant.
198 * @param string the string constant
199 * @param border the border char
200 * @param skip number of chars to skip at the end
202 static void print_quoted_string(const string_t *const string, char border,
206 const char *end = string->begin + string->size - skip;
207 for (const char *c = string->begin; c != end; ++c) {
213 case '\\': print_string("\\\\"); break;
214 case '\a': print_string("\\a"); break;
215 case '\b': print_string("\\b"); break;
216 case '\f': print_string("\\f"); break;
217 case '\n': print_string("\\n"); break;
218 case '\r': print_string("\\r"); break;
219 case '\t': print_string("\\t"); break;
220 case '\v': print_string("\\v"); break;
221 case '\?': print_string("\\?"); break;
223 if (c_mode & _GNUC) {
224 print_string("\\e"); break;
228 if ((unsigned)tc < 0x80 && !isprint(tc)) {
229 print_format("\\%03o", (unsigned)tc);
239 static void print_string_literal(const string_literal_expression_t *literal)
241 if (literal->base.kind == EXPR_WIDE_STRING_LITERAL) {
244 print_quoted_string(&literal->value, '"', 1);
247 static void print_literal(const literal_expression_t *literal)
249 switch (literal->base.kind) {
250 case EXPR_LITERAL_MS_NOOP:
251 print_string("__noop");
254 case EXPR_LITERAL_BOOLEAN:
255 case EXPR_LITERAL_FLOATINGPOINT:
256 case EXPR_LITERAL_FLOATINGPOINT_HEXADECIMAL:
257 case EXPR_LITERAL_INTEGER:
258 print_stringrep(&literal->value);
259 print_stringrep(&literal->suffix);
262 case EXPR_LITERAL_WIDE_CHARACTER:
265 case EXPR_LITERAL_CHARACTER:
266 print_quoted_string(&literal->value, '\'', 0);
271 print_string("INVALID LITERAL KIND");
275 * Prints a predefined symbol.
277 static void print_funcname(const funcname_expression_t *funcname)
280 switch (funcname->kind) {
281 case FUNCNAME_FUNCTION: s = (c_mode & _C99) ? "__func__" : "__FUNCTION__"; break;
282 case FUNCNAME_PRETTY_FUNCTION: s = "__PRETTY_FUNCTION__"; break;
283 case FUNCNAME_FUNCSIG: s = "__FUNCSIG__"; break;
284 case FUNCNAME_FUNCDNAME: s = "__FUNCDNAME__"; break;
289 static void print_compound_literal(
290 const compound_literal_expression_t *expression)
293 print_type(expression->type);
295 print_initializer(expression->initializer);
298 static void print_assignment_expression(const expression_t *const expr)
300 print_expression_prec(expr, PREC_ASSIGNMENT);
304 * Prints a call expression.
306 * @param call the call expression
308 static void print_call_expression(const call_expression_t *call)
310 print_expression_prec(call->function, PREC_POSTFIX);
312 char const *sep = "";
313 for (call_argument_t const *arg = call->arguments; arg; arg = arg->next) {
316 print_assignment_expression(arg->expression);
322 * Prints a binary expression.
324 * @param binexpr the binary expression
326 static void print_binary_expression(const binary_expression_t *binexpr)
328 unsigned prec = get_expression_precedence(binexpr->base.kind);
329 int r2l = right_to_left(prec);
331 print_expression_prec(binexpr->left, prec + r2l);
333 switch (binexpr->base.kind) {
334 case EXPR_BINARY_COMMA: op = ", "; break;
335 case EXPR_BINARY_ASSIGN: op = " = "; break;
336 case EXPR_BINARY_ADD: op = " + "; break;
337 case EXPR_BINARY_SUB: op = " - "; break;
338 case EXPR_BINARY_MUL: op = " * "; break;
339 case EXPR_BINARY_MOD: op = " % "; break;
340 case EXPR_BINARY_DIV: op = " / "; break;
341 case EXPR_BINARY_BITWISE_OR: op = " | "; break;
342 case EXPR_BINARY_BITWISE_AND: op = " & "; break;
343 case EXPR_BINARY_BITWISE_XOR: op = " ^ "; break;
344 case EXPR_BINARY_LOGICAL_OR: op = " || "; break;
345 case EXPR_BINARY_LOGICAL_AND: op = " && "; break;
346 case EXPR_BINARY_NOTEQUAL: op = " != "; break;
347 case EXPR_BINARY_EQUAL: op = " == "; break;
348 case EXPR_BINARY_LESS: op = " < "; break;
349 case EXPR_BINARY_LESSEQUAL: op = " <= "; break;
350 case EXPR_BINARY_GREATER: op = " > "; break;
351 case EXPR_BINARY_GREATEREQUAL: op = " >= "; break;
352 case EXPR_BINARY_SHIFTLEFT: op = " << "; break;
353 case EXPR_BINARY_SHIFTRIGHT: op = " >> "; break;
355 case EXPR_BINARY_ADD_ASSIGN: op = " += "; break;
356 case EXPR_BINARY_SUB_ASSIGN: op = " -= "; break;
357 case EXPR_BINARY_MUL_ASSIGN: op = " *= "; break;
358 case EXPR_BINARY_MOD_ASSIGN: op = " %= "; break;
359 case EXPR_BINARY_DIV_ASSIGN: op = " /= "; break;
360 case EXPR_BINARY_BITWISE_OR_ASSIGN: op = " |= "; break;
361 case EXPR_BINARY_BITWISE_AND_ASSIGN: op = " &= "; break;
362 case EXPR_BINARY_BITWISE_XOR_ASSIGN: op = " ^= "; break;
363 case EXPR_BINARY_SHIFTLEFT_ASSIGN: op = " <<= "; break;
364 case EXPR_BINARY_SHIFTRIGHT_ASSIGN: op = " >>= "; break;
365 default: panic("invalid binexpression found");
368 print_expression_prec(binexpr->right, prec + 1 - r2l);
372 * Prints an unary expression.
374 * @param unexpr the unary expression
376 static void print_unary_expression(const unary_expression_t *unexpr)
378 unsigned prec = get_expression_precedence(unexpr->base.kind);
379 switch (unexpr->base.kind) {
380 case EXPR_UNARY_NEGATE: print_char ('-' ); break;
381 case EXPR_UNARY_PLUS: print_char ('+' ); break;
382 case EXPR_UNARY_NOT: print_char ('!' ); break;
383 case EXPR_UNARY_BITWISE_NEGATE: print_char ('~' ); break;
384 case EXPR_UNARY_PREFIX_INCREMENT: print_string("++"); break;
385 case EXPR_UNARY_PREFIX_DECREMENT: print_string("--"); break;
386 case EXPR_UNARY_DEREFERENCE: print_char ('*' ); break;
387 case EXPR_UNARY_TAKE_ADDRESS: print_char ('&' ); break;
388 case EXPR_UNARY_DELETE: print_string("delete "); break;
389 case EXPR_UNARY_DELETE_ARRAY: print_string("delete [] "); break;
391 case EXPR_UNARY_POSTFIX_INCREMENT:
392 print_expression_prec(unexpr->value, prec);
395 case EXPR_UNARY_POSTFIX_DECREMENT:
396 print_expression_prec(unexpr->value, prec);
399 case EXPR_UNARY_CAST:
401 print_type(unexpr->base.type);
404 case EXPR_UNARY_ASSUME:
405 print_string("__assume(");
406 print_assignment_expression(unexpr->value);
410 case EXPR_UNARY_THROW:
411 if (unexpr->value == NULL) {
412 print_string("throw");
415 print_string("throw ");
419 panic("invalid unary expression found");
421 print_expression_prec(unexpr->value, prec);
425 * Prints a reference expression.
427 * @param ref the reference expression
429 static void print_reference_expression(const reference_expression_t *ref)
431 print_string(ref->entity->base.symbol->string);
435 * Prints a label address expression.
437 * @param ref the reference expression
439 static void print_label_address_expression(const label_address_expression_t *le)
441 print_format("&&%s", le->label->base.symbol->string);
445 * Prints an array expression.
447 * @param expression the array expression
449 static void print_array_expression(const array_access_expression_t *expression)
451 if (!expression->flipped) {
452 print_expression_prec(expression->array_ref, PREC_POSTFIX);
454 print_expression(expression->index);
457 print_expression_prec(expression->index, PREC_POSTFIX);
459 print_expression(expression->array_ref);
465 * Prints a typeproperty expression (sizeof or __alignof__).
467 * @param expression the type property expression
469 static void print_typeprop_expression(const typeprop_expression_t *expression)
471 if (expression->base.kind == EXPR_SIZEOF) {
472 print_string("sizeof");
474 assert(expression->base.kind == EXPR_ALIGNOF);
475 print_string("__alignof__");
477 if (expression->tp_expression != NULL) {
478 /* PREC_TOP: always print the '()' here, sizeof x is right but unusual */
479 print_expression_prec(expression->tp_expression, PREC_TOP);
482 print_type(expression->type);
488 * Prints a builtin constant expression.
490 * @param expression the builtin constant expression
492 static void print_builtin_constant(const builtin_constant_expression_t *expression)
494 print_string("__builtin_constant_p(");
495 print_assignment_expression(expression->value);
500 * Prints a builtin types compatible expression.
502 * @param expression the builtin types compatible expression
504 static void print_builtin_types_compatible(
505 const builtin_types_compatible_expression_t *expression)
507 print_string("__builtin_types_compatible_p(");
508 print_type(expression->left);
510 print_type(expression->right);
515 * Prints a conditional expression.
517 * @param expression the conditional expression
519 static void print_conditional(const conditional_expression_t *expression)
521 print_expression_prec(expression->condition, PREC_LOGICAL_OR);
522 if (expression->true_expression != NULL) {
524 print_expression_prec(expression->true_expression, PREC_EXPRESSION);
527 print_string(" ?: ");
529 precedence_t prec = c_mode & _CXX ? PREC_ASSIGNMENT : PREC_CONDITIONAL;
530 print_expression_prec(expression->false_expression, prec);
534 * Prints a va_start expression.
536 * @param expression the va_start expression
538 static void print_va_start(const va_start_expression_t *const expression)
540 print_string("__builtin_va_start(");
541 print_assignment_expression(expression->ap);
543 print_string(expression->parameter->base.base.symbol->string);
548 * Prints a va_arg expression.
550 * @param expression the va_arg expression
552 static void print_va_arg(const va_arg_expression_t *expression)
554 print_string("__builtin_va_arg(");
555 print_assignment_expression(expression->ap);
557 print_type(expression->base.type);
562 * Prints a va_copy expression.
564 * @param expression the va_copy expression
566 static void print_va_copy(const va_copy_expression_t *expression)
568 print_string("__builtin_va_copy(");
569 print_assignment_expression(expression->dst);
571 print_assignment_expression(expression->src);
576 * Prints a select expression (. or ->).
578 * @param expression the select expression
580 static void print_select(const select_expression_t *expression)
582 print_expression_prec(expression->compound, PREC_POSTFIX);
583 /* do not print anything for anonymous struct/union selects
584 * FIXME: if the anonymous select was a '->' this will print '.'
586 if (expression->compound_entry->base.symbol == NULL)
589 if (is_type_pointer(skip_typeref(expression->compound->base.type))) {
594 print_string(expression->compound_entry->base.symbol->string);
598 * Prints a type classify expression.
600 * @param expr the type classify expression
602 static void print_classify_type_expression(
603 const classify_type_expression_t *const expr)
605 print_string("__builtin_classify_type(");
606 print_assignment_expression(expr->type_expression);
611 * Prints a designator.
613 * @param designator the designator
615 static void print_designator(const designator_t *designator)
617 for ( ; designator != NULL; designator = designator->next) {
618 if (designator->symbol == NULL) {
620 print_expression(designator->array_index);
624 print_string(designator->symbol->string);
630 * Prints an offsetof expression.
632 * @param expression the offset expression
634 static void print_offsetof_expression(const offsetof_expression_t *expression)
636 print_string("__builtin_offsetof(");
637 print_type(expression->type);
639 print_designator(expression->designator);
644 * Prints a statement expression.
646 * @param expression the statement expression
648 static void print_statement_expression(const statement_expression_t *expression)
651 print_statement(expression->statement);
656 * Prints an expression with parenthesis if needed.
658 * @param expression the expression to print
659 * @param top_prec the precedence of the user of this expression.
661 static void print_expression_prec(expression_t const *expr, unsigned const top_prec)
663 if (expr->kind == EXPR_UNARY_CAST && expr->base.implicit && !print_implicit_casts) {
664 expr = expr->unary.value;
668 expr->base.parenthesized ||
669 (print_parenthesis && top_prec != PREC_BOTTOM) ||
670 top_prec > get_expression_precedence(expr->base.kind);
674 switch (expr->kind) {
676 case EXPR_SIZEOF: print_typeprop_expression( &expr->typeprop); break;
677 case EXPR_ARRAY_ACCESS: print_array_expression( &expr->array_access); break;
678 case EXPR_BINARY_CASES: print_binary_expression( &expr->binary); break;
679 case EXPR_BUILTIN_CONSTANT_P: print_builtin_constant( &expr->builtin_constant); break;
680 case EXPR_BUILTIN_TYPES_COMPATIBLE_P: print_builtin_types_compatible(&expr->builtin_types_compatible); break;
681 case EXPR_CALL: print_call_expression( &expr->call); break;
682 case EXPR_CLASSIFY_TYPE: print_classify_type_expression(&expr->classify_type); break;
683 case EXPR_COMPOUND_LITERAL: print_compound_literal( &expr->compound_literal); break;
684 case EXPR_CONDITIONAL: print_conditional( &expr->conditional); break;
685 case EXPR_ERROR: print_string("$error$"); break;
686 case EXPR_FUNCNAME: print_funcname( &expr->funcname); break;
687 case EXPR_LABEL_ADDRESS: print_label_address_expression(&expr->label_address); break;
688 case EXPR_LITERAL_CASES: print_literal( &expr->literal); break;
689 case EXPR_OFFSETOF: print_offsetof_expression( &expr->offsetofe); break;
691 case EXPR_ENUM_CONSTANT: print_reference_expression( &expr->reference); break;
692 case EXPR_SELECT: print_select( &expr->select); break;
693 case EXPR_STATEMENT: print_statement_expression( &expr->statement); break;
694 case EXPR_STRING_LITERAL:
695 case EXPR_WIDE_STRING_LITERAL: print_string_literal( &expr->string_literal); break;
696 case EXPR_UNARY_CASES: print_unary_expression( &expr->unary); break;
697 case EXPR_VA_ARG: print_va_arg( &expr->va_arge); break;
698 case EXPR_VA_COPY: print_va_copy( &expr->va_copye); break;
699 case EXPR_VA_START: print_va_start( &expr->va_starte); break;
705 static void print_indented_statement(statement_t const *const stmt)
707 switch (stmt->kind) {
708 case STATEMENT_LABEL:
711 case STATEMENT_CASE_LABEL:
712 for (int i = 0; i != case_indent; ++i)
720 print_statement(stmt);
724 * Print an compound statement.
726 * @param block the compound statement
728 static void print_compound_statement(const compound_statement_t *block)
733 for (statement_t const *stmt = block->statements; stmt; stmt = stmt->base.next) {
734 print_indented_statement(stmt);
744 * Print a return statement.
746 * @param statement the return statement
748 static void print_return_statement(const return_statement_t *statement)
750 expression_t const *const val = statement->value;
752 print_string("return ");
753 print_expression(val);
756 print_string("return;");
761 * Print an expression statement.
763 * @param statement the expression statement
765 static void print_expression_statement(const expression_statement_t *statement)
767 print_expression(statement->expression);
772 * Print a computed goto statement.
774 * @param statement the computed goto statement
776 static void print_computed_goto_statement(computed_goto_statement_t const *const stmt)
778 print_string("goto *");
779 print_expression(stmt->expression);
784 * Print a goto statement.
786 * @param statement the goto statement
788 static void print_goto_statement(const goto_statement_t *statement)
790 print_string("goto ");
791 print_string(statement->label->base.symbol->string);
796 * Print a label statement.
798 * @param statement the label statement
800 static void print_label_statement(const label_statement_t *statement)
802 print_format("%s:\n", statement->label->base.symbol->string);
803 print_indented_statement(statement->statement);
806 static void print_inner_statement(statement_t const *const stmt)
808 if (stmt->kind == STATEMENT_COMPOUND) {
810 print_compound_statement(&stmt->compound);
814 print_indented_statement(stmt);
819 static void print_after_inner_statement(statement_t const *const stmt)
821 if (stmt->kind == STATEMENT_COMPOUND) {
830 * Print an if statement.
832 * @param statement the if statement
834 static void print_if_statement(const if_statement_t *statement)
836 print_string("if (");
837 print_expression(statement->condition);
839 print_inner_statement(statement->true_statement);
841 statement_t const *const f = statement->false_statement;
843 print_after_inner_statement(statement->true_statement);
844 print_string("else");
845 if (f->kind == STATEMENT_IF) {
847 print_if_statement(&f->ifs);
849 print_inner_statement(f);
855 * Print a switch statement.
857 * @param statement the switch statement
859 static void print_switch_statement(const switch_statement_t *statement)
861 int const old_case_indent = case_indent;
862 case_indent = indent;
864 print_string("switch (");
865 print_expression(statement->expression);
867 print_inner_statement(statement->body);
869 case_indent = old_case_indent;
873 * Print a case label (including the default label).
875 * @param statement the case label statement
877 static void print_case_label(const case_label_statement_t *statement)
879 if (statement->expression == NULL) {
880 print_string("default:\n");
882 print_string("case ");
883 print_expression(statement->expression);
884 if (statement->end_range != NULL) {
885 print_string(" ... ");
886 print_expression(statement->end_range);
890 print_indented_statement(statement->statement);
893 static void print_typedef(const entity_t *entity)
895 print_string("typedef ");
896 print_type_ext(entity->typedefe.type, entity->base.symbol, NULL);
901 * returns true if the entity is a compiler generated one and has no real
902 * correspondenc in the source file
904 static bool is_generated_entity(const entity_t *entity)
906 if (entity->kind == ENTITY_TYPEDEF)
907 return entity->typedefe.builtin;
909 if (is_declaration(entity))
910 return entity->declaration.implicit;
916 * Print a declaration statement.
918 * @param statement the statement
920 static void print_declaration_statement(
921 const declaration_statement_t *statement)
924 entity_t *entity = statement->declarations_begin;
925 if (entity == NULL) {
926 print_string("/* empty declaration statement */");
930 entity_t *const end = statement->declarations_end->base.next;
931 for (; entity != end; entity = entity->base.next) {
932 if (entity->kind == ENTITY_ENUM_VALUE)
934 if (is_generated_entity(entity))
944 print_entity(entity);
949 * Print a while statement.
951 * @param statement the statement
953 static void print_while_statement(const while_statement_t *statement)
955 print_string("while (");
956 print_expression(statement->condition);
958 print_inner_statement(statement->body);
962 * Print a do-while statement.
964 * @param statement the statement
966 static void print_do_while_statement(const do_while_statement_t *statement)
969 print_inner_statement(statement->body);
970 print_after_inner_statement(statement->body);
971 print_string("while (");
972 print_expression(statement->condition);
977 * Print a for statement.
979 * @param statement the statement
981 static void print_for_statement(const for_statement_t *statement)
983 print_string("for (");
984 if (statement->initialisation != NULL) {
985 print_expression(statement->initialisation);
988 entity_t const *entity = statement->scope.entities;
989 for (; entity != NULL; entity = entity->base.next) {
990 if (is_generated_entity(entity))
992 /* FIXME display of multiple declarations is wrong */
993 print_declaration(entity);
996 if (statement->condition != NULL) {
998 print_expression(statement->condition);
1001 if (statement->step != NULL) {
1003 print_expression(statement->step);
1006 print_inner_statement(statement->body);
1010 * Print assembler arguments.
1012 * @param arguments the arguments
1014 static void print_asm_arguments(asm_argument_t *arguments)
1016 asm_argument_t *argument = arguments;
1017 for (; argument != NULL; argument = argument->next) {
1018 if (argument != arguments)
1021 if (argument->symbol) {
1022 print_format("[%s] ", argument->symbol->string);
1024 print_quoted_string(&argument->constraints, '"', 1);
1026 print_expression(argument->expression);
1032 * Print assembler clobbers.
1034 * @param clobbers the clobbers
1036 static void print_asm_clobbers(asm_clobber_t *clobbers)
1038 asm_clobber_t *clobber = clobbers;
1039 for (; clobber != NULL; clobber = clobber->next) {
1040 if (clobber != clobbers)
1043 print_quoted_string(&clobber->clobber, '"', 1);
1048 * Print an assembler statement.
1050 * @param statement the statement
1052 static void print_asm_statement(const asm_statement_t *statement)
1054 print_string("asm ");
1055 if (statement->is_volatile) {
1056 print_string("volatile ");
1059 print_quoted_string(&statement->asm_text, '"', 1);
1060 if (statement->outputs == NULL &&
1061 statement->inputs == NULL &&
1062 statement->clobbers == NULL)
1063 goto end_of_print_asm_statement;
1065 print_string(" : ");
1066 print_asm_arguments(statement->outputs);
1067 if (statement->inputs == NULL && statement->clobbers == NULL)
1068 goto end_of_print_asm_statement;
1070 print_string(" : ");
1071 print_asm_arguments(statement->inputs);
1072 if (statement->clobbers == NULL)
1073 goto end_of_print_asm_statement;
1075 print_string(" : ");
1076 print_asm_clobbers(statement->clobbers);
1078 end_of_print_asm_statement:
1083 * Print a microsoft __try statement.
1085 * @param statement the statement
1087 static void print_ms_try_statement(const ms_try_statement_t *statement)
1089 print_string("__try");
1090 print_inner_statement(statement->try_statement);
1091 print_after_inner_statement(statement->try_statement);
1092 if (statement->except_expression != NULL) {
1093 print_string("__except(");
1094 print_expression(statement->except_expression);
1097 print_string("__finally");
1099 print_inner_statement(statement->final_statement);
1103 * Print a microsoft __leave statement.
1105 * @param statement the statement
1107 static void print_leave_statement(const leave_statement_t *statement)
1110 print_string("__leave;");
1114 * Print a statement.
1116 * @param statement the statement
1118 void print_statement(statement_t const *const stmt)
1120 switch (stmt->kind) {
1121 case STATEMENT_ASM: print_asm_statement( &stmt->asms); break;
1122 case STATEMENT_BREAK: print_string("break;"); break;
1123 case STATEMENT_CASE_LABEL: print_case_label( &stmt->case_label); break;
1124 case STATEMENT_COMPOUND: print_compound_statement( &stmt->compound); break;
1125 case STATEMENT_COMPUTED_GOTO: print_computed_goto_statement(&stmt->computed_goto); break;
1126 case STATEMENT_CONTINUE: print_string("continue;"); break;
1127 case STATEMENT_DECLARATION: print_declaration_statement( &stmt->declaration); break;
1128 case STATEMENT_DO_WHILE: print_do_while_statement( &stmt->do_while); break;
1129 case STATEMENT_EMPTY: print_char(';'); break;
1130 case STATEMENT_ERROR: print_string("$error statement$"); break;
1131 case STATEMENT_EXPRESSION: print_expression_statement( &stmt->expression); break;
1132 case STATEMENT_FOR: print_for_statement( &stmt->fors); break;
1133 case STATEMENT_GOTO: print_goto_statement( &stmt->gotos); break;
1134 case STATEMENT_IF: print_if_statement( &stmt->ifs); break;
1135 case STATEMENT_LABEL: print_label_statement( &stmt->label); break;
1136 case STATEMENT_LEAVE: print_leave_statement( &stmt->leave); break;
1137 case STATEMENT_MS_TRY: print_ms_try_statement( &stmt->ms_try); break;
1138 case STATEMENT_RETURN: print_return_statement( &stmt->returns); break;
1139 case STATEMENT_SWITCH: print_switch_statement( &stmt->switchs); break;
1140 case STATEMENT_WHILE: print_while_statement( &stmt->whiles); break;
1145 * Print a storage class.
1147 * @param storage_class the storage class
1149 static void print_storage_class(storage_class_tag_t storage_class)
1151 switch (storage_class) {
1152 case STORAGE_CLASS_NONE: return;
1153 case STORAGE_CLASS_TYPEDEF: print_string("typedef "); return;
1154 case STORAGE_CLASS_EXTERN: print_string("extern "); return;
1155 case STORAGE_CLASS_STATIC: print_string("static "); return;
1156 case STORAGE_CLASS_AUTO: print_string("auto "); return;
1157 case STORAGE_CLASS_REGISTER: print_string("register "); return;
1159 panic("invalid storage class");
1163 * Print an initializer.
1165 * @param initializer the initializer
1167 void print_initializer(const initializer_t *initializer)
1169 if (initializer == NULL) {
1174 switch (initializer->kind) {
1175 case INITIALIZER_VALUE: {
1176 const initializer_value_t *value = &initializer->value;
1177 print_assignment_expression(value->value);
1180 case INITIALIZER_LIST: {
1181 assert(initializer->kind == INITIALIZER_LIST);
1183 const initializer_list_t *list = &initializer->list;
1185 for (size_t i = 0 ; i < list->len; ++i) {
1186 const initializer_t *sub_init = list->initializers[i];
1187 print_initializer(list->initializers[i]);
1188 if (i < list->len-1) {
1189 if (sub_init == NULL || sub_init->kind != INITIALIZER_DESIGNATOR)
1196 case INITIALIZER_STRING:
1197 print_quoted_string(&initializer->string.string, '"', 1);
1199 case INITIALIZER_WIDE_STRING:
1200 print_quoted_string(&initializer->string.string, '"', 1);
1202 case INITIALIZER_DESIGNATOR:
1203 print_designator(initializer->designator.designator);
1204 print_string(" = ");
1208 panic("invalid initializer kind found");
1213 * Print microsoft extended declaration modifiers.
1215 static void print_ms_modifiers(const declaration_t *declaration)
1217 if ((c_mode & _MS) == 0)
1220 decl_modifiers_t modifiers = declaration->modifiers;
1222 bool ds_shown = false;
1223 const char *next = "(";
1225 if (declaration->base.kind == ENTITY_VARIABLE) {
1226 variable_t *variable = (variable_t*)declaration;
1227 if (variable->alignment != 0
1228 || variable->get_property_sym != NULL
1229 || variable->put_property_sym != NULL) {
1231 print_string("__declspec");
1235 if (variable->alignment != 0) {
1236 print_string(next); next = ", "; print_format("align(%u)", variable->alignment);
1238 if (variable->get_property_sym != NULL
1239 || variable->put_property_sym != NULL) {
1241 print_string(next); next = ", "; print_string("property(");
1242 if (variable->get_property_sym != NULL) {
1243 print_format("get=%s", variable->get_property_sym->string);
1246 if (variable->put_property_sym != NULL)
1247 print_format("%sput=%s", comma, variable->put_property_sym->string);
1253 /* DM_FORCEINLINE handled outside. */
1254 if ((modifiers & ~DM_FORCEINLINE) != 0) {
1256 print_string("__declspec");
1259 if (modifiers & DM_DLLIMPORT) {
1260 print_string(next); next = ", "; print_string("dllimport");
1262 if (modifiers & DM_DLLEXPORT) {
1263 print_string(next); next = ", "; print_string("dllexport");
1265 if (modifiers & DM_THREAD) {
1266 print_string(next); next = ", "; print_string("thread");
1268 if (modifiers & DM_NAKED) {
1269 print_string(next); next = ", "; print_string("naked");
1271 if (modifiers & DM_THREAD) {
1272 print_string(next); next = ", "; print_string("thread");
1274 if (modifiers & DM_SELECTANY) {
1275 print_string(next); next = ", "; print_string("selectany");
1277 if (modifiers & DM_NOTHROW) {
1278 print_string(next); next = ", "; print_string("nothrow");
1280 if (modifiers & DM_NORETURN) {
1281 print_string(next); next = ", "; print_string("noreturn");
1283 if (modifiers & DM_NOINLINE) {
1284 print_string(next); next = ", "; print_string("noinline");
1286 if (modifiers & DM_DEPRECATED) {
1287 print_string(next); next = ", "; print_string("deprecated");
1288 if (declaration->deprecated_string != NULL)
1289 print_format("(\"%s\")",
1290 declaration->deprecated_string);
1292 if (modifiers & DM_RESTRICT) {
1293 print_string(next); next = ", "; print_string("restrict");
1295 if (modifiers & DM_NOALIAS) {
1296 print_string(next); next = ", "; print_string("noalias");
1305 static void print_scope(const scope_t *scope)
1307 const entity_t *entity = scope->entities;
1308 for ( ; entity != NULL; entity = entity->base.next) {
1310 print_entity(entity);
1315 static void print_namespace(const namespace_t *namespace)
1317 print_string("namespace ");
1318 if (namespace->base.symbol != NULL) {
1319 print_string(namespace->base.symbol->string);
1323 print_string("{\n");
1326 print_scope(&namespace->members);
1330 print_string("}\n");
1334 * Print a variable or function declaration
1336 void print_declaration(const entity_t *entity)
1338 assert(is_declaration(entity));
1339 const declaration_t *declaration = &entity->declaration;
1341 print_storage_class((storage_class_tag_t)declaration->declared_storage_class);
1342 if (entity->kind == ENTITY_FUNCTION) {
1343 function_t *function = (function_t*)declaration;
1344 if (function->is_inline) {
1345 if (declaration->modifiers & DM_FORCEINLINE) {
1346 print_string("__forceinline ");
1347 } else if (declaration->modifiers & DM_MICROSOFT_INLINE) {
1348 print_string("__inline ");
1350 print_string("inline ");
1354 //print_ms_modifiers(declaration);
1355 switch (entity->kind) {
1356 case ENTITY_FUNCTION:
1357 print_type_ext(entity->declaration.type, entity->base.symbol,
1358 &entity->function.parameters);
1360 if (entity->function.statement != NULL) {
1362 print_indented_statement(entity->function.statement);
1368 case ENTITY_VARIABLE:
1369 if (entity->variable.thread_local)
1370 print_string("__thread ");
1371 print_type_ext(declaration->type, declaration->base.symbol, NULL);
1372 if (entity->variable.initializer != NULL) {
1373 print_string(" = ");
1374 print_initializer(entity->variable.initializer);
1378 case ENTITY_COMPOUND_MEMBER:
1379 print_type_ext(declaration->type, declaration->base.symbol, NULL);
1380 if (entity->compound_member.bitfield) {
1381 print_format(" : %u", entity->compound_member.bit_size);
1386 print_type_ext(declaration->type, declaration->base.symbol, NULL);
1393 * Prints an expression.
1395 * @param expression the expression
1397 void print_expression(const expression_t *expression)
1399 print_expression_prec(expression, PREC_BOTTOM);
1403 * Print a declaration.
1405 * @param declaration the declaration
1407 void print_entity(const entity_t *entity)
1409 if (entity->base.namespc != NAMESPACE_NORMAL && entity->base.symbol == NULL)
1412 switch ((entity_kind_tag_t)entity->kind) {
1413 case ENTITY_VARIABLE:
1414 case ENTITY_PARAMETER:
1415 case ENTITY_COMPOUND_MEMBER:
1416 case ENTITY_FUNCTION:
1417 print_declaration(entity);
1419 case ENTITY_TYPEDEF:
1420 print_typedef(entity);
1424 print_string("class ");
1425 print_string(entity->base.symbol->string);
1426 print_string("; /* TODO */\n");
1429 print_string("struct ");
1430 goto print_compound;
1432 print_string("union ");
1434 print_string(entity->base.symbol->string);
1435 if (entity->compound.complete) {
1437 print_compound_definition(&entity->compound);
1442 print_string("enum ");
1443 print_string(entity->base.symbol->string);
1445 print_enum_definition(&entity->enume);
1448 case ENTITY_NAMESPACE:
1449 print_namespace(&entity->namespacee);
1451 case ENTITY_LOCAL_LABEL:
1452 print_string("__label__ ");
1453 print_string(entity->base.symbol->string);
1457 case ENTITY_ENUM_VALUE:
1458 panic("print_entity used on unexpected entity type");
1460 panic("Invalid entity type encountered");
1464 * Print the AST of a translation unit.
1466 * @param unit the translation unit
1468 void print_ast(const translation_unit_t *unit)
1470 entity_t *entity = unit->scope.entities;
1471 for ( ; entity != NULL; entity = entity->base.next) {
1472 if (entity->kind == ENTITY_ENUM_VALUE)
1474 if (entity->base.namespc != NAMESPACE_NORMAL
1475 && entity->base.symbol == NULL)
1477 if (is_generated_entity(entity))
1481 print_entity(entity);
1486 expression_classification_t is_constant_initializer(const initializer_t *initializer)
1488 switch (initializer->kind) {
1489 case INITIALIZER_STRING:
1490 case INITIALIZER_WIDE_STRING:
1491 case INITIALIZER_DESIGNATOR:
1492 return EXPR_CLASS_CONSTANT;
1494 case INITIALIZER_VALUE:
1495 return is_linker_constant(initializer->value.value);
1497 case INITIALIZER_LIST: {
1498 expression_classification_t all = EXPR_CLASS_CONSTANT;
1499 for (size_t i = 0; i < initializer->list.len; ++i) {
1500 initializer_t *sub_initializer = initializer->list.initializers[i];
1501 expression_classification_t const cur = is_constant_initializer(sub_initializer);
1509 panic("invalid initializer kind found");
1513 * Checks if an expression references an object with a constant/known location
1514 * to the linker. Example:
1515 * - "x", "*&x" with x being a global variable. The value of x need not be
1516 * constant but the address of x is.
1517 * - "a.b.c" when a has a constant/known location to the linker
1519 static expression_classification_t is_object_with_linker_constant_address(
1520 const expression_t *expression)
1522 switch (expression->kind) {
1523 case EXPR_UNARY_DEREFERENCE:
1524 return is_linker_constant(expression->unary.value);
1527 type_t *base_type = skip_typeref(expression->select.compound->base.type);
1528 if (is_type_pointer(base_type)) {
1530 return is_linker_constant(expression->select.compound);
1532 return is_object_with_linker_constant_address(expression->select.compound);
1536 case EXPR_ARRAY_ACCESS: {
1537 expression_classification_t const ref = is_linker_constant(expression->array_access.array_ref);
1538 expression_classification_t const idx = is_constant_expression(expression->array_access.index);
1539 return ref < idx ? ref : idx;
1542 case EXPR_REFERENCE: {
1543 entity_t *entity = expression->reference.entity;
1544 if (!is_declaration(entity))
1545 return EXPR_CLASS_VARIABLE;
1547 switch ((storage_class_tag_t)entity->declaration.storage_class) {
1548 case STORAGE_CLASS_NONE:
1549 case STORAGE_CLASS_EXTERN:
1550 case STORAGE_CLASS_STATIC:
1552 entity->kind != ENTITY_VARIABLE ||
1553 !entity->variable.thread_local ? EXPR_CLASS_CONSTANT :
1554 EXPR_CLASS_VARIABLE;
1556 case STORAGE_CLASS_REGISTER:
1557 case STORAGE_CLASS_TYPEDEF:
1558 case STORAGE_CLASS_AUTO:
1561 return EXPR_CLASS_VARIABLE;
1565 return EXPR_CLASS_ERROR;
1568 return EXPR_CLASS_VARIABLE;
1572 expression_classification_t is_linker_constant(const expression_t *expression)
1574 switch (expression->kind) {
1575 case EXPR_STRING_LITERAL:
1576 case EXPR_WIDE_STRING_LITERAL:
1578 case EXPR_LABEL_ADDRESS:
1579 return EXPR_CLASS_CONSTANT;
1581 case EXPR_COMPOUND_LITERAL:
1582 return is_constant_initializer(expression->compound_literal.initializer);
1584 case EXPR_UNARY_TAKE_ADDRESS:
1585 return is_object_with_linker_constant_address(expression->unary.value);
1587 case EXPR_UNARY_DEREFERENCE: {
1589 = revert_automatic_type_conversion(expression->unary.value);
1590 /* dereferencing a function is a NOP */
1591 if (is_type_function(real_type)) {
1592 return is_linker_constant(expression->unary.value);
1597 case EXPR_UNARY_CAST: {
1598 type_t *dest = skip_typeref(expression->base.type);
1599 if (!is_type_pointer(dest) && (
1600 dest->kind != TYPE_ATOMIC ||
1601 !(get_atomic_type_flags(dest->atomic.akind) & ATOMIC_TYPE_FLAG_INTEGER) ||
1602 get_atomic_type_size(dest->atomic.akind) < get_type_size(type_void_ptr)
1604 return is_constant_expression(expression);
1606 return is_linker_constant(expression->unary.value);
1609 case EXPR_BINARY_ADD:
1610 case EXPR_BINARY_SUB: {
1611 expression_t *const left = expression->binary.left;
1612 expression_t *const right = expression->binary.right;
1613 type_t *const ltype = skip_typeref(left->base.type);
1614 type_t *const rtype = skip_typeref(right->base.type);
1616 if (is_type_pointer(ltype)) {
1617 expression_classification_t const l = is_linker_constant(left);
1618 expression_classification_t const r = is_constant_expression(right);
1619 return l < r ? l : r;
1620 } else if (is_type_pointer(rtype)) {
1621 expression_classification_t const l = is_constant_expression(left);
1622 expression_classification_t const r = is_linker_constant(right);
1623 return l < r ? l : r;
1624 } else if (!is_type_valid(ltype) || !is_type_valid(rtype)) {
1625 return EXPR_CLASS_ERROR;
1627 return is_constant_expression(expression);
1631 case EXPR_REFERENCE: {
1632 entity_t *entity = expression->reference.entity;
1633 if (!is_declaration(entity))
1634 return EXPR_CLASS_VARIABLE;
1636 type_t *type = skip_typeref(entity->declaration.type);
1637 if (is_type_function(type))
1638 return EXPR_CLASS_CONSTANT;
1639 if (is_type_array(type)) {
1640 return is_object_with_linker_constant_address(expression);
1642 /* Prevent stray errors */
1643 if (!is_type_valid(type))
1644 return EXPR_CLASS_ERROR;
1645 return EXPR_CLASS_VARIABLE;
1648 case EXPR_ARRAY_ACCESS: {
1649 type_t *const type =
1650 skip_typeref(revert_automatic_type_conversion(expression));
1651 if (!is_type_array(type))
1652 return EXPR_CLASS_VARIABLE;
1653 return is_linker_constant(expression->array_access.array_ref);
1656 case EXPR_CONDITIONAL: {
1657 expression_t *const c = expression->conditional.condition;
1658 expression_classification_t const cclass = is_constant_expression(c);
1659 if (cclass != EXPR_CLASS_CONSTANT)
1662 if (fold_constant_to_bool(c)) {
1663 expression_t const *const t = expression->conditional.true_expression;
1664 return is_linker_constant(t != NULL ? t : c);
1666 return is_linker_constant(expression->conditional.false_expression);
1671 entity_t *entity = expression->select.compound_entry;
1672 if (!is_declaration(entity))
1673 return EXPR_CLASS_VARIABLE;
1674 type_t *type = skip_typeref(entity->declaration.type);
1675 if (is_type_array(type)) {
1676 /* arrays automatically convert to their address */
1677 expression_t *compound = expression->select.compound;
1678 type_t *base_type = skip_typeref(compound->base.type);
1679 if (is_type_pointer(base_type)) {
1681 return is_linker_constant(compound);
1683 return is_object_with_linker_constant_address(compound);
1686 return EXPR_CLASS_VARIABLE;
1690 return is_constant_expression(expression);
1695 * Check if the given expression is a call to a builtin function
1696 * returning a constant result.
1698 static expression_classification_t is_builtin_const_call(const expression_t *expression)
1700 expression_t *function = expression->call.function;
1701 if (function->kind != EXPR_REFERENCE)
1702 return EXPR_CLASS_VARIABLE;
1703 reference_expression_t *ref = &function->reference;
1704 if (ref->entity->kind != ENTITY_FUNCTION)
1705 return EXPR_CLASS_VARIABLE;
1707 switch (ref->entity->function.btk) {
1710 return EXPR_CLASS_CONSTANT;
1712 return EXPR_CLASS_VARIABLE;
1717 static expression_classification_t is_constant_pointer(const expression_t *expression)
1719 expression_classification_t const expr_class = is_constant_expression(expression);
1720 if (expr_class != EXPR_CLASS_VARIABLE)
1723 switch (expression->kind) {
1724 case EXPR_UNARY_CAST:
1725 return is_constant_pointer(expression->unary.value);
1727 return EXPR_CLASS_VARIABLE;
1731 static expression_classification_t is_object_with_constant_address(const expression_t *expression)
1733 switch (expression->kind) {
1735 expression_t *compound = expression->select.compound;
1736 type_t *compound_type = compound->base.type;
1737 compound_type = skip_typeref(compound_type);
1738 if (is_type_pointer(compound_type)) {
1739 return is_constant_pointer(compound);
1741 return is_object_with_constant_address(compound);
1745 case EXPR_ARRAY_ACCESS: {
1746 array_access_expression_t const* const array_access =
1747 &expression->array_access;
1748 expression_classification_t const idx_class = is_constant_expression(array_access->index);
1749 if (idx_class != EXPR_CLASS_CONSTANT)
1751 expression_classification_t const ref_addr = is_object_with_constant_address(array_access->array_ref);
1752 expression_classification_t const ref_ptr = is_constant_pointer(array_access->array_ref);
1753 return ref_addr > ref_ptr ? ref_addr : ref_ptr;
1756 case EXPR_UNARY_DEREFERENCE:
1757 return is_constant_pointer(expression->unary.value);
1760 return EXPR_CLASS_ERROR;
1763 return EXPR_CLASS_VARIABLE;
1767 expression_classification_t is_constant_expression(const expression_t *expression)
1769 switch (expression->kind) {
1770 case EXPR_LITERAL_CASES:
1771 case EXPR_CLASSIFY_TYPE:
1774 case EXPR_BUILTIN_CONSTANT_P:
1775 case EXPR_BUILTIN_TYPES_COMPATIBLE_P:
1776 case EXPR_ENUM_CONSTANT:
1777 return EXPR_CLASS_CONSTANT;
1780 type_t *const type = skip_typeref(expression->typeprop.type);
1782 !is_type_array(type) || !type->array.is_vla ? EXPR_CLASS_CONSTANT :
1783 EXPR_CLASS_VARIABLE;
1786 case EXPR_STRING_LITERAL:
1787 case EXPR_WIDE_STRING_LITERAL:
1789 case EXPR_LABEL_ADDRESS:
1794 case EXPR_STATEMENT:
1795 case EXPR_UNARY_POSTFIX_INCREMENT:
1796 case EXPR_UNARY_POSTFIX_DECREMENT:
1797 case EXPR_UNARY_PREFIX_INCREMENT:
1798 case EXPR_UNARY_PREFIX_DECREMENT:
1799 case EXPR_UNARY_ASSUME: /* has VOID type */
1800 case EXPR_UNARY_DEREFERENCE:
1801 case EXPR_UNARY_DELETE:
1802 case EXPR_UNARY_DELETE_ARRAY:
1803 case EXPR_UNARY_THROW:
1804 case EXPR_BINARY_ASSIGN:
1805 case EXPR_BINARY_MUL_ASSIGN:
1806 case EXPR_BINARY_DIV_ASSIGN:
1807 case EXPR_BINARY_MOD_ASSIGN:
1808 case EXPR_BINARY_ADD_ASSIGN:
1809 case EXPR_BINARY_SUB_ASSIGN:
1810 case EXPR_BINARY_SHIFTLEFT_ASSIGN:
1811 case EXPR_BINARY_SHIFTRIGHT_ASSIGN:
1812 case EXPR_BINARY_BITWISE_AND_ASSIGN:
1813 case EXPR_BINARY_BITWISE_XOR_ASSIGN:
1814 case EXPR_BINARY_BITWISE_OR_ASSIGN:
1815 case EXPR_BINARY_COMMA:
1816 case EXPR_ARRAY_ACCESS:
1817 return EXPR_CLASS_VARIABLE;
1819 case EXPR_REFERENCE: {
1820 type_t *const type = skip_typeref(expression->base.type);
1821 return is_type_valid(type) ? EXPR_CLASS_VARIABLE : EXPR_CLASS_ERROR;
1824 case EXPR_UNARY_TAKE_ADDRESS:
1825 return is_object_with_constant_address(expression->unary.value);
1828 return is_builtin_const_call(expression);
1830 case EXPR_UNARY_NEGATE:
1831 case EXPR_UNARY_PLUS:
1832 case EXPR_UNARY_BITWISE_NEGATE:
1833 case EXPR_UNARY_NOT:
1834 return is_constant_expression(expression->unary.value);
1836 case EXPR_UNARY_CAST: {
1837 type_t *const type = skip_typeref(expression->base.type);
1838 if (is_type_scalar(type))
1839 return is_constant_expression(expression->unary.value);
1840 if (!is_type_valid(type))
1841 return EXPR_CLASS_ERROR;
1842 return EXPR_CLASS_VARIABLE;
1845 case EXPR_BINARY_ADD:
1846 case EXPR_BINARY_SUB:
1847 case EXPR_BINARY_MUL:
1848 case EXPR_BINARY_DIV:
1849 case EXPR_BINARY_MOD:
1850 case EXPR_BINARY_EQUAL:
1851 case EXPR_BINARY_NOTEQUAL:
1852 case EXPR_BINARY_LESS:
1853 case EXPR_BINARY_LESSEQUAL:
1854 case EXPR_BINARY_GREATER:
1855 case EXPR_BINARY_GREATEREQUAL:
1856 case EXPR_BINARY_BITWISE_AND:
1857 case EXPR_BINARY_BITWISE_OR:
1858 case EXPR_BINARY_BITWISE_XOR:
1859 case EXPR_BINARY_SHIFTLEFT:
1860 case EXPR_BINARY_SHIFTRIGHT:
1861 case EXPR_BINARY_ISGREATER:
1862 case EXPR_BINARY_ISGREATEREQUAL:
1863 case EXPR_BINARY_ISLESS:
1864 case EXPR_BINARY_ISLESSEQUAL:
1865 case EXPR_BINARY_ISLESSGREATER:
1866 case EXPR_BINARY_ISUNORDERED: {
1867 expression_classification_t const l = is_constant_expression(expression->binary.left);
1868 expression_classification_t const r = is_constant_expression(expression->binary.right);
1869 return l < r ? l : r;
1872 case EXPR_BINARY_LOGICAL_AND: {
1873 expression_t const *const left = expression->binary.left;
1874 expression_classification_t const lclass = is_constant_expression(left);
1875 if (lclass != EXPR_CLASS_CONSTANT)
1877 if (!fold_constant_to_bool(left))
1878 return EXPR_CLASS_CONSTANT;
1879 return is_constant_expression(expression->binary.right);
1882 case EXPR_BINARY_LOGICAL_OR: {
1883 expression_t const *const left = expression->binary.left;
1884 expression_classification_t const lclass = is_constant_expression(left);
1885 if (lclass != EXPR_CLASS_CONSTANT)
1887 if (fold_constant_to_bool(left))
1888 return EXPR_CLASS_CONSTANT;
1889 return is_constant_expression(expression->binary.right);
1892 case EXPR_COMPOUND_LITERAL:
1893 return is_constant_initializer(expression->compound_literal.initializer);
1895 case EXPR_CONDITIONAL: {
1896 expression_t *const condition = expression->conditional.condition;
1897 expression_classification_t const cclass = is_constant_expression(condition);
1898 if (cclass != EXPR_CLASS_CONSTANT)
1901 if (fold_constant_to_bool(condition)) {
1902 expression_t const *const t = expression->conditional.true_expression;
1903 return t == NULL ? EXPR_CLASS_CONSTANT : is_constant_expression(t);
1905 return is_constant_expression(expression->conditional.false_expression);
1910 return EXPR_CLASS_ERROR;
1912 panic("invalid expression found (is constant expression)");
1917 obstack_init(&ast_obstack);
1922 obstack_free(&ast_obstack, NULL);