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 #include "adt/error.h"
36 struct obstack ast_obstack;
41 /** If set, implicit casts are printed. */
42 bool print_implicit_casts = false;
44 /** If set parenthesis are printed to indicate operator precedence. */
45 bool print_parenthesis = false;
47 static void print_statement(const statement_t *statement);
48 static void print_expression_prec(const expression_t *expression, unsigned prec);
50 void change_indent(int delta)
56 void print_indent(void)
58 for(int i = 0; i < indent; ++i)
64 PREC_COMMA = 2, /* , left to right */
65 PREC_ASSIGN = 4, /* = += -= *= /= %= <<= >>= &= ^= |= right to left */
66 PREC_COND = 6, /* ?: right to left */
67 PREC_LOG_OR = 8, /* || left to right */
68 PREC_LOG_AND = 10, /* && left to right */
69 PREC_BIT_OR = 12, /* | left to right */
70 PREC_BIT_XOR = 14, /* ^ left to right */
71 PREC_BIT_AND = 16, /* & left to right */
72 PREC_EQ = 18, /* == != left to right */
73 PREC_CMP = 20, /* < <= > >= left to right */
74 PREC_SHF = 22, /* << >> left to right */
75 PREC_PLUS = 24, /* + - left to right */
76 PREC_MUL = 26, /* * / % left to right */
77 PREC_UNARY = 28, /* ! ~ ++ -- + - (type) * & sizeof right to left */
78 PREC_ACCESS = 30, /* () [] -> . left to right */
79 PREC_PRIM = 32, /* primary */
84 * Returns 1 if a given precedence level has right-to-left
85 * associativity, else -1.
87 * @param precedence the operator precedence
89 static int right_to_left(unsigned precedence) {
90 return (precedence == PREC_ASSIGN || precedence == PREC_COND ||
91 precedence == PREC_UNARY) ? 1 : -1;
95 * Return the precedence of an expression given by its kind.
97 * @param kind the expression kind
99 static unsigned get_expression_precedence(expression_kind_t kind)
101 static const unsigned prec[] = {
102 [EXPR_UNKNOWN] = PREC_PRIM,
103 [EXPR_INVALID] = PREC_PRIM,
104 [EXPR_REFERENCE] = PREC_PRIM,
105 [EXPR_CHARACTER_CONSTANT] = PREC_PRIM,
106 [EXPR_WIDE_CHARACTER_CONSTANT] = PREC_PRIM,
107 [EXPR_CONST] = PREC_PRIM,
108 [EXPR_STRING_LITERAL] = PREC_PRIM,
109 [EXPR_WIDE_STRING_LITERAL] = PREC_PRIM,
110 [EXPR_COMPOUND_LITERAL] = PREC_UNARY,
111 [EXPR_CALL] = PREC_PRIM,
112 [EXPR_CONDITIONAL] = PREC_COND,
113 [EXPR_SELECT] = PREC_ACCESS,
114 [EXPR_ARRAY_ACCESS] = PREC_ACCESS,
115 [EXPR_SIZEOF] = PREC_UNARY,
116 [EXPR_CLASSIFY_TYPE] = PREC_UNARY,
117 [EXPR_ALIGNOF] = PREC_UNARY,
119 [EXPR_FUNCNAME] = PREC_PRIM,
120 [EXPR_BUILTIN_SYMBOL] = PREC_PRIM,
121 [EXPR_BUILTIN_CONSTANT_P] = PREC_PRIM,
122 [EXPR_BUILTIN_PREFETCH] = PREC_PRIM,
123 [EXPR_OFFSETOF] = PREC_PRIM,
124 [EXPR_VA_START] = PREC_PRIM,
125 [EXPR_VA_ARG] = PREC_PRIM,
126 [EXPR_STATEMENT] = PREC_ACCESS,
128 [EXPR_UNARY_NEGATE] = PREC_UNARY,
129 [EXPR_UNARY_PLUS] = PREC_UNARY,
130 [EXPR_UNARY_BITWISE_NEGATE] = PREC_UNARY,
131 [EXPR_UNARY_NOT] = PREC_UNARY,
132 [EXPR_UNARY_DEREFERENCE] = PREC_UNARY,
133 [EXPR_UNARY_TAKE_ADDRESS] = PREC_UNARY,
134 [EXPR_UNARY_POSTFIX_INCREMENT] = PREC_UNARY,
135 [EXPR_UNARY_POSTFIX_DECREMENT] = PREC_UNARY,
136 [EXPR_UNARY_PREFIX_INCREMENT] = PREC_UNARY,
137 [EXPR_UNARY_PREFIX_DECREMENT] = PREC_UNARY,
138 [EXPR_UNARY_CAST] = PREC_UNARY,
139 [EXPR_UNARY_CAST_IMPLICIT] = PREC_UNARY,
140 [EXPR_UNARY_ASSUME] = PREC_PRIM,
142 [EXPR_BINARY_ADD] = PREC_PLUS,
143 [EXPR_BINARY_SUB] = PREC_PLUS,
144 [EXPR_BINARY_MUL] = PREC_MUL,
145 [EXPR_BINARY_DIV] = PREC_MUL,
146 [EXPR_BINARY_MOD] = PREC_MUL,
147 [EXPR_BINARY_EQUAL] = PREC_EQ,
148 [EXPR_BINARY_NOTEQUAL] = PREC_EQ,
149 [EXPR_BINARY_LESS] = PREC_CMP,
150 [EXPR_BINARY_LESSEQUAL] = PREC_CMP,
151 [EXPR_BINARY_GREATER] = PREC_CMP,
152 [EXPR_BINARY_GREATEREQUAL] = PREC_CMP,
153 [EXPR_BINARY_BITWISE_AND] = PREC_BIT_AND,
154 [EXPR_BINARY_BITWISE_OR] = PREC_BIT_OR,
155 [EXPR_BINARY_BITWISE_XOR] = PREC_BIT_XOR,
156 [EXPR_BINARY_LOGICAL_AND] = PREC_LOG_AND,
157 [EXPR_BINARY_LOGICAL_OR] = PREC_LOG_OR,
158 [EXPR_BINARY_SHIFTLEFT] = PREC_SHF,
159 [EXPR_BINARY_SHIFTRIGHT] = PREC_SHF,
160 [EXPR_BINARY_ASSIGN] = PREC_ASSIGN,
161 [EXPR_BINARY_MUL_ASSIGN] = PREC_ASSIGN,
162 [EXPR_BINARY_DIV_ASSIGN] = PREC_ASSIGN,
163 [EXPR_BINARY_MOD_ASSIGN] = PREC_ASSIGN,
164 [EXPR_BINARY_ADD_ASSIGN] = PREC_ASSIGN,
165 [EXPR_BINARY_SUB_ASSIGN] = PREC_ASSIGN,
166 [EXPR_BINARY_SHIFTLEFT_ASSIGN] = PREC_ASSIGN,
167 [EXPR_BINARY_SHIFTRIGHT_ASSIGN] = PREC_ASSIGN,
168 [EXPR_BINARY_BITWISE_AND_ASSIGN] = PREC_ASSIGN,
169 [EXPR_BINARY_BITWISE_XOR_ASSIGN] = PREC_ASSIGN,
170 [EXPR_BINARY_BITWISE_OR_ASSIGN] = PREC_ASSIGN,
171 [EXPR_BINARY_COMMA] = PREC_COMMA,
173 [EXPR_BINARY_BUILTIN_EXPECT] = PREC_PRIM,
174 [EXPR_BINARY_ISGREATER] = PREC_PRIM,
175 [EXPR_BINARY_ISGREATEREQUAL] = PREC_PRIM,
176 [EXPR_BINARY_ISLESS] = PREC_PRIM,
177 [EXPR_BINARY_ISLESSEQUAL] = PREC_PRIM,
178 [EXPR_BINARY_ISLESSGREATER] = PREC_PRIM,
179 [EXPR_BINARY_ISUNORDERED] = PREC_PRIM
181 assert((unsigned)kind < (sizeof(prec)/sizeof(prec[0])));
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_integer(type)) {
201 fprintf(out, "%lld", cnst->v.int_value);
202 } else if (is_type_float(type)) {
203 long double const val = cnst->v.float_value;
204 fprintf(out, "%.20Lg", val);
205 if (isfinite(val) && truncl(val) == val)
208 panic("unknown constant");
212 switch (type->atomic.akind) {
213 case ATOMIC_TYPE_UINT: suffix = "U"; break;
214 case ATOMIC_TYPE_LONG: suffix = "L"; break;
215 case ATOMIC_TYPE_ULONG: suffix = "UL"; break;
216 case ATOMIC_TYPE_LONGLONG: suffix = "LL"; break;
217 case ATOMIC_TYPE_ULONGLONG: suffix = "ULL"; break;
218 case ATOMIC_TYPE_FLOAT: suffix = "F"; break;
219 case ATOMIC_TYPE_LONG_DOUBLE: suffix = "L"; break;
221 default: suffix = NULL; break;
228 * Print a quoted string constant.
230 * @param string the string constant
231 * @param border the border char
232 * @param skip number of chars to skip at the end
234 static void print_quoted_string(const string_t *const string, char border, int skip)
237 const char *end = string->begin + string->size - skip;
238 for (const char *c = string->begin; c != end; ++c) {
243 case '\\': fputs("\\\\", out); break;
244 case '\a': fputs("\\a", out); break;
245 case '\b': fputs("\\b", out); break;
246 case '\f': fputs("\\f", out); break;
247 case '\n': fputs("\\n", out); break;
248 case '\r': fputs("\\r", out); break;
249 case '\t': fputs("\\t", out); break;
250 case '\v': fputs("\\v", out); break;
251 case '\?': fputs("\\?", out); break;
253 if (c_mode & _GNUC) {
254 fputs("\\e", out); break;
259 fprintf(out, "\\%03o", *c);
270 * Prints a wide string literal expression.
272 * @param wstr the wide string literal expression
273 * @param border the border char
274 * @param skip number of chars to skip at the end
276 static void print_quoted_wide_string(const wide_string_t *const wstr,
277 char border, int skip)
281 const wchar_rep_t *end = wstr->begin + wstr->size - skip;
282 for (const wchar_rep_t *c = wstr->begin; c != end; ++c) {
284 case L'\"': fputs("\\\"", out); break;
285 case L'\\': fputs("\\\\", out); break;
286 case L'\a': fputs("\\a", out); break;
287 case L'\b': fputs("\\b", out); break;
288 case L'\f': fputs("\\f", out); break;
289 case L'\n': fputs("\\n", out); break;
290 case L'\r': fputs("\\r", out); break;
291 case L'\t': fputs("\\t", out); break;
292 case L'\v': fputs("\\v", out); break;
293 case L'\?': fputs("\\?", out); break;
295 if (c_mode & _GNUC) {
296 fputs("\\e", out); break;
300 const unsigned tc = *c;
303 fprintf(out, "\\%03o", (char)*c);
307 } else if (tc < 0x800) {
308 fputc(0xC0 | (tc >> 6), out);
309 fputc(0x80 | (tc & 0x3F), out);
310 } else if (tc < 0x10000) {
311 fputc(0xE0 | ( tc >> 12), out);
312 fputc(0x80 | ((tc >> 6) & 0x3F), out);
313 fputc(0x80 | ( tc & 0x3F), out);
315 fputc(0xF0 | ( tc >> 18), out);
316 fputc(0x80 | ((tc >> 12) & 0x3F), out);
317 fputc(0x80 | ((tc >> 6) & 0x3F), out);
318 fputc(0x80 | ( tc & 0x3F), out);
327 * Print a constant character expression.
329 * @param cnst the constant character expression
331 static void print_character_constant(const const_expression_t *cnst)
333 print_quoted_string(&cnst->v.character, '\'', 0);
336 static void print_wide_character_constant(const const_expression_t *cnst)
338 print_quoted_wide_string(&cnst->v.wide_character, '\'', 0);
342 * Prints a string literal expression.
344 * @param string_literal the string literal expression
346 static void print_string_literal(
347 const string_literal_expression_t *string_literal)
349 print_quoted_string(&string_literal->value, '"', 1);
353 * Prints a predefined symbol.
355 static void print_funcname(
356 const funcname_expression_t *funcname)
359 switch(funcname->kind) {
360 case FUNCNAME_FUNCTION: s = (c_mode & _C99) ? "__func__" : "__FUNCTION__"; break;
361 case FUNCNAME_PRETTY_FUNCTION: s = "__PRETTY_FUNCTION__"; break;
362 case FUNCNAME_FUNCSIG: s = "__FUNCSIG__"; break;
363 case FUNCNAME_FUNCDNAME: s = "__FUNCDNAME__"; break;
370 static void print_wide_string_literal(
371 const wide_string_literal_expression_t *const wstr)
373 print_quoted_wide_string(&wstr->value, '"', 1);
376 static void print_compound_literal(
377 const compound_literal_expression_t *expression)
380 print_type(expression->type);
382 print_initializer(expression->initializer);
386 * Prints a call expression.
388 * @param call the call expression
390 static void print_call_expression(const call_expression_t *call)
392 unsigned prec = get_expression_precedence(call->base.kind);
393 print_expression_prec(call->function, prec);
395 call_argument_t *argument = call->arguments;
397 while(argument != NULL) {
403 print_expression_prec(argument->expression, PREC_COMMA + 1);
405 argument = argument->next;
411 * Prints a binary expression.
413 * @param binexpr the binary expression
415 static void print_binary_expression(const binary_expression_t *binexpr)
417 unsigned prec = get_expression_precedence(binexpr->base.kind);
418 int r2l = right_to_left(prec);
420 if(binexpr->base.kind == EXPR_BINARY_BUILTIN_EXPECT) {
421 fputs("__builtin_expect(", out);
422 print_expression_prec(binexpr->left, prec);
424 print_expression_prec(binexpr->right, prec);
429 print_expression_prec(binexpr->left, prec + r2l);
430 if (binexpr->base.kind != EXPR_BINARY_COMMA) {
433 switch (binexpr->base.kind) {
434 case EXPR_BINARY_COMMA: fputs(",", out); break;
435 case EXPR_BINARY_ASSIGN: fputs("=", out); break;
436 case EXPR_BINARY_ADD: fputs("+", out); break;
437 case EXPR_BINARY_SUB: fputs("-", out); break;
438 case EXPR_BINARY_MUL: fputs("*", out); break;
439 case EXPR_BINARY_MOD: fputs("%", out); break;
440 case EXPR_BINARY_DIV: fputs("/", out); break;
441 case EXPR_BINARY_BITWISE_OR: fputs("|", out); break;
442 case EXPR_BINARY_BITWISE_AND: fputs("&", out); break;
443 case EXPR_BINARY_BITWISE_XOR: fputs("^", out); break;
444 case EXPR_BINARY_LOGICAL_OR: fputs("||", out); break;
445 case EXPR_BINARY_LOGICAL_AND: fputs("&&", out); break;
446 case EXPR_BINARY_NOTEQUAL: fputs("!=", out); break;
447 case EXPR_BINARY_EQUAL: fputs("==", out); break;
448 case EXPR_BINARY_LESS: fputs("<", out); break;
449 case EXPR_BINARY_LESSEQUAL: fputs("<=", out); break;
450 case EXPR_BINARY_GREATER: fputs(">", out); break;
451 case EXPR_BINARY_GREATEREQUAL: fputs(">=", out); break;
452 case EXPR_BINARY_SHIFTLEFT: fputs("<<", out); break;
453 case EXPR_BINARY_SHIFTRIGHT: fputs(">>", out); break;
455 case EXPR_BINARY_ADD_ASSIGN: fputs("+=", out); break;
456 case EXPR_BINARY_SUB_ASSIGN: fputs("-=", out); break;
457 case EXPR_BINARY_MUL_ASSIGN: fputs("*=", out); break;
458 case EXPR_BINARY_MOD_ASSIGN: fputs("%=", out); break;
459 case EXPR_BINARY_DIV_ASSIGN: fputs("/=", out); break;
460 case EXPR_BINARY_BITWISE_OR_ASSIGN: fputs("|=", out); break;
461 case EXPR_BINARY_BITWISE_AND_ASSIGN: fputs("&=", out); break;
462 case EXPR_BINARY_BITWISE_XOR_ASSIGN: fputs("^=", out); break;
463 case EXPR_BINARY_SHIFTLEFT_ASSIGN: fputs("<<=", out); break;
464 case EXPR_BINARY_SHIFTRIGHT_ASSIGN: fputs(">>=", out); break;
465 default: panic("invalid binexpression found");
468 print_expression_prec(binexpr->right, prec - r2l);
472 * Prints an unary expression.
474 * @param unexpr the unary expression
476 static void print_unary_expression(const unary_expression_t *unexpr)
478 unsigned prec = get_expression_precedence(unexpr->base.kind);
479 switch(unexpr->base.kind) {
480 case EXPR_UNARY_NEGATE: fputs("-", out); break;
481 case EXPR_UNARY_PLUS: fputs("+", out); break;
482 case EXPR_UNARY_NOT: fputs("!", out); break;
483 case EXPR_UNARY_BITWISE_NEGATE: fputs("~", out); break;
484 case EXPR_UNARY_PREFIX_INCREMENT: fputs("++", out); break;
485 case EXPR_UNARY_PREFIX_DECREMENT: fputs("--", out); break;
486 case EXPR_UNARY_DEREFERENCE: fputs("*", out); break;
487 case EXPR_UNARY_TAKE_ADDRESS: fputs("&", 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_expression_prec(unexpr->value, PREC_COMMA + 1);
509 panic("invalid unary expression found");
511 print_expression_prec(unexpr->value, prec);
515 * Prints a reference expression.
517 * @param ref the reference expression
519 static void print_reference_expression(const reference_expression_t *ref)
521 fprintf(out, "%s", ref->declaration->symbol->string);
525 * Prints an array expression.
527 * @param expression the array expression
529 static void print_array_expression(const array_access_expression_t *expression)
531 unsigned prec = get_expression_precedence(expression->base.kind);
532 if(!expression->flipped) {
533 print_expression_prec(expression->array_ref, prec);
535 print_expression_prec(expression->index, PREC_BOTTOM);
538 print_expression_prec(expression->index, prec);
540 print_expression_prec(expression->array_ref, PREC_BOTTOM);
546 * Prints a typeproperty expression (sizeof or __alignof__).
548 * @param expression the type property expression
550 static void print_typeprop_expression(const typeprop_expression_t *expression)
552 if (expression->base.kind == EXPR_SIZEOF) {
553 fputs("sizeof", out);
555 assert(expression->base.kind == EXPR_ALIGNOF);
556 fputs("__alignof__", out);
558 if(expression->tp_expression != NULL) {
559 /* always print the '()' here, sizeof x is right but unusual */
561 print_expression_prec(expression->tp_expression, PREC_ACCESS);
565 print_type(expression->type);
571 * Prints an builtin symbol.
573 * @param expression the builtin symbol expression
575 static void print_builtin_symbol(const builtin_symbol_expression_t *expression)
577 fputs(expression->symbol->string, out);
581 * Prints a builtin constant expression.
583 * @param expression the builtin constant expression
585 static void print_builtin_constant(const builtin_constant_expression_t *expression)
587 fputs("__builtin_constant_p(", out);
588 print_expression_prec(expression->value, PREC_COMMA + 1);
593 * Prints a builtin prefetch expression.
595 * @param expression the builtin prefetch expression
597 static void print_builtin_prefetch(const builtin_prefetch_expression_t *expression)
599 fputs("__builtin_prefetch(", out);
600 print_expression_prec(expression->adr, PREC_COMMA + 1);
601 if (expression->rw) {
603 print_expression_prec(expression->rw, PREC_COMMA + 1);
605 if (expression->locality) {
607 print_expression_prec(expression->locality, PREC_COMMA + 1);
613 * Prints a conditional expression.
615 * @param expression the conditional expression
617 static void print_conditional(const conditional_expression_t *expression)
619 unsigned prec = get_expression_precedence(expression->base.kind);
621 print_expression_prec(expression->condition, prec);
623 if (expression->true_expression != NULL) {
624 print_expression_prec(expression->true_expression, prec);
629 print_expression_prec(expression->false_expression, prec);
634 * Prints a va_start expression.
636 * @param expression the va_start expression
638 static void print_va_start(const va_start_expression_t *const expression)
640 fputs("__builtin_va_start(", out);
641 print_expression_prec(expression->ap, PREC_COMMA + 1);
643 fputs(expression->parameter->symbol->string, out);
648 * Prints a va_arg expression.
650 * @param expression the va_arg expression
652 static void print_va_arg(const va_arg_expression_t *expression)
654 fputs("__builtin_va_arg(", out);
655 print_expression_prec(expression->ap, PREC_COMMA + 1);
657 print_type(expression->base.type);
662 * Prints a select expression (. or ->).
664 * @param expression the select expression
666 static void print_select(const select_expression_t *expression)
668 unsigned prec = get_expression_precedence(expression->base.kind);
669 print_expression_prec(expression->compound, prec);
670 if(is_type_pointer(skip_typeref(expression->compound->base.type))) {
675 fputs(expression->symbol->string, out);
679 * Prints a type classify expression.
681 * @param expr the type classify expression
683 static void print_classify_type_expression(
684 const classify_type_expression_t *const expr)
686 fputs("__builtin_classify_type(", out);
687 print_expression_prec(expr->type_expression, PREC_COMMA + 1);
692 * Prints a designator.
694 * @param designator the designator
696 static void print_designator(const designator_t *designator)
698 for ( ; designator != NULL; designator = designator->next) {
699 if (designator->symbol == NULL) {
701 print_expression_prec(designator->array_index, PREC_BOTTOM);
705 fputs(designator->symbol->string, out);
711 * Prints an offsetof expression.
713 * @param expression the offset expression
715 static void print_offsetof_expression(const offsetof_expression_t *expression)
717 fputs("__builtin_offsetof", out);
719 print_type(expression->type);
721 print_designator(expression->designator);
726 * Prints a statement expression.
728 * @param expression the statement expression
730 static void print_statement_expression(const statement_expression_t *expression)
733 print_statement(expression->statement);
738 * Prints an expression with parenthesis if needed.
740 * @param expression the expression to print
741 * @param top_prec the precedence of the user of this expression.
743 static void print_expression_prec(const expression_t *expression, unsigned top_prec)
745 if (expression->kind == EXPR_UNARY_CAST_IMPLICIT && !print_implicit_casts) {
746 expression = expression->unary.value;
748 unsigned prec = get_expression_precedence(expression->base.kind);
749 if (print_parenthesis && top_prec != PREC_BOTTOM)
753 switch(expression->kind) {
756 fprintf(out, "$invalid expression$");
758 case EXPR_CHARACTER_CONSTANT:
759 print_character_constant(&expression->conste);
761 case EXPR_WIDE_CHARACTER_CONSTANT:
762 print_wide_character_constant(&expression->conste);
765 print_const(&expression->conste);
768 print_funcname(&expression->funcname);
770 case EXPR_STRING_LITERAL:
771 print_string_literal(&expression->string);
773 case EXPR_WIDE_STRING_LITERAL:
774 print_wide_string_literal(&expression->wide_string);
776 case EXPR_COMPOUND_LITERAL:
777 print_compound_literal(&expression->compound_literal);
780 print_call_expression(&expression->call);
783 print_binary_expression(&expression->binary);
786 print_reference_expression(&expression->reference);
788 case EXPR_ARRAY_ACCESS:
789 print_array_expression(&expression->array_access);
792 print_unary_expression(&expression->unary);
796 print_typeprop_expression(&expression->typeprop);
798 case EXPR_BUILTIN_SYMBOL:
799 print_builtin_symbol(&expression->builtin_symbol);
801 case EXPR_BUILTIN_CONSTANT_P:
802 print_builtin_constant(&expression->builtin_constant);
804 case EXPR_BUILTIN_PREFETCH:
805 print_builtin_prefetch(&expression->builtin_prefetch);
807 case EXPR_CONDITIONAL:
808 print_conditional(&expression->conditional);
811 print_va_start(&expression->va_starte);
814 print_va_arg(&expression->va_arge);
817 print_select(&expression->select);
819 case EXPR_CLASSIFY_TYPE:
820 print_classify_type_expression(&expression->classify_type);
823 print_offsetof_expression(&expression->offsetofe);
826 print_statement_expression(&expression->statement);
831 fprintf(out, "some expression of type %d", (int) expression->kind);
839 * Print an compound statement.
841 * @param block the compound statement
843 static void print_compound_statement(const compound_statement_t *block)
848 statement_t *statement = block->statements;
849 while(statement != NULL) {
850 if (statement->base.kind == STATEMENT_CASE_LABEL)
853 print_statement(statement);
855 statement = statement->base.next;
863 * Print a return statement.
865 * @param statement the return statement
867 static void print_return_statement(const return_statement_t *statement)
869 fprintf(out, "return ");
870 if(statement->value != NULL)
871 print_expression(statement->value);
876 * Print an expression statement.
878 * @param statement the expression statement
880 static void print_expression_statement(const expression_statement_t *statement)
882 print_expression(statement->expression);
887 * Print a goto statement.
889 * @param statement the goto statement
891 static void print_goto_statement(const goto_statement_t *statement)
893 fprintf(out, "goto ");
894 fputs(statement->label->symbol->string, out);
895 fprintf(stderr, "(%p)", (void*) statement->label);
900 * Print a label statement.
902 * @param statement the label statement
904 static void print_label_statement(const label_statement_t *statement)
906 fprintf(stderr, "(%p)", (void*) statement->label);
907 fprintf(out, "%s:\n", statement->label->symbol->string);
908 print_statement(statement->statement);
912 * Print an if statement.
914 * @param statement the if statement
916 static void print_if_statement(const if_statement_t *statement)
919 print_expression(statement->condition);
921 print_statement(statement->true_statement);
923 if(statement->false_statement != NULL) {
926 print_statement(statement->false_statement);
931 * Print a switch statement.
933 * @param statement the switch statement
935 static void print_switch_statement(const switch_statement_t *statement)
937 fputs("switch (", out);
938 print_expression(statement->expression);
940 print_statement(statement->body);
944 * Print a case label (including the default label).
946 * @param statement the case label statement
948 static void print_case_label(const case_label_statement_t *statement)
950 if(statement->expression == NULL) {
951 fputs("default:\n", out);
954 print_expression(statement->expression);
955 if (statement->end_range != NULL) {
957 print_expression(statement->end_range);
962 if(statement->statement != NULL) {
963 if (statement->statement->base.kind == STATEMENT_CASE_LABEL) {
967 print_statement(statement->statement);
972 * Print a declaration statement.
974 * @param statement the statement
976 static void print_declaration_statement(
977 const declaration_statement_t *statement)
980 for (declaration_t *declaration = statement->declarations_begin;
981 declaration != statement->declarations_end->next;
982 declaration = declaration->next) {
983 if (declaration->storage_class == STORAGE_CLASS_ENUM_ENTRY)
985 if (declaration->implicit)
993 print_declaration(declaration);
999 * Print a while statement.
1001 * @param statement the statement
1003 static void print_while_statement(const while_statement_t *statement)
1005 fputs("while (", out);
1006 print_expression(statement->condition);
1008 print_statement(statement->body);
1012 * Print a do-while statement.
1014 * @param statement the statement
1016 static void print_do_while_statement(const do_while_statement_t *statement)
1019 print_statement(statement->body);
1021 fputs("while (", out);
1022 print_expression(statement->condition);
1027 * Print a for statement.
1029 * @param statement the statement
1031 static void print_for_statement(const for_statement_t *statement)
1033 fputs("for (", out);
1034 declaration_t *decl = statement->scope.declarations;
1035 while (decl != NULL && decl->implicit)
1038 assert(statement->initialisation == NULL);
1039 print_declaration(decl);
1040 if (decl->next != NULL) {
1041 panic("multiple declarations in for statement not supported yet");
1045 if(statement->initialisation) {
1046 print_expression(statement->initialisation);
1050 if(statement->condition != NULL) {
1051 print_expression(statement->condition);
1054 if(statement->step != NULL) {
1055 print_expression(statement->step);
1058 print_statement(statement->body);
1062 * Print assembler arguments.
1064 * @param arguments the arguments
1066 static void print_asm_arguments(asm_argument_t *arguments)
1068 asm_argument_t *argument = arguments;
1069 for( ; argument != NULL; argument = argument->next) {
1070 if(argument != arguments)
1073 if(argument->symbol) {
1074 fprintf(out, "[%s] ", argument->symbol->string);
1076 print_quoted_string(&argument->constraints, '"', 1);
1078 print_expression(argument->expression);
1084 * Print assembler clobbers.
1086 * @param clobbers the clobbers
1088 static void print_asm_clobbers(asm_clobber_t *clobbers)
1090 asm_clobber_t *clobber = clobbers;
1091 for( ; clobber != NULL; clobber = clobber->next) {
1092 if(clobber != clobbers)
1095 print_quoted_string(&clobber->clobber, '"', 1);
1100 * Print an assembler statement.
1102 * @param statement the statement
1104 static void print_asm_statement(const asm_statement_t *statement)
1107 if(statement->is_volatile) {
1108 fputs("volatile ", out);
1111 print_quoted_string(&statement->asm_text, '"', 1);
1112 if(statement->inputs == NULL && statement->outputs == NULL
1113 && statement->clobbers == NULL)
1114 goto end_of_print_asm_statement;
1117 print_asm_arguments(statement->inputs);
1118 if(statement->outputs == NULL && statement->clobbers == NULL)
1119 goto end_of_print_asm_statement;
1122 print_asm_arguments(statement->outputs);
1123 if(statement->clobbers == NULL)
1124 goto end_of_print_asm_statement;
1127 print_asm_clobbers(statement->clobbers);
1129 end_of_print_asm_statement:
1134 * Print a microsoft __try statement.
1136 * @param statement the statement
1138 static void print_ms_try_statement(const ms_try_statement_t *statement)
1140 fputs("__try ", out);
1141 print_statement(statement->try_statement);
1143 if(statement->except_expression != NULL) {
1144 fputs("__except(", out);
1145 print_expression(statement->except_expression);
1148 fputs("__finally ", out);
1150 print_statement(statement->final_statement);
1154 * Print a microsoft __leave statement.
1156 * @param statement the statement
1158 static void print_leave_statement(const leave_statement_t *statement)
1161 fputs("__leave;\n", out);
1165 * Print a statement.
1167 * @param statement the statement
1169 void print_statement(const statement_t *statement)
1171 switch(statement->kind) {
1172 case STATEMENT_EMPTY:
1175 case STATEMENT_COMPOUND:
1176 print_compound_statement(&statement->compound);
1178 case STATEMENT_RETURN:
1179 print_return_statement(&statement->returns);
1181 case STATEMENT_EXPRESSION:
1182 print_expression_statement(&statement->expression);
1184 case STATEMENT_LABEL:
1185 print_label_statement(&statement->label);
1187 case STATEMENT_GOTO:
1188 print_goto_statement(&statement->gotos);
1190 case STATEMENT_CONTINUE:
1191 fputs("continue;\n", out);
1193 case STATEMENT_BREAK:
1194 fputs("break;\n", out);
1197 print_if_statement(&statement->ifs);
1199 case STATEMENT_SWITCH:
1200 print_switch_statement(&statement->switchs);
1202 case STATEMENT_CASE_LABEL:
1203 print_case_label(&statement->case_label);
1205 case STATEMENT_DECLARATION:
1206 print_declaration_statement(&statement->declaration);
1208 case STATEMENT_WHILE:
1209 print_while_statement(&statement->whiles);
1211 case STATEMENT_DO_WHILE:
1212 print_do_while_statement(&statement->do_while);
1215 print_for_statement(&statement->fors);
1218 print_asm_statement(&statement->asms);
1220 case STATEMENT_MS_TRY:
1221 print_ms_try_statement(&statement->ms_try);
1223 case STATEMENT_LEAVE:
1224 print_leave_statement(&statement->leave);
1226 case STATEMENT_INVALID:
1227 fprintf(out, "$invalid statement$");
1233 * Print a storage class.
1235 * @param storage_class the storage class
1237 static void print_storage_class(storage_class_tag_t storage_class)
1239 switch(storage_class) {
1240 case STORAGE_CLASS_ENUM_ENTRY:
1241 case STORAGE_CLASS_NONE:
1243 case STORAGE_CLASS_TYPEDEF: fputs("typedef ", out); break;
1244 case STORAGE_CLASS_EXTERN: fputs("extern ", out); break;
1245 case STORAGE_CLASS_STATIC: fputs("static ", out); break;
1246 case STORAGE_CLASS_AUTO: fputs("auto ", out); break;
1247 case STORAGE_CLASS_REGISTER: fputs("register ", out); break;
1248 case STORAGE_CLASS_THREAD: fputs("__thread", out); break;
1249 case STORAGE_CLASS_THREAD_EXTERN: fputs("extern __thread", out); break;
1250 case STORAGE_CLASS_THREAD_STATIC: fputs("static __thread", out); break;
1255 * Print an initializer.
1257 * @param initializer the initializer
1259 void print_initializer(const initializer_t *initializer)
1261 if(initializer == NULL) {
1266 switch(initializer->kind) {
1267 case INITIALIZER_VALUE: {
1268 const initializer_value_t *value = &initializer->value;
1269 print_expression(value->value);
1272 case INITIALIZER_LIST: {
1273 assert(initializer->kind == INITIALIZER_LIST);
1275 const initializer_list_t *list = &initializer->list;
1277 for(size_t i = 0 ; i < list->len; ++i) {
1278 const initializer_t *sub_init = list->initializers[i];
1279 print_initializer(list->initializers[i]);
1280 if(i < list->len-1) {
1281 if(sub_init == NULL || sub_init->kind != INITIALIZER_DESIGNATOR)
1288 case INITIALIZER_STRING:
1289 print_quoted_string(&initializer->string.string, '"', 1);
1291 case INITIALIZER_WIDE_STRING:
1292 print_quoted_wide_string(&initializer->wide_string.string, '"', 1);
1294 case INITIALIZER_DESIGNATOR:
1295 print_designator(initializer->designator.designator);
1300 panic("invalid initializer kind found");
1304 * Print microsoft extended declaration modifiers.
1306 static void print_ms_modifiers(const declaration_t *declaration) {
1307 if((c_mode & _MS) == 0)
1310 decl_modifiers_t modifiers = declaration->modifiers;
1312 /* DM_FORCEINLINE handled outside. */
1313 if ((modifiers & ~DM_FORCEINLINE) != 0 ||
1314 declaration->alignment != 0 ||
1315 declaration->get_property_sym != NULL ||
1316 declaration->put_property_sym != NULL) {
1319 fputs("__declspec", out);
1320 if(modifiers & DM_DLLIMPORT) {
1321 fputs(next, out); next = ", "; fputs("dllimport", out);
1323 if(modifiers & DM_DLLEXPORT) {
1324 fputs(next, out); next = ", "; fputs("dllexport", out);
1326 if(modifiers & DM_THREAD) {
1327 fputs(next, out); next = ", "; fputs("thread", out);
1329 if(modifiers & DM_NAKED) {
1330 fputs(next, out); next = ", "; fputs("naked", out);
1332 if(modifiers & DM_THREAD) {
1333 fputs(next, out); next = ", "; fputs("thread", out);
1335 if(modifiers & DM_SELECTANY) {
1336 fputs(next, out); next = ", "; fputs("selectany", out);
1338 if(modifiers & DM_NOTHROW) {
1339 fputs(next, out); next = ", "; fputs("nothrow", out);
1341 if(modifiers & DM_NORETURN) {
1342 fputs(next, out); next = ", "; fputs("noreturn", out);
1344 if(modifiers & DM_NOINLINE) {
1345 fputs(next, out); next = ", "; fputs("noinline", out);
1347 if (modifiers & DM_DEPRECATED) {
1348 fputs(next, out); next = ", "; fputs("deprecated", out);
1349 if(declaration->deprecated_string != NULL)
1350 fprintf(out, "(\"%s\")", declaration->deprecated_string);
1352 if(declaration->alignment != 0) {
1353 fputs(next, out); next = ", "; fprintf(out, "align(%u)", declaration->alignment);
1355 if(modifiers & DM_RESTRICT) {
1356 fputs(next, out); next = ", "; fputs("restrict", out);
1358 if(modifiers & DM_NOALIAS) {
1359 fputs(next, out); next = ", "; fputs("noalias", out);
1361 if(declaration->get_property_sym != NULL || declaration->put_property_sym != NULL) {
1363 fputs(next, out); next = ", "; fprintf(out, "property(");
1364 if(declaration->get_property_sym != NULL) {
1365 fprintf(out, "get=%s", declaration->get_property_sym->string);
1368 if(declaration->put_property_sym != NULL)
1369 fprintf(out, "%sput=%s", comma, declaration->put_property_sym->string);
1377 * Print a declaration in the NORMAL namespace.
1379 * @param declaration the declaration
1381 static void print_normal_declaration(const declaration_t *declaration)
1383 print_storage_class((storage_class_tag_t) declaration->declared_storage_class);
1384 if (declaration->is_inline) {
1385 if (declaration->modifiers & DM_FORCEINLINE) {
1386 fputs("__forceinline ", out);
1387 } else if (declaration->modifiers & DM_MICROSOFT_INLINE) {
1388 fputs("__inline ", out);
1390 fputs("inline ", out);
1393 print_ms_modifiers(declaration);
1394 print_type_ext(declaration->type, declaration->symbol,
1395 &declaration->scope);
1397 if(declaration->type->kind == TYPE_FUNCTION) {
1398 if(declaration->init.statement != NULL) {
1400 print_statement(declaration->init.statement);
1403 } else if(declaration->init.initializer != NULL) {
1405 print_initializer(declaration->init.initializer);
1411 * Prints an expression.
1413 * @param expression the expression
1415 void print_expression(const expression_t *expression) {
1416 print_expression_prec(expression, PREC_BOTTOM);
1420 * Print a declaration.
1422 * @param declaration the declaration
1424 void print_declaration(const declaration_t *declaration)
1426 if(declaration->namespc != NAMESPACE_NORMAL &&
1427 declaration->symbol == NULL)
1430 switch(declaration->namespc) {
1431 case NAMESPACE_NORMAL:
1432 print_normal_declaration(declaration);
1434 case NAMESPACE_STRUCT:
1435 fputs("struct ", out);
1436 fputs(declaration->symbol->string, out);
1438 print_compound_definition(declaration);
1441 case NAMESPACE_UNION:
1442 fputs("union ", out);
1443 fputs(declaration->symbol->string, out);
1445 print_compound_definition(declaration);
1448 case NAMESPACE_ENUM:
1449 fputs("enum ", out);
1450 fputs(declaration->symbol->string, out);
1452 print_enum_definition(declaration);
1459 * Print the AST of a translation unit.
1461 * @param unit the translation unit
1463 void print_ast(const translation_unit_t *unit)
1467 declaration_t *declaration = unit->scope.declarations;
1468 for( ; declaration != NULL; declaration = declaration->next) {
1469 if(declaration->storage_class == STORAGE_CLASS_ENUM_ENTRY)
1471 if(declaration->namespc != NAMESPACE_NORMAL &&
1472 declaration->symbol == NULL)
1474 if (declaration->implicit)
1478 print_declaration(declaration);
1483 bool is_constant_initializer(const initializer_t *initializer)
1485 switch(initializer->kind) {
1486 case INITIALIZER_STRING:
1487 case INITIALIZER_WIDE_STRING:
1488 case INITIALIZER_DESIGNATOR:
1491 case INITIALIZER_VALUE:
1492 return is_constant_expression(initializer->value.value);
1494 case INITIALIZER_LIST:
1495 for(size_t i = 0; i < initializer->list.len; ++i) {
1496 initializer_t *sub_initializer = initializer->list.initializers[i];
1497 if(!is_constant_initializer(sub_initializer))
1502 panic("invalid initializer kind found");
1505 static bool is_object_with_linker_constant_address(const expression_t *expression)
1507 switch(expression->kind) {
1508 case EXPR_UNARY_DEREFERENCE:
1509 return is_address_constant(expression->unary.value);
1512 type_t *base_type = skip_typeref(expression->select.compound->base.type);
1513 if(is_type_pointer(base_type)) {
1515 return is_address_constant(expression->select.compound);
1517 return is_object_with_linker_constant_address(expression->select.compound);
1521 case EXPR_ARRAY_ACCESS:
1522 return is_constant_expression(expression->array_access.index)
1523 && is_address_constant(expression->array_access.array_ref);
1525 case EXPR_REFERENCE: {
1526 declaration_t *declaration = expression->reference.declaration;
1527 switch((storage_class_tag_t) declaration->storage_class) {
1528 case STORAGE_CLASS_NONE:
1529 case STORAGE_CLASS_EXTERN:
1530 case STORAGE_CLASS_STATIC:
1542 bool is_address_constant(const expression_t *expression)
1544 switch(expression->kind) {
1545 case EXPR_UNARY_TAKE_ADDRESS:
1546 return is_object_with_linker_constant_address(expression->unary.value);
1548 case EXPR_UNARY_DEREFERENCE: {
1550 = revert_automatic_type_conversion(expression->unary.value);
1551 /* dereferencing a function is a NOP */
1552 if(is_type_function(real_type)) {
1553 return is_address_constant(expression->unary.value);
1559 case EXPR_UNARY_CAST: {
1560 type_t *dest = skip_typeref(expression->base.type);
1561 if (!is_type_pointer(dest) &&
1562 ! (dest->kind == TYPE_ATOMIC
1563 && (get_atomic_type_flags(dest->atomic.akind) & ATOMIC_TYPE_FLAG_INTEGER)
1564 && (get_atomic_type_size(dest->atomic.akind) >= get_atomic_type_size(get_intptr_kind()))))
1567 return (is_constant_expression(expression->unary.value)
1568 || is_address_constant(expression->unary.value));
1571 case EXPR_BINARY_ADD:
1572 case EXPR_BINARY_SUB: {
1573 expression_t *left = expression->binary.left;
1574 expression_t *right = expression->binary.right;
1576 if(is_type_pointer(skip_typeref(left->base.type))) {
1577 return is_address_constant(left) && is_constant_expression(right);
1578 } else if(is_type_pointer(skip_typeref(right->base.type))) {
1579 return is_constant_expression(left) && is_address_constant(right);
1585 case EXPR_REFERENCE: {
1586 declaration_t *declaration = expression->reference.declaration;
1587 type_t *type = skip_typeref(declaration->type);
1588 if(is_type_function(type))
1590 if(is_type_array(type)) {
1591 return is_object_with_linker_constant_address(expression);
1601 static bool is_builtin_const_call(const expression_t *expression)
1603 expression_t *function = expression->call.function;
1604 if (function->kind != EXPR_BUILTIN_SYMBOL) {
1608 symbol_t *symbol = function->builtin_symbol.symbol;
1610 switch (symbol->ID) {
1611 case T___builtin_huge_val:
1612 case T___builtin_nan:
1613 case T___builtin_nanf:
1614 case T___builtin_nand:
1621 static bool is_constant_pointer(const expression_t *expression)
1623 if (is_constant_expression(expression))
1626 switch (expression->kind) {
1628 return is_constant_pointer(expression->select.compound);
1629 case EXPR_UNARY_CAST:
1630 return is_constant_pointer(expression->unary.value);
1636 static bool is_object_with_constant_address(const expression_t *expression)
1638 switch(expression->kind) {
1640 expression_t *compound = expression->select.compound;
1641 type_t *compound_type = compound->base.type;
1642 compound_type = skip_typeref(compound_type);
1643 if(is_type_pointer(compound_type)) {
1644 return is_constant_pointer(compound);
1646 return is_object_with_constant_address(compound);
1649 case EXPR_ARRAY_ACCESS:
1650 return is_constant_pointer(expression->array_access.array_ref)
1651 && is_constant_expression(expression->array_access.index);
1652 case EXPR_UNARY_DEREFERENCE:
1653 return is_constant_pointer(expression->unary.value);
1659 bool is_constant_expression(const expression_t *expression)
1661 switch(expression->kind) {
1664 case EXPR_CHARACTER_CONSTANT:
1665 case EXPR_WIDE_CHARACTER_CONSTANT:
1666 case EXPR_STRING_LITERAL:
1667 case EXPR_WIDE_STRING_LITERAL:
1668 case EXPR_CLASSIFY_TYPE:
1672 case EXPR_BUILTIN_CONSTANT_P:
1676 type_t *type = expression->typeprop.type;
1678 type = expression->typeprop.tp_expression->base.type;
1680 type = skip_typeref(type);
1681 if (is_type_array(type) && type->array.is_vla)
1686 case EXPR_BUILTIN_SYMBOL:
1687 case EXPR_BUILTIN_PREFETCH:
1691 case EXPR_STATEMENT:
1692 case EXPR_UNARY_POSTFIX_INCREMENT:
1693 case EXPR_UNARY_POSTFIX_DECREMENT:
1694 case EXPR_UNARY_PREFIX_INCREMENT:
1695 case EXPR_UNARY_PREFIX_DECREMENT:
1696 case EXPR_UNARY_ASSUME: /* has VOID type */
1697 case EXPR_UNARY_DEREFERENCE:
1698 case EXPR_BINARY_ASSIGN:
1699 case EXPR_BINARY_MUL_ASSIGN:
1700 case EXPR_BINARY_DIV_ASSIGN:
1701 case EXPR_BINARY_MOD_ASSIGN:
1702 case EXPR_BINARY_ADD_ASSIGN:
1703 case EXPR_BINARY_SUB_ASSIGN:
1704 case EXPR_BINARY_SHIFTLEFT_ASSIGN:
1705 case EXPR_BINARY_SHIFTRIGHT_ASSIGN:
1706 case EXPR_BINARY_BITWISE_AND_ASSIGN:
1707 case EXPR_BINARY_BITWISE_XOR_ASSIGN:
1708 case EXPR_BINARY_BITWISE_OR_ASSIGN:
1709 case EXPR_BINARY_COMMA:
1712 case EXPR_UNARY_TAKE_ADDRESS:
1713 return is_object_with_constant_address(expression->unary.value);
1716 return is_builtin_const_call(expression);
1718 case EXPR_UNARY_NEGATE:
1719 case EXPR_UNARY_PLUS:
1720 case EXPR_UNARY_BITWISE_NEGATE:
1721 case EXPR_UNARY_NOT:
1722 return is_constant_expression(expression->unary.value);
1724 case EXPR_UNARY_CAST:
1725 case EXPR_UNARY_CAST_IMPLICIT:
1726 return is_type_arithmetic(skip_typeref(expression->base.type))
1727 && is_constant_expression(expression->unary.value);
1729 case EXPR_BINARY_ADD:
1730 case EXPR_BINARY_SUB:
1731 case EXPR_BINARY_MUL:
1732 case EXPR_BINARY_DIV:
1733 case EXPR_BINARY_MOD:
1734 case EXPR_BINARY_EQUAL:
1735 case EXPR_BINARY_NOTEQUAL:
1736 case EXPR_BINARY_LESS:
1737 case EXPR_BINARY_LESSEQUAL:
1738 case EXPR_BINARY_GREATER:
1739 case EXPR_BINARY_GREATEREQUAL:
1740 case EXPR_BINARY_BITWISE_AND:
1741 case EXPR_BINARY_BITWISE_OR:
1742 case EXPR_BINARY_BITWISE_XOR:
1743 case EXPR_BINARY_LOGICAL_AND:
1744 case EXPR_BINARY_LOGICAL_OR:
1745 case EXPR_BINARY_SHIFTLEFT:
1746 case EXPR_BINARY_SHIFTRIGHT:
1747 case EXPR_BINARY_BUILTIN_EXPECT:
1748 case EXPR_BINARY_ISGREATER:
1749 case EXPR_BINARY_ISGREATEREQUAL:
1750 case EXPR_BINARY_ISLESS:
1751 case EXPR_BINARY_ISLESSEQUAL:
1752 case EXPR_BINARY_ISLESSGREATER:
1753 case EXPR_BINARY_ISUNORDERED:
1754 return is_constant_expression(expression->binary.left)
1755 && is_constant_expression(expression->binary.right);
1757 case EXPR_COMPOUND_LITERAL:
1758 return is_constant_initializer(expression->compound_literal.initializer);
1760 case EXPR_CONDITIONAL: {
1761 expression_t *condition = expression->conditional.condition;
1762 if(!is_constant_expression(condition))
1765 long val = fold_constant(condition);
1767 return is_constant_expression(expression->conditional.true_expression);
1769 return is_constant_expression(expression->conditional.false_expression);
1772 case EXPR_ARRAY_ACCESS:
1773 return is_constant_expression(expression->array_access.array_ref)
1774 && is_constant_expression(expression->array_access.index);
1776 case EXPR_REFERENCE: {
1777 declaration_t *declaration = expression->reference.declaration;
1778 if(declaration->storage_class == STORAGE_CLASS_ENUM_ENTRY)
1790 panic("invalid expression found (is constant expression)");
1794 * Initialize the AST construction.
1798 obstack_init(&ast_obstack);
1806 obstack_free(&ast_obstack, NULL);
1810 * Set the output stream for the AST printer.
1812 * @param stream the output stream
1814 void ast_set_output(FILE *stream)
1817 type_set_output(stream);
1821 * Allocate an AST object of the given size.
1823 * @param size the size of the object to allocate
1825 * @return A new allocated object in the AST memeory space.
1827 void *(allocate_ast)(size_t size)
1829 return _allocate_ast(size);