11 #include "adt/error.h"
13 struct obstack ast_obstack;
18 /** If set, implicit casts are printed. */
19 bool print_implicit_casts = false;
21 /** If set parenthesis are printed to indicate operator precedence. */
22 bool print_parenthesis = false;
24 static void print_statement(const statement_t *statement);
25 static void print_expression_prec(const expression_t *expression, unsigned prec);
27 void change_indent(int delta)
33 void print_indent(void)
35 for(int i = 0; i < indent; ++i)
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 */
61 * Returns 1 if a given precedence level has right-to-left
62 * associativity, else -1.
64 * @param precedence the operator precedence
66 static int right_to_left(unsigned precedence) {
67 return (precedence == PREC_ASSIGN || precedence == PREC_COND ||
68 precedence == PREC_UNARY) ? 1 : -1;
72 * Return the precedence of an expression given by its kind.
74 * @param kind the expression kind
76 static unsigned get_expression_precedence(expression_kind_t kind)
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_COMPOUND_LITERAL] = PREC_UNARY,
87 [EXPR_CALL] = PREC_PRIM,
88 [EXPR_CONDITIONAL] = PREC_COND,
89 [EXPR_SELECT] = PREC_ACCESS,
90 [EXPR_ARRAY_ACCESS] = PREC_ACCESS,
91 [EXPR_SIZEOF] = PREC_UNARY,
92 [EXPR_CLASSIFY_TYPE] = PREC_UNARY,
93 [EXPR_ALIGNOF] = PREC_UNARY,
95 [EXPR_FUNCTION] = PREC_PRIM,
96 [EXPR_PRETTY_FUNCTION] = PREC_PRIM,
97 [EXPR_BUILTIN_SYMBOL] = PREC_PRIM,
98 [EXPR_BUILTIN_CONSTANT_P] = PREC_PRIM,
99 [EXPR_BUILTIN_PREFETCH] = PREC_PRIM,
100 [EXPR_OFFSETOF] = PREC_PRIM,
101 [EXPR_VA_START] = PREC_PRIM,
102 [EXPR_VA_ARG] = PREC_PRIM,
103 [EXPR_STATEMENT] = PREC_ACCESS,
105 [EXPR_UNARY_NEGATE] = PREC_UNARY,
106 [EXPR_UNARY_PLUS] = PREC_UNARY,
107 [EXPR_UNARY_BITWISE_NEGATE] = PREC_UNARY,
108 [EXPR_UNARY_NOT] = PREC_UNARY,
109 [EXPR_UNARY_DEREFERENCE] = PREC_UNARY,
110 [EXPR_UNARY_TAKE_ADDRESS] = PREC_UNARY,
111 [EXPR_UNARY_POSTFIX_INCREMENT] = PREC_UNARY,
112 [EXPR_UNARY_POSTFIX_DECREMENT] = PREC_UNARY,
113 [EXPR_UNARY_PREFIX_INCREMENT] = PREC_UNARY,
114 [EXPR_UNARY_PREFIX_DECREMENT] = PREC_UNARY,
115 [EXPR_UNARY_CAST] = PREC_UNARY,
116 [EXPR_UNARY_CAST_IMPLICIT] = PREC_UNARY,
117 [EXPR_UNARY_ASSUME] = PREC_PRIM,
118 [EXPR_UNARY_BITFIELD_EXTRACT] = PREC_ACCESS,
120 [EXPR_BINARY_ADD] = PREC_PLUS,
121 [EXPR_BINARY_SUB] = PREC_PLUS,
122 [EXPR_BINARY_MUL] = PREC_MUL,
123 [EXPR_BINARY_DIV] = PREC_MUL,
124 [EXPR_BINARY_MOD] = PREC_MUL,
125 [EXPR_BINARY_EQUAL] = PREC_EQ,
126 [EXPR_BINARY_NOTEQUAL] = PREC_EQ,
127 [EXPR_BINARY_LESS] = PREC_CMP,
128 [EXPR_BINARY_LESSEQUAL] = PREC_CMP,
129 [EXPR_BINARY_GREATER] = PREC_CMP,
130 [EXPR_BINARY_GREATEREQUAL] = PREC_CMP,
131 [EXPR_BINARY_BITWISE_AND] = PREC_BIT_AND,
132 [EXPR_BINARY_BITWISE_OR] = PREC_BIT_OR,
133 [EXPR_BINARY_BITWISE_XOR] = PREC_BIT_XOR,
134 [EXPR_BINARY_LOGICAL_AND] = PREC_LOG_AND,
135 [EXPR_BINARY_LOGICAL_OR] = PREC_LOG_OR,
136 [EXPR_BINARY_SHIFTLEFT] = PREC_SHF,
137 [EXPR_BINARY_SHIFTRIGHT] = PREC_SHF,
138 [EXPR_BINARY_ASSIGN] = PREC_ASSIGN,
139 [EXPR_BINARY_MUL_ASSIGN] = PREC_ASSIGN,
140 [EXPR_BINARY_DIV_ASSIGN] = PREC_ASSIGN,
141 [EXPR_BINARY_MOD_ASSIGN] = PREC_ASSIGN,
142 [EXPR_BINARY_ADD_ASSIGN] = PREC_ASSIGN,
143 [EXPR_BINARY_SUB_ASSIGN] = PREC_ASSIGN,
144 [EXPR_BINARY_SHIFTLEFT_ASSIGN] = PREC_ASSIGN,
145 [EXPR_BINARY_SHIFTRIGHT_ASSIGN] = PREC_ASSIGN,
146 [EXPR_BINARY_BITWISE_AND_ASSIGN] = PREC_ASSIGN,
147 [EXPR_BINARY_BITWISE_XOR_ASSIGN] = PREC_ASSIGN,
148 [EXPR_BINARY_BITWISE_OR_ASSIGN] = PREC_ASSIGN,
149 [EXPR_BINARY_COMMA] = PREC_COMMA,
151 [EXPR_BINARY_BUILTIN_EXPECT] = PREC_PRIM,
152 [EXPR_BINARY_ISGREATER] = PREC_PRIM,
153 [EXPR_BINARY_ISGREATEREQUAL] = PREC_PRIM,
154 [EXPR_BINARY_ISLESS] = PREC_PRIM,
155 [EXPR_BINARY_ISLESSEQUAL] = PREC_PRIM,
156 [EXPR_BINARY_ISLESSGREATER] = PREC_PRIM,
157 [EXPR_BINARY_ISUNORDERED] = PREC_PRIM
159 assert((unsigned)kind < (sizeof(prec)/sizeof(prec[0])));
160 unsigned res = prec[kind];
162 assert(res != PREC_BOTTOM);
167 * Print a constant expression.
169 * @param cnst the constant expression
171 static void print_const(const const_expression_t *cnst)
173 if(cnst->base.type == NULL)
176 const type_t *const type = skip_typeref(cnst->base.type);
178 if (is_type_integer(type)) {
179 fprintf(out, "%lld", cnst->v.int_value);
180 } else if (is_type_float(type)) {
181 fprintf(out, "%Lf", cnst->v.float_value);
183 panic("unknown constant");
188 * Print a quoted string constant.
190 * @param string the string constant
191 * @param border the border char
193 static void print_quoted_string(const string_t *const string, char border)
196 const char *end = string->begin + string->size;
197 for (const char *c = string->begin; c != end; ++c) {
202 case '\\': fputs("\\\\", out); break;
203 case '\a': fputs("\\a", out); break;
204 case '\b': fputs("\\b", out); break;
205 case '\f': fputs("\\f", out); break;
206 case '\n': fputs("\\n", out); break;
207 case '\r': fputs("\\r", out); break;
208 case '\t': fputs("\\t", out); break;
209 case '\v': fputs("\\v", out); break;
210 case '\?': fputs("\\?", out); break;
213 fprintf(out, "\\%03o", *c);
224 * Print a constant character expression.
226 * @param cnst the constant character expression
228 static void print_char_const(const const_expression_t *cnst)
230 print_quoted_string(&cnst->v.chars, '\'');
234 * Prints a string literal expression.
236 * @param string_literal the string literal expression
238 static void print_string_literal(
239 const string_literal_expression_t *string_literal)
241 print_quoted_string(&string_literal->value, '"');
245 * Prints a wide string literal expression.
247 * @param wstr the wide string literal expression
249 static void print_quoted_wide_string(const wide_string_t *const wstr)
252 for (const wchar_rep_t *c = wstr->begin, *end = wstr->begin + wstr->size;
255 case L'\"': fputs("\\\"", out); break;
256 case L'\\': fputs("\\\\", out); break;
257 case L'\a': fputs("\\a", out); break;
258 case L'\b': fputs("\\b", out); break;
259 case L'\f': fputs("\\f", out); break;
260 case L'\n': fputs("\\n", out); break;
261 case L'\r': fputs("\\r", out); break;
262 case L'\t': fputs("\\t", out); break;
263 case L'\v': fputs("\\v", out); break;
264 case L'\?': fputs("\\?", out); break;
266 const unsigned tc = *c;
269 fprintf(out, "\\%03o", (char)*c);
273 } else if (tc < 0x800) {
274 fputc(0xC0 | (tc >> 6), out);
275 fputc(0x80 | (tc & 0x3F), out);
276 } else if (tc < 0x10000) {
277 fputc(0xE0 | ( tc >> 12), out);
278 fputc(0x80 | ((tc >> 6) & 0x3F), out);
279 fputc(0x80 | ( tc & 0x3F), out);
281 fputc(0xF0 | ( tc >> 18), out);
282 fputc(0x80 | ((tc >> 12) & 0x3F), out);
283 fputc(0x80 | ((tc >> 6) & 0x3F), out);
284 fputc(0x80 | ( tc & 0x3F), out);
292 static void print_wide_string_literal(
293 const wide_string_literal_expression_t *const wstr)
295 print_quoted_wide_string(&wstr->value);
298 static void print_compound_literal(
299 const compound_literal_expression_t *expression)
302 print_type(expression->type);
304 print_initializer(expression->initializer);
308 * Prints a call expression.
310 * @param call the call expression
312 static void print_call_expression(const call_expression_t *call)
314 unsigned prec = get_expression_precedence(call->base.kind);
315 print_expression_prec(call->function, prec);
317 call_argument_t *argument = call->arguments;
319 while(argument != NULL) {
325 print_expression_prec(argument->expression, PREC_COMMA + 1);
327 argument = argument->next;
333 * Prints a binary expression.
335 * @param binexpr the binary expression
337 static void print_binary_expression(const binary_expression_t *binexpr)
339 unsigned prec = get_expression_precedence(binexpr->base.kind);
340 int r2l = right_to_left(prec);
342 if(binexpr->base.kind == EXPR_BINARY_BUILTIN_EXPECT) {
343 fputs("__builtin_expect(", out);
344 print_expression_prec(binexpr->left, prec);
346 print_expression_prec(binexpr->right, prec);
351 print_expression_prec(binexpr->left, prec + r2l);
352 if (binexpr->base.kind != EXPR_BINARY_COMMA) {
355 switch (binexpr->base.kind) {
356 case EXPR_BINARY_COMMA: fputs(",", out); break;
357 case EXPR_BINARY_ASSIGN: fputs("=", out); break;
358 case EXPR_BINARY_ADD: fputs("+", out); break;
359 case EXPR_BINARY_SUB: fputs("-", out); break;
360 case EXPR_BINARY_MUL: fputs("*", out); break;
361 case EXPR_BINARY_MOD: fputs("%", out); break;
362 case EXPR_BINARY_DIV: fputs("/", out); break;
363 case EXPR_BINARY_BITWISE_OR: fputs("|", out); break;
364 case EXPR_BINARY_BITWISE_AND: fputs("&", out); break;
365 case EXPR_BINARY_BITWISE_XOR: fputs("^", out); break;
366 case EXPR_BINARY_LOGICAL_OR: fputs("||", out); break;
367 case EXPR_BINARY_LOGICAL_AND: fputs("&&", out); break;
368 case EXPR_BINARY_NOTEQUAL: fputs("!=", out); break;
369 case EXPR_BINARY_EQUAL: fputs("==", out); break;
370 case EXPR_BINARY_LESS: fputs("<", out); break;
371 case EXPR_BINARY_LESSEQUAL: fputs("<=", out); break;
372 case EXPR_BINARY_GREATER: fputs(">", out); break;
373 case EXPR_BINARY_GREATEREQUAL: fputs(">=", out); break;
374 case EXPR_BINARY_SHIFTLEFT: fputs("<<", out); break;
375 case EXPR_BINARY_SHIFTRIGHT: fputs(">>", out); break;
377 case EXPR_BINARY_ADD_ASSIGN: fputs("+=", out); break;
378 case EXPR_BINARY_SUB_ASSIGN: fputs("-=", out); break;
379 case EXPR_BINARY_MUL_ASSIGN: fputs("*=", out); break;
380 case EXPR_BINARY_MOD_ASSIGN: fputs("%=", out); break;
381 case EXPR_BINARY_DIV_ASSIGN: fputs("/=", out); break;
382 case EXPR_BINARY_BITWISE_OR_ASSIGN: fputs("|=", out); break;
383 case EXPR_BINARY_BITWISE_AND_ASSIGN: fputs("&=", out); break;
384 case EXPR_BINARY_BITWISE_XOR_ASSIGN: fputs("^=", out); break;
385 case EXPR_BINARY_SHIFTLEFT_ASSIGN: fputs("<<=", out); break;
386 case EXPR_BINARY_SHIFTRIGHT_ASSIGN: fputs(">>=", out); break;
387 default: panic("invalid binexpression found");
390 print_expression_prec(binexpr->right, prec - r2l);
394 * Prints an unary expression.
396 * @param unexpr the unary expression
398 static void print_unary_expression(const unary_expression_t *unexpr)
400 unsigned prec = get_expression_precedence(unexpr->base.kind);
401 switch(unexpr->base.kind) {
402 case EXPR_UNARY_NEGATE: fputs("-", out); break;
403 case EXPR_UNARY_PLUS: fputs("+", out); break;
404 case EXPR_UNARY_NOT: fputs("!", out); break;
405 case EXPR_UNARY_BITWISE_NEGATE: fputs("~", out); break;
406 case EXPR_UNARY_PREFIX_INCREMENT: fputs("++", out); break;
407 case EXPR_UNARY_PREFIX_DECREMENT: fputs("--", out); break;
408 case EXPR_UNARY_DEREFERENCE: fputs("*", out); break;
409 case EXPR_UNARY_TAKE_ADDRESS: fputs("&", out); break;
411 case EXPR_UNARY_BITFIELD_EXTRACT:
412 print_expression_prec(unexpr->value, prec);
415 case EXPR_UNARY_POSTFIX_INCREMENT:
416 print_expression_prec(unexpr->value, prec);
419 case EXPR_UNARY_POSTFIX_DECREMENT:
420 print_expression_prec(unexpr->value, prec);
423 case EXPR_UNARY_CAST_IMPLICIT:
424 if(!print_implicit_casts) {
425 print_expression_prec(unexpr->value, prec);
429 case EXPR_UNARY_CAST:
431 print_type(unexpr->base.type);
434 case EXPR_UNARY_ASSUME:
435 fputs("__assume(", out);
436 print_expression_prec(unexpr->value, PREC_COMMA + 1);
440 panic("invalid unary expression found");
442 print_expression_prec(unexpr->value, prec);
446 * Prints a reference expression.
448 * @param ref the reference expression
450 static void print_reference_expression(const reference_expression_t *ref)
452 fprintf(out, "%s", ref->declaration->symbol->string);
456 * Prints an array expression.
458 * @param expression the array expression
460 static void print_array_expression(const array_access_expression_t *expression)
462 unsigned prec = get_expression_precedence(expression->base.kind);
463 if(!expression->flipped) {
464 print_expression_prec(expression->array_ref, prec);
466 print_expression_prec(expression->index, prec);
469 print_expression_prec(expression->index, prec);
471 print_expression_prec(expression->array_ref, prec);
477 * Prints a typeproperty expression (sizeof or __alignof__).
479 * @param expression the type property expression
481 static void print_typeprop_expression(const typeprop_expression_t *expression)
483 if (expression->base.kind == EXPR_SIZEOF) {
484 fputs("sizeof", out);
486 assert(expression->base.kind == EXPR_ALIGNOF);
487 fputs("__alignof__", out);
489 if(expression->tp_expression != NULL) {
490 /* always print the '()' here, sizeof x is right but unusual */
492 print_expression_prec(expression->tp_expression, PREC_ACCESS);
496 print_type(expression->type);
502 * Prints an builtin symbol.
504 * @param expression the builtin symbol expression
506 static void print_builtin_symbol(const builtin_symbol_expression_t *expression)
508 fputs(expression->symbol->string, out);
512 * Prints a builtin constant expression.
514 * @param expression the builtin constant expression
516 static void print_builtin_constant(const builtin_constant_expression_t *expression)
518 fputs("__builtin_constant_p(", out);
519 print_expression_prec(expression->value, PREC_COMMA + 1);
524 * Prints a builtin prefetch expression.
526 * @param expression the builtin prefetch expression
528 static void print_builtin_prefetch(const builtin_prefetch_expression_t *expression)
530 fputs("__builtin_prefetch(", out);
531 print_expression_prec(expression->adr, PREC_COMMA + 1);
532 if (expression->rw) {
534 print_expression_prec(expression->rw, PREC_COMMA + 1);
536 if (expression->locality) {
538 print_expression_prec(expression->locality, PREC_COMMA + 1);
544 * Prints a conditional expression.
546 * @param expression the conditional expression
548 static void print_conditional(const conditional_expression_t *expression)
550 unsigned prec = get_expression_precedence(expression->base.kind);
552 print_expression_prec(expression->condition, prec);
554 print_expression_prec(expression->true_expression, prec);
556 print_expression_prec(expression->false_expression, prec);
561 * Prints a va_start expression.
563 * @param expression the va_start expression
565 static void print_va_start(const va_start_expression_t *const expression)
567 fputs("__builtin_va_start(", out);
568 print_expression_prec(expression->ap, PREC_COMMA + 1);
570 fputs(expression->parameter->symbol->string, out);
575 * Prints a va_arg expression.
577 * @param expression the va_arg expression
579 static void print_va_arg(const va_arg_expression_t *expression)
581 fputs("__builtin_va_arg(", out);
582 print_expression_prec(expression->ap, PREC_COMMA + 1);
584 print_type(expression->base.type);
589 * Prints a select expression (. or ->).
591 * @param expression the select expression
593 static void print_select(const select_expression_t *expression)
595 unsigned prec = get_expression_precedence(expression->base.kind);
596 print_expression_prec(expression->compound, prec);
597 if(is_type_pointer(expression->compound->base.type)) {
602 fputs(expression->symbol->string, out);
606 * Prints a type classify expression.
608 * @param expr the type classify expression
610 static void print_classify_type_expression(
611 const classify_type_expression_t *const expr)
613 fputs("__builtin_classify_type(", out);
614 print_expression_prec(expr->type_expression, PREC_COMMA + 1);
619 * Prints a designator.
621 * @param designator the designator
623 static void print_designator(const designator_t *designator)
625 for ( ; designator != NULL; designator = designator->next) {
626 if (designator->symbol == NULL) {
628 print_expression_prec(designator->array_index, PREC_ACCESS);
632 fputs(designator->symbol->string, out);
638 * Prints an offsetof expression.
640 * @param expression the offset expression
642 static void print_offsetof_expression(const offsetof_expression_t *expression)
644 fputs("__builtin_offsetof", out);
646 print_type(expression->type);
648 print_designator(expression->designator);
653 * Prints a statement expression.
655 * @param expression the statement expression
657 static void print_statement_expression(const statement_expression_t *expression)
660 print_statement(expression->statement);
665 * Prints an expression with parenthesis if needed.
667 * @param expression the expression to print
668 * @param top_prec the precedence of the user of this expression.
670 static void print_expression_prec(const expression_t *expression, unsigned top_prec)
672 unsigned prec = get_expression_precedence(expression->base.kind);
673 if (print_parenthesis && top_prec != PREC_BOTTOM)
677 switch(expression->kind) {
680 fprintf(out, "*invalid expression*");
682 case EXPR_CHAR_CONST:
683 print_char_const(&expression->conste);
686 print_const(&expression->conste);
689 case EXPR_PRETTY_FUNCTION:
690 case EXPR_STRING_LITERAL:
691 print_string_literal(&expression->string);
693 case EXPR_WIDE_STRING_LITERAL:
694 print_wide_string_literal(&expression->wide_string);
696 case EXPR_COMPOUND_LITERAL:
697 print_compound_literal(&expression->compound_literal);
700 print_call_expression(&expression->call);
703 print_binary_expression(&expression->binary);
706 print_reference_expression(&expression->reference);
708 case EXPR_ARRAY_ACCESS:
709 print_array_expression(&expression->array_access);
712 print_unary_expression(&expression->unary);
716 print_typeprop_expression(&expression->typeprop);
718 case EXPR_BUILTIN_SYMBOL:
719 print_builtin_symbol(&expression->builtin_symbol);
721 case EXPR_BUILTIN_CONSTANT_P:
722 print_builtin_constant(&expression->builtin_constant);
724 case EXPR_BUILTIN_PREFETCH:
725 print_builtin_prefetch(&expression->builtin_prefetch);
727 case EXPR_CONDITIONAL:
728 print_conditional(&expression->conditional);
731 print_va_start(&expression->va_starte);
734 print_va_arg(&expression->va_arge);
737 print_select(&expression->select);
739 case EXPR_CLASSIFY_TYPE:
740 print_classify_type_expression(&expression->classify_type);
743 print_offsetof_expression(&expression->offsetofe);
746 print_statement_expression(&expression->statement);
751 fprintf(out, "some expression of type %d", (int) expression->kind);
759 * Print an compound statement.
761 * @param block the compound statement
763 static void print_compound_statement(const compound_statement_t *block)
768 statement_t *statement = block->statements;
769 while(statement != NULL) {
770 if (statement->base.kind == STATEMENT_CASE_LABEL)
773 print_statement(statement);
775 statement = statement->base.next;
783 * Print a return statement.
785 * @param statement the return statement
787 static void print_return_statement(const return_statement_t *statement)
789 fprintf(out, "return ");
790 if(statement->value != NULL)
791 print_expression(statement->value);
796 * Print an expression statement.
798 * @param statement the expression statement
800 static void print_expression_statement(const expression_statement_t *statement)
802 print_expression(statement->expression);
807 * Print a goto statement.
809 * @param statement the goto statement
811 static void print_goto_statement(const goto_statement_t *statement)
813 fprintf(out, "goto ");
814 fputs(statement->label->symbol->string, out);
815 fprintf(stderr, "(%p)", (void*) statement->label);
820 * Print a label statement.
822 * @param statement the label statement
824 static void print_label_statement(const label_statement_t *statement)
826 fprintf(stderr, "(%p)", (void*) statement->label);
827 fprintf(out, "%s:\n", statement->label->symbol->string);
828 if(statement->statement != NULL) {
829 print_statement(statement->statement);
834 * Print an if statement.
836 * @param statement the if statement
838 static void print_if_statement(const if_statement_t *statement)
841 print_expression(statement->condition);
843 if(statement->true_statement != NULL) {
844 print_statement(statement->true_statement);
847 if(statement->false_statement != NULL) {
850 print_statement(statement->false_statement);
855 * Print a switch statement.
857 * @param statement the switch statement
859 static void print_switch_statement(const switch_statement_t *statement)
861 fputs("switch(", out);
862 print_expression(statement->expression);
864 print_statement(statement->body);
868 * Print a case label (including the default label).
870 * @param statement the case label statement
872 static void print_case_label(const case_label_statement_t *statement)
874 if(statement->expression == NULL) {
875 fputs("default:\n", out);
878 print_expression(statement->expression);
879 if (statement->end_range != NULL) {
881 print_expression(statement->end_range);
886 if(statement->statement != NULL) {
887 if (statement->statement->base.kind == STATEMENT_CASE_LABEL) {
891 print_statement(statement->statement);
896 * Print a declaration statement.
898 * @param statement the statement
900 static void print_declaration_statement(
901 const declaration_statement_t *statement)
904 declaration_t *declaration = statement->declarations_begin;
905 for( ; declaration != statement->declarations_end->next;
906 declaration = declaration->next) {
912 print_declaration(declaration);
918 * Print a while statement.
920 * @param statement the statement
922 static void print_while_statement(const while_statement_t *statement)
924 fputs("while(", out);
925 print_expression(statement->condition);
927 print_statement(statement->body);
931 * Print a do-while statement.
933 * @param statement the statement
935 static void print_do_while_statement(const do_while_statement_t *statement)
938 print_statement(statement->body);
940 fputs("while(", out);
941 print_expression(statement->condition);
946 * Print a for statement.
948 * @param statement the statement
950 static void print_for_statement(const for_statement_t *statement)
953 if(statement->scope.declarations != NULL) {
954 assert(statement->initialisation == NULL);
955 print_declaration(statement->scope.declarations);
956 if(statement->scope.declarations->next != NULL) {
957 panic("multiple declarations in for statement not supported yet");
961 if(statement->initialisation) {
962 print_expression(statement->initialisation);
966 if(statement->condition != NULL) {
967 print_expression(statement->condition);
970 if(statement->step != NULL) {
971 print_expression(statement->step);
974 print_statement(statement->body);
978 * Print assembler constraints.
980 * @param constraints the constraints
982 static void print_asm_constraints(asm_constraint_t *constraints)
984 asm_constraint_t *constraint = constraints;
985 for( ; constraint != NULL; constraint = constraint->next) {
986 if(constraint != constraints)
989 if(constraint->symbol) {
990 fprintf(out, "[%s] ", constraint->symbol->string);
992 print_quoted_string(&constraint->constraints, '"');
994 print_expression(constraint->expression);
1000 * Print assembler clobbers.
1002 * @param clobbers the clobbers
1004 static void print_asm_clobbers(asm_clobber_t *clobbers)
1006 asm_clobber_t *clobber = clobbers;
1007 for( ; clobber != NULL; clobber = clobber->next) {
1008 if(clobber != clobbers)
1011 print_quoted_string(&clobber->clobber, '"');
1016 * Print an assembler statement.
1018 * @param statement the statement
1020 static void print_asm_statement(const asm_statement_t *statement)
1023 if(statement->is_volatile) {
1024 fputs("volatile ", out);
1027 print_quoted_string(&statement->asm_text, '"');
1028 if(statement->inputs == NULL && statement->outputs == NULL
1029 && statement->clobbers == NULL)
1030 goto end_of_print_asm_statement;
1033 print_asm_constraints(statement->inputs);
1034 if(statement->outputs == NULL && statement->clobbers == NULL)
1035 goto end_of_print_asm_statement;
1038 print_asm_constraints(statement->outputs);
1039 if(statement->clobbers == NULL)
1040 goto end_of_print_asm_statement;
1043 print_asm_clobbers(statement->clobbers);
1045 end_of_print_asm_statement:
1050 * Print a statement.
1052 * @param statement the statement
1054 void print_statement(const statement_t *statement)
1056 switch(statement->kind) {
1057 case STATEMENT_COMPOUND:
1058 print_compound_statement(&statement->compound);
1060 case STATEMENT_RETURN:
1061 print_return_statement(&statement->returns);
1063 case STATEMENT_EXPRESSION:
1064 print_expression_statement(&statement->expression);
1066 case STATEMENT_LABEL:
1067 print_label_statement(&statement->label);
1069 case STATEMENT_GOTO:
1070 print_goto_statement(&statement->gotos);
1072 case STATEMENT_CONTINUE:
1073 fputs("continue;\n", out);
1075 case STATEMENT_BREAK:
1076 fputs("break;\n", out);
1079 print_if_statement(&statement->ifs);
1081 case STATEMENT_SWITCH:
1082 print_switch_statement(&statement->switchs);
1084 case STATEMENT_CASE_LABEL:
1085 print_case_label(&statement->case_label);
1087 case STATEMENT_DECLARATION:
1088 print_declaration_statement(&statement->declaration);
1090 case STATEMENT_WHILE:
1091 print_while_statement(&statement->whiles);
1093 case STATEMENT_DO_WHILE:
1094 print_do_while_statement(&statement->do_while);
1097 print_for_statement(&statement->fors);
1100 print_asm_statement(&statement->asms);
1102 case STATEMENT_INVALID:
1103 fprintf(out, "*invalid statement*");
1109 * Print a storage class.
1111 * @param storage_class the storage class
1113 static void print_storage_class(storage_class_tag_t storage_class)
1115 switch(storage_class) {
1116 case STORAGE_CLASS_ENUM_ENTRY:
1117 case STORAGE_CLASS_NONE:
1119 case STORAGE_CLASS_TYPEDEF: fputs("typedef ", out); break;
1120 case STORAGE_CLASS_EXTERN: fputs("extern ", out); break;
1121 case STORAGE_CLASS_STATIC: fputs("static ", out); break;
1122 case STORAGE_CLASS_AUTO: fputs("auto ", out); break;
1123 case STORAGE_CLASS_REGISTER: fputs("register ", out); break;
1124 case STORAGE_CLASS_THREAD: fputs("__thread", out); break;
1125 case STORAGE_CLASS_THREAD_EXTERN: fputs("extern __thread", out); break;
1126 case STORAGE_CLASS_THREAD_STATIC: fputs("static __thread", out); break;
1131 * Print an initializer.
1133 * @param initializer the initializer
1135 void print_initializer(const initializer_t *initializer)
1137 if(initializer == NULL) {
1138 fputs("{ NIL-INITIALIZER }", out);
1142 switch(initializer->kind) {
1143 case INITIALIZER_VALUE: {
1144 const initializer_value_t *value = &initializer->value;
1145 print_expression(value->value);
1148 case INITIALIZER_LIST: {
1149 assert(initializer->kind == INITIALIZER_LIST);
1151 const initializer_list_t *list = &initializer->list;
1153 for(size_t i = 0 ; i < list->len; ++i) {
1154 const initializer_t *sub_init = list->initializers[i];
1155 print_initializer(list->initializers[i]);
1156 if(i < list->len-1 && sub_init->kind != INITIALIZER_DESIGNATOR) {
1163 case INITIALIZER_STRING:
1164 print_quoted_string(&initializer->string.string, '"');
1166 case INITIALIZER_WIDE_STRING:
1167 print_quoted_wide_string(&initializer->wide_string.string);
1169 case INITIALIZER_DESIGNATOR:
1170 print_designator(initializer->designator.designator);
1175 panic("invalid initializer kind found");
1179 * Print a declaration in the NORMAL namespace.
1181 * @param declaration the declaration
1183 static void print_normal_declaration(const declaration_t *declaration)
1185 print_storage_class((storage_class_tag_t) declaration->declared_storage_class);
1186 if(declaration->is_inline) {
1187 if (declaration->modifiers & DM_FORCEINLINE)
1188 fputs("__forceinline ", out);
1190 fputs("inline ", out);
1192 print_type_ext(declaration->type, declaration->symbol,
1193 &declaration->scope);
1195 if(declaration->type->kind == TYPE_FUNCTION) {
1196 if(declaration->init.statement != NULL) {
1198 print_statement(declaration->init.statement);
1201 } else if(declaration->init.initializer != NULL) {
1203 print_initializer(declaration->init.initializer);
1209 * Prints an expression.
1211 * @param expression the expression
1213 void print_expression(const expression_t *expression) {
1214 print_expression_prec(expression, PREC_BOTTOM);
1218 * Print a declaration.
1220 * @param declaration the declaration
1222 void print_declaration(const declaration_t *declaration)
1224 if(declaration->namespc != NAMESPACE_NORMAL &&
1225 declaration->symbol == NULL)
1228 switch(declaration->namespc) {
1229 case NAMESPACE_NORMAL:
1230 print_normal_declaration(declaration);
1232 case NAMESPACE_STRUCT:
1233 fputs("struct ", out);
1234 fputs(declaration->symbol->string, out);
1236 print_compound_definition(declaration);
1239 case NAMESPACE_UNION:
1240 fputs("union ", out);
1241 fputs(declaration->symbol->string, out);
1243 print_compound_definition(declaration);
1246 case NAMESPACE_ENUM:
1247 fputs("enum ", out);
1248 fputs(declaration->symbol->string, out);
1250 print_enum_definition(declaration);
1257 * Print the AST of a translation unit.
1259 * @param unit the translation unit
1261 void print_ast(const translation_unit_t *unit)
1265 declaration_t *declaration = unit->scope.declarations;
1266 for( ; declaration != NULL; declaration = declaration->next) {
1267 if(declaration->storage_class == STORAGE_CLASS_ENUM_ENTRY)
1269 if(declaration->namespc != NAMESPACE_NORMAL &&
1270 declaration->symbol == NULL)
1274 print_declaration(declaration);
1279 bool is_constant_initializer(const initializer_t *initializer)
1281 switch(initializer->kind) {
1282 case INITIALIZER_STRING:
1283 case INITIALIZER_WIDE_STRING:
1284 case INITIALIZER_DESIGNATOR:
1287 case INITIALIZER_VALUE:
1288 return is_constant_expression(initializer->value.value);
1290 case INITIALIZER_LIST: {
1291 for(size_t i = 0; i < initializer->list.len; ++i) {
1292 initializer_t *sub_initializer = initializer->list.initializers[i];
1293 if(!is_constant_initializer(sub_initializer))
1299 panic("invalid initializer kind found");
1302 static bool is_object_with_constant_address(const expression_t *expression)
1304 switch(expression->kind) {
1305 case EXPR_UNARY_DEREFERENCE:
1306 return is_address_constant(expression->unary.value);
1309 if(is_type_pointer(expression->select.compound->base.type)) {
1311 return is_address_constant(expression->select.compound);
1313 return is_object_with_constant_address(expression->select.compound);
1317 case EXPR_ARRAY_ACCESS:
1318 return is_constant_expression(expression->array_access.index)
1319 && is_address_constant(expression->array_access.array_ref);
1321 case EXPR_REFERENCE: {
1322 declaration_t *declaration = expression->reference.declaration;
1323 switch((storage_class_tag_t) declaration->storage_class) {
1324 case STORAGE_CLASS_NONE:
1325 case STORAGE_CLASS_EXTERN:
1326 case STORAGE_CLASS_STATIC:
1338 bool is_address_constant(const expression_t *expression)
1340 switch(expression->kind) {
1341 case EXPR_UNARY_TAKE_ADDRESS:
1342 return is_object_with_constant_address(expression->unary.value);
1344 case EXPR_UNARY_CAST:
1345 return is_type_pointer(skip_typeref(expression->base.type))
1346 && (is_constant_expression(expression->unary.value)
1347 || is_address_constant(expression->unary.value));
1349 case EXPR_BINARY_ADD:
1350 case EXPR_BINARY_SUB: {
1351 expression_t *left = expression->binary.left;
1352 expression_t *right = expression->binary.right;
1354 if(is_type_pointer(skip_typeref(left->base.type))) {
1355 return is_address_constant(left) && is_constant_expression(right);
1356 } else if(is_type_pointer(skip_typeref(right->base.type))) {
1357 return is_constant_expression(left) && is_address_constant(right);
1363 case EXPR_REFERENCE: {
1364 declaration_t *declaration = expression->reference.declaration;
1365 type_t *type = skip_typeref(declaration->type);
1366 if(is_type_function(type))
1368 if(is_type_array(type)) {
1369 return is_object_with_constant_address(expression);
1379 bool is_constant_expression(const expression_t *expression)
1381 switch(expression->kind) {
1384 case EXPR_CHAR_CONST:
1385 case EXPR_STRING_LITERAL:
1386 case EXPR_WIDE_STRING_LITERAL:
1388 case EXPR_CLASSIFY_TYPE:
1390 case EXPR_PRETTY_FUNCTION:
1393 case EXPR_BUILTIN_CONSTANT_P:
1396 case EXPR_BUILTIN_SYMBOL:
1397 case EXPR_BUILTIN_PREFETCH:
1402 case EXPR_STATEMENT:
1403 case EXPR_UNARY_POSTFIX_INCREMENT:
1404 case EXPR_UNARY_POSTFIX_DECREMENT:
1405 case EXPR_UNARY_PREFIX_INCREMENT:
1406 case EXPR_UNARY_PREFIX_DECREMENT:
1407 case EXPR_UNARY_BITFIELD_EXTRACT:
1408 case EXPR_UNARY_ASSUME: /* has VOID type */
1409 case EXPR_UNARY_DEREFERENCE:
1410 case EXPR_UNARY_TAKE_ADDRESS:
1411 case EXPR_BINARY_ASSIGN:
1412 case EXPR_BINARY_MUL_ASSIGN:
1413 case EXPR_BINARY_DIV_ASSIGN:
1414 case EXPR_BINARY_MOD_ASSIGN:
1415 case EXPR_BINARY_ADD_ASSIGN:
1416 case EXPR_BINARY_SUB_ASSIGN:
1417 case EXPR_BINARY_SHIFTLEFT_ASSIGN:
1418 case EXPR_BINARY_SHIFTRIGHT_ASSIGN:
1419 case EXPR_BINARY_BITWISE_AND_ASSIGN:
1420 case EXPR_BINARY_BITWISE_XOR_ASSIGN:
1421 case EXPR_BINARY_BITWISE_OR_ASSIGN:
1422 case EXPR_BINARY_COMMA:
1425 case EXPR_UNARY_NEGATE:
1426 case EXPR_UNARY_PLUS:
1427 case EXPR_UNARY_BITWISE_NEGATE:
1428 case EXPR_UNARY_NOT:
1429 return is_constant_expression(expression->unary.value);
1431 case EXPR_UNARY_CAST:
1432 case EXPR_UNARY_CAST_IMPLICIT:
1433 return is_type_arithmetic(skip_typeref(expression->base.type))
1434 && is_constant_expression(expression->unary.value);
1436 case EXPR_BINARY_ADD:
1437 case EXPR_BINARY_SUB:
1438 case EXPR_BINARY_MUL:
1439 case EXPR_BINARY_DIV:
1440 case EXPR_BINARY_MOD:
1441 case EXPR_BINARY_EQUAL:
1442 case EXPR_BINARY_NOTEQUAL:
1443 case EXPR_BINARY_LESS:
1444 case EXPR_BINARY_LESSEQUAL:
1445 case EXPR_BINARY_GREATER:
1446 case EXPR_BINARY_GREATEREQUAL:
1447 case EXPR_BINARY_BITWISE_AND:
1448 case EXPR_BINARY_BITWISE_OR:
1449 case EXPR_BINARY_BITWISE_XOR:
1450 case EXPR_BINARY_LOGICAL_AND:
1451 case EXPR_BINARY_LOGICAL_OR:
1452 case EXPR_BINARY_SHIFTLEFT:
1453 case EXPR_BINARY_SHIFTRIGHT:
1454 case EXPR_BINARY_BUILTIN_EXPECT:
1455 case EXPR_BINARY_ISGREATER:
1456 case EXPR_BINARY_ISGREATEREQUAL:
1457 case EXPR_BINARY_ISLESS:
1458 case EXPR_BINARY_ISLESSEQUAL:
1459 case EXPR_BINARY_ISLESSGREATER:
1460 case EXPR_BINARY_ISUNORDERED:
1461 return is_constant_expression(expression->binary.left)
1462 && is_constant_expression(expression->binary.right);
1464 case EXPR_COMPOUND_LITERAL:
1465 return is_constant_initializer(expression->compound_literal.initializer);
1467 case EXPR_CONDITIONAL:
1468 /* TODO: not correct, we only have to test expressions which are
1469 * evaluated, which means either the true or false part might be not
1471 return is_constant_expression(expression->conditional.condition)
1472 && is_constant_expression(expression->conditional.true_expression)
1473 && is_constant_expression(expression->conditional.false_expression);
1475 case EXPR_ARRAY_ACCESS:
1476 return is_constant_expression(expression->array_access.array_ref)
1477 && is_constant_expression(expression->array_access.index);
1479 case EXPR_REFERENCE: {
1480 declaration_t *declaration = expression->reference.declaration;
1481 if(declaration->storage_class == STORAGE_CLASS_ENUM_ENTRY)
1491 panic("invalid expression found (is constant expression)");
1495 * Initialize the AST construction.
1499 obstack_init(&ast_obstack);
1507 obstack_free(&ast_obstack, NULL);
1511 * Set the output stream for the AST printer.
1513 * @param stream the output stream
1515 void ast_set_output(FILE *stream)
1518 type_set_output(stream);
1522 * Allocate an AST object of the given size.
1524 * @param size the size of the object to allocate
1526 * @return A new allocated object in the AST memeory space.
1528 void *(allocate_ast)(size_t size)
1530 return _allocate_ast(size);