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)
71 * Returns 1 if a given precedence level has right-to-left
72 * associativity, else 0.
74 * @param precedence the operator precedence
76 static int right_to_left(unsigned precedence)
80 case PREC_CONDITIONAL:
90 * Return the precedence of an expression given by its kind.
92 * @param kind the expression kind
94 static unsigned get_expression_precedence(expression_kind_t kind)
96 static const unsigned prec[] = {
97 [EXPR_ERROR] = PREC_PRIMARY,
98 [EXPR_REFERENCE] = PREC_PRIMARY,
99 [EXPR_ENUM_CONSTANT] = PREC_PRIMARY,
100 [EXPR_LITERAL_INTEGER] = PREC_PRIMARY,
101 [EXPR_LITERAL_FLOATINGPOINT] = PREC_PRIMARY,
102 [EXPR_LITERAL_CHARACTER] = PREC_PRIMARY,
103 [EXPR_LITERAL_MS_NOOP] = PREC_PRIMARY,
104 [EXPR_STRING_LITERAL] = PREC_PRIMARY,
105 [EXPR_COMPOUND_LITERAL] = PREC_UNARY,
106 [EXPR_CALL] = PREC_POSTFIX,
107 [EXPR_CONDITIONAL] = PREC_CONDITIONAL,
108 [EXPR_SELECT] = PREC_POSTFIX,
109 [EXPR_ARRAY_ACCESS] = PREC_POSTFIX,
110 [EXPR_SIZEOF] = PREC_UNARY,
111 [EXPR_CLASSIFY_TYPE] = PREC_UNARY,
112 [EXPR_ALIGNOF] = PREC_UNARY,
114 [EXPR_FUNCNAME] = PREC_PRIMARY,
115 [EXPR_BUILTIN_CONSTANT_P] = PREC_PRIMARY,
116 [EXPR_BUILTIN_TYPES_COMPATIBLE_P] = PREC_PRIMARY,
117 [EXPR_OFFSETOF] = PREC_PRIMARY,
118 [EXPR_VA_START] = PREC_PRIMARY,
119 [EXPR_VA_ARG] = PREC_PRIMARY,
120 [EXPR_VA_COPY] = PREC_PRIMARY,
121 [EXPR_STATEMENT] = PREC_PRIMARY,
122 [EXPR_LABEL_ADDRESS] = PREC_PRIMARY,
124 [EXPR_UNARY_NEGATE] = PREC_UNARY,
125 [EXPR_UNARY_PLUS] = PREC_UNARY,
126 [EXPR_UNARY_BITWISE_NEGATE] = PREC_UNARY,
127 [EXPR_UNARY_NOT] = PREC_UNARY,
128 [EXPR_UNARY_DEREFERENCE] = PREC_UNARY,
129 [EXPR_UNARY_TAKE_ADDRESS] = PREC_UNARY,
130 [EXPR_UNARY_POSTFIX_INCREMENT] = PREC_POSTFIX,
131 [EXPR_UNARY_POSTFIX_DECREMENT] = PREC_POSTFIX,
132 [EXPR_UNARY_PREFIX_INCREMENT] = PREC_UNARY,
133 [EXPR_UNARY_PREFIX_DECREMENT] = PREC_UNARY,
134 [EXPR_UNARY_CAST] = PREC_UNARY,
135 [EXPR_UNARY_ASSUME] = PREC_PRIMARY,
136 [EXPR_UNARY_DELETE] = PREC_UNARY,
137 [EXPR_UNARY_DELETE_ARRAY] = PREC_UNARY,
138 [EXPR_UNARY_THROW] = PREC_ASSIGNMENT,
140 [EXPR_BINARY_ADD] = PREC_ADDITIVE,
141 [EXPR_BINARY_SUB] = PREC_ADDITIVE,
142 [EXPR_BINARY_MUL] = PREC_MULTIPLICATIVE,
143 [EXPR_BINARY_DIV] = PREC_MULTIPLICATIVE,
144 [EXPR_BINARY_MOD] = PREC_MULTIPLICATIVE,
145 [EXPR_BINARY_EQUAL] = PREC_EQUALITY,
146 [EXPR_BINARY_NOTEQUAL] = PREC_EQUALITY,
147 [EXPR_BINARY_LESS] = PREC_RELATIONAL,
148 [EXPR_BINARY_LESSEQUAL] = PREC_RELATIONAL,
149 [EXPR_BINARY_GREATER] = PREC_RELATIONAL,
150 [EXPR_BINARY_GREATEREQUAL] = PREC_RELATIONAL,
151 [EXPR_BINARY_BITWISE_AND] = PREC_AND,
152 [EXPR_BINARY_BITWISE_OR] = PREC_OR,
153 [EXPR_BINARY_BITWISE_XOR] = PREC_XOR,
154 [EXPR_BINARY_LOGICAL_AND] = PREC_LOGICAL_AND,
155 [EXPR_BINARY_LOGICAL_OR] = PREC_LOGICAL_OR,
156 [EXPR_BINARY_SHIFTLEFT] = PREC_SHIFT,
157 [EXPR_BINARY_SHIFTRIGHT] = PREC_SHIFT,
158 [EXPR_BINARY_ASSIGN] = PREC_ASSIGNMENT,
159 [EXPR_BINARY_MUL_ASSIGN] = PREC_ASSIGNMENT,
160 [EXPR_BINARY_DIV_ASSIGN] = PREC_ASSIGNMENT,
161 [EXPR_BINARY_MOD_ASSIGN] = PREC_ASSIGNMENT,
162 [EXPR_BINARY_ADD_ASSIGN] = PREC_ASSIGNMENT,
163 [EXPR_BINARY_SUB_ASSIGN] = PREC_ASSIGNMENT,
164 [EXPR_BINARY_SHIFTLEFT_ASSIGN] = PREC_ASSIGNMENT,
165 [EXPR_BINARY_SHIFTRIGHT_ASSIGN] = PREC_ASSIGNMENT,
166 [EXPR_BINARY_BITWISE_AND_ASSIGN] = PREC_ASSIGNMENT,
167 [EXPR_BINARY_BITWISE_XOR_ASSIGN] = PREC_ASSIGNMENT,
168 [EXPR_BINARY_BITWISE_OR_ASSIGN] = PREC_ASSIGNMENT,
169 [EXPR_BINARY_COMMA] = PREC_EXPRESSION,
171 [EXPR_BINARY_ISGREATER] = PREC_PRIMARY,
172 [EXPR_BINARY_ISGREATEREQUAL] = PREC_PRIMARY,
173 [EXPR_BINARY_ISLESS] = PREC_PRIMARY,
174 [EXPR_BINARY_ISLESSEQUAL] = PREC_PRIMARY,
175 [EXPR_BINARY_ISLESSGREATER] = PREC_PRIMARY,
176 [EXPR_BINARY_ISUNORDERED] = PREC_PRIMARY
178 assert((size_t)kind < lengthof(prec));
179 unsigned res = prec[kind];
181 assert(res != PREC_BOTTOM);
186 * Print a quoted string constant.
188 * @param string the string constant
189 * @param border the border char
191 static void print_quoted_string(const string_t *const string, char border)
193 print_string(get_string_encoding_prefix(string->encoding));
196 const char *end = string->begin + string->size;
197 for (const char *c = string->begin; c != end; ++c) {
203 case '\\': print_string("\\\\"); break;
204 case '\a': print_string("\\a"); break;
205 case '\b': print_string("\\b"); break;
206 case '\f': print_string("\\f"); break;
207 case '\n': print_string("\\n"); break;
208 case '\r': print_string("\\r"); break;
209 case '\t': print_string("\\t"); break;
210 case '\v': print_string("\\v"); break;
211 case '\?': print_string("\\?"); break;
213 if (c_mode & _GNUC) {
214 print_string("\\e"); break;
218 if ((unsigned)tc < 0x80 && !isprint(tc)) {
219 print_format("\\%03o", (unsigned)tc);
229 static void print_string_literal(string_literal_expression_t const *const literal, char const delimiter)
231 print_quoted_string(&literal->value, delimiter);
234 static void print_literal(const literal_expression_t *literal)
236 switch (literal->base.kind) {
237 case EXPR_LITERAL_MS_NOOP:
238 print_string("__noop");
241 case EXPR_LITERAL_BOOLEAN:
242 case EXPR_LITERAL_FLOATINGPOINT:
243 case EXPR_LITERAL_INTEGER:
244 print_string(literal->value.begin);
250 print_string("INVALID LITERAL KIND");
254 * Prints a predefined symbol.
256 static void print_funcname(const funcname_expression_t *funcname)
259 switch (funcname->kind) {
260 case FUNCNAME_FUNCTION: s = (c_mode & _C99) ? "__func__" : "__FUNCTION__"; break;
261 case FUNCNAME_PRETTY_FUNCTION: s = "__PRETTY_FUNCTION__"; break;
262 case FUNCNAME_FUNCSIG: s = "__FUNCSIG__"; break;
263 case FUNCNAME_FUNCDNAME: s = "__FUNCDNAME__"; break;
268 static void print_compound_literal(
269 const compound_literal_expression_t *expression)
272 print_type(expression->type);
274 print_initializer(expression->initializer);
277 static void print_assignment_expression(const expression_t *const expr)
279 print_expression_prec(expr, PREC_ASSIGNMENT);
283 * Prints a call expression.
285 * @param call the call expression
287 static void print_call_expression(const call_expression_t *call)
289 print_expression_prec(call->function, PREC_POSTFIX);
291 char const *sep = "";
292 for (call_argument_t const *arg = call->arguments; arg; arg = arg->next) {
295 print_assignment_expression(arg->expression);
301 * Prints a binary expression.
303 * @param binexpr the binary expression
305 static void print_binary_expression(const binary_expression_t *binexpr)
307 unsigned prec = get_expression_precedence(binexpr->base.kind);
308 int r2l = right_to_left(prec);
310 print_expression_prec(binexpr->left, prec + r2l);
312 switch (binexpr->base.kind) {
313 case EXPR_BINARY_COMMA: op = ", "; break;
314 case EXPR_BINARY_ASSIGN: op = " = "; break;
315 case EXPR_BINARY_ADD: op = " + "; break;
316 case EXPR_BINARY_SUB: op = " - "; break;
317 case EXPR_BINARY_MUL: op = " * "; break;
318 case EXPR_BINARY_MOD: op = " % "; break;
319 case EXPR_BINARY_DIV: op = " / "; break;
320 case EXPR_BINARY_BITWISE_OR: op = " | "; break;
321 case EXPR_BINARY_BITWISE_AND: op = " & "; break;
322 case EXPR_BINARY_BITWISE_XOR: op = " ^ "; break;
323 case EXPR_BINARY_LOGICAL_OR: op = " || "; break;
324 case EXPR_BINARY_LOGICAL_AND: op = " && "; break;
325 case EXPR_BINARY_NOTEQUAL: op = " != "; break;
326 case EXPR_BINARY_EQUAL: op = " == "; break;
327 case EXPR_BINARY_LESS: op = " < "; break;
328 case EXPR_BINARY_LESSEQUAL: op = " <= "; break;
329 case EXPR_BINARY_GREATER: op = " > "; break;
330 case EXPR_BINARY_GREATEREQUAL: op = " >= "; break;
331 case EXPR_BINARY_SHIFTLEFT: op = " << "; break;
332 case EXPR_BINARY_SHIFTRIGHT: op = " >> "; break;
334 case EXPR_BINARY_ADD_ASSIGN: op = " += "; break;
335 case EXPR_BINARY_SUB_ASSIGN: op = " -= "; break;
336 case EXPR_BINARY_MUL_ASSIGN: op = " *= "; break;
337 case EXPR_BINARY_MOD_ASSIGN: op = " %= "; break;
338 case EXPR_BINARY_DIV_ASSIGN: op = " /= "; break;
339 case EXPR_BINARY_BITWISE_OR_ASSIGN: op = " |= "; break;
340 case EXPR_BINARY_BITWISE_AND_ASSIGN: op = " &= "; break;
341 case EXPR_BINARY_BITWISE_XOR_ASSIGN: op = " ^= "; break;
342 case EXPR_BINARY_SHIFTLEFT_ASSIGN: op = " <<= "; break;
343 case EXPR_BINARY_SHIFTRIGHT_ASSIGN: op = " >>= "; break;
344 default: panic("invalid binexpression found");
347 print_expression_prec(binexpr->right, prec + 1 - r2l);
351 * Prints an unary expression.
353 * @param unexpr the unary expression
355 static void print_unary_expression(const unary_expression_t *unexpr)
357 unsigned prec = get_expression_precedence(unexpr->base.kind);
358 switch (unexpr->base.kind) {
359 case EXPR_UNARY_NEGATE: print_char ('-' ); break;
360 case EXPR_UNARY_PLUS: print_char ('+' ); break;
361 case EXPR_UNARY_NOT: print_char ('!' ); break;
362 case EXPR_UNARY_BITWISE_NEGATE: print_char ('~' ); break;
363 case EXPR_UNARY_PREFIX_INCREMENT: print_string("++"); break;
364 case EXPR_UNARY_PREFIX_DECREMENT: print_string("--"); break;
365 case EXPR_UNARY_DEREFERENCE: print_char ('*' ); break;
366 case EXPR_UNARY_TAKE_ADDRESS: print_char ('&' ); break;
367 case EXPR_UNARY_DELETE: print_string("delete "); break;
368 case EXPR_UNARY_DELETE_ARRAY: print_string("delete [] "); break;
370 case EXPR_UNARY_POSTFIX_INCREMENT:
371 print_expression_prec(unexpr->value, prec);
374 case EXPR_UNARY_POSTFIX_DECREMENT:
375 print_expression_prec(unexpr->value, prec);
378 case EXPR_UNARY_CAST:
380 print_type(unexpr->base.type);
383 case EXPR_UNARY_ASSUME:
384 print_string("__assume(");
385 print_assignment_expression(unexpr->value);
389 case EXPR_UNARY_THROW:
390 if (unexpr->value == NULL) {
391 print_string("throw");
394 print_string("throw ");
398 panic("invalid unary expression found");
400 print_expression_prec(unexpr->value, prec);
404 * Prints a reference expression.
406 * @param ref the reference expression
408 static void print_reference_expression(const reference_expression_t *ref)
410 print_string(ref->entity->base.symbol->string);
414 * Prints a label address expression.
416 * @param ref the reference expression
418 static void print_label_address_expression(const label_address_expression_t *le)
420 print_format("&&%s", le->label->base.symbol->string);
424 * Prints an array expression.
426 * @param expression the array expression
428 static void print_array_expression(const array_access_expression_t *expression)
430 if (!expression->flipped) {
431 print_expression_prec(expression->array_ref, PREC_POSTFIX);
433 print_expression(expression->index);
436 print_expression_prec(expression->index, PREC_POSTFIX);
438 print_expression(expression->array_ref);
444 * Prints a typeproperty expression (sizeof or __alignof__).
446 * @param expression the type property expression
448 static void print_typeprop_expression(const typeprop_expression_t *expression)
450 switch (expression->base.kind) {
451 case EXPR_SIZEOF: print_string("sizeof"); break;
452 case EXPR_ALIGNOF: print_string("__alignof__"); break;
453 default: panic("invalid typeprop kind");
455 if (expression->tp_expression != NULL) {
456 /* PREC_TOP: always print the '()' here, sizeof x is right but unusual */
457 print_expression_prec(expression->tp_expression, PREC_TOP);
460 print_type(expression->type);
466 * Prints a builtin constant expression.
468 * @param expression the builtin constant expression
470 static void print_builtin_constant(const builtin_constant_expression_t *expression)
472 print_string("__builtin_constant_p(");
473 print_assignment_expression(expression->value);
478 * Prints a builtin types compatible expression.
480 * @param expression the builtin types compatible expression
482 static void print_builtin_types_compatible(
483 const builtin_types_compatible_expression_t *expression)
485 print_string("__builtin_types_compatible_p(");
486 print_type(expression->left);
488 print_type(expression->right);
493 * Prints a conditional expression.
495 * @param expression the conditional expression
497 static void print_conditional(const conditional_expression_t *expression)
499 print_expression_prec(expression->condition, PREC_LOGICAL_OR);
500 if (expression->true_expression != NULL) {
502 print_expression_prec(expression->true_expression, PREC_EXPRESSION);
505 print_string(" ?: ");
507 precedence_t prec = c_mode & _CXX ? PREC_ASSIGNMENT : PREC_CONDITIONAL;
508 print_expression_prec(expression->false_expression, prec);
512 * Prints a va_start expression.
514 * @param expression the va_start expression
516 static void print_va_start(const va_start_expression_t *const expression)
518 print_string("__builtin_va_start(");
519 print_assignment_expression(expression->ap);
521 print_assignment_expression(expression->parameter);
526 * Prints a va_arg expression.
528 * @param expression the va_arg expression
530 static void print_va_arg(const va_arg_expression_t *expression)
532 print_string("__builtin_va_arg(");
533 print_assignment_expression(expression->ap);
535 print_type(expression->base.type);
540 * Prints a va_copy expression.
542 * @param expression the va_copy expression
544 static void print_va_copy(const va_copy_expression_t *expression)
546 print_string("__builtin_va_copy(");
547 print_assignment_expression(expression->dst);
549 print_assignment_expression(expression->src);
554 * Prints a select expression (. or ->).
556 * @param expression the select expression
558 static void print_select(const select_expression_t *expression)
560 print_expression_prec(expression->compound, PREC_POSTFIX);
561 /* do not print anything for anonymous struct/union selects
562 * FIXME: if the anonymous select was a '->' this will print '.'
564 if (expression->compound_entry->base.symbol == NULL)
567 if (is_type_pointer(skip_typeref(expression->compound->base.type))) {
572 print_string(expression->compound_entry->base.symbol->string);
576 * Prints a type classify expression.
578 * @param expr the type classify expression
580 static void print_classify_type_expression(
581 const classify_type_expression_t *const expr)
583 print_string("__builtin_classify_type(");
584 print_assignment_expression(expr->type_expression);
589 * Prints a designator.
591 * @param designator the designator
593 static void print_designator(const designator_t *designator)
595 for ( ; designator != NULL; designator = designator->next) {
596 if (designator->symbol == NULL) {
598 print_expression(designator->array_index);
602 print_string(designator->symbol->string);
608 * Prints an offsetof expression.
610 * @param expression the offset expression
612 static void print_offsetof_expression(const offsetof_expression_t *expression)
614 print_string("__builtin_offsetof(");
615 print_type(expression->type);
617 print_designator(expression->designator);
622 * Prints a statement expression.
624 * @param expression the statement expression
626 static void print_statement_expression(const statement_expression_t *expression)
629 print_statement(expression->statement);
633 static bool needs_parentheses(expression_t const *const expr, unsigned const top_prec)
635 if (expr->base.parenthesized)
638 if (top_prec > get_expression_precedence(expr->base.kind))
641 if (print_parenthesis && top_prec != PREC_BOTTOM) {
642 switch (expr->kind) {
643 case EXPR_ENUM_CONSTANT:
645 case EXPR_LITERAL_CASES:
646 case EXPR_LITERAL_CHARACTER:
648 case EXPR_STRING_LITERAL:
649 /* Do not print () around subexpressions consisting of a single token. */
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;
672 bool const parenthesized = needs_parentheses(expr, top_prec);
676 switch (expr->kind) {
678 case EXPR_SIZEOF: print_typeprop_expression( &expr->typeprop); break;
679 case EXPR_ARRAY_ACCESS: print_array_expression( &expr->array_access); break;
680 case EXPR_BINARY_CASES: print_binary_expression( &expr->binary); break;
681 case EXPR_BUILTIN_CONSTANT_P: print_builtin_constant( &expr->builtin_constant); break;
682 case EXPR_BUILTIN_TYPES_COMPATIBLE_P: print_builtin_types_compatible(&expr->builtin_types_compatible); break;
683 case EXPR_CALL: print_call_expression( &expr->call); break;
684 case EXPR_CLASSIFY_TYPE: print_classify_type_expression(&expr->classify_type); break;
685 case EXPR_COMPOUND_LITERAL: print_compound_literal( &expr->compound_literal); break;
686 case EXPR_CONDITIONAL: print_conditional( &expr->conditional); break;
687 case EXPR_ERROR: print_string("$error$"); break;
688 case EXPR_FUNCNAME: print_funcname( &expr->funcname); break;
689 case EXPR_LABEL_ADDRESS: print_label_address_expression(&expr->label_address); break;
690 case EXPR_LITERAL_CASES: print_literal( &expr->literal); break;
691 case EXPR_LITERAL_CHARACTER: print_string_literal( &expr->string_literal, '\''); break;
692 case EXPR_OFFSETOF: print_offsetof_expression( &expr->offsetofe); break;
694 case EXPR_ENUM_CONSTANT: print_reference_expression( &expr->reference); break;
695 case EXPR_SELECT: print_select( &expr->select); break;
696 case EXPR_STATEMENT: print_statement_expression( &expr->statement); break;
697 case EXPR_STRING_LITERAL: print_string_literal( &expr->string_literal, '"'); break;
698 case EXPR_UNARY_CASES: print_unary_expression( &expr->unary); break;
699 case EXPR_VA_ARG: print_va_arg( &expr->va_arge); break;
700 case EXPR_VA_COPY: print_va_copy( &expr->va_copye); break;
701 case EXPR_VA_START: print_va_start( &expr->va_starte); break;
707 static void print_indented_statement(statement_t const *const stmt)
709 switch (stmt->kind) {
710 case STATEMENT_LABEL:
713 case STATEMENT_CASE_LABEL:
714 for (int i = 0; i != case_indent; ++i)
722 print_statement(stmt);
726 * Print an compound statement.
728 * @param block the compound statement
730 static void print_compound_statement(const compound_statement_t *block)
735 for (statement_t const *stmt = block->statements; stmt; stmt = stmt->base.next) {
736 print_indented_statement(stmt);
746 * Print a return statement.
748 * @param statement the return statement
750 static void print_return_statement(const return_statement_t *statement)
752 expression_t const *const val = statement->value;
754 print_string("return ");
755 print_expression(val);
758 print_string("return;");
763 * Print an expression statement.
765 * @param statement the expression statement
767 static void print_expression_statement(const expression_statement_t *statement)
769 print_expression(statement->expression);
774 * Print a computed goto statement.
776 * @param statement the computed goto statement
778 static void print_computed_goto_statement(computed_goto_statement_t const *const stmt)
780 print_string("goto *");
781 print_expression(stmt->expression);
786 * Print a goto statement.
788 * @param statement the goto statement
790 static void print_goto_statement(const goto_statement_t *statement)
792 print_string("goto ");
793 print_string(statement->label->base.symbol->string);
798 * Print a label statement.
800 * @param statement the label statement
802 static void print_label_statement(const label_statement_t *statement)
804 print_format("%s:\n", statement->label->base.symbol->string);
805 print_indented_statement(statement->statement);
808 static void print_inner_statement(statement_t const *const stmt)
810 if (stmt->kind == STATEMENT_COMPOUND) {
812 print_compound_statement(&stmt->compound);
816 print_indented_statement(stmt);
821 static void print_after_inner_statement(statement_t const *const stmt)
823 if (stmt->kind == STATEMENT_COMPOUND) {
832 * Print an if statement.
834 * @param statement the if statement
836 static void print_if_statement(const if_statement_t *statement)
838 print_string("if (");
839 print_expression(statement->condition);
841 print_inner_statement(statement->true_statement);
843 statement_t const *const f = statement->false_statement;
845 print_after_inner_statement(statement->true_statement);
846 print_string("else");
847 if (f->kind == STATEMENT_IF) {
849 print_if_statement(&f->ifs);
851 print_inner_statement(f);
857 * Print a switch statement.
859 * @param statement the switch statement
861 static void print_switch_statement(const switch_statement_t *statement)
863 int const old_case_indent = case_indent;
864 case_indent = indent;
866 print_string("switch (");
867 print_expression(statement->expression);
869 print_inner_statement(statement->body);
871 case_indent = old_case_indent;
875 * Print a case label (including the default label).
877 * @param statement the case label statement
879 static void print_case_label(const case_label_statement_t *statement)
881 if (statement->expression == NULL) {
882 print_string("default:\n");
884 print_string("case ");
885 print_expression(statement->expression);
886 if (statement->end_range != NULL) {
887 print_string(" ... ");
888 print_expression(statement->end_range);
892 print_indented_statement(statement->statement);
895 static void print_typedef(const entity_t *entity)
897 print_string("typedef ");
898 print_type_ext(entity->typedefe.type, entity->base.symbol, NULL);
903 * returns true if the entity is a compiler generated one and has no real
904 * correspondenc in the source file
906 static bool is_generated_entity(const entity_t *entity)
908 if (entity->kind == ENTITY_TYPEDEF)
909 return entity->typedefe.builtin;
911 if (is_declaration(entity))
912 return entity->declaration.implicit;
918 * Print a declaration statement.
920 * @param statement the statement
922 static void print_declaration_statement(
923 const declaration_statement_t *statement)
926 entity_t *entity = statement->declarations_begin;
927 if (entity == NULL) {
928 print_string("/* empty declaration statement */");
932 entity_t *const end = statement->declarations_end->base.next;
933 for (; entity != end; entity = entity->base.next) {
934 if (entity->kind == ENTITY_ENUM_VALUE)
936 if (is_generated_entity(entity))
946 print_entity(entity);
951 * Print a while statement.
953 * @param statement the statement
955 static void print_while_statement(const while_statement_t *statement)
957 print_string("while (");
958 print_expression(statement->condition);
960 print_inner_statement(statement->body);
964 * Print a do-while statement.
966 * @param statement the statement
968 static void print_do_while_statement(const do_while_statement_t *statement)
971 print_inner_statement(statement->body);
972 print_after_inner_statement(statement->body);
973 print_string("while (");
974 print_expression(statement->condition);
979 * Print a for statement.
981 * @param statement the statement
983 static void print_for_statement(const for_statement_t *statement)
985 print_string("for (");
986 if (statement->initialisation != NULL) {
987 print_expression(statement->initialisation);
990 entity_t const *entity = statement->scope.entities;
991 for (; entity != NULL; entity = entity->base.next) {
992 if (is_generated_entity(entity))
994 /* FIXME display of multiple declarations is wrong */
995 print_declaration(entity);
998 if (statement->condition != NULL) {
1000 print_expression(statement->condition);
1003 if (statement->step != NULL) {
1005 print_expression(statement->step);
1008 print_inner_statement(statement->body);
1012 * Print assembler arguments.
1014 * @param arguments the arguments
1016 static void print_asm_arguments(asm_argument_t *arguments)
1018 asm_argument_t *argument = arguments;
1019 for (; argument != NULL; argument = argument->next) {
1020 if (argument != arguments)
1023 if (argument->symbol) {
1024 print_format("[%s] ", argument->symbol->string);
1026 print_quoted_string(&argument->constraints, '"');
1028 print_expression(argument->expression);
1034 * Print assembler clobbers.
1036 * @param clobbers the clobbers
1038 static void print_asm_clobbers(asm_clobber_t *clobbers)
1040 asm_clobber_t *clobber = clobbers;
1041 for (; clobber != NULL; clobber = clobber->next) {
1042 if (clobber != clobbers)
1045 print_quoted_string(&clobber->clobber, '"');
1050 * Print an assembler statement.
1052 * @param statement the statement
1054 static void print_asm_statement(const asm_statement_t *statement)
1056 print_string("asm ");
1057 if (statement->is_volatile) {
1058 print_string("volatile ");
1061 print_quoted_string(&statement->asm_text, '"');
1062 if (statement->outputs == NULL &&
1063 statement->inputs == NULL &&
1064 statement->clobbers == NULL)
1065 goto end_of_print_asm_statement;
1067 print_string(" : ");
1068 print_asm_arguments(statement->outputs);
1069 if (statement->inputs == NULL && statement->clobbers == NULL)
1070 goto end_of_print_asm_statement;
1072 print_string(" : ");
1073 print_asm_arguments(statement->inputs);
1074 if (statement->clobbers == NULL)
1075 goto end_of_print_asm_statement;
1077 print_string(" : ");
1078 print_asm_clobbers(statement->clobbers);
1080 end_of_print_asm_statement:
1085 * Print a microsoft __try statement.
1087 * @param statement the statement
1089 static void print_ms_try_statement(const ms_try_statement_t *statement)
1091 print_string("__try");
1092 print_inner_statement(statement->try_statement);
1093 print_after_inner_statement(statement->try_statement);
1094 if (statement->except_expression != NULL) {
1095 print_string("__except(");
1096 print_expression(statement->except_expression);
1099 print_string("__finally");
1101 print_inner_statement(statement->final_statement);
1105 * Print a microsoft __leave statement.
1107 * @param statement the statement
1109 static void print_leave_statement(const leave_statement_t *statement)
1112 print_string("__leave;");
1116 * Print a statement.
1118 * @param statement the statement
1120 void print_statement(statement_t const *const stmt)
1122 switch (stmt->kind) {
1123 case STATEMENT_ASM: print_asm_statement( &stmt->asms); break;
1124 case STATEMENT_BREAK: print_string("break;"); break;
1125 case STATEMENT_CASE_LABEL: print_case_label( &stmt->case_label); break;
1126 case STATEMENT_COMPOUND: print_compound_statement( &stmt->compound); break;
1127 case STATEMENT_COMPUTED_GOTO: print_computed_goto_statement(&stmt->computed_goto); break;
1128 case STATEMENT_CONTINUE: print_string("continue;"); break;
1129 case STATEMENT_DECLARATION: print_declaration_statement( &stmt->declaration); break;
1130 case STATEMENT_DO_WHILE: print_do_while_statement( &stmt->do_while); break;
1131 case STATEMENT_EMPTY: print_char(';'); break;
1132 case STATEMENT_ERROR: print_string("$error statement$"); break;
1133 case STATEMENT_EXPRESSION: print_expression_statement( &stmt->expression); break;
1134 case STATEMENT_FOR: print_for_statement( &stmt->fors); break;
1135 case STATEMENT_GOTO: print_goto_statement( &stmt->gotos); break;
1136 case STATEMENT_IF: print_if_statement( &stmt->ifs); break;
1137 case STATEMENT_LABEL: print_label_statement( &stmt->label); break;
1138 case STATEMENT_LEAVE: print_leave_statement( &stmt->leave); break;
1139 case STATEMENT_MS_TRY: print_ms_try_statement( &stmt->ms_try); break;
1140 case STATEMENT_RETURN: print_return_statement( &stmt->returns); break;
1141 case STATEMENT_SWITCH: print_switch_statement( &stmt->switchs); break;
1142 case STATEMENT_WHILE: print_while_statement( &stmt->whiles); break;
1147 * Print a storage class.
1149 * @param storage_class the storage class
1151 static void print_storage_class(storage_class_tag_t storage_class)
1153 switch (storage_class) {
1154 case STORAGE_CLASS_NONE: return;
1155 case STORAGE_CLASS_TYPEDEF: print_string("typedef "); return;
1156 case STORAGE_CLASS_EXTERN: print_string("extern "); return;
1157 case STORAGE_CLASS_STATIC: print_string("static "); return;
1158 case STORAGE_CLASS_AUTO: print_string("auto "); return;
1159 case STORAGE_CLASS_REGISTER: print_string("register "); return;
1161 panic("invalid storage class");
1165 * Print an initializer.
1167 * @param initializer the initializer
1169 void print_initializer(const initializer_t *initializer)
1171 if (initializer == NULL) {
1176 switch (initializer->kind) {
1177 case INITIALIZER_STRING:
1178 case INITIALIZER_VALUE:
1179 print_assignment_expression(initializer->value.value);
1182 case INITIALIZER_LIST: {
1184 const initializer_list_t *list = &initializer->list;
1186 for (size_t i = 0 ; i < list->len; ++i) {
1187 const initializer_t *sub_init = list->initializers[i];
1188 print_initializer(list->initializers[i]);
1189 if (i < list->len-1) {
1190 if (sub_init == NULL || sub_init->kind != INITIALIZER_DESIGNATOR)
1198 case INITIALIZER_DESIGNATOR:
1199 print_designator(initializer->designator.designator);
1200 print_string(" = ");
1204 panic("invalid initializer kind found");
1209 * Print microsoft extended declaration modifiers.
1211 static void print_ms_modifiers(const declaration_t *declaration)
1213 if ((c_mode & _MS) == 0)
1216 decl_modifiers_t modifiers = declaration->modifiers;
1218 bool ds_shown = false;
1219 const char *next = "(";
1221 if (declaration->base.kind == ENTITY_VARIABLE) {
1222 variable_t *variable = (variable_t*)declaration;
1223 if (variable->alignment != 0
1224 || variable->get_property_sym != NULL
1225 || variable->put_property_sym != NULL) {
1227 print_string("__declspec");
1231 if (variable->alignment != 0) {
1232 print_string(next); next = ", "; print_format("align(%u)", variable->alignment);
1234 if (variable->get_property_sym != NULL
1235 || variable->put_property_sym != NULL) {
1237 print_string(next); next = ", "; print_string("property(");
1238 if (variable->get_property_sym != NULL) {
1239 print_format("get=%s", variable->get_property_sym->string);
1242 if (variable->put_property_sym != NULL)
1243 print_format("%sput=%s", comma, variable->put_property_sym->string);
1249 /* DM_FORCEINLINE handled outside. */
1250 if ((modifiers & ~DM_FORCEINLINE) != 0) {
1252 print_string("__declspec");
1255 if (modifiers & DM_DLLIMPORT) {
1256 print_string(next); next = ", "; print_string("dllimport");
1258 if (modifiers & DM_DLLEXPORT) {
1259 print_string(next); next = ", "; print_string("dllexport");
1261 if (modifiers & DM_THREAD) {
1262 print_string(next); next = ", "; print_string("thread");
1264 if (modifiers & DM_NAKED) {
1265 print_string(next); next = ", "; print_string("naked");
1267 if (modifiers & DM_THREAD) {
1268 print_string(next); next = ", "; print_string("thread");
1270 if (modifiers & DM_SELECTANY) {
1271 print_string(next); next = ", "; print_string("selectany");
1273 if (modifiers & DM_NOTHROW) {
1274 print_string(next); next = ", "; print_string("nothrow");
1276 if (modifiers & DM_NORETURN) {
1277 print_string(next); next = ", "; print_string("noreturn");
1279 if (modifiers & DM_NOINLINE) {
1280 print_string(next); next = ", "; print_string("noinline");
1282 if (modifiers & DM_DEPRECATED) {
1283 print_string(next); next = ", "; print_string("deprecated");
1284 if (declaration->deprecated_string != NULL)
1285 print_format("(\"%s\")",
1286 declaration->deprecated_string);
1288 if (modifiers & DM_RESTRICT) {
1289 print_string(next); next = ", "; print_string("restrict");
1291 if (modifiers & DM_NOALIAS) {
1292 print_string(next); next = ", "; print_string("noalias");
1301 static void print_scope(const scope_t *scope)
1303 const entity_t *entity = scope->entities;
1304 for ( ; entity != NULL; entity = entity->base.next) {
1306 print_entity(entity);
1311 static void print_namespace(const namespace_t *namespace)
1313 print_string("namespace ");
1314 if (namespace->base.symbol != NULL) {
1315 print_string(namespace->base.symbol->string);
1319 print_string("{\n");
1322 print_scope(&namespace->members);
1326 print_string("}\n");
1330 * Print a variable or function declaration
1332 void print_declaration(const entity_t *entity)
1334 assert(is_declaration(entity));
1335 const declaration_t *declaration = &entity->declaration;
1337 print_storage_class((storage_class_tag_t)declaration->declared_storage_class);
1338 if (entity->kind == ENTITY_FUNCTION) {
1339 function_t *function = (function_t*)declaration;
1340 if (function->is_inline) {
1341 if (declaration->modifiers & DM_FORCEINLINE) {
1342 print_string("__forceinline ");
1343 } else if (declaration->modifiers & DM_MICROSOFT_INLINE) {
1344 print_string("__inline ");
1346 print_string("inline ");
1350 //print_ms_modifiers(declaration);
1351 switch (entity->kind) {
1352 case ENTITY_FUNCTION:
1353 print_type_ext(entity->declaration.type, entity->base.symbol,
1354 &entity->function.parameters);
1356 if (entity->function.statement != NULL) {
1358 print_indented_statement(entity->function.statement);
1364 case ENTITY_VARIABLE:
1365 if (entity->variable.thread_local)
1366 print_string("__thread ");
1367 print_type_ext(declaration->type, declaration->base.symbol, NULL);
1368 if (entity->variable.initializer != NULL) {
1369 print_string(" = ");
1370 print_initializer(entity->variable.initializer);
1374 case ENTITY_COMPOUND_MEMBER:
1375 print_type_ext(declaration->type, declaration->base.symbol, NULL);
1376 if (entity->compound_member.bitfield) {
1377 print_format(" : %u", entity->compound_member.bit_size);
1382 print_type_ext(declaration->type, declaration->base.symbol, NULL);
1389 * Prints an expression.
1391 * @param expression the expression
1393 void print_expression(const expression_t *expression)
1395 print_expression_prec(expression, PREC_BOTTOM);
1399 * Print a declaration.
1401 * @param declaration the declaration
1403 void print_entity(const entity_t *entity)
1405 if (entity->base.namespc != NAMESPACE_NORMAL && entity->base.symbol == NULL)
1408 switch ((entity_kind_tag_t)entity->kind) {
1409 case ENTITY_VARIABLE:
1410 case ENTITY_PARAMETER:
1411 case ENTITY_COMPOUND_MEMBER:
1412 case ENTITY_FUNCTION:
1413 print_declaration(entity);
1415 case ENTITY_TYPEDEF:
1416 print_typedef(entity);
1420 print_string("class ");
1421 print_string(entity->base.symbol->string);
1422 print_string("; /* TODO */\n");
1425 print_string("struct ");
1426 goto print_compound;
1428 print_string("union ");
1430 print_string(entity->base.symbol->string);
1431 if (entity->compound.complete) {
1433 print_compound_definition(&entity->compound);
1438 print_string("enum ");
1439 print_string(entity->base.symbol->string);
1441 print_enum_definition(&entity->enume);
1444 case ENTITY_NAMESPACE:
1445 print_namespace(&entity->namespacee);
1447 case ENTITY_LOCAL_LABEL:
1448 print_string("__label__ ");
1449 print_string(entity->base.symbol->string);
1453 case ENTITY_ENUM_VALUE:
1454 panic("print_entity used on unexpected entity type");
1456 panic("Invalid entity type encountered");
1460 * Print the AST of a translation unit.
1462 * @param unit the translation unit
1464 void print_ast(const translation_unit_t *unit)
1466 entity_t *entity = unit->scope.entities;
1467 for ( ; entity != NULL; entity = entity->base.next) {
1468 if (entity->kind == ENTITY_ENUM_VALUE)
1470 if (entity->base.namespc != NAMESPACE_NORMAL
1471 && entity->base.symbol == NULL)
1473 if (is_generated_entity(entity))
1477 print_entity(entity);
1482 expression_classification_t is_constant_initializer(const initializer_t *initializer)
1484 switch (initializer->kind) {
1485 case INITIALIZER_STRING:
1486 case INITIALIZER_DESIGNATOR:
1487 return EXPR_CLASS_CONSTANT;
1489 case INITIALIZER_VALUE:
1490 return is_linker_constant(initializer->value.value);
1492 case INITIALIZER_LIST: {
1493 expression_classification_t all = EXPR_CLASS_CONSTANT;
1494 for (size_t i = 0; i < initializer->list.len; ++i) {
1495 initializer_t *sub_initializer = initializer->list.initializers[i];
1496 expression_classification_t const cur = is_constant_initializer(sub_initializer);
1504 panic("invalid initializer kind found");
1508 * Checks if an expression references an object with a constant/known location
1509 * to the linker. Example:
1510 * - "x", "*&x" with x being a global variable. The value of x need not be
1511 * constant but the address of x is.
1512 * - "a.b.c" when a has a constant/known location to the linker
1514 static expression_classification_t is_object_with_linker_constant_address(
1515 const expression_t *expression)
1517 switch (expression->kind) {
1518 case EXPR_UNARY_DEREFERENCE:
1519 return is_linker_constant(expression->unary.value);
1522 type_t *base_type = skip_typeref(expression->select.compound->base.type);
1523 if (is_type_pointer(base_type)) {
1525 return is_linker_constant(expression->select.compound);
1527 return is_object_with_linker_constant_address(expression->select.compound);
1531 case EXPR_ARRAY_ACCESS: {
1532 expression_classification_t const ref = is_linker_constant(expression->array_access.array_ref);
1533 expression_classification_t const idx = is_constant_expression(expression->array_access.index);
1534 return ref < idx ? ref : idx;
1537 case EXPR_REFERENCE: {
1538 entity_t *entity = expression->reference.entity;
1539 if (!is_declaration(entity))
1540 return EXPR_CLASS_VARIABLE;
1542 switch ((storage_class_tag_t)entity->declaration.storage_class) {
1543 case STORAGE_CLASS_NONE:
1544 case STORAGE_CLASS_EXTERN:
1545 case STORAGE_CLASS_STATIC:
1547 entity->kind != ENTITY_VARIABLE ||
1548 !entity->variable.thread_local ? EXPR_CLASS_CONSTANT :
1549 EXPR_CLASS_VARIABLE;
1551 case STORAGE_CLASS_REGISTER:
1552 case STORAGE_CLASS_TYPEDEF:
1553 case STORAGE_CLASS_AUTO:
1556 return EXPR_CLASS_VARIABLE;
1560 return EXPR_CLASS_ERROR;
1563 return EXPR_CLASS_VARIABLE;
1567 expression_classification_t is_linker_constant(const expression_t *expression)
1569 switch (expression->kind) {
1570 case EXPR_STRING_LITERAL:
1572 case EXPR_LABEL_ADDRESS:
1573 return EXPR_CLASS_CONSTANT;
1575 case EXPR_COMPOUND_LITERAL:
1576 return is_constant_initializer(expression->compound_literal.initializer);
1578 case EXPR_UNARY_TAKE_ADDRESS:
1579 return is_object_with_linker_constant_address(expression->unary.value);
1581 case EXPR_UNARY_DEREFERENCE: {
1583 = revert_automatic_type_conversion(expression->unary.value);
1584 /* dereferencing a function is a NOP */
1585 if (is_type_function(real_type)) {
1586 return is_linker_constant(expression->unary.value);
1591 case EXPR_UNARY_CAST: {
1592 type_t *dest = skip_typeref(expression->base.type);
1593 if (!is_type_pointer(dest) && (
1594 dest->kind != TYPE_ATOMIC ||
1595 !(get_atomic_type_flags(dest->atomic.akind) & ATOMIC_TYPE_FLAG_INTEGER) ||
1596 get_atomic_type_size(dest->atomic.akind) < get_type_size(type_void_ptr)
1598 return is_constant_expression(expression);
1600 return is_linker_constant(expression->unary.value);
1603 case EXPR_BINARY_ADD:
1604 case EXPR_BINARY_SUB: {
1605 expression_t *const left = expression->binary.left;
1606 expression_t *const right = expression->binary.right;
1607 type_t *const ltype = skip_typeref(left->base.type);
1608 type_t *const rtype = skip_typeref(right->base.type);
1610 if (is_type_pointer(ltype)) {
1611 expression_classification_t const l = is_linker_constant(left);
1612 expression_classification_t const r = is_constant_expression(right);
1613 return l < r ? l : r;
1614 } else if (is_type_pointer(rtype)) {
1615 expression_classification_t const l = is_constant_expression(left);
1616 expression_classification_t const r = is_linker_constant(right);
1617 return l < r ? l : r;
1618 } else if (!is_type_valid(ltype) || !is_type_valid(rtype)) {
1619 return EXPR_CLASS_ERROR;
1621 return is_constant_expression(expression);
1625 case EXPR_REFERENCE: {
1626 entity_t *entity = expression->reference.entity;
1627 if (!is_declaration(entity))
1628 return EXPR_CLASS_VARIABLE;
1630 type_t *type = skip_typeref(entity->declaration.type);
1631 if (is_type_function(type))
1632 return EXPR_CLASS_CONSTANT;
1633 if (is_type_array(type)) {
1634 return is_object_with_linker_constant_address(expression);
1636 /* Prevent stray errors */
1637 if (!is_type_valid(type))
1638 return EXPR_CLASS_ERROR;
1639 return EXPR_CLASS_VARIABLE;
1642 case EXPR_ARRAY_ACCESS: {
1643 type_t *const type =
1644 skip_typeref(revert_automatic_type_conversion(expression));
1645 if (!is_type_array(type))
1646 return EXPR_CLASS_VARIABLE;
1647 return is_linker_constant(expression->array_access.array_ref);
1650 case EXPR_CONDITIONAL: {
1651 expression_t *const c = expression->conditional.condition;
1652 expression_classification_t const cclass = is_constant_expression(c);
1653 if (cclass != EXPR_CLASS_CONSTANT)
1656 if (fold_constant_to_bool(c)) {
1657 expression_t const *const t = expression->conditional.true_expression;
1658 return is_linker_constant(t != NULL ? t : c);
1660 return is_linker_constant(expression->conditional.false_expression);
1665 entity_t *entity = expression->select.compound_entry;
1666 if (!is_declaration(entity))
1667 return EXPR_CLASS_VARIABLE;
1668 type_t *type = skip_typeref(entity->declaration.type);
1669 if (is_type_array(type)) {
1670 /* arrays automatically convert to their address */
1671 expression_t *compound = expression->select.compound;
1672 type_t *base_type = skip_typeref(compound->base.type);
1673 if (is_type_pointer(base_type)) {
1675 return is_linker_constant(compound);
1677 return is_object_with_linker_constant_address(compound);
1680 return EXPR_CLASS_VARIABLE;
1684 return is_constant_expression(expression);
1689 * Check if the given expression is a call to a builtin function
1690 * returning a constant result.
1692 static expression_classification_t is_builtin_const_call(const expression_t *expression)
1694 expression_t *function = expression->call.function;
1695 if (function->kind != EXPR_REFERENCE)
1696 return EXPR_CLASS_VARIABLE;
1697 reference_expression_t *ref = &function->reference;
1698 if (ref->entity->kind != ENTITY_FUNCTION)
1699 return EXPR_CLASS_VARIABLE;
1701 switch (ref->entity->function.btk) {
1704 return EXPR_CLASS_CONSTANT;
1706 return EXPR_CLASS_VARIABLE;
1711 static expression_classification_t is_constant_pointer(const expression_t *expression)
1713 expression_classification_t const expr_class = is_constant_expression(expression);
1714 if (expr_class != EXPR_CLASS_VARIABLE)
1717 switch (expression->kind) {
1718 case EXPR_UNARY_CAST:
1719 return is_constant_pointer(expression->unary.value);
1721 return EXPR_CLASS_VARIABLE;
1725 static expression_classification_t is_object_with_constant_address(const expression_t *expression)
1727 switch (expression->kind) {
1729 expression_t *compound = expression->select.compound;
1730 type_t *compound_type = compound->base.type;
1731 compound_type = skip_typeref(compound_type);
1732 if (is_type_pointer(compound_type)) {
1733 return is_constant_pointer(compound);
1735 return is_object_with_constant_address(compound);
1739 case EXPR_ARRAY_ACCESS: {
1740 array_access_expression_t const* const array_access =
1741 &expression->array_access;
1742 expression_classification_t const idx_class = is_constant_expression(array_access->index);
1743 if (idx_class != EXPR_CLASS_CONSTANT)
1745 expression_classification_t const ref_addr = is_object_with_constant_address(array_access->array_ref);
1746 expression_classification_t const ref_ptr = is_constant_pointer(array_access->array_ref);
1747 return ref_addr > ref_ptr ? ref_addr : ref_ptr;
1750 case EXPR_UNARY_DEREFERENCE:
1751 return is_constant_pointer(expression->unary.value);
1754 return EXPR_CLASS_ERROR;
1757 return EXPR_CLASS_VARIABLE;
1761 expression_classification_t is_constant_expression(const expression_t *expression)
1763 switch (expression->kind) {
1764 case EXPR_LITERAL_CASES:
1765 case EXPR_LITERAL_CHARACTER:
1766 case EXPR_CLASSIFY_TYPE:
1769 case EXPR_BUILTIN_CONSTANT_P:
1770 case EXPR_BUILTIN_TYPES_COMPATIBLE_P:
1771 case EXPR_ENUM_CONSTANT:
1772 return EXPR_CLASS_CONSTANT;
1775 type_t *const type = skip_typeref(expression->typeprop.type);
1777 !is_type_array(type) || !type->array.is_vla ? EXPR_CLASS_CONSTANT :
1778 EXPR_CLASS_VARIABLE;
1781 case EXPR_STRING_LITERAL:
1783 case EXPR_LABEL_ADDRESS:
1788 case EXPR_STATEMENT:
1789 case EXPR_UNARY_POSTFIX_INCREMENT:
1790 case EXPR_UNARY_POSTFIX_DECREMENT:
1791 case EXPR_UNARY_PREFIX_INCREMENT:
1792 case EXPR_UNARY_PREFIX_DECREMENT:
1793 case EXPR_UNARY_ASSUME: /* has VOID type */
1794 case EXPR_UNARY_DEREFERENCE:
1795 case EXPR_UNARY_DELETE:
1796 case EXPR_UNARY_DELETE_ARRAY:
1797 case EXPR_UNARY_THROW:
1798 case EXPR_BINARY_ASSIGN:
1799 case EXPR_BINARY_MUL_ASSIGN:
1800 case EXPR_BINARY_DIV_ASSIGN:
1801 case EXPR_BINARY_MOD_ASSIGN:
1802 case EXPR_BINARY_ADD_ASSIGN:
1803 case EXPR_BINARY_SUB_ASSIGN:
1804 case EXPR_BINARY_SHIFTLEFT_ASSIGN:
1805 case EXPR_BINARY_SHIFTRIGHT_ASSIGN:
1806 case EXPR_BINARY_BITWISE_AND_ASSIGN:
1807 case EXPR_BINARY_BITWISE_XOR_ASSIGN:
1808 case EXPR_BINARY_BITWISE_OR_ASSIGN:
1809 case EXPR_BINARY_COMMA:
1810 case EXPR_ARRAY_ACCESS:
1811 return EXPR_CLASS_VARIABLE;
1813 case EXPR_REFERENCE: {
1814 type_t *const type = skip_typeref(expression->base.type);
1815 return is_type_valid(type) ? EXPR_CLASS_VARIABLE : EXPR_CLASS_ERROR;
1818 case EXPR_UNARY_TAKE_ADDRESS:
1819 return is_object_with_constant_address(expression->unary.value);
1822 return is_builtin_const_call(expression);
1824 case EXPR_UNARY_NEGATE:
1825 case EXPR_UNARY_PLUS:
1826 case EXPR_UNARY_BITWISE_NEGATE:
1827 case EXPR_UNARY_NOT:
1828 return is_constant_expression(expression->unary.value);
1830 case EXPR_UNARY_CAST: {
1831 type_t *const type = skip_typeref(expression->base.type);
1832 if (is_type_scalar(type))
1833 return is_constant_expression(expression->unary.value);
1834 if (!is_type_valid(type))
1835 return EXPR_CLASS_ERROR;
1836 return EXPR_CLASS_VARIABLE;
1839 case EXPR_BINARY_ADD:
1840 case EXPR_BINARY_SUB:
1841 case EXPR_BINARY_MUL:
1842 case EXPR_BINARY_DIV:
1843 case EXPR_BINARY_MOD:
1844 case EXPR_BINARY_EQUAL:
1845 case EXPR_BINARY_NOTEQUAL:
1846 case EXPR_BINARY_LESS:
1847 case EXPR_BINARY_LESSEQUAL:
1848 case EXPR_BINARY_GREATER:
1849 case EXPR_BINARY_GREATEREQUAL:
1850 case EXPR_BINARY_BITWISE_AND:
1851 case EXPR_BINARY_BITWISE_OR:
1852 case EXPR_BINARY_BITWISE_XOR:
1853 case EXPR_BINARY_SHIFTLEFT:
1854 case EXPR_BINARY_SHIFTRIGHT:
1855 case EXPR_BINARY_ISGREATER:
1856 case EXPR_BINARY_ISGREATEREQUAL:
1857 case EXPR_BINARY_ISLESS:
1858 case EXPR_BINARY_ISLESSEQUAL:
1859 case EXPR_BINARY_ISLESSGREATER:
1860 case EXPR_BINARY_ISUNORDERED: {
1861 expression_classification_t const l = is_constant_expression(expression->binary.left);
1862 expression_classification_t const r = is_constant_expression(expression->binary.right);
1863 return l < r ? l : r;
1866 case EXPR_BINARY_LOGICAL_AND: {
1867 expression_t const *const left = expression->binary.left;
1868 expression_classification_t const lclass = is_constant_expression(left);
1869 if (lclass != EXPR_CLASS_CONSTANT)
1871 if (!fold_constant_to_bool(left))
1872 return EXPR_CLASS_CONSTANT;
1873 return is_constant_expression(expression->binary.right);
1876 case EXPR_BINARY_LOGICAL_OR: {
1877 expression_t const *const left = expression->binary.left;
1878 expression_classification_t const lclass = is_constant_expression(left);
1879 if (lclass != EXPR_CLASS_CONSTANT)
1881 if (fold_constant_to_bool(left))
1882 return EXPR_CLASS_CONSTANT;
1883 return is_constant_expression(expression->binary.right);
1886 case EXPR_COMPOUND_LITERAL:
1887 return is_constant_initializer(expression->compound_literal.initializer);
1889 case EXPR_CONDITIONAL: {
1890 expression_t *const condition = expression->conditional.condition;
1891 expression_classification_t const cclass = is_constant_expression(condition);
1892 if (cclass != EXPR_CLASS_CONSTANT)
1895 if (fold_constant_to_bool(condition)) {
1896 expression_t const *const t = expression->conditional.true_expression;
1897 return t == NULL ? EXPR_CLASS_CONSTANT : is_constant_expression(t);
1899 return is_constant_expression(expression->conditional.false_expression);
1904 return EXPR_CLASS_ERROR;
1906 panic("invalid expression found (is constant expression)");
1911 obstack_init(&ast_obstack);
1916 obstack_free(&ast_obstack, NULL);