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