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;
51 bool print_implicit_casts = false;
52 bool print_parenthesis = false;
54 static void print_statement(const statement_t *statement);
55 static void print_expression_prec(const expression_t *expression, unsigned prec);
57 void change_indent(int delta)
63 void print_indent(void)
65 for (int i = 0; i < indent; ++i)
69 static void print_stringrep(const string_t *string)
71 for (size_t i = 0; i < string->size; ++i) {
72 print_char(string->begin[i]);
77 * Returns 1 if a given precedence level has right-to-left
78 * associativity, else 0.
80 * @param precedence the operator precedence
82 static int right_to_left(unsigned precedence)
86 case PREC_CONDITIONAL:
96 * Return the precedence of an expression given by its kind.
98 * @param kind the expression kind
100 static unsigned get_expression_precedence(expression_kind_t kind)
102 static const unsigned prec[] = {
103 [EXPR_REFERENCE] = PREC_PRIMARY,
104 [EXPR_REFERENCE_ENUM_VALUE] = PREC_PRIMARY,
105 [EXPR_LITERAL_INTEGER] = PREC_PRIMARY,
106 [EXPR_LITERAL_INTEGER_OCTAL] = PREC_PRIMARY,
107 [EXPR_LITERAL_INTEGER_HEXADECIMAL] = PREC_PRIMARY,
108 [EXPR_LITERAL_FLOATINGPOINT] = PREC_PRIMARY,
109 [EXPR_LITERAL_FLOATINGPOINT_HEXADECIMAL] = PREC_PRIMARY,
110 [EXPR_LITERAL_CHARACTER] = PREC_PRIMARY,
111 [EXPR_LITERAL_WIDE_CHARACTER] = PREC_PRIMARY,
112 [EXPR_LITERAL_MS_NOOP] = PREC_PRIMARY,
113 [EXPR_STRING_LITERAL] = PREC_PRIMARY,
114 [EXPR_WIDE_STRING_LITERAL] = PREC_PRIMARY,
115 [EXPR_COMPOUND_LITERAL] = PREC_UNARY,
116 [EXPR_CALL] = PREC_POSTFIX,
117 [EXPR_CONDITIONAL] = PREC_CONDITIONAL,
118 [EXPR_SELECT] = PREC_POSTFIX,
119 [EXPR_ARRAY_ACCESS] = PREC_POSTFIX,
120 [EXPR_SIZEOF] = PREC_UNARY,
121 [EXPR_CLASSIFY_TYPE] = PREC_UNARY,
122 [EXPR_ALIGNOF] = PREC_UNARY,
124 [EXPR_FUNCNAME] = PREC_PRIMARY,
125 [EXPR_BUILTIN_CONSTANT_P] = PREC_PRIMARY,
126 [EXPR_BUILTIN_TYPES_COMPATIBLE_P] = PREC_PRIMARY,
127 [EXPR_OFFSETOF] = PREC_PRIMARY,
128 [EXPR_VA_START] = PREC_PRIMARY,
129 [EXPR_VA_ARG] = PREC_PRIMARY,
130 [EXPR_VA_COPY] = PREC_PRIMARY,
131 [EXPR_STATEMENT] = PREC_PRIMARY,
132 [EXPR_LABEL_ADDRESS] = PREC_PRIMARY,
134 [EXPR_UNARY_NEGATE] = PREC_UNARY,
135 [EXPR_UNARY_PLUS] = PREC_UNARY,
136 [EXPR_UNARY_BITWISE_NEGATE] = PREC_UNARY,
137 [EXPR_UNARY_NOT] = PREC_UNARY,
138 [EXPR_UNARY_DEREFERENCE] = PREC_UNARY,
139 [EXPR_UNARY_TAKE_ADDRESS] = PREC_UNARY,
140 [EXPR_UNARY_POSTFIX_INCREMENT] = PREC_POSTFIX,
141 [EXPR_UNARY_POSTFIX_DECREMENT] = PREC_POSTFIX,
142 [EXPR_UNARY_PREFIX_INCREMENT] = PREC_UNARY,
143 [EXPR_UNARY_PREFIX_DECREMENT] = PREC_UNARY,
144 [EXPR_UNARY_CAST] = PREC_UNARY,
145 [EXPR_UNARY_ASSUME] = PREC_PRIMARY,
146 [EXPR_UNARY_DELETE] = PREC_UNARY,
147 [EXPR_UNARY_DELETE_ARRAY] = PREC_UNARY,
148 [EXPR_UNARY_THROW] = PREC_ASSIGNMENT,
150 [EXPR_BINARY_ADD] = PREC_ADDITIVE,
151 [EXPR_BINARY_SUB] = PREC_ADDITIVE,
152 [EXPR_BINARY_MUL] = PREC_MULTIPLICATIVE,
153 [EXPR_BINARY_DIV] = PREC_MULTIPLICATIVE,
154 [EXPR_BINARY_MOD] = PREC_MULTIPLICATIVE,
155 [EXPR_BINARY_EQUAL] = PREC_EQUALITY,
156 [EXPR_BINARY_NOTEQUAL] = PREC_EQUALITY,
157 [EXPR_BINARY_LESS] = PREC_RELATIONAL,
158 [EXPR_BINARY_LESSEQUAL] = PREC_RELATIONAL,
159 [EXPR_BINARY_GREATER] = PREC_RELATIONAL,
160 [EXPR_BINARY_GREATEREQUAL] = PREC_RELATIONAL,
161 [EXPR_BINARY_BITWISE_AND] = PREC_AND,
162 [EXPR_BINARY_BITWISE_OR] = PREC_OR,
163 [EXPR_BINARY_BITWISE_XOR] = PREC_XOR,
164 [EXPR_BINARY_LOGICAL_AND] = PREC_LOGICAL_AND,
165 [EXPR_BINARY_LOGICAL_OR] = PREC_LOGICAL_OR,
166 [EXPR_BINARY_SHIFTLEFT] = PREC_SHIFT,
167 [EXPR_BINARY_SHIFTRIGHT] = PREC_SHIFT,
168 [EXPR_BINARY_ASSIGN] = PREC_ASSIGNMENT,
169 [EXPR_BINARY_MUL_ASSIGN] = PREC_ASSIGNMENT,
170 [EXPR_BINARY_DIV_ASSIGN] = PREC_ASSIGNMENT,
171 [EXPR_BINARY_MOD_ASSIGN] = PREC_ASSIGNMENT,
172 [EXPR_BINARY_ADD_ASSIGN] = PREC_ASSIGNMENT,
173 [EXPR_BINARY_SUB_ASSIGN] = PREC_ASSIGNMENT,
174 [EXPR_BINARY_SHIFTLEFT_ASSIGN] = PREC_ASSIGNMENT,
175 [EXPR_BINARY_SHIFTRIGHT_ASSIGN] = PREC_ASSIGNMENT,
176 [EXPR_BINARY_BITWISE_AND_ASSIGN] = PREC_ASSIGNMENT,
177 [EXPR_BINARY_BITWISE_XOR_ASSIGN] = PREC_ASSIGNMENT,
178 [EXPR_BINARY_BITWISE_OR_ASSIGN] = PREC_ASSIGNMENT,
179 [EXPR_BINARY_COMMA] = PREC_EXPRESSION,
181 [EXPR_BINARY_ISGREATER] = PREC_PRIMARY,
182 [EXPR_BINARY_ISGREATEREQUAL] = PREC_PRIMARY,
183 [EXPR_BINARY_ISLESS] = PREC_PRIMARY,
184 [EXPR_BINARY_ISLESSEQUAL] = PREC_PRIMARY,
185 [EXPR_BINARY_ISLESSGREATER] = PREC_PRIMARY,
186 [EXPR_BINARY_ISUNORDERED] = PREC_PRIMARY
188 assert((size_t)kind < lengthof(prec));
189 unsigned res = prec[kind];
191 assert(res != PREC_BOTTOM);
196 * Print a quoted string constant.
198 * @param string the string constant
199 * @param border the border char
200 * @param skip number of chars to skip at the end
202 static void print_quoted_string(const string_t *const string, char border,
206 const char *end = string->begin + string->size - skip;
207 for (const char *c = string->begin; c != end; ++c) {
213 case '\\': print_string("\\\\"); break;
214 case '\a': print_string("\\a"); break;
215 case '\b': print_string("\\b"); break;
216 case '\f': print_string("\\f"); break;
217 case '\n': print_string("\\n"); break;
218 case '\r': print_string("\\r"); break;
219 case '\t': print_string("\\t"); break;
220 case '\v': print_string("\\v"); break;
221 case '\?': print_string("\\?"); break;
223 if (c_mode & _GNUC) {
224 print_string("\\e"); break;
228 if ((unsigned)tc < 0x80 && !isprint(tc)) {
229 print_format("\\%03o", (unsigned)tc);
239 static void print_string_literal(const string_literal_expression_t *literal)
241 if (literal->base.kind == EXPR_WIDE_STRING_LITERAL) {
244 print_quoted_string(&literal->value, '"', 1);
247 static void print_literal(const literal_expression_t *literal)
249 switch (literal->base.kind) {
250 case EXPR_LITERAL_MS_NOOP:
251 print_string("__noop");
253 case EXPR_LITERAL_INTEGER_HEXADECIMAL:
254 case EXPR_LITERAL_FLOATINGPOINT_HEXADECIMAL:
257 case EXPR_LITERAL_BOOLEAN:
258 case EXPR_LITERAL_INTEGER:
259 case EXPR_LITERAL_INTEGER_OCTAL:
260 case EXPR_LITERAL_FLOATINGPOINT:
261 print_stringrep(&literal->value);
262 if (literal->suffix.size > 0)
263 print_stringrep(&literal->suffix);
265 case EXPR_LITERAL_WIDE_CHARACTER:
268 case EXPR_LITERAL_CHARACTER:
269 print_quoted_string(&literal->value, '\'', 0);
274 print_string("INVALID LITERAL KIND");
278 * Prints a predefined symbol.
280 static void print_funcname(const funcname_expression_t *funcname)
283 switch (funcname->kind) {
284 case FUNCNAME_FUNCTION: s = (c_mode & _C99) ? "__func__" : "__FUNCTION__"; break;
285 case FUNCNAME_PRETTY_FUNCTION: s = "__PRETTY_FUNCTION__"; break;
286 case FUNCNAME_FUNCSIG: s = "__FUNCSIG__"; break;
287 case FUNCNAME_FUNCDNAME: s = "__FUNCDNAME__"; break;
292 static void print_compound_literal(
293 const compound_literal_expression_t *expression)
296 print_type(expression->type);
298 print_initializer(expression->initializer);
301 static void print_assignment_expression(const expression_t *const expr)
303 print_expression_prec(expr, PREC_ASSIGNMENT);
307 * Prints a call expression.
309 * @param call the call expression
311 static void print_call_expression(const call_expression_t *call)
313 print_expression_prec(call->function, PREC_POSTFIX);
315 call_argument_t *argument = call->arguments;
317 while (argument != NULL) {
323 print_assignment_expression(argument->expression);
325 argument = argument->next;
331 * Prints a binary expression.
333 * @param binexpr the binary expression
335 static void print_binary_expression(const binary_expression_t *binexpr)
337 unsigned prec = get_expression_precedence(binexpr->base.kind);
338 int r2l = right_to_left(prec);
340 print_expression_prec(binexpr->left, prec + r2l);
342 switch (binexpr->base.kind) {
343 case EXPR_BINARY_COMMA: op = ", "; break;
344 case EXPR_BINARY_ASSIGN: op = " = "; break;
345 case EXPR_BINARY_ADD: op = " + "; break;
346 case EXPR_BINARY_SUB: op = " - "; break;
347 case EXPR_BINARY_MUL: op = " * "; break;
348 case EXPR_BINARY_MOD: op = " % "; break;
349 case EXPR_BINARY_DIV: op = " / "; break;
350 case EXPR_BINARY_BITWISE_OR: op = " | "; break;
351 case EXPR_BINARY_BITWISE_AND: op = " & "; break;
352 case EXPR_BINARY_BITWISE_XOR: op = " ^ "; break;
353 case EXPR_BINARY_LOGICAL_OR: op = " || "; break;
354 case EXPR_BINARY_LOGICAL_AND: op = " && "; break;
355 case EXPR_BINARY_NOTEQUAL: op = " != "; break;
356 case EXPR_BINARY_EQUAL: op = " == "; break;
357 case EXPR_BINARY_LESS: op = " < "; break;
358 case EXPR_BINARY_LESSEQUAL: op = " <= "; break;
359 case EXPR_BINARY_GREATER: op = " > "; break;
360 case EXPR_BINARY_GREATEREQUAL: op = " >= "; break;
361 case EXPR_BINARY_SHIFTLEFT: op = " << "; break;
362 case EXPR_BINARY_SHIFTRIGHT: op = " >> "; break;
364 case EXPR_BINARY_ADD_ASSIGN: op = " += "; break;
365 case EXPR_BINARY_SUB_ASSIGN: op = " -= "; break;
366 case EXPR_BINARY_MUL_ASSIGN: op = " *= "; break;
367 case EXPR_BINARY_MOD_ASSIGN: op = " %= "; break;
368 case EXPR_BINARY_DIV_ASSIGN: op = " /= "; break;
369 case EXPR_BINARY_BITWISE_OR_ASSIGN: op = " |= "; break;
370 case EXPR_BINARY_BITWISE_AND_ASSIGN: op = " &= "; break;
371 case EXPR_BINARY_BITWISE_XOR_ASSIGN: op = " ^= "; break;
372 case EXPR_BINARY_SHIFTLEFT_ASSIGN: op = " <<= "; break;
373 case EXPR_BINARY_SHIFTRIGHT_ASSIGN: op = " >>= "; break;
374 default: panic("invalid binexpression found");
377 print_expression_prec(binexpr->right, prec + 1 - r2l);
381 * Prints an unary expression.
383 * @param unexpr the unary expression
385 static void print_unary_expression(const unary_expression_t *unexpr)
387 unsigned prec = get_expression_precedence(unexpr->base.kind);
388 switch (unexpr->base.kind) {
389 case EXPR_UNARY_NEGATE: print_string("-"); break;
390 case EXPR_UNARY_PLUS: print_string("+"); break;
391 case EXPR_UNARY_NOT: print_string("!"); break;
392 case EXPR_UNARY_BITWISE_NEGATE: print_string("~"); break;
393 case EXPR_UNARY_PREFIX_INCREMENT: print_string("++"); break;
394 case EXPR_UNARY_PREFIX_DECREMENT: print_string("--"); break;
395 case EXPR_UNARY_DEREFERENCE: print_string("*"); break;
396 case EXPR_UNARY_TAKE_ADDRESS: print_string("&"); break;
397 case EXPR_UNARY_DELETE: print_string("delete "); break;
398 case EXPR_UNARY_DELETE_ARRAY: print_string("delete [] "); break;
400 case EXPR_UNARY_POSTFIX_INCREMENT:
401 print_expression_prec(unexpr->value, prec);
404 case EXPR_UNARY_POSTFIX_DECREMENT:
405 print_expression_prec(unexpr->value, prec);
408 case EXPR_UNARY_CAST:
410 print_type(unexpr->base.type);
413 case EXPR_UNARY_ASSUME:
414 print_string("__assume(");
415 print_assignment_expression(unexpr->value);
419 case EXPR_UNARY_THROW:
420 if (unexpr->value == NULL) {
421 print_string("throw");
424 print_string("throw ");
428 panic("invalid unary expression found");
430 print_expression_prec(unexpr->value, prec);
434 * Prints a reference expression.
436 * @param ref the reference expression
438 static void print_reference_expression(const reference_expression_t *ref)
440 print_string(ref->entity->base.symbol->string);
444 * Prints a label address expression.
446 * @param ref the reference expression
448 static void print_label_address_expression(const label_address_expression_t *le)
450 print_format("&&%s", le->label->base.symbol->string);
454 * Prints an array expression.
456 * @param expression the array expression
458 static void print_array_expression(const array_access_expression_t *expression)
460 if (!expression->flipped) {
461 print_expression_prec(expression->array_ref, PREC_POSTFIX);
463 print_expression(expression->index);
466 print_expression_prec(expression->index, PREC_POSTFIX);
468 print_expression(expression->array_ref);
474 * Prints a typeproperty expression (sizeof or __alignof__).
476 * @param expression the type property expression
478 static void print_typeprop_expression(const typeprop_expression_t *expression)
480 if (expression->base.kind == EXPR_SIZEOF) {
481 print_string("sizeof");
483 assert(expression->base.kind == EXPR_ALIGNOF);
484 print_string("__alignof__");
486 if (expression->tp_expression != NULL) {
487 /* PREC_TOP: always print the '()' here, sizeof x is right but unusual */
488 print_expression_prec(expression->tp_expression, PREC_TOP);
491 print_type(expression->type);
497 * Prints a builtin constant expression.
499 * @param expression the builtin constant expression
501 static void print_builtin_constant(const builtin_constant_expression_t *expression)
503 print_string("__builtin_constant_p(");
504 print_assignment_expression(expression->value);
509 * Prints a builtin types compatible expression.
511 * @param expression the builtin types compatible expression
513 static void print_builtin_types_compatible(
514 const builtin_types_compatible_expression_t *expression)
516 print_string("__builtin_types_compatible_p(");
517 print_type(expression->left);
519 print_type(expression->right);
524 * Prints a conditional expression.
526 * @param expression the conditional expression
528 static void print_conditional(const conditional_expression_t *expression)
530 print_expression_prec(expression->condition, PREC_LOGICAL_OR);
531 if (expression->true_expression != NULL) {
533 print_expression_prec(expression->true_expression, PREC_EXPRESSION);
536 print_string(" ?: ");
538 precedence_t prec = c_mode & _CXX ? PREC_ASSIGNMENT : PREC_CONDITIONAL;
539 print_expression_prec(expression->false_expression, prec);
543 * Prints a va_start expression.
545 * @param expression the va_start expression
547 static void print_va_start(const va_start_expression_t *const expression)
549 print_string("__builtin_va_start(");
550 print_assignment_expression(expression->ap);
552 print_string(expression->parameter->base.base.symbol->string);
557 * Prints a va_arg expression.
559 * @param expression the va_arg expression
561 static void print_va_arg(const va_arg_expression_t *expression)
563 print_string("__builtin_va_arg(");
564 print_assignment_expression(expression->ap);
566 print_type(expression->base.type);
571 * Prints a va_copy expression.
573 * @param expression the va_copy expression
575 static void print_va_copy(const va_copy_expression_t *expression)
577 print_string("__builtin_va_copy(");
578 print_assignment_expression(expression->dst);
580 print_assignment_expression(expression->src);
585 * Prints a select expression (. or ->).
587 * @param expression the select expression
589 static void print_select(const select_expression_t *expression)
591 print_expression_prec(expression->compound, PREC_POSTFIX);
592 /* do not print anything for anonymous struct/union selects
593 * FIXME: if the anonymous select was a '->' this will print '.'
595 if (expression->compound_entry->base.symbol == NULL)
598 if (is_type_pointer(skip_typeref(expression->compound->base.type))) {
603 print_string(expression->compound_entry->base.symbol->string);
607 * Prints a type classify expression.
609 * @param expr the type classify expression
611 static void print_classify_type_expression(
612 const classify_type_expression_t *const expr)
614 print_string("__builtin_classify_type(");
615 print_assignment_expression(expr->type_expression);
620 * Prints a designator.
622 * @param designator the designator
624 static void print_designator(const designator_t *designator)
626 for ( ; designator != NULL; designator = designator->next) {
627 if (designator->symbol == NULL) {
629 print_expression(designator->array_index);
633 print_string(designator->symbol->string);
639 * Prints an offsetof expression.
641 * @param expression the offset expression
643 static void print_offsetof_expression(const offsetof_expression_t *expression)
645 print_string("__builtin_offsetof(");
646 print_type(expression->type);
648 print_designator(expression->designator);
653 * Prints a statement expression.
655 * @param expression the statement expression
657 static void print_statement_expression(const statement_expression_t *expression)
660 print_statement(expression->statement);
665 * Prints an expression with parenthesis if needed.
667 * @param expression the expression to print
668 * @param top_prec the precedence of the user of this expression.
670 static void print_expression_prec(const expression_t *expression, unsigned top_prec)
672 if (expression->kind == EXPR_UNARY_CAST
673 && expression->base.implicit && !print_implicit_casts) {
674 expression = expression->unary.value;
678 expression->base.parenthesized ||
679 (print_parenthesis && top_prec != PREC_BOTTOM) ||
680 top_prec > get_expression_precedence(expression->base.kind);
684 switch (expression->kind) {
686 print_string("$error$");
688 case EXPR_WIDE_STRING_LITERAL:
689 case EXPR_STRING_LITERAL:
690 print_string_literal(&expression->string_literal);
693 print_literal(&expression->literal);
696 print_funcname(&expression->funcname);
698 case EXPR_COMPOUND_LITERAL:
699 print_compound_literal(&expression->compound_literal);
702 print_call_expression(&expression->call);
705 print_binary_expression(&expression->binary);
708 case EXPR_REFERENCE_ENUM_VALUE:
709 print_reference_expression(&expression->reference);
711 case EXPR_ARRAY_ACCESS:
712 print_array_expression(&expression->array_access);
714 case EXPR_LABEL_ADDRESS:
715 print_label_address_expression(&expression->label_address);
718 print_unary_expression(&expression->unary);
722 print_typeprop_expression(&expression->typeprop);
724 case EXPR_BUILTIN_CONSTANT_P:
725 print_builtin_constant(&expression->builtin_constant);
727 case EXPR_BUILTIN_TYPES_COMPATIBLE_P:
728 print_builtin_types_compatible(&expression->builtin_types_compatible);
730 case EXPR_CONDITIONAL:
731 print_conditional(&expression->conditional);
734 print_va_start(&expression->va_starte);
737 print_va_arg(&expression->va_arge);
740 print_va_copy(&expression->va_copye);
743 print_select(&expression->select);
745 case EXPR_CLASSIFY_TYPE:
746 print_classify_type_expression(&expression->classify_type);
749 print_offsetof_expression(&expression->offsetofe);
752 print_statement_expression(&expression->statement);
759 static void print_indented_statement(statement_t const *const stmt)
761 switch (stmt->kind) {
762 case STATEMENT_LABEL:
765 case STATEMENT_CASE_LABEL:
772 print_statement(stmt);
776 * Print an compound statement.
778 * @param block the compound statement
780 static void print_compound_statement(const compound_statement_t *block)
785 statement_t *statement = block->statements;
786 while (statement != NULL) {
787 print_indented_statement(statement);
790 statement = statement->base.next;
798 * Print a return statement.
800 * @param statement the return statement
802 static void print_return_statement(const return_statement_t *statement)
804 expression_t const *const val = statement->value;
806 print_string("return ");
807 print_expression(val);
810 print_string("return;");
815 * Print an expression statement.
817 * @param statement the expression statement
819 static void print_expression_statement(const expression_statement_t *statement)
821 print_expression(statement->expression);
826 * Print a goto statement.
828 * @param statement the goto statement
830 static void print_goto_statement(const goto_statement_t *statement)
832 print_string("goto ");
833 if (statement->expression != NULL) {
835 print_expression(statement->expression);
837 print_string(statement->label->base.symbol->string);
843 * Print a label statement.
845 * @param statement the label statement
847 static void print_label_statement(const label_statement_t *statement)
849 print_format("%s:\n", statement->label->base.symbol->string);
850 print_indented_statement(statement->statement);
853 static void print_inner_statement(statement_t const *const stmt)
855 if (stmt->kind == STATEMENT_COMPOUND) {
857 print_compound_statement(&stmt->compound);
861 print_indented_statement(stmt);
867 * Print an if statement.
869 * @param statement the if statement
871 static void print_if_statement(const if_statement_t *statement)
873 print_string("if (");
874 print_expression(statement->condition);
876 print_inner_statement(statement->true_statement);
878 statement_t const *const f = statement->false_statement;
882 print_string("else");
883 if (f->kind == STATEMENT_IF) {
885 print_if_statement(&f->ifs);
887 print_inner_statement(f);
893 * Print a switch statement.
895 * @param statement the switch statement
897 static void print_switch_statement(const switch_statement_t *statement)
899 print_string("switch (");
900 print_expression(statement->expression);
902 print_inner_statement(statement->body);
906 * Print a case label (including the default label).
908 * @param statement the case label statement
910 static void print_case_label(const case_label_statement_t *statement)
912 if (statement->expression == NULL) {
913 print_string("default:\n");
915 print_string("case ");
916 print_expression(statement->expression);
917 if (statement->end_range != NULL) {
918 print_string(" ... ");
919 print_expression(statement->end_range);
924 print_indented_statement(statement->statement);
927 static void print_typedef(const entity_t *entity)
929 print_string("typedef ");
930 print_type_ext(entity->typedefe.type, entity->base.symbol, NULL);
935 * returns true if the entity is a compiler generated one and has no real
936 * correspondenc in the source file
938 static bool is_generated_entity(const entity_t *entity)
940 if (entity->kind == ENTITY_TYPEDEF)
941 return entity->typedefe.builtin;
943 if (is_declaration(entity))
944 return entity->declaration.implicit;
950 * Print a declaration statement.
952 * @param statement the statement
954 static void print_declaration_statement(
955 const declaration_statement_t *statement)
958 entity_t *entity = statement->declarations_begin;
959 if (entity == NULL) {
960 print_string("/* empty declaration statement */");
964 entity_t *const end = statement->declarations_end->base.next;
965 for (; entity != end; entity = entity->base.next) {
966 if (entity->kind == ENTITY_ENUM_VALUE)
968 if (is_generated_entity(entity))
978 print_entity(entity);
983 * Print a while statement.
985 * @param statement the statement
987 static void print_while_statement(const while_statement_t *statement)
989 print_string("while (");
990 print_expression(statement->condition);
992 print_inner_statement(statement->body);
996 * Print a do-while statement.
998 * @param statement the statement
1000 static void print_do_while_statement(const do_while_statement_t *statement)
1003 print_inner_statement(statement->body);
1006 print_string("while (");
1007 print_expression(statement->condition);
1012 * Print a for statement.
1014 * @param statement the statement
1016 static void print_for_statement(const for_statement_t *statement)
1018 print_string("for (");
1019 if (statement->initialisation != NULL) {
1020 print_expression(statement->initialisation);
1023 entity_t const *entity = statement->scope.entities;
1024 for (; entity != NULL; entity = entity->base.next) {
1025 if (is_generated_entity(entity))
1027 /* FIXME display of multiple declarations is wrong */
1028 print_declaration(entity);
1031 if (statement->condition != NULL) {
1033 print_expression(statement->condition);
1036 if (statement->step != NULL) {
1038 print_expression(statement->step);
1041 print_inner_statement(statement->body);
1045 * Print assembler arguments.
1047 * @param arguments the arguments
1049 static void print_asm_arguments(asm_argument_t *arguments)
1051 asm_argument_t *argument = arguments;
1052 for (; argument != NULL; argument = argument->next) {
1053 if (argument != arguments)
1056 if (argument->symbol) {
1057 print_format("[%s] ", argument->symbol->string);
1059 print_quoted_string(&argument->constraints, '"', 1);
1061 print_expression(argument->expression);
1067 * Print assembler clobbers.
1069 * @param clobbers the clobbers
1071 static void print_asm_clobbers(asm_clobber_t *clobbers)
1073 asm_clobber_t *clobber = clobbers;
1074 for (; clobber != NULL; clobber = clobber->next) {
1075 if (clobber != clobbers)
1078 print_quoted_string(&clobber->clobber, '"', 1);
1083 * Print an assembler statement.
1085 * @param statement the statement
1087 static void print_asm_statement(const asm_statement_t *statement)
1089 print_string("asm ");
1090 if (statement->is_volatile) {
1091 print_string("volatile ");
1094 print_quoted_string(&statement->asm_text, '"', 1);
1095 if (statement->outputs == NULL &&
1096 statement->inputs == NULL &&
1097 statement->clobbers == NULL)
1098 goto end_of_print_asm_statement;
1100 print_string(" : ");
1101 print_asm_arguments(statement->outputs);
1102 if (statement->inputs == NULL && statement->clobbers == NULL)
1103 goto end_of_print_asm_statement;
1105 print_string(" : ");
1106 print_asm_arguments(statement->inputs);
1107 if (statement->clobbers == NULL)
1108 goto end_of_print_asm_statement;
1110 print_string(" : ");
1111 print_asm_clobbers(statement->clobbers);
1113 end_of_print_asm_statement:
1118 * Print a microsoft __try statement.
1120 * @param statement the statement
1122 static void print_ms_try_statement(const ms_try_statement_t *statement)
1124 print_string("__try");
1125 print_inner_statement(statement->try_statement);
1128 if (statement->except_expression != NULL) {
1129 print_string("__except(");
1130 print_expression(statement->except_expression);
1133 print_string("__finally");
1135 print_inner_statement(statement->final_statement);
1139 * Print a microsoft __leave statement.
1141 * @param statement the statement
1143 static void print_leave_statement(const leave_statement_t *statement)
1146 print_string("__leave;");
1150 * Print a statement.
1152 * @param statement the statement
1154 void print_statement(const statement_t *statement)
1156 switch (statement->kind) {
1157 case STATEMENT_EMPTY:
1160 case STATEMENT_COMPOUND:
1161 print_compound_statement(&statement->compound);
1163 case STATEMENT_RETURN:
1164 print_return_statement(&statement->returns);
1166 case STATEMENT_EXPRESSION:
1167 print_expression_statement(&statement->expression);
1169 case STATEMENT_LABEL:
1170 print_label_statement(&statement->label);
1172 case STATEMENT_GOTO:
1173 print_goto_statement(&statement->gotos);
1175 case STATEMENT_CONTINUE:
1176 print_string("continue;");
1178 case STATEMENT_BREAK:
1179 print_string("break;");
1182 print_if_statement(&statement->ifs);
1184 case STATEMENT_SWITCH:
1185 print_switch_statement(&statement->switchs);
1187 case STATEMENT_CASE_LABEL:
1188 print_case_label(&statement->case_label);
1190 case STATEMENT_DECLARATION:
1191 print_declaration_statement(&statement->declaration);
1193 case STATEMENT_WHILE:
1194 print_while_statement(&statement->whiles);
1196 case STATEMENT_DO_WHILE:
1197 print_do_while_statement(&statement->do_while);
1200 print_for_statement(&statement->fors);
1203 print_asm_statement(&statement->asms);
1205 case STATEMENT_MS_TRY:
1206 print_ms_try_statement(&statement->ms_try);
1208 case STATEMENT_LEAVE:
1209 print_leave_statement(&statement->leave);
1211 case STATEMENT_ERROR:
1212 print_string("$error statement$");
1218 * Print a storage class.
1220 * @param storage_class the storage class
1222 static void print_storage_class(storage_class_tag_t storage_class)
1224 switch (storage_class) {
1225 case STORAGE_CLASS_NONE: return;
1226 case STORAGE_CLASS_TYPEDEF: print_string("typedef "); return;
1227 case STORAGE_CLASS_EXTERN: print_string("extern "); return;
1228 case STORAGE_CLASS_STATIC: print_string("static "); return;
1229 case STORAGE_CLASS_AUTO: print_string("auto "); return;
1230 case STORAGE_CLASS_REGISTER: print_string("register "); return;
1232 panic("invalid storage class");
1236 * Print an initializer.
1238 * @param initializer the initializer
1240 void print_initializer(const initializer_t *initializer)
1242 if (initializer == NULL) {
1247 switch (initializer->kind) {
1248 case INITIALIZER_VALUE: {
1249 const initializer_value_t *value = &initializer->value;
1250 print_assignment_expression(value->value);
1253 case INITIALIZER_LIST: {
1254 assert(initializer->kind == INITIALIZER_LIST);
1256 const initializer_list_t *list = &initializer->list;
1258 for (size_t i = 0 ; i < list->len; ++i) {
1259 const initializer_t *sub_init = list->initializers[i];
1260 print_initializer(list->initializers[i]);
1261 if (i < list->len-1) {
1262 if (sub_init == NULL || sub_init->kind != INITIALIZER_DESIGNATOR)
1269 case INITIALIZER_STRING:
1270 print_quoted_string(&initializer->string.string, '"', 1);
1272 case INITIALIZER_WIDE_STRING:
1273 print_quoted_string(&initializer->string.string, '"', 1);
1275 case INITIALIZER_DESIGNATOR:
1276 print_designator(initializer->designator.designator);
1277 print_string(" = ");
1281 panic("invalid initializer kind found");
1286 * Print microsoft extended declaration modifiers.
1288 static void print_ms_modifiers(const declaration_t *declaration)
1290 if ((c_mode & _MS) == 0)
1293 decl_modifiers_t modifiers = declaration->modifiers;
1295 bool ds_shown = false;
1296 const char *next = "(";
1298 if (declaration->base.kind == ENTITY_VARIABLE) {
1299 variable_t *variable = (variable_t*)declaration;
1300 if (variable->alignment != 0
1301 || variable->get_property_sym != NULL
1302 || variable->put_property_sym != NULL) {
1304 print_string("__declspec");
1308 if (variable->alignment != 0) {
1309 print_string(next); next = ", "; print_format("align(%u)", variable->alignment);
1311 if (variable->get_property_sym != NULL
1312 || variable->put_property_sym != NULL) {
1314 print_string(next); next = ", "; print_string("property(");
1315 if (variable->get_property_sym != NULL) {
1316 print_format("get=%s", variable->get_property_sym->string);
1319 if (variable->put_property_sym != NULL)
1320 print_format("%sput=%s", comma, variable->put_property_sym->string);
1326 /* DM_FORCEINLINE handled outside. */
1327 if ((modifiers & ~DM_FORCEINLINE) != 0) {
1329 print_string("__declspec");
1332 if (modifiers & DM_DLLIMPORT) {
1333 print_string(next); next = ", "; print_string("dllimport");
1335 if (modifiers & DM_DLLEXPORT) {
1336 print_string(next); next = ", "; print_string("dllexport");
1338 if (modifiers & DM_THREAD) {
1339 print_string(next); next = ", "; print_string("thread");
1341 if (modifiers & DM_NAKED) {
1342 print_string(next); next = ", "; print_string("naked");
1344 if (modifiers & DM_THREAD) {
1345 print_string(next); next = ", "; print_string("thread");
1347 if (modifiers & DM_SELECTANY) {
1348 print_string(next); next = ", "; print_string("selectany");
1350 if (modifiers & DM_NOTHROW) {
1351 print_string(next); next = ", "; print_string("nothrow");
1353 if (modifiers & DM_NORETURN) {
1354 print_string(next); next = ", "; print_string("noreturn");
1356 if (modifiers & DM_NOINLINE) {
1357 print_string(next); next = ", "; print_string("noinline");
1359 if (modifiers & DM_DEPRECATED) {
1360 print_string(next); next = ", "; print_string("deprecated");
1361 if (declaration->deprecated_string != NULL)
1362 print_format("(\"%s\")",
1363 declaration->deprecated_string);
1365 if (modifiers & DM_RESTRICT) {
1366 print_string(next); next = ", "; print_string("restrict");
1368 if (modifiers & DM_NOALIAS) {
1369 print_string(next); next = ", "; print_string("noalias");
1378 static void print_scope(const scope_t *scope)
1380 const entity_t *entity = scope->entities;
1381 for ( ; entity != NULL; entity = entity->base.next) {
1383 print_entity(entity);
1388 static void print_namespace(const namespace_t *namespace)
1390 print_string("namespace ");
1391 if (namespace->base.symbol != NULL) {
1392 print_string(namespace->base.symbol->string);
1396 print_string("{\n");
1399 print_scope(&namespace->members);
1403 print_string("}\n");
1407 * Print a variable or function declaration
1409 void print_declaration(const entity_t *entity)
1411 assert(is_declaration(entity));
1412 const declaration_t *declaration = &entity->declaration;
1414 print_storage_class((storage_class_tag_t)declaration->declared_storage_class);
1415 if (entity->kind == ENTITY_FUNCTION) {
1416 function_t *function = (function_t*)declaration;
1417 if (function->is_inline) {
1418 if (declaration->modifiers & DM_FORCEINLINE) {
1419 print_string("__forceinline ");
1420 } else if (declaration->modifiers & DM_MICROSOFT_INLINE) {
1421 print_string("__inline ");
1423 print_string("inline ");
1427 //print_ms_modifiers(declaration);
1428 switch (entity->kind) {
1429 case ENTITY_FUNCTION:
1430 print_type_ext(entity->declaration.type, entity->base.symbol,
1431 &entity->function.parameters);
1433 if (entity->function.statement != NULL) {
1435 print_indented_statement(entity->function.statement);
1441 case ENTITY_VARIABLE:
1442 if (entity->variable.thread_local)
1443 print_string("__thread ");
1444 print_type_ext(declaration->type, declaration->base.symbol, NULL);
1445 if (entity->variable.initializer != NULL) {
1446 print_string(" = ");
1447 print_initializer(entity->variable.initializer);
1451 case ENTITY_COMPOUND_MEMBER:
1452 print_type_ext(declaration->type, declaration->base.symbol, NULL);
1453 if (entity->compound_member.bitfield) {
1454 print_format(" : %u", entity->compound_member.bit_size);
1459 print_type_ext(declaration->type, declaration->base.symbol, NULL);
1466 * Prints an expression.
1468 * @param expression the expression
1470 void print_expression(const expression_t *expression)
1472 print_expression_prec(expression, PREC_BOTTOM);
1476 * Print a declaration.
1478 * @param declaration the declaration
1480 void print_entity(const entity_t *entity)
1482 if (entity->base.namespc != NAMESPACE_NORMAL && entity->base.symbol == NULL)
1485 switch ((entity_kind_tag_t)entity->kind) {
1486 case ENTITY_VARIABLE:
1487 case ENTITY_PARAMETER:
1488 case ENTITY_COMPOUND_MEMBER:
1489 case ENTITY_FUNCTION:
1490 print_declaration(entity);
1492 case ENTITY_TYPEDEF:
1493 print_typedef(entity);
1497 print_string("class ");
1498 print_string(entity->base.symbol->string);
1499 print_string("; /* TODO */\n");
1502 print_string("struct ");
1503 goto print_compound;
1505 print_string("union ");
1507 print_string(entity->base.symbol->string);
1508 if (entity->compound.complete) {
1510 print_compound_definition(&entity->compound);
1515 print_string("enum ");
1516 print_string(entity->base.symbol->string);
1518 print_enum_definition(&entity->enume);
1521 case ENTITY_NAMESPACE:
1522 print_namespace(&entity->namespacee);
1524 case ENTITY_LOCAL_LABEL:
1525 print_string("__label__ ");
1526 print_string(entity->base.symbol->string);
1530 case ENTITY_ENUM_VALUE:
1531 panic("print_entity used on unexpected entity type");
1533 panic("Invalid entity type encountered");
1537 * Print the AST of a translation unit.
1539 * @param unit the translation unit
1541 void print_ast(const translation_unit_t *unit)
1543 entity_t *entity = unit->scope.entities;
1544 for ( ; entity != NULL; entity = entity->base.next) {
1545 if (entity->kind == ENTITY_ENUM_VALUE)
1547 if (entity->base.namespc != NAMESPACE_NORMAL
1548 && entity->base.symbol == NULL)
1550 if (is_generated_entity(entity))
1554 print_entity(entity);
1559 expression_classification_t is_constant_initializer(const initializer_t *initializer)
1561 switch (initializer->kind) {
1562 case INITIALIZER_STRING:
1563 case INITIALIZER_WIDE_STRING:
1564 case INITIALIZER_DESIGNATOR:
1565 return EXPR_CLASS_CONSTANT;
1567 case INITIALIZER_VALUE:
1568 return is_linker_constant(initializer->value.value);
1570 case INITIALIZER_LIST: {
1571 expression_classification_t all = EXPR_CLASS_CONSTANT;
1572 for (size_t i = 0; i < initializer->list.len; ++i) {
1573 initializer_t *sub_initializer = initializer->list.initializers[i];
1574 expression_classification_t const cur = is_constant_initializer(sub_initializer);
1582 panic("invalid initializer kind found");
1586 * Checks if an expression references an object with a constant/known location
1587 * to the linker. Example:
1588 * - "x", "*&x" with x being a global variable. The value of x need not be
1589 * constant but the address of x is.
1590 * - "a.b.c" when a has a constant/known location to the linker
1592 static expression_classification_t is_object_with_linker_constant_address(
1593 const expression_t *expression)
1595 switch (expression->kind) {
1596 case EXPR_UNARY_DEREFERENCE:
1597 return is_linker_constant(expression->unary.value);
1600 type_t *base_type = skip_typeref(expression->select.compound->base.type);
1601 if (is_type_pointer(base_type)) {
1603 return is_linker_constant(expression->select.compound);
1605 return is_object_with_linker_constant_address(expression->select.compound);
1609 case EXPR_ARRAY_ACCESS: {
1610 expression_classification_t const ref = is_linker_constant(expression->array_access.array_ref);
1611 expression_classification_t const idx = is_constant_expression(expression->array_access.index);
1612 return ref < idx ? ref : idx;
1615 case EXPR_REFERENCE: {
1616 entity_t *entity = expression->reference.entity;
1617 if (!is_declaration(entity))
1618 return EXPR_CLASS_VARIABLE;
1620 switch ((storage_class_tag_t)entity->declaration.storage_class) {
1621 case STORAGE_CLASS_NONE:
1622 case STORAGE_CLASS_EXTERN:
1623 case STORAGE_CLASS_STATIC:
1625 entity->kind != ENTITY_VARIABLE ||
1626 !entity->variable.thread_local ? EXPR_CLASS_CONSTANT :
1627 EXPR_CLASS_VARIABLE;
1629 case STORAGE_CLASS_REGISTER:
1630 case STORAGE_CLASS_TYPEDEF:
1631 case STORAGE_CLASS_AUTO:
1634 return EXPR_CLASS_VARIABLE;
1638 return EXPR_CLASS_ERROR;
1641 return EXPR_CLASS_VARIABLE;
1645 expression_classification_t is_linker_constant(const expression_t *expression)
1647 switch (expression->kind) {
1648 case EXPR_STRING_LITERAL:
1649 case EXPR_WIDE_STRING_LITERAL:
1651 case EXPR_LABEL_ADDRESS:
1652 return EXPR_CLASS_CONSTANT;
1654 case EXPR_COMPOUND_LITERAL:
1655 return is_constant_initializer(expression->compound_literal.initializer);
1657 case EXPR_UNARY_TAKE_ADDRESS:
1658 return is_object_with_linker_constant_address(expression->unary.value);
1660 case EXPR_UNARY_DEREFERENCE: {
1662 = revert_automatic_type_conversion(expression->unary.value);
1663 /* dereferencing a function is a NOP */
1664 if (is_type_function(real_type)) {
1665 return is_linker_constant(expression->unary.value);
1670 case EXPR_UNARY_CAST: {
1671 type_t *dest = skip_typeref(expression->base.type);
1672 if (!is_type_pointer(dest) && (
1673 dest->kind != TYPE_ATOMIC ||
1674 !(get_atomic_type_flags(dest->atomic.akind) & ATOMIC_TYPE_FLAG_INTEGER) ||
1675 get_atomic_type_size(dest->atomic.akind) < get_type_size(type_void_ptr)
1677 return is_constant_expression(expression);
1679 return is_linker_constant(expression->unary.value);
1682 case EXPR_BINARY_ADD:
1683 case EXPR_BINARY_SUB: {
1684 expression_t *const left = expression->binary.left;
1685 expression_t *const right = expression->binary.right;
1686 type_t *const ltype = skip_typeref(left->base.type);
1687 type_t *const rtype = skip_typeref(right->base.type);
1689 if (is_type_pointer(ltype)) {
1690 expression_classification_t const l = is_linker_constant(left);
1691 expression_classification_t const r = is_constant_expression(right);
1692 return l < r ? l : r;
1693 } else if (is_type_pointer(rtype)) {
1694 expression_classification_t const l = is_constant_expression(left);
1695 expression_classification_t const r = is_linker_constant(right);
1696 return l < r ? l : r;
1697 } else if (!is_type_valid(ltype) || !is_type_valid(rtype)) {
1698 return EXPR_CLASS_ERROR;
1700 return is_constant_expression(expression);
1704 case EXPR_REFERENCE: {
1705 entity_t *entity = expression->reference.entity;
1706 if (!is_declaration(entity))
1707 return EXPR_CLASS_VARIABLE;
1709 type_t *type = skip_typeref(entity->declaration.type);
1710 if (is_type_function(type))
1711 return EXPR_CLASS_CONSTANT;
1712 if (is_type_array(type)) {
1713 return is_object_with_linker_constant_address(expression);
1715 /* Prevent stray errors */
1716 if (!is_type_valid(type))
1717 return EXPR_CLASS_ERROR;
1718 return EXPR_CLASS_VARIABLE;
1721 case EXPR_ARRAY_ACCESS: {
1722 type_t *const type =
1723 skip_typeref(revert_automatic_type_conversion(expression));
1724 if (!is_type_array(type))
1725 return EXPR_CLASS_VARIABLE;
1726 return is_linker_constant(expression->array_access.array_ref);
1729 case EXPR_CONDITIONAL: {
1730 expression_t *const c = expression->conditional.condition;
1731 expression_classification_t const cclass = is_constant_expression(c);
1732 if (cclass != EXPR_CLASS_CONSTANT)
1735 if (fold_constant_to_bool(c)) {
1736 expression_t const *const t = expression->conditional.true_expression;
1737 return is_linker_constant(t != NULL ? t : c);
1739 return is_linker_constant(expression->conditional.false_expression);
1744 entity_t *entity = expression->select.compound_entry;
1745 if (!is_declaration(entity))
1746 return EXPR_CLASS_VARIABLE;
1747 type_t *type = skip_typeref(entity->declaration.type);
1748 if (is_type_array(type)) {
1749 /* arrays automatically convert to their address */
1750 expression_t *compound = expression->select.compound;
1751 type_t *base_type = skip_typeref(compound->base.type);
1752 if (is_type_pointer(base_type)) {
1754 return is_linker_constant(compound);
1756 return is_object_with_linker_constant_address(compound);
1759 return EXPR_CLASS_VARIABLE;
1763 return is_constant_expression(expression);
1768 * Check if the given expression is a call to a builtin function
1769 * returning a constant result.
1771 static expression_classification_t is_builtin_const_call(const expression_t *expression)
1773 expression_t *function = expression->call.function;
1774 if (function->kind != EXPR_REFERENCE)
1775 return EXPR_CLASS_VARIABLE;
1776 reference_expression_t *ref = &function->reference;
1777 if (ref->entity->kind != ENTITY_FUNCTION)
1778 return EXPR_CLASS_VARIABLE;
1780 switch (ref->entity->function.btk) {
1783 return EXPR_CLASS_CONSTANT;
1785 return EXPR_CLASS_VARIABLE;
1790 static expression_classification_t is_constant_pointer(const expression_t *expression)
1792 expression_classification_t const expr_class = is_constant_expression(expression);
1793 if (expr_class != EXPR_CLASS_VARIABLE)
1796 switch (expression->kind) {
1797 case EXPR_UNARY_CAST:
1798 return is_constant_pointer(expression->unary.value);
1800 return EXPR_CLASS_VARIABLE;
1804 static expression_classification_t is_object_with_constant_address(const expression_t *expression)
1806 switch (expression->kind) {
1808 expression_t *compound = expression->select.compound;
1809 type_t *compound_type = compound->base.type;
1810 compound_type = skip_typeref(compound_type);
1811 if (is_type_pointer(compound_type)) {
1812 return is_constant_pointer(compound);
1814 return is_object_with_constant_address(compound);
1818 case EXPR_ARRAY_ACCESS: {
1819 array_access_expression_t const* const array_access =
1820 &expression->array_access;
1821 expression_classification_t const idx_class = is_constant_expression(array_access->index);
1822 if (idx_class != EXPR_CLASS_CONSTANT)
1824 expression_classification_t const ref_addr = is_object_with_constant_address(array_access->array_ref);
1825 expression_classification_t const ref_ptr = is_constant_pointer(array_access->array_ref);
1826 return ref_addr > ref_ptr ? ref_addr : ref_ptr;
1829 case EXPR_UNARY_DEREFERENCE:
1830 return is_constant_pointer(expression->unary.value);
1833 return EXPR_CLASS_ERROR;
1836 return EXPR_CLASS_VARIABLE;
1840 expression_classification_t is_constant_expression(const expression_t *expression)
1842 switch (expression->kind) {
1844 case EXPR_CLASSIFY_TYPE:
1847 case EXPR_BUILTIN_CONSTANT_P:
1848 case EXPR_BUILTIN_TYPES_COMPATIBLE_P:
1849 case EXPR_REFERENCE_ENUM_VALUE:
1850 return EXPR_CLASS_CONSTANT;
1853 type_t *const type = skip_typeref(expression->typeprop.type);
1855 !is_type_array(type) || !type->array.is_vla ? EXPR_CLASS_CONSTANT :
1856 EXPR_CLASS_VARIABLE;
1859 case EXPR_STRING_LITERAL:
1860 case EXPR_WIDE_STRING_LITERAL:
1862 case EXPR_LABEL_ADDRESS:
1867 case EXPR_STATEMENT:
1868 case EXPR_UNARY_POSTFIX_INCREMENT:
1869 case EXPR_UNARY_POSTFIX_DECREMENT:
1870 case EXPR_UNARY_PREFIX_INCREMENT:
1871 case EXPR_UNARY_PREFIX_DECREMENT:
1872 case EXPR_UNARY_ASSUME: /* has VOID type */
1873 case EXPR_UNARY_DEREFERENCE:
1874 case EXPR_UNARY_DELETE:
1875 case EXPR_UNARY_DELETE_ARRAY:
1876 case EXPR_UNARY_THROW:
1877 case EXPR_BINARY_ASSIGN:
1878 case EXPR_BINARY_MUL_ASSIGN:
1879 case EXPR_BINARY_DIV_ASSIGN:
1880 case EXPR_BINARY_MOD_ASSIGN:
1881 case EXPR_BINARY_ADD_ASSIGN:
1882 case EXPR_BINARY_SUB_ASSIGN:
1883 case EXPR_BINARY_SHIFTLEFT_ASSIGN:
1884 case EXPR_BINARY_SHIFTRIGHT_ASSIGN:
1885 case EXPR_BINARY_BITWISE_AND_ASSIGN:
1886 case EXPR_BINARY_BITWISE_XOR_ASSIGN:
1887 case EXPR_BINARY_BITWISE_OR_ASSIGN:
1888 case EXPR_BINARY_COMMA:
1889 case EXPR_ARRAY_ACCESS:
1890 return EXPR_CLASS_VARIABLE;
1892 case EXPR_REFERENCE: {
1893 type_t *const type = skip_typeref(expression->base.type);
1894 return is_type_valid(type) ? EXPR_CLASS_VARIABLE : EXPR_CLASS_ERROR;
1897 case EXPR_UNARY_TAKE_ADDRESS:
1898 return is_object_with_constant_address(expression->unary.value);
1901 return is_builtin_const_call(expression);
1903 case EXPR_UNARY_NEGATE:
1904 case EXPR_UNARY_PLUS:
1905 case EXPR_UNARY_BITWISE_NEGATE:
1906 case EXPR_UNARY_NOT:
1907 return is_constant_expression(expression->unary.value);
1909 case EXPR_UNARY_CAST: {
1910 type_t *const type = skip_typeref(expression->base.type);
1911 if (is_type_scalar(type))
1912 return is_constant_expression(expression->unary.value);
1913 if (!is_type_valid(type))
1914 return EXPR_CLASS_ERROR;
1915 return EXPR_CLASS_VARIABLE;
1918 case EXPR_BINARY_ADD:
1919 case EXPR_BINARY_SUB:
1920 case EXPR_BINARY_MUL:
1921 case EXPR_BINARY_DIV:
1922 case EXPR_BINARY_MOD:
1923 case EXPR_BINARY_EQUAL:
1924 case EXPR_BINARY_NOTEQUAL:
1925 case EXPR_BINARY_LESS:
1926 case EXPR_BINARY_LESSEQUAL:
1927 case EXPR_BINARY_GREATER:
1928 case EXPR_BINARY_GREATEREQUAL:
1929 case EXPR_BINARY_BITWISE_AND:
1930 case EXPR_BINARY_BITWISE_OR:
1931 case EXPR_BINARY_BITWISE_XOR:
1932 case EXPR_BINARY_SHIFTLEFT:
1933 case EXPR_BINARY_SHIFTRIGHT:
1934 case EXPR_BINARY_ISGREATER:
1935 case EXPR_BINARY_ISGREATEREQUAL:
1936 case EXPR_BINARY_ISLESS:
1937 case EXPR_BINARY_ISLESSEQUAL:
1938 case EXPR_BINARY_ISLESSGREATER:
1939 case EXPR_BINARY_ISUNORDERED: {
1940 expression_classification_t const l = is_constant_expression(expression->binary.left);
1941 expression_classification_t const r = is_constant_expression(expression->binary.right);
1942 return l < r ? l : r;
1945 case EXPR_BINARY_LOGICAL_AND: {
1946 expression_t const *const left = expression->binary.left;
1947 expression_classification_t const lclass = is_constant_expression(left);
1948 if (lclass != EXPR_CLASS_CONSTANT)
1950 if (!fold_constant_to_bool(left))
1951 return EXPR_CLASS_CONSTANT;
1952 return is_constant_expression(expression->binary.right);
1955 case EXPR_BINARY_LOGICAL_OR: {
1956 expression_t const *const left = expression->binary.left;
1957 expression_classification_t const lclass = is_constant_expression(left);
1958 if (lclass != EXPR_CLASS_CONSTANT)
1960 if (fold_constant_to_bool(left))
1961 return EXPR_CLASS_CONSTANT;
1962 return is_constant_expression(expression->binary.right);
1965 case EXPR_COMPOUND_LITERAL:
1966 return is_constant_initializer(expression->compound_literal.initializer);
1968 case EXPR_CONDITIONAL: {
1969 expression_t *const condition = expression->conditional.condition;
1970 expression_classification_t const cclass = is_constant_expression(condition);
1971 if (cclass != EXPR_CLASS_CONSTANT)
1974 if (fold_constant_to_bool(condition)) {
1975 expression_t const *const t = expression->conditional.true_expression;
1976 return t == NULL ? EXPR_CLASS_CONSTANT : is_constant_expression(t);
1978 return is_constant_expression(expression->conditional.false_expression);
1983 return EXPR_CLASS_ERROR;
1985 panic("invalid expression found (is constant expression)");
1990 obstack_init(&ast_obstack);
1995 obstack_free(&ast_obstack, NULL);