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