2 * This file is part of cparser.
3 * Copyright (C) 2007-2008 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"
34 #if defined(__INTEL_COMPILER)
36 #elif defined(__CYGWIN__)
37 #include "win32/cygwin_math_ext.h"
42 #include "adt/error.h"
45 struct obstack ast_obstack;
50 /** If set, implicit casts are printed. */
51 bool print_implicit_casts = false;
53 /** If set parenthesis are printed to indicate operator precedence. */
54 bool print_parenthesis = false;
56 static void print_statement(const statement_t *statement);
57 static void print_expression_prec(const expression_t *expression, unsigned prec);
59 void change_indent(int delta)
65 void print_indent(void)
67 for (int i = 0; i < indent; ++i)
72 * Returns 1 if a given precedence level has right-to-left
73 * associativity, else 0.
75 * @param precedence the operator precedence
77 static int right_to_left(unsigned precedence)
81 case PREC_CONDITIONAL:
91 * Return the precedence of an expression given by its kind.
93 * @param kind the expression kind
95 static unsigned get_expression_precedence(expression_kind_t kind)
97 static const unsigned prec[] = {
98 [EXPR_UNKNOWN] = PREC_PRIMARY,
99 [EXPR_INVALID] = PREC_PRIMARY,
100 [EXPR_REFERENCE] = PREC_PRIMARY,
101 [EXPR_REFERENCE_ENUM_VALUE] = PREC_PRIMARY,
102 [EXPR_CHARACTER_CONSTANT] = PREC_PRIMARY,
103 [EXPR_WIDE_CHARACTER_CONSTANT] = PREC_PRIMARY,
104 [EXPR_CONST] = PREC_PRIMARY,
105 [EXPR_STRING_LITERAL] = PREC_PRIMARY,
106 [EXPR_WIDE_STRING_LITERAL] = PREC_PRIMARY,
107 [EXPR_COMPOUND_LITERAL] = PREC_UNARY,
108 [EXPR_CALL] = PREC_POSTFIX,
109 [EXPR_CONDITIONAL] = PREC_CONDITIONAL,
110 [EXPR_SELECT] = PREC_POSTFIX,
111 [EXPR_ARRAY_ACCESS] = PREC_POSTFIX,
112 [EXPR_SIZEOF] = PREC_UNARY,
113 [EXPR_CLASSIFY_TYPE] = PREC_UNARY,
114 [EXPR_ALIGNOF] = PREC_UNARY,
116 [EXPR_FUNCNAME] = PREC_PRIMARY,
117 [EXPR_BUILTIN_SYMBOL] = PREC_PRIMARY,
118 [EXPR_BUILTIN_CONSTANT_P] = PREC_PRIMARY,
119 [EXPR_BUILTIN_PREFETCH] = PREC_PRIMARY,
120 [EXPR_OFFSETOF] = PREC_PRIMARY,
121 [EXPR_VA_START] = PREC_PRIMARY,
122 [EXPR_VA_ARG] = PREC_PRIMARY,
123 [EXPR_STATEMENT] = PREC_PRIMARY,
124 [EXPR_LABEL_ADDRESS] = PREC_PRIMARY,
126 [EXPR_UNARY_NEGATE] = PREC_UNARY,
127 [EXPR_UNARY_PLUS] = PREC_UNARY,
128 [EXPR_UNARY_BITWISE_NEGATE] = PREC_UNARY,
129 [EXPR_UNARY_NOT] = PREC_UNARY,
130 [EXPR_UNARY_DEREFERENCE] = PREC_UNARY,
131 [EXPR_UNARY_TAKE_ADDRESS] = PREC_UNARY,
132 [EXPR_UNARY_POSTFIX_INCREMENT] = PREC_POSTFIX,
133 [EXPR_UNARY_POSTFIX_DECREMENT] = PREC_POSTFIX,
134 [EXPR_UNARY_PREFIX_INCREMENT] = PREC_UNARY,
135 [EXPR_UNARY_PREFIX_DECREMENT] = PREC_UNARY,
136 [EXPR_UNARY_CAST] = PREC_UNARY,
137 [EXPR_UNARY_CAST_IMPLICIT] = PREC_UNARY,
138 [EXPR_UNARY_ASSUME] = PREC_PRIMARY,
139 [EXPR_UNARY_DELETE] = PREC_UNARY,
140 [EXPR_UNARY_DELETE_ARRAY] = PREC_UNARY,
141 [EXPR_UNARY_THROW] = PREC_ASSIGNMENT,
143 [EXPR_BINARY_ADD] = PREC_ADDITIVE,
144 [EXPR_BINARY_SUB] = PREC_ADDITIVE,
145 [EXPR_BINARY_MUL] = PREC_MULTIPLICATIVE,
146 [EXPR_BINARY_DIV] = PREC_MULTIPLICATIVE,
147 [EXPR_BINARY_MOD] = PREC_MULTIPLICATIVE,
148 [EXPR_BINARY_EQUAL] = PREC_EQUALITY,
149 [EXPR_BINARY_NOTEQUAL] = PREC_EQUALITY,
150 [EXPR_BINARY_LESS] = PREC_RELATIONAL,
151 [EXPR_BINARY_LESSEQUAL] = PREC_RELATIONAL,
152 [EXPR_BINARY_GREATER] = PREC_RELATIONAL,
153 [EXPR_BINARY_GREATEREQUAL] = PREC_RELATIONAL,
154 [EXPR_BINARY_BITWISE_AND] = PREC_AND,
155 [EXPR_BINARY_BITWISE_OR] = PREC_OR,
156 [EXPR_BINARY_BITWISE_XOR] = PREC_XOR,
157 [EXPR_BINARY_LOGICAL_AND] = PREC_LOGICAL_AND,
158 [EXPR_BINARY_LOGICAL_OR] = PREC_LOGICAL_OR,
159 [EXPR_BINARY_SHIFTLEFT] = PREC_SHIFT,
160 [EXPR_BINARY_SHIFTRIGHT] = PREC_SHIFT,
161 [EXPR_BINARY_ASSIGN] = PREC_ASSIGNMENT,
162 [EXPR_BINARY_MUL_ASSIGN] = PREC_ASSIGNMENT,
163 [EXPR_BINARY_DIV_ASSIGN] = PREC_ASSIGNMENT,
164 [EXPR_BINARY_MOD_ASSIGN] = PREC_ASSIGNMENT,
165 [EXPR_BINARY_ADD_ASSIGN] = PREC_ASSIGNMENT,
166 [EXPR_BINARY_SUB_ASSIGN] = PREC_ASSIGNMENT,
167 [EXPR_BINARY_SHIFTLEFT_ASSIGN] = PREC_ASSIGNMENT,
168 [EXPR_BINARY_SHIFTRIGHT_ASSIGN] = PREC_ASSIGNMENT,
169 [EXPR_BINARY_BITWISE_AND_ASSIGN] = PREC_ASSIGNMENT,
170 [EXPR_BINARY_BITWISE_XOR_ASSIGN] = PREC_ASSIGNMENT,
171 [EXPR_BINARY_BITWISE_OR_ASSIGN] = PREC_ASSIGNMENT,
172 [EXPR_BINARY_COMMA] = PREC_EXPRESSION,
174 [EXPR_BINARY_ISGREATER] = PREC_PRIMARY,
175 [EXPR_BINARY_ISGREATEREQUAL] = PREC_PRIMARY,
176 [EXPR_BINARY_ISLESS] = PREC_PRIMARY,
177 [EXPR_BINARY_ISLESSEQUAL] = PREC_PRIMARY,
178 [EXPR_BINARY_ISLESSGREATER] = PREC_PRIMARY,
179 [EXPR_BINARY_ISUNORDERED] = PREC_PRIMARY
181 assert((size_t)kind < lengthof(prec));
182 unsigned res = prec[kind];
184 assert(res != PREC_BOTTOM);
189 * Print a constant expression.
191 * @param cnst the constant expression
193 static void print_const(const const_expression_t *cnst)
195 if (cnst->base.type == NULL)
198 const type_t *const type = skip_typeref(cnst->base.type);
200 if (is_type_atomic(type, ATOMIC_TYPE_BOOL)) {
201 fputs(cnst->v.int_value ? "true" : "false", out);
202 } else if (is_type_integer(type)) {
203 fprintf(out, "%lld", cnst->v.int_value);
204 } else if (is_type_float(type)) {
205 long double const val = cnst->v.float_value;
207 /* ARG, no way to print long double */
208 fprintf(out, "%.20g", (double)val);
210 fprintf(out, "%.20Lg", val);
212 if (isfinite(val) && truncl(val) == val)
215 panic("unknown constant");
219 switch (type->atomic.akind) {
220 case ATOMIC_TYPE_UINT: suffix = "U"; break;
221 case ATOMIC_TYPE_LONG: suffix = "L"; break;
222 case ATOMIC_TYPE_ULONG: suffix = "UL"; break;
223 case ATOMIC_TYPE_LONGLONG: suffix = "LL"; break;
224 case ATOMIC_TYPE_ULONGLONG: suffix = "ULL"; break;
225 case ATOMIC_TYPE_FLOAT: suffix = "F"; break;
226 case ATOMIC_TYPE_LONG_DOUBLE: suffix = "L"; break;
234 * Print a quoted string constant.
236 * @param string the string constant
237 * @param border the border char
238 * @param skip number of chars to skip at the end
240 static void print_quoted_string(const string_t *const string, char border, int skip)
243 const char *end = string->begin + string->size - skip;
244 for (const char *c = string->begin; c != end; ++c) {
245 unsigned char const tc = *c;
250 case '\\': fputs("\\\\", out); break;
251 case '\a': fputs("\\a", out); break;
252 case '\b': fputs("\\b", out); break;
253 case '\f': fputs("\\f", out); break;
254 case '\n': fputs("\\n", out); break;
255 case '\r': fputs("\\r", out); break;
256 case '\t': fputs("\\t", out); break;
257 case '\v': fputs("\\v", out); break;
258 case '\?': fputs("\\?", out); break;
260 if (c_mode & _GNUC) {
261 fputs("\\e", out); break;
265 if (tc < 0x80 && !isprint(tc)) {
266 fprintf(out, "\\%03o", (unsigned)tc);
277 * Prints a wide string literal expression.
279 * @param wstr the wide string literal expression
280 * @param border the border char
281 * @param skip number of chars to skip at the end
283 static void print_quoted_wide_string(const wide_string_t *const wstr,
284 char border, int skip)
288 const wchar_rep_t *end = wstr->begin + wstr->size - skip;
289 for (const wchar_rep_t *c = wstr->begin; c != end; ++c) {
291 case L'\"': fputs("\\\"", out); break;
292 case L'\\': fputs("\\\\", out); break;
293 case L'\a': fputs("\\a", out); break;
294 case L'\b': fputs("\\b", out); break;
295 case L'\f': fputs("\\f", out); break;
296 case L'\n': fputs("\\n", out); break;
297 case L'\r': fputs("\\r", out); break;
298 case L'\t': fputs("\\t", out); break;
299 case L'\v': fputs("\\v", out); break;
300 case L'\?': fputs("\\?", out); break;
302 if (c_mode & _GNUC) {
303 fputs("\\e", out); break;
307 const unsigned tc = *c;
312 fprintf(out, "\\%03o", tc);
314 } else if (tc < 0x800) {
315 fputc(0xC0 | (tc >> 6), out);
316 fputc(0x80 | (tc & 0x3F), out);
317 } else if (tc < 0x10000) {
318 fputc(0xE0 | ( tc >> 12), out);
319 fputc(0x80 | ((tc >> 6) & 0x3F), out);
320 fputc(0x80 | ( tc & 0x3F), out);
322 fputc(0xF0 | ( tc >> 18), out);
323 fputc(0x80 | ((tc >> 12) & 0x3F), out);
324 fputc(0x80 | ((tc >> 6) & 0x3F), out);
325 fputc(0x80 | ( tc & 0x3F), out);
334 * Print a constant character expression.
336 * @param cnst the constant character expression
338 static void print_character_constant(const const_expression_t *cnst)
340 print_quoted_string(&cnst->v.character, '\'', 0);
343 static void print_wide_character_constant(const const_expression_t *cnst)
345 print_quoted_wide_string(&cnst->v.wide_character, '\'', 0);
349 * Prints a string literal expression.
351 * @param string_literal the string literal expression
353 static void print_string_literal(
354 const string_literal_expression_t *string_literal)
356 print_quoted_string(&string_literal->value, '"', 1);
360 * Prints a predefined symbol.
362 static void print_funcname(const funcname_expression_t *funcname)
365 switch (funcname->kind) {
366 case FUNCNAME_FUNCTION: s = (c_mode & _C99) ? "__func__" : "__FUNCTION__"; break;
367 case FUNCNAME_PRETTY_FUNCTION: s = "__PRETTY_FUNCTION__"; break;
368 case FUNCNAME_FUNCSIG: s = "__FUNCSIG__"; break;
369 case FUNCNAME_FUNCDNAME: s = "__FUNCDNAME__"; break;
374 static void print_wide_string_literal(
375 const wide_string_literal_expression_t *const wstr)
377 print_quoted_wide_string(&wstr->value, '"', 1);
380 static void print_compound_literal(
381 const compound_literal_expression_t *expression)
384 print_type(expression->type);
386 print_initializer(expression->initializer);
389 static void print_assignment_expression(const expression_t *const expr)
391 print_expression_prec(expr, PREC_ASSIGNMENT);
395 * Prints a call expression.
397 * @param call the call expression
399 static void print_call_expression(const call_expression_t *call)
401 unsigned prec = get_expression_precedence(call->base.kind);
402 print_expression_prec(call->function, prec);
404 call_argument_t *argument = call->arguments;
406 while (argument != NULL) {
412 print_assignment_expression(argument->expression);
414 argument = argument->next;
420 * Prints a binary expression.
422 * @param binexpr the binary expression
424 static void print_binary_expression(const binary_expression_t *binexpr)
426 unsigned prec = get_expression_precedence(binexpr->base.kind);
427 int r2l = right_to_left(prec);
429 print_expression_prec(binexpr->left, prec + r2l);
431 switch (binexpr->base.kind) {
432 case EXPR_BINARY_COMMA: op = ", "; break;
433 case EXPR_BINARY_ASSIGN: op = " = "; break;
434 case EXPR_BINARY_ADD: op = " + "; break;
435 case EXPR_BINARY_SUB: op = " - "; break;
436 case EXPR_BINARY_MUL: op = " * "; break;
437 case EXPR_BINARY_MOD: op = " % "; break;
438 case EXPR_BINARY_DIV: op = " / "; break;
439 case EXPR_BINARY_BITWISE_OR: op = " | "; break;
440 case EXPR_BINARY_BITWISE_AND: op = " & "; break;
441 case EXPR_BINARY_BITWISE_XOR: op = " ^ "; break;
442 case EXPR_BINARY_LOGICAL_OR: op = " || "; break;
443 case EXPR_BINARY_LOGICAL_AND: op = " && "; break;
444 case EXPR_BINARY_NOTEQUAL: op = " != "; break;
445 case EXPR_BINARY_EQUAL: op = " == "; break;
446 case EXPR_BINARY_LESS: op = " < "; break;
447 case EXPR_BINARY_LESSEQUAL: op = " <= "; break;
448 case EXPR_BINARY_GREATER: op = " > "; break;
449 case EXPR_BINARY_GREATEREQUAL: op = " >= "; break;
450 case EXPR_BINARY_SHIFTLEFT: op = " << "; break;
451 case EXPR_BINARY_SHIFTRIGHT: op = " >> "; break;
453 case EXPR_BINARY_ADD_ASSIGN: op = " += "; break;
454 case EXPR_BINARY_SUB_ASSIGN: op = " -= "; break;
455 case EXPR_BINARY_MUL_ASSIGN: op = " *= "; break;
456 case EXPR_BINARY_MOD_ASSIGN: op = " %= "; break;
457 case EXPR_BINARY_DIV_ASSIGN: op = " /= "; break;
458 case EXPR_BINARY_BITWISE_OR_ASSIGN: op = " |= "; break;
459 case EXPR_BINARY_BITWISE_AND_ASSIGN: op = " &= "; break;
460 case EXPR_BINARY_BITWISE_XOR_ASSIGN: op = " ^= "; break;
461 case EXPR_BINARY_SHIFTLEFT_ASSIGN: op = " <<= "; break;
462 case EXPR_BINARY_SHIFTRIGHT_ASSIGN: op = " >>= "; break;
463 default: panic("invalid binexpression found");
466 print_expression_prec(binexpr->right, prec + 1 - r2l);
470 * Prints an unary expression.
472 * @param unexpr the unary expression
474 static void print_unary_expression(const unary_expression_t *unexpr)
476 unsigned prec = get_expression_precedence(unexpr->base.kind);
477 switch (unexpr->base.kind) {
478 case EXPR_UNARY_NEGATE: fputc('-', out); break;
479 case EXPR_UNARY_PLUS: fputc('+', out); break;
480 case EXPR_UNARY_NOT: fputc('!', out); break;
481 case EXPR_UNARY_BITWISE_NEGATE: fputc('~', out); break;
482 case EXPR_UNARY_PREFIX_INCREMENT: fputs("++", out); break;
483 case EXPR_UNARY_PREFIX_DECREMENT: fputs("--", out); break;
484 case EXPR_UNARY_DEREFERENCE: fputc('*', out); break;
485 case EXPR_UNARY_TAKE_ADDRESS: fputc('&', out); break;
486 case EXPR_UNARY_DELETE: fputs("delete ", out); break;
487 case EXPR_UNARY_DELETE_ARRAY: fputs("delete [] ", out); break;
489 case EXPR_UNARY_POSTFIX_INCREMENT:
490 print_expression_prec(unexpr->value, prec);
493 case EXPR_UNARY_POSTFIX_DECREMENT:
494 print_expression_prec(unexpr->value, prec);
497 case EXPR_UNARY_CAST_IMPLICIT:
498 case EXPR_UNARY_CAST:
500 print_type(unexpr->base.type);
503 case EXPR_UNARY_ASSUME:
504 fputs("__assume(", out);
505 print_assignment_expression(unexpr->value);
509 case EXPR_UNARY_THROW:
510 if (unexpr->value == NULL) {
514 fputs("throw ", out);
518 panic("invalid unary expression found");
520 print_expression_prec(unexpr->value, prec);
524 * Prints a reference expression.
526 * @param ref the reference expression
528 static void print_reference_expression(const reference_expression_t *ref)
530 fputs(ref->entity->base.symbol->string, out);
534 * Prints a label address expression.
536 * @param ref the reference expression
538 static void print_label_address_expression(const label_address_expression_t *le)
540 fprintf(out, "&&%s", le->label->base.symbol->string);
544 * Prints an array expression.
546 * @param expression the array expression
548 static void print_array_expression(const array_access_expression_t *expression)
550 unsigned prec = get_expression_precedence(expression->base.kind);
551 if (!expression->flipped) {
552 print_expression_prec(expression->array_ref, prec);
554 print_expression(expression->index);
557 print_expression_prec(expression->index, prec);
559 print_expression(expression->array_ref);
565 * Prints a typeproperty expression (sizeof or __alignof__).
567 * @param expression the type property expression
569 static void print_typeprop_expression(const typeprop_expression_t *expression)
571 if (expression->base.kind == EXPR_SIZEOF) {
572 fputs("sizeof", out);
574 assert(expression->base.kind == EXPR_ALIGNOF);
575 fputs("__alignof__", out);
577 if (expression->tp_expression != NULL) {
578 /* always print the '()' here, sizeof x is right but unusual */
580 print_expression(expression->tp_expression);
584 print_type(expression->type);
590 * Prints an builtin symbol.
592 * @param expression the builtin symbol expression
594 static void print_builtin_symbol(const builtin_symbol_expression_t *expression)
596 fputs(expression->symbol->string, out);
600 * Prints a builtin constant expression.
602 * @param expression the builtin constant expression
604 static void print_builtin_constant(const builtin_constant_expression_t *expression)
606 fputs("__builtin_constant_p(", out);
607 print_assignment_expression(expression->value);
612 * Prints a builtin prefetch expression.
614 * @param expression the builtin prefetch expression
616 static void print_builtin_prefetch(const builtin_prefetch_expression_t *expression)
618 fputs("__builtin_prefetch(", out);
619 print_assignment_expression(expression->adr);
620 if (expression->rw) {
622 print_assignment_expression(expression->rw);
624 if (expression->locality) {
626 print_assignment_expression(expression->locality);
632 * Prints a conditional expression.
634 * @param expression the conditional expression
636 static void print_conditional(const conditional_expression_t *expression)
638 print_expression_prec(expression->condition, PREC_LOGICAL_OR);
640 if (expression->true_expression != NULL) {
641 print_expression_prec(expression->true_expression, PREC_EXPRESSION);
646 precedence_t prec = c_mode & _CXX ? PREC_ASSIGNMENT : PREC_CONDITIONAL;
647 print_expression_prec(expression->false_expression, prec);
651 * Prints a va_start expression.
653 * @param expression the va_start expression
655 static void print_va_start(const va_start_expression_t *const expression)
657 fputs("__builtin_va_start(", out);
658 print_assignment_expression(expression->ap);
660 fputs(expression->parameter->base.base.symbol->string, out);
665 * Prints a va_arg expression.
667 * @param expression the va_arg expression
669 static void print_va_arg(const va_arg_expression_t *expression)
671 fputs("__builtin_va_arg(", out);
672 print_assignment_expression(expression->ap);
674 print_type(expression->base.type);
679 * Prints a select expression (. or ->).
681 * @param expression the select expression
683 static void print_select(const select_expression_t *expression)
685 unsigned prec = get_expression_precedence(expression->base.kind);
686 print_expression_prec(expression->compound, prec);
687 if (is_type_pointer(skip_typeref(expression->compound->base.type))) {
692 fputs(expression->compound_entry->base.symbol->string, out);
696 * Prints a type classify expression.
698 * @param expr the type classify expression
700 static void print_classify_type_expression(
701 const classify_type_expression_t *const expr)
703 fputs("__builtin_classify_type(", out);
704 print_assignment_expression(expr->type_expression);
709 * Prints a designator.
711 * @param designator the designator
713 static void print_designator(const designator_t *designator)
715 for ( ; designator != NULL; designator = designator->next) {
716 if (designator->symbol == NULL) {
718 print_expression(designator->array_index);
722 fputs(designator->symbol->string, out);
728 * Prints an offsetof expression.
730 * @param expression the offset expression
732 static void print_offsetof_expression(const offsetof_expression_t *expression)
734 fputs("__builtin_offsetof", out);
736 print_type(expression->type);
738 print_designator(expression->designator);
743 * Prints a statement expression.
745 * @param expression the statement expression
747 static void print_statement_expression(const statement_expression_t *expression)
750 print_statement(expression->statement);
755 * Prints an expression with parenthesis if needed.
757 * @param expression the expression to print
758 * @param top_prec the precedence of the user of this expression.
760 static void print_expression_prec(const expression_t *expression, unsigned top_prec)
762 if (expression->kind == EXPR_UNARY_CAST_IMPLICIT && !print_implicit_casts) {
763 expression = expression->unary.value;
767 expression->base.parenthesized ||
768 (print_parenthesis && top_prec != PREC_BOTTOM) ||
769 top_prec > get_expression_precedence(expression->base.kind);
773 switch (expression->kind) {
776 fputs("$invalid expression$", out);
778 case EXPR_CHARACTER_CONSTANT:
779 print_character_constant(&expression->conste);
781 case EXPR_WIDE_CHARACTER_CONSTANT:
782 print_wide_character_constant(&expression->conste);
785 print_const(&expression->conste);
788 print_funcname(&expression->funcname);
790 case EXPR_STRING_LITERAL:
791 print_string_literal(&expression->string);
793 case EXPR_WIDE_STRING_LITERAL:
794 print_wide_string_literal(&expression->wide_string);
796 case EXPR_COMPOUND_LITERAL:
797 print_compound_literal(&expression->compound_literal);
800 print_call_expression(&expression->call);
803 print_binary_expression(&expression->binary);
806 case EXPR_REFERENCE_ENUM_VALUE:
807 print_reference_expression(&expression->reference);
809 case EXPR_ARRAY_ACCESS:
810 print_array_expression(&expression->array_access);
812 case EXPR_LABEL_ADDRESS:
813 print_label_address_expression(&expression->label_address);
816 print_unary_expression(&expression->unary);
820 print_typeprop_expression(&expression->typeprop);
822 case EXPR_BUILTIN_SYMBOL:
823 print_builtin_symbol(&expression->builtin_symbol);
825 case EXPR_BUILTIN_CONSTANT_P:
826 print_builtin_constant(&expression->builtin_constant);
828 case EXPR_BUILTIN_PREFETCH:
829 print_builtin_prefetch(&expression->builtin_prefetch);
831 case EXPR_CONDITIONAL:
832 print_conditional(&expression->conditional);
835 print_va_start(&expression->va_starte);
838 print_va_arg(&expression->va_arge);
841 print_select(&expression->select);
843 case EXPR_CLASSIFY_TYPE:
844 print_classify_type_expression(&expression->classify_type);
847 print_offsetof_expression(&expression->offsetofe);
850 print_statement_expression(&expression->statement);
855 fprintf(out, "some expression of type %d", (int)expression->kind);
863 * Print an compound statement.
865 * @param block the compound statement
867 static void print_compound_statement(const compound_statement_t *block)
872 statement_t *statement = block->statements;
873 while (statement != NULL) {
874 if (statement->base.kind == STATEMENT_CASE_LABEL)
876 if (statement->kind != STATEMENT_LABEL)
878 print_statement(statement);
880 statement = statement->base.next;
884 fputs(block->stmt_expr ? "}" : "}\n", out);
888 * Print a return statement.
890 * @param statement the return statement
892 static void print_return_statement(const return_statement_t *statement)
894 expression_t const *const val = statement->value;
896 fputs("return ", out);
897 print_expression(val);
900 fputs("return;\n", out);
905 * Print an expression statement.
907 * @param statement the expression statement
909 static void print_expression_statement(const expression_statement_t *statement)
911 print_expression(statement->expression);
916 * Print a goto statement.
918 * @param statement the goto statement
920 static void print_goto_statement(const goto_statement_t *statement)
923 if (statement->expression != NULL) {
925 print_expression(statement->expression);
927 fputs(statement->label->base.symbol->string, out);
933 * Print a label statement.
935 * @param statement the label statement
937 static void print_label_statement(const label_statement_t *statement)
939 fprintf(out, "%s:\n", statement->label->base.symbol->string);
941 print_statement(statement->statement);
945 * Print an if statement.
947 * @param statement the if statement
949 static void print_if_statement(const if_statement_t *statement)
952 print_expression(statement->condition);
954 print_statement(statement->true_statement);
956 if (statement->false_statement != NULL) {
959 print_statement(statement->false_statement);
964 * Print a switch statement.
966 * @param statement the switch statement
968 static void print_switch_statement(const switch_statement_t *statement)
970 fputs("switch (", out);
971 print_expression(statement->expression);
973 print_statement(statement->body);
977 * Print a case label (including the default label).
979 * @param statement the case label statement
981 static void print_case_label(const case_label_statement_t *statement)
983 if (statement->expression == NULL) {
984 fputs("default:\n", out);
987 print_expression(statement->expression);
988 if (statement->end_range != NULL) {
990 print_expression(statement->end_range);
995 if (statement->statement != NULL) {
996 if (statement->statement->base.kind == STATEMENT_CASE_LABEL) {
1000 print_statement(statement->statement);
1004 static void print_typedef(const entity_t *entity)
1006 fputs("typedef ", out);
1007 print_type_ext(entity->typedefe.type, entity->base.symbol, NULL);
1012 * returns true if the entity is a compiler generated one and has no real
1013 * correspondenc in the source file
1015 static bool is_generated_entity(const entity_t *entity)
1017 if (entity->kind == ENTITY_TYPEDEF)
1018 return entity->typedefe.builtin;
1020 if (is_declaration(entity))
1021 return entity->declaration.implicit;
1027 * Print a declaration statement.
1029 * @param statement the statement
1031 static void print_declaration_statement(
1032 const declaration_statement_t *statement)
1035 entity_t *entity = statement->declarations_begin;
1036 if (entity == NULL) {
1037 fputs("/* empty declaration statement */\n", out);
1041 entity_t *const end = statement->declarations_end->base.next;
1042 for (; entity != end; entity = entity->base.next) {
1043 if (entity->kind == ENTITY_ENUM_VALUE)
1045 if (is_generated_entity(entity))
1054 print_entity(entity);
1060 * Print a while statement.
1062 * @param statement the statement
1064 static void print_while_statement(const while_statement_t *statement)
1066 fputs("while (", out);
1067 print_expression(statement->condition);
1069 print_statement(statement->body);
1073 * Print a do-while statement.
1075 * @param statement the statement
1077 static void print_do_while_statement(const do_while_statement_t *statement)
1080 print_statement(statement->body);
1082 fputs("while (", out);
1083 print_expression(statement->condition);
1088 * Print a for statement.
1090 * @param statement the statement
1092 static void print_for_statement(const for_statement_t *statement)
1094 fputs("for (", out);
1095 entity_t *entity = statement->scope.entities;
1096 while (entity != NULL && is_generated_entity(entity))
1097 entity = entity->base.next;
1099 if (entity != NULL) {
1100 assert(statement->initialisation == NULL);
1101 assert(is_declaration(entity));
1102 print_declaration(entity);
1103 if (entity->base.next != NULL) {
1104 panic("multiple declarations in for statement not supported yet");
1107 if (statement->initialisation) {
1108 print_expression(statement->initialisation);
1112 if (statement->condition != NULL) {
1114 print_expression(statement->condition);
1117 if (statement->step != NULL) {
1119 print_expression(statement->step);
1122 print_statement(statement->body);
1126 * Print assembler arguments.
1128 * @param arguments the arguments
1130 static void print_asm_arguments(asm_argument_t *arguments)
1132 asm_argument_t *argument = arguments;
1133 for (; argument != NULL; argument = argument->next) {
1134 if (argument != arguments)
1137 if (argument->symbol) {
1138 fprintf(out, "[%s] ", argument->symbol->string);
1140 print_quoted_string(&argument->constraints, '"', 1);
1142 print_expression(argument->expression);
1148 * Print assembler clobbers.
1150 * @param clobbers the clobbers
1152 static void print_asm_clobbers(asm_clobber_t *clobbers)
1154 asm_clobber_t *clobber = clobbers;
1155 for (; clobber != NULL; clobber = clobber->next) {
1156 if (clobber != clobbers)
1159 print_quoted_string(&clobber->clobber, '"', 1);
1164 * Print an assembler statement.
1166 * @param statement the statement
1168 static void print_asm_statement(const asm_statement_t *statement)
1171 if (statement->is_volatile) {
1172 fputs("volatile ", out);
1175 print_quoted_string(&statement->asm_text, '"', 1);
1176 if (statement->outputs == NULL &&
1177 statement->inputs == NULL &&
1178 statement->clobbers == NULL)
1179 goto end_of_print_asm_statement;
1182 print_asm_arguments(statement->outputs);
1183 if (statement->inputs == NULL && statement->clobbers == NULL)
1184 goto end_of_print_asm_statement;
1187 print_asm_arguments(statement->inputs);
1188 if (statement->clobbers == NULL)
1189 goto end_of_print_asm_statement;
1192 print_asm_clobbers(statement->clobbers);
1194 end_of_print_asm_statement:
1199 * Print a microsoft __try statement.
1201 * @param statement the statement
1203 static void print_ms_try_statement(const ms_try_statement_t *statement)
1205 fputs("__try ", out);
1206 print_statement(statement->try_statement);
1208 if (statement->except_expression != NULL) {
1209 fputs("__except(", out);
1210 print_expression(statement->except_expression);
1213 fputs("__finally ", out);
1215 print_statement(statement->final_statement);
1219 * Print a microsoft __leave statement.
1221 * @param statement the statement
1223 static void print_leave_statement(const leave_statement_t *statement)
1226 fputs("__leave;\n", out);
1230 * Print a statement.
1232 * @param statement the statement
1234 void print_statement(const statement_t *statement)
1236 switch (statement->kind) {
1237 case STATEMENT_EMPTY:
1240 case STATEMENT_COMPOUND:
1241 print_compound_statement(&statement->compound);
1243 case STATEMENT_RETURN:
1244 print_return_statement(&statement->returns);
1246 case STATEMENT_EXPRESSION:
1247 print_expression_statement(&statement->expression);
1249 case STATEMENT_LABEL:
1250 print_label_statement(&statement->label);
1252 case STATEMENT_GOTO:
1253 print_goto_statement(&statement->gotos);
1255 case STATEMENT_CONTINUE:
1256 fputs("continue;\n", out);
1258 case STATEMENT_BREAK:
1259 fputs("break;\n", out);
1262 print_if_statement(&statement->ifs);
1264 case STATEMENT_SWITCH:
1265 print_switch_statement(&statement->switchs);
1267 case STATEMENT_CASE_LABEL:
1268 print_case_label(&statement->case_label);
1270 case STATEMENT_DECLARATION:
1271 print_declaration_statement(&statement->declaration);
1273 case STATEMENT_WHILE:
1274 print_while_statement(&statement->whiles);
1276 case STATEMENT_DO_WHILE:
1277 print_do_while_statement(&statement->do_while);
1280 print_for_statement(&statement->fors);
1283 print_asm_statement(&statement->asms);
1285 case STATEMENT_MS_TRY:
1286 print_ms_try_statement(&statement->ms_try);
1288 case STATEMENT_LEAVE:
1289 print_leave_statement(&statement->leave);
1291 case STATEMENT_INVALID:
1292 fputs("$invalid statement$\n", out);
1298 * Print a storage class.
1300 * @param storage_class the storage class
1302 static void print_storage_class(storage_class_tag_t storage_class)
1304 switch (storage_class) {
1305 case STORAGE_CLASS_NONE: return;
1306 case STORAGE_CLASS_TYPEDEF: fputs("typedef ", out); return;
1307 case STORAGE_CLASS_EXTERN: fputs("extern ", out); return;
1308 case STORAGE_CLASS_STATIC: fputs("static ", out); return;
1309 case STORAGE_CLASS_AUTO: fputs("auto ", out); return;
1310 case STORAGE_CLASS_REGISTER: fputs("register ", out); return;
1312 panic("invalid storage class");
1316 * Print an initializer.
1318 * @param initializer the initializer
1320 void print_initializer(const initializer_t *initializer)
1322 if (initializer == NULL) {
1327 switch (initializer->kind) {
1328 case INITIALIZER_VALUE: {
1329 const initializer_value_t *value = &initializer->value;
1330 print_assignment_expression(value->value);
1333 case INITIALIZER_LIST: {
1334 assert(initializer->kind == INITIALIZER_LIST);
1336 const initializer_list_t *list = &initializer->list;
1338 for (size_t i = 0 ; i < list->len; ++i) {
1339 const initializer_t *sub_init = list->initializers[i];
1340 print_initializer(list->initializers[i]);
1341 if (i < list->len-1) {
1342 if (sub_init == NULL || sub_init->kind != INITIALIZER_DESIGNATOR)
1349 case INITIALIZER_STRING:
1350 print_quoted_string(&initializer->string.string, '"', 1);
1352 case INITIALIZER_WIDE_STRING:
1353 print_quoted_wide_string(&initializer->wide_string.string, '"', 1);
1355 case INITIALIZER_DESIGNATOR:
1356 print_designator(initializer->designator.designator);
1361 panic("invalid initializer kind found");
1365 * Print microsoft extended declaration modifiers.
1367 static void print_ms_modifiers(const declaration_t *declaration)
1369 if ((c_mode & _MS) == 0)
1372 decl_modifiers_t modifiers = declaration->modifiers;
1374 bool ds_shown = false;
1375 const char *next = "(";
1377 if (declaration->base.kind == ENTITY_VARIABLE) {
1378 variable_t *variable = (variable_t*)declaration;
1379 if (variable->alignment != 0
1380 || variable->get_property_sym != NULL
1381 || variable->put_property_sym != NULL) {
1383 fputs("__declspec", out);
1387 if (variable->alignment != 0) {
1388 fputs(next, out); next = ", "; fprintf(out, "align(%u)", variable->alignment);
1390 if (variable->get_property_sym != NULL
1391 || variable->put_property_sym != NULL) {
1393 fputs(next, out); next = ", "; fputs("property(", out);
1394 if (variable->get_property_sym != NULL) {
1395 fprintf(out, "get=%s", variable->get_property_sym->string);
1398 if (variable->put_property_sym != NULL)
1399 fprintf(out, "%sput=%s", comma, variable->put_property_sym->string);
1405 /* DM_FORCEINLINE handled outside. */
1406 if ((modifiers & ~DM_FORCEINLINE) != 0) {
1408 fputs("__declspec", out);
1411 if (modifiers & DM_DLLIMPORT) {
1412 fputs(next, out); next = ", "; fputs("dllimport", out);
1414 if (modifiers & DM_DLLEXPORT) {
1415 fputs(next, out); next = ", "; fputs("dllexport", out);
1417 if (modifiers & DM_THREAD) {
1418 fputs(next, out); next = ", "; fputs("thread", out);
1420 if (modifiers & DM_NAKED) {
1421 fputs(next, out); next = ", "; fputs("naked", out);
1423 if (modifiers & DM_THREAD) {
1424 fputs(next, out); next = ", "; fputs("thread", out);
1426 if (modifiers & DM_SELECTANY) {
1427 fputs(next, out); next = ", "; fputs("selectany", out);
1429 if (modifiers & DM_NOTHROW) {
1430 fputs(next, out); next = ", "; fputs("nothrow", out);
1432 if (modifiers & DM_NORETURN) {
1433 fputs(next, out); next = ", "; fputs("noreturn", out);
1435 if (modifiers & DM_NOINLINE) {
1436 fputs(next, out); next = ", "; fputs("noinline", out);
1438 if (modifiers & DM_DEPRECATED) {
1439 fputs(next, out); next = ", "; fputs("deprecated", out);
1440 if (declaration->deprecated_string != NULL)
1441 fprintf(out, "(\"%s\")",
1442 declaration->deprecated_string);
1444 if (modifiers & DM_RESTRICT) {
1445 fputs(next, out); next = ", "; fputs("restrict", out);
1447 if (modifiers & DM_NOALIAS) {
1448 fputs(next, out); next = ", "; fputs("noalias", out);
1456 static void print_scope(const scope_t *scope)
1458 const entity_t *entity = scope->entities;
1459 for ( ; entity != NULL; entity = entity->base.next) {
1461 print_entity(entity);
1466 static void print_namespace(const namespace_t *namespace)
1468 fputs("namespace ", out);
1469 if (namespace->base.symbol != NULL) {
1470 fputs(namespace->base.symbol->string, out);
1477 print_scope(&namespace->members);
1485 * Print a variable or function declaration
1487 void print_declaration(const entity_t *entity)
1489 assert(is_declaration(entity));
1490 const declaration_t *declaration = &entity->declaration;
1492 print_storage_class((storage_class_tag_t)declaration->declared_storage_class);
1493 if (entity->kind == ENTITY_FUNCTION) {
1494 function_t *function = (function_t*)declaration;
1495 if (function->is_inline) {
1496 if (declaration->modifiers & DM_FORCEINLINE) {
1497 fputs("__forceinline ", out);
1498 } else if (declaration->modifiers & DM_MICROSOFT_INLINE) {
1499 fputs("__inline ", out);
1501 fputs("inline ", out);
1505 print_ms_modifiers(declaration);
1506 switch (entity->kind) {
1507 case ENTITY_FUNCTION:
1508 print_type_ext(entity->declaration.type, entity->base.symbol,
1509 &entity->function.parameters);
1511 if (entity->function.statement != NULL) {
1514 print_statement(entity->function.statement);
1519 case ENTITY_VARIABLE:
1520 if (entity->variable.thread_local)
1521 fputs("__thread ", out);
1522 print_type_ext(declaration->type, declaration->base.symbol, NULL);
1523 if (entity->variable.initializer != NULL) {
1525 print_initializer(entity->variable.initializer);
1530 print_type_ext(declaration->type, declaration->base.symbol, NULL);
1537 * Prints an expression.
1539 * @param expression the expression
1541 void print_expression(const expression_t *expression)
1543 print_expression_prec(expression, PREC_BOTTOM);
1547 * Print a declaration.
1549 * @param declaration the declaration
1551 void print_entity(const entity_t *entity)
1553 if (entity->base.namespc != NAMESPACE_NORMAL && entity->base.symbol == NULL)
1556 switch ((entity_kind_tag_t)entity->kind) {
1557 case ENTITY_VARIABLE:
1558 case ENTITY_PARAMETER:
1559 case ENTITY_COMPOUND_MEMBER:
1560 print_declaration(entity);
1562 case ENTITY_FUNCTION:
1563 print_declaration(entity);
1565 case ENTITY_TYPEDEF:
1566 print_typedef(entity);
1569 fputs("struct ", out);
1570 fputs(entity->base.symbol->string, out);
1571 if (entity->structe.complete) {
1573 print_compound_definition(&entity->structe);
1578 fputs("union ", out);
1579 fputs(entity->base.symbol->string, out);
1580 if (entity->unione.complete) {
1582 print_compound_definition(&entity->unione);
1587 fputs("enum ", out);
1588 fputs(entity->base.symbol->string, out);
1590 print_enum_definition(&entity->enume);
1593 case ENTITY_NAMESPACE:
1594 print_namespace(&entity->namespacee);
1596 case ENTITY_LOCAL_LABEL:
1597 fprintf(out, "__label__ %s;", entity->base.symbol->string);
1600 case ENTITY_ENUM_VALUE:
1601 panic("print_entity used on unexpected entity type");
1602 case ENTITY_INVALID:
1605 panic("Invalid entity type encountered");
1609 * Print the AST of a translation unit.
1611 * @param unit the translation unit
1613 void print_ast(const translation_unit_t *unit)
1617 entity_t *entity = unit->scope.entities;
1618 for ( ; entity != NULL; entity = entity->base.next) {
1619 if (entity->kind == ENTITY_ENUM_VALUE)
1621 if (entity->base.namespc != NAMESPACE_NORMAL
1622 && entity->base.symbol == NULL)
1624 if (is_generated_entity(entity))
1628 print_entity(entity);
1633 bool is_constant_initializer(const initializer_t *initializer)
1635 switch (initializer->kind) {
1636 case INITIALIZER_STRING:
1637 case INITIALIZER_WIDE_STRING:
1638 case INITIALIZER_DESIGNATOR:
1641 case INITIALIZER_VALUE:
1642 return is_constant_expression(initializer->value.value);
1644 case INITIALIZER_LIST:
1645 for (size_t i = 0; i < initializer->list.len; ++i) {
1646 initializer_t *sub_initializer = initializer->list.initializers[i];
1647 if (!is_constant_initializer(sub_initializer))
1652 panic("invalid initializer kind found");
1655 static bool is_object_with_linker_constant_address(const expression_t *expression)
1657 switch (expression->kind) {
1658 case EXPR_UNARY_DEREFERENCE:
1659 return is_address_constant(expression->unary.value);
1662 type_t *base_type = skip_typeref(expression->select.compound->base.type);
1663 if (is_type_pointer(base_type)) {
1665 return is_address_constant(expression->select.compound);
1667 return is_object_with_linker_constant_address(expression->select.compound);
1671 case EXPR_ARRAY_ACCESS:
1672 return is_constant_expression(expression->array_access.index)
1673 && is_address_constant(expression->array_access.array_ref);
1675 case EXPR_REFERENCE: {
1676 entity_t *entity = expression->reference.entity;
1677 if (is_declaration(entity)) {
1678 switch ((storage_class_tag_t)entity->declaration.storage_class) {
1679 case STORAGE_CLASS_NONE:
1680 case STORAGE_CLASS_EXTERN:
1681 case STORAGE_CLASS_STATIC:
1683 entity->kind != ENTITY_VARIABLE ||
1684 !entity->variable.thread_local;
1686 case STORAGE_CLASS_REGISTER:
1687 case STORAGE_CLASS_TYPEDEF:
1688 case STORAGE_CLASS_AUTO:
1700 bool is_address_constant(const expression_t *expression)
1702 switch (expression->kind) {
1703 case EXPR_UNARY_TAKE_ADDRESS:
1704 return is_object_with_linker_constant_address(expression->unary.value);
1706 case EXPR_UNARY_DEREFERENCE: {
1708 = revert_automatic_type_conversion(expression->unary.value);
1709 /* dereferencing a function is a NOP */
1710 if (is_type_function(real_type)) {
1711 return is_address_constant(expression->unary.value);
1716 case EXPR_UNARY_CAST: {
1717 type_t *dest = skip_typeref(expression->base.type);
1718 if (!is_type_pointer(dest) && (
1719 dest->kind != TYPE_ATOMIC ||
1720 !(get_atomic_type_flags(dest->atomic.akind) & ATOMIC_TYPE_FLAG_INTEGER) ||
1721 get_atomic_type_size(dest->atomic.akind) < get_atomic_type_size(get_intptr_kind())
1725 return (is_constant_expression(expression->unary.value)
1726 || is_address_constant(expression->unary.value));
1729 case EXPR_BINARY_ADD:
1730 case EXPR_BINARY_SUB: {
1731 expression_t *left = expression->binary.left;
1732 expression_t *right = expression->binary.right;
1734 if (is_type_pointer(skip_typeref(left->base.type))) {
1735 return is_address_constant(left) && is_constant_expression(right);
1736 } else if (is_type_pointer(skip_typeref(right->base.type))) {
1737 return is_constant_expression(left) && is_address_constant(right);
1743 case EXPR_REFERENCE: {
1744 entity_t *entity = expression->reference.entity;
1745 if (!is_declaration(entity))
1748 type_t *type = skip_typeref(entity->declaration.type);
1749 if (is_type_function(type))
1751 if (is_type_array(type)) {
1752 return is_object_with_linker_constant_address(expression);
1754 /* Prevent stray errors */
1755 if (!is_type_valid(type))
1760 case EXPR_ARRAY_ACCESS: {
1761 type_t *const type =
1762 skip_typeref(revert_automatic_type_conversion(expression));
1764 is_type_array(type) &&
1765 is_constant_expression(expression->array_access.index) &&
1766 is_address_constant(expression->array_access.array_ref);
1774 static bool is_builtin_const_call(const expression_t *expression)
1776 expression_t *function = expression->call.function;
1777 if (function->kind != EXPR_BUILTIN_SYMBOL) {
1781 symbol_t *symbol = function->builtin_symbol.symbol;
1783 switch (symbol->ID) {
1784 case T___builtin_huge_val:
1785 case T___builtin_inf:
1786 case T___builtin_inff:
1787 case T___builtin_infl:
1788 case T___builtin_nan:
1789 case T___builtin_nanf:
1790 case T___builtin_nanl:
1797 static bool is_constant_pointer(const expression_t *expression)
1799 if (is_constant_expression(expression))
1802 switch (expression->kind) {
1803 case EXPR_UNARY_CAST:
1804 return is_constant_pointer(expression->unary.value);
1810 static bool is_object_with_constant_address(const expression_t *expression)
1812 switch (expression->kind) {
1814 expression_t *compound = expression->select.compound;
1815 type_t *compound_type = compound->base.type;
1816 compound_type = skip_typeref(compound_type);
1817 if (is_type_pointer(compound_type)) {
1818 return is_constant_pointer(compound);
1820 return is_object_with_constant_address(compound);
1824 case EXPR_ARRAY_ACCESS: {
1825 array_access_expression_t const* const array_access =
1826 &expression->array_access;
1828 is_constant_expression(array_access->index) && (
1829 is_object_with_constant_address(array_access->array_ref) ||
1830 is_constant_pointer(array_access->array_ref)
1834 case EXPR_UNARY_DEREFERENCE:
1835 return is_constant_pointer(expression->unary.value);
1841 bool is_constant_expression(const expression_t *expression)
1843 switch (expression->kind) {
1846 case EXPR_CHARACTER_CONSTANT:
1847 case EXPR_WIDE_CHARACTER_CONSTANT:
1848 case EXPR_STRING_LITERAL:
1849 case EXPR_WIDE_STRING_LITERAL:
1850 case EXPR_CLASSIFY_TYPE:
1854 case EXPR_BUILTIN_CONSTANT_P:
1855 case EXPR_LABEL_ADDRESS:
1856 case EXPR_REFERENCE_ENUM_VALUE:
1860 type_t *type = expression->typeprop.type;
1862 type = expression->typeprop.tp_expression->base.type;
1864 type = skip_typeref(type);
1865 if (is_type_array(type) && type->array.is_vla)
1870 case EXPR_BUILTIN_SYMBOL:
1871 case EXPR_BUILTIN_PREFETCH:
1875 case EXPR_STATEMENT:
1876 case EXPR_REFERENCE:
1877 case EXPR_UNARY_POSTFIX_INCREMENT:
1878 case EXPR_UNARY_POSTFIX_DECREMENT:
1879 case EXPR_UNARY_PREFIX_INCREMENT:
1880 case EXPR_UNARY_PREFIX_DECREMENT:
1881 case EXPR_UNARY_ASSUME: /* has VOID type */
1882 case EXPR_UNARY_DEREFERENCE:
1883 case EXPR_UNARY_DELETE:
1884 case EXPR_UNARY_DELETE_ARRAY:
1885 case EXPR_UNARY_THROW:
1886 case EXPR_BINARY_ASSIGN:
1887 case EXPR_BINARY_MUL_ASSIGN:
1888 case EXPR_BINARY_DIV_ASSIGN:
1889 case EXPR_BINARY_MOD_ASSIGN:
1890 case EXPR_BINARY_ADD_ASSIGN:
1891 case EXPR_BINARY_SUB_ASSIGN:
1892 case EXPR_BINARY_SHIFTLEFT_ASSIGN:
1893 case EXPR_BINARY_SHIFTRIGHT_ASSIGN:
1894 case EXPR_BINARY_BITWISE_AND_ASSIGN:
1895 case EXPR_BINARY_BITWISE_XOR_ASSIGN:
1896 case EXPR_BINARY_BITWISE_OR_ASSIGN:
1897 case EXPR_BINARY_COMMA:
1898 case EXPR_ARRAY_ACCESS:
1901 case EXPR_UNARY_TAKE_ADDRESS:
1902 return is_object_with_constant_address(expression->unary.value);
1905 return is_builtin_const_call(expression);
1907 case EXPR_UNARY_NEGATE:
1908 case EXPR_UNARY_PLUS:
1909 case EXPR_UNARY_BITWISE_NEGATE:
1910 case EXPR_UNARY_NOT:
1911 return is_constant_expression(expression->unary.value);
1913 case EXPR_UNARY_CAST:
1914 case EXPR_UNARY_CAST_IMPLICIT:
1915 return is_type_arithmetic(skip_typeref(expression->base.type))
1916 && is_constant_expression(expression->unary.value);
1918 case EXPR_BINARY_ADD:
1919 case EXPR_BINARY_SUB:
1920 case EXPR_BINARY_MUL:
1921 case EXPR_BINARY_DIV:
1922 case EXPR_BINARY_MOD:
1923 case EXPR_BINARY_EQUAL:
1924 case EXPR_BINARY_NOTEQUAL:
1925 case EXPR_BINARY_LESS:
1926 case EXPR_BINARY_LESSEQUAL:
1927 case EXPR_BINARY_GREATER:
1928 case EXPR_BINARY_GREATEREQUAL:
1929 case EXPR_BINARY_BITWISE_AND:
1930 case EXPR_BINARY_BITWISE_OR:
1931 case EXPR_BINARY_BITWISE_XOR:
1932 case EXPR_BINARY_SHIFTLEFT:
1933 case EXPR_BINARY_SHIFTRIGHT:
1934 case EXPR_BINARY_ISGREATER:
1935 case EXPR_BINARY_ISGREATEREQUAL:
1936 case EXPR_BINARY_ISLESS:
1937 case EXPR_BINARY_ISLESSEQUAL:
1938 case EXPR_BINARY_ISLESSGREATER:
1939 case EXPR_BINARY_ISUNORDERED:
1940 return is_constant_expression(expression->binary.left)
1941 && is_constant_expression(expression->binary.right);
1943 case EXPR_BINARY_LOGICAL_AND: {
1944 expression_t const *const left = expression->binary.left;
1945 if (!is_constant_expression(left))
1947 if (fold_constant(left) == 0)
1949 return is_constant_expression(expression->binary.right);
1952 case EXPR_BINARY_LOGICAL_OR: {
1953 expression_t const *const left = expression->binary.left;
1954 if (!is_constant_expression(left))
1956 if (fold_constant(left) != 0)
1958 return is_constant_expression(expression->binary.right);
1961 case EXPR_COMPOUND_LITERAL:
1962 return is_constant_initializer(expression->compound_literal.initializer);
1964 case EXPR_CONDITIONAL: {
1965 expression_t *condition = expression->conditional.condition;
1966 if (!is_constant_expression(condition))
1969 long val = fold_constant(condition);
1971 return is_constant_expression(expression->conditional.true_expression);
1973 return is_constant_expression(expression->conditional.false_expression);
1982 panic("invalid expression found (is constant expression)");
1986 * Initialize the AST construction.
1990 obstack_init(&ast_obstack);
1998 obstack_free(&ast_obstack, NULL);
2002 * Set the output stream for the AST printer.
2004 * @param stream the output stream
2006 void ast_set_output(FILE *stream)
2009 type_set_output(stream);
2013 * Allocate an AST object of the given size.
2015 * @param size the size of the object to allocate
2017 * @return A new allocated object in the AST memeory space.
2019 void *(allocate_ast)(size_t size)
2021 return _allocate_ast(size);