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