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