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