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