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,
141 [EXPR_BINARY_ADD] = PREC_PLUS,
142 [EXPR_BINARY_SUB] = PREC_PLUS,
143 [EXPR_BINARY_MUL] = PREC_MUL,
144 [EXPR_BINARY_DIV] = PREC_MUL,
145 [EXPR_BINARY_MOD] = PREC_MUL,
146 [EXPR_BINARY_EQUAL] = PREC_EQ,
147 [EXPR_BINARY_NOTEQUAL] = PREC_EQ,
148 [EXPR_BINARY_LESS] = PREC_CMP,
149 [EXPR_BINARY_LESSEQUAL] = PREC_CMP,
150 [EXPR_BINARY_GREATER] = PREC_CMP,
151 [EXPR_BINARY_GREATEREQUAL] = PREC_CMP,
152 [EXPR_BINARY_BITWISE_AND] = PREC_BIT_AND,
153 [EXPR_BINARY_BITWISE_OR] = PREC_BIT_OR,
154 [EXPR_BINARY_BITWISE_XOR] = PREC_BIT_XOR,
155 [EXPR_BINARY_LOGICAL_AND] = PREC_LOG_AND,
156 [EXPR_BINARY_LOGICAL_OR] = PREC_LOG_OR,
157 [EXPR_BINARY_SHIFTLEFT] = PREC_SHF,
158 [EXPR_BINARY_SHIFTRIGHT] = PREC_SHF,
159 [EXPR_BINARY_ASSIGN] = PREC_ASSIGN,
160 [EXPR_BINARY_MUL_ASSIGN] = PREC_ASSIGN,
161 [EXPR_BINARY_DIV_ASSIGN] = PREC_ASSIGN,
162 [EXPR_BINARY_MOD_ASSIGN] = PREC_ASSIGN,
163 [EXPR_BINARY_ADD_ASSIGN] = PREC_ASSIGN,
164 [EXPR_BINARY_SUB_ASSIGN] = PREC_ASSIGN,
165 [EXPR_BINARY_SHIFTLEFT_ASSIGN] = PREC_ASSIGN,
166 [EXPR_BINARY_SHIFTRIGHT_ASSIGN] = PREC_ASSIGN,
167 [EXPR_BINARY_BITWISE_AND_ASSIGN] = PREC_ASSIGN,
168 [EXPR_BINARY_BITWISE_XOR_ASSIGN] = PREC_ASSIGN,
169 [EXPR_BINARY_BITWISE_OR_ASSIGN] = PREC_ASSIGN,
170 [EXPR_BINARY_COMMA] = PREC_COMMA,
172 [EXPR_BINARY_BUILTIN_EXPECT] = PREC_PRIM,
173 [EXPR_BINARY_ISGREATER] = PREC_PRIM,
174 [EXPR_BINARY_ISGREATEREQUAL] = PREC_PRIM,
175 [EXPR_BINARY_ISLESS] = PREC_PRIM,
176 [EXPR_BINARY_ISLESSEQUAL] = PREC_PRIM,
177 [EXPR_BINARY_ISLESSGREATER] = PREC_PRIM,
178 [EXPR_BINARY_ISUNORDERED] = PREC_PRIM
180 assert((unsigned)kind < (sizeof(prec)/sizeof(prec[0])));
181 unsigned res = prec[kind];
183 assert(res != PREC_BOTTOM);
188 * Print a constant expression.
190 * @param cnst the constant expression
192 static void print_const(const const_expression_t *cnst)
194 if(cnst->base.type == NULL)
197 const type_t *const type = skip_typeref(cnst->base.type);
199 if (is_type_integer(type)) {
200 fprintf(out, "%lld", cnst->v.int_value);
201 } else if (is_type_float(type)) {
202 fprintf(out, "%Lf", cnst->v.float_value);
204 panic("unknown constant");
209 * Print a quoted string constant.
211 * @param string the string constant
212 * @param border the border char
214 static void print_quoted_string(const string_t *const string, char border)
217 const char *end = string->begin + string->size - 1;
218 for (const char *c = string->begin; c != end; ++c) {
223 case '\\': fputs("\\\\", out); break;
224 case '\a': fputs("\\a", out); break;
225 case '\b': fputs("\\b", out); break;
226 case '\f': fputs("\\f", out); break;
227 case '\n': fputs("\\n", out); break;
228 case '\r': fputs("\\r", out); break;
229 case '\t': fputs("\\t", out); break;
230 case '\v': fputs("\\v", out); break;
231 case '\?': fputs("\\?", out); break;
234 fprintf(out, "\\%03o", *c);
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,
254 for (const wchar_rep_t *c = wstr->begin, *end = wstr->begin + wstr->size-1;
257 case L'\"': fputs("\\\"", out); break;
258 case L'\\': fputs("\\\\", out); break;
259 case L'\a': fputs("\\a", out); break;
260 case L'\b': fputs("\\b", out); break;
261 case L'\f': fputs("\\f", out); break;
262 case L'\n': fputs("\\n", out); break;
263 case L'\r': fputs("\\r", out); break;
264 case L'\t': fputs("\\t", out); break;
265 case L'\v': fputs("\\v", out); break;
266 case L'\?': fputs("\\?", out); break;
268 const unsigned tc = *c;
271 fprintf(out, "\\%03o", (char)*c);
275 } else if (tc < 0x800) {
276 fputc(0xC0 | (tc >> 6), out);
277 fputc(0x80 | (tc & 0x3F), out);
278 } else if (tc < 0x10000) {
279 fputc(0xE0 | ( tc >> 12), out);
280 fputc(0x80 | ((tc >> 6) & 0x3F), out);
281 fputc(0x80 | ( tc & 0x3F), out);
283 fputc(0xF0 | ( tc >> 18), out);
284 fputc(0x80 | ((tc >> 12) & 0x3F), out);
285 fputc(0x80 | ((tc >> 6) & 0x3F), out);
286 fputc(0x80 | ( tc & 0x3F), out);
295 * Print a constant character expression.
297 * @param cnst the constant character expression
299 static void print_character_constant(const const_expression_t *cnst)
301 print_quoted_string(&cnst->v.character, '\'');
304 static void print_wide_character_constant(const const_expression_t *cnst)
306 print_quoted_wide_string(&cnst->v.wide_character, '\'');
310 * Prints a string literal expression.
312 * @param string_literal the string literal expression
314 static void print_string_literal(
315 const string_literal_expression_t *string_literal)
317 print_quoted_string(&string_literal->value, '"');
321 * Prints a predefined symbol.
323 static void print_funcname(
324 const funcname_expression_t *funcname)
327 switch(funcname->kind) {
328 case FUNCNAME_FUNCTION: s = (c_mode & _C99) ? "__func__" : "__FUNCTION__"; break;
329 case FUNCNAME_PRETTY_FUNCTION: s = "__PRETTY_FUNCTION__"; break;
330 case FUNCNAME_FUNCSIG: s = "__FUNCSIG__"; break;
331 case FUNCNAME_FUNCDNAME: s = "__FUNCDNAME__"; break;
338 static void print_wide_string_literal(
339 const wide_string_literal_expression_t *const wstr)
341 print_quoted_wide_string(&wstr->value, '"');
344 static void print_compound_literal(
345 const compound_literal_expression_t *expression)
348 print_type(expression->type);
350 print_initializer(expression->initializer);
354 * Prints a call expression.
356 * @param call the call expression
358 static void print_call_expression(const call_expression_t *call)
360 unsigned prec = get_expression_precedence(call->base.kind);
361 print_expression_prec(call->function, prec);
363 call_argument_t *argument = call->arguments;
365 while(argument != NULL) {
371 print_expression_prec(argument->expression, PREC_COMMA + 1);
373 argument = argument->next;
379 * Prints a binary expression.
381 * @param binexpr the binary expression
383 static void print_binary_expression(const binary_expression_t *binexpr)
385 unsigned prec = get_expression_precedence(binexpr->base.kind);
386 int r2l = right_to_left(prec);
388 if(binexpr->base.kind == EXPR_BINARY_BUILTIN_EXPECT) {
389 fputs("__builtin_expect(", out);
390 print_expression_prec(binexpr->left, prec);
392 print_expression_prec(binexpr->right, prec);
397 print_expression_prec(binexpr->left, prec + r2l);
398 if (binexpr->base.kind != EXPR_BINARY_COMMA) {
401 switch (binexpr->base.kind) {
402 case EXPR_BINARY_COMMA: fputs(",", out); break;
403 case EXPR_BINARY_ASSIGN: fputs("=", out); break;
404 case EXPR_BINARY_ADD: fputs("+", out); break;
405 case EXPR_BINARY_SUB: fputs("-", out); break;
406 case EXPR_BINARY_MUL: fputs("*", out); break;
407 case EXPR_BINARY_MOD: fputs("%", out); break;
408 case EXPR_BINARY_DIV: fputs("/", out); break;
409 case EXPR_BINARY_BITWISE_OR: fputs("|", out); break;
410 case EXPR_BINARY_BITWISE_AND: fputs("&", out); break;
411 case EXPR_BINARY_BITWISE_XOR: fputs("^", out); break;
412 case EXPR_BINARY_LOGICAL_OR: fputs("||", out); break;
413 case EXPR_BINARY_LOGICAL_AND: fputs("&&", out); break;
414 case EXPR_BINARY_NOTEQUAL: fputs("!=", out); break;
415 case EXPR_BINARY_EQUAL: fputs("==", out); break;
416 case EXPR_BINARY_LESS: fputs("<", out); break;
417 case EXPR_BINARY_LESSEQUAL: fputs("<=", out); break;
418 case EXPR_BINARY_GREATER: fputs(">", out); break;
419 case EXPR_BINARY_GREATEREQUAL: fputs(">=", out); break;
420 case EXPR_BINARY_SHIFTLEFT: fputs("<<", out); break;
421 case EXPR_BINARY_SHIFTRIGHT: fputs(">>", out); break;
423 case EXPR_BINARY_ADD_ASSIGN: fputs("+=", out); break;
424 case EXPR_BINARY_SUB_ASSIGN: fputs("-=", out); break;
425 case EXPR_BINARY_MUL_ASSIGN: fputs("*=", out); break;
426 case EXPR_BINARY_MOD_ASSIGN: fputs("%=", out); break;
427 case EXPR_BINARY_DIV_ASSIGN: fputs("/=", out); break;
428 case EXPR_BINARY_BITWISE_OR_ASSIGN: fputs("|=", out); break;
429 case EXPR_BINARY_BITWISE_AND_ASSIGN: fputs("&=", out); break;
430 case EXPR_BINARY_BITWISE_XOR_ASSIGN: fputs("^=", out); break;
431 case EXPR_BINARY_SHIFTLEFT_ASSIGN: fputs("<<=", out); break;
432 case EXPR_BINARY_SHIFTRIGHT_ASSIGN: fputs(">>=", out); break;
433 default: panic("invalid binexpression found");
436 print_expression_prec(binexpr->right, prec - r2l);
440 * Prints an unary expression.
442 * @param unexpr the unary expression
444 static void print_unary_expression(const unary_expression_t *unexpr)
446 unsigned prec = get_expression_precedence(unexpr->base.kind);
447 switch(unexpr->base.kind) {
448 case EXPR_UNARY_NEGATE: fputs("-", out); break;
449 case EXPR_UNARY_PLUS: fputs("+", out); break;
450 case EXPR_UNARY_NOT: fputs("!", out); break;
451 case EXPR_UNARY_BITWISE_NEGATE: fputs("~", out); break;
452 case EXPR_UNARY_PREFIX_INCREMENT: fputs("++", out); break;
453 case EXPR_UNARY_PREFIX_DECREMENT: fputs("--", out); break;
454 case EXPR_UNARY_DEREFERENCE: fputs("*", out); break;
455 case EXPR_UNARY_TAKE_ADDRESS: fputs("&", out); break;
457 case EXPR_UNARY_POSTFIX_INCREMENT:
458 print_expression_prec(unexpr->value, prec);
461 case EXPR_UNARY_POSTFIX_DECREMENT:
462 print_expression_prec(unexpr->value, prec);
465 case EXPR_UNARY_CAST_IMPLICIT:
466 case EXPR_UNARY_CAST:
468 print_type(unexpr->base.type);
471 case EXPR_UNARY_ASSUME:
472 fputs("__assume(", out);
473 print_expression_prec(unexpr->value, PREC_COMMA + 1);
477 panic("invalid unary expression found");
479 print_expression_prec(unexpr->value, prec);
483 * Prints a reference expression.
485 * @param ref the reference expression
487 static void print_reference_expression(const reference_expression_t *ref)
489 fprintf(out, "%s", ref->declaration->symbol->string);
493 * Prints an array expression.
495 * @param expression the array expression
497 static void print_array_expression(const array_access_expression_t *expression)
499 unsigned prec = get_expression_precedence(expression->base.kind);
500 if(!expression->flipped) {
501 print_expression_prec(expression->array_ref, prec);
503 print_expression_prec(expression->index, PREC_BOTTOM);
506 print_expression_prec(expression->index, prec);
508 print_expression_prec(expression->array_ref, PREC_BOTTOM);
514 * Prints a typeproperty expression (sizeof or __alignof__).
516 * @param expression the type property expression
518 static void print_typeprop_expression(const typeprop_expression_t *expression)
520 if (expression->base.kind == EXPR_SIZEOF) {
521 fputs("sizeof", out);
523 assert(expression->base.kind == EXPR_ALIGNOF);
524 fputs("__alignof__", out);
526 if(expression->tp_expression != NULL) {
527 /* always print the '()' here, sizeof x is right but unusual */
529 print_expression_prec(expression->tp_expression, PREC_ACCESS);
533 print_type(expression->type);
539 * Prints an builtin symbol.
541 * @param expression the builtin symbol expression
543 static void print_builtin_symbol(const builtin_symbol_expression_t *expression)
545 fputs(expression->symbol->string, out);
549 * Prints a builtin constant expression.
551 * @param expression the builtin constant expression
553 static void print_builtin_constant(const builtin_constant_expression_t *expression)
555 fputs("__builtin_constant_p(", out);
556 print_expression_prec(expression->value, PREC_COMMA + 1);
561 * Prints a builtin prefetch expression.
563 * @param expression the builtin prefetch expression
565 static void print_builtin_prefetch(const builtin_prefetch_expression_t *expression)
567 fputs("__builtin_prefetch(", out);
568 print_expression_prec(expression->adr, PREC_COMMA + 1);
569 if (expression->rw) {
571 print_expression_prec(expression->rw, PREC_COMMA + 1);
573 if (expression->locality) {
575 print_expression_prec(expression->locality, PREC_COMMA + 1);
581 * Prints a conditional expression.
583 * @param expression the conditional expression
585 static void print_conditional(const conditional_expression_t *expression)
587 unsigned prec = get_expression_precedence(expression->base.kind);
589 print_expression_prec(expression->condition, prec);
591 print_expression_prec(expression->true_expression, prec);
593 print_expression_prec(expression->false_expression, prec);
598 * Prints a va_start expression.
600 * @param expression the va_start expression
602 static void print_va_start(const va_start_expression_t *const expression)
604 fputs("__builtin_va_start(", out);
605 print_expression_prec(expression->ap, PREC_COMMA + 1);
607 fputs(expression->parameter->symbol->string, out);
612 * Prints a va_arg expression.
614 * @param expression the va_arg expression
616 static void print_va_arg(const va_arg_expression_t *expression)
618 fputs("__builtin_va_arg(", out);
619 print_expression_prec(expression->ap, PREC_COMMA + 1);
621 print_type(expression->base.type);
626 * Prints a select expression (. or ->).
628 * @param expression the select expression
630 static void print_select(const select_expression_t *expression)
632 unsigned prec = get_expression_precedence(expression->base.kind);
633 print_expression_prec(expression->compound, prec);
634 if(is_type_pointer(skip_typeref(expression->compound->base.type))) {
639 fputs(expression->symbol->string, out);
643 * Prints a type classify expression.
645 * @param expr the type classify expression
647 static void print_classify_type_expression(
648 const classify_type_expression_t *const expr)
650 fputs("__builtin_classify_type(", out);
651 print_expression_prec(expr->type_expression, PREC_COMMA + 1);
656 * Prints a designator.
658 * @param designator the designator
660 static void print_designator(const designator_t *designator)
662 for ( ; designator != NULL; designator = designator->next) {
663 if (designator->symbol == NULL) {
665 print_expression_prec(designator->array_index, PREC_BOTTOM);
669 fputs(designator->symbol->string, out);
675 * Prints an offsetof expression.
677 * @param expression the offset expression
679 static void print_offsetof_expression(const offsetof_expression_t *expression)
681 fputs("__builtin_offsetof", out);
683 print_type(expression->type);
685 print_designator(expression->designator);
690 * Prints a statement expression.
692 * @param expression the statement expression
694 static void print_statement_expression(const statement_expression_t *expression)
697 print_statement(expression->statement);
702 * Prints an expression with parenthesis if needed.
704 * @param expression the expression to print
705 * @param top_prec the precedence of the user of this expression.
707 static void print_expression_prec(const expression_t *expression, unsigned top_prec)
709 if (expression->kind == EXPR_UNARY_CAST_IMPLICIT && !print_implicit_casts) {
710 expression = expression->unary.value;
712 unsigned prec = get_expression_precedence(expression->base.kind);
713 if (print_parenthesis && top_prec != PREC_BOTTOM)
717 switch(expression->kind) {
720 fprintf(out, "$invalid expression$");
722 case EXPR_CHARACTER_CONSTANT:
723 print_character_constant(&expression->conste);
725 case EXPR_WIDE_CHARACTER_CONSTANT:
726 print_wide_character_constant(&expression->conste);
729 print_const(&expression->conste);
732 print_funcname(&expression->funcname);
734 case EXPR_STRING_LITERAL:
735 print_string_literal(&expression->string);
737 case EXPR_WIDE_STRING_LITERAL:
738 print_wide_string_literal(&expression->wide_string);
740 case EXPR_COMPOUND_LITERAL:
741 print_compound_literal(&expression->compound_literal);
744 print_call_expression(&expression->call);
747 print_binary_expression(&expression->binary);
750 print_reference_expression(&expression->reference);
752 case EXPR_ARRAY_ACCESS:
753 print_array_expression(&expression->array_access);
756 print_unary_expression(&expression->unary);
760 print_typeprop_expression(&expression->typeprop);
762 case EXPR_BUILTIN_SYMBOL:
763 print_builtin_symbol(&expression->builtin_symbol);
765 case EXPR_BUILTIN_CONSTANT_P:
766 print_builtin_constant(&expression->builtin_constant);
768 case EXPR_BUILTIN_PREFETCH:
769 print_builtin_prefetch(&expression->builtin_prefetch);
771 case EXPR_CONDITIONAL:
772 print_conditional(&expression->conditional);
775 print_va_start(&expression->va_starte);
778 print_va_arg(&expression->va_arge);
781 print_select(&expression->select);
783 case EXPR_CLASSIFY_TYPE:
784 print_classify_type_expression(&expression->classify_type);
787 print_offsetof_expression(&expression->offsetofe);
790 print_statement_expression(&expression->statement);
795 fprintf(out, "some expression of type %d", (int) expression->kind);
803 * Print an compound statement.
805 * @param block the compound statement
807 static void print_compound_statement(const compound_statement_t *block)
812 statement_t *statement = block->statements;
813 while(statement != NULL) {
814 if (statement->base.kind == STATEMENT_CASE_LABEL)
817 print_statement(statement);
819 statement = statement->base.next;
827 * Print a return statement.
829 * @param statement the return statement
831 static void print_return_statement(const return_statement_t *statement)
833 fprintf(out, "return ");
834 if(statement->value != NULL)
835 print_expression(statement->value);
840 * Print an expression statement.
842 * @param statement the expression statement
844 static void print_expression_statement(const expression_statement_t *statement)
846 print_expression(statement->expression);
851 * Print a goto statement.
853 * @param statement the goto statement
855 static void print_goto_statement(const goto_statement_t *statement)
857 fprintf(out, "goto ");
858 fputs(statement->label->symbol->string, out);
859 fprintf(stderr, "(%p)", (void*) statement->label);
864 * Print a label statement.
866 * @param statement the label statement
868 static void print_label_statement(const label_statement_t *statement)
870 fprintf(stderr, "(%p)", (void*) statement->label);
871 fprintf(out, "%s:\n", statement->label->symbol->string);
872 print_statement(statement->statement);
876 * Print an if statement.
878 * @param statement the if statement
880 static void print_if_statement(const if_statement_t *statement)
883 print_expression(statement->condition);
885 print_statement(statement->true_statement);
887 if(statement->false_statement != NULL) {
890 print_statement(statement->false_statement);
895 * Print a switch statement.
897 * @param statement the switch statement
899 static void print_switch_statement(const switch_statement_t *statement)
901 fputs("switch (", out);
902 print_expression(statement->expression);
904 print_statement(statement->body);
908 * Print a case label (including the default label).
910 * @param statement the case label statement
912 static void print_case_label(const case_label_statement_t *statement)
914 if(statement->expression == NULL) {
915 fputs("default:\n", out);
918 print_expression(statement->expression);
919 if (statement->end_range != NULL) {
921 print_expression(statement->end_range);
926 if(statement->statement != NULL) {
927 if (statement->statement->base.kind == STATEMENT_CASE_LABEL) {
931 print_statement(statement->statement);
936 * Print a declaration statement.
938 * @param statement the statement
940 static void print_declaration_statement(
941 const declaration_statement_t *statement)
944 declaration_t *declaration = statement->declarations_begin;
945 for( ; declaration != statement->declarations_end->next;
946 declaration = declaration->next) {
947 if(declaration->storage_class == STORAGE_CLASS_ENUM_ENTRY)
955 print_declaration(declaration);
961 * Print a while statement.
963 * @param statement the statement
965 static void print_while_statement(const while_statement_t *statement)
967 fputs("while (", out);
968 print_expression(statement->condition);
970 print_statement(statement->body);
974 * Print a do-while statement.
976 * @param statement the statement
978 static void print_do_while_statement(const do_while_statement_t *statement)
981 print_statement(statement->body);
983 fputs("while (", out);
984 print_expression(statement->condition);
989 * Print a for statement.
991 * @param statement the statement
993 static void print_for_statement(const for_statement_t *statement)
996 if(statement->scope.declarations != NULL) {
997 assert(statement->initialisation == NULL);
998 print_declaration(statement->scope.declarations);
999 if(statement->scope.declarations->next != NULL) {
1000 panic("multiple declarations in for statement not supported yet");
1004 if(statement->initialisation) {
1005 print_expression(statement->initialisation);
1009 if(statement->condition != NULL) {
1010 print_expression(statement->condition);
1013 if(statement->step != NULL) {
1014 print_expression(statement->step);
1017 print_statement(statement->body);
1021 * Print assembler arguments.
1023 * @param arguments the arguments
1025 static void print_asm_arguments(asm_argument_t *arguments)
1027 asm_argument_t *argument = arguments;
1028 for( ; argument != NULL; argument = argument->next) {
1029 if(argument != arguments)
1032 if(argument->symbol) {
1033 fprintf(out, "[%s] ", argument->symbol->string);
1035 print_quoted_string(&argument->constraints, '"');
1037 print_expression(argument->expression);
1043 * Print assembler clobbers.
1045 * @param clobbers the clobbers
1047 static void print_asm_clobbers(asm_clobber_t *clobbers)
1049 asm_clobber_t *clobber = clobbers;
1050 for( ; clobber != NULL; clobber = clobber->next) {
1051 if(clobber != clobbers)
1054 print_quoted_string(&clobber->clobber, '"');
1059 * Print an assembler statement.
1061 * @param statement the statement
1063 static void print_asm_statement(const asm_statement_t *statement)
1066 if(statement->is_volatile) {
1067 fputs("volatile ", out);
1070 print_quoted_string(&statement->asm_text, '"');
1071 if(statement->inputs == NULL && statement->outputs == NULL
1072 && statement->clobbers == NULL)
1073 goto end_of_print_asm_statement;
1076 print_asm_arguments(statement->inputs);
1077 if(statement->outputs == NULL && statement->clobbers == NULL)
1078 goto end_of_print_asm_statement;
1081 print_asm_arguments(statement->outputs);
1082 if(statement->clobbers == NULL)
1083 goto end_of_print_asm_statement;
1086 print_asm_clobbers(statement->clobbers);
1088 end_of_print_asm_statement:
1093 * Print a microsoft __try statement.
1095 * @param statement the statement
1097 static void print_ms_try_statement(const ms_try_statement_t *statement)
1099 fputs("__try ", out);
1100 print_statement(statement->try_statement);
1102 if(statement->except_expression != NULL) {
1103 fputs("__except(", out);
1104 print_expression(statement->except_expression);
1107 fputs("__finally ", out);
1109 print_statement(statement->final_statement);
1113 * Print a microsoft __leave statement.
1115 * @param statement the statement
1117 static void print_leave_statement(const leave_statement_t *statement)
1120 fputs("__leave;\n", out);
1124 * Print a statement.
1126 * @param statement the statement
1128 void print_statement(const statement_t *statement)
1130 switch(statement->kind) {
1131 case STATEMENT_EMPTY:
1134 case STATEMENT_COMPOUND:
1135 print_compound_statement(&statement->compound);
1137 case STATEMENT_RETURN:
1138 print_return_statement(&statement->returns);
1140 case STATEMENT_EXPRESSION:
1141 print_expression_statement(&statement->expression);
1143 case STATEMENT_LABEL:
1144 print_label_statement(&statement->label);
1146 case STATEMENT_GOTO:
1147 print_goto_statement(&statement->gotos);
1149 case STATEMENT_CONTINUE:
1150 fputs("continue;\n", out);
1152 case STATEMENT_BREAK:
1153 fputs("break;\n", out);
1156 print_if_statement(&statement->ifs);
1158 case STATEMENT_SWITCH:
1159 print_switch_statement(&statement->switchs);
1161 case STATEMENT_CASE_LABEL:
1162 print_case_label(&statement->case_label);
1164 case STATEMENT_DECLARATION:
1165 print_declaration_statement(&statement->declaration);
1167 case STATEMENT_WHILE:
1168 print_while_statement(&statement->whiles);
1170 case STATEMENT_DO_WHILE:
1171 print_do_while_statement(&statement->do_while);
1174 print_for_statement(&statement->fors);
1177 print_asm_statement(&statement->asms);
1179 case STATEMENT_MS_TRY:
1180 print_ms_try_statement(&statement->ms_try);
1182 case STATEMENT_LEAVE:
1183 print_leave_statement(&statement->leave);
1185 case STATEMENT_INVALID:
1186 fprintf(out, "$invalid statement$");
1192 * Print a storage class.
1194 * @param storage_class the storage class
1196 static void print_storage_class(storage_class_tag_t storage_class)
1198 switch(storage_class) {
1199 case STORAGE_CLASS_ENUM_ENTRY:
1200 case STORAGE_CLASS_NONE:
1202 case STORAGE_CLASS_TYPEDEF: fputs("typedef ", out); break;
1203 case STORAGE_CLASS_EXTERN: fputs("extern ", out); break;
1204 case STORAGE_CLASS_STATIC: fputs("static ", out); break;
1205 case STORAGE_CLASS_AUTO: fputs("auto ", out); break;
1206 case STORAGE_CLASS_REGISTER: fputs("register ", out); break;
1207 case STORAGE_CLASS_THREAD: fputs("__thread", out); break;
1208 case STORAGE_CLASS_THREAD_EXTERN: fputs("extern __thread", out); break;
1209 case STORAGE_CLASS_THREAD_STATIC: fputs("static __thread", out); break;
1214 * Print an initializer.
1216 * @param initializer the initializer
1218 void print_initializer(const initializer_t *initializer)
1220 if(initializer == NULL) {
1225 switch(initializer->kind) {
1226 case INITIALIZER_VALUE: {
1227 const initializer_value_t *value = &initializer->value;
1228 print_expression(value->value);
1231 case INITIALIZER_LIST: {
1232 assert(initializer->kind == INITIALIZER_LIST);
1234 const initializer_list_t *list = &initializer->list;
1236 for(size_t i = 0 ; i < list->len; ++i) {
1237 const initializer_t *sub_init = list->initializers[i];
1238 print_initializer(list->initializers[i]);
1239 if(i < list->len-1) {
1240 if(sub_init == NULL || sub_init->kind != INITIALIZER_DESIGNATOR)
1247 case INITIALIZER_STRING:
1248 print_quoted_string(&initializer->string.string, '"');
1250 case INITIALIZER_WIDE_STRING:
1251 print_quoted_wide_string(&initializer->wide_string.string, '"');
1253 case INITIALIZER_DESIGNATOR:
1254 print_designator(initializer->designator.designator);
1259 panic("invalid initializer kind found");
1263 * Print microsoft extended declaration modifiers.
1265 static void print_ms_modifiers(const declaration_t *declaration) {
1266 if((c_mode & _MS) == 0)
1269 decl_modifiers_t modifiers = declaration->modifiers;
1271 /* DM_FORCEINLINE handled outside. */
1272 if ((modifiers & ~DM_FORCEINLINE) != 0 ||
1273 declaration->alignment != 0 ||
1274 declaration->get_property_sym != NULL ||
1275 declaration->put_property_sym != NULL) {
1278 fputs("__declspec", out);
1279 if(modifiers & DM_DLLIMPORT) {
1280 fputs(next, out); next = ", "; fputs("dllimport", out);
1282 if(modifiers & DM_DLLEXPORT) {
1283 fputs(next, out); next = ", "; fputs("dllexport", out);
1285 if(modifiers & DM_THREAD) {
1286 fputs(next, out); next = ", "; fputs("thread", out);
1288 if(modifiers & DM_NAKED) {
1289 fputs(next, out); next = ", "; fputs("naked", out);
1291 if(modifiers & DM_THREAD) {
1292 fputs(next, out); next = ", "; fputs("thread", out);
1294 if(modifiers & DM_SELECTANY) {
1295 fputs(next, out); next = ", "; fputs("selectany", out);
1297 if(modifiers & DM_NOTHROW) {
1298 fputs(next, out); next = ", "; fputs("nothrow", out);
1300 if(modifiers & DM_NORETURN) {
1301 fputs(next, out); next = ", "; fputs("noreturn", out);
1303 if(modifiers & DM_NOINLINE) {
1304 fputs(next, out); next = ", "; fputs("noinline", out);
1306 if (modifiers & DM_DEPRECATED) {
1307 fputs(next, out); next = ", "; fputs("deprecated", out);
1308 if(declaration->deprecated_string != NULL)
1309 fprintf(out, "(\"%s\")", declaration->deprecated_string);
1311 if(declaration->alignment != 0) {
1312 fputs(next, out); next = ", "; fprintf(out, "align(%u)", declaration->alignment);
1314 if(modifiers & DM_RESTRICT) {
1315 fputs(next, out); next = ", "; fputs("restrict", out);
1317 if(modifiers & DM_NOALIAS) {
1318 fputs(next, out); next = ", "; fputs("noalias", out);
1320 if(declaration->get_property_sym != NULL || declaration->put_property_sym != NULL) {
1322 fputs(next, out); next = ", "; fprintf(out, "property(");
1323 if(declaration->get_property_sym != NULL) {
1324 fprintf(out, "get=%s", declaration->get_property_sym->string);
1327 if(declaration->put_property_sym != NULL)
1328 fprintf(out, "%sput=%s", comma, declaration->put_property_sym->string);
1336 * Print a declaration in the NORMAL namespace.
1338 * @param declaration the declaration
1340 static void print_normal_declaration(const declaration_t *declaration)
1342 print_storage_class((storage_class_tag_t) declaration->declared_storage_class);
1343 if (declaration->is_inline) {
1344 if (declaration->modifiers & DM_FORCEINLINE) {
1345 fputs("__forceinline ", out);
1346 } else if (declaration->modifiers & DM_MICROSOFT_INLINE) {
1347 fputs("__inline ", out);
1349 fputs("inline ", out);
1352 print_ms_modifiers(declaration);
1353 print_type_ext(declaration->type, declaration->symbol,
1354 &declaration->scope);
1356 if(declaration->type->kind == TYPE_FUNCTION) {
1357 if(declaration->init.statement != NULL) {
1359 print_statement(declaration->init.statement);
1362 } else if(declaration->init.initializer != NULL) {
1364 print_initializer(declaration->init.initializer);
1370 * Prints an expression.
1372 * @param expression the expression
1374 void print_expression(const expression_t *expression) {
1375 print_expression_prec(expression, PREC_BOTTOM);
1379 * Print a declaration.
1381 * @param declaration the declaration
1383 void print_declaration(const declaration_t *declaration)
1385 if(declaration->namespc != NAMESPACE_NORMAL &&
1386 declaration->symbol == NULL)
1389 switch(declaration->namespc) {
1390 case NAMESPACE_NORMAL:
1391 print_normal_declaration(declaration);
1393 case NAMESPACE_STRUCT:
1394 fputs("struct ", out);
1395 fputs(declaration->symbol->string, out);
1397 print_compound_definition(declaration);
1400 case NAMESPACE_UNION:
1401 fputs("union ", out);
1402 fputs(declaration->symbol->string, out);
1404 print_compound_definition(declaration);
1407 case NAMESPACE_ENUM:
1408 fputs("enum ", out);
1409 fputs(declaration->symbol->string, out);
1411 print_enum_definition(declaration);
1418 * Print the AST of a translation unit.
1420 * @param unit the translation unit
1422 void print_ast(const translation_unit_t *unit)
1426 declaration_t *declaration = unit->scope.declarations;
1427 for( ; declaration != NULL; declaration = declaration->next) {
1428 if(declaration->storage_class == STORAGE_CLASS_ENUM_ENTRY)
1430 if(declaration->namespc != NAMESPACE_NORMAL &&
1431 declaration->symbol == NULL)
1435 print_declaration(declaration);
1440 bool is_constant_initializer(const initializer_t *initializer)
1442 switch(initializer->kind) {
1443 case INITIALIZER_STRING:
1444 case INITIALIZER_WIDE_STRING:
1445 case INITIALIZER_DESIGNATOR:
1448 case INITIALIZER_VALUE:
1449 return is_constant_expression(initializer->value.value);
1451 case INITIALIZER_LIST:
1452 for(size_t i = 0; i < initializer->list.len; ++i) {
1453 initializer_t *sub_initializer = initializer->list.initializers[i];
1454 if(!is_constant_initializer(sub_initializer))
1459 panic("invalid initializer kind found");
1462 static bool is_object_with_linker_constant_address(const expression_t *expression)
1464 switch(expression->kind) {
1465 case EXPR_UNARY_DEREFERENCE:
1466 return is_address_constant(expression->unary.value);
1469 type_t *base_type = skip_typeref(expression->select.compound->base.type);
1470 if(is_type_pointer(base_type)) {
1472 return is_address_constant(expression->select.compound);
1474 return is_object_with_linker_constant_address(expression->select.compound);
1478 case EXPR_ARRAY_ACCESS:
1479 return is_constant_expression(expression->array_access.index)
1480 && is_address_constant(expression->array_access.array_ref);
1482 case EXPR_REFERENCE: {
1483 declaration_t *declaration = expression->reference.declaration;
1484 switch((storage_class_tag_t) declaration->storage_class) {
1485 case STORAGE_CLASS_NONE:
1486 case STORAGE_CLASS_EXTERN:
1487 case STORAGE_CLASS_STATIC:
1499 bool is_address_constant(const expression_t *expression)
1501 switch(expression->kind) {
1502 case EXPR_UNARY_TAKE_ADDRESS:
1503 return is_object_with_linker_constant_address(expression->unary.value);
1505 case EXPR_UNARY_DEREFERENCE: {
1507 = revert_automatic_type_conversion(expression->unary.value);
1508 /* dereferencing a function is a NOP */
1509 if(is_type_function(real_type)) {
1510 return is_address_constant(expression->unary.value);
1516 case EXPR_UNARY_CAST: {
1517 type_t *dest = skip_typeref(expression->base.type);
1518 if (!is_type_pointer(dest) &&
1519 ! (dest->kind == TYPE_ATOMIC
1520 && (get_atomic_type_flags(dest->atomic.akind) & ATOMIC_TYPE_FLAG_INTEGER)
1521 && (get_atomic_type_size(dest->atomic.akind) >= get_atomic_type_size(get_intptr_kind()))))
1524 return (is_constant_expression(expression->unary.value)
1525 || is_address_constant(expression->unary.value));
1528 case EXPR_BINARY_ADD:
1529 case EXPR_BINARY_SUB: {
1530 expression_t *left = expression->binary.left;
1531 expression_t *right = expression->binary.right;
1533 if(is_type_pointer(skip_typeref(left->base.type))) {
1534 return is_address_constant(left) && is_constant_expression(right);
1535 } else if(is_type_pointer(skip_typeref(right->base.type))) {
1536 return is_constant_expression(left) && is_address_constant(right);
1542 case EXPR_REFERENCE: {
1543 declaration_t *declaration = expression->reference.declaration;
1544 type_t *type = skip_typeref(declaration->type);
1545 if(is_type_function(type))
1547 if(is_type_array(type)) {
1548 return is_object_with_linker_constant_address(expression);
1558 static bool is_builtin_const_call(const expression_t *expression)
1560 expression_t *function = expression->call.function;
1561 if (function->kind != EXPR_BUILTIN_SYMBOL) {
1565 symbol_t *symbol = function->builtin_symbol.symbol;
1567 switch (symbol->ID) {
1568 case T___builtin_huge_val:
1569 case T___builtin_nan:
1570 case T___builtin_nanf:
1571 case T___builtin_nand:
1578 static bool is_constant_pointer(const expression_t *expression)
1580 if (is_constant_expression(expression))
1583 switch (expression->kind) {
1585 return is_constant_pointer(expression->select.compound);
1586 case EXPR_UNARY_CAST:
1587 return is_constant_pointer(expression->unary.value);
1593 static bool is_object_with_constant_address(const expression_t *expression)
1595 switch(expression->kind) {
1597 expression_t *compound = expression->select.compound;
1598 type_t *compound_type = compound->base.type;
1599 compound_type = skip_typeref(compound_type);
1600 if(is_type_pointer(compound_type)) {
1601 return is_constant_pointer(compound);
1603 return is_object_with_constant_address(compound);
1606 case EXPR_ARRAY_ACCESS:
1607 return is_constant_pointer(expression->array_access.array_ref)
1608 && is_constant_expression(expression->array_access.index);
1609 case EXPR_UNARY_DEREFERENCE:
1610 return is_constant_pointer(expression->unary.value);
1616 bool is_constant_expression(const expression_t *expression)
1618 switch(expression->kind) {
1621 case EXPR_CHARACTER_CONSTANT:
1622 case EXPR_WIDE_CHARACTER_CONSTANT:
1623 case EXPR_STRING_LITERAL:
1624 case EXPR_WIDE_STRING_LITERAL:
1626 case EXPR_CLASSIFY_TYPE:
1630 case EXPR_BUILTIN_CONSTANT_P:
1633 case EXPR_BUILTIN_SYMBOL:
1634 case EXPR_BUILTIN_PREFETCH:
1638 case EXPR_STATEMENT:
1639 case EXPR_UNARY_POSTFIX_INCREMENT:
1640 case EXPR_UNARY_POSTFIX_DECREMENT:
1641 case EXPR_UNARY_PREFIX_INCREMENT:
1642 case EXPR_UNARY_PREFIX_DECREMENT:
1643 case EXPR_UNARY_ASSUME: /* has VOID type */
1644 case EXPR_UNARY_DEREFERENCE:
1645 case EXPR_BINARY_ASSIGN:
1646 case EXPR_BINARY_MUL_ASSIGN:
1647 case EXPR_BINARY_DIV_ASSIGN:
1648 case EXPR_BINARY_MOD_ASSIGN:
1649 case EXPR_BINARY_ADD_ASSIGN:
1650 case EXPR_BINARY_SUB_ASSIGN:
1651 case EXPR_BINARY_SHIFTLEFT_ASSIGN:
1652 case EXPR_BINARY_SHIFTRIGHT_ASSIGN:
1653 case EXPR_BINARY_BITWISE_AND_ASSIGN:
1654 case EXPR_BINARY_BITWISE_XOR_ASSIGN:
1655 case EXPR_BINARY_BITWISE_OR_ASSIGN:
1656 case EXPR_BINARY_COMMA:
1659 case EXPR_UNARY_TAKE_ADDRESS:
1660 return is_object_with_constant_address(expression->unary.value);
1663 return is_builtin_const_call(expression);
1665 case EXPR_UNARY_NEGATE:
1666 case EXPR_UNARY_PLUS:
1667 case EXPR_UNARY_BITWISE_NEGATE:
1668 case EXPR_UNARY_NOT:
1669 return is_constant_expression(expression->unary.value);
1671 case EXPR_UNARY_CAST:
1672 case EXPR_UNARY_CAST_IMPLICIT:
1673 return is_type_arithmetic(skip_typeref(expression->base.type))
1674 && is_constant_expression(expression->unary.value);
1676 case EXPR_BINARY_ADD:
1677 case EXPR_BINARY_SUB:
1678 case EXPR_BINARY_MUL:
1679 case EXPR_BINARY_DIV:
1680 case EXPR_BINARY_MOD:
1681 case EXPR_BINARY_EQUAL:
1682 case EXPR_BINARY_NOTEQUAL:
1683 case EXPR_BINARY_LESS:
1684 case EXPR_BINARY_LESSEQUAL:
1685 case EXPR_BINARY_GREATER:
1686 case EXPR_BINARY_GREATEREQUAL:
1687 case EXPR_BINARY_BITWISE_AND:
1688 case EXPR_BINARY_BITWISE_OR:
1689 case EXPR_BINARY_BITWISE_XOR:
1690 case EXPR_BINARY_LOGICAL_AND:
1691 case EXPR_BINARY_LOGICAL_OR:
1692 case EXPR_BINARY_SHIFTLEFT:
1693 case EXPR_BINARY_SHIFTRIGHT:
1694 case EXPR_BINARY_BUILTIN_EXPECT:
1695 case EXPR_BINARY_ISGREATER:
1696 case EXPR_BINARY_ISGREATEREQUAL:
1697 case EXPR_BINARY_ISLESS:
1698 case EXPR_BINARY_ISLESSEQUAL:
1699 case EXPR_BINARY_ISLESSGREATER:
1700 case EXPR_BINARY_ISUNORDERED:
1701 return is_constant_expression(expression->binary.left)
1702 && is_constant_expression(expression->binary.right);
1704 case EXPR_COMPOUND_LITERAL:
1705 return is_constant_initializer(expression->compound_literal.initializer);
1707 case EXPR_CONDITIONAL: {
1708 expression_t *condition = expression->conditional.condition;
1709 if(!is_constant_expression(condition))
1712 long val = fold_constant(condition);
1714 return is_constant_expression(expression->conditional.true_expression);
1716 return is_constant_expression(expression->conditional.false_expression);
1719 case EXPR_ARRAY_ACCESS:
1720 return is_constant_expression(expression->array_access.array_ref)
1721 && is_constant_expression(expression->array_access.index);
1723 case EXPR_REFERENCE: {
1724 declaration_t *declaration = expression->reference.declaration;
1725 if(declaration->storage_class == STORAGE_CLASS_ENUM_ENTRY)
1737 panic("invalid expression found (is constant expression)");
1741 * Initialize the AST construction.
1745 obstack_init(&ast_obstack);
1753 obstack_free(&ast_obstack, NULL);
1757 * Set the output stream for the AST printer.
1759 * @param stream the output stream
1761 void ast_set_output(FILE *stream)
1764 type_set_output(stream);
1768 * Allocate an AST object of the given size.
1770 * @param size the size of the object to allocate
1772 * @return A new allocated object in the AST memeory space.
1774 void *(allocate_ast)(size_t size)
1776 return _allocate_ast(size);