2 * This file is part of cparser.
3 * Copyright (C) 2007-2008 Matthias Braun <matze@braunis.de>
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
32 #include "adt/error.h"
34 struct obstack ast_obstack;
39 /** If set, implicit casts are printed. */
40 bool print_implicit_casts = false;
42 /** If set parenthesis are printed to indicate operator precedence. */
43 bool print_parenthesis = false;
45 static void print_statement(const statement_t *statement);
46 static void print_expression_prec(const expression_t *expression, unsigned prec);
48 void change_indent(int delta)
54 void print_indent(void)
56 for(int i = 0; i < indent; ++i)
62 PREC_COMMA = 2, /* , left to right */
63 PREC_ASSIGN = 4, /* = += -= *= /= %= <<= >>= &= ^= |= right to left */
64 PREC_COND = 6, /* ?: right to left */
65 PREC_LOG_OR = 8, /* || left to right */
66 PREC_LOG_AND = 10, /* && left to right */
67 PREC_BIT_OR = 12, /* | left to right */
68 PREC_BIT_XOR = 14, /* ^ left to right */
69 PREC_BIT_AND = 16, /* & left to right */
70 PREC_EQ = 18, /* == != left to right */
71 PREC_CMP = 20, /* < <= > >= left to right */
72 PREC_SHF = 22, /* << >> left to right */
73 PREC_PLUS = 24, /* + - left to right */
74 PREC_MUL = 26, /* * / % left to right */
75 PREC_UNARY = 28, /* ! ~ ++ -- + - (type) * & sizeof right to left */
76 PREC_ACCESS = 30, /* () [] -> . left to right */
77 PREC_PRIM = 32, /* primary */
82 * Returns 1 if a given precedence level has right-to-left
83 * associativity, else -1.
85 * @param precedence the operator precedence
87 static int right_to_left(unsigned precedence) {
88 return (precedence == PREC_ASSIGN || precedence == PREC_COND ||
89 precedence == PREC_UNARY) ? 1 : -1;
93 * Return the precedence of an expression given by its kind.
95 * @param kind the expression kind
97 static unsigned get_expression_precedence(expression_kind_t kind)
99 static const unsigned prec[] = {
100 [EXPR_UNKNOWN] = PREC_PRIM,
101 [EXPR_INVALID] = PREC_PRIM,
102 [EXPR_REFERENCE] = PREC_PRIM,
103 [EXPR_CHARACTER_CONSTANT] = PREC_PRIM,
104 [EXPR_WIDE_CHARACTER_CONSTANT] = PREC_PRIM,
105 [EXPR_CONST] = PREC_PRIM,
106 [EXPR_STRING_LITERAL] = PREC_PRIM,
107 [EXPR_WIDE_STRING_LITERAL] = PREC_PRIM,
108 [EXPR_COMPOUND_LITERAL] = PREC_UNARY,
109 [EXPR_CALL] = PREC_PRIM,
110 [EXPR_CONDITIONAL] = PREC_COND,
111 [EXPR_SELECT] = PREC_ACCESS,
112 [EXPR_ARRAY_ACCESS] = PREC_ACCESS,
113 [EXPR_SIZEOF] = PREC_UNARY,
114 [EXPR_CLASSIFY_TYPE] = PREC_UNARY,
115 [EXPR_ALIGNOF] = PREC_UNARY,
117 [EXPR_FUNCTION] = PREC_PRIM,
118 [EXPR_PRETTY_FUNCTION] = PREC_PRIM,
119 [EXPR_BUILTIN_SYMBOL] = PREC_PRIM,
120 [EXPR_BUILTIN_CONSTANT_P] = PREC_PRIM,
121 [EXPR_BUILTIN_PREFETCH] = PREC_PRIM,
122 [EXPR_OFFSETOF] = PREC_PRIM,
123 [EXPR_VA_START] = PREC_PRIM,
124 [EXPR_VA_ARG] = PREC_PRIM,
125 [EXPR_STATEMENT] = PREC_ACCESS,
127 [EXPR_UNARY_NEGATE] = PREC_UNARY,
128 [EXPR_UNARY_PLUS] = PREC_UNARY,
129 [EXPR_UNARY_BITWISE_NEGATE] = PREC_UNARY,
130 [EXPR_UNARY_NOT] = PREC_UNARY,
131 [EXPR_UNARY_DEREFERENCE] = PREC_UNARY,
132 [EXPR_UNARY_TAKE_ADDRESS] = PREC_UNARY,
133 [EXPR_UNARY_POSTFIX_INCREMENT] = PREC_UNARY,
134 [EXPR_UNARY_POSTFIX_DECREMENT] = PREC_UNARY,
135 [EXPR_UNARY_PREFIX_INCREMENT] = PREC_UNARY,
136 [EXPR_UNARY_PREFIX_DECREMENT] = PREC_UNARY,
137 [EXPR_UNARY_CAST] = PREC_UNARY,
138 [EXPR_UNARY_CAST_IMPLICIT] = PREC_UNARY,
139 [EXPR_UNARY_ASSUME] = PREC_PRIM,
140 [EXPR_UNARY_BITFIELD_EXTRACT] = PREC_ACCESS,
142 [EXPR_BINARY_ADD] = PREC_PLUS,
143 [EXPR_BINARY_SUB] = PREC_PLUS,
144 [EXPR_BINARY_MUL] = PREC_MUL,
145 [EXPR_BINARY_DIV] = PREC_MUL,
146 [EXPR_BINARY_MOD] = PREC_MUL,
147 [EXPR_BINARY_EQUAL] = PREC_EQ,
148 [EXPR_BINARY_NOTEQUAL] = PREC_EQ,
149 [EXPR_BINARY_LESS] = PREC_CMP,
150 [EXPR_BINARY_LESSEQUAL] = PREC_CMP,
151 [EXPR_BINARY_GREATER] = PREC_CMP,
152 [EXPR_BINARY_GREATEREQUAL] = PREC_CMP,
153 [EXPR_BINARY_BITWISE_AND] = PREC_BIT_AND,
154 [EXPR_BINARY_BITWISE_OR] = PREC_BIT_OR,
155 [EXPR_BINARY_BITWISE_XOR] = PREC_BIT_XOR,
156 [EXPR_BINARY_LOGICAL_AND] = PREC_LOG_AND,
157 [EXPR_BINARY_LOGICAL_OR] = PREC_LOG_OR,
158 [EXPR_BINARY_SHIFTLEFT] = PREC_SHF,
159 [EXPR_BINARY_SHIFTRIGHT] = PREC_SHF,
160 [EXPR_BINARY_ASSIGN] = PREC_ASSIGN,
161 [EXPR_BINARY_MUL_ASSIGN] = PREC_ASSIGN,
162 [EXPR_BINARY_DIV_ASSIGN] = PREC_ASSIGN,
163 [EXPR_BINARY_MOD_ASSIGN] = PREC_ASSIGN,
164 [EXPR_BINARY_ADD_ASSIGN] = PREC_ASSIGN,
165 [EXPR_BINARY_SUB_ASSIGN] = PREC_ASSIGN,
166 [EXPR_BINARY_SHIFTLEFT_ASSIGN] = PREC_ASSIGN,
167 [EXPR_BINARY_SHIFTRIGHT_ASSIGN] = PREC_ASSIGN,
168 [EXPR_BINARY_BITWISE_AND_ASSIGN] = PREC_ASSIGN,
169 [EXPR_BINARY_BITWISE_XOR_ASSIGN] = PREC_ASSIGN,
170 [EXPR_BINARY_BITWISE_OR_ASSIGN] = PREC_ASSIGN,
171 [EXPR_BINARY_COMMA] = PREC_COMMA,
173 [EXPR_BINARY_BUILTIN_EXPECT] = PREC_PRIM,
174 [EXPR_BINARY_ISGREATER] = PREC_PRIM,
175 [EXPR_BINARY_ISGREATEREQUAL] = PREC_PRIM,
176 [EXPR_BINARY_ISLESS] = PREC_PRIM,
177 [EXPR_BINARY_ISLESSEQUAL] = PREC_PRIM,
178 [EXPR_BINARY_ISLESSGREATER] = PREC_PRIM,
179 [EXPR_BINARY_ISUNORDERED] = PREC_PRIM
181 assert((unsigned)kind < (sizeof(prec)/sizeof(prec[0])));
182 unsigned res = prec[kind];
184 assert(res != PREC_BOTTOM);
189 * Print a constant expression.
191 * @param cnst the constant expression
193 static void print_const(const const_expression_t *cnst)
195 if(cnst->base.type == NULL)
198 const type_t *const type = skip_typeref(cnst->base.type);
200 if (is_type_integer(type)) {
201 fprintf(out, "%lld", cnst->v.int_value);
202 } else if (is_type_float(type)) {
203 fprintf(out, "%Lf", cnst->v.float_value);
205 panic("unknown constant");
210 * Print a quoted string constant.
212 * @param string the string constant
213 * @param border the border char
215 static void print_quoted_string(const string_t *const string, char border)
218 const char *end = string->begin + string->size - 1;
219 for (const char *c = string->begin; c != end; ++c) {
224 case '\\': fputs("\\\\", out); break;
225 case '\a': fputs("\\a", out); break;
226 case '\b': fputs("\\b", out); break;
227 case '\f': fputs("\\f", out); break;
228 case '\n': fputs("\\n", out); break;
229 case '\r': fputs("\\r", out); break;
230 case '\t': fputs("\\t", out); break;
231 case '\v': fputs("\\v", out); break;
232 case '\?': fputs("\\?", out); break;
235 fprintf(out, "\\%03o", *c);
246 * Prints a wide string literal expression.
248 * @param wstr the wide string literal expression
250 static void print_quoted_wide_string(const wide_string_t *const wstr,
255 for (const wchar_rep_t *c = wstr->begin, *end = wstr->begin + wstr->size-1;
258 case L'\"': fputs("\\\"", out); break;
259 case L'\\': fputs("\\\\", out); break;
260 case L'\a': fputs("\\a", out); break;
261 case L'\b': fputs("\\b", out); break;
262 case L'\f': fputs("\\f", out); break;
263 case L'\n': fputs("\\n", out); break;
264 case L'\r': fputs("\\r", out); break;
265 case L'\t': fputs("\\t", out); break;
266 case L'\v': fputs("\\v", out); break;
267 case L'\?': fputs("\\?", out); break;
269 const unsigned tc = *c;
272 fprintf(out, "\\%03o", (char)*c);
276 } else if (tc < 0x800) {
277 fputc(0xC0 | (tc >> 6), out);
278 fputc(0x80 | (tc & 0x3F), out);
279 } else if (tc < 0x10000) {
280 fputc(0xE0 | ( tc >> 12), out);
281 fputc(0x80 | ((tc >> 6) & 0x3F), out);
282 fputc(0x80 | ( tc & 0x3F), out);
284 fputc(0xF0 | ( tc >> 18), out);
285 fputc(0x80 | ((tc >> 12) & 0x3F), out);
286 fputc(0x80 | ((tc >> 6) & 0x3F), out);
287 fputc(0x80 | ( tc & 0x3F), out);
296 * Print a constant character expression.
298 * @param cnst the constant character expression
300 static void print_character_constant(const const_expression_t *cnst)
302 print_quoted_string(&cnst->v.character, '\'');
305 static void print_wide_character_constant(const const_expression_t *cnst)
307 print_quoted_wide_string(&cnst->v.wide_character, '\'');
311 * Prints a string literal expression.
313 * @param string_literal the string literal expression
315 static void print_string_literal(
316 const string_literal_expression_t *string_literal)
318 print_quoted_string(&string_literal->value, '"');
321 static void print_wide_string_literal(
322 const wide_string_literal_expression_t *const wstr)
324 print_quoted_wide_string(&wstr->value, '"');
327 static void print_compound_literal(
328 const compound_literal_expression_t *expression)
331 print_type(expression->type);
333 print_initializer(expression->initializer);
337 * Prints a call expression.
339 * @param call the call expression
341 static void print_call_expression(const call_expression_t *call)
343 unsigned prec = get_expression_precedence(call->base.kind);
344 print_expression_prec(call->function, prec);
346 call_argument_t *argument = call->arguments;
348 while(argument != NULL) {
354 print_expression_prec(argument->expression, PREC_COMMA + 1);
356 argument = argument->next;
362 * Prints a binary expression.
364 * @param binexpr the binary expression
366 static void print_binary_expression(const binary_expression_t *binexpr)
368 unsigned prec = get_expression_precedence(binexpr->base.kind);
369 int r2l = right_to_left(prec);
371 if(binexpr->base.kind == EXPR_BINARY_BUILTIN_EXPECT) {
372 fputs("__builtin_expect(", out);
373 print_expression_prec(binexpr->left, prec);
375 print_expression_prec(binexpr->right, prec);
380 print_expression_prec(binexpr->left, prec + r2l);
381 if (binexpr->base.kind != EXPR_BINARY_COMMA) {
384 switch (binexpr->base.kind) {
385 case EXPR_BINARY_COMMA: fputs(",", out); break;
386 case EXPR_BINARY_ASSIGN: fputs("=", out); break;
387 case EXPR_BINARY_ADD: fputs("+", out); break;
388 case EXPR_BINARY_SUB: fputs("-", out); break;
389 case EXPR_BINARY_MUL: fputs("*", out); break;
390 case EXPR_BINARY_MOD: fputs("%", out); break;
391 case EXPR_BINARY_DIV: fputs("/", out); break;
392 case EXPR_BINARY_BITWISE_OR: fputs("|", out); break;
393 case EXPR_BINARY_BITWISE_AND: fputs("&", out); break;
394 case EXPR_BINARY_BITWISE_XOR: fputs("^", out); break;
395 case EXPR_BINARY_LOGICAL_OR: fputs("||", out); break;
396 case EXPR_BINARY_LOGICAL_AND: fputs("&&", out); break;
397 case EXPR_BINARY_NOTEQUAL: fputs("!=", out); break;
398 case EXPR_BINARY_EQUAL: fputs("==", out); break;
399 case EXPR_BINARY_LESS: fputs("<", out); break;
400 case EXPR_BINARY_LESSEQUAL: fputs("<=", out); break;
401 case EXPR_BINARY_GREATER: fputs(">", out); break;
402 case EXPR_BINARY_GREATEREQUAL: fputs(">=", out); break;
403 case EXPR_BINARY_SHIFTLEFT: fputs("<<", out); break;
404 case EXPR_BINARY_SHIFTRIGHT: fputs(">>", out); break;
406 case EXPR_BINARY_ADD_ASSIGN: fputs("+=", out); break;
407 case EXPR_BINARY_SUB_ASSIGN: fputs("-=", out); break;
408 case EXPR_BINARY_MUL_ASSIGN: fputs("*=", out); break;
409 case EXPR_BINARY_MOD_ASSIGN: fputs("%=", out); break;
410 case EXPR_BINARY_DIV_ASSIGN: fputs("/=", out); break;
411 case EXPR_BINARY_BITWISE_OR_ASSIGN: fputs("|=", out); break;
412 case EXPR_BINARY_BITWISE_AND_ASSIGN: fputs("&=", out); break;
413 case EXPR_BINARY_BITWISE_XOR_ASSIGN: fputs("^=", out); break;
414 case EXPR_BINARY_SHIFTLEFT_ASSIGN: fputs("<<=", out); break;
415 case EXPR_BINARY_SHIFTRIGHT_ASSIGN: fputs(">>=", out); break;
416 default: panic("invalid binexpression found");
419 print_expression_prec(binexpr->right, prec - r2l);
423 * Prints an unary expression.
425 * @param unexpr the unary expression
427 static void print_unary_expression(const unary_expression_t *unexpr)
429 unsigned prec = get_expression_precedence(unexpr->base.kind);
430 switch(unexpr->base.kind) {
431 case EXPR_UNARY_NEGATE: fputs("-", out); break;
432 case EXPR_UNARY_PLUS: fputs("+", out); break;
433 case EXPR_UNARY_NOT: fputs("!", out); break;
434 case EXPR_UNARY_BITWISE_NEGATE: fputs("~", out); break;
435 case EXPR_UNARY_PREFIX_INCREMENT: fputs("++", out); break;
436 case EXPR_UNARY_PREFIX_DECREMENT: fputs("--", out); break;
437 case EXPR_UNARY_DEREFERENCE: fputs("*", out); break;
438 case EXPR_UNARY_TAKE_ADDRESS: fputs("&", out); break;
440 case EXPR_UNARY_BITFIELD_EXTRACT:
441 print_expression_prec(unexpr->value, prec);
444 case EXPR_UNARY_POSTFIX_INCREMENT:
445 print_expression_prec(unexpr->value, prec);
448 case EXPR_UNARY_POSTFIX_DECREMENT:
449 print_expression_prec(unexpr->value, prec);
452 case EXPR_UNARY_CAST_IMPLICIT:
453 if(!print_implicit_casts) {
454 print_expression_prec(unexpr->value, prec);
458 case EXPR_UNARY_CAST:
460 print_type(unexpr->base.type);
463 case EXPR_UNARY_ASSUME:
464 fputs("__assume(", out);
465 print_expression_prec(unexpr->value, PREC_COMMA + 1);
469 panic("invalid unary expression found");
471 print_expression_prec(unexpr->value, prec);
475 * Prints a reference expression.
477 * @param ref the reference expression
479 static void print_reference_expression(const reference_expression_t *ref)
481 fprintf(out, "%s", ref->declaration->symbol->string);
485 * Prints an array expression.
487 * @param expression the array expression
489 static void print_array_expression(const array_access_expression_t *expression)
491 unsigned prec = get_expression_precedence(expression->base.kind);
492 if(!expression->flipped) {
493 print_expression_prec(expression->array_ref, prec);
495 print_expression_prec(expression->index, prec);
498 print_expression_prec(expression->index, prec);
500 print_expression_prec(expression->array_ref, prec);
506 * Prints a typeproperty expression (sizeof or __alignof__).
508 * @param expression the type property expression
510 static void print_typeprop_expression(const typeprop_expression_t *expression)
512 if (expression->base.kind == EXPR_SIZEOF) {
513 fputs("sizeof", out);
515 assert(expression->base.kind == EXPR_ALIGNOF);
516 fputs("__alignof__", out);
518 if(expression->tp_expression != NULL) {
519 /* always print the '()' here, sizeof x is right but unusual */
521 print_expression_prec(expression->tp_expression, PREC_ACCESS);
525 print_type(expression->type);
531 * Prints an builtin symbol.
533 * @param expression the builtin symbol expression
535 static void print_builtin_symbol(const builtin_symbol_expression_t *expression)
537 fputs(expression->symbol->string, out);
541 * Prints a builtin constant expression.
543 * @param expression the builtin constant expression
545 static void print_builtin_constant(const builtin_constant_expression_t *expression)
547 fputs("__builtin_constant_p(", out);
548 print_expression_prec(expression->value, PREC_COMMA + 1);
553 * Prints a builtin prefetch expression.
555 * @param expression the builtin prefetch expression
557 static void print_builtin_prefetch(const builtin_prefetch_expression_t *expression)
559 fputs("__builtin_prefetch(", out);
560 print_expression_prec(expression->adr, PREC_COMMA + 1);
561 if (expression->rw) {
563 print_expression_prec(expression->rw, PREC_COMMA + 1);
565 if (expression->locality) {
567 print_expression_prec(expression->locality, PREC_COMMA + 1);
573 * Prints a conditional expression.
575 * @param expression the conditional expression
577 static void print_conditional(const conditional_expression_t *expression)
579 unsigned prec = get_expression_precedence(expression->base.kind);
581 print_expression_prec(expression->condition, prec);
583 print_expression_prec(expression->true_expression, prec);
585 print_expression_prec(expression->false_expression, prec);
590 * Prints a va_start expression.
592 * @param expression the va_start expression
594 static void print_va_start(const va_start_expression_t *const expression)
596 fputs("__builtin_va_start(", out);
597 print_expression_prec(expression->ap, PREC_COMMA + 1);
599 fputs(expression->parameter->symbol->string, out);
604 * Prints a va_arg expression.
606 * @param expression the va_arg expression
608 static void print_va_arg(const va_arg_expression_t *expression)
610 fputs("__builtin_va_arg(", out);
611 print_expression_prec(expression->ap, PREC_COMMA + 1);
613 print_type(expression->base.type);
618 * Prints a select expression (. or ->).
620 * @param expression the select expression
622 static void print_select(const select_expression_t *expression)
624 unsigned prec = get_expression_precedence(expression->base.kind);
625 print_expression_prec(expression->compound, prec);
626 if(is_type_pointer(expression->compound->base.type)) {
631 fputs(expression->symbol->string, out);
635 * Prints a type classify expression.
637 * @param expr the type classify expression
639 static void print_classify_type_expression(
640 const classify_type_expression_t *const expr)
642 fputs("__builtin_classify_type(", out);
643 print_expression_prec(expr->type_expression, PREC_COMMA + 1);
648 * Prints a designator.
650 * @param designator the designator
652 static void print_designator(const designator_t *designator)
654 for ( ; designator != NULL; designator = designator->next) {
655 if (designator->symbol == NULL) {
657 print_expression_prec(designator->array_index, PREC_ACCESS);
661 fputs(designator->symbol->string, out);
667 * Prints an offsetof expression.
669 * @param expression the offset expression
671 static void print_offsetof_expression(const offsetof_expression_t *expression)
673 fputs("__builtin_offsetof", out);
675 print_type(expression->type);
677 print_designator(expression->designator);
682 * Prints a statement expression.
684 * @param expression the statement expression
686 static void print_statement_expression(const statement_expression_t *expression)
689 print_statement(expression->statement);
694 * Prints an expression with parenthesis if needed.
696 * @param expression the expression to print
697 * @param top_prec the precedence of the user of this expression.
699 static void print_expression_prec(const expression_t *expression, unsigned top_prec)
701 unsigned prec = get_expression_precedence(expression->base.kind);
702 if (print_parenthesis && top_prec != PREC_BOTTOM)
706 switch(expression->kind) {
709 fprintf(out, "$invalid expression$");
711 case EXPR_CHARACTER_CONSTANT:
712 print_character_constant(&expression->conste);
714 case EXPR_WIDE_CHARACTER_CONSTANT:
715 print_wide_character_constant(&expression->conste);
718 print_const(&expression->conste);
721 case EXPR_PRETTY_FUNCTION:
722 case EXPR_STRING_LITERAL:
723 print_string_literal(&expression->string);
725 case EXPR_WIDE_STRING_LITERAL:
726 print_wide_string_literal(&expression->wide_string);
728 case EXPR_COMPOUND_LITERAL:
729 print_compound_literal(&expression->compound_literal);
732 print_call_expression(&expression->call);
735 print_binary_expression(&expression->binary);
738 print_reference_expression(&expression->reference);
740 case EXPR_ARRAY_ACCESS:
741 print_array_expression(&expression->array_access);
744 print_unary_expression(&expression->unary);
748 print_typeprop_expression(&expression->typeprop);
750 case EXPR_BUILTIN_SYMBOL:
751 print_builtin_symbol(&expression->builtin_symbol);
753 case EXPR_BUILTIN_CONSTANT_P:
754 print_builtin_constant(&expression->builtin_constant);
756 case EXPR_BUILTIN_PREFETCH:
757 print_builtin_prefetch(&expression->builtin_prefetch);
759 case EXPR_CONDITIONAL:
760 print_conditional(&expression->conditional);
763 print_va_start(&expression->va_starte);
766 print_va_arg(&expression->va_arge);
769 print_select(&expression->select);
771 case EXPR_CLASSIFY_TYPE:
772 print_classify_type_expression(&expression->classify_type);
775 print_offsetof_expression(&expression->offsetofe);
778 print_statement_expression(&expression->statement);
783 fprintf(out, "some expression of type %d", (int) expression->kind);
791 * Print an compound statement.
793 * @param block the compound statement
795 static void print_compound_statement(const compound_statement_t *block)
800 statement_t *statement = block->statements;
801 while(statement != NULL) {
802 if (statement->base.kind == STATEMENT_CASE_LABEL)
805 print_statement(statement);
807 statement = statement->base.next;
815 * Print a return statement.
817 * @param statement the return statement
819 static void print_return_statement(const return_statement_t *statement)
821 fprintf(out, "return ");
822 if(statement->value != NULL)
823 print_expression(statement->value);
828 * Print an expression statement.
830 * @param statement the expression statement
832 static void print_expression_statement(const expression_statement_t *statement)
834 print_expression(statement->expression);
839 * Print a goto statement.
841 * @param statement the goto statement
843 static void print_goto_statement(const goto_statement_t *statement)
845 fprintf(out, "goto ");
846 fputs(statement->label->symbol->string, out);
847 fprintf(stderr, "(%p)", (void*) statement->label);
852 * Print a label statement.
854 * @param statement the label statement
856 static void print_label_statement(const label_statement_t *statement)
858 fprintf(stderr, "(%p)", (void*) statement->label);
859 fprintf(out, "%s:\n", statement->label->symbol->string);
860 print_statement(statement->statement);
864 * Print an if statement.
866 * @param statement the if statement
868 static void print_if_statement(const if_statement_t *statement)
871 print_expression(statement->condition);
873 print_statement(statement->true_statement);
875 if(statement->false_statement != NULL) {
878 print_statement(statement->false_statement);
883 * Print a switch statement.
885 * @param statement the switch statement
887 static void print_switch_statement(const switch_statement_t *statement)
889 fputs("switch (", out);
890 print_expression(statement->expression);
892 print_statement(statement->body);
896 * Print a case label (including the default label).
898 * @param statement the case label statement
900 static void print_case_label(const case_label_statement_t *statement)
902 if(statement->expression == NULL) {
903 fputs("default:\n", out);
906 print_expression(statement->expression);
907 if (statement->end_range != NULL) {
909 print_expression(statement->end_range);
914 if(statement->statement != NULL) {
915 if (statement->statement->base.kind == STATEMENT_CASE_LABEL) {
919 print_statement(statement->statement);
924 * Print a declaration statement.
926 * @param statement the statement
928 static void print_declaration_statement(
929 const declaration_statement_t *statement)
932 declaration_t *declaration = statement->declarations_begin;
933 for( ; declaration != statement->declarations_end->next;
934 declaration = declaration->next) {
935 if(declaration->storage_class == STORAGE_CLASS_ENUM_ENTRY)
943 print_declaration(declaration);
949 * Print a while statement.
951 * @param statement the statement
953 static void print_while_statement(const while_statement_t *statement)
955 fputs("while (", out);
956 print_expression(statement->condition);
958 print_statement(statement->body);
962 * Print a do-while statement.
964 * @param statement the statement
966 static void print_do_while_statement(const do_while_statement_t *statement)
969 print_statement(statement->body);
971 fputs("while (", out);
972 print_expression(statement->condition);
977 * Print a for statement.
979 * @param statement the statement
981 static void print_for_statement(const for_statement_t *statement)
984 if(statement->scope.declarations != NULL) {
985 assert(statement->initialisation == NULL);
986 print_declaration(statement->scope.declarations);
987 if(statement->scope.declarations->next != NULL) {
988 panic("multiple declarations in for statement not supported yet");
992 if(statement->initialisation) {
993 print_expression(statement->initialisation);
997 if(statement->condition != NULL) {
998 print_expression(statement->condition);
1001 if(statement->step != NULL) {
1002 print_expression(statement->step);
1005 print_statement(statement->body);
1009 * Print assembler constraints.
1011 * @param constraints the constraints
1013 static void print_asm_constraints(asm_constraint_t *constraints)
1015 asm_constraint_t *constraint = constraints;
1016 for( ; constraint != NULL; constraint = constraint->next) {
1017 if(constraint != constraints)
1020 if(constraint->symbol) {
1021 fprintf(out, "[%s] ", constraint->symbol->string);
1023 print_quoted_string(&constraint->constraints, '"');
1025 print_expression(constraint->expression);
1031 * Print assembler clobbers.
1033 * @param clobbers the clobbers
1035 static void print_asm_clobbers(asm_clobber_t *clobbers)
1037 asm_clobber_t *clobber = clobbers;
1038 for( ; clobber != NULL; clobber = clobber->next) {
1039 if(clobber != clobbers)
1042 print_quoted_string(&clobber->clobber, '"');
1047 * Print an assembler statement.
1049 * @param statement the statement
1051 static void print_asm_statement(const asm_statement_t *statement)
1054 if(statement->is_volatile) {
1055 fputs("volatile ", out);
1058 print_quoted_string(&statement->asm_text, '"');
1059 if(statement->inputs == NULL && statement->outputs == NULL
1060 && statement->clobbers == NULL)
1061 goto end_of_print_asm_statement;
1064 print_asm_constraints(statement->inputs);
1065 if(statement->outputs == NULL && statement->clobbers == NULL)
1066 goto end_of_print_asm_statement;
1069 print_asm_constraints(statement->outputs);
1070 if(statement->clobbers == NULL)
1071 goto end_of_print_asm_statement;
1074 print_asm_clobbers(statement->clobbers);
1076 end_of_print_asm_statement:
1081 * Print a statement.
1083 * @param statement the statement
1085 void print_statement(const statement_t *statement)
1087 switch(statement->kind) {
1088 case STATEMENT_EMPTY:
1091 case STATEMENT_COMPOUND:
1092 print_compound_statement(&statement->compound);
1094 case STATEMENT_RETURN:
1095 print_return_statement(&statement->returns);
1097 case STATEMENT_EXPRESSION:
1098 print_expression_statement(&statement->expression);
1100 case STATEMENT_LABEL:
1101 print_label_statement(&statement->label);
1103 case STATEMENT_GOTO:
1104 print_goto_statement(&statement->gotos);
1106 case STATEMENT_CONTINUE:
1107 fputs("continue;\n", out);
1109 case STATEMENT_BREAK:
1110 fputs("break;\n", out);
1113 print_if_statement(&statement->ifs);
1115 case STATEMENT_SWITCH:
1116 print_switch_statement(&statement->switchs);
1118 case STATEMENT_CASE_LABEL:
1119 print_case_label(&statement->case_label);
1121 case STATEMENT_DECLARATION:
1122 print_declaration_statement(&statement->declaration);
1124 case STATEMENT_WHILE:
1125 print_while_statement(&statement->whiles);
1127 case STATEMENT_DO_WHILE:
1128 print_do_while_statement(&statement->do_while);
1131 print_for_statement(&statement->fors);
1134 print_asm_statement(&statement->asms);
1136 case STATEMENT_INVALID:
1137 fprintf(out, "$invalid statement$");
1143 * Print a storage class.
1145 * @param storage_class the storage class
1147 static void print_storage_class(storage_class_tag_t storage_class)
1149 switch(storage_class) {
1150 case STORAGE_CLASS_ENUM_ENTRY:
1151 case STORAGE_CLASS_NONE:
1153 case STORAGE_CLASS_TYPEDEF: fputs("typedef ", out); break;
1154 case STORAGE_CLASS_EXTERN: fputs("extern ", out); break;
1155 case STORAGE_CLASS_STATIC: fputs("static ", out); break;
1156 case STORAGE_CLASS_AUTO: fputs("auto ", out); break;
1157 case STORAGE_CLASS_REGISTER: fputs("register ", out); break;
1158 case STORAGE_CLASS_THREAD: fputs("__thread", out); break;
1159 case STORAGE_CLASS_THREAD_EXTERN: fputs("extern __thread", out); break;
1160 case STORAGE_CLASS_THREAD_STATIC: fputs("static __thread", out); break;
1165 * Print an initializer.
1167 * @param initializer the initializer
1169 void print_initializer(const initializer_t *initializer)
1171 if(initializer == NULL) {
1176 switch(initializer->kind) {
1177 case INITIALIZER_VALUE: {
1178 const initializer_value_t *value = &initializer->value;
1179 print_expression(value->value);
1182 case INITIALIZER_LIST: {
1183 assert(initializer->kind == INITIALIZER_LIST);
1185 const initializer_list_t *list = &initializer->list;
1187 for(size_t i = 0 ; i < list->len; ++i) {
1188 const initializer_t *sub_init = list->initializers[i];
1189 print_initializer(list->initializers[i]);
1190 if(i < list->len-1) {
1191 if(sub_init == NULL || sub_init->kind != INITIALIZER_DESIGNATOR)
1198 case INITIALIZER_STRING:
1199 print_quoted_string(&initializer->string.string, '"');
1201 case INITIALIZER_WIDE_STRING:
1202 print_quoted_wide_string(&initializer->wide_string.string, '"');
1204 case INITIALIZER_DESIGNATOR:
1205 print_designator(initializer->designator.designator);
1210 panic("invalid initializer kind found");
1214 * Print microsoft extended declaration modifiers.
1216 static void print_ms_modifiers(const declaration_t *declaration) {
1217 decl_modifiers_t modifiers = declaration->modifiers;
1219 /* DM_FORCEINLINE handled outside. */
1220 if((modifiers & ~DM_FORCEINLINE) != 0 || declaration->alignment != 0 ||
1221 declaration->get_property_sym != NULL || declaration->put_property_sym != NULL) {
1224 fputs("__declspec", out);
1225 if(modifiers & DM_DLLIMPORT) {
1226 fputs(next, out); next = ", "; fputs("dllimport", out);
1228 if(modifiers & DM_DLLEXPORT) {
1229 fputs(next, out); next = ", "; fputs("dllexport", out);
1231 if(modifiers & DM_THREAD) {
1232 fputs(next, out); next = ", "; fputs("thread", out);
1234 if(modifiers & DM_NAKED) {
1235 fputs(next, out); next = ", "; fputs("naked", out);
1237 if(modifiers & DM_THREAD) {
1238 fputs(next, out); next = ", "; fputs("thread", out);
1240 if(modifiers & DM_SELECTANY) {
1241 fputs(next, out); next = ", "; fputs("selectany", out);
1243 if(modifiers & DM_NOTHROW) {
1244 fputs(next, out); next = ", "; fputs("nothrow", out);
1246 if(modifiers & DM_NORETURN) {
1247 fputs(next, out); next = ", "; fputs("noreturn", out);
1249 if(modifiers & DM_NOINLINE) {
1250 fputs(next, out); next = ", "; fputs("noinline", out);
1252 if(modifiers & DM_DEPRECATED) {
1253 fputs(next, out); next = ", "; fputs("deprecated", out);
1254 if(declaration->deprecated_string != NULL)
1255 fprintf(out, "(\"%s\")", declaration->deprecated_string);
1257 if(declaration->alignment != 0) {
1258 fputs(next, out); next = ", "; fprintf(out, "align(%u)", declaration->alignment);
1260 if(modifiers & DM_RESTRICT) {
1261 fputs(next, out); next = ", "; fputs("restrict", out);
1263 if(modifiers & DM_NOALIAS) {
1264 fputs(next, out); next = ", "; fputs("noalias", out);
1266 if(declaration->get_property_sym != NULL || declaration->put_property_sym != NULL) {
1268 fputs(next, out); next = ", "; fprintf(out, "property(");
1269 if(declaration->get_property_sym != NULL) {
1270 fprintf(out, "get=%s", declaration->get_property_sym->string);
1273 if(declaration->put_property_sym != NULL)
1274 fprintf(out, "%sput=%s", comma, declaration->put_property_sym->string);
1282 * Print a declaration in the NORMAL namespace.
1284 * @param declaration the declaration
1286 static void print_normal_declaration(const declaration_t *declaration)
1288 print_storage_class((storage_class_tag_t) declaration->declared_storage_class);
1289 if(declaration->is_inline) {
1290 if(declaration->modifiers & DM_FORCEINLINE)
1291 fputs("__forceinline ", out);
1293 if(declaration->modifiers & DM_MICROSOFT_INLINE)
1294 fputs("__inline ", out);
1296 fputs("inline ", out);
1299 print_ms_modifiers(declaration);
1300 print_type_ext(declaration->type, declaration->symbol,
1301 &declaration->scope);
1303 if(declaration->type->kind == TYPE_FUNCTION) {
1304 if(declaration->init.statement != NULL) {
1306 print_statement(declaration->init.statement);
1309 } else if(declaration->init.initializer != NULL) {
1311 print_initializer(declaration->init.initializer);
1317 * Prints an expression.
1319 * @param expression the expression
1321 void print_expression(const expression_t *expression) {
1322 print_expression_prec(expression, PREC_BOTTOM);
1326 * Print a declaration.
1328 * @param declaration the declaration
1330 void print_declaration(const declaration_t *declaration)
1332 if(declaration->namespc != NAMESPACE_NORMAL &&
1333 declaration->symbol == NULL)
1336 switch(declaration->namespc) {
1337 case NAMESPACE_NORMAL:
1338 print_normal_declaration(declaration);
1340 case NAMESPACE_STRUCT:
1341 fputs("struct ", out);
1342 fputs(declaration->symbol->string, out);
1344 print_compound_definition(declaration);
1347 case NAMESPACE_UNION:
1348 fputs("union ", out);
1349 fputs(declaration->symbol->string, out);
1351 print_compound_definition(declaration);
1354 case NAMESPACE_ENUM:
1355 fputs("enum ", out);
1356 fputs(declaration->symbol->string, out);
1358 print_enum_definition(declaration);
1365 * Print the AST of a translation unit.
1367 * @param unit the translation unit
1369 void print_ast(const translation_unit_t *unit)
1373 declaration_t *declaration = unit->scope.declarations;
1374 for( ; declaration != NULL; declaration = declaration->next) {
1375 if(declaration->storage_class == STORAGE_CLASS_ENUM_ENTRY)
1377 if(declaration->namespc != NAMESPACE_NORMAL &&
1378 declaration->symbol == NULL)
1382 print_declaration(declaration);
1387 bool is_constant_initializer(const initializer_t *initializer)
1389 switch(initializer->kind) {
1390 case INITIALIZER_STRING:
1391 case INITIALIZER_WIDE_STRING:
1392 case INITIALIZER_DESIGNATOR:
1395 case INITIALIZER_VALUE:
1396 return is_constant_expression(initializer->value.value);
1398 case INITIALIZER_LIST:
1399 for(size_t i = 0; i < initializer->list.len; ++i) {
1400 initializer_t *sub_initializer = initializer->list.initializers[i];
1401 if(!is_constant_initializer(sub_initializer))
1406 panic("invalid initializer kind found");
1409 static bool is_object_with_constant_address(const expression_t *expression)
1411 switch(expression->kind) {
1412 case EXPR_UNARY_DEREFERENCE:
1413 return is_address_constant(expression->unary.value);
1416 if(is_type_pointer(expression->select.compound->base.type)) {
1418 return is_address_constant(expression->select.compound);
1420 return is_object_with_constant_address(expression->select.compound);
1424 case EXPR_ARRAY_ACCESS:
1425 return is_constant_expression(expression->array_access.index)
1426 && is_address_constant(expression->array_access.array_ref);
1428 case EXPR_REFERENCE: {
1429 declaration_t *declaration = expression->reference.declaration;
1430 switch((storage_class_tag_t) declaration->storage_class) {
1431 case STORAGE_CLASS_NONE:
1432 case STORAGE_CLASS_EXTERN:
1433 case STORAGE_CLASS_STATIC:
1445 bool is_address_constant(const expression_t *expression)
1447 switch(expression->kind) {
1448 case EXPR_UNARY_TAKE_ADDRESS:
1449 return is_object_with_constant_address(expression->unary.value);
1451 case EXPR_UNARY_DEREFERENCE: {
1452 type_t *real_type = revert_automatic_type_conversion(expression->unary.value);
1453 /* dereferencing a function is a NOP */
1454 if(is_type_function(real_type)) {
1455 return is_address_constant(expression->unary.value);
1459 case EXPR_UNARY_CAST:
1460 return is_type_pointer(skip_typeref(expression->base.type))
1461 && (is_constant_expression(expression->unary.value)
1462 || is_address_constant(expression->unary.value));
1464 case EXPR_BINARY_ADD:
1465 case EXPR_BINARY_SUB: {
1466 expression_t *left = expression->binary.left;
1467 expression_t *right = expression->binary.right;
1469 if(is_type_pointer(skip_typeref(left->base.type))) {
1470 return is_address_constant(left) && is_constant_expression(right);
1471 } else if(is_type_pointer(skip_typeref(right->base.type))) {
1472 return is_constant_expression(left) && is_address_constant(right);
1478 case EXPR_REFERENCE: {
1479 declaration_t *declaration = expression->reference.declaration;
1480 type_t *type = skip_typeref(declaration->type);
1481 if(is_type_function(type))
1483 if(is_type_array(type)) {
1484 return is_object_with_constant_address(expression);
1494 bool is_constant_expression(const expression_t *expression)
1496 switch(expression->kind) {
1499 case EXPR_CHARACTER_CONSTANT:
1500 case EXPR_WIDE_CHARACTER_CONSTANT:
1501 case EXPR_STRING_LITERAL:
1502 case EXPR_WIDE_STRING_LITERAL:
1504 case EXPR_CLASSIFY_TYPE:
1506 case EXPR_PRETTY_FUNCTION:
1509 case EXPR_BUILTIN_CONSTANT_P:
1512 case EXPR_BUILTIN_SYMBOL:
1513 case EXPR_BUILTIN_PREFETCH:
1518 case EXPR_STATEMENT:
1519 case EXPR_UNARY_POSTFIX_INCREMENT:
1520 case EXPR_UNARY_POSTFIX_DECREMENT:
1521 case EXPR_UNARY_PREFIX_INCREMENT:
1522 case EXPR_UNARY_PREFIX_DECREMENT:
1523 case EXPR_UNARY_BITFIELD_EXTRACT:
1524 case EXPR_UNARY_ASSUME: /* has VOID type */
1525 case EXPR_UNARY_TAKE_ADDRESS:
1526 case EXPR_UNARY_DEREFERENCE:
1527 case EXPR_BINARY_ASSIGN:
1528 case EXPR_BINARY_MUL_ASSIGN:
1529 case EXPR_BINARY_DIV_ASSIGN:
1530 case EXPR_BINARY_MOD_ASSIGN:
1531 case EXPR_BINARY_ADD_ASSIGN:
1532 case EXPR_BINARY_SUB_ASSIGN:
1533 case EXPR_BINARY_SHIFTLEFT_ASSIGN:
1534 case EXPR_BINARY_SHIFTRIGHT_ASSIGN:
1535 case EXPR_BINARY_BITWISE_AND_ASSIGN:
1536 case EXPR_BINARY_BITWISE_XOR_ASSIGN:
1537 case EXPR_BINARY_BITWISE_OR_ASSIGN:
1538 case EXPR_BINARY_COMMA:
1541 case EXPR_UNARY_NEGATE:
1542 case EXPR_UNARY_PLUS:
1543 case EXPR_UNARY_BITWISE_NEGATE:
1544 case EXPR_UNARY_NOT:
1545 return is_constant_expression(expression->unary.value);
1547 case EXPR_UNARY_CAST:
1548 case EXPR_UNARY_CAST_IMPLICIT:
1549 return is_type_arithmetic(skip_typeref(expression->base.type))
1550 && is_constant_expression(expression->unary.value);
1552 case EXPR_BINARY_ADD:
1553 case EXPR_BINARY_SUB:
1554 case EXPR_BINARY_MUL:
1555 case EXPR_BINARY_DIV:
1556 case EXPR_BINARY_MOD:
1557 case EXPR_BINARY_EQUAL:
1558 case EXPR_BINARY_NOTEQUAL:
1559 case EXPR_BINARY_LESS:
1560 case EXPR_BINARY_LESSEQUAL:
1561 case EXPR_BINARY_GREATER:
1562 case EXPR_BINARY_GREATEREQUAL:
1563 case EXPR_BINARY_BITWISE_AND:
1564 case EXPR_BINARY_BITWISE_OR:
1565 case EXPR_BINARY_BITWISE_XOR:
1566 case EXPR_BINARY_LOGICAL_AND:
1567 case EXPR_BINARY_LOGICAL_OR:
1568 case EXPR_BINARY_SHIFTLEFT:
1569 case EXPR_BINARY_SHIFTRIGHT:
1570 case EXPR_BINARY_BUILTIN_EXPECT:
1571 case EXPR_BINARY_ISGREATER:
1572 case EXPR_BINARY_ISGREATEREQUAL:
1573 case EXPR_BINARY_ISLESS:
1574 case EXPR_BINARY_ISLESSEQUAL:
1575 case EXPR_BINARY_ISLESSGREATER:
1576 case EXPR_BINARY_ISUNORDERED:
1577 return is_constant_expression(expression->binary.left)
1578 && is_constant_expression(expression->binary.right);
1580 case EXPR_COMPOUND_LITERAL:
1581 return is_constant_initializer(expression->compound_literal.initializer);
1583 case EXPR_CONDITIONAL: {
1584 expression_t *condition = expression->conditional.condition;
1585 if(!is_constant_expression(condition))
1588 long val = fold_constant(condition);
1590 return is_constant_expression(expression->conditional.true_expression);
1592 return is_constant_expression(expression->conditional.false_expression);
1595 case EXPR_ARRAY_ACCESS:
1596 return is_constant_expression(expression->array_access.array_ref)
1597 && is_constant_expression(expression->array_access.index);
1599 case EXPR_REFERENCE: {
1600 declaration_t *declaration = expression->reference.declaration;
1601 if(declaration->storage_class == STORAGE_CLASS_ENUM_ENTRY)
1613 panic("invalid expression found (is constant expression)");
1617 * Initialize the AST construction.
1621 obstack_init(&ast_obstack);
1629 obstack_free(&ast_obstack, NULL);
1633 * Set the output stream for the AST printer.
1635 * @param stream the output stream
1637 void ast_set_output(FILE *stream)
1640 type_set_output(stream);
1644 * Allocate an AST object of the given size.
1646 * @param size the size of the object to allocate
1648 * @return A new allocated object in the AST memeory space.
1650 void *(allocate_ast)(size_t size)
1652 return _allocate_ast(size);