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