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_REFERENCE_ENUM_VALUE] = PREC_PRIMARY,
107 [EXPR_LITERAL_INTEGER] = PREC_PRIMARY,
108 [EXPR_LITERAL_INTEGER_OCTAL] = PREC_PRIMARY,
109 [EXPR_LITERAL_INTEGER_HEXADECIMAL] = PREC_PRIMARY,
110 [EXPR_LITERAL_FLOATINGPOINT] = PREC_PRIMARY,
111 [EXPR_LITERAL_FLOATINGPOINT_HEXADECIMAL] = PREC_PRIMARY,
112 [EXPR_LITERAL_CHARACTER] = PREC_PRIMARY,
113 [EXPR_LITERAL_WIDE_CHARACTER] = PREC_PRIMARY,
114 [EXPR_LITERAL_MS_NOOP] = PREC_PRIMARY,
115 [EXPR_STRING_LITERAL] = PREC_PRIMARY,
116 [EXPR_WIDE_STRING_LITERAL] = PREC_PRIMARY,
117 [EXPR_COMPOUND_LITERAL] = PREC_UNARY,
118 [EXPR_CALL] = PREC_POSTFIX,
119 [EXPR_CONDITIONAL] = PREC_CONDITIONAL,
120 [EXPR_SELECT] = PREC_POSTFIX,
121 [EXPR_ARRAY_ACCESS] = PREC_POSTFIX,
122 [EXPR_SIZEOF] = PREC_UNARY,
123 [EXPR_CLASSIFY_TYPE] = PREC_UNARY,
124 [EXPR_ALIGNOF] = PREC_UNARY,
126 [EXPR_FUNCNAME] = PREC_PRIMARY,
127 [EXPR_BUILTIN_CONSTANT_P] = PREC_PRIMARY,
128 [EXPR_BUILTIN_TYPES_COMPATIBLE_P] = PREC_PRIMARY,
129 [EXPR_OFFSETOF] = PREC_PRIMARY,
130 [EXPR_VA_START] = PREC_PRIMARY,
131 [EXPR_VA_ARG] = PREC_PRIMARY,
132 [EXPR_VA_COPY] = PREC_PRIMARY,
133 [EXPR_STATEMENT] = PREC_PRIMARY,
134 [EXPR_LABEL_ADDRESS] = PREC_PRIMARY,
136 [EXPR_UNARY_NEGATE] = PREC_UNARY,
137 [EXPR_UNARY_PLUS] = PREC_UNARY,
138 [EXPR_UNARY_BITWISE_NEGATE] = PREC_UNARY,
139 [EXPR_UNARY_NOT] = PREC_UNARY,
140 [EXPR_UNARY_DEREFERENCE] = PREC_UNARY,
141 [EXPR_UNARY_TAKE_ADDRESS] = PREC_UNARY,
142 [EXPR_UNARY_POSTFIX_INCREMENT] = PREC_POSTFIX,
143 [EXPR_UNARY_POSTFIX_DECREMENT] = PREC_POSTFIX,
144 [EXPR_UNARY_PREFIX_INCREMENT] = PREC_UNARY,
145 [EXPR_UNARY_PREFIX_DECREMENT] = PREC_UNARY,
146 [EXPR_UNARY_CAST] = PREC_UNARY,
147 [EXPR_UNARY_ASSUME] = PREC_PRIMARY,
148 [EXPR_UNARY_DELETE] = PREC_UNARY,
149 [EXPR_UNARY_DELETE_ARRAY] = PREC_UNARY,
150 [EXPR_UNARY_THROW] = PREC_ASSIGNMENT,
152 [EXPR_BINARY_ADD] = PREC_ADDITIVE,
153 [EXPR_BINARY_SUB] = PREC_ADDITIVE,
154 [EXPR_BINARY_MUL] = PREC_MULTIPLICATIVE,
155 [EXPR_BINARY_DIV] = PREC_MULTIPLICATIVE,
156 [EXPR_BINARY_MOD] = PREC_MULTIPLICATIVE,
157 [EXPR_BINARY_EQUAL] = PREC_EQUALITY,
158 [EXPR_BINARY_NOTEQUAL] = PREC_EQUALITY,
159 [EXPR_BINARY_LESS] = PREC_RELATIONAL,
160 [EXPR_BINARY_LESSEQUAL] = PREC_RELATIONAL,
161 [EXPR_BINARY_GREATER] = PREC_RELATIONAL,
162 [EXPR_BINARY_GREATEREQUAL] = PREC_RELATIONAL,
163 [EXPR_BINARY_BITWISE_AND] = PREC_AND,
164 [EXPR_BINARY_BITWISE_OR] = PREC_OR,
165 [EXPR_BINARY_BITWISE_XOR] = PREC_XOR,
166 [EXPR_BINARY_LOGICAL_AND] = PREC_LOGICAL_AND,
167 [EXPR_BINARY_LOGICAL_OR] = PREC_LOGICAL_OR,
168 [EXPR_BINARY_SHIFTLEFT] = PREC_SHIFT,
169 [EXPR_BINARY_SHIFTRIGHT] = PREC_SHIFT,
170 [EXPR_BINARY_ASSIGN] = PREC_ASSIGNMENT,
171 [EXPR_BINARY_MUL_ASSIGN] = PREC_ASSIGNMENT,
172 [EXPR_BINARY_DIV_ASSIGN] = PREC_ASSIGNMENT,
173 [EXPR_BINARY_MOD_ASSIGN] = PREC_ASSIGNMENT,
174 [EXPR_BINARY_ADD_ASSIGN] = PREC_ASSIGNMENT,
175 [EXPR_BINARY_SUB_ASSIGN] = PREC_ASSIGNMENT,
176 [EXPR_BINARY_SHIFTLEFT_ASSIGN] = PREC_ASSIGNMENT,
177 [EXPR_BINARY_SHIFTRIGHT_ASSIGN] = PREC_ASSIGNMENT,
178 [EXPR_BINARY_BITWISE_AND_ASSIGN] = PREC_ASSIGNMENT,
179 [EXPR_BINARY_BITWISE_XOR_ASSIGN] = PREC_ASSIGNMENT,
180 [EXPR_BINARY_BITWISE_OR_ASSIGN] = PREC_ASSIGNMENT,
181 [EXPR_BINARY_COMMA] = PREC_EXPRESSION,
183 [EXPR_BINARY_ISGREATER] = PREC_PRIMARY,
184 [EXPR_BINARY_ISGREATEREQUAL] = PREC_PRIMARY,
185 [EXPR_BINARY_ISLESS] = PREC_PRIMARY,
186 [EXPR_BINARY_ISLESSEQUAL] = PREC_PRIMARY,
187 [EXPR_BINARY_ISLESSGREATER] = PREC_PRIMARY,
188 [EXPR_BINARY_ISUNORDERED] = PREC_PRIMARY
190 assert((size_t)kind < lengthof(prec));
191 unsigned res = prec[kind];
193 assert(res != PREC_BOTTOM);
198 * Print a quoted string constant.
200 * @param string the string constant
201 * @param border the border char
202 * @param skip number of chars to skip at the end
204 static void print_quoted_string(const string_t *const string, char border,
208 const char *end = string->begin + string->size - skip;
209 for (const char *c = string->begin; c != end; ++c) {
215 case '\\': print_string("\\\\"); break;
216 case '\a': print_string("\\a"); break;
217 case '\b': print_string("\\b"); break;
218 case '\f': print_string("\\f"); break;
219 case '\n': print_string("\\n"); break;
220 case '\r': print_string("\\r"); break;
221 case '\t': print_string("\\t"); break;
222 case '\v': print_string("\\v"); break;
223 case '\?': print_string("\\?"); break;
225 if (c_mode & _GNUC) {
226 print_string("\\e"); break;
230 if ((unsigned)tc < 0x80 && !isprint(tc)) {
231 print_format("\\%03o", (unsigned)tc);
241 static void print_string_literal(const string_literal_expression_t *literal)
243 if (literal->base.kind == EXPR_WIDE_STRING_LITERAL) {
246 print_quoted_string(&literal->value, '"', 1);
249 static void print_literal(const literal_expression_t *literal)
251 switch (literal->base.kind) {
252 case EXPR_LITERAL_MS_NOOP:
253 print_string("__noop");
255 case EXPR_LITERAL_INTEGER_HEXADECIMAL:
256 case EXPR_LITERAL_FLOATINGPOINT_HEXADECIMAL:
259 case EXPR_LITERAL_BOOLEAN:
260 case EXPR_LITERAL_INTEGER:
261 case EXPR_LITERAL_INTEGER_OCTAL:
262 case EXPR_LITERAL_FLOATINGPOINT:
263 print_stringrep(&literal->value);
264 if (literal->suffix.size > 0)
265 print_stringrep(&literal->suffix);
267 case EXPR_LITERAL_WIDE_CHARACTER:
270 case EXPR_LITERAL_CHARACTER:
271 print_quoted_string(&literal->value, '\'', 0);
276 print_string("INVALID LITERAL KIND");
280 * Prints a predefined symbol.
282 static void print_funcname(const funcname_expression_t *funcname)
285 switch (funcname->kind) {
286 case FUNCNAME_FUNCTION: s = (c_mode & _C99) ? "__func__" : "__FUNCTION__"; break;
287 case FUNCNAME_PRETTY_FUNCTION: s = "__PRETTY_FUNCTION__"; break;
288 case FUNCNAME_FUNCSIG: s = "__FUNCSIG__"; break;
289 case FUNCNAME_FUNCDNAME: s = "__FUNCDNAME__"; break;
294 static void print_compound_literal(
295 const compound_literal_expression_t *expression)
298 print_type(expression->type);
300 print_initializer(expression->initializer);
303 static void print_assignment_expression(const expression_t *const expr)
305 print_expression_prec(expr, PREC_ASSIGNMENT);
309 * Prints a call expression.
311 * @param call the call expression
313 static void print_call_expression(const call_expression_t *call)
315 print_expression_prec(call->function, PREC_POSTFIX);
317 char const *sep = "";
318 for (call_argument_t const *arg = call->arguments; arg; arg = arg->next) {
321 print_assignment_expression(arg->expression);
327 * Prints a binary expression.
329 * @param binexpr the binary expression
331 static void print_binary_expression(const binary_expression_t *binexpr)
333 unsigned prec = get_expression_precedence(binexpr->base.kind);
334 int r2l = right_to_left(prec);
336 print_expression_prec(binexpr->left, prec + r2l);
338 switch (binexpr->base.kind) {
339 case EXPR_BINARY_COMMA: op = ", "; break;
340 case EXPR_BINARY_ASSIGN: op = " = "; break;
341 case EXPR_BINARY_ADD: op = " + "; break;
342 case EXPR_BINARY_SUB: op = " - "; break;
343 case EXPR_BINARY_MUL: op = " * "; break;
344 case EXPR_BINARY_MOD: op = " % "; break;
345 case EXPR_BINARY_DIV: op = " / "; break;
346 case EXPR_BINARY_BITWISE_OR: op = " | "; break;
347 case EXPR_BINARY_BITWISE_AND: op = " & "; break;
348 case EXPR_BINARY_BITWISE_XOR: op = " ^ "; break;
349 case EXPR_BINARY_LOGICAL_OR: op = " || "; break;
350 case EXPR_BINARY_LOGICAL_AND: op = " && "; break;
351 case EXPR_BINARY_NOTEQUAL: op = " != "; break;
352 case EXPR_BINARY_EQUAL: op = " == "; break;
353 case EXPR_BINARY_LESS: op = " < "; break;
354 case EXPR_BINARY_LESSEQUAL: op = " <= "; break;
355 case EXPR_BINARY_GREATER: op = " > "; break;
356 case EXPR_BINARY_GREATEREQUAL: op = " >= "; break;
357 case EXPR_BINARY_SHIFTLEFT: op = " << "; break;
358 case EXPR_BINARY_SHIFTRIGHT: op = " >> "; break;
360 case EXPR_BINARY_ADD_ASSIGN: op = " += "; break;
361 case EXPR_BINARY_SUB_ASSIGN: op = " -= "; break;
362 case EXPR_BINARY_MUL_ASSIGN: op = " *= "; break;
363 case EXPR_BINARY_MOD_ASSIGN: op = " %= "; break;
364 case EXPR_BINARY_DIV_ASSIGN: op = " /= "; break;
365 case EXPR_BINARY_BITWISE_OR_ASSIGN: op = " |= "; break;
366 case EXPR_BINARY_BITWISE_AND_ASSIGN: op = " &= "; break;
367 case EXPR_BINARY_BITWISE_XOR_ASSIGN: op = " ^= "; break;
368 case EXPR_BINARY_SHIFTLEFT_ASSIGN: op = " <<= "; break;
369 case EXPR_BINARY_SHIFTRIGHT_ASSIGN: op = " >>= "; break;
370 default: panic("invalid binexpression found");
373 print_expression_prec(binexpr->right, prec + 1 - r2l);
377 * Prints an unary expression.
379 * @param unexpr the unary expression
381 static void print_unary_expression(const unary_expression_t *unexpr)
383 unsigned prec = get_expression_precedence(unexpr->base.kind);
384 switch (unexpr->base.kind) {
385 case EXPR_UNARY_NEGATE: print_char ('-' ); break;
386 case EXPR_UNARY_PLUS: print_char ('+' ); break;
387 case EXPR_UNARY_NOT: print_char ('!' ); break;
388 case EXPR_UNARY_BITWISE_NEGATE: print_char ('~' ); break;
389 case EXPR_UNARY_PREFIX_INCREMENT: print_string("++"); break;
390 case EXPR_UNARY_PREFIX_DECREMENT: print_string("--"); break;
391 case EXPR_UNARY_DEREFERENCE: print_char ('*' ); break;
392 case EXPR_UNARY_TAKE_ADDRESS: print_char ('&' ); break;
393 case EXPR_UNARY_DELETE: print_string("delete "); break;
394 case EXPR_UNARY_DELETE_ARRAY: print_string("delete [] "); break;
396 case EXPR_UNARY_POSTFIX_INCREMENT:
397 print_expression_prec(unexpr->value, prec);
400 case EXPR_UNARY_POSTFIX_DECREMENT:
401 print_expression_prec(unexpr->value, prec);
404 case EXPR_UNARY_CAST:
406 print_type(unexpr->base.type);
409 case EXPR_UNARY_ASSUME:
410 print_string("__assume(");
411 print_assignment_expression(unexpr->value);
415 case EXPR_UNARY_THROW:
416 if (unexpr->value == NULL) {
417 print_string("throw");
420 print_string("throw ");
424 panic("invalid unary expression found");
426 print_expression_prec(unexpr->value, prec);
430 * Prints a reference expression.
432 * @param ref the reference expression
434 static void print_reference_expression(const reference_expression_t *ref)
436 print_string(ref->entity->base.symbol->string);
440 * Prints a label address expression.
442 * @param ref the reference expression
444 static void print_label_address_expression(const label_address_expression_t *le)
446 print_format("&&%s", le->label->base.symbol->string);
450 * Prints an array expression.
452 * @param expression the array expression
454 static void print_array_expression(const array_access_expression_t *expression)
456 if (!expression->flipped) {
457 print_expression_prec(expression->array_ref, PREC_POSTFIX);
459 print_expression(expression->index);
462 print_expression_prec(expression->index, PREC_POSTFIX);
464 print_expression(expression->array_ref);
470 * Prints a typeproperty expression (sizeof or __alignof__).
472 * @param expression the type property expression
474 static void print_typeprop_expression(const typeprop_expression_t *expression)
476 if (expression->base.kind == EXPR_SIZEOF) {
477 print_string("sizeof");
479 assert(expression->base.kind == EXPR_ALIGNOF);
480 print_string("__alignof__");
482 if (expression->tp_expression != NULL) {
483 /* PREC_TOP: always print the '()' here, sizeof x is right but unusual */
484 print_expression_prec(expression->tp_expression, PREC_TOP);
487 print_type(expression->type);
493 * Prints a builtin constant expression.
495 * @param expression the builtin constant expression
497 static void print_builtin_constant(const builtin_constant_expression_t *expression)
499 print_string("__builtin_constant_p(");
500 print_assignment_expression(expression->value);
505 * Prints a builtin types compatible expression.
507 * @param expression the builtin types compatible expression
509 static void print_builtin_types_compatible(
510 const builtin_types_compatible_expression_t *expression)
512 print_string("__builtin_types_compatible_p(");
513 print_type(expression->left);
515 print_type(expression->right);
520 * Prints a conditional expression.
522 * @param expression the conditional expression
524 static void print_conditional(const conditional_expression_t *expression)
526 print_expression_prec(expression->condition, PREC_LOGICAL_OR);
527 if (expression->true_expression != NULL) {
529 print_expression_prec(expression->true_expression, PREC_EXPRESSION);
532 print_string(" ?: ");
534 precedence_t prec = c_mode & _CXX ? PREC_ASSIGNMENT : PREC_CONDITIONAL;
535 print_expression_prec(expression->false_expression, prec);
539 * Prints a va_start expression.
541 * @param expression the va_start expression
543 static void print_va_start(const va_start_expression_t *const expression)
545 print_string("__builtin_va_start(");
546 print_assignment_expression(expression->ap);
548 print_string(expression->parameter->base.base.symbol->string);
553 * Prints a va_arg expression.
555 * @param expression the va_arg expression
557 static void print_va_arg(const va_arg_expression_t *expression)
559 print_string("__builtin_va_arg(");
560 print_assignment_expression(expression->ap);
562 print_type(expression->base.type);
567 * Prints a va_copy expression.
569 * @param expression the va_copy expression
571 static void print_va_copy(const va_copy_expression_t *expression)
573 print_string("__builtin_va_copy(");
574 print_assignment_expression(expression->dst);
576 print_assignment_expression(expression->src);
581 * Prints a select expression (. or ->).
583 * @param expression the select expression
585 static void print_select(const select_expression_t *expression)
587 print_expression_prec(expression->compound, PREC_POSTFIX);
588 /* do not print anything for anonymous struct/union selects
589 * FIXME: if the anonymous select was a '->' this will print '.'
591 if (expression->compound_entry->base.symbol == NULL)
594 if (is_type_pointer(skip_typeref(expression->compound->base.type))) {
599 print_string(expression->compound_entry->base.symbol->string);
603 * Prints a type classify expression.
605 * @param expr the type classify expression
607 static void print_classify_type_expression(
608 const classify_type_expression_t *const expr)
610 print_string("__builtin_classify_type(");
611 print_assignment_expression(expr->type_expression);
616 * Prints a designator.
618 * @param designator the designator
620 static void print_designator(const designator_t *designator)
622 for ( ; designator != NULL; designator = designator->next) {
623 if (designator->symbol == NULL) {
625 print_expression(designator->array_index);
629 print_string(designator->symbol->string);
635 * Prints an offsetof expression.
637 * @param expression the offset expression
639 static void print_offsetof_expression(const offsetof_expression_t *expression)
641 print_string("__builtin_offsetof(");
642 print_type(expression->type);
644 print_designator(expression->designator);
649 * Prints a statement expression.
651 * @param expression the statement expression
653 static void print_statement_expression(const statement_expression_t *expression)
656 print_statement(expression->statement);
661 * Prints an expression with parenthesis if needed.
663 * @param expression the expression to print
664 * @param top_prec the precedence of the user of this expression.
666 static void print_expression_prec(expression_t const *expr, unsigned const top_prec)
668 if (expr->kind == EXPR_UNARY_CAST && expr->base.implicit && !print_implicit_casts) {
669 expr = expr->unary.value;
673 expr->base.parenthesized ||
674 (print_parenthesis && top_prec != PREC_BOTTOM) ||
675 top_prec > get_expression_precedence(expr->base.kind);
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_OFFSETOF: print_offsetof_expression( &expr->offsetofe); break;
696 case EXPR_REFERENCE_ENUM_VALUE: print_reference_expression( &expr->reference); break;
697 case EXPR_SELECT: print_select( &expr->select); break;
698 case EXPR_STATEMENT: print_statement_expression( &expr->statement); break;
699 case EXPR_STRING_LITERAL:
700 case EXPR_WIDE_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 goto statement.
779 * @param statement the goto statement
781 static void print_goto_statement(const goto_statement_t *statement)
783 print_string("goto ");
784 if (statement->expression != NULL) {
786 print_expression(statement->expression);
788 print_string(statement->label->base.symbol->string);
794 * Print a label statement.
796 * @param statement the label statement
798 static void print_label_statement(const label_statement_t *statement)
800 print_format("%s:\n", statement->label->base.symbol->string);
801 print_indented_statement(statement->statement);
804 static void print_inner_statement(statement_t const *const stmt)
806 if (stmt->kind == STATEMENT_COMPOUND) {
808 print_compound_statement(&stmt->compound);
812 print_indented_statement(stmt);
817 static void print_after_inner_statement(statement_t const *const stmt)
819 if (stmt->kind == STATEMENT_COMPOUND) {
828 * Print an if statement.
830 * @param statement the if statement
832 static void print_if_statement(const if_statement_t *statement)
834 print_string("if (");
835 print_expression(statement->condition);
837 print_inner_statement(statement->true_statement);
839 statement_t const *const f = statement->false_statement;
841 print_after_inner_statement(statement->true_statement);
842 print_string("else");
843 if (f->kind == STATEMENT_IF) {
845 print_if_statement(&f->ifs);
847 print_inner_statement(f);
853 * Print a switch statement.
855 * @param statement the switch statement
857 static void print_switch_statement(const switch_statement_t *statement)
859 int const old_case_indent = case_indent;
860 case_indent = indent;
862 print_string("switch (");
863 print_expression(statement->expression);
865 print_inner_statement(statement->body);
867 case_indent = old_case_indent;
871 * Print a case label (including the default label).
873 * @param statement the case label statement
875 static void print_case_label(const case_label_statement_t *statement)
877 if (statement->expression == NULL) {
878 print_string("default:\n");
880 print_string("case ");
881 print_expression(statement->expression);
882 if (statement->end_range != NULL) {
883 print_string(" ... ");
884 print_expression(statement->end_range);
888 print_indented_statement(statement->statement);
891 static void print_typedef(const entity_t *entity)
893 print_string("typedef ");
894 print_type_ext(entity->typedefe.type, entity->base.symbol, NULL);
899 * returns true if the entity is a compiler generated one and has no real
900 * correspondenc in the source file
902 static bool is_generated_entity(const entity_t *entity)
904 if (entity->kind == ENTITY_TYPEDEF)
905 return entity->typedefe.builtin;
907 if (is_declaration(entity))
908 return entity->declaration.implicit;
914 * Print a declaration statement.
916 * @param statement the statement
918 static void print_declaration_statement(
919 const declaration_statement_t *statement)
922 entity_t *entity = statement->declarations_begin;
923 if (entity == NULL) {
924 print_string("/* empty declaration statement */");
928 entity_t *const end = statement->declarations_end->base.next;
929 for (; entity != end; entity = entity->base.next) {
930 if (entity->kind == ENTITY_ENUM_VALUE)
932 if (is_generated_entity(entity))
942 print_entity(entity);
947 * Print a while statement.
949 * @param statement the statement
951 static void print_while_statement(const while_statement_t *statement)
953 print_string("while (");
954 print_expression(statement->condition);
956 print_inner_statement(statement->body);
960 * Print a do-while statement.
962 * @param statement the statement
964 static void print_do_while_statement(const do_while_statement_t *statement)
967 print_inner_statement(statement->body);
968 print_after_inner_statement(statement->body);
969 print_string("while (");
970 print_expression(statement->condition);
975 * Print a for statement.
977 * @param statement the statement
979 static void print_for_statement(const for_statement_t *statement)
981 print_string("for (");
982 if (statement->initialisation != NULL) {
983 print_expression(statement->initialisation);
986 entity_t const *entity = statement->scope.entities;
987 for (; entity != NULL; entity = entity->base.next) {
988 if (is_generated_entity(entity))
990 /* FIXME display of multiple declarations is wrong */
991 print_declaration(entity);
994 if (statement->condition != NULL) {
996 print_expression(statement->condition);
999 if (statement->step != NULL) {
1001 print_expression(statement->step);
1004 print_inner_statement(statement->body);
1008 * Print assembler arguments.
1010 * @param arguments the arguments
1012 static void print_asm_arguments(asm_argument_t *arguments)
1014 asm_argument_t *argument = arguments;
1015 for (; argument != NULL; argument = argument->next) {
1016 if (argument != arguments)
1019 if (argument->symbol) {
1020 print_format("[%s] ", argument->symbol->string);
1022 print_quoted_string(&argument->constraints, '"', 1);
1024 print_expression(argument->expression);
1030 * Print assembler clobbers.
1032 * @param clobbers the clobbers
1034 static void print_asm_clobbers(asm_clobber_t *clobbers)
1036 asm_clobber_t *clobber = clobbers;
1037 for (; clobber != NULL; clobber = clobber->next) {
1038 if (clobber != clobbers)
1041 print_quoted_string(&clobber->clobber, '"', 1);
1046 * Print an assembler statement.
1048 * @param statement the statement
1050 static void print_asm_statement(const asm_statement_t *statement)
1052 print_string("asm ");
1053 if (statement->is_volatile) {
1054 print_string("volatile ");
1057 print_quoted_string(&statement->asm_text, '"', 1);
1058 if (statement->outputs == NULL &&
1059 statement->inputs == NULL &&
1060 statement->clobbers == NULL)
1061 goto end_of_print_asm_statement;
1063 print_string(" : ");
1064 print_asm_arguments(statement->outputs);
1065 if (statement->inputs == NULL && statement->clobbers == NULL)
1066 goto end_of_print_asm_statement;
1068 print_string(" : ");
1069 print_asm_arguments(statement->inputs);
1070 if (statement->clobbers == NULL)
1071 goto end_of_print_asm_statement;
1073 print_string(" : ");
1074 print_asm_clobbers(statement->clobbers);
1076 end_of_print_asm_statement:
1081 * Print a microsoft __try statement.
1083 * @param statement the statement
1085 static void print_ms_try_statement(const ms_try_statement_t *statement)
1087 print_string("__try");
1088 print_inner_statement(statement->try_statement);
1089 print_after_inner_statement(statement->try_statement);
1090 if (statement->except_expression != NULL) {
1091 print_string("__except(");
1092 print_expression(statement->except_expression);
1095 print_string("__finally");
1097 print_inner_statement(statement->final_statement);
1101 * Print a microsoft __leave statement.
1103 * @param statement the statement
1105 static void print_leave_statement(const leave_statement_t *statement)
1108 print_string("__leave;");
1112 * Print a statement.
1114 * @param statement the statement
1116 void print_statement(statement_t const *const stmt)
1118 switch (stmt->kind) {
1119 case STATEMENT_ASM: print_asm_statement( &stmt->asms); break;
1120 case STATEMENT_BREAK: print_string("break;"); break;
1121 case STATEMENT_CASE_LABEL: print_case_label( &stmt->case_label); break;
1122 case STATEMENT_COMPOUND: print_compound_statement( &stmt->compound); break;
1123 case STATEMENT_CONTINUE: print_string("continue;"); break;
1124 case STATEMENT_DECLARATION: print_declaration_statement(&stmt->declaration); break;
1125 case STATEMENT_DO_WHILE: print_do_while_statement( &stmt->do_while); break;
1126 case STATEMENT_EMPTY: print_char(';'); break;
1127 case STATEMENT_ERROR: print_string("$error statement$"); break;
1128 case STATEMENT_EXPRESSION: print_expression_statement( &stmt->expression); break;
1129 case STATEMENT_FOR: print_for_statement( &stmt->fors); break;
1130 case STATEMENT_GOTO: print_goto_statement( &stmt->gotos); break;
1131 case STATEMENT_IF: print_if_statement( &stmt->ifs); break;
1132 case STATEMENT_LABEL: print_label_statement( &stmt->label); break;
1133 case STATEMENT_LEAVE: print_leave_statement( &stmt->leave); break;
1134 case STATEMENT_MS_TRY: print_ms_try_statement( &stmt->ms_try); break;
1135 case STATEMENT_RETURN: print_return_statement( &stmt->returns); break;
1136 case STATEMENT_SWITCH: print_switch_statement( &stmt->switchs); break;
1137 case STATEMENT_WHILE: print_while_statement( &stmt->whiles); break;
1142 * Print a storage class.
1144 * @param storage_class the storage class
1146 static void print_storage_class(storage_class_tag_t storage_class)
1148 switch (storage_class) {
1149 case STORAGE_CLASS_NONE: return;
1150 case STORAGE_CLASS_TYPEDEF: print_string("typedef "); return;
1151 case STORAGE_CLASS_EXTERN: print_string("extern "); return;
1152 case STORAGE_CLASS_STATIC: print_string("static "); return;
1153 case STORAGE_CLASS_AUTO: print_string("auto "); return;
1154 case STORAGE_CLASS_REGISTER: print_string("register "); return;
1156 panic("invalid storage class");
1160 * Print an initializer.
1162 * @param initializer the initializer
1164 void print_initializer(const initializer_t *initializer)
1166 if (initializer == NULL) {
1171 switch (initializer->kind) {
1172 case INITIALIZER_VALUE: {
1173 const initializer_value_t *value = &initializer->value;
1174 print_assignment_expression(value->value);
1177 case INITIALIZER_LIST: {
1178 assert(initializer->kind == INITIALIZER_LIST);
1180 const initializer_list_t *list = &initializer->list;
1182 for (size_t i = 0 ; i < list->len; ++i) {
1183 const initializer_t *sub_init = list->initializers[i];
1184 print_initializer(list->initializers[i]);
1185 if (i < list->len-1) {
1186 if (sub_init == NULL || sub_init->kind != INITIALIZER_DESIGNATOR)
1193 case INITIALIZER_STRING:
1194 print_quoted_string(&initializer->string.string, '"', 1);
1196 case INITIALIZER_WIDE_STRING:
1197 print_quoted_string(&initializer->string.string, '"', 1);
1199 case INITIALIZER_DESIGNATOR:
1200 print_designator(initializer->designator.designator);
1201 print_string(" = ");
1205 panic("invalid initializer kind found");
1210 * Print microsoft extended declaration modifiers.
1212 static void print_ms_modifiers(const declaration_t *declaration)
1214 if ((c_mode & _MS) == 0)
1217 decl_modifiers_t modifiers = declaration->modifiers;
1219 bool ds_shown = false;
1220 const char *next = "(";
1222 if (declaration->base.kind == ENTITY_VARIABLE) {
1223 variable_t *variable = (variable_t*)declaration;
1224 if (variable->alignment != 0
1225 || variable->get_property_sym != NULL
1226 || variable->put_property_sym != NULL) {
1228 print_string("__declspec");
1232 if (variable->alignment != 0) {
1233 print_string(next); next = ", "; print_format("align(%u)", variable->alignment);
1235 if (variable->get_property_sym != NULL
1236 || variable->put_property_sym != NULL) {
1238 print_string(next); next = ", "; print_string("property(");
1239 if (variable->get_property_sym != NULL) {
1240 print_format("get=%s", variable->get_property_sym->string);
1243 if (variable->put_property_sym != NULL)
1244 print_format("%sput=%s", comma, variable->put_property_sym->string);
1250 /* DM_FORCEINLINE handled outside. */
1251 if ((modifiers & ~DM_FORCEINLINE) != 0) {
1253 print_string("__declspec");
1256 if (modifiers & DM_DLLIMPORT) {
1257 print_string(next); next = ", "; print_string("dllimport");
1259 if (modifiers & DM_DLLEXPORT) {
1260 print_string(next); next = ", "; print_string("dllexport");
1262 if (modifiers & DM_THREAD) {
1263 print_string(next); next = ", "; print_string("thread");
1265 if (modifiers & DM_NAKED) {
1266 print_string(next); next = ", "; print_string("naked");
1268 if (modifiers & DM_THREAD) {
1269 print_string(next); next = ", "; print_string("thread");
1271 if (modifiers & DM_SELECTANY) {
1272 print_string(next); next = ", "; print_string("selectany");
1274 if (modifiers & DM_NOTHROW) {
1275 print_string(next); next = ", "; print_string("nothrow");
1277 if (modifiers & DM_NORETURN) {
1278 print_string(next); next = ", "; print_string("noreturn");
1280 if (modifiers & DM_NOINLINE) {
1281 print_string(next); next = ", "; print_string("noinline");
1283 if (modifiers & DM_DEPRECATED) {
1284 print_string(next); next = ", "; print_string("deprecated");
1285 if (declaration->deprecated_string != NULL)
1286 print_format("(\"%s\")",
1287 declaration->deprecated_string);
1289 if (modifiers & DM_RESTRICT) {
1290 print_string(next); next = ", "; print_string("restrict");
1292 if (modifiers & DM_NOALIAS) {
1293 print_string(next); next = ", "; print_string("noalias");
1302 static void print_scope(const scope_t *scope)
1304 const entity_t *entity = scope->entities;
1305 for ( ; entity != NULL; entity = entity->base.next) {
1307 print_entity(entity);
1312 static void print_namespace(const namespace_t *namespace)
1314 print_string("namespace ");
1315 if (namespace->base.symbol != NULL) {
1316 print_string(namespace->base.symbol->string);
1320 print_string("{\n");
1323 print_scope(&namespace->members);
1327 print_string("}\n");
1331 * Print a variable or function declaration
1333 void print_declaration(const entity_t *entity)
1335 assert(is_declaration(entity));
1336 const declaration_t *declaration = &entity->declaration;
1338 print_storage_class((storage_class_tag_t)declaration->declared_storage_class);
1339 if (entity->kind == ENTITY_FUNCTION) {
1340 function_t *function = (function_t*)declaration;
1341 if (function->is_inline) {
1342 if (declaration->modifiers & DM_FORCEINLINE) {
1343 print_string("__forceinline ");
1344 } else if (declaration->modifiers & DM_MICROSOFT_INLINE) {
1345 print_string("__inline ");
1347 print_string("inline ");
1351 //print_ms_modifiers(declaration);
1352 switch (entity->kind) {
1353 case ENTITY_FUNCTION:
1354 print_type_ext(entity->declaration.type, entity->base.symbol,
1355 &entity->function.parameters);
1357 if (entity->function.statement != NULL) {
1359 print_indented_statement(entity->function.statement);
1365 case ENTITY_VARIABLE:
1366 if (entity->variable.thread_local)
1367 print_string("__thread ");
1368 print_type_ext(declaration->type, declaration->base.symbol, NULL);
1369 if (entity->variable.initializer != NULL) {
1370 print_string(" = ");
1371 print_initializer(entity->variable.initializer);
1375 case ENTITY_COMPOUND_MEMBER:
1376 print_type_ext(declaration->type, declaration->base.symbol, NULL);
1377 if (entity->compound_member.bitfield) {
1378 print_format(" : %u", entity->compound_member.bit_size);
1383 print_type_ext(declaration->type, declaration->base.symbol, NULL);
1390 * Prints an expression.
1392 * @param expression the expression
1394 void print_expression(const expression_t *expression)
1396 print_expression_prec(expression, PREC_BOTTOM);
1400 * Print a declaration.
1402 * @param declaration the declaration
1404 void print_entity(const entity_t *entity)
1406 if (entity->base.namespc != NAMESPACE_NORMAL && entity->base.symbol == NULL)
1409 switch ((entity_kind_tag_t)entity->kind) {
1410 case ENTITY_VARIABLE:
1411 case ENTITY_PARAMETER:
1412 case ENTITY_COMPOUND_MEMBER:
1413 case ENTITY_FUNCTION:
1414 print_declaration(entity);
1416 case ENTITY_TYPEDEF:
1417 print_typedef(entity);
1421 print_string("class ");
1422 print_string(entity->base.symbol->string);
1423 print_string("; /* TODO */\n");
1426 print_string("struct ");
1427 goto print_compound;
1429 print_string("union ");
1431 print_string(entity->base.symbol->string);
1432 if (entity->compound.complete) {
1434 print_compound_definition(&entity->compound);
1439 print_string("enum ");
1440 print_string(entity->base.symbol->string);
1442 print_enum_definition(&entity->enume);
1445 case ENTITY_NAMESPACE:
1446 print_namespace(&entity->namespacee);
1448 case ENTITY_LOCAL_LABEL:
1449 print_string("__label__ ");
1450 print_string(entity->base.symbol->string);
1454 case ENTITY_ENUM_VALUE:
1455 panic("print_entity used on unexpected entity type");
1457 panic("Invalid entity type encountered");
1461 * Print the AST of a translation unit.
1463 * @param unit the translation unit
1465 void print_ast(const translation_unit_t *unit)
1467 entity_t *entity = unit->scope.entities;
1468 for ( ; entity != NULL; entity = entity->base.next) {
1469 if (entity->kind == ENTITY_ENUM_VALUE)
1471 if (entity->base.namespc != NAMESPACE_NORMAL
1472 && entity->base.symbol == NULL)
1474 if (is_generated_entity(entity))
1478 print_entity(entity);
1483 expression_classification_t is_constant_initializer(const initializer_t *initializer)
1485 switch (initializer->kind) {
1486 case INITIALIZER_STRING:
1487 case INITIALIZER_WIDE_STRING:
1488 case INITIALIZER_DESIGNATOR:
1489 return EXPR_CLASS_CONSTANT;
1491 case INITIALIZER_VALUE:
1492 return is_linker_constant(initializer->value.value);
1494 case INITIALIZER_LIST: {
1495 expression_classification_t all = EXPR_CLASS_CONSTANT;
1496 for (size_t i = 0; i < initializer->list.len; ++i) {
1497 initializer_t *sub_initializer = initializer->list.initializers[i];
1498 expression_classification_t const cur = is_constant_initializer(sub_initializer);
1506 panic("invalid initializer kind found");
1510 * Checks if an expression references an object with a constant/known location
1511 * to the linker. Example:
1512 * - "x", "*&x" with x being a global variable. The value of x need not be
1513 * constant but the address of x is.
1514 * - "a.b.c" when a has a constant/known location to the linker
1516 static expression_classification_t is_object_with_linker_constant_address(
1517 const expression_t *expression)
1519 switch (expression->kind) {
1520 case EXPR_UNARY_DEREFERENCE:
1521 return is_linker_constant(expression->unary.value);
1524 type_t *base_type = skip_typeref(expression->select.compound->base.type);
1525 if (is_type_pointer(base_type)) {
1527 return is_linker_constant(expression->select.compound);
1529 return is_object_with_linker_constant_address(expression->select.compound);
1533 case EXPR_ARRAY_ACCESS: {
1534 expression_classification_t const ref = is_linker_constant(expression->array_access.array_ref);
1535 expression_classification_t const idx = is_constant_expression(expression->array_access.index);
1536 return ref < idx ? ref : idx;
1539 case EXPR_REFERENCE: {
1540 entity_t *entity = expression->reference.entity;
1541 if (!is_declaration(entity))
1542 return EXPR_CLASS_VARIABLE;
1544 switch ((storage_class_tag_t)entity->declaration.storage_class) {
1545 case STORAGE_CLASS_NONE:
1546 case STORAGE_CLASS_EXTERN:
1547 case STORAGE_CLASS_STATIC:
1549 entity->kind != ENTITY_VARIABLE ||
1550 !entity->variable.thread_local ? EXPR_CLASS_CONSTANT :
1551 EXPR_CLASS_VARIABLE;
1553 case STORAGE_CLASS_REGISTER:
1554 case STORAGE_CLASS_TYPEDEF:
1555 case STORAGE_CLASS_AUTO:
1558 return EXPR_CLASS_VARIABLE;
1562 return EXPR_CLASS_ERROR;
1565 return EXPR_CLASS_VARIABLE;
1569 expression_classification_t is_linker_constant(const expression_t *expression)
1571 switch (expression->kind) {
1572 case EXPR_STRING_LITERAL:
1573 case EXPR_WIDE_STRING_LITERAL:
1575 case EXPR_LABEL_ADDRESS:
1576 return EXPR_CLASS_CONSTANT;
1578 case EXPR_COMPOUND_LITERAL:
1579 return is_constant_initializer(expression->compound_literal.initializer);
1581 case EXPR_UNARY_TAKE_ADDRESS:
1582 return is_object_with_linker_constant_address(expression->unary.value);
1584 case EXPR_UNARY_DEREFERENCE: {
1586 = revert_automatic_type_conversion(expression->unary.value);
1587 /* dereferencing a function is a NOP */
1588 if (is_type_function(real_type)) {
1589 return is_linker_constant(expression->unary.value);
1594 case EXPR_UNARY_CAST: {
1595 type_t *dest = skip_typeref(expression->base.type);
1596 if (!is_type_pointer(dest) && (
1597 dest->kind != TYPE_ATOMIC ||
1598 !(get_atomic_type_flags(dest->atomic.akind) & ATOMIC_TYPE_FLAG_INTEGER) ||
1599 get_atomic_type_size(dest->atomic.akind) < get_type_size(type_void_ptr)
1601 return is_constant_expression(expression);
1603 return is_linker_constant(expression->unary.value);
1606 case EXPR_BINARY_ADD:
1607 case EXPR_BINARY_SUB: {
1608 expression_t *const left = expression->binary.left;
1609 expression_t *const right = expression->binary.right;
1610 type_t *const ltype = skip_typeref(left->base.type);
1611 type_t *const rtype = skip_typeref(right->base.type);
1613 if (is_type_pointer(ltype)) {
1614 expression_classification_t const l = is_linker_constant(left);
1615 expression_classification_t const r = is_constant_expression(right);
1616 return l < r ? l : r;
1617 } else if (is_type_pointer(rtype)) {
1618 expression_classification_t const l = is_constant_expression(left);
1619 expression_classification_t const r = is_linker_constant(right);
1620 return l < r ? l : r;
1621 } else if (!is_type_valid(ltype) || !is_type_valid(rtype)) {
1622 return EXPR_CLASS_ERROR;
1624 return is_constant_expression(expression);
1628 case EXPR_REFERENCE: {
1629 entity_t *entity = expression->reference.entity;
1630 if (!is_declaration(entity))
1631 return EXPR_CLASS_VARIABLE;
1633 type_t *type = skip_typeref(entity->declaration.type);
1634 if (is_type_function(type))
1635 return EXPR_CLASS_CONSTANT;
1636 if (is_type_array(type)) {
1637 return is_object_with_linker_constant_address(expression);
1639 /* Prevent stray errors */
1640 if (!is_type_valid(type))
1641 return EXPR_CLASS_ERROR;
1642 return EXPR_CLASS_VARIABLE;
1645 case EXPR_ARRAY_ACCESS: {
1646 type_t *const type =
1647 skip_typeref(revert_automatic_type_conversion(expression));
1648 if (!is_type_array(type))
1649 return EXPR_CLASS_VARIABLE;
1650 return is_linker_constant(expression->array_access.array_ref);
1653 case EXPR_CONDITIONAL: {
1654 expression_t *const c = expression->conditional.condition;
1655 expression_classification_t const cclass = is_constant_expression(c);
1656 if (cclass != EXPR_CLASS_CONSTANT)
1659 if (fold_constant_to_bool(c)) {
1660 expression_t const *const t = expression->conditional.true_expression;
1661 return is_linker_constant(t != NULL ? t : c);
1663 return is_linker_constant(expression->conditional.false_expression);
1668 entity_t *entity = expression->select.compound_entry;
1669 if (!is_declaration(entity))
1670 return EXPR_CLASS_VARIABLE;
1671 type_t *type = skip_typeref(entity->declaration.type);
1672 if (is_type_array(type)) {
1673 /* arrays automatically convert to their address */
1674 expression_t *compound = expression->select.compound;
1675 type_t *base_type = skip_typeref(compound->base.type);
1676 if (is_type_pointer(base_type)) {
1678 return is_linker_constant(compound);
1680 return is_object_with_linker_constant_address(compound);
1683 return EXPR_CLASS_VARIABLE;
1687 return is_constant_expression(expression);
1692 * Check if the given expression is a call to a builtin function
1693 * returning a constant result.
1695 static expression_classification_t is_builtin_const_call(const expression_t *expression)
1697 expression_t *function = expression->call.function;
1698 if (function->kind != EXPR_REFERENCE)
1699 return EXPR_CLASS_VARIABLE;
1700 reference_expression_t *ref = &function->reference;
1701 if (ref->entity->kind != ENTITY_FUNCTION)
1702 return EXPR_CLASS_VARIABLE;
1704 switch (ref->entity->function.btk) {
1707 return EXPR_CLASS_CONSTANT;
1709 return EXPR_CLASS_VARIABLE;
1714 static expression_classification_t is_constant_pointer(const expression_t *expression)
1716 expression_classification_t const expr_class = is_constant_expression(expression);
1717 if (expr_class != EXPR_CLASS_VARIABLE)
1720 switch (expression->kind) {
1721 case EXPR_UNARY_CAST:
1722 return is_constant_pointer(expression->unary.value);
1724 return EXPR_CLASS_VARIABLE;
1728 static expression_classification_t is_object_with_constant_address(const expression_t *expression)
1730 switch (expression->kind) {
1732 expression_t *compound = expression->select.compound;
1733 type_t *compound_type = compound->base.type;
1734 compound_type = skip_typeref(compound_type);
1735 if (is_type_pointer(compound_type)) {
1736 return is_constant_pointer(compound);
1738 return is_object_with_constant_address(compound);
1742 case EXPR_ARRAY_ACCESS: {
1743 array_access_expression_t const* const array_access =
1744 &expression->array_access;
1745 expression_classification_t const idx_class = is_constant_expression(array_access->index);
1746 if (idx_class != EXPR_CLASS_CONSTANT)
1748 expression_classification_t const ref_addr = is_object_with_constant_address(array_access->array_ref);
1749 expression_classification_t const ref_ptr = is_constant_pointer(array_access->array_ref);
1750 return ref_addr > ref_ptr ? ref_addr : ref_ptr;
1753 case EXPR_UNARY_DEREFERENCE:
1754 return is_constant_pointer(expression->unary.value);
1757 return EXPR_CLASS_ERROR;
1760 return EXPR_CLASS_VARIABLE;
1764 expression_classification_t is_constant_expression(const expression_t *expression)
1766 switch (expression->kind) {
1767 case EXPR_LITERAL_CASES:
1768 case EXPR_CLASSIFY_TYPE:
1771 case EXPR_BUILTIN_CONSTANT_P:
1772 case EXPR_BUILTIN_TYPES_COMPATIBLE_P:
1773 case EXPR_REFERENCE_ENUM_VALUE:
1774 return EXPR_CLASS_CONSTANT;
1777 type_t *const type = skip_typeref(expression->typeprop.type);
1779 !is_type_array(type) || !type->array.is_vla ? EXPR_CLASS_CONSTANT :
1780 EXPR_CLASS_VARIABLE;
1783 case EXPR_STRING_LITERAL:
1784 case EXPR_WIDE_STRING_LITERAL:
1786 case EXPR_LABEL_ADDRESS:
1791 case EXPR_STATEMENT:
1792 case EXPR_UNARY_POSTFIX_INCREMENT:
1793 case EXPR_UNARY_POSTFIX_DECREMENT:
1794 case EXPR_UNARY_PREFIX_INCREMENT:
1795 case EXPR_UNARY_PREFIX_DECREMENT:
1796 case EXPR_UNARY_ASSUME: /* has VOID type */
1797 case EXPR_UNARY_DEREFERENCE:
1798 case EXPR_UNARY_DELETE:
1799 case EXPR_UNARY_DELETE_ARRAY:
1800 case EXPR_UNARY_THROW:
1801 case EXPR_BINARY_ASSIGN:
1802 case EXPR_BINARY_MUL_ASSIGN:
1803 case EXPR_BINARY_DIV_ASSIGN:
1804 case EXPR_BINARY_MOD_ASSIGN:
1805 case EXPR_BINARY_ADD_ASSIGN:
1806 case EXPR_BINARY_SUB_ASSIGN:
1807 case EXPR_BINARY_SHIFTLEFT_ASSIGN:
1808 case EXPR_BINARY_SHIFTRIGHT_ASSIGN:
1809 case EXPR_BINARY_BITWISE_AND_ASSIGN:
1810 case EXPR_BINARY_BITWISE_XOR_ASSIGN:
1811 case EXPR_BINARY_BITWISE_OR_ASSIGN:
1812 case EXPR_BINARY_COMMA:
1813 case EXPR_ARRAY_ACCESS:
1814 return EXPR_CLASS_VARIABLE;
1816 case EXPR_REFERENCE: {
1817 type_t *const type = skip_typeref(expression->base.type);
1818 return is_type_valid(type) ? EXPR_CLASS_VARIABLE : EXPR_CLASS_ERROR;
1821 case EXPR_UNARY_TAKE_ADDRESS:
1822 return is_object_with_constant_address(expression->unary.value);
1825 return is_builtin_const_call(expression);
1827 case EXPR_UNARY_NEGATE:
1828 case EXPR_UNARY_PLUS:
1829 case EXPR_UNARY_BITWISE_NEGATE:
1830 case EXPR_UNARY_NOT:
1831 return is_constant_expression(expression->unary.value);
1833 case EXPR_UNARY_CAST: {
1834 type_t *const type = skip_typeref(expression->base.type);
1835 if (is_type_scalar(type))
1836 return is_constant_expression(expression->unary.value);
1837 if (!is_type_valid(type))
1838 return EXPR_CLASS_ERROR;
1839 return EXPR_CLASS_VARIABLE;
1842 case EXPR_BINARY_ADD:
1843 case EXPR_BINARY_SUB:
1844 case EXPR_BINARY_MUL:
1845 case EXPR_BINARY_DIV:
1846 case EXPR_BINARY_MOD:
1847 case EXPR_BINARY_EQUAL:
1848 case EXPR_BINARY_NOTEQUAL:
1849 case EXPR_BINARY_LESS:
1850 case EXPR_BINARY_LESSEQUAL:
1851 case EXPR_BINARY_GREATER:
1852 case EXPR_BINARY_GREATEREQUAL:
1853 case EXPR_BINARY_BITWISE_AND:
1854 case EXPR_BINARY_BITWISE_OR:
1855 case EXPR_BINARY_BITWISE_XOR:
1856 case EXPR_BINARY_SHIFTLEFT:
1857 case EXPR_BINARY_SHIFTRIGHT:
1858 case EXPR_BINARY_ISGREATER:
1859 case EXPR_BINARY_ISGREATEREQUAL:
1860 case EXPR_BINARY_ISLESS:
1861 case EXPR_BINARY_ISLESSEQUAL:
1862 case EXPR_BINARY_ISLESSGREATER:
1863 case EXPR_BINARY_ISUNORDERED: {
1864 expression_classification_t const l = is_constant_expression(expression->binary.left);
1865 expression_classification_t const r = is_constant_expression(expression->binary.right);
1866 return l < r ? l : r;
1869 case EXPR_BINARY_LOGICAL_AND: {
1870 expression_t const *const left = expression->binary.left;
1871 expression_classification_t const lclass = is_constant_expression(left);
1872 if (lclass != EXPR_CLASS_CONSTANT)
1874 if (!fold_constant_to_bool(left))
1875 return EXPR_CLASS_CONSTANT;
1876 return is_constant_expression(expression->binary.right);
1879 case EXPR_BINARY_LOGICAL_OR: {
1880 expression_t const *const left = expression->binary.left;
1881 expression_classification_t const lclass = is_constant_expression(left);
1882 if (lclass != EXPR_CLASS_CONSTANT)
1884 if (fold_constant_to_bool(left))
1885 return EXPR_CLASS_CONSTANT;
1886 return is_constant_expression(expression->binary.right);
1889 case EXPR_COMPOUND_LITERAL:
1890 return is_constant_initializer(expression->compound_literal.initializer);
1892 case EXPR_CONDITIONAL: {
1893 expression_t *const condition = expression->conditional.condition;
1894 expression_classification_t const cclass = is_constant_expression(condition);
1895 if (cclass != EXPR_CLASS_CONSTANT)
1898 if (fold_constant_to_bool(condition)) {
1899 expression_t const *const t = expression->conditional.true_expression;
1900 return t == NULL ? EXPR_CLASS_CONSTANT : is_constant_expression(t);
1902 return is_constant_expression(expression->conditional.false_expression);
1907 return EXPR_CLASS_ERROR;
1909 panic("invalid expression found (is constant expression)");
1914 obstack_init(&ast_obstack);
1919 obstack_free(&ast_obstack, NULL);