Use given storage class as declared storage class in create_error_declaration().
[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                 fprintf(out, "\t");
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         fprintf(out, "(");
396         call_argument_t *argument = call->arguments;
397         int              first    = 1;
398         while(argument != NULL) {
399                 if(!first) {
400                         fprintf(out, ", ");
401                 } else {
402                         first = 0;
403                 }
404                 print_expression_prec(argument->expression, PREC_COMMA + 1);
405
406                 argument = argument->next;
407         }
408         fprintf(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         fprintf(out, "%s", ref->declaration->symbol->string);
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                 fprintf(out, "$invalid expression$");
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         fprintf(out, "return ");
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         fprintf(out, "goto ");
892         fputs(statement->label->symbol->string, out);
893         fprintf(stderr, "(%p)", (void*) statement->label);
894         fputs(";\n", out);
895 }
896
897 /**
898  * Print a label statement.
899  *
900  * @param statement  the label statement
901  */
902 static void print_label_statement(const label_statement_t *statement)
903 {
904         fprintf(stderr, "(%p)", (void*) statement->label);
905         fprintf(out, "%s:\n", statement->label->symbol->string);
906         print_statement(statement->statement);
907 }
908
909 /**
910  * Print an if statement.
911  *
912  * @param statement  the if statement
913  */
914 static void print_if_statement(const if_statement_t *statement)
915 {
916         fputs("if (", out);
917         print_expression(statement->condition);
918         fputs(") ", out);
919         print_statement(statement->true_statement);
920
921         if(statement->false_statement != NULL) {
922                 print_indent();
923                 fputs("else ", out);
924                 print_statement(statement->false_statement);
925         }
926 }
927
928 /**
929  * Print a switch statement.
930  *
931  * @param statement  the switch statement
932  */
933 static void print_switch_statement(const switch_statement_t *statement)
934 {
935         fputs("switch (", out);
936         print_expression(statement->expression);
937         fputs(") ", out);
938         print_statement(statement->body);
939 }
940
941 /**
942  * Print a case label (including the default label).
943  *
944  * @param statement  the case label statement
945  */
946 static void print_case_label(const case_label_statement_t *statement)
947 {
948         if(statement->expression == NULL) {
949                 fputs("default:\n", out);
950         } else {
951                 fputs("case ", out);
952                 print_expression(statement->expression);
953                 if (statement->end_range != NULL) {
954                         fputs(" ... ", out);
955                         print_expression(statement->end_range);
956                 }
957                 fputs(":\n", out);
958         }
959         ++indent;
960         if(statement->statement != NULL) {
961                 if (statement->statement->base.kind == STATEMENT_CASE_LABEL) {
962                         --indent;
963                 }
964                 print_indent();
965                 print_statement(statement->statement);
966         }
967 }
968
969 /**
970  * Print a declaration statement.
971  *
972  * @param statement   the statement
973  */
974 static void print_declaration_statement(
975                 const declaration_statement_t *statement)
976 {
977         bool first = true;
978         for (declaration_t *declaration = statement->declarations_begin;
979              declaration != statement->declarations_end->next;
980              declaration = declaration->next) {
981                 if (declaration->storage_class == STORAGE_CLASS_ENUM_ENTRY)
982                         continue;
983                 if (declaration->implicit)
984                         continue;
985
986                 if (!first) {
987                         print_indent();
988                 } else {
989                         first = false;
990                 }
991                 print_declaration(declaration);
992                 fputc('\n', out);
993         }
994 }
995
996 /**
997  * Print a while statement.
998  *
999  * @param statement   the statement
1000  */
1001 static void print_while_statement(const while_statement_t *statement)
1002 {
1003         fputs("while (", out);
1004         print_expression(statement->condition);
1005         fputs(") ", out);
1006         print_statement(statement->body);
1007 }
1008
1009 /**
1010  * Print a do-while statement.
1011  *
1012  * @param statement   the statement
1013  */
1014 static void print_do_while_statement(const do_while_statement_t *statement)
1015 {
1016         fputs("do ", out);
1017         print_statement(statement->body);
1018         print_indent();
1019         fputs("while (", out);
1020         print_expression(statement->condition);
1021         fputs(");\n", out);
1022 }
1023
1024 /**
1025  * Print a for statement.
1026  *
1027  * @param statement   the statement
1028  */
1029 static void print_for_statement(const for_statement_t *statement)
1030 {
1031         fputs("for (", out);
1032         declaration_t *decl = statement->scope.declarations;
1033         while (decl != NULL && decl->implicit)
1034                 decl = decl->next;
1035         if (decl != NULL) {
1036                 assert(statement->initialisation == NULL);
1037                 print_declaration(decl);
1038                 if (decl->next != NULL) {
1039                         panic("multiple declarations in for statement not supported yet");
1040                 }
1041                 fputc(' ', out);
1042         } else {
1043                 if(statement->initialisation) {
1044                         print_expression(statement->initialisation);
1045                 }
1046                 fputs("; ", out);
1047         }
1048         if(statement->condition != NULL) {
1049                 print_expression(statement->condition);
1050         }
1051         fputs("; ", out);
1052         if(statement->step != NULL) {
1053                 print_expression(statement->step);
1054         }
1055         fputs(") ", out);
1056         print_statement(statement->body);
1057 }
1058
1059 /**
1060  * Print assembler arguments.
1061  *
1062  * @param arguments   the arguments
1063  */
1064 static void print_asm_arguments(asm_argument_t *arguments)
1065 {
1066         asm_argument_t *argument = arguments;
1067         for( ; argument != NULL; argument = argument->next) {
1068                 if(argument != arguments)
1069                         fputs(", ", out);
1070
1071                 if(argument->symbol) {
1072                         fprintf(out, "[%s] ", argument->symbol->string);
1073                 }
1074                 print_quoted_string(&argument->constraints, '"', 1);
1075                 fputs(" (", out);
1076                 print_expression(argument->expression);
1077                 fputs(")", out);
1078         }
1079 }
1080
1081 /**
1082  * Print assembler clobbers.
1083  *
1084  * @param clobbers   the clobbers
1085  */
1086 static void print_asm_clobbers(asm_clobber_t *clobbers)
1087 {
1088         asm_clobber_t *clobber = clobbers;
1089         for( ; clobber != NULL; clobber = clobber->next) {
1090                 if(clobber != clobbers)
1091                         fputs(", ", out);
1092
1093                 print_quoted_string(&clobber->clobber, '"', 1);
1094         }
1095 }
1096
1097 /**
1098  * Print an assembler statement.
1099  *
1100  * @param statement   the statement
1101  */
1102 static void print_asm_statement(const asm_statement_t *statement)
1103 {
1104         fputs("asm ", out);
1105         if(statement->is_volatile) {
1106                 fputs("volatile ", out);
1107         }
1108         fputs("(", out);
1109         print_quoted_string(&statement->asm_text, '"', 1);
1110         if(statement->inputs == NULL && statement->outputs == NULL
1111                         && statement->clobbers == NULL)
1112                 goto end_of_print_asm_statement;
1113
1114         fputs(" : ", out);
1115         print_asm_arguments(statement->inputs);
1116         if(statement->outputs == NULL && statement->clobbers == NULL)
1117                 goto end_of_print_asm_statement;
1118
1119         fputs(" : ", out);
1120         print_asm_arguments(statement->outputs);
1121         if(statement->clobbers == NULL)
1122                 goto end_of_print_asm_statement;
1123
1124         fputs(" : ", out);
1125         print_asm_clobbers(statement->clobbers);
1126
1127 end_of_print_asm_statement:
1128         fputs(");\n", out);
1129 }
1130
1131 /**
1132  * Print a microsoft __try statement.
1133  *
1134  * @param statement   the statement
1135  */
1136 static void print_ms_try_statement(const ms_try_statement_t *statement)
1137 {
1138         fputs("__try ", out);
1139         print_statement(statement->try_statement);
1140         print_indent();
1141         if(statement->except_expression != NULL) {
1142                 fputs("__except(", out);
1143                 print_expression(statement->except_expression);
1144                 fputs(") ", out);
1145         } else {
1146                 fputs("__finally ", out);
1147         }
1148         print_statement(statement->final_statement);
1149 }
1150
1151 /**
1152  * Print a microsoft __leave statement.
1153  *
1154  * @param statement   the statement
1155  */
1156 static void print_leave_statement(const leave_statement_t *statement)
1157 {
1158         (void) statement;
1159         fputs("__leave;\n", out);
1160 }
1161
1162 /**
1163  * Print a statement.
1164  *
1165  * @param statement   the statement
1166  */
1167 void print_statement(const statement_t *statement)
1168 {
1169         switch(statement->kind) {
1170         case STATEMENT_EMPTY:
1171                 fputs(";\n", out);
1172                 break;
1173         case STATEMENT_COMPOUND:
1174                 print_compound_statement(&statement->compound);
1175                 break;
1176         case STATEMENT_RETURN:
1177                 print_return_statement(&statement->returns);
1178                 break;
1179         case STATEMENT_EXPRESSION:
1180                 print_expression_statement(&statement->expression);
1181                 break;
1182         case STATEMENT_LABEL:
1183                 print_label_statement(&statement->label);
1184                 break;
1185         case STATEMENT_GOTO:
1186                 print_goto_statement(&statement->gotos);
1187                 break;
1188         case STATEMENT_CONTINUE:
1189                 fputs("continue;\n", out);
1190                 break;
1191         case STATEMENT_BREAK:
1192                 fputs("break;\n", out);
1193                 break;
1194         case STATEMENT_IF:
1195                 print_if_statement(&statement->ifs);
1196                 break;
1197         case STATEMENT_SWITCH:
1198                 print_switch_statement(&statement->switchs);
1199                 break;
1200         case STATEMENT_CASE_LABEL:
1201                 print_case_label(&statement->case_label);
1202                 break;
1203         case STATEMENT_DECLARATION:
1204                 print_declaration_statement(&statement->declaration);
1205                 break;
1206         case STATEMENT_WHILE:
1207                 print_while_statement(&statement->whiles);
1208                 break;
1209         case STATEMENT_DO_WHILE:
1210                 print_do_while_statement(&statement->do_while);
1211                 break;
1212         case STATEMENT_FOR:
1213                 print_for_statement(&statement->fors);
1214                 break;
1215         case STATEMENT_ASM:
1216                 print_asm_statement(&statement->asms);
1217                 break;
1218         case STATEMENT_MS_TRY:
1219                 print_ms_try_statement(&statement->ms_try);
1220                 break;
1221         case STATEMENT_LEAVE:
1222                 print_leave_statement(&statement->leave);
1223                 break;
1224         case STATEMENT_INVALID:
1225                 fprintf(out, "$invalid statement$");
1226                 break;
1227         }
1228 }
1229
1230 /**
1231  * Print a storage class.
1232  *
1233  * @param storage_class   the storage class
1234  */
1235 static void print_storage_class(storage_class_tag_t storage_class)
1236 {
1237         switch(storage_class) {
1238         case STORAGE_CLASS_ENUM_ENTRY:
1239         case STORAGE_CLASS_NONE:
1240                 break;
1241         case STORAGE_CLASS_TYPEDEF:       fputs("typedef ",        out); break;
1242         case STORAGE_CLASS_EXTERN:        fputs("extern ",         out); break;
1243         case STORAGE_CLASS_STATIC:        fputs("static ",         out); break;
1244         case STORAGE_CLASS_AUTO:          fputs("auto ",           out); break;
1245         case STORAGE_CLASS_REGISTER:      fputs("register ",       out); break;
1246         case STORAGE_CLASS_THREAD:        fputs("__thread",        out); break;
1247         case STORAGE_CLASS_THREAD_EXTERN: fputs("extern __thread", out); break;
1248         case STORAGE_CLASS_THREAD_STATIC: fputs("static __thread", out); break;
1249         }
1250 }
1251
1252 /**
1253  * Print an initializer.
1254  *
1255  * @param initializer  the initializer
1256  */
1257 void print_initializer(const initializer_t *initializer)
1258 {
1259         if(initializer == NULL) {
1260                 fputs("{}", out);
1261                 return;
1262         }
1263
1264         switch(initializer->kind) {
1265         case INITIALIZER_VALUE: {
1266                 const initializer_value_t *value = &initializer->value;
1267                 print_expression(value->value);
1268                 return;
1269         }
1270         case INITIALIZER_LIST: {
1271                 assert(initializer->kind == INITIALIZER_LIST);
1272                 fputs("{ ", out);
1273                 const initializer_list_t *list = &initializer->list;
1274
1275                 for(size_t i = 0 ; i < list->len; ++i) {
1276                         const initializer_t *sub_init = list->initializers[i];
1277                         print_initializer(list->initializers[i]);
1278                         if(i < list->len-1) {
1279                                 if(sub_init == NULL || sub_init->kind != INITIALIZER_DESIGNATOR)
1280                                         fputs(", ", out);
1281                         }
1282                 }
1283                 fputs(" }", out);
1284                 return;
1285         }
1286         case INITIALIZER_STRING:
1287                 print_quoted_string(&initializer->string.string, '"', 1);
1288                 return;
1289         case INITIALIZER_WIDE_STRING:
1290                 print_quoted_wide_string(&initializer->wide_string.string, '"', 1);
1291                 return;
1292         case INITIALIZER_DESIGNATOR:
1293                 print_designator(initializer->designator.designator);
1294                 fputs(" = ", out);
1295                 return;
1296         }
1297
1298         panic("invalid initializer kind found");
1299 }
1300
1301 /**
1302  * Print microsoft extended declaration modifiers.
1303  */
1304 static void print_ms_modifiers(const declaration_t *declaration) {
1305         if((c_mode & _MS) == 0)
1306                 return;
1307
1308         decl_modifiers_t modifiers = declaration->modifiers;
1309
1310         /* DM_FORCEINLINE handled outside. */
1311         if ((modifiers & ~DM_FORCEINLINE) != 0    ||
1312             declaration->alignment        != 0    ||
1313             declaration->get_property_sym != NULL ||
1314             declaration->put_property_sym != NULL) {
1315                 char *next = "(";
1316
1317                 fputs("__declspec", out);
1318                 if(modifiers & DM_DLLIMPORT) {
1319                         fputs(next, out); next = ", "; fputs("dllimport", out);
1320                 }
1321                 if(modifiers & DM_DLLEXPORT) {
1322                         fputs(next, out); next = ", "; fputs("dllexport", out);
1323                 }
1324                 if(modifiers & DM_THREAD) {
1325                         fputs(next, out); next = ", "; fputs("thread", out);
1326                 }
1327                 if(modifiers & DM_NAKED) {
1328                         fputs(next, out); next = ", "; fputs("naked", out);
1329                 }
1330                 if(modifiers & DM_THREAD) {
1331                         fputs(next, out); next = ", "; fputs("thread", out);
1332                 }
1333                 if(modifiers & DM_SELECTANY) {
1334                         fputs(next, out); next = ", "; fputs("selectany", out);
1335                 }
1336                 if(modifiers & DM_NOTHROW) {
1337                         fputs(next, out); next = ", "; fputs("nothrow", out);
1338                 }
1339                 if(modifiers & DM_NORETURN) {
1340                         fputs(next, out); next = ", "; fputs("noreturn", out);
1341                 }
1342                 if(modifiers & DM_NOINLINE) {
1343                         fputs(next, out); next = ", "; fputs("noinline", out);
1344                 }
1345                 if (modifiers & DM_DEPRECATED) {
1346                         fputs(next, out); next = ", "; fputs("deprecated", out);
1347                         if(declaration->deprecated_string != NULL)
1348                                 fprintf(out, "(\"%s\")", declaration->deprecated_string);
1349                 }
1350                 if(declaration->alignment != 0) {
1351                         fputs(next, out); next = ", "; fprintf(out, "align(%u)", declaration->alignment);
1352                 }
1353                 if(modifiers & DM_RESTRICT) {
1354                         fputs(next, out); next = ", "; fputs("restrict", out);
1355                 }
1356                 if(modifiers & DM_NOALIAS) {
1357                         fputs(next, out); next = ", "; fputs("noalias", out);
1358                 }
1359             if(declaration->get_property_sym != NULL || declaration->put_property_sym != NULL) {
1360                 char *comma = "";
1361                         fputs(next, out); next = ", "; fprintf(out, "property(");
1362                 if(declaration->get_property_sym != NULL) {
1363                         fprintf(out, "get=%s", declaration->get_property_sym->string);
1364                         comma = ", ";
1365                         }
1366                 if(declaration->put_property_sym != NULL)
1367                         fprintf(out, "%sput=%s", comma, declaration->put_property_sym->string);
1368                         fputc(')', out);
1369                 }
1370                 fputs(") ", out);
1371         }
1372 }
1373
1374 /**
1375  * Print a declaration in the NORMAL namespace.
1376  *
1377  * @param declaration  the declaration
1378  */
1379 static void print_normal_declaration(const declaration_t *declaration)
1380 {
1381         print_storage_class((storage_class_tag_t) declaration->declared_storage_class);
1382         if (declaration->is_inline) {
1383                 if (declaration->modifiers & DM_FORCEINLINE) {
1384                         fputs("__forceinline ", out);
1385                 } else if (declaration->modifiers & DM_MICROSOFT_INLINE) {
1386                         fputs("__inline ", out);
1387                 } else {
1388                         fputs("inline ", out);
1389                 }
1390         }
1391         print_ms_modifiers(declaration);
1392         print_type_ext(declaration->type, declaration->symbol,
1393                        &declaration->scope);
1394
1395         if(declaration->type->kind == TYPE_FUNCTION) {
1396                 if(declaration->init.statement != NULL) {
1397                         fputs("\n", out);
1398                         print_statement(declaration->init.statement);
1399                         return;
1400                 }
1401         } else if(declaration->init.initializer != NULL) {
1402                 fputs(" = ", out);
1403                 print_initializer(declaration->init.initializer);
1404         }
1405         fputc(';', out);
1406 }
1407
1408 /**
1409  * Prints an expression.
1410  *
1411  * @param expression  the expression
1412  */
1413 void print_expression(const expression_t *expression) {
1414         print_expression_prec(expression, PREC_BOTTOM);
1415 }
1416
1417 /**
1418  * Print a declaration.
1419  *
1420  * @param declaration  the declaration
1421  */
1422 void print_declaration(const declaration_t *declaration)
1423 {
1424         if(declaration->namespc != NAMESPACE_NORMAL &&
1425                         declaration->symbol == NULL)
1426                 return;
1427
1428         switch(declaration->namespc) {
1429         case NAMESPACE_NORMAL:
1430                 print_normal_declaration(declaration);
1431                 break;
1432         case NAMESPACE_STRUCT:
1433                 fputs("struct ", out);
1434                 fputs(declaration->symbol->string, out);
1435                 fputc(' ', out);
1436                 print_compound_definition(declaration);
1437                 fputc(';', out);
1438                 break;
1439         case NAMESPACE_UNION:
1440                 fputs("union ", out);
1441                 fputs(declaration->symbol->string, out);
1442                 fputc(' ', out);
1443                 print_compound_definition(declaration);
1444                 fputc(';', out);
1445                 break;
1446         case NAMESPACE_ENUM:
1447                 fputs("enum ", out);
1448                 fputs(declaration->symbol->string, out);
1449                 fputc(' ', out);
1450                 print_enum_definition(declaration);
1451                 fputc(';', out);
1452                 break;
1453         }
1454 }
1455
1456 /**
1457  * Print the AST of a translation unit.
1458  *
1459  * @param unit   the translation unit
1460  */
1461 void print_ast(const translation_unit_t *unit)
1462 {
1463         inc_type_visited();
1464
1465         declaration_t *declaration = unit->scope.declarations;
1466         for( ; declaration != NULL; declaration = declaration->next) {
1467                 if(declaration->storage_class == STORAGE_CLASS_ENUM_ENTRY)
1468                         continue;
1469                 if(declaration->namespc != NAMESPACE_NORMAL &&
1470                                 declaration->symbol == NULL)
1471                         continue;
1472                 if (declaration->implicit)
1473                         continue;
1474
1475                 print_indent();
1476                 print_declaration(declaration);
1477                 fputc('\n', out);
1478         }
1479 }
1480
1481 bool is_constant_initializer(const initializer_t *initializer)
1482 {
1483         switch(initializer->kind) {
1484         case INITIALIZER_STRING:
1485         case INITIALIZER_WIDE_STRING:
1486         case INITIALIZER_DESIGNATOR:
1487                 return true;
1488
1489         case INITIALIZER_VALUE:
1490                 return is_constant_expression(initializer->value.value);
1491
1492         case INITIALIZER_LIST:
1493                 for(size_t i = 0; i < initializer->list.len; ++i) {
1494                         initializer_t *sub_initializer = initializer->list.initializers[i];
1495                         if(!is_constant_initializer(sub_initializer))
1496                                 return false;
1497                 }
1498                 return true;
1499         }
1500         panic("invalid initializer kind found");
1501 }
1502
1503 static bool is_object_with_linker_constant_address(const expression_t *expression)
1504 {
1505         switch(expression->kind) {
1506         case EXPR_UNARY_DEREFERENCE:
1507                 return is_address_constant(expression->unary.value);
1508
1509         case EXPR_SELECT: {
1510                 type_t *base_type = skip_typeref(expression->select.compound->base.type);
1511                 if(is_type_pointer(base_type)) {
1512                         /* it's a -> */
1513                         return is_address_constant(expression->select.compound);
1514                 } else {
1515                         return is_object_with_linker_constant_address(expression->select.compound);
1516                 }
1517         }
1518
1519         case EXPR_ARRAY_ACCESS:
1520                 return is_constant_expression(expression->array_access.index)
1521                         && is_address_constant(expression->array_access.array_ref);
1522
1523         case EXPR_REFERENCE: {
1524                 declaration_t *declaration = expression->reference.declaration;
1525                 switch((storage_class_tag_t) declaration->storage_class) {
1526                 case STORAGE_CLASS_NONE:
1527                 case STORAGE_CLASS_EXTERN:
1528                 case STORAGE_CLASS_STATIC:
1529                         return true;
1530                 default:
1531                         return false;
1532                 }
1533         }
1534
1535         default:
1536                 return false;
1537         }
1538 }
1539
1540 bool is_address_constant(const expression_t *expression)
1541 {
1542         switch(expression->kind) {
1543         case EXPR_UNARY_TAKE_ADDRESS:
1544                 return is_object_with_linker_constant_address(expression->unary.value);
1545
1546         case EXPR_UNARY_DEREFERENCE: {
1547                 type_t *real_type
1548                         = revert_automatic_type_conversion(expression->unary.value);
1549                 /* dereferencing a function is a NOP */
1550                 if(is_type_function(real_type)) {
1551                         return is_address_constant(expression->unary.value);
1552                 }
1553
1554                 /* fallthrough */
1555         }
1556
1557         case EXPR_UNARY_CAST: {
1558                 type_t *dest = skip_typeref(expression->base.type);
1559                 if (!is_type_pointer(dest) &&
1560                                 ! (dest->kind == TYPE_ATOMIC
1561                                         && (get_atomic_type_flags(dest->atomic.akind) & ATOMIC_TYPE_FLAG_INTEGER)
1562                                         && (get_atomic_type_size(dest->atomic.akind) >= get_atomic_type_size(get_intptr_kind()))))
1563                         return false;
1564
1565                 return (is_constant_expression(expression->unary.value)
1566                         || is_address_constant(expression->unary.value));
1567         }
1568
1569         case EXPR_BINARY_ADD:
1570         case EXPR_BINARY_SUB: {
1571                 expression_t *left  = expression->binary.left;
1572                 expression_t *right = expression->binary.right;
1573
1574                 if(is_type_pointer(skip_typeref(left->base.type))) {
1575                         return is_address_constant(left) && is_constant_expression(right);
1576                 } else if(is_type_pointer(skip_typeref(right->base.type))) {
1577                         return is_constant_expression(left)     && is_address_constant(right);
1578                 }
1579
1580                 return false;
1581         }
1582
1583         case EXPR_REFERENCE: {
1584                 declaration_t *declaration = expression->reference.declaration;
1585                 type_t *type = skip_typeref(declaration->type);
1586                 if(is_type_function(type))
1587                         return true;
1588                 if(is_type_array(type)) {
1589                         return is_object_with_linker_constant_address(expression);
1590                 }
1591                 return false;
1592         }
1593
1594         default:
1595                 return false;
1596         }
1597 }
1598
1599 static bool is_builtin_const_call(const expression_t *expression)
1600 {
1601         expression_t *function = expression->call.function;
1602         if (function->kind != EXPR_BUILTIN_SYMBOL) {
1603                 return false;
1604         }
1605
1606         symbol_t *symbol = function->builtin_symbol.symbol;
1607
1608         switch (symbol->ID) {
1609         case T___builtin_huge_val:
1610         case T___builtin_nan:
1611         case T___builtin_nanf:
1612         case T___builtin_nand:
1613                 return true;
1614         }
1615
1616         return false;
1617 }
1618
1619 static bool is_constant_pointer(const expression_t *expression)
1620 {
1621         if (is_constant_expression(expression))
1622                 return true;
1623
1624         switch (expression->kind) {
1625         case EXPR_SELECT:
1626                 return is_constant_pointer(expression->select.compound);
1627         case EXPR_UNARY_CAST:
1628                 return is_constant_pointer(expression->unary.value);
1629         default:
1630                 return false;
1631         }
1632 }
1633
1634 static bool is_object_with_constant_address(const expression_t *expression)
1635 {
1636         switch(expression->kind) {
1637         case EXPR_SELECT: {
1638                 expression_t *compound      = expression->select.compound;
1639                 type_t       *compound_type = compound->base.type;
1640                 compound_type = skip_typeref(compound_type);
1641                 if(is_type_pointer(compound_type)) {
1642                         return is_constant_pointer(compound);
1643                 } else {
1644                         return is_object_with_constant_address(compound);
1645                 }
1646         }
1647         case EXPR_ARRAY_ACCESS:
1648                 return is_constant_pointer(expression->array_access.array_ref)
1649                         && is_constant_expression(expression->array_access.index);
1650         case EXPR_UNARY_DEREFERENCE:
1651                 return is_constant_pointer(expression->unary.value);
1652         default:
1653                 return false;
1654         }
1655 }
1656
1657 bool is_constant_expression(const expression_t *expression)
1658 {
1659         switch(expression->kind) {
1660
1661         case EXPR_CONST:
1662         case EXPR_CHARACTER_CONSTANT:
1663         case EXPR_WIDE_CHARACTER_CONSTANT:
1664         case EXPR_STRING_LITERAL:
1665         case EXPR_WIDE_STRING_LITERAL:
1666         case EXPR_CLASSIFY_TYPE:
1667         case EXPR_FUNCNAME:
1668         case EXPR_OFFSETOF:
1669         case EXPR_ALIGNOF:
1670         case EXPR_BUILTIN_CONSTANT_P:
1671                 return true;
1672
1673         case EXPR_SIZEOF: {
1674                 type_t *type = expression->typeprop.type;
1675                 if (type == NULL)
1676                         type = expression->typeprop.tp_expression->base.type;
1677
1678                 type = skip_typeref(type);
1679                 if (is_type_array(type) && type->array.is_vla)
1680                         return false;
1681                 return true;
1682         }
1683
1684         case EXPR_BUILTIN_SYMBOL:
1685         case EXPR_BUILTIN_PREFETCH:
1686         case EXPR_SELECT:
1687         case EXPR_VA_START:
1688         case EXPR_VA_ARG:
1689         case EXPR_STATEMENT:
1690         case EXPR_UNARY_POSTFIX_INCREMENT:
1691         case EXPR_UNARY_POSTFIX_DECREMENT:
1692         case EXPR_UNARY_PREFIX_INCREMENT:
1693         case EXPR_UNARY_PREFIX_DECREMENT:
1694         case EXPR_UNARY_ASSUME: /* has VOID type */
1695         case EXPR_UNARY_DEREFERENCE:
1696         case EXPR_BINARY_ASSIGN:
1697         case EXPR_BINARY_MUL_ASSIGN:
1698         case EXPR_BINARY_DIV_ASSIGN:
1699         case EXPR_BINARY_MOD_ASSIGN:
1700         case EXPR_BINARY_ADD_ASSIGN:
1701         case EXPR_BINARY_SUB_ASSIGN:
1702         case EXPR_BINARY_SHIFTLEFT_ASSIGN:
1703         case EXPR_BINARY_SHIFTRIGHT_ASSIGN:
1704         case EXPR_BINARY_BITWISE_AND_ASSIGN:
1705         case EXPR_BINARY_BITWISE_XOR_ASSIGN:
1706         case EXPR_BINARY_BITWISE_OR_ASSIGN:
1707         case EXPR_BINARY_COMMA:
1708                 return false;
1709
1710         case EXPR_UNARY_TAKE_ADDRESS:
1711                 return is_object_with_constant_address(expression->unary.value);
1712
1713         case EXPR_CALL:
1714                 return is_builtin_const_call(expression);
1715
1716         case EXPR_UNARY_NEGATE:
1717         case EXPR_UNARY_PLUS:
1718         case EXPR_UNARY_BITWISE_NEGATE:
1719         case EXPR_UNARY_NOT:
1720                 return is_constant_expression(expression->unary.value);
1721
1722         case EXPR_UNARY_CAST:
1723         case EXPR_UNARY_CAST_IMPLICIT:
1724                 return is_type_arithmetic(skip_typeref(expression->base.type))
1725                         && is_constant_expression(expression->unary.value);
1726
1727         case EXPR_BINARY_ADD:
1728         case EXPR_BINARY_SUB:
1729         case EXPR_BINARY_MUL:
1730         case EXPR_BINARY_DIV:
1731         case EXPR_BINARY_MOD:
1732         case EXPR_BINARY_EQUAL:
1733         case EXPR_BINARY_NOTEQUAL:
1734         case EXPR_BINARY_LESS:
1735         case EXPR_BINARY_LESSEQUAL:
1736         case EXPR_BINARY_GREATER:
1737         case EXPR_BINARY_GREATEREQUAL:
1738         case EXPR_BINARY_BITWISE_AND:
1739         case EXPR_BINARY_BITWISE_OR:
1740         case EXPR_BINARY_BITWISE_XOR:
1741         case EXPR_BINARY_LOGICAL_AND:
1742         case EXPR_BINARY_LOGICAL_OR:
1743         case EXPR_BINARY_SHIFTLEFT:
1744         case EXPR_BINARY_SHIFTRIGHT:
1745         case EXPR_BINARY_BUILTIN_EXPECT:
1746         case EXPR_BINARY_ISGREATER:
1747         case EXPR_BINARY_ISGREATEREQUAL:
1748         case EXPR_BINARY_ISLESS:
1749         case EXPR_BINARY_ISLESSEQUAL:
1750         case EXPR_BINARY_ISLESSGREATER:
1751         case EXPR_BINARY_ISUNORDERED:
1752                 return is_constant_expression(expression->binary.left)
1753                         && is_constant_expression(expression->binary.right);
1754
1755         case EXPR_COMPOUND_LITERAL:
1756                 return is_constant_initializer(expression->compound_literal.initializer);
1757
1758         case EXPR_CONDITIONAL: {
1759                 expression_t *condition = expression->conditional.condition;
1760                 if(!is_constant_expression(condition))
1761                         return false;
1762
1763                 long val = fold_constant(condition);
1764                 if(val != 0)
1765                         return is_constant_expression(expression->conditional.true_expression);
1766                 else
1767                         return is_constant_expression(expression->conditional.false_expression);
1768         }
1769
1770         case EXPR_ARRAY_ACCESS:
1771                 return is_constant_expression(expression->array_access.array_ref)
1772                         && is_constant_expression(expression->array_access.index);
1773
1774         case EXPR_REFERENCE: {
1775                 declaration_t *declaration = expression->reference.declaration;
1776                 if(declaration->storage_class == STORAGE_CLASS_ENUM_ENTRY)
1777                         return true;
1778
1779                 return false;
1780         }
1781
1782         case EXPR_INVALID:
1783                 return true;
1784
1785         case EXPR_UNKNOWN:
1786                 break;
1787         }
1788         panic("invalid expression found (is constant expression)");
1789 }
1790
1791 /**
1792  * Initialize the AST construction.
1793  */
1794 void init_ast(void)
1795 {
1796         obstack_init(&ast_obstack);
1797 }
1798
1799 /**
1800  * Free the AST.
1801  */
1802 void exit_ast(void)
1803 {
1804         obstack_free(&ast_obstack, NULL);
1805 }
1806
1807 /**
1808  * Set the output stream for the AST printer.
1809  *
1810  * @param stream  the output stream
1811  */
1812 void ast_set_output(FILE *stream)
1813 {
1814         out = stream;
1815         type_set_output(stream);
1816 }
1817
1818 /**
1819  * Allocate an AST object of the given size.
1820  *
1821  * @param size  the size of the object to allocate
1822  *
1823  * @return  A new allocated object in the AST memeory space.
1824  */
1825 void *(allocate_ast)(size_t size)
1826 {
1827         return _allocate_ast(size);
1828 }