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