2 * This file is part of cparser.
3 * Copyright (C) 2007-2009 Matthias Braun <matze@braunis.de>
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
26 #include "lang_features.h"
36 #if defined(__INTEL_COMPILER)
38 #elif defined(__CYGWIN__)
39 #include "win32/cygwin_math_ext.h"
44 #include "adt/error.h"
47 struct obstack ast_obstack;
51 bool print_implicit_casts = false;
52 bool print_parenthesis = false;
54 static void print_statement(const statement_t *statement);
55 static void print_expression_prec(const expression_t *expression, unsigned prec);
57 void change_indent(int delta)
63 void print_indent(void)
65 for (int i = 0; i < indent; ++i)
69 static void print_stringrep(const string_t *string)
71 for (size_t i = 0; i < string->size; ++i) {
72 print_char(string->begin[i]);
77 * Returns 1 if a given precedence level has right-to-left
78 * associativity, else 0.
80 * @param precedence the operator precedence
82 static int right_to_left(unsigned precedence)
86 case PREC_CONDITIONAL:
96 * Return the precedence of an expression given by its kind.
98 * @param kind the expression kind
100 static unsigned get_expression_precedence(expression_kind_t kind)
102 static const unsigned prec[] = {
103 [EXPR_REFERENCE] = PREC_PRIMARY,
104 [EXPR_REFERENCE_ENUM_VALUE] = PREC_PRIMARY,
105 [EXPR_LITERAL_INTEGER] = PREC_PRIMARY,
106 [EXPR_LITERAL_INTEGER_OCTAL] = PREC_PRIMARY,
107 [EXPR_LITERAL_INTEGER_HEXADECIMAL] = PREC_PRIMARY,
108 [EXPR_LITERAL_FLOATINGPOINT] = PREC_PRIMARY,
109 [EXPR_LITERAL_FLOATINGPOINT_HEXADECIMAL] = PREC_PRIMARY,
110 [EXPR_LITERAL_CHARACTER] = PREC_PRIMARY,
111 [EXPR_LITERAL_WIDE_CHARACTER] = PREC_PRIMARY,
112 [EXPR_LITERAL_MS_NOOP] = PREC_PRIMARY,
113 [EXPR_STRING_LITERAL] = PREC_PRIMARY,
114 [EXPR_WIDE_STRING_LITERAL] = PREC_PRIMARY,
115 [EXPR_COMPOUND_LITERAL] = PREC_UNARY,
116 [EXPR_CALL] = PREC_POSTFIX,
117 [EXPR_CONDITIONAL] = PREC_CONDITIONAL,
118 [EXPR_SELECT] = PREC_POSTFIX,
119 [EXPR_ARRAY_ACCESS] = PREC_POSTFIX,
120 [EXPR_SIZEOF] = PREC_UNARY,
121 [EXPR_CLASSIFY_TYPE] = PREC_UNARY,
122 [EXPR_ALIGNOF] = PREC_UNARY,
124 [EXPR_FUNCNAME] = PREC_PRIMARY,
125 [EXPR_BUILTIN_CONSTANT_P] = PREC_PRIMARY,
126 [EXPR_BUILTIN_TYPES_COMPATIBLE_P] = PREC_PRIMARY,
127 [EXPR_OFFSETOF] = PREC_PRIMARY,
128 [EXPR_VA_START] = PREC_PRIMARY,
129 [EXPR_VA_ARG] = PREC_PRIMARY,
130 [EXPR_VA_COPY] = PREC_PRIMARY,
131 [EXPR_STATEMENT] = PREC_PRIMARY,
132 [EXPR_LABEL_ADDRESS] = PREC_PRIMARY,
134 [EXPR_UNARY_NEGATE] = PREC_UNARY,
135 [EXPR_UNARY_PLUS] = PREC_UNARY,
136 [EXPR_UNARY_BITWISE_NEGATE] = PREC_UNARY,
137 [EXPR_UNARY_NOT] = PREC_UNARY,
138 [EXPR_UNARY_DEREFERENCE] = PREC_UNARY,
139 [EXPR_UNARY_TAKE_ADDRESS] = PREC_UNARY,
140 [EXPR_UNARY_POSTFIX_INCREMENT] = PREC_POSTFIX,
141 [EXPR_UNARY_POSTFIX_DECREMENT] = PREC_POSTFIX,
142 [EXPR_UNARY_PREFIX_INCREMENT] = PREC_UNARY,
143 [EXPR_UNARY_PREFIX_DECREMENT] = PREC_UNARY,
144 [EXPR_UNARY_CAST] = PREC_UNARY,
145 [EXPR_UNARY_ASSUME] = PREC_PRIMARY,
146 [EXPR_UNARY_DELETE] = PREC_UNARY,
147 [EXPR_UNARY_DELETE_ARRAY] = PREC_UNARY,
148 [EXPR_UNARY_THROW] = PREC_ASSIGNMENT,
150 [EXPR_BINARY_ADD] = PREC_ADDITIVE,
151 [EXPR_BINARY_SUB] = PREC_ADDITIVE,
152 [EXPR_BINARY_MUL] = PREC_MULTIPLICATIVE,
153 [EXPR_BINARY_DIV] = PREC_MULTIPLICATIVE,
154 [EXPR_BINARY_MOD] = PREC_MULTIPLICATIVE,
155 [EXPR_BINARY_EQUAL] = PREC_EQUALITY,
156 [EXPR_BINARY_NOTEQUAL] = PREC_EQUALITY,
157 [EXPR_BINARY_LESS] = PREC_RELATIONAL,
158 [EXPR_BINARY_LESSEQUAL] = PREC_RELATIONAL,
159 [EXPR_BINARY_GREATER] = PREC_RELATIONAL,
160 [EXPR_BINARY_GREATEREQUAL] = PREC_RELATIONAL,
161 [EXPR_BINARY_BITWISE_AND] = PREC_AND,
162 [EXPR_BINARY_BITWISE_OR] = PREC_OR,
163 [EXPR_BINARY_BITWISE_XOR] = PREC_XOR,
164 [EXPR_BINARY_LOGICAL_AND] = PREC_LOGICAL_AND,
165 [EXPR_BINARY_LOGICAL_OR] = PREC_LOGICAL_OR,
166 [EXPR_BINARY_SHIFTLEFT] = PREC_SHIFT,
167 [EXPR_BINARY_SHIFTRIGHT] = PREC_SHIFT,
168 [EXPR_BINARY_ASSIGN] = PREC_ASSIGNMENT,
169 [EXPR_BINARY_MUL_ASSIGN] = PREC_ASSIGNMENT,
170 [EXPR_BINARY_DIV_ASSIGN] = PREC_ASSIGNMENT,
171 [EXPR_BINARY_MOD_ASSIGN] = PREC_ASSIGNMENT,
172 [EXPR_BINARY_ADD_ASSIGN] = PREC_ASSIGNMENT,
173 [EXPR_BINARY_SUB_ASSIGN] = PREC_ASSIGNMENT,
174 [EXPR_BINARY_SHIFTLEFT_ASSIGN] = PREC_ASSIGNMENT,
175 [EXPR_BINARY_SHIFTRIGHT_ASSIGN] = PREC_ASSIGNMENT,
176 [EXPR_BINARY_BITWISE_AND_ASSIGN] = PREC_ASSIGNMENT,
177 [EXPR_BINARY_BITWISE_XOR_ASSIGN] = PREC_ASSIGNMENT,
178 [EXPR_BINARY_BITWISE_OR_ASSIGN] = PREC_ASSIGNMENT,
179 [EXPR_BINARY_COMMA] = PREC_EXPRESSION,
181 [EXPR_BINARY_ISGREATER] = PREC_PRIMARY,
182 [EXPR_BINARY_ISGREATEREQUAL] = PREC_PRIMARY,
183 [EXPR_BINARY_ISLESS] = PREC_PRIMARY,
184 [EXPR_BINARY_ISLESSEQUAL] = PREC_PRIMARY,
185 [EXPR_BINARY_ISLESSGREATER] = PREC_PRIMARY,
186 [EXPR_BINARY_ISUNORDERED] = PREC_PRIMARY
188 assert((size_t)kind < lengthof(prec));
189 unsigned res = prec[kind];
191 assert(res != PREC_BOTTOM);
196 * Print a quoted string constant.
198 * @param string the string constant
199 * @param border the border char
200 * @param skip number of chars to skip at the end
202 static void print_quoted_string(const string_t *const string, char border,
206 const char *end = string->begin + string->size - skip;
207 for (const char *c = string->begin; c != end; ++c) {
213 case '\\': print_string("\\\\"); break;
214 case '\a': print_string("\\a"); break;
215 case '\b': print_string("\\b"); break;
216 case '\f': print_string("\\f"); break;
217 case '\n': print_string("\\n"); break;
218 case '\r': print_string("\\r"); break;
219 case '\t': print_string("\\t"); break;
220 case '\v': print_string("\\v"); break;
221 case '\?': print_string("\\?"); break;
223 if (c_mode & _GNUC) {
224 print_string("\\e"); break;
228 if ((unsigned)tc < 0x80 && !isprint(tc)) {
229 print_format("\\%03o", (unsigned)tc);
239 static void print_string_literal(const string_literal_expression_t *literal)
241 if (literal->base.kind == EXPR_WIDE_STRING_LITERAL) {
244 print_quoted_string(&literal->value, '"', 1);
247 static void print_literal(const literal_expression_t *literal)
249 switch (literal->base.kind) {
250 case EXPR_LITERAL_MS_NOOP:
251 print_string("__noop");
253 case EXPR_LITERAL_INTEGER_HEXADECIMAL:
254 case EXPR_LITERAL_FLOATINGPOINT_HEXADECIMAL:
257 case EXPR_LITERAL_BOOLEAN:
258 case EXPR_LITERAL_INTEGER:
259 case EXPR_LITERAL_INTEGER_OCTAL:
260 case EXPR_LITERAL_FLOATINGPOINT:
261 print_stringrep(&literal->value);
262 if (literal->suffix.size > 0)
263 print_stringrep(&literal->suffix);
265 case EXPR_LITERAL_WIDE_CHARACTER:
268 case EXPR_LITERAL_CHARACTER:
269 print_quoted_string(&literal->value, '\'', 0);
274 print_string("INVALID LITERAL KIND");
278 * Prints a predefined symbol.
280 static void print_funcname(const funcname_expression_t *funcname)
283 switch (funcname->kind) {
284 case FUNCNAME_FUNCTION: s = (c_mode & _C99) ? "__func__" : "__FUNCTION__"; break;
285 case FUNCNAME_PRETTY_FUNCTION: s = "__PRETTY_FUNCTION__"; break;
286 case FUNCNAME_FUNCSIG: s = "__FUNCSIG__"; break;
287 case FUNCNAME_FUNCDNAME: s = "__FUNCDNAME__"; break;
292 static void print_compound_literal(
293 const compound_literal_expression_t *expression)
296 print_type(expression->type);
298 print_initializer(expression->initializer);
301 static void print_assignment_expression(const expression_t *const expr)
303 print_expression_prec(expr, PREC_ASSIGNMENT);
307 * Prints a call expression.
309 * @param call the call expression
311 static void print_call_expression(const call_expression_t *call)
313 print_expression_prec(call->function, PREC_POSTFIX);
315 call_argument_t *argument = call->arguments;
317 while (argument != NULL) {
323 print_assignment_expression(argument->expression);
325 argument = argument->next;
331 * Prints a binary expression.
333 * @param binexpr the binary expression
335 static void print_binary_expression(const binary_expression_t *binexpr)
337 unsigned prec = get_expression_precedence(binexpr->base.kind);
338 int r2l = right_to_left(prec);
340 print_expression_prec(binexpr->left, prec + r2l);
342 switch (binexpr->base.kind) {
343 case EXPR_BINARY_COMMA: op = ", "; break;
344 case EXPR_BINARY_ASSIGN: op = " = "; break;
345 case EXPR_BINARY_ADD: op = " + "; break;
346 case EXPR_BINARY_SUB: op = " - "; break;
347 case EXPR_BINARY_MUL: op = " * "; break;
348 case EXPR_BINARY_MOD: op = " % "; break;
349 case EXPR_BINARY_DIV: op = " / "; break;
350 case EXPR_BINARY_BITWISE_OR: op = " | "; break;
351 case EXPR_BINARY_BITWISE_AND: op = " & "; break;
352 case EXPR_BINARY_BITWISE_XOR: op = " ^ "; break;
353 case EXPR_BINARY_LOGICAL_OR: op = " || "; break;
354 case EXPR_BINARY_LOGICAL_AND: op = " && "; break;
355 case EXPR_BINARY_NOTEQUAL: op = " != "; break;
356 case EXPR_BINARY_EQUAL: op = " == "; break;
357 case EXPR_BINARY_LESS: op = " < "; break;
358 case EXPR_BINARY_LESSEQUAL: op = " <= "; break;
359 case EXPR_BINARY_GREATER: op = " > "; break;
360 case EXPR_BINARY_GREATEREQUAL: op = " >= "; break;
361 case EXPR_BINARY_SHIFTLEFT: op = " << "; break;
362 case EXPR_BINARY_SHIFTRIGHT: op = " >> "; break;
364 case EXPR_BINARY_ADD_ASSIGN: op = " += "; break;
365 case EXPR_BINARY_SUB_ASSIGN: op = " -= "; break;
366 case EXPR_BINARY_MUL_ASSIGN: op = " *= "; break;
367 case EXPR_BINARY_MOD_ASSIGN: op = " %= "; break;
368 case EXPR_BINARY_DIV_ASSIGN: op = " /= "; break;
369 case EXPR_BINARY_BITWISE_OR_ASSIGN: op = " |= "; break;
370 case EXPR_BINARY_BITWISE_AND_ASSIGN: op = " &= "; break;
371 case EXPR_BINARY_BITWISE_XOR_ASSIGN: op = " ^= "; break;
372 case EXPR_BINARY_SHIFTLEFT_ASSIGN: op = " <<= "; break;
373 case EXPR_BINARY_SHIFTRIGHT_ASSIGN: op = " >>= "; break;
374 default: panic("invalid binexpression found");
377 print_expression_prec(binexpr->right, prec + 1 - r2l);
381 * Prints an unary expression.
383 * @param unexpr the unary expression
385 static void print_unary_expression(const unary_expression_t *unexpr)
387 unsigned prec = get_expression_precedence(unexpr->base.kind);
388 switch (unexpr->base.kind) {
389 case EXPR_UNARY_NEGATE: print_string("-"); break;
390 case EXPR_UNARY_PLUS: print_string("+"); break;
391 case EXPR_UNARY_NOT: print_string("!"); break;
392 case EXPR_UNARY_BITWISE_NEGATE: print_string("~"); break;
393 case EXPR_UNARY_PREFIX_INCREMENT: print_string("++"); break;
394 case EXPR_UNARY_PREFIX_DECREMENT: print_string("--"); break;
395 case EXPR_UNARY_DEREFERENCE: print_string("*"); break;
396 case EXPR_UNARY_TAKE_ADDRESS: print_string("&"); break;
397 case EXPR_UNARY_DELETE: print_string("delete "); break;
398 case EXPR_UNARY_DELETE_ARRAY: print_string("delete [] "); break;
400 case EXPR_UNARY_POSTFIX_INCREMENT:
401 print_expression_prec(unexpr->value, prec);
404 case EXPR_UNARY_POSTFIX_DECREMENT:
405 print_expression_prec(unexpr->value, prec);
408 case EXPR_UNARY_CAST:
410 print_type(unexpr->base.type);
413 case EXPR_UNARY_ASSUME:
414 print_string("__assume(");
415 print_assignment_expression(unexpr->value);
419 case EXPR_UNARY_THROW:
420 if (unexpr->value == NULL) {
421 print_string("throw");
424 print_string("throw ");
428 panic("invalid unary expression found");
430 print_expression_prec(unexpr->value, prec);
434 * Prints a reference expression.
436 * @param ref the reference expression
438 static void print_reference_expression(const reference_expression_t *ref)
440 print_string(ref->entity->base.symbol->string);
444 * Prints a label address expression.
446 * @param ref the reference expression
448 static void print_label_address_expression(const label_address_expression_t *le)
450 print_format("&&%s", le->label->base.symbol->string);
454 * Prints an array expression.
456 * @param expression the array expression
458 static void print_array_expression(const array_access_expression_t *expression)
460 if (!expression->flipped) {
461 print_expression_prec(expression->array_ref, PREC_POSTFIX);
463 print_expression(expression->index);
466 print_expression_prec(expression->index, PREC_POSTFIX);
468 print_expression(expression->array_ref);
474 * Prints a typeproperty expression (sizeof or __alignof__).
476 * @param expression the type property expression
478 static void print_typeprop_expression(const typeprop_expression_t *expression)
480 if (expression->base.kind == EXPR_SIZEOF) {
481 print_string("sizeof");
483 assert(expression->base.kind == EXPR_ALIGNOF);
484 print_string("__alignof__");
486 if (expression->tp_expression != NULL) {
487 /* PREC_TOP: always print the '()' here, sizeof x is right but unusual */
488 print_expression_prec(expression->tp_expression, PREC_TOP);
491 print_type(expression->type);
497 * Prints a builtin constant expression.
499 * @param expression the builtin constant expression
501 static void print_builtin_constant(const builtin_constant_expression_t *expression)
503 print_string("__builtin_constant_p(");
504 print_assignment_expression(expression->value);
509 * Prints a builtin types compatible expression.
511 * @param expression the builtin types compatible expression
513 static void print_builtin_types_compatible(
514 const builtin_types_compatible_expression_t *expression)
516 print_string("__builtin_types_compatible_p(");
517 print_type(expression->left);
519 print_type(expression->right);
524 * Prints a conditional expression.
526 * @param expression the conditional expression
528 static void print_conditional(const conditional_expression_t *expression)
530 print_expression_prec(expression->condition, PREC_LOGICAL_OR);
531 if (expression->true_expression != NULL) {
533 print_expression_prec(expression->true_expression, PREC_EXPRESSION);
536 print_string(" ?: ");
538 precedence_t prec = c_mode & _CXX ? PREC_ASSIGNMENT : PREC_CONDITIONAL;
539 print_expression_prec(expression->false_expression, prec);
543 * Prints a va_start expression.
545 * @param expression the va_start expression
547 static void print_va_start(const va_start_expression_t *const expression)
549 print_string("__builtin_va_start(");
550 print_assignment_expression(expression->ap);
552 print_string(expression->parameter->base.base.symbol->string);
557 * Prints a va_arg expression.
559 * @param expression the va_arg expression
561 static void print_va_arg(const va_arg_expression_t *expression)
563 print_string("__builtin_va_arg(");
564 print_assignment_expression(expression->ap);
566 print_type(expression->base.type);
571 * Prints a va_copy expression.
573 * @param expression the va_copy expression
575 static void print_va_copy(const va_copy_expression_t *expression)
577 print_string("__builtin_va_copy(");
578 print_assignment_expression(expression->dst);
580 print_assignment_expression(expression->src);
585 * Prints a select expression (. or ->).
587 * @param expression the select expression
589 static void print_select(const select_expression_t *expression)
591 print_expression_prec(expression->compound, PREC_POSTFIX);
592 /* do not print anything for anonymous struct/union selects
593 * FIXME: if the anonymous select was a '->' this will print '.'
595 if (expression->compound_entry->base.symbol == NULL)
598 if (is_type_pointer(skip_typeref(expression->compound->base.type))) {
603 print_string(expression->compound_entry->base.symbol->string);
607 * Prints a type classify expression.
609 * @param expr the type classify expression
611 static void print_classify_type_expression(
612 const classify_type_expression_t *const expr)
614 print_string("__builtin_classify_type(");
615 print_assignment_expression(expr->type_expression);
620 * Prints a designator.
622 * @param designator the designator
624 static void print_designator(const designator_t *designator)
626 for ( ; designator != NULL; designator = designator->next) {
627 if (designator->symbol == NULL) {
629 print_expression(designator->array_index);
633 print_string(designator->symbol->string);
639 * Prints an offsetof expression.
641 * @param expression the offset expression
643 static void print_offsetof_expression(const offsetof_expression_t *expression)
645 print_string("__builtin_offsetof(");
646 print_type(expression->type);
648 print_designator(expression->designator);
653 * Prints a statement expression.
655 * @param expression the statement expression
657 static void print_statement_expression(const statement_expression_t *expression)
660 print_statement(expression->statement);
665 * Prints an expression with parenthesis if needed.
667 * @param expression the expression to print
668 * @param top_prec the precedence of the user of this expression.
670 static void print_expression_prec(const expression_t *expression, unsigned top_prec)
672 if (expression->kind == EXPR_UNARY_CAST
673 && expression->base.implicit && !print_implicit_casts) {
674 expression = expression->unary.value;
678 expression->base.parenthesized ||
679 (print_parenthesis && top_prec != PREC_BOTTOM) ||
680 top_prec > get_expression_precedence(expression->base.kind);
684 switch (expression->kind) {
686 print_string("$error$");
688 case EXPR_WIDE_STRING_LITERAL:
689 case EXPR_STRING_LITERAL:
690 print_string_literal(&expression->string_literal);
693 print_literal(&expression->literal);
696 print_funcname(&expression->funcname);
698 case EXPR_COMPOUND_LITERAL:
699 print_compound_literal(&expression->compound_literal);
702 print_call_expression(&expression->call);
705 print_binary_expression(&expression->binary);
708 case EXPR_REFERENCE_ENUM_VALUE:
709 print_reference_expression(&expression->reference);
711 case EXPR_ARRAY_ACCESS:
712 print_array_expression(&expression->array_access);
714 case EXPR_LABEL_ADDRESS:
715 print_label_address_expression(&expression->label_address);
718 print_unary_expression(&expression->unary);
722 print_typeprop_expression(&expression->typeprop);
724 case EXPR_BUILTIN_CONSTANT_P:
725 print_builtin_constant(&expression->builtin_constant);
727 case EXPR_BUILTIN_TYPES_COMPATIBLE_P:
728 print_builtin_types_compatible(&expression->builtin_types_compatible);
730 case EXPR_CONDITIONAL:
731 print_conditional(&expression->conditional);
734 print_va_start(&expression->va_starte);
737 print_va_arg(&expression->va_arge);
740 print_va_copy(&expression->va_copye);
743 print_select(&expression->select);
745 case EXPR_CLASSIFY_TYPE:
746 print_classify_type_expression(&expression->classify_type);
749 print_offsetof_expression(&expression->offsetofe);
752 print_statement_expression(&expression->statement);
759 static void print_indented_statement(statement_t const *const stmt)
761 switch (stmt->kind) {
762 case STATEMENT_LABEL:
765 case STATEMENT_CASE_LABEL:
772 print_statement(stmt);
776 * Print an compound statement.
778 * @param block the compound statement
780 static void print_compound_statement(const compound_statement_t *block)
785 statement_t *statement = block->statements;
786 while (statement != NULL) {
787 print_indented_statement(statement);
790 statement = statement->base.next;
798 * Print a return statement.
800 * @param statement the return statement
802 static void print_return_statement(const return_statement_t *statement)
804 expression_t const *const val = statement->value;
806 print_string("return ");
807 print_expression(val);
810 print_string("return;");
815 * Print an expression statement.
817 * @param statement the expression statement
819 static void print_expression_statement(const expression_statement_t *statement)
821 print_expression(statement->expression);
826 * Print a goto statement.
828 * @param statement the goto statement
830 static void print_goto_statement(const goto_statement_t *statement)
832 print_string("goto ");
833 if (statement->expression != NULL) {
835 print_expression(statement->expression);
837 print_string(statement->label->base.symbol->string);
843 * Print a label statement.
845 * @param statement the label statement
847 static void print_label_statement(const label_statement_t *statement)
849 print_format("%s:\n", statement->label->base.symbol->string);
850 print_indented_statement(statement->statement);
854 * Print an if statement.
856 * @param statement the if statement
858 static void print_if_statement(const if_statement_t *statement)
860 print_string("if (");
861 print_expression(statement->condition);
863 print_statement(statement->true_statement);
865 if (statement->false_statement != NULL) {
868 print_string("else ");
869 print_statement(statement->false_statement);
874 * Print a switch statement.
876 * @param statement the switch statement
878 static void print_switch_statement(const switch_statement_t *statement)
880 print_string("switch (");
881 print_expression(statement->expression);
883 print_statement(statement->body);
887 * Print a case label (including the default label).
889 * @param statement the case label statement
891 static void print_case_label(const case_label_statement_t *statement)
893 if (statement->expression == NULL) {
894 print_string("default:\n");
896 print_string("case ");
897 print_expression(statement->expression);
898 if (statement->end_range != NULL) {
899 print_string(" ... ");
900 print_expression(statement->end_range);
905 print_indented_statement(statement->statement);
908 static void print_typedef(const entity_t *entity)
910 print_string("typedef ");
911 print_type_ext(entity->typedefe.type, entity->base.symbol, NULL);
916 * returns true if the entity is a compiler generated one and has no real
917 * correspondenc in the source file
919 static bool is_generated_entity(const entity_t *entity)
921 if (entity->kind == ENTITY_TYPEDEF)
922 return entity->typedefe.builtin;
924 if (is_declaration(entity))
925 return entity->declaration.implicit;
931 * Print a declaration statement.
933 * @param statement the statement
935 static void print_declaration_statement(
936 const declaration_statement_t *statement)
939 entity_t *entity = statement->declarations_begin;
940 if (entity == NULL) {
941 print_string("/* empty declaration statement */");
945 entity_t *const end = statement->declarations_end->base.next;
946 for (; entity != end; entity = entity->base.next) {
947 if (entity->kind == ENTITY_ENUM_VALUE)
949 if (is_generated_entity(entity))
959 print_entity(entity);
964 * Print a while statement.
966 * @param statement the statement
968 static void print_while_statement(const while_statement_t *statement)
970 print_string("while (");
971 print_expression(statement->condition);
973 print_statement(statement->body);
977 * Print a do-while statement.
979 * @param statement the statement
981 static void print_do_while_statement(const do_while_statement_t *statement)
984 print_statement(statement->body);
987 print_string("while (");
988 print_expression(statement->condition);
993 * Print a for statement.
995 * @param statement the statement
997 static void print_for_statement(const for_statement_t *statement)
999 print_string("for (");
1000 if (statement->initialisation != NULL) {
1001 print_expression(statement->initialisation);
1004 entity_t const *entity = statement->scope.entities;
1005 for (; entity != NULL; entity = entity->base.next) {
1006 if (is_generated_entity(entity))
1008 /* FIXME display of multiple declarations is wrong */
1009 print_declaration(entity);
1012 if (statement->condition != NULL) {
1014 print_expression(statement->condition);
1017 if (statement->step != NULL) {
1019 print_expression(statement->step);
1022 print_statement(statement->body);
1026 * Print assembler arguments.
1028 * @param arguments the arguments
1030 static void print_asm_arguments(asm_argument_t *arguments)
1032 asm_argument_t *argument = arguments;
1033 for (; argument != NULL; argument = argument->next) {
1034 if (argument != arguments)
1037 if (argument->symbol) {
1038 print_format("[%s] ", argument->symbol->string);
1040 print_quoted_string(&argument->constraints, '"', 1);
1042 print_expression(argument->expression);
1048 * Print assembler clobbers.
1050 * @param clobbers the clobbers
1052 static void print_asm_clobbers(asm_clobber_t *clobbers)
1054 asm_clobber_t *clobber = clobbers;
1055 for (; clobber != NULL; clobber = clobber->next) {
1056 if (clobber != clobbers)
1059 print_quoted_string(&clobber->clobber, '"', 1);
1064 * Print an assembler statement.
1066 * @param statement the statement
1068 static void print_asm_statement(const asm_statement_t *statement)
1070 print_string("asm ");
1071 if (statement->is_volatile) {
1072 print_string("volatile ");
1075 print_quoted_string(&statement->asm_text, '"', 1);
1076 if (statement->outputs == NULL &&
1077 statement->inputs == NULL &&
1078 statement->clobbers == NULL)
1079 goto end_of_print_asm_statement;
1081 print_string(" : ");
1082 print_asm_arguments(statement->outputs);
1083 if (statement->inputs == NULL && statement->clobbers == NULL)
1084 goto end_of_print_asm_statement;
1086 print_string(" : ");
1087 print_asm_arguments(statement->inputs);
1088 if (statement->clobbers == NULL)
1089 goto end_of_print_asm_statement;
1091 print_string(" : ");
1092 print_asm_clobbers(statement->clobbers);
1094 end_of_print_asm_statement:
1099 * Print a microsoft __try statement.
1101 * @param statement the statement
1103 static void print_ms_try_statement(const ms_try_statement_t *statement)
1105 print_string("__try ");
1106 print_statement(statement->try_statement);
1109 if (statement->except_expression != NULL) {
1110 print_string("__except(");
1111 print_expression(statement->except_expression);
1114 print_string("__finally ");
1116 print_statement(statement->final_statement);
1120 * Print a microsoft __leave statement.
1122 * @param statement the statement
1124 static void print_leave_statement(const leave_statement_t *statement)
1127 print_string("__leave;");
1131 * Print a statement.
1133 * @param statement the statement
1135 void print_statement(const statement_t *statement)
1137 switch (statement->kind) {
1138 case STATEMENT_EMPTY:
1141 case STATEMENT_COMPOUND:
1142 print_compound_statement(&statement->compound);
1144 case STATEMENT_RETURN:
1145 print_return_statement(&statement->returns);
1147 case STATEMENT_EXPRESSION:
1148 print_expression_statement(&statement->expression);
1150 case STATEMENT_LABEL:
1151 print_label_statement(&statement->label);
1153 case STATEMENT_GOTO:
1154 print_goto_statement(&statement->gotos);
1156 case STATEMENT_CONTINUE:
1157 print_string("continue;");
1159 case STATEMENT_BREAK:
1160 print_string("break;");
1163 print_if_statement(&statement->ifs);
1165 case STATEMENT_SWITCH:
1166 print_switch_statement(&statement->switchs);
1168 case STATEMENT_CASE_LABEL:
1169 print_case_label(&statement->case_label);
1171 case STATEMENT_DECLARATION:
1172 print_declaration_statement(&statement->declaration);
1174 case STATEMENT_WHILE:
1175 print_while_statement(&statement->whiles);
1177 case STATEMENT_DO_WHILE:
1178 print_do_while_statement(&statement->do_while);
1181 print_for_statement(&statement->fors);
1184 print_asm_statement(&statement->asms);
1186 case STATEMENT_MS_TRY:
1187 print_ms_try_statement(&statement->ms_try);
1189 case STATEMENT_LEAVE:
1190 print_leave_statement(&statement->leave);
1192 case STATEMENT_ERROR:
1193 print_string("$error statement$");
1199 * Print a storage class.
1201 * @param storage_class the storage class
1203 static void print_storage_class(storage_class_tag_t storage_class)
1205 switch (storage_class) {
1206 case STORAGE_CLASS_NONE: return;
1207 case STORAGE_CLASS_TYPEDEF: print_string("typedef "); return;
1208 case STORAGE_CLASS_EXTERN: print_string("extern "); return;
1209 case STORAGE_CLASS_STATIC: print_string("static "); return;
1210 case STORAGE_CLASS_AUTO: print_string("auto "); return;
1211 case STORAGE_CLASS_REGISTER: print_string("register "); return;
1213 panic("invalid storage class");
1217 * Print an initializer.
1219 * @param initializer the initializer
1221 void print_initializer(const initializer_t *initializer)
1223 if (initializer == NULL) {
1228 switch (initializer->kind) {
1229 case INITIALIZER_VALUE: {
1230 const initializer_value_t *value = &initializer->value;
1231 print_assignment_expression(value->value);
1234 case INITIALIZER_LIST: {
1235 assert(initializer->kind == INITIALIZER_LIST);
1237 const initializer_list_t *list = &initializer->list;
1239 for (size_t i = 0 ; i < list->len; ++i) {
1240 const initializer_t *sub_init = list->initializers[i];
1241 print_initializer(list->initializers[i]);
1242 if (i < list->len-1) {
1243 if (sub_init == NULL || sub_init->kind != INITIALIZER_DESIGNATOR)
1250 case INITIALIZER_STRING:
1251 print_quoted_string(&initializer->string.string, '"', 1);
1253 case INITIALIZER_WIDE_STRING:
1254 print_quoted_string(&initializer->string.string, '"', 1);
1256 case INITIALIZER_DESIGNATOR:
1257 print_designator(initializer->designator.designator);
1258 print_string(" = ");
1262 panic("invalid initializer kind found");
1267 * Print microsoft extended declaration modifiers.
1269 static void print_ms_modifiers(const declaration_t *declaration)
1271 if ((c_mode & _MS) == 0)
1274 decl_modifiers_t modifiers = declaration->modifiers;
1276 bool ds_shown = false;
1277 const char *next = "(";
1279 if (declaration->base.kind == ENTITY_VARIABLE) {
1280 variable_t *variable = (variable_t*)declaration;
1281 if (variable->alignment != 0
1282 || variable->get_property_sym != NULL
1283 || variable->put_property_sym != NULL) {
1285 print_string("__declspec");
1289 if (variable->alignment != 0) {
1290 print_string(next); next = ", "; print_format("align(%u)", variable->alignment);
1292 if (variable->get_property_sym != NULL
1293 || variable->put_property_sym != NULL) {
1295 print_string(next); next = ", "; print_string("property(");
1296 if (variable->get_property_sym != NULL) {
1297 print_format("get=%s", variable->get_property_sym->string);
1300 if (variable->put_property_sym != NULL)
1301 print_format("%sput=%s", comma, variable->put_property_sym->string);
1307 /* DM_FORCEINLINE handled outside. */
1308 if ((modifiers & ~DM_FORCEINLINE) != 0) {
1310 print_string("__declspec");
1313 if (modifiers & DM_DLLIMPORT) {
1314 print_string(next); next = ", "; print_string("dllimport");
1316 if (modifiers & DM_DLLEXPORT) {
1317 print_string(next); next = ", "; print_string("dllexport");
1319 if (modifiers & DM_THREAD) {
1320 print_string(next); next = ", "; print_string("thread");
1322 if (modifiers & DM_NAKED) {
1323 print_string(next); next = ", "; print_string("naked");
1325 if (modifiers & DM_THREAD) {
1326 print_string(next); next = ", "; print_string("thread");
1328 if (modifiers & DM_SELECTANY) {
1329 print_string(next); next = ", "; print_string("selectany");
1331 if (modifiers & DM_NOTHROW) {
1332 print_string(next); next = ", "; print_string("nothrow");
1334 if (modifiers & DM_NORETURN) {
1335 print_string(next); next = ", "; print_string("noreturn");
1337 if (modifiers & DM_NOINLINE) {
1338 print_string(next); next = ", "; print_string("noinline");
1340 if (modifiers & DM_DEPRECATED) {
1341 print_string(next); next = ", "; print_string("deprecated");
1342 if (declaration->deprecated_string != NULL)
1343 print_format("(\"%s\")",
1344 declaration->deprecated_string);
1346 if (modifiers & DM_RESTRICT) {
1347 print_string(next); next = ", "; print_string("restrict");
1349 if (modifiers & DM_NOALIAS) {
1350 print_string(next); next = ", "; print_string("noalias");
1359 static void print_scope(const scope_t *scope)
1361 const entity_t *entity = scope->entities;
1362 for ( ; entity != NULL; entity = entity->base.next) {
1364 print_entity(entity);
1369 static void print_namespace(const namespace_t *namespace)
1371 print_string("namespace ");
1372 if (namespace->base.symbol != NULL) {
1373 print_string(namespace->base.symbol->string);
1377 print_string("{\n");
1380 print_scope(&namespace->members);
1384 print_string("}\n");
1388 * Print a variable or function declaration
1390 void print_declaration(const entity_t *entity)
1392 assert(is_declaration(entity));
1393 const declaration_t *declaration = &entity->declaration;
1395 print_storage_class((storage_class_tag_t)declaration->declared_storage_class);
1396 if (entity->kind == ENTITY_FUNCTION) {
1397 function_t *function = (function_t*)declaration;
1398 if (function->is_inline) {
1399 if (declaration->modifiers & DM_FORCEINLINE) {
1400 print_string("__forceinline ");
1401 } else if (declaration->modifiers & DM_MICROSOFT_INLINE) {
1402 print_string("__inline ");
1404 print_string("inline ");
1408 //print_ms_modifiers(declaration);
1409 switch (entity->kind) {
1410 case ENTITY_FUNCTION:
1411 print_type_ext(entity->declaration.type, entity->base.symbol,
1412 &entity->function.parameters);
1414 if (entity->function.statement != NULL) {
1416 print_indented_statement(entity->function.statement);
1422 case ENTITY_VARIABLE:
1423 if (entity->variable.thread_local)
1424 print_string("__thread ");
1425 print_type_ext(declaration->type, declaration->base.symbol, NULL);
1426 if (entity->variable.initializer != NULL) {
1427 print_string(" = ");
1428 print_initializer(entity->variable.initializer);
1432 case ENTITY_COMPOUND_MEMBER:
1433 print_type_ext(declaration->type, declaration->base.symbol, NULL);
1434 if (entity->compound_member.bitfield) {
1435 print_format(" : %u", entity->compound_member.bit_size);
1440 print_type_ext(declaration->type, declaration->base.symbol, NULL);
1447 * Prints an expression.
1449 * @param expression the expression
1451 void print_expression(const expression_t *expression)
1453 print_expression_prec(expression, PREC_BOTTOM);
1457 * Print a declaration.
1459 * @param declaration the declaration
1461 void print_entity(const entity_t *entity)
1463 if (entity->base.namespc != NAMESPACE_NORMAL && entity->base.symbol == NULL)
1466 switch ((entity_kind_tag_t)entity->kind) {
1467 case ENTITY_VARIABLE:
1468 case ENTITY_PARAMETER:
1469 case ENTITY_COMPOUND_MEMBER:
1470 case ENTITY_FUNCTION:
1471 print_declaration(entity);
1473 case ENTITY_TYPEDEF:
1474 print_typedef(entity);
1478 print_string("class ");
1479 print_string(entity->base.symbol->string);
1480 print_string("; /* TODO */\n");
1483 print_string("struct ");
1484 goto print_compound;
1486 print_string("union ");
1488 print_string(entity->base.symbol->string);
1489 if (entity->compound.complete) {
1491 print_compound_definition(&entity->compound);
1496 print_string("enum ");
1497 print_string(entity->base.symbol->string);
1499 print_enum_definition(&entity->enume);
1502 case ENTITY_NAMESPACE:
1503 print_namespace(&entity->namespacee);
1505 case ENTITY_LOCAL_LABEL:
1506 print_string("__label__ ");
1507 print_string(entity->base.symbol->string);
1511 case ENTITY_ENUM_VALUE:
1512 panic("print_entity used on unexpected entity type");
1514 panic("Invalid entity type encountered");
1518 * Print the AST of a translation unit.
1520 * @param unit the translation unit
1522 void print_ast(const translation_unit_t *unit)
1524 entity_t *entity = unit->scope.entities;
1525 for ( ; entity != NULL; entity = entity->base.next) {
1526 if (entity->kind == ENTITY_ENUM_VALUE)
1528 if (entity->base.namespc != NAMESPACE_NORMAL
1529 && entity->base.symbol == NULL)
1531 if (is_generated_entity(entity))
1535 print_entity(entity);
1540 expression_classification_t is_constant_initializer(const initializer_t *initializer)
1542 switch (initializer->kind) {
1543 case INITIALIZER_STRING:
1544 case INITIALIZER_WIDE_STRING:
1545 case INITIALIZER_DESIGNATOR:
1546 return EXPR_CLASS_CONSTANT;
1548 case INITIALIZER_VALUE:
1549 return is_linker_constant(initializer->value.value);
1551 case INITIALIZER_LIST: {
1552 expression_classification_t all = EXPR_CLASS_CONSTANT;
1553 for (size_t i = 0; i < initializer->list.len; ++i) {
1554 initializer_t *sub_initializer = initializer->list.initializers[i];
1555 expression_classification_t const cur = is_constant_initializer(sub_initializer);
1563 panic("invalid initializer kind found");
1567 * Checks if an expression references an object with a constant/known location
1568 * to the linker. Example:
1569 * - "x", "*&x" with x being a global variable. The value of x need not be
1570 * constant but the address of x is.
1571 * - "a.b.c" when a has a constant/known location to the linker
1573 static expression_classification_t is_object_with_linker_constant_address(
1574 const expression_t *expression)
1576 switch (expression->kind) {
1577 case EXPR_UNARY_DEREFERENCE:
1578 return is_linker_constant(expression->unary.value);
1581 type_t *base_type = skip_typeref(expression->select.compound->base.type);
1582 if (is_type_pointer(base_type)) {
1584 return is_linker_constant(expression->select.compound);
1586 return is_object_with_linker_constant_address(expression->select.compound);
1590 case EXPR_ARRAY_ACCESS: {
1591 expression_classification_t const ref = is_linker_constant(expression->array_access.array_ref);
1592 expression_classification_t const idx = is_constant_expression(expression->array_access.index);
1593 return ref < idx ? ref : idx;
1596 case EXPR_REFERENCE: {
1597 entity_t *entity = expression->reference.entity;
1598 if (!is_declaration(entity))
1599 return EXPR_CLASS_VARIABLE;
1601 switch ((storage_class_tag_t)entity->declaration.storage_class) {
1602 case STORAGE_CLASS_NONE:
1603 case STORAGE_CLASS_EXTERN:
1604 case STORAGE_CLASS_STATIC:
1606 entity->kind != ENTITY_VARIABLE ||
1607 !entity->variable.thread_local ? EXPR_CLASS_CONSTANT :
1608 EXPR_CLASS_VARIABLE;
1610 case STORAGE_CLASS_REGISTER:
1611 case STORAGE_CLASS_TYPEDEF:
1612 case STORAGE_CLASS_AUTO:
1615 return EXPR_CLASS_VARIABLE;
1619 return EXPR_CLASS_ERROR;
1622 return EXPR_CLASS_VARIABLE;
1626 expression_classification_t is_linker_constant(const expression_t *expression)
1628 switch (expression->kind) {
1629 case EXPR_STRING_LITERAL:
1630 case EXPR_WIDE_STRING_LITERAL:
1632 case EXPR_LABEL_ADDRESS:
1633 return EXPR_CLASS_CONSTANT;
1635 case EXPR_COMPOUND_LITERAL:
1636 return is_constant_initializer(expression->compound_literal.initializer);
1638 case EXPR_UNARY_TAKE_ADDRESS:
1639 return is_object_with_linker_constant_address(expression->unary.value);
1641 case EXPR_UNARY_DEREFERENCE: {
1643 = revert_automatic_type_conversion(expression->unary.value);
1644 /* dereferencing a function is a NOP */
1645 if (is_type_function(real_type)) {
1646 return is_linker_constant(expression->unary.value);
1651 case EXPR_UNARY_CAST: {
1652 type_t *dest = skip_typeref(expression->base.type);
1653 if (!is_type_pointer(dest) && (
1654 dest->kind != TYPE_ATOMIC ||
1655 !(get_atomic_type_flags(dest->atomic.akind) & ATOMIC_TYPE_FLAG_INTEGER) ||
1656 get_atomic_type_size(dest->atomic.akind) < get_type_size(type_void_ptr)
1658 return is_constant_expression(expression);
1660 return is_linker_constant(expression->unary.value);
1663 case EXPR_BINARY_ADD:
1664 case EXPR_BINARY_SUB: {
1665 expression_t *const left = expression->binary.left;
1666 expression_t *const right = expression->binary.right;
1667 type_t *const ltype = skip_typeref(left->base.type);
1668 type_t *const rtype = skip_typeref(right->base.type);
1670 if (is_type_pointer(ltype)) {
1671 expression_classification_t const l = is_linker_constant(left);
1672 expression_classification_t const r = is_constant_expression(right);
1673 return l < r ? l : r;
1674 } else if (is_type_pointer(rtype)) {
1675 expression_classification_t const l = is_constant_expression(left);
1676 expression_classification_t const r = is_linker_constant(right);
1677 return l < r ? l : r;
1678 } else if (!is_type_valid(ltype) || !is_type_valid(rtype)) {
1679 return EXPR_CLASS_ERROR;
1681 return is_constant_expression(expression);
1685 case EXPR_REFERENCE: {
1686 entity_t *entity = expression->reference.entity;
1687 if (!is_declaration(entity))
1688 return EXPR_CLASS_VARIABLE;
1690 type_t *type = skip_typeref(entity->declaration.type);
1691 if (is_type_function(type))
1692 return EXPR_CLASS_CONSTANT;
1693 if (is_type_array(type)) {
1694 return is_object_with_linker_constant_address(expression);
1696 /* Prevent stray errors */
1697 if (!is_type_valid(type))
1698 return EXPR_CLASS_ERROR;
1699 return EXPR_CLASS_VARIABLE;
1702 case EXPR_ARRAY_ACCESS: {
1703 type_t *const type =
1704 skip_typeref(revert_automatic_type_conversion(expression));
1705 if (!is_type_array(type))
1706 return EXPR_CLASS_VARIABLE;
1707 return is_linker_constant(expression->array_access.array_ref);
1710 case EXPR_CONDITIONAL: {
1711 expression_t *const c = expression->conditional.condition;
1712 expression_classification_t const cclass = is_constant_expression(c);
1713 if (cclass != EXPR_CLASS_CONSTANT)
1716 if (fold_constant_to_bool(c)) {
1717 expression_t const *const t = expression->conditional.true_expression;
1718 return is_linker_constant(t != NULL ? t : c);
1720 return is_linker_constant(expression->conditional.false_expression);
1725 entity_t *entity = expression->select.compound_entry;
1726 if (!is_declaration(entity))
1727 return EXPR_CLASS_VARIABLE;
1728 type_t *type = skip_typeref(entity->declaration.type);
1729 if (is_type_array(type)) {
1730 /* arrays automatically convert to their address */
1731 expression_t *compound = expression->select.compound;
1732 type_t *base_type = skip_typeref(compound->base.type);
1733 if (is_type_pointer(base_type)) {
1735 return is_linker_constant(compound);
1737 return is_object_with_linker_constant_address(compound);
1740 return EXPR_CLASS_VARIABLE;
1744 return is_constant_expression(expression);
1749 * Check if the given expression is a call to a builtin function
1750 * returning a constant result.
1752 static expression_classification_t is_builtin_const_call(const expression_t *expression)
1754 expression_t *function = expression->call.function;
1755 if (function->kind != EXPR_REFERENCE)
1756 return EXPR_CLASS_VARIABLE;
1757 reference_expression_t *ref = &function->reference;
1758 if (ref->entity->kind != ENTITY_FUNCTION)
1759 return EXPR_CLASS_VARIABLE;
1761 switch (ref->entity->function.btk) {
1764 return EXPR_CLASS_CONSTANT;
1766 return EXPR_CLASS_VARIABLE;
1771 static expression_classification_t is_constant_pointer(const expression_t *expression)
1773 expression_classification_t const expr_class = is_constant_expression(expression);
1774 if (expr_class != EXPR_CLASS_VARIABLE)
1777 switch (expression->kind) {
1778 case EXPR_UNARY_CAST:
1779 return is_constant_pointer(expression->unary.value);
1781 return EXPR_CLASS_VARIABLE;
1785 static expression_classification_t is_object_with_constant_address(const expression_t *expression)
1787 switch (expression->kind) {
1789 expression_t *compound = expression->select.compound;
1790 type_t *compound_type = compound->base.type;
1791 compound_type = skip_typeref(compound_type);
1792 if (is_type_pointer(compound_type)) {
1793 return is_constant_pointer(compound);
1795 return is_object_with_constant_address(compound);
1799 case EXPR_ARRAY_ACCESS: {
1800 array_access_expression_t const* const array_access =
1801 &expression->array_access;
1802 expression_classification_t const idx_class = is_constant_expression(array_access->index);
1803 if (idx_class != EXPR_CLASS_CONSTANT)
1805 expression_classification_t const ref_addr = is_object_with_constant_address(array_access->array_ref);
1806 expression_classification_t const ref_ptr = is_constant_pointer(array_access->array_ref);
1807 return ref_addr > ref_ptr ? ref_addr : ref_ptr;
1810 case EXPR_UNARY_DEREFERENCE:
1811 return is_constant_pointer(expression->unary.value);
1814 return EXPR_CLASS_ERROR;
1817 return EXPR_CLASS_VARIABLE;
1821 expression_classification_t is_constant_expression(const expression_t *expression)
1823 switch (expression->kind) {
1825 case EXPR_CLASSIFY_TYPE:
1828 case EXPR_BUILTIN_CONSTANT_P:
1829 case EXPR_BUILTIN_TYPES_COMPATIBLE_P:
1830 case EXPR_REFERENCE_ENUM_VALUE:
1831 return EXPR_CLASS_CONSTANT;
1834 type_t *const type = skip_typeref(expression->typeprop.type);
1836 !is_type_array(type) || !type->array.is_vla ? EXPR_CLASS_CONSTANT :
1837 EXPR_CLASS_VARIABLE;
1840 case EXPR_STRING_LITERAL:
1841 case EXPR_WIDE_STRING_LITERAL:
1843 case EXPR_LABEL_ADDRESS:
1848 case EXPR_STATEMENT:
1849 case EXPR_UNARY_POSTFIX_INCREMENT:
1850 case EXPR_UNARY_POSTFIX_DECREMENT:
1851 case EXPR_UNARY_PREFIX_INCREMENT:
1852 case EXPR_UNARY_PREFIX_DECREMENT:
1853 case EXPR_UNARY_ASSUME: /* has VOID type */
1854 case EXPR_UNARY_DEREFERENCE:
1855 case EXPR_UNARY_DELETE:
1856 case EXPR_UNARY_DELETE_ARRAY:
1857 case EXPR_UNARY_THROW:
1858 case EXPR_BINARY_ASSIGN:
1859 case EXPR_BINARY_MUL_ASSIGN:
1860 case EXPR_BINARY_DIV_ASSIGN:
1861 case EXPR_BINARY_MOD_ASSIGN:
1862 case EXPR_BINARY_ADD_ASSIGN:
1863 case EXPR_BINARY_SUB_ASSIGN:
1864 case EXPR_BINARY_SHIFTLEFT_ASSIGN:
1865 case EXPR_BINARY_SHIFTRIGHT_ASSIGN:
1866 case EXPR_BINARY_BITWISE_AND_ASSIGN:
1867 case EXPR_BINARY_BITWISE_XOR_ASSIGN:
1868 case EXPR_BINARY_BITWISE_OR_ASSIGN:
1869 case EXPR_BINARY_COMMA:
1870 case EXPR_ARRAY_ACCESS:
1871 return EXPR_CLASS_VARIABLE;
1873 case EXPR_REFERENCE: {
1874 type_t *const type = skip_typeref(expression->base.type);
1875 return is_type_valid(type) ? EXPR_CLASS_VARIABLE : EXPR_CLASS_ERROR;
1878 case EXPR_UNARY_TAKE_ADDRESS:
1879 return is_object_with_constant_address(expression->unary.value);
1882 return is_builtin_const_call(expression);
1884 case EXPR_UNARY_NEGATE:
1885 case EXPR_UNARY_PLUS:
1886 case EXPR_UNARY_BITWISE_NEGATE:
1887 case EXPR_UNARY_NOT:
1888 return is_constant_expression(expression->unary.value);
1890 case EXPR_UNARY_CAST: {
1891 type_t *const type = skip_typeref(expression->base.type);
1892 if (is_type_scalar(type))
1893 return is_constant_expression(expression->unary.value);
1894 if (!is_type_valid(type))
1895 return EXPR_CLASS_ERROR;
1896 return EXPR_CLASS_VARIABLE;
1899 case EXPR_BINARY_ADD:
1900 case EXPR_BINARY_SUB:
1901 case EXPR_BINARY_MUL:
1902 case EXPR_BINARY_DIV:
1903 case EXPR_BINARY_MOD:
1904 case EXPR_BINARY_EQUAL:
1905 case EXPR_BINARY_NOTEQUAL:
1906 case EXPR_BINARY_LESS:
1907 case EXPR_BINARY_LESSEQUAL:
1908 case EXPR_BINARY_GREATER:
1909 case EXPR_BINARY_GREATEREQUAL:
1910 case EXPR_BINARY_BITWISE_AND:
1911 case EXPR_BINARY_BITWISE_OR:
1912 case EXPR_BINARY_BITWISE_XOR:
1913 case EXPR_BINARY_SHIFTLEFT:
1914 case EXPR_BINARY_SHIFTRIGHT:
1915 case EXPR_BINARY_ISGREATER:
1916 case EXPR_BINARY_ISGREATEREQUAL:
1917 case EXPR_BINARY_ISLESS:
1918 case EXPR_BINARY_ISLESSEQUAL:
1919 case EXPR_BINARY_ISLESSGREATER:
1920 case EXPR_BINARY_ISUNORDERED: {
1921 expression_classification_t const l = is_constant_expression(expression->binary.left);
1922 expression_classification_t const r = is_constant_expression(expression->binary.right);
1923 return l < r ? l : r;
1926 case EXPR_BINARY_LOGICAL_AND: {
1927 expression_t const *const left = expression->binary.left;
1928 expression_classification_t const lclass = is_constant_expression(left);
1929 if (lclass != EXPR_CLASS_CONSTANT)
1931 if (!fold_constant_to_bool(left))
1932 return EXPR_CLASS_CONSTANT;
1933 return is_constant_expression(expression->binary.right);
1936 case EXPR_BINARY_LOGICAL_OR: {
1937 expression_t const *const left = expression->binary.left;
1938 expression_classification_t const lclass = is_constant_expression(left);
1939 if (lclass != EXPR_CLASS_CONSTANT)
1941 if (fold_constant_to_bool(left))
1942 return EXPR_CLASS_CONSTANT;
1943 return is_constant_expression(expression->binary.right);
1946 case EXPR_COMPOUND_LITERAL:
1947 return is_constant_initializer(expression->compound_literal.initializer);
1949 case EXPR_CONDITIONAL: {
1950 expression_t *const condition = expression->conditional.condition;
1951 expression_classification_t const cclass = is_constant_expression(condition);
1952 if (cclass != EXPR_CLASS_CONSTANT)
1955 if (fold_constant_to_bool(condition)) {
1956 expression_t const *const t = expression->conditional.true_expression;
1957 return t == NULL ? EXPR_CLASS_CONSTANT : is_constant_expression(t);
1959 return is_constant_expression(expression->conditional.false_expression);
1964 return EXPR_CLASS_ERROR;
1966 panic("invalid expression found (is constant expression)");
1971 obstack_init(&ast_obstack);
1976 obstack_free(&ast_obstack, NULL);