- fixed warnings
[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 #include "adt/util.h"
44
45 struct obstack ast_obstack;
46
47 static FILE *out;
48 static int   indent;
49
50 /** If set, implicit casts are printed. */
51 bool print_implicit_casts = false;
52
53 /** If set parenthesis are printed to indicate operator precedence. */
54 bool print_parenthesis = false;
55
56 static void print_statement(const statement_t *statement);
57 static void print_expression_prec(const expression_t *expression, unsigned prec);
58
59 void change_indent(int delta)
60 {
61         indent += delta;
62         assert(indent >= 0);
63 }
64
65 void print_indent(void)
66 {
67         for (int i = 0; i < indent; ++i)
68                 fputc('\t', out);
69 }
70
71 /**
72  * Returns 1 if a given precedence level has right-to-left
73  * associativity, else 0.
74  *
75  * @param precedence   the operator precedence
76  */
77 static int right_to_left(unsigned precedence)
78 {
79         switch (precedence) {
80                 case PREC_ASSIGNMENT:
81                 case PREC_CONDITIONAL:
82                 case PREC_UNARY:
83                         return 1;
84
85                 default:
86                         return 0;
87         }
88 }
89
90 /**
91  * Return the precedence of an expression given by its kind.
92  *
93  * @param kind   the expression kind
94  */
95 static unsigned get_expression_precedence(expression_kind_t kind)
96 {
97         static const unsigned prec[] = {
98                 [EXPR_UNKNOWN]                    = PREC_PRIMARY,
99                 [EXPR_INVALID]                    = PREC_PRIMARY,
100                 [EXPR_REFERENCE]                  = PREC_PRIMARY,
101                 [EXPR_REFERENCE_ENUM_VALUE]       = PREC_PRIMARY,
102                 [EXPR_CHARACTER_CONSTANT]         = PREC_PRIMARY,
103                 [EXPR_WIDE_CHARACTER_CONSTANT]    = PREC_PRIMARY,
104                 [EXPR_CONST]                      = PREC_PRIMARY,
105                 [EXPR_STRING_LITERAL]             = PREC_PRIMARY,
106                 [EXPR_WIDE_STRING_LITERAL]        = PREC_PRIMARY,
107                 [EXPR_COMPOUND_LITERAL]           = PREC_UNARY,
108                 [EXPR_CALL]                       = PREC_POSTFIX,
109                 [EXPR_CONDITIONAL]                = PREC_CONDITIONAL,
110                 [EXPR_SELECT]                     = PREC_POSTFIX,
111                 [EXPR_ARRAY_ACCESS]               = PREC_POSTFIX,
112                 [EXPR_SIZEOF]                     = PREC_UNARY,
113                 [EXPR_CLASSIFY_TYPE]              = PREC_UNARY,
114                 [EXPR_ALIGNOF]                    = PREC_UNARY,
115
116                 [EXPR_FUNCNAME]                   = PREC_PRIMARY,
117                 [EXPR_BUILTIN_CONSTANT_P]         = PREC_PRIMARY,
118                 [EXPR_BUILTIN_TYPES_COMPATIBLE_P] = 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((size_t)kind < lengthof(prec));
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                 /* PREC_TOP: always print the '()' here, sizeof x is right but unusual */
578                 print_expression_prec(expression->tp_expression, PREC_TOP);
579         } else {
580                 fputc('(', out);
581                 print_type(expression->type);
582                 fputc(')', out);
583         }
584 }
585
586 /**
587  * Prints a builtin constant expression.
588  *
589  * @param expression   the builtin constant expression
590  */
591 static void print_builtin_constant(const builtin_constant_expression_t *expression)
592 {
593         fputs("__builtin_constant_p(", out);
594         print_assignment_expression(expression->value);
595         fputc(')', out);
596 }
597
598 /**
599  * Prints a builtin types compatible expression.
600  *
601  * @param expression   the builtin types compatible expression
602  */
603 static void print_builtin_types_compatible(
604                 const builtin_types_compatible_expression_t *expression)
605 {
606         fputs("__builtin_types_compatible_p(", out);
607         print_type(expression->left);
608         fputs(", ", out);
609         print_type(expression->right);
610         fputc(')', out);
611 }
612
613 /**
614  * Prints a conditional expression.
615  *
616  * @param expression   the conditional expression
617  */
618 static void print_conditional(const conditional_expression_t *expression)
619 {
620         print_expression_prec(expression->condition, PREC_LOGICAL_OR);
621         if (expression->true_expression != NULL) {
622                 fputs(" ? ", out);
623                 print_expression_prec(expression->true_expression, PREC_EXPRESSION);
624                 fputs(" : ", out);
625         } else {
626                 fputs(" ?: ", out);
627         }
628         precedence_t prec = c_mode & _CXX ? PREC_ASSIGNMENT : PREC_CONDITIONAL;
629         print_expression_prec(expression->false_expression, prec);
630 }
631
632 /**
633  * Prints a va_start expression.
634  *
635  * @param expression   the va_start expression
636  */
637 static void print_va_start(const va_start_expression_t *const expression)
638 {
639         fputs("__builtin_va_start(", out);
640         print_assignment_expression(expression->ap);
641         fputs(", ", out);
642         fputs(expression->parameter->base.base.symbol->string, out);
643         fputc(')', out);
644 }
645
646 /**
647  * Prints a va_arg expression.
648  *
649  * @param expression   the va_arg expression
650  */
651 static void print_va_arg(const va_arg_expression_t *expression)
652 {
653         fputs("__builtin_va_arg(", out);
654         print_assignment_expression(expression->ap);
655         fputs(", ", out);
656         print_type(expression->base.type);
657         fputc(')', out);
658 }
659
660 /**
661  * Prints a select expression (. or ->).
662  *
663  * @param expression   the select expression
664  */
665 static void print_select(const select_expression_t *expression)
666 {
667         unsigned prec = get_expression_precedence(expression->base.kind);
668         print_expression_prec(expression->compound, prec);
669         if (is_type_pointer(skip_typeref(expression->compound->base.type))) {
670                 fputs("->", out);
671         } else {
672                 fputc('.', out);
673         }
674         fputs(expression->compound_entry->base.symbol->string, out);
675 }
676
677 /**
678  * Prints a type classify expression.
679  *
680  * @param expr   the type classify expression
681  */
682 static void print_classify_type_expression(
683         const classify_type_expression_t *const expr)
684 {
685         fputs("__builtin_classify_type(", out);
686         print_assignment_expression(expr->type_expression);
687         fputc(')', out);
688 }
689
690 /**
691  * Prints a designator.
692  *
693  * @param designator  the designator
694  */
695 static void print_designator(const designator_t *designator)
696 {
697         for ( ; designator != NULL; designator = designator->next) {
698                 if (designator->symbol == NULL) {
699                         fputc('[', out);
700                         print_expression(designator->array_index);
701                         fputc(']', out);
702                 } else {
703                         fputc('.', out);
704                         fputs(designator->symbol->string, out);
705                 }
706         }
707 }
708
709 /**
710  * Prints an offsetof expression.
711  *
712  * @param expression   the offset expression
713  */
714 static void print_offsetof_expression(const offsetof_expression_t *expression)
715 {
716         fputs("__builtin_offsetof", out);
717         fputc('(', out);
718         print_type(expression->type);
719         fputc(',', out);
720         print_designator(expression->designator);
721         fputc(')', out);
722 }
723
724 /**
725  * Prints a statement expression.
726  *
727  * @param expression   the statement expression
728  */
729 static void print_statement_expression(const statement_expression_t *expression)
730 {
731         fputc('(', out);
732         print_statement(expression->statement);
733         fputc(')', out);
734 }
735
736 /**
737  * Prints an expression with parenthesis if needed.
738  *
739  * @param expression  the expression to print
740  * @param top_prec    the precedence of the user of this expression.
741  */
742 static void print_expression_prec(const expression_t *expression, unsigned top_prec)
743 {
744         if (expression->kind == EXPR_UNARY_CAST_IMPLICIT && !print_implicit_casts) {
745                 expression = expression->unary.value;
746         }
747
748         bool parenthesized =
749                 expression->base.parenthesized                 ||
750                 (print_parenthesis && top_prec != PREC_BOTTOM) ||
751                 top_prec > get_expression_precedence(expression->base.kind);
752
753         if (parenthesized)
754                 fputc('(', out);
755         switch (expression->kind) {
756         case EXPR_UNKNOWN:
757         case EXPR_INVALID:
758                 fputs("$invalid expression$", out);
759                 break;
760         case EXPR_CHARACTER_CONSTANT:
761                 print_character_constant(&expression->conste);
762                 break;
763         case EXPR_WIDE_CHARACTER_CONSTANT:
764                 print_wide_character_constant(&expression->conste);
765                 break;
766         case EXPR_CONST:
767                 print_const(&expression->conste);
768                 break;
769         case EXPR_FUNCNAME:
770                 print_funcname(&expression->funcname);
771                 break;
772         case EXPR_STRING_LITERAL:
773                 print_string_literal(&expression->string);
774                 break;
775         case EXPR_WIDE_STRING_LITERAL:
776                 print_wide_string_literal(&expression->wide_string);
777                 break;
778         case EXPR_COMPOUND_LITERAL:
779                 print_compound_literal(&expression->compound_literal);
780                 break;
781         case EXPR_CALL:
782                 print_call_expression(&expression->call);
783                 break;
784         EXPR_BINARY_CASES
785                 print_binary_expression(&expression->binary);
786                 break;
787         case EXPR_REFERENCE:
788         case EXPR_REFERENCE_ENUM_VALUE:
789                 print_reference_expression(&expression->reference);
790                 break;
791         case EXPR_ARRAY_ACCESS:
792                 print_array_expression(&expression->array_access);
793                 break;
794         case EXPR_LABEL_ADDRESS:
795                 print_label_address_expression(&expression->label_address);
796                 break;
797         EXPR_UNARY_CASES
798                 print_unary_expression(&expression->unary);
799                 break;
800         case EXPR_SIZEOF:
801         case EXPR_ALIGNOF:
802                 print_typeprop_expression(&expression->typeprop);
803                 break;
804         case EXPR_BUILTIN_CONSTANT_P:
805                 print_builtin_constant(&expression->builtin_constant);
806                 break;
807         case EXPR_BUILTIN_TYPES_COMPATIBLE_P:
808                 print_builtin_types_compatible(&expression->builtin_types_compatible);
809                 break;
810         case EXPR_CONDITIONAL:
811                 print_conditional(&expression->conditional);
812                 break;
813         case EXPR_VA_START:
814                 print_va_start(&expression->va_starte);
815                 break;
816         case EXPR_VA_ARG:
817                 print_va_arg(&expression->va_arge);
818                 break;
819         case EXPR_SELECT:
820                 print_select(&expression->select);
821                 break;
822         case EXPR_CLASSIFY_TYPE:
823                 print_classify_type_expression(&expression->classify_type);
824                 break;
825         case EXPR_OFFSETOF:
826                 print_offsetof_expression(&expression->offsetofe);
827                 break;
828         case EXPR_STATEMENT:
829                 print_statement_expression(&expression->statement);
830                 break;
831
832         default:
833                 /* TODO */
834                 fprintf(out, "some expression of type %d", (int)expression->kind);
835                 break;
836         }
837         if (parenthesized)
838                 fputc(')', out);
839 }
840
841 /**
842  * Print an compound statement.
843  *
844  * @param block  the compound statement
845  */
846 static void print_compound_statement(const compound_statement_t *block)
847 {
848         fputs("{\n", out);
849         ++indent;
850
851         statement_t *statement = block->statements;
852         while (statement != NULL) {
853                 if (statement->base.kind == STATEMENT_CASE_LABEL)
854                         --indent;
855                 if (statement->kind != STATEMENT_LABEL)
856                         print_indent();
857                 print_statement(statement);
858
859                 statement = statement->base.next;
860         }
861         --indent;
862         print_indent();
863         fputs(block->stmt_expr ? "}" : "}\n", out);
864 }
865
866 /**
867  * Print a return statement.
868  *
869  * @param statement  the return statement
870  */
871 static void print_return_statement(const return_statement_t *statement)
872 {
873         expression_t const *const val = statement->value;
874         if (val != NULL) {
875                 fputs("return ", out);
876                 print_expression(val);
877                 fputs(";\n", out);
878         } else {
879                 fputs("return;\n", out);
880         }
881 }
882
883 /**
884  * Print an expression statement.
885  *
886  * @param statement  the expression statement
887  */
888 static void print_expression_statement(const expression_statement_t *statement)
889 {
890         print_expression(statement->expression);
891         fputs(";\n", out);
892 }
893
894 /**
895  * Print a goto statement.
896  *
897  * @param statement  the goto statement
898  */
899 static void print_goto_statement(const goto_statement_t *statement)
900 {
901         fputs("goto ", out);
902         if (statement->expression != NULL) {
903                 fputc('*', out);
904                 print_expression(statement->expression);
905         } else {
906                 fputs(statement->label->base.symbol->string, out);
907         }
908         fputs(";\n", out);
909 }
910
911 /**
912  * Print a label statement.
913  *
914  * @param statement  the label statement
915  */
916 static void print_label_statement(const label_statement_t *statement)
917 {
918         fprintf(out, "%s:\n", statement->label->base.symbol->string);
919         print_indent();
920         print_statement(statement->statement);
921 }
922
923 /**
924  * Print an if statement.
925  *
926  * @param statement  the if statement
927  */
928 static void print_if_statement(const if_statement_t *statement)
929 {
930         fputs("if (", out);
931         print_expression(statement->condition);
932         fputs(") ", out);
933         print_statement(statement->true_statement);
934
935         if (statement->false_statement != NULL) {
936                 print_indent();
937                 fputs("else ", out);
938                 print_statement(statement->false_statement);
939         }
940 }
941
942 /**
943  * Print a switch statement.
944  *
945  * @param statement  the switch statement
946  */
947 static void print_switch_statement(const switch_statement_t *statement)
948 {
949         fputs("switch (", out);
950         print_expression(statement->expression);
951         fputs(") ", out);
952         print_statement(statement->body);
953 }
954
955 /**
956  * Print a case label (including the default label).
957  *
958  * @param statement  the case label statement
959  */
960 static void print_case_label(const case_label_statement_t *statement)
961 {
962         if (statement->expression == NULL) {
963                 fputs("default:\n", out);
964         } else {
965                 fputs("case ", out);
966                 print_expression(statement->expression);
967                 if (statement->end_range != NULL) {
968                         fputs(" ... ", out);
969                         print_expression(statement->end_range);
970                 }
971                 fputs(":\n", out);
972         }
973         ++indent;
974         if (statement->statement != NULL) {
975                 if (statement->statement->base.kind == STATEMENT_CASE_LABEL) {
976                         --indent;
977                 }
978                 print_indent();
979                 print_statement(statement->statement);
980         }
981 }
982
983 static void print_typedef(const entity_t *entity)
984 {
985         fputs("typedef ", out);
986         print_type_ext(entity->typedefe.type, entity->base.symbol, NULL);
987         fputc(';', out);
988 }
989
990 /**
991  * returns true if the entity is a compiler generated one and has no real
992  * correspondenc in the source file
993  */
994 static bool is_generated_entity(const entity_t *entity)
995 {
996         if (entity->kind == ENTITY_TYPEDEF)
997                 return entity->typedefe.builtin;
998
999         if (is_declaration(entity))
1000                 return entity->declaration.implicit;
1001
1002         return false;
1003 }
1004
1005 /**
1006  * Print a declaration statement.
1007  *
1008  * @param statement   the statement
1009  */
1010 static void print_declaration_statement(
1011                 const declaration_statement_t *statement)
1012 {
1013         bool first = true;
1014         entity_t *entity = statement->declarations_begin;
1015         if (entity == NULL) {
1016                 fputs("/* empty declaration statement */\n", out);
1017                 return;
1018         }
1019
1020         entity_t *const end = statement->declarations_end->base.next;
1021         for (; entity != end; entity = entity->base.next) {
1022                 if (entity->kind == ENTITY_ENUM_VALUE)
1023                         continue;
1024                 if (is_generated_entity(entity))
1025                         continue;
1026
1027                 if (!first) {
1028                         print_indent();
1029                 } else {
1030                         first = false;
1031                 }
1032
1033                 print_entity(entity);
1034                 fputc('\n', out);
1035         }
1036 }
1037
1038 /**
1039  * Print a while statement.
1040  *
1041  * @param statement   the statement
1042  */
1043 static void print_while_statement(const while_statement_t *statement)
1044 {
1045         fputs("while (", out);
1046         print_expression(statement->condition);
1047         fputs(") ", out);
1048         print_statement(statement->body);
1049 }
1050
1051 /**
1052  * Print a do-while statement.
1053  *
1054  * @param statement   the statement
1055  */
1056 static void print_do_while_statement(const do_while_statement_t *statement)
1057 {
1058         fputs("do ", out);
1059         print_statement(statement->body);
1060         print_indent();
1061         fputs("while (", out);
1062         print_expression(statement->condition);
1063         fputs(");\n", out);
1064 }
1065
1066 /**
1067  * Print a for statement.
1068  *
1069  * @param statement   the statement
1070  */
1071 static void print_for_statement(const for_statement_t *statement)
1072 {
1073         fputs("for (", out);
1074         if (statement->initialisation != NULL) {
1075                 print_expression(statement->initialisation);
1076                 fputc(';', out);
1077         } else {
1078                 entity_t const *entity = statement->scope.entities;
1079                 for (; entity != NULL; entity = entity->base.next) {
1080                         if (is_generated_entity(entity))
1081                                 continue;
1082                         /* FIXME display of multiple declarations is wrong */
1083                         print_declaration(entity);
1084                 }
1085         }
1086         if (statement->condition != NULL) {
1087                 fputc(' ', out);
1088                 print_expression(statement->condition);
1089         }
1090         fputc(';', out);
1091         if (statement->step != NULL) {
1092                 fputc(' ', out);
1093                 print_expression(statement->step);
1094         }
1095         fputs(") ", out);
1096         print_statement(statement->body);
1097 }
1098
1099 /**
1100  * Print assembler arguments.
1101  *
1102  * @param arguments   the arguments
1103  */
1104 static void print_asm_arguments(asm_argument_t *arguments)
1105 {
1106         asm_argument_t *argument = arguments;
1107         for (; argument != NULL; argument = argument->next) {
1108                 if (argument != arguments)
1109                         fputs(", ", out);
1110
1111                 if (argument->symbol) {
1112                         fprintf(out, "[%s] ", argument->symbol->string);
1113                 }
1114                 print_quoted_string(&argument->constraints, '"', 1);
1115                 fputs(" (", out);
1116                 print_expression(argument->expression);
1117                 fputc(')', out);
1118         }
1119 }
1120
1121 /**
1122  * Print assembler clobbers.
1123  *
1124  * @param clobbers   the clobbers
1125  */
1126 static void print_asm_clobbers(asm_clobber_t *clobbers)
1127 {
1128         asm_clobber_t *clobber = clobbers;
1129         for (; clobber != NULL; clobber = clobber->next) {
1130                 if (clobber != clobbers)
1131                         fputs(", ", out);
1132
1133                 print_quoted_string(&clobber->clobber, '"', 1);
1134         }
1135 }
1136
1137 /**
1138  * Print an assembler statement.
1139  *
1140  * @param statement   the statement
1141  */
1142 static void print_asm_statement(const asm_statement_t *statement)
1143 {
1144         fputs("asm ", out);
1145         if (statement->is_volatile) {
1146                 fputs("volatile ", out);
1147         }
1148         fputc('(', out);
1149         print_quoted_string(&statement->asm_text, '"', 1);
1150         if (statement->outputs  == NULL &&
1151             statement->inputs   == NULL &&
1152             statement->clobbers == NULL)
1153                 goto end_of_print_asm_statement;
1154
1155         fputs(" : ", out);
1156         print_asm_arguments(statement->outputs);
1157         if (statement->inputs == NULL && statement->clobbers == NULL)
1158                 goto end_of_print_asm_statement;
1159
1160         fputs(" : ", out);
1161         print_asm_arguments(statement->inputs);
1162         if (statement->clobbers == NULL)
1163                 goto end_of_print_asm_statement;
1164
1165         fputs(" : ", out);
1166         print_asm_clobbers(statement->clobbers);
1167
1168 end_of_print_asm_statement:
1169         fputs(");\n", out);
1170 }
1171
1172 /**
1173  * Print a microsoft __try statement.
1174  *
1175  * @param statement   the statement
1176  */
1177 static void print_ms_try_statement(const ms_try_statement_t *statement)
1178 {
1179         fputs("__try ", out);
1180         print_statement(statement->try_statement);
1181         print_indent();
1182         if (statement->except_expression != NULL) {
1183                 fputs("__except(", out);
1184                 print_expression(statement->except_expression);
1185                 fputs(") ", out);
1186         } else {
1187                 fputs("__finally ", out);
1188         }
1189         print_statement(statement->final_statement);
1190 }
1191
1192 /**
1193  * Print a microsoft __leave statement.
1194  *
1195  * @param statement   the statement
1196  */
1197 static void print_leave_statement(const leave_statement_t *statement)
1198 {
1199         (void)statement;
1200         fputs("__leave;\n", out);
1201 }
1202
1203 /**
1204  * Print a statement.
1205  *
1206  * @param statement   the statement
1207  */
1208 void print_statement(const statement_t *statement)
1209 {
1210         switch (statement->kind) {
1211         case STATEMENT_EMPTY:
1212                 fputs(";\n", out);
1213                 break;
1214         case STATEMENT_COMPOUND:
1215                 print_compound_statement(&statement->compound);
1216                 break;
1217         case STATEMENT_RETURN:
1218                 print_return_statement(&statement->returns);
1219                 break;
1220         case STATEMENT_EXPRESSION:
1221                 print_expression_statement(&statement->expression);
1222                 break;
1223         case STATEMENT_LABEL:
1224                 print_label_statement(&statement->label);
1225                 break;
1226         case STATEMENT_GOTO:
1227                 print_goto_statement(&statement->gotos);
1228                 break;
1229         case STATEMENT_CONTINUE:
1230                 fputs("continue;\n", out);
1231                 break;
1232         case STATEMENT_BREAK:
1233                 fputs("break;\n", out);
1234                 break;
1235         case STATEMENT_IF:
1236                 print_if_statement(&statement->ifs);
1237                 break;
1238         case STATEMENT_SWITCH:
1239                 print_switch_statement(&statement->switchs);
1240                 break;
1241         case STATEMENT_CASE_LABEL:
1242                 print_case_label(&statement->case_label);
1243                 break;
1244         case STATEMENT_DECLARATION:
1245                 print_declaration_statement(&statement->declaration);
1246                 break;
1247         case STATEMENT_WHILE:
1248                 print_while_statement(&statement->whiles);
1249                 break;
1250         case STATEMENT_DO_WHILE:
1251                 print_do_while_statement(&statement->do_while);
1252                 break;
1253         case STATEMENT_FOR:
1254                 print_for_statement(&statement->fors);
1255                 break;
1256         case STATEMENT_ASM:
1257                 print_asm_statement(&statement->asms);
1258                 break;
1259         case STATEMENT_MS_TRY:
1260                 print_ms_try_statement(&statement->ms_try);
1261                 break;
1262         case STATEMENT_LEAVE:
1263                 print_leave_statement(&statement->leave);
1264                 break;
1265         case STATEMENT_INVALID:
1266                 fputs("$invalid statement$\n", out);
1267                 break;
1268         }
1269 }
1270
1271 /**
1272  * Print a storage class.
1273  *
1274  * @param storage_class   the storage class
1275  */
1276 static void print_storage_class(storage_class_tag_t storage_class)
1277 {
1278         switch (storage_class) {
1279         case STORAGE_CLASS_NONE:     return;
1280         case STORAGE_CLASS_TYPEDEF:  fputs("typedef ",  out); return;
1281         case STORAGE_CLASS_EXTERN:   fputs("extern ",   out); return;
1282         case STORAGE_CLASS_STATIC:   fputs("static ",   out); return;
1283         case STORAGE_CLASS_AUTO:     fputs("auto ",     out); return;
1284         case STORAGE_CLASS_REGISTER: fputs("register ", out); return;
1285         }
1286         panic("invalid storage class");
1287 }
1288
1289 /**
1290  * Print an initializer.
1291  *
1292  * @param initializer  the initializer
1293  */
1294 void print_initializer(const initializer_t *initializer)
1295 {
1296         if (initializer == NULL) {
1297                 fputs("{}", out);
1298                 return;
1299         }
1300
1301         switch (initializer->kind) {
1302         case INITIALIZER_VALUE: {
1303                 const initializer_value_t *value = &initializer->value;
1304                 print_assignment_expression(value->value);
1305                 return;
1306         }
1307         case INITIALIZER_LIST: {
1308                 assert(initializer->kind == INITIALIZER_LIST);
1309                 fputs("{ ", out);
1310                 const initializer_list_t *list = &initializer->list;
1311
1312                 for (size_t i = 0 ; i < list->len; ++i) {
1313                         const initializer_t *sub_init = list->initializers[i];
1314                         print_initializer(list->initializers[i]);
1315                         if (i < list->len-1) {
1316                                 if (sub_init == NULL || sub_init->kind != INITIALIZER_DESIGNATOR)
1317                                         fputs(", ", out);
1318                         }
1319                 }
1320                 fputs(" }", out);
1321                 return;
1322         }
1323         case INITIALIZER_STRING:
1324                 print_quoted_string(&initializer->string.string, '"', 1);
1325                 return;
1326         case INITIALIZER_WIDE_STRING:
1327                 print_quoted_wide_string(&initializer->wide_string.string, '"', 1);
1328                 return;
1329         case INITIALIZER_DESIGNATOR:
1330                 print_designator(initializer->designator.designator);
1331                 fputs(" = ", out);
1332                 return;
1333         }
1334
1335         panic("invalid initializer kind found");
1336 }
1337
1338 /**
1339  * Print microsoft extended declaration modifiers.
1340  */
1341 static void print_ms_modifiers(const declaration_t *declaration)
1342 {
1343         if ((c_mode & _MS) == 0)
1344                 return;
1345
1346         decl_modifiers_t modifiers = declaration->modifiers;
1347
1348         bool        ds_shown = false;
1349         const char *next     = "(";
1350
1351         if (declaration->base.kind == ENTITY_VARIABLE) {
1352                 variable_t *variable = (variable_t*)declaration;
1353                 if (variable->alignment != 0
1354                                 || variable->get_property_sym != NULL
1355                                 || variable->put_property_sym != NULL) {
1356                         if (!ds_shown) {
1357                                 fputs("__declspec", out);
1358                                 ds_shown = true;
1359                         }
1360
1361                         if (variable->alignment != 0) {
1362                                 fputs(next, out); next = ", "; fprintf(out, "align(%u)", variable->alignment);
1363                         }
1364                         if (variable->get_property_sym != NULL
1365                                         || variable->put_property_sym != NULL) {
1366                                 char *comma = "";
1367                                 fputs(next, out); next = ", "; fputs("property(", out);
1368                                 if (variable->get_property_sym != NULL) {
1369                                         fprintf(out, "get=%s", variable->get_property_sym->string);
1370                                         comma = ", ";
1371                                 }
1372                                 if (variable->put_property_sym != NULL)
1373                                         fprintf(out, "%sput=%s", comma, variable->put_property_sym->string);
1374                                 fputc(')', out);
1375                         }
1376                 }
1377         }
1378
1379         /* DM_FORCEINLINE handled outside. */
1380         if ((modifiers & ~DM_FORCEINLINE) != 0) {
1381                 if (!ds_shown) {
1382                         fputs("__declspec", out);
1383                         ds_shown = true;
1384                 }
1385                 if (modifiers & DM_DLLIMPORT) {
1386                         fputs(next, out); next = ", "; fputs("dllimport", out);
1387                 }
1388                 if (modifiers & DM_DLLEXPORT) {
1389                         fputs(next, out); next = ", "; fputs("dllexport", out);
1390                 }
1391                 if (modifiers & DM_THREAD) {
1392                         fputs(next, out); next = ", "; fputs("thread", out);
1393                 }
1394                 if (modifiers & DM_NAKED) {
1395                         fputs(next, out); next = ", "; fputs("naked", out);
1396                 }
1397                 if (modifiers & DM_THREAD) {
1398                         fputs(next, out); next = ", "; fputs("thread", out);
1399                 }
1400                 if (modifiers & DM_SELECTANY) {
1401                         fputs(next, out); next = ", "; fputs("selectany", out);
1402                 }
1403                 if (modifiers & DM_NOTHROW) {
1404                         fputs(next, out); next = ", "; fputs("nothrow", out);
1405                 }
1406                 if (modifiers & DM_NORETURN) {
1407                         fputs(next, out); next = ", "; fputs("noreturn", out);
1408                 }
1409                 if (modifiers & DM_NOINLINE) {
1410                         fputs(next, out); next = ", "; fputs("noinline", out);
1411                 }
1412                 if (modifiers & DM_DEPRECATED) {
1413                         fputs(next, out); next = ", "; fputs("deprecated", out);
1414                         if (declaration->deprecated_string != NULL)
1415                                 fprintf(out, "(\"%s\")",
1416                                         declaration->deprecated_string);
1417                 }
1418                 if (modifiers & DM_RESTRICT) {
1419                         fputs(next, out); next = ", "; fputs("restrict", out);
1420                 }
1421                 if (modifiers & DM_NOALIAS) {
1422                         fputs(next, out); next = ", "; fputs("noalias", out);
1423                 }
1424         }
1425
1426         if (ds_shown)
1427                 fputs(") ", out);
1428 }
1429
1430 static void print_scope(const scope_t *scope)
1431 {
1432         const entity_t *entity = scope->entities;
1433         for ( ; entity != NULL; entity = entity->base.next) {
1434                 print_indent();
1435                 print_entity(entity);
1436                 fputs("\n", out);
1437         }
1438 }
1439
1440 static void print_namespace(const namespace_t *namespace)
1441 {
1442         fputs("namespace ", out);
1443         if (namespace->base.symbol != NULL) {
1444                 fputs(namespace->base.symbol->string, out);
1445                 fputc(' ', out);
1446         }
1447
1448         fputs("{\n", out);
1449         ++indent;
1450
1451         print_scope(&namespace->members);
1452
1453         --indent;
1454         print_indent();
1455         fputs("}\n", out);
1456 }
1457
1458 /**
1459  * Print a variable or function declaration
1460  */
1461 void print_declaration(const entity_t *entity)
1462 {
1463         assert(is_declaration(entity));
1464         const declaration_t *declaration = &entity->declaration;
1465
1466         print_storage_class((storage_class_tag_t)declaration->declared_storage_class);
1467         if (entity->kind == ENTITY_FUNCTION) {
1468                 function_t *function = (function_t*)declaration;
1469                 if (function->is_inline) {
1470                         if (declaration->modifiers & DM_FORCEINLINE) {
1471                                 fputs("__forceinline ", out);
1472                         } else if (declaration->modifiers & DM_MICROSOFT_INLINE) {
1473                                 fputs("__inline ", out);
1474                         } else {
1475                                 fputs("inline ", out);
1476                         }
1477                 }
1478         }
1479         print_ms_modifiers(declaration);
1480         switch (entity->kind) {
1481                 case ENTITY_FUNCTION:
1482                         print_type_ext(entity->declaration.type, entity->base.symbol,
1483                                         &entity->function.parameters);
1484
1485                         if (entity->function.statement != NULL) {
1486                                 fputc('\n', out);
1487                                 print_indent();
1488                                 print_statement(entity->function.statement);
1489                                 return;
1490                         }
1491                         break;
1492
1493                 case ENTITY_VARIABLE:
1494                         if (entity->variable.thread_local)
1495                                 fputs("__thread ", out);
1496                         print_type_ext(declaration->type, declaration->base.symbol, NULL);
1497                         if (entity->variable.initializer != NULL) {
1498                                 fputs(" = ", out);
1499                                 print_initializer(entity->variable.initializer);
1500                         }
1501                         break;
1502
1503                 default:
1504                         print_type_ext(declaration->type, declaration->base.symbol, NULL);
1505                         break;
1506         }
1507         fputc(';', out);
1508 }
1509
1510 /**
1511  * Prints an expression.
1512  *
1513  * @param expression  the expression
1514  */
1515 void print_expression(const expression_t *expression)
1516 {
1517         print_expression_prec(expression, PREC_BOTTOM);
1518 }
1519
1520 /**
1521  * Print a declaration.
1522  *
1523  * @param declaration  the declaration
1524  */
1525 void print_entity(const entity_t *entity)
1526 {
1527         if (entity->base.namespc != NAMESPACE_NORMAL && entity->base.symbol == NULL)
1528                 return;
1529
1530         switch ((entity_kind_tag_t)entity->kind) {
1531         case ENTITY_VARIABLE:
1532         case ENTITY_PARAMETER:
1533         case ENTITY_COMPOUND_MEMBER:
1534         case ENTITY_FUNCTION:
1535                 print_declaration(entity);
1536                 return;
1537         case ENTITY_TYPEDEF:
1538                 print_typedef(entity);
1539                 return;
1540         case ENTITY_STRUCT:
1541                 fputs("struct ", out);
1542                 fputs(entity->base.symbol->string, out);
1543                 if (entity->structe.complete) {
1544                         fputc(' ', out);
1545                         print_compound_definition(&entity->structe);
1546                 }
1547                 fputc(';', out);
1548                 return;
1549         case ENTITY_UNION:
1550                 fputs("union ", out);
1551                 fputs(entity->base.symbol->string, out);
1552                 if (entity->unione.complete) {
1553                         fputc(' ', out);
1554                         print_compound_definition(&entity->unione);
1555                 }
1556                 fputc(';', out);
1557                 return;
1558         case ENTITY_ENUM:
1559                 fputs("enum ", out);
1560                 fputs(entity->base.symbol->string, out);
1561                 fputc(' ', out);
1562                 print_enum_definition(&entity->enume);
1563                 fputc(';', out);
1564                 return;
1565         case ENTITY_NAMESPACE:
1566                 print_namespace(&entity->namespacee);
1567                 return;
1568         case ENTITY_LOCAL_LABEL:
1569                 fprintf(out, "__label__ %s;", entity->base.symbol->string);
1570                 return;
1571         case ENTITY_LABEL:
1572         case ENTITY_ENUM_VALUE:
1573                 panic("print_entity used on unexpected entity type");
1574         case ENTITY_INVALID:
1575                 break;
1576         }
1577         panic("Invalid entity type encountered");
1578 }
1579
1580 /**
1581  * Print the AST of a translation unit.
1582  *
1583  * @param unit   the translation unit
1584  */
1585 void print_ast(const translation_unit_t *unit)
1586 {
1587         inc_type_visited();
1588
1589         entity_t *entity = unit->scope.entities;
1590         for ( ; entity != NULL; entity = entity->base.next) {
1591                 if (entity->kind == ENTITY_ENUM_VALUE)
1592                         continue;
1593                 if (entity->base.namespc != NAMESPACE_NORMAL
1594                                 && entity->base.symbol == NULL)
1595                         continue;
1596                 if (is_generated_entity(entity))
1597                         continue;
1598
1599                 print_indent();
1600                 print_entity(entity);
1601                 fputc('\n', out);
1602         }
1603 }
1604
1605 bool is_constant_initializer(const initializer_t *initializer)
1606 {
1607         switch (initializer->kind) {
1608         case INITIALIZER_STRING:
1609         case INITIALIZER_WIDE_STRING:
1610         case INITIALIZER_DESIGNATOR:
1611                 return true;
1612
1613         case INITIALIZER_VALUE:
1614                 return is_constant_expression(initializer->value.value);
1615
1616         case INITIALIZER_LIST:
1617                 for (size_t i = 0; i < initializer->list.len; ++i) {
1618                         initializer_t *sub_initializer = initializer->list.initializers[i];
1619                         if (!is_constant_initializer(sub_initializer))
1620                                 return false;
1621                 }
1622                 return true;
1623         }
1624         panic("invalid initializer kind found");
1625 }
1626
1627 static bool is_object_with_linker_constant_address(const expression_t *expression)
1628 {
1629         switch (expression->kind) {
1630         case EXPR_UNARY_DEREFERENCE:
1631                 return is_address_constant(expression->unary.value);
1632
1633         case EXPR_SELECT: {
1634                 type_t *base_type = skip_typeref(expression->select.compound->base.type);
1635                 if (is_type_pointer(base_type)) {
1636                         /* it's a -> */
1637                         return is_address_constant(expression->select.compound);
1638                 } else {
1639                         return is_object_with_linker_constant_address(expression->select.compound);
1640                 }
1641         }
1642
1643         case EXPR_ARRAY_ACCESS:
1644                 return is_constant_expression(expression->array_access.index)
1645                         && is_address_constant(expression->array_access.array_ref);
1646
1647         case EXPR_REFERENCE: {
1648                 entity_t *entity = expression->reference.entity;
1649                 if (is_declaration(entity)) {
1650                         switch ((storage_class_tag_t)entity->declaration.storage_class) {
1651                         case STORAGE_CLASS_NONE:
1652                         case STORAGE_CLASS_EXTERN:
1653                         case STORAGE_CLASS_STATIC:
1654                                 return
1655                                         entity->kind != ENTITY_VARIABLE ||
1656                                         !entity->variable.thread_local;
1657
1658                         case STORAGE_CLASS_REGISTER:
1659                         case STORAGE_CLASS_TYPEDEF:
1660                         case STORAGE_CLASS_AUTO:
1661                                 break;
1662                         }
1663                 }
1664                 return false;
1665         }
1666
1667         default:
1668                 return false;
1669         }
1670 }
1671
1672 bool is_address_constant(const expression_t *expression)
1673 {
1674         switch (expression->kind) {
1675         case EXPR_UNARY_TAKE_ADDRESS:
1676                 return is_object_with_linker_constant_address(expression->unary.value);
1677
1678         case EXPR_UNARY_DEREFERENCE: {
1679                 type_t *real_type
1680                         = revert_automatic_type_conversion(expression->unary.value);
1681                 /* dereferencing a function is a NOP */
1682                 if (is_type_function(real_type)) {
1683                         return is_address_constant(expression->unary.value);
1684                 }
1685                 /* FALLTHROUGH */
1686         }
1687
1688         case EXPR_UNARY_CAST: {
1689                 type_t *dest = skip_typeref(expression->base.type);
1690                 if (!is_type_pointer(dest) && (
1691                         dest->kind != TYPE_ATOMIC                                               ||
1692                         !(get_atomic_type_flags(dest->atomic.akind) & ATOMIC_TYPE_FLAG_INTEGER) ||
1693                         get_atomic_type_size(dest->atomic.akind) < get_atomic_type_size(get_intptr_kind())
1694                     ))
1695                         return false;
1696
1697                 return (is_constant_expression(expression->unary.value)
1698                         || is_address_constant(expression->unary.value));
1699         }
1700
1701         case EXPR_BINARY_ADD:
1702         case EXPR_BINARY_SUB: {
1703                 expression_t *left  = expression->binary.left;
1704                 expression_t *right = expression->binary.right;
1705
1706                 if (is_type_pointer(skip_typeref(left->base.type))) {
1707                         return is_address_constant(left) && is_constant_expression(right);
1708                 } else if (is_type_pointer(skip_typeref(right->base.type))) {
1709                         return is_constant_expression(left)     && is_address_constant(right);
1710                 }
1711
1712                 return false;
1713         }
1714
1715         case EXPR_REFERENCE: {
1716                 entity_t *entity = expression->reference.entity;
1717                 if (!is_declaration(entity))
1718                         return false;
1719
1720                 type_t *type = skip_typeref(entity->declaration.type);
1721                 if (is_type_function(type))
1722                         return true;
1723                 if (is_type_array(type)) {
1724                         return is_object_with_linker_constant_address(expression);
1725                 }
1726                 /* Prevent stray errors */
1727                 if (!is_type_valid(type))
1728                         return true;
1729                 return false;
1730         }
1731
1732         case EXPR_ARRAY_ACCESS: {
1733                 type_t *const type =
1734                         skip_typeref(revert_automatic_type_conversion(expression));
1735                 return
1736                         is_type_array(type)                                    &&
1737                         is_constant_expression(expression->array_access.index) &&
1738                         is_address_constant(expression->array_access.array_ref);
1739         }
1740
1741         default:
1742                 return false;
1743         }
1744 }
1745
1746 /**
1747  * Check if the given expression is a call to a builtin function
1748  * returning a constant result.
1749  */
1750 static bool is_builtin_const_call(const expression_t *expression)
1751 {
1752         expression_t *function = expression->call.function;
1753         if (function->kind != EXPR_REFERENCE)
1754                 return false;
1755         reference_expression_t *ref = &function->reference;
1756         if (ref->entity->kind != ENTITY_FUNCTION)
1757                 return false;
1758
1759         switch (ref->entity->function.btk) {
1760         case bk_gnu_builtin_huge_val:
1761         case bk_gnu_builtin_inf:
1762         case bk_gnu_builtin_inff:
1763         case bk_gnu_builtin_infl:
1764         case bk_gnu_builtin_nan:
1765         case bk_gnu_builtin_nanf:
1766         case bk_gnu_builtin_nanl:
1767                 return true;
1768         default:
1769                 return false;
1770         }
1771
1772 }
1773
1774 static bool is_constant_pointer(const expression_t *expression)
1775 {
1776         if (is_constant_expression(expression))
1777                 return true;
1778
1779         switch (expression->kind) {
1780         case EXPR_UNARY_CAST:
1781                 return is_constant_pointer(expression->unary.value);
1782         default:
1783                 return false;
1784         }
1785 }
1786
1787 static bool is_object_with_constant_address(const expression_t *expression)
1788 {
1789         switch (expression->kind) {
1790         case EXPR_SELECT: {
1791                 expression_t *compound      = expression->select.compound;
1792                 type_t       *compound_type = compound->base.type;
1793                 compound_type = skip_typeref(compound_type);
1794                 if (is_type_pointer(compound_type)) {
1795                         return is_constant_pointer(compound);
1796                 } else {
1797                         return is_object_with_constant_address(compound);
1798                 }
1799         }
1800
1801         case EXPR_ARRAY_ACCESS: {
1802                 array_access_expression_t const* const array_access =
1803                         &expression->array_access;
1804                 return
1805                         is_constant_expression(array_access->index) && (
1806                                 is_object_with_constant_address(array_access->array_ref) ||
1807                                 is_constant_pointer(array_access->array_ref)
1808                         );
1809         }
1810
1811         case EXPR_UNARY_DEREFERENCE:
1812                 return is_constant_pointer(expression->unary.value);
1813         default:
1814                 return false;
1815         }
1816 }
1817
1818 bool is_constant_expression(const expression_t *expression)
1819 {
1820         switch (expression->kind) {
1821
1822         case EXPR_CONST:
1823         case EXPR_CHARACTER_CONSTANT:
1824         case EXPR_WIDE_CHARACTER_CONSTANT:
1825         case EXPR_STRING_LITERAL:
1826         case EXPR_WIDE_STRING_LITERAL:
1827         case EXPR_CLASSIFY_TYPE:
1828         case EXPR_FUNCNAME:
1829         case EXPR_OFFSETOF:
1830         case EXPR_ALIGNOF:
1831         case EXPR_BUILTIN_CONSTANT_P:
1832         case EXPR_BUILTIN_TYPES_COMPATIBLE_P:
1833         case EXPR_LABEL_ADDRESS:
1834         case EXPR_REFERENCE_ENUM_VALUE:
1835                 return true;
1836
1837         case EXPR_SIZEOF: {
1838                 type_t *type = expression->typeprop.type;
1839                 if (type == NULL)
1840                         type = expression->typeprop.tp_expression->base.type;
1841
1842                 type = skip_typeref(type);
1843                 if (is_type_array(type) && type->array.is_vla)
1844                         return false;
1845                 return true;
1846         }
1847
1848         case EXPR_SELECT:
1849         case EXPR_VA_START:
1850         case EXPR_VA_ARG:
1851         case EXPR_STATEMENT:
1852         case EXPR_REFERENCE:
1853         case EXPR_UNARY_POSTFIX_INCREMENT:
1854         case EXPR_UNARY_POSTFIX_DECREMENT:
1855         case EXPR_UNARY_PREFIX_INCREMENT:
1856         case EXPR_UNARY_PREFIX_DECREMENT:
1857         case EXPR_UNARY_ASSUME: /* has VOID type */
1858         case EXPR_UNARY_DEREFERENCE:
1859         case EXPR_UNARY_DELETE:
1860         case EXPR_UNARY_DELETE_ARRAY:
1861         case EXPR_UNARY_THROW:
1862         case EXPR_BINARY_ASSIGN:
1863         case EXPR_BINARY_MUL_ASSIGN:
1864         case EXPR_BINARY_DIV_ASSIGN:
1865         case EXPR_BINARY_MOD_ASSIGN:
1866         case EXPR_BINARY_ADD_ASSIGN:
1867         case EXPR_BINARY_SUB_ASSIGN:
1868         case EXPR_BINARY_SHIFTLEFT_ASSIGN:
1869         case EXPR_BINARY_SHIFTRIGHT_ASSIGN:
1870         case EXPR_BINARY_BITWISE_AND_ASSIGN:
1871         case EXPR_BINARY_BITWISE_XOR_ASSIGN:
1872         case EXPR_BINARY_BITWISE_OR_ASSIGN:
1873         case EXPR_BINARY_COMMA:
1874         case EXPR_ARRAY_ACCESS:
1875                 return false;
1876
1877         case EXPR_UNARY_TAKE_ADDRESS:
1878                 return is_object_with_constant_address(expression->unary.value);
1879
1880         case EXPR_CALL:
1881                 return is_builtin_const_call(expression);
1882
1883         case EXPR_UNARY_NEGATE:
1884         case EXPR_UNARY_PLUS:
1885         case EXPR_UNARY_BITWISE_NEGATE:
1886         case EXPR_UNARY_NOT:
1887                 return is_constant_expression(expression->unary.value);
1888
1889         case EXPR_UNARY_CAST:
1890         case EXPR_UNARY_CAST_IMPLICIT:
1891                 return is_type_arithmetic(skip_typeref(expression->base.type))
1892                         && is_constant_expression(expression->unary.value);
1893
1894         case EXPR_BINARY_ADD:
1895         case EXPR_BINARY_SUB:
1896         case EXPR_BINARY_MUL:
1897         case EXPR_BINARY_DIV:
1898         case EXPR_BINARY_MOD:
1899         case EXPR_BINARY_EQUAL:
1900         case EXPR_BINARY_NOTEQUAL:
1901         case EXPR_BINARY_LESS:
1902         case EXPR_BINARY_LESSEQUAL:
1903         case EXPR_BINARY_GREATER:
1904         case EXPR_BINARY_GREATEREQUAL:
1905         case EXPR_BINARY_BITWISE_AND:
1906         case EXPR_BINARY_BITWISE_OR:
1907         case EXPR_BINARY_BITWISE_XOR:
1908         case EXPR_BINARY_SHIFTLEFT:
1909         case EXPR_BINARY_SHIFTRIGHT:
1910         case EXPR_BINARY_ISGREATER:
1911         case EXPR_BINARY_ISGREATEREQUAL:
1912         case EXPR_BINARY_ISLESS:
1913         case EXPR_BINARY_ISLESSEQUAL:
1914         case EXPR_BINARY_ISLESSGREATER:
1915         case EXPR_BINARY_ISUNORDERED:
1916                 return is_constant_expression(expression->binary.left)
1917                         && is_constant_expression(expression->binary.right);
1918
1919         case EXPR_BINARY_LOGICAL_AND: {
1920                 expression_t const *const left = expression->binary.left;
1921                 if (!is_constant_expression(left))
1922                         return false;
1923                 if (fold_constant(left) == 0)
1924                         return true;
1925                 return is_constant_expression(expression->binary.right);
1926         }
1927
1928         case EXPR_BINARY_LOGICAL_OR: {
1929                 expression_t const *const left = expression->binary.left;
1930                 if (!is_constant_expression(left))
1931                         return false;
1932                 if (fold_constant(left) != 0)
1933                         return true;
1934                 return is_constant_expression(expression->binary.right);
1935         }
1936
1937         case EXPR_COMPOUND_LITERAL:
1938                 return is_constant_initializer(expression->compound_literal.initializer);
1939
1940         case EXPR_CONDITIONAL: {
1941                 expression_t *condition = expression->conditional.condition;
1942                 if (!is_constant_expression(condition))
1943                         return false;
1944
1945                 long val = fold_constant(condition);
1946                 if (val != 0) {
1947                         expression_t const *const t = expression->conditional.true_expression;
1948                         return t == NULL || is_constant_expression(t);
1949                 } else {
1950                         return is_constant_expression(expression->conditional.false_expression);
1951                 }
1952         }
1953
1954         case EXPR_INVALID:
1955                 return true;
1956
1957         case EXPR_UNKNOWN:
1958                 break;
1959         }
1960         panic("invalid expression found (is constant expression)");
1961 }
1962
1963 /**
1964  * Initialize the AST construction.
1965  */
1966 void init_ast(void)
1967 {
1968         obstack_init(&ast_obstack);
1969 }
1970
1971 /**
1972  * Free the AST.
1973  */
1974 void exit_ast(void)
1975 {
1976         obstack_free(&ast_obstack, NULL);
1977 }
1978
1979 /**
1980  * Set the output stream for the AST printer.
1981  *
1982  * @param stream  the output stream
1983  */
1984 void ast_set_output(FILE *stream)
1985 {
1986         out = stream;
1987         type_set_output(stream);
1988 }
1989
1990 /**
1991  * Allocate an AST object of the given size.
1992  *
1993  * @param size  the size of the object to allocate
1994  *
1995  * @return  A new allocated object in the AST memeory space.
1996  */
1997 void *(allocate_ast)(size_t size)
1998 {
1999         return _allocate_ast(size);
2000 }