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