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);
760 * Print an compound statement.
762 * @param block the compound statement
764 static void print_compound_statement(const compound_statement_t *block)
769 statement_t *statement = block->statements;
770 while (statement != NULL) {
771 if (statement->base.kind == STATEMENT_CASE_LABEL)
773 if (statement->kind != STATEMENT_LABEL)
775 print_statement(statement);
777 statement = statement->base.next;
781 print_string(block->stmt_expr ? "}" : "}\n");
785 * Print a return statement.
787 * @param statement the return statement
789 static void print_return_statement(const return_statement_t *statement)
791 expression_t const *const val = statement->value;
793 print_string("return ");
794 print_expression(val);
797 print_string("return;\n");
802 * Print an expression statement.
804 * @param statement the expression statement
806 static void print_expression_statement(const expression_statement_t *statement)
808 print_expression(statement->expression);
813 * Print a goto statement.
815 * @param statement the goto statement
817 static void print_goto_statement(const goto_statement_t *statement)
819 print_string("goto ");
820 if (statement->expression != NULL) {
822 print_expression(statement->expression);
824 print_string(statement->label->base.symbol->string);
830 * Print a label statement.
832 * @param statement the label statement
834 static void print_label_statement(const label_statement_t *statement)
836 print_format("%s:\n", statement->label->base.symbol->string);
838 print_statement(statement->statement);
842 * Print an if statement.
844 * @param statement the if statement
846 static void print_if_statement(const if_statement_t *statement)
848 print_string("if (");
849 print_expression(statement->condition);
851 print_statement(statement->true_statement);
853 if (statement->false_statement != NULL) {
855 print_string("else ");
856 print_statement(statement->false_statement);
861 * Print a switch statement.
863 * @param statement the switch statement
865 static void print_switch_statement(const switch_statement_t *statement)
867 print_string("switch (");
868 print_expression(statement->expression);
870 print_statement(statement->body);
874 * Print a case label (including the default label).
876 * @param statement the case label statement
878 static void print_case_label(const case_label_statement_t *statement)
880 if (statement->expression == NULL) {
881 print_string("default:\n");
883 print_string("case ");
884 print_expression(statement->expression);
885 if (statement->end_range != NULL) {
886 print_string(" ... ");
887 print_expression(statement->end_range);
892 if (statement->statement != NULL) {
893 if (statement->statement->base.kind == STATEMENT_CASE_LABEL) {
897 print_statement(statement->statement);
901 static void print_typedef(const entity_t *entity)
903 print_string("typedef ");
904 print_type_ext(entity->typedefe.type, entity->base.symbol, NULL);
909 * returns true if the entity is a compiler generated one and has no real
910 * correspondenc in the source file
912 static bool is_generated_entity(const entity_t *entity)
914 if (entity->kind == ENTITY_TYPEDEF)
915 return entity->typedefe.builtin;
917 if (is_declaration(entity))
918 return entity->declaration.implicit;
924 * Print a declaration statement.
926 * @param statement the statement
928 static void print_declaration_statement(
929 const declaration_statement_t *statement)
932 entity_t *entity = statement->declarations_begin;
933 if (entity == NULL) {
934 print_string("/* empty declaration statement */\n");
938 entity_t *const end = statement->declarations_end->base.next;
939 for (; entity != end; entity = entity->base.next) {
940 if (entity->kind == ENTITY_ENUM_VALUE)
942 if (is_generated_entity(entity))
951 print_entity(entity);
957 * Print a while statement.
959 * @param statement the statement
961 static void print_while_statement(const while_statement_t *statement)
963 print_string("while (");
964 print_expression(statement->condition);
966 print_statement(statement->body);
970 * Print a do-while statement.
972 * @param statement the statement
974 static void print_do_while_statement(const do_while_statement_t *statement)
977 print_statement(statement->body);
979 print_string("while (");
980 print_expression(statement->condition);
981 print_string(");\n");
985 * Print a for statement.
987 * @param statement the statement
989 static void print_for_statement(const for_statement_t *statement)
991 print_string("for (");
992 if (statement->initialisation != NULL) {
993 print_expression(statement->initialisation);
996 entity_t const *entity = statement->scope.entities;
997 for (; entity != NULL; entity = entity->base.next) {
998 if (is_generated_entity(entity))
1000 /* FIXME display of multiple declarations is wrong */
1001 print_declaration(entity);
1004 if (statement->condition != NULL) {
1006 print_expression(statement->condition);
1009 if (statement->step != NULL) {
1011 print_expression(statement->step);
1014 print_statement(statement->body);
1018 * Print assembler arguments.
1020 * @param arguments the arguments
1022 static void print_asm_arguments(asm_argument_t *arguments)
1024 asm_argument_t *argument = arguments;
1025 for (; argument != NULL; argument = argument->next) {
1026 if (argument != arguments)
1029 if (argument->symbol) {
1030 print_format("[%s] ", argument->symbol->string);
1032 print_quoted_string(&argument->constraints, '"', 1);
1034 print_expression(argument->expression);
1040 * Print assembler clobbers.
1042 * @param clobbers the clobbers
1044 static void print_asm_clobbers(asm_clobber_t *clobbers)
1046 asm_clobber_t *clobber = clobbers;
1047 for (; clobber != NULL; clobber = clobber->next) {
1048 if (clobber != clobbers)
1051 print_quoted_string(&clobber->clobber, '"', 1);
1056 * Print an assembler statement.
1058 * @param statement the statement
1060 static void print_asm_statement(const asm_statement_t *statement)
1062 print_string("asm ");
1063 if (statement->is_volatile) {
1064 print_string("volatile ");
1067 print_quoted_string(&statement->asm_text, '"', 1);
1068 if (statement->outputs == NULL &&
1069 statement->inputs == NULL &&
1070 statement->clobbers == NULL)
1071 goto end_of_print_asm_statement;
1073 print_string(" : ");
1074 print_asm_arguments(statement->outputs);
1075 if (statement->inputs == NULL && statement->clobbers == NULL)
1076 goto end_of_print_asm_statement;
1078 print_string(" : ");
1079 print_asm_arguments(statement->inputs);
1080 if (statement->clobbers == NULL)
1081 goto end_of_print_asm_statement;
1083 print_string(" : ");
1084 print_asm_clobbers(statement->clobbers);
1086 end_of_print_asm_statement:
1087 print_string(");\n");
1091 * Print a microsoft __try statement.
1093 * @param statement the statement
1095 static void print_ms_try_statement(const ms_try_statement_t *statement)
1097 print_string("__try ");
1098 print_statement(statement->try_statement);
1100 if (statement->except_expression != NULL) {
1101 print_string("__except(");
1102 print_expression(statement->except_expression);
1105 print_string("__finally ");
1107 print_statement(statement->final_statement);
1111 * Print a microsoft __leave statement.
1113 * @param statement the statement
1115 static void print_leave_statement(const leave_statement_t *statement)
1118 print_string("__leave;\n");
1122 * Print a statement.
1124 * @param statement the statement
1126 void print_statement(const statement_t *statement)
1128 switch (statement->kind) {
1129 case STATEMENT_EMPTY:
1130 print_string(";\n");
1132 case STATEMENT_COMPOUND:
1133 print_compound_statement(&statement->compound);
1135 case STATEMENT_RETURN:
1136 print_return_statement(&statement->returns);
1138 case STATEMENT_EXPRESSION:
1139 print_expression_statement(&statement->expression);
1141 case STATEMENT_LABEL:
1142 print_label_statement(&statement->label);
1144 case STATEMENT_GOTO:
1145 print_goto_statement(&statement->gotos);
1147 case STATEMENT_CONTINUE:
1148 print_string("continue;\n");
1150 case STATEMENT_BREAK:
1151 print_string("break;\n");
1154 print_if_statement(&statement->ifs);
1156 case STATEMENT_SWITCH:
1157 print_switch_statement(&statement->switchs);
1159 case STATEMENT_CASE_LABEL:
1160 print_case_label(&statement->case_label);
1162 case STATEMENT_DECLARATION:
1163 print_declaration_statement(&statement->declaration);
1165 case STATEMENT_WHILE:
1166 print_while_statement(&statement->whiles);
1168 case STATEMENT_DO_WHILE:
1169 print_do_while_statement(&statement->do_while);
1172 print_for_statement(&statement->fors);
1175 print_asm_statement(&statement->asms);
1177 case STATEMENT_MS_TRY:
1178 print_ms_try_statement(&statement->ms_try);
1180 case STATEMENT_LEAVE:
1181 print_leave_statement(&statement->leave);
1183 case STATEMENT_ERROR:
1184 print_string("$error statement$\n");
1190 * Print a storage class.
1192 * @param storage_class the storage class
1194 static void print_storage_class(storage_class_tag_t storage_class)
1196 switch (storage_class) {
1197 case STORAGE_CLASS_NONE: return;
1198 case STORAGE_CLASS_TYPEDEF: print_string("typedef "); return;
1199 case STORAGE_CLASS_EXTERN: print_string("extern "); return;
1200 case STORAGE_CLASS_STATIC: print_string("static "); return;
1201 case STORAGE_CLASS_AUTO: print_string("auto "); return;
1202 case STORAGE_CLASS_REGISTER: print_string("register "); return;
1204 panic("invalid storage class");
1208 * Print an initializer.
1210 * @param initializer the initializer
1212 void print_initializer(const initializer_t *initializer)
1214 if (initializer == NULL) {
1219 switch (initializer->kind) {
1220 case INITIALIZER_VALUE: {
1221 const initializer_value_t *value = &initializer->value;
1222 print_assignment_expression(value->value);
1225 case INITIALIZER_LIST: {
1226 assert(initializer->kind == INITIALIZER_LIST);
1228 const initializer_list_t *list = &initializer->list;
1230 for (size_t i = 0 ; i < list->len; ++i) {
1231 const initializer_t *sub_init = list->initializers[i];
1232 print_initializer(list->initializers[i]);
1233 if (i < list->len-1) {
1234 if (sub_init == NULL || sub_init->kind != INITIALIZER_DESIGNATOR)
1241 case INITIALIZER_STRING:
1242 print_quoted_string(&initializer->string.string, '"', 1);
1244 case INITIALIZER_WIDE_STRING:
1245 print_quoted_string(&initializer->string.string, '"', 1);
1247 case INITIALIZER_DESIGNATOR:
1248 print_designator(initializer->designator.designator);
1249 print_string(" = ");
1253 panic("invalid initializer kind found");
1258 * Print microsoft extended declaration modifiers.
1260 static void print_ms_modifiers(const declaration_t *declaration)
1262 if ((c_mode & _MS) == 0)
1265 decl_modifiers_t modifiers = declaration->modifiers;
1267 bool ds_shown = false;
1268 const char *next = "(";
1270 if (declaration->base.kind == ENTITY_VARIABLE) {
1271 variable_t *variable = (variable_t*)declaration;
1272 if (variable->alignment != 0
1273 || variable->get_property_sym != NULL
1274 || variable->put_property_sym != NULL) {
1276 print_string("__declspec");
1280 if (variable->alignment != 0) {
1281 print_string(next); next = ", "; print_format("align(%u)", variable->alignment);
1283 if (variable->get_property_sym != NULL
1284 || variable->put_property_sym != NULL) {
1286 print_string(next); next = ", "; print_string("property(");
1287 if (variable->get_property_sym != NULL) {
1288 print_format("get=%s", variable->get_property_sym->string);
1291 if (variable->put_property_sym != NULL)
1292 print_format("%sput=%s", comma, variable->put_property_sym->string);
1298 /* DM_FORCEINLINE handled outside. */
1299 if ((modifiers & ~DM_FORCEINLINE) != 0) {
1301 print_string("__declspec");
1304 if (modifiers & DM_DLLIMPORT) {
1305 print_string(next); next = ", "; print_string("dllimport");
1307 if (modifiers & DM_DLLEXPORT) {
1308 print_string(next); next = ", "; print_string("dllexport");
1310 if (modifiers & DM_THREAD) {
1311 print_string(next); next = ", "; print_string("thread");
1313 if (modifiers & DM_NAKED) {
1314 print_string(next); next = ", "; print_string("naked");
1316 if (modifiers & DM_THREAD) {
1317 print_string(next); next = ", "; print_string("thread");
1319 if (modifiers & DM_SELECTANY) {
1320 print_string(next); next = ", "; print_string("selectany");
1322 if (modifiers & DM_NOTHROW) {
1323 print_string(next); next = ", "; print_string("nothrow");
1325 if (modifiers & DM_NORETURN) {
1326 print_string(next); next = ", "; print_string("noreturn");
1328 if (modifiers & DM_NOINLINE) {
1329 print_string(next); next = ", "; print_string("noinline");
1331 if (modifiers & DM_DEPRECATED) {
1332 print_string(next); next = ", "; print_string("deprecated");
1333 if (declaration->deprecated_string != NULL)
1334 print_format("(\"%s\")",
1335 declaration->deprecated_string);
1337 if (modifiers & DM_RESTRICT) {
1338 print_string(next); next = ", "; print_string("restrict");
1340 if (modifiers & DM_NOALIAS) {
1341 print_string(next); next = ", "; print_string("noalias");
1350 static void print_scope(const scope_t *scope)
1352 const entity_t *entity = scope->entities;
1353 for ( ; entity != NULL; entity = entity->base.next) {
1355 print_entity(entity);
1360 static void print_namespace(const namespace_t *namespace)
1362 print_string("namespace ");
1363 if (namespace->base.symbol != NULL) {
1364 print_string(namespace->base.symbol->string);
1368 print_string("{\n");
1371 print_scope(&namespace->members);
1375 print_string("}\n");
1379 * Print a variable or function declaration
1381 void print_declaration(const entity_t *entity)
1383 assert(is_declaration(entity));
1384 const declaration_t *declaration = &entity->declaration;
1386 print_storage_class((storage_class_tag_t)declaration->declared_storage_class);
1387 if (entity->kind == ENTITY_FUNCTION) {
1388 function_t *function = (function_t*)declaration;
1389 if (function->is_inline) {
1390 if (declaration->modifiers & DM_FORCEINLINE) {
1391 print_string("__forceinline ");
1392 } else if (declaration->modifiers & DM_MICROSOFT_INLINE) {
1393 print_string("__inline ");
1395 print_string("inline ");
1399 //print_ms_modifiers(declaration);
1400 switch (entity->kind) {
1401 case ENTITY_FUNCTION:
1402 print_type_ext(entity->declaration.type, entity->base.symbol,
1403 &entity->function.parameters);
1405 if (entity->function.statement != NULL) {
1408 print_statement(entity->function.statement);
1413 case ENTITY_VARIABLE:
1414 if (entity->variable.thread_local)
1415 print_string("__thread ");
1416 print_type_ext(declaration->type, declaration->base.symbol, NULL);
1417 if (entity->variable.initializer != NULL) {
1418 print_string(" = ");
1419 print_initializer(entity->variable.initializer);
1423 case ENTITY_COMPOUND_MEMBER:
1424 print_type_ext(declaration->type, declaration->base.symbol, NULL);
1425 if (entity->compound_member.bitfield) {
1426 print_format(" : %u", entity->compound_member.bit_size);
1431 print_type_ext(declaration->type, declaration->base.symbol, NULL);
1438 * Prints an expression.
1440 * @param expression the expression
1442 void print_expression(const expression_t *expression)
1444 print_expression_prec(expression, PREC_BOTTOM);
1448 * Print a declaration.
1450 * @param declaration the declaration
1452 void print_entity(const entity_t *entity)
1454 if (entity->base.namespc != NAMESPACE_NORMAL && entity->base.symbol == NULL)
1457 switch ((entity_kind_tag_t)entity->kind) {
1458 case ENTITY_VARIABLE:
1459 case ENTITY_PARAMETER:
1460 case ENTITY_COMPOUND_MEMBER:
1461 case ENTITY_FUNCTION:
1462 print_declaration(entity);
1464 case ENTITY_TYPEDEF:
1465 print_typedef(entity);
1469 print_string("class ");
1470 print_string(entity->base.symbol->string);
1471 print_string("; /* TODO */\n");
1474 print_string("struct ");
1475 goto print_compound;
1477 print_string("union ");
1479 print_string(entity->base.symbol->string);
1480 if (entity->compound.complete) {
1482 print_compound_definition(&entity->compound);
1487 print_string("enum ");
1488 print_string(entity->base.symbol->string);
1490 print_enum_definition(&entity->enume);
1493 case ENTITY_NAMESPACE:
1494 print_namespace(&entity->namespacee);
1496 case ENTITY_LOCAL_LABEL:
1497 print_string("__label__ ");
1498 print_string(entity->base.symbol->string);
1502 case ENTITY_ENUM_VALUE:
1503 panic("print_entity used on unexpected entity type");
1505 panic("Invalid entity type encountered");
1509 * Print the AST of a translation unit.
1511 * @param unit the translation unit
1513 void print_ast(const translation_unit_t *unit)
1515 entity_t *entity = unit->scope.entities;
1516 for ( ; entity != NULL; entity = entity->base.next) {
1517 if (entity->kind == ENTITY_ENUM_VALUE)
1519 if (entity->base.namespc != NAMESPACE_NORMAL
1520 && entity->base.symbol == NULL)
1522 if (is_generated_entity(entity))
1526 print_entity(entity);
1531 expression_classification_t is_constant_initializer(const initializer_t *initializer)
1533 switch (initializer->kind) {
1534 case INITIALIZER_STRING:
1535 case INITIALIZER_WIDE_STRING:
1536 case INITIALIZER_DESIGNATOR:
1537 return EXPR_CLASS_CONSTANT;
1539 case INITIALIZER_VALUE:
1540 return is_constant_expression(initializer->value.value);
1542 case INITIALIZER_LIST: {
1543 expression_classification_t all = EXPR_CLASS_CONSTANT;
1544 for (size_t i = 0; i < initializer->list.len; ++i) {
1545 initializer_t *sub_initializer = initializer->list.initializers[i];
1546 expression_classification_t const cur = is_constant_initializer(sub_initializer);
1554 panic("invalid initializer kind found");
1558 * Checks if an expression references an object with a constant/known location
1559 * to the linker. Example:
1560 * - "x", "*&x" with x being a global variable. The value of x need not be
1561 * constant but the address of x is.
1562 * - "a.b.c" when a has a constant/known location to the linker
1564 static expression_classification_t is_object_with_linker_constant_address(
1565 const expression_t *expression)
1567 switch (expression->kind) {
1568 case EXPR_UNARY_DEREFERENCE:
1569 return is_linker_constant(expression->unary.value);
1572 type_t *base_type = skip_typeref(expression->select.compound->base.type);
1573 if (is_type_pointer(base_type)) {
1575 return is_linker_constant(expression->select.compound);
1577 return is_object_with_linker_constant_address(expression->select.compound);
1581 case EXPR_ARRAY_ACCESS: {
1582 expression_classification_t const ref = is_linker_constant(expression->array_access.array_ref);
1583 expression_classification_t const idx = is_constant_expression(expression->array_access.index);
1584 return ref < idx ? ref : idx;
1587 case EXPR_REFERENCE: {
1588 entity_t *entity = expression->reference.entity;
1589 if (!is_declaration(entity))
1590 return EXPR_CLASS_VARIABLE;
1592 switch ((storage_class_tag_t)entity->declaration.storage_class) {
1593 case STORAGE_CLASS_NONE:
1594 case STORAGE_CLASS_EXTERN:
1595 case STORAGE_CLASS_STATIC:
1597 entity->kind != ENTITY_VARIABLE ||
1598 !entity->variable.thread_local ? EXPR_CLASS_CONSTANT :
1599 EXPR_CLASS_VARIABLE;
1601 case STORAGE_CLASS_REGISTER:
1602 case STORAGE_CLASS_TYPEDEF:
1603 case STORAGE_CLASS_AUTO:
1606 return EXPR_CLASS_VARIABLE;
1610 return EXPR_CLASS_ERROR;
1613 return EXPR_CLASS_VARIABLE;
1617 expression_classification_t is_linker_constant(const expression_t *expression)
1619 switch (expression->kind) {
1620 case EXPR_STRING_LITERAL:
1621 case EXPR_WIDE_STRING_LITERAL:
1623 case EXPR_LABEL_ADDRESS:
1624 return EXPR_CLASS_CONSTANT;
1626 case EXPR_UNARY_TAKE_ADDRESS:
1627 return is_object_with_linker_constant_address(expression->unary.value);
1629 case EXPR_UNARY_DEREFERENCE: {
1631 = revert_automatic_type_conversion(expression->unary.value);
1632 /* dereferencing a function is a NOP */
1633 if (is_type_function(real_type)) {
1634 return is_linker_constant(expression->unary.value);
1639 case EXPR_UNARY_CAST: {
1640 type_t *dest = skip_typeref(expression->base.type);
1641 if (!is_type_pointer(dest) && (
1642 dest->kind != TYPE_ATOMIC ||
1643 !(get_atomic_type_flags(dest->atomic.akind) & ATOMIC_TYPE_FLAG_INTEGER) ||
1644 get_atomic_type_size(dest->atomic.akind) < get_type_size(type_void_ptr)
1646 return EXPR_CLASS_VARIABLE;
1648 expression_classification_t const expr = is_constant_expression(expression->unary.value);
1649 expression_classification_t const addr = is_linker_constant(expression->unary.value);
1650 return expr > addr ? expr : addr;
1653 case EXPR_BINARY_ADD:
1654 case EXPR_BINARY_SUB: {
1655 expression_t *const left = expression->binary.left;
1656 expression_t *const right = expression->binary.right;
1657 type_t *const ltype = skip_typeref(left->base.type);
1658 type_t *const rtype = skip_typeref(right->base.type);
1660 if (is_type_pointer(ltype)) {
1661 expression_classification_t const l = is_linker_constant(left);
1662 expression_classification_t const r = is_constant_expression(right);
1663 return l < r ? l : r;
1664 } else if (is_type_pointer(rtype)) {
1665 expression_classification_t const l = is_constant_expression(left);
1666 expression_classification_t const r = is_linker_constant(right);
1667 return l < r ? l : r;
1668 } else if (!is_type_valid(ltype) || !is_type_valid(rtype)) {
1669 return EXPR_CLASS_ERROR;
1671 return EXPR_CLASS_VARIABLE;
1675 case EXPR_REFERENCE: {
1676 entity_t *entity = expression->reference.entity;
1677 if (!is_declaration(entity))
1678 return EXPR_CLASS_VARIABLE;
1680 type_t *type = skip_typeref(entity->declaration.type);
1681 if (is_type_function(type))
1682 return EXPR_CLASS_CONSTANT;
1683 if (is_type_array(type)) {
1684 return is_object_with_linker_constant_address(expression);
1686 /* Prevent stray errors */
1687 if (!is_type_valid(type))
1688 return EXPR_CLASS_ERROR;
1689 return EXPR_CLASS_VARIABLE;
1692 case EXPR_ARRAY_ACCESS: {
1693 type_t *const type =
1694 skip_typeref(revert_automatic_type_conversion(expression));
1695 if (!is_type_array(type))
1696 return EXPR_CLASS_VARIABLE;
1697 expression_classification_t const ref = is_linker_constant(expression->array_access.array_ref);
1698 expression_classification_t const idx = is_constant_expression(expression->array_access.index);
1699 return ref < idx ? ref : idx;
1702 case EXPR_CONDITIONAL: {
1703 expression_t *const c = expression->conditional.condition;
1704 expression_classification_t const cclass = is_constant_expression(c);
1705 if (cclass != EXPR_CLASS_CONSTANT)
1708 if (fold_constant_to_bool(c)) {
1709 expression_t const *const t = expression->conditional.true_expression;
1710 return is_linker_constant(t != NULL ? t : c);
1712 return is_linker_constant(expression->conditional.false_expression);
1717 entity_t *entity = expression->select.compound_entry;
1718 if (!is_declaration(entity))
1719 return EXPR_CLASS_VARIABLE;
1720 type_t *type = skip_typeref(entity->declaration.type);
1721 if (is_type_array(type)) {
1722 /* arrays automatically convert to their address */
1723 expression_t *compound = expression->select.compound;
1724 type_t *base_type = skip_typeref(compound->base.type);
1725 if (is_type_pointer(base_type)) {
1727 return is_linker_constant(compound);
1729 return is_object_with_linker_constant_address(compound);
1732 return EXPR_CLASS_VARIABLE;
1736 return EXPR_CLASS_ERROR;
1739 return EXPR_CLASS_VARIABLE;
1744 * Check if the given expression is a call to a builtin function
1745 * returning a constant result.
1747 static expression_classification_t is_builtin_const_call(const expression_t *expression)
1749 expression_t *function = expression->call.function;
1750 if (function->kind != EXPR_REFERENCE)
1751 return EXPR_CLASS_VARIABLE;
1752 reference_expression_t *ref = &function->reference;
1753 if (ref->entity->kind != ENTITY_FUNCTION)
1754 return EXPR_CLASS_VARIABLE;
1756 switch (ref->entity->function.btk) {
1759 return EXPR_CLASS_CONSTANT;
1761 return EXPR_CLASS_VARIABLE;
1766 static expression_classification_t is_constant_pointer(const expression_t *expression)
1768 expression_classification_t const expr_class = is_constant_expression(expression);
1769 if (expr_class != EXPR_CLASS_VARIABLE)
1772 switch (expression->kind) {
1773 case EXPR_UNARY_CAST:
1774 return is_constant_pointer(expression->unary.value);
1776 return EXPR_CLASS_VARIABLE;
1780 static expression_classification_t is_object_with_constant_address(const expression_t *expression)
1782 switch (expression->kind) {
1784 expression_t *compound = expression->select.compound;
1785 type_t *compound_type = compound->base.type;
1786 compound_type = skip_typeref(compound_type);
1787 if (is_type_pointer(compound_type)) {
1788 return is_constant_pointer(compound);
1790 return is_object_with_constant_address(compound);
1794 case EXPR_ARRAY_ACCESS: {
1795 array_access_expression_t const* const array_access =
1796 &expression->array_access;
1797 expression_classification_t const idx_class = is_constant_expression(array_access->index);
1798 if (idx_class != EXPR_CLASS_CONSTANT)
1800 expression_classification_t const ref_addr = is_object_with_constant_address(array_access->array_ref);
1801 expression_classification_t const ref_ptr = is_constant_pointer(array_access->array_ref);
1802 return ref_addr > ref_ptr ? ref_addr : ref_ptr;
1805 case EXPR_UNARY_DEREFERENCE:
1806 return is_constant_pointer(expression->unary.value);
1809 return EXPR_CLASS_ERROR;
1812 return EXPR_CLASS_VARIABLE;
1816 expression_classification_t is_constant_expression(const expression_t *expression)
1818 switch (expression->kind) {
1820 case EXPR_CLASSIFY_TYPE:
1823 case EXPR_BUILTIN_CONSTANT_P:
1824 case EXPR_BUILTIN_TYPES_COMPATIBLE_P:
1825 case EXPR_REFERENCE_ENUM_VALUE:
1826 return EXPR_CLASS_CONSTANT;
1829 type_t *const type = skip_typeref(expression->typeprop.type);
1831 !is_type_array(type) || !type->array.is_vla ? EXPR_CLASS_CONSTANT :
1832 EXPR_CLASS_VARIABLE;
1835 case EXPR_STRING_LITERAL:
1836 case EXPR_WIDE_STRING_LITERAL:
1838 case EXPR_LABEL_ADDRESS:
1843 case EXPR_STATEMENT:
1844 case EXPR_UNARY_POSTFIX_INCREMENT:
1845 case EXPR_UNARY_POSTFIX_DECREMENT:
1846 case EXPR_UNARY_PREFIX_INCREMENT:
1847 case EXPR_UNARY_PREFIX_DECREMENT:
1848 case EXPR_UNARY_ASSUME: /* has VOID type */
1849 case EXPR_UNARY_DEREFERENCE:
1850 case EXPR_UNARY_DELETE:
1851 case EXPR_UNARY_DELETE_ARRAY:
1852 case EXPR_UNARY_THROW:
1853 case EXPR_BINARY_ASSIGN:
1854 case EXPR_BINARY_MUL_ASSIGN:
1855 case EXPR_BINARY_DIV_ASSIGN:
1856 case EXPR_BINARY_MOD_ASSIGN:
1857 case EXPR_BINARY_ADD_ASSIGN:
1858 case EXPR_BINARY_SUB_ASSIGN:
1859 case EXPR_BINARY_SHIFTLEFT_ASSIGN:
1860 case EXPR_BINARY_SHIFTRIGHT_ASSIGN:
1861 case EXPR_BINARY_BITWISE_AND_ASSIGN:
1862 case EXPR_BINARY_BITWISE_XOR_ASSIGN:
1863 case EXPR_BINARY_BITWISE_OR_ASSIGN:
1864 case EXPR_BINARY_COMMA:
1865 case EXPR_ARRAY_ACCESS:
1866 return EXPR_CLASS_VARIABLE;
1868 case EXPR_REFERENCE: {
1869 type_t *const type = skip_typeref(expression->base.type);
1870 return is_type_valid(type) ? EXPR_CLASS_VARIABLE : EXPR_CLASS_ERROR;
1873 case EXPR_UNARY_TAKE_ADDRESS:
1874 return is_object_with_constant_address(expression->unary.value);
1877 return is_builtin_const_call(expression);
1879 case EXPR_UNARY_NEGATE:
1880 case EXPR_UNARY_PLUS:
1881 case EXPR_UNARY_BITWISE_NEGATE:
1882 case EXPR_UNARY_NOT:
1883 return is_constant_expression(expression->unary.value);
1885 case EXPR_UNARY_CAST: {
1886 type_t *const type = skip_typeref(expression->base.type);
1887 if (is_type_scalar(type))
1888 return is_constant_expression(expression->unary.value);
1889 if (!is_type_valid(type))
1890 return EXPR_CLASS_ERROR;
1891 return EXPR_CLASS_VARIABLE;
1894 case EXPR_BINARY_ADD:
1895 case EXPR_BINARY_SUB:
1896 case EXPR_BINARY_MUL:
1897 case EXPR_BINARY_DIV:
1898 case EXPR_BINARY_MOD:
1899 case EXPR_BINARY_EQUAL:
1900 case EXPR_BINARY_NOTEQUAL:
1901 case EXPR_BINARY_LESS:
1902 case EXPR_BINARY_LESSEQUAL:
1903 case EXPR_BINARY_GREATER:
1904 case EXPR_BINARY_GREATEREQUAL:
1905 case EXPR_BINARY_BITWISE_AND:
1906 case EXPR_BINARY_BITWISE_OR:
1907 case EXPR_BINARY_BITWISE_XOR:
1908 case EXPR_BINARY_SHIFTLEFT:
1909 case EXPR_BINARY_SHIFTRIGHT:
1910 case EXPR_BINARY_ISGREATER:
1911 case EXPR_BINARY_ISGREATEREQUAL:
1912 case EXPR_BINARY_ISLESS:
1913 case EXPR_BINARY_ISLESSEQUAL:
1914 case EXPR_BINARY_ISLESSGREATER:
1915 case EXPR_BINARY_ISUNORDERED: {
1916 expression_classification_t const l = is_constant_expression(expression->binary.left);
1917 expression_classification_t const r = is_constant_expression(expression->binary.right);
1918 return l < r ? l : r;
1921 case EXPR_BINARY_LOGICAL_AND: {
1922 expression_t const *const left = expression->binary.left;
1923 expression_classification_t const lclass = is_constant_expression(left);
1924 if (lclass != EXPR_CLASS_CONSTANT)
1926 if (!fold_constant_to_bool(left))
1927 return EXPR_CLASS_CONSTANT;
1928 return is_constant_expression(expression->binary.right);
1931 case EXPR_BINARY_LOGICAL_OR: {
1932 expression_t const *const left = expression->binary.left;
1933 expression_classification_t const lclass = is_constant_expression(left);
1934 if (lclass != EXPR_CLASS_CONSTANT)
1936 if (fold_constant_to_bool(left))
1937 return EXPR_CLASS_CONSTANT;
1938 return is_constant_expression(expression->binary.right);
1941 case EXPR_COMPOUND_LITERAL:
1942 return is_constant_initializer(expression->compound_literal.initializer);
1944 case EXPR_CONDITIONAL: {
1945 expression_t *const condition = expression->conditional.condition;
1946 expression_classification_t const cclass = is_constant_expression(condition);
1947 if (cclass != EXPR_CLASS_CONSTANT)
1950 if (fold_constant_to_bool(condition)) {
1951 expression_t const *const t = expression->conditional.true_expression;
1952 return t == NULL ? EXPR_CLASS_CONSTANT : is_constant_expression(t);
1954 return is_constant_expression(expression->conditional.false_expression);
1959 return EXPR_CLASS_ERROR;
1961 panic("invalid expression found (is constant expression)");
1966 obstack_init(&ast_obstack);
1971 obstack_free(&ast_obstack, NULL);