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_INVALID] = PREC_PRIMARY,
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);
316 call_argument_t *argument = call->arguments;
318 while (argument != NULL) {
324 print_assignment_expression(argument->expression);
326 argument = argument->next;
332 * Prints a binary expression.
334 * @param binexpr the binary expression
336 static void print_binary_expression(const binary_expression_t *binexpr)
338 unsigned prec = get_expression_precedence(binexpr->base.kind);
339 int r2l = right_to_left(prec);
341 print_expression_prec(binexpr->left, prec + r2l);
343 switch (binexpr->base.kind) {
344 case EXPR_BINARY_COMMA: op = ", "; break;
345 case EXPR_BINARY_ASSIGN: op = " = "; break;
346 case EXPR_BINARY_ADD: op = " + "; break;
347 case EXPR_BINARY_SUB: op = " - "; break;
348 case EXPR_BINARY_MUL: op = " * "; break;
349 case EXPR_BINARY_MOD: op = " % "; break;
350 case EXPR_BINARY_DIV: op = " / "; break;
351 case EXPR_BINARY_BITWISE_OR: op = " | "; break;
352 case EXPR_BINARY_BITWISE_AND: op = " & "; break;
353 case EXPR_BINARY_BITWISE_XOR: op = " ^ "; break;
354 case EXPR_BINARY_LOGICAL_OR: op = " || "; break;
355 case EXPR_BINARY_LOGICAL_AND: op = " && "; break;
356 case EXPR_BINARY_NOTEQUAL: op = " != "; break;
357 case EXPR_BINARY_EQUAL: op = " == "; break;
358 case EXPR_BINARY_LESS: op = " < "; break;
359 case EXPR_BINARY_LESSEQUAL: op = " <= "; break;
360 case EXPR_BINARY_GREATER: op = " > "; break;
361 case EXPR_BINARY_GREATEREQUAL: op = " >= "; break;
362 case EXPR_BINARY_SHIFTLEFT: op = " << "; break;
363 case EXPR_BINARY_SHIFTRIGHT: op = " >> "; break;
365 case EXPR_BINARY_ADD_ASSIGN: op = " += "; break;
366 case EXPR_BINARY_SUB_ASSIGN: op = " -= "; break;
367 case EXPR_BINARY_MUL_ASSIGN: op = " *= "; break;
368 case EXPR_BINARY_MOD_ASSIGN: op = " %= "; break;
369 case EXPR_BINARY_DIV_ASSIGN: op = " /= "; break;
370 case EXPR_BINARY_BITWISE_OR_ASSIGN: op = " |= "; break;
371 case EXPR_BINARY_BITWISE_AND_ASSIGN: op = " &= "; break;
372 case EXPR_BINARY_BITWISE_XOR_ASSIGN: op = " ^= "; break;
373 case EXPR_BINARY_SHIFTLEFT_ASSIGN: op = " <<= "; break;
374 case EXPR_BINARY_SHIFTRIGHT_ASSIGN: op = " >>= "; break;
375 default: panic("invalid binexpression found");
378 print_expression_prec(binexpr->right, prec + 1 - r2l);
382 * Prints an unary expression.
384 * @param unexpr the unary expression
386 static void print_unary_expression(const unary_expression_t *unexpr)
388 unsigned prec = get_expression_precedence(unexpr->base.kind);
389 switch (unexpr->base.kind) {
390 case EXPR_UNARY_NEGATE: print_string("-"); break;
391 case EXPR_UNARY_PLUS: print_string("+"); break;
392 case EXPR_UNARY_NOT: print_string("!"); break;
393 case EXPR_UNARY_BITWISE_NEGATE: print_string("~"); break;
394 case EXPR_UNARY_PREFIX_INCREMENT: print_string("++"); break;
395 case EXPR_UNARY_PREFIX_DECREMENT: print_string("--"); break;
396 case EXPR_UNARY_DEREFERENCE: print_string("*"); break;
397 case EXPR_UNARY_TAKE_ADDRESS: print_string("&"); break;
398 case EXPR_UNARY_DELETE: print_string("delete "); break;
399 case EXPR_UNARY_DELETE_ARRAY: print_string("delete [] "); break;
401 case EXPR_UNARY_POSTFIX_INCREMENT:
402 print_expression_prec(unexpr->value, prec);
405 case EXPR_UNARY_POSTFIX_DECREMENT:
406 print_expression_prec(unexpr->value, prec);
409 case EXPR_UNARY_CAST:
411 print_type(unexpr->base.type);
414 case EXPR_UNARY_ASSUME:
415 print_string("__assume(");
416 print_assignment_expression(unexpr->value);
420 case EXPR_UNARY_THROW:
421 if (unexpr->value == NULL) {
422 print_string("throw");
425 print_string("throw ");
429 panic("invalid unary expression found");
431 print_expression_prec(unexpr->value, prec);
435 * Prints a reference expression.
437 * @param ref the reference expression
439 static void print_reference_expression(const reference_expression_t *ref)
441 print_string(ref->entity->base.symbol->string);
445 * Prints a label address expression.
447 * @param ref the reference expression
449 static void print_label_address_expression(const label_address_expression_t *le)
451 print_format("&&%s", le->label->base.symbol->string);
455 * Prints an array expression.
457 * @param expression the array expression
459 static void print_array_expression(const array_access_expression_t *expression)
461 if (!expression->flipped) {
462 print_expression_prec(expression->array_ref, PREC_POSTFIX);
464 print_expression(expression->index);
467 print_expression_prec(expression->index, PREC_POSTFIX);
469 print_expression(expression->array_ref);
475 * Prints a typeproperty expression (sizeof or __alignof__).
477 * @param expression the type property expression
479 static void print_typeprop_expression(const typeprop_expression_t *expression)
481 if (expression->base.kind == EXPR_SIZEOF) {
482 print_string("sizeof");
484 assert(expression->base.kind == EXPR_ALIGNOF);
485 print_string("__alignof__");
487 if (expression->tp_expression != NULL) {
488 /* PREC_TOP: always print the '()' here, sizeof x is right but unusual */
489 print_expression_prec(expression->tp_expression, PREC_TOP);
492 print_type(expression->type);
498 * Prints a builtin constant expression.
500 * @param expression the builtin constant expression
502 static void print_builtin_constant(const builtin_constant_expression_t *expression)
504 print_string("__builtin_constant_p(");
505 print_assignment_expression(expression->value);
510 * Prints a builtin types compatible expression.
512 * @param expression the builtin types compatible expression
514 static void print_builtin_types_compatible(
515 const builtin_types_compatible_expression_t *expression)
517 print_string("__builtin_types_compatible_p(");
518 print_type(expression->left);
520 print_type(expression->right);
525 * Prints a conditional expression.
527 * @param expression the conditional expression
529 static void print_conditional(const conditional_expression_t *expression)
531 print_expression_prec(expression->condition, PREC_LOGICAL_OR);
532 if (expression->true_expression != NULL) {
534 print_expression_prec(expression->true_expression, PREC_EXPRESSION);
537 print_string(" ?: ");
539 precedence_t prec = c_mode & _CXX ? PREC_ASSIGNMENT : PREC_CONDITIONAL;
540 print_expression_prec(expression->false_expression, prec);
544 * Prints a va_start expression.
546 * @param expression the va_start expression
548 static void print_va_start(const va_start_expression_t *const expression)
550 print_string("__builtin_va_start(");
551 print_assignment_expression(expression->ap);
553 print_string(expression->parameter->base.base.symbol->string);
558 * Prints a va_arg expression.
560 * @param expression the va_arg expression
562 static void print_va_arg(const va_arg_expression_t *expression)
564 print_string("__builtin_va_arg(");
565 print_assignment_expression(expression->ap);
567 print_type(expression->base.type);
572 * Prints a va_copy expression.
574 * @param expression the va_copy expression
576 static void print_va_copy(const va_copy_expression_t *expression)
578 print_string("__builtin_va_copy(");
579 print_assignment_expression(expression->dst);
581 print_assignment_expression(expression->src);
586 * Prints a select expression (. or ->).
588 * @param expression the select expression
590 static void print_select(const select_expression_t *expression)
592 print_expression_prec(expression->compound, PREC_POSTFIX);
593 /* do not print anything for anonymous struct/union selects
594 * FIXME: if the anonymous select was a '->' this will print '.'
596 if (expression->compound_entry->base.symbol == NULL)
599 if (is_type_pointer(skip_typeref(expression->compound->base.type))) {
604 print_string(expression->compound_entry->base.symbol->string);
608 * Prints a type classify expression.
610 * @param expr the type classify expression
612 static void print_classify_type_expression(
613 const classify_type_expression_t *const expr)
615 print_string("__builtin_classify_type(");
616 print_assignment_expression(expr->type_expression);
621 * Prints a designator.
623 * @param designator the designator
625 static void print_designator(const designator_t *designator)
627 for ( ; designator != NULL; designator = designator->next) {
628 if (designator->symbol == NULL) {
630 print_expression(designator->array_index);
634 print_string(designator->symbol->string);
640 * Prints an offsetof expression.
642 * @param expression the offset expression
644 static void print_offsetof_expression(const offsetof_expression_t *expression)
646 print_string("__builtin_offsetof(");
647 print_type(expression->type);
649 print_designator(expression->designator);
654 * Prints a statement expression.
656 * @param expression the statement expression
658 static void print_statement_expression(const statement_expression_t *expression)
661 print_statement(expression->statement);
666 * Prints an expression with parenthesis if needed.
668 * @param expression the expression to print
669 * @param top_prec the precedence of the user of this expression.
671 static void print_expression_prec(const expression_t *expression, unsigned top_prec)
673 if (expression->kind == EXPR_UNARY_CAST
674 && expression->base.implicit && !print_implicit_casts) {
675 expression = expression->unary.value;
679 expression->base.parenthesized ||
680 (print_parenthesis && top_prec != PREC_BOTTOM) ||
681 top_prec > get_expression_precedence(expression->base.kind);
685 switch (expression->kind) {
687 print_string("$error$");
689 case EXPR_WIDE_STRING_LITERAL:
690 case EXPR_STRING_LITERAL:
691 print_string_literal(&expression->string_literal);
694 print_literal(&expression->literal);
697 print_funcname(&expression->funcname);
699 case EXPR_COMPOUND_LITERAL:
700 print_compound_literal(&expression->compound_literal);
703 print_call_expression(&expression->call);
706 print_binary_expression(&expression->binary);
709 case EXPR_REFERENCE_ENUM_VALUE:
710 print_reference_expression(&expression->reference);
712 case EXPR_ARRAY_ACCESS:
713 print_array_expression(&expression->array_access);
715 case EXPR_LABEL_ADDRESS:
716 print_label_address_expression(&expression->label_address);
719 print_unary_expression(&expression->unary);
723 print_typeprop_expression(&expression->typeprop);
725 case EXPR_BUILTIN_CONSTANT_P:
726 print_builtin_constant(&expression->builtin_constant);
728 case EXPR_BUILTIN_TYPES_COMPATIBLE_P:
729 print_builtin_types_compatible(&expression->builtin_types_compatible);
731 case EXPR_CONDITIONAL:
732 print_conditional(&expression->conditional);
735 print_va_start(&expression->va_starte);
738 print_va_arg(&expression->va_arge);
741 print_va_copy(&expression->va_copye);
744 print_select(&expression->select);
746 case EXPR_CLASSIFY_TYPE:
747 print_classify_type_expression(&expression->classify_type);
750 print_offsetof_expression(&expression->offsetofe);
753 print_statement_expression(&expression->statement);
756 panic("invalid expression found");
761 print_format("some expression of type %d", (int)expression->kind);
770 * Print an compound statement.
772 * @param block the compound statement
774 static void print_compound_statement(const compound_statement_t *block)
779 statement_t *statement = block->statements;
780 while (statement != NULL) {
781 if (statement->base.kind == STATEMENT_CASE_LABEL)
783 if (statement->kind != STATEMENT_LABEL)
785 print_statement(statement);
787 statement = statement->base.next;
791 print_string(block->stmt_expr ? "}" : "}\n");
795 * Print a return statement.
797 * @param statement the return statement
799 static void print_return_statement(const return_statement_t *statement)
801 expression_t const *const val = statement->value;
803 print_string("return ");
804 print_expression(val);
807 print_string("return;\n");
812 * Print an expression statement.
814 * @param statement the expression statement
816 static void print_expression_statement(const expression_statement_t *statement)
818 print_expression(statement->expression);
823 * Print a goto statement.
825 * @param statement the goto statement
827 static void print_goto_statement(const goto_statement_t *statement)
829 print_string("goto ");
830 if (statement->expression != NULL) {
832 print_expression(statement->expression);
834 print_string(statement->label->base.symbol->string);
840 * Print a label statement.
842 * @param statement the label statement
844 static void print_label_statement(const label_statement_t *statement)
846 print_format("%s:\n", statement->label->base.symbol->string);
848 print_statement(statement->statement);
852 * Print an if statement.
854 * @param statement the if statement
856 static void print_if_statement(const if_statement_t *statement)
858 print_string("if (");
859 print_expression(statement->condition);
861 print_statement(statement->true_statement);
863 if (statement->false_statement != NULL) {
865 print_string("else ");
866 print_statement(statement->false_statement);
871 * Print a switch statement.
873 * @param statement the switch statement
875 static void print_switch_statement(const switch_statement_t *statement)
877 print_string("switch (");
878 print_expression(statement->expression);
880 print_statement(statement->body);
884 * Print a case label (including the default label).
886 * @param statement the case label statement
888 static void print_case_label(const case_label_statement_t *statement)
890 if (statement->expression == NULL) {
891 print_string("default:\n");
893 print_string("case ");
894 print_expression(statement->expression);
895 if (statement->end_range != NULL) {
896 print_string(" ... ");
897 print_expression(statement->end_range);
902 if (statement->statement != NULL) {
903 if (statement->statement->base.kind == STATEMENT_CASE_LABEL) {
907 print_statement(statement->statement);
911 static void print_typedef(const entity_t *entity)
913 print_string("typedef ");
914 print_type_ext(entity->typedefe.type, entity->base.symbol, NULL);
919 * returns true if the entity is a compiler generated one and has no real
920 * correspondenc in the source file
922 static bool is_generated_entity(const entity_t *entity)
924 if (entity->kind == ENTITY_TYPEDEF)
925 return entity->typedefe.builtin;
927 if (is_declaration(entity))
928 return entity->declaration.implicit;
934 * Print a declaration statement.
936 * @param statement the statement
938 static void print_declaration_statement(
939 const declaration_statement_t *statement)
942 entity_t *entity = statement->declarations_begin;
943 if (entity == NULL) {
944 print_string("/* empty declaration statement */\n");
948 entity_t *const end = statement->declarations_end->base.next;
949 for (; entity != end; entity = entity->base.next) {
950 if (entity->kind == ENTITY_ENUM_VALUE)
952 if (is_generated_entity(entity))
961 print_entity(entity);
967 * Print a while statement.
969 * @param statement the statement
971 static void print_while_statement(const while_statement_t *statement)
973 print_string("while (");
974 print_expression(statement->condition);
976 print_statement(statement->body);
980 * Print a do-while statement.
982 * @param statement the statement
984 static void print_do_while_statement(const do_while_statement_t *statement)
987 print_statement(statement->body);
989 print_string("while (");
990 print_expression(statement->condition);
991 print_string(");\n");
995 * Print a for statement.
997 * @param statement the statement
999 static void print_for_statement(const for_statement_t *statement)
1001 print_string("for (");
1002 if (statement->initialisation != NULL) {
1003 print_expression(statement->initialisation);
1006 entity_t const *entity = statement->scope.entities;
1007 for (; entity != NULL; entity = entity->base.next) {
1008 if (is_generated_entity(entity))
1010 /* FIXME display of multiple declarations is wrong */
1011 print_declaration(entity);
1014 if (statement->condition != NULL) {
1016 print_expression(statement->condition);
1019 if (statement->step != NULL) {
1021 print_expression(statement->step);
1024 print_statement(statement->body);
1028 * Print assembler arguments.
1030 * @param arguments the arguments
1032 static void print_asm_arguments(asm_argument_t *arguments)
1034 asm_argument_t *argument = arguments;
1035 for (; argument != NULL; argument = argument->next) {
1036 if (argument != arguments)
1039 if (argument->symbol) {
1040 print_format("[%s] ", argument->symbol->string);
1042 print_quoted_string(&argument->constraints, '"', 1);
1044 print_expression(argument->expression);
1050 * Print assembler clobbers.
1052 * @param clobbers the clobbers
1054 static void print_asm_clobbers(asm_clobber_t *clobbers)
1056 asm_clobber_t *clobber = clobbers;
1057 for (; clobber != NULL; clobber = clobber->next) {
1058 if (clobber != clobbers)
1061 print_quoted_string(&clobber->clobber, '"', 1);
1066 * Print an assembler statement.
1068 * @param statement the statement
1070 static void print_asm_statement(const asm_statement_t *statement)
1072 print_string("asm ");
1073 if (statement->is_volatile) {
1074 print_string("volatile ");
1077 print_quoted_string(&statement->asm_text, '"', 1);
1078 if (statement->outputs == NULL &&
1079 statement->inputs == NULL &&
1080 statement->clobbers == NULL)
1081 goto end_of_print_asm_statement;
1083 print_string(" : ");
1084 print_asm_arguments(statement->outputs);
1085 if (statement->inputs == NULL && statement->clobbers == NULL)
1086 goto end_of_print_asm_statement;
1088 print_string(" : ");
1089 print_asm_arguments(statement->inputs);
1090 if (statement->clobbers == NULL)
1091 goto end_of_print_asm_statement;
1093 print_string(" : ");
1094 print_asm_clobbers(statement->clobbers);
1096 end_of_print_asm_statement:
1097 print_string(");\n");
1101 * Print a microsoft __try statement.
1103 * @param statement the statement
1105 static void print_ms_try_statement(const ms_try_statement_t *statement)
1107 print_string("__try ");
1108 print_statement(statement->try_statement);
1110 if (statement->except_expression != NULL) {
1111 print_string("__except(");
1112 print_expression(statement->except_expression);
1115 print_string("__finally ");
1117 print_statement(statement->final_statement);
1121 * Print a microsoft __leave statement.
1123 * @param statement the statement
1125 static void print_leave_statement(const leave_statement_t *statement)
1128 print_string("__leave;\n");
1132 * Print a statement.
1134 * @param statement the statement
1136 void print_statement(const statement_t *statement)
1138 switch (statement->kind) {
1139 case STATEMENT_EMPTY:
1140 print_string(";\n");
1142 case STATEMENT_COMPOUND:
1143 print_compound_statement(&statement->compound);
1145 case STATEMENT_RETURN:
1146 print_return_statement(&statement->returns);
1148 case STATEMENT_EXPRESSION:
1149 print_expression_statement(&statement->expression);
1151 case STATEMENT_LABEL:
1152 print_label_statement(&statement->label);
1154 case STATEMENT_GOTO:
1155 print_goto_statement(&statement->gotos);
1157 case STATEMENT_CONTINUE:
1158 print_string("continue;\n");
1160 case STATEMENT_BREAK:
1161 print_string("break;\n");
1164 print_if_statement(&statement->ifs);
1166 case STATEMENT_SWITCH:
1167 print_switch_statement(&statement->switchs);
1169 case STATEMENT_CASE_LABEL:
1170 print_case_label(&statement->case_label);
1172 case STATEMENT_DECLARATION:
1173 print_declaration_statement(&statement->declaration);
1175 case STATEMENT_WHILE:
1176 print_while_statement(&statement->whiles);
1178 case STATEMENT_DO_WHILE:
1179 print_do_while_statement(&statement->do_while);
1182 print_for_statement(&statement->fors);
1185 print_asm_statement(&statement->asms);
1187 case STATEMENT_MS_TRY:
1188 print_ms_try_statement(&statement->ms_try);
1190 case STATEMENT_LEAVE:
1191 print_leave_statement(&statement->leave);
1193 case STATEMENT_INVALID:
1194 print_string("$invalid statement$\n");
1200 * Print a storage class.
1202 * @param storage_class the storage class
1204 static void print_storage_class(storage_class_tag_t storage_class)
1206 switch (storage_class) {
1207 case STORAGE_CLASS_NONE: return;
1208 case STORAGE_CLASS_TYPEDEF: print_string("typedef "); return;
1209 case STORAGE_CLASS_EXTERN: print_string("extern "); return;
1210 case STORAGE_CLASS_STATIC: print_string("static "); return;
1211 case STORAGE_CLASS_AUTO: print_string("auto "); return;
1212 case STORAGE_CLASS_REGISTER: print_string("register "); return;
1214 panic("invalid storage class");
1218 * Print an initializer.
1220 * @param initializer the initializer
1222 void print_initializer(const initializer_t *initializer)
1224 if (initializer == NULL) {
1229 switch (initializer->kind) {
1230 case INITIALIZER_VALUE: {
1231 const initializer_value_t *value = &initializer->value;
1232 print_assignment_expression(value->value);
1235 case INITIALIZER_LIST: {
1236 assert(initializer->kind == INITIALIZER_LIST);
1238 const initializer_list_t *list = &initializer->list;
1240 for (size_t i = 0 ; i < list->len; ++i) {
1241 const initializer_t *sub_init = list->initializers[i];
1242 print_initializer(list->initializers[i]);
1243 if (i < list->len-1) {
1244 if (sub_init == NULL || sub_init->kind != INITIALIZER_DESIGNATOR)
1251 case INITIALIZER_STRING:
1252 print_quoted_string(&initializer->string.string, '"', 1);
1254 case INITIALIZER_WIDE_STRING:
1255 print_quoted_string(&initializer->string.string, '"', 1);
1257 case INITIALIZER_DESIGNATOR:
1258 print_designator(initializer->designator.designator);
1259 print_string(" = ");
1263 panic("invalid initializer kind found");
1268 * Print microsoft extended declaration modifiers.
1270 static void print_ms_modifiers(const declaration_t *declaration)
1272 if ((c_mode & _MS) == 0)
1275 decl_modifiers_t modifiers = declaration->modifiers;
1277 bool ds_shown = false;
1278 const char *next = "(";
1280 if (declaration->base.kind == ENTITY_VARIABLE) {
1281 variable_t *variable = (variable_t*)declaration;
1282 if (variable->alignment != 0
1283 || variable->get_property_sym != NULL
1284 || variable->put_property_sym != NULL) {
1286 print_string("__declspec");
1290 if (variable->alignment != 0) {
1291 print_string(next); next = ", "; print_format("align(%u)", variable->alignment);
1293 if (variable->get_property_sym != NULL
1294 || variable->put_property_sym != NULL) {
1296 print_string(next); next = ", "; print_string("property(");
1297 if (variable->get_property_sym != NULL) {
1298 print_format("get=%s", variable->get_property_sym->string);
1301 if (variable->put_property_sym != NULL)
1302 print_format("%sput=%s", comma, variable->put_property_sym->string);
1308 /* DM_FORCEINLINE handled outside. */
1309 if ((modifiers & ~DM_FORCEINLINE) != 0) {
1311 print_string("__declspec");
1314 if (modifiers & DM_DLLIMPORT) {
1315 print_string(next); next = ", "; print_string("dllimport");
1317 if (modifiers & DM_DLLEXPORT) {
1318 print_string(next); next = ", "; print_string("dllexport");
1320 if (modifiers & DM_THREAD) {
1321 print_string(next); next = ", "; print_string("thread");
1323 if (modifiers & DM_NAKED) {
1324 print_string(next); next = ", "; print_string("naked");
1326 if (modifiers & DM_THREAD) {
1327 print_string(next); next = ", "; print_string("thread");
1329 if (modifiers & DM_SELECTANY) {
1330 print_string(next); next = ", "; print_string("selectany");
1332 if (modifiers & DM_NOTHROW) {
1333 print_string(next); next = ", "; print_string("nothrow");
1335 if (modifiers & DM_NORETURN) {
1336 print_string(next); next = ", "; print_string("noreturn");
1338 if (modifiers & DM_NOINLINE) {
1339 print_string(next); next = ", "; print_string("noinline");
1341 if (modifiers & DM_DEPRECATED) {
1342 print_string(next); next = ", "; print_string("deprecated");
1343 if (declaration->deprecated_string != NULL)
1344 print_format("(\"%s\")",
1345 declaration->deprecated_string);
1347 if (modifiers & DM_RESTRICT) {
1348 print_string(next); next = ", "; print_string("restrict");
1350 if (modifiers & DM_NOALIAS) {
1351 print_string(next); next = ", "; print_string("noalias");
1360 static void print_scope(const scope_t *scope)
1362 const entity_t *entity = scope->entities;
1363 for ( ; entity != NULL; entity = entity->base.next) {
1365 print_entity(entity);
1370 static void print_namespace(const namespace_t *namespace)
1372 print_string("namespace ");
1373 if (namespace->base.symbol != NULL) {
1374 print_string(namespace->base.symbol->string);
1378 print_string("{\n");
1381 print_scope(&namespace->members);
1385 print_string("}\n");
1389 * Print a variable or function declaration
1391 void print_declaration(const entity_t *entity)
1393 assert(is_declaration(entity));
1394 const declaration_t *declaration = &entity->declaration;
1396 print_storage_class((storage_class_tag_t)declaration->declared_storage_class);
1397 if (entity->kind == ENTITY_FUNCTION) {
1398 function_t *function = (function_t*)declaration;
1399 if (function->is_inline) {
1400 if (declaration->modifiers & DM_FORCEINLINE) {
1401 print_string("__forceinline ");
1402 } else if (declaration->modifiers & DM_MICROSOFT_INLINE) {
1403 print_string("__inline ");
1405 print_string("inline ");
1409 //print_ms_modifiers(declaration);
1410 switch (entity->kind) {
1411 case ENTITY_FUNCTION:
1412 print_type_ext(entity->declaration.type, entity->base.symbol,
1413 &entity->function.parameters);
1415 if (entity->function.statement != NULL) {
1418 print_statement(entity->function.statement);
1423 case ENTITY_VARIABLE:
1424 if (entity->variable.thread_local)
1425 print_string("__thread ");
1426 print_type_ext(declaration->type, declaration->base.symbol, NULL);
1427 if (entity->variable.initializer != NULL) {
1428 print_string(" = ");
1429 print_initializer(entity->variable.initializer);
1433 case ENTITY_COMPOUND_MEMBER:
1434 print_type_ext(declaration->type, declaration->base.symbol, NULL);
1435 if (entity->compound_member.bitfield) {
1436 print_format(" : %u", entity->compound_member.bit_size);
1441 print_type_ext(declaration->type, declaration->base.symbol, NULL);
1448 * Prints an expression.
1450 * @param expression the expression
1452 void print_expression(const expression_t *expression)
1454 print_expression_prec(expression, PREC_BOTTOM);
1458 * Print a declaration.
1460 * @param declaration the declaration
1462 void print_entity(const entity_t *entity)
1464 if (entity->base.namespc != NAMESPACE_NORMAL && entity->base.symbol == NULL)
1467 switch ((entity_kind_tag_t)entity->kind) {
1468 case ENTITY_VARIABLE:
1469 case ENTITY_PARAMETER:
1470 case ENTITY_COMPOUND_MEMBER:
1471 case ENTITY_FUNCTION:
1472 print_declaration(entity);
1474 case ENTITY_TYPEDEF:
1475 print_typedef(entity);
1479 print_string("class ");
1480 print_string(entity->base.symbol->string);
1481 print_string("; /* TODO */\n");
1484 print_string("struct ");
1485 goto print_compound;
1487 print_string("union ");
1489 print_string(entity->base.symbol->string);
1490 if (entity->compound.complete) {
1492 print_compound_definition(&entity->compound);
1497 print_string("enum ");
1498 print_string(entity->base.symbol->string);
1500 print_enum_definition(&entity->enume);
1503 case ENTITY_NAMESPACE:
1504 print_namespace(&entity->namespacee);
1506 case ENTITY_LOCAL_LABEL:
1507 print_string("__label__ ");
1508 print_string(entity->base.symbol->string);
1512 case ENTITY_ENUM_VALUE:
1513 panic("print_entity used on unexpected entity type");
1514 case ENTITY_INVALID:
1517 panic("Invalid entity type encountered");
1521 * Print the AST of a translation unit.
1523 * @param unit the translation unit
1525 void print_ast(const translation_unit_t *unit)
1527 entity_t *entity = unit->scope.entities;
1528 for ( ; entity != NULL; entity = entity->base.next) {
1529 if (entity->kind == ENTITY_ENUM_VALUE)
1531 if (entity->base.namespc != NAMESPACE_NORMAL
1532 && entity->base.symbol == NULL)
1534 if (is_generated_entity(entity))
1538 print_entity(entity);
1543 expression_classification_t is_constant_initializer(const initializer_t *initializer)
1545 switch (initializer->kind) {
1546 case INITIALIZER_STRING:
1547 case INITIALIZER_WIDE_STRING:
1548 case INITIALIZER_DESIGNATOR:
1549 return EXPR_CLASS_CONSTANT;
1551 case INITIALIZER_VALUE:
1552 return is_constant_expression(initializer->value.value);
1554 case INITIALIZER_LIST: {
1555 expression_classification_t all = EXPR_CLASS_CONSTANT;
1556 for (size_t i = 0; i < initializer->list.len; ++i) {
1557 initializer_t *sub_initializer = initializer->list.initializers[i];
1558 expression_classification_t const cur = is_constant_initializer(sub_initializer);
1566 panic("invalid initializer kind found");
1570 * Checks if an expression references an object with a constant/known location
1571 * to the linker. Example:
1572 * - "x", "*&x" with x being a global variable. The value of x need not be
1573 * constant but the address of x is.
1574 * - "a.b.c" when a has a constant/known location to the linker
1576 static expression_classification_t is_object_with_linker_constant_address(
1577 const expression_t *expression)
1579 switch (expression->kind) {
1580 case EXPR_UNARY_DEREFERENCE:
1581 return is_linker_constant(expression->unary.value);
1584 type_t *base_type = skip_typeref(expression->select.compound->base.type);
1585 if (is_type_pointer(base_type)) {
1587 return is_linker_constant(expression->select.compound);
1589 return is_object_with_linker_constant_address(expression->select.compound);
1593 case EXPR_ARRAY_ACCESS: {
1594 expression_classification_t const ref = is_linker_constant(expression->array_access.array_ref);
1595 expression_classification_t const idx = is_constant_expression(expression->array_access.index);
1596 return ref < idx ? ref : idx;
1599 case EXPR_REFERENCE: {
1600 entity_t *entity = expression->reference.entity;
1601 if (!is_declaration(entity))
1602 return EXPR_CLASS_VARIABLE;
1604 switch ((storage_class_tag_t)entity->declaration.storage_class) {
1605 case STORAGE_CLASS_NONE:
1606 case STORAGE_CLASS_EXTERN:
1607 case STORAGE_CLASS_STATIC:
1609 entity->kind != ENTITY_VARIABLE ||
1610 !entity->variable.thread_local ? EXPR_CLASS_CONSTANT :
1611 EXPR_CLASS_VARIABLE;
1613 case STORAGE_CLASS_REGISTER:
1614 case STORAGE_CLASS_TYPEDEF:
1615 case STORAGE_CLASS_AUTO:
1618 return EXPR_CLASS_VARIABLE;
1622 return EXPR_CLASS_ERROR;
1624 panic("invalid expression found");
1627 return EXPR_CLASS_VARIABLE;
1631 expression_classification_t is_linker_constant(const expression_t *expression)
1633 switch (expression->kind) {
1634 case EXPR_STRING_LITERAL:
1635 case EXPR_WIDE_STRING_LITERAL:
1637 case EXPR_LABEL_ADDRESS:
1638 return EXPR_CLASS_CONSTANT;
1640 case EXPR_UNARY_TAKE_ADDRESS:
1641 return is_object_with_linker_constant_address(expression->unary.value);
1643 case EXPR_UNARY_DEREFERENCE: {
1645 = revert_automatic_type_conversion(expression->unary.value);
1646 /* dereferencing a function is a NOP */
1647 if (is_type_function(real_type)) {
1648 return is_linker_constant(expression->unary.value);
1653 case EXPR_UNARY_CAST: {
1654 type_t *dest = skip_typeref(expression->base.type);
1655 if (!is_type_pointer(dest) && (
1656 dest->kind != TYPE_ATOMIC ||
1657 !(get_atomic_type_flags(dest->atomic.akind) & ATOMIC_TYPE_FLAG_INTEGER) ||
1658 get_atomic_type_size(dest->atomic.akind) < get_type_size(type_void_ptr)
1660 return EXPR_CLASS_VARIABLE;
1662 expression_classification_t const expr = is_constant_expression(expression->unary.value);
1663 expression_classification_t const addr = is_linker_constant(expression->unary.value);
1664 return expr > addr ? expr : addr;
1667 case EXPR_BINARY_ADD:
1668 case EXPR_BINARY_SUB: {
1669 expression_t *const left = expression->binary.left;
1670 expression_t *const right = expression->binary.right;
1671 type_t *const ltype = skip_typeref(left->base.type);
1672 type_t *const rtype = skip_typeref(right->base.type);
1674 if (is_type_pointer(ltype)) {
1675 expression_classification_t const l = is_linker_constant(left);
1676 expression_classification_t const r = is_constant_expression(right);
1677 return l < r ? l : r;
1678 } else if (is_type_pointer(rtype)) {
1679 expression_classification_t const l = is_constant_expression(left);
1680 expression_classification_t const r = is_linker_constant(right);
1681 return l < r ? l : r;
1682 } else if (!is_type_valid(ltype) || !is_type_valid(rtype)) {
1683 return EXPR_CLASS_ERROR;
1685 return EXPR_CLASS_VARIABLE;
1689 case EXPR_REFERENCE: {
1690 entity_t *entity = expression->reference.entity;
1691 if (!is_declaration(entity))
1692 return EXPR_CLASS_VARIABLE;
1694 type_t *type = skip_typeref(entity->declaration.type);
1695 if (is_type_function(type))
1696 return EXPR_CLASS_CONSTANT;
1697 if (is_type_array(type)) {
1698 return is_object_with_linker_constant_address(expression);
1700 /* Prevent stray errors */
1701 if (!is_type_valid(type))
1702 return EXPR_CLASS_ERROR;
1703 return EXPR_CLASS_VARIABLE;
1706 case EXPR_ARRAY_ACCESS: {
1707 type_t *const type =
1708 skip_typeref(revert_automatic_type_conversion(expression));
1709 if (!is_type_array(type))
1710 return EXPR_CLASS_VARIABLE;
1711 expression_classification_t const ref = is_linker_constant(expression->array_access.array_ref);
1712 expression_classification_t const idx = is_constant_expression(expression->array_access.index);
1713 return ref < idx ? ref : idx;
1716 case EXPR_CONDITIONAL: {
1717 expression_t *const c = expression->conditional.condition;
1718 expression_classification_t const cclass = is_constant_expression(c);
1719 if (cclass != EXPR_CLASS_CONSTANT)
1722 if (fold_constant_to_bool(c)) {
1723 expression_t const *const t = expression->conditional.true_expression;
1724 return is_linker_constant(t != NULL ? t : c);
1726 return is_linker_constant(expression->conditional.false_expression);
1731 entity_t *entity = expression->select.compound_entry;
1732 if (!is_declaration(entity))
1733 return EXPR_CLASS_VARIABLE;
1734 type_t *type = skip_typeref(entity->declaration.type);
1735 if (is_type_array(type)) {
1736 /* arrays automatically convert to their address */
1737 expression_t *compound = expression->select.compound;
1738 type_t *base_type = skip_typeref(compound->base.type);
1739 if (is_type_pointer(base_type)) {
1741 return is_linker_constant(compound);
1743 return is_object_with_linker_constant_address(compound);
1746 return EXPR_CLASS_VARIABLE;
1750 return EXPR_CLASS_ERROR;
1752 panic("invalid expression found");
1755 return EXPR_CLASS_VARIABLE;
1760 * Check if the given expression is a call to a builtin function
1761 * returning a constant result.
1763 static expression_classification_t is_builtin_const_call(const expression_t *expression)
1765 expression_t *function = expression->call.function;
1766 if (function->kind != EXPR_REFERENCE)
1767 return EXPR_CLASS_VARIABLE;
1768 reference_expression_t *ref = &function->reference;
1769 if (ref->entity->kind != ENTITY_FUNCTION)
1770 return EXPR_CLASS_VARIABLE;
1772 switch (ref->entity->function.btk) {
1773 case bk_gnu_builtin_huge_val:
1774 case bk_gnu_builtin_huge_valf:
1775 case bk_gnu_builtin_huge_vall:
1776 case bk_gnu_builtin_inf:
1777 case bk_gnu_builtin_inff:
1778 case bk_gnu_builtin_infl:
1779 case bk_gnu_builtin_nan:
1780 case bk_gnu_builtin_nanf:
1781 case bk_gnu_builtin_nanl:
1782 return EXPR_CLASS_CONSTANT;
1784 return EXPR_CLASS_VARIABLE;
1789 static expression_classification_t is_constant_pointer(const expression_t *expression)
1791 expression_classification_t const expr_class = is_constant_expression(expression);
1792 if (expr_class != EXPR_CLASS_VARIABLE)
1795 switch (expression->kind) {
1796 case EXPR_UNARY_CAST:
1797 return is_constant_pointer(expression->unary.value);
1799 return EXPR_CLASS_VARIABLE;
1803 static expression_classification_t is_object_with_constant_address(const expression_t *expression)
1805 switch (expression->kind) {
1807 expression_t *compound = expression->select.compound;
1808 type_t *compound_type = compound->base.type;
1809 compound_type = skip_typeref(compound_type);
1810 if (is_type_pointer(compound_type)) {
1811 return is_constant_pointer(compound);
1813 return is_object_with_constant_address(compound);
1817 case EXPR_ARRAY_ACCESS: {
1818 array_access_expression_t const* const array_access =
1819 &expression->array_access;
1820 expression_classification_t const idx_class = is_constant_expression(array_access->index);
1821 if (idx_class != EXPR_CLASS_CONSTANT)
1823 expression_classification_t const ref_addr = is_object_with_constant_address(array_access->array_ref);
1824 expression_classification_t const ref_ptr = is_constant_pointer(array_access->array_ref);
1825 return ref_addr > ref_ptr ? ref_addr : ref_ptr;
1828 case EXPR_UNARY_DEREFERENCE:
1829 return is_constant_pointer(expression->unary.value);
1832 return EXPR_CLASS_ERROR;
1834 panic("invalid expression found");
1837 return EXPR_CLASS_VARIABLE;
1841 expression_classification_t is_constant_expression(const expression_t *expression)
1843 switch (expression->kind) {
1845 case EXPR_CLASSIFY_TYPE:
1848 case EXPR_BUILTIN_CONSTANT_P:
1849 case EXPR_BUILTIN_TYPES_COMPATIBLE_P:
1850 case EXPR_REFERENCE_ENUM_VALUE:
1851 return EXPR_CLASS_CONSTANT;
1854 type_t *const type = skip_typeref(expression->typeprop.type);
1856 !is_type_array(type) || !type->array.is_vla ? EXPR_CLASS_CONSTANT :
1857 EXPR_CLASS_VARIABLE;
1860 case EXPR_STRING_LITERAL:
1861 case EXPR_WIDE_STRING_LITERAL:
1863 case EXPR_LABEL_ADDRESS:
1868 case EXPR_STATEMENT:
1869 case EXPR_UNARY_POSTFIX_INCREMENT:
1870 case EXPR_UNARY_POSTFIX_DECREMENT:
1871 case EXPR_UNARY_PREFIX_INCREMENT:
1872 case EXPR_UNARY_PREFIX_DECREMENT:
1873 case EXPR_UNARY_ASSUME: /* has VOID type */
1874 case EXPR_UNARY_DEREFERENCE:
1875 case EXPR_UNARY_DELETE:
1876 case EXPR_UNARY_DELETE_ARRAY:
1877 case EXPR_UNARY_THROW:
1878 case EXPR_BINARY_ASSIGN:
1879 case EXPR_BINARY_MUL_ASSIGN:
1880 case EXPR_BINARY_DIV_ASSIGN:
1881 case EXPR_BINARY_MOD_ASSIGN:
1882 case EXPR_BINARY_ADD_ASSIGN:
1883 case EXPR_BINARY_SUB_ASSIGN:
1884 case EXPR_BINARY_SHIFTLEFT_ASSIGN:
1885 case EXPR_BINARY_SHIFTRIGHT_ASSIGN:
1886 case EXPR_BINARY_BITWISE_AND_ASSIGN:
1887 case EXPR_BINARY_BITWISE_XOR_ASSIGN:
1888 case EXPR_BINARY_BITWISE_OR_ASSIGN:
1889 case EXPR_BINARY_COMMA:
1890 case EXPR_ARRAY_ACCESS:
1891 return EXPR_CLASS_VARIABLE;
1893 case EXPR_REFERENCE: {
1894 type_t *const type = skip_typeref(expression->base.type);
1895 return is_type_valid(type) ? EXPR_CLASS_VARIABLE : EXPR_CLASS_ERROR;
1898 case EXPR_UNARY_TAKE_ADDRESS:
1899 return is_object_with_constant_address(expression->unary.value);
1902 return is_builtin_const_call(expression);
1904 case EXPR_UNARY_NEGATE:
1905 case EXPR_UNARY_PLUS:
1906 case EXPR_UNARY_BITWISE_NEGATE:
1907 case EXPR_UNARY_NOT:
1908 return is_constant_expression(expression->unary.value);
1910 case EXPR_UNARY_CAST: {
1911 type_t *const type = skip_typeref(expression->base.type);
1912 if (is_type_scalar(type))
1913 return is_constant_expression(expression->unary.value);
1914 if (!is_type_valid(type))
1915 return EXPR_CLASS_ERROR;
1916 return EXPR_CLASS_VARIABLE;
1919 case EXPR_BINARY_ADD:
1920 case EXPR_BINARY_SUB:
1921 case EXPR_BINARY_MUL:
1922 case EXPR_BINARY_DIV:
1923 case EXPR_BINARY_MOD:
1924 case EXPR_BINARY_EQUAL:
1925 case EXPR_BINARY_NOTEQUAL:
1926 case EXPR_BINARY_LESS:
1927 case EXPR_BINARY_LESSEQUAL:
1928 case EXPR_BINARY_GREATER:
1929 case EXPR_BINARY_GREATEREQUAL:
1930 case EXPR_BINARY_BITWISE_AND:
1931 case EXPR_BINARY_BITWISE_OR:
1932 case EXPR_BINARY_BITWISE_XOR:
1933 case EXPR_BINARY_SHIFTLEFT:
1934 case EXPR_BINARY_SHIFTRIGHT:
1935 case EXPR_BINARY_ISGREATER:
1936 case EXPR_BINARY_ISGREATEREQUAL:
1937 case EXPR_BINARY_ISLESS:
1938 case EXPR_BINARY_ISLESSEQUAL:
1939 case EXPR_BINARY_ISLESSGREATER:
1940 case EXPR_BINARY_ISUNORDERED: {
1941 expression_classification_t const l = is_constant_expression(expression->binary.left);
1942 expression_classification_t const r = is_constant_expression(expression->binary.right);
1943 return l < r ? l : r;
1946 case EXPR_BINARY_LOGICAL_AND: {
1947 expression_t const *const left = expression->binary.left;
1948 expression_classification_t const lclass = is_constant_expression(left);
1949 if (lclass != EXPR_CLASS_CONSTANT)
1951 if (!fold_constant_to_bool(left))
1952 return EXPR_CLASS_CONSTANT;
1953 return is_constant_expression(expression->binary.right);
1956 case EXPR_BINARY_LOGICAL_OR: {
1957 expression_t const *const left = expression->binary.left;
1958 expression_classification_t const lclass = is_constant_expression(left);
1959 if (lclass != EXPR_CLASS_CONSTANT)
1961 if (fold_constant_to_bool(left))
1962 return EXPR_CLASS_CONSTANT;
1963 return is_constant_expression(expression->binary.right);
1966 case EXPR_COMPOUND_LITERAL:
1967 return is_constant_initializer(expression->compound_literal.initializer);
1969 case EXPR_CONDITIONAL: {
1970 expression_t *const condition = expression->conditional.condition;
1971 expression_classification_t const cclass = is_constant_expression(condition);
1972 if (cclass != EXPR_CLASS_CONSTANT)
1975 if (fold_constant_to_bool(condition)) {
1976 expression_t const *const t = expression->conditional.true_expression;
1977 return t == NULL ? EXPR_CLASS_CONSTANT : is_constant_expression(t);
1979 return is_constant_expression(expression->conditional.false_expression);
1984 return EXPR_CLASS_ERROR;
1988 panic("invalid expression found (is constant expression)");
1993 obstack_init(&ast_obstack);
1998 obstack_free(&ast_obstack, NULL);