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