51b6a7ccd625131233189c8635fac14e9c734c59
[cparser] / ast.c
1 #include <config.h>
2
3 #include "ast_t.h"
4 #include "type_t.h"
5
6 #include <assert.h>
7 #include <stdio.h>
8 #include <stdlib.h>
9 #include <ctype.h>
10
11 #include "adt/error.h"
12
13 struct obstack ast_obstack;
14
15 static FILE *out;
16 static int   indent;
17
18 /** If set, implicit casts are printed. */
19 bool print_implicit_casts = false;
20
21 /** If set parenthesis are printed to indicate operator precedence. */
22 bool print_parenthesis = false;
23
24 static void print_statement(const statement_t *statement);
25 static void print_expression_prec(const expression_t *expression, unsigned prec);
26
27 void change_indent(int delta)
28 {
29         indent += delta;
30         assert(indent >= 0);
31 }
32
33 void print_indent(void)
34 {
35         for(int i = 0; i < indent; ++i)
36                 fprintf(out, "\t");
37 }
38
39 enum precedence_t {
40         PREC_BOTTOM  =  0,
41         PREC_COMMA   =  2, /* ,                                    left to right */
42         PREC_ASSIGN  =  4, /* = += -= *= /= %= <<= >>= &= ^= |=    right to left */
43         PREC_COND    =  6, /* ?:                                   right to left */
44         PREC_LOG_OR  =  8, /* ||                                   left to right */
45         PREC_LOG_AND = 10, /* &&                                   left to right */
46         PREC_BIT_OR  = 12, /* |                                    left to right */
47         PREC_BIT_XOR = 14, /* ^                                    left to right */
48         PREC_BIT_AND = 16, /* &                                    left to right */
49         PREC_EQ      = 18, /* == !=                                left to right */
50         PREC_CMP     = 20, /* < <= > >=                            left to right */
51         PREC_SHF     = 22, /* << >>                                left to right */
52         PREC_PLUS    = 24, /* + -                                  left to right */
53         PREC_MUL     = 26, /* * / %                                left to right */
54         PREC_UNARY   = 28, /* ! ~ ++ -- + - (type) * & sizeof      right to left */
55         PREC_ACCESS  = 30, /* () [] -> .                           left to right */
56         PREC_PRIM    = 32, /* primary */
57         PREC_TOP     = 34
58 };
59
60 /**
61  * Returns 1 if a given precedence level has right-to-left
62  * associativity, else -1.
63  *
64  * @param precedence   the operator precedence
65  */
66 static int right_to_left(unsigned precedence) {
67         return (precedence == PREC_ASSIGN || precedence == PREC_COND ||
68                 precedence == PREC_UNARY) ? 1 : -1;
69 }
70
71 /**
72  * Return the precedence of an expression given by its kind.
73  *
74  * @param kind   the expression kind
75  */
76 static unsigned get_expression_precedence(expression_kind_t kind)
77 {
78         static const unsigned prec[] = {
79                 [EXPR_UNKNOWN]                   = PREC_PRIM,
80                 [EXPR_INVALID]                   = PREC_PRIM,
81                 [EXPR_REFERENCE]                 = PREC_PRIM,
82                 [EXPR_CHAR_CONST]                = PREC_PRIM,
83                 [EXPR_CONST]                     = PREC_PRIM,
84                 [EXPR_STRING_LITERAL]            = PREC_PRIM,
85                 [EXPR_WIDE_STRING_LITERAL]       = PREC_PRIM,
86                 [EXPR_CALL]                      = PREC_PRIM,
87                 [EXPR_CONDITIONAL]               = PREC_COND,
88                 [EXPR_SELECT]                    = PREC_ACCESS,
89                 [EXPR_ARRAY_ACCESS]              = PREC_ACCESS,
90                 [EXPR_SIZEOF]                    = PREC_UNARY,
91                 [EXPR_CLASSIFY_TYPE]             = PREC_UNARY,
92                 [EXPR_ALIGNOF]                   = PREC_UNARY,
93
94                 [EXPR_FUNCTION]                  = PREC_PRIM,
95                 [EXPR_PRETTY_FUNCTION]           = PREC_PRIM,
96                 [EXPR_BUILTIN_SYMBOL]            = PREC_PRIM,
97                 [EXPR_BUILTIN_CONSTANT_P]        = PREC_PRIM,
98                 [EXPR_BUILTIN_PREFETCH]          = PREC_PRIM,
99                 [EXPR_OFFSETOF]                  = PREC_PRIM,
100                 [EXPR_VA_START]                  = PREC_PRIM,
101                 [EXPR_VA_ARG]                    = PREC_PRIM,
102                 [EXPR_STATEMENT]                 = PREC_ACCESS,
103
104                 [EXPR_UNARY_NEGATE]              = PREC_UNARY,
105                 [EXPR_UNARY_PLUS]                = PREC_UNARY,
106                 [EXPR_UNARY_BITWISE_NEGATE]      = PREC_UNARY,
107                 [EXPR_UNARY_NOT]                 = PREC_UNARY,
108                 [EXPR_UNARY_DEREFERENCE]         = PREC_UNARY,
109                 [EXPR_UNARY_TAKE_ADDRESS]        = PREC_UNARY,
110                 [EXPR_UNARY_POSTFIX_INCREMENT]   = PREC_UNARY,
111                 [EXPR_UNARY_POSTFIX_DECREMENT]   = PREC_UNARY,
112                 [EXPR_UNARY_PREFIX_INCREMENT]    = PREC_UNARY,
113                 [EXPR_UNARY_PREFIX_DECREMENT]    = PREC_UNARY,
114                 [EXPR_UNARY_CAST]                = PREC_UNARY,
115                 [EXPR_UNARY_CAST_IMPLICIT]       = PREC_UNARY,
116                 [EXPR_UNARY_ASSUME]              = PREC_PRIM,
117                 [EXPR_UNARY_BITFIELD_EXTRACT]    = PREC_ACCESS,
118
119                 [EXPR_BINARY_ADD]                = PREC_PLUS,
120                 [EXPR_BINARY_SUB]                = PREC_PLUS,
121                 [EXPR_BINARY_MUL]                = PREC_MUL,
122                 [EXPR_BINARY_DIV]                = PREC_MUL,
123                 [EXPR_BINARY_MOD]                = PREC_MUL,
124                 [EXPR_BINARY_EQUAL]              = PREC_EQ,
125                 [EXPR_BINARY_NOTEQUAL]           = PREC_EQ,
126                 [EXPR_BINARY_LESS]               = PREC_CMP,
127                 [EXPR_BINARY_LESSEQUAL]          = PREC_CMP,
128                 [EXPR_BINARY_GREATER]            = PREC_CMP,
129                 [EXPR_BINARY_GREATEREQUAL]       = PREC_CMP,
130                 [EXPR_BINARY_BITWISE_AND]        = PREC_BIT_AND,
131                 [EXPR_BINARY_BITWISE_OR]         = PREC_BIT_OR,
132                 [EXPR_BINARY_BITWISE_XOR]        = PREC_BIT_XOR,
133                 [EXPR_BINARY_LOGICAL_AND]        = PREC_LOG_AND,
134                 [EXPR_BINARY_LOGICAL_OR]         = PREC_LOG_OR,
135                 [EXPR_BINARY_SHIFTLEFT]          = PREC_SHF,
136                 [EXPR_BINARY_SHIFTRIGHT]         = PREC_SHF,
137                 [EXPR_BINARY_ASSIGN]             = PREC_ASSIGN,
138                 [EXPR_BINARY_MUL_ASSIGN]         = PREC_ASSIGN,
139                 [EXPR_BINARY_DIV_ASSIGN]         = PREC_ASSIGN,
140                 [EXPR_BINARY_MOD_ASSIGN]         = PREC_ASSIGN,
141                 [EXPR_BINARY_ADD_ASSIGN]         = PREC_ASSIGN,
142                 [EXPR_BINARY_SUB_ASSIGN]         = PREC_ASSIGN,
143                 [EXPR_BINARY_SHIFTLEFT_ASSIGN]   = PREC_ASSIGN,
144                 [EXPR_BINARY_SHIFTRIGHT_ASSIGN]  = PREC_ASSIGN,
145                 [EXPR_BINARY_BITWISE_AND_ASSIGN] = PREC_ASSIGN,
146                 [EXPR_BINARY_BITWISE_XOR_ASSIGN] = PREC_ASSIGN,
147                 [EXPR_BINARY_BITWISE_OR_ASSIGN]  = PREC_ASSIGN,
148                 [EXPR_BINARY_COMMA]              = PREC_COMMA,
149
150                 [EXPR_BINARY_BUILTIN_EXPECT]     = PREC_PRIM,
151                 [EXPR_BINARY_ISGREATER]          = PREC_PRIM,
152                 [EXPR_BINARY_ISGREATEREQUAL]     = PREC_PRIM,
153                 [EXPR_BINARY_ISLESS]             = PREC_PRIM,
154                 [EXPR_BINARY_ISLESSEQUAL]        = PREC_PRIM,
155                 [EXPR_BINARY_ISLESSGREATER]      = PREC_PRIM,
156                 [EXPR_BINARY_ISUNORDERED]        = PREC_PRIM
157         };
158         assert((unsigned)kind < (sizeof(prec)/sizeof(prec[0])));
159         unsigned res = prec[kind];
160
161         assert(res != PREC_BOTTOM);
162         return res;
163 }
164
165 /**
166  * Print a constant expression.
167  *
168  * @param cnst  the constant expression
169  */
170 static void print_const(const const_expression_t *cnst)
171 {
172         if(cnst->base.type == NULL)
173                 return;
174
175         const type_t *const type = skip_typeref(cnst->base.type);
176
177         if (is_type_integer(type)) {
178                 fprintf(out, "%lld", cnst->v.int_value);
179         } else if (is_type_float(type)) {
180                 fprintf(out, "%Lf", cnst->v.float_value);
181         } else {
182                 panic("unknown constant");
183         }
184 }
185
186 /**
187  * Print a quoted string constant.
188  *
189  * @param string  the string constant
190  * @param border  the border char
191  */
192 static void print_quoted_string(const string_t *const string, char border)
193 {
194         fputc(border, out);
195         const char *end = string->begin + string->size;
196         for (const char *c = string->begin; c != end; ++c) {
197                 if (*c == border) {
198                         fputc('\\', out);
199                 }
200                 switch(*c) {
201                 case '\\':  fputs("\\\\", out); break;
202                 case '\a':  fputs("\\a", out); break;
203                 case '\b':  fputs("\\b", out); break;
204                 case '\f':  fputs("\\f", out); break;
205                 case '\n':  fputs("\\n", out); break;
206                 case '\r':  fputs("\\r", out); break;
207                 case '\t':  fputs("\\t", out); break;
208                 case '\v':  fputs("\\v", out); break;
209                 case '\?':  fputs("\\?", out); break;
210                 default:
211                         if(!isprint(*c)) {
212                                 fprintf(out, "\\%03o", *c);
213                                 break;
214                         }
215                         fputc(*c, out);
216                         break;
217                 }
218         }
219         fputc(border, out);
220 }
221
222 /**
223  * Print a constant character expression.
224  *
225  * @param cnst  the constant character expression
226  */
227 static void print_char_const(const const_expression_t *cnst)
228 {
229         print_quoted_string(&cnst->v.chars, '\'');
230 }
231
232 /**
233  * Prints a string literal expression.
234  *
235  * @param string_literal  the string literal expression
236  */
237 static void print_string_literal(
238                 const string_literal_expression_t *string_literal)
239 {
240         print_quoted_string(&string_literal->value, '"');
241 }
242
243 /**
244  * Prints a wide string literal expression.
245  *
246  * @param wstr  the wide string literal expression
247  */
248 static void print_wide_string_literal(
249         const wide_string_literal_expression_t *const wstr)
250 {
251         fputs("L\"", out);
252         for (const wchar_rep_t *c   = wstr->value.begin,
253                                *end = c + wstr->value.size;
254              c != end; ++c) {
255                 switch (*c) {
256                         case L'\"':  fputs("\\\"", out); break;
257                         case L'\\':  fputs("\\\\", out); break;
258                         case L'\a':  fputs("\\a",  out); break;
259                         case L'\b':  fputs("\\b",  out); break;
260                         case L'\f':  fputs("\\f",  out); break;
261                         case L'\n':  fputs("\\n",  out); break;
262                         case L'\r':  fputs("\\r",  out); break;
263                         case L'\t':  fputs("\\t",  out); break;
264                         case L'\v':  fputs("\\v",  out); break;
265                         case L'\?':  fputs("\\?",  out); break;
266                         default: {
267                                 const unsigned tc = *c;
268                                 if (tc < 0x80U) {
269                                         if (!isprint(*c))  {
270                                                 fprintf(out, "\\%03o", (char)*c);
271                                         } else {
272                                                 fputc(*c, out);
273                                         }
274                                 } else if (tc < 0x800) {
275                                         fputc(0xC0 | (tc >> 6),   out);
276                                         fputc(0x80 | (tc & 0x3F), out);
277                                 } else if (tc < 0x10000) {
278                                         fputc(0xE0 | ( tc >> 12),         out);
279                                         fputc(0x80 | ((tc >>  6) & 0x3F), out);
280                                         fputc(0x80 | ( tc        & 0x3F), out);
281                                 } else {
282                                         fputc(0xF0 | ( tc >> 18),         out);
283                                         fputc(0x80 | ((tc >> 12) & 0x3F), out);
284                                         fputc(0x80 | ((tc >>  6) & 0x3F), out);
285                                         fputc(0x80 | ( tc        & 0x3F), out);
286                                 }
287                         }
288                 }
289         }
290         fputc('"', out);
291 }
292
293 /**
294  * Prints a call expression.
295  *
296  * @param call  the call expression
297  */
298 static void print_call_expression(const call_expression_t *call)
299 {
300         unsigned prec = get_expression_precedence(call->base.kind);
301         print_expression_prec(call->function, prec);
302         fprintf(out, "(");
303         call_argument_t *argument = call->arguments;
304         int              first    = 1;
305         while(argument != NULL) {
306                 if(!first) {
307                         fprintf(out, ", ");
308                 } else {
309                         first = 0;
310                 }
311                 print_expression_prec(argument->expression, PREC_COMMA + 1);
312
313                 argument = argument->next;
314         }
315         fprintf(out, ")");
316 }
317
318 /**
319  * Prints a binary expression.
320  *
321  * @param binexpr   the binary expression
322  */
323 static void print_binary_expression(const binary_expression_t *binexpr)
324 {
325         unsigned prec = get_expression_precedence(binexpr->base.kind);
326         int      r2l  = right_to_left(prec);
327
328         if(binexpr->base.kind == EXPR_BINARY_BUILTIN_EXPECT) {
329                 fputs("__builtin_expect(", out);
330                 print_expression_prec(binexpr->left, prec);
331                 fputs(", ", out);
332                 print_expression_prec(binexpr->right, prec);
333                 fputc(')', out);
334                 return;
335         }
336
337         print_expression_prec(binexpr->left, prec + r2l);
338         if (binexpr->base.kind != EXPR_BINARY_COMMA) {
339                 fputc(' ', out);
340         }
341         switch (binexpr->base.kind) {
342         case EXPR_BINARY_COMMA:              fputs(",", out);     break;
343         case EXPR_BINARY_ASSIGN:             fputs("=", out);     break;
344         case EXPR_BINARY_ADD:                fputs("+", out);     break;
345         case EXPR_BINARY_SUB:                fputs("-", out);     break;
346         case EXPR_BINARY_MUL:                fputs("*", out);     break;
347         case EXPR_BINARY_MOD:                fputs("%", out);     break;
348         case EXPR_BINARY_DIV:                fputs("/", out);     break;
349         case EXPR_BINARY_BITWISE_OR:         fputs("|", out);     break;
350         case EXPR_BINARY_BITWISE_AND:        fputs("&", out);     break;
351         case EXPR_BINARY_BITWISE_XOR:        fputs("^", out);     break;
352         case EXPR_BINARY_LOGICAL_OR:         fputs("||", out);    break;
353         case EXPR_BINARY_LOGICAL_AND:        fputs("&&", out);    break;
354         case EXPR_BINARY_NOTEQUAL:           fputs("!=", out);    break;
355         case EXPR_BINARY_EQUAL:              fputs("==", out);    break;
356         case EXPR_BINARY_LESS:               fputs("<", out);     break;
357         case EXPR_BINARY_LESSEQUAL:          fputs("<=", out);    break;
358         case EXPR_BINARY_GREATER:            fputs(">", out);     break;
359         case EXPR_BINARY_GREATEREQUAL:       fputs(">=", out);    break;
360         case EXPR_BINARY_SHIFTLEFT:          fputs("<<", out);    break;
361         case EXPR_BINARY_SHIFTRIGHT:         fputs(">>", out);    break;
362
363         case EXPR_BINARY_ADD_ASSIGN:         fputs("+=", out);    break;
364         case EXPR_BINARY_SUB_ASSIGN:         fputs("-=", out);    break;
365         case EXPR_BINARY_MUL_ASSIGN:         fputs("*=", out);    break;
366         case EXPR_BINARY_MOD_ASSIGN:         fputs("%=", out);    break;
367         case EXPR_BINARY_DIV_ASSIGN:         fputs("/=", out);    break;
368         case EXPR_BINARY_BITWISE_OR_ASSIGN:  fputs("|=", out);    break;
369         case EXPR_BINARY_BITWISE_AND_ASSIGN: fputs("&=", out);    break;
370         case EXPR_BINARY_BITWISE_XOR_ASSIGN: fputs("^=", out);    break;
371         case EXPR_BINARY_SHIFTLEFT_ASSIGN:   fputs("<<=", out);   break;
372         case EXPR_BINARY_SHIFTRIGHT_ASSIGN:  fputs(">>=", out);   break;
373         default: panic("invalid binexpression found");
374         }
375         fputc(' ', out);
376         print_expression_prec(binexpr->right, prec - r2l);
377 }
378
379 /**
380  * Prints an unary expression.
381  *
382  * @param unexpr   the unary expression
383  */
384 static void print_unary_expression(const unary_expression_t *unexpr)
385 {
386         unsigned prec = get_expression_precedence(unexpr->base.kind);
387         switch(unexpr->base.kind) {
388         case EXPR_UNARY_NEGATE:           fputs("-", out);  break;
389         case EXPR_UNARY_PLUS:             fputs("+", out);  break;
390         case EXPR_UNARY_NOT:              fputs("!", out);  break;
391         case EXPR_UNARY_BITWISE_NEGATE:   fputs("~", out);  break;
392         case EXPR_UNARY_PREFIX_INCREMENT: fputs("++", out); break;
393         case EXPR_UNARY_PREFIX_DECREMENT: fputs("--", out); break;
394         case EXPR_UNARY_DEREFERENCE:      fputs("*", out);  break;
395         case EXPR_UNARY_TAKE_ADDRESS:     fputs("&", out);  break;
396
397         case EXPR_UNARY_BITFIELD_EXTRACT:
398                 print_expression_prec(unexpr->value, prec);
399                 return;
400
401         case EXPR_UNARY_POSTFIX_INCREMENT:
402                 print_expression_prec(unexpr->value, prec);
403                 fputs("++", out);
404                 return;
405         case EXPR_UNARY_POSTFIX_DECREMENT:
406                 print_expression_prec(unexpr->value, prec);
407                 fputs("--", out);
408                 return;
409         case EXPR_UNARY_CAST_IMPLICIT:
410                 if(!print_implicit_casts) {
411                         print_expression_prec(unexpr->value, prec);
412                         return;
413                 }
414                 /* fallthrough */
415         case EXPR_UNARY_CAST:
416                 fputc('(', out);
417                 print_type(unexpr->base.type);
418                 fputc(')', out);
419                 break;
420         case EXPR_UNARY_ASSUME:
421                 fputs("__assume(", out);
422                 print_expression_prec(unexpr->value, PREC_COMMA + 1);
423                 fputc(')', out);
424                 return;
425         default:
426                 panic("invalid unary expression found");
427         }
428         print_expression_prec(unexpr->value, prec);
429 }
430
431 /**
432  * Prints a reference expression.
433  *
434  * @param ref   the reference expression
435  */
436 static void print_reference_expression(const reference_expression_t *ref)
437 {
438         fprintf(out, "%s", ref->declaration->symbol->string);
439 }
440
441 /**
442  * Prints an array expression.
443  *
444  * @param expression   the array expression
445  */
446 static void print_array_expression(const array_access_expression_t *expression)
447 {
448         unsigned prec = get_expression_precedence(expression->base.kind);
449         if(!expression->flipped) {
450                 print_expression_prec(expression->array_ref, prec);
451                 fputc('[', out);
452                 print_expression_prec(expression->index, prec);
453                 fputc(']', out);
454         } else {
455                 print_expression_prec(expression->index, prec);
456                 fputc('[', out);
457                 print_expression_prec(expression->array_ref, prec);
458                 fputc(']', out);
459         }
460 }
461
462 /**
463  * Prints a typeproperty expression (sizeof or __alignof__).
464  *
465  * @param expression   the type property expression
466  */
467 static void print_typeprop_expression(const typeprop_expression_t *expression)
468 {
469         if (expression->base.kind == EXPR_SIZEOF) {
470                 fputs("sizeof", out);
471         } else {
472                 assert(expression->base.kind == EXPR_ALIGNOF);
473                 fputs("__alignof__", out);
474         }
475         if(expression->tp_expression != NULL) {
476                 /* always print the '()' here, sizeof x is right but unusual */
477                 fputc('(', out);
478                 print_expression_prec(expression->tp_expression, PREC_ACCESS);
479                 fputc(')', out);
480         } else {
481                 fputc('(', out);
482                 print_type(expression->type);
483                 fputc(')', out);
484         }
485 }
486
487 /**
488  * Prints an builtin symbol.
489  *
490  * @param expression   the builtin symbol expression
491  */
492 static void print_builtin_symbol(const builtin_symbol_expression_t *expression)
493 {
494         fputs(expression->symbol->string, out);
495 }
496
497 /**
498  * Prints a builtin constant expression.
499  *
500  * @param expression   the builtin constant expression
501  */
502 static void print_builtin_constant(const builtin_constant_expression_t *expression)
503 {
504         fputs("__builtin_constant_p(", out);
505         print_expression_prec(expression->value, PREC_COMMA + 1);
506         fputc(')', out);
507 }
508
509 /**
510  * Prints a builtin prefetch expression.
511  *
512  * @param expression   the builtin prefetch expression
513  */
514 static void print_builtin_prefetch(const builtin_prefetch_expression_t *expression)
515 {
516         fputs("__builtin_prefetch(", out);
517         print_expression_prec(expression->adr, PREC_COMMA + 1);
518         if (expression->rw) {
519                 fputc(',', out);
520                 print_expression_prec(expression->rw, PREC_COMMA + 1);
521         }
522         if (expression->locality) {
523                 fputc(',', out);
524                 print_expression_prec(expression->locality, PREC_COMMA + 1);
525         }
526         fputc(')', out);
527 }
528
529 /**
530  * Prints a conditional expression.
531  *
532  * @param expression   the conditional expression
533  */
534 static void print_conditional(const conditional_expression_t *expression)
535 {
536         unsigned prec = get_expression_precedence(expression->base.kind);
537         fputs("(", out);
538         print_expression_prec(expression->condition, prec);
539         fputs(" ? ", out);
540         print_expression_prec(expression->true_expression, prec);
541         fputs(" : ", out);
542         print_expression_prec(expression->false_expression, prec);
543         fputs(")", out);
544 }
545
546 /**
547  * Prints a va_start expression.
548  *
549  * @param expression   the va_start expression
550  */
551 static void print_va_start(const va_start_expression_t *const expression)
552 {
553         fputs("__builtin_va_start(", out);
554         print_expression_prec(expression->ap, PREC_COMMA + 1);
555         fputs(", ", out);
556         fputs(expression->parameter->symbol->string, out);
557         fputs(")", out);
558 }
559
560 /**
561  * Prints a va_arg expression.
562  *
563  * @param expression   the va_arg expression
564  */
565 static void print_va_arg(const va_arg_expression_t *expression)
566 {
567         fputs("__builtin_va_arg(", out);
568         print_expression_prec(expression->ap, PREC_COMMA + 1);
569         fputs(", ", out);
570         print_type(expression->base.type);
571         fputs(")", out);
572 }
573
574 /**
575  * Prints a select expression (. or ->).
576  *
577  * @param expression   the select expression
578  */
579 static void print_select(const select_expression_t *expression)
580 {
581         unsigned prec = get_expression_precedence(expression->base.kind);
582         print_expression_prec(expression->compound, prec);
583         if(expression->compound->base.type == NULL ||
584                         expression->compound->base.type->kind == TYPE_POINTER) {
585                 fputs("->", out);
586         } else {
587                 fputc('.', out);
588         }
589         fputs(expression->symbol->string, out);
590 }
591
592 /**
593  * Prints a type classify expression.
594  *
595  * @param expr   the type classify expression
596  */
597 static void print_classify_type_expression(
598         const classify_type_expression_t *const expr)
599 {
600         fputs("__builtin_classify_type(", out);
601         print_expression_prec(expr->type_expression, PREC_COMMA + 1);
602         fputc(')', out);
603 }
604
605 /**
606  * Prints a designator.
607  *
608  * @param designator  the designator
609  */
610 static void print_designator(const designator_t *designator)
611 {
612         fputs(designator->symbol->string, out);
613         for (designator = designator->next; designator != NULL; designator = designator->next) {
614                 if (designator->array_access) {
615                         fputc('[', out);
616                         print_expression_prec(designator->array_access, PREC_ACCESS);
617                         fputc(']', out);
618                 } else {
619                         fputc('.', out);
620                         fputs(designator->symbol->string, out);
621                 }
622         }
623 }
624
625 /**
626  * Prints an offsetof expression.
627  *
628  * @param expression   the offset expression
629  */
630 static void print_offsetof_expression(const offsetof_expression_t *expression)
631 {
632         fputs("__builtin_offsetof", out);
633         fputc('(', out);
634         print_type(expression->type);
635         fputc(',', out);
636         print_designator(expression->designator);
637         fputc(')', out);
638 }
639
640 /**
641  * Prints a statement expression.
642  *
643  * @param expression   the statement expression
644  */
645 static void print_statement_expression(const statement_expression_t *expression)
646 {
647         fputc('(', out);
648         print_statement(expression->statement);
649         fputc(')', out);
650 }
651
652 /**
653  * Prints an expression with parenthesis if needed.
654  *
655  * @param expression  the expression to print
656  * @param top_prec    the precedence of the user of this expression.
657  */
658 static void print_expression_prec(const expression_t *expression, unsigned top_prec)
659 {
660         unsigned prec = get_expression_precedence(expression->base.kind);
661         if (print_parenthesis && top_prec != PREC_BOTTOM)
662                 top_prec = PREC_TOP;
663         if (top_prec > prec)
664                 fputc('(', out);
665         switch(expression->kind) {
666         case EXPR_UNKNOWN:
667         case EXPR_INVALID:
668                 fprintf(out, "*invalid expression*");
669                 break;
670         case EXPR_CHAR_CONST:
671                 print_char_const(&expression->conste);
672                 break;
673         case EXPR_CONST:
674                 print_const(&expression->conste);
675                 break;
676         case EXPR_FUNCTION:
677         case EXPR_PRETTY_FUNCTION:
678         case EXPR_STRING_LITERAL:
679                 print_string_literal(&expression->string);
680                 break;
681         case EXPR_WIDE_STRING_LITERAL:
682                 print_wide_string_literal(&expression->wide_string);
683                 break;
684         case EXPR_CALL:
685                 print_call_expression(&expression->call);
686                 break;
687         EXPR_BINARY_CASES
688                 print_binary_expression(&expression->binary);
689                 break;
690         case EXPR_REFERENCE:
691                 print_reference_expression(&expression->reference);
692                 break;
693         case EXPR_ARRAY_ACCESS:
694                 print_array_expression(&expression->array_access);
695                 break;
696         EXPR_UNARY_CASES
697                 print_unary_expression(&expression->unary);
698                 break;
699         case EXPR_SIZEOF:
700         case EXPR_ALIGNOF:
701                 print_typeprop_expression(&expression->typeprop);
702                 break;
703         case EXPR_BUILTIN_SYMBOL:
704                 print_builtin_symbol(&expression->builtin_symbol);
705                 break;
706         case EXPR_BUILTIN_CONSTANT_P:
707                 print_builtin_constant(&expression->builtin_constant);
708                 break;
709         case EXPR_BUILTIN_PREFETCH:
710                 print_builtin_prefetch(&expression->builtin_prefetch);
711                 break;
712         case EXPR_CONDITIONAL:
713                 print_conditional(&expression->conditional);
714                 break;
715         case EXPR_VA_START:
716                 print_va_start(&expression->va_starte);
717                 break;
718         case EXPR_VA_ARG:
719                 print_va_arg(&expression->va_arge);
720                 break;
721         case EXPR_SELECT:
722                 print_select(&expression->select);
723                 break;
724         case EXPR_CLASSIFY_TYPE:
725                 print_classify_type_expression(&expression->classify_type);
726                 break;
727         case EXPR_OFFSETOF:
728                 print_offsetof_expression(&expression->offsetofe);
729                 break;
730         case EXPR_STATEMENT:
731                 print_statement_expression(&expression->statement);
732                 break;
733
734         default:
735                 /* TODO */
736                 fprintf(out, "some expression of type %d", (int) expression->kind);
737                 break;
738         }
739         if (top_prec > prec)
740                 fputc(')', out);
741 }
742
743 /**
744  * Print an compound statement.
745  *
746  * @param block  the compound statement
747  */
748 static void print_compound_statement(const compound_statement_t *block)
749 {
750         fputs("{\n", out);
751         ++indent;
752
753         statement_t *statement = block->statements;
754         while(statement != NULL) {
755                 if (statement->base.kind == STATEMENT_CASE_LABEL)
756                         --indent;
757                 print_indent();
758                 print_statement(statement);
759
760                 statement = statement->base.next;
761         }
762         --indent;
763         print_indent();
764         fputs("}\n", out);
765 }
766
767 /**
768  * Print a return statement.
769  *
770  * @param statement  the return statement
771  */
772 static void print_return_statement(const return_statement_t *statement)
773 {
774         fprintf(out, "return ");
775         if(statement->value != NULL)
776                 print_expression(statement->value);
777         fputs(";\n", out);
778 }
779
780 /**
781  * Print an expression statement.
782  *
783  * @param statement  the expression statement
784  */
785 static void print_expression_statement(const expression_statement_t *statement)
786 {
787         print_expression(statement->expression);
788         fputs(";\n", out);
789 }
790
791 /**
792  * Print a goto statement.
793  *
794  * @param statement  the goto statement
795  */
796 static void print_goto_statement(const goto_statement_t *statement)
797 {
798         fprintf(out, "goto ");
799         fputs(statement->label->symbol->string, out);
800         fprintf(stderr, "(%p)", (void*) statement->label);
801         fputs(";\n", out);
802 }
803
804 /**
805  * Print a label statement.
806  *
807  * @param statement  the label statement
808  */
809 static void print_label_statement(const label_statement_t *statement)
810 {
811         fprintf(stderr, "(%p)", (void*) statement->label);
812         fprintf(out, "%s:\n", statement->label->symbol->string);
813         if(statement->statement != NULL) {
814                 print_statement(statement->statement);
815         }
816 }
817
818 /**
819  * Print an if statement.
820  *
821  * @param statement  the if statement
822  */
823 static void print_if_statement(const if_statement_t *statement)
824 {
825         fputs("if(", out);
826         print_expression(statement->condition);
827         fputs(") ", out);
828         if(statement->true_statement != NULL) {
829                 print_statement(statement->true_statement);
830         }
831
832         if(statement->false_statement != NULL) {
833                 print_indent();
834                 fputs("else ", out);
835                 print_statement(statement->false_statement);
836         }
837 }
838
839 /**
840  * Print a switch statement.
841  *
842  * @param statement  the switch statement
843  */
844 static void print_switch_statement(const switch_statement_t *statement)
845 {
846         fputs("switch(", out);
847         print_expression(statement->expression);
848         fputs(") ", out);
849         print_statement(statement->body);
850 }
851
852 /**
853  * Print a case label (including the default label).
854  *
855  * @param statement  the case label statement
856  */
857 static void print_case_label(const case_label_statement_t *statement)
858 {
859         if(statement->expression == NULL) {
860                 fputs("default:\n", out);
861         } else {
862                 fputs("case ", out);
863                 print_expression(statement->expression);
864                 if (statement->end_range != NULL) {
865                         fputs(" ... ", out);
866                         print_expression(statement->end_range);
867                 }
868                 fputs(":\n", out);
869         }
870         ++indent;
871         if(statement->statement != NULL) {
872                 if (statement->statement->base.kind == STATEMENT_CASE_LABEL) {
873                         --indent;
874                 }
875                 print_indent();
876                 print_statement(statement->statement);
877         }
878 }
879
880 /**
881  * Print a declaration statement.
882  *
883  * @param statement   the statement
884  */
885 static void print_declaration_statement(
886                 const declaration_statement_t *statement)
887 {
888         int first = 1;
889         declaration_t *declaration = statement->declarations_begin;
890         for( ; declaration != statement->declarations_end->next;
891                declaration = declaration->next) {
892                 if(!first) {
893                         print_indent();
894                 } else {
895                         first = 0;
896                 }
897                 print_declaration(declaration);
898                 fputc('\n', out);
899         }
900 }
901
902 /**
903  * Print a while statement.
904  *
905  * @param statement   the statement
906  */
907 static void print_while_statement(const while_statement_t *statement)
908 {
909         fputs("while(", out);
910         print_expression(statement->condition);
911         fputs(") ", out);
912         print_statement(statement->body);
913 }
914
915 /**
916  * Print a do-while statement.
917  *
918  * @param statement   the statement
919  */
920 static void print_do_while_statement(const do_while_statement_t *statement)
921 {
922         fputs("do ", out);
923         print_statement(statement->body);
924         print_indent();
925         fputs("while(", out);
926         print_expression(statement->condition);
927         fputs(");\n", out);
928 }
929
930 /**
931  * Print a for statement.
932  *
933  * @param statement   the statement
934  */
935 static void print_for_statement(const for_statement_t *statement)
936 {
937         fputs("for(", out);
938         if(statement->scope.declarations != NULL) {
939                 assert(statement->initialisation == NULL);
940                 print_declaration(statement->scope.declarations);
941                 if(statement->scope.declarations->next != NULL) {
942                         panic("multiple declarations in for statement not supported yet");
943                 }
944                 fputc(' ', out);
945         } else {
946                 if(statement->initialisation) {
947                         print_expression(statement->initialisation);
948                 }
949                 fputs("; ", out);
950         }
951         if(statement->condition != NULL) {
952                 print_expression(statement->condition);
953         }
954         fputs("; ", out);
955         if(statement->step != NULL) {
956                 print_expression(statement->step);
957         }
958         fputs(")", out);
959         print_statement(statement->body);
960 }
961
962 /**
963  * Print assembler constraints.
964  *
965  * @param constraints   the constraints
966  */
967 static void print_asm_constraints(asm_constraint_t *constraints)
968 {
969         asm_constraint_t *constraint = constraints;
970         for( ; constraint != NULL; constraint = constraint->next) {
971                 if(constraint != constraints)
972                         fputs(", ", out);
973
974                 if(constraint->symbol) {
975                         fprintf(out, "[%s] ", constraint->symbol->string);
976                 }
977                 print_quoted_string(&constraint->constraints, '"');
978                 fputs(" (", out);
979                 print_expression(constraint->expression);
980                 fputs(")", out);
981         }
982 }
983
984 /**
985  * Print assembler clobbers.
986  *
987  * @param clobbers   the clobbers
988  */
989 static void print_asm_clobbers(asm_clobber_t *clobbers)
990 {
991         asm_clobber_t *clobber = clobbers;
992         for( ; clobber != NULL; clobber = clobber->next) {
993                 if(clobber != clobbers)
994                         fputs(", ", out);
995
996                 print_quoted_string(&clobber->clobber, '"');
997         }
998 }
999
1000 /**
1001  * Print an assembler statement.
1002  *
1003  * @param statement   the statement
1004  */
1005 static void print_asm_statement(const asm_statement_t *statement)
1006 {
1007         fputs("asm ", out);
1008         if(statement->is_volatile) {
1009                 fputs("volatile ", out);
1010         }
1011         fputs("(", out);
1012         print_quoted_string(&statement->asm_text, '"');
1013         if(statement->inputs == NULL && statement->outputs == NULL
1014                         && statement->clobbers == NULL)
1015                 goto end_of_print_asm_statement;
1016
1017         fputs(" : ", out);
1018         print_asm_constraints(statement->inputs);
1019         if(statement->outputs == NULL && statement->clobbers == NULL)
1020                 goto end_of_print_asm_statement;
1021
1022         fputs(" : ", out);
1023         print_asm_constraints(statement->outputs);
1024         if(statement->clobbers == NULL)
1025                 goto end_of_print_asm_statement;
1026
1027         fputs(" : ", out);
1028         print_asm_clobbers(statement->clobbers);
1029
1030 end_of_print_asm_statement:
1031         fputs(");\n", out);
1032 }
1033
1034 /**
1035  * Print a statement.
1036  *
1037  * @param statement   the statement
1038  */
1039 void print_statement(const statement_t *statement)
1040 {
1041         switch(statement->kind) {
1042         case STATEMENT_COMPOUND:
1043                 print_compound_statement(&statement->compound);
1044                 break;
1045         case STATEMENT_RETURN:
1046                 print_return_statement(&statement->returns);
1047                 break;
1048         case STATEMENT_EXPRESSION:
1049                 print_expression_statement(&statement->expression);
1050                 break;
1051         case STATEMENT_LABEL:
1052                 print_label_statement(&statement->label);
1053                 break;
1054         case STATEMENT_GOTO:
1055                 print_goto_statement(&statement->gotos);
1056                 break;
1057         case STATEMENT_CONTINUE:
1058                 fputs("continue;\n", out);
1059                 break;
1060         case STATEMENT_BREAK:
1061                 fputs("break;\n", out);
1062                 break;
1063         case STATEMENT_IF:
1064                 print_if_statement(&statement->ifs);
1065                 break;
1066         case STATEMENT_SWITCH:
1067                 print_switch_statement(&statement->switchs);
1068                 break;
1069         case STATEMENT_CASE_LABEL:
1070                 print_case_label(&statement->case_label);
1071                 break;
1072         case STATEMENT_DECLARATION:
1073                 print_declaration_statement(&statement->declaration);
1074                 break;
1075         case STATEMENT_WHILE:
1076                 print_while_statement(&statement->whiles);
1077                 break;
1078         case STATEMENT_DO_WHILE:
1079                 print_do_while_statement(&statement->do_while);
1080                 break;
1081         case STATEMENT_FOR:
1082                 print_for_statement(&statement->fors);
1083                 break;
1084         case STATEMENT_ASM:
1085                 print_asm_statement(&statement->asms);
1086                 break;
1087         case STATEMENT_INVALID:
1088                 fprintf(out, "*invalid statement*");
1089                 break;
1090         }
1091 }
1092
1093 /**
1094  * Print a storage class.
1095  *
1096  * @param storage_class   the storage class
1097  */
1098 static void print_storage_class(unsigned storage_class)
1099 {
1100         switch((storage_class_tag_t) storage_class) {
1101         case STORAGE_CLASS_ENUM_ENTRY:
1102         case STORAGE_CLASS_NONE:
1103                 break;
1104         case STORAGE_CLASS_TYPEDEF:       fputs("typedef ",        out); break;
1105         case STORAGE_CLASS_EXTERN:        fputs("extern ",         out); break;
1106         case STORAGE_CLASS_STATIC:        fputs("static ",         out); break;
1107         case STORAGE_CLASS_AUTO:          fputs("auto ",           out); break;
1108         case STORAGE_CLASS_REGISTER:      fputs("register ",       out); break;
1109         case STORAGE_CLASS_THREAD:        fputs("__thread",        out); break;
1110         case STORAGE_CLASS_THREAD_EXTERN: fputs("extern __thread", out); break;
1111         case STORAGE_CLASS_THREAD_STATIC: fputs("static __thread", out); break;
1112         }
1113 }
1114
1115 /**
1116  * Print an initializer.
1117  *
1118  * @param initializer  the initializer
1119  */
1120 void print_initializer(const initializer_t *initializer)
1121 {
1122         if(initializer->kind == INITIALIZER_VALUE) {
1123                 const initializer_value_t *value = &initializer->value;
1124                 print_expression(value->value);
1125                 return;
1126         }
1127
1128         assert(initializer->kind == INITIALIZER_LIST);
1129         fputs("{ ", out);
1130         const initializer_list_t *list = &initializer->list;
1131
1132         for(size_t i = 0 ; i < list->len; ++i) {
1133                 if(i > 0) {
1134                         fputs(", ", out);
1135                 }
1136                 print_initializer(list->initializers[i]);
1137         }
1138         fputs("}", out);
1139 }
1140
1141 /**
1142  * Print a declaration in the NORMAL namespace.
1143  *
1144  * @param declaration  the declaration
1145  */
1146 static void print_normal_declaration(const declaration_t *declaration)
1147 {
1148         print_storage_class(declaration->storage_class);
1149         if(declaration->is_inline) {
1150                 if (declaration->modifiers & DM_FORCEINLINE)
1151                         fputs("__forceinline ", out);
1152                 else
1153                         fputs("inline ", out);
1154         }
1155         print_type_ext(declaration->type, declaration->symbol,
1156                        &declaration->scope);
1157
1158         if(declaration->type->kind == TYPE_FUNCTION) {
1159                 if(declaration->init.statement != NULL) {
1160                         fputs("\n", out);
1161                         print_statement(declaration->init.statement);
1162                         return;
1163                 }
1164         } else if(declaration->init.initializer != NULL) {
1165                 fputs(" = ", out);
1166                 print_initializer(declaration->init.initializer);
1167         }
1168         fputc(';', out);
1169 }
1170
1171 /**
1172  * Prints an expression.
1173  *
1174  * @param expression  the expression
1175  */
1176 void print_expression(const expression_t *expression) {
1177         print_expression_prec(expression, PREC_BOTTOM);
1178 }
1179
1180 /**
1181  * Print a declaration.
1182  *
1183  * @param declaration  the declaration
1184  */
1185 void print_declaration(const declaration_t *declaration)
1186 {
1187         if(declaration->namespc != NAMESPACE_NORMAL &&
1188                         declaration->symbol == NULL)
1189                 return;
1190
1191         switch(declaration->namespc) {
1192         case NAMESPACE_NORMAL:
1193                 print_normal_declaration(declaration);
1194                 break;
1195         case NAMESPACE_STRUCT:
1196                 fputs("struct ", out);
1197                 fputs(declaration->symbol->string, out);
1198                 fputc(' ', out);
1199                 print_compound_definition(declaration);
1200                 fputc(';', out);
1201                 break;
1202         case NAMESPACE_UNION:
1203                 fputs("union ", out);
1204                 fputs(declaration->symbol->string, out);
1205                 fputc(' ', out);
1206                 print_compound_definition(declaration);
1207                 fputc(';', out);
1208                 break;
1209         case NAMESPACE_ENUM:
1210                 fputs("enum ", out);
1211                 fputs(declaration->symbol->string, out);
1212                 fputc(' ', out);
1213                 print_enum_definition(declaration);
1214                 fputc(';', out);
1215                 break;
1216         }
1217 }
1218
1219 /**
1220  * Print the AST of a translation unit.
1221  *
1222  * @param unit   the translation unit
1223  */
1224 void print_ast(const translation_unit_t *unit)
1225 {
1226         inc_type_visited();
1227
1228         declaration_t *declaration = unit->scope.declarations;
1229         for( ; declaration != NULL; declaration = declaration->next) {
1230                 if(declaration->storage_class == STORAGE_CLASS_ENUM_ENTRY)
1231                         continue;
1232                 if(declaration->namespc != NAMESPACE_NORMAL &&
1233                                 declaration->symbol == NULL)
1234                         continue;
1235
1236                 print_indent();
1237                 print_declaration(declaration);
1238                 fputc('\n', out);
1239         }
1240 }
1241
1242 /**
1243  * Returns true if a given expression is a compile time
1244  * constant.
1245  *
1246  * @param expression  the expression to check
1247  */
1248 bool is_constant_expression(const expression_t *expression)
1249 {
1250         switch(expression->kind) {
1251
1252         case EXPR_CONST:
1253         case EXPR_CHAR_CONST:
1254         case EXPR_STRING_LITERAL:
1255         case EXPR_WIDE_STRING_LITERAL:
1256         case EXPR_SIZEOF:
1257         case EXPR_CLASSIFY_TYPE:
1258         case EXPR_FUNCTION:
1259         case EXPR_PRETTY_FUNCTION:
1260         case EXPR_OFFSETOF:
1261         case EXPR_ALIGNOF:
1262         case EXPR_BUILTIN_CONSTANT_P:
1263                 return true;
1264
1265         case EXPR_BUILTIN_SYMBOL:
1266         case EXPR_BUILTIN_PREFETCH:
1267         case EXPR_CALL:
1268         case EXPR_SELECT:
1269         case EXPR_VA_START:
1270         case EXPR_VA_ARG:
1271         case EXPR_STATEMENT:
1272         case EXPR_UNARY_POSTFIX_INCREMENT:
1273         case EXPR_UNARY_POSTFIX_DECREMENT:
1274         case EXPR_UNARY_PREFIX_INCREMENT:
1275         case EXPR_UNARY_PREFIX_DECREMENT:
1276         case EXPR_UNARY_BITFIELD_EXTRACT:
1277         case EXPR_UNARY_ASSUME: /* has VOID type */
1278         case EXPR_BINARY_ASSIGN:
1279         case EXPR_BINARY_MUL_ASSIGN:
1280         case EXPR_BINARY_DIV_ASSIGN:
1281         case EXPR_BINARY_MOD_ASSIGN:
1282         case EXPR_BINARY_ADD_ASSIGN:
1283         case EXPR_BINARY_SUB_ASSIGN:
1284         case EXPR_BINARY_SHIFTLEFT_ASSIGN:
1285         case EXPR_BINARY_SHIFTRIGHT_ASSIGN:
1286         case EXPR_BINARY_BITWISE_AND_ASSIGN:
1287         case EXPR_BINARY_BITWISE_XOR_ASSIGN:
1288         case EXPR_BINARY_BITWISE_OR_ASSIGN:
1289         case EXPR_BINARY_COMMA:
1290                 return false;
1291
1292         case EXPR_UNARY_NEGATE:
1293         case EXPR_UNARY_PLUS:
1294         case EXPR_UNARY_BITWISE_NEGATE:
1295         case EXPR_UNARY_NOT:
1296         case EXPR_UNARY_DEREFERENCE:
1297         case EXPR_UNARY_TAKE_ADDRESS:
1298         case EXPR_UNARY_CAST:
1299         case EXPR_UNARY_CAST_IMPLICIT:
1300                 return is_constant_expression(expression->unary.value);
1301
1302         case EXPR_BINARY_ADD:
1303         case EXPR_BINARY_SUB:
1304         case EXPR_BINARY_MUL:
1305         case EXPR_BINARY_DIV:
1306         case EXPR_BINARY_MOD:
1307         case EXPR_BINARY_EQUAL:
1308         case EXPR_BINARY_NOTEQUAL:
1309         case EXPR_BINARY_LESS:
1310         case EXPR_BINARY_LESSEQUAL:
1311         case EXPR_BINARY_GREATER:
1312         case EXPR_BINARY_GREATEREQUAL:
1313         case EXPR_BINARY_BITWISE_AND:
1314         case EXPR_BINARY_BITWISE_OR:
1315         case EXPR_BINARY_BITWISE_XOR:
1316         case EXPR_BINARY_LOGICAL_AND:
1317         case EXPR_BINARY_LOGICAL_OR:
1318         case EXPR_BINARY_SHIFTLEFT:
1319         case EXPR_BINARY_SHIFTRIGHT:
1320         case EXPR_BINARY_BUILTIN_EXPECT:
1321         case EXPR_BINARY_ISGREATER:
1322         case EXPR_BINARY_ISGREATEREQUAL:
1323         case EXPR_BINARY_ISLESS:
1324         case EXPR_BINARY_ISLESSEQUAL:
1325         case EXPR_BINARY_ISLESSGREATER:
1326         case EXPR_BINARY_ISUNORDERED:
1327                 return is_constant_expression(expression->binary.left)
1328                         && is_constant_expression(expression->binary.right);
1329
1330         case EXPR_CONDITIONAL:
1331                 /* TODO: not correct, we only have to test expressions which are
1332                  * evaluated, which means either the true or false part might be not
1333                  * constant */
1334                 return is_constant_expression(expression->conditional.condition)
1335                         && is_constant_expression(expression->conditional.true_expression)
1336                         && is_constant_expression(expression->conditional.false_expression);
1337
1338         case EXPR_ARRAY_ACCESS:
1339                 return is_constant_expression(expression->array_access.array_ref)
1340                         && is_constant_expression(expression->array_access.index);
1341
1342         case EXPR_REFERENCE: {
1343                 declaration_t *declaration = expression->reference.declaration;
1344                 if(declaration->storage_class == STORAGE_CLASS_ENUM_ENTRY)
1345                         return true;
1346
1347                 return false;
1348         }
1349
1350         case EXPR_UNKNOWN:
1351         case EXPR_INVALID:
1352                 break;
1353         }
1354         panic("invalid expression found (is constant expression)");
1355 }
1356
1357 /**
1358  * Initialize the AST construction.
1359  */
1360 void init_ast(void)
1361 {
1362         obstack_init(&ast_obstack);
1363 }
1364
1365 /**
1366  * Free the AST.
1367  */
1368 void exit_ast(void)
1369 {
1370         obstack_free(&ast_obstack, NULL);
1371 }
1372
1373 /**
1374  * Set the output stream for the AST printer.
1375  *
1376  * @param stream  the output stream
1377  */
1378 void ast_set_output(FILE *stream)
1379 {
1380         out = stream;
1381         type_set_output(stream);
1382 }
1383
1384 /**
1385  * Allocate an AST object of the given size.
1386  *
1387  * @param size  the size of the object to allocate
1388  *
1389  * @return  A new allocated object in the AST memeory space.
1390  */
1391 void *(allocate_ast)(size_t size)
1392 {
1393         return _allocate_ast(size);
1394 }