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