2 * This file is part of cparser.
3 * Copyright (C) 2007-2009 Matthias Braun <matze@braunis.de>
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
26 #include "lang_features.h"
36 #if defined(__INTEL_COMPILER)
38 #elif defined(__CYGWIN__)
39 #include "win32/cygwin_math_ext.h"
44 #include "adt/error.h"
47 struct obstack ast_obstack;
50 static int case_indent;
52 bool print_implicit_casts = false;
53 bool print_parenthesis = false;
55 static void print_statement(const statement_t *statement);
56 static void print_expression_prec(const expression_t *expression, unsigned prec);
58 void change_indent(int delta)
64 void print_indent(void)
66 for (int i = 0; i < indent; ++i)
70 static void print_stringrep(const string_t *string)
72 for (size_t i = 0; i < string->size; ++i) {
73 print_char(string->begin[i]);
78 * Returns 1 if a given precedence level has right-to-left
79 * associativity, else 0.
81 * @param precedence the operator precedence
83 static int right_to_left(unsigned precedence)
87 case PREC_CONDITIONAL:
97 * Return the precedence of an expression given by its kind.
99 * @param kind the expression kind
101 static unsigned get_expression_precedence(expression_kind_t kind)
103 static const unsigned prec[] = {
104 [EXPR_REFERENCE] = PREC_PRIMARY,
105 [EXPR_REFERENCE_ENUM_VALUE] = PREC_PRIMARY,
106 [EXPR_LITERAL_INTEGER] = PREC_PRIMARY,
107 [EXPR_LITERAL_INTEGER_OCTAL] = PREC_PRIMARY,
108 [EXPR_LITERAL_INTEGER_HEXADECIMAL] = PREC_PRIMARY,
109 [EXPR_LITERAL_FLOATINGPOINT] = PREC_PRIMARY,
110 [EXPR_LITERAL_FLOATINGPOINT_HEXADECIMAL] = PREC_PRIMARY,
111 [EXPR_LITERAL_CHARACTER] = PREC_PRIMARY,
112 [EXPR_LITERAL_WIDE_CHARACTER] = PREC_PRIMARY,
113 [EXPR_LITERAL_MS_NOOP] = PREC_PRIMARY,
114 [EXPR_STRING_LITERAL] = PREC_PRIMARY,
115 [EXPR_WIDE_STRING_LITERAL] = PREC_PRIMARY,
116 [EXPR_COMPOUND_LITERAL] = PREC_UNARY,
117 [EXPR_CALL] = PREC_POSTFIX,
118 [EXPR_CONDITIONAL] = PREC_CONDITIONAL,
119 [EXPR_SELECT] = PREC_POSTFIX,
120 [EXPR_ARRAY_ACCESS] = PREC_POSTFIX,
121 [EXPR_SIZEOF] = PREC_UNARY,
122 [EXPR_CLASSIFY_TYPE] = PREC_UNARY,
123 [EXPR_ALIGNOF] = PREC_UNARY,
125 [EXPR_FUNCNAME] = PREC_PRIMARY,
126 [EXPR_BUILTIN_CONSTANT_P] = PREC_PRIMARY,
127 [EXPR_BUILTIN_TYPES_COMPATIBLE_P] = PREC_PRIMARY,
128 [EXPR_OFFSETOF] = PREC_PRIMARY,
129 [EXPR_VA_START] = PREC_PRIMARY,
130 [EXPR_VA_ARG] = PREC_PRIMARY,
131 [EXPR_VA_COPY] = PREC_PRIMARY,
132 [EXPR_STATEMENT] = PREC_PRIMARY,
133 [EXPR_LABEL_ADDRESS] = PREC_PRIMARY,
135 [EXPR_UNARY_NEGATE] = PREC_UNARY,
136 [EXPR_UNARY_PLUS] = PREC_UNARY,
137 [EXPR_UNARY_BITWISE_NEGATE] = PREC_UNARY,
138 [EXPR_UNARY_NOT] = PREC_UNARY,
139 [EXPR_UNARY_DEREFERENCE] = PREC_UNARY,
140 [EXPR_UNARY_TAKE_ADDRESS] = PREC_UNARY,
141 [EXPR_UNARY_POSTFIX_INCREMENT] = PREC_POSTFIX,
142 [EXPR_UNARY_POSTFIX_DECREMENT] = PREC_POSTFIX,
143 [EXPR_UNARY_PREFIX_INCREMENT] = PREC_UNARY,
144 [EXPR_UNARY_PREFIX_DECREMENT] = PREC_UNARY,
145 [EXPR_UNARY_CAST] = PREC_UNARY,
146 [EXPR_UNARY_ASSUME] = PREC_PRIMARY,
147 [EXPR_UNARY_DELETE] = PREC_UNARY,
148 [EXPR_UNARY_DELETE_ARRAY] = PREC_UNARY,
149 [EXPR_UNARY_THROW] = PREC_ASSIGNMENT,
151 [EXPR_BINARY_ADD] = PREC_ADDITIVE,
152 [EXPR_BINARY_SUB] = PREC_ADDITIVE,
153 [EXPR_BINARY_MUL] = PREC_MULTIPLICATIVE,
154 [EXPR_BINARY_DIV] = PREC_MULTIPLICATIVE,
155 [EXPR_BINARY_MOD] = PREC_MULTIPLICATIVE,
156 [EXPR_BINARY_EQUAL] = PREC_EQUALITY,
157 [EXPR_BINARY_NOTEQUAL] = PREC_EQUALITY,
158 [EXPR_BINARY_LESS] = PREC_RELATIONAL,
159 [EXPR_BINARY_LESSEQUAL] = PREC_RELATIONAL,
160 [EXPR_BINARY_GREATER] = PREC_RELATIONAL,
161 [EXPR_BINARY_GREATEREQUAL] = PREC_RELATIONAL,
162 [EXPR_BINARY_BITWISE_AND] = PREC_AND,
163 [EXPR_BINARY_BITWISE_OR] = PREC_OR,
164 [EXPR_BINARY_BITWISE_XOR] = PREC_XOR,
165 [EXPR_BINARY_LOGICAL_AND] = PREC_LOGICAL_AND,
166 [EXPR_BINARY_LOGICAL_OR] = PREC_LOGICAL_OR,
167 [EXPR_BINARY_SHIFTLEFT] = PREC_SHIFT,
168 [EXPR_BINARY_SHIFTRIGHT] = PREC_SHIFT,
169 [EXPR_BINARY_ASSIGN] = PREC_ASSIGNMENT,
170 [EXPR_BINARY_MUL_ASSIGN] = PREC_ASSIGNMENT,
171 [EXPR_BINARY_DIV_ASSIGN] = PREC_ASSIGNMENT,
172 [EXPR_BINARY_MOD_ASSIGN] = PREC_ASSIGNMENT,
173 [EXPR_BINARY_ADD_ASSIGN] = PREC_ASSIGNMENT,
174 [EXPR_BINARY_SUB_ASSIGN] = PREC_ASSIGNMENT,
175 [EXPR_BINARY_SHIFTLEFT_ASSIGN] = PREC_ASSIGNMENT,
176 [EXPR_BINARY_SHIFTRIGHT_ASSIGN] = PREC_ASSIGNMENT,
177 [EXPR_BINARY_BITWISE_AND_ASSIGN] = PREC_ASSIGNMENT,
178 [EXPR_BINARY_BITWISE_XOR_ASSIGN] = PREC_ASSIGNMENT,
179 [EXPR_BINARY_BITWISE_OR_ASSIGN] = PREC_ASSIGNMENT,
180 [EXPR_BINARY_COMMA] = PREC_EXPRESSION,
182 [EXPR_BINARY_ISGREATER] = PREC_PRIMARY,
183 [EXPR_BINARY_ISGREATEREQUAL] = PREC_PRIMARY,
184 [EXPR_BINARY_ISLESS] = PREC_PRIMARY,
185 [EXPR_BINARY_ISLESSEQUAL] = PREC_PRIMARY,
186 [EXPR_BINARY_ISLESSGREATER] = PREC_PRIMARY,
187 [EXPR_BINARY_ISUNORDERED] = PREC_PRIMARY
189 assert((size_t)kind < lengthof(prec));
190 unsigned res = prec[kind];
192 assert(res != PREC_BOTTOM);
197 * Print a quoted string constant.
199 * @param string the string constant
200 * @param border the border char
201 * @param skip number of chars to skip at the end
203 static void print_quoted_string(const string_t *const string, char border,
207 const char *end = string->begin + string->size - skip;
208 for (const char *c = string->begin; c != end; ++c) {
214 case '\\': print_string("\\\\"); break;
215 case '\a': print_string("\\a"); break;
216 case '\b': print_string("\\b"); break;
217 case '\f': print_string("\\f"); break;
218 case '\n': print_string("\\n"); break;
219 case '\r': print_string("\\r"); break;
220 case '\t': print_string("\\t"); break;
221 case '\v': print_string("\\v"); break;
222 case '\?': print_string("\\?"); break;
224 if (c_mode & _GNUC) {
225 print_string("\\e"); break;
229 if ((unsigned)tc < 0x80 && !isprint(tc)) {
230 print_format("\\%03o", (unsigned)tc);
240 static void print_string_literal(const string_literal_expression_t *literal)
242 if (literal->base.kind == EXPR_WIDE_STRING_LITERAL) {
245 print_quoted_string(&literal->value, '"', 1);
248 static void print_literal(const literal_expression_t *literal)
250 switch (literal->base.kind) {
251 case EXPR_LITERAL_MS_NOOP:
252 print_string("__noop");
254 case EXPR_LITERAL_INTEGER_HEXADECIMAL:
255 case EXPR_LITERAL_FLOATINGPOINT_HEXADECIMAL:
258 case EXPR_LITERAL_BOOLEAN:
259 case EXPR_LITERAL_INTEGER:
260 case EXPR_LITERAL_INTEGER_OCTAL:
261 case EXPR_LITERAL_FLOATINGPOINT:
262 print_stringrep(&literal->value);
263 if (literal->suffix.size > 0)
264 print_stringrep(&literal->suffix);
266 case EXPR_LITERAL_WIDE_CHARACTER:
269 case EXPR_LITERAL_CHARACTER:
270 print_quoted_string(&literal->value, '\'', 0);
275 print_string("INVALID LITERAL KIND");
279 * Prints a predefined symbol.
281 static void print_funcname(const funcname_expression_t *funcname)
284 switch (funcname->kind) {
285 case FUNCNAME_FUNCTION: s = (c_mode & _C99) ? "__func__" : "__FUNCTION__"; break;
286 case FUNCNAME_PRETTY_FUNCTION: s = "__PRETTY_FUNCTION__"; break;
287 case FUNCNAME_FUNCSIG: s = "__FUNCSIG__"; break;
288 case FUNCNAME_FUNCDNAME: s = "__FUNCDNAME__"; break;
293 static void print_compound_literal(
294 const compound_literal_expression_t *expression)
297 print_type(expression->type);
299 print_initializer(expression->initializer);
302 static void print_assignment_expression(const expression_t *const expr)
304 print_expression_prec(expr, PREC_ASSIGNMENT);
308 * Prints a call expression.
310 * @param call the call expression
312 static void print_call_expression(const call_expression_t *call)
314 print_expression_prec(call->function, PREC_POSTFIX);
317 for (call_argument_t const *arg = call->arguments; arg; arg = arg->next) {
323 print_assignment_expression(arg->expression);
329 * Prints a binary expression.
331 * @param binexpr the binary expression
333 static void print_binary_expression(const binary_expression_t *binexpr)
335 unsigned prec = get_expression_precedence(binexpr->base.kind);
336 int r2l = right_to_left(prec);
338 print_expression_prec(binexpr->left, prec + r2l);
340 switch (binexpr->base.kind) {
341 case EXPR_BINARY_COMMA: op = ", "; break;
342 case EXPR_BINARY_ASSIGN: op = " = "; break;
343 case EXPR_BINARY_ADD: op = " + "; break;
344 case EXPR_BINARY_SUB: op = " - "; break;
345 case EXPR_BINARY_MUL: op = " * "; break;
346 case EXPR_BINARY_MOD: op = " % "; break;
347 case EXPR_BINARY_DIV: op = " / "; break;
348 case EXPR_BINARY_BITWISE_OR: op = " | "; break;
349 case EXPR_BINARY_BITWISE_AND: op = " & "; break;
350 case EXPR_BINARY_BITWISE_XOR: op = " ^ "; break;
351 case EXPR_BINARY_LOGICAL_OR: op = " || "; break;
352 case EXPR_BINARY_LOGICAL_AND: op = " && "; break;
353 case EXPR_BINARY_NOTEQUAL: op = " != "; break;
354 case EXPR_BINARY_EQUAL: op = " == "; break;
355 case EXPR_BINARY_LESS: op = " < "; break;
356 case EXPR_BINARY_LESSEQUAL: op = " <= "; break;
357 case EXPR_BINARY_GREATER: op = " > "; break;
358 case EXPR_BINARY_GREATEREQUAL: op = " >= "; break;
359 case EXPR_BINARY_SHIFTLEFT: op = " << "; break;
360 case EXPR_BINARY_SHIFTRIGHT: op = " >> "; break;
362 case EXPR_BINARY_ADD_ASSIGN: op = " += "; break;
363 case EXPR_BINARY_SUB_ASSIGN: op = " -= "; break;
364 case EXPR_BINARY_MUL_ASSIGN: op = " *= "; break;
365 case EXPR_BINARY_MOD_ASSIGN: op = " %= "; break;
366 case EXPR_BINARY_DIV_ASSIGN: op = " /= "; break;
367 case EXPR_BINARY_BITWISE_OR_ASSIGN: op = " |= "; break;
368 case EXPR_BINARY_BITWISE_AND_ASSIGN: op = " &= "; break;
369 case EXPR_BINARY_BITWISE_XOR_ASSIGN: op = " ^= "; break;
370 case EXPR_BINARY_SHIFTLEFT_ASSIGN: op = " <<= "; break;
371 case EXPR_BINARY_SHIFTRIGHT_ASSIGN: op = " >>= "; break;
372 default: panic("invalid binexpression found");
375 print_expression_prec(binexpr->right, prec + 1 - r2l);
379 * Prints an unary expression.
381 * @param unexpr the unary expression
383 static void print_unary_expression(const unary_expression_t *unexpr)
385 unsigned prec = get_expression_precedence(unexpr->base.kind);
386 switch (unexpr->base.kind) {
387 case EXPR_UNARY_NEGATE: print_char ('-' ); break;
388 case EXPR_UNARY_PLUS: print_char ('+' ); break;
389 case EXPR_UNARY_NOT: print_char ('!' ); break;
390 case EXPR_UNARY_BITWISE_NEGATE: print_char ('~' ); break;
391 case EXPR_UNARY_PREFIX_INCREMENT: print_string("++"); break;
392 case EXPR_UNARY_PREFIX_DECREMENT: print_string("--"); break;
393 case EXPR_UNARY_DEREFERENCE: print_char ('*' ); break;
394 case EXPR_UNARY_TAKE_ADDRESS: print_char ('&' ); break;
395 case EXPR_UNARY_DELETE: print_string("delete "); break;
396 case EXPR_UNARY_DELETE_ARRAY: print_string("delete [] "); break;
398 case EXPR_UNARY_POSTFIX_INCREMENT:
399 print_expression_prec(unexpr->value, prec);
402 case EXPR_UNARY_POSTFIX_DECREMENT:
403 print_expression_prec(unexpr->value, prec);
406 case EXPR_UNARY_CAST:
408 print_type(unexpr->base.type);
411 case EXPR_UNARY_ASSUME:
412 print_string("__assume(");
413 print_assignment_expression(unexpr->value);
417 case EXPR_UNARY_THROW:
418 if (unexpr->value == NULL) {
419 print_string("throw");
422 print_string("throw ");
426 panic("invalid unary expression found");
428 print_expression_prec(unexpr->value, prec);
432 * Prints a reference expression.
434 * @param ref the reference expression
436 static void print_reference_expression(const reference_expression_t *ref)
438 print_string(ref->entity->base.symbol->string);
442 * Prints a label address expression.
444 * @param ref the reference expression
446 static void print_label_address_expression(const label_address_expression_t *le)
448 print_format("&&%s", le->label->base.symbol->string);
452 * Prints an array expression.
454 * @param expression the array expression
456 static void print_array_expression(const array_access_expression_t *expression)
458 if (!expression->flipped) {
459 print_expression_prec(expression->array_ref, PREC_POSTFIX);
461 print_expression(expression->index);
464 print_expression_prec(expression->index, PREC_POSTFIX);
466 print_expression(expression->array_ref);
472 * Prints a typeproperty expression (sizeof or __alignof__).
474 * @param expression the type property expression
476 static void print_typeprop_expression(const typeprop_expression_t *expression)
478 if (expression->base.kind == EXPR_SIZEOF) {
479 print_string("sizeof");
481 assert(expression->base.kind == EXPR_ALIGNOF);
482 print_string("__alignof__");
484 if (expression->tp_expression != NULL) {
485 /* PREC_TOP: always print the '()' here, sizeof x is right but unusual */
486 print_expression_prec(expression->tp_expression, PREC_TOP);
489 print_type(expression->type);
495 * Prints a builtin constant expression.
497 * @param expression the builtin constant expression
499 static void print_builtin_constant(const builtin_constant_expression_t *expression)
501 print_string("__builtin_constant_p(");
502 print_assignment_expression(expression->value);
507 * Prints a builtin types compatible expression.
509 * @param expression the builtin types compatible expression
511 static void print_builtin_types_compatible(
512 const builtin_types_compatible_expression_t *expression)
514 print_string("__builtin_types_compatible_p(");
515 print_type(expression->left);
517 print_type(expression->right);
522 * Prints a conditional expression.
524 * @param expression the conditional expression
526 static void print_conditional(const conditional_expression_t *expression)
528 print_expression_prec(expression->condition, PREC_LOGICAL_OR);
529 if (expression->true_expression != NULL) {
531 print_expression_prec(expression->true_expression, PREC_EXPRESSION);
534 print_string(" ?: ");
536 precedence_t prec = c_mode & _CXX ? PREC_ASSIGNMENT : PREC_CONDITIONAL;
537 print_expression_prec(expression->false_expression, prec);
541 * Prints a va_start expression.
543 * @param expression the va_start expression
545 static void print_va_start(const va_start_expression_t *const expression)
547 print_string("__builtin_va_start(");
548 print_assignment_expression(expression->ap);
550 print_string(expression->parameter->base.base.symbol->string);
555 * Prints a va_arg expression.
557 * @param expression the va_arg expression
559 static void print_va_arg(const va_arg_expression_t *expression)
561 print_string("__builtin_va_arg(");
562 print_assignment_expression(expression->ap);
564 print_type(expression->base.type);
569 * Prints a va_copy expression.
571 * @param expression the va_copy expression
573 static void print_va_copy(const va_copy_expression_t *expression)
575 print_string("__builtin_va_copy(");
576 print_assignment_expression(expression->dst);
578 print_assignment_expression(expression->src);
583 * Prints a select expression (. or ->).
585 * @param expression the select expression
587 static void print_select(const select_expression_t *expression)
589 print_expression_prec(expression->compound, PREC_POSTFIX);
590 /* do not print anything for anonymous struct/union selects
591 * FIXME: if the anonymous select was a '->' this will print '.'
593 if (expression->compound_entry->base.symbol == NULL)
596 if (is_type_pointer(skip_typeref(expression->compound->base.type))) {
601 print_string(expression->compound_entry->base.symbol->string);
605 * Prints a type classify expression.
607 * @param expr the type classify expression
609 static void print_classify_type_expression(
610 const classify_type_expression_t *const expr)
612 print_string("__builtin_classify_type(");
613 print_assignment_expression(expr->type_expression);
618 * Prints a designator.
620 * @param designator the designator
622 static void print_designator(const designator_t *designator)
624 for ( ; designator != NULL; designator = designator->next) {
625 if (designator->symbol == NULL) {
627 print_expression(designator->array_index);
631 print_string(designator->symbol->string);
637 * Prints an offsetof expression.
639 * @param expression the offset expression
641 static void print_offsetof_expression(const offsetof_expression_t *expression)
643 print_string("__builtin_offsetof(");
644 print_type(expression->type);
646 print_designator(expression->designator);
651 * Prints a statement expression.
653 * @param expression the statement expression
655 static void print_statement_expression(const statement_expression_t *expression)
658 print_statement(expression->statement);
663 * Prints an expression with parenthesis if needed.
665 * @param expression the expression to print
666 * @param top_prec the precedence of the user of this expression.
668 static void print_expression_prec(const expression_t *expression, unsigned top_prec)
670 if (expression->kind == EXPR_UNARY_CAST
671 && expression->base.implicit && !print_implicit_casts) {
672 expression = expression->unary.value;
676 expression->base.parenthesized ||
677 (print_parenthesis && top_prec != PREC_BOTTOM) ||
678 top_prec > get_expression_precedence(expression->base.kind);
682 switch (expression->kind) {
684 print_string("$error$");
686 case EXPR_WIDE_STRING_LITERAL:
687 case EXPR_STRING_LITERAL:
688 print_string_literal(&expression->string_literal);
691 print_literal(&expression->literal);
694 print_funcname(&expression->funcname);
696 case EXPR_COMPOUND_LITERAL:
697 print_compound_literal(&expression->compound_literal);
700 print_call_expression(&expression->call);
703 print_binary_expression(&expression->binary);
706 case EXPR_REFERENCE_ENUM_VALUE:
707 print_reference_expression(&expression->reference);
709 case EXPR_ARRAY_ACCESS:
710 print_array_expression(&expression->array_access);
712 case EXPR_LABEL_ADDRESS:
713 print_label_address_expression(&expression->label_address);
716 print_unary_expression(&expression->unary);
720 print_typeprop_expression(&expression->typeprop);
722 case EXPR_BUILTIN_CONSTANT_P:
723 print_builtin_constant(&expression->builtin_constant);
725 case EXPR_BUILTIN_TYPES_COMPATIBLE_P:
726 print_builtin_types_compatible(&expression->builtin_types_compatible);
728 case EXPR_CONDITIONAL:
729 print_conditional(&expression->conditional);
732 print_va_start(&expression->va_starte);
735 print_va_arg(&expression->va_arge);
738 print_va_copy(&expression->va_copye);
741 print_select(&expression->select);
743 case EXPR_CLASSIFY_TYPE:
744 print_classify_type_expression(&expression->classify_type);
747 print_offsetof_expression(&expression->offsetofe);
750 print_statement_expression(&expression->statement);
757 static void print_indented_statement(statement_t const *const stmt)
759 switch (stmt->kind) {
760 case STATEMENT_LABEL:
763 case STATEMENT_CASE_LABEL:
764 for (int i = 0; i != case_indent; ++i)
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 for (statement_t const *stmt = block->statements; stmt; stmt = stmt->base.next) {
786 print_indented_statement(stmt);
796 * Print a return statement.
798 * @param statement the return statement
800 static void print_return_statement(const return_statement_t *statement)
802 expression_t const *const val = statement->value;
804 print_string("return ");
805 print_expression(val);
808 print_string("return;");
813 * Print an expression statement.
815 * @param statement the expression statement
817 static void print_expression_statement(const expression_statement_t *statement)
819 print_expression(statement->expression);
824 * Print a goto statement.
826 * @param statement the goto statement
828 static void print_goto_statement(const goto_statement_t *statement)
830 print_string("goto ");
831 if (statement->expression != NULL) {
833 print_expression(statement->expression);
835 print_string(statement->label->base.symbol->string);
841 * Print a label statement.
843 * @param statement the label statement
845 static void print_label_statement(const label_statement_t *statement)
847 print_format("%s:\n", statement->label->base.symbol->string);
848 print_indented_statement(statement->statement);
851 static void print_inner_statement(statement_t const *const stmt)
853 if (stmt->kind == STATEMENT_COMPOUND) {
855 print_compound_statement(&stmt->compound);
859 print_indented_statement(stmt);
864 static void print_after_inner_statement(statement_t const *const stmt)
866 if (stmt->kind == STATEMENT_COMPOUND) {
875 * Print an if statement.
877 * @param statement the if statement
879 static void print_if_statement(const if_statement_t *statement)
881 print_string("if (");
882 print_expression(statement->condition);
884 print_inner_statement(statement->true_statement);
886 statement_t const *const f = statement->false_statement;
888 print_after_inner_statement(statement->true_statement);
889 print_string("else");
890 if (f->kind == STATEMENT_IF) {
892 print_if_statement(&f->ifs);
894 print_inner_statement(f);
900 * Print a switch statement.
902 * @param statement the switch statement
904 static void print_switch_statement(const switch_statement_t *statement)
906 int const old_case_indent = case_indent;
907 case_indent = indent;
909 print_string("switch (");
910 print_expression(statement->expression);
912 print_inner_statement(statement->body);
914 case_indent = old_case_indent;
918 * Print a case label (including the default label).
920 * @param statement the case label statement
922 static void print_case_label(const case_label_statement_t *statement)
924 if (statement->expression == NULL) {
925 print_string("default:\n");
927 print_string("case ");
928 print_expression(statement->expression);
929 if (statement->end_range != NULL) {
930 print_string(" ... ");
931 print_expression(statement->end_range);
935 print_indented_statement(statement->statement);
938 static void print_typedef(const entity_t *entity)
940 print_string("typedef ");
941 print_type_ext(entity->typedefe.type, entity->base.symbol, NULL);
946 * returns true if the entity is a compiler generated one and has no real
947 * correspondenc in the source file
949 static bool is_generated_entity(const entity_t *entity)
951 if (entity->kind == ENTITY_TYPEDEF)
952 return entity->typedefe.builtin;
954 if (is_declaration(entity))
955 return entity->declaration.implicit;
961 * Print a declaration statement.
963 * @param statement the statement
965 static void print_declaration_statement(
966 const declaration_statement_t *statement)
969 entity_t *entity = statement->declarations_begin;
970 if (entity == NULL) {
971 print_string("/* empty declaration statement */");
975 entity_t *const end = statement->declarations_end->base.next;
976 for (; entity != end; entity = entity->base.next) {
977 if (entity->kind == ENTITY_ENUM_VALUE)
979 if (is_generated_entity(entity))
989 print_entity(entity);
994 * Print a while statement.
996 * @param statement the statement
998 static void print_while_statement(const while_statement_t *statement)
1000 print_string("while (");
1001 print_expression(statement->condition);
1003 print_inner_statement(statement->body);
1007 * Print a do-while statement.
1009 * @param statement the statement
1011 static void print_do_while_statement(const do_while_statement_t *statement)
1014 print_inner_statement(statement->body);
1015 print_after_inner_statement(statement->body);
1016 print_string("while (");
1017 print_expression(statement->condition);
1022 * Print a for statement.
1024 * @param statement the statement
1026 static void print_for_statement(const for_statement_t *statement)
1028 print_string("for (");
1029 if (statement->initialisation != NULL) {
1030 print_expression(statement->initialisation);
1033 entity_t const *entity = statement->scope.entities;
1034 for (; entity != NULL; entity = entity->base.next) {
1035 if (is_generated_entity(entity))
1037 /* FIXME display of multiple declarations is wrong */
1038 print_declaration(entity);
1041 if (statement->condition != NULL) {
1043 print_expression(statement->condition);
1046 if (statement->step != NULL) {
1048 print_expression(statement->step);
1051 print_inner_statement(statement->body);
1055 * Print assembler arguments.
1057 * @param arguments the arguments
1059 static void print_asm_arguments(asm_argument_t *arguments)
1061 asm_argument_t *argument = arguments;
1062 for (; argument != NULL; argument = argument->next) {
1063 if (argument != arguments)
1066 if (argument->symbol) {
1067 print_format("[%s] ", argument->symbol->string);
1069 print_quoted_string(&argument->constraints, '"', 1);
1071 print_expression(argument->expression);
1077 * Print assembler clobbers.
1079 * @param clobbers the clobbers
1081 static void print_asm_clobbers(asm_clobber_t *clobbers)
1083 asm_clobber_t *clobber = clobbers;
1084 for (; clobber != NULL; clobber = clobber->next) {
1085 if (clobber != clobbers)
1088 print_quoted_string(&clobber->clobber, '"', 1);
1093 * Print an assembler statement.
1095 * @param statement the statement
1097 static void print_asm_statement(const asm_statement_t *statement)
1099 print_string("asm ");
1100 if (statement->is_volatile) {
1101 print_string("volatile ");
1104 print_quoted_string(&statement->asm_text, '"', 1);
1105 if (statement->outputs == NULL &&
1106 statement->inputs == NULL &&
1107 statement->clobbers == NULL)
1108 goto end_of_print_asm_statement;
1110 print_string(" : ");
1111 print_asm_arguments(statement->outputs);
1112 if (statement->inputs == NULL && statement->clobbers == NULL)
1113 goto end_of_print_asm_statement;
1115 print_string(" : ");
1116 print_asm_arguments(statement->inputs);
1117 if (statement->clobbers == NULL)
1118 goto end_of_print_asm_statement;
1120 print_string(" : ");
1121 print_asm_clobbers(statement->clobbers);
1123 end_of_print_asm_statement:
1128 * Print a microsoft __try statement.
1130 * @param statement the statement
1132 static void print_ms_try_statement(const ms_try_statement_t *statement)
1134 print_string("__try");
1135 print_inner_statement(statement->try_statement);
1136 print_after_inner_statement(statement->try_statement);
1137 if (statement->except_expression != NULL) {
1138 print_string("__except(");
1139 print_expression(statement->except_expression);
1142 print_string("__finally");
1144 print_inner_statement(statement->final_statement);
1148 * Print a microsoft __leave statement.
1150 * @param statement the statement
1152 static void print_leave_statement(const leave_statement_t *statement)
1155 print_string("__leave;");
1159 * Print a statement.
1161 * @param statement the statement
1163 void print_statement(const statement_t *statement)
1165 switch (statement->kind) {
1166 case STATEMENT_EMPTY:
1169 case STATEMENT_COMPOUND:
1170 print_compound_statement(&statement->compound);
1172 case STATEMENT_RETURN:
1173 print_return_statement(&statement->returns);
1175 case STATEMENT_EXPRESSION:
1176 print_expression_statement(&statement->expression);
1178 case STATEMENT_LABEL:
1179 print_label_statement(&statement->label);
1181 case STATEMENT_GOTO:
1182 print_goto_statement(&statement->gotos);
1184 case STATEMENT_CONTINUE:
1185 print_string("continue;");
1187 case STATEMENT_BREAK:
1188 print_string("break;");
1191 print_if_statement(&statement->ifs);
1193 case STATEMENT_SWITCH:
1194 print_switch_statement(&statement->switchs);
1196 case STATEMENT_CASE_LABEL:
1197 print_case_label(&statement->case_label);
1199 case STATEMENT_DECLARATION:
1200 print_declaration_statement(&statement->declaration);
1202 case STATEMENT_WHILE:
1203 print_while_statement(&statement->whiles);
1205 case STATEMENT_DO_WHILE:
1206 print_do_while_statement(&statement->do_while);
1209 print_for_statement(&statement->fors);
1212 print_asm_statement(&statement->asms);
1214 case STATEMENT_MS_TRY:
1215 print_ms_try_statement(&statement->ms_try);
1217 case STATEMENT_LEAVE:
1218 print_leave_statement(&statement->leave);
1220 case STATEMENT_ERROR:
1221 print_string("$error statement$");
1227 * Print a storage class.
1229 * @param storage_class the storage class
1231 static void print_storage_class(storage_class_tag_t storage_class)
1233 switch (storage_class) {
1234 case STORAGE_CLASS_NONE: return;
1235 case STORAGE_CLASS_TYPEDEF: print_string("typedef "); return;
1236 case STORAGE_CLASS_EXTERN: print_string("extern "); return;
1237 case STORAGE_CLASS_STATIC: print_string("static "); return;
1238 case STORAGE_CLASS_AUTO: print_string("auto "); return;
1239 case STORAGE_CLASS_REGISTER: print_string("register "); return;
1241 panic("invalid storage class");
1245 * Print an initializer.
1247 * @param initializer the initializer
1249 void print_initializer(const initializer_t *initializer)
1251 if (initializer == NULL) {
1256 switch (initializer->kind) {
1257 case INITIALIZER_VALUE: {
1258 const initializer_value_t *value = &initializer->value;
1259 print_assignment_expression(value->value);
1262 case INITIALIZER_LIST: {
1263 assert(initializer->kind == INITIALIZER_LIST);
1265 const initializer_list_t *list = &initializer->list;
1267 for (size_t i = 0 ; i < list->len; ++i) {
1268 const initializer_t *sub_init = list->initializers[i];
1269 print_initializer(list->initializers[i]);
1270 if (i < list->len-1) {
1271 if (sub_init == NULL || sub_init->kind != INITIALIZER_DESIGNATOR)
1278 case INITIALIZER_STRING:
1279 print_quoted_string(&initializer->string.string, '"', 1);
1281 case INITIALIZER_WIDE_STRING:
1282 print_quoted_string(&initializer->string.string, '"', 1);
1284 case INITIALIZER_DESIGNATOR:
1285 print_designator(initializer->designator.designator);
1286 print_string(" = ");
1290 panic("invalid initializer kind found");
1295 * Print microsoft extended declaration modifiers.
1297 static void print_ms_modifiers(const declaration_t *declaration)
1299 if ((c_mode & _MS) == 0)
1302 decl_modifiers_t modifiers = declaration->modifiers;
1304 bool ds_shown = false;
1305 const char *next = "(";
1307 if (declaration->base.kind == ENTITY_VARIABLE) {
1308 variable_t *variable = (variable_t*)declaration;
1309 if (variable->alignment != 0
1310 || variable->get_property_sym != NULL
1311 || variable->put_property_sym != NULL) {
1313 print_string("__declspec");
1317 if (variable->alignment != 0) {
1318 print_string(next); next = ", "; print_format("align(%u)", variable->alignment);
1320 if (variable->get_property_sym != NULL
1321 || variable->put_property_sym != NULL) {
1323 print_string(next); next = ", "; print_string("property(");
1324 if (variable->get_property_sym != NULL) {
1325 print_format("get=%s", variable->get_property_sym->string);
1328 if (variable->put_property_sym != NULL)
1329 print_format("%sput=%s", comma, variable->put_property_sym->string);
1335 /* DM_FORCEINLINE handled outside. */
1336 if ((modifiers & ~DM_FORCEINLINE) != 0) {
1338 print_string("__declspec");
1341 if (modifiers & DM_DLLIMPORT) {
1342 print_string(next); next = ", "; print_string("dllimport");
1344 if (modifiers & DM_DLLEXPORT) {
1345 print_string(next); next = ", "; print_string("dllexport");
1347 if (modifiers & DM_THREAD) {
1348 print_string(next); next = ", "; print_string("thread");
1350 if (modifiers & DM_NAKED) {
1351 print_string(next); next = ", "; print_string("naked");
1353 if (modifiers & DM_THREAD) {
1354 print_string(next); next = ", "; print_string("thread");
1356 if (modifiers & DM_SELECTANY) {
1357 print_string(next); next = ", "; print_string("selectany");
1359 if (modifiers & DM_NOTHROW) {
1360 print_string(next); next = ", "; print_string("nothrow");
1362 if (modifiers & DM_NORETURN) {
1363 print_string(next); next = ", "; print_string("noreturn");
1365 if (modifiers & DM_NOINLINE) {
1366 print_string(next); next = ", "; print_string("noinline");
1368 if (modifiers & DM_DEPRECATED) {
1369 print_string(next); next = ", "; print_string("deprecated");
1370 if (declaration->deprecated_string != NULL)
1371 print_format("(\"%s\")",
1372 declaration->deprecated_string);
1374 if (modifiers & DM_RESTRICT) {
1375 print_string(next); next = ", "; print_string("restrict");
1377 if (modifiers & DM_NOALIAS) {
1378 print_string(next); next = ", "; print_string("noalias");
1387 static void print_scope(const scope_t *scope)
1389 const entity_t *entity = scope->entities;
1390 for ( ; entity != NULL; entity = entity->base.next) {
1392 print_entity(entity);
1397 static void print_namespace(const namespace_t *namespace)
1399 print_string("namespace ");
1400 if (namespace->base.symbol != NULL) {
1401 print_string(namespace->base.symbol->string);
1405 print_string("{\n");
1408 print_scope(&namespace->members);
1412 print_string("}\n");
1416 * Print a variable or function declaration
1418 void print_declaration(const entity_t *entity)
1420 assert(is_declaration(entity));
1421 const declaration_t *declaration = &entity->declaration;
1423 print_storage_class((storage_class_tag_t)declaration->declared_storage_class);
1424 if (entity->kind == ENTITY_FUNCTION) {
1425 function_t *function = (function_t*)declaration;
1426 if (function->is_inline) {
1427 if (declaration->modifiers & DM_FORCEINLINE) {
1428 print_string("__forceinline ");
1429 } else if (declaration->modifiers & DM_MICROSOFT_INLINE) {
1430 print_string("__inline ");
1432 print_string("inline ");
1436 //print_ms_modifiers(declaration);
1437 switch (entity->kind) {
1438 case ENTITY_FUNCTION:
1439 print_type_ext(entity->declaration.type, entity->base.symbol,
1440 &entity->function.parameters);
1442 if (entity->function.statement != NULL) {
1444 print_indented_statement(entity->function.statement);
1450 case ENTITY_VARIABLE:
1451 if (entity->variable.thread_local)
1452 print_string("__thread ");
1453 print_type_ext(declaration->type, declaration->base.symbol, NULL);
1454 if (entity->variable.initializer != NULL) {
1455 print_string(" = ");
1456 print_initializer(entity->variable.initializer);
1460 case ENTITY_COMPOUND_MEMBER:
1461 print_type_ext(declaration->type, declaration->base.symbol, NULL);
1462 if (entity->compound_member.bitfield) {
1463 print_format(" : %u", entity->compound_member.bit_size);
1468 print_type_ext(declaration->type, declaration->base.symbol, NULL);
1475 * Prints an expression.
1477 * @param expression the expression
1479 void print_expression(const expression_t *expression)
1481 print_expression_prec(expression, PREC_BOTTOM);
1485 * Print a declaration.
1487 * @param declaration the declaration
1489 void print_entity(const entity_t *entity)
1491 if (entity->base.namespc != NAMESPACE_NORMAL && entity->base.symbol == NULL)
1494 switch ((entity_kind_tag_t)entity->kind) {
1495 case ENTITY_VARIABLE:
1496 case ENTITY_PARAMETER:
1497 case ENTITY_COMPOUND_MEMBER:
1498 case ENTITY_FUNCTION:
1499 print_declaration(entity);
1501 case ENTITY_TYPEDEF:
1502 print_typedef(entity);
1506 print_string("class ");
1507 print_string(entity->base.symbol->string);
1508 print_string("; /* TODO */\n");
1511 print_string("struct ");
1512 goto print_compound;
1514 print_string("union ");
1516 print_string(entity->base.symbol->string);
1517 if (entity->compound.complete) {
1519 print_compound_definition(&entity->compound);
1524 print_string("enum ");
1525 print_string(entity->base.symbol->string);
1527 print_enum_definition(&entity->enume);
1530 case ENTITY_NAMESPACE:
1531 print_namespace(&entity->namespacee);
1533 case ENTITY_LOCAL_LABEL:
1534 print_string("__label__ ");
1535 print_string(entity->base.symbol->string);
1539 case ENTITY_ENUM_VALUE:
1540 panic("print_entity used on unexpected entity type");
1542 panic("Invalid entity type encountered");
1546 * Print the AST of a translation unit.
1548 * @param unit the translation unit
1550 void print_ast(const translation_unit_t *unit)
1552 entity_t *entity = unit->scope.entities;
1553 for ( ; entity != NULL; entity = entity->base.next) {
1554 if (entity->kind == ENTITY_ENUM_VALUE)
1556 if (entity->base.namespc != NAMESPACE_NORMAL
1557 && entity->base.symbol == NULL)
1559 if (is_generated_entity(entity))
1563 print_entity(entity);
1568 expression_classification_t is_constant_initializer(const initializer_t *initializer)
1570 switch (initializer->kind) {
1571 case INITIALIZER_STRING:
1572 case INITIALIZER_WIDE_STRING:
1573 case INITIALIZER_DESIGNATOR:
1574 return EXPR_CLASS_CONSTANT;
1576 case INITIALIZER_VALUE:
1577 return is_linker_constant(initializer->value.value);
1579 case INITIALIZER_LIST: {
1580 expression_classification_t all = EXPR_CLASS_CONSTANT;
1581 for (size_t i = 0; i < initializer->list.len; ++i) {
1582 initializer_t *sub_initializer = initializer->list.initializers[i];
1583 expression_classification_t const cur = is_constant_initializer(sub_initializer);
1591 panic("invalid initializer kind found");
1595 * Checks if an expression references an object with a constant/known location
1596 * to the linker. Example:
1597 * - "x", "*&x" with x being a global variable. The value of x need not be
1598 * constant but the address of x is.
1599 * - "a.b.c" when a has a constant/known location to the linker
1601 static expression_classification_t is_object_with_linker_constant_address(
1602 const expression_t *expression)
1604 switch (expression->kind) {
1605 case EXPR_UNARY_DEREFERENCE:
1606 return is_linker_constant(expression->unary.value);
1609 type_t *base_type = skip_typeref(expression->select.compound->base.type);
1610 if (is_type_pointer(base_type)) {
1612 return is_linker_constant(expression->select.compound);
1614 return is_object_with_linker_constant_address(expression->select.compound);
1618 case EXPR_ARRAY_ACCESS: {
1619 expression_classification_t const ref = is_linker_constant(expression->array_access.array_ref);
1620 expression_classification_t const idx = is_constant_expression(expression->array_access.index);
1621 return ref < idx ? ref : idx;
1624 case EXPR_REFERENCE: {
1625 entity_t *entity = expression->reference.entity;
1626 if (!is_declaration(entity))
1627 return EXPR_CLASS_VARIABLE;
1629 switch ((storage_class_tag_t)entity->declaration.storage_class) {
1630 case STORAGE_CLASS_NONE:
1631 case STORAGE_CLASS_EXTERN:
1632 case STORAGE_CLASS_STATIC:
1634 entity->kind != ENTITY_VARIABLE ||
1635 !entity->variable.thread_local ? EXPR_CLASS_CONSTANT :
1636 EXPR_CLASS_VARIABLE;
1638 case STORAGE_CLASS_REGISTER:
1639 case STORAGE_CLASS_TYPEDEF:
1640 case STORAGE_CLASS_AUTO:
1643 return EXPR_CLASS_VARIABLE;
1647 return EXPR_CLASS_ERROR;
1650 return EXPR_CLASS_VARIABLE;
1654 expression_classification_t is_linker_constant(const expression_t *expression)
1656 switch (expression->kind) {
1657 case EXPR_STRING_LITERAL:
1658 case EXPR_WIDE_STRING_LITERAL:
1660 case EXPR_LABEL_ADDRESS:
1661 return EXPR_CLASS_CONSTANT;
1663 case EXPR_COMPOUND_LITERAL:
1664 return is_constant_initializer(expression->compound_literal.initializer);
1666 case EXPR_UNARY_TAKE_ADDRESS:
1667 return is_object_with_linker_constant_address(expression->unary.value);
1669 case EXPR_UNARY_DEREFERENCE: {
1671 = revert_automatic_type_conversion(expression->unary.value);
1672 /* dereferencing a function is a NOP */
1673 if (is_type_function(real_type)) {
1674 return is_linker_constant(expression->unary.value);
1679 case EXPR_UNARY_CAST: {
1680 type_t *dest = skip_typeref(expression->base.type);
1681 if (!is_type_pointer(dest) && (
1682 dest->kind != TYPE_ATOMIC ||
1683 !(get_atomic_type_flags(dest->atomic.akind) & ATOMIC_TYPE_FLAG_INTEGER) ||
1684 get_atomic_type_size(dest->atomic.akind) < get_type_size(type_void_ptr)
1686 return is_constant_expression(expression);
1688 return is_linker_constant(expression->unary.value);
1691 case EXPR_BINARY_ADD:
1692 case EXPR_BINARY_SUB: {
1693 expression_t *const left = expression->binary.left;
1694 expression_t *const right = expression->binary.right;
1695 type_t *const ltype = skip_typeref(left->base.type);
1696 type_t *const rtype = skip_typeref(right->base.type);
1698 if (is_type_pointer(ltype)) {
1699 expression_classification_t const l = is_linker_constant(left);
1700 expression_classification_t const r = is_constant_expression(right);
1701 return l < r ? l : r;
1702 } else if (is_type_pointer(rtype)) {
1703 expression_classification_t const l = is_constant_expression(left);
1704 expression_classification_t const r = is_linker_constant(right);
1705 return l < r ? l : r;
1706 } else if (!is_type_valid(ltype) || !is_type_valid(rtype)) {
1707 return EXPR_CLASS_ERROR;
1709 return is_constant_expression(expression);
1713 case EXPR_REFERENCE: {
1714 entity_t *entity = expression->reference.entity;
1715 if (!is_declaration(entity))
1716 return EXPR_CLASS_VARIABLE;
1718 type_t *type = skip_typeref(entity->declaration.type);
1719 if (is_type_function(type))
1720 return EXPR_CLASS_CONSTANT;
1721 if (is_type_array(type)) {
1722 return is_object_with_linker_constant_address(expression);
1724 /* Prevent stray errors */
1725 if (!is_type_valid(type))
1726 return EXPR_CLASS_ERROR;
1727 return EXPR_CLASS_VARIABLE;
1730 case EXPR_ARRAY_ACCESS: {
1731 type_t *const type =
1732 skip_typeref(revert_automatic_type_conversion(expression));
1733 if (!is_type_array(type))
1734 return EXPR_CLASS_VARIABLE;
1735 return is_linker_constant(expression->array_access.array_ref);
1738 case EXPR_CONDITIONAL: {
1739 expression_t *const c = expression->conditional.condition;
1740 expression_classification_t const cclass = is_constant_expression(c);
1741 if (cclass != EXPR_CLASS_CONSTANT)
1744 if (fold_constant_to_bool(c)) {
1745 expression_t const *const t = expression->conditional.true_expression;
1746 return is_linker_constant(t != NULL ? t : c);
1748 return is_linker_constant(expression->conditional.false_expression);
1753 entity_t *entity = expression->select.compound_entry;
1754 if (!is_declaration(entity))
1755 return EXPR_CLASS_VARIABLE;
1756 type_t *type = skip_typeref(entity->declaration.type);
1757 if (is_type_array(type)) {
1758 /* arrays automatically convert to their address */
1759 expression_t *compound = expression->select.compound;
1760 type_t *base_type = skip_typeref(compound->base.type);
1761 if (is_type_pointer(base_type)) {
1763 return is_linker_constant(compound);
1765 return is_object_with_linker_constant_address(compound);
1768 return EXPR_CLASS_VARIABLE;
1772 return is_constant_expression(expression);
1777 * Check if the given expression is a call to a builtin function
1778 * returning a constant result.
1780 static expression_classification_t is_builtin_const_call(const expression_t *expression)
1782 expression_t *function = expression->call.function;
1783 if (function->kind != EXPR_REFERENCE)
1784 return EXPR_CLASS_VARIABLE;
1785 reference_expression_t *ref = &function->reference;
1786 if (ref->entity->kind != ENTITY_FUNCTION)
1787 return EXPR_CLASS_VARIABLE;
1789 switch (ref->entity->function.btk) {
1792 return EXPR_CLASS_CONSTANT;
1794 return EXPR_CLASS_VARIABLE;
1799 static expression_classification_t is_constant_pointer(const expression_t *expression)
1801 expression_classification_t const expr_class = is_constant_expression(expression);
1802 if (expr_class != EXPR_CLASS_VARIABLE)
1805 switch (expression->kind) {
1806 case EXPR_UNARY_CAST:
1807 return is_constant_pointer(expression->unary.value);
1809 return EXPR_CLASS_VARIABLE;
1813 static expression_classification_t is_object_with_constant_address(const expression_t *expression)
1815 switch (expression->kind) {
1817 expression_t *compound = expression->select.compound;
1818 type_t *compound_type = compound->base.type;
1819 compound_type = skip_typeref(compound_type);
1820 if (is_type_pointer(compound_type)) {
1821 return is_constant_pointer(compound);
1823 return is_object_with_constant_address(compound);
1827 case EXPR_ARRAY_ACCESS: {
1828 array_access_expression_t const* const array_access =
1829 &expression->array_access;
1830 expression_classification_t const idx_class = is_constant_expression(array_access->index);
1831 if (idx_class != EXPR_CLASS_CONSTANT)
1833 expression_classification_t const ref_addr = is_object_with_constant_address(array_access->array_ref);
1834 expression_classification_t const ref_ptr = is_constant_pointer(array_access->array_ref);
1835 return ref_addr > ref_ptr ? ref_addr : ref_ptr;
1838 case EXPR_UNARY_DEREFERENCE:
1839 return is_constant_pointer(expression->unary.value);
1842 return EXPR_CLASS_ERROR;
1845 return EXPR_CLASS_VARIABLE;
1849 expression_classification_t is_constant_expression(const expression_t *expression)
1851 switch (expression->kind) {
1853 case EXPR_CLASSIFY_TYPE:
1856 case EXPR_BUILTIN_CONSTANT_P:
1857 case EXPR_BUILTIN_TYPES_COMPATIBLE_P:
1858 case EXPR_REFERENCE_ENUM_VALUE:
1859 return EXPR_CLASS_CONSTANT;
1862 type_t *const type = skip_typeref(expression->typeprop.type);
1864 !is_type_array(type) || !type->array.is_vla ? EXPR_CLASS_CONSTANT :
1865 EXPR_CLASS_VARIABLE;
1868 case EXPR_STRING_LITERAL:
1869 case EXPR_WIDE_STRING_LITERAL:
1871 case EXPR_LABEL_ADDRESS:
1876 case EXPR_STATEMENT:
1877 case EXPR_UNARY_POSTFIX_INCREMENT:
1878 case EXPR_UNARY_POSTFIX_DECREMENT:
1879 case EXPR_UNARY_PREFIX_INCREMENT:
1880 case EXPR_UNARY_PREFIX_DECREMENT:
1881 case EXPR_UNARY_ASSUME: /* has VOID type */
1882 case EXPR_UNARY_DEREFERENCE:
1883 case EXPR_UNARY_DELETE:
1884 case EXPR_UNARY_DELETE_ARRAY:
1885 case EXPR_UNARY_THROW:
1886 case EXPR_BINARY_ASSIGN:
1887 case EXPR_BINARY_MUL_ASSIGN:
1888 case EXPR_BINARY_DIV_ASSIGN:
1889 case EXPR_BINARY_MOD_ASSIGN:
1890 case EXPR_BINARY_ADD_ASSIGN:
1891 case EXPR_BINARY_SUB_ASSIGN:
1892 case EXPR_BINARY_SHIFTLEFT_ASSIGN:
1893 case EXPR_BINARY_SHIFTRIGHT_ASSIGN:
1894 case EXPR_BINARY_BITWISE_AND_ASSIGN:
1895 case EXPR_BINARY_BITWISE_XOR_ASSIGN:
1896 case EXPR_BINARY_BITWISE_OR_ASSIGN:
1897 case EXPR_BINARY_COMMA:
1898 case EXPR_ARRAY_ACCESS:
1899 return EXPR_CLASS_VARIABLE;
1901 case EXPR_REFERENCE: {
1902 type_t *const type = skip_typeref(expression->base.type);
1903 return is_type_valid(type) ? EXPR_CLASS_VARIABLE : EXPR_CLASS_ERROR;
1906 case EXPR_UNARY_TAKE_ADDRESS:
1907 return is_object_with_constant_address(expression->unary.value);
1910 return is_builtin_const_call(expression);
1912 case EXPR_UNARY_NEGATE:
1913 case EXPR_UNARY_PLUS:
1914 case EXPR_UNARY_BITWISE_NEGATE:
1915 case EXPR_UNARY_NOT:
1916 return is_constant_expression(expression->unary.value);
1918 case EXPR_UNARY_CAST: {
1919 type_t *const type = skip_typeref(expression->base.type);
1920 if (is_type_scalar(type))
1921 return is_constant_expression(expression->unary.value);
1922 if (!is_type_valid(type))
1923 return EXPR_CLASS_ERROR;
1924 return EXPR_CLASS_VARIABLE;
1927 case EXPR_BINARY_ADD:
1928 case EXPR_BINARY_SUB:
1929 case EXPR_BINARY_MUL:
1930 case EXPR_BINARY_DIV:
1931 case EXPR_BINARY_MOD:
1932 case EXPR_BINARY_EQUAL:
1933 case EXPR_BINARY_NOTEQUAL:
1934 case EXPR_BINARY_LESS:
1935 case EXPR_BINARY_LESSEQUAL:
1936 case EXPR_BINARY_GREATER:
1937 case EXPR_BINARY_GREATEREQUAL:
1938 case EXPR_BINARY_BITWISE_AND:
1939 case EXPR_BINARY_BITWISE_OR:
1940 case EXPR_BINARY_BITWISE_XOR:
1941 case EXPR_BINARY_SHIFTLEFT:
1942 case EXPR_BINARY_SHIFTRIGHT:
1943 case EXPR_BINARY_ISGREATER:
1944 case EXPR_BINARY_ISGREATEREQUAL:
1945 case EXPR_BINARY_ISLESS:
1946 case EXPR_BINARY_ISLESSEQUAL:
1947 case EXPR_BINARY_ISLESSGREATER:
1948 case EXPR_BINARY_ISUNORDERED: {
1949 expression_classification_t const l = is_constant_expression(expression->binary.left);
1950 expression_classification_t const r = is_constant_expression(expression->binary.right);
1951 return l < r ? l : r;
1954 case EXPR_BINARY_LOGICAL_AND: {
1955 expression_t const *const left = expression->binary.left;
1956 expression_classification_t const lclass = is_constant_expression(left);
1957 if (lclass != EXPR_CLASS_CONSTANT)
1959 if (!fold_constant_to_bool(left))
1960 return EXPR_CLASS_CONSTANT;
1961 return is_constant_expression(expression->binary.right);
1964 case EXPR_BINARY_LOGICAL_OR: {
1965 expression_t const *const left = expression->binary.left;
1966 expression_classification_t const lclass = is_constant_expression(left);
1967 if (lclass != EXPR_CLASS_CONSTANT)
1969 if (fold_constant_to_bool(left))
1970 return EXPR_CLASS_CONSTANT;
1971 return is_constant_expression(expression->binary.right);
1974 case EXPR_COMPOUND_LITERAL:
1975 return is_constant_initializer(expression->compound_literal.initializer);
1977 case EXPR_CONDITIONAL: {
1978 expression_t *const condition = expression->conditional.condition;
1979 expression_classification_t const cclass = is_constant_expression(condition);
1980 if (cclass != EXPR_CLASS_CONSTANT)
1983 if (fold_constant_to_bool(condition)) {
1984 expression_t const *const t = expression->conditional.true_expression;
1985 return t == NULL ? EXPR_CLASS_CONSTANT : is_constant_expression(t);
1987 return is_constant_expression(expression->conditional.false_expression);
1992 return EXPR_CLASS_ERROR;
1994 panic("invalid expression found (is constant expression)");
1999 obstack_init(&ast_obstack);
2004 obstack_free(&ast_obstack, NULL);