- add empty and invalid statements
[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
26 #include <assert.h>
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <ctype.h>
30
31 #include "adt/error.h"
32
33 struct obstack ast_obstack;
34
35 static FILE *out;
36 static int   indent;
37
38 /** If set, implicit casts are printed. */
39 bool print_implicit_casts = false;
40
41 /** If set parenthesis are printed to indicate operator precedence. */
42 bool print_parenthesis = false;
43
44 static void print_statement(const statement_t *statement);
45 static void print_expression_prec(const expression_t *expression, unsigned prec);
46
47 void change_indent(int delta)
48 {
49         indent += delta;
50         assert(indent >= 0);
51 }
52
53 void print_indent(void)
54 {
55         for(int i = 0; i < indent; ++i)
56                 fprintf(out, "\t");
57 }
58
59 enum precedence_t {
60         PREC_BOTTOM  =  0,
61         PREC_COMMA   =  2, /* ,                                    left to right */
62         PREC_ASSIGN  =  4, /* = += -= *= /= %= <<= >>= &= ^= |=    right to left */
63         PREC_COND    =  6, /* ?:                                   right to left */
64         PREC_LOG_OR  =  8, /* ||                                   left to right */
65         PREC_LOG_AND = 10, /* &&                                   left to right */
66         PREC_BIT_OR  = 12, /* |                                    left to right */
67         PREC_BIT_XOR = 14, /* ^                                    left to right */
68         PREC_BIT_AND = 16, /* &                                    left to right */
69         PREC_EQ      = 18, /* == !=                                left to right */
70         PREC_CMP     = 20, /* < <= > >=                            left to right */
71         PREC_SHF     = 22, /* << >>                                left to right */
72         PREC_PLUS    = 24, /* + -                                  left to right */
73         PREC_MUL     = 26, /* * / %                                left to right */
74         PREC_UNARY   = 28, /* ! ~ ++ -- + - (type) * & sizeof      right to left */
75         PREC_ACCESS  = 30, /* () [] -> .                           left to right */
76         PREC_PRIM    = 32, /* primary */
77         PREC_TOP     = 34
78 };
79
80 /**
81  * Returns 1 if a given precedence level has right-to-left
82  * associativity, else -1.
83  *
84  * @param precedence   the operator precedence
85  */
86 static int right_to_left(unsigned precedence) {
87         return (precedence == PREC_ASSIGN || precedence == PREC_COND ||
88                 precedence == PREC_UNARY) ? 1 : -1;
89 }
90
91 /**
92  * Return the precedence of an expression given by its kind.
93  *
94  * @param kind   the expression kind
95  */
96 static unsigned get_expression_precedence(expression_kind_t kind)
97 {
98         static const unsigned prec[] = {
99                 [EXPR_UNKNOWN]                   = PREC_PRIM,
100                 [EXPR_INVALID]                   = PREC_PRIM,
101                 [EXPR_REFERENCE]                 = PREC_PRIM,
102                 [EXPR_CHARACTER_CONSTANT]        = PREC_PRIM,
103                 [EXPR_WIDE_CHARACTER_CONSTANT]   = PREC_PRIM,
104                 [EXPR_CONST]                     = PREC_PRIM,
105                 [EXPR_STRING_LITERAL]            = PREC_PRIM,
106                 [EXPR_WIDE_STRING_LITERAL]       = PREC_PRIM,
107                 [EXPR_COMPOUND_LITERAL]          = PREC_UNARY,
108                 [EXPR_CALL]                      = PREC_PRIM,
109                 [EXPR_CONDITIONAL]               = PREC_COND,
110                 [EXPR_SELECT]                    = PREC_ACCESS,
111                 [EXPR_ARRAY_ACCESS]              = PREC_ACCESS,
112                 [EXPR_SIZEOF]                    = PREC_UNARY,
113                 [EXPR_CLASSIFY_TYPE]             = PREC_UNARY,
114                 [EXPR_ALIGNOF]                   = PREC_UNARY,
115
116                 [EXPR_FUNCTION]                  = PREC_PRIM,
117                 [EXPR_PRETTY_FUNCTION]           = PREC_PRIM,
118                 [EXPR_BUILTIN_SYMBOL]            = PREC_PRIM,
119                 [EXPR_BUILTIN_CONSTANT_P]        = PREC_PRIM,
120                 [EXPR_BUILTIN_PREFETCH]          = PREC_PRIM,
121                 [EXPR_OFFSETOF]                  = PREC_PRIM,
122                 [EXPR_VA_START]                  = PREC_PRIM,
123                 [EXPR_VA_ARG]                    = PREC_PRIM,
124                 [EXPR_STATEMENT]                 = PREC_ACCESS,
125
126                 [EXPR_UNARY_NEGATE]              = PREC_UNARY,
127                 [EXPR_UNARY_PLUS]                = PREC_UNARY,
128                 [EXPR_UNARY_BITWISE_NEGATE]      = PREC_UNARY,
129                 [EXPR_UNARY_NOT]                 = PREC_UNARY,
130                 [EXPR_UNARY_DEREFERENCE]         = PREC_UNARY,
131                 [EXPR_UNARY_TAKE_ADDRESS]        = PREC_UNARY,
132                 [EXPR_UNARY_POSTFIX_INCREMENT]   = PREC_UNARY,
133                 [EXPR_UNARY_POSTFIX_DECREMENT]   = PREC_UNARY,
134                 [EXPR_UNARY_PREFIX_INCREMENT]    = PREC_UNARY,
135                 [EXPR_UNARY_PREFIX_DECREMENT]    = PREC_UNARY,
136                 [EXPR_UNARY_CAST]                = PREC_UNARY,
137                 [EXPR_UNARY_CAST_IMPLICIT]       = PREC_UNARY,
138                 [EXPR_UNARY_ASSUME]              = PREC_PRIM,
139                 [EXPR_UNARY_BITFIELD_EXTRACT]    = PREC_ACCESS,
140
141                 [EXPR_BINARY_ADD]                = PREC_PLUS,
142                 [EXPR_BINARY_SUB]                = PREC_PLUS,
143                 [EXPR_BINARY_MUL]                = PREC_MUL,
144                 [EXPR_BINARY_DIV]                = PREC_MUL,
145                 [EXPR_BINARY_MOD]                = PREC_MUL,
146                 [EXPR_BINARY_EQUAL]              = PREC_EQ,
147                 [EXPR_BINARY_NOTEQUAL]           = PREC_EQ,
148                 [EXPR_BINARY_LESS]               = PREC_CMP,
149                 [EXPR_BINARY_LESSEQUAL]          = PREC_CMP,
150                 [EXPR_BINARY_GREATER]            = PREC_CMP,
151                 [EXPR_BINARY_GREATEREQUAL]       = PREC_CMP,
152                 [EXPR_BINARY_BITWISE_AND]        = PREC_BIT_AND,
153                 [EXPR_BINARY_BITWISE_OR]         = PREC_BIT_OR,
154                 [EXPR_BINARY_BITWISE_XOR]        = PREC_BIT_XOR,
155                 [EXPR_BINARY_LOGICAL_AND]        = PREC_LOG_AND,
156                 [EXPR_BINARY_LOGICAL_OR]         = PREC_LOG_OR,
157                 [EXPR_BINARY_SHIFTLEFT]          = PREC_SHF,
158                 [EXPR_BINARY_SHIFTRIGHT]         = PREC_SHF,
159                 [EXPR_BINARY_ASSIGN]             = PREC_ASSIGN,
160                 [EXPR_BINARY_MUL_ASSIGN]         = PREC_ASSIGN,
161                 [EXPR_BINARY_DIV_ASSIGN]         = PREC_ASSIGN,
162                 [EXPR_BINARY_MOD_ASSIGN]         = PREC_ASSIGN,
163                 [EXPR_BINARY_ADD_ASSIGN]         = PREC_ASSIGN,
164                 [EXPR_BINARY_SUB_ASSIGN]         = PREC_ASSIGN,
165                 [EXPR_BINARY_SHIFTLEFT_ASSIGN]   = PREC_ASSIGN,
166                 [EXPR_BINARY_SHIFTRIGHT_ASSIGN]  = PREC_ASSIGN,
167                 [EXPR_BINARY_BITWISE_AND_ASSIGN] = PREC_ASSIGN,
168                 [EXPR_BINARY_BITWISE_XOR_ASSIGN] = PREC_ASSIGN,
169                 [EXPR_BINARY_BITWISE_OR_ASSIGN]  = PREC_ASSIGN,
170                 [EXPR_BINARY_COMMA]              = PREC_COMMA,
171
172                 [EXPR_BINARY_BUILTIN_EXPECT]     = PREC_PRIM,
173                 [EXPR_BINARY_ISGREATER]          = PREC_PRIM,
174                 [EXPR_BINARY_ISGREATEREQUAL]     = PREC_PRIM,
175                 [EXPR_BINARY_ISLESS]             = PREC_PRIM,
176                 [EXPR_BINARY_ISLESSEQUAL]        = PREC_PRIM,
177                 [EXPR_BINARY_ISLESSGREATER]      = PREC_PRIM,
178                 [EXPR_BINARY_ISUNORDERED]        = PREC_PRIM
179         };
180         assert((unsigned)kind < (sizeof(prec)/sizeof(prec[0])));
181         unsigned res = prec[kind];
182
183         assert(res != PREC_BOTTOM);
184         return res;
185 }
186
187 /**
188  * Print a constant expression.
189  *
190  * @param cnst  the constant expression
191  */
192 static void print_const(const const_expression_t *cnst)
193 {
194         if(cnst->base.type == NULL)
195                 return;
196
197         const type_t *const type = skip_typeref(cnst->base.type);
198
199         if (is_type_integer(type)) {
200                 fprintf(out, "%lld", cnst->v.int_value);
201         } else if (is_type_float(type)) {
202                 fprintf(out, "%Lf", cnst->v.float_value);
203         } else {
204                 panic("unknown constant");
205         }
206 }
207
208 /**
209  * Print a quoted string constant.
210  *
211  * @param string  the string constant
212  * @param border  the border char
213  */
214 static void print_quoted_string(const string_t *const string, char border)
215 {
216         fputc(border, out);
217         const char *end = string->begin + string->size - 1;
218         for (const char *c = string->begin; c != end; ++c) {
219                 if (*c == border) {
220                         fputc('\\', out);
221                 }
222                 switch(*c) {
223                 case '\\':  fputs("\\\\", out); break;
224                 case '\a':  fputs("\\a", out); break;
225                 case '\b':  fputs("\\b", out); break;
226                 case '\f':  fputs("\\f", out); break;
227                 case '\n':  fputs("\\n", out); break;
228                 case '\r':  fputs("\\r", out); break;
229                 case '\t':  fputs("\\t", out); break;
230                 case '\v':  fputs("\\v", out); break;
231                 case '\?':  fputs("\\?", out); break;
232                 default:
233                         if(!isprint(*c)) {
234                                 fprintf(out, "\\%03o", *c);
235                                 break;
236                         }
237                         fputc(*c, out);
238                         break;
239                 }
240         }
241         fputc(border, out);
242 }
243
244 /**
245  * Prints a wide string literal expression.
246  *
247  * @param wstr  the wide string literal expression
248  */
249 static void print_quoted_wide_string(const wide_string_t *const wstr,
250                                      char border)
251 {
252         fputc('L', out);
253         fputc(border, out);
254         for (const wchar_rep_t *c = wstr->begin, *end = wstr->begin + wstr->size-1;
255              c != end; ++c) {
256                 switch (*c) {
257                         case L'\"':  fputs("\\\"", out); break;
258                         case L'\\':  fputs("\\\\", out); break;
259                         case L'\a':  fputs("\\a",  out); break;
260                         case L'\b':  fputs("\\b",  out); break;
261                         case L'\f':  fputs("\\f",  out); break;
262                         case L'\n':  fputs("\\n",  out); break;
263                         case L'\r':  fputs("\\r",  out); break;
264                         case L'\t':  fputs("\\t",  out); break;
265                         case L'\v':  fputs("\\v",  out); break;
266                         case L'\?':  fputs("\\?",  out); break;
267                         default: {
268                                 const unsigned tc = *c;
269                                 if (tc < 0x80U) {
270                                         if (!isprint(*c))  {
271                                                 fprintf(out, "\\%03o", (char)*c);
272                                         } else {
273                                                 fputc(*c, out);
274                                         }
275                                 } else if (tc < 0x800) {
276                                         fputc(0xC0 | (tc >> 6),   out);
277                                         fputc(0x80 | (tc & 0x3F), out);
278                                 } else if (tc < 0x10000) {
279                                         fputc(0xE0 | ( tc >> 12),         out);
280                                         fputc(0x80 | ((tc >>  6) & 0x3F), out);
281                                         fputc(0x80 | ( tc        & 0x3F), out);
282                                 } else {
283                                         fputc(0xF0 | ( tc >> 18),         out);
284                                         fputc(0x80 | ((tc >> 12) & 0x3F), out);
285                                         fputc(0x80 | ((tc >>  6) & 0x3F), out);
286                                         fputc(0x80 | ( tc        & 0x3F), out);
287                                 }
288                         }
289                 }
290         }
291         fputc(border, out);
292 }
293
294 /**
295  * Print a constant character expression.
296  *
297  * @param cnst  the constant character expression
298  */
299 static void print_character_constant(const const_expression_t *cnst)
300 {
301         print_quoted_string(&cnst->v.character, '\'');
302 }
303
304 static void print_wide_character_constant(const const_expression_t *cnst)
305 {
306         print_quoted_wide_string(&cnst->v.wide_character, '\'');
307 }
308
309 /**
310  * Prints a string literal expression.
311  *
312  * @param string_literal  the string literal expression
313  */
314 static void print_string_literal(
315                 const string_literal_expression_t *string_literal)
316 {
317         print_quoted_string(&string_literal->value, '"');
318 }
319
320 static void print_wide_string_literal(
321         const wide_string_literal_expression_t *const wstr)
322 {
323         print_quoted_wide_string(&wstr->value, '"');
324 }
325
326 static void print_compound_literal(
327                 const compound_literal_expression_t *expression)
328 {
329         fputc('(', out);
330         print_type(expression->type);
331         fputs(") ", out);
332         print_initializer(expression->initializer);
333 }
334
335 /**
336  * Prints a call expression.
337  *
338  * @param call  the call expression
339  */
340 static void print_call_expression(const call_expression_t *call)
341 {
342         unsigned prec = get_expression_precedence(call->base.kind);
343         print_expression_prec(call->function, prec);
344         fprintf(out, "(");
345         call_argument_t *argument = call->arguments;
346         int              first    = 1;
347         while(argument != NULL) {
348                 if(!first) {
349                         fprintf(out, ", ");
350                 } else {
351                         first = 0;
352                 }
353                 print_expression_prec(argument->expression, PREC_COMMA + 1);
354
355                 argument = argument->next;
356         }
357         fprintf(out, ")");
358 }
359
360 /**
361  * Prints a binary expression.
362  *
363  * @param binexpr   the binary expression
364  */
365 static void print_binary_expression(const binary_expression_t *binexpr)
366 {
367         unsigned prec = get_expression_precedence(binexpr->base.kind);
368         int      r2l  = right_to_left(prec);
369
370         if(binexpr->base.kind == EXPR_BINARY_BUILTIN_EXPECT) {
371                 fputs("__builtin_expect(", out);
372                 print_expression_prec(binexpr->left, prec);
373                 fputs(", ", out);
374                 print_expression_prec(binexpr->right, prec);
375                 fputc(')', out);
376                 return;
377         }
378
379         print_expression_prec(binexpr->left, prec + r2l);
380         if (binexpr->base.kind != EXPR_BINARY_COMMA) {
381                 fputc(' ', out);
382         }
383         switch (binexpr->base.kind) {
384         case EXPR_BINARY_COMMA:              fputs(",", out);     break;
385         case EXPR_BINARY_ASSIGN:             fputs("=", out);     break;
386         case EXPR_BINARY_ADD:                fputs("+", out);     break;
387         case EXPR_BINARY_SUB:                fputs("-", out);     break;
388         case EXPR_BINARY_MUL:                fputs("*", out);     break;
389         case EXPR_BINARY_MOD:                fputs("%", out);     break;
390         case EXPR_BINARY_DIV:                fputs("/", out);     break;
391         case EXPR_BINARY_BITWISE_OR:         fputs("|", out);     break;
392         case EXPR_BINARY_BITWISE_AND:        fputs("&", out);     break;
393         case EXPR_BINARY_BITWISE_XOR:        fputs("^", out);     break;
394         case EXPR_BINARY_LOGICAL_OR:         fputs("||", out);    break;
395         case EXPR_BINARY_LOGICAL_AND:        fputs("&&", out);    break;
396         case EXPR_BINARY_NOTEQUAL:           fputs("!=", out);    break;
397         case EXPR_BINARY_EQUAL:              fputs("==", out);    break;
398         case EXPR_BINARY_LESS:               fputs("<", out);     break;
399         case EXPR_BINARY_LESSEQUAL:          fputs("<=", out);    break;
400         case EXPR_BINARY_GREATER:            fputs(">", out);     break;
401         case EXPR_BINARY_GREATEREQUAL:       fputs(">=", out);    break;
402         case EXPR_BINARY_SHIFTLEFT:          fputs("<<", out);    break;
403         case EXPR_BINARY_SHIFTRIGHT:         fputs(">>", out);    break;
404
405         case EXPR_BINARY_ADD_ASSIGN:         fputs("+=", out);    break;
406         case EXPR_BINARY_SUB_ASSIGN:         fputs("-=", out);    break;
407         case EXPR_BINARY_MUL_ASSIGN:         fputs("*=", out);    break;
408         case EXPR_BINARY_MOD_ASSIGN:         fputs("%=", out);    break;
409         case EXPR_BINARY_DIV_ASSIGN:         fputs("/=", out);    break;
410         case EXPR_BINARY_BITWISE_OR_ASSIGN:  fputs("|=", out);    break;
411         case EXPR_BINARY_BITWISE_AND_ASSIGN: fputs("&=", out);    break;
412         case EXPR_BINARY_BITWISE_XOR_ASSIGN: fputs("^=", out);    break;
413         case EXPR_BINARY_SHIFTLEFT_ASSIGN:   fputs("<<=", out);   break;
414         case EXPR_BINARY_SHIFTRIGHT_ASSIGN:  fputs(">>=", out);   break;
415         default: panic("invalid binexpression found");
416         }
417         fputc(' ', out);
418         print_expression_prec(binexpr->right, prec - r2l);
419 }
420
421 /**
422  * Prints an unary expression.
423  *
424  * @param unexpr   the unary expression
425  */
426 static void print_unary_expression(const unary_expression_t *unexpr)
427 {
428         unsigned prec = get_expression_precedence(unexpr->base.kind);
429         switch(unexpr->base.kind) {
430         case EXPR_UNARY_NEGATE:           fputs("-", out);  break;
431         case EXPR_UNARY_PLUS:             fputs("+", out);  break;
432         case EXPR_UNARY_NOT:              fputs("!", out);  break;
433         case EXPR_UNARY_BITWISE_NEGATE:   fputs("~", out);  break;
434         case EXPR_UNARY_PREFIX_INCREMENT: fputs("++", out); break;
435         case EXPR_UNARY_PREFIX_DECREMENT: fputs("--", out); break;
436         case EXPR_UNARY_DEREFERENCE:      fputs("*", out);  break;
437         case EXPR_UNARY_TAKE_ADDRESS:     fputs("&", out);  break;
438
439         case EXPR_UNARY_BITFIELD_EXTRACT:
440                 print_expression_prec(unexpr->value, prec);
441                 return;
442
443         case EXPR_UNARY_POSTFIX_INCREMENT:
444                 print_expression_prec(unexpr->value, prec);
445                 fputs("++", out);
446                 return;
447         case EXPR_UNARY_POSTFIX_DECREMENT:
448                 print_expression_prec(unexpr->value, prec);
449                 fputs("--", out);
450                 return;
451         case EXPR_UNARY_CAST_IMPLICIT:
452                 if(!print_implicit_casts) {
453                         print_expression_prec(unexpr->value, prec);
454                         return;
455                 }
456                 /* fallthrough */
457         case EXPR_UNARY_CAST:
458                 fputc('(', out);
459                 print_type(unexpr->base.type);
460                 fputc(')', out);
461                 break;
462         case EXPR_UNARY_ASSUME:
463                 fputs("__assume(", out);
464                 print_expression_prec(unexpr->value, PREC_COMMA + 1);
465                 fputc(')', out);
466                 return;
467         default:
468                 panic("invalid unary expression found");
469         }
470         print_expression_prec(unexpr->value, prec);
471 }
472
473 /**
474  * Prints a reference expression.
475  *
476  * @param ref   the reference expression
477  */
478 static void print_reference_expression(const reference_expression_t *ref)
479 {
480         fprintf(out, "%s", ref->declaration->symbol->string);
481 }
482
483 /**
484  * Prints an array expression.
485  *
486  * @param expression   the array expression
487  */
488 static void print_array_expression(const array_access_expression_t *expression)
489 {
490         unsigned prec = get_expression_precedence(expression->base.kind);
491         if(!expression->flipped) {
492                 print_expression_prec(expression->array_ref, prec);
493                 fputc('[', out);
494                 print_expression_prec(expression->index, prec);
495                 fputc(']', out);
496         } else {
497                 print_expression_prec(expression->index, prec);
498                 fputc('[', out);
499                 print_expression_prec(expression->array_ref, prec);
500                 fputc(']', out);
501         }
502 }
503
504 /**
505  * Prints a typeproperty expression (sizeof or __alignof__).
506  *
507  * @param expression   the type property expression
508  */
509 static void print_typeprop_expression(const typeprop_expression_t *expression)
510 {
511         if (expression->base.kind == EXPR_SIZEOF) {
512                 fputs("sizeof", out);
513         } else {
514                 assert(expression->base.kind == EXPR_ALIGNOF);
515                 fputs("__alignof__", out);
516         }
517         if(expression->tp_expression != NULL) {
518                 /* always print the '()' here, sizeof x is right but unusual */
519                 fputc('(', out);
520                 print_expression_prec(expression->tp_expression, PREC_ACCESS);
521                 fputc(')', out);
522         } else {
523                 fputc('(', out);
524                 print_type(expression->type);
525                 fputc(')', out);
526         }
527 }
528
529 /**
530  * Prints an builtin symbol.
531  *
532  * @param expression   the builtin symbol expression
533  */
534 static void print_builtin_symbol(const builtin_symbol_expression_t *expression)
535 {
536         fputs(expression->symbol->string, out);
537 }
538
539 /**
540  * Prints a builtin constant expression.
541  *
542  * @param expression   the builtin constant expression
543  */
544 static void print_builtin_constant(const builtin_constant_expression_t *expression)
545 {
546         fputs("__builtin_constant_p(", out);
547         print_expression_prec(expression->value, PREC_COMMA + 1);
548         fputc(')', out);
549 }
550
551 /**
552  * Prints a builtin prefetch expression.
553  *
554  * @param expression   the builtin prefetch expression
555  */
556 static void print_builtin_prefetch(const builtin_prefetch_expression_t *expression)
557 {
558         fputs("__builtin_prefetch(", out);
559         print_expression_prec(expression->adr, PREC_COMMA + 1);
560         if (expression->rw) {
561                 fputc(',', out);
562                 print_expression_prec(expression->rw, PREC_COMMA + 1);
563         }
564         if (expression->locality) {
565                 fputc(',', out);
566                 print_expression_prec(expression->locality, PREC_COMMA + 1);
567         }
568         fputc(')', out);
569 }
570
571 /**
572  * Prints a conditional expression.
573  *
574  * @param expression   the conditional expression
575  */
576 static void print_conditional(const conditional_expression_t *expression)
577 {
578         unsigned prec = get_expression_precedence(expression->base.kind);
579         fputs("(", out);
580         print_expression_prec(expression->condition, prec);
581         fputs(" ? ", out);
582         print_expression_prec(expression->true_expression, prec);
583         fputs(" : ", out);
584         print_expression_prec(expression->false_expression, prec);
585         fputs(")", out);
586 }
587
588 /**
589  * Prints a va_start expression.
590  *
591  * @param expression   the va_start expression
592  */
593 static void print_va_start(const va_start_expression_t *const expression)
594 {
595         fputs("__builtin_va_start(", out);
596         print_expression_prec(expression->ap, PREC_COMMA + 1);
597         fputs(", ", out);
598         fputs(expression->parameter->symbol->string, out);
599         fputs(")", out);
600 }
601
602 /**
603  * Prints a va_arg expression.
604  *
605  * @param expression   the va_arg expression
606  */
607 static void print_va_arg(const va_arg_expression_t *expression)
608 {
609         fputs("__builtin_va_arg(", out);
610         print_expression_prec(expression->ap, PREC_COMMA + 1);
611         fputs(", ", out);
612         print_type(expression->base.type);
613         fputs(")", out);
614 }
615
616 /**
617  * Prints a select expression (. or ->).
618  *
619  * @param expression   the select expression
620  */
621 static void print_select(const select_expression_t *expression)
622 {
623         unsigned prec = get_expression_precedence(expression->base.kind);
624         print_expression_prec(expression->compound, prec);
625         if(is_type_pointer(expression->compound->base.type)) {
626                 fputs("->", out);
627         } else {
628                 fputc('.', out);
629         }
630         fputs(expression->symbol->string, out);
631 }
632
633 /**
634  * Prints a type classify expression.
635  *
636  * @param expr   the type classify expression
637  */
638 static void print_classify_type_expression(
639         const classify_type_expression_t *const expr)
640 {
641         fputs("__builtin_classify_type(", out);
642         print_expression_prec(expr->type_expression, PREC_COMMA + 1);
643         fputc(')', out);
644 }
645
646 /**
647  * Prints a designator.
648  *
649  * @param designator  the designator
650  */
651 static void print_designator(const designator_t *designator)
652 {
653         for ( ; designator != NULL; designator = designator->next) {
654                 if (designator->symbol == NULL) {
655                         fputc('[', out);
656                         print_expression_prec(designator->array_index, PREC_ACCESS);
657                         fputc(']', out);
658                 } else {
659                         fputc('.', out);
660                         fputs(designator->symbol->string, out);
661                 }
662         }
663 }
664
665 /**
666  * Prints an offsetof expression.
667  *
668  * @param expression   the offset expression
669  */
670 static void print_offsetof_expression(const offsetof_expression_t *expression)
671 {
672         fputs("__builtin_offsetof", out);
673         fputc('(', out);
674         print_type(expression->type);
675         fputc(',', out);
676         print_designator(expression->designator);
677         fputc(')', out);
678 }
679
680 /**
681  * Prints a statement expression.
682  *
683  * @param expression   the statement expression
684  */
685 static void print_statement_expression(const statement_expression_t *expression)
686 {
687         fputc('(', out);
688         print_statement(expression->statement);
689         fputc(')', out);
690 }
691
692 /**
693  * Prints an expression with parenthesis if needed.
694  *
695  * @param expression  the expression to print
696  * @param top_prec    the precedence of the user of this expression.
697  */
698 static void print_expression_prec(const expression_t *expression, unsigned top_prec)
699 {
700         unsigned prec = get_expression_precedence(expression->base.kind);
701         if (print_parenthesis && top_prec != PREC_BOTTOM)
702                 top_prec = PREC_TOP;
703         if (top_prec > prec)
704                 fputc('(', out);
705         switch(expression->kind) {
706         case EXPR_UNKNOWN:
707         case EXPR_INVALID:
708                 fprintf(out, "$invalid expression$");
709                 break;
710         case EXPR_CHARACTER_CONSTANT:
711                 print_character_constant(&expression->conste);
712                 break;
713         case EXPR_WIDE_CHARACTER_CONSTANT:
714                 print_wide_character_constant(&expression->conste);
715                 break;
716         case EXPR_CONST:
717                 print_const(&expression->conste);
718                 break;
719         case EXPR_FUNCTION:
720         case EXPR_PRETTY_FUNCTION:
721         case EXPR_STRING_LITERAL:
722                 print_string_literal(&expression->string);
723                 break;
724         case EXPR_WIDE_STRING_LITERAL:
725                 print_wide_string_literal(&expression->wide_string);
726                 break;
727         case EXPR_COMPOUND_LITERAL:
728                 print_compound_literal(&expression->compound_literal);
729                 break;
730         case EXPR_CALL:
731                 print_call_expression(&expression->call);
732                 break;
733         EXPR_BINARY_CASES
734                 print_binary_expression(&expression->binary);
735                 break;
736         case EXPR_REFERENCE:
737                 print_reference_expression(&expression->reference);
738                 break;
739         case EXPR_ARRAY_ACCESS:
740                 print_array_expression(&expression->array_access);
741                 break;
742         EXPR_UNARY_CASES
743                 print_unary_expression(&expression->unary);
744                 break;
745         case EXPR_SIZEOF:
746         case EXPR_ALIGNOF:
747                 print_typeprop_expression(&expression->typeprop);
748                 break;
749         case EXPR_BUILTIN_SYMBOL:
750                 print_builtin_symbol(&expression->builtin_symbol);
751                 break;
752         case EXPR_BUILTIN_CONSTANT_P:
753                 print_builtin_constant(&expression->builtin_constant);
754                 break;
755         case EXPR_BUILTIN_PREFETCH:
756                 print_builtin_prefetch(&expression->builtin_prefetch);
757                 break;
758         case EXPR_CONDITIONAL:
759                 print_conditional(&expression->conditional);
760                 break;
761         case EXPR_VA_START:
762                 print_va_start(&expression->va_starte);
763                 break;
764         case EXPR_VA_ARG:
765                 print_va_arg(&expression->va_arge);
766                 break;
767         case EXPR_SELECT:
768                 print_select(&expression->select);
769                 break;
770         case EXPR_CLASSIFY_TYPE:
771                 print_classify_type_expression(&expression->classify_type);
772                 break;
773         case EXPR_OFFSETOF:
774                 print_offsetof_expression(&expression->offsetofe);
775                 break;
776         case EXPR_STATEMENT:
777                 print_statement_expression(&expression->statement);
778                 break;
779
780         default:
781                 /* TODO */
782                 fprintf(out, "some expression of type %d", (int) expression->kind);
783                 break;
784         }
785         if (top_prec > prec)
786                 fputc(')', out);
787 }
788
789 /**
790  * Print an compound statement.
791  *
792  * @param block  the compound statement
793  */
794 static void print_compound_statement(const compound_statement_t *block)
795 {
796         fputs("{\n", out);
797         ++indent;
798
799         statement_t *statement = block->statements;
800         while(statement != NULL) {
801                 if (statement->base.kind == STATEMENT_CASE_LABEL)
802                         --indent;
803                 print_indent();
804                 print_statement(statement);
805
806                 statement = statement->base.next;
807         }
808         --indent;
809         print_indent();
810         fputs("}\n", out);
811 }
812
813 /**
814  * Print a return statement.
815  *
816  * @param statement  the return statement
817  */
818 static void print_return_statement(const return_statement_t *statement)
819 {
820         fprintf(out, "return ");
821         if(statement->value != NULL)
822                 print_expression(statement->value);
823         fputs(";\n", out);
824 }
825
826 /**
827  * Print an expression statement.
828  *
829  * @param statement  the expression statement
830  */
831 static void print_expression_statement(const expression_statement_t *statement)
832 {
833         print_expression(statement->expression);
834         fputs(";\n", out);
835 }
836
837 /**
838  * Print a goto statement.
839  *
840  * @param statement  the goto statement
841  */
842 static void print_goto_statement(const goto_statement_t *statement)
843 {
844         fprintf(out, "goto ");
845         fputs(statement->label->symbol->string, out);
846         fprintf(stderr, "(%p)", (void*) statement->label);
847         fputs(";\n", out);
848 }
849
850 /**
851  * Print a label statement.
852  *
853  * @param statement  the label statement
854  */
855 static void print_label_statement(const label_statement_t *statement)
856 {
857         fprintf(stderr, "(%p)", (void*) statement->label);
858         fprintf(out, "%s:\n", statement->label->symbol->string);
859         print_statement(statement->statement);
860 }
861
862 /**
863  * Print an if statement.
864  *
865  * @param statement  the if statement
866  */
867 static void print_if_statement(const if_statement_t *statement)
868 {
869         fputs("if (", out);
870         print_expression(statement->condition);
871         fputs(") ", out);
872         if(statement->true_statement != NULL) {
873                 print_statement(statement->true_statement);
874         }
875
876         if(statement->false_statement != NULL) {
877                 print_indent();
878                 fputs("else ", out);
879                 print_statement(statement->false_statement);
880         }
881 }
882
883 /**
884  * Print a switch statement.
885  *
886  * @param statement  the switch statement
887  */
888 static void print_switch_statement(const switch_statement_t *statement)
889 {
890         fputs("switch (", out);
891         print_expression(statement->expression);
892         fputs(") ", out);
893         print_statement(statement->body);
894 }
895
896 /**
897  * Print a case label (including the default label).
898  *
899  * @param statement  the case label statement
900  */
901 static void print_case_label(const case_label_statement_t *statement)
902 {
903         if(statement->expression == NULL) {
904                 fputs("default:\n", out);
905         } else {
906                 fputs("case ", out);
907                 print_expression(statement->expression);
908                 if (statement->end_range != NULL) {
909                         fputs(" ... ", out);
910                         print_expression(statement->end_range);
911                 }
912                 fputs(":\n", out);
913         }
914         ++indent;
915         if(statement->statement != NULL) {
916                 if (statement->statement->base.kind == STATEMENT_CASE_LABEL) {
917                         --indent;
918                 }
919                 print_indent();
920                 print_statement(statement->statement);
921         }
922 }
923
924 /**
925  * Print a declaration statement.
926  *
927  * @param statement   the statement
928  */
929 static void print_declaration_statement(
930                 const declaration_statement_t *statement)
931 {
932         int first = 1;
933         declaration_t *declaration = statement->declarations_begin;
934         for( ; declaration != statement->declarations_end->next;
935                declaration = declaration->next) {
936                 if(!first) {
937                         print_indent();
938                 } else {
939                         first = 0;
940                 }
941                 print_declaration(declaration);
942                 fputc('\n', out);
943         }
944 }
945
946 /**
947  * Print a while statement.
948  *
949  * @param statement   the statement
950  */
951 static void print_while_statement(const while_statement_t *statement)
952 {
953         fputs("while (", out);
954         print_expression(statement->condition);
955         fputs(") ", out);
956         print_statement(statement->body);
957 }
958
959 /**
960  * Print a do-while statement.
961  *
962  * @param statement   the statement
963  */
964 static void print_do_while_statement(const do_while_statement_t *statement)
965 {
966         fputs("do ", out);
967         print_statement(statement->body);
968         print_indent();
969         fputs("while (", out);
970         print_expression(statement->condition);
971         fputs(");\n", out);
972 }
973
974 /**
975  * Print a for statement.
976  *
977  * @param statement   the statement
978  */
979 static void print_for_statement(const for_statement_t *statement)
980 {
981         fputs("for (", out);
982         if(statement->scope.declarations != NULL) {
983                 assert(statement->initialisation == NULL);
984                 print_declaration(statement->scope.declarations);
985                 if(statement->scope.declarations->next != NULL) {
986                         panic("multiple declarations in for statement not supported yet");
987                 }
988                 fputc(' ', out);
989         } else {
990                 if(statement->initialisation) {
991                         print_expression(statement->initialisation);
992                 }
993                 fputs("; ", out);
994         }
995         if(statement->condition != NULL) {
996                 print_expression(statement->condition);
997         }
998         fputs("; ", out);
999         if(statement->step != NULL) {
1000                 print_expression(statement->step);
1001         }
1002         fputs(")", out);
1003         print_statement(statement->body);
1004 }
1005
1006 /**
1007  * Print assembler constraints.
1008  *
1009  * @param constraints   the constraints
1010  */
1011 static void print_asm_constraints(asm_constraint_t *constraints)
1012 {
1013         asm_constraint_t *constraint = constraints;
1014         for( ; constraint != NULL; constraint = constraint->next) {
1015                 if(constraint != constraints)
1016                         fputs(", ", out);
1017
1018                 if(constraint->symbol) {
1019                         fprintf(out, "[%s] ", constraint->symbol->string);
1020                 }
1021                 print_quoted_string(&constraint->constraints, '"');
1022                 fputs(" (", out);
1023                 print_expression(constraint->expression);
1024                 fputs(")", out);
1025         }
1026 }
1027
1028 /**
1029  * Print assembler clobbers.
1030  *
1031  * @param clobbers   the clobbers
1032  */
1033 static void print_asm_clobbers(asm_clobber_t *clobbers)
1034 {
1035         asm_clobber_t *clobber = clobbers;
1036         for( ; clobber != NULL; clobber = clobber->next) {
1037                 if(clobber != clobbers)
1038                         fputs(", ", out);
1039
1040                 print_quoted_string(&clobber->clobber, '"');
1041         }
1042 }
1043
1044 /**
1045  * Print an assembler statement.
1046  *
1047  * @param statement   the statement
1048  */
1049 static void print_asm_statement(const asm_statement_t *statement)
1050 {
1051         fputs("asm ", out);
1052         if(statement->is_volatile) {
1053                 fputs("volatile ", out);
1054         }
1055         fputs("(", out);
1056         print_quoted_string(&statement->asm_text, '"');
1057         if(statement->inputs == NULL && statement->outputs == NULL
1058                         && statement->clobbers == NULL)
1059                 goto end_of_print_asm_statement;
1060
1061         fputs(" : ", out);
1062         print_asm_constraints(statement->inputs);
1063         if(statement->outputs == NULL && statement->clobbers == NULL)
1064                 goto end_of_print_asm_statement;
1065
1066         fputs(" : ", out);
1067         print_asm_constraints(statement->outputs);
1068         if(statement->clobbers == NULL)
1069                 goto end_of_print_asm_statement;
1070
1071         fputs(" : ", out);
1072         print_asm_clobbers(statement->clobbers);
1073
1074 end_of_print_asm_statement:
1075         fputs(");\n", out);
1076 }
1077
1078 /**
1079  * Print a statement.
1080  *
1081  * @param statement   the statement
1082  */
1083 void print_statement(const statement_t *statement)
1084 {
1085         switch(statement->kind) {
1086         case STATEMENT_EMPTY:
1087                 fputs(";\n", out);
1088                 break;
1089         case STATEMENT_COMPOUND:
1090                 print_compound_statement(&statement->compound);
1091                 break;
1092         case STATEMENT_RETURN:
1093                 print_return_statement(&statement->returns);
1094                 break;
1095         case STATEMENT_EXPRESSION:
1096                 print_expression_statement(&statement->expression);
1097                 break;
1098         case STATEMENT_LABEL:
1099                 print_label_statement(&statement->label);
1100                 break;
1101         case STATEMENT_GOTO:
1102                 print_goto_statement(&statement->gotos);
1103                 break;
1104         case STATEMENT_CONTINUE:
1105                 fputs("continue;\n", out);
1106                 break;
1107         case STATEMENT_BREAK:
1108                 fputs("break;\n", out);
1109                 break;
1110         case STATEMENT_IF:
1111                 print_if_statement(&statement->ifs);
1112                 break;
1113         case STATEMENT_SWITCH:
1114                 print_switch_statement(&statement->switchs);
1115                 break;
1116         case STATEMENT_CASE_LABEL:
1117                 print_case_label(&statement->case_label);
1118                 break;
1119         case STATEMENT_DECLARATION:
1120                 print_declaration_statement(&statement->declaration);
1121                 break;
1122         case STATEMENT_WHILE:
1123                 print_while_statement(&statement->whiles);
1124                 break;
1125         case STATEMENT_DO_WHILE:
1126                 print_do_while_statement(&statement->do_while);
1127                 break;
1128         case STATEMENT_FOR:
1129                 print_for_statement(&statement->fors);
1130                 break;
1131         case STATEMENT_ASM:
1132                 print_asm_statement(&statement->asms);
1133                 break;
1134         case STATEMENT_INVALID:
1135                 fprintf(out, "$invalid statement$");
1136                 break;
1137         }
1138 }
1139
1140 /**
1141  * Print a storage class.
1142  *
1143  * @param storage_class   the storage class
1144  */
1145 static void print_storage_class(storage_class_tag_t storage_class)
1146 {
1147         switch(storage_class) {
1148         case STORAGE_CLASS_ENUM_ENTRY:
1149         case STORAGE_CLASS_NONE:
1150                 break;
1151         case STORAGE_CLASS_TYPEDEF:       fputs("typedef ",        out); break;
1152         case STORAGE_CLASS_EXTERN:        fputs("extern ",         out); break;
1153         case STORAGE_CLASS_STATIC:        fputs("static ",         out); break;
1154         case STORAGE_CLASS_AUTO:          fputs("auto ",           out); break;
1155         case STORAGE_CLASS_REGISTER:      fputs("register ",       out); break;
1156         case STORAGE_CLASS_THREAD:        fputs("__thread",        out); break;
1157         case STORAGE_CLASS_THREAD_EXTERN: fputs("extern __thread", out); break;
1158         case STORAGE_CLASS_THREAD_STATIC: fputs("static __thread", out); break;
1159         }
1160 }
1161
1162 /**
1163  * Print an initializer.
1164  *
1165  * @param initializer  the initializer
1166  */
1167 void print_initializer(const initializer_t *initializer)
1168 {
1169         if(initializer == NULL) {
1170                 fputs("{ NIL-INITIALIZER }", out);
1171                 return;
1172         }
1173
1174         switch(initializer->kind) {
1175         case INITIALIZER_VALUE: {
1176                 const initializer_value_t *value = &initializer->value;
1177                 print_expression(value->value);
1178                 return;
1179         }
1180         case INITIALIZER_LIST: {
1181                 assert(initializer->kind == INITIALIZER_LIST);
1182                 fputs("{ ", out);
1183                 const initializer_list_t *list = &initializer->list;
1184
1185                 for(size_t i = 0 ; i < list->len; ++i) {
1186                         const initializer_t *sub_init = list->initializers[i];
1187                         print_initializer(list->initializers[i]);
1188                         if(i < list->len-1 && sub_init->kind != INITIALIZER_DESIGNATOR) {
1189                                 fputs(", ", out);
1190                         }
1191                 }
1192                 fputs(" }", out);
1193                 return;
1194         }
1195         case INITIALIZER_STRING:
1196                 print_quoted_string(&initializer->string.string, '"');
1197                 return;
1198         case INITIALIZER_WIDE_STRING:
1199                 print_quoted_wide_string(&initializer->wide_string.string, '"');
1200                 return;
1201         case INITIALIZER_DESIGNATOR:
1202                 print_designator(initializer->designator.designator);
1203                 fputs(" = ", out);
1204                 return;
1205         }
1206
1207         panic("invalid initializer kind found");
1208 }
1209
1210 /**
1211  * Print microsoft extended declaration modifiers.
1212  */
1213 static void print_ms_modifiers(const declaration_t *declaration) {
1214         decl_modifiers_t modifiers = declaration->modifiers;
1215
1216         /* DM_FORCEINLINE handled outside. */
1217         if((modifiers & ~DM_FORCEINLINE) != 0 || declaration->alignment != 0 ||
1218             declaration->get_property_sym != NULL || declaration->put_property_sym != NULL) {
1219                 char *next = "(";
1220
1221                 fputs("__declspec", out);
1222                 if(modifiers & DM_DLLIMPORT) {
1223                         fputs(next, out); next = ", "; fputs("dllimport", out);
1224                 }
1225                 if(modifiers & DM_DLLEXPORT) {
1226                         fputs(next, out); next = ", "; fputs("dllexport", out);
1227                 }
1228                 if(modifiers & DM_THREAD) {
1229                         fputs(next, out); next = ", "; fputs("thread", out);
1230                 }
1231                 if(modifiers & DM_NAKED) {
1232                         fputs(next, out); next = ", "; fputs("naked", out);
1233                 }
1234                 if(modifiers & DM_THREAD) {
1235                         fputs(next, out); next = ", "; fputs("thread", out);
1236                 }
1237                 if(modifiers & DM_SELECTANY) {
1238                         fputs(next, out); next = ", "; fputs("selectany", out);
1239                 }
1240                 if(modifiers & DM_NOTHROW) {
1241                         fputs(next, out); next = ", "; fputs("nothrow", out);
1242                 }
1243                 if(modifiers & DM_NORETURN) {
1244                         fputs(next, out); next = ", "; fputs("noreturn", out);
1245                 }
1246                 if(modifiers & DM_NOINLINE) {
1247                         fputs(next, out); next = ", "; fputs("noinline", out);
1248                 }
1249                 if(modifiers & DM_DEPRECATED) {
1250                         fputs(next, out); next = ", "; fputs("deprecated", out);
1251                         if(declaration->deprecated_string != NULL)
1252                                 fprintf(out, "(\"%s\")", declaration->deprecated_string);
1253                 }
1254                 if(declaration->alignment != 0) {
1255                         fputs(next, out); next = ", "; fprintf(out, "align(%u)", declaration->alignment);
1256                 }
1257             if(declaration->get_property_sym != NULL || declaration->put_property_sym != NULL) {
1258                 char *comma = "";
1259                         fputs(next, out); next = ", "; fprintf(out, "property(");
1260                 if(declaration->get_property_sym != NULL) {
1261                         fprintf(out, "get=%s", declaration->get_property_sym->string);
1262                         comma = ", ";
1263                         }
1264                 if(declaration->put_property_sym != NULL)
1265                         fprintf(out, "%sput=%s", comma, declaration->put_property_sym->string);
1266                         fputc(')', out);
1267                 }
1268                 fputs(") ", out);
1269         }
1270 }
1271
1272 /**
1273  * Print a declaration in the NORMAL namespace.
1274  *
1275  * @param declaration  the declaration
1276  */
1277 static void print_normal_declaration(const declaration_t *declaration)
1278 {
1279         print_storage_class((storage_class_tag_t) declaration->declared_storage_class);
1280         if(declaration->is_inline) {
1281                 if(declaration->modifiers & DM_FORCEINLINE)
1282                         fputs("__forceinline ", out);
1283                 else {
1284                         if(declaration->modifiers & DM_MICROSOFT_INLINE)
1285                                 fputs("__inline ", out);
1286                         else
1287                                 fputs("inline ", out);
1288                 }
1289         }
1290         print_ms_modifiers(declaration);
1291         print_type_ext(declaration->type, declaration->symbol,
1292                        &declaration->scope);
1293
1294         if(declaration->type->kind == TYPE_FUNCTION) {
1295                 if(declaration->init.statement != NULL) {
1296                         fputs("\n", out);
1297                         print_statement(declaration->init.statement);
1298                         return;
1299                 }
1300         } else if(declaration->init.initializer != NULL) {
1301                 fputs(" = ", out);
1302                 print_initializer(declaration->init.initializer);
1303         }
1304         fputc(';', out);
1305 }
1306
1307 /**
1308  * Prints an expression.
1309  *
1310  * @param expression  the expression
1311  */
1312 void print_expression(const expression_t *expression) {
1313         print_expression_prec(expression, PREC_BOTTOM);
1314 }
1315
1316 /**
1317  * Print a declaration.
1318  *
1319  * @param declaration  the declaration
1320  */
1321 void print_declaration(const declaration_t *declaration)
1322 {
1323         if(declaration->namespc != NAMESPACE_NORMAL &&
1324                         declaration->symbol == NULL)
1325                 return;
1326
1327         switch(declaration->namespc) {
1328         case NAMESPACE_NORMAL:
1329                 print_normal_declaration(declaration);
1330                 break;
1331         case NAMESPACE_STRUCT:
1332                 fputs("struct ", out);
1333                 fputs(declaration->symbol->string, out);
1334                 fputc(' ', out);
1335                 print_compound_definition(declaration);
1336                 fputc(';', out);
1337                 break;
1338         case NAMESPACE_UNION:
1339                 fputs("union ", out);
1340                 fputs(declaration->symbol->string, out);
1341                 fputc(' ', out);
1342                 print_compound_definition(declaration);
1343                 fputc(';', out);
1344                 break;
1345         case NAMESPACE_ENUM:
1346                 fputs("enum ", out);
1347                 fputs(declaration->symbol->string, out);
1348                 fputc(' ', out);
1349                 print_enum_definition(declaration);
1350                 fputc(';', out);
1351                 break;
1352         }
1353 }
1354
1355 /**
1356  * Print the AST of a translation unit.
1357  *
1358  * @param unit   the translation unit
1359  */
1360 void print_ast(const translation_unit_t *unit)
1361 {
1362         inc_type_visited();
1363
1364         declaration_t *declaration = unit->scope.declarations;
1365         for( ; declaration != NULL; declaration = declaration->next) {
1366                 if(declaration->storage_class == STORAGE_CLASS_ENUM_ENTRY)
1367                         continue;
1368                 if(declaration->namespc != NAMESPACE_NORMAL &&
1369                                 declaration->symbol == NULL)
1370                         continue;
1371
1372                 print_indent();
1373                 print_declaration(declaration);
1374                 fputc('\n', out);
1375         }
1376 }
1377
1378 bool is_constant_initializer(const initializer_t *initializer)
1379 {
1380         switch(initializer->kind) {
1381         case INITIALIZER_STRING:
1382         case INITIALIZER_WIDE_STRING:
1383         case INITIALIZER_DESIGNATOR:
1384                 return true;
1385
1386         case INITIALIZER_VALUE:
1387                 return is_constant_expression(initializer->value.value);
1388
1389         case INITIALIZER_LIST: {
1390                 for(size_t i = 0; i < initializer->list.len; ++i) {
1391                         initializer_t *sub_initializer = initializer->list.initializers[i];
1392                         if(!is_constant_initializer(sub_initializer))
1393                                 return false;
1394                 }
1395                 return true;
1396         }
1397         }
1398         panic("invalid initializer kind found");
1399 }
1400
1401 static bool is_object_with_constant_address(const expression_t *expression)
1402 {
1403         switch(expression->kind) {
1404         case EXPR_UNARY_DEREFERENCE:
1405                 return is_address_constant(expression->unary.value);
1406
1407         case EXPR_SELECT: {
1408                 if(is_type_pointer(expression->select.compound->base.type)) {
1409                         /* it's a -> */
1410                         return is_address_constant(expression->select.compound);
1411                 } else {
1412                         return is_object_with_constant_address(expression->select.compound);
1413                 }
1414         }
1415
1416         case EXPR_ARRAY_ACCESS:
1417                 return is_constant_expression(expression->array_access.index)
1418                         && is_address_constant(expression->array_access.array_ref);
1419
1420         case EXPR_REFERENCE: {
1421                 declaration_t *declaration = expression->reference.declaration;
1422                 switch((storage_class_tag_t) declaration->storage_class) {
1423                 case STORAGE_CLASS_NONE:
1424                 case STORAGE_CLASS_EXTERN:
1425                 case STORAGE_CLASS_STATIC:
1426                         return true;
1427                 default:
1428                         return false;
1429                 }
1430         }
1431
1432         default:
1433                 return false;
1434         }
1435 }
1436
1437 bool is_address_constant(const expression_t *expression)
1438 {
1439         switch(expression->kind) {
1440         case EXPR_UNARY_TAKE_ADDRESS:
1441                 return is_object_with_constant_address(expression->unary.value);
1442
1443         case EXPR_UNARY_CAST:
1444                 return is_type_pointer(skip_typeref(expression->base.type))
1445                         && (is_constant_expression(expression->unary.value)
1446                         || is_address_constant(expression->unary.value));
1447
1448         case EXPR_BINARY_ADD:
1449         case EXPR_BINARY_SUB: {
1450                 expression_t *left  = expression->binary.left;
1451                 expression_t *right = expression->binary.right;
1452
1453                 if(is_type_pointer(skip_typeref(left->base.type))) {
1454                         return is_address_constant(left) && is_constant_expression(right);
1455                 } else if(is_type_pointer(skip_typeref(right->base.type))) {
1456                         return is_constant_expression(left)     && is_address_constant(right);
1457                 }
1458
1459                 return false;
1460         }
1461
1462         case EXPR_REFERENCE: {
1463                 declaration_t *declaration = expression->reference.declaration;
1464                 type_t *type = skip_typeref(declaration->type);
1465                 if(is_type_function(type))
1466                         return true;
1467                 if(is_type_array(type)) {
1468                         return is_object_with_constant_address(expression);
1469                 }
1470                 return false;
1471         }
1472
1473         default:
1474                 return false;
1475         }
1476 }
1477
1478 bool is_constant_expression(const expression_t *expression)
1479 {
1480         switch(expression->kind) {
1481
1482         case EXPR_CONST:
1483         case EXPR_CHARACTER_CONSTANT:
1484         case EXPR_WIDE_CHARACTER_CONSTANT:
1485         case EXPR_STRING_LITERAL:
1486         case EXPR_WIDE_STRING_LITERAL:
1487         case EXPR_SIZEOF:
1488         case EXPR_CLASSIFY_TYPE:
1489         case EXPR_FUNCTION:
1490         case EXPR_PRETTY_FUNCTION:
1491         case EXPR_OFFSETOF:
1492         case EXPR_ALIGNOF:
1493         case EXPR_BUILTIN_CONSTANT_P:
1494                 return true;
1495
1496         case EXPR_BUILTIN_SYMBOL:
1497         case EXPR_BUILTIN_PREFETCH:
1498         case EXPR_CALL:
1499         case EXPR_SELECT:
1500         case EXPR_VA_START:
1501         case EXPR_VA_ARG:
1502         case EXPR_STATEMENT:
1503         case EXPR_UNARY_POSTFIX_INCREMENT:
1504         case EXPR_UNARY_POSTFIX_DECREMENT:
1505         case EXPR_UNARY_PREFIX_INCREMENT:
1506         case EXPR_UNARY_PREFIX_DECREMENT:
1507         case EXPR_UNARY_BITFIELD_EXTRACT:
1508         case EXPR_UNARY_ASSUME: /* has VOID type */
1509         case EXPR_UNARY_DEREFERENCE:
1510         case EXPR_UNARY_TAKE_ADDRESS:
1511         case EXPR_BINARY_ASSIGN:
1512         case EXPR_BINARY_MUL_ASSIGN:
1513         case EXPR_BINARY_DIV_ASSIGN:
1514         case EXPR_BINARY_MOD_ASSIGN:
1515         case EXPR_BINARY_ADD_ASSIGN:
1516         case EXPR_BINARY_SUB_ASSIGN:
1517         case EXPR_BINARY_SHIFTLEFT_ASSIGN:
1518         case EXPR_BINARY_SHIFTRIGHT_ASSIGN:
1519         case EXPR_BINARY_BITWISE_AND_ASSIGN:
1520         case EXPR_BINARY_BITWISE_XOR_ASSIGN:
1521         case EXPR_BINARY_BITWISE_OR_ASSIGN:
1522         case EXPR_BINARY_COMMA:
1523                 return false;
1524
1525         case EXPR_UNARY_NEGATE:
1526         case EXPR_UNARY_PLUS:
1527         case EXPR_UNARY_BITWISE_NEGATE:
1528         case EXPR_UNARY_NOT:
1529                 return is_constant_expression(expression->unary.value);
1530
1531         case EXPR_UNARY_CAST:
1532         case EXPR_UNARY_CAST_IMPLICIT:
1533                 return is_type_arithmetic(skip_typeref(expression->base.type))
1534                         && is_constant_expression(expression->unary.value);
1535
1536         case EXPR_BINARY_ADD:
1537         case EXPR_BINARY_SUB:
1538         case EXPR_BINARY_MUL:
1539         case EXPR_BINARY_DIV:
1540         case EXPR_BINARY_MOD:
1541         case EXPR_BINARY_EQUAL:
1542         case EXPR_BINARY_NOTEQUAL:
1543         case EXPR_BINARY_LESS:
1544         case EXPR_BINARY_LESSEQUAL:
1545         case EXPR_BINARY_GREATER:
1546         case EXPR_BINARY_GREATEREQUAL:
1547         case EXPR_BINARY_BITWISE_AND:
1548         case EXPR_BINARY_BITWISE_OR:
1549         case EXPR_BINARY_BITWISE_XOR:
1550         case EXPR_BINARY_LOGICAL_AND:
1551         case EXPR_BINARY_LOGICAL_OR:
1552         case EXPR_BINARY_SHIFTLEFT:
1553         case EXPR_BINARY_SHIFTRIGHT:
1554         case EXPR_BINARY_BUILTIN_EXPECT:
1555         case EXPR_BINARY_ISGREATER:
1556         case EXPR_BINARY_ISGREATEREQUAL:
1557         case EXPR_BINARY_ISLESS:
1558         case EXPR_BINARY_ISLESSEQUAL:
1559         case EXPR_BINARY_ISLESSGREATER:
1560         case EXPR_BINARY_ISUNORDERED:
1561                 return is_constant_expression(expression->binary.left)
1562                         && is_constant_expression(expression->binary.right);
1563
1564         case EXPR_COMPOUND_LITERAL:
1565                 return is_constant_initializer(expression->compound_literal.initializer);
1566
1567         case EXPR_CONDITIONAL: {
1568                 expression_t *condition = expression->conditional.condition;
1569                 if(!is_constant_expression(condition))
1570                         return false;
1571
1572                 long val = fold_constant(condition);
1573                 if(val != 0)
1574                         return is_constant_expression(expression->conditional.true_expression);
1575                 else
1576                         return is_constant_expression(expression->conditional.false_expression);
1577         }
1578
1579         case EXPR_ARRAY_ACCESS:
1580                 return is_constant_expression(expression->array_access.array_ref)
1581                         && is_constant_expression(expression->array_access.index);
1582
1583         case EXPR_REFERENCE: {
1584                 declaration_t *declaration = expression->reference.declaration;
1585                 if(declaration->storage_class == STORAGE_CLASS_ENUM_ENTRY)
1586                         return true;
1587
1588                 return false;
1589         }
1590
1591         case EXPR_INVALID:
1592                 return true;
1593
1594         case EXPR_UNKNOWN:
1595                 break;
1596         }
1597         panic("invalid expression found (is constant expression)");
1598 }
1599
1600 /**
1601  * Initialize the AST construction.
1602  */
1603 void init_ast(void)
1604 {
1605         obstack_init(&ast_obstack);
1606 }
1607
1608 /**
1609  * Free the AST.
1610  */
1611 void exit_ast(void)
1612 {
1613         obstack_free(&ast_obstack, NULL);
1614 }
1615
1616 /**
1617  * Set the output stream for the AST printer.
1618  *
1619  * @param stream  the output stream
1620  */
1621 void ast_set_output(FILE *stream)
1622 {
1623         out = stream;
1624         type_set_output(stream);
1625 }
1626
1627 /**
1628  * Allocate an AST object of the given size.
1629  *
1630  * @param size  the size of the object to allocate
1631  *
1632  * @return  A new allocated object in the AST memeory space.
1633  */
1634 void *(allocate_ast)(size_t size)
1635 {
1636         return _allocate_ast(size);
1637 }