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_UNKNOWN] = PREC_PRIMARY,
104 [EXPR_INVALID] = PREC_PRIMARY,
105 [EXPR_REFERENCE] = PREC_PRIMARY,
106 [EXPR_REFERENCE_ENUM_VALUE] = PREC_PRIMARY,
107 [EXPR_LITERAL_INTEGER] = PREC_PRIMARY,
108 [EXPR_LITERAL_INTEGER_OCTAL] = PREC_PRIMARY,
109 [EXPR_LITERAL_INTEGER_HEXADECIMAL] = PREC_PRIMARY,
110 [EXPR_LITERAL_FLOATINGPOINT] = PREC_PRIMARY,
111 [EXPR_LITERAL_FLOATINGPOINT_HEXADECIMAL] = PREC_PRIMARY,
112 [EXPR_LITERAL_CHARACTER] = PREC_PRIMARY,
113 [EXPR_LITERAL_WIDE_CHARACTER] = PREC_PRIMARY,
114 [EXPR_LITERAL_MS_NOOP] = PREC_PRIMARY,
115 [EXPR_STRING_LITERAL] = PREC_PRIMARY,
116 [EXPR_WIDE_STRING_LITERAL] = PREC_PRIMARY,
117 [EXPR_COMPOUND_LITERAL] = PREC_UNARY,
118 [EXPR_CALL] = PREC_POSTFIX,
119 [EXPR_CONDITIONAL] = PREC_CONDITIONAL,
120 [EXPR_SELECT] = PREC_POSTFIX,
121 [EXPR_ARRAY_ACCESS] = PREC_POSTFIX,
122 [EXPR_SIZEOF] = PREC_UNARY,
123 [EXPR_CLASSIFY_TYPE] = PREC_UNARY,
124 [EXPR_ALIGNOF] = PREC_UNARY,
126 [EXPR_FUNCNAME] = PREC_PRIMARY,
127 [EXPR_BUILTIN_CONSTANT_P] = PREC_PRIMARY,
128 [EXPR_BUILTIN_TYPES_COMPATIBLE_P] = PREC_PRIMARY,
129 [EXPR_OFFSETOF] = PREC_PRIMARY,
130 [EXPR_VA_START] = PREC_PRIMARY,
131 [EXPR_VA_ARG] = PREC_PRIMARY,
132 [EXPR_VA_COPY] = PREC_PRIMARY,
133 [EXPR_STATEMENT] = PREC_PRIMARY,
134 [EXPR_LABEL_ADDRESS] = PREC_PRIMARY,
136 [EXPR_UNARY_NEGATE] = PREC_UNARY,
137 [EXPR_UNARY_PLUS] = PREC_UNARY,
138 [EXPR_UNARY_BITWISE_NEGATE] = PREC_UNARY,
139 [EXPR_UNARY_NOT] = PREC_UNARY,
140 [EXPR_UNARY_DEREFERENCE] = PREC_UNARY,
141 [EXPR_UNARY_TAKE_ADDRESS] = PREC_UNARY,
142 [EXPR_UNARY_POSTFIX_INCREMENT] = PREC_POSTFIX,
143 [EXPR_UNARY_POSTFIX_DECREMENT] = PREC_POSTFIX,
144 [EXPR_UNARY_PREFIX_INCREMENT] = PREC_UNARY,
145 [EXPR_UNARY_PREFIX_DECREMENT] = PREC_UNARY,
146 [EXPR_UNARY_CAST] = PREC_UNARY,
147 [EXPR_UNARY_ASSUME] = PREC_PRIMARY,
148 [EXPR_UNARY_DELETE] = PREC_UNARY,
149 [EXPR_UNARY_DELETE_ARRAY] = PREC_UNARY,
150 [EXPR_UNARY_THROW] = PREC_ASSIGNMENT,
152 [EXPR_BINARY_ADD] = PREC_ADDITIVE,
153 [EXPR_BINARY_SUB] = PREC_ADDITIVE,
154 [EXPR_BINARY_MUL] = PREC_MULTIPLICATIVE,
155 [EXPR_BINARY_DIV] = PREC_MULTIPLICATIVE,
156 [EXPR_BINARY_MOD] = PREC_MULTIPLICATIVE,
157 [EXPR_BINARY_EQUAL] = PREC_EQUALITY,
158 [EXPR_BINARY_NOTEQUAL] = PREC_EQUALITY,
159 [EXPR_BINARY_LESS] = PREC_RELATIONAL,
160 [EXPR_BINARY_LESSEQUAL] = PREC_RELATIONAL,
161 [EXPR_BINARY_GREATER] = PREC_RELATIONAL,
162 [EXPR_BINARY_GREATEREQUAL] = PREC_RELATIONAL,
163 [EXPR_BINARY_BITWISE_AND] = PREC_AND,
164 [EXPR_BINARY_BITWISE_OR] = PREC_OR,
165 [EXPR_BINARY_BITWISE_XOR] = PREC_XOR,
166 [EXPR_BINARY_LOGICAL_AND] = PREC_LOGICAL_AND,
167 [EXPR_BINARY_LOGICAL_OR] = PREC_LOGICAL_OR,
168 [EXPR_BINARY_SHIFTLEFT] = PREC_SHIFT,
169 [EXPR_BINARY_SHIFTRIGHT] = PREC_SHIFT,
170 [EXPR_BINARY_ASSIGN] = PREC_ASSIGNMENT,
171 [EXPR_BINARY_MUL_ASSIGN] = PREC_ASSIGNMENT,
172 [EXPR_BINARY_DIV_ASSIGN] = PREC_ASSIGNMENT,
173 [EXPR_BINARY_MOD_ASSIGN] = PREC_ASSIGNMENT,
174 [EXPR_BINARY_ADD_ASSIGN] = PREC_ASSIGNMENT,
175 [EXPR_BINARY_SUB_ASSIGN] = PREC_ASSIGNMENT,
176 [EXPR_BINARY_SHIFTLEFT_ASSIGN] = PREC_ASSIGNMENT,
177 [EXPR_BINARY_SHIFTRIGHT_ASSIGN] = PREC_ASSIGNMENT,
178 [EXPR_BINARY_BITWISE_AND_ASSIGN] = PREC_ASSIGNMENT,
179 [EXPR_BINARY_BITWISE_XOR_ASSIGN] = PREC_ASSIGNMENT,
180 [EXPR_BINARY_BITWISE_OR_ASSIGN] = PREC_ASSIGNMENT,
181 [EXPR_BINARY_COMMA] = PREC_EXPRESSION,
183 [EXPR_BINARY_ISGREATER] = PREC_PRIMARY,
184 [EXPR_BINARY_ISGREATEREQUAL] = PREC_PRIMARY,
185 [EXPR_BINARY_ISLESS] = PREC_PRIMARY,
186 [EXPR_BINARY_ISLESSEQUAL] = PREC_PRIMARY,
187 [EXPR_BINARY_ISLESSGREATER] = PREC_PRIMARY,
188 [EXPR_BINARY_ISUNORDERED] = PREC_PRIMARY
190 assert((size_t)kind < lengthof(prec));
191 unsigned res = prec[kind];
193 assert(res != PREC_BOTTOM);
198 * Print a quoted string constant.
200 * @param string the string constant
201 * @param border the border char
202 * @param skip number of chars to skip at the end
204 static void print_quoted_string(const string_t *const string, char border,
208 const char *end = string->begin + string->size - skip;
209 for (const char *c = string->begin; c != end; ++c) {
215 case '\\': print_string("\\\\"); break;
216 case '\a': print_string("\\a"); break;
217 case '\b': print_string("\\b"); break;
218 case '\f': print_string("\\f"); break;
219 case '\n': print_string("\\n"); break;
220 case '\r': print_string("\\r"); break;
221 case '\t': print_string("\\t"); break;
222 case '\v': print_string("\\v"); break;
223 case '\?': print_string("\\?"); break;
225 if (c_mode & _GNUC) {
226 print_string("\\e"); break;
230 if ((unsigned)tc < 0x80 && !isprint(tc)) {
231 print_format("\\%03o", (unsigned)tc);
241 static void print_string_literal(const string_literal_expression_t *literal)
243 if (literal->base.kind == EXPR_WIDE_STRING_LITERAL) {
246 print_quoted_string(&literal->value, '"', 1);
249 static void print_literal(const literal_expression_t *literal)
251 switch (literal->base.kind) {
252 case EXPR_LITERAL_MS_NOOP:
253 print_string("__noop");
255 case EXPR_LITERAL_INTEGER_HEXADECIMAL:
256 case EXPR_LITERAL_FLOATINGPOINT_HEXADECIMAL:
259 case EXPR_LITERAL_BOOLEAN:
260 case EXPR_LITERAL_INTEGER:
261 case EXPR_LITERAL_INTEGER_OCTAL:
262 case EXPR_LITERAL_FLOATINGPOINT:
263 print_stringrep(&literal->value);
264 if (literal->suffix.size > 0)
265 print_stringrep(&literal->suffix);
267 case EXPR_LITERAL_WIDE_CHARACTER:
270 case EXPR_LITERAL_CHARACTER:
271 print_quoted_string(&literal->value, '\'', 0);
276 print_string("INVALID LITERAL KIND");
280 * Prints a predefined symbol.
282 static void print_funcname(const funcname_expression_t *funcname)
285 switch (funcname->kind) {
286 case FUNCNAME_FUNCTION: s = (c_mode & _C99) ? "__func__" : "__FUNCTION__"; break;
287 case FUNCNAME_PRETTY_FUNCTION: s = "__PRETTY_FUNCTION__"; break;
288 case FUNCNAME_FUNCSIG: s = "__FUNCSIG__"; break;
289 case FUNCNAME_FUNCDNAME: s = "__FUNCDNAME__"; break;
294 static void print_compound_literal(
295 const compound_literal_expression_t *expression)
298 print_type(expression->type);
300 print_initializer(expression->initializer);
303 static void print_assignment_expression(const expression_t *const expr)
305 print_expression_prec(expr, PREC_ASSIGNMENT);
309 * Prints a call expression.
311 * @param call the call expression
313 static void print_call_expression(const call_expression_t *call)
315 print_expression_prec(call->function, PREC_POSTFIX);
317 call_argument_t *argument = call->arguments;
319 while (argument != NULL) {
325 print_assignment_expression(argument->expression);
327 argument = argument->next;
333 * Prints a binary expression.
335 * @param binexpr the binary expression
337 static void print_binary_expression(const binary_expression_t *binexpr)
339 unsigned prec = get_expression_precedence(binexpr->base.kind);
340 int r2l = right_to_left(prec);
342 print_expression_prec(binexpr->left, prec + r2l);
344 switch (binexpr->base.kind) {
345 case EXPR_BINARY_COMMA: op = ", "; break;
346 case EXPR_BINARY_ASSIGN: op = " = "; break;
347 case EXPR_BINARY_ADD: op = " + "; break;
348 case EXPR_BINARY_SUB: op = " - "; break;
349 case EXPR_BINARY_MUL: op = " * "; break;
350 case EXPR_BINARY_MOD: op = " % "; break;
351 case EXPR_BINARY_DIV: op = " / "; break;
352 case EXPR_BINARY_BITWISE_OR: op = " | "; break;
353 case EXPR_BINARY_BITWISE_AND: op = " & "; break;
354 case EXPR_BINARY_BITWISE_XOR: op = " ^ "; break;
355 case EXPR_BINARY_LOGICAL_OR: op = " || "; break;
356 case EXPR_BINARY_LOGICAL_AND: op = " && "; break;
357 case EXPR_BINARY_NOTEQUAL: op = " != "; break;
358 case EXPR_BINARY_EQUAL: op = " == "; break;
359 case EXPR_BINARY_LESS: op = " < "; break;
360 case EXPR_BINARY_LESSEQUAL: op = " <= "; break;
361 case EXPR_BINARY_GREATER: op = " > "; break;
362 case EXPR_BINARY_GREATEREQUAL: op = " >= "; break;
363 case EXPR_BINARY_SHIFTLEFT: op = " << "; break;
364 case EXPR_BINARY_SHIFTRIGHT: op = " >> "; break;
366 case EXPR_BINARY_ADD_ASSIGN: op = " += "; break;
367 case EXPR_BINARY_SUB_ASSIGN: op = " -= "; break;
368 case EXPR_BINARY_MUL_ASSIGN: op = " *= "; break;
369 case EXPR_BINARY_MOD_ASSIGN: op = " %= "; break;
370 case EXPR_BINARY_DIV_ASSIGN: op = " /= "; break;
371 case EXPR_BINARY_BITWISE_OR_ASSIGN: op = " |= "; break;
372 case EXPR_BINARY_BITWISE_AND_ASSIGN: op = " &= "; break;
373 case EXPR_BINARY_BITWISE_XOR_ASSIGN: op = " ^= "; break;
374 case EXPR_BINARY_SHIFTLEFT_ASSIGN: op = " <<= "; break;
375 case EXPR_BINARY_SHIFTRIGHT_ASSIGN: op = " >>= "; break;
376 default: panic("invalid binexpression found");
379 print_expression_prec(binexpr->right, prec + 1 - r2l);
383 * Prints an unary expression.
385 * @param unexpr the unary expression
387 static void print_unary_expression(const unary_expression_t *unexpr)
389 unsigned prec = get_expression_precedence(unexpr->base.kind);
390 switch (unexpr->base.kind) {
391 case EXPR_UNARY_NEGATE: print_string("-"); break;
392 case EXPR_UNARY_PLUS: print_string("+"); break;
393 case EXPR_UNARY_NOT: print_string("!"); break;
394 case EXPR_UNARY_BITWISE_NEGATE: print_string("~"); break;
395 case EXPR_UNARY_PREFIX_INCREMENT: print_string("++"); break;
396 case EXPR_UNARY_PREFIX_DECREMENT: print_string("--"); break;
397 case EXPR_UNARY_DEREFERENCE: print_string("*"); break;
398 case EXPR_UNARY_TAKE_ADDRESS: print_string("&"); break;
399 case EXPR_UNARY_DELETE: print_string("delete "); break;
400 case EXPR_UNARY_DELETE_ARRAY: print_string("delete [] "); break;
402 case EXPR_UNARY_POSTFIX_INCREMENT:
403 print_expression_prec(unexpr->value, prec);
406 case EXPR_UNARY_POSTFIX_DECREMENT:
407 print_expression_prec(unexpr->value, prec);
410 case EXPR_UNARY_CAST:
412 print_type(unexpr->base.type);
415 case EXPR_UNARY_ASSUME:
416 print_string("__assume(");
417 print_assignment_expression(unexpr->value);
421 case EXPR_UNARY_THROW:
422 if (unexpr->value == NULL) {
423 print_string("throw");
426 print_string("throw ");
430 panic("invalid unary expression found");
432 print_expression_prec(unexpr->value, prec);
436 * Prints a reference expression.
438 * @param ref the reference expression
440 static void print_reference_expression(const reference_expression_t *ref)
442 print_string(ref->entity->base.symbol->string);
446 * Prints a label address expression.
448 * @param ref the reference expression
450 static void print_label_address_expression(const label_address_expression_t *le)
452 print_format("&&%s", le->label->base.symbol->string);
456 * Prints an array expression.
458 * @param expression the array expression
460 static void print_array_expression(const array_access_expression_t *expression)
462 if (!expression->flipped) {
463 print_expression_prec(expression->array_ref, PREC_POSTFIX);
465 print_expression(expression->index);
468 print_expression_prec(expression->index, PREC_POSTFIX);
470 print_expression(expression->array_ref);
476 * Prints a typeproperty expression (sizeof or __alignof__).
478 * @param expression the type property expression
480 static void print_typeprop_expression(const typeprop_expression_t *expression)
482 if (expression->base.kind == EXPR_SIZEOF) {
483 print_string("sizeof");
485 assert(expression->base.kind == EXPR_ALIGNOF);
486 print_string("__alignof__");
488 if (expression->tp_expression != NULL) {
489 /* PREC_TOP: always print the '()' here, sizeof x is right but unusual */
490 print_expression_prec(expression->tp_expression, PREC_TOP);
493 print_type(expression->type);
499 * Prints a builtin constant expression.
501 * @param expression the builtin constant expression
503 static void print_builtin_constant(const builtin_constant_expression_t *expression)
505 print_string("__builtin_constant_p(");
506 print_assignment_expression(expression->value);
511 * Prints a builtin types compatible expression.
513 * @param expression the builtin types compatible expression
515 static void print_builtin_types_compatible(
516 const builtin_types_compatible_expression_t *expression)
518 print_string("__builtin_types_compatible_p(");
519 print_type(expression->left);
521 print_type(expression->right);
526 * Prints a conditional expression.
528 * @param expression the conditional expression
530 static void print_conditional(const conditional_expression_t *expression)
532 print_expression_prec(expression->condition, PREC_LOGICAL_OR);
533 if (expression->true_expression != NULL) {
535 print_expression_prec(expression->true_expression, PREC_EXPRESSION);
538 print_string(" ?: ");
540 precedence_t prec = c_mode & _CXX ? PREC_ASSIGNMENT : PREC_CONDITIONAL;
541 print_expression_prec(expression->false_expression, prec);
545 * Prints a va_start expression.
547 * @param expression the va_start expression
549 static void print_va_start(const va_start_expression_t *const expression)
551 print_string("__builtin_va_start(");
552 print_assignment_expression(expression->ap);
554 print_string(expression->parameter->base.base.symbol->string);
559 * Prints a va_arg expression.
561 * @param expression the va_arg expression
563 static void print_va_arg(const va_arg_expression_t *expression)
565 print_string("__builtin_va_arg(");
566 print_assignment_expression(expression->ap);
568 print_type(expression->base.type);
573 * Prints a va_copy expression.
575 * @param expression the va_copy expression
577 static void print_va_copy(const va_copy_expression_t *expression)
579 print_string("__builtin_va_copy(");
580 print_assignment_expression(expression->dst);
582 print_assignment_expression(expression->src);
587 * Prints a select expression (. or ->).
589 * @param expression the select expression
591 static void print_select(const select_expression_t *expression)
593 print_expression_prec(expression->compound, PREC_POSTFIX);
594 if (is_type_pointer(skip_typeref(expression->compound->base.type))) {
599 print_string(expression->compound_entry->base.symbol->string);
603 * Prints a type classify expression.
605 * @param expr the type classify expression
607 static void print_classify_type_expression(
608 const classify_type_expression_t *const expr)
610 print_string("__builtin_classify_type(");
611 print_assignment_expression(expr->type_expression);
616 * Prints a designator.
618 * @param designator the designator
620 static void print_designator(const designator_t *designator)
622 for ( ; designator != NULL; designator = designator->next) {
623 if (designator->symbol == NULL) {
625 print_expression(designator->array_index);
629 print_string(designator->symbol->string);
635 * Prints an offsetof expression.
637 * @param expression the offset expression
639 static void print_offsetof_expression(const offsetof_expression_t *expression)
641 print_string("__builtin_offsetof(");
642 print_type(expression->type);
644 print_designator(expression->designator);
649 * Prints a statement expression.
651 * @param expression the statement expression
653 static void print_statement_expression(const statement_expression_t *expression)
656 print_statement(expression->statement);
661 * Prints an expression with parenthesis if needed.
663 * @param expression the expression to print
664 * @param top_prec the precedence of the user of this expression.
666 static void print_expression_prec(const expression_t *expression, unsigned top_prec)
668 if (expression->kind == EXPR_UNARY_CAST
669 && expression->base.implicit && !print_implicit_casts) {
670 expression = expression->unary.value;
674 expression->base.parenthesized ||
675 (print_parenthesis && top_prec != PREC_BOTTOM) ||
676 top_prec > get_expression_precedence(expression->base.kind);
680 switch (expression->kind) {
683 print_string("$invalid expression$");
685 case EXPR_WIDE_STRING_LITERAL:
686 case EXPR_STRING_LITERAL:
687 print_string_literal(&expression->string_literal);
690 print_literal(&expression->literal);
693 print_funcname(&expression->funcname);
695 case EXPR_COMPOUND_LITERAL:
696 print_compound_literal(&expression->compound_literal);
699 print_call_expression(&expression->call);
702 print_binary_expression(&expression->binary);
705 case EXPR_REFERENCE_ENUM_VALUE:
706 print_reference_expression(&expression->reference);
708 case EXPR_ARRAY_ACCESS:
709 print_array_expression(&expression->array_access);
711 case EXPR_LABEL_ADDRESS:
712 print_label_address_expression(&expression->label_address);
715 print_unary_expression(&expression->unary);
719 print_typeprop_expression(&expression->typeprop);
721 case EXPR_BUILTIN_CONSTANT_P:
722 print_builtin_constant(&expression->builtin_constant);
724 case EXPR_BUILTIN_TYPES_COMPATIBLE_P:
725 print_builtin_types_compatible(&expression->builtin_types_compatible);
727 case EXPR_CONDITIONAL:
728 print_conditional(&expression->conditional);
731 print_va_start(&expression->va_starte);
734 print_va_arg(&expression->va_arge);
737 print_va_copy(&expression->va_copye);
740 print_select(&expression->select);
742 case EXPR_CLASSIFY_TYPE:
743 print_classify_type_expression(&expression->classify_type);
746 print_offsetof_expression(&expression->offsetofe);
749 print_statement_expression(&expression->statement);
755 print_format("some expression of type %d", (int)expression->kind);
764 * Print an compound statement.
766 * @param block the compound statement
768 static void print_compound_statement(const compound_statement_t *block)
773 statement_t *statement = block->statements;
774 while (statement != NULL) {
775 if (statement->base.kind == STATEMENT_CASE_LABEL)
777 if (statement->kind != STATEMENT_LABEL)
779 print_statement(statement);
781 statement = statement->base.next;
785 print_string(block->stmt_expr ? "}" : "}\n");
789 * Print a return statement.
791 * @param statement the return statement
793 static void print_return_statement(const return_statement_t *statement)
795 expression_t const *const val = statement->value;
797 print_string("return ");
798 print_expression(val);
801 print_string("return;\n");
806 * Print an expression statement.
808 * @param statement the expression statement
810 static void print_expression_statement(const expression_statement_t *statement)
812 print_expression(statement->expression);
817 * Print a goto statement.
819 * @param statement the goto statement
821 static void print_goto_statement(const goto_statement_t *statement)
823 print_string("goto ");
824 if (statement->expression != NULL) {
826 print_expression(statement->expression);
828 print_string(statement->label->base.symbol->string);
834 * Print a label statement.
836 * @param statement the label statement
838 static void print_label_statement(const label_statement_t *statement)
840 print_format("%s:\n", statement->label->base.symbol->string);
842 print_statement(statement->statement);
846 * Print an if statement.
848 * @param statement the if statement
850 static void print_if_statement(const if_statement_t *statement)
852 print_string("if (");
853 print_expression(statement->condition);
855 print_statement(statement->true_statement);
857 if (statement->false_statement != NULL) {
859 print_string("else ");
860 print_statement(statement->false_statement);
865 * Print a switch statement.
867 * @param statement the switch statement
869 static void print_switch_statement(const switch_statement_t *statement)
871 print_string("switch (");
872 print_expression(statement->expression);
874 print_statement(statement->body);
878 * Print a case label (including the default label).
880 * @param statement the case label statement
882 static void print_case_label(const case_label_statement_t *statement)
884 if (statement->expression == NULL) {
885 print_string("default:\n");
887 print_string("case ");
888 print_expression(statement->expression);
889 if (statement->end_range != NULL) {
890 print_string(" ... ");
891 print_expression(statement->end_range);
896 if (statement->statement != NULL) {
897 if (statement->statement->base.kind == STATEMENT_CASE_LABEL) {
901 print_statement(statement->statement);
905 static void print_typedef(const entity_t *entity)
907 print_string("typedef ");
908 print_type_ext(entity->typedefe.type, entity->base.symbol, NULL);
913 * returns true if the entity is a compiler generated one and has no real
914 * correspondenc in the source file
916 static bool is_generated_entity(const entity_t *entity)
918 if (entity->kind == ENTITY_TYPEDEF)
919 return entity->typedefe.builtin;
921 if (is_declaration(entity))
922 return entity->declaration.implicit;
928 * Print a declaration statement.
930 * @param statement the statement
932 static void print_declaration_statement(
933 const declaration_statement_t *statement)
936 entity_t *entity = statement->declarations_begin;
937 if (entity == NULL) {
938 print_string("/* empty declaration statement */\n");
942 entity_t *const end = statement->declarations_end->base.next;
943 for (; entity != end; entity = entity->base.next) {
944 if (entity->kind == ENTITY_ENUM_VALUE)
946 if (is_generated_entity(entity))
955 print_entity(entity);
961 * Print a while statement.
963 * @param statement the statement
965 static void print_while_statement(const while_statement_t *statement)
967 print_string("while (");
968 print_expression(statement->condition);
970 print_statement(statement->body);
974 * Print a do-while statement.
976 * @param statement the statement
978 static void print_do_while_statement(const do_while_statement_t *statement)
981 print_statement(statement->body);
983 print_string("while (");
984 print_expression(statement->condition);
985 print_string(");\n");
989 * Print a for statement.
991 * @param statement the statement
993 static void print_for_statement(const for_statement_t *statement)
995 print_string("for (");
996 if (statement->initialisation != NULL) {
997 print_expression(statement->initialisation);
1000 entity_t const *entity = statement->scope.entities;
1001 for (; entity != NULL; entity = entity->base.next) {
1002 if (is_generated_entity(entity))
1004 /* FIXME display of multiple declarations is wrong */
1005 print_declaration(entity);
1008 if (statement->condition != NULL) {
1010 print_expression(statement->condition);
1013 if (statement->step != NULL) {
1015 print_expression(statement->step);
1018 print_statement(statement->body);
1022 * Print assembler arguments.
1024 * @param arguments the arguments
1026 static void print_asm_arguments(asm_argument_t *arguments)
1028 asm_argument_t *argument = arguments;
1029 for (; argument != NULL; argument = argument->next) {
1030 if (argument != arguments)
1033 if (argument->symbol) {
1034 print_format("[%s] ", argument->symbol->string);
1036 print_quoted_string(&argument->constraints, '"', 1);
1038 print_expression(argument->expression);
1044 * Print assembler clobbers.
1046 * @param clobbers the clobbers
1048 static void print_asm_clobbers(asm_clobber_t *clobbers)
1050 asm_clobber_t *clobber = clobbers;
1051 for (; clobber != NULL; clobber = clobber->next) {
1052 if (clobber != clobbers)
1055 print_quoted_string(&clobber->clobber, '"', 1);
1060 * Print an assembler statement.
1062 * @param statement the statement
1064 static void print_asm_statement(const asm_statement_t *statement)
1066 print_string("asm ");
1067 if (statement->is_volatile) {
1068 print_string("volatile ");
1071 print_quoted_string(&statement->asm_text, '"', 1);
1072 if (statement->outputs == NULL &&
1073 statement->inputs == NULL &&
1074 statement->clobbers == NULL)
1075 goto end_of_print_asm_statement;
1077 print_string(" : ");
1078 print_asm_arguments(statement->outputs);
1079 if (statement->inputs == NULL && statement->clobbers == NULL)
1080 goto end_of_print_asm_statement;
1082 print_string(" : ");
1083 print_asm_arguments(statement->inputs);
1084 if (statement->clobbers == NULL)
1085 goto end_of_print_asm_statement;
1087 print_string(" : ");
1088 print_asm_clobbers(statement->clobbers);
1090 end_of_print_asm_statement:
1091 print_string(");\n");
1095 * Print a microsoft __try statement.
1097 * @param statement the statement
1099 static void print_ms_try_statement(const ms_try_statement_t *statement)
1101 print_string("__try ");
1102 print_statement(statement->try_statement);
1104 if (statement->except_expression != NULL) {
1105 print_string("__except(");
1106 print_expression(statement->except_expression);
1109 print_string("__finally ");
1111 print_statement(statement->final_statement);
1115 * Print a microsoft __leave statement.
1117 * @param statement the statement
1119 static void print_leave_statement(const leave_statement_t *statement)
1122 print_string("__leave;\n");
1126 * Print a statement.
1128 * @param statement the statement
1130 void print_statement(const statement_t *statement)
1132 switch (statement->kind) {
1133 case STATEMENT_EMPTY:
1134 print_string(";\n");
1136 case STATEMENT_COMPOUND:
1137 print_compound_statement(&statement->compound);
1139 case STATEMENT_RETURN:
1140 print_return_statement(&statement->returns);
1142 case STATEMENT_EXPRESSION:
1143 print_expression_statement(&statement->expression);
1145 case STATEMENT_LABEL:
1146 print_label_statement(&statement->label);
1148 case STATEMENT_GOTO:
1149 print_goto_statement(&statement->gotos);
1151 case STATEMENT_CONTINUE:
1152 print_string("continue;\n");
1154 case STATEMENT_BREAK:
1155 print_string("break;\n");
1158 print_if_statement(&statement->ifs);
1160 case STATEMENT_SWITCH:
1161 print_switch_statement(&statement->switchs);
1163 case STATEMENT_CASE_LABEL:
1164 print_case_label(&statement->case_label);
1166 case STATEMENT_DECLARATION:
1167 print_declaration_statement(&statement->declaration);
1169 case STATEMENT_WHILE:
1170 print_while_statement(&statement->whiles);
1172 case STATEMENT_DO_WHILE:
1173 print_do_while_statement(&statement->do_while);
1176 print_for_statement(&statement->fors);
1179 print_asm_statement(&statement->asms);
1181 case STATEMENT_MS_TRY:
1182 print_ms_try_statement(&statement->ms_try);
1184 case STATEMENT_LEAVE:
1185 print_leave_statement(&statement->leave);
1187 case STATEMENT_INVALID:
1188 print_string("$invalid statement$\n");
1194 * Print a storage class.
1196 * @param storage_class the storage class
1198 static void print_storage_class(storage_class_tag_t storage_class)
1200 switch (storage_class) {
1201 case STORAGE_CLASS_NONE: return;
1202 case STORAGE_CLASS_TYPEDEF: print_string("typedef "); return;
1203 case STORAGE_CLASS_EXTERN: print_string("extern "); return;
1204 case STORAGE_CLASS_STATIC: print_string("static "); return;
1205 case STORAGE_CLASS_AUTO: print_string("auto "); return;
1206 case STORAGE_CLASS_REGISTER: print_string("register "); return;
1208 panic("invalid storage class");
1212 * Print an initializer.
1214 * @param initializer the initializer
1216 void print_initializer(const initializer_t *initializer)
1218 if (initializer == NULL) {
1223 switch (initializer->kind) {
1224 case INITIALIZER_VALUE: {
1225 const initializer_value_t *value = &initializer->value;
1226 print_assignment_expression(value->value);
1229 case INITIALIZER_LIST: {
1230 assert(initializer->kind == INITIALIZER_LIST);
1232 const initializer_list_t *list = &initializer->list;
1234 for (size_t i = 0 ; i < list->len; ++i) {
1235 const initializer_t *sub_init = list->initializers[i];
1236 print_initializer(list->initializers[i]);
1237 if (i < list->len-1) {
1238 if (sub_init == NULL || sub_init->kind != INITIALIZER_DESIGNATOR)
1245 case INITIALIZER_STRING:
1246 print_quoted_string(&initializer->string.string, '"', 1);
1248 case INITIALIZER_WIDE_STRING:
1249 print_quoted_string(&initializer->string.string, '"', 1);
1251 case INITIALIZER_DESIGNATOR:
1252 print_designator(initializer->designator.designator);
1253 print_string(" = ");
1257 panic("invalid initializer kind found");
1262 * Print microsoft extended declaration modifiers.
1264 static void print_ms_modifiers(const declaration_t *declaration)
1266 if ((c_mode & _MS) == 0)
1269 decl_modifiers_t modifiers = declaration->modifiers;
1271 bool ds_shown = false;
1272 const char *next = "(";
1274 if (declaration->base.kind == ENTITY_VARIABLE) {
1275 variable_t *variable = (variable_t*)declaration;
1276 if (variable->alignment != 0
1277 || variable->get_property_sym != NULL
1278 || variable->put_property_sym != NULL) {
1280 print_string("__declspec");
1284 if (variable->alignment != 0) {
1285 print_string(next); next = ", "; print_format("align(%u)", variable->alignment);
1287 if (variable->get_property_sym != NULL
1288 || variable->put_property_sym != NULL) {
1290 print_string(next); next = ", "; print_string("property(");
1291 if (variable->get_property_sym != NULL) {
1292 print_format("get=%s", variable->get_property_sym->string);
1295 if (variable->put_property_sym != NULL)
1296 print_format("%sput=%s", comma, variable->put_property_sym->string);
1302 /* DM_FORCEINLINE handled outside. */
1303 if ((modifiers & ~DM_FORCEINLINE) != 0) {
1305 print_string("__declspec");
1308 if (modifiers & DM_DLLIMPORT) {
1309 print_string(next); next = ", "; print_string("dllimport");
1311 if (modifiers & DM_DLLEXPORT) {
1312 print_string(next); next = ", "; print_string("dllexport");
1314 if (modifiers & DM_THREAD) {
1315 print_string(next); next = ", "; print_string("thread");
1317 if (modifiers & DM_NAKED) {
1318 print_string(next); next = ", "; print_string("naked");
1320 if (modifiers & DM_THREAD) {
1321 print_string(next); next = ", "; print_string("thread");
1323 if (modifiers & DM_SELECTANY) {
1324 print_string(next); next = ", "; print_string("selectany");
1326 if (modifiers & DM_NOTHROW) {
1327 print_string(next); next = ", "; print_string("nothrow");
1329 if (modifiers & DM_NORETURN) {
1330 print_string(next); next = ", "; print_string("noreturn");
1332 if (modifiers & DM_NOINLINE) {
1333 print_string(next); next = ", "; print_string("noinline");
1335 if (modifiers & DM_DEPRECATED) {
1336 print_string(next); next = ", "; print_string("deprecated");
1337 if (declaration->deprecated_string != NULL)
1338 print_format("(\"%s\")",
1339 declaration->deprecated_string);
1341 if (modifiers & DM_RESTRICT) {
1342 print_string(next); next = ", "; print_string("restrict");
1344 if (modifiers & DM_NOALIAS) {
1345 print_string(next); next = ", "; print_string("noalias");
1354 static void print_scope(const scope_t *scope)
1356 const entity_t *entity = scope->entities;
1357 for ( ; entity != NULL; entity = entity->base.next) {
1359 print_entity(entity);
1364 static void print_namespace(const namespace_t *namespace)
1366 print_string("namespace ");
1367 if (namespace->base.symbol != NULL) {
1368 print_string(namespace->base.symbol->string);
1372 print_string("{\n");
1375 print_scope(&namespace->members);
1379 print_string("}\n");
1383 * Print a variable or function declaration
1385 void print_declaration(const entity_t *entity)
1387 assert(is_declaration(entity));
1388 const declaration_t *declaration = &entity->declaration;
1390 print_storage_class((storage_class_tag_t)declaration->declared_storage_class);
1391 if (entity->kind == ENTITY_FUNCTION) {
1392 function_t *function = (function_t*)declaration;
1393 if (function->is_inline) {
1394 if (declaration->modifiers & DM_FORCEINLINE) {
1395 print_string("__forceinline ");
1396 } else if (declaration->modifiers & DM_MICROSOFT_INLINE) {
1397 print_string("__inline ");
1399 print_string("inline ");
1403 //print_ms_modifiers(declaration);
1404 switch (entity->kind) {
1405 case ENTITY_FUNCTION:
1406 print_type_ext(entity->declaration.type, entity->base.symbol,
1407 &entity->function.parameters);
1409 if (entity->function.statement != NULL) {
1412 print_statement(entity->function.statement);
1417 case ENTITY_VARIABLE:
1418 if (entity->variable.thread_local)
1419 print_string("__thread ");
1420 print_type_ext(declaration->type, declaration->base.symbol, NULL);
1421 if (entity->variable.initializer != NULL) {
1422 print_string(" = ");
1423 print_initializer(entity->variable.initializer);
1428 print_type_ext(declaration->type, declaration->base.symbol, NULL);
1435 * Prints an expression.
1437 * @param expression the expression
1439 void print_expression(const expression_t *expression)
1441 print_expression_prec(expression, PREC_BOTTOM);
1445 * Print a declaration.
1447 * @param declaration the declaration
1449 void print_entity(const entity_t *entity)
1451 if (entity->base.namespc != NAMESPACE_NORMAL && entity->base.symbol == NULL)
1454 switch ((entity_kind_tag_t)entity->kind) {
1455 case ENTITY_VARIABLE:
1456 case ENTITY_PARAMETER:
1457 case ENTITY_COMPOUND_MEMBER:
1458 case ENTITY_FUNCTION:
1459 print_declaration(entity);
1461 case ENTITY_TYPEDEF:
1462 print_typedef(entity);
1466 print_string("class ");
1467 print_string(entity->base.symbol->string);
1468 print_string("; /* TODO */\n");
1471 print_string("struct ");
1472 goto print_compound;
1474 print_string("union ");
1476 print_string(entity->base.symbol->string);
1477 if (entity->compound.complete) {
1479 print_compound_definition(&entity->compound);
1484 print_string("enum ");
1485 print_string(entity->base.symbol->string);
1487 print_enum_definition(&entity->enume);
1490 case ENTITY_NAMESPACE:
1491 print_namespace(&entity->namespacee);
1493 case ENTITY_LOCAL_LABEL:
1494 print_string("__label__ ");
1495 print_string(entity->base.symbol->string);
1499 case ENTITY_ENUM_VALUE:
1500 panic("print_entity used on unexpected entity type");
1501 case ENTITY_INVALID:
1504 panic("Invalid entity type encountered");
1508 * Print the AST of a translation unit.
1510 * @param unit the translation unit
1512 void print_ast(const translation_unit_t *unit)
1514 entity_t *entity = unit->scope.entities;
1515 for ( ; entity != NULL; entity = entity->base.next) {
1516 if (entity->kind == ENTITY_ENUM_VALUE)
1518 if (entity->base.namespc != NAMESPACE_NORMAL
1519 && entity->base.symbol == NULL)
1521 if (is_generated_entity(entity))
1525 print_entity(entity);
1530 expression_classification_t is_constant_initializer(const initializer_t *initializer)
1532 switch (initializer->kind) {
1533 case INITIALIZER_STRING:
1534 case INITIALIZER_WIDE_STRING:
1535 case INITIALIZER_DESIGNATOR:
1536 return EXPR_CLASS_CONSTANT;
1538 case INITIALIZER_VALUE:
1539 return is_constant_expression(initializer->value.value);
1541 case INITIALIZER_LIST: {
1542 expression_classification_t all = EXPR_CLASS_CONSTANT;
1543 for (size_t i = 0; i < initializer->list.len; ++i) {
1544 initializer_t *sub_initializer = initializer->list.initializers[i];
1545 expression_classification_t const cur = is_constant_initializer(sub_initializer);
1553 panic("invalid initializer kind found");
1557 * Checks if an expression references an object with a constant/known location
1558 * to the linker. Example:
1559 * - "x", "*&x" with x being a global variable. The value of x need not be
1560 * constant but the address of x is.
1561 * - "a.b.c" when a has a constant/known location to the linker
1563 static expression_classification_t is_object_with_linker_constant_address(
1564 const expression_t *expression)
1566 switch (expression->kind) {
1567 case EXPR_UNARY_DEREFERENCE:
1568 return is_linker_constant(expression->unary.value);
1571 type_t *base_type = skip_typeref(expression->select.compound->base.type);
1572 if (is_type_pointer(base_type)) {
1574 return is_linker_constant(expression->select.compound);
1576 return is_object_with_linker_constant_address(expression->select.compound);
1580 case EXPR_ARRAY_ACCESS: {
1581 expression_classification_t const ref = is_linker_constant(expression->array_access.array_ref);
1582 expression_classification_t const idx = is_constant_expression(expression->array_access.index);
1583 return ref < idx ? ref : idx;
1586 case EXPR_REFERENCE: {
1587 entity_t *entity = expression->reference.entity;
1588 if (!is_declaration(entity))
1589 return EXPR_CLASS_VARIABLE;
1591 switch ((storage_class_tag_t)entity->declaration.storage_class) {
1592 case STORAGE_CLASS_NONE:
1593 case STORAGE_CLASS_EXTERN:
1594 case STORAGE_CLASS_STATIC:
1596 entity->kind != ENTITY_VARIABLE ||
1597 !entity->variable.thread_local ? EXPR_CLASS_CONSTANT :
1598 EXPR_CLASS_VARIABLE;
1600 case STORAGE_CLASS_REGISTER:
1601 case STORAGE_CLASS_TYPEDEF:
1602 case STORAGE_CLASS_AUTO:
1605 return EXPR_CLASS_VARIABLE;
1609 return EXPR_CLASS_ERROR;
1612 return EXPR_CLASS_VARIABLE;
1616 expression_classification_t is_linker_constant(const expression_t *expression)
1618 switch (expression->kind) {
1619 case EXPR_STRING_LITERAL:
1620 case EXPR_WIDE_STRING_LITERAL:
1622 case EXPR_LABEL_ADDRESS:
1623 return EXPR_CLASS_CONSTANT;
1625 case EXPR_UNARY_TAKE_ADDRESS:
1626 return is_object_with_linker_constant_address(expression->unary.value);
1628 case EXPR_UNARY_DEREFERENCE: {
1630 = revert_automatic_type_conversion(expression->unary.value);
1631 /* dereferencing a function is a NOP */
1632 if (is_type_function(real_type)) {
1633 return is_linker_constant(expression->unary.value);
1638 case EXPR_UNARY_CAST: {
1639 type_t *dest = skip_typeref(expression->base.type);
1640 if (!is_type_pointer(dest) && (
1641 dest->kind != TYPE_ATOMIC ||
1642 !(get_atomic_type_flags(dest->atomic.akind) & ATOMIC_TYPE_FLAG_INTEGER) ||
1643 get_atomic_type_size(dest->atomic.akind) < get_atomic_type_size(get_intptr_kind())
1645 return EXPR_CLASS_VARIABLE;
1647 expression_classification_t const expr = is_constant_expression(expression->unary.value);
1648 expression_classification_t const addr = is_linker_constant(expression->unary.value);
1649 return expr > addr ? expr : addr;
1652 case EXPR_BINARY_ADD:
1653 case EXPR_BINARY_SUB: {
1654 expression_t *const left = expression->binary.left;
1655 expression_t *const right = expression->binary.right;
1656 type_t *const ltype = skip_typeref(left->base.type);
1657 type_t *const rtype = skip_typeref(right->base.type);
1659 if (is_type_pointer(ltype)) {
1660 expression_classification_t const l = is_linker_constant(left);
1661 expression_classification_t const r = is_constant_expression(right);
1662 return l < r ? l : r;
1663 } else if (is_type_pointer(rtype)) {
1664 expression_classification_t const l = is_constant_expression(left);
1665 expression_classification_t const r = is_linker_constant(right);
1666 return l < r ? l : r;
1667 } else if (!is_type_valid(ltype) || !is_type_valid(rtype)) {
1668 return EXPR_CLASS_ERROR;
1670 return EXPR_CLASS_VARIABLE;
1674 case EXPR_REFERENCE: {
1675 entity_t *entity = expression->reference.entity;
1676 if (!is_declaration(entity))
1677 return EXPR_CLASS_VARIABLE;
1679 type_t *type = skip_typeref(entity->declaration.type);
1680 if (is_type_function(type))
1681 return EXPR_CLASS_CONSTANT;
1682 if (is_type_array(type)) {
1683 return is_object_with_linker_constant_address(expression);
1685 /* Prevent stray errors */
1686 if (!is_type_valid(type))
1687 return EXPR_CLASS_ERROR;
1688 return EXPR_CLASS_VARIABLE;
1691 case EXPR_ARRAY_ACCESS: {
1692 type_t *const type =
1693 skip_typeref(revert_automatic_type_conversion(expression));
1694 if (!is_type_array(type))
1695 return EXPR_CLASS_VARIABLE;
1696 expression_classification_t const ref = is_linker_constant(expression->array_access.array_ref);
1697 expression_classification_t const idx = is_constant_expression(expression->array_access.index);
1698 return ref < idx ? ref : idx;
1701 case EXPR_CONDITIONAL: {
1702 expression_t *const c = expression->conditional.condition;
1703 expression_classification_t const cclass = is_constant_expression(c);
1704 if (cclass != EXPR_CLASS_CONSTANT)
1707 if (fold_constant_to_bool(c)) {
1708 expression_t const *const t = expression->conditional.true_expression;
1709 return is_linker_constant(t != NULL ? t : c);
1711 return is_linker_constant(expression->conditional.false_expression);
1716 entity_t *entity = expression->select.compound_entry;
1717 if (!is_declaration(entity))
1718 return EXPR_CLASS_VARIABLE;
1719 type_t *type = skip_typeref(entity->declaration.type);
1720 if (is_type_array(type)) {
1721 /* arrays automatically convert to their address */
1722 expression_t *compound = expression->select.compound;
1723 type_t *base_type = skip_typeref(compound->base.type);
1724 if (is_type_pointer(base_type)) {
1726 return is_linker_constant(compound);
1728 return is_object_with_linker_constant_address(compound);
1731 return EXPR_CLASS_VARIABLE;
1735 return EXPR_CLASS_ERROR;
1738 return EXPR_CLASS_VARIABLE;
1743 * Check if the given expression is a call to a builtin function
1744 * returning a constant result.
1746 static expression_classification_t is_builtin_const_call(const expression_t *expression)
1748 expression_t *function = expression->call.function;
1749 if (function->kind != EXPR_REFERENCE)
1750 return EXPR_CLASS_VARIABLE;
1751 reference_expression_t *ref = &function->reference;
1752 if (ref->entity->kind != ENTITY_FUNCTION)
1753 return EXPR_CLASS_VARIABLE;
1755 switch (ref->entity->function.btk) {
1756 case bk_gnu_builtin_huge_val:
1757 case bk_gnu_builtin_huge_valf:
1758 case bk_gnu_builtin_huge_vall:
1759 case bk_gnu_builtin_inf:
1760 case bk_gnu_builtin_inff:
1761 case bk_gnu_builtin_infl:
1762 case bk_gnu_builtin_nan:
1763 case bk_gnu_builtin_nanf:
1764 case bk_gnu_builtin_nanl:
1765 return EXPR_CLASS_CONSTANT;
1767 return EXPR_CLASS_VARIABLE;
1772 static expression_classification_t is_constant_pointer(const expression_t *expression)
1774 expression_classification_t const expr_class = is_constant_expression(expression);
1775 if (expr_class != EXPR_CLASS_VARIABLE)
1778 switch (expression->kind) {
1779 case EXPR_UNARY_CAST:
1780 return is_constant_pointer(expression->unary.value);
1782 return EXPR_CLASS_VARIABLE;
1786 static expression_classification_t is_object_with_constant_address(const expression_t *expression)
1788 switch (expression->kind) {
1790 expression_t *compound = expression->select.compound;
1791 type_t *compound_type = compound->base.type;
1792 compound_type = skip_typeref(compound_type);
1793 if (is_type_pointer(compound_type)) {
1794 return is_constant_pointer(compound);
1796 return is_object_with_constant_address(compound);
1800 case EXPR_ARRAY_ACCESS: {
1801 array_access_expression_t const* const array_access =
1802 &expression->array_access;
1803 expression_classification_t const idx_class = is_constant_expression(array_access->index);
1804 if (idx_class != EXPR_CLASS_CONSTANT)
1806 expression_classification_t const ref_addr = is_object_with_constant_address(array_access->array_ref);
1807 expression_classification_t const ref_ptr = is_constant_pointer(array_access->array_ref);
1808 return ref_addr > ref_ptr ? ref_addr : ref_ptr;
1811 case EXPR_UNARY_DEREFERENCE:
1812 return is_constant_pointer(expression->unary.value);
1815 return EXPR_CLASS_ERROR;
1818 return EXPR_CLASS_VARIABLE;
1822 expression_classification_t is_constant_expression(const expression_t *expression)
1824 switch (expression->kind) {
1826 case EXPR_CLASSIFY_TYPE:
1829 case EXPR_BUILTIN_CONSTANT_P:
1830 case EXPR_BUILTIN_TYPES_COMPATIBLE_P:
1831 case EXPR_REFERENCE_ENUM_VALUE:
1832 return EXPR_CLASS_CONSTANT;
1835 type_t *const type = skip_typeref(expression->typeprop.type);
1837 !is_type_array(type) || !type->array.is_vla ? EXPR_CLASS_CONSTANT :
1838 EXPR_CLASS_VARIABLE;
1841 case EXPR_STRING_LITERAL:
1842 case EXPR_WIDE_STRING_LITERAL:
1844 case EXPR_LABEL_ADDRESS:
1849 case EXPR_STATEMENT:
1850 case EXPR_UNARY_POSTFIX_INCREMENT:
1851 case EXPR_UNARY_POSTFIX_DECREMENT:
1852 case EXPR_UNARY_PREFIX_INCREMENT:
1853 case EXPR_UNARY_PREFIX_DECREMENT:
1854 case EXPR_UNARY_ASSUME: /* has VOID type */
1855 case EXPR_UNARY_DEREFERENCE:
1856 case EXPR_UNARY_DELETE:
1857 case EXPR_UNARY_DELETE_ARRAY:
1858 case EXPR_UNARY_THROW:
1859 case EXPR_BINARY_ASSIGN:
1860 case EXPR_BINARY_MUL_ASSIGN:
1861 case EXPR_BINARY_DIV_ASSIGN:
1862 case EXPR_BINARY_MOD_ASSIGN:
1863 case EXPR_BINARY_ADD_ASSIGN:
1864 case EXPR_BINARY_SUB_ASSIGN:
1865 case EXPR_BINARY_SHIFTLEFT_ASSIGN:
1866 case EXPR_BINARY_SHIFTRIGHT_ASSIGN:
1867 case EXPR_BINARY_BITWISE_AND_ASSIGN:
1868 case EXPR_BINARY_BITWISE_XOR_ASSIGN:
1869 case EXPR_BINARY_BITWISE_OR_ASSIGN:
1870 case EXPR_BINARY_COMMA:
1871 case EXPR_ARRAY_ACCESS:
1872 return EXPR_CLASS_VARIABLE;
1874 case EXPR_REFERENCE: {
1875 type_t *const type = skip_typeref(expression->base.type);
1876 return is_type_valid(type) ? EXPR_CLASS_VARIABLE : EXPR_CLASS_ERROR;
1879 case EXPR_UNARY_TAKE_ADDRESS:
1880 return is_object_with_constant_address(expression->unary.value);
1883 return is_builtin_const_call(expression);
1885 case EXPR_UNARY_NEGATE:
1886 case EXPR_UNARY_PLUS:
1887 case EXPR_UNARY_BITWISE_NEGATE:
1888 case EXPR_UNARY_NOT:
1889 return is_constant_expression(expression->unary.value);
1891 case EXPR_UNARY_CAST: {
1892 type_t *const type = skip_typeref(expression->base.type);
1893 if (is_type_scalar(type))
1894 return is_constant_expression(expression->unary.value);
1895 if (!is_type_valid(type))
1896 return EXPR_CLASS_ERROR;
1897 return EXPR_CLASS_VARIABLE;
1900 case EXPR_BINARY_ADD:
1901 case EXPR_BINARY_SUB:
1902 case EXPR_BINARY_MUL:
1903 case EXPR_BINARY_DIV:
1904 case EXPR_BINARY_MOD:
1905 case EXPR_BINARY_EQUAL:
1906 case EXPR_BINARY_NOTEQUAL:
1907 case EXPR_BINARY_LESS:
1908 case EXPR_BINARY_LESSEQUAL:
1909 case EXPR_BINARY_GREATER:
1910 case EXPR_BINARY_GREATEREQUAL:
1911 case EXPR_BINARY_BITWISE_AND:
1912 case EXPR_BINARY_BITWISE_OR:
1913 case EXPR_BINARY_BITWISE_XOR:
1914 case EXPR_BINARY_SHIFTLEFT:
1915 case EXPR_BINARY_SHIFTRIGHT:
1916 case EXPR_BINARY_ISGREATER:
1917 case EXPR_BINARY_ISGREATEREQUAL:
1918 case EXPR_BINARY_ISLESS:
1919 case EXPR_BINARY_ISLESSEQUAL:
1920 case EXPR_BINARY_ISLESSGREATER:
1921 case EXPR_BINARY_ISUNORDERED: {
1922 expression_classification_t const l = is_constant_expression(expression->binary.left);
1923 expression_classification_t const r = is_constant_expression(expression->binary.right);
1924 return l < r ? l : r;
1927 case EXPR_BINARY_LOGICAL_AND: {
1928 expression_t const *const left = expression->binary.left;
1929 expression_classification_t const lclass = is_constant_expression(left);
1930 if (lclass != EXPR_CLASS_CONSTANT)
1932 if (!fold_constant_to_bool(left))
1933 return EXPR_CLASS_CONSTANT;
1934 return is_constant_expression(expression->binary.right);
1937 case EXPR_BINARY_LOGICAL_OR: {
1938 expression_t const *const left = expression->binary.left;
1939 expression_classification_t const lclass = is_constant_expression(left);
1940 if (lclass != EXPR_CLASS_CONSTANT)
1942 if (fold_constant_to_bool(left))
1943 return EXPR_CLASS_CONSTANT;
1944 return is_constant_expression(expression->binary.right);
1947 case EXPR_COMPOUND_LITERAL:
1948 return is_constant_initializer(expression->compound_literal.initializer);
1950 case EXPR_CONDITIONAL: {
1951 expression_t *const condition = expression->conditional.condition;
1952 expression_classification_t const cclass = is_constant_expression(condition);
1953 if (cclass != EXPR_CLASS_CONSTANT)
1956 if (fold_constant_to_bool(condition)) {
1957 expression_t const *const t = expression->conditional.true_expression;
1958 return t == NULL ? EXPR_CLASS_CONSTANT : is_constant_expression(t);
1960 return is_constant_expression(expression->conditional.false_expression);
1965 return EXPR_CLASS_ERROR;
1970 panic("invalid expression found (is constant expression)");
1975 obstack_init(&ast_obstack);
1980 obstack_free(&ast_obstack, NULL);