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