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