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