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
26 #include "lang_features.h"
33 #include "adt/error.h"
35 struct obstack ast_obstack;
40 /** If set, implicit casts are printed. */
41 bool print_implicit_casts = false;
43 /** If set parenthesis are printed to indicate operator precedence. */
44 bool print_parenthesis = false;
46 static void print_statement(const statement_t *statement);
47 static void print_expression_prec(const expression_t *expression, unsigned prec);
49 void change_indent(int delta)
55 void print_indent(void)
57 for(int i = 0; i < indent; ++i)
63 PREC_COMMA = 2, /* , left to right */
64 PREC_ASSIGN = 4, /* = += -= *= /= %= <<= >>= &= ^= |= right to left */
65 PREC_COND = 6, /* ?: right to left */
66 PREC_LOG_OR = 8, /* || left to right */
67 PREC_LOG_AND = 10, /* && left to right */
68 PREC_BIT_OR = 12, /* | left to right */
69 PREC_BIT_XOR = 14, /* ^ left to right */
70 PREC_BIT_AND = 16, /* & left to right */
71 PREC_EQ = 18, /* == != left to right */
72 PREC_CMP = 20, /* < <= > >= left to right */
73 PREC_SHF = 22, /* << >> left to right */
74 PREC_PLUS = 24, /* + - left to right */
75 PREC_MUL = 26, /* * / % left to right */
76 PREC_UNARY = 28, /* ! ~ ++ -- + - (type) * & sizeof right to left */
77 PREC_ACCESS = 30, /* () [] -> . left to right */
78 PREC_PRIM = 32, /* primary */
83 * Returns 1 if a given precedence level has right-to-left
84 * associativity, else -1.
86 * @param precedence the operator precedence
88 static int right_to_left(unsigned precedence) {
89 return (precedence == PREC_ASSIGN || precedence == PREC_COND ||
90 precedence == PREC_UNARY) ? 1 : -1;
94 * Return the precedence of an expression given by its kind.
96 * @param kind the expression kind
98 static unsigned get_expression_precedence(expression_kind_t kind)
100 static const unsigned prec[] = {
101 [EXPR_UNKNOWN] = PREC_PRIM,
102 [EXPR_INVALID] = PREC_PRIM,
103 [EXPR_REFERENCE] = PREC_PRIM,
104 [EXPR_CHARACTER_CONSTANT] = PREC_PRIM,
105 [EXPR_WIDE_CHARACTER_CONSTANT] = PREC_PRIM,
106 [EXPR_CONST] = PREC_PRIM,
107 [EXPR_STRING_LITERAL] = PREC_PRIM,
108 [EXPR_WIDE_STRING_LITERAL] = PREC_PRIM,
109 [EXPR_COMPOUND_LITERAL] = PREC_UNARY,
110 [EXPR_CALL] = PREC_PRIM,
111 [EXPR_CONDITIONAL] = PREC_COND,
112 [EXPR_SELECT] = PREC_ACCESS,
113 [EXPR_ARRAY_ACCESS] = PREC_ACCESS,
114 [EXPR_SIZEOF] = PREC_UNARY,
115 [EXPR_CLASSIFY_TYPE] = PREC_UNARY,
116 [EXPR_ALIGNOF] = PREC_UNARY,
118 [EXPR_FUNCNAME] = 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, '"');
322 * Prints a predefined symbol.
324 static void print_funcname(
325 const funcname_expression_t *funcname)
328 switch(funcname->kind) {
329 case FUNCNAME_FUNCTION: s = (c_mode & _C99) ? "__func__" : "__FUNCTION__"; break;
330 case FUNCNAME_PRETTY_FUNCTION: s = "__PRETTY_FUNCTION__"; break;
331 case FUNCNAME_FUNCSIG: s = "__FUNCSIG__"; break;
332 case FUNCNAME_FUNCDNAME: s = "__FUNCDNAME__"; break;
339 static void print_wide_string_literal(
340 const wide_string_literal_expression_t *const wstr)
342 print_quoted_wide_string(&wstr->value, '"');
345 static void print_compound_literal(
346 const compound_literal_expression_t *expression)
349 print_type(expression->type);
351 print_initializer(expression->initializer);
355 * Prints a call expression.
357 * @param call the call expression
359 static void print_call_expression(const call_expression_t *call)
361 unsigned prec = get_expression_precedence(call->base.kind);
362 print_expression_prec(call->function, prec);
364 call_argument_t *argument = call->arguments;
366 while(argument != NULL) {
372 print_expression_prec(argument->expression, PREC_COMMA + 1);
374 argument = argument->next;
380 * Prints a binary expression.
382 * @param binexpr the binary expression
384 static void print_binary_expression(const binary_expression_t *binexpr)
386 unsigned prec = get_expression_precedence(binexpr->base.kind);
387 int r2l = right_to_left(prec);
389 if(binexpr->base.kind == EXPR_BINARY_BUILTIN_EXPECT) {
390 fputs("__builtin_expect(", out);
391 print_expression_prec(binexpr->left, prec);
393 print_expression_prec(binexpr->right, prec);
398 print_expression_prec(binexpr->left, prec + r2l);
399 if (binexpr->base.kind != EXPR_BINARY_COMMA) {
402 switch (binexpr->base.kind) {
403 case EXPR_BINARY_COMMA: fputs(",", out); break;
404 case EXPR_BINARY_ASSIGN: fputs("=", out); break;
405 case EXPR_BINARY_ADD: fputs("+", out); break;
406 case EXPR_BINARY_SUB: fputs("-", out); break;
407 case EXPR_BINARY_MUL: fputs("*", out); break;
408 case EXPR_BINARY_MOD: fputs("%", out); break;
409 case EXPR_BINARY_DIV: fputs("/", out); break;
410 case EXPR_BINARY_BITWISE_OR: fputs("|", out); break;
411 case EXPR_BINARY_BITWISE_AND: fputs("&", out); break;
412 case EXPR_BINARY_BITWISE_XOR: fputs("^", out); break;
413 case EXPR_BINARY_LOGICAL_OR: fputs("||", out); break;
414 case EXPR_BINARY_LOGICAL_AND: fputs("&&", out); break;
415 case EXPR_BINARY_NOTEQUAL: fputs("!=", out); break;
416 case EXPR_BINARY_EQUAL: fputs("==", out); break;
417 case EXPR_BINARY_LESS: fputs("<", out); break;
418 case EXPR_BINARY_LESSEQUAL: fputs("<=", out); break;
419 case EXPR_BINARY_GREATER: fputs(">", out); break;
420 case EXPR_BINARY_GREATEREQUAL: fputs(">=", out); break;
421 case EXPR_BINARY_SHIFTLEFT: fputs("<<", out); break;
422 case EXPR_BINARY_SHIFTRIGHT: fputs(">>", out); break;
424 case EXPR_BINARY_ADD_ASSIGN: fputs("+=", out); break;
425 case EXPR_BINARY_SUB_ASSIGN: fputs("-=", out); break;
426 case EXPR_BINARY_MUL_ASSIGN: fputs("*=", out); break;
427 case EXPR_BINARY_MOD_ASSIGN: fputs("%=", out); break;
428 case EXPR_BINARY_DIV_ASSIGN: fputs("/=", out); break;
429 case EXPR_BINARY_BITWISE_OR_ASSIGN: fputs("|=", out); break;
430 case EXPR_BINARY_BITWISE_AND_ASSIGN: fputs("&=", out); break;
431 case EXPR_BINARY_BITWISE_XOR_ASSIGN: fputs("^=", out); break;
432 case EXPR_BINARY_SHIFTLEFT_ASSIGN: fputs("<<=", out); break;
433 case EXPR_BINARY_SHIFTRIGHT_ASSIGN: fputs(">>=", out); break;
434 default: panic("invalid binexpression found");
437 print_expression_prec(binexpr->right, prec - r2l);
441 * Prints an unary expression.
443 * @param unexpr the unary expression
445 static void print_unary_expression(const unary_expression_t *unexpr)
447 unsigned prec = get_expression_precedence(unexpr->base.kind);
448 switch(unexpr->base.kind) {
449 case EXPR_UNARY_NEGATE: fputs("-", out); break;
450 case EXPR_UNARY_PLUS: fputs("+", out); break;
451 case EXPR_UNARY_NOT: fputs("!", out); break;
452 case EXPR_UNARY_BITWISE_NEGATE: fputs("~", out); break;
453 case EXPR_UNARY_PREFIX_INCREMENT: fputs("++", out); break;
454 case EXPR_UNARY_PREFIX_DECREMENT: fputs("--", out); break;
455 case EXPR_UNARY_DEREFERENCE: fputs("*", out); break;
456 case EXPR_UNARY_TAKE_ADDRESS: fputs("&", out); break;
458 case EXPR_UNARY_BITFIELD_EXTRACT:
459 print_expression_prec(unexpr->value, prec);
462 case EXPR_UNARY_POSTFIX_INCREMENT:
463 print_expression_prec(unexpr->value, prec);
466 case EXPR_UNARY_POSTFIX_DECREMENT:
467 print_expression_prec(unexpr->value, prec);
470 case EXPR_UNARY_CAST_IMPLICIT:
471 if(!print_implicit_casts) {
472 print_expression_prec(unexpr->value, prec);
476 case EXPR_UNARY_CAST:
478 print_type(unexpr->base.type);
481 case EXPR_UNARY_ASSUME:
482 fputs("__assume(", out);
483 print_expression_prec(unexpr->value, PREC_COMMA + 1);
487 panic("invalid unary expression found");
489 print_expression_prec(unexpr->value, prec);
493 * Prints a reference expression.
495 * @param ref the reference expression
497 static void print_reference_expression(const reference_expression_t *ref)
499 fprintf(out, "%s", ref->declaration->symbol->string);
503 * Prints an array expression.
505 * @param expression the array expression
507 static void print_array_expression(const array_access_expression_t *expression)
509 unsigned prec = get_expression_precedence(expression->base.kind);
510 if(!expression->flipped) {
511 print_expression_prec(expression->array_ref, prec);
513 print_expression_prec(expression->index, prec);
516 print_expression_prec(expression->index, prec);
518 print_expression_prec(expression->array_ref, prec);
524 * Prints a typeproperty expression (sizeof or __alignof__).
526 * @param expression the type property expression
528 static void print_typeprop_expression(const typeprop_expression_t *expression)
530 if (expression->base.kind == EXPR_SIZEOF) {
531 fputs("sizeof", out);
533 assert(expression->base.kind == EXPR_ALIGNOF);
534 fputs("__alignof__", out);
536 if(expression->tp_expression != NULL) {
537 /* always print the '()' here, sizeof x is right but unusual */
539 print_expression_prec(expression->tp_expression, PREC_ACCESS);
543 print_type(expression->type);
549 * Prints an builtin symbol.
551 * @param expression the builtin symbol expression
553 static void print_builtin_symbol(const builtin_symbol_expression_t *expression)
555 fputs(expression->symbol->string, out);
559 * Prints a builtin constant expression.
561 * @param expression the builtin constant expression
563 static void print_builtin_constant(const builtin_constant_expression_t *expression)
565 fputs("__builtin_constant_p(", out);
566 print_expression_prec(expression->value, PREC_COMMA + 1);
571 * Prints a builtin prefetch expression.
573 * @param expression the builtin prefetch expression
575 static void print_builtin_prefetch(const builtin_prefetch_expression_t *expression)
577 fputs("__builtin_prefetch(", out);
578 print_expression_prec(expression->adr, PREC_COMMA + 1);
579 if (expression->rw) {
581 print_expression_prec(expression->rw, PREC_COMMA + 1);
583 if (expression->locality) {
585 print_expression_prec(expression->locality, PREC_COMMA + 1);
591 * Prints a conditional expression.
593 * @param expression the conditional expression
595 static void print_conditional(const conditional_expression_t *expression)
597 unsigned prec = get_expression_precedence(expression->base.kind);
599 print_expression_prec(expression->condition, prec);
601 print_expression_prec(expression->true_expression, prec);
603 print_expression_prec(expression->false_expression, prec);
608 * Prints a va_start expression.
610 * @param expression the va_start expression
612 static void print_va_start(const va_start_expression_t *const expression)
614 fputs("__builtin_va_start(", out);
615 print_expression_prec(expression->ap, PREC_COMMA + 1);
617 fputs(expression->parameter->symbol->string, out);
622 * Prints a va_arg expression.
624 * @param expression the va_arg expression
626 static void print_va_arg(const va_arg_expression_t *expression)
628 fputs("__builtin_va_arg(", out);
629 print_expression_prec(expression->ap, PREC_COMMA + 1);
631 print_type(expression->base.type);
636 * Prints a select expression (. or ->).
638 * @param expression the select expression
640 static void print_select(const select_expression_t *expression)
642 unsigned prec = get_expression_precedence(expression->base.kind);
643 print_expression_prec(expression->compound, prec);
644 if(is_type_pointer(expression->compound->base.type)) {
649 fputs(expression->symbol->string, out);
653 * Prints a type classify expression.
655 * @param expr the type classify expression
657 static void print_classify_type_expression(
658 const classify_type_expression_t *const expr)
660 fputs("__builtin_classify_type(", out);
661 print_expression_prec(expr->type_expression, PREC_COMMA + 1);
666 * Prints a designator.
668 * @param designator the designator
670 static void print_designator(const designator_t *designator)
672 for ( ; designator != NULL; designator = designator->next) {
673 if (designator->symbol == NULL) {
675 print_expression_prec(designator->array_index, PREC_ACCESS);
679 fputs(designator->symbol->string, out);
685 * Prints an offsetof expression.
687 * @param expression the offset expression
689 static void print_offsetof_expression(const offsetof_expression_t *expression)
691 fputs("__builtin_offsetof", out);
693 print_type(expression->type);
695 print_designator(expression->designator);
700 * Prints a statement expression.
702 * @param expression the statement expression
704 static void print_statement_expression(const statement_expression_t *expression)
707 print_statement(expression->statement);
712 * Prints an expression with parenthesis if needed.
714 * @param expression the expression to print
715 * @param top_prec the precedence of the user of this expression.
717 static void print_expression_prec(const expression_t *expression, unsigned top_prec)
719 unsigned prec = get_expression_precedence(expression->base.kind);
720 if (print_parenthesis && top_prec != PREC_BOTTOM)
724 switch(expression->kind) {
727 fprintf(out, "$invalid expression$");
729 case EXPR_CHARACTER_CONSTANT:
730 print_character_constant(&expression->conste);
732 case EXPR_WIDE_CHARACTER_CONSTANT:
733 print_wide_character_constant(&expression->conste);
736 print_const(&expression->conste);
739 print_funcname(&expression->funcname);
741 case EXPR_STRING_LITERAL:
742 print_string_literal(&expression->string);
744 case EXPR_WIDE_STRING_LITERAL:
745 print_wide_string_literal(&expression->wide_string);
747 case EXPR_COMPOUND_LITERAL:
748 print_compound_literal(&expression->compound_literal);
751 print_call_expression(&expression->call);
754 print_binary_expression(&expression->binary);
757 print_reference_expression(&expression->reference);
759 case EXPR_ARRAY_ACCESS:
760 print_array_expression(&expression->array_access);
763 print_unary_expression(&expression->unary);
767 print_typeprop_expression(&expression->typeprop);
769 case EXPR_BUILTIN_SYMBOL:
770 print_builtin_symbol(&expression->builtin_symbol);
772 case EXPR_BUILTIN_CONSTANT_P:
773 print_builtin_constant(&expression->builtin_constant);
775 case EXPR_BUILTIN_PREFETCH:
776 print_builtin_prefetch(&expression->builtin_prefetch);
778 case EXPR_CONDITIONAL:
779 print_conditional(&expression->conditional);
782 print_va_start(&expression->va_starte);
785 print_va_arg(&expression->va_arge);
788 print_select(&expression->select);
790 case EXPR_CLASSIFY_TYPE:
791 print_classify_type_expression(&expression->classify_type);
794 print_offsetof_expression(&expression->offsetofe);
797 print_statement_expression(&expression->statement);
802 fprintf(out, "some expression of type %d", (int) expression->kind);
810 * Print an compound statement.
812 * @param block the compound statement
814 static void print_compound_statement(const compound_statement_t *block)
819 statement_t *statement = block->statements;
820 while(statement != NULL) {
821 if (statement->base.kind == STATEMENT_CASE_LABEL)
824 print_statement(statement);
826 statement = statement->base.next;
834 * Print a return statement.
836 * @param statement the return statement
838 static void print_return_statement(const return_statement_t *statement)
840 fprintf(out, "return ");
841 if(statement->value != NULL)
842 print_expression(statement->value);
847 * Print an expression statement.
849 * @param statement the expression statement
851 static void print_expression_statement(const expression_statement_t *statement)
853 print_expression(statement->expression);
858 * Print a goto statement.
860 * @param statement the goto statement
862 static void print_goto_statement(const goto_statement_t *statement)
864 fprintf(out, "goto ");
865 fputs(statement->label->symbol->string, out);
866 fprintf(stderr, "(%p)", (void*) statement->label);
871 * Print a label statement.
873 * @param statement the label statement
875 static void print_label_statement(const label_statement_t *statement)
877 fprintf(stderr, "(%p)", (void*) statement->label);
878 fprintf(out, "%s:\n", statement->label->symbol->string);
879 print_statement(statement->statement);
883 * Print an if statement.
885 * @param statement the if statement
887 static void print_if_statement(const if_statement_t *statement)
890 print_expression(statement->condition);
892 print_statement(statement->true_statement);
894 if(statement->false_statement != NULL) {
897 print_statement(statement->false_statement);
902 * Print a switch statement.
904 * @param statement the switch statement
906 static void print_switch_statement(const switch_statement_t *statement)
908 fputs("switch (", out);
909 print_expression(statement->expression);
911 print_statement(statement->body);
915 * Print a case label (including the default label).
917 * @param statement the case label statement
919 static void print_case_label(const case_label_statement_t *statement)
921 if(statement->expression == NULL) {
922 fputs("default:\n", out);
925 print_expression(statement->expression);
926 if (statement->end_range != NULL) {
928 print_expression(statement->end_range);
933 if(statement->statement != NULL) {
934 if (statement->statement->base.kind == STATEMENT_CASE_LABEL) {
938 print_statement(statement->statement);
943 * Print a declaration statement.
945 * @param statement the statement
947 static void print_declaration_statement(
948 const declaration_statement_t *statement)
951 declaration_t *declaration = statement->declarations_begin;
952 for( ; declaration != statement->declarations_end->next;
953 declaration = declaration->next) {
954 if(declaration->storage_class == STORAGE_CLASS_ENUM_ENTRY)
962 print_declaration(declaration);
968 * Print a while statement.
970 * @param statement the statement
972 static void print_while_statement(const while_statement_t *statement)
974 fputs("while (", out);
975 print_expression(statement->condition);
977 print_statement(statement->body);
981 * Print a do-while statement.
983 * @param statement the statement
985 static void print_do_while_statement(const do_while_statement_t *statement)
988 print_statement(statement->body);
990 fputs("while (", out);
991 print_expression(statement->condition);
996 * Print a for statement.
998 * @param statement the statement
1000 static void print_for_statement(const for_statement_t *statement)
1002 fputs("for (", out);
1003 if(statement->scope.declarations != NULL) {
1004 assert(statement->initialisation == NULL);
1005 print_declaration(statement->scope.declarations);
1006 if(statement->scope.declarations->next != NULL) {
1007 panic("multiple declarations in for statement not supported yet");
1011 if(statement->initialisation) {
1012 print_expression(statement->initialisation);
1016 if(statement->condition != NULL) {
1017 print_expression(statement->condition);
1020 if(statement->step != NULL) {
1021 print_expression(statement->step);
1024 print_statement(statement->body);
1028 * Print assembler constraints.
1030 * @param constraints the constraints
1032 static void print_asm_constraints(asm_constraint_t *constraints)
1034 asm_constraint_t *constraint = constraints;
1035 for( ; constraint != NULL; constraint = constraint->next) {
1036 if(constraint != constraints)
1039 if(constraint->symbol) {
1040 fprintf(out, "[%s] ", constraint->symbol->string);
1042 print_quoted_string(&constraint->constraints, '"');
1044 print_expression(constraint->expression);
1050 * Print assembler clobbers.
1052 * @param clobbers the clobbers
1054 static void print_asm_clobbers(asm_clobber_t *clobbers)
1056 asm_clobber_t *clobber = clobbers;
1057 for( ; clobber != NULL; clobber = clobber->next) {
1058 if(clobber != clobbers)
1061 print_quoted_string(&clobber->clobber, '"');
1066 * Print an assembler statement.
1068 * @param statement the statement
1070 static void print_asm_statement(const asm_statement_t *statement)
1073 if(statement->is_volatile) {
1074 fputs("volatile ", out);
1077 print_quoted_string(&statement->asm_text, '"');
1078 if(statement->inputs == NULL && statement->outputs == NULL
1079 && statement->clobbers == NULL)
1080 goto end_of_print_asm_statement;
1083 print_asm_constraints(statement->inputs);
1084 if(statement->outputs == NULL && statement->clobbers == NULL)
1085 goto end_of_print_asm_statement;
1088 print_asm_constraints(statement->outputs);
1089 if(statement->clobbers == NULL)
1090 goto end_of_print_asm_statement;
1093 print_asm_clobbers(statement->clobbers);
1095 end_of_print_asm_statement:
1100 * Print a microsoft __try statement.
1102 * @param statement the statement
1104 static void print_ms_try_statement(const ms_try_statement_t *statement)
1106 fputs("__try ", out);
1107 print_statement(statement->try_statement);
1109 if(statement->except_expression != NULL) {
1110 fputs("__except(", out);
1111 print_expression(statement->except_expression);
1114 fputs("__finally ", out);
1116 print_statement(statement->final_statement);
1120 * Print a microsoft __leave statement.
1122 * @param statement the statement
1124 static void print_leave_statement(const leave_statement_t *statement)
1127 fputs("__leave;\n", out);
1131 * Print a statement.
1133 * @param statement the statement
1135 void print_statement(const statement_t *statement)
1137 switch(statement->kind) {
1138 case STATEMENT_EMPTY:
1141 case STATEMENT_COMPOUND:
1142 print_compound_statement(&statement->compound);
1144 case STATEMENT_RETURN:
1145 print_return_statement(&statement->returns);
1147 case STATEMENT_EXPRESSION:
1148 print_expression_statement(&statement->expression);
1150 case STATEMENT_LABEL:
1151 print_label_statement(&statement->label);
1153 case STATEMENT_GOTO:
1154 print_goto_statement(&statement->gotos);
1156 case STATEMENT_CONTINUE:
1157 fputs("continue;\n", out);
1159 case STATEMENT_BREAK:
1160 fputs("break;\n", out);
1163 print_if_statement(&statement->ifs);
1165 case STATEMENT_SWITCH:
1166 print_switch_statement(&statement->switchs);
1168 case STATEMENT_CASE_LABEL:
1169 print_case_label(&statement->case_label);
1171 case STATEMENT_DECLARATION:
1172 print_declaration_statement(&statement->declaration);
1174 case STATEMENT_WHILE:
1175 print_while_statement(&statement->whiles);
1177 case STATEMENT_DO_WHILE:
1178 print_do_while_statement(&statement->do_while);
1181 print_for_statement(&statement->fors);
1184 print_asm_statement(&statement->asms);
1186 case STATEMENT_MS_TRY:
1187 print_ms_try_statement(&statement->ms_try);
1189 case STATEMENT_LEAVE:
1190 print_leave_statement(&statement->leave);
1192 case STATEMENT_INVALID:
1193 fprintf(out, "$invalid statement$");
1199 * Print a storage class.
1201 * @param storage_class the storage class
1203 static void print_storage_class(storage_class_tag_t storage_class)
1205 switch(storage_class) {
1206 case STORAGE_CLASS_ENUM_ENTRY:
1207 case STORAGE_CLASS_NONE:
1209 case STORAGE_CLASS_TYPEDEF: fputs("typedef ", out); break;
1210 case STORAGE_CLASS_EXTERN: fputs("extern ", out); break;
1211 case STORAGE_CLASS_STATIC: fputs("static ", out); break;
1212 case STORAGE_CLASS_AUTO: fputs("auto ", out); break;
1213 case STORAGE_CLASS_REGISTER: fputs("register ", out); break;
1214 case STORAGE_CLASS_THREAD: fputs("__thread", out); break;
1215 case STORAGE_CLASS_THREAD_EXTERN: fputs("extern __thread", out); break;
1216 case STORAGE_CLASS_THREAD_STATIC: fputs("static __thread", out); break;
1221 * Print an initializer.
1223 * @param initializer the initializer
1225 void print_initializer(const initializer_t *initializer)
1227 if(initializer == NULL) {
1232 switch(initializer->kind) {
1233 case INITIALIZER_VALUE: {
1234 const initializer_value_t *value = &initializer->value;
1235 print_expression(value->value);
1238 case INITIALIZER_LIST: {
1239 assert(initializer->kind == INITIALIZER_LIST);
1241 const initializer_list_t *list = &initializer->list;
1243 for(size_t i = 0 ; i < list->len; ++i) {
1244 const initializer_t *sub_init = list->initializers[i];
1245 print_initializer(list->initializers[i]);
1246 if(i < list->len-1) {
1247 if(sub_init == NULL || sub_init->kind != INITIALIZER_DESIGNATOR)
1254 case INITIALIZER_STRING:
1255 print_quoted_string(&initializer->string.string, '"');
1257 case INITIALIZER_WIDE_STRING:
1258 print_quoted_wide_string(&initializer->wide_string.string, '"');
1260 case INITIALIZER_DESIGNATOR:
1261 print_designator(initializer->designator.designator);
1266 panic("invalid initializer kind found");
1270 * Print microsoft extended declaration modifiers.
1272 static void print_ms_modifiers(const declaration_t *declaration) {
1273 if((c_mode & _MS) == 0)
1276 decl_modifiers_t modifiers = declaration->modifiers;
1278 /* DM_FORCEINLINE handled outside. */
1279 if((modifiers & ~DM_FORCEINLINE) != 0 ||
1280 declaration->alignment != 0 || declaration->deprecated != 0 ||
1281 declaration->get_property_sym != NULL || declaration->put_property_sym != NULL) {
1284 fputs("__declspec", out);
1285 if(modifiers & DM_DLLIMPORT) {
1286 fputs(next, out); next = ", "; fputs("dllimport", out);
1288 if(modifiers & DM_DLLEXPORT) {
1289 fputs(next, out); next = ", "; fputs("dllexport", out);
1291 if(modifiers & DM_THREAD) {
1292 fputs(next, out); next = ", "; fputs("thread", out);
1294 if(modifiers & DM_NAKED) {
1295 fputs(next, out); next = ", "; fputs("naked", out);
1297 if(modifiers & DM_THREAD) {
1298 fputs(next, out); next = ", "; fputs("thread", out);
1300 if(modifiers & DM_SELECTANY) {
1301 fputs(next, out); next = ", "; fputs("selectany", out);
1303 if(modifiers & DM_NOTHROW) {
1304 fputs(next, out); next = ", "; fputs("nothrow", out);
1306 if(modifiers & DM_NORETURN) {
1307 fputs(next, out); next = ", "; fputs("noreturn", out);
1309 if(modifiers & DM_NOINLINE) {
1310 fputs(next, out); next = ", "; fputs("noinline", out);
1312 if(declaration->deprecated != 0) {
1313 fputs(next, out); next = ", "; fputs("deprecated", out);
1314 if(declaration->deprecated_string != NULL)
1315 fprintf(out, "(\"%s\")", declaration->deprecated_string);
1317 if(declaration->alignment != 0) {
1318 fputs(next, out); next = ", "; fprintf(out, "align(%u)", declaration->alignment);
1320 if(modifiers & DM_RESTRICT) {
1321 fputs(next, out); next = ", "; fputs("restrict", out);
1323 if(modifiers & DM_NOALIAS) {
1324 fputs(next, out); next = ", "; fputs("noalias", out);
1326 if(declaration->get_property_sym != NULL || declaration->put_property_sym != NULL) {
1328 fputs(next, out); next = ", "; fprintf(out, "property(");
1329 if(declaration->get_property_sym != NULL) {
1330 fprintf(out, "get=%s", declaration->get_property_sym->string);
1333 if(declaration->put_property_sym != NULL)
1334 fprintf(out, "%sput=%s", comma, declaration->put_property_sym->string);
1342 * Print a declaration in the NORMAL namespace.
1344 * @param declaration the declaration
1346 static void print_normal_declaration(const declaration_t *declaration)
1348 print_storage_class((storage_class_tag_t) declaration->declared_storage_class);
1349 if(declaration->is_inline) {
1350 if(declaration->modifiers & DM_FORCEINLINE)
1351 fputs("__forceinline ", out);
1353 if(declaration->modifiers & DM_MICROSOFT_INLINE)
1354 fputs("__inline ", out);
1356 fputs("inline ", out);
1359 print_ms_modifiers(declaration);
1360 print_type_ext(declaration->type, declaration->symbol,
1361 &declaration->scope);
1363 if(declaration->type->kind == TYPE_FUNCTION) {
1364 if(declaration->init.statement != NULL) {
1366 print_statement(declaration->init.statement);
1369 } else if(declaration->init.initializer != NULL) {
1371 print_initializer(declaration->init.initializer);
1377 * Prints an expression.
1379 * @param expression the expression
1381 void print_expression(const expression_t *expression) {
1382 print_expression_prec(expression, PREC_BOTTOM);
1386 * Print a declaration.
1388 * @param declaration the declaration
1390 void print_declaration(const declaration_t *declaration)
1392 if(declaration->namespc != NAMESPACE_NORMAL &&
1393 declaration->symbol == NULL)
1396 switch(declaration->namespc) {
1397 case NAMESPACE_NORMAL:
1398 print_normal_declaration(declaration);
1400 case NAMESPACE_STRUCT:
1401 fputs("struct ", out);
1402 fputs(declaration->symbol->string, out);
1404 print_compound_definition(declaration);
1407 case NAMESPACE_UNION:
1408 fputs("union ", out);
1409 fputs(declaration->symbol->string, out);
1411 print_compound_definition(declaration);
1414 case NAMESPACE_ENUM:
1415 fputs("enum ", out);
1416 fputs(declaration->symbol->string, out);
1418 print_enum_definition(declaration);
1425 * Print the AST of a translation unit.
1427 * @param unit the translation unit
1429 void print_ast(const translation_unit_t *unit)
1433 declaration_t *declaration = unit->scope.declarations;
1434 for( ; declaration != NULL; declaration = declaration->next) {
1435 if(declaration->storage_class == STORAGE_CLASS_ENUM_ENTRY)
1437 if(declaration->namespc != NAMESPACE_NORMAL &&
1438 declaration->symbol == NULL)
1442 print_declaration(declaration);
1447 bool is_constant_initializer(const initializer_t *initializer)
1449 switch(initializer->kind) {
1450 case INITIALIZER_STRING:
1451 case INITIALIZER_WIDE_STRING:
1452 case INITIALIZER_DESIGNATOR:
1455 case INITIALIZER_VALUE:
1456 return is_constant_expression(initializer->value.value);
1458 case INITIALIZER_LIST:
1459 for(size_t i = 0; i < initializer->list.len; ++i) {
1460 initializer_t *sub_initializer = initializer->list.initializers[i];
1461 if(!is_constant_initializer(sub_initializer))
1466 panic("invalid initializer kind found");
1469 static bool is_object_with_constant_address(const expression_t *expression)
1471 switch(expression->kind) {
1472 case EXPR_UNARY_DEREFERENCE:
1473 return is_address_constant(expression->unary.value);
1476 if(is_type_pointer(expression->select.compound->base.type)) {
1478 return is_address_constant(expression->select.compound);
1480 return is_object_with_constant_address(expression->select.compound);
1484 case EXPR_ARRAY_ACCESS:
1485 return is_constant_expression(expression->array_access.index)
1486 && is_address_constant(expression->array_access.array_ref);
1488 case EXPR_REFERENCE: {
1489 declaration_t *declaration = expression->reference.declaration;
1490 switch((storage_class_tag_t) declaration->storage_class) {
1491 case STORAGE_CLASS_NONE:
1492 case STORAGE_CLASS_EXTERN:
1493 case STORAGE_CLASS_STATIC:
1505 bool is_address_constant(const expression_t *expression)
1507 switch(expression->kind) {
1508 case EXPR_UNARY_TAKE_ADDRESS:
1509 return is_object_with_constant_address(expression->unary.value);
1511 case EXPR_UNARY_DEREFERENCE: {
1512 type_t *real_type = revert_automatic_type_conversion(expression->unary.value);
1513 /* dereferencing a function is a NOP */
1514 if(is_type_function(real_type)) {
1515 return is_address_constant(expression->unary.value);
1519 case EXPR_UNARY_CAST:
1520 return is_type_pointer(skip_typeref(expression->base.type))
1521 && (is_constant_expression(expression->unary.value)
1522 || is_address_constant(expression->unary.value));
1524 case EXPR_BINARY_ADD:
1525 case EXPR_BINARY_SUB: {
1526 expression_t *left = expression->binary.left;
1527 expression_t *right = expression->binary.right;
1529 if(is_type_pointer(skip_typeref(left->base.type))) {
1530 return is_address_constant(left) && is_constant_expression(right);
1531 } else if(is_type_pointer(skip_typeref(right->base.type))) {
1532 return is_constant_expression(left) && is_address_constant(right);
1538 case EXPR_REFERENCE: {
1539 declaration_t *declaration = expression->reference.declaration;
1540 type_t *type = skip_typeref(declaration->type);
1541 if(is_type_function(type))
1543 if(is_type_array(type)) {
1544 return is_object_with_constant_address(expression);
1554 bool is_constant_expression(const expression_t *expression)
1556 switch(expression->kind) {
1559 case EXPR_CHARACTER_CONSTANT:
1560 case EXPR_WIDE_CHARACTER_CONSTANT:
1561 case EXPR_STRING_LITERAL:
1562 case EXPR_WIDE_STRING_LITERAL:
1564 case EXPR_CLASSIFY_TYPE:
1568 case EXPR_BUILTIN_CONSTANT_P:
1571 case EXPR_BUILTIN_SYMBOL:
1572 case EXPR_BUILTIN_PREFETCH:
1577 case EXPR_STATEMENT:
1578 case EXPR_UNARY_POSTFIX_INCREMENT:
1579 case EXPR_UNARY_POSTFIX_DECREMENT:
1580 case EXPR_UNARY_PREFIX_INCREMENT:
1581 case EXPR_UNARY_PREFIX_DECREMENT:
1582 case EXPR_UNARY_BITFIELD_EXTRACT:
1583 case EXPR_UNARY_ASSUME: /* has VOID type */
1584 case EXPR_UNARY_TAKE_ADDRESS:
1585 case EXPR_UNARY_DEREFERENCE:
1586 case EXPR_BINARY_ASSIGN:
1587 case EXPR_BINARY_MUL_ASSIGN:
1588 case EXPR_BINARY_DIV_ASSIGN:
1589 case EXPR_BINARY_MOD_ASSIGN:
1590 case EXPR_BINARY_ADD_ASSIGN:
1591 case EXPR_BINARY_SUB_ASSIGN:
1592 case EXPR_BINARY_SHIFTLEFT_ASSIGN:
1593 case EXPR_BINARY_SHIFTRIGHT_ASSIGN:
1594 case EXPR_BINARY_BITWISE_AND_ASSIGN:
1595 case EXPR_BINARY_BITWISE_XOR_ASSIGN:
1596 case EXPR_BINARY_BITWISE_OR_ASSIGN:
1597 case EXPR_BINARY_COMMA:
1600 case EXPR_UNARY_NEGATE:
1601 case EXPR_UNARY_PLUS:
1602 case EXPR_UNARY_BITWISE_NEGATE:
1603 case EXPR_UNARY_NOT:
1604 return is_constant_expression(expression->unary.value);
1606 case EXPR_UNARY_CAST:
1607 case EXPR_UNARY_CAST_IMPLICIT:
1608 return is_type_arithmetic(skip_typeref(expression->base.type))
1609 && is_constant_expression(expression->unary.value);
1611 case EXPR_BINARY_ADD:
1612 case EXPR_BINARY_SUB:
1613 case EXPR_BINARY_MUL:
1614 case EXPR_BINARY_DIV:
1615 case EXPR_BINARY_MOD:
1616 case EXPR_BINARY_EQUAL:
1617 case EXPR_BINARY_NOTEQUAL:
1618 case EXPR_BINARY_LESS:
1619 case EXPR_BINARY_LESSEQUAL:
1620 case EXPR_BINARY_GREATER:
1621 case EXPR_BINARY_GREATEREQUAL:
1622 case EXPR_BINARY_BITWISE_AND:
1623 case EXPR_BINARY_BITWISE_OR:
1624 case EXPR_BINARY_BITWISE_XOR:
1625 case EXPR_BINARY_LOGICAL_AND:
1626 case EXPR_BINARY_LOGICAL_OR:
1627 case EXPR_BINARY_SHIFTLEFT:
1628 case EXPR_BINARY_SHIFTRIGHT:
1629 case EXPR_BINARY_BUILTIN_EXPECT:
1630 case EXPR_BINARY_ISGREATER:
1631 case EXPR_BINARY_ISGREATEREQUAL:
1632 case EXPR_BINARY_ISLESS:
1633 case EXPR_BINARY_ISLESSEQUAL:
1634 case EXPR_BINARY_ISLESSGREATER:
1635 case EXPR_BINARY_ISUNORDERED:
1636 return is_constant_expression(expression->binary.left)
1637 && is_constant_expression(expression->binary.right);
1639 case EXPR_COMPOUND_LITERAL:
1640 return is_constant_initializer(expression->compound_literal.initializer);
1642 case EXPR_CONDITIONAL: {
1643 expression_t *condition = expression->conditional.condition;
1644 if(!is_constant_expression(condition))
1647 long val = fold_constant(condition);
1649 return is_constant_expression(expression->conditional.true_expression);
1651 return is_constant_expression(expression->conditional.false_expression);
1654 case EXPR_ARRAY_ACCESS:
1655 return is_constant_expression(expression->array_access.array_ref)
1656 && is_constant_expression(expression->array_access.index);
1658 case EXPR_REFERENCE: {
1659 declaration_t *declaration = expression->reference.declaration;
1660 if(declaration->storage_class == STORAGE_CLASS_ENUM_ENTRY)
1672 panic("invalid expression found (is constant expression)");
1676 * Initialize the AST construction.
1680 obstack_init(&ast_obstack);
1688 obstack_free(&ast_obstack, NULL);
1692 * Set the output stream for the AST printer.
1694 * @param stream the output stream
1696 void ast_set_output(FILE *stream)
1699 type_set_output(stream);
1703 * Allocate an AST object of the given size.
1705 * @param size the size of the object to allocate
1707 * @return A new allocated object in the AST memeory space.
1709 void *(allocate_ast)(size_t size)
1711 return _allocate_ast(size);