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