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 #ifdef __INTEL_COMPILER
40 #include "adt/error.h"
42 struct obstack ast_obstack;
47 /** If set, implicit casts are printed. */
48 bool print_implicit_casts = false;
50 /** If set parenthesis are printed to indicate operator precedence. */
51 bool print_parenthesis = false;
53 static void print_statement(const statement_t *statement);
54 static void print_expression_prec(const expression_t *expression, unsigned prec);
56 void change_indent(int delta)
62 void print_indent(void)
64 for (int i = 0; i < indent; ++i)
69 * Returns 1 if a given precedence level has right-to-left
70 * associativity, else 0.
72 * @param precedence the operator precedence
74 static int right_to_left(unsigned precedence)
77 case 2*PREC_ASSIGNMENT:
78 case 2*PREC_CONDITIONAL:
88 * Return the precedence of an expression given by its kind.
90 * @param kind the expression kind
92 static unsigned get_expression_precedence(expression_kind_t kind)
94 static const unsigned prec[] = {
95 [EXPR_UNKNOWN] = PREC_PRIMARY,
96 [EXPR_INVALID] = PREC_PRIMARY,
97 [EXPR_REFERENCE] = PREC_PRIMARY,
98 [EXPR_CHARACTER_CONSTANT] = PREC_PRIMARY,
99 [EXPR_WIDE_CHARACTER_CONSTANT] = PREC_PRIMARY,
100 [EXPR_CONST] = PREC_PRIMARY,
101 [EXPR_STRING_LITERAL] = PREC_PRIMARY,
102 [EXPR_WIDE_STRING_LITERAL] = PREC_PRIMARY,
103 [EXPR_COMPOUND_LITERAL] = PREC_UNARY,
104 [EXPR_CALL] = PREC_POSTFIX,
105 [EXPR_CONDITIONAL] = PREC_CONDITIONAL,
106 [EXPR_SELECT] = PREC_POSTFIX,
107 [EXPR_ARRAY_ACCESS] = PREC_POSTFIX,
108 [EXPR_SIZEOF] = PREC_UNARY,
109 [EXPR_CLASSIFY_TYPE] = PREC_UNARY,
110 [EXPR_ALIGNOF] = PREC_UNARY,
112 [EXPR_FUNCNAME] = PREC_PRIMARY,
113 [EXPR_BUILTIN_SYMBOL] = PREC_PRIMARY,
114 [EXPR_BUILTIN_CONSTANT_P] = PREC_PRIMARY,
115 [EXPR_BUILTIN_PREFETCH] = PREC_PRIMARY,
116 [EXPR_OFFSETOF] = PREC_PRIMARY,
117 [EXPR_VA_START] = PREC_PRIMARY,
118 [EXPR_VA_ARG] = PREC_PRIMARY,
119 [EXPR_STATEMENT] = PREC_PRIMARY,
120 [EXPR_LABEL_ADDRESS] = PREC_PRIMARY,
122 [EXPR_UNARY_NEGATE] = PREC_UNARY,
123 [EXPR_UNARY_PLUS] = PREC_UNARY,
124 [EXPR_UNARY_BITWISE_NEGATE] = PREC_UNARY,
125 [EXPR_UNARY_NOT] = PREC_UNARY,
126 [EXPR_UNARY_DEREFERENCE] = PREC_UNARY,
127 [EXPR_UNARY_TAKE_ADDRESS] = PREC_UNARY,
128 [EXPR_UNARY_POSTFIX_INCREMENT] = PREC_UNARY,
129 [EXPR_UNARY_POSTFIX_DECREMENT] = PREC_UNARY,
130 [EXPR_UNARY_PREFIX_INCREMENT] = PREC_UNARY,
131 [EXPR_UNARY_PREFIX_DECREMENT] = PREC_UNARY,
132 [EXPR_UNARY_CAST] = PREC_UNARY,
133 [EXPR_UNARY_CAST_IMPLICIT] = PREC_UNARY,
134 [EXPR_UNARY_ASSUME] = PREC_PRIMARY,
135 [EXPR_UNARY_DELETE] = PREC_UNARY,
136 [EXPR_UNARY_DELETE_ARRAY] = PREC_UNARY,
137 [EXPR_UNARY_THROW] = PREC_ASSIGNMENT,
139 [EXPR_BINARY_ADD] = PREC_ADDITIVE,
140 [EXPR_BINARY_SUB] = PREC_ADDITIVE,
141 [EXPR_BINARY_MUL] = PREC_MULTIPLICATIVE,
142 [EXPR_BINARY_DIV] = PREC_MULTIPLICATIVE,
143 [EXPR_BINARY_MOD] = PREC_MULTIPLICATIVE,
144 [EXPR_BINARY_EQUAL] = PREC_EQUALITY,
145 [EXPR_BINARY_NOTEQUAL] = PREC_EQUALITY,
146 [EXPR_BINARY_LESS] = PREC_RELATIONAL,
147 [EXPR_BINARY_LESSEQUAL] = PREC_RELATIONAL,
148 [EXPR_BINARY_GREATER] = PREC_RELATIONAL,
149 [EXPR_BINARY_GREATEREQUAL] = PREC_RELATIONAL,
150 [EXPR_BINARY_BITWISE_AND] = PREC_AND,
151 [EXPR_BINARY_BITWISE_OR] = PREC_OR,
152 [EXPR_BINARY_BITWISE_XOR] = PREC_XOR,
153 [EXPR_BINARY_LOGICAL_AND] = PREC_LOGICAL_AND,
154 [EXPR_BINARY_LOGICAL_OR] = PREC_LOGICAL_OR,
155 [EXPR_BINARY_SHIFTLEFT] = PREC_SHIFT,
156 [EXPR_BINARY_SHIFTRIGHT] = PREC_SHIFT,
157 [EXPR_BINARY_ASSIGN] = PREC_ASSIGNMENT,
158 [EXPR_BINARY_MUL_ASSIGN] = PREC_ASSIGNMENT,
159 [EXPR_BINARY_DIV_ASSIGN] = PREC_ASSIGNMENT,
160 [EXPR_BINARY_MOD_ASSIGN] = PREC_ASSIGNMENT,
161 [EXPR_BINARY_ADD_ASSIGN] = PREC_ASSIGNMENT,
162 [EXPR_BINARY_SUB_ASSIGN] = PREC_ASSIGNMENT,
163 [EXPR_BINARY_SHIFTLEFT_ASSIGN] = PREC_ASSIGNMENT,
164 [EXPR_BINARY_SHIFTRIGHT_ASSIGN] = PREC_ASSIGNMENT,
165 [EXPR_BINARY_BITWISE_AND_ASSIGN] = PREC_ASSIGNMENT,
166 [EXPR_BINARY_BITWISE_XOR_ASSIGN] = PREC_ASSIGNMENT,
167 [EXPR_BINARY_BITWISE_OR_ASSIGN] = PREC_ASSIGNMENT,
168 [EXPR_BINARY_COMMA] = PREC_EXPRESSION,
170 [EXPR_BINARY_BUILTIN_EXPECT] = PREC_PRIMARY,
171 [EXPR_BINARY_ISGREATER] = PREC_PRIMARY,
172 [EXPR_BINARY_ISGREATEREQUAL] = PREC_PRIMARY,
173 [EXPR_BINARY_ISLESS] = PREC_PRIMARY,
174 [EXPR_BINARY_ISLESSEQUAL] = PREC_PRIMARY,
175 [EXPR_BINARY_ISLESSGREATER] = PREC_PRIMARY,
176 [EXPR_BINARY_ISUNORDERED] = PREC_PRIMARY
178 assert((unsigned)kind < (sizeof(prec)/sizeof(prec[0])));
179 unsigned res = prec[kind];
181 assert(res != PREC_BOTTOM);
182 /* we need the lowest bit for right-to-left precedence */
187 * Print a constant expression.
189 * @param cnst the constant expression
191 static void print_const(const const_expression_t *cnst)
193 if(cnst->base.type == NULL)
196 const type_t *const type = skip_typeref(cnst->base.type);
198 if (is_type_integer(type)) {
199 fprintf(out, "%lld", cnst->v.int_value);
200 } else if (is_type_float(type)) {
201 long double const val = cnst->v.float_value;
203 /* ARG, no way to print long double */
204 fprintf(out, "%.20g", (double)val);
206 fprintf(out, "%.20Lg", val);
208 if (isfinite(val) && truncl(val) == val)
211 panic("unknown constant");
215 switch (type->atomic.akind) {
216 case ATOMIC_TYPE_UINT: suffix = "U"; break;
217 case ATOMIC_TYPE_LONG: suffix = "L"; break;
218 case ATOMIC_TYPE_ULONG: suffix = "UL"; break;
219 case ATOMIC_TYPE_LONGLONG: suffix = "LL"; break;
220 case ATOMIC_TYPE_ULONGLONG: suffix = "ULL"; break;
221 case ATOMIC_TYPE_FLOAT: suffix = "F"; break;
222 case ATOMIC_TYPE_LONG_DOUBLE: suffix = "L"; break;
224 default: suffix = NULL; break;
231 * Print a quoted string constant.
233 * @param string the string constant
234 * @param border the border char
235 * @param skip number of chars to skip at the end
237 static void print_quoted_string(const string_t *const string, char border, int skip)
240 const char *end = string->begin + string->size - skip;
241 for (const char *c = string->begin; c != end; ++c) {
246 case '\\': fputs("\\\\", out); break;
247 case '\a': fputs("\\a", out); break;
248 case '\b': fputs("\\b", out); break;
249 case '\f': fputs("\\f", out); break;
250 case '\n': fputs("\\n", out); break;
251 case '\r': fputs("\\r", out); break;
252 case '\t': fputs("\\t", out); break;
253 case '\v': fputs("\\v", out); break;
254 case '\?': fputs("\\?", out); break;
256 if (c_mode & _GNUC) {
257 fputs("\\e", out); break;
262 fprintf(out, "\\%03o", (unsigned)*c);
273 * Prints a wide string literal expression.
275 * @param wstr the wide string literal expression
276 * @param border the border char
277 * @param skip number of chars to skip at the end
279 static void print_quoted_wide_string(const wide_string_t *const wstr,
280 char border, int skip)
284 const wchar_rep_t *end = wstr->begin + wstr->size - skip;
285 for (const wchar_rep_t *c = wstr->begin; c != end; ++c) {
287 case L'\"': fputs("\\\"", out); break;
288 case L'\\': fputs("\\\\", out); break;
289 case L'\a': fputs("\\a", out); break;
290 case L'\b': fputs("\\b", out); break;
291 case L'\f': fputs("\\f", out); break;
292 case L'\n': fputs("\\n", out); break;
293 case L'\r': fputs("\\r", out); break;
294 case L'\t': fputs("\\t", out); break;
295 case L'\v': fputs("\\v", out); break;
296 case L'\?': fputs("\\?", out); break;
298 if (c_mode & _GNUC) {
299 fputs("\\e", out); break;
303 const unsigned tc = *c;
306 fprintf(out, "\\%03o", (char)*c);
310 } else if (tc < 0x800) {
311 fputc(0xC0 | (tc >> 6), out);
312 fputc(0x80 | (tc & 0x3F), out);
313 } else if (tc < 0x10000) {
314 fputc(0xE0 | ( tc >> 12), out);
315 fputc(0x80 | ((tc >> 6) & 0x3F), out);
316 fputc(0x80 | ( tc & 0x3F), out);
318 fputc(0xF0 | ( tc >> 18), out);
319 fputc(0x80 | ((tc >> 12) & 0x3F), out);
320 fputc(0x80 | ((tc >> 6) & 0x3F), out);
321 fputc(0x80 | ( tc & 0x3F), out);
330 * Print a constant character expression.
332 * @param cnst the constant character expression
334 static void print_character_constant(const const_expression_t *cnst)
336 print_quoted_string(&cnst->v.character, '\'', 0);
339 static void print_wide_character_constant(const const_expression_t *cnst)
341 print_quoted_wide_string(&cnst->v.wide_character, '\'', 0);
345 * Prints a string literal expression.
347 * @param string_literal the string literal expression
349 static void print_string_literal(
350 const string_literal_expression_t *string_literal)
352 print_quoted_string(&string_literal->value, '"', 1);
356 * Prints a predefined symbol.
358 static void print_funcname(
359 const funcname_expression_t *funcname)
362 switch (funcname->kind) {
363 case FUNCNAME_FUNCTION: s = (c_mode & _C99) ? "__func__" : "__FUNCTION__"; break;
364 case FUNCNAME_PRETTY_FUNCTION: s = "__PRETTY_FUNCTION__"; break;
365 case FUNCNAME_FUNCSIG: s = "__FUNCSIG__"; break;
366 case FUNCNAME_FUNCDNAME: s = "__FUNCDNAME__"; break;
373 static void print_wide_string_literal(
374 const wide_string_literal_expression_t *const wstr)
376 print_quoted_wide_string(&wstr->value, '"', 1);
379 static void print_compound_literal(
380 const compound_literal_expression_t *expression)
383 print_type(expression->type);
385 print_initializer(expression->initializer);
389 * Prints a call expression.
391 * @param call the call expression
393 static void print_call_expression(const call_expression_t *call)
395 unsigned prec = get_expression_precedence(call->base.kind);
396 print_expression_prec(call->function, prec);
398 call_argument_t *argument = call->arguments;
400 while(argument != NULL) {
406 print_expression_prec(argument->expression, 2 * PREC_ASSIGNMENT);
408 argument = argument->next;
414 * Prints a binary expression.
416 * @param binexpr the binary expression
418 static void print_binary_expression(const binary_expression_t *binexpr)
420 unsigned prec = get_expression_precedence(binexpr->base.kind);
421 int r2l = right_to_left(prec);
423 if(binexpr->base.kind == EXPR_BINARY_BUILTIN_EXPECT) {
424 fputs("__builtin_expect(", out);
425 print_expression_prec(binexpr->left, prec);
427 print_expression_prec(binexpr->right, prec);
432 print_expression_prec(binexpr->left, prec + r2l);
434 switch (binexpr->base.kind) {
435 case EXPR_BINARY_COMMA: op = ", "; break;
436 case EXPR_BINARY_ASSIGN: op = " = "; break;
437 case EXPR_BINARY_ADD: op = " + "; break;
438 case EXPR_BINARY_SUB: op = " - "; break;
439 case EXPR_BINARY_MUL: op = " * "; break;
440 case EXPR_BINARY_MOD: op = " % "; break;
441 case EXPR_BINARY_DIV: op = " / "; break;
442 case EXPR_BINARY_BITWISE_OR: op = " | "; break;
443 case EXPR_BINARY_BITWISE_AND: op = " & "; break;
444 case EXPR_BINARY_BITWISE_XOR: op = " ^ "; break;
445 case EXPR_BINARY_LOGICAL_OR: op = " || "; break;
446 case EXPR_BINARY_LOGICAL_AND: op = " && "; break;
447 case EXPR_BINARY_NOTEQUAL: op = " != "; break;
448 case EXPR_BINARY_EQUAL: op = " == "; break;
449 case EXPR_BINARY_LESS: op = " < "; break;
450 case EXPR_BINARY_LESSEQUAL: op = " <= "; break;
451 case EXPR_BINARY_GREATER: op = " > "; break;
452 case EXPR_BINARY_GREATEREQUAL: op = " >= "; break;
453 case EXPR_BINARY_SHIFTLEFT: op = " << "; break;
454 case EXPR_BINARY_SHIFTRIGHT: op = " >> "; break;
456 case EXPR_BINARY_ADD_ASSIGN: op = " += "; break;
457 case EXPR_BINARY_SUB_ASSIGN: op = " -= "; break;
458 case EXPR_BINARY_MUL_ASSIGN: op = " *= "; break;
459 case EXPR_BINARY_MOD_ASSIGN: op = " %= "; break;
460 case EXPR_BINARY_DIV_ASSIGN: op = " /= "; break;
461 case EXPR_BINARY_BITWISE_OR_ASSIGN: op = " |= "; break;
462 case EXPR_BINARY_BITWISE_AND_ASSIGN: op = " &= "; break;
463 case EXPR_BINARY_BITWISE_XOR_ASSIGN: op = " ^= "; break;
464 case EXPR_BINARY_SHIFTLEFT_ASSIGN: op = " <<= "; break;
465 case EXPR_BINARY_SHIFTRIGHT_ASSIGN: op = " >>= "; break;
466 default: panic("invalid binexpression found");
469 print_expression_prec(binexpr->right, prec - r2l);
473 * Prints an unary expression.
475 * @param unexpr the unary expression
477 static void print_unary_expression(const unary_expression_t *unexpr)
479 unsigned prec = get_expression_precedence(unexpr->base.kind);
480 switch (unexpr->base.kind) {
481 case EXPR_UNARY_NEGATE: fputc('-', out); break;
482 case EXPR_UNARY_PLUS: fputc('+', out); break;
483 case EXPR_UNARY_NOT: fputc('!', out); break;
484 case EXPR_UNARY_BITWISE_NEGATE: fputc('~', out); break;
485 case EXPR_UNARY_PREFIX_INCREMENT: fputs("++", out); break;
486 case EXPR_UNARY_PREFIX_DECREMENT: fputs("--", out); break;
487 case EXPR_UNARY_DEREFERENCE: fputc('*', out); break;
488 case EXPR_UNARY_TAKE_ADDRESS: fputc('&', out); break;
489 case EXPR_UNARY_DELETE: fputs("delete ", out); break;
490 case EXPR_UNARY_DELETE_ARRAY: fputs("delete [] ", out); break;
492 case EXPR_UNARY_POSTFIX_INCREMENT:
493 print_expression_prec(unexpr->value, prec);
496 case EXPR_UNARY_POSTFIX_DECREMENT:
497 print_expression_prec(unexpr->value, prec);
500 case EXPR_UNARY_CAST_IMPLICIT:
501 case EXPR_UNARY_CAST:
503 print_type(unexpr->base.type);
506 case EXPR_UNARY_ASSUME:
507 fputs("__assume(", out);
508 print_expression_prec(unexpr->value, 2 * PREC_ASSIGNMENT);
512 case EXPR_UNARY_THROW:
513 if (unexpr->value == NULL) {
517 fputs("throw ", out);
521 panic("invalid unary expression found");
523 print_expression_prec(unexpr->value, prec);
527 * Prints a reference expression.
529 * @param ref the reference expression
531 static void print_reference_expression(const reference_expression_t *ref)
533 fputs(ref->entity->base.symbol->string, out);
537 * Prints a label address expression.
539 * @param ref the reference expression
541 static void print_label_address_expression(const label_address_expression_t *le)
543 fprintf(out, "&&%s", le->label->base.symbol->string);
547 * Prints an array expression.
549 * @param expression the array expression
551 static void print_array_expression(const array_access_expression_t *expression)
553 unsigned prec = get_expression_precedence(expression->base.kind);
554 if(!expression->flipped) {
555 print_expression_prec(expression->array_ref, prec);
557 print_expression(expression->index);
560 print_expression_prec(expression->index, prec);
562 print_expression(expression->array_ref);
568 * Prints a typeproperty expression (sizeof or __alignof__).
570 * @param expression the type property expression
572 static void print_typeprop_expression(const typeprop_expression_t *expression)
574 if (expression->base.kind == EXPR_SIZEOF) {
575 fputs("sizeof", out);
577 assert(expression->base.kind == EXPR_ALIGNOF);
578 fputs("__alignof__", out);
580 if(expression->tp_expression != NULL) {
581 /* always print the '()' here, sizeof x is right but unusual */
583 print_expression(expression->tp_expression);
587 print_type(expression->type);
593 * Prints an builtin symbol.
595 * @param expression the builtin symbol expression
597 static void print_builtin_symbol(const builtin_symbol_expression_t *expression)
599 fputs(expression->symbol->string, out);
603 * Prints a builtin constant expression.
605 * @param expression the builtin constant expression
607 static void print_builtin_constant(const builtin_constant_expression_t *expression)
609 fputs("__builtin_constant_p(", out);
610 print_expression_prec(expression->value, 2 * PREC_ASSIGNMENT);
615 * Prints a builtin prefetch expression.
617 * @param expression the builtin prefetch expression
619 static void print_builtin_prefetch(const builtin_prefetch_expression_t *expression)
621 fputs("__builtin_prefetch(", out);
622 print_expression_prec(expression->adr, 2 * PREC_ASSIGNMENT);
623 if (expression->rw) {
625 print_expression_prec(expression->rw, 2 * PREC_ASSIGNMENT);
627 if (expression->locality) {
629 print_expression_prec(expression->locality, 2 * PREC_ASSIGNMENT);
635 * Prints a conditional expression.
637 * @param expression the conditional expression
639 static void print_conditional(const conditional_expression_t *expression)
641 print_expression_prec(expression->condition, 2 * PREC_LOGICAL_OR);
643 if (expression->true_expression != NULL) {
644 print_expression_prec(expression->true_expression, 2 * PREC_EXPRESSION);
649 print_expression_prec(expression->false_expression, 2 * PREC_CONDITIONAL);
653 * Prints a va_start expression.
655 * @param expression the va_start expression
657 static void print_va_start(const va_start_expression_t *const expression)
659 fputs("__builtin_va_start(", out);
660 print_expression_prec(expression->ap, 2 * PREC_ASSIGNMENT);
662 fputs(expression->parameter->base.base.symbol->string, out);
667 * Prints a va_arg expression.
669 * @param expression the va_arg expression
671 static void print_va_arg(const va_arg_expression_t *expression)
673 fputs("__builtin_va_arg(", out);
674 print_expression_prec(expression->ap, 2 * PREC_ASSIGNMENT);
676 print_type(expression->base.type);
681 * Prints a select expression (. or ->).
683 * @param expression the select expression
685 static void print_select(const select_expression_t *expression)
687 unsigned prec = get_expression_precedence(expression->base.kind);
688 print_expression_prec(expression->compound, prec);
689 if(is_type_pointer(skip_typeref(expression->compound->base.type))) {
694 fputs(expression->compound_entry->base.symbol->string, out);
698 * Prints a type classify expression.
700 * @param expr the type classify expression
702 static void print_classify_type_expression(
703 const classify_type_expression_t *const expr)
705 fputs("__builtin_classify_type(", out);
706 print_expression_prec(expr->type_expression, 2 * PREC_ASSIGNMENT);
711 * Prints a designator.
713 * @param designator the designator
715 static void print_designator(const designator_t *designator)
717 for ( ; designator != NULL; designator = designator->next) {
718 if (designator->symbol == NULL) {
720 print_expression(designator->array_index);
724 fputs(designator->symbol->string, out);
730 * Prints an offsetof expression.
732 * @param expression the offset expression
734 static void print_offsetof_expression(const offsetof_expression_t *expression)
736 fputs("__builtin_offsetof", out);
738 print_type(expression->type);
740 print_designator(expression->designator);
745 * Prints a statement expression.
747 * @param expression the statement expression
749 static void print_statement_expression(const statement_expression_t *expression)
752 print_statement(expression->statement);
757 * Prints an expression with parenthesis if needed.
759 * @param expression the expression to print
760 * @param top_prec the precedence of the user of this expression.
762 static void print_expression_prec(const expression_t *expression, unsigned top_prec)
764 if (expression->kind == EXPR_UNARY_CAST_IMPLICIT && !print_implicit_casts) {
765 expression = expression->unary.value;
767 unsigned prec = get_expression_precedence(expression->base.kind);
768 if (print_parenthesis && top_prec != PREC_BOTTOM)
772 switch (expression->kind) {
775 fputs("$invalid expression$", out);
777 case EXPR_CHARACTER_CONSTANT:
778 print_character_constant(&expression->conste);
780 case EXPR_WIDE_CHARACTER_CONSTANT:
781 print_wide_character_constant(&expression->conste);
784 print_const(&expression->conste);
787 print_funcname(&expression->funcname);
789 case EXPR_STRING_LITERAL:
790 print_string_literal(&expression->string);
792 case EXPR_WIDE_STRING_LITERAL:
793 print_wide_string_literal(&expression->wide_string);
795 case EXPR_COMPOUND_LITERAL:
796 print_compound_literal(&expression->compound_literal);
799 print_call_expression(&expression->call);
802 print_binary_expression(&expression->binary);
805 print_reference_expression(&expression->reference);
807 case EXPR_ARRAY_ACCESS:
808 print_array_expression(&expression->array_access);
810 case EXPR_LABEL_ADDRESS:
811 print_label_address_expression(&expression->label_address);
814 print_unary_expression(&expression->unary);
818 print_typeprop_expression(&expression->typeprop);
820 case EXPR_BUILTIN_SYMBOL:
821 print_builtin_symbol(&expression->builtin_symbol);
823 case EXPR_BUILTIN_CONSTANT_P:
824 print_builtin_constant(&expression->builtin_constant);
826 case EXPR_BUILTIN_PREFETCH:
827 print_builtin_prefetch(&expression->builtin_prefetch);
829 case EXPR_CONDITIONAL:
830 print_conditional(&expression->conditional);
833 print_va_start(&expression->va_starte);
836 print_va_arg(&expression->va_arge);
839 print_select(&expression->select);
841 case EXPR_CLASSIFY_TYPE:
842 print_classify_type_expression(&expression->classify_type);
845 print_offsetof_expression(&expression->offsetofe);
848 print_statement_expression(&expression->statement);
853 fprintf(out, "some expression of type %d", (int) expression->kind);
861 * Print an compound statement.
863 * @param block the compound statement
865 static void print_compound_statement(const compound_statement_t *block)
870 statement_t *statement = block->statements;
871 while (statement != NULL) {
872 if (statement->base.kind == STATEMENT_CASE_LABEL)
874 if (statement->kind != STATEMENT_LABEL)
876 print_statement(statement);
878 statement = statement->base.next;
886 * Print a return statement.
888 * @param statement the return statement
890 static void print_return_statement(const return_statement_t *statement)
892 fputs("return ", out);
893 if(statement->value != NULL)
894 print_expression(statement->value);
899 * Print an expression statement.
901 * @param statement the expression statement
903 static void print_expression_statement(const expression_statement_t *statement)
905 print_expression(statement->expression);
910 * Print a goto statement.
912 * @param statement the goto statement
914 static void print_goto_statement(const goto_statement_t *statement)
917 if (statement->expression != NULL) {
919 print_expression(statement->expression);
921 fputs(statement->label->base.symbol->string, out);
927 * Print a label statement.
929 * @param statement the label statement
931 static void print_label_statement(const label_statement_t *statement)
933 fprintf(out, "%s:\n", statement->label->base.symbol->string);
935 print_statement(statement->statement);
939 * Print an if statement.
941 * @param statement the if statement
943 static void print_if_statement(const if_statement_t *statement)
946 print_expression(statement->condition);
948 print_statement(statement->true_statement);
950 if(statement->false_statement != NULL) {
953 print_statement(statement->false_statement);
958 * Print a switch statement.
960 * @param statement the switch statement
962 static void print_switch_statement(const switch_statement_t *statement)
964 fputs("switch (", out);
965 print_expression(statement->expression);
967 print_statement(statement->body);
971 * Print a case label (including the default label).
973 * @param statement the case label statement
975 static void print_case_label(const case_label_statement_t *statement)
977 if(statement->expression == NULL) {
978 fputs("default:\n", out);
981 print_expression(statement->expression);
982 if (statement->end_range != NULL) {
984 print_expression(statement->end_range);
989 if(statement->statement != NULL) {
990 if (statement->statement->base.kind == STATEMENT_CASE_LABEL) {
994 print_statement(statement->statement);
998 static void print_local_label(const local_label_statement_t *statement)
1000 fputs("__label__ ", out);
1003 entity_t *entity = statement->labels_begin;
1005 entity != statement->labels_end->base.next;
1006 entity = entity->base.next) {
1012 fputs(entity->base.symbol->string, out);
1017 static void print_typedef(const entity_t *entity)
1019 fputs("typedef ", out);
1020 print_type_ext(entity->typedefe.type, entity->base.symbol, NULL);
1025 * returns true if the entity is a compiler generated one and has no real
1026 * correspondenc in the source file
1028 static bool is_generated_entity(const entity_t *entity)
1030 if (entity->kind == ENTITY_TYPEDEF)
1031 return entity->typedefe.builtin;
1033 if (is_declaration(entity))
1034 return entity->declaration.implicit;
1040 * Print a declaration statement.
1042 * @param statement the statement
1044 static void print_declaration_statement(
1045 const declaration_statement_t *statement)
1048 entity_t *entity = statement->declarations_begin;
1050 entity != statement->declarations_end->base.next;
1051 entity = entity->base.next) {
1052 if (!is_declaration(entity) && entity->kind != ENTITY_TYPEDEF)
1054 if (is_generated_entity(entity))
1063 if (entity->kind == ENTITY_TYPEDEF) {
1064 print_typedef(entity);
1066 assert(is_declaration(entity));
1067 print_declaration(entity);
1075 * Print a while statement.
1077 * @param statement the statement
1079 static void print_while_statement(const while_statement_t *statement)
1081 fputs("while (", out);
1082 print_expression(statement->condition);
1084 print_statement(statement->body);
1088 * Print a do-while statement.
1090 * @param statement the statement
1092 static void print_do_while_statement(const do_while_statement_t *statement)
1095 print_statement(statement->body);
1097 fputs("while (", out);
1098 print_expression(statement->condition);
1103 * Print a for statement.
1105 * @param statement the statement
1107 static void print_for_statement(const for_statement_t *statement)
1109 fputs("for (", out);
1110 entity_t *entity = statement->scope.entities;
1111 while (entity != NULL && is_generated_entity(entity))
1112 entity = entity->base.next;
1114 if (entity != NULL) {
1115 assert(statement->initialisation == NULL);
1116 assert(is_declaration(entity));
1117 print_declaration(entity);
1118 if (entity->base.next != NULL) {
1119 panic("multiple declarations in for statement not supported yet");
1123 if(statement->initialisation) {
1124 print_expression(statement->initialisation);
1128 if(statement->condition != NULL) {
1129 print_expression(statement->condition);
1132 if(statement->step != NULL) {
1133 print_expression(statement->step);
1136 print_statement(statement->body);
1140 * Print assembler arguments.
1142 * @param arguments the arguments
1144 static void print_asm_arguments(asm_argument_t *arguments)
1146 asm_argument_t *argument = arguments;
1147 for( ; argument != NULL; argument = argument->next) {
1148 if(argument != arguments)
1151 if(argument->symbol) {
1152 fprintf(out, "[%s] ", argument->symbol->string);
1154 print_quoted_string(&argument->constraints, '"', 1);
1156 print_expression(argument->expression);
1162 * Print assembler clobbers.
1164 * @param clobbers the clobbers
1166 static void print_asm_clobbers(asm_clobber_t *clobbers)
1168 asm_clobber_t *clobber = clobbers;
1169 for( ; clobber != NULL; clobber = clobber->next) {
1170 if(clobber != clobbers)
1173 print_quoted_string(&clobber->clobber, '"', 1);
1178 * Print an assembler statement.
1180 * @param statement the statement
1182 static void print_asm_statement(const asm_statement_t *statement)
1185 if(statement->is_volatile) {
1186 fputs("volatile ", out);
1189 print_quoted_string(&statement->asm_text, '"', 1);
1190 if (statement->outputs == NULL &&
1191 statement->inputs == NULL &&
1192 statement->clobbers == NULL)
1193 goto end_of_print_asm_statement;
1196 print_asm_arguments(statement->outputs);
1197 if (statement->inputs == NULL && statement->clobbers == NULL)
1198 goto end_of_print_asm_statement;
1201 print_asm_arguments(statement->inputs);
1202 if (statement->clobbers == NULL)
1203 goto end_of_print_asm_statement;
1206 print_asm_clobbers(statement->clobbers);
1208 end_of_print_asm_statement:
1213 * Print a microsoft __try statement.
1215 * @param statement the statement
1217 static void print_ms_try_statement(const ms_try_statement_t *statement)
1219 fputs("__try ", out);
1220 print_statement(statement->try_statement);
1222 if(statement->except_expression != NULL) {
1223 fputs("__except(", out);
1224 print_expression(statement->except_expression);
1227 fputs("__finally ", out);
1229 print_statement(statement->final_statement);
1233 * Print a microsoft __leave statement.
1235 * @param statement the statement
1237 static void print_leave_statement(const leave_statement_t *statement)
1240 fputs("__leave;\n", out);
1244 * Print a statement.
1246 * @param statement the statement
1248 void print_statement(const statement_t *statement)
1250 switch (statement->kind) {
1251 case STATEMENT_EMPTY:
1254 case STATEMENT_COMPOUND:
1255 print_compound_statement(&statement->compound);
1257 case STATEMENT_RETURN:
1258 print_return_statement(&statement->returns);
1260 case STATEMENT_EXPRESSION:
1261 print_expression_statement(&statement->expression);
1263 case STATEMENT_LABEL:
1264 print_label_statement(&statement->label);
1266 case STATEMENT_LOCAL_LABEL:
1267 print_local_label(&statement->local_label);
1269 case STATEMENT_GOTO:
1270 print_goto_statement(&statement->gotos);
1272 case STATEMENT_CONTINUE:
1273 fputs("continue;\n", out);
1275 case STATEMENT_BREAK:
1276 fputs("break;\n", out);
1279 print_if_statement(&statement->ifs);
1281 case STATEMENT_SWITCH:
1282 print_switch_statement(&statement->switchs);
1284 case STATEMENT_CASE_LABEL:
1285 print_case_label(&statement->case_label);
1287 case STATEMENT_DECLARATION:
1288 print_declaration_statement(&statement->declaration);
1290 case STATEMENT_WHILE:
1291 print_while_statement(&statement->whiles);
1293 case STATEMENT_DO_WHILE:
1294 print_do_while_statement(&statement->do_while);
1297 print_for_statement(&statement->fors);
1300 print_asm_statement(&statement->asms);
1302 case STATEMENT_MS_TRY:
1303 print_ms_try_statement(&statement->ms_try);
1305 case STATEMENT_LEAVE:
1306 print_leave_statement(&statement->leave);
1308 case STATEMENT_INVALID:
1309 fputs("$invalid statement$\n", out);
1315 * Print a storage class.
1317 * @param storage_class the storage class
1319 static void print_storage_class(storage_class_tag_t storage_class)
1321 switch (storage_class) {
1322 case STORAGE_CLASS_NONE:
1324 case STORAGE_CLASS_TYPEDEF: fputs("typedef ", out); break;
1325 case STORAGE_CLASS_EXTERN: fputs("extern ", out); break;
1326 case STORAGE_CLASS_STATIC: fputs("static ", out); break;
1327 case STORAGE_CLASS_AUTO: fputs("auto ", out); break;
1328 case STORAGE_CLASS_REGISTER: fputs("register ", out); break;
1329 case STORAGE_CLASS_THREAD: fputs("__thread", out); break;
1330 case STORAGE_CLASS_THREAD_EXTERN: fputs("extern __thread", out); break;
1331 case STORAGE_CLASS_THREAD_STATIC: fputs("static __thread", out); break;
1336 * Print an initializer.
1338 * @param initializer the initializer
1340 void print_initializer(const initializer_t *initializer)
1342 if(initializer == NULL) {
1347 switch (initializer->kind) {
1348 case INITIALIZER_VALUE: {
1349 const initializer_value_t *value = &initializer->value;
1350 print_expression(value->value);
1353 case INITIALIZER_LIST: {
1354 assert(initializer->kind == INITIALIZER_LIST);
1356 const initializer_list_t *list = &initializer->list;
1358 for(size_t i = 0 ; i < list->len; ++i) {
1359 const initializer_t *sub_init = list->initializers[i];
1360 print_initializer(list->initializers[i]);
1361 if(i < list->len-1) {
1362 if(sub_init == NULL || sub_init->kind != INITIALIZER_DESIGNATOR)
1369 case INITIALIZER_STRING:
1370 print_quoted_string(&initializer->string.string, '"', 1);
1372 case INITIALIZER_WIDE_STRING:
1373 print_quoted_wide_string(&initializer->wide_string.string, '"', 1);
1375 case INITIALIZER_DESIGNATOR:
1376 print_designator(initializer->designator.designator);
1381 panic("invalid initializer kind found");
1385 * Print microsoft extended declaration modifiers.
1387 static void print_ms_modifiers(const declaration_t *declaration)
1389 if((c_mode & _MS) == 0)
1392 decl_modifiers_t modifiers = declaration->modifiers;
1394 bool ds_shown = false;
1395 const char *next = "(";
1397 if (declaration->base.kind == ENTITY_VARIABLE) {
1398 variable_t *variable = (variable_t*) declaration;
1399 if (variable->alignment != 0
1400 || variable->get_property_sym != NULL
1401 || variable->put_property_sym != NULL) {
1403 fputs("__declspec", out);
1407 if(variable->alignment != 0) {
1408 fputs(next, out); next = ", "; fprintf(out, "align(%u)", variable->alignment);
1410 if(variable->get_property_sym != NULL
1411 || variable->put_property_sym != NULL) {
1413 fputs(next, out); next = ", "; fputs("property(", out);
1414 if(variable->get_property_sym != NULL) {
1415 fprintf(out, "get=%s", variable->get_property_sym->string);
1418 if(variable->put_property_sym != NULL)
1419 fprintf(out, "%sput=%s", comma, variable->put_property_sym->string);
1425 /* DM_FORCEINLINE handled outside. */
1426 if ((modifiers & ~DM_FORCEINLINE) != 0) {
1428 fputs("__declspec", out);
1431 if(modifiers & DM_DLLIMPORT) {
1432 fputs(next, out); next = ", "; fputs("dllimport", out);
1434 if(modifiers & DM_DLLEXPORT) {
1435 fputs(next, out); next = ", "; fputs("dllexport", out);
1437 if(modifiers & DM_THREAD) {
1438 fputs(next, out); next = ", "; fputs("thread", out);
1440 if(modifiers & DM_NAKED) {
1441 fputs(next, out); next = ", "; fputs("naked", out);
1443 if(modifiers & DM_THREAD) {
1444 fputs(next, out); next = ", "; fputs("thread", out);
1446 if(modifiers & DM_SELECTANY) {
1447 fputs(next, out); next = ", "; fputs("selectany", out);
1449 if(modifiers & DM_NOTHROW) {
1450 fputs(next, out); next = ", "; fputs("nothrow", out);
1452 if(modifiers & DM_NORETURN) {
1453 fputs(next, out); next = ", "; fputs("noreturn", out);
1455 if(modifiers & DM_NOINLINE) {
1456 fputs(next, out); next = ", "; fputs("noinline", out);
1458 if (modifiers & DM_DEPRECATED) {
1459 fputs(next, out); next = ", "; fputs("deprecated", out);
1460 if(declaration->deprecated_string != NULL)
1461 fprintf(out, "(\"%s\")",
1462 declaration->deprecated_string);
1464 if(modifiers & DM_RESTRICT) {
1465 fputs(next, out); next = ", "; fputs("restrict", out);
1467 if(modifiers & DM_NOALIAS) {
1468 fputs(next, out); next = ", "; fputs("noalias", out);
1477 * Print a variable or function declaration
1479 void print_declaration(const entity_t *entity)
1481 assert(is_declaration(entity));
1482 const declaration_t *declaration = &entity->declaration;
1484 print_storage_class((storage_class_tag_t) declaration->declared_storage_class);
1485 if (entity->kind == ENTITY_FUNCTION) {
1486 function_t *function = (function_t*) declaration;
1487 if (function->is_inline) {
1488 if (declaration->modifiers & DM_FORCEINLINE) {
1489 fputs("__forceinline ", out);
1490 } else if (declaration->modifiers & DM_MICROSOFT_INLINE) {
1491 fputs("__inline ", out);
1493 fputs("inline ", out);
1497 print_ms_modifiers(declaration);
1498 if (entity->kind == ENTITY_FUNCTION) {
1499 print_type_ext(entity->declaration.type, entity->base.symbol,
1500 &entity->function.parameters);
1502 if (entity->function.statement != NULL) {
1505 print_statement(entity->function.statement);
1509 print_type_ext(declaration->type, declaration->base.symbol, NULL);
1511 if (entity->kind == ENTITY_VARIABLE
1512 && entity->variable.initializer != NULL) {
1514 print_initializer(entity->variable.initializer);
1521 * Prints an expression.
1523 * @param expression the expression
1525 void print_expression(const expression_t *expression)
1527 print_expression_prec(expression, 2 * PREC_BOTTOM);
1531 * Print a declaration.
1533 * @param declaration the declaration
1535 void print_entity(const entity_t *entity)
1537 if (entity->base.namespc != NAMESPACE_NORMAL && entity->base.symbol == NULL)
1540 switch ((entity_kind_tag_t) entity->kind) {
1541 case ENTITY_VARIABLE:
1542 case ENTITY_FUNCTION:
1543 case ENTITY_COMPOUND_MEMBER:
1544 print_declaration(entity);
1546 case ENTITY_TYPEDEF:
1547 print_typedef(entity);
1550 fputs("struct ", out);
1551 fputs(entity->base.symbol->string, out);
1552 if (entity->structe.complete) {
1554 print_compound_definition(&entity->structe);
1559 fputs("union ", out);
1560 fputs(entity->base.symbol->string, out);
1561 if (entity->unione.complete) {
1563 print_compound_definition(&entity->unione);
1568 fputs("enum ", out);
1569 fputs(entity->base.symbol->string, out);
1571 print_enum_definition(&entity->enume);
1575 case ENTITY_ENUM_VALUE:
1576 case ENTITY_LOCAL_LABEL:
1577 panic("print_entity used on unexpected entity type");
1578 case ENTITY_INVALID:
1581 panic("Invalid entity type encountered");
1585 * Print the AST of a translation unit.
1587 * @param unit the translation unit
1589 void print_ast(const translation_unit_t *unit)
1593 entity_t *entity = unit->scope.entities;
1594 for ( ; entity != NULL; entity = entity->base.next) {
1595 if (entity->kind == ENTITY_ENUM_VALUE)
1597 if (entity->base.namespc != NAMESPACE_NORMAL
1598 && entity->base.symbol == NULL)
1600 if (is_generated_entity(entity))
1604 print_entity(entity);
1609 bool is_constant_initializer(const initializer_t *initializer)
1611 switch (initializer->kind) {
1612 case INITIALIZER_STRING:
1613 case INITIALIZER_WIDE_STRING:
1614 case INITIALIZER_DESIGNATOR:
1617 case INITIALIZER_VALUE:
1618 return is_constant_expression(initializer->value.value);
1620 case INITIALIZER_LIST:
1621 for(size_t i = 0; i < initializer->list.len; ++i) {
1622 initializer_t *sub_initializer = initializer->list.initializers[i];
1623 if(!is_constant_initializer(sub_initializer))
1628 panic("invalid initializer kind found");
1631 static bool is_object_with_linker_constant_address(const expression_t *expression)
1633 switch (expression->kind) {
1634 case EXPR_UNARY_DEREFERENCE:
1635 return is_address_constant(expression->unary.value);
1638 type_t *base_type = skip_typeref(expression->select.compound->base.type);
1639 if(is_type_pointer(base_type)) {
1641 return is_address_constant(expression->select.compound);
1643 return is_object_with_linker_constant_address(expression->select.compound);
1647 case EXPR_ARRAY_ACCESS:
1648 return is_constant_expression(expression->array_access.index)
1649 && is_address_constant(expression->array_access.array_ref);
1651 case EXPR_REFERENCE: {
1652 entity_t *entity = expression->reference.entity;
1653 if (is_declaration(entity)) {
1654 switch ((storage_class_tag_t) entity->declaration.storage_class) {
1655 case STORAGE_CLASS_NONE:
1656 case STORAGE_CLASS_EXTERN:
1657 case STORAGE_CLASS_STATIC:
1660 case STORAGE_CLASS_REGISTER:
1661 case STORAGE_CLASS_TYPEDEF:
1662 case STORAGE_CLASS_AUTO:
1663 case STORAGE_CLASS_THREAD:
1664 case STORAGE_CLASS_THREAD_EXTERN:
1665 case STORAGE_CLASS_THREAD_STATIC:
1677 bool is_address_constant(const expression_t *expression)
1679 switch (expression->kind) {
1680 case EXPR_UNARY_TAKE_ADDRESS:
1681 return is_object_with_linker_constant_address(expression->unary.value);
1683 case EXPR_UNARY_DEREFERENCE: {
1685 = revert_automatic_type_conversion(expression->unary.value);
1686 /* dereferencing a function is a NOP */
1687 if(is_type_function(real_type)) {
1688 return is_address_constant(expression->unary.value);
1694 case EXPR_UNARY_CAST: {
1695 type_t *dest = skip_typeref(expression->base.type);
1696 if (!is_type_pointer(dest) && (
1697 dest->kind != TYPE_ATOMIC ||
1698 !(get_atomic_type_flags(dest->atomic.akind) & ATOMIC_TYPE_FLAG_INTEGER) ||
1699 (get_atomic_type_size(dest->atomic.akind) < get_atomic_type_size(get_intptr_kind())
1703 return (is_constant_expression(expression->unary.value)
1704 || is_address_constant(expression->unary.value));
1707 case EXPR_BINARY_ADD:
1708 case EXPR_BINARY_SUB: {
1709 expression_t *left = expression->binary.left;
1710 expression_t *right = expression->binary.right;
1712 if(is_type_pointer(skip_typeref(left->base.type))) {
1713 return is_address_constant(left) && is_constant_expression(right);
1714 } else if(is_type_pointer(skip_typeref(right->base.type))) {
1715 return is_constant_expression(left) && is_address_constant(right);
1721 case EXPR_REFERENCE: {
1722 entity_t *entity = expression->reference.entity;
1723 if (!is_declaration(entity))
1726 type_t *type = skip_typeref(entity->declaration.type);
1727 if(is_type_function(type))
1729 if(is_type_array(type)) {
1730 return is_object_with_linker_constant_address(expression);
1732 /* Prevent stray errors */
1733 if (!is_type_valid(type))
1738 case EXPR_ARRAY_ACCESS: {
1739 type_t *const type =
1740 skip_typeref(revert_automatic_type_conversion(expression));
1742 is_type_array(type) &&
1743 is_constant_expression(expression->array_access.index) &&
1744 is_address_constant(expression->array_access.array_ref);
1752 static bool is_builtin_const_call(const expression_t *expression)
1754 expression_t *function = expression->call.function;
1755 if (function->kind != EXPR_BUILTIN_SYMBOL) {
1759 symbol_t *symbol = function->builtin_symbol.symbol;
1761 switch (symbol->ID) {
1762 case T___builtin_huge_val:
1763 case T___builtin_inf:
1764 case T___builtin_inff:
1765 case T___builtin_infl:
1766 case T___builtin_nan:
1767 case T___builtin_nanf:
1768 case T___builtin_nanl:
1775 static bool is_constant_pointer(const expression_t *expression)
1777 if (is_constant_expression(expression))
1780 switch (expression->kind) {
1781 case EXPR_UNARY_CAST:
1782 return is_constant_pointer(expression->unary.value);
1788 static bool is_object_with_constant_address(const expression_t *expression)
1790 switch (expression->kind) {
1792 expression_t *compound = expression->select.compound;
1793 type_t *compound_type = compound->base.type;
1794 compound_type = skip_typeref(compound_type);
1795 if(is_type_pointer(compound_type)) {
1796 return is_constant_pointer(compound);
1798 return is_object_with_constant_address(compound);
1802 case EXPR_ARRAY_ACCESS: {
1803 array_access_expression_t const* const array_access =
1804 &expression->array_access;
1806 is_constant_expression(array_access->index) && (
1807 is_object_with_constant_address(array_access->array_ref) ||
1808 is_constant_pointer(array_access->array_ref)
1812 case EXPR_UNARY_DEREFERENCE:
1813 return is_constant_pointer(expression->unary.value);
1819 bool is_constant_expression(const expression_t *expression)
1821 switch (expression->kind) {
1824 case EXPR_CHARACTER_CONSTANT:
1825 case EXPR_WIDE_CHARACTER_CONSTANT:
1826 case EXPR_STRING_LITERAL:
1827 case EXPR_WIDE_STRING_LITERAL:
1828 case EXPR_CLASSIFY_TYPE:
1832 case EXPR_BUILTIN_CONSTANT_P:
1833 case EXPR_LABEL_ADDRESS:
1834 case EXPR_REFERENCE_ENUM_VALUE:
1838 type_t *type = expression->typeprop.type;
1840 type = expression->typeprop.tp_expression->base.type;
1842 type = skip_typeref(type);
1843 if (is_type_array(type) && type->array.is_vla)
1848 case EXPR_BUILTIN_SYMBOL:
1849 case EXPR_BUILTIN_PREFETCH:
1853 case EXPR_STATEMENT:
1854 case EXPR_REFERENCE:
1855 case EXPR_UNARY_POSTFIX_INCREMENT:
1856 case EXPR_UNARY_POSTFIX_DECREMENT:
1857 case EXPR_UNARY_PREFIX_INCREMENT:
1858 case EXPR_UNARY_PREFIX_DECREMENT:
1859 case EXPR_UNARY_ASSUME: /* has VOID type */
1860 case EXPR_UNARY_DEREFERENCE:
1861 case EXPR_UNARY_DELETE:
1862 case EXPR_UNARY_DELETE_ARRAY:
1863 case EXPR_UNARY_THROW:
1864 case EXPR_BINARY_ASSIGN:
1865 case EXPR_BINARY_MUL_ASSIGN:
1866 case EXPR_BINARY_DIV_ASSIGN:
1867 case EXPR_BINARY_MOD_ASSIGN:
1868 case EXPR_BINARY_ADD_ASSIGN:
1869 case EXPR_BINARY_SUB_ASSIGN:
1870 case EXPR_BINARY_SHIFTLEFT_ASSIGN:
1871 case EXPR_BINARY_SHIFTRIGHT_ASSIGN:
1872 case EXPR_BINARY_BITWISE_AND_ASSIGN:
1873 case EXPR_BINARY_BITWISE_XOR_ASSIGN:
1874 case EXPR_BINARY_BITWISE_OR_ASSIGN:
1875 case EXPR_BINARY_COMMA:
1876 case EXPR_ARRAY_ACCESS:
1879 case EXPR_UNARY_TAKE_ADDRESS:
1880 return is_object_with_constant_address(expression->unary.value);
1883 return is_builtin_const_call(expression);
1885 case EXPR_UNARY_NEGATE:
1886 case EXPR_UNARY_PLUS:
1887 case EXPR_UNARY_BITWISE_NEGATE:
1888 case EXPR_UNARY_NOT:
1889 return is_constant_expression(expression->unary.value);
1891 case EXPR_UNARY_CAST:
1892 case EXPR_UNARY_CAST_IMPLICIT:
1893 return is_type_arithmetic(skip_typeref(expression->base.type))
1894 && is_constant_expression(expression->unary.value);
1896 case EXPR_BINARY_ADD:
1897 case EXPR_BINARY_SUB:
1898 case EXPR_BINARY_MUL:
1899 case EXPR_BINARY_DIV:
1900 case EXPR_BINARY_MOD:
1901 case EXPR_BINARY_EQUAL:
1902 case EXPR_BINARY_NOTEQUAL:
1903 case EXPR_BINARY_LESS:
1904 case EXPR_BINARY_LESSEQUAL:
1905 case EXPR_BINARY_GREATER:
1906 case EXPR_BINARY_GREATEREQUAL:
1907 case EXPR_BINARY_BITWISE_AND:
1908 case EXPR_BINARY_BITWISE_OR:
1909 case EXPR_BINARY_BITWISE_XOR:
1910 case EXPR_BINARY_LOGICAL_AND:
1911 case EXPR_BINARY_LOGICAL_OR:
1912 case EXPR_BINARY_SHIFTLEFT:
1913 case EXPR_BINARY_SHIFTRIGHT:
1914 case EXPR_BINARY_BUILTIN_EXPECT:
1915 case EXPR_BINARY_ISGREATER:
1916 case EXPR_BINARY_ISGREATEREQUAL:
1917 case EXPR_BINARY_ISLESS:
1918 case EXPR_BINARY_ISLESSEQUAL:
1919 case EXPR_BINARY_ISLESSGREATER:
1920 case EXPR_BINARY_ISUNORDERED:
1921 return is_constant_expression(expression->binary.left)
1922 && is_constant_expression(expression->binary.right);
1924 case EXPR_COMPOUND_LITERAL:
1925 return is_constant_initializer(expression->compound_literal.initializer);
1927 case EXPR_CONDITIONAL: {
1928 expression_t *condition = expression->conditional.condition;
1929 if(!is_constant_expression(condition))
1932 long val = fold_constant(condition);
1934 return is_constant_expression(expression->conditional.true_expression);
1936 return is_constant_expression(expression->conditional.false_expression);
1945 panic("invalid expression found (is constant expression)");
1949 * Initialize the AST construction.
1953 obstack_init(&ast_obstack);
1961 obstack_free(&ast_obstack, NULL);
1965 * Set the output stream for the AST printer.
1967 * @param stream the output stream
1969 void ast_set_output(FILE *stream)
1972 type_set_output(stream);
1976 * Allocate an AST object of the given size.
1978 * @param size the size of the object to allocate
1980 * @return A new allocated object in the AST memeory space.
1982 void *(allocate_ast)(size_t size)
1984 return _allocate_ast(size);