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 /** If set, implicit casts are printed. */
52 bool print_implicit_casts = false;
54 /** If set parenthesis are printed to indicate operator precedence. */
55 bool print_parenthesis = false;
57 static void print_statement(const statement_t *statement);
58 static void print_expression_prec(const expression_t *expression, unsigned prec);
60 void change_indent(int delta)
66 void print_indent(void)
68 for (int i = 0; i < indent; ++i)
72 static void print_symbol(const symbol_t *symbol)
74 print_string(symbol->string);
77 static void print_stringrep(const string_t *string)
79 for (size_t i = 0; i < string->size; ++i) {
80 print_char(string->begin[i]);
85 * Returns 1 if a given precedence level has right-to-left
86 * associativity, else 0.
88 * @param precedence the operator precedence
90 static int right_to_left(unsigned precedence)
94 case PREC_CONDITIONAL:
104 * Return the precedence of an expression given by its kind.
106 * @param kind the expression kind
108 static unsigned get_expression_precedence(expression_kind_t kind)
110 static const unsigned prec[] = {
111 [EXPR_UNKNOWN] = PREC_PRIMARY,
112 [EXPR_INVALID] = PREC_PRIMARY,
113 [EXPR_REFERENCE] = PREC_PRIMARY,
114 [EXPR_REFERENCE_ENUM_VALUE] = PREC_PRIMARY,
115 [EXPR_LITERAL_INTEGER] = PREC_PRIMARY,
116 [EXPR_LITERAL_INTEGER_OCTAL] = PREC_PRIMARY,
117 [EXPR_LITERAL_INTEGER_HEXADECIMAL] = PREC_PRIMARY,
118 [EXPR_LITERAL_FLOATINGPOINT] = PREC_PRIMARY,
119 [EXPR_LITERAL_FLOATINGPOINT_HEXADECIMAL] = PREC_PRIMARY,
120 [EXPR_LITERAL_CHARACTER] = PREC_PRIMARY,
121 [EXPR_LITERAL_WIDE_CHARACTER] = PREC_PRIMARY,
122 [EXPR_LITERAL_MS_NOOP] = PREC_PRIMARY,
123 [EXPR_STRING_LITERAL] = PREC_PRIMARY,
124 [EXPR_WIDE_STRING_LITERAL] = PREC_PRIMARY,
125 [EXPR_COMPOUND_LITERAL] = PREC_UNARY,
126 [EXPR_CALL] = PREC_POSTFIX,
127 [EXPR_CONDITIONAL] = PREC_CONDITIONAL,
128 [EXPR_SELECT] = PREC_POSTFIX,
129 [EXPR_ARRAY_ACCESS] = PREC_POSTFIX,
130 [EXPR_SIZEOF] = PREC_UNARY,
131 [EXPR_CLASSIFY_TYPE] = PREC_UNARY,
132 [EXPR_ALIGNOF] = PREC_UNARY,
134 [EXPR_FUNCNAME] = PREC_PRIMARY,
135 [EXPR_BUILTIN_CONSTANT_P] = PREC_PRIMARY,
136 [EXPR_BUILTIN_TYPES_COMPATIBLE_P] = PREC_PRIMARY,
137 [EXPR_OFFSETOF] = PREC_PRIMARY,
138 [EXPR_VA_START] = PREC_PRIMARY,
139 [EXPR_VA_ARG] = PREC_PRIMARY,
140 [EXPR_VA_COPY] = PREC_PRIMARY,
141 [EXPR_STATEMENT] = PREC_PRIMARY,
142 [EXPR_LABEL_ADDRESS] = PREC_PRIMARY,
144 [EXPR_UNARY_NEGATE] = PREC_UNARY,
145 [EXPR_UNARY_PLUS] = PREC_UNARY,
146 [EXPR_UNARY_BITWISE_NEGATE] = PREC_UNARY,
147 [EXPR_UNARY_NOT] = PREC_UNARY,
148 [EXPR_UNARY_DEREFERENCE] = PREC_UNARY,
149 [EXPR_UNARY_TAKE_ADDRESS] = PREC_UNARY,
150 [EXPR_UNARY_POSTFIX_INCREMENT] = PREC_POSTFIX,
151 [EXPR_UNARY_POSTFIX_DECREMENT] = PREC_POSTFIX,
152 [EXPR_UNARY_PREFIX_INCREMENT] = PREC_UNARY,
153 [EXPR_UNARY_PREFIX_DECREMENT] = PREC_UNARY,
154 [EXPR_UNARY_CAST] = PREC_UNARY,
155 [EXPR_UNARY_CAST_IMPLICIT] = PREC_UNARY,
156 [EXPR_UNARY_ASSUME] = PREC_PRIMARY,
157 [EXPR_UNARY_DELETE] = PREC_UNARY,
158 [EXPR_UNARY_DELETE_ARRAY] = PREC_UNARY,
159 [EXPR_UNARY_THROW] = PREC_ASSIGNMENT,
161 [EXPR_BINARY_ADD] = PREC_ADDITIVE,
162 [EXPR_BINARY_SUB] = PREC_ADDITIVE,
163 [EXPR_BINARY_MUL] = PREC_MULTIPLICATIVE,
164 [EXPR_BINARY_DIV] = PREC_MULTIPLICATIVE,
165 [EXPR_BINARY_MOD] = PREC_MULTIPLICATIVE,
166 [EXPR_BINARY_EQUAL] = PREC_EQUALITY,
167 [EXPR_BINARY_NOTEQUAL] = PREC_EQUALITY,
168 [EXPR_BINARY_LESS] = PREC_RELATIONAL,
169 [EXPR_BINARY_LESSEQUAL] = PREC_RELATIONAL,
170 [EXPR_BINARY_GREATER] = PREC_RELATIONAL,
171 [EXPR_BINARY_GREATEREQUAL] = PREC_RELATIONAL,
172 [EXPR_BINARY_BITWISE_AND] = PREC_AND,
173 [EXPR_BINARY_BITWISE_OR] = PREC_OR,
174 [EXPR_BINARY_BITWISE_XOR] = PREC_XOR,
175 [EXPR_BINARY_LOGICAL_AND] = PREC_LOGICAL_AND,
176 [EXPR_BINARY_LOGICAL_OR] = PREC_LOGICAL_OR,
177 [EXPR_BINARY_SHIFTLEFT] = PREC_SHIFT,
178 [EXPR_BINARY_SHIFTRIGHT] = PREC_SHIFT,
179 [EXPR_BINARY_ASSIGN] = PREC_ASSIGNMENT,
180 [EXPR_BINARY_MUL_ASSIGN] = PREC_ASSIGNMENT,
181 [EXPR_BINARY_DIV_ASSIGN] = PREC_ASSIGNMENT,
182 [EXPR_BINARY_MOD_ASSIGN] = PREC_ASSIGNMENT,
183 [EXPR_BINARY_ADD_ASSIGN] = PREC_ASSIGNMENT,
184 [EXPR_BINARY_SUB_ASSIGN] = PREC_ASSIGNMENT,
185 [EXPR_BINARY_SHIFTLEFT_ASSIGN] = PREC_ASSIGNMENT,
186 [EXPR_BINARY_SHIFTRIGHT_ASSIGN] = PREC_ASSIGNMENT,
187 [EXPR_BINARY_BITWISE_AND_ASSIGN] = PREC_ASSIGNMENT,
188 [EXPR_BINARY_BITWISE_XOR_ASSIGN] = PREC_ASSIGNMENT,
189 [EXPR_BINARY_BITWISE_OR_ASSIGN] = PREC_ASSIGNMENT,
190 [EXPR_BINARY_COMMA] = PREC_EXPRESSION,
192 [EXPR_BINARY_ISGREATER] = PREC_PRIMARY,
193 [EXPR_BINARY_ISGREATEREQUAL] = PREC_PRIMARY,
194 [EXPR_BINARY_ISLESS] = PREC_PRIMARY,
195 [EXPR_BINARY_ISLESSEQUAL] = PREC_PRIMARY,
196 [EXPR_BINARY_ISLESSGREATER] = PREC_PRIMARY,
197 [EXPR_BINARY_ISUNORDERED] = PREC_PRIMARY
199 assert((size_t)kind < lengthof(prec));
200 unsigned res = prec[kind];
202 assert(res != PREC_BOTTOM);
207 * Print a quoted string constant.
209 * @param string the string constant
210 * @param border the border char
211 * @param skip number of chars to skip at the end
213 static void print_quoted_string(const string_t *const string, char border,
217 const char *end = string->begin + string->size - skip;
218 for (const char *c = string->begin; c != end; ++c) {
219 unsigned char const tc = *c;
224 case '\\': print_string("\\\\"); break;
225 case '\a': print_string("\\a"); break;
226 case '\b': print_string("\\b"); break;
227 case '\f': print_string("\\f"); break;
228 case '\n': print_string("\\n"); break;
229 case '\r': print_string("\\r"); break;
230 case '\t': print_string("\\t"); break;
231 case '\v': print_string("\\v"); break;
232 case '\?': print_string("\\?"); break;
234 if (c_mode & _GNUC) {
235 print_string("\\e"); break;
239 if (tc < 0x80 && !isprint(tc)) {
240 print_format("\\%03o", (unsigned)tc);
250 static void print_string_literal(const string_literal_expression_t *literal)
252 if (literal->base.kind == EXPR_WIDE_STRING_LITERAL) {
255 print_quoted_string(&literal->value, '"', 1);
258 static void print_literal(const literal_expression_t *literal)
260 switch (literal->base.kind) {
261 case EXPR_LITERAL_MS_NOOP:
262 print_string("__noop");
264 case EXPR_LITERAL_INTEGER_HEXADECIMAL:
265 case EXPR_LITERAL_FLOATINGPOINT_HEXADECIMAL:
268 case EXPR_LITERAL_BOOLEAN:
269 case EXPR_LITERAL_INTEGER:
270 case EXPR_LITERAL_INTEGER_OCTAL:
271 case EXPR_LITERAL_FLOATINGPOINT:
272 print_stringrep(&literal->value);
273 if (literal->suffix != NULL)
274 print_symbol(literal->suffix);
276 case EXPR_LITERAL_WIDE_CHARACTER:
279 case EXPR_LITERAL_CHARACTER:
280 print_quoted_string(&literal->value, '\'', 0);
285 print_string("INVALID LITERAL KIND");
289 * Prints a predefined symbol.
291 static void print_funcname(const funcname_expression_t *funcname)
294 switch (funcname->kind) {
295 case FUNCNAME_FUNCTION: s = (c_mode & _C99) ? "__func__" : "__FUNCTION__"; break;
296 case FUNCNAME_PRETTY_FUNCTION: s = "__PRETTY_FUNCTION__"; break;
297 case FUNCNAME_FUNCSIG: s = "__FUNCSIG__"; break;
298 case FUNCNAME_FUNCDNAME: s = "__FUNCDNAME__"; break;
303 static void print_compound_literal(
304 const compound_literal_expression_t *expression)
307 print_type(expression->type);
309 print_initializer(expression->initializer);
312 static void print_assignment_expression(const expression_t *const expr)
314 print_expression_prec(expr, PREC_ASSIGNMENT);
318 * Prints a call expression.
320 * @param call the call expression
322 static void print_call_expression(const call_expression_t *call)
324 print_expression_prec(call->function, PREC_POSTFIX);
326 call_argument_t *argument = call->arguments;
328 while (argument != NULL) {
334 print_assignment_expression(argument->expression);
336 argument = argument->next;
342 * Prints a binary expression.
344 * @param binexpr the binary expression
346 static void print_binary_expression(const binary_expression_t *binexpr)
348 unsigned prec = get_expression_precedence(binexpr->base.kind);
349 int r2l = right_to_left(prec);
351 print_expression_prec(binexpr->left, prec + r2l);
353 switch (binexpr->base.kind) {
354 case EXPR_BINARY_COMMA: op = ", "; break;
355 case EXPR_BINARY_ASSIGN: op = " = "; break;
356 case EXPR_BINARY_ADD: op = " + "; break;
357 case EXPR_BINARY_SUB: op = " - "; break;
358 case EXPR_BINARY_MUL: op = " * "; break;
359 case EXPR_BINARY_MOD: op = " % "; break;
360 case EXPR_BINARY_DIV: op = " / "; break;
361 case EXPR_BINARY_BITWISE_OR: op = " | "; break;
362 case EXPR_BINARY_BITWISE_AND: op = " & "; break;
363 case EXPR_BINARY_BITWISE_XOR: op = " ^ "; break;
364 case EXPR_BINARY_LOGICAL_OR: op = " || "; break;
365 case EXPR_BINARY_LOGICAL_AND: op = " && "; break;
366 case EXPR_BINARY_NOTEQUAL: op = " != "; break;
367 case EXPR_BINARY_EQUAL: op = " == "; break;
368 case EXPR_BINARY_LESS: op = " < "; break;
369 case EXPR_BINARY_LESSEQUAL: op = " <= "; break;
370 case EXPR_BINARY_GREATER: op = " > "; break;
371 case EXPR_BINARY_GREATEREQUAL: op = " >= "; break;
372 case EXPR_BINARY_SHIFTLEFT: op = " << "; break;
373 case EXPR_BINARY_SHIFTRIGHT: op = " >> "; break;
375 case EXPR_BINARY_ADD_ASSIGN: op = " += "; break;
376 case EXPR_BINARY_SUB_ASSIGN: op = " -= "; break;
377 case EXPR_BINARY_MUL_ASSIGN: op = " *= "; break;
378 case EXPR_BINARY_MOD_ASSIGN: op = " %= "; break;
379 case EXPR_BINARY_DIV_ASSIGN: op = " /= "; break;
380 case EXPR_BINARY_BITWISE_OR_ASSIGN: op = " |= "; break;
381 case EXPR_BINARY_BITWISE_AND_ASSIGN: op = " &= "; break;
382 case EXPR_BINARY_BITWISE_XOR_ASSIGN: op = " ^= "; break;
383 case EXPR_BINARY_SHIFTLEFT_ASSIGN: op = " <<= "; break;
384 case EXPR_BINARY_SHIFTRIGHT_ASSIGN: op = " >>= "; break;
385 default: panic("invalid binexpression found");
388 print_expression_prec(binexpr->right, prec + 1 - r2l);
392 * Prints an unary expression.
394 * @param unexpr the unary expression
396 static void print_unary_expression(const unary_expression_t *unexpr)
398 unsigned prec = get_expression_precedence(unexpr->base.kind);
399 switch (unexpr->base.kind) {
400 case EXPR_UNARY_NEGATE: print_string("-"); break;
401 case EXPR_UNARY_PLUS: print_string("+"); break;
402 case EXPR_UNARY_NOT: print_string("!"); break;
403 case EXPR_UNARY_BITWISE_NEGATE: print_string("~"); break;
404 case EXPR_UNARY_PREFIX_INCREMENT: print_string("++"); break;
405 case EXPR_UNARY_PREFIX_DECREMENT: print_string("--"); break;
406 case EXPR_UNARY_DEREFERENCE: print_string("*"); break;
407 case EXPR_UNARY_TAKE_ADDRESS: print_string("&"); break;
408 case EXPR_UNARY_DELETE: print_string("delete "); break;
409 case EXPR_UNARY_DELETE_ARRAY: print_string("delete [] "); break;
411 case EXPR_UNARY_POSTFIX_INCREMENT:
412 print_expression_prec(unexpr->value, prec);
415 case EXPR_UNARY_POSTFIX_DECREMENT:
416 print_expression_prec(unexpr->value, prec);
419 case EXPR_UNARY_CAST_IMPLICIT:
420 case EXPR_UNARY_CAST:
422 print_type(unexpr->base.type);
425 case EXPR_UNARY_ASSUME:
426 print_string("__assume(");
427 print_assignment_expression(unexpr->value);
431 case EXPR_UNARY_THROW:
432 if (unexpr->value == NULL) {
433 print_string("throw");
436 print_string("throw ");
440 panic("invalid unary expression found");
442 print_expression_prec(unexpr->value, prec);
446 * Prints a reference expression.
448 * @param ref the reference expression
450 static void print_reference_expression(const reference_expression_t *ref)
452 print_string(ref->entity->base.symbol->string);
456 * Prints a label address expression.
458 * @param ref the reference expression
460 static void print_label_address_expression(const label_address_expression_t *le)
462 print_format("&&%s", le->label->base.symbol->string);
466 * Prints an array expression.
468 * @param expression the array expression
470 static void print_array_expression(const array_access_expression_t *expression)
472 if (!expression->flipped) {
473 print_expression_prec(expression->array_ref, PREC_POSTFIX);
475 print_expression(expression->index);
478 print_expression_prec(expression->index, PREC_POSTFIX);
480 print_expression(expression->array_ref);
486 * Prints a typeproperty expression (sizeof or __alignof__).
488 * @param expression the type property expression
490 static void print_typeprop_expression(const typeprop_expression_t *expression)
492 if (expression->base.kind == EXPR_SIZEOF) {
493 print_string("sizeof");
495 assert(expression->base.kind == EXPR_ALIGNOF);
496 print_string("__alignof__");
498 if (expression->tp_expression != NULL) {
499 /* PREC_TOP: always print the '()' here, sizeof x is right but unusual */
500 print_expression_prec(expression->tp_expression, PREC_TOP);
503 print_type(expression->type);
509 * Prints a builtin constant expression.
511 * @param expression the builtin constant expression
513 static void print_builtin_constant(const builtin_constant_expression_t *expression)
515 print_string("__builtin_constant_p(");
516 print_assignment_expression(expression->value);
521 * Prints a builtin types compatible expression.
523 * @param expression the builtin types compatible expression
525 static void print_builtin_types_compatible(
526 const builtin_types_compatible_expression_t *expression)
528 print_string("__builtin_types_compatible_p(");
529 print_type(expression->left);
531 print_type(expression->right);
536 * Prints a conditional expression.
538 * @param expression the conditional expression
540 static void print_conditional(const conditional_expression_t *expression)
542 print_expression_prec(expression->condition, PREC_LOGICAL_OR);
543 if (expression->true_expression != NULL) {
545 print_expression_prec(expression->true_expression, PREC_EXPRESSION);
548 print_string(" ?: ");
550 precedence_t prec = c_mode & _CXX ? PREC_ASSIGNMENT : PREC_CONDITIONAL;
551 print_expression_prec(expression->false_expression, prec);
555 * Prints a va_start expression.
557 * @param expression the va_start expression
559 static void print_va_start(const va_start_expression_t *const expression)
561 print_string("__builtin_va_start(");
562 print_assignment_expression(expression->ap);
564 print_string(expression->parameter->base.base.symbol->string);
569 * Prints a va_arg expression.
571 * @param expression the va_arg expression
573 static void print_va_arg(const va_arg_expression_t *expression)
575 print_string("__builtin_va_arg(");
576 print_assignment_expression(expression->ap);
578 print_type(expression->base.type);
583 * Prints a va_copy expression.
585 * @param expression the va_copy expression
587 static void print_va_copy(const va_copy_expression_t *expression)
589 print_string("__builtin_va_copy(");
590 print_assignment_expression(expression->dst);
592 print_assignment_expression(expression->src);
597 * Prints a select expression (. or ->).
599 * @param expression the select expression
601 static void print_select(const select_expression_t *expression)
603 print_expression_prec(expression->compound, PREC_POSTFIX);
604 if (is_type_pointer(skip_typeref(expression->compound->base.type))) {
609 print_string(expression->compound_entry->base.symbol->string);
613 * Prints a type classify expression.
615 * @param expr the type classify expression
617 static void print_classify_type_expression(
618 const classify_type_expression_t *const expr)
620 print_string("__builtin_classify_type(");
621 print_assignment_expression(expr->type_expression);
626 * Prints a designator.
628 * @param designator the designator
630 static void print_designator(const designator_t *designator)
632 for ( ; designator != NULL; designator = designator->next) {
633 if (designator->symbol == NULL) {
635 print_expression(designator->array_index);
639 print_string(designator->symbol->string);
645 * Prints an offsetof expression.
647 * @param expression the offset expression
649 static void print_offsetof_expression(const offsetof_expression_t *expression)
651 print_string("__builtin_offsetof(");
652 print_type(expression->type);
654 print_designator(expression->designator);
659 * Prints a statement expression.
661 * @param expression the statement expression
663 static void print_statement_expression(const statement_expression_t *expression)
666 print_statement(expression->statement);
671 * Prints an expression with parenthesis if needed.
673 * @param expression the expression to print
674 * @param top_prec the precedence of the user of this expression.
676 static void print_expression_prec(const expression_t *expression, unsigned top_prec)
678 if (expression->kind == EXPR_UNARY_CAST_IMPLICIT && !print_implicit_casts) {
679 expression = expression->unary.value;
683 expression->base.parenthesized ||
684 (print_parenthesis && top_prec != PREC_BOTTOM) ||
685 top_prec > get_expression_precedence(expression->base.kind);
689 switch (expression->kind) {
692 print_string("$invalid expression$");
694 case EXPR_WIDE_STRING_LITERAL:
695 case EXPR_STRING_LITERAL:
696 print_string_literal(&expression->string_literal);
699 print_literal(&expression->literal);
702 print_funcname(&expression->funcname);
704 case EXPR_COMPOUND_LITERAL:
705 print_compound_literal(&expression->compound_literal);
708 print_call_expression(&expression->call);
711 print_binary_expression(&expression->binary);
714 case EXPR_REFERENCE_ENUM_VALUE:
715 print_reference_expression(&expression->reference);
717 case EXPR_ARRAY_ACCESS:
718 print_array_expression(&expression->array_access);
720 case EXPR_LABEL_ADDRESS:
721 print_label_address_expression(&expression->label_address);
724 print_unary_expression(&expression->unary);
728 print_typeprop_expression(&expression->typeprop);
730 case EXPR_BUILTIN_CONSTANT_P:
731 print_builtin_constant(&expression->builtin_constant);
733 case EXPR_BUILTIN_TYPES_COMPATIBLE_P:
734 print_builtin_types_compatible(&expression->builtin_types_compatible);
736 case EXPR_CONDITIONAL:
737 print_conditional(&expression->conditional);
740 print_va_start(&expression->va_starte);
743 print_va_arg(&expression->va_arge);
746 print_va_copy(&expression->va_copye);
749 print_select(&expression->select);
751 case EXPR_CLASSIFY_TYPE:
752 print_classify_type_expression(&expression->classify_type);
755 print_offsetof_expression(&expression->offsetofe);
758 print_statement_expression(&expression->statement);
764 print_format("some expression of type %d", (int)expression->kind);
773 * Print an compound statement.
775 * @param block the compound statement
777 static void print_compound_statement(const compound_statement_t *block)
782 statement_t *statement = block->statements;
783 while (statement != NULL) {
784 if (statement->base.kind == STATEMENT_CASE_LABEL)
786 if (statement->kind != STATEMENT_LABEL)
788 print_statement(statement);
790 statement = statement->base.next;
794 print_string(block->stmt_expr ? "}" : "}\n");
798 * Print a return statement.
800 * @param statement the return statement
802 static void print_return_statement(const return_statement_t *statement)
804 expression_t const *const val = statement->value;
806 print_string("return ");
807 print_expression(val);
810 print_string("return;\n");
815 * Print an expression statement.
817 * @param statement the expression statement
819 static void print_expression_statement(const expression_statement_t *statement)
821 print_expression(statement->expression);
826 * Print a goto statement.
828 * @param statement the goto statement
830 static void print_goto_statement(const goto_statement_t *statement)
832 print_string("goto ");
833 if (statement->expression != NULL) {
835 print_expression(statement->expression);
837 print_string(statement->label->base.symbol->string);
843 * Print a label statement.
845 * @param statement the label statement
847 static void print_label_statement(const label_statement_t *statement)
849 print_format("%s:\n", statement->label->base.symbol->string);
851 print_statement(statement->statement);
855 * Print an if statement.
857 * @param statement the if statement
859 static void print_if_statement(const if_statement_t *statement)
861 print_string("if (");
862 print_expression(statement->condition);
864 print_statement(statement->true_statement);
866 if (statement->false_statement != NULL) {
868 print_string("else ");
869 print_statement(statement->false_statement);
874 * Print a switch statement.
876 * @param statement the switch statement
878 static void print_switch_statement(const switch_statement_t *statement)
880 print_string("switch (");
881 print_expression(statement->expression);
883 print_statement(statement->body);
887 * Print a case label (including the default label).
889 * @param statement the case label statement
891 static void print_case_label(const case_label_statement_t *statement)
893 if (statement->expression == NULL) {
894 print_string("default:\n");
896 print_string("case ");
897 print_expression(statement->expression);
898 if (statement->end_range != NULL) {
899 print_string(" ... ");
900 print_expression(statement->end_range);
905 if (statement->statement != NULL) {
906 if (statement->statement->base.kind == STATEMENT_CASE_LABEL) {
910 print_statement(statement->statement);
914 static void print_typedef(const entity_t *entity)
916 print_string("typedef ");
917 print_type_ext(entity->typedefe.type, entity->base.symbol, NULL);
922 * returns true if the entity is a compiler generated one and has no real
923 * correspondenc in the source file
925 static bool is_generated_entity(const entity_t *entity)
927 if (entity->kind == ENTITY_TYPEDEF)
928 return entity->typedefe.builtin;
930 if (is_declaration(entity))
931 return entity->declaration.implicit;
937 * Print a declaration statement.
939 * @param statement the statement
941 static void print_declaration_statement(
942 const declaration_statement_t *statement)
945 entity_t *entity = statement->declarations_begin;
946 if (entity == NULL) {
947 print_string("/* empty declaration statement */\n");
951 entity_t *const end = statement->declarations_end->base.next;
952 for (; entity != end; entity = entity->base.next) {
953 if (entity->kind == ENTITY_ENUM_VALUE)
955 if (is_generated_entity(entity))
964 print_entity(entity);
970 * Print a while statement.
972 * @param statement the statement
974 static void print_while_statement(const while_statement_t *statement)
976 print_string("while (");
977 print_expression(statement->condition);
979 print_statement(statement->body);
983 * Print a do-while statement.
985 * @param statement the statement
987 static void print_do_while_statement(const do_while_statement_t *statement)
990 print_statement(statement->body);
992 print_string("while (");
993 print_expression(statement->condition);
994 print_string(");\n");
998 * Print a for statement.
1000 * @param statement the statement
1002 static void print_for_statement(const for_statement_t *statement)
1004 print_string("for (");
1005 if (statement->initialisation != NULL) {
1006 print_expression(statement->initialisation);
1009 entity_t const *entity = statement->scope.entities;
1010 for (; entity != NULL; entity = entity->base.next) {
1011 if (is_generated_entity(entity))
1013 /* FIXME display of multiple declarations is wrong */
1014 print_declaration(entity);
1017 if (statement->condition != NULL) {
1019 print_expression(statement->condition);
1022 if (statement->step != NULL) {
1024 print_expression(statement->step);
1027 print_statement(statement->body);
1031 * Print assembler arguments.
1033 * @param arguments the arguments
1035 static void print_asm_arguments(asm_argument_t *arguments)
1037 asm_argument_t *argument = arguments;
1038 for (; argument != NULL; argument = argument->next) {
1039 if (argument != arguments)
1042 if (argument->symbol) {
1043 print_format("[%s] ", argument->symbol->string);
1045 print_quoted_string(&argument->constraints, '"', 1);
1047 print_expression(argument->expression);
1053 * Print assembler clobbers.
1055 * @param clobbers the clobbers
1057 static void print_asm_clobbers(asm_clobber_t *clobbers)
1059 asm_clobber_t *clobber = clobbers;
1060 for (; clobber != NULL; clobber = clobber->next) {
1061 if (clobber != clobbers)
1064 print_quoted_string(&clobber->clobber, '"', 1);
1069 * Print an assembler statement.
1071 * @param statement the statement
1073 static void print_asm_statement(const asm_statement_t *statement)
1075 print_string("asm ");
1076 if (statement->is_volatile) {
1077 print_string("volatile ");
1080 print_quoted_string(&statement->asm_text, '"', 1);
1081 if (statement->outputs == NULL &&
1082 statement->inputs == NULL &&
1083 statement->clobbers == NULL)
1084 goto end_of_print_asm_statement;
1086 print_string(" : ");
1087 print_asm_arguments(statement->outputs);
1088 if (statement->inputs == NULL && statement->clobbers == NULL)
1089 goto end_of_print_asm_statement;
1091 print_string(" : ");
1092 print_asm_arguments(statement->inputs);
1093 if (statement->clobbers == NULL)
1094 goto end_of_print_asm_statement;
1096 print_string(" : ");
1097 print_asm_clobbers(statement->clobbers);
1099 end_of_print_asm_statement:
1100 print_string(");\n");
1104 * Print a microsoft __try statement.
1106 * @param statement the statement
1108 static void print_ms_try_statement(const ms_try_statement_t *statement)
1110 print_string("__try ");
1111 print_statement(statement->try_statement);
1113 if (statement->except_expression != NULL) {
1114 print_string("__except(");
1115 print_expression(statement->except_expression);
1118 print_string("__finally ");
1120 print_statement(statement->final_statement);
1124 * Print a microsoft __leave statement.
1126 * @param statement the statement
1128 static void print_leave_statement(const leave_statement_t *statement)
1131 print_string("__leave;\n");
1135 * Print a statement.
1137 * @param statement the statement
1139 void print_statement(const statement_t *statement)
1141 switch (statement->kind) {
1142 case STATEMENT_EMPTY:
1143 print_string(";\n");
1145 case STATEMENT_COMPOUND:
1146 print_compound_statement(&statement->compound);
1148 case STATEMENT_RETURN:
1149 print_return_statement(&statement->returns);
1151 case STATEMENT_EXPRESSION:
1152 print_expression_statement(&statement->expression);
1154 case STATEMENT_LABEL:
1155 print_label_statement(&statement->label);
1157 case STATEMENT_GOTO:
1158 print_goto_statement(&statement->gotos);
1160 case STATEMENT_CONTINUE:
1161 print_string("continue;\n");
1163 case STATEMENT_BREAK:
1164 print_string("break;\n");
1167 print_if_statement(&statement->ifs);
1169 case STATEMENT_SWITCH:
1170 print_switch_statement(&statement->switchs);
1172 case STATEMENT_CASE_LABEL:
1173 print_case_label(&statement->case_label);
1175 case STATEMENT_DECLARATION:
1176 print_declaration_statement(&statement->declaration);
1178 case STATEMENT_WHILE:
1179 print_while_statement(&statement->whiles);
1181 case STATEMENT_DO_WHILE:
1182 print_do_while_statement(&statement->do_while);
1185 print_for_statement(&statement->fors);
1188 print_asm_statement(&statement->asms);
1190 case STATEMENT_MS_TRY:
1191 print_ms_try_statement(&statement->ms_try);
1193 case STATEMENT_LEAVE:
1194 print_leave_statement(&statement->leave);
1196 case STATEMENT_INVALID:
1197 print_string("$invalid statement$\n");
1203 * Print a storage class.
1205 * @param storage_class the storage class
1207 static void print_storage_class(storage_class_tag_t storage_class)
1209 switch (storage_class) {
1210 case STORAGE_CLASS_NONE: return;
1211 case STORAGE_CLASS_TYPEDEF: print_string("typedef "); return;
1212 case STORAGE_CLASS_EXTERN: print_string("extern "); return;
1213 case STORAGE_CLASS_STATIC: print_string("static "); return;
1214 case STORAGE_CLASS_AUTO: print_string("auto "); return;
1215 case STORAGE_CLASS_REGISTER: print_string("register "); return;
1217 panic("invalid storage class");
1221 * Print an initializer.
1223 * @param initializer the initializer
1225 void print_initializer(const initializer_t *initializer)
1227 if (initializer == NULL) {
1232 switch (initializer->kind) {
1233 case INITIALIZER_VALUE: {
1234 const initializer_value_t *value = &initializer->value;
1235 print_assignment_expression(value->value);
1238 case INITIALIZER_LIST: {
1239 assert(initializer->kind == INITIALIZER_LIST);
1241 const initializer_list_t *list = &initializer->list;
1243 for (size_t i = 0 ; i < list->len; ++i) {
1244 const initializer_t *sub_init = list->initializers[i];
1245 print_initializer(list->initializers[i]);
1246 if (i < list->len-1) {
1247 if (sub_init == NULL || sub_init->kind != INITIALIZER_DESIGNATOR)
1254 case INITIALIZER_STRING:
1255 print_quoted_string(&initializer->string.string, '"', 1);
1257 case INITIALIZER_WIDE_STRING:
1258 print_quoted_string(&initializer->string.string, '"', 1);
1260 case INITIALIZER_DESIGNATOR:
1261 print_designator(initializer->designator.designator);
1262 print_string(" = ");
1266 panic("invalid initializer kind found");
1271 * Print microsoft extended declaration modifiers.
1273 static void print_ms_modifiers(const declaration_t *declaration)
1275 if ((c_mode & _MS) == 0)
1278 decl_modifiers_t modifiers = declaration->modifiers;
1280 bool ds_shown = false;
1281 const char *next = "(";
1283 if (declaration->base.kind == ENTITY_VARIABLE) {
1284 variable_t *variable = (variable_t*)declaration;
1285 if (variable->alignment != 0
1286 || variable->get_property_sym != NULL
1287 || variable->put_property_sym != NULL) {
1289 print_string("__declspec");
1293 if (variable->alignment != 0) {
1294 print_string(next); next = ", "; print_format("align(%u)", variable->alignment);
1296 if (variable->get_property_sym != NULL
1297 || variable->put_property_sym != NULL) {
1299 print_string(next); next = ", "; print_string("property(");
1300 if (variable->get_property_sym != NULL) {
1301 print_format("get=%s", variable->get_property_sym->string);
1304 if (variable->put_property_sym != NULL)
1305 print_format("%sput=%s", comma, variable->put_property_sym->string);
1311 /* DM_FORCEINLINE handled outside. */
1312 if ((modifiers & ~DM_FORCEINLINE) != 0) {
1314 print_string("__declspec");
1317 if (modifiers & DM_DLLIMPORT) {
1318 print_string(next); next = ", "; print_string("dllimport");
1320 if (modifiers & DM_DLLEXPORT) {
1321 print_string(next); next = ", "; print_string("dllexport");
1323 if (modifiers & DM_THREAD) {
1324 print_string(next); next = ", "; print_string("thread");
1326 if (modifiers & DM_NAKED) {
1327 print_string(next); next = ", "; print_string("naked");
1329 if (modifiers & DM_THREAD) {
1330 print_string(next); next = ", "; print_string("thread");
1332 if (modifiers & DM_SELECTANY) {
1333 print_string(next); next = ", "; print_string("selectany");
1335 if (modifiers & DM_NOTHROW) {
1336 print_string(next); next = ", "; print_string("nothrow");
1338 if (modifiers & DM_NORETURN) {
1339 print_string(next); next = ", "; print_string("noreturn");
1341 if (modifiers & DM_NOINLINE) {
1342 print_string(next); next = ", "; print_string("noinline");
1344 if (modifiers & DM_DEPRECATED) {
1345 print_string(next); next = ", "; print_string("deprecated");
1346 if (declaration->deprecated_string != NULL)
1347 print_format("(\"%s\")",
1348 declaration->deprecated_string);
1350 if (modifiers & DM_RESTRICT) {
1351 print_string(next); next = ", "; print_string("restrict");
1353 if (modifiers & DM_NOALIAS) {
1354 print_string(next); next = ", "; print_string("noalias");
1363 static void print_scope(const scope_t *scope)
1365 const entity_t *entity = scope->entities;
1366 for ( ; entity != NULL; entity = entity->base.next) {
1368 print_entity(entity);
1373 static void print_namespace(const namespace_t *namespace)
1375 print_string("namespace ");
1376 if (namespace->base.symbol != NULL) {
1377 print_string(namespace->base.symbol->string);
1381 print_string("{\n");
1384 print_scope(&namespace->members);
1388 print_string("}\n");
1392 * Print a variable or function declaration
1394 void print_declaration(const entity_t *entity)
1396 assert(is_declaration(entity));
1397 const declaration_t *declaration = &entity->declaration;
1399 print_storage_class((storage_class_tag_t)declaration->declared_storage_class);
1400 if (entity->kind == ENTITY_FUNCTION) {
1401 function_t *function = (function_t*)declaration;
1402 if (function->is_inline) {
1403 if (declaration->modifiers & DM_FORCEINLINE) {
1404 print_string("__forceinline ");
1405 } else if (declaration->modifiers & DM_MICROSOFT_INLINE) {
1406 print_string("__inline ");
1408 print_string("inline ");
1412 //print_ms_modifiers(declaration);
1413 switch (entity->kind) {
1414 case ENTITY_FUNCTION:
1415 print_type_ext(entity->declaration.type, entity->base.symbol,
1416 &entity->function.parameters);
1418 if (entity->function.statement != NULL) {
1421 print_statement(entity->function.statement);
1426 case ENTITY_VARIABLE:
1427 if (entity->variable.thread_local)
1428 print_string("__thread ");
1429 print_type_ext(declaration->type, declaration->base.symbol, NULL);
1430 if (entity->variable.initializer != NULL) {
1431 print_string(" = ");
1432 print_initializer(entity->variable.initializer);
1437 print_type_ext(declaration->type, declaration->base.symbol, NULL);
1444 * Prints an expression.
1446 * @param expression the expression
1448 void print_expression(const expression_t *expression)
1450 print_expression_prec(expression, PREC_BOTTOM);
1454 * Print a declaration.
1456 * @param declaration the declaration
1458 void print_entity(const entity_t *entity)
1460 if (entity->base.namespc != NAMESPACE_NORMAL && entity->base.symbol == NULL)
1463 switch ((entity_kind_tag_t)entity->kind) {
1464 case ENTITY_VARIABLE:
1465 case ENTITY_PARAMETER:
1466 case ENTITY_COMPOUND_MEMBER:
1467 case ENTITY_FUNCTION:
1468 print_declaration(entity);
1470 case ENTITY_TYPEDEF:
1471 print_typedef(entity);
1475 print_string("class ");
1476 print_string(entity->base.symbol->string);
1477 print_string("; /* TODO */\n");
1480 print_string("struct ");
1481 print_string(entity->base.symbol->string);
1482 if (entity->structe.complete) {
1484 print_compound_definition(&entity->structe);
1489 print_string("union ");
1490 print_string(entity->base.symbol->string);
1491 if (entity->unione.complete) {
1493 print_compound_definition(&entity->unione);
1498 print_string("enum ");
1499 print_string(entity->base.symbol->string);
1501 print_enum_definition(&entity->enume);
1504 case ENTITY_NAMESPACE:
1505 print_namespace(&entity->namespacee);
1507 case ENTITY_LOCAL_LABEL:
1508 print_string("__label__ ");
1509 print_string(entity->base.symbol->string);
1513 case ENTITY_ENUM_VALUE:
1514 panic("print_entity used on unexpected entity type");
1515 case ENTITY_INVALID:
1518 panic("Invalid entity type encountered");
1522 * Print the AST of a translation unit.
1524 * @param unit the translation unit
1526 void print_ast(const translation_unit_t *unit)
1528 entity_t *entity = unit->scope.entities;
1529 for ( ; entity != NULL; entity = entity->base.next) {
1530 if (entity->kind == ENTITY_ENUM_VALUE)
1532 if (entity->base.namespc != NAMESPACE_NORMAL
1533 && entity->base.symbol == NULL)
1535 if (is_generated_entity(entity))
1539 print_entity(entity);
1544 expression_classification_t is_constant_initializer(const initializer_t *initializer)
1546 switch (initializer->kind) {
1547 case INITIALIZER_STRING:
1548 case INITIALIZER_WIDE_STRING:
1549 case INITIALIZER_DESIGNATOR:
1550 return EXPR_CLASS_CONSTANT;
1552 case INITIALIZER_VALUE:
1553 return is_constant_expression(initializer->value.value);
1555 case INITIALIZER_LIST: {
1556 expression_classification_t all = EXPR_CLASS_CONSTANT;
1557 for (size_t i = 0; i < initializer->list.len; ++i) {
1558 initializer_t *sub_initializer = initializer->list.initializers[i];
1559 expression_classification_t const cur = is_constant_initializer(sub_initializer);
1567 panic("invalid initializer kind found");
1570 static expression_classification_t is_object_with_linker_constant_address(const expression_t *expression)
1572 switch (expression->kind) {
1573 case EXPR_UNARY_DEREFERENCE:
1574 return is_address_constant(expression->unary.value);
1577 type_t *base_type = skip_typeref(expression->select.compound->base.type);
1578 if (is_type_pointer(base_type)) {
1580 return is_address_constant(expression->select.compound);
1582 return is_object_with_linker_constant_address(expression->select.compound);
1586 case EXPR_ARRAY_ACCESS: {
1587 expression_classification_t const ref = is_address_constant(expression->array_access.array_ref);
1588 expression_classification_t const idx = is_constant_expression(expression->array_access.index);
1589 return ref < idx ? ref : idx;
1592 case EXPR_REFERENCE: {
1593 entity_t *entity = expression->reference.entity;
1594 if (is_declaration(entity)) {
1595 switch ((storage_class_tag_t)entity->declaration.storage_class) {
1596 case STORAGE_CLASS_NONE:
1597 case STORAGE_CLASS_EXTERN:
1598 case STORAGE_CLASS_STATIC:
1600 entity->kind != ENTITY_VARIABLE ||
1601 !entity->variable.thread_local ? EXPR_CLASS_CONSTANT :
1602 EXPR_CLASS_VARIABLE;
1604 case STORAGE_CLASS_REGISTER:
1605 case STORAGE_CLASS_TYPEDEF:
1606 case STORAGE_CLASS_AUTO:
1610 return EXPR_CLASS_VARIABLE;
1614 return EXPR_CLASS_ERROR;
1617 return EXPR_CLASS_VARIABLE;
1621 expression_classification_t is_address_constant(const expression_t *expression)
1623 switch (expression->kind) {
1624 case EXPR_STRING_LITERAL:
1625 case EXPR_WIDE_STRING_LITERAL:
1627 case EXPR_LABEL_ADDRESS:
1628 return EXPR_CLASS_CONSTANT;
1630 case EXPR_UNARY_TAKE_ADDRESS:
1631 return is_object_with_linker_constant_address(expression->unary.value);
1633 case EXPR_UNARY_DEREFERENCE: {
1635 = revert_automatic_type_conversion(expression->unary.value);
1636 /* dereferencing a function is a NOP */
1637 if (is_type_function(real_type)) {
1638 return is_address_constant(expression->unary.value);
1643 case EXPR_UNARY_CAST: {
1644 type_t *dest = skip_typeref(expression->base.type);
1645 if (!is_type_pointer(dest) && (
1646 dest->kind != TYPE_ATOMIC ||
1647 !(get_atomic_type_flags(dest->atomic.akind) & ATOMIC_TYPE_FLAG_INTEGER) ||
1648 get_atomic_type_size(dest->atomic.akind) < get_atomic_type_size(get_intptr_kind())
1650 return EXPR_CLASS_VARIABLE;
1652 expression_classification_t const expr = is_constant_expression(expression->unary.value);
1653 expression_classification_t const addr = is_address_constant(expression->unary.value);
1654 return expr > addr ? expr : addr;
1657 case EXPR_BINARY_ADD:
1658 case EXPR_BINARY_SUB: {
1659 expression_t *const left = expression->binary.left;
1660 expression_t *const right = expression->binary.right;
1661 type_t *const ltype = skip_typeref(left->base.type);
1662 type_t *const rtype = skip_typeref(right->base.type);
1664 if (is_type_pointer(ltype)) {
1665 expression_classification_t const l = is_address_constant(left);
1666 expression_classification_t const r = is_constant_expression(right);
1667 return l < r ? l : r;
1668 } else if (is_type_pointer(rtype)) {
1669 expression_classification_t const l = is_constant_expression(left);
1670 expression_classification_t const r = is_address_constant(right);
1671 return l < r ? l : r;
1672 } else if (!is_type_valid(ltype) || !is_type_valid(rtype)) {
1673 return EXPR_CLASS_ERROR;
1675 return EXPR_CLASS_VARIABLE;
1679 case EXPR_REFERENCE: {
1680 entity_t *entity = expression->reference.entity;
1681 if (!is_declaration(entity))
1682 return EXPR_CLASS_VARIABLE;
1684 type_t *type = skip_typeref(entity->declaration.type);
1685 if (is_type_function(type))
1686 return EXPR_CLASS_CONSTANT;
1687 if (is_type_array(type)) {
1688 return is_object_with_linker_constant_address(expression);
1690 /* Prevent stray errors */
1691 if (!is_type_valid(type))
1692 return EXPR_CLASS_ERROR;
1693 return EXPR_CLASS_VARIABLE;
1696 case EXPR_ARRAY_ACCESS: {
1697 type_t *const type =
1698 skip_typeref(revert_automatic_type_conversion(expression));
1699 if (!is_type_array(type))
1700 return EXPR_CLASS_VARIABLE;
1701 expression_classification_t const ref = is_address_constant(expression->array_access.array_ref);
1702 expression_classification_t const idx = is_constant_expression(expression->array_access.index);
1703 return ref < idx ? ref : idx;
1706 case EXPR_CONDITIONAL: {
1707 expression_t *const c = expression->conditional.condition;
1708 expression_classification_t const cclass = is_constant_expression(c);
1709 if (cclass != EXPR_CLASS_CONSTANT)
1712 if (fold_constant_to_bool(c)) {
1713 expression_t const *const t = expression->conditional.true_expression;
1714 return is_address_constant(t != NULL ? t : c);
1716 return is_address_constant(expression->conditional.false_expression);
1721 return EXPR_CLASS_ERROR;
1724 return EXPR_CLASS_VARIABLE;
1729 * Check if the given expression is a call to a builtin function
1730 * returning a constant result.
1732 static expression_classification_t is_builtin_const_call(const expression_t *expression)
1734 expression_t *function = expression->call.function;
1735 if (function->kind != EXPR_REFERENCE)
1736 return EXPR_CLASS_VARIABLE;
1737 reference_expression_t *ref = &function->reference;
1738 if (ref->entity->kind != ENTITY_FUNCTION)
1739 return EXPR_CLASS_VARIABLE;
1741 switch (ref->entity->function.btk) {
1742 case bk_gnu_builtin_huge_val:
1743 case bk_gnu_builtin_huge_valf:
1744 case bk_gnu_builtin_huge_vall:
1745 case bk_gnu_builtin_inf:
1746 case bk_gnu_builtin_inff:
1747 case bk_gnu_builtin_infl:
1748 case bk_gnu_builtin_nan:
1749 case bk_gnu_builtin_nanf:
1750 case bk_gnu_builtin_nanl:
1751 return EXPR_CLASS_CONSTANT;
1753 return EXPR_CLASS_VARIABLE;
1758 static expression_classification_t is_constant_pointer(const expression_t *expression)
1760 expression_classification_t const expr_class = is_constant_expression(expression);
1761 if (expr_class != EXPR_CLASS_VARIABLE)
1764 switch (expression->kind) {
1765 case EXPR_UNARY_CAST:
1766 return is_constant_pointer(expression->unary.value);
1768 return EXPR_CLASS_VARIABLE;
1772 static expression_classification_t is_object_with_constant_address(const expression_t *expression)
1774 switch (expression->kind) {
1776 expression_t *compound = expression->select.compound;
1777 type_t *compound_type = compound->base.type;
1778 compound_type = skip_typeref(compound_type);
1779 if (is_type_pointer(compound_type)) {
1780 return is_constant_pointer(compound);
1782 return is_object_with_constant_address(compound);
1786 case EXPR_ARRAY_ACCESS: {
1787 array_access_expression_t const* const array_access =
1788 &expression->array_access;
1789 expression_classification_t const idx_class = is_constant_expression(array_access->index);
1790 if (idx_class != EXPR_CLASS_CONSTANT)
1792 expression_classification_t const ref_addr = is_object_with_constant_address(array_access->array_ref);
1793 expression_classification_t const ref_ptr = is_constant_pointer(array_access->array_ref);
1794 return ref_addr > ref_ptr ? ref_addr : ref_ptr;
1797 case EXPR_UNARY_DEREFERENCE:
1798 return is_constant_pointer(expression->unary.value);
1801 return EXPR_CLASS_ERROR;
1804 return EXPR_CLASS_VARIABLE;
1808 expression_classification_t is_constant_expression(const expression_t *expression)
1810 switch (expression->kind) {
1812 case EXPR_CLASSIFY_TYPE:
1815 case EXPR_BUILTIN_CONSTANT_P:
1816 case EXPR_BUILTIN_TYPES_COMPATIBLE_P:
1817 case EXPR_REFERENCE_ENUM_VALUE:
1818 return EXPR_CLASS_CONSTANT;
1821 type_t *const type = skip_typeref(expression->typeprop.type);
1823 !is_type_array(type) || !type->array.is_vla ? EXPR_CLASS_CONSTANT :
1824 EXPR_CLASS_VARIABLE;
1827 case EXPR_STRING_LITERAL:
1828 case EXPR_WIDE_STRING_LITERAL:
1830 case EXPR_LABEL_ADDRESS:
1835 case EXPR_STATEMENT:
1836 case EXPR_UNARY_POSTFIX_INCREMENT:
1837 case EXPR_UNARY_POSTFIX_DECREMENT:
1838 case EXPR_UNARY_PREFIX_INCREMENT:
1839 case EXPR_UNARY_PREFIX_DECREMENT:
1840 case EXPR_UNARY_ASSUME: /* has VOID type */
1841 case EXPR_UNARY_DEREFERENCE:
1842 case EXPR_UNARY_DELETE:
1843 case EXPR_UNARY_DELETE_ARRAY:
1844 case EXPR_UNARY_THROW:
1845 case EXPR_BINARY_ASSIGN:
1846 case EXPR_BINARY_MUL_ASSIGN:
1847 case EXPR_BINARY_DIV_ASSIGN:
1848 case EXPR_BINARY_MOD_ASSIGN:
1849 case EXPR_BINARY_ADD_ASSIGN:
1850 case EXPR_BINARY_SUB_ASSIGN:
1851 case EXPR_BINARY_SHIFTLEFT_ASSIGN:
1852 case EXPR_BINARY_SHIFTRIGHT_ASSIGN:
1853 case EXPR_BINARY_BITWISE_AND_ASSIGN:
1854 case EXPR_BINARY_BITWISE_XOR_ASSIGN:
1855 case EXPR_BINARY_BITWISE_OR_ASSIGN:
1856 case EXPR_BINARY_COMMA:
1857 case EXPR_ARRAY_ACCESS:
1858 return EXPR_CLASS_VARIABLE;
1860 case EXPR_REFERENCE: {
1861 type_t *const type = skip_typeref(expression->base.type);
1862 return is_type_valid(type) ? EXPR_CLASS_VARIABLE : EXPR_CLASS_ERROR;
1865 case EXPR_UNARY_TAKE_ADDRESS:
1866 return is_object_with_constant_address(expression->unary.value);
1869 return is_builtin_const_call(expression);
1871 case EXPR_UNARY_NEGATE:
1872 case EXPR_UNARY_PLUS:
1873 case EXPR_UNARY_BITWISE_NEGATE:
1874 case EXPR_UNARY_NOT:
1875 return is_constant_expression(expression->unary.value);
1877 case EXPR_UNARY_CAST:
1878 case EXPR_UNARY_CAST_IMPLICIT: {
1879 type_t *const type = skip_typeref(expression->base.type);
1880 if (is_type_scalar(type))
1881 return is_constant_expression(expression->unary.value);
1882 if (!is_type_valid(type))
1883 return EXPR_CLASS_ERROR;
1884 return EXPR_CLASS_VARIABLE;
1887 case EXPR_BINARY_ADD:
1888 case EXPR_BINARY_SUB:
1889 case EXPR_BINARY_MUL:
1890 case EXPR_BINARY_DIV:
1891 case EXPR_BINARY_MOD:
1892 case EXPR_BINARY_EQUAL:
1893 case EXPR_BINARY_NOTEQUAL:
1894 case EXPR_BINARY_LESS:
1895 case EXPR_BINARY_LESSEQUAL:
1896 case EXPR_BINARY_GREATER:
1897 case EXPR_BINARY_GREATEREQUAL:
1898 case EXPR_BINARY_BITWISE_AND:
1899 case EXPR_BINARY_BITWISE_OR:
1900 case EXPR_BINARY_BITWISE_XOR:
1901 case EXPR_BINARY_SHIFTLEFT:
1902 case EXPR_BINARY_SHIFTRIGHT:
1903 case EXPR_BINARY_ISGREATER:
1904 case EXPR_BINARY_ISGREATEREQUAL:
1905 case EXPR_BINARY_ISLESS:
1906 case EXPR_BINARY_ISLESSEQUAL:
1907 case EXPR_BINARY_ISLESSGREATER:
1908 case EXPR_BINARY_ISUNORDERED: {
1909 expression_classification_t const l = is_constant_expression(expression->binary.left);
1910 expression_classification_t const r = is_constant_expression(expression->binary.right);
1911 return l < r ? l : r;
1914 case EXPR_BINARY_LOGICAL_AND: {
1915 expression_t const *const left = expression->binary.left;
1916 expression_classification_t const lclass = is_constant_expression(left);
1917 if (lclass != EXPR_CLASS_CONSTANT)
1919 if (fold_constant_to_bool(left) == false)
1920 return EXPR_CLASS_CONSTANT;
1921 return is_constant_expression(expression->binary.right);
1924 case EXPR_BINARY_LOGICAL_OR: {
1925 expression_t const *const left = expression->binary.left;
1926 expression_classification_t const lclass = is_constant_expression(left);
1927 if (lclass != EXPR_CLASS_CONSTANT)
1929 if (fold_constant_to_bool(left) == true)
1930 return EXPR_CLASS_CONSTANT;
1931 return is_constant_expression(expression->binary.right);
1934 case EXPR_COMPOUND_LITERAL:
1935 return is_constant_initializer(expression->compound_literal.initializer);
1937 case EXPR_CONDITIONAL: {
1938 expression_t *const condition = expression->conditional.condition;
1939 expression_classification_t const cclass = is_constant_expression(condition);
1940 if (cclass != EXPR_CLASS_CONSTANT)
1943 if (fold_constant_to_bool(condition) == true) {
1944 expression_t const *const t = expression->conditional.true_expression;
1945 return t == NULL ? EXPR_CLASS_CONSTANT : is_constant_expression(t);
1947 return is_constant_expression(expression->conditional.false_expression);
1952 return EXPR_CLASS_ERROR;
1957 panic("invalid expression found (is constant expression)");
1961 * Initialize the AST construction.
1965 obstack_init(&ast_obstack);
1973 obstack_free(&ast_obstack, NULL);
1977 * Allocate an AST object of the given size.
1979 * @param size the size of the object to allocate
1981 * @return A new allocated object in the AST memeory space.
1983 void *(allocate_ast)(size_t size)
1985 return _allocate_ast(size);