fix is_constant_expression for conditionals
[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 - 1;
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 - 1;
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(is_type_pointer(expression->compound->base.type)) {
598                 fputs("->", out);
599         } else {
600                 fputc('.', out);
601         }
602         fputs(expression->symbol->string, out);
603 }
604
605 /**
606  * Prints a type classify expression.
607  *
608  * @param expr   the type classify expression
609  */
610 static void print_classify_type_expression(
611         const classify_type_expression_t *const expr)
612 {
613         fputs("__builtin_classify_type(", out);
614         print_expression_prec(expr->type_expression, PREC_COMMA + 1);
615         fputc(')', out);
616 }
617
618 /**
619  * Prints a designator.
620  *
621  * @param designator  the designator
622  */
623 static void print_designator(const designator_t *designator)
624 {
625         for ( ; designator != NULL; designator = designator->next) {
626                 if (designator->symbol == NULL) {
627                         fputc('[', out);
628                         print_expression_prec(designator->array_index, PREC_ACCESS);
629                         fputc(']', out);
630                 } else {
631                         fputc('.', out);
632                         fputs(designator->symbol->string, out);
633                 }
634         }
635 }
636
637 /**
638  * Prints an offsetof expression.
639  *
640  * @param expression   the offset expression
641  */
642 static void print_offsetof_expression(const offsetof_expression_t *expression)
643 {
644         fputs("__builtin_offsetof", out);
645         fputc('(', out);
646         print_type(expression->type);
647         fputc(',', out);
648         print_designator(expression->designator);
649         fputc(')', out);
650 }
651
652 /**
653  * Prints a statement expression.
654  *
655  * @param expression   the statement expression
656  */
657 static void print_statement_expression(const statement_expression_t *expression)
658 {
659         fputc('(', out);
660         print_statement(expression->statement);
661         fputc(')', out);
662 }
663
664 /**
665  * Prints an expression with parenthesis if needed.
666  *
667  * @param expression  the expression to print
668  * @param top_prec    the precedence of the user of this expression.
669  */
670 static void print_expression_prec(const expression_t *expression, unsigned top_prec)
671 {
672         unsigned prec = get_expression_precedence(expression->base.kind);
673         if (print_parenthesis && top_prec != PREC_BOTTOM)
674                 top_prec = PREC_TOP;
675         if (top_prec > prec)
676                 fputc('(', out);
677         switch(expression->kind) {
678         case EXPR_UNKNOWN:
679         case EXPR_INVALID:
680                 fprintf(out, "*invalid expression*");
681                 break;
682         case EXPR_CHAR_CONST:
683                 print_char_const(&expression->conste);
684                 break;
685         case EXPR_CONST:
686                 print_const(&expression->conste);
687                 break;
688         case EXPR_FUNCTION:
689         case EXPR_PRETTY_FUNCTION:
690         case EXPR_STRING_LITERAL:
691                 print_string_literal(&expression->string);
692                 break;
693         case EXPR_WIDE_STRING_LITERAL:
694                 print_wide_string_literal(&expression->wide_string);
695                 break;
696         case EXPR_COMPOUND_LITERAL:
697                 print_compound_literal(&expression->compound_literal);
698                 break;
699         case EXPR_CALL:
700                 print_call_expression(&expression->call);
701                 break;
702         EXPR_BINARY_CASES
703                 print_binary_expression(&expression->binary);
704                 break;
705         case EXPR_REFERENCE:
706                 print_reference_expression(&expression->reference);
707                 break;
708         case EXPR_ARRAY_ACCESS:
709                 print_array_expression(&expression->array_access);
710                 break;
711         EXPR_UNARY_CASES
712                 print_unary_expression(&expression->unary);
713                 break;
714         case EXPR_SIZEOF:
715         case EXPR_ALIGNOF:
716                 print_typeprop_expression(&expression->typeprop);
717                 break;
718         case EXPR_BUILTIN_SYMBOL:
719                 print_builtin_symbol(&expression->builtin_symbol);
720                 break;
721         case EXPR_BUILTIN_CONSTANT_P:
722                 print_builtin_constant(&expression->builtin_constant);
723                 break;
724         case EXPR_BUILTIN_PREFETCH:
725                 print_builtin_prefetch(&expression->builtin_prefetch);
726                 break;
727         case EXPR_CONDITIONAL:
728                 print_conditional(&expression->conditional);
729                 break;
730         case EXPR_VA_START:
731                 print_va_start(&expression->va_starte);
732                 break;
733         case EXPR_VA_ARG:
734                 print_va_arg(&expression->va_arge);
735                 break;
736         case EXPR_SELECT:
737                 print_select(&expression->select);
738                 break;
739         case EXPR_CLASSIFY_TYPE:
740                 print_classify_type_expression(&expression->classify_type);
741                 break;
742         case EXPR_OFFSETOF:
743                 print_offsetof_expression(&expression->offsetofe);
744                 break;
745         case EXPR_STATEMENT:
746                 print_statement_expression(&expression->statement);
747                 break;
748
749         default:
750                 /* TODO */
751                 fprintf(out, "some expression of type %d", (int) expression->kind);
752                 break;
753         }
754         if (top_prec > prec)
755                 fputc(')', out);
756 }
757
758 /**
759  * Print an compound statement.
760  *
761  * @param block  the compound statement
762  */
763 static void print_compound_statement(const compound_statement_t *block)
764 {
765         fputs("{\n", out);
766         ++indent;
767
768         statement_t *statement = block->statements;
769         while(statement != NULL) {
770                 if (statement->base.kind == STATEMENT_CASE_LABEL)
771                         --indent;
772                 print_indent();
773                 print_statement(statement);
774
775                 statement = statement->base.next;
776         }
777         --indent;
778         print_indent();
779         fputs("}\n", out);
780 }
781
782 /**
783  * Print a return statement.
784  *
785  * @param statement  the return statement
786  */
787 static void print_return_statement(const return_statement_t *statement)
788 {
789         fprintf(out, "return ");
790         if(statement->value != NULL)
791                 print_expression(statement->value);
792         fputs(";\n", out);
793 }
794
795 /**
796  * Print an expression statement.
797  *
798  * @param statement  the expression statement
799  */
800 static void print_expression_statement(const expression_statement_t *statement)
801 {
802         print_expression(statement->expression);
803         fputs(";\n", out);
804 }
805
806 /**
807  * Print a goto statement.
808  *
809  * @param statement  the goto statement
810  */
811 static void print_goto_statement(const goto_statement_t *statement)
812 {
813         fprintf(out, "goto ");
814         fputs(statement->label->symbol->string, out);
815         fprintf(stderr, "(%p)", (void*) statement->label);
816         fputs(";\n", out);
817 }
818
819 /**
820  * Print a label statement.
821  *
822  * @param statement  the label statement
823  */
824 static void print_label_statement(const label_statement_t *statement)
825 {
826         fprintf(stderr, "(%p)", (void*) statement->label);
827         fprintf(out, "%s:\n", statement->label->symbol->string);
828         if(statement->statement != NULL) {
829                 print_statement(statement->statement);
830         }
831 }
832
833 /**
834  * Print an if statement.
835  *
836  * @param statement  the if statement
837  */
838 static void print_if_statement(const if_statement_t *statement)
839 {
840         fputs("if(", out);
841         print_expression(statement->condition);
842         fputs(") ", out);
843         if(statement->true_statement != NULL) {
844                 print_statement(statement->true_statement);
845         }
846
847         if(statement->false_statement != NULL) {
848                 print_indent();
849                 fputs("else ", out);
850                 print_statement(statement->false_statement);
851         }
852 }
853
854 /**
855  * Print a switch statement.
856  *
857  * @param statement  the switch statement
858  */
859 static void print_switch_statement(const switch_statement_t *statement)
860 {
861         fputs("switch(", out);
862         print_expression(statement->expression);
863         fputs(") ", out);
864         print_statement(statement->body);
865 }
866
867 /**
868  * Print a case label (including the default label).
869  *
870  * @param statement  the case label statement
871  */
872 static void print_case_label(const case_label_statement_t *statement)
873 {
874         if(statement->expression == NULL) {
875                 fputs("default:\n", out);
876         } else {
877                 fputs("case ", out);
878                 print_expression(statement->expression);
879                 if (statement->end_range != NULL) {
880                         fputs(" ... ", out);
881                         print_expression(statement->end_range);
882                 }
883                 fputs(":\n", out);
884         }
885         ++indent;
886         if(statement->statement != NULL) {
887                 if (statement->statement->base.kind == STATEMENT_CASE_LABEL) {
888                         --indent;
889                 }
890                 print_indent();
891                 print_statement(statement->statement);
892         }
893 }
894
895 /**
896  * Print a declaration statement.
897  *
898  * @param statement   the statement
899  */
900 static void print_declaration_statement(
901                 const declaration_statement_t *statement)
902 {
903         int first = 1;
904         declaration_t *declaration = statement->declarations_begin;
905         for( ; declaration != statement->declarations_end->next;
906                declaration = declaration->next) {
907                 if(!first) {
908                         print_indent();
909                 } else {
910                         first = 0;
911                 }
912                 print_declaration(declaration);
913                 fputc('\n', out);
914         }
915 }
916
917 /**
918  * Print a while statement.
919  *
920  * @param statement   the statement
921  */
922 static void print_while_statement(const while_statement_t *statement)
923 {
924         fputs("while(", out);
925         print_expression(statement->condition);
926         fputs(") ", out);
927         print_statement(statement->body);
928 }
929
930 /**
931  * Print a do-while statement.
932  *
933  * @param statement   the statement
934  */
935 static void print_do_while_statement(const do_while_statement_t *statement)
936 {
937         fputs("do ", out);
938         print_statement(statement->body);
939         print_indent();
940         fputs("while(", out);
941         print_expression(statement->condition);
942         fputs(");\n", out);
943 }
944
945 /**
946  * Print a for statement.
947  *
948  * @param statement   the statement
949  */
950 static void print_for_statement(const for_statement_t *statement)
951 {
952         fputs("for(", out);
953         if(statement->scope.declarations != NULL) {
954                 assert(statement->initialisation == NULL);
955                 print_declaration(statement->scope.declarations);
956                 if(statement->scope.declarations->next != NULL) {
957                         panic("multiple declarations in for statement not supported yet");
958                 }
959                 fputc(' ', out);
960         } else {
961                 if(statement->initialisation) {
962                         print_expression(statement->initialisation);
963                 }
964                 fputs("; ", out);
965         }
966         if(statement->condition != NULL) {
967                 print_expression(statement->condition);
968         }
969         fputs("; ", out);
970         if(statement->step != NULL) {
971                 print_expression(statement->step);
972         }
973         fputs(")", out);
974         print_statement(statement->body);
975 }
976
977 /**
978  * Print assembler constraints.
979  *
980  * @param constraints   the constraints
981  */
982 static void print_asm_constraints(asm_constraint_t *constraints)
983 {
984         asm_constraint_t *constraint = constraints;
985         for( ; constraint != NULL; constraint = constraint->next) {
986                 if(constraint != constraints)
987                         fputs(", ", out);
988
989                 if(constraint->symbol) {
990                         fprintf(out, "[%s] ", constraint->symbol->string);
991                 }
992                 print_quoted_string(&constraint->constraints, '"');
993                 fputs(" (", out);
994                 print_expression(constraint->expression);
995                 fputs(")", out);
996         }
997 }
998
999 /**
1000  * Print assembler clobbers.
1001  *
1002  * @param clobbers   the clobbers
1003  */
1004 static void print_asm_clobbers(asm_clobber_t *clobbers)
1005 {
1006         asm_clobber_t *clobber = clobbers;
1007         for( ; clobber != NULL; clobber = clobber->next) {
1008                 if(clobber != clobbers)
1009                         fputs(", ", out);
1010
1011                 print_quoted_string(&clobber->clobber, '"');
1012         }
1013 }
1014
1015 /**
1016  * Print an assembler statement.
1017  *
1018  * @param statement   the statement
1019  */
1020 static void print_asm_statement(const asm_statement_t *statement)
1021 {
1022         fputs("asm ", out);
1023         if(statement->is_volatile) {
1024                 fputs("volatile ", out);
1025         }
1026         fputs("(", out);
1027         print_quoted_string(&statement->asm_text, '"');
1028         if(statement->inputs == NULL && statement->outputs == NULL
1029                         && statement->clobbers == NULL)
1030                 goto end_of_print_asm_statement;
1031
1032         fputs(" : ", out);
1033         print_asm_constraints(statement->inputs);
1034         if(statement->outputs == NULL && statement->clobbers == NULL)
1035                 goto end_of_print_asm_statement;
1036
1037         fputs(" : ", out);
1038         print_asm_constraints(statement->outputs);
1039         if(statement->clobbers == NULL)
1040                 goto end_of_print_asm_statement;
1041
1042         fputs(" : ", out);
1043         print_asm_clobbers(statement->clobbers);
1044
1045 end_of_print_asm_statement:
1046         fputs(");\n", out);
1047 }
1048
1049 /**
1050  * Print a statement.
1051  *
1052  * @param statement   the statement
1053  */
1054 void print_statement(const statement_t *statement)
1055 {
1056         switch(statement->kind) {
1057         case STATEMENT_COMPOUND:
1058                 print_compound_statement(&statement->compound);
1059                 break;
1060         case STATEMENT_RETURN:
1061                 print_return_statement(&statement->returns);
1062                 break;
1063         case STATEMENT_EXPRESSION:
1064                 print_expression_statement(&statement->expression);
1065                 break;
1066         case STATEMENT_LABEL:
1067                 print_label_statement(&statement->label);
1068                 break;
1069         case STATEMENT_GOTO:
1070                 print_goto_statement(&statement->gotos);
1071                 break;
1072         case STATEMENT_CONTINUE:
1073                 fputs("continue;\n", out);
1074                 break;
1075         case STATEMENT_BREAK:
1076                 fputs("break;\n", out);
1077                 break;
1078         case STATEMENT_IF:
1079                 print_if_statement(&statement->ifs);
1080                 break;
1081         case STATEMENT_SWITCH:
1082                 print_switch_statement(&statement->switchs);
1083                 break;
1084         case STATEMENT_CASE_LABEL:
1085                 print_case_label(&statement->case_label);
1086                 break;
1087         case STATEMENT_DECLARATION:
1088                 print_declaration_statement(&statement->declaration);
1089                 break;
1090         case STATEMENT_WHILE:
1091                 print_while_statement(&statement->whiles);
1092                 break;
1093         case STATEMENT_DO_WHILE:
1094                 print_do_while_statement(&statement->do_while);
1095                 break;
1096         case STATEMENT_FOR:
1097                 print_for_statement(&statement->fors);
1098                 break;
1099         case STATEMENT_ASM:
1100                 print_asm_statement(&statement->asms);
1101                 break;
1102         case STATEMENT_INVALID:
1103                 fprintf(out, "*invalid statement*");
1104                 break;
1105         }
1106 }
1107
1108 /**
1109  * Print a storage class.
1110  *
1111  * @param storage_class   the storage class
1112  */
1113 static void print_storage_class(storage_class_tag_t storage_class)
1114 {
1115         switch(storage_class) {
1116         case STORAGE_CLASS_ENUM_ENTRY:
1117         case STORAGE_CLASS_NONE:
1118                 break;
1119         case STORAGE_CLASS_TYPEDEF:       fputs("typedef ",        out); break;
1120         case STORAGE_CLASS_EXTERN:        fputs("extern ",         out); break;
1121         case STORAGE_CLASS_STATIC:        fputs("static ",         out); break;
1122         case STORAGE_CLASS_AUTO:          fputs("auto ",           out); break;
1123         case STORAGE_CLASS_REGISTER:      fputs("register ",       out); break;
1124         case STORAGE_CLASS_THREAD:        fputs("__thread",        out); break;
1125         case STORAGE_CLASS_THREAD_EXTERN: fputs("extern __thread", out); break;
1126         case STORAGE_CLASS_THREAD_STATIC: fputs("static __thread", out); break;
1127         }
1128 }
1129
1130 /**
1131  * Print an initializer.
1132  *
1133  * @param initializer  the initializer
1134  */
1135 void print_initializer(const initializer_t *initializer)
1136 {
1137         if(initializer == NULL) {
1138                 fputs("{ NIL-INITIALIZER }", out);
1139                 return;
1140         }
1141
1142         switch(initializer->kind) {
1143         case INITIALIZER_VALUE: {
1144                 const initializer_value_t *value = &initializer->value;
1145                 print_expression(value->value);
1146                 return;
1147         }
1148         case INITIALIZER_LIST: {
1149                 assert(initializer->kind == INITIALIZER_LIST);
1150                 fputs("{ ", out);
1151                 const initializer_list_t *list = &initializer->list;
1152
1153                 for(size_t i = 0 ; i < list->len; ++i) {
1154                         const initializer_t *sub_init = list->initializers[i];
1155                         print_initializer(list->initializers[i]);
1156                         if(i < list->len-1 && sub_init->kind != INITIALIZER_DESIGNATOR) {
1157                                 fputs(", ", out);
1158                         }
1159                 }
1160                 fputs(" }", out);
1161                 return;
1162         }
1163         case INITIALIZER_STRING:
1164                 print_quoted_string(&initializer->string.string, '"');
1165                 return;
1166         case INITIALIZER_WIDE_STRING:
1167                 print_quoted_wide_string(&initializer->wide_string.string);
1168                 return;
1169         case INITIALIZER_DESIGNATOR:
1170                 print_designator(initializer->designator.designator);
1171                 fputs(" = ", out);
1172                 return;
1173         }
1174
1175         panic("invalid initializer kind found");
1176 }
1177
1178 /**
1179  * Print a declaration in the NORMAL namespace.
1180  *
1181  * @param declaration  the declaration
1182  */
1183 static void print_normal_declaration(const declaration_t *declaration)
1184 {
1185         print_storage_class((storage_class_tag_t) declaration->declared_storage_class);
1186         if(declaration->is_inline) {
1187                 if (declaration->modifiers & DM_FORCEINLINE)
1188                         fputs("__forceinline ", out);
1189                 else
1190                         fputs("inline ", out);
1191         }
1192         print_type_ext(declaration->type, declaration->symbol,
1193                        &declaration->scope);
1194
1195         if(declaration->type->kind == TYPE_FUNCTION) {
1196                 if(declaration->init.statement != NULL) {
1197                         fputs("\n", out);
1198                         print_statement(declaration->init.statement);
1199                         return;
1200                 }
1201         } else if(declaration->init.initializer != NULL) {
1202                 fputs(" = ", out);
1203                 print_initializer(declaration->init.initializer);
1204         }
1205         fputc(';', out);
1206 }
1207
1208 /**
1209  * Prints an expression.
1210  *
1211  * @param expression  the expression
1212  */
1213 void print_expression(const expression_t *expression) {
1214         print_expression_prec(expression, PREC_BOTTOM);
1215 }
1216
1217 /**
1218  * Print a declaration.
1219  *
1220  * @param declaration  the declaration
1221  */
1222 void print_declaration(const declaration_t *declaration)
1223 {
1224         if(declaration->namespc != NAMESPACE_NORMAL &&
1225                         declaration->symbol == NULL)
1226                 return;
1227
1228         switch(declaration->namespc) {
1229         case NAMESPACE_NORMAL:
1230                 print_normal_declaration(declaration);
1231                 break;
1232         case NAMESPACE_STRUCT:
1233                 fputs("struct ", out);
1234                 fputs(declaration->symbol->string, out);
1235                 fputc(' ', out);
1236                 print_compound_definition(declaration);
1237                 fputc(';', out);
1238                 break;
1239         case NAMESPACE_UNION:
1240                 fputs("union ", out);
1241                 fputs(declaration->symbol->string, out);
1242                 fputc(' ', out);
1243                 print_compound_definition(declaration);
1244                 fputc(';', out);
1245                 break;
1246         case NAMESPACE_ENUM:
1247                 fputs("enum ", out);
1248                 fputs(declaration->symbol->string, out);
1249                 fputc(' ', out);
1250                 print_enum_definition(declaration);
1251                 fputc(';', out);
1252                 break;
1253         }
1254 }
1255
1256 /**
1257  * Print the AST of a translation unit.
1258  *
1259  * @param unit   the translation unit
1260  */
1261 void print_ast(const translation_unit_t *unit)
1262 {
1263         inc_type_visited();
1264
1265         declaration_t *declaration = unit->scope.declarations;
1266         for( ; declaration != NULL; declaration = declaration->next) {
1267                 if(declaration->storage_class == STORAGE_CLASS_ENUM_ENTRY)
1268                         continue;
1269                 if(declaration->namespc != NAMESPACE_NORMAL &&
1270                                 declaration->symbol == NULL)
1271                         continue;
1272
1273                 print_indent();
1274                 print_declaration(declaration);
1275                 fputc('\n', out);
1276         }
1277 }
1278
1279 bool is_constant_initializer(const initializer_t *initializer)
1280 {
1281         switch(initializer->kind) {
1282         case INITIALIZER_STRING:
1283         case INITIALIZER_WIDE_STRING:
1284         case INITIALIZER_DESIGNATOR:
1285                 return true;
1286
1287         case INITIALIZER_VALUE:
1288                 return is_constant_expression(initializer->value.value);
1289
1290         case INITIALIZER_LIST: {
1291                 for(size_t i = 0; i < initializer->list.len; ++i) {
1292                         initializer_t *sub_initializer = initializer->list.initializers[i];
1293                         if(!is_constant_initializer(sub_initializer))
1294                                 return false;
1295                 }
1296                 return true;
1297         }
1298         }
1299         panic("invalid initializer kind found");
1300 }
1301
1302 static bool is_object_with_constant_address(const expression_t *expression)
1303 {
1304         switch(expression->kind) {
1305         case EXPR_UNARY_DEREFERENCE:
1306                 return is_address_constant(expression->unary.value);
1307
1308         case EXPR_SELECT: {
1309                 if(is_type_pointer(expression->select.compound->base.type)) {
1310                         /* it's a -> */
1311                         return is_address_constant(expression->select.compound);
1312                 } else {
1313                         return is_object_with_constant_address(expression->select.compound);
1314                 }
1315         }
1316
1317         case EXPR_ARRAY_ACCESS:
1318                 return is_constant_expression(expression->array_access.index)
1319                         && is_address_constant(expression->array_access.array_ref);
1320
1321         case EXPR_REFERENCE: {
1322                 declaration_t *declaration = expression->reference.declaration;
1323                 switch((storage_class_tag_t) declaration->storage_class) {
1324                 case STORAGE_CLASS_NONE:
1325                 case STORAGE_CLASS_EXTERN:
1326                 case STORAGE_CLASS_STATIC:
1327                         return true;
1328                 default:
1329                         return false;
1330                 }
1331         }
1332
1333         default:
1334                 return false;
1335         }
1336 }
1337
1338 bool is_address_constant(const expression_t *expression)
1339 {
1340         switch(expression->kind) {
1341         case EXPR_UNARY_TAKE_ADDRESS:
1342                 return is_object_with_constant_address(expression->unary.value);
1343
1344         case EXPR_UNARY_CAST:
1345                 return is_type_pointer(skip_typeref(expression->base.type))
1346                         && (is_constant_expression(expression->unary.value)
1347                         || is_address_constant(expression->unary.value));
1348
1349         case EXPR_BINARY_ADD:
1350         case EXPR_BINARY_SUB: {
1351                 expression_t *left  = expression->binary.left;
1352                 expression_t *right = expression->binary.right;
1353
1354                 if(is_type_pointer(skip_typeref(left->base.type))) {
1355                         return is_address_constant(left) && is_constant_expression(right);
1356                 } else if(is_type_pointer(skip_typeref(right->base.type))) {
1357                         return is_constant_expression(left)     && is_address_constant(right);
1358                 }
1359
1360                 return false;
1361         }
1362
1363         case EXPR_REFERENCE: {
1364                 declaration_t *declaration = expression->reference.declaration;
1365                 type_t *type = skip_typeref(declaration->type);
1366                 if(is_type_function(type))
1367                         return true;
1368                 if(is_type_array(type)) {
1369                         return is_object_with_constant_address(expression);
1370                 }
1371                 return false;
1372         }
1373
1374         default:
1375                 return false;
1376         }
1377 }
1378
1379 bool is_constant_expression(const expression_t *expression)
1380 {
1381         switch(expression->kind) {
1382
1383         case EXPR_CONST:
1384         case EXPR_CHAR_CONST:
1385         case EXPR_STRING_LITERAL:
1386         case EXPR_WIDE_STRING_LITERAL:
1387         case EXPR_SIZEOF:
1388         case EXPR_CLASSIFY_TYPE:
1389         case EXPR_FUNCTION:
1390         case EXPR_PRETTY_FUNCTION:
1391         case EXPR_OFFSETOF:
1392         case EXPR_ALIGNOF:
1393         case EXPR_BUILTIN_CONSTANT_P:
1394                 return true;
1395
1396         case EXPR_BUILTIN_SYMBOL:
1397         case EXPR_BUILTIN_PREFETCH:
1398         case EXPR_CALL:
1399         case EXPR_SELECT:
1400         case EXPR_VA_START:
1401         case EXPR_VA_ARG:
1402         case EXPR_STATEMENT:
1403         case EXPR_UNARY_POSTFIX_INCREMENT:
1404         case EXPR_UNARY_POSTFIX_DECREMENT:
1405         case EXPR_UNARY_PREFIX_INCREMENT:
1406         case EXPR_UNARY_PREFIX_DECREMENT:
1407         case EXPR_UNARY_BITFIELD_EXTRACT:
1408         case EXPR_UNARY_ASSUME: /* has VOID type */
1409         case EXPR_UNARY_DEREFERENCE:
1410         case EXPR_UNARY_TAKE_ADDRESS:
1411         case EXPR_BINARY_ASSIGN:
1412         case EXPR_BINARY_MUL_ASSIGN:
1413         case EXPR_BINARY_DIV_ASSIGN:
1414         case EXPR_BINARY_MOD_ASSIGN:
1415         case EXPR_BINARY_ADD_ASSIGN:
1416         case EXPR_BINARY_SUB_ASSIGN:
1417         case EXPR_BINARY_SHIFTLEFT_ASSIGN:
1418         case EXPR_BINARY_SHIFTRIGHT_ASSIGN:
1419         case EXPR_BINARY_BITWISE_AND_ASSIGN:
1420         case EXPR_BINARY_BITWISE_XOR_ASSIGN:
1421         case EXPR_BINARY_BITWISE_OR_ASSIGN:
1422         case EXPR_BINARY_COMMA:
1423                 return false;
1424
1425         case EXPR_UNARY_NEGATE:
1426         case EXPR_UNARY_PLUS:
1427         case EXPR_UNARY_BITWISE_NEGATE:
1428         case EXPR_UNARY_NOT:
1429                 return is_constant_expression(expression->unary.value);
1430
1431         case EXPR_UNARY_CAST:
1432         case EXPR_UNARY_CAST_IMPLICIT:
1433                 return is_type_arithmetic(skip_typeref(expression->base.type))
1434                         && is_constant_expression(expression->unary.value);
1435
1436         case EXPR_BINARY_ADD:
1437         case EXPR_BINARY_SUB:
1438         case EXPR_BINARY_MUL:
1439         case EXPR_BINARY_DIV:
1440         case EXPR_BINARY_MOD:
1441         case EXPR_BINARY_EQUAL:
1442         case EXPR_BINARY_NOTEQUAL:
1443         case EXPR_BINARY_LESS:
1444         case EXPR_BINARY_LESSEQUAL:
1445         case EXPR_BINARY_GREATER:
1446         case EXPR_BINARY_GREATEREQUAL:
1447         case EXPR_BINARY_BITWISE_AND:
1448         case EXPR_BINARY_BITWISE_OR:
1449         case EXPR_BINARY_BITWISE_XOR:
1450         case EXPR_BINARY_LOGICAL_AND:
1451         case EXPR_BINARY_LOGICAL_OR:
1452         case EXPR_BINARY_SHIFTLEFT:
1453         case EXPR_BINARY_SHIFTRIGHT:
1454         case EXPR_BINARY_BUILTIN_EXPECT:
1455         case EXPR_BINARY_ISGREATER:
1456         case EXPR_BINARY_ISGREATEREQUAL:
1457         case EXPR_BINARY_ISLESS:
1458         case EXPR_BINARY_ISLESSEQUAL:
1459         case EXPR_BINARY_ISLESSGREATER:
1460         case EXPR_BINARY_ISUNORDERED:
1461                 return is_constant_expression(expression->binary.left)
1462                         && is_constant_expression(expression->binary.right);
1463
1464         case EXPR_COMPOUND_LITERAL:
1465                 return is_constant_initializer(expression->compound_literal.initializer);
1466
1467         case EXPR_CONDITIONAL: {
1468                 expression_t *condition = expression->conditional.condition;
1469                 if(!is_constant_expression(condition))
1470                         return false;
1471
1472                 long val = fold_constant(condition);
1473                 if(val != 0)
1474                         return is_constant_expression(expression->conditional.true_expression);
1475                 else
1476                         return is_constant_expression(expression->conditional.false_expression);
1477         }
1478
1479         case EXPR_ARRAY_ACCESS:
1480                 return is_constant_expression(expression->array_access.array_ref)
1481                         && is_constant_expression(expression->array_access.index);
1482
1483         case EXPR_REFERENCE: {
1484                 declaration_t *declaration = expression->reference.declaration;
1485                 if(declaration->storage_class == STORAGE_CLASS_ENUM_ENTRY)
1486                         return true;
1487
1488                 return false;
1489         }
1490
1491         case EXPR_UNKNOWN:
1492         case EXPR_INVALID:
1493                 break;
1494         }
1495         panic("invalid expression found (is constant expression)");
1496 }
1497
1498 /**
1499  * Initialize the AST construction.
1500  */
1501 void init_ast(void)
1502 {
1503         obstack_init(&ast_obstack);
1504 }
1505
1506 /**
1507  * Free the AST.
1508  */
1509 void exit_ast(void)
1510 {
1511         obstack_free(&ast_obstack, NULL);
1512 }
1513
1514 /**
1515  * Set the output stream for the AST printer.
1516  *
1517  * @param stream  the output stream
1518  */
1519 void ast_set_output(FILE *stream)
1520 {
1521         out = stream;
1522         type_set_output(stream);
1523 }
1524
1525 /**
1526  * Allocate an AST object of the given size.
1527  *
1528  * @param size  the size of the object to allocate
1529  *
1530  * @return  A new allocated object in the AST memeory space.
1531  */
1532 void *(allocate_ast)(size_t size)
1533 {
1534         return _allocate_ast(size);
1535 }