Simplify printing suffix of constants.
[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: return;
227         }
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, 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 + 1 - 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, 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, 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, PREC_ASSIGNMENT);
624         if (expression->rw) {
625                 fputc(',', out);
626                 print_expression_prec(expression->rw, PREC_ASSIGNMENT);
627         }
628         if (expression->locality) {
629                 fputc(',', out);
630                 print_expression_prec(expression->locality, 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, PREC_LOGICAL_OR);
643         fputs(" ? ", out);
644         if (expression->true_expression != NULL) {
645                 print_expression_prec(expression->true_expression, PREC_EXPRESSION);
646                 fputs(" : ", out);
647         } else {
648                 fputs(": ", out);
649         }
650         print_expression_prec(expression->false_expression, 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, 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, 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, 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         const char *text;
1324         switch (storage_class) {
1325         case STORAGE_CLASS_NONE:     return;
1326         case STORAGE_CLASS_TYPEDEF:  text = "typedef ";  break;
1327         case STORAGE_CLASS_EXTERN:   text = "extern ";   break;
1328         case STORAGE_CLASS_STATIC:   text = "static ";   break;
1329         case STORAGE_CLASS_AUTO:     text = "auto ";     break;
1330         case STORAGE_CLASS_REGISTER: text = "register "; break;
1331         }
1332         fputs(text, out);
1333 }
1334
1335 /**
1336  * Print an initializer.
1337  *
1338  * @param initializer  the initializer
1339  */
1340 void print_initializer(const initializer_t *initializer)
1341 {
1342         if(initializer == NULL) {
1343                 fputs("{}", out);
1344                 return;
1345         }
1346
1347         switch (initializer->kind) {
1348         case INITIALIZER_VALUE: {
1349                 const initializer_value_t *value = &initializer->value;
1350                 print_expression(value->value);
1351                 return;
1352         }
1353         case INITIALIZER_LIST: {
1354                 assert(initializer->kind == INITIALIZER_LIST);
1355                 fputs("{ ", out);
1356                 const initializer_list_t *list = &initializer->list;
1357
1358                 for(size_t i = 0 ; i < list->len; ++i) {
1359                         const initializer_t *sub_init = list->initializers[i];
1360                         print_initializer(list->initializers[i]);
1361                         if(i < list->len-1) {
1362                                 if(sub_init == NULL || sub_init->kind != INITIALIZER_DESIGNATOR)
1363                                         fputs(", ", out);
1364                         }
1365                 }
1366                 fputs(" }", out);
1367                 return;
1368         }
1369         case INITIALIZER_STRING:
1370                 print_quoted_string(&initializer->string.string, '"', 1);
1371                 return;
1372         case INITIALIZER_WIDE_STRING:
1373                 print_quoted_wide_string(&initializer->wide_string.string, '"', 1);
1374                 return;
1375         case INITIALIZER_DESIGNATOR:
1376                 print_designator(initializer->designator.designator);
1377                 fputs(" = ", out);
1378                 return;
1379         }
1380
1381         panic("invalid initializer kind found");
1382 }
1383
1384 /**
1385  * Print microsoft extended declaration modifiers.
1386  */
1387 static void print_ms_modifiers(const declaration_t *declaration)
1388 {
1389         if((c_mode & _MS) == 0)
1390                 return;
1391
1392         decl_modifiers_t modifiers = declaration->modifiers;
1393
1394         bool        ds_shown = false;
1395         const char *next     = "(";
1396
1397         if (declaration->base.kind == ENTITY_VARIABLE) {
1398                 variable_t *variable = (variable_t*) declaration;
1399                 if (variable->alignment != 0
1400                                 || variable->get_property_sym != NULL
1401                                 || variable->put_property_sym != NULL) {
1402                         if (!ds_shown) {
1403                                 fputs("__declspec", out);
1404                                 ds_shown = true;
1405                         }
1406
1407                         if(variable->alignment != 0) {
1408                                 fputs(next, out); next = ", "; fprintf(out, "align(%u)", variable->alignment);
1409                         }
1410                         if(variable->get_property_sym != NULL
1411                                         || variable->put_property_sym != NULL) {
1412                                 char *comma = "";
1413                                 fputs(next, out); next = ", "; fputs("property(", out);
1414                                 if(variable->get_property_sym != NULL) {
1415                                         fprintf(out, "get=%s", variable->get_property_sym->string);
1416                                         comma = ", ";
1417                                 }
1418                                 if(variable->put_property_sym != NULL)
1419                                         fprintf(out, "%sput=%s", comma, variable->put_property_sym->string);
1420                                 fputc(')', out);
1421                         }
1422                 }
1423         }
1424
1425         /* DM_FORCEINLINE handled outside. */
1426         if ((modifiers & ~DM_FORCEINLINE) != 0) {
1427                 if (!ds_shown) {
1428                         fputs("__declspec", out);
1429                         ds_shown = true;
1430                 }
1431                 if(modifiers & DM_DLLIMPORT) {
1432                         fputs(next, out); next = ", "; fputs("dllimport", out);
1433                 }
1434                 if(modifiers & DM_DLLEXPORT) {
1435                         fputs(next, out); next = ", "; fputs("dllexport", out);
1436                 }
1437                 if(modifiers & DM_THREAD) {
1438                         fputs(next, out); next = ", "; fputs("thread", out);
1439                 }
1440                 if(modifiers & DM_NAKED) {
1441                         fputs(next, out); next = ", "; fputs("naked", out);
1442                 }
1443                 if(modifiers & DM_THREAD) {
1444                         fputs(next, out); next = ", "; fputs("thread", out);
1445                 }
1446                 if(modifiers & DM_SELECTANY) {
1447                         fputs(next, out); next = ", "; fputs("selectany", out);
1448                 }
1449                 if(modifiers & DM_NOTHROW) {
1450                         fputs(next, out); next = ", "; fputs("nothrow", out);
1451                 }
1452                 if(modifiers & DM_NORETURN) {
1453                         fputs(next, out); next = ", "; fputs("noreturn", out);
1454                 }
1455                 if(modifiers & DM_NOINLINE) {
1456                         fputs(next, out); next = ", "; fputs("noinline", out);
1457                 }
1458                 if (modifiers & DM_DEPRECATED) {
1459                         fputs(next, out); next = ", "; fputs("deprecated", out);
1460                         if(declaration->deprecated_string != NULL)
1461                                 fprintf(out, "(\"%s\")",
1462                                         declaration->deprecated_string);
1463                 }
1464                 if(modifiers & DM_RESTRICT) {
1465                         fputs(next, out); next = ", "; fputs("restrict", out);
1466                 }
1467                 if(modifiers & DM_NOALIAS) {
1468                         fputs(next, out); next = ", "; fputs("noalias", out);
1469                 }
1470         }
1471
1472         if (ds_shown)
1473                 fputs(") ", out);
1474 }
1475
1476 static void print_scope(const scope_t *scope)
1477 {
1478         const entity_t *entity = scope->entities;
1479         for ( ; entity != NULL; entity = entity->base.next) {
1480                 print_indent();
1481                 print_entity(entity);
1482                 fputs("\n", out);
1483         }
1484 }
1485
1486 static void print_namespace(const namespace_t *namespace)
1487 {
1488         fputs("namespace ", out);
1489         if (namespace->base.symbol != NULL) {
1490                 fputs(namespace->base.symbol->string, out);
1491                 fputc(' ', out);
1492         }
1493
1494         fputs("{\n", out);
1495         ++indent;
1496
1497         print_scope(&namespace->members);
1498
1499         --indent;
1500         print_indent();
1501         fputs("}\n", out);
1502 }
1503
1504 /**
1505  * Print a variable or function declaration
1506  */
1507 void print_declaration(const entity_t *entity)
1508 {
1509         assert(is_declaration(entity));
1510         const declaration_t *declaration = &entity->declaration;
1511
1512         print_storage_class((storage_class_tag_t) declaration->declared_storage_class);
1513         if (entity->kind == ENTITY_FUNCTION) {
1514                 function_t *function = (function_t*) declaration;
1515                 if (function->is_inline) {
1516                         if (declaration->modifiers & DM_FORCEINLINE) {
1517                                 fputs("__forceinline ", out);
1518                         } else if (declaration->modifiers & DM_MICROSOFT_INLINE) {
1519                                 fputs("__inline ", out);
1520                         } else {
1521                                 fputs("inline ", out);
1522                         }
1523                 }
1524         }
1525         print_ms_modifiers(declaration);
1526         switch (entity->kind) {
1527                 case ENTITY_FUNCTION:
1528                         print_type_ext(entity->declaration.type, entity->base.symbol,
1529                                         &entity->function.parameters);
1530
1531                         if (entity->function.statement != NULL) {
1532                                 fputc('\n', out);
1533                                 print_indent();
1534                                 print_statement(entity->function.statement);
1535                                 return;
1536                         }
1537                         break;
1538
1539                 case ENTITY_VARIABLE:
1540                         if (entity->variable.thread_local)
1541                                 fputs("__thread ", out);
1542                         print_type_ext(declaration->type, declaration->base.symbol, NULL);
1543                         if (entity->variable.initializer != NULL) {
1544                                 fputs(" = ", out);
1545                                 print_initializer(entity->variable.initializer);
1546                         }
1547                         break;
1548
1549                 default:
1550                         print_type_ext(declaration->type, declaration->base.symbol, NULL);
1551                         break;
1552         }
1553         fputc(';', out);
1554 }
1555
1556 /**
1557  * Prints an expression.
1558  *
1559  * @param expression  the expression
1560  */
1561 void print_expression(const expression_t *expression)
1562 {
1563         print_expression_prec(expression, PREC_BOTTOM);
1564 }
1565
1566 /**
1567  * Print a declaration.
1568  *
1569  * @param declaration  the declaration
1570  */
1571 void print_entity(const entity_t *entity)
1572 {
1573         if (entity->base.namespc != NAMESPACE_NORMAL && entity->base.symbol == NULL)
1574                 return;
1575
1576         switch ((entity_kind_tag_t) entity->kind) {
1577         case ENTITY_VARIABLE:
1578         case ENTITY_COMPOUND_MEMBER:
1579                 print_declaration(entity);
1580                 return;
1581         case ENTITY_FUNCTION:
1582                 print_declaration(entity);
1583                 return;
1584         case ENTITY_TYPEDEF:
1585                 print_typedef(entity);
1586                 return;
1587         case ENTITY_STRUCT:
1588                 fputs("struct ", out);
1589                 fputs(entity->base.symbol->string, out);
1590                 if (entity->structe.complete) {
1591                         fputc(' ', out);
1592                         print_compound_definition(&entity->structe);
1593                 }
1594                 fputc(';', out);
1595                 return;
1596         case ENTITY_UNION:
1597                 fputs("union ", out);
1598                 fputs(entity->base.symbol->string, out);
1599                 if (entity->unione.complete) {
1600                         fputc(' ', out);
1601                         print_compound_definition(&entity->unione);
1602                 }
1603                 fputc(';', out);
1604                 return;
1605         case ENTITY_ENUM:
1606                 fputs("enum ", out);
1607                 fputs(entity->base.symbol->string, out);
1608                 fputc(' ', out);
1609                 print_enum_definition(&entity->enume);
1610                 fputc(';', out);
1611                 return;
1612         case ENTITY_NAMESPACE:
1613                 print_namespace(&entity->namespacee);
1614                 return;
1615         case ENTITY_LABEL:
1616         case ENTITY_ENUM_VALUE:
1617         case ENTITY_LOCAL_LABEL:
1618                 panic("print_entity used on unexpected entity type");
1619         case ENTITY_INVALID:
1620                 break;
1621         }
1622         panic("Invalid entity type encountered");
1623 }
1624
1625 /**
1626  * Print the AST of a translation unit.
1627  *
1628  * @param unit   the translation unit
1629  */
1630 void print_ast(const translation_unit_t *unit)
1631 {
1632         inc_type_visited();
1633
1634         entity_t *entity = unit->scope.entities;
1635         for ( ; entity != NULL; entity = entity->base.next) {
1636                 if (entity->kind == ENTITY_ENUM_VALUE)
1637                         continue;
1638                 if (entity->base.namespc != NAMESPACE_NORMAL
1639                                 && entity->base.symbol == NULL)
1640                         continue;
1641                 if (is_generated_entity(entity))
1642                         continue;
1643
1644                 print_indent();
1645                 print_entity(entity);
1646                 fputc('\n', out);
1647         }
1648 }
1649
1650 bool is_constant_initializer(const initializer_t *initializer)
1651 {
1652         switch (initializer->kind) {
1653         case INITIALIZER_STRING:
1654         case INITIALIZER_WIDE_STRING:
1655         case INITIALIZER_DESIGNATOR:
1656                 return true;
1657
1658         case INITIALIZER_VALUE:
1659                 return is_constant_expression(initializer->value.value);
1660
1661         case INITIALIZER_LIST:
1662                 for(size_t i = 0; i < initializer->list.len; ++i) {
1663                         initializer_t *sub_initializer = initializer->list.initializers[i];
1664                         if(!is_constant_initializer(sub_initializer))
1665                                 return false;
1666                 }
1667                 return true;
1668         }
1669         panic("invalid initializer kind found");
1670 }
1671
1672 static bool is_object_with_linker_constant_address(const expression_t *expression)
1673 {
1674         switch (expression->kind) {
1675         case EXPR_UNARY_DEREFERENCE:
1676                 return is_address_constant(expression->unary.value);
1677
1678         case EXPR_SELECT: {
1679                 type_t *base_type = skip_typeref(expression->select.compound->base.type);
1680                 if(is_type_pointer(base_type)) {
1681                         /* it's a -> */
1682                         return is_address_constant(expression->select.compound);
1683                 } else {
1684                         return is_object_with_linker_constant_address(expression->select.compound);
1685                 }
1686         }
1687
1688         case EXPR_ARRAY_ACCESS:
1689                 return is_constant_expression(expression->array_access.index)
1690                         && is_address_constant(expression->array_access.array_ref);
1691
1692         case EXPR_REFERENCE: {
1693                 entity_t *entity = expression->reference.entity;
1694                 if (is_declaration(entity)) {
1695                         switch ((storage_class_tag_t) entity->declaration.storage_class) {
1696                         case STORAGE_CLASS_NONE:
1697                         case STORAGE_CLASS_EXTERN:
1698                         case STORAGE_CLASS_STATIC:
1699                                 return
1700                                         entity->kind != ENTITY_VARIABLE ||
1701                                         !entity->variable.thread_local;
1702
1703                         case STORAGE_CLASS_REGISTER:
1704                         case STORAGE_CLASS_TYPEDEF:
1705                         case STORAGE_CLASS_AUTO:
1706                                 break;
1707                         }
1708                 }
1709                 return false;
1710         }
1711
1712         default:
1713                 return false;
1714         }
1715 }
1716
1717 bool is_address_constant(const expression_t *expression)
1718 {
1719         switch (expression->kind) {
1720         case EXPR_UNARY_TAKE_ADDRESS:
1721                 return is_object_with_linker_constant_address(expression->unary.value);
1722
1723         case EXPR_UNARY_DEREFERENCE: {
1724                 type_t *real_type
1725                         = revert_automatic_type_conversion(expression->unary.value);
1726                 /* dereferencing a function is a NOP */
1727                 if(is_type_function(real_type)) {
1728                         return is_address_constant(expression->unary.value);
1729                 }
1730
1731                 /* fallthrough */
1732         }
1733
1734         case EXPR_UNARY_CAST: {
1735                 type_t *dest = skip_typeref(expression->base.type);
1736                 if (!is_type_pointer(dest) && (
1737                         dest->kind != TYPE_ATOMIC                                               ||
1738                         !(get_atomic_type_flags(dest->atomic.akind) & ATOMIC_TYPE_FLAG_INTEGER) ||
1739                         (get_atomic_type_size(dest->atomic.akind) < get_atomic_type_size(get_intptr_kind())
1740                     )))
1741                         return false;
1742
1743                 return (is_constant_expression(expression->unary.value)
1744                         || is_address_constant(expression->unary.value));
1745         }
1746
1747         case EXPR_BINARY_ADD:
1748         case EXPR_BINARY_SUB: {
1749                 expression_t *left  = expression->binary.left;
1750                 expression_t *right = expression->binary.right;
1751
1752                 if(is_type_pointer(skip_typeref(left->base.type))) {
1753                         return is_address_constant(left) && is_constant_expression(right);
1754                 } else if(is_type_pointer(skip_typeref(right->base.type))) {
1755                         return is_constant_expression(left)     && is_address_constant(right);
1756                 }
1757
1758                 return false;
1759         }
1760
1761         case EXPR_REFERENCE: {
1762                 entity_t *entity = expression->reference.entity;
1763                 if (!is_declaration(entity))
1764                         return false;
1765
1766                 type_t *type = skip_typeref(entity->declaration.type);
1767                 if(is_type_function(type))
1768                         return true;
1769                 if(is_type_array(type)) {
1770                         return is_object_with_linker_constant_address(expression);
1771                 }
1772                 /* Prevent stray errors */
1773                 if (!is_type_valid(type))
1774                         return true;
1775                 return false;
1776         }
1777
1778         case EXPR_ARRAY_ACCESS: {
1779                 type_t *const type =
1780                         skip_typeref(revert_automatic_type_conversion(expression));
1781                 return
1782                         is_type_array(type)                                    &&
1783                         is_constant_expression(expression->array_access.index) &&
1784                         is_address_constant(expression->array_access.array_ref);
1785         }
1786
1787         default:
1788                 return false;
1789         }
1790 }
1791
1792 static bool is_builtin_const_call(const expression_t *expression)
1793 {
1794         expression_t *function = expression->call.function;
1795         if (function->kind != EXPR_BUILTIN_SYMBOL) {
1796                 return false;
1797         }
1798
1799         symbol_t *symbol = function->builtin_symbol.symbol;
1800
1801         switch (symbol->ID) {
1802         case T___builtin_huge_val:
1803         case T___builtin_inf:
1804         case T___builtin_inff:
1805         case T___builtin_infl:
1806         case T___builtin_nan:
1807         case T___builtin_nanf:
1808         case T___builtin_nanl:
1809                 return true;
1810         }
1811
1812         return false;
1813 }
1814
1815 static bool is_constant_pointer(const expression_t *expression)
1816 {
1817         if (is_constant_expression(expression))
1818                 return true;
1819
1820         switch (expression->kind) {
1821         case EXPR_UNARY_CAST:
1822                 return is_constant_pointer(expression->unary.value);
1823         default:
1824                 return false;
1825         }
1826 }
1827
1828 static bool is_object_with_constant_address(const expression_t *expression)
1829 {
1830         switch (expression->kind) {
1831         case EXPR_SELECT: {
1832                 expression_t *compound      = expression->select.compound;
1833                 type_t       *compound_type = compound->base.type;
1834                 compound_type = skip_typeref(compound_type);
1835                 if(is_type_pointer(compound_type)) {
1836                         return is_constant_pointer(compound);
1837                 } else {
1838                         return is_object_with_constant_address(compound);
1839                 }
1840         }
1841
1842         case EXPR_ARRAY_ACCESS: {
1843                 array_access_expression_t const* const array_access =
1844                         &expression->array_access;
1845                 return
1846                         is_constant_expression(array_access->index) && (
1847                                 is_object_with_constant_address(array_access->array_ref) ||
1848                                 is_constant_pointer(array_access->array_ref)
1849                         );
1850         }
1851
1852         case EXPR_UNARY_DEREFERENCE:
1853                 return is_constant_pointer(expression->unary.value);
1854         default:
1855                 return false;
1856         }
1857 }
1858
1859 bool is_constant_expression(const expression_t *expression)
1860 {
1861         switch (expression->kind) {
1862
1863         case EXPR_CONST:
1864         case EXPR_CHARACTER_CONSTANT:
1865         case EXPR_WIDE_CHARACTER_CONSTANT:
1866         case EXPR_STRING_LITERAL:
1867         case EXPR_WIDE_STRING_LITERAL:
1868         case EXPR_CLASSIFY_TYPE:
1869         case EXPR_FUNCNAME:
1870         case EXPR_OFFSETOF:
1871         case EXPR_ALIGNOF:
1872         case EXPR_BUILTIN_CONSTANT_P:
1873         case EXPR_LABEL_ADDRESS:
1874         case EXPR_REFERENCE_ENUM_VALUE:
1875                 return true;
1876
1877         case EXPR_SIZEOF: {
1878                 type_t *type = expression->typeprop.type;
1879                 if (type == NULL)
1880                         type = expression->typeprop.tp_expression->base.type;
1881
1882                 type = skip_typeref(type);
1883                 if (is_type_array(type) && type->array.is_vla)
1884                         return false;
1885                 return true;
1886         }
1887
1888         case EXPR_BUILTIN_SYMBOL:
1889         case EXPR_BUILTIN_PREFETCH:
1890         case EXPR_SELECT:
1891         case EXPR_VA_START:
1892         case EXPR_VA_ARG:
1893         case EXPR_STATEMENT:
1894         case EXPR_REFERENCE:
1895         case EXPR_UNARY_POSTFIX_INCREMENT:
1896         case EXPR_UNARY_POSTFIX_DECREMENT:
1897         case EXPR_UNARY_PREFIX_INCREMENT:
1898         case EXPR_UNARY_PREFIX_DECREMENT:
1899         case EXPR_UNARY_ASSUME: /* has VOID type */
1900         case EXPR_UNARY_DEREFERENCE:
1901         case EXPR_UNARY_DELETE:
1902         case EXPR_UNARY_DELETE_ARRAY:
1903         case EXPR_UNARY_THROW:
1904         case EXPR_BINARY_ASSIGN:
1905         case EXPR_BINARY_MUL_ASSIGN:
1906         case EXPR_BINARY_DIV_ASSIGN:
1907         case EXPR_BINARY_MOD_ASSIGN:
1908         case EXPR_BINARY_ADD_ASSIGN:
1909         case EXPR_BINARY_SUB_ASSIGN:
1910         case EXPR_BINARY_SHIFTLEFT_ASSIGN:
1911         case EXPR_BINARY_SHIFTRIGHT_ASSIGN:
1912         case EXPR_BINARY_BITWISE_AND_ASSIGN:
1913         case EXPR_BINARY_BITWISE_XOR_ASSIGN:
1914         case EXPR_BINARY_BITWISE_OR_ASSIGN:
1915         case EXPR_BINARY_COMMA:
1916         case EXPR_ARRAY_ACCESS:
1917                 return false;
1918
1919         case EXPR_UNARY_TAKE_ADDRESS:
1920                 return is_object_with_constant_address(expression->unary.value);
1921
1922         case EXPR_CALL:
1923                 return is_builtin_const_call(expression);
1924
1925         case EXPR_UNARY_NEGATE:
1926         case EXPR_UNARY_PLUS:
1927         case EXPR_UNARY_BITWISE_NEGATE:
1928         case EXPR_UNARY_NOT:
1929                 return is_constant_expression(expression->unary.value);
1930
1931         case EXPR_UNARY_CAST:
1932         case EXPR_UNARY_CAST_IMPLICIT:
1933                 return is_type_arithmetic(skip_typeref(expression->base.type))
1934                         && is_constant_expression(expression->unary.value);
1935
1936         case EXPR_BINARY_ADD:
1937         case EXPR_BINARY_SUB:
1938         case EXPR_BINARY_MUL:
1939         case EXPR_BINARY_DIV:
1940         case EXPR_BINARY_MOD:
1941         case EXPR_BINARY_EQUAL:
1942         case EXPR_BINARY_NOTEQUAL:
1943         case EXPR_BINARY_LESS:
1944         case EXPR_BINARY_LESSEQUAL:
1945         case EXPR_BINARY_GREATER:
1946         case EXPR_BINARY_GREATEREQUAL:
1947         case EXPR_BINARY_BITWISE_AND:
1948         case EXPR_BINARY_BITWISE_OR:
1949         case EXPR_BINARY_BITWISE_XOR:
1950         case EXPR_BINARY_LOGICAL_AND:
1951         case EXPR_BINARY_LOGICAL_OR:
1952         case EXPR_BINARY_SHIFTLEFT:
1953         case EXPR_BINARY_SHIFTRIGHT:
1954         case EXPR_BINARY_BUILTIN_EXPECT:
1955         case EXPR_BINARY_ISGREATER:
1956         case EXPR_BINARY_ISGREATEREQUAL:
1957         case EXPR_BINARY_ISLESS:
1958         case EXPR_BINARY_ISLESSEQUAL:
1959         case EXPR_BINARY_ISLESSGREATER:
1960         case EXPR_BINARY_ISUNORDERED:
1961                 return is_constant_expression(expression->binary.left)
1962                         && is_constant_expression(expression->binary.right);
1963
1964         case EXPR_COMPOUND_LITERAL:
1965                 return is_constant_initializer(expression->compound_literal.initializer);
1966
1967         case EXPR_CONDITIONAL: {
1968                 expression_t *condition = expression->conditional.condition;
1969                 if(!is_constant_expression(condition))
1970                         return false;
1971
1972                 long val = fold_constant(condition);
1973                 if(val != 0)
1974                         return is_constant_expression(expression->conditional.true_expression);
1975                 else
1976                         return is_constant_expression(expression->conditional.false_expression);
1977         }
1978
1979         case EXPR_INVALID:
1980                 return true;
1981
1982         case EXPR_UNKNOWN:
1983                 break;
1984         }
1985         panic("invalid expression found (is constant expression)");
1986 }
1987
1988 /**
1989  * Initialize the AST construction.
1990  */
1991 void init_ast(void)
1992 {
1993         obstack_init(&ast_obstack);
1994 }
1995
1996 /**
1997  * Free the AST.
1998  */
1999 void exit_ast(void)
2000 {
2001         obstack_free(&ast_obstack, NULL);
2002 }
2003
2004 /**
2005  * Set the output stream for the AST printer.
2006  *
2007  * @param stream  the output stream
2008  */
2009 void ast_set_output(FILE *stream)
2010 {
2011         out = stream;
2012         type_set_output(stream);
2013 }
2014
2015 /**
2016  * Allocate an AST object of the given size.
2017  *
2018  * @param size  the size of the object to allocate
2019  *
2020  * @return  A new allocated object in the AST memeory space.
2021  */
2022 void *(allocate_ast)(size_t size)
2023 {
2024         return _allocate_ast(size);
2025 }