fix one more problematic omitted conditional case
[cparser] / ast.c
1 /*
2  * This file is part of cparser.
3  * Copyright (C) 2007-2008 Matthias Braun <matze@braunis.de>
4  *
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.
9  *
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.
14  *
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
18  * 02111-1307, USA.
19  */
20 #include <config.h>
21
22 #include "ast_t.h"
23 #include "symbol_t.h"
24 #include "type_t.h"
25 #include "parser.h"
26 #include "lang_features.h"
27 #include "entity_t.h"
28
29 #include <assert.h>
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <ctype.h>
33
34 #ifdef __INTEL_COMPILER
35 #include <mathimf.h>
36 #else
37 #include <math.h>
38 #endif
39
40 #include "adt/error.h"
41
42 struct obstack ast_obstack;
43
44 static FILE *out;
45 static int   indent;
46
47 /** If set, implicit casts are printed. */
48 bool print_implicit_casts = false;
49
50 /** If set parenthesis are printed to indicate operator precedence. */
51 bool print_parenthesis = false;
52
53 static void print_statement(const statement_t *statement);
54 static void print_expression_prec(const expression_t *expression, unsigned prec);
55
56 void change_indent(int delta)
57 {
58         indent += delta;
59         assert(indent >= 0);
60 }
61
62 void print_indent(void)
63 {
64         for (int i = 0; i < indent; ++i)
65                 fputc('\t', out);
66 }
67
68 /**
69  * Returns 1 if a given precedence level has right-to-left
70  * associativity, else 0.
71  *
72  * @param precedence   the operator precedence
73  */
74 static int right_to_left(unsigned precedence)
75 {
76         switch (precedence) {
77                 case PREC_ASSIGNMENT:
78                 case PREC_CONDITIONAL:
79                 case PREC_UNARY:
80                         return 1;
81
82                 default:
83                         return 0;
84         }
85 }
86
87 /**
88  * Return the precedence of an expression given by its kind.
89  *
90  * @param kind   the expression kind
91  */
92 static unsigned get_expression_precedence(expression_kind_t kind)
93 {
94         static const unsigned prec[] = {
95                 [EXPR_UNKNOWN]                   = PREC_PRIMARY,
96                 [EXPR_INVALID]                   = PREC_PRIMARY,
97                 [EXPR_REFERENCE]                 = PREC_PRIMARY,
98                 [EXPR_REFERENCE_ENUM_VALUE]      = PREC_PRIMARY,
99                 [EXPR_CHARACTER_CONSTANT]        = PREC_PRIMARY,
100                 [EXPR_WIDE_CHARACTER_CONSTANT]   = PREC_PRIMARY,
101                 [EXPR_CONST]                     = PREC_PRIMARY,
102                 [EXPR_STRING_LITERAL]            = PREC_PRIMARY,
103                 [EXPR_WIDE_STRING_LITERAL]       = PREC_PRIMARY,
104                 [EXPR_COMPOUND_LITERAL]          = PREC_UNARY,
105                 [EXPR_CALL]                      = PREC_POSTFIX,
106                 [EXPR_CONDITIONAL]               = PREC_CONDITIONAL,
107                 [EXPR_SELECT]                    = PREC_POSTFIX,
108                 [EXPR_ARRAY_ACCESS]              = PREC_POSTFIX,
109                 [EXPR_SIZEOF]                    = PREC_UNARY,
110                 [EXPR_CLASSIFY_TYPE]             = PREC_UNARY,
111                 [EXPR_ALIGNOF]                   = PREC_UNARY,
112
113                 [EXPR_FUNCNAME]                  = PREC_PRIMARY,
114                 [EXPR_BUILTIN_SYMBOL]            = PREC_PRIMARY,
115                 [EXPR_BUILTIN_CONSTANT_P]        = PREC_PRIMARY,
116                 [EXPR_BUILTIN_PREFETCH]          = PREC_PRIMARY,
117                 [EXPR_OFFSETOF]                  = PREC_PRIMARY,
118                 [EXPR_VA_START]                  = PREC_PRIMARY,
119                 [EXPR_VA_ARG]                    = PREC_PRIMARY,
120                 [EXPR_STATEMENT]                 = PREC_PRIMARY,
121                 [EXPR_LABEL_ADDRESS]             = PREC_PRIMARY,
122
123                 [EXPR_UNARY_NEGATE]              = PREC_UNARY,
124                 [EXPR_UNARY_PLUS]                = PREC_UNARY,
125                 [EXPR_UNARY_BITWISE_NEGATE]      = PREC_UNARY,
126                 [EXPR_UNARY_NOT]                 = PREC_UNARY,
127                 [EXPR_UNARY_DEREFERENCE]         = PREC_UNARY,
128                 [EXPR_UNARY_TAKE_ADDRESS]        = PREC_UNARY,
129                 [EXPR_UNARY_POSTFIX_INCREMENT]   = PREC_POSTFIX,
130                 [EXPR_UNARY_POSTFIX_DECREMENT]   = PREC_POSTFIX,
131                 [EXPR_UNARY_PREFIX_INCREMENT]    = PREC_UNARY,
132                 [EXPR_UNARY_PREFIX_DECREMENT]    = PREC_UNARY,
133                 [EXPR_UNARY_CAST]                = PREC_UNARY,
134                 [EXPR_UNARY_CAST_IMPLICIT]       = PREC_UNARY,
135                 [EXPR_UNARY_ASSUME]              = PREC_PRIMARY,
136                 [EXPR_UNARY_DELETE]              = PREC_UNARY,
137                 [EXPR_UNARY_DELETE_ARRAY]        = PREC_UNARY,
138                 [EXPR_UNARY_THROW]               = PREC_ASSIGNMENT,
139
140                 [EXPR_BINARY_ADD]                = PREC_ADDITIVE,
141                 [EXPR_BINARY_SUB]                = PREC_ADDITIVE,
142                 [EXPR_BINARY_MUL]                = PREC_MULTIPLICATIVE,
143                 [EXPR_BINARY_DIV]                = PREC_MULTIPLICATIVE,
144                 [EXPR_BINARY_MOD]                = PREC_MULTIPLICATIVE,
145                 [EXPR_BINARY_EQUAL]              = PREC_EQUALITY,
146                 [EXPR_BINARY_NOTEQUAL]           = PREC_EQUALITY,
147                 [EXPR_BINARY_LESS]               = PREC_RELATIONAL,
148                 [EXPR_BINARY_LESSEQUAL]          = PREC_RELATIONAL,
149                 [EXPR_BINARY_GREATER]            = PREC_RELATIONAL,
150                 [EXPR_BINARY_GREATEREQUAL]       = PREC_RELATIONAL,
151                 [EXPR_BINARY_BITWISE_AND]        = PREC_AND,
152                 [EXPR_BINARY_BITWISE_OR]         = PREC_OR,
153                 [EXPR_BINARY_BITWISE_XOR]        = PREC_XOR,
154                 [EXPR_BINARY_LOGICAL_AND]        = PREC_LOGICAL_AND,
155                 [EXPR_BINARY_LOGICAL_OR]         = PREC_LOGICAL_OR,
156                 [EXPR_BINARY_SHIFTLEFT]          = PREC_SHIFT,
157                 [EXPR_BINARY_SHIFTRIGHT]         = PREC_SHIFT,
158                 [EXPR_BINARY_ASSIGN]             = PREC_ASSIGNMENT,
159                 [EXPR_BINARY_MUL_ASSIGN]         = PREC_ASSIGNMENT,
160                 [EXPR_BINARY_DIV_ASSIGN]         = PREC_ASSIGNMENT,
161                 [EXPR_BINARY_MOD_ASSIGN]         = PREC_ASSIGNMENT,
162                 [EXPR_BINARY_ADD_ASSIGN]         = PREC_ASSIGNMENT,
163                 [EXPR_BINARY_SUB_ASSIGN]         = PREC_ASSIGNMENT,
164                 [EXPR_BINARY_SHIFTLEFT_ASSIGN]   = PREC_ASSIGNMENT,
165                 [EXPR_BINARY_SHIFTRIGHT_ASSIGN]  = PREC_ASSIGNMENT,
166                 [EXPR_BINARY_BITWISE_AND_ASSIGN] = PREC_ASSIGNMENT,
167                 [EXPR_BINARY_BITWISE_XOR_ASSIGN] = PREC_ASSIGNMENT,
168                 [EXPR_BINARY_BITWISE_OR_ASSIGN]  = PREC_ASSIGNMENT,
169                 [EXPR_BINARY_COMMA]              = PREC_EXPRESSION,
170
171                 [EXPR_BINARY_BUILTIN_EXPECT]     = PREC_PRIMARY,
172                 [EXPR_BINARY_ISGREATER]          = PREC_PRIMARY,
173                 [EXPR_BINARY_ISGREATEREQUAL]     = PREC_PRIMARY,
174                 [EXPR_BINARY_ISLESS]             = PREC_PRIMARY,
175                 [EXPR_BINARY_ISLESSEQUAL]        = PREC_PRIMARY,
176                 [EXPR_BINARY_ISLESSGREATER]      = PREC_PRIMARY,
177                 [EXPR_BINARY_ISUNORDERED]        = PREC_PRIMARY
178         };
179         assert((unsigned)kind < (sizeof(prec)/sizeof(prec[0])));
180         unsigned res = prec[kind];
181
182         assert(res != PREC_BOTTOM);
183         return res;
184 }
185
186 /**
187  * Print a constant expression.
188  *
189  * @param cnst  the constant expression
190  */
191 static void print_const(const const_expression_t *cnst)
192 {
193         if (cnst->base.type == NULL)
194                 return;
195
196         const type_t *const type = skip_typeref(cnst->base.type);
197
198         if (is_type_atomic(type, ATOMIC_TYPE_BOOL)) {
199                 fputs(cnst->v.int_value ? "true" : "false", out);
200         } else 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 #ifdef _WIN32
205                 /* ARG, no way to print long double */
206                 fprintf(out, "%.20g", (double)val);
207 #else
208                 fprintf(out, "%.20Lg", val);
209 #endif
210                 if (isfinite(val) && truncl(val) == val)
211                         fputs(".0", out);
212         } else {
213                 panic("unknown constant");
214         }
215
216         char const* suffix;
217         switch (type->atomic.akind) {
218                 case ATOMIC_TYPE_UINT:        suffix = "U";   break;
219                 case ATOMIC_TYPE_LONG:        suffix = "L";   break;
220                 case ATOMIC_TYPE_ULONG:       suffix = "UL";  break;
221                 case ATOMIC_TYPE_LONGLONG:    suffix = "LL";  break;
222                 case ATOMIC_TYPE_ULONGLONG:   suffix = "ULL"; break;
223                 case ATOMIC_TYPE_FLOAT:       suffix = "F";   break;
224                 case ATOMIC_TYPE_LONG_DOUBLE: suffix = "L";   break;
225
226                 default: return;
227         }
228         fputs(suffix, out);
229 }
230
231 /**
232  * Print a quoted string constant.
233  *
234  * @param string  the string constant
235  * @param border  the border char
236  * @param skip    number of chars to skip at the end
237  */
238 static void print_quoted_string(const string_t *const string, char border, int skip)
239 {
240         fputc(border, out);
241         const char *end = string->begin + string->size - skip;
242         for (const char *c = string->begin; c != end; ++c) {
243                 if (*c == border) {
244                         fputc('\\', out);
245                 }
246                 switch (*c) {
247                 case '\\':  fputs("\\\\", out); break;
248                 case '\a':  fputs("\\a", out); break;
249                 case '\b':  fputs("\\b", out); break;
250                 case '\f':  fputs("\\f", out); break;
251                 case '\n':  fputs("\\n", out); break;
252                 case '\r':  fputs("\\r", out); break;
253                 case '\t':  fputs("\\t", out); break;
254                 case '\v':  fputs("\\v", out); break;
255                 case '\?':  fputs("\\?", out); break;
256                 case 27:
257                         if (c_mode & _GNUC) {
258                                 fputs("\\e", out); break;
259                         }
260                         /* FALLTHROUGH */
261                 default:
262                         if (!isprint(*c)) {
263                                 fprintf(out, "\\%03o", (unsigned)*c);
264                                 break;
265                         }
266                         fputc(*c, out);
267                         break;
268                 }
269         }
270         fputc(border, out);
271 }
272
273 /**
274  * Prints a wide string literal expression.
275  *
276  * @param wstr    the wide string literal expression
277  * @param border  the border char
278  * @param skip    number of chars to skip at the end
279  */
280 static void print_quoted_wide_string(const wide_string_t *const wstr,
281                                      char border, int skip)
282 {
283         fputc('L', out);
284         fputc(border, out);
285         const wchar_rep_t *end = wstr->begin + wstr->size - skip;
286         for (const wchar_rep_t *c = wstr->begin; c != end; ++c) {
287                 switch (*c) {
288                         case L'\"':  fputs("\\\"", out); break;
289                         case L'\\':  fputs("\\\\", out); break;
290                         case L'\a':  fputs("\\a",  out); break;
291                         case L'\b':  fputs("\\b",  out); break;
292                         case L'\f':  fputs("\\f",  out); break;
293                         case L'\n':  fputs("\\n",  out); break;
294                         case L'\r':  fputs("\\r",  out); break;
295                         case L'\t':  fputs("\\t",  out); break;
296                         case L'\v':  fputs("\\v",  out); break;
297                         case L'\?':  fputs("\\?",  out); break;
298                         case 27:
299                                 if (c_mode & _GNUC) {
300                                         fputs("\\e", out); break;
301                                 }
302                                 /* FALLTHROUGH */
303                         default: {
304                                 const unsigned tc = *c;
305                                 if (tc < 0x80U) {
306                                         if (!isprint(*c)) {
307                                                 fprintf(out, "\\%03o", (char)*c);
308                                         } else {
309                                                 fputc(*c, out);
310                                         }
311                                 } else if (tc < 0x800) {
312                                         fputc(0xC0 | (tc >> 6),   out);
313                                         fputc(0x80 | (tc & 0x3F), out);
314                                 } else if (tc < 0x10000) {
315                                         fputc(0xE0 | ( tc >> 12),         out);
316                                         fputc(0x80 | ((tc >>  6) & 0x3F), out);
317                                         fputc(0x80 | ( tc        & 0x3F), out);
318                                 } else {
319                                         fputc(0xF0 | ( tc >> 18),         out);
320                                         fputc(0x80 | ((tc >> 12) & 0x3F), out);
321                                         fputc(0x80 | ((tc >>  6) & 0x3F), out);
322                                         fputc(0x80 | ( tc        & 0x3F), out);
323                                 }
324                         }
325                 }
326         }
327         fputc(border, out);
328 }
329
330 /**
331  * Print a constant character expression.
332  *
333  * @param cnst  the constant character expression
334  */
335 static void print_character_constant(const const_expression_t *cnst)
336 {
337         print_quoted_string(&cnst->v.character, '\'', 0);
338 }
339
340 static void print_wide_character_constant(const const_expression_t *cnst)
341 {
342         print_quoted_wide_string(&cnst->v.wide_character, '\'', 0);
343 }
344
345 /**
346  * Prints a string literal expression.
347  *
348  * @param string_literal  the string literal expression
349  */
350 static void print_string_literal(
351                 const string_literal_expression_t *string_literal)
352 {
353         print_quoted_string(&string_literal->value, '"', 1);
354 }
355
356 /**
357  * Prints a predefined symbol.
358  */
359 static void print_funcname(const funcname_expression_t *funcname)
360 {
361         const char *s = "";
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;
367         }
368         fputs(s, out);
369 }
370
371 static void print_wide_string_literal(
372         const wide_string_literal_expression_t *const wstr)
373 {
374         print_quoted_wide_string(&wstr->value, '"', 1);
375 }
376
377 static void print_compound_literal(
378                 const compound_literal_expression_t *expression)
379 {
380         fputc('(', out);
381         print_type(expression->type);
382         fputc(')', out);
383         print_initializer(expression->initializer);
384 }
385
386 /**
387  * Prints a call expression.
388  *
389  * @param call  the call expression
390  */
391 static void print_call_expression(const call_expression_t *call)
392 {
393         unsigned prec = get_expression_precedence(call->base.kind);
394         print_expression_prec(call->function, prec);
395         fputc('(', out);
396         call_argument_t *argument = call->arguments;
397         int              first    = 1;
398         while (argument != NULL) {
399                 if (!first) {
400                         fputs(", ", out);
401                 } else {
402                         first = 0;
403                 }
404                 print_expression_prec(argument->expression, PREC_ASSIGNMENT);
405
406                 argument = argument->next;
407         }
408         fputc(')', out);
409 }
410
411 /**
412  * Prints a binary expression.
413  *
414  * @param binexpr   the binary expression
415  */
416 static void print_binary_expression(const binary_expression_t *binexpr)
417 {
418         unsigned prec = get_expression_precedence(binexpr->base.kind);
419         int      r2l  = right_to_left(prec);
420
421         if (binexpr->base.kind == EXPR_BINARY_BUILTIN_EXPECT) {
422                 fputs("__builtin_expect(", out);
423                 print_expression_prec(binexpr->left, prec);
424                 fputs(", ", out);
425                 print_expression_prec(binexpr->right, prec);
426                 fputc(')', out);
427                 return;
428         }
429
430         print_expression_prec(binexpr->left, prec + r2l);
431         char const* op;
432         switch (binexpr->base.kind) {
433         case EXPR_BINARY_COMMA:              op = ", ";    break;
434         case EXPR_BINARY_ASSIGN:             op = " = ";   break;
435         case EXPR_BINARY_ADD:                op = " + ";   break;
436         case EXPR_BINARY_SUB:                op = " - ";   break;
437         case EXPR_BINARY_MUL:                op = " * ";   break;
438         case EXPR_BINARY_MOD:                op = " % ";   break;
439         case EXPR_BINARY_DIV:                op = " / ";   break;
440         case EXPR_BINARY_BITWISE_OR:         op = " | ";   break;
441         case EXPR_BINARY_BITWISE_AND:        op = " & ";   break;
442         case EXPR_BINARY_BITWISE_XOR:        op = " ^ ";   break;
443         case EXPR_BINARY_LOGICAL_OR:         op = " || ";  break;
444         case EXPR_BINARY_LOGICAL_AND:        op = " && ";  break;
445         case EXPR_BINARY_NOTEQUAL:           op = " != ";  break;
446         case EXPR_BINARY_EQUAL:              op = " == ";  break;
447         case EXPR_BINARY_LESS:               op = " < ";   break;
448         case EXPR_BINARY_LESSEQUAL:          op = " <= ";  break;
449         case EXPR_BINARY_GREATER:            op = " > ";   break;
450         case EXPR_BINARY_GREATEREQUAL:       op = " >= ";  break;
451         case EXPR_BINARY_SHIFTLEFT:          op = " << ";  break;
452         case EXPR_BINARY_SHIFTRIGHT:         op = " >> ";  break;
453
454         case EXPR_BINARY_ADD_ASSIGN:         op = " += ";  break;
455         case EXPR_BINARY_SUB_ASSIGN:         op = " -= ";  break;
456         case EXPR_BINARY_MUL_ASSIGN:         op = " *= ";  break;
457         case EXPR_BINARY_MOD_ASSIGN:         op = " %= ";  break;
458         case EXPR_BINARY_DIV_ASSIGN:         op = " /= ";  break;
459         case EXPR_BINARY_BITWISE_OR_ASSIGN:  op = " |= ";  break;
460         case EXPR_BINARY_BITWISE_AND_ASSIGN: op = " &= ";  break;
461         case EXPR_BINARY_BITWISE_XOR_ASSIGN: op = " ^= ";  break;
462         case EXPR_BINARY_SHIFTLEFT_ASSIGN:   op = " <<= "; break;
463         case EXPR_BINARY_SHIFTRIGHT_ASSIGN:  op = " >>= "; break;
464         default: panic("invalid binexpression found");
465         }
466         fputs(op, out);
467         print_expression_prec(binexpr->right, prec + 1 - r2l);
468 }
469
470 /**
471  * Prints an unary expression.
472  *
473  * @param unexpr   the unary expression
474  */
475 static void print_unary_expression(const unary_expression_t *unexpr)
476 {
477         unsigned prec = get_expression_precedence(unexpr->base.kind);
478         switch (unexpr->base.kind) {
479         case EXPR_UNARY_NEGATE:           fputc('-',          out); break;
480         case EXPR_UNARY_PLUS:             fputc('+',          out); break;
481         case EXPR_UNARY_NOT:              fputc('!',          out); break;
482         case EXPR_UNARY_BITWISE_NEGATE:   fputc('~',          out); break;
483         case EXPR_UNARY_PREFIX_INCREMENT: fputs("++",         out); break;
484         case EXPR_UNARY_PREFIX_DECREMENT: fputs("--",         out); break;
485         case EXPR_UNARY_DEREFERENCE:      fputc('*',          out); break;
486         case EXPR_UNARY_TAKE_ADDRESS:     fputc('&',          out); break;
487         case EXPR_UNARY_DELETE:           fputs("delete ",    out); break;
488         case EXPR_UNARY_DELETE_ARRAY:     fputs("delete [] ", out); break;
489
490         case EXPR_UNARY_POSTFIX_INCREMENT:
491                 print_expression_prec(unexpr->value, prec);
492                 fputs("++", out);
493                 return;
494         case EXPR_UNARY_POSTFIX_DECREMENT:
495                 print_expression_prec(unexpr->value, prec);
496                 fputs("--", out);
497                 return;
498         case EXPR_UNARY_CAST_IMPLICIT:
499         case EXPR_UNARY_CAST:
500                 fputc('(', out);
501                 print_type(unexpr->base.type);
502                 fputc(')', out);
503                 break;
504         case EXPR_UNARY_ASSUME:
505                 fputs("__assume(", out);
506                 print_expression_prec(unexpr->value, PREC_ASSIGNMENT);
507                 fputc(')', out);
508                 return;
509
510         case EXPR_UNARY_THROW:
511                 if (unexpr->value == NULL) {
512                         fputs("throw", out);
513                         return;
514                 }
515                 fputs("throw ", out);
516                 break;
517
518         default:
519                 panic("invalid unary expression found");
520         }
521         print_expression_prec(unexpr->value, prec);
522 }
523
524 /**
525  * Prints a reference expression.
526  *
527  * @param ref   the reference expression
528  */
529 static void print_reference_expression(const reference_expression_t *ref)
530 {
531         fputs(ref->entity->base.symbol->string, out);
532 }
533
534 /**
535  * Prints a label address expression.
536  *
537  * @param ref   the reference expression
538  */
539 static void print_label_address_expression(const label_address_expression_t *le)
540 {
541         fprintf(out, "&&%s", le->label->base.symbol->string);
542 }
543
544 /**
545  * Prints an array expression.
546  *
547  * @param expression   the array expression
548  */
549 static void print_array_expression(const array_access_expression_t *expression)
550 {
551         unsigned prec = get_expression_precedence(expression->base.kind);
552         if (!expression->flipped) {
553                 print_expression_prec(expression->array_ref, prec);
554                 fputc('[', out);
555                 print_expression(expression->index);
556                 fputc(']', out);
557         } else {
558                 print_expression_prec(expression->index, prec);
559                 fputc('[', out);
560                 print_expression(expression->array_ref);
561                 fputc(']', out);
562         }
563 }
564
565 /**
566  * Prints a typeproperty expression (sizeof or __alignof__).
567  *
568  * @param expression   the type property expression
569  */
570 static void print_typeprop_expression(const typeprop_expression_t *expression)
571 {
572         if (expression->base.kind == EXPR_SIZEOF) {
573                 fputs("sizeof", out);
574         } else {
575                 assert(expression->base.kind == EXPR_ALIGNOF);
576                 fputs("__alignof__", out);
577         }
578         if (expression->tp_expression != NULL) {
579                 /* always print the '()' here, sizeof x is right but unusual */
580                 fputc('(', out);
581                 print_expression(expression->tp_expression);
582                 fputc(')', out);
583         } else {
584                 fputc('(', out);
585                 print_type(expression->type);
586                 fputc(')', out);
587         }
588 }
589
590 /**
591  * Prints an builtin symbol.
592  *
593  * @param expression   the builtin symbol expression
594  */
595 static void print_builtin_symbol(const builtin_symbol_expression_t *expression)
596 {
597         fputs(expression->symbol->string, out);
598 }
599
600 /**
601  * Prints a builtin constant expression.
602  *
603  * @param expression   the builtin constant expression
604  */
605 static void print_builtin_constant(const builtin_constant_expression_t *expression)
606 {
607         fputs("__builtin_constant_p(", out);
608         print_expression_prec(expression->value, PREC_ASSIGNMENT);
609         fputc(')', out);
610 }
611
612 /**
613  * Prints a builtin prefetch expression.
614  *
615  * @param expression   the builtin prefetch expression
616  */
617 static void print_builtin_prefetch(const builtin_prefetch_expression_t *expression)
618 {
619         fputs("__builtin_prefetch(", out);
620         print_expression_prec(expression->adr, PREC_ASSIGNMENT);
621         if (expression->rw) {
622                 fputc(',', out);
623                 print_expression_prec(expression->rw, PREC_ASSIGNMENT);
624         }
625         if (expression->locality) {
626                 fputc(',', out);
627                 print_expression_prec(expression->locality, PREC_ASSIGNMENT);
628         }
629         fputc(')', out);
630 }
631
632 /**
633  * Prints a conditional expression.
634  *
635  * @param expression   the conditional expression
636  */
637 static void print_conditional(const conditional_expression_t *expression)
638 {
639         print_expression_prec(expression->condition, PREC_LOGICAL_OR);
640         fputs(" ? ", out);
641         if (expression->true_expression != NULL) {
642                 print_expression_prec(expression->true_expression, PREC_EXPRESSION);
643                 fputs(" : ", out);
644         } else {
645                 fputs(": ", out);
646         }
647         precedence_t prec = c_mode & _CXX ? PREC_ASSIGNMENT : PREC_CONDITIONAL;
648         print_expression_prec(expression->false_expression, prec);
649 }
650
651 /**
652  * Prints a va_start expression.
653  *
654  * @param expression   the va_start expression
655  */
656 static void print_va_start(const va_start_expression_t *const expression)
657 {
658         fputs("__builtin_va_start(", out);
659         print_expression_prec(expression->ap, PREC_ASSIGNMENT);
660         fputs(", ", out);
661         fputs(expression->parameter->base.base.symbol->string, out);
662         fputc(')', out);
663 }
664
665 /**
666  * Prints a va_arg expression.
667  *
668  * @param expression   the va_arg expression
669  */
670 static void print_va_arg(const va_arg_expression_t *expression)
671 {
672         fputs("__builtin_va_arg(", out);
673         print_expression_prec(expression->ap, PREC_ASSIGNMENT);
674         fputs(", ", out);
675         print_type(expression->base.type);
676         fputc(')', out);
677 }
678
679 /**
680  * Prints a select expression (. or ->).
681  *
682  * @param expression   the select expression
683  */
684 static void print_select(const select_expression_t *expression)
685 {
686         unsigned prec = get_expression_precedence(expression->base.kind);
687         print_expression_prec(expression->compound, prec);
688         if (is_type_pointer(skip_typeref(expression->compound->base.type))) {
689                 fputs("->", out);
690         } else {
691                 fputc('.', out);
692         }
693         fputs(expression->compound_entry->base.symbol->string, out);
694 }
695
696 /**
697  * Prints a type classify expression.
698  *
699  * @param expr   the type classify expression
700  */
701 static void print_classify_type_expression(
702         const classify_type_expression_t *const expr)
703 {
704         fputs("__builtin_classify_type(", out);
705         print_expression_prec(expr->type_expression, PREC_ASSIGNMENT);
706         fputc(')', out);
707 }
708
709 /**
710  * Prints a designator.
711  *
712  * @param designator  the designator
713  */
714 static void print_designator(const designator_t *designator)
715 {
716         for ( ; designator != NULL; designator = designator->next) {
717                 if (designator->symbol == NULL) {
718                         fputc('[', out);
719                         print_expression(designator->array_index);
720                         fputc(']', out);
721                 } else {
722                         fputc('.', out);
723                         fputs(designator->symbol->string, out);
724                 }
725         }
726 }
727
728 /**
729  * Prints an offsetof expression.
730  *
731  * @param expression   the offset expression
732  */
733 static void print_offsetof_expression(const offsetof_expression_t *expression)
734 {
735         fputs("__builtin_offsetof", out);
736         fputc('(', out);
737         print_type(expression->type);
738         fputc(',', out);
739         print_designator(expression->designator);
740         fputc(')', out);
741 }
742
743 /**
744  * Prints a statement expression.
745  *
746  * @param expression   the statement expression
747  */
748 static void print_statement_expression(const statement_expression_t *expression)
749 {
750         fputc('(', out);
751         print_statement(expression->statement);
752         fputc(')', out);
753 }
754
755 /**
756  * Prints an expression with parenthesis if needed.
757  *
758  * @param expression  the expression to print
759  * @param top_prec    the precedence of the user of this expression.
760  */
761 static void print_expression_prec(const expression_t *expression, unsigned top_prec)
762 {
763         if (expression->kind == EXPR_UNARY_CAST_IMPLICIT && !print_implicit_casts) {
764                 expression = expression->unary.value;
765         }
766         unsigned prec = get_expression_precedence(expression->base.kind);
767         if (print_parenthesis && top_prec != PREC_BOTTOM)
768                 top_prec = PREC_TOP;
769         if (top_prec > prec)
770                 fputc('(', out);
771         switch (expression->kind) {
772         case EXPR_UNKNOWN:
773         case EXPR_INVALID:
774                 fputs("$invalid expression$", out);
775                 break;
776         case EXPR_CHARACTER_CONSTANT:
777                 print_character_constant(&expression->conste);
778                 break;
779         case EXPR_WIDE_CHARACTER_CONSTANT:
780                 print_wide_character_constant(&expression->conste);
781                 break;
782         case EXPR_CONST:
783                 print_const(&expression->conste);
784                 break;
785         case EXPR_FUNCNAME:
786                 print_funcname(&expression->funcname);
787                 break;
788         case EXPR_STRING_LITERAL:
789                 print_string_literal(&expression->string);
790                 break;
791         case EXPR_WIDE_STRING_LITERAL:
792                 print_wide_string_literal(&expression->wide_string);
793                 break;
794         case EXPR_COMPOUND_LITERAL:
795                 print_compound_literal(&expression->compound_literal);
796                 break;
797         case EXPR_CALL:
798                 print_call_expression(&expression->call);
799                 break;
800         EXPR_BINARY_CASES
801                 print_binary_expression(&expression->binary);
802                 break;
803         case EXPR_REFERENCE:
804         case EXPR_REFERENCE_ENUM_VALUE:
805                 print_reference_expression(&expression->reference);
806                 break;
807         case EXPR_ARRAY_ACCESS:
808                 print_array_expression(&expression->array_access);
809                 break;
810         case EXPR_LABEL_ADDRESS:
811                 print_label_address_expression(&expression->label_address);
812                 break;
813         EXPR_UNARY_CASES
814                 print_unary_expression(&expression->unary);
815                 break;
816         case EXPR_SIZEOF:
817         case EXPR_ALIGNOF:
818                 print_typeprop_expression(&expression->typeprop);
819                 break;
820         case EXPR_BUILTIN_SYMBOL:
821                 print_builtin_symbol(&expression->builtin_symbol);
822                 break;
823         case EXPR_BUILTIN_CONSTANT_P:
824                 print_builtin_constant(&expression->builtin_constant);
825                 break;
826         case EXPR_BUILTIN_PREFETCH:
827                 print_builtin_prefetch(&expression->builtin_prefetch);
828                 break;
829         case EXPR_CONDITIONAL:
830                 print_conditional(&expression->conditional);
831                 break;
832         case EXPR_VA_START:
833                 print_va_start(&expression->va_starte);
834                 break;
835         case EXPR_VA_ARG:
836                 print_va_arg(&expression->va_arge);
837                 break;
838         case EXPR_SELECT:
839                 print_select(&expression->select);
840                 break;
841         case EXPR_CLASSIFY_TYPE:
842                 print_classify_type_expression(&expression->classify_type);
843                 break;
844         case EXPR_OFFSETOF:
845                 print_offsetof_expression(&expression->offsetofe);
846                 break;
847         case EXPR_STATEMENT:
848                 print_statement_expression(&expression->statement);
849                 break;
850
851         default:
852                 /* TODO */
853                 fprintf(out, "some expression of type %d", (int)expression->kind);
854                 break;
855         }
856         if (top_prec > prec)
857                 fputc(')', out);
858 }
859
860 /**
861  * Print an compound statement.
862  *
863  * @param block  the compound statement
864  */
865 static void print_compound_statement(const compound_statement_t *block)
866 {
867         fputs("{\n", out);
868         ++indent;
869
870         statement_t *statement = block->statements;
871         while (statement != NULL) {
872                 if (statement->base.kind == STATEMENT_CASE_LABEL)
873                         --indent;
874                 if (statement->kind != STATEMENT_LABEL)
875                         print_indent();
876                 print_statement(statement);
877
878                 statement = statement->base.next;
879         }
880         --indent;
881         print_indent();
882         fputs("}\n", out);
883 }
884
885 /**
886  * Print a return statement.
887  *
888  * @param statement  the return statement
889  */
890 static void print_return_statement(const return_statement_t *statement)
891 {
892         fputs("return ", out);
893         if (statement->value != NULL)
894                 print_expression(statement->value);
895         fputs(";\n", out);
896 }
897
898 /**
899  * Print an expression statement.
900  *
901  * @param statement  the expression statement
902  */
903 static void print_expression_statement(const expression_statement_t *statement)
904 {
905         print_expression(statement->expression);
906         fputs(";\n", out);
907 }
908
909 /**
910  * Print a goto statement.
911  *
912  * @param statement  the goto statement
913  */
914 static void print_goto_statement(const goto_statement_t *statement)
915 {
916         fputs("goto ", out);
917         if (statement->expression != NULL) {
918                 fputc('*', out);
919                 print_expression(statement->expression);
920         } else {
921                 fputs(statement->label->base.symbol->string, out);
922         }
923         fputs(";\n", out);
924 }
925
926 /**
927  * Print a label statement.
928  *
929  * @param statement  the label statement
930  */
931 static void print_label_statement(const label_statement_t *statement)
932 {
933         fprintf(out, "%s:\n", statement->label->base.symbol->string);
934         print_indent();
935         print_statement(statement->statement);
936 }
937
938 /**
939  * Print an if statement.
940  *
941  * @param statement  the if statement
942  */
943 static void print_if_statement(const if_statement_t *statement)
944 {
945         fputs("if (", out);
946         print_expression(statement->condition);
947         fputs(") ", out);
948         print_statement(statement->true_statement);
949
950         if (statement->false_statement != NULL) {
951                 print_indent();
952                 fputs("else ", out);
953                 print_statement(statement->false_statement);
954         }
955 }
956
957 /**
958  * Print a switch statement.
959  *
960  * @param statement  the switch statement
961  */
962 static void print_switch_statement(const switch_statement_t *statement)
963 {
964         fputs("switch (", out);
965         print_expression(statement->expression);
966         fputs(") ", out);
967         print_statement(statement->body);
968 }
969
970 /**
971  * Print a case label (including the default label).
972  *
973  * @param statement  the case label statement
974  */
975 static void print_case_label(const case_label_statement_t *statement)
976 {
977         if (statement->expression == NULL) {
978                 fputs("default:\n", out);
979         } else {
980                 fputs("case ", out);
981                 print_expression(statement->expression);
982                 if (statement->end_range != NULL) {
983                         fputs(" ... ", out);
984                         print_expression(statement->end_range);
985                 }
986                 fputs(":\n", out);
987         }
988         ++indent;
989         if (statement->statement != NULL) {
990                 if (statement->statement->base.kind == STATEMENT_CASE_LABEL) {
991                         --indent;
992                 }
993                 print_indent();
994                 print_statement(statement->statement);
995         }
996 }
997
998 static void print_local_label(const local_label_statement_t *statement)
999 {
1000         fputs("__label__ ", out);
1001
1002         bool      first  = true;
1003         entity_t *entity = statement->labels_begin;
1004         for (;
1005                 entity != statement->labels_end->base.next;
1006                 entity = entity->base.next) {
1007                 if (!first) {
1008                         fputs(", ", out);
1009                 } else {
1010                         first = false;
1011                 }
1012                 fputs(entity->base.symbol->string, out);
1013         }
1014         fputs(";\n", out);
1015 }
1016
1017 static void print_typedef(const entity_t *entity)
1018 {
1019         fputs("typedef ", out);
1020         print_type_ext(entity->typedefe.type, entity->base.symbol, NULL);
1021         fputs(";", out);
1022 }
1023
1024 /**
1025  * returns true if the entity is a compiler generated one and has no real
1026  * correspondenc in the source file
1027  */
1028 static bool is_generated_entity(const entity_t *entity)
1029 {
1030         if (entity->kind == ENTITY_TYPEDEF)
1031                 return entity->typedefe.builtin;
1032
1033         if (is_declaration(entity))
1034                 return entity->declaration.implicit;
1035
1036         return false;
1037 }
1038
1039 /**
1040  * Print a declaration statement.
1041  *
1042  * @param statement   the statement
1043  */
1044 static void print_declaration_statement(
1045                 const declaration_statement_t *statement)
1046 {
1047         bool first = true;
1048         entity_t *entity = statement->declarations_begin;
1049         for (;
1050                  entity != statement->declarations_end->base.next;
1051                  entity = entity->base.next) {
1052                 if (!is_declaration(entity) && entity->kind != ENTITY_TYPEDEF)
1053                         continue;
1054                 if (is_generated_entity(entity))
1055                         continue;
1056
1057                 if (!first) {
1058                         print_indent();
1059                 } else {
1060                         first = false;
1061                 }
1062
1063                 if (entity->kind == ENTITY_TYPEDEF) {
1064                         print_typedef(entity);
1065                 } else {
1066                         assert(is_declaration(entity));
1067                         print_declaration(entity);
1068                 }
1069
1070                 fputc('\n', out);
1071         }
1072 }
1073
1074 /**
1075  * Print a while statement.
1076  *
1077  * @param statement   the statement
1078  */
1079 static void print_while_statement(const while_statement_t *statement)
1080 {
1081         fputs("while (", out);
1082         print_expression(statement->condition);
1083         fputs(") ", out);
1084         print_statement(statement->body);
1085 }
1086
1087 /**
1088  * Print a do-while statement.
1089  *
1090  * @param statement   the statement
1091  */
1092 static void print_do_while_statement(const do_while_statement_t *statement)
1093 {
1094         fputs("do ", out);
1095         print_statement(statement->body);
1096         print_indent();
1097         fputs("while (", out);
1098         print_expression(statement->condition);
1099         fputs(");\n", out);
1100 }
1101
1102 /**
1103  * Print a for statement.
1104  *
1105  * @param statement   the statement
1106  */
1107 static void print_for_statement(const for_statement_t *statement)
1108 {
1109         fputs("for (", out);
1110         entity_t *entity = statement->scope.entities;
1111         while (entity != NULL && is_generated_entity(entity))
1112                 entity = entity->base.next;
1113
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");
1120                 }
1121                 fputc(' ', out);
1122         } else {
1123                 if (statement->initialisation) {
1124                         print_expression(statement->initialisation);
1125                 }
1126                 fputs("; ", out);
1127         }
1128         if (statement->condition != NULL) {
1129                 print_expression(statement->condition);
1130         }
1131         fputs("; ", out);
1132         if (statement->step != NULL) {
1133                 print_expression(statement->step);
1134         }
1135         fputs(") ", out);
1136         print_statement(statement->body);
1137 }
1138
1139 /**
1140  * Print assembler arguments.
1141  *
1142  * @param arguments   the arguments
1143  */
1144 static void print_asm_arguments(asm_argument_t *arguments)
1145 {
1146         asm_argument_t *argument = arguments;
1147         for (; argument != NULL; argument = argument->next) {
1148                 if (argument != arguments)
1149                         fputs(", ", out);
1150
1151                 if (argument->symbol) {
1152                         fprintf(out, "[%s] ", argument->symbol->string);
1153                 }
1154                 print_quoted_string(&argument->constraints, '"', 1);
1155                 fputs(" (", out);
1156                 print_expression(argument->expression);
1157                 fputc(')', out);
1158         }
1159 }
1160
1161 /**
1162  * Print assembler clobbers.
1163  *
1164  * @param clobbers   the clobbers
1165  */
1166 static void print_asm_clobbers(asm_clobber_t *clobbers)
1167 {
1168         asm_clobber_t *clobber = clobbers;
1169         for (; clobber != NULL; clobber = clobber->next) {
1170                 if (clobber != clobbers)
1171                         fputs(", ", out);
1172
1173                 print_quoted_string(&clobber->clobber, '"', 1);
1174         }
1175 }
1176
1177 /**
1178  * Print an assembler statement.
1179  *
1180  * @param statement   the statement
1181  */
1182 static void print_asm_statement(const asm_statement_t *statement)
1183 {
1184         fputs("asm ", out);
1185         if (statement->is_volatile) {
1186                 fputs("volatile ", out);
1187         }
1188         fputc('(', 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;
1194
1195         fputs(" : ", out);
1196         print_asm_arguments(statement->outputs);
1197         if (statement->inputs == NULL && statement->clobbers == NULL)
1198                 goto end_of_print_asm_statement;
1199
1200         fputs(" : ", out);
1201         print_asm_arguments(statement->inputs);
1202         if (statement->clobbers == NULL)
1203                 goto end_of_print_asm_statement;
1204
1205         fputs(" : ", out);
1206         print_asm_clobbers(statement->clobbers);
1207
1208 end_of_print_asm_statement:
1209         fputs(");\n", out);
1210 }
1211
1212 /**
1213  * Print a microsoft __try statement.
1214  *
1215  * @param statement   the statement
1216  */
1217 static void print_ms_try_statement(const ms_try_statement_t *statement)
1218 {
1219         fputs("__try ", out);
1220         print_statement(statement->try_statement);
1221         print_indent();
1222         if (statement->except_expression != NULL) {
1223                 fputs("__except(", out);
1224                 print_expression(statement->except_expression);
1225                 fputs(") ", out);
1226         } else {
1227                 fputs("__finally ", out);
1228         }
1229         print_statement(statement->final_statement);
1230 }
1231
1232 /**
1233  * Print a microsoft __leave statement.
1234  *
1235  * @param statement   the statement
1236  */
1237 static void print_leave_statement(const leave_statement_t *statement)
1238 {
1239         (void)statement;
1240         fputs("__leave;\n", out);
1241 }
1242
1243 /**
1244  * Print a statement.
1245  *
1246  * @param statement   the statement
1247  */
1248 void print_statement(const statement_t *statement)
1249 {
1250         switch (statement->kind) {
1251         case STATEMENT_EMPTY:
1252                 fputs(";\n", out);
1253                 break;
1254         case STATEMENT_COMPOUND:
1255                 print_compound_statement(&statement->compound);
1256                 break;
1257         case STATEMENT_RETURN:
1258                 print_return_statement(&statement->returns);
1259                 break;
1260         case STATEMENT_EXPRESSION:
1261                 print_expression_statement(&statement->expression);
1262                 break;
1263         case STATEMENT_LABEL:
1264                 print_label_statement(&statement->label);
1265                 break;
1266         case STATEMENT_LOCAL_LABEL:
1267                 print_local_label(&statement->local_label);
1268                 break;
1269         case STATEMENT_GOTO:
1270                 print_goto_statement(&statement->gotos);
1271                 break;
1272         case STATEMENT_CONTINUE:
1273                 fputs("continue;\n", out);
1274                 break;
1275         case STATEMENT_BREAK:
1276                 fputs("break;\n", out);
1277                 break;
1278         case STATEMENT_IF:
1279                 print_if_statement(&statement->ifs);
1280                 break;
1281         case STATEMENT_SWITCH:
1282                 print_switch_statement(&statement->switchs);
1283                 break;
1284         case STATEMENT_CASE_LABEL:
1285                 print_case_label(&statement->case_label);
1286                 break;
1287         case STATEMENT_DECLARATION:
1288                 print_declaration_statement(&statement->declaration);
1289                 break;
1290         case STATEMENT_WHILE:
1291                 print_while_statement(&statement->whiles);
1292                 break;
1293         case STATEMENT_DO_WHILE:
1294                 print_do_while_statement(&statement->do_while);
1295                 break;
1296         case STATEMENT_FOR:
1297                 print_for_statement(&statement->fors);
1298                 break;
1299         case STATEMENT_ASM:
1300                 print_asm_statement(&statement->asms);
1301                 break;
1302         case STATEMENT_MS_TRY:
1303                 print_ms_try_statement(&statement->ms_try);
1304                 break;
1305         case STATEMENT_LEAVE:
1306                 print_leave_statement(&statement->leave);
1307                 break;
1308         case STATEMENT_INVALID:
1309                 fputs("$invalid statement$\n", out);
1310                 break;
1311         }
1312 }
1313
1314 /**
1315  * Print a storage class.
1316  *
1317  * @param storage_class   the storage class
1318  */
1319 static void print_storage_class(storage_class_tag_t storage_class)
1320 {
1321         const char *text;
1322         switch (storage_class) {
1323         case STORAGE_CLASS_NONE:     return;
1324         case STORAGE_CLASS_TYPEDEF:  text = "typedef ";  break;
1325         case STORAGE_CLASS_EXTERN:   text = "extern ";   break;
1326         case STORAGE_CLASS_STATIC:   text = "static ";   break;
1327         case STORAGE_CLASS_AUTO:     text = "auto ";     break;
1328         case STORAGE_CLASS_REGISTER: text = "register "; break;
1329         }
1330         fputs(text, out);
1331 }
1332
1333 /**
1334  * Print an initializer.
1335  *
1336  * @param initializer  the initializer
1337  */
1338 void print_initializer(const initializer_t *initializer)
1339 {
1340         if (initializer == NULL) {
1341                 fputs("{}", out);
1342                 return;
1343         }
1344
1345         switch (initializer->kind) {
1346         case INITIALIZER_VALUE: {
1347                 const initializer_value_t *value = &initializer->value;
1348                 print_expression(value->value);
1349                 return;
1350         }
1351         case INITIALIZER_LIST: {
1352                 assert(initializer->kind == INITIALIZER_LIST);
1353                 fputs("{ ", out);
1354                 const initializer_list_t *list = &initializer->list;
1355
1356                 for (size_t i = 0 ; i < list->len; ++i) {
1357                         const initializer_t *sub_init = list->initializers[i];
1358                         print_initializer(list->initializers[i]);
1359                         if (i < list->len-1) {
1360                                 if (sub_init == NULL || sub_init->kind != INITIALIZER_DESIGNATOR)
1361                                         fputs(", ", out);
1362                         }
1363                 }
1364                 fputs(" }", out);
1365                 return;
1366         }
1367         case INITIALIZER_STRING:
1368                 print_quoted_string(&initializer->string.string, '"', 1);
1369                 return;
1370         case INITIALIZER_WIDE_STRING:
1371                 print_quoted_wide_string(&initializer->wide_string.string, '"', 1);
1372                 return;
1373         case INITIALIZER_DESIGNATOR:
1374                 print_designator(initializer->designator.designator);
1375                 fputs(" = ", out);
1376                 return;
1377         }
1378
1379         panic("invalid initializer kind found");
1380 }
1381
1382 /**
1383  * Print microsoft extended declaration modifiers.
1384  */
1385 static void print_ms_modifiers(const declaration_t *declaration)
1386 {
1387         if ((c_mode & _MS) == 0)
1388                 return;
1389
1390         decl_modifiers_t modifiers = declaration->modifiers;
1391
1392         bool        ds_shown = false;
1393         const char *next     = "(";
1394
1395         if (declaration->base.kind == ENTITY_VARIABLE) {
1396                 variable_t *variable = (variable_t*)declaration;
1397                 if (variable->alignment != 0
1398                                 || variable->get_property_sym != NULL
1399                                 || variable->put_property_sym != NULL) {
1400                         if (!ds_shown) {
1401                                 fputs("__declspec", out);
1402                                 ds_shown = true;
1403                         }
1404
1405                         if (variable->alignment != 0) {
1406                                 fputs(next, out); next = ", "; fprintf(out, "align(%u)", variable->alignment);
1407                         }
1408                         if (variable->get_property_sym != NULL
1409                                         || variable->put_property_sym != NULL) {
1410                                 char *comma = "";
1411                                 fputs(next, out); next = ", "; fputs("property(", out);
1412                                 if (variable->get_property_sym != NULL) {
1413                                         fprintf(out, "get=%s", variable->get_property_sym->string);
1414                                         comma = ", ";
1415                                 }
1416                                 if (variable->put_property_sym != NULL)
1417                                         fprintf(out, "%sput=%s", comma, variable->put_property_sym->string);
1418                                 fputc(')', out);
1419                         }
1420                 }
1421         }
1422
1423         /* DM_FORCEINLINE handled outside. */
1424         if ((modifiers & ~DM_FORCEINLINE) != 0) {
1425                 if (!ds_shown) {
1426                         fputs("__declspec", out);
1427                         ds_shown = true;
1428                 }
1429                 if (modifiers & DM_DLLIMPORT) {
1430                         fputs(next, out); next = ", "; fputs("dllimport", out);
1431                 }
1432                 if (modifiers & DM_DLLEXPORT) {
1433                         fputs(next, out); next = ", "; fputs("dllexport", out);
1434                 }
1435                 if (modifiers & DM_THREAD) {
1436                         fputs(next, out); next = ", "; fputs("thread", out);
1437                 }
1438                 if (modifiers & DM_NAKED) {
1439                         fputs(next, out); next = ", "; fputs("naked", out);
1440                 }
1441                 if (modifiers & DM_THREAD) {
1442                         fputs(next, out); next = ", "; fputs("thread", out);
1443                 }
1444                 if (modifiers & DM_SELECTANY) {
1445                         fputs(next, out); next = ", "; fputs("selectany", out);
1446                 }
1447                 if (modifiers & DM_NOTHROW) {
1448                         fputs(next, out); next = ", "; fputs("nothrow", out);
1449                 }
1450                 if (modifiers & DM_NORETURN) {
1451                         fputs(next, out); next = ", "; fputs("noreturn", out);
1452                 }
1453                 if (modifiers & DM_NOINLINE) {
1454                         fputs(next, out); next = ", "; fputs("noinline", out);
1455                 }
1456                 if (modifiers & DM_DEPRECATED) {
1457                         fputs(next, out); next = ", "; fputs("deprecated", out);
1458                         if (declaration->deprecated_string != NULL)
1459                                 fprintf(out, "(\"%s\")",
1460                                         declaration->deprecated_string);
1461                 }
1462                 if (modifiers & DM_RESTRICT) {
1463                         fputs(next, out); next = ", "; fputs("restrict", out);
1464                 }
1465                 if (modifiers & DM_NOALIAS) {
1466                         fputs(next, out); next = ", "; fputs("noalias", out);
1467                 }
1468         }
1469
1470         if (ds_shown)
1471                 fputs(") ", out);
1472 }
1473
1474 static void print_scope(const scope_t *scope)
1475 {
1476         const entity_t *entity = scope->entities;
1477         for ( ; entity != NULL; entity = entity->base.next) {
1478                 print_indent();
1479                 print_entity(entity);
1480                 fputs("\n", out);
1481         }
1482 }
1483
1484 static void print_namespace(const namespace_t *namespace)
1485 {
1486         fputs("namespace ", out);
1487         if (namespace->base.symbol != NULL) {
1488                 fputs(namespace->base.symbol->string, out);
1489                 fputc(' ', out);
1490         }
1491
1492         fputs("{\n", out);
1493         ++indent;
1494
1495         print_scope(&namespace->members);
1496
1497         --indent;
1498         print_indent();
1499         fputs("}\n", out);
1500 }
1501
1502 /**
1503  * Print a variable or function declaration
1504  */
1505 void print_declaration(const entity_t *entity)
1506 {
1507         assert(is_declaration(entity));
1508         const declaration_t *declaration = &entity->declaration;
1509
1510         print_storage_class((storage_class_tag_t)declaration->declared_storage_class);
1511         if (entity->kind == ENTITY_FUNCTION) {
1512                 function_t *function = (function_t*)declaration;
1513                 if (function->is_inline) {
1514                         if (declaration->modifiers & DM_FORCEINLINE) {
1515                                 fputs("__forceinline ", out);
1516                         } else if (declaration->modifiers & DM_MICROSOFT_INLINE) {
1517                                 fputs("__inline ", out);
1518                         } else {
1519                                 fputs("inline ", out);
1520                         }
1521                 }
1522         }
1523         print_ms_modifiers(declaration);
1524         switch (entity->kind) {
1525                 case ENTITY_FUNCTION:
1526                         print_type_ext(entity->declaration.type, entity->base.symbol,
1527                                         &entity->function.parameters);
1528
1529                         if (entity->function.statement != NULL) {
1530                                 fputc('\n', out);
1531                                 print_indent();
1532                                 print_statement(entity->function.statement);
1533                                 return;
1534                         }
1535                         break;
1536
1537                 case ENTITY_VARIABLE:
1538                         if (entity->variable.thread_local)
1539                                 fputs("__thread ", out);
1540                         print_type_ext(declaration->type, declaration->base.symbol, NULL);
1541                         if (entity->variable.initializer != NULL) {
1542                                 fputs(" = ", out);
1543                                 print_initializer(entity->variable.initializer);
1544                         }
1545                         break;
1546
1547                 default:
1548                         print_type_ext(declaration->type, declaration->base.symbol, NULL);
1549                         break;
1550         }
1551         fputc(';', out);
1552 }
1553
1554 /**
1555  * Prints an expression.
1556  *
1557  * @param expression  the expression
1558  */
1559 void print_expression(const expression_t *expression)
1560 {
1561         print_expression_prec(expression, PREC_BOTTOM);
1562 }
1563
1564 /**
1565  * Print a declaration.
1566  *
1567  * @param declaration  the declaration
1568  */
1569 void print_entity(const entity_t *entity)
1570 {
1571         if (entity->base.namespc != NAMESPACE_NORMAL && entity->base.symbol == NULL)
1572                 return;
1573
1574         switch ((entity_kind_tag_t)entity->kind) {
1575         case ENTITY_VARIABLE:
1576         case ENTITY_PARAMETER:
1577         case ENTITY_COMPOUND_MEMBER:
1578                 print_declaration(entity);
1579                 return;
1580         case ENTITY_FUNCTION:
1581                 print_declaration(entity);
1582                 return;
1583         case ENTITY_TYPEDEF:
1584                 print_typedef(entity);
1585                 return;
1586         case ENTITY_STRUCT:
1587                 fputs("struct ", out);
1588                 fputs(entity->base.symbol->string, out);
1589                 if (entity->structe.complete) {
1590                         fputc(' ', out);
1591                         print_compound_definition(&entity->structe);
1592                 }
1593                 fputc(';', out);
1594                 return;
1595         case ENTITY_UNION:
1596                 fputs("union ", out);
1597                 fputs(entity->base.symbol->string, out);
1598                 if (entity->unione.complete) {
1599                         fputc(' ', out);
1600                         print_compound_definition(&entity->unione);
1601                 }
1602                 fputc(';', out);
1603                 return;
1604         case ENTITY_ENUM:
1605                 fputs("enum ", out);
1606                 fputs(entity->base.symbol->string, out);
1607                 fputc(' ', out);
1608                 print_enum_definition(&entity->enume);
1609                 fputc(';', out);
1610                 return;
1611         case ENTITY_NAMESPACE:
1612                 print_namespace(&entity->namespacee);
1613                 return;
1614         case ENTITY_LABEL:
1615         case ENTITY_ENUM_VALUE:
1616         case ENTITY_LOCAL_LABEL:
1617                 panic("print_entity used on unexpected entity type");
1618         case ENTITY_INVALID:
1619                 break;
1620         }
1621         panic("Invalid entity type encountered");
1622 }
1623
1624 /**
1625  * Print the AST of a translation unit.
1626  *
1627  * @param unit   the translation unit
1628  */
1629 void print_ast(const translation_unit_t *unit)
1630 {
1631         inc_type_visited();
1632
1633         entity_t *entity = unit->scope.entities;
1634         for ( ; entity != NULL; entity = entity->base.next) {
1635                 if (entity->kind == ENTITY_ENUM_VALUE)
1636                         continue;
1637                 if (entity->base.namespc != NAMESPACE_NORMAL
1638                                 && entity->base.symbol == NULL)
1639                         continue;
1640                 if (is_generated_entity(entity))
1641                         continue;
1642
1643                 print_indent();
1644                 print_entity(entity);
1645                 fputc('\n', out);
1646         }
1647 }
1648
1649 bool is_constant_initializer(const initializer_t *initializer)
1650 {
1651         switch (initializer->kind) {
1652         case INITIALIZER_STRING:
1653         case INITIALIZER_WIDE_STRING:
1654         case INITIALIZER_DESIGNATOR:
1655                 return true;
1656
1657         case INITIALIZER_VALUE:
1658                 return is_constant_expression(initializer->value.value);
1659
1660         case INITIALIZER_LIST:
1661                 for (size_t i = 0; i < initializer->list.len; ++i) {
1662                         initializer_t *sub_initializer = initializer->list.initializers[i];
1663                         if (!is_constant_initializer(sub_initializer))
1664                                 return false;
1665                 }
1666                 return true;
1667         }
1668         panic("invalid initializer kind found");
1669 }
1670
1671 static bool is_object_with_linker_constant_address(const expression_t *expression)
1672 {
1673         switch (expression->kind) {
1674         case EXPR_UNARY_DEREFERENCE:
1675                 return is_address_constant(expression->unary.value);
1676
1677         case EXPR_SELECT: {
1678                 type_t *base_type = skip_typeref(expression->select.compound->base.type);
1679                 if (is_type_pointer(base_type)) {
1680                         /* it's a -> */
1681                         return is_address_constant(expression->select.compound);
1682                 } else {
1683                         return is_object_with_linker_constant_address(expression->select.compound);
1684                 }
1685         }
1686
1687         case EXPR_ARRAY_ACCESS:
1688                 return is_constant_expression(expression->array_access.index)
1689                         && is_address_constant(expression->array_access.array_ref);
1690
1691         case EXPR_REFERENCE: {
1692                 entity_t *entity = expression->reference.entity;
1693                 if (is_declaration(entity)) {
1694                         switch ((storage_class_tag_t)entity->declaration.storage_class) {
1695                         case STORAGE_CLASS_NONE:
1696                         case STORAGE_CLASS_EXTERN:
1697                         case STORAGE_CLASS_STATIC:
1698                                 return
1699                                         entity->kind != ENTITY_VARIABLE ||
1700                                         !entity->variable.thread_local;
1701
1702                         case STORAGE_CLASS_REGISTER:
1703                         case STORAGE_CLASS_TYPEDEF:
1704                         case STORAGE_CLASS_AUTO:
1705                                 break;
1706                         }
1707                 }
1708                 return false;
1709         }
1710
1711         default:
1712                 return false;
1713         }
1714 }
1715
1716 bool is_address_constant(const expression_t *expression)
1717 {
1718         switch (expression->kind) {
1719         case EXPR_UNARY_TAKE_ADDRESS:
1720                 return is_object_with_linker_constant_address(expression->unary.value);
1721
1722         case EXPR_UNARY_DEREFERENCE: {
1723                 type_t *real_type
1724                         = revert_automatic_type_conversion(expression->unary.value);
1725                 /* dereferencing a function is a NOP */
1726                 if (is_type_function(real_type)) {
1727                         return is_address_constant(expression->unary.value);
1728                 }
1729                 /* FALLTHROUGH */
1730         }
1731
1732         case EXPR_UNARY_CAST: {
1733                 type_t *dest = skip_typeref(expression->base.type);
1734                 if (!is_type_pointer(dest) && (
1735                         dest->kind != TYPE_ATOMIC                                               ||
1736                         !(get_atomic_type_flags(dest->atomic.akind) & ATOMIC_TYPE_FLAG_INTEGER) ||
1737                         get_atomic_type_size(dest->atomic.akind) < get_atomic_type_size(get_intptr_kind())
1738                     ))
1739                         return false;
1740
1741                 return (is_constant_expression(expression->unary.value)
1742                         || is_address_constant(expression->unary.value));
1743         }
1744
1745         case EXPR_BINARY_ADD:
1746         case EXPR_BINARY_SUB: {
1747                 expression_t *left  = expression->binary.left;
1748                 expression_t *right = expression->binary.right;
1749
1750                 if (is_type_pointer(skip_typeref(left->base.type))) {
1751                         return is_address_constant(left) && is_constant_expression(right);
1752                 } else if (is_type_pointer(skip_typeref(right->base.type))) {
1753                         return is_constant_expression(left)     && is_address_constant(right);
1754                 }
1755
1756                 return false;
1757         }
1758
1759         case EXPR_REFERENCE: {
1760                 entity_t *entity = expression->reference.entity;
1761                 if (!is_declaration(entity))
1762                         return false;
1763
1764                 type_t *type = skip_typeref(entity->declaration.type);
1765                 if (is_type_function(type))
1766                         return true;
1767                 if (is_type_array(type)) {
1768                         return is_object_with_linker_constant_address(expression);
1769                 }
1770                 /* Prevent stray errors */
1771                 if (!is_type_valid(type))
1772                         return true;
1773                 return false;
1774         }
1775
1776         case EXPR_ARRAY_ACCESS: {
1777                 type_t *const type =
1778                         skip_typeref(revert_automatic_type_conversion(expression));
1779                 return
1780                         is_type_array(type)                                    &&
1781                         is_constant_expression(expression->array_access.index) &&
1782                         is_address_constant(expression->array_access.array_ref);
1783         }
1784
1785         default:
1786                 return false;
1787         }
1788 }
1789
1790 static bool is_builtin_const_call(const expression_t *expression)
1791 {
1792         expression_t *function = expression->call.function;
1793         if (function->kind != EXPR_BUILTIN_SYMBOL) {
1794                 return false;
1795         }
1796
1797         symbol_t *symbol = function->builtin_symbol.symbol;
1798
1799         switch (symbol->ID) {
1800         case T___builtin_huge_val:
1801         case T___builtin_inf:
1802         case T___builtin_inff:
1803         case T___builtin_infl:
1804         case T___builtin_nan:
1805         case T___builtin_nanf:
1806         case T___builtin_nanl:
1807                 return true;
1808         }
1809
1810         return false;
1811 }
1812
1813 static bool is_constant_pointer(const expression_t *expression)
1814 {
1815         if (is_constant_expression(expression))
1816                 return true;
1817
1818         switch (expression->kind) {
1819         case EXPR_UNARY_CAST:
1820                 return is_constant_pointer(expression->unary.value);
1821         default:
1822                 return false;
1823         }
1824 }
1825
1826 static bool is_object_with_constant_address(const expression_t *expression)
1827 {
1828         switch (expression->kind) {
1829         case EXPR_SELECT: {
1830                 expression_t *compound      = expression->select.compound;
1831                 type_t       *compound_type = compound->base.type;
1832                 compound_type = skip_typeref(compound_type);
1833                 if (is_type_pointer(compound_type)) {
1834                         return is_constant_pointer(compound);
1835                 } else {
1836                         return is_object_with_constant_address(compound);
1837                 }
1838         }
1839
1840         case EXPR_ARRAY_ACCESS: {
1841                 array_access_expression_t const* const array_access =
1842                         &expression->array_access;
1843                 return
1844                         is_constant_expression(array_access->index) && (
1845                                 is_object_with_constant_address(array_access->array_ref) ||
1846                                 is_constant_pointer(array_access->array_ref)
1847                         );
1848         }
1849
1850         case EXPR_UNARY_DEREFERENCE:
1851                 return is_constant_pointer(expression->unary.value);
1852         default:
1853                 return false;
1854         }
1855 }
1856
1857 bool is_constant_expression(const expression_t *expression)
1858 {
1859         switch (expression->kind) {
1860
1861         case EXPR_CONST:
1862         case EXPR_CHARACTER_CONSTANT:
1863         case EXPR_WIDE_CHARACTER_CONSTANT:
1864         case EXPR_STRING_LITERAL:
1865         case EXPR_WIDE_STRING_LITERAL:
1866         case EXPR_CLASSIFY_TYPE:
1867         case EXPR_FUNCNAME:
1868         case EXPR_OFFSETOF:
1869         case EXPR_ALIGNOF:
1870         case EXPR_BUILTIN_CONSTANT_P:
1871         case EXPR_LABEL_ADDRESS:
1872         case EXPR_REFERENCE_ENUM_VALUE:
1873                 return true;
1874
1875         case EXPR_SIZEOF: {
1876                 type_t *type = expression->typeprop.type;
1877                 if (type == NULL)
1878                         type = expression->typeprop.tp_expression->base.type;
1879
1880                 type = skip_typeref(type);
1881                 if (is_type_array(type) && type->array.is_vla)
1882                         return false;
1883                 return true;
1884         }
1885
1886         case EXPR_BUILTIN_SYMBOL:
1887         case EXPR_BUILTIN_PREFETCH:
1888         case EXPR_SELECT:
1889         case EXPR_VA_START:
1890         case EXPR_VA_ARG:
1891         case EXPR_STATEMENT:
1892         case EXPR_REFERENCE:
1893         case EXPR_UNARY_POSTFIX_INCREMENT:
1894         case EXPR_UNARY_POSTFIX_DECREMENT:
1895         case EXPR_UNARY_PREFIX_INCREMENT:
1896         case EXPR_UNARY_PREFIX_DECREMENT:
1897         case EXPR_UNARY_ASSUME: /* has VOID type */
1898         case EXPR_UNARY_DEREFERENCE:
1899         case EXPR_UNARY_DELETE:
1900         case EXPR_UNARY_DELETE_ARRAY:
1901         case EXPR_UNARY_THROW:
1902         case EXPR_BINARY_ASSIGN:
1903         case EXPR_BINARY_MUL_ASSIGN:
1904         case EXPR_BINARY_DIV_ASSIGN:
1905         case EXPR_BINARY_MOD_ASSIGN:
1906         case EXPR_BINARY_ADD_ASSIGN:
1907         case EXPR_BINARY_SUB_ASSIGN:
1908         case EXPR_BINARY_SHIFTLEFT_ASSIGN:
1909         case EXPR_BINARY_SHIFTRIGHT_ASSIGN:
1910         case EXPR_BINARY_BITWISE_AND_ASSIGN:
1911         case EXPR_BINARY_BITWISE_XOR_ASSIGN:
1912         case EXPR_BINARY_BITWISE_OR_ASSIGN:
1913         case EXPR_BINARY_COMMA:
1914         case EXPR_ARRAY_ACCESS:
1915                 return false;
1916
1917         case EXPR_UNARY_TAKE_ADDRESS:
1918                 return is_object_with_constant_address(expression->unary.value);
1919
1920         case EXPR_CALL:
1921                 return is_builtin_const_call(expression);
1922
1923         case EXPR_UNARY_NEGATE:
1924         case EXPR_UNARY_PLUS:
1925         case EXPR_UNARY_BITWISE_NEGATE:
1926         case EXPR_UNARY_NOT:
1927                 return is_constant_expression(expression->unary.value);
1928
1929         case EXPR_UNARY_CAST:
1930         case EXPR_UNARY_CAST_IMPLICIT:
1931                 return is_type_arithmetic(skip_typeref(expression->base.type))
1932                         && is_constant_expression(expression->unary.value);
1933
1934         case EXPR_BINARY_ADD:
1935         case EXPR_BINARY_SUB:
1936         case EXPR_BINARY_MUL:
1937         case EXPR_BINARY_DIV:
1938         case EXPR_BINARY_MOD:
1939         case EXPR_BINARY_EQUAL:
1940         case EXPR_BINARY_NOTEQUAL:
1941         case EXPR_BINARY_LESS:
1942         case EXPR_BINARY_LESSEQUAL:
1943         case EXPR_BINARY_GREATER:
1944         case EXPR_BINARY_GREATEREQUAL:
1945         case EXPR_BINARY_BITWISE_AND:
1946         case EXPR_BINARY_BITWISE_OR:
1947         case EXPR_BINARY_BITWISE_XOR:
1948         case EXPR_BINARY_LOGICAL_AND:
1949         case EXPR_BINARY_LOGICAL_OR:
1950         case EXPR_BINARY_SHIFTLEFT:
1951         case EXPR_BINARY_SHIFTRIGHT:
1952         case EXPR_BINARY_BUILTIN_EXPECT:
1953         case EXPR_BINARY_ISGREATER:
1954         case EXPR_BINARY_ISGREATEREQUAL:
1955         case EXPR_BINARY_ISLESS:
1956         case EXPR_BINARY_ISLESSEQUAL:
1957         case EXPR_BINARY_ISLESSGREATER:
1958         case EXPR_BINARY_ISUNORDERED:
1959                 return is_constant_expression(expression->binary.left)
1960                         && is_constant_expression(expression->binary.right);
1961
1962         case EXPR_COMPOUND_LITERAL:
1963                 return is_constant_initializer(expression->compound_literal.initializer);
1964
1965         case EXPR_CONDITIONAL: {
1966                 expression_t *condition = expression->conditional.condition;
1967                 if (!is_constant_expression(condition))
1968                         return false;
1969
1970                 long val = fold_constant(condition);
1971                 if (val != 0)
1972                         return is_constant_expression(expression->conditional.true_expression);
1973                 else
1974                         return is_constant_expression(expression->conditional.false_expression);
1975         }
1976
1977         case EXPR_INVALID:
1978                 return true;
1979
1980         case EXPR_UNKNOWN:
1981                 break;
1982         }
1983         panic("invalid expression found (is constant expression)");
1984 }
1985
1986 /**
1987  * Initialize the AST construction.
1988  */
1989 void init_ast(void)
1990 {
1991         obstack_init(&ast_obstack);
1992 }
1993
1994 /**
1995  * Free the AST.
1996  */
1997 void exit_ast(void)
1998 {
1999         obstack_free(&ast_obstack, NULL);
2000 }
2001
2002 /**
2003  * Set the output stream for the AST printer.
2004  *
2005  * @param stream  the output stream
2006  */
2007 void ast_set_output(FILE *stream)
2008 {
2009         out = stream;
2010         type_set_output(stream);
2011 }
2012
2013 /**
2014  * Allocate an AST object of the given size.
2015  *
2016  * @param size  the size of the object to allocate
2017  *
2018  * @return  A new allocated object in the AST memeory space.
2019  */
2020 void *(allocate_ast)(size_t size)
2021 {
2022         return _allocate_ast(size);
2023 }