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