Do not show implicit declarations in --print-ast.
[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         bool first = true;
961         for (declaration_t *declaration = statement->declarations_begin;
962              declaration != statement->declarations_end->next;
963              declaration = declaration->next) {
964                 if (declaration->storage_class == STORAGE_CLASS_ENUM_ENTRY)
965                         continue;
966                 if (declaration->implicit)
967                         continue;
968
969                 if (!first) {
970                         print_indent();
971                 } else {
972                         first = false;
973                 }
974                 print_declaration(declaration);
975                 fputc('\n', out);
976         }
977 }
978
979 /**
980  * Print a while statement.
981  *
982  * @param statement   the statement
983  */
984 static void print_while_statement(const while_statement_t *statement)
985 {
986         fputs("while (", out);
987         print_expression(statement->condition);
988         fputs(") ", out);
989         print_statement(statement->body);
990 }
991
992 /**
993  * Print a do-while statement.
994  *
995  * @param statement   the statement
996  */
997 static void print_do_while_statement(const do_while_statement_t *statement)
998 {
999         fputs("do ", out);
1000         print_statement(statement->body);
1001         print_indent();
1002         fputs("while (", out);
1003         print_expression(statement->condition);
1004         fputs(");\n", out);
1005 }
1006
1007 /**
1008  * Print a for statement.
1009  *
1010  * @param statement   the statement
1011  */
1012 static void print_for_statement(const for_statement_t *statement)
1013 {
1014         fputs("for (", out);
1015         declaration_t *decl = statement->scope.declarations;
1016         while (decl != NULL && decl->implicit)
1017                 decl = decl->next;
1018         if (decl != NULL) {
1019                 assert(statement->initialisation == NULL);
1020                 print_declaration(decl);
1021                 if (decl->next != NULL) {
1022                         panic("multiple declarations in for statement not supported yet");
1023                 }
1024                 fputc(' ', out);
1025         } else {
1026                 if(statement->initialisation) {
1027                         print_expression(statement->initialisation);
1028                 }
1029                 fputs("; ", out);
1030         }
1031         if(statement->condition != NULL) {
1032                 print_expression(statement->condition);
1033         }
1034         fputs("; ", out);
1035         if(statement->step != NULL) {
1036                 print_expression(statement->step);
1037         }
1038         fputs(")", out);
1039         print_statement(statement->body);
1040 }
1041
1042 /**
1043  * Print assembler arguments.
1044  *
1045  * @param arguments   the arguments
1046  */
1047 static void print_asm_arguments(asm_argument_t *arguments)
1048 {
1049         asm_argument_t *argument = arguments;
1050         for( ; argument != NULL; argument = argument->next) {
1051                 if(argument != arguments)
1052                         fputs(", ", out);
1053
1054                 if(argument->symbol) {
1055                         fprintf(out, "[%s] ", argument->symbol->string);
1056                 }
1057                 print_quoted_string(&argument->constraints, '"', 1);
1058                 fputs(" (", out);
1059                 print_expression(argument->expression);
1060                 fputs(")", out);
1061         }
1062 }
1063
1064 /**
1065  * Print assembler clobbers.
1066  *
1067  * @param clobbers   the clobbers
1068  */
1069 static void print_asm_clobbers(asm_clobber_t *clobbers)
1070 {
1071         asm_clobber_t *clobber = clobbers;
1072         for( ; clobber != NULL; clobber = clobber->next) {
1073                 if(clobber != clobbers)
1074                         fputs(", ", out);
1075
1076                 print_quoted_string(&clobber->clobber, '"', 1);
1077         }
1078 }
1079
1080 /**
1081  * Print an assembler statement.
1082  *
1083  * @param statement   the statement
1084  */
1085 static void print_asm_statement(const asm_statement_t *statement)
1086 {
1087         fputs("asm ", out);
1088         if(statement->is_volatile) {
1089                 fputs("volatile ", out);
1090         }
1091         fputs("(", out);
1092         print_quoted_string(&statement->asm_text, '"', 1);
1093         if(statement->inputs == NULL && statement->outputs == NULL
1094                         && statement->clobbers == NULL)
1095                 goto end_of_print_asm_statement;
1096
1097         fputs(" : ", out);
1098         print_asm_arguments(statement->inputs);
1099         if(statement->outputs == NULL && statement->clobbers == NULL)
1100                 goto end_of_print_asm_statement;
1101
1102         fputs(" : ", out);
1103         print_asm_arguments(statement->outputs);
1104         if(statement->clobbers == NULL)
1105                 goto end_of_print_asm_statement;
1106
1107         fputs(" : ", out);
1108         print_asm_clobbers(statement->clobbers);
1109
1110 end_of_print_asm_statement:
1111         fputs(");\n", out);
1112 }
1113
1114 /**
1115  * Print a microsoft __try statement.
1116  *
1117  * @param statement   the statement
1118  */
1119 static void print_ms_try_statement(const ms_try_statement_t *statement)
1120 {
1121         fputs("__try ", out);
1122         print_statement(statement->try_statement);
1123         print_indent();
1124         if(statement->except_expression != NULL) {
1125                 fputs("__except(", out);
1126                 print_expression(statement->except_expression);
1127                 fputs(") ", out);
1128         } else {
1129                 fputs("__finally ", out);
1130         }
1131         print_statement(statement->final_statement);
1132 }
1133
1134 /**
1135  * Print a microsoft __leave statement.
1136  *
1137  * @param statement   the statement
1138  */
1139 static void print_leave_statement(const leave_statement_t *statement)
1140 {
1141         (void) statement;
1142         fputs("__leave;\n", out);
1143 }
1144
1145 /**
1146  * Print a statement.
1147  *
1148  * @param statement   the statement
1149  */
1150 void print_statement(const statement_t *statement)
1151 {
1152         switch(statement->kind) {
1153         case STATEMENT_EMPTY:
1154                 fputs(";\n", out);
1155                 break;
1156         case STATEMENT_COMPOUND:
1157                 print_compound_statement(&statement->compound);
1158                 break;
1159         case STATEMENT_RETURN:
1160                 print_return_statement(&statement->returns);
1161                 break;
1162         case STATEMENT_EXPRESSION:
1163                 print_expression_statement(&statement->expression);
1164                 break;
1165         case STATEMENT_LABEL:
1166                 print_label_statement(&statement->label);
1167                 break;
1168         case STATEMENT_GOTO:
1169                 print_goto_statement(&statement->gotos);
1170                 break;
1171         case STATEMENT_CONTINUE:
1172                 fputs("continue;\n", out);
1173                 break;
1174         case STATEMENT_BREAK:
1175                 fputs("break;\n", out);
1176                 break;
1177         case STATEMENT_IF:
1178                 print_if_statement(&statement->ifs);
1179                 break;
1180         case STATEMENT_SWITCH:
1181                 print_switch_statement(&statement->switchs);
1182                 break;
1183         case STATEMENT_CASE_LABEL:
1184                 print_case_label(&statement->case_label);
1185                 break;
1186         case STATEMENT_DECLARATION:
1187                 print_declaration_statement(&statement->declaration);
1188                 break;
1189         case STATEMENT_WHILE:
1190                 print_while_statement(&statement->whiles);
1191                 break;
1192         case STATEMENT_DO_WHILE:
1193                 print_do_while_statement(&statement->do_while);
1194                 break;
1195         case STATEMENT_FOR:
1196                 print_for_statement(&statement->fors);
1197                 break;
1198         case STATEMENT_ASM:
1199                 print_asm_statement(&statement->asms);
1200                 break;
1201         case STATEMENT_MS_TRY:
1202                 print_ms_try_statement(&statement->ms_try);
1203                 break;
1204         case STATEMENT_LEAVE:
1205                 print_leave_statement(&statement->leave);
1206                 break;
1207         case STATEMENT_INVALID:
1208                 fprintf(out, "$invalid statement$");
1209                 break;
1210         }
1211 }
1212
1213 /**
1214  * Print a storage class.
1215  *
1216  * @param storage_class   the storage class
1217  */
1218 static void print_storage_class(storage_class_tag_t storage_class)
1219 {
1220         switch(storage_class) {
1221         case STORAGE_CLASS_ENUM_ENTRY:
1222         case STORAGE_CLASS_NONE:
1223                 break;
1224         case STORAGE_CLASS_TYPEDEF:       fputs("typedef ",        out); break;
1225         case STORAGE_CLASS_EXTERN:        fputs("extern ",         out); break;
1226         case STORAGE_CLASS_STATIC:        fputs("static ",         out); break;
1227         case STORAGE_CLASS_AUTO:          fputs("auto ",           out); break;
1228         case STORAGE_CLASS_REGISTER:      fputs("register ",       out); break;
1229         case STORAGE_CLASS_THREAD:        fputs("__thread",        out); break;
1230         case STORAGE_CLASS_THREAD_EXTERN: fputs("extern __thread", out); break;
1231         case STORAGE_CLASS_THREAD_STATIC: fputs("static __thread", out); break;
1232         }
1233 }
1234
1235 /**
1236  * Print an initializer.
1237  *
1238  * @param initializer  the initializer
1239  */
1240 void print_initializer(const initializer_t *initializer)
1241 {
1242         if(initializer == NULL) {
1243                 fputs("{}", out);
1244                 return;
1245         }
1246
1247         switch(initializer->kind) {
1248         case INITIALIZER_VALUE: {
1249                 const initializer_value_t *value = &initializer->value;
1250                 print_expression(value->value);
1251                 return;
1252         }
1253         case INITIALIZER_LIST: {
1254                 assert(initializer->kind == INITIALIZER_LIST);
1255                 fputs("{ ", out);
1256                 const initializer_list_t *list = &initializer->list;
1257
1258                 for(size_t i = 0 ; i < list->len; ++i) {
1259                         const initializer_t *sub_init = list->initializers[i];
1260                         print_initializer(list->initializers[i]);
1261                         if(i < list->len-1) {
1262                                 if(sub_init == NULL || sub_init->kind != INITIALIZER_DESIGNATOR)
1263                                         fputs(", ", out);
1264                         }
1265                 }
1266                 fputs(" }", out);
1267                 return;
1268         }
1269         case INITIALIZER_STRING:
1270                 print_quoted_string(&initializer->string.string, '"', 1);
1271                 return;
1272         case INITIALIZER_WIDE_STRING:
1273                 print_quoted_wide_string(&initializer->wide_string.string, '"', 1);
1274                 return;
1275         case INITIALIZER_DESIGNATOR:
1276                 print_designator(initializer->designator.designator);
1277                 fputs(" = ", out);
1278                 return;
1279         }
1280
1281         panic("invalid initializer kind found");
1282 }
1283
1284 /**
1285  * Print microsoft extended declaration modifiers.
1286  */
1287 static void print_ms_modifiers(const declaration_t *declaration) {
1288         if((c_mode & _MS) == 0)
1289                 return;
1290
1291         decl_modifiers_t modifiers = declaration->modifiers;
1292
1293         /* DM_FORCEINLINE handled outside. */
1294         if ((modifiers & ~DM_FORCEINLINE) != 0    ||
1295             declaration->alignment        != 0    ||
1296             declaration->get_property_sym != NULL ||
1297             declaration->put_property_sym != NULL) {
1298                 char *next = "(";
1299
1300                 fputs("__declspec", out);
1301                 if(modifiers & DM_DLLIMPORT) {
1302                         fputs(next, out); next = ", "; fputs("dllimport", out);
1303                 }
1304                 if(modifiers & DM_DLLEXPORT) {
1305                         fputs(next, out); next = ", "; fputs("dllexport", out);
1306                 }
1307                 if(modifiers & DM_THREAD) {
1308                         fputs(next, out); next = ", "; fputs("thread", out);
1309                 }
1310                 if(modifiers & DM_NAKED) {
1311                         fputs(next, out); next = ", "; fputs("naked", out);
1312                 }
1313                 if(modifiers & DM_THREAD) {
1314                         fputs(next, out); next = ", "; fputs("thread", out);
1315                 }
1316                 if(modifiers & DM_SELECTANY) {
1317                         fputs(next, out); next = ", "; fputs("selectany", out);
1318                 }
1319                 if(modifiers & DM_NOTHROW) {
1320                         fputs(next, out); next = ", "; fputs("nothrow", out);
1321                 }
1322                 if(modifiers & DM_NORETURN) {
1323                         fputs(next, out); next = ", "; fputs("noreturn", out);
1324                 }
1325                 if(modifiers & DM_NOINLINE) {
1326                         fputs(next, out); next = ", "; fputs("noinline", out);
1327                 }
1328                 if (modifiers & DM_DEPRECATED) {
1329                         fputs(next, out); next = ", "; fputs("deprecated", out);
1330                         if(declaration->deprecated_string != NULL)
1331                                 fprintf(out, "(\"%s\")", declaration->deprecated_string);
1332                 }
1333                 if(declaration->alignment != 0) {
1334                         fputs(next, out); next = ", "; fprintf(out, "align(%u)", declaration->alignment);
1335                 }
1336                 if(modifiers & DM_RESTRICT) {
1337                         fputs(next, out); next = ", "; fputs("restrict", out);
1338                 }
1339                 if(modifiers & DM_NOALIAS) {
1340                         fputs(next, out); next = ", "; fputs("noalias", out);
1341                 }
1342             if(declaration->get_property_sym != NULL || declaration->put_property_sym != NULL) {
1343                 char *comma = "";
1344                         fputs(next, out); next = ", "; fprintf(out, "property(");
1345                 if(declaration->get_property_sym != NULL) {
1346                         fprintf(out, "get=%s", declaration->get_property_sym->string);
1347                         comma = ", ";
1348                         }
1349                 if(declaration->put_property_sym != NULL)
1350                         fprintf(out, "%sput=%s", comma, declaration->put_property_sym->string);
1351                         fputc(')', out);
1352                 }
1353                 fputs(") ", out);
1354         }
1355 }
1356
1357 /**
1358  * Print a declaration in the NORMAL namespace.
1359  *
1360  * @param declaration  the declaration
1361  */
1362 static void print_normal_declaration(const declaration_t *declaration)
1363 {
1364         print_storage_class((storage_class_tag_t) declaration->declared_storage_class);
1365         if (declaration->is_inline) {
1366                 if (declaration->modifiers & DM_FORCEINLINE) {
1367                         fputs("__forceinline ", out);
1368                 } else if (declaration->modifiers & DM_MICROSOFT_INLINE) {
1369                         fputs("__inline ", out);
1370                 } else {
1371                         fputs("inline ", out);
1372                 }
1373         }
1374         print_ms_modifiers(declaration);
1375         print_type_ext(declaration->type, declaration->symbol,
1376                        &declaration->scope);
1377
1378         if(declaration->type->kind == TYPE_FUNCTION) {
1379                 if(declaration->init.statement != NULL) {
1380                         fputs("\n", out);
1381                         print_statement(declaration->init.statement);
1382                         return;
1383                 }
1384         } else if(declaration->init.initializer != NULL) {
1385                 fputs(" = ", out);
1386                 print_initializer(declaration->init.initializer);
1387         }
1388         fputc(';', out);
1389 }
1390
1391 /**
1392  * Prints an expression.
1393  *
1394  * @param expression  the expression
1395  */
1396 void print_expression(const expression_t *expression) {
1397         print_expression_prec(expression, PREC_BOTTOM);
1398 }
1399
1400 /**
1401  * Print a declaration.
1402  *
1403  * @param declaration  the declaration
1404  */
1405 void print_declaration(const declaration_t *declaration)
1406 {
1407         if(declaration->namespc != NAMESPACE_NORMAL &&
1408                         declaration->symbol == NULL)
1409                 return;
1410
1411         switch(declaration->namespc) {
1412         case NAMESPACE_NORMAL:
1413                 print_normal_declaration(declaration);
1414                 break;
1415         case NAMESPACE_STRUCT:
1416                 fputs("struct ", out);
1417                 fputs(declaration->symbol->string, out);
1418                 fputc(' ', out);
1419                 print_compound_definition(declaration);
1420                 fputc(';', out);
1421                 break;
1422         case NAMESPACE_UNION:
1423                 fputs("union ", out);
1424                 fputs(declaration->symbol->string, out);
1425                 fputc(' ', out);
1426                 print_compound_definition(declaration);
1427                 fputc(';', out);
1428                 break;
1429         case NAMESPACE_ENUM:
1430                 fputs("enum ", out);
1431                 fputs(declaration->symbol->string, out);
1432                 fputc(' ', out);
1433                 print_enum_definition(declaration);
1434                 fputc(';', out);
1435                 break;
1436         }
1437 }
1438
1439 /**
1440  * Print the AST of a translation unit.
1441  *
1442  * @param unit   the translation unit
1443  */
1444 void print_ast(const translation_unit_t *unit)
1445 {
1446         inc_type_visited();
1447
1448         declaration_t *declaration = unit->scope.declarations;
1449         for( ; declaration != NULL; declaration = declaration->next) {
1450                 if(declaration->storage_class == STORAGE_CLASS_ENUM_ENTRY)
1451                         continue;
1452                 if(declaration->namespc != NAMESPACE_NORMAL &&
1453                                 declaration->symbol == NULL)
1454                         continue;
1455                 if (declaration->implicit)
1456                         continue;
1457
1458                 print_indent();
1459                 print_declaration(declaration);
1460                 fputc('\n', out);
1461         }
1462 }
1463
1464 bool is_constant_initializer(const initializer_t *initializer)
1465 {
1466         switch(initializer->kind) {
1467         case INITIALIZER_STRING:
1468         case INITIALIZER_WIDE_STRING:
1469         case INITIALIZER_DESIGNATOR:
1470                 return true;
1471
1472         case INITIALIZER_VALUE:
1473                 return is_constant_expression(initializer->value.value);
1474
1475         case INITIALIZER_LIST:
1476                 for(size_t i = 0; i < initializer->list.len; ++i) {
1477                         initializer_t *sub_initializer = initializer->list.initializers[i];
1478                         if(!is_constant_initializer(sub_initializer))
1479                                 return false;
1480                 }
1481                 return true;
1482         }
1483         panic("invalid initializer kind found");
1484 }
1485
1486 static bool is_object_with_linker_constant_address(const expression_t *expression)
1487 {
1488         switch(expression->kind) {
1489         case EXPR_UNARY_DEREFERENCE:
1490                 return is_address_constant(expression->unary.value);
1491
1492         case EXPR_SELECT: {
1493                 type_t *base_type = skip_typeref(expression->select.compound->base.type);
1494                 if(is_type_pointer(base_type)) {
1495                         /* it's a -> */
1496                         return is_address_constant(expression->select.compound);
1497                 } else {
1498                         return is_object_with_linker_constant_address(expression->select.compound);
1499                 }
1500         }
1501
1502         case EXPR_ARRAY_ACCESS:
1503                 return is_constant_expression(expression->array_access.index)
1504                         && is_address_constant(expression->array_access.array_ref);
1505
1506         case EXPR_REFERENCE: {
1507                 declaration_t *declaration = expression->reference.declaration;
1508                 switch((storage_class_tag_t) declaration->storage_class) {
1509                 case STORAGE_CLASS_NONE:
1510                 case STORAGE_CLASS_EXTERN:
1511                 case STORAGE_CLASS_STATIC:
1512                         return true;
1513                 default:
1514                         return false;
1515                 }
1516         }
1517
1518         default:
1519                 return false;
1520         }
1521 }
1522
1523 bool is_address_constant(const expression_t *expression)
1524 {
1525         switch(expression->kind) {
1526         case EXPR_UNARY_TAKE_ADDRESS:
1527                 return is_object_with_linker_constant_address(expression->unary.value);
1528
1529         case EXPR_UNARY_DEREFERENCE: {
1530                 type_t *real_type
1531                         = revert_automatic_type_conversion(expression->unary.value);
1532                 /* dereferencing a function is a NOP */
1533                 if(is_type_function(real_type)) {
1534                         return is_address_constant(expression->unary.value);
1535                 }
1536
1537                 /* fallthrough */
1538         }
1539
1540         case EXPR_UNARY_CAST: {
1541                 type_t *dest = skip_typeref(expression->base.type);
1542                 if (!is_type_pointer(dest) &&
1543                                 ! (dest->kind == TYPE_ATOMIC
1544                                         && (get_atomic_type_flags(dest->atomic.akind) & ATOMIC_TYPE_FLAG_INTEGER)
1545                                         && (get_atomic_type_size(dest->atomic.akind) >= get_atomic_type_size(get_intptr_kind()))))
1546                         return false;
1547
1548                 return (is_constant_expression(expression->unary.value)
1549                         || is_address_constant(expression->unary.value));
1550         }
1551
1552         case EXPR_BINARY_ADD:
1553         case EXPR_BINARY_SUB: {
1554                 expression_t *left  = expression->binary.left;
1555                 expression_t *right = expression->binary.right;
1556
1557                 if(is_type_pointer(skip_typeref(left->base.type))) {
1558                         return is_address_constant(left) && is_constant_expression(right);
1559                 } else if(is_type_pointer(skip_typeref(right->base.type))) {
1560                         return is_constant_expression(left)     && is_address_constant(right);
1561                 }
1562
1563                 return false;
1564         }
1565
1566         case EXPR_REFERENCE: {
1567                 declaration_t *declaration = expression->reference.declaration;
1568                 type_t *type = skip_typeref(declaration->type);
1569                 if(is_type_function(type))
1570                         return true;
1571                 if(is_type_array(type)) {
1572                         return is_object_with_linker_constant_address(expression);
1573                 }
1574                 return false;
1575         }
1576
1577         default:
1578                 return false;
1579         }
1580 }
1581
1582 static bool is_builtin_const_call(const expression_t *expression)
1583 {
1584         expression_t *function = expression->call.function;
1585         if (function->kind != EXPR_BUILTIN_SYMBOL) {
1586                 return false;
1587         }
1588
1589         symbol_t *symbol = function->builtin_symbol.symbol;
1590
1591         switch (symbol->ID) {
1592         case T___builtin_huge_val:
1593         case T___builtin_nan:
1594         case T___builtin_nanf:
1595         case T___builtin_nand:
1596                 return true;
1597         }
1598
1599         return false;
1600 }
1601
1602 static bool is_constant_pointer(const expression_t *expression)
1603 {
1604         if (is_constant_expression(expression))
1605                 return true;
1606
1607         switch (expression->kind) {
1608         case EXPR_SELECT:
1609                 return is_constant_pointer(expression->select.compound);
1610         case EXPR_UNARY_CAST:
1611                 return is_constant_pointer(expression->unary.value);
1612         default:
1613                 return false;
1614         }
1615 }
1616
1617 static bool is_object_with_constant_address(const expression_t *expression)
1618 {
1619         switch(expression->kind) {
1620         case EXPR_SELECT: {
1621                 expression_t *compound      = expression->select.compound;
1622                 type_t       *compound_type = compound->base.type;
1623                 compound_type = skip_typeref(compound_type);
1624                 if(is_type_pointer(compound_type)) {
1625                         return is_constant_pointer(compound);
1626                 } else {
1627                         return is_object_with_constant_address(compound);
1628                 }
1629         }
1630         case EXPR_ARRAY_ACCESS:
1631                 return is_constant_pointer(expression->array_access.array_ref)
1632                         && is_constant_expression(expression->array_access.index);
1633         case EXPR_UNARY_DEREFERENCE:
1634                 return is_constant_pointer(expression->unary.value);
1635         default:
1636                 return false;
1637         }
1638 }
1639
1640 bool is_constant_expression(const expression_t *expression)
1641 {
1642         switch(expression->kind) {
1643
1644         case EXPR_CONST:
1645         case EXPR_CHARACTER_CONSTANT:
1646         case EXPR_WIDE_CHARACTER_CONSTANT:
1647         case EXPR_STRING_LITERAL:
1648         case EXPR_WIDE_STRING_LITERAL:
1649         case EXPR_CLASSIFY_TYPE:
1650         case EXPR_FUNCNAME:
1651         case EXPR_OFFSETOF:
1652         case EXPR_ALIGNOF:
1653         case EXPR_BUILTIN_CONSTANT_P:
1654                 return true;
1655
1656         case EXPR_SIZEOF: {
1657                 type_t *type = expression->typeprop.type;
1658                 if (type == NULL)
1659                         type = expression->typeprop.tp_expression->base.type;
1660
1661                 type = skip_typeref(type);
1662                 if (is_type_array(type) && type->array.is_vla)
1663                         return false;
1664                 return true;
1665         }
1666
1667         case EXPR_BUILTIN_SYMBOL:
1668         case EXPR_BUILTIN_PREFETCH:
1669         case EXPR_SELECT:
1670         case EXPR_VA_START:
1671         case EXPR_VA_ARG:
1672         case EXPR_STATEMENT:
1673         case EXPR_UNARY_POSTFIX_INCREMENT:
1674         case EXPR_UNARY_POSTFIX_DECREMENT:
1675         case EXPR_UNARY_PREFIX_INCREMENT:
1676         case EXPR_UNARY_PREFIX_DECREMENT:
1677         case EXPR_UNARY_ASSUME: /* has VOID type */
1678         case EXPR_UNARY_DEREFERENCE:
1679         case EXPR_BINARY_ASSIGN:
1680         case EXPR_BINARY_MUL_ASSIGN:
1681         case EXPR_BINARY_DIV_ASSIGN:
1682         case EXPR_BINARY_MOD_ASSIGN:
1683         case EXPR_BINARY_ADD_ASSIGN:
1684         case EXPR_BINARY_SUB_ASSIGN:
1685         case EXPR_BINARY_SHIFTLEFT_ASSIGN:
1686         case EXPR_BINARY_SHIFTRIGHT_ASSIGN:
1687         case EXPR_BINARY_BITWISE_AND_ASSIGN:
1688         case EXPR_BINARY_BITWISE_XOR_ASSIGN:
1689         case EXPR_BINARY_BITWISE_OR_ASSIGN:
1690         case EXPR_BINARY_COMMA:
1691                 return false;
1692
1693         case EXPR_UNARY_TAKE_ADDRESS:
1694                 return is_object_with_constant_address(expression->unary.value);
1695
1696         case EXPR_CALL:
1697                 return is_builtin_const_call(expression);
1698
1699         case EXPR_UNARY_NEGATE:
1700         case EXPR_UNARY_PLUS:
1701         case EXPR_UNARY_BITWISE_NEGATE:
1702         case EXPR_UNARY_NOT:
1703                 return is_constant_expression(expression->unary.value);
1704
1705         case EXPR_UNARY_CAST:
1706         case EXPR_UNARY_CAST_IMPLICIT:
1707                 return is_type_arithmetic(skip_typeref(expression->base.type))
1708                         && is_constant_expression(expression->unary.value);
1709
1710         case EXPR_BINARY_ADD:
1711         case EXPR_BINARY_SUB:
1712         case EXPR_BINARY_MUL:
1713         case EXPR_BINARY_DIV:
1714         case EXPR_BINARY_MOD:
1715         case EXPR_BINARY_EQUAL:
1716         case EXPR_BINARY_NOTEQUAL:
1717         case EXPR_BINARY_LESS:
1718         case EXPR_BINARY_LESSEQUAL:
1719         case EXPR_BINARY_GREATER:
1720         case EXPR_BINARY_GREATEREQUAL:
1721         case EXPR_BINARY_BITWISE_AND:
1722         case EXPR_BINARY_BITWISE_OR:
1723         case EXPR_BINARY_BITWISE_XOR:
1724         case EXPR_BINARY_LOGICAL_AND:
1725         case EXPR_BINARY_LOGICAL_OR:
1726         case EXPR_BINARY_SHIFTLEFT:
1727         case EXPR_BINARY_SHIFTRIGHT:
1728         case EXPR_BINARY_BUILTIN_EXPECT:
1729         case EXPR_BINARY_ISGREATER:
1730         case EXPR_BINARY_ISGREATEREQUAL:
1731         case EXPR_BINARY_ISLESS:
1732         case EXPR_BINARY_ISLESSEQUAL:
1733         case EXPR_BINARY_ISLESSGREATER:
1734         case EXPR_BINARY_ISUNORDERED:
1735                 return is_constant_expression(expression->binary.left)
1736                         && is_constant_expression(expression->binary.right);
1737
1738         case EXPR_COMPOUND_LITERAL:
1739                 return is_constant_initializer(expression->compound_literal.initializer);
1740
1741         case EXPR_CONDITIONAL: {
1742                 expression_t *condition = expression->conditional.condition;
1743                 if(!is_constant_expression(condition))
1744                         return false;
1745
1746                 long val = fold_constant(condition);
1747                 if(val != 0)
1748                         return is_constant_expression(expression->conditional.true_expression);
1749                 else
1750                         return is_constant_expression(expression->conditional.false_expression);
1751         }
1752
1753         case EXPR_ARRAY_ACCESS:
1754                 return is_constant_expression(expression->array_access.array_ref)
1755                         && is_constant_expression(expression->array_access.index);
1756
1757         case EXPR_REFERENCE: {
1758                 declaration_t *declaration = expression->reference.declaration;
1759                 if(declaration->storage_class == STORAGE_CLASS_ENUM_ENTRY)
1760                         return true;
1761
1762                 return false;
1763         }
1764
1765         case EXPR_INVALID:
1766                 return true;
1767
1768         case EXPR_UNKNOWN:
1769                 break;
1770         }
1771         panic("invalid expression found (is constant expression)");
1772 }
1773
1774 /**
1775  * Initialize the AST construction.
1776  */
1777 void init_ast(void)
1778 {
1779         obstack_init(&ast_obstack);
1780 }
1781
1782 /**
1783  * Free the AST.
1784  */
1785 void exit_ast(void)
1786 {
1787         obstack_free(&ast_obstack, NULL);
1788 }
1789
1790 /**
1791  * Set the output stream for the AST printer.
1792  *
1793  * @param stream  the output stream
1794  */
1795 void ast_set_output(FILE *stream)
1796 {
1797         out = stream;
1798         type_set_output(stream);
1799 }
1800
1801 /**
1802  * Allocate an AST object of the given size.
1803  *
1804  * @param size  the size of the object to allocate
1805  *
1806  * @return  A new allocated object in the AST memeory space.
1807  */
1808 void *(allocate_ast)(size_t size)
1809 {
1810         return _allocate_ast(size);
1811 }