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