2 * This file is part of cparser.
3 * Copyright (C) 2012 Matthias Braun <matze@braunis.de>
11 #include "lang_features.h"
13 #include "separator_t.h"
18 #include "adt/error.h"
21 struct obstack ast_obstack;
24 static int case_indent;
26 bool print_implicit_casts = false;
27 bool print_parenthesis = false;
29 static void print_statement(const statement_t *statement);
30 static void print_expression_prec(const expression_t *expression, unsigned prec);
32 void change_indent(int delta)
38 void print_indent(void)
40 for (int i = 0; i < indent; ++i)
45 * Returns 1 if a given precedence level has right-to-left
46 * associativity, else 0.
48 * @param precedence the operator precedence
50 static int right_to_left(unsigned precedence)
54 case PREC_CONDITIONAL:
64 * Return the precedence of an expression given by its kind.
66 * @param kind the expression kind
68 static unsigned get_expression_precedence(expression_kind_t kind)
70 static const unsigned prec[] = {
71 [EXPR_ERROR] = PREC_PRIMARY,
72 [EXPR_REFERENCE] = PREC_PRIMARY,
73 [EXPR_ENUM_CONSTANT] = PREC_PRIMARY,
74 [EXPR_LITERAL_INTEGER] = PREC_PRIMARY,
75 [EXPR_LITERAL_FLOATINGPOINT] = PREC_PRIMARY,
76 [EXPR_LITERAL_CHARACTER] = PREC_PRIMARY,
77 [EXPR_LITERAL_MS_NOOP] = PREC_PRIMARY,
78 [EXPR_STRING_LITERAL] = PREC_PRIMARY,
79 [EXPR_COMPOUND_LITERAL] = PREC_UNARY,
80 [EXPR_CALL] = PREC_POSTFIX,
81 [EXPR_CONDITIONAL] = PREC_CONDITIONAL,
82 [EXPR_SELECT] = PREC_POSTFIX,
83 [EXPR_ARRAY_ACCESS] = PREC_POSTFIX,
84 [EXPR_SIZEOF] = PREC_UNARY,
85 [EXPR_CLASSIFY_TYPE] = PREC_UNARY,
86 [EXPR_ALIGNOF] = PREC_UNARY,
88 [EXPR_FUNCNAME] = PREC_PRIMARY,
89 [EXPR_BUILTIN_CONSTANT_P] = PREC_PRIMARY,
90 [EXPR_BUILTIN_TYPES_COMPATIBLE_P] = PREC_PRIMARY,
91 [EXPR_OFFSETOF] = PREC_PRIMARY,
92 [EXPR_VA_START] = PREC_PRIMARY,
93 [EXPR_VA_ARG] = PREC_PRIMARY,
94 [EXPR_VA_COPY] = PREC_PRIMARY,
95 [EXPR_STATEMENT] = PREC_PRIMARY,
96 [EXPR_LABEL_ADDRESS] = PREC_PRIMARY,
98 [EXPR_UNARY_NEGATE] = PREC_UNARY,
99 [EXPR_UNARY_PLUS] = PREC_UNARY,
100 [EXPR_UNARY_COMPLEMENT] = PREC_UNARY,
101 [EXPR_UNARY_NOT] = PREC_UNARY,
102 [EXPR_UNARY_DEREFERENCE] = PREC_UNARY,
103 [EXPR_UNARY_TAKE_ADDRESS] = PREC_UNARY,
104 [EXPR_UNARY_POSTFIX_INCREMENT] = PREC_POSTFIX,
105 [EXPR_UNARY_POSTFIX_DECREMENT] = PREC_POSTFIX,
106 [EXPR_UNARY_PREFIX_INCREMENT] = PREC_UNARY,
107 [EXPR_UNARY_PREFIX_DECREMENT] = PREC_UNARY,
108 [EXPR_UNARY_CAST] = PREC_UNARY,
109 [EXPR_UNARY_ASSUME] = PREC_PRIMARY,
110 [EXPR_UNARY_DELETE] = PREC_UNARY,
111 [EXPR_UNARY_DELETE_ARRAY] = PREC_UNARY,
112 [EXPR_UNARY_THROW] = PREC_ASSIGNMENT,
113 [EXPR_UNARY_IMAG] = PREC_UNARY,
114 [EXPR_UNARY_REAL] = PREC_UNARY,
116 [EXPR_BINARY_ADD] = PREC_ADDITIVE,
117 [EXPR_BINARY_SUB] = PREC_ADDITIVE,
118 [EXPR_BINARY_MUL] = PREC_MULTIPLICATIVE,
119 [EXPR_BINARY_DIV] = PREC_MULTIPLICATIVE,
120 [EXPR_BINARY_MOD] = PREC_MULTIPLICATIVE,
121 [EXPR_BINARY_EQUAL] = PREC_EQUALITY,
122 [EXPR_BINARY_NOTEQUAL] = PREC_EQUALITY,
123 [EXPR_BINARY_LESS] = PREC_RELATIONAL,
124 [EXPR_BINARY_LESSEQUAL] = PREC_RELATIONAL,
125 [EXPR_BINARY_GREATER] = PREC_RELATIONAL,
126 [EXPR_BINARY_GREATEREQUAL] = PREC_RELATIONAL,
127 [EXPR_BINARY_BITWISE_AND] = PREC_AND,
128 [EXPR_BINARY_BITWISE_OR] = PREC_OR,
129 [EXPR_BINARY_BITWISE_XOR] = PREC_XOR,
130 [EXPR_BINARY_LOGICAL_AND] = PREC_LOGICAL_AND,
131 [EXPR_BINARY_LOGICAL_OR] = PREC_LOGICAL_OR,
132 [EXPR_BINARY_SHIFTLEFT] = PREC_SHIFT,
133 [EXPR_BINARY_SHIFTRIGHT] = PREC_SHIFT,
134 [EXPR_BINARY_ASSIGN] = PREC_ASSIGNMENT,
135 [EXPR_BINARY_MUL_ASSIGN] = PREC_ASSIGNMENT,
136 [EXPR_BINARY_DIV_ASSIGN] = PREC_ASSIGNMENT,
137 [EXPR_BINARY_MOD_ASSIGN] = PREC_ASSIGNMENT,
138 [EXPR_BINARY_ADD_ASSIGN] = PREC_ASSIGNMENT,
139 [EXPR_BINARY_SUB_ASSIGN] = PREC_ASSIGNMENT,
140 [EXPR_BINARY_SHIFTLEFT_ASSIGN] = PREC_ASSIGNMENT,
141 [EXPR_BINARY_SHIFTRIGHT_ASSIGN] = PREC_ASSIGNMENT,
142 [EXPR_BINARY_BITWISE_AND_ASSIGN] = PREC_ASSIGNMENT,
143 [EXPR_BINARY_BITWISE_XOR_ASSIGN] = PREC_ASSIGNMENT,
144 [EXPR_BINARY_BITWISE_OR_ASSIGN] = PREC_ASSIGNMENT,
145 [EXPR_BINARY_COMMA] = PREC_EXPRESSION,
147 [EXPR_BINARY_ISGREATER] = PREC_PRIMARY,
148 [EXPR_BINARY_ISGREATEREQUAL] = PREC_PRIMARY,
149 [EXPR_BINARY_ISLESS] = PREC_PRIMARY,
150 [EXPR_BINARY_ISLESSEQUAL] = PREC_PRIMARY,
151 [EXPR_BINARY_ISLESSGREATER] = PREC_PRIMARY,
152 [EXPR_BINARY_ISUNORDERED] = PREC_PRIMARY
154 assert((size_t)kind < lengthof(prec));
155 unsigned res = prec[kind];
157 assert(res != PREC_BOTTOM);
162 * Print a quoted string constant.
164 * @param string the string constant
165 * @param border the border char
167 static void print_quoted_string(const string_t *const string, char border)
169 print_string(get_string_encoding_prefix(string->encoding));
172 const char *end = string->begin + string->size;
173 for (const char *c = string->begin; c != end; ++c) {
179 case '\\': print_string("\\\\"); break;
180 case '\a': print_string("\\a"); break;
181 case '\b': print_string("\\b"); break;
182 case '\f': print_string("\\f"); break;
183 case '\n': print_string("\\n"); break;
184 case '\r': print_string("\\r"); break;
185 case '\t': print_string("\\t"); break;
186 case '\v': print_string("\\v"); break;
187 case '\?': print_string("\\?"); break;
189 if (c_mode & _GNUC) {
190 print_string("\\e"); break;
194 if ((unsigned)tc < 0x80 && !isprint(tc)) {
195 print_format("\\%03o", (unsigned)tc);
205 static void print_string_literal(string_literal_expression_t const *const literal, char const delimiter)
207 print_quoted_string(&literal->value, delimiter);
210 static void print_literal(const literal_expression_t *literal)
212 switch (literal->base.kind) {
213 case EXPR_LITERAL_MS_NOOP:
214 print_string("__noop");
217 case EXPR_LITERAL_BOOLEAN:
218 case EXPR_LITERAL_FLOATINGPOINT:
219 case EXPR_LITERAL_INTEGER:
220 print_string(literal->value.begin);
226 print_string("INVALID LITERAL KIND");
230 * Prints a predefined symbol.
232 static void print_funcname(const funcname_expression_t *funcname)
235 switch (funcname->kind) {
236 case FUNCNAME_FUNCTION: s = (c_mode & _C99) ? "__func__" : "__FUNCTION__"; break;
237 case FUNCNAME_PRETTY_FUNCTION: s = "__PRETTY_FUNCTION__"; break;
238 case FUNCNAME_FUNCSIG: s = "__FUNCSIG__"; break;
239 case FUNCNAME_FUNCDNAME: s = "__FUNCDNAME__"; break;
244 static void print_compound_literal(
245 const compound_literal_expression_t *expression)
248 print_type(expression->type);
250 print_initializer(expression->initializer);
253 static void print_assignment_expression(const expression_t *const expr)
255 print_expression_prec(expr, PREC_ASSIGNMENT);
259 * Prints a call expression.
261 * @param call the call expression
263 static void print_call_expression(const call_expression_t *call)
265 print_expression_prec(call->function, PREC_POSTFIX);
267 separator_t sep = { "", ", " };
268 for (call_argument_t const *arg = call->arguments; arg; arg = arg->next) {
269 print_string(sep_next(&sep));
270 print_assignment_expression(arg->expression);
276 * Prints a binary expression.
278 * @param binexpr the binary expression
280 static void print_binary_expression(const binary_expression_t *binexpr)
282 unsigned prec = get_expression_precedence(binexpr->base.kind);
283 int r2l = right_to_left(prec);
285 print_expression_prec(binexpr->left, prec + r2l);
287 switch (binexpr->base.kind) {
288 case EXPR_BINARY_COMMA: op = ", "; break;
289 case EXPR_BINARY_ASSIGN: op = " = "; break;
290 case EXPR_BINARY_ADD: op = " + "; break;
291 case EXPR_BINARY_SUB: op = " - "; break;
292 case EXPR_BINARY_MUL: op = " * "; break;
293 case EXPR_BINARY_MOD: op = " % "; break;
294 case EXPR_BINARY_DIV: op = " / "; break;
295 case EXPR_BINARY_BITWISE_OR: op = " | "; break;
296 case EXPR_BINARY_BITWISE_AND: op = " & "; break;
297 case EXPR_BINARY_BITWISE_XOR: op = " ^ "; break;
298 case EXPR_BINARY_LOGICAL_OR: op = " || "; break;
299 case EXPR_BINARY_LOGICAL_AND: op = " && "; break;
300 case EXPR_BINARY_NOTEQUAL: op = " != "; break;
301 case EXPR_BINARY_EQUAL: op = " == "; break;
302 case EXPR_BINARY_LESS: op = " < "; break;
303 case EXPR_BINARY_LESSEQUAL: op = " <= "; break;
304 case EXPR_BINARY_GREATER: op = " > "; break;
305 case EXPR_BINARY_GREATEREQUAL: op = " >= "; break;
306 case EXPR_BINARY_SHIFTLEFT: op = " << "; break;
307 case EXPR_BINARY_SHIFTRIGHT: op = " >> "; break;
309 case EXPR_BINARY_ADD_ASSIGN: op = " += "; break;
310 case EXPR_BINARY_SUB_ASSIGN: op = " -= "; break;
311 case EXPR_BINARY_MUL_ASSIGN: op = " *= "; break;
312 case EXPR_BINARY_MOD_ASSIGN: op = " %= "; break;
313 case EXPR_BINARY_DIV_ASSIGN: op = " /= "; break;
314 case EXPR_BINARY_BITWISE_OR_ASSIGN: op = " |= "; break;
315 case EXPR_BINARY_BITWISE_AND_ASSIGN: op = " &= "; break;
316 case EXPR_BINARY_BITWISE_XOR_ASSIGN: op = " ^= "; break;
317 case EXPR_BINARY_SHIFTLEFT_ASSIGN: op = " <<= "; break;
318 case EXPR_BINARY_SHIFTRIGHT_ASSIGN: op = " >>= "; break;
319 default: panic("invalid binexpression found");
322 print_expression_prec(binexpr->right, prec + 1 - r2l);
326 * Prints an unary expression.
328 * @param unexpr the unary expression
330 static void print_unary_expression(const unary_expression_t *unexpr)
332 unsigned prec = get_expression_precedence(unexpr->base.kind);
333 switch (unexpr->base.kind) {
334 case EXPR_UNARY_NEGATE: print_char ('-' ); break;
335 case EXPR_UNARY_PLUS: print_char ('+' ); break;
336 case EXPR_UNARY_NOT: print_char ('!' ); break;
337 case EXPR_UNARY_COMPLEMENT: print_char ('~' ); break;
338 case EXPR_UNARY_PREFIX_INCREMENT: print_string("++"); break;
339 case EXPR_UNARY_PREFIX_DECREMENT: print_string("--"); break;
340 case EXPR_UNARY_DEREFERENCE: print_char ('*' ); break;
341 case EXPR_UNARY_TAKE_ADDRESS: print_char ('&' ); break;
342 case EXPR_UNARY_DELETE: print_string("delete "); break;
343 case EXPR_UNARY_DELETE_ARRAY: print_string("delete [] "); break;
344 case EXPR_UNARY_REAL: print_string("__real__ "); break;
345 case EXPR_UNARY_IMAG: print_string("__imag__ "); break;
347 case EXPR_UNARY_POSTFIX_INCREMENT:
348 print_expression_prec(unexpr->value, prec);
351 case EXPR_UNARY_POSTFIX_DECREMENT:
352 print_expression_prec(unexpr->value, prec);
355 case EXPR_UNARY_CAST:
357 print_type(unexpr->base.type);
360 case EXPR_UNARY_ASSUME:
361 print_string("__assume(");
362 print_assignment_expression(unexpr->value);
365 case EXPR_UNARY_THROW:
366 if (unexpr->value == NULL) {
367 print_string("throw");
370 print_string("throw ");
374 panic("invalid unary expression found");
376 print_expression_prec(unexpr->value, prec);
380 * Prints a reference expression.
382 * @param ref the reference expression
384 static void print_reference_expression(const reference_expression_t *ref)
386 print_string(ref->entity->base.symbol->string);
390 * Prints a label address expression.
392 * @param ref the reference expression
394 static void print_label_address_expression(const label_address_expression_t *le)
396 print_format("&&%s", le->label->base.symbol->string);
400 * Prints an array expression.
402 * @param expression the array expression
404 static void print_array_expression(const array_access_expression_t *expression)
406 if (!expression->flipped) {
407 print_expression_prec(expression->array_ref, PREC_POSTFIX);
409 print_expression(expression->index);
412 print_expression_prec(expression->index, PREC_POSTFIX);
414 print_expression(expression->array_ref);
420 * Prints a typeproperty expression (sizeof or __alignof__).
422 * @param expression the type property expression
424 static void print_typeprop_expression(const typeprop_expression_t *expression)
426 switch (expression->base.kind) {
427 case EXPR_SIZEOF: print_string("sizeof"); break;
428 case EXPR_ALIGNOF: print_string(c_mode & _C11 ? "_Alignof" : "__alignof__"); break;
429 default: panic("invalid typeprop kind");
431 if (expression->tp_expression != NULL) {
432 /* PREC_TOP: always print the '()' here, sizeof x is right but unusual */
433 print_expression_prec(expression->tp_expression, PREC_TOP);
436 print_type(expression->type);
442 * Prints a builtin constant expression.
444 * @param expression the builtin constant expression
446 static void print_builtin_constant(const builtin_constant_expression_t *expression)
448 print_string("__builtin_constant_p(");
449 print_assignment_expression(expression->value);
454 * Prints a builtin types compatible expression.
456 * @param expression the builtin types compatible expression
458 static void print_builtin_types_compatible(
459 const builtin_types_compatible_expression_t *expression)
461 print_string("__builtin_types_compatible_p(");
462 print_type(expression->left);
464 print_type(expression->right);
469 * Prints a conditional expression.
471 * @param expression the conditional expression
473 static void print_conditional(const conditional_expression_t *expression)
475 print_expression_prec(expression->condition, PREC_LOGICAL_OR);
476 if (expression->true_expression != NULL) {
478 print_expression_prec(expression->true_expression, PREC_EXPRESSION);
481 print_string(" ?: ");
483 precedence_t prec = c_mode & _CXX ? PREC_ASSIGNMENT : PREC_CONDITIONAL;
484 print_expression_prec(expression->false_expression, prec);
488 * Prints a va_start expression.
490 * @param expression the va_start expression
492 static void print_va_start(const va_start_expression_t *const expression)
494 print_string("__builtin_va_start(");
495 print_assignment_expression(expression->ap);
497 print_assignment_expression(expression->parameter);
502 * Prints a va_arg expression.
504 * @param expression the va_arg expression
506 static void print_va_arg(const va_arg_expression_t *expression)
508 print_string("__builtin_va_arg(");
509 print_assignment_expression(expression->ap);
511 print_type(expression->base.type);
516 * Prints a va_copy expression.
518 * @param expression the va_copy expression
520 static void print_va_copy(const va_copy_expression_t *expression)
522 print_string("__builtin_va_copy(");
523 print_assignment_expression(expression->dst);
525 print_assignment_expression(expression->src);
530 * Prints a select expression (. or ->).
532 * @param expression the select expression
534 static void print_select(const select_expression_t *expression)
536 print_expression_prec(expression->compound, PREC_POSTFIX);
537 /* do not print anything for anonymous struct/union selects
538 * FIXME: if the anonymous select was a '->' this will print '.'
540 if (expression->compound_entry->base.symbol == NULL)
543 if (is_type_pointer(skip_typeref(expression->compound->base.type))) {
548 print_string(expression->compound_entry->base.symbol->string);
552 * Prints a type classify expression.
554 * @param expr the type classify expression
556 static void print_classify_type_expression(
557 const classify_type_expression_t *const expr)
559 print_string("__builtin_classify_type(");
560 print_assignment_expression(expr->type_expression);
565 * Prints a designator.
567 * @param designator the designator
569 static void print_designator(const designator_t *designator)
571 for ( ; designator != NULL; designator = designator->next) {
572 if (designator->symbol == NULL) {
574 print_expression(designator->array_index);
575 if (designator->range_last) {
576 print_string(" ... ");
577 print_expression(designator->range_last);
582 print_string(designator->symbol->string);
588 * Prints an offsetof expression.
590 * @param expression the offset expression
592 static void print_offsetof_expression(const offsetof_expression_t *expression)
594 print_string("__builtin_offsetof(");
595 print_type(expression->type);
597 print_designator(expression->designator);
602 * Prints a statement expression.
604 * @param expression the statement expression
606 static void print_statement_expression(const statement_expression_t *expression)
609 print_statement(expression->statement);
613 static bool needs_parentheses(expression_t const *const expr, unsigned const top_prec)
615 if (expr->base.parenthesized)
618 if (top_prec > get_expression_precedence(expr->base.kind))
621 if (print_parenthesis && top_prec != PREC_BOTTOM) {
622 switch (expr->kind) {
623 case EXPR_ENUM_CONSTANT:
625 case EXPR_LITERAL_CASES:
626 case EXPR_LITERAL_CHARACTER:
628 case EXPR_STRING_LITERAL:
629 /* Do not print () around subexpressions consisting of a single token. */
641 * Prints an expression with parenthesis if needed.
643 * @param expression the expression to print
644 * @param top_prec the precedence of the user of this expression.
646 static void print_expression_prec(expression_t const *expr, unsigned const top_prec)
648 if (expr->kind == EXPR_UNARY_CAST && expr->base.implicit && !print_implicit_casts) {
649 expr = expr->unary.value;
652 bool const parenthesized = needs_parentheses(expr, top_prec);
656 switch (expr->kind) {
658 case EXPR_SIZEOF: print_typeprop_expression( &expr->typeprop); break;
659 case EXPR_ARRAY_ACCESS: print_array_expression( &expr->array_access); break;
660 case EXPR_BINARY_CASES: print_binary_expression( &expr->binary); break;
661 case EXPR_BUILTIN_CONSTANT_P: print_builtin_constant( &expr->builtin_constant); break;
662 case EXPR_BUILTIN_TYPES_COMPATIBLE_P: print_builtin_types_compatible(&expr->builtin_types_compatible); break;
663 case EXPR_CALL: print_call_expression( &expr->call); break;
664 case EXPR_CLASSIFY_TYPE: print_classify_type_expression(&expr->classify_type); break;
665 case EXPR_COMPOUND_LITERAL: print_compound_literal( &expr->compound_literal); break;
666 case EXPR_CONDITIONAL: print_conditional( &expr->conditional); break;
667 case EXPR_ERROR: print_string("$error$"); break;
668 case EXPR_FUNCNAME: print_funcname( &expr->funcname); break;
669 case EXPR_LABEL_ADDRESS: print_label_address_expression(&expr->label_address); break;
670 case EXPR_LITERAL_CASES: print_literal( &expr->literal); break;
671 case EXPR_LITERAL_CHARACTER: print_string_literal( &expr->string_literal, '\''); break;
672 case EXPR_OFFSETOF: print_offsetof_expression( &expr->offsetofe); break;
674 case EXPR_ENUM_CONSTANT: print_reference_expression( &expr->reference); break;
675 case EXPR_SELECT: print_select( &expr->select); break;
676 case EXPR_STATEMENT: print_statement_expression( &expr->statement); break;
677 case EXPR_STRING_LITERAL: print_string_literal( &expr->string_literal, '"'); break;
678 case EXPR_UNARY_CASES: print_unary_expression( &expr->unary); break;
679 case EXPR_VA_ARG: print_va_arg( &expr->va_arge); break;
680 case EXPR_VA_COPY: print_va_copy( &expr->va_copye); break;
681 case EXPR_VA_START: print_va_start( &expr->va_starte); break;
687 static void print_indented_statement(statement_t const *const stmt)
689 switch (stmt->kind) {
690 case STATEMENT_LABEL:
693 case STATEMENT_CASE_LABEL:
694 for (int i = 0; i != case_indent; ++i)
702 print_statement(stmt);
706 * Print an compound statement.
708 * @param block the compound statement
710 static void print_compound_statement(const compound_statement_t *block)
715 for (statement_t const *stmt = block->statements; stmt; stmt = stmt->base.next) {
716 print_indented_statement(stmt);
726 * Print a return statement.
728 * @param statement the return statement
730 static void print_return_statement(const return_statement_t *statement)
732 expression_t const *const val = statement->value;
734 print_string("return ");
735 print_expression(val);
738 print_string("return;");
743 * Print an expression statement.
745 * @param statement the expression statement
747 static void print_expression_statement(const expression_statement_t *statement)
749 print_expression(statement->expression);
754 * Print a computed goto statement.
756 * @param statement the computed goto statement
758 static void print_computed_goto_statement(computed_goto_statement_t const *const stmt)
760 print_string("goto *");
761 print_expression(stmt->expression);
766 * Print a goto statement.
768 * @param statement the goto statement
770 static void print_goto_statement(const goto_statement_t *statement)
772 print_string("goto ");
773 print_string(statement->label->base.symbol->string);
778 * Print a label statement.
780 * @param statement the label statement
782 static void print_label_statement(const label_statement_t *statement)
784 print_format("%s:\n", statement->label->base.symbol->string);
785 print_indented_statement(statement->statement);
788 static void print_inner_statement(statement_t const *const stmt)
790 if (stmt->kind == STATEMENT_COMPOUND) {
792 print_compound_statement(&stmt->compound);
796 print_indented_statement(stmt);
801 static void print_after_inner_statement(statement_t const *const stmt)
803 if (stmt->kind == STATEMENT_COMPOUND) {
812 * Print an if statement.
814 * @param statement the if statement
816 static void print_if_statement(const if_statement_t *statement)
818 print_string("if (");
819 print_expression(statement->condition);
821 print_inner_statement(statement->true_statement);
823 statement_t const *const f = statement->false_statement;
825 print_after_inner_statement(statement->true_statement);
826 print_string("else");
827 if (f->kind == STATEMENT_IF) {
829 print_if_statement(&f->ifs);
831 print_inner_statement(f);
837 * Print a switch statement.
839 * @param statement the switch statement
841 static void print_switch_statement(const switch_statement_t *statement)
843 int const old_case_indent = case_indent;
844 case_indent = indent;
846 print_string("switch (");
847 print_expression(statement->expression);
849 print_inner_statement(statement->body);
851 case_indent = old_case_indent;
855 * Print a case label (including the default label).
857 * @param statement the case label statement
859 static void print_case_label(const case_label_statement_t *statement)
861 if (statement->expression == NULL) {
862 print_string("default:\n");
864 print_string("case ");
865 print_expression(statement->expression);
866 if (statement->end_range != NULL) {
867 print_string(" ... ");
868 print_expression(statement->end_range);
872 print_indented_statement(statement->statement);
875 static void print_typedef(const entity_t *entity)
877 print_string("typedef ");
878 print_type_ext(entity->typedefe.type, entity->base.symbol, NULL);
883 * returns true if the entity is a compiler generated one and has no real
884 * correspondenc in the source file
886 static bool is_generated_entity(const entity_t *entity)
888 if (entity->kind == ENTITY_TYPEDEF)
889 return entity->typedefe.builtin;
891 if (is_declaration(entity))
892 return entity->declaration.implicit;
898 * Print a declaration statement.
900 * @param statement the statement
902 static void print_declaration_statement(
903 const declaration_statement_t *statement)
906 entity_t *entity = statement->declarations_begin;
907 if (entity == NULL) {
908 print_string("/* empty declaration statement */");
912 entity_t *const end = statement->declarations_end->base.next;
913 for (; entity != end; entity = entity->base.next) {
914 if (entity->kind == ENTITY_ENUM_VALUE)
916 if (is_generated_entity(entity))
926 print_entity(entity);
931 * Print a do-while statement.
933 * @param statement the statement
935 static void print_do_while_statement(const do_while_statement_t *statement)
938 print_inner_statement(statement->body);
939 print_after_inner_statement(statement->body);
940 print_string("while (");
941 print_expression(statement->condition);
946 * Print a for statement.
948 * @param statement the statement
950 static void print_for_statement(const for_statement_t *statement)
952 if (statement->initialisation || statement->scope.entities || !statement->condition || statement->step) {
953 print_string("for (");
954 if (statement->initialisation != NULL) {
955 print_expression(statement->initialisation);
958 entity_t const *entity = statement->scope.entities;
959 for (; entity != NULL; entity = entity->base.next) {
960 if (is_generated_entity(entity))
962 /* FIXME display of multiple declarations is wrong */
963 print_declaration(entity);
966 if (statement->condition != NULL) {
968 print_expression(statement->condition);
971 if (statement->step != NULL) {
973 print_expression(statement->step);
976 print_string("while (");
977 print_expression(statement->condition);
980 print_inner_statement(statement->body);
984 * Print assembler arguments.
986 * @param arguments the arguments
988 static void print_asm_arguments(asm_argument_t const *const arguments)
991 separator_t sep = { " ", ", " };
992 for (asm_argument_t const *i = arguments; i; i = i->next) {
993 print_string(sep_next(&sep));
995 print_format("[%s] ", i->symbol->string);
996 print_quoted_string(&i->constraints, '"');
998 print_expression(i->expression);
1004 * Print assembler clobbers.
1006 * @param clobbers the clobbers
1008 static void print_asm_clobbers(asm_clobber_t const *const clobbers)
1011 separator_t sep = { " ", ", " };
1012 for (asm_clobber_t const *i = clobbers; i; i = i->next) {
1013 print_string(sep_next(&sep));
1014 print_quoted_string(&i->clobber, '"');
1018 static void print_asm_labels(asm_label_t const *const labels)
1021 separator_t sep = { " ", ", " };
1022 for (asm_label_t const *i = labels; i; i = i->next) {
1023 print_string(sep_next(&sep));
1024 print_string(i->label->base.symbol->string);
1029 * Print an assembler statement.
1031 * @param stmt the statement
1033 static void print_asm_statement(asm_statement_t const *const stmt)
1035 print_string("asm");
1036 if (stmt->is_volatile) print_string(" volatile");
1037 if (stmt->labels) print_string(" goto");
1039 print_quoted_string(&stmt->asm_text, '"');
1043 stmt->clobbers ? 3 :
1047 if (n >= 1) print_asm_arguments(stmt->outputs);
1048 if (n >= 2) print_asm_arguments(stmt->inputs);
1049 if (n >= 3) print_asm_clobbers( stmt->clobbers);
1050 if (n >= 4) print_asm_labels( stmt->labels);
1056 * Print a microsoft __try statement.
1058 * @param statement the statement
1060 static void print_ms_try_statement(const ms_try_statement_t *statement)
1062 print_string("__try");
1063 print_inner_statement(statement->try_statement);
1064 print_after_inner_statement(statement->try_statement);
1065 if (statement->except_expression != NULL) {
1066 print_string("__except(");
1067 print_expression(statement->except_expression);
1070 print_string("__finally");
1072 print_inner_statement(statement->final_statement);
1076 * Print a microsoft __leave statement.
1078 * @param statement the statement
1080 static void print_leave_statement(const leave_statement_t *statement)
1083 print_string("__leave;");
1087 * Print a statement.
1089 * @param statement the statement
1091 void print_statement(statement_t const *const stmt)
1093 switch (stmt->kind) {
1094 case STATEMENT_ASM: print_asm_statement( &stmt->asms); break;
1095 case STATEMENT_BREAK: print_string("break;"); break;
1096 case STATEMENT_CASE_LABEL: print_case_label( &stmt->case_label); break;
1097 case STATEMENT_COMPOUND: print_compound_statement( &stmt->compound); break;
1098 case STATEMENT_COMPUTED_GOTO: print_computed_goto_statement(&stmt->computed_goto); break;
1099 case STATEMENT_CONTINUE: print_string("continue;"); break;
1100 case STATEMENT_DECLARATION: print_declaration_statement( &stmt->declaration); break;
1101 case STATEMENT_DO_WHILE: print_do_while_statement( &stmt->do_while); break;
1102 case STATEMENT_EMPTY: print_char(';'); break;
1103 case STATEMENT_ERROR: print_string("$error statement$"); break;
1104 case STATEMENT_EXPRESSION: print_expression_statement( &stmt->expression); break;
1105 case STATEMENT_FOR: print_for_statement( &stmt->fors); break;
1106 case STATEMENT_GOTO: print_goto_statement( &stmt->gotos); break;
1107 case STATEMENT_IF: print_if_statement( &stmt->ifs); break;
1108 case STATEMENT_LABEL: print_label_statement( &stmt->label); break;
1109 case STATEMENT_LEAVE: print_leave_statement( &stmt->leave); break;
1110 case STATEMENT_MS_TRY: print_ms_try_statement( &stmt->ms_try); break;
1111 case STATEMENT_RETURN: print_return_statement( &stmt->returns); break;
1112 case STATEMENT_SWITCH: print_switch_statement( &stmt->switchs); break;
1117 * Print a storage class.
1119 * @param storage_class the storage class
1121 static void print_storage_class(storage_class_tag_t storage_class)
1123 switch (storage_class) {
1124 case STORAGE_CLASS_NONE: return;
1125 case STORAGE_CLASS_TYPEDEF: print_string("typedef "); return;
1126 case STORAGE_CLASS_EXTERN: print_string("extern "); return;
1127 case STORAGE_CLASS_STATIC: print_string("static "); return;
1128 case STORAGE_CLASS_AUTO: print_string("auto "); return;
1129 case STORAGE_CLASS_REGISTER: print_string("register "); return;
1131 panic("invalid storage class");
1135 * Print an initializer.
1137 * @param initializer the initializer
1139 void print_initializer(const initializer_t *initializer)
1141 if (initializer == NULL) {
1146 switch (initializer->kind) {
1147 case INITIALIZER_STRING:
1148 case INITIALIZER_VALUE:
1149 print_assignment_expression(initializer->value.value);
1152 case INITIALIZER_LIST: {
1154 const initializer_list_t *list = &initializer->list;
1156 for (size_t i = 0 ; i < list->len; ++i) {
1157 const initializer_t *sub_init = list->initializers[i];
1158 print_initializer(list->initializers[i]);
1159 if (i < list->len-1) {
1160 if (sub_init == NULL || sub_init->kind != INITIALIZER_DESIGNATOR)
1168 case INITIALIZER_DESIGNATOR:
1169 print_designator(initializer->designator.designator);
1170 print_string(" = ");
1174 panic("invalid initializer kind found");
1179 * Print microsoft extended declaration modifiers.
1181 static void print_ms_modifiers(const declaration_t *declaration)
1183 if ((c_mode & _MS) == 0)
1186 decl_modifiers_t modifiers = declaration->modifiers;
1188 separator_t sep = { "__declspec(", ", " };
1190 if (declaration->base.kind == ENTITY_VARIABLE) {
1191 variable_t *variable = (variable_t*)declaration;
1192 if (variable->alignment != 0
1193 || variable->get_property_sym != NULL
1194 || variable->put_property_sym != NULL) {
1195 if (variable->alignment != 0) {
1196 print_format("%salign(%u)", sep_next(&sep), variable->alignment);
1198 if (variable->get_property_sym != NULL
1199 || variable->put_property_sym != NULL) {
1201 print_format("%sproperty(", sep_next(&sep));
1202 if (variable->get_property_sym != NULL) {
1203 print_format("get=%s", variable->get_property_sym->string);
1206 if (variable->put_property_sym != NULL)
1207 print_format("%sput=%s", comma, variable->put_property_sym->string);
1213 /* DM_FORCEINLINE handled outside. */
1214 if ((modifiers & ~DM_FORCEINLINE) != 0) {
1215 if (modifiers & DM_DLLIMPORT) {
1216 print_format("%sdllimport", sep_next(&sep));
1218 if (modifiers & DM_DLLEXPORT) {
1219 print_format("%sdllexport", sep_next(&sep));
1221 if (modifiers & DM_THREAD) {
1222 print_format("%sthread", sep_next(&sep));
1224 if (modifiers & DM_NAKED) {
1225 print_format("%snaked", sep_next(&sep));
1227 if (modifiers & DM_THREAD) {
1228 print_format("%sthread", sep_next(&sep));
1230 if (modifiers & DM_SELECTANY) {
1231 print_format("%sselectany", sep_next(&sep));
1233 if (modifiers & DM_NOTHROW) {
1234 print_format("%snothrow", sep_next(&sep));
1236 if (modifiers & DM_NORETURN) {
1237 print_format("%snoreturn", sep_next(&sep));
1239 if (modifiers & DM_NOINLINE) {
1240 print_format("%snoinline", sep_next(&sep));
1242 if (modifiers & DM_DEPRECATED) {
1243 print_format("%sdeprecated", sep_next(&sep));
1244 if (declaration->deprecated_string != NULL)
1245 print_format("(\"%s\")",
1246 declaration->deprecated_string);
1248 if (modifiers & DM_RESTRICT) {
1249 print_format("%srestrict", sep_next(&sep));
1251 if (modifiers & DM_NOALIAS) {
1252 print_format("%snoalias", sep_next(&sep));
1256 if (!sep_at_first(&sep))
1261 static void print_scope(const scope_t *scope)
1263 const entity_t *entity = scope->entities;
1264 for ( ; entity != NULL; entity = entity->base.next) {
1266 print_entity(entity);
1271 static void print_namespace(const namespace_t *namespace)
1273 print_string("namespace ");
1274 if (namespace->base.symbol != NULL) {
1275 print_string(namespace->base.symbol->string);
1279 print_string("{\n");
1282 print_scope(&namespace->members);
1286 print_string("}\n");
1290 * Print a variable or function declaration
1292 void print_declaration(const entity_t *entity)
1294 assert(is_declaration(entity));
1295 const declaration_t *declaration = &entity->declaration;
1297 print_storage_class((storage_class_tag_t)declaration->declared_storage_class);
1298 if (entity->kind == ENTITY_FUNCTION) {
1299 function_t *function = (function_t*)declaration;
1300 if (function->is_inline) {
1301 if (declaration->modifiers & DM_FORCEINLINE) {
1302 print_string("__forceinline ");
1303 } else if (declaration->modifiers & DM_MICROSOFT_INLINE) {
1304 print_string("__inline ");
1306 print_string("inline ");
1310 //print_ms_modifiers(declaration);
1311 switch (entity->kind) {
1312 case ENTITY_FUNCTION:
1313 print_type_ext(entity->declaration.type, entity->base.symbol,
1314 &entity->function.parameters);
1316 if (entity->function.body != NULL) {
1318 print_indented_statement(entity->function.body);
1324 case ENTITY_VARIABLE:
1325 if (entity->variable.thread_local)
1326 print_string(c_mode & _C11 ? "_Thread_local " : "__thread ");
1327 print_type_ext(declaration->type, declaration->base.symbol, NULL);
1328 if (entity->variable.initializer != NULL) {
1329 print_string(" = ");
1330 print_initializer(entity->variable.initializer);
1334 case ENTITY_COMPOUND_MEMBER:
1335 print_type_ext(declaration->type, declaration->base.symbol, NULL);
1336 if (entity->compound_member.bitfield) {
1337 print_format(" : %u", entity->compound_member.bit_size);
1342 print_type_ext(declaration->type, declaration->base.symbol, NULL);
1349 * Prints an expression.
1351 * @param expression the expression
1353 void print_expression(const expression_t *expression)
1355 print_expression_prec(expression, PREC_BOTTOM);
1359 * Print a declaration.
1361 * @param declaration the declaration
1363 void print_entity(const entity_t *entity)
1365 if (entity->base.namespc != NAMESPACE_NORMAL && entity->base.symbol == NULL)
1368 switch ((entity_kind_tag_t)entity->kind) {
1369 case ENTITY_VARIABLE:
1370 case ENTITY_PARAMETER:
1371 case ENTITY_COMPOUND_MEMBER:
1372 case ENTITY_FUNCTION:
1373 print_declaration(entity);
1375 case ENTITY_TYPEDEF:
1376 print_typedef(entity);
1380 print_string("class ");
1381 print_string(entity->base.symbol->string);
1382 print_string("; /* TODO */\n");
1385 print_string("struct ");
1386 goto print_compound;
1388 print_string("union ");
1390 print_string(entity->base.symbol->string);
1391 if (entity->compound.complete) {
1393 print_compound_definition(&entity->compound);
1398 print_string("enum ");
1399 print_string(entity->base.symbol->string);
1401 print_enum_definition(&entity->enume);
1404 case ENTITY_NAMESPACE:
1405 print_namespace(&entity->namespacee);
1407 case ENTITY_LOCAL_LABEL:
1408 print_string("__label__ ");
1409 print_string(entity->base.symbol->string);
1413 case ENTITY_ENUM_VALUE:
1414 panic("print_entity used on unexpected entity type");
1416 panic("Invalid entity type encountered");
1420 * Print the AST of a translation unit.
1422 * @param unit the translation unit
1424 void print_ast(const translation_unit_t *unit)
1426 entity_t *entity = unit->scope.entities;
1427 for ( ; entity != NULL; entity = entity->base.next) {
1428 if (entity->kind == ENTITY_ENUM_VALUE)
1430 if (entity->base.namespc != NAMESPACE_NORMAL
1431 && entity->base.symbol == NULL)
1433 if (is_generated_entity(entity))
1437 print_entity(entity);
1442 expression_classification_t is_constant_initializer(const initializer_t *initializer)
1444 switch (initializer->kind) {
1445 case INITIALIZER_STRING:
1446 case INITIALIZER_DESIGNATOR:
1447 return EXPR_CLASS_CONSTANT;
1449 case INITIALIZER_VALUE:
1450 return is_linker_constant(initializer->value.value);
1452 case INITIALIZER_LIST: {
1453 expression_classification_t all = EXPR_CLASS_CONSTANT;
1454 for (size_t i = 0; i < initializer->list.len; ++i) {
1455 initializer_t *sub_initializer = initializer->list.initializers[i];
1456 expression_classification_t const cur = is_constant_initializer(sub_initializer);
1457 all = MIN(all, cur);
1462 panic("invalid initializer kind found");
1466 * Checks if an expression references an object with a constant/known location
1467 * to the linker. Example:
1468 * - "x", "*&x" with x being a global variable. The value of x need not be
1469 * constant but the address of x is.
1470 * - "a.b.c" when a has a constant/known location to the linker
1472 static expression_classification_t is_object_with_linker_constant_address(
1473 const expression_t *expression)
1475 switch (expression->kind) {
1476 case EXPR_UNARY_DEREFERENCE:
1477 return is_linker_constant(expression->unary.value);
1479 case EXPR_COMPOUND_LITERAL: {
1480 const compound_literal_expression_t *literal
1481 = &expression->compound_literal;
1482 return literal->global_scope ||
1483 ((literal->type->base.qualifiers & TYPE_QUALIFIER_CONST)
1484 && is_constant_initializer(literal->initializer));
1488 type_t *base_type = skip_typeref(expression->select.compound->base.type);
1489 if (is_type_pointer(base_type)) {
1491 return is_linker_constant(expression->select.compound);
1493 return is_object_with_linker_constant_address(expression->select.compound);
1497 case EXPR_ARRAY_ACCESS: {
1498 expression_classification_t const ref = is_linker_constant(expression->array_access.array_ref);
1499 expression_classification_t const idx = is_constant_expression(expression->array_access.index);
1500 return MIN(ref, idx);
1503 case EXPR_REFERENCE: {
1504 entity_t *entity = expression->reference.entity;
1505 if (!is_declaration(entity))
1506 return EXPR_CLASS_VARIABLE;
1508 switch ((storage_class_tag_t)entity->declaration.storage_class) {
1509 case STORAGE_CLASS_NONE:
1510 case STORAGE_CLASS_EXTERN:
1511 case STORAGE_CLASS_STATIC:
1513 entity->kind != ENTITY_VARIABLE ||
1514 !entity->variable.thread_local ? EXPR_CLASS_CONSTANT :
1515 EXPR_CLASS_VARIABLE;
1517 case STORAGE_CLASS_REGISTER:
1518 case STORAGE_CLASS_TYPEDEF:
1519 case STORAGE_CLASS_AUTO:
1522 return EXPR_CLASS_VARIABLE;
1526 return EXPR_CLASS_ERROR;
1529 return EXPR_CLASS_VARIABLE;
1533 expression_classification_t is_linker_constant(const expression_t *expression)
1535 switch (expression->kind) {
1536 case EXPR_STRING_LITERAL:
1538 case EXPR_LABEL_ADDRESS:
1539 return EXPR_CLASS_CONSTANT;
1541 case EXPR_COMPOUND_LITERAL:
1542 return is_constant_initializer(expression->compound_literal.initializer);
1544 case EXPR_UNARY_TAKE_ADDRESS:
1545 return is_object_with_linker_constant_address(expression->unary.value);
1547 case EXPR_UNARY_DEREFERENCE: {
1549 = revert_automatic_type_conversion(expression->unary.value);
1550 /* dereferencing a function is a NOP */
1551 if (is_type_function(real_type)) {
1552 return is_linker_constant(expression->unary.value);
1557 case EXPR_UNARY_CAST: {
1558 type_t *dest = skip_typeref(expression->base.type);
1559 if (!is_type_pointer(dest) && (
1560 dest->kind != TYPE_ATOMIC ||
1561 !(get_atomic_type_flags(dest->atomic.akind) & ATOMIC_TYPE_FLAG_INTEGER) ||
1562 get_atomic_type_size(dest->atomic.akind) < get_type_size(type_void_ptr)
1564 return is_constant_expression(expression);
1566 return is_linker_constant(expression->unary.value);
1569 case EXPR_BINARY_ADD:
1570 case EXPR_BINARY_SUB: {
1571 expression_t *const left = expression->binary.left;
1572 expression_t *const right = expression->binary.right;
1573 type_t *const ltype = skip_typeref(left->base.type);
1574 type_t *const rtype = skip_typeref(right->base.type);
1576 if (is_type_pointer(ltype)) {
1577 expression_classification_t const l = is_linker_constant(left);
1578 expression_classification_t const r = is_constant_expression(right);
1580 } else if (is_type_pointer(rtype)) {
1581 expression_classification_t const l = is_constant_expression(left);
1582 expression_classification_t const r = is_linker_constant(right);
1584 } else if (!is_type_valid(ltype) || !is_type_valid(rtype)) {
1585 return EXPR_CLASS_ERROR;
1587 return is_constant_expression(expression);
1591 case EXPR_REFERENCE: {
1592 entity_t *entity = expression->reference.entity;
1593 if (!is_declaration(entity))
1594 return EXPR_CLASS_VARIABLE;
1596 type_t *type = skip_typeref(entity->declaration.type);
1597 if (is_type_function(type))
1598 return EXPR_CLASS_CONSTANT;
1599 if (is_type_array(type)) {
1600 return is_object_with_linker_constant_address(expression);
1602 /* Prevent stray errors */
1603 if (!is_type_valid(type))
1604 return EXPR_CLASS_ERROR;
1605 return EXPR_CLASS_VARIABLE;
1608 case EXPR_ARRAY_ACCESS: {
1609 type_t *const type =
1610 skip_typeref(revert_automatic_type_conversion(expression));
1611 if (!is_type_array(type))
1612 return EXPR_CLASS_VARIABLE;
1613 return is_linker_constant(expression->array_access.array_ref);
1616 case EXPR_CONDITIONAL: {
1617 expression_t *const c = expression->conditional.condition;
1618 expression_classification_t const cclass = is_constant_expression(c);
1619 if (cclass < EXPR_CLASS_CONSTANT)
1622 if (fold_constant_to_bool(c)) {
1623 expression_t const *const t = expression->conditional.true_expression;
1624 return is_linker_constant(t != NULL ? t : c);
1626 return is_linker_constant(expression->conditional.false_expression);
1631 entity_t *entity = expression->select.compound_entry;
1632 if (!is_declaration(entity))
1633 return EXPR_CLASS_VARIABLE;
1634 type_t *type = skip_typeref(entity->declaration.type);
1635 if (is_type_array(type)) {
1636 /* arrays automatically convert to their address */
1637 expression_t *compound = expression->select.compound;
1638 type_t *base_type = skip_typeref(compound->base.type);
1639 if (is_type_pointer(base_type)) {
1641 return is_linker_constant(compound);
1643 return is_object_with_linker_constant_address(compound);
1646 return EXPR_CLASS_VARIABLE;
1650 return is_constant_expression(expression);
1655 * Check if the given expression is a call to a builtin function
1656 * returning a constant result.
1658 static expression_classification_t is_builtin_const_call(const expression_t *expression)
1660 expression_t *function = expression->call.function;
1661 if (function->kind != EXPR_REFERENCE)
1662 return EXPR_CLASS_VARIABLE;
1663 reference_expression_t *ref = &function->reference;
1664 if (ref->entity->kind != ENTITY_FUNCTION)
1665 return EXPR_CLASS_VARIABLE;
1667 switch (ref->entity->function.btk) {
1670 return EXPR_CLASS_CONSTANT;
1672 return EXPR_CLASS_VARIABLE;
1677 static expression_classification_t is_constant_pointer(const expression_t *expression)
1679 expression_classification_t const expr_class = is_constant_expression(expression);
1680 if (expr_class != EXPR_CLASS_VARIABLE)
1683 switch (expression->kind) {
1684 case EXPR_UNARY_CAST:
1685 return is_constant_pointer(expression->unary.value);
1687 return EXPR_CLASS_VARIABLE;
1691 static expression_classification_t is_object_with_constant_address(const expression_t *expression)
1693 switch (expression->kind) {
1695 expression_t *compound = expression->select.compound;
1696 type_t *compound_type = compound->base.type;
1697 compound_type = skip_typeref(compound_type);
1698 if (is_type_pointer(compound_type)) {
1699 return is_constant_pointer(compound);
1701 return is_object_with_constant_address(compound);
1705 case EXPR_ARRAY_ACCESS: {
1706 array_access_expression_t const* const array_access =
1707 &expression->array_access;
1708 expression_classification_t const idx_class = is_constant_expression(array_access->index);
1709 if (idx_class < EXPR_CLASS_CONSTANT)
1711 expression_classification_t const ref_addr = is_object_with_constant_address(array_access->array_ref);
1712 expression_classification_t const ref_ptr = is_constant_pointer(array_access->array_ref);
1713 return MAX(ref_addr, ref_ptr);
1716 case EXPR_UNARY_DEREFERENCE:
1717 return is_constant_pointer(expression->unary.value);
1720 return EXPR_CLASS_ERROR;
1723 return EXPR_CLASS_VARIABLE;
1727 expression_classification_t is_constant_expression(const expression_t *expression)
1729 switch (expression->kind) {
1730 case EXPR_LITERAL_CHARACTER:
1731 case EXPR_BUILTIN_TYPES_COMPATIBLE_P:
1732 case EXPR_ENUM_CONSTANT:
1733 case EXPR_LITERAL_BOOLEAN:
1734 case EXPR_LITERAL_MS_NOOP:
1735 return EXPR_CLASS_INTEGER_CONSTANT;
1740 type = skip_typeref(expression->typeprop.type);
1743 case EXPR_CLASSIFY_TYPE:
1744 type = skip_typeref(expression->classify_type.type_expression->base.type);
1747 case EXPR_LITERAL_INTEGER:
1748 type = skip_typeref(expression->base.type);
1752 type = skip_typeref(expression->offsetofe.type);
1756 type = skip_typeref(expression->typeprop.type);
1757 if (is_type_array(type) && type->array.is_vla)
1758 return EXPR_CLASS_VARIABLE;
1762 return is_type_valid(type) ? EXPR_CLASS_INTEGER_CONSTANT : EXPR_CLASS_ERROR;
1765 case EXPR_LITERAL_FLOATINGPOINT: {
1766 type_t *const type = skip_typeref(expression->base.type);
1767 return is_type_valid(type) ? EXPR_CLASS_CONSTANT : EXPR_CLASS_ERROR;
1770 case EXPR_BUILTIN_CONSTANT_P: {
1771 expression_classification_t const c = is_constant_expression(expression->builtin_constant.value);
1772 return c != EXPR_CLASS_ERROR ? EXPR_CLASS_INTEGER_CONSTANT : EXPR_CLASS_ERROR;
1775 case EXPR_STRING_LITERAL:
1777 case EXPR_LABEL_ADDRESS:
1782 case EXPR_STATEMENT:
1783 case EXPR_UNARY_POSTFIX_INCREMENT:
1784 case EXPR_UNARY_POSTFIX_DECREMENT:
1785 case EXPR_UNARY_PREFIX_INCREMENT:
1786 case EXPR_UNARY_PREFIX_DECREMENT:
1787 case EXPR_UNARY_ASSUME: /* has VOID type */
1788 case EXPR_UNARY_DEREFERENCE:
1789 case EXPR_UNARY_DELETE:
1790 case EXPR_UNARY_DELETE_ARRAY:
1791 case EXPR_UNARY_THROW:
1792 case EXPR_BINARY_ASSIGN:
1793 case EXPR_BINARY_MUL_ASSIGN:
1794 case EXPR_BINARY_DIV_ASSIGN:
1795 case EXPR_BINARY_MOD_ASSIGN:
1796 case EXPR_BINARY_ADD_ASSIGN:
1797 case EXPR_BINARY_SUB_ASSIGN:
1798 case EXPR_BINARY_SHIFTLEFT_ASSIGN:
1799 case EXPR_BINARY_SHIFTRIGHT_ASSIGN:
1800 case EXPR_BINARY_BITWISE_AND_ASSIGN:
1801 case EXPR_BINARY_BITWISE_XOR_ASSIGN:
1802 case EXPR_BINARY_BITWISE_OR_ASSIGN:
1803 case EXPR_BINARY_COMMA:
1804 case EXPR_ARRAY_ACCESS:
1805 return EXPR_CLASS_VARIABLE;
1807 case EXPR_REFERENCE: {
1808 type_t *const type = skip_typeref(expression->base.type);
1809 return is_type_valid(type) ? EXPR_CLASS_VARIABLE : EXPR_CLASS_ERROR;
1812 case EXPR_UNARY_TAKE_ADDRESS:
1813 return is_object_with_constant_address(expression->unary.value);
1816 return is_builtin_const_call(expression);
1818 case EXPR_UNARY_NEGATE:
1819 case EXPR_UNARY_PLUS:
1820 case EXPR_UNARY_COMPLEMENT:
1821 case EXPR_UNARY_NOT:
1822 return is_constant_expression(expression->unary.value);
1824 case EXPR_UNARY_IMAG:
1825 case EXPR_UNARY_REAL: {
1826 type_t *type = skip_typeref(expression->base.type);
1827 if (!is_type_valid(type))
1828 return EXPR_CLASS_ERROR;
1829 return is_constant_expression(expression->unary.value);
1832 case EXPR_UNARY_CAST: {
1833 type_t *const type = skip_typeref(expression->base.type);
1834 if (is_type_integer(type)) {
1835 expression_t *const val = expression->unary.value;
1836 if (is_type_arithmetic(skip_typeref(val->base.type))) {
1837 return val->kind == EXPR_LITERAL_FLOATINGPOINT
1838 ? EXPR_CLASS_INTEGER_CONSTANT
1839 : is_constant_expression(val);
1842 if (is_type_scalar(type))
1843 return is_constant_expression(expression->unary.value);
1844 if (!is_type_valid(type))
1845 return EXPR_CLASS_ERROR;
1846 return EXPR_CLASS_VARIABLE;
1849 case EXPR_BINARY_ADD:
1850 case EXPR_BINARY_SUB:
1851 case EXPR_BINARY_MUL:
1852 case EXPR_BINARY_DIV:
1853 case EXPR_BINARY_MOD:
1854 case EXPR_BINARY_EQUAL:
1855 case EXPR_BINARY_NOTEQUAL:
1856 case EXPR_BINARY_LESS:
1857 case EXPR_BINARY_LESSEQUAL:
1858 case EXPR_BINARY_GREATER:
1859 case EXPR_BINARY_GREATEREQUAL:
1860 case EXPR_BINARY_BITWISE_AND:
1861 case EXPR_BINARY_BITWISE_OR:
1862 case EXPR_BINARY_BITWISE_XOR:
1863 case EXPR_BINARY_SHIFTLEFT:
1864 case EXPR_BINARY_SHIFTRIGHT:
1865 case EXPR_BINARY_ISGREATER:
1866 case EXPR_BINARY_ISGREATEREQUAL:
1867 case EXPR_BINARY_ISLESS:
1868 case EXPR_BINARY_ISLESSEQUAL:
1869 case EXPR_BINARY_ISLESSGREATER:
1870 case EXPR_BINARY_ISUNORDERED: {
1871 expression_classification_t const l = is_constant_expression(expression->binary.left);
1872 expression_classification_t const r = is_constant_expression(expression->binary.right);
1876 case EXPR_BINARY_LOGICAL_AND: {
1877 expression_t const *const left = expression->binary.left;
1878 expression_classification_t const lcls = is_constant_expression(left);
1879 if (lcls < EXPR_CLASS_CONSTANT)
1881 expression_classification_t const rcls = is_constant_expression(expression->binary.right);
1882 if (lcls == EXPR_CLASS_INTEGER_CONSTANT && rcls == EXPR_CLASS_INTEGER_CONSTANT)
1883 return EXPR_CLASS_INTEGER_CONSTANT;
1884 if (!fold_constant_to_bool(left))
1885 return EXPR_CLASS_CONSTANT;
1886 return MIN(rcls, EXPR_CLASS_CONSTANT);
1889 case EXPR_BINARY_LOGICAL_OR: {
1890 expression_t const *const left = expression->binary.left;
1891 expression_classification_t const lcls = is_constant_expression(left);
1892 if (lcls < EXPR_CLASS_CONSTANT)
1894 expression_classification_t const rcls = is_constant_expression(expression->binary.right);
1895 if (lcls == EXPR_CLASS_INTEGER_CONSTANT && rcls == EXPR_CLASS_INTEGER_CONSTANT)
1896 return EXPR_CLASS_INTEGER_CONSTANT;
1897 if (fold_constant_to_bool(left))
1898 return EXPR_CLASS_CONSTANT;
1899 return MIN(rcls, EXPR_CLASS_CONSTANT);
1902 case EXPR_COMPOUND_LITERAL:
1903 return is_constant_initializer(expression->compound_literal.initializer);
1905 case EXPR_CONDITIONAL: {
1906 expression_t *const cond = expression->conditional.condition;
1907 expression_classification_t const ccls = is_constant_expression(cond);
1908 if (ccls < EXPR_CLASS_CONSTANT)
1910 expression_t const *const t = expression->conditional.true_expression;
1911 expression_classification_t const tcls = t == NULL ? ccls : is_constant_expression(t);
1912 expression_classification_t const fcls = is_constant_expression(expression->conditional.false_expression);
1913 if (ccls == EXPR_CLASS_INTEGER_CONSTANT &&
1914 tcls == EXPR_CLASS_INTEGER_CONSTANT &&
1915 fcls == EXPR_CLASS_INTEGER_CONSTANT)
1916 return EXPR_CLASS_INTEGER_CONSTANT;
1917 expression_classification_t const cls = fold_constant_to_bool(cond) ? tcls : fcls;
1918 return MIN(cls, EXPR_CLASS_CONSTANT);
1922 return EXPR_CLASS_ERROR;
1924 panic("invalid expression");
1929 obstack_init(&ast_obstack);
1934 obstack_free(&ast_obstack, NULL);