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