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 if(!print_implicit_casts) {
467 print_expression_prec(unexpr->value, prec);
471 case EXPR_UNARY_CAST:
473 print_type(unexpr->base.type);
476 case EXPR_UNARY_ASSUME:
477 fputs("__assume(", out);
478 print_expression_prec(unexpr->value, PREC_COMMA + 1);
482 panic("invalid unary expression found");
484 print_expression_prec(unexpr->value, prec);
488 * Prints a reference expression.
490 * @param ref the reference expression
492 static void print_reference_expression(const reference_expression_t *ref)
494 fprintf(out, "%s", ref->declaration->symbol->string);
498 * Prints an array expression.
500 * @param expression the array expression
502 static void print_array_expression(const array_access_expression_t *expression)
504 unsigned prec = get_expression_precedence(expression->base.kind);
505 if(!expression->flipped) {
506 print_expression_prec(expression->array_ref, prec);
508 print_expression_prec(expression->index, prec);
511 print_expression_prec(expression->index, prec);
513 print_expression_prec(expression->array_ref, prec);
519 * Prints a typeproperty expression (sizeof or __alignof__).
521 * @param expression the type property expression
523 static void print_typeprop_expression(const typeprop_expression_t *expression)
525 if (expression->base.kind == EXPR_SIZEOF) {
526 fputs("sizeof", out);
528 assert(expression->base.kind == EXPR_ALIGNOF);
529 fputs("__alignof__", out);
531 if(expression->tp_expression != NULL) {
532 /* always print the '()' here, sizeof x is right but unusual */
534 print_expression_prec(expression->tp_expression, PREC_ACCESS);
538 print_type(expression->type);
544 * Prints an builtin symbol.
546 * @param expression the builtin symbol expression
548 static void print_builtin_symbol(const builtin_symbol_expression_t *expression)
550 fputs(expression->symbol->string, out);
554 * Prints a builtin constant expression.
556 * @param expression the builtin constant expression
558 static void print_builtin_constant(const builtin_constant_expression_t *expression)
560 fputs("__builtin_constant_p(", out);
561 print_expression_prec(expression->value, PREC_COMMA + 1);
566 * Prints a builtin prefetch expression.
568 * @param expression the builtin prefetch expression
570 static void print_builtin_prefetch(const builtin_prefetch_expression_t *expression)
572 fputs("__builtin_prefetch(", out);
573 print_expression_prec(expression->adr, PREC_COMMA + 1);
574 if (expression->rw) {
576 print_expression_prec(expression->rw, PREC_COMMA + 1);
578 if (expression->locality) {
580 print_expression_prec(expression->locality, PREC_COMMA + 1);
586 * Prints a conditional expression.
588 * @param expression the conditional expression
590 static void print_conditional(const conditional_expression_t *expression)
592 unsigned prec = get_expression_precedence(expression->base.kind);
594 print_expression_prec(expression->condition, prec);
596 print_expression_prec(expression->true_expression, prec);
598 print_expression_prec(expression->false_expression, prec);
603 * Prints a va_start expression.
605 * @param expression the va_start expression
607 static void print_va_start(const va_start_expression_t *const expression)
609 fputs("__builtin_va_start(", out);
610 print_expression_prec(expression->ap, PREC_COMMA + 1);
612 fputs(expression->parameter->symbol->string, out);
617 * Prints a va_arg expression.
619 * @param expression the va_arg expression
621 static void print_va_arg(const va_arg_expression_t *expression)
623 fputs("__builtin_va_arg(", out);
624 print_expression_prec(expression->ap, PREC_COMMA + 1);
626 print_type(expression->base.type);
631 * Prints a select expression (. or ->).
633 * @param expression the select expression
635 static void print_select(const select_expression_t *expression)
637 unsigned prec = get_expression_precedence(expression->base.kind);
638 print_expression_prec(expression->compound, prec);
639 if(is_type_pointer(skip_typeref(expression->compound->base.type))) {
644 fputs(expression->symbol->string, out);
648 * Prints a type classify expression.
650 * @param expr the type classify expression
652 static void print_classify_type_expression(
653 const classify_type_expression_t *const expr)
655 fputs("__builtin_classify_type(", out);
656 print_expression_prec(expr->type_expression, PREC_COMMA + 1);
661 * Prints a designator.
663 * @param designator the designator
665 static void print_designator(const designator_t *designator)
667 for ( ; designator != NULL; designator = designator->next) {
668 if (designator->symbol == NULL) {
670 print_expression_prec(designator->array_index, PREC_ACCESS);
674 fputs(designator->symbol->string, out);
680 * Prints an offsetof expression.
682 * @param expression the offset expression
684 static void print_offsetof_expression(const offsetof_expression_t *expression)
686 fputs("__builtin_offsetof", out);
688 print_type(expression->type);
690 print_designator(expression->designator);
695 * Prints a statement expression.
697 * @param expression the statement expression
699 static void print_statement_expression(const statement_expression_t *expression)
702 print_statement(expression->statement);
707 * Prints an expression with parenthesis if needed.
709 * @param expression the expression to print
710 * @param top_prec the precedence of the user of this expression.
712 static void print_expression_prec(const expression_t *expression, unsigned top_prec)
714 unsigned prec = get_expression_precedence(expression->base.kind);
715 if (print_parenthesis && top_prec != PREC_BOTTOM)
719 switch(expression->kind) {
722 fprintf(out, "$invalid expression$");
724 case EXPR_CHARACTER_CONSTANT:
725 print_character_constant(&expression->conste);
727 case EXPR_WIDE_CHARACTER_CONSTANT:
728 print_wide_character_constant(&expression->conste);
731 print_const(&expression->conste);
734 print_funcname(&expression->funcname);
736 case EXPR_STRING_LITERAL:
737 print_string_literal(&expression->string);
739 case EXPR_WIDE_STRING_LITERAL:
740 print_wide_string_literal(&expression->wide_string);
742 case EXPR_COMPOUND_LITERAL:
743 print_compound_literal(&expression->compound_literal);
746 print_call_expression(&expression->call);
749 print_binary_expression(&expression->binary);
752 print_reference_expression(&expression->reference);
754 case EXPR_ARRAY_ACCESS:
755 print_array_expression(&expression->array_access);
758 print_unary_expression(&expression->unary);
762 print_typeprop_expression(&expression->typeprop);
764 case EXPR_BUILTIN_SYMBOL:
765 print_builtin_symbol(&expression->builtin_symbol);
767 case EXPR_BUILTIN_CONSTANT_P:
768 print_builtin_constant(&expression->builtin_constant);
770 case EXPR_BUILTIN_PREFETCH:
771 print_builtin_prefetch(&expression->builtin_prefetch);
773 case EXPR_CONDITIONAL:
774 print_conditional(&expression->conditional);
777 print_va_start(&expression->va_starte);
780 print_va_arg(&expression->va_arge);
783 print_select(&expression->select);
785 case EXPR_CLASSIFY_TYPE:
786 print_classify_type_expression(&expression->classify_type);
789 print_offsetof_expression(&expression->offsetofe);
792 print_statement_expression(&expression->statement);
797 fprintf(out, "some expression of type %d", (int) expression->kind);
805 * Print an compound statement.
807 * @param block the compound statement
809 static void print_compound_statement(const compound_statement_t *block)
814 statement_t *statement = block->statements;
815 while(statement != NULL) {
816 if (statement->base.kind == STATEMENT_CASE_LABEL)
819 print_statement(statement);
821 statement = statement->base.next;
829 * Print a return statement.
831 * @param statement the return statement
833 static void print_return_statement(const return_statement_t *statement)
835 fprintf(out, "return ");
836 if(statement->value != NULL)
837 print_expression(statement->value);
842 * Print an expression statement.
844 * @param statement the expression statement
846 static void print_expression_statement(const expression_statement_t *statement)
848 print_expression(statement->expression);
853 * Print a goto statement.
855 * @param statement the goto statement
857 static void print_goto_statement(const goto_statement_t *statement)
859 fprintf(out, "goto ");
860 fputs(statement->label->symbol->string, out);
861 fprintf(stderr, "(%p)", (void*) statement->label);
866 * Print a label statement.
868 * @param statement the label statement
870 static void print_label_statement(const label_statement_t *statement)
872 fprintf(stderr, "(%p)", (void*) statement->label);
873 fprintf(out, "%s:\n", statement->label->symbol->string);
874 print_statement(statement->statement);
878 * Print an if statement.
880 * @param statement the if statement
882 static void print_if_statement(const if_statement_t *statement)
885 print_expression(statement->condition);
887 print_statement(statement->true_statement);
889 if(statement->false_statement != NULL) {
892 print_statement(statement->false_statement);
897 * Print a switch statement.
899 * @param statement the switch statement
901 static void print_switch_statement(const switch_statement_t *statement)
903 fputs("switch (", out);
904 print_expression(statement->expression);
906 print_statement(statement->body);
910 * Print a case label (including the default label).
912 * @param statement the case label statement
914 static void print_case_label(const case_label_statement_t *statement)
916 if(statement->expression == NULL) {
917 fputs("default:\n", out);
920 print_expression(statement->expression);
921 if (statement->end_range != NULL) {
923 print_expression(statement->end_range);
928 if(statement->statement != NULL) {
929 if (statement->statement->base.kind == STATEMENT_CASE_LABEL) {
933 print_statement(statement->statement);
938 * Print a declaration statement.
940 * @param statement the statement
942 static void print_declaration_statement(
943 const declaration_statement_t *statement)
946 declaration_t *declaration = statement->declarations_begin;
947 for( ; declaration != statement->declarations_end->next;
948 declaration = declaration->next) {
949 if(declaration->storage_class == STORAGE_CLASS_ENUM_ENTRY)
957 print_declaration(declaration);
963 * Print a while statement.
965 * @param statement the statement
967 static void print_while_statement(const while_statement_t *statement)
969 fputs("while (", out);
970 print_expression(statement->condition);
972 print_statement(statement->body);
976 * Print a do-while statement.
978 * @param statement the statement
980 static void print_do_while_statement(const do_while_statement_t *statement)
983 print_statement(statement->body);
985 fputs("while (", out);
986 print_expression(statement->condition);
991 * Print a for statement.
993 * @param statement the statement
995 static void print_for_statement(const for_statement_t *statement)
998 if(statement->scope.declarations != NULL) {
999 assert(statement->initialisation == NULL);
1000 print_declaration(statement->scope.declarations);
1001 if(statement->scope.declarations->next != NULL) {
1002 panic("multiple declarations in for statement not supported yet");
1006 if(statement->initialisation) {
1007 print_expression(statement->initialisation);
1011 if(statement->condition != NULL) {
1012 print_expression(statement->condition);
1015 if(statement->step != NULL) {
1016 print_expression(statement->step);
1019 print_statement(statement->body);
1023 * Print assembler arguments.
1025 * @param arguments the arguments
1027 static void print_asm_arguments(asm_argument_t *arguments)
1029 asm_argument_t *argument = arguments;
1030 for( ; argument != NULL; argument = argument->next) {
1031 if(argument != arguments)
1034 if(argument->symbol) {
1035 fprintf(out, "[%s] ", argument->symbol->string);
1037 print_quoted_string(&argument->constraints, '"');
1039 print_expression(argument->expression);
1045 * Print assembler clobbers.
1047 * @param clobbers the clobbers
1049 static void print_asm_clobbers(asm_clobber_t *clobbers)
1051 asm_clobber_t *clobber = clobbers;
1052 for( ; clobber != NULL; clobber = clobber->next) {
1053 if(clobber != clobbers)
1056 print_quoted_string(&clobber->clobber, '"');
1061 * Print an assembler statement.
1063 * @param statement the statement
1065 static void print_asm_statement(const asm_statement_t *statement)
1068 if(statement->is_volatile) {
1069 fputs("volatile ", out);
1072 print_quoted_string(&statement->asm_text, '"');
1073 if(statement->inputs == NULL && statement->outputs == NULL
1074 && statement->clobbers == NULL)
1075 goto end_of_print_asm_statement;
1078 print_asm_arguments(statement->inputs);
1079 if(statement->outputs == NULL && statement->clobbers == NULL)
1080 goto end_of_print_asm_statement;
1083 print_asm_arguments(statement->outputs);
1084 if(statement->clobbers == NULL)
1085 goto end_of_print_asm_statement;
1088 print_asm_clobbers(statement->clobbers);
1090 end_of_print_asm_statement:
1095 * Print a microsoft __try statement.
1097 * @param statement the statement
1099 static void print_ms_try_statement(const ms_try_statement_t *statement)
1101 fputs("__try ", out);
1102 print_statement(statement->try_statement);
1104 if(statement->except_expression != NULL) {
1105 fputs("__except(", out);
1106 print_expression(statement->except_expression);
1109 fputs("__finally ", out);
1111 print_statement(statement->final_statement);
1115 * Print a microsoft __leave statement.
1117 * @param statement the statement
1119 static void print_leave_statement(const leave_statement_t *statement)
1122 fputs("__leave;\n", out);
1126 * Print a statement.
1128 * @param statement the statement
1130 void print_statement(const statement_t *statement)
1132 switch(statement->kind) {
1133 case STATEMENT_EMPTY:
1136 case STATEMENT_COMPOUND:
1137 print_compound_statement(&statement->compound);
1139 case STATEMENT_RETURN:
1140 print_return_statement(&statement->returns);
1142 case STATEMENT_EXPRESSION:
1143 print_expression_statement(&statement->expression);
1145 case STATEMENT_LABEL:
1146 print_label_statement(&statement->label);
1148 case STATEMENT_GOTO:
1149 print_goto_statement(&statement->gotos);
1151 case STATEMENT_CONTINUE:
1152 fputs("continue;\n", out);
1154 case STATEMENT_BREAK:
1155 fputs("break;\n", out);
1158 print_if_statement(&statement->ifs);
1160 case STATEMENT_SWITCH:
1161 print_switch_statement(&statement->switchs);
1163 case STATEMENT_CASE_LABEL:
1164 print_case_label(&statement->case_label);
1166 case STATEMENT_DECLARATION:
1167 print_declaration_statement(&statement->declaration);
1169 case STATEMENT_WHILE:
1170 print_while_statement(&statement->whiles);
1172 case STATEMENT_DO_WHILE:
1173 print_do_while_statement(&statement->do_while);
1176 print_for_statement(&statement->fors);
1179 print_asm_statement(&statement->asms);
1181 case STATEMENT_MS_TRY:
1182 print_ms_try_statement(&statement->ms_try);
1184 case STATEMENT_LEAVE:
1185 print_leave_statement(&statement->leave);
1187 case STATEMENT_INVALID:
1188 fprintf(out, "$invalid statement$");
1194 * Print a storage class.
1196 * @param storage_class the storage class
1198 static void print_storage_class(storage_class_tag_t storage_class)
1200 switch(storage_class) {
1201 case STORAGE_CLASS_ENUM_ENTRY:
1202 case STORAGE_CLASS_NONE:
1204 case STORAGE_CLASS_TYPEDEF: fputs("typedef ", out); break;
1205 case STORAGE_CLASS_EXTERN: fputs("extern ", out); break;
1206 case STORAGE_CLASS_STATIC: fputs("static ", out); break;
1207 case STORAGE_CLASS_AUTO: fputs("auto ", out); break;
1208 case STORAGE_CLASS_REGISTER: fputs("register ", out); break;
1209 case STORAGE_CLASS_THREAD: fputs("__thread", out); break;
1210 case STORAGE_CLASS_THREAD_EXTERN: fputs("extern __thread", out); break;
1211 case STORAGE_CLASS_THREAD_STATIC: fputs("static __thread", out); break;
1216 * Print an initializer.
1218 * @param initializer the initializer
1220 void print_initializer(const initializer_t *initializer)
1222 if(initializer == NULL) {
1227 switch(initializer->kind) {
1228 case INITIALIZER_VALUE: {
1229 const initializer_value_t *value = &initializer->value;
1230 print_expression(value->value);
1233 case INITIALIZER_LIST: {
1234 assert(initializer->kind == INITIALIZER_LIST);
1236 const initializer_list_t *list = &initializer->list;
1238 for(size_t i = 0 ; i < list->len; ++i) {
1239 const initializer_t *sub_init = list->initializers[i];
1240 print_initializer(list->initializers[i]);
1241 if(i < list->len-1) {
1242 if(sub_init == NULL || sub_init->kind != INITIALIZER_DESIGNATOR)
1249 case INITIALIZER_STRING:
1250 print_quoted_string(&initializer->string.string, '"');
1252 case INITIALIZER_WIDE_STRING:
1253 print_quoted_wide_string(&initializer->wide_string.string, '"');
1255 case INITIALIZER_DESIGNATOR:
1256 print_designator(initializer->designator.designator);
1261 panic("invalid initializer kind found");
1265 * Print microsoft extended declaration modifiers.
1267 static void print_ms_modifiers(const declaration_t *declaration) {
1268 if((c_mode & _MS) == 0)
1271 decl_modifiers_t modifiers = declaration->modifiers;
1273 /* DM_FORCEINLINE handled outside. */
1274 if((modifiers & ~DM_FORCEINLINE) != 0 ||
1275 declaration->alignment != 0 || declaration->deprecated != 0 ||
1276 declaration->get_property_sym != NULL || declaration->put_property_sym != NULL) {
1279 fputs("__declspec", out);
1280 if(modifiers & DM_DLLIMPORT) {
1281 fputs(next, out); next = ", "; fputs("dllimport", out);
1283 if(modifiers & DM_DLLEXPORT) {
1284 fputs(next, out); next = ", "; fputs("dllexport", out);
1286 if(modifiers & DM_THREAD) {
1287 fputs(next, out); next = ", "; fputs("thread", out);
1289 if(modifiers & DM_NAKED) {
1290 fputs(next, out); next = ", "; fputs("naked", out);
1292 if(modifiers & DM_THREAD) {
1293 fputs(next, out); next = ", "; fputs("thread", out);
1295 if(modifiers & DM_SELECTANY) {
1296 fputs(next, out); next = ", "; fputs("selectany", out);
1298 if(modifiers & DM_NOTHROW) {
1299 fputs(next, out); next = ", "; fputs("nothrow", out);
1301 if(modifiers & DM_NORETURN) {
1302 fputs(next, out); next = ", "; fputs("noreturn", out);
1304 if(modifiers & DM_NOINLINE) {
1305 fputs(next, out); next = ", "; fputs("noinline", out);
1307 if(declaration->deprecated != 0) {
1308 fputs(next, out); next = ", "; fputs("deprecated", out);
1309 if(declaration->deprecated_string != NULL)
1310 fprintf(out, "(\"%s\")", declaration->deprecated_string);
1312 if(declaration->alignment != 0) {
1313 fputs(next, out); next = ", "; fprintf(out, "align(%u)", declaration->alignment);
1315 if(modifiers & DM_RESTRICT) {
1316 fputs(next, out); next = ", "; fputs("restrict", out);
1318 if(modifiers & DM_NOALIAS) {
1319 fputs(next, out); next = ", "; fputs("noalias", out);
1321 if(declaration->get_property_sym != NULL || declaration->put_property_sym != NULL) {
1323 fputs(next, out); next = ", "; fprintf(out, "property(");
1324 if(declaration->get_property_sym != NULL) {
1325 fprintf(out, "get=%s", declaration->get_property_sym->string);
1328 if(declaration->put_property_sym != NULL)
1329 fprintf(out, "%sput=%s", comma, declaration->put_property_sym->string);
1337 * Print a declaration in the NORMAL namespace.
1339 * @param declaration the declaration
1341 static void print_normal_declaration(const declaration_t *declaration)
1343 print_storage_class((storage_class_tag_t) declaration->declared_storage_class);
1344 if(declaration->is_inline) {
1345 if(declaration->modifiers & DM_FORCEINLINE)
1346 fputs("__forceinline ", out);
1348 if(declaration->modifiers & DM_MICROSOFT_INLINE)
1349 fputs("__inline ", out);
1351 fputs("inline ", out);
1354 print_ms_modifiers(declaration);
1355 print_type_ext(declaration->type, declaration->symbol,
1356 &declaration->scope);
1358 if(declaration->type->kind == TYPE_FUNCTION) {
1359 if(declaration->init.statement != NULL) {
1361 print_statement(declaration->init.statement);
1364 } else if(declaration->init.initializer != NULL) {
1366 print_initializer(declaration->init.initializer);
1372 * Prints an expression.
1374 * @param expression the expression
1376 void print_expression(const expression_t *expression) {
1377 print_expression_prec(expression, PREC_BOTTOM);
1381 * Print a declaration.
1383 * @param declaration the declaration
1385 void print_declaration(const declaration_t *declaration)
1387 if(declaration->namespc != NAMESPACE_NORMAL &&
1388 declaration->symbol == NULL)
1391 switch(declaration->namespc) {
1392 case NAMESPACE_NORMAL:
1393 print_normal_declaration(declaration);
1395 case NAMESPACE_STRUCT:
1396 fputs("struct ", out);
1397 fputs(declaration->symbol->string, out);
1399 print_compound_definition(declaration);
1402 case NAMESPACE_UNION:
1403 fputs("union ", out);
1404 fputs(declaration->symbol->string, out);
1406 print_compound_definition(declaration);
1409 case NAMESPACE_ENUM:
1410 fputs("enum ", out);
1411 fputs(declaration->symbol->string, out);
1413 print_enum_definition(declaration);
1420 * Print the AST of a translation unit.
1422 * @param unit the translation unit
1424 void print_ast(const translation_unit_t *unit)
1428 declaration_t *declaration = unit->scope.declarations;
1429 for( ; declaration != NULL; declaration = declaration->next) {
1430 if(declaration->storage_class == STORAGE_CLASS_ENUM_ENTRY)
1432 if(declaration->namespc != NAMESPACE_NORMAL &&
1433 declaration->symbol == NULL)
1437 print_declaration(declaration);
1442 bool is_constant_initializer(const initializer_t *initializer)
1444 switch(initializer->kind) {
1445 case INITIALIZER_STRING:
1446 case INITIALIZER_WIDE_STRING:
1447 case INITIALIZER_DESIGNATOR:
1450 case INITIALIZER_VALUE:
1451 return is_constant_expression(initializer->value.value);
1453 case INITIALIZER_LIST:
1454 for(size_t i = 0; i < initializer->list.len; ++i) {
1455 initializer_t *sub_initializer = initializer->list.initializers[i];
1456 if(!is_constant_initializer(sub_initializer))
1461 panic("invalid initializer kind found");
1464 static bool is_object_with_linker_constant_address(const expression_t *expression)
1466 switch(expression->kind) {
1467 case EXPR_UNARY_DEREFERENCE:
1468 return is_address_constant(expression->unary.value);
1471 if(is_type_pointer(expression->select.compound->base.type)) {
1473 return is_address_constant(expression->select.compound);
1475 return is_object_with_linker_constant_address(expression->select.compound);
1479 case EXPR_ARRAY_ACCESS:
1480 return is_constant_expression(expression->array_access.index)
1481 && is_address_constant(expression->array_access.array_ref);
1483 case EXPR_REFERENCE: {
1484 declaration_t *declaration = expression->reference.declaration;
1485 switch((storage_class_tag_t) declaration->storage_class) {
1486 case STORAGE_CLASS_NONE:
1487 case STORAGE_CLASS_EXTERN:
1488 case STORAGE_CLASS_STATIC:
1500 bool is_address_constant(const expression_t *expression)
1502 switch(expression->kind) {
1503 case EXPR_UNARY_TAKE_ADDRESS:
1504 return is_object_with_linker_constant_address(expression->unary.value);
1506 case EXPR_UNARY_DEREFERENCE: {
1508 = revert_automatic_type_conversion(expression->unary.value);
1509 /* dereferencing a function is a NOP */
1510 if(is_type_function(real_type)) {
1511 return is_address_constant(expression->unary.value);
1517 case EXPR_UNARY_CAST:
1518 return is_type_pointer(skip_typeref(expression->base.type))
1519 && (is_constant_expression(expression->unary.value)
1520 || is_address_constant(expression->unary.value));
1522 case EXPR_BINARY_ADD:
1523 case EXPR_BINARY_SUB: {
1524 expression_t *left = expression->binary.left;
1525 expression_t *right = expression->binary.right;
1527 if(is_type_pointer(skip_typeref(left->base.type))) {
1528 return is_address_constant(left) && is_constant_expression(right);
1529 } else if(is_type_pointer(skip_typeref(right->base.type))) {
1530 return is_constant_expression(left) && is_address_constant(right);
1536 case EXPR_REFERENCE: {
1537 declaration_t *declaration = expression->reference.declaration;
1538 type_t *type = skip_typeref(declaration->type);
1539 if(is_type_function(type))
1541 if(is_type_array(type)) {
1542 return is_object_with_linker_constant_address(expression);
1552 static bool is_builtin_const_call(const expression_t *expression)
1554 expression_t *function = expression->call.function;
1555 if (function->kind != EXPR_BUILTIN_SYMBOL) {
1559 symbol_t *symbol = function->builtin_symbol.symbol;
1561 switch (symbol->ID) {
1562 case T___builtin_huge_val:
1563 case T___builtin_nan:
1564 case T___builtin_nanf:
1565 case T___builtin_nand:
1572 static bool is_constant_pointer(const expression_t *expression)
1574 if (is_constant_expression(expression))
1577 switch (expression->kind) {
1578 case EXPR_UNARY_CAST:
1579 return is_constant_pointer(expression->unary.value);
1585 static bool is_object_with_constant_address(const expression_t *expression)
1587 switch(expression->kind) {
1589 expression_t *compound = expression->select.compound;
1590 type_t *compound_type = compound->base.type;
1591 compound_type = skip_typeref(compound_type);
1592 if(is_type_pointer(compound_type)) {
1593 return is_constant_pointer(compound);
1595 return is_object_with_constant_address(compound);
1598 case EXPR_ARRAY_ACCESS:
1599 return is_constant_pointer(expression->array_access.array_ref)
1600 && is_constant_expression(expression->array_access.index);
1601 case EXPR_UNARY_DEREFERENCE:
1602 return is_constant_pointer(expression->unary.value);
1608 bool is_constant_expression(const expression_t *expression)
1610 switch(expression->kind) {
1613 case EXPR_CHARACTER_CONSTANT:
1614 case EXPR_WIDE_CHARACTER_CONSTANT:
1615 case EXPR_STRING_LITERAL:
1616 case EXPR_WIDE_STRING_LITERAL:
1618 case EXPR_CLASSIFY_TYPE:
1622 case EXPR_BUILTIN_CONSTANT_P:
1625 case EXPR_BUILTIN_SYMBOL:
1626 case EXPR_BUILTIN_PREFETCH:
1630 case EXPR_STATEMENT:
1631 case EXPR_UNARY_POSTFIX_INCREMENT:
1632 case EXPR_UNARY_POSTFIX_DECREMENT:
1633 case EXPR_UNARY_PREFIX_INCREMENT:
1634 case EXPR_UNARY_PREFIX_DECREMENT:
1635 case EXPR_UNARY_ASSUME: /* has VOID type */
1636 case EXPR_UNARY_DEREFERENCE:
1637 case EXPR_BINARY_ASSIGN:
1638 case EXPR_BINARY_MUL_ASSIGN:
1639 case EXPR_BINARY_DIV_ASSIGN:
1640 case EXPR_BINARY_MOD_ASSIGN:
1641 case EXPR_BINARY_ADD_ASSIGN:
1642 case EXPR_BINARY_SUB_ASSIGN:
1643 case EXPR_BINARY_SHIFTLEFT_ASSIGN:
1644 case EXPR_BINARY_SHIFTRIGHT_ASSIGN:
1645 case EXPR_BINARY_BITWISE_AND_ASSIGN:
1646 case EXPR_BINARY_BITWISE_XOR_ASSIGN:
1647 case EXPR_BINARY_BITWISE_OR_ASSIGN:
1648 case EXPR_BINARY_COMMA:
1651 case EXPR_UNARY_TAKE_ADDRESS:
1652 return is_object_with_constant_address(expression->unary.value);
1655 return is_builtin_const_call(expression);
1657 case EXPR_UNARY_NEGATE:
1658 case EXPR_UNARY_PLUS:
1659 case EXPR_UNARY_BITWISE_NEGATE:
1660 case EXPR_UNARY_NOT:
1661 return is_constant_expression(expression->unary.value);
1663 case EXPR_UNARY_CAST:
1664 case EXPR_UNARY_CAST_IMPLICIT:
1665 return is_type_arithmetic(skip_typeref(expression->base.type))
1666 && is_constant_expression(expression->unary.value);
1668 case EXPR_BINARY_ADD:
1669 case EXPR_BINARY_SUB:
1670 case EXPR_BINARY_MUL:
1671 case EXPR_BINARY_DIV:
1672 case EXPR_BINARY_MOD:
1673 case EXPR_BINARY_EQUAL:
1674 case EXPR_BINARY_NOTEQUAL:
1675 case EXPR_BINARY_LESS:
1676 case EXPR_BINARY_LESSEQUAL:
1677 case EXPR_BINARY_GREATER:
1678 case EXPR_BINARY_GREATEREQUAL:
1679 case EXPR_BINARY_BITWISE_AND:
1680 case EXPR_BINARY_BITWISE_OR:
1681 case EXPR_BINARY_BITWISE_XOR:
1682 case EXPR_BINARY_LOGICAL_AND:
1683 case EXPR_BINARY_LOGICAL_OR:
1684 case EXPR_BINARY_SHIFTLEFT:
1685 case EXPR_BINARY_SHIFTRIGHT:
1686 case EXPR_BINARY_BUILTIN_EXPECT:
1687 case EXPR_BINARY_ISGREATER:
1688 case EXPR_BINARY_ISGREATEREQUAL:
1689 case EXPR_BINARY_ISLESS:
1690 case EXPR_BINARY_ISLESSEQUAL:
1691 case EXPR_BINARY_ISLESSGREATER:
1692 case EXPR_BINARY_ISUNORDERED:
1693 return is_constant_expression(expression->binary.left)
1694 && is_constant_expression(expression->binary.right);
1696 case EXPR_COMPOUND_LITERAL:
1697 return is_constant_initializer(expression->compound_literal.initializer);
1699 case EXPR_CONDITIONAL: {
1700 expression_t *condition = expression->conditional.condition;
1701 if(!is_constant_expression(condition))
1704 long val = fold_constant(condition);
1706 return is_constant_expression(expression->conditional.true_expression);
1708 return is_constant_expression(expression->conditional.false_expression);
1711 case EXPR_ARRAY_ACCESS:
1712 return is_constant_expression(expression->array_access.array_ref)
1713 && is_constant_expression(expression->array_access.index);
1715 case EXPR_REFERENCE: {
1716 declaration_t *declaration = expression->reference.declaration;
1717 if(declaration->storage_class == STORAGE_CLASS_ENUM_ENTRY)
1729 panic("invalid expression found (is constant expression)");
1733 * Initialize the AST construction.
1737 obstack_init(&ast_obstack);
1745 obstack_free(&ast_obstack, NULL);
1749 * Set the output stream for the AST printer.
1751 * @param stream the output stream
1753 void ast_set_output(FILE *stream)
1756 type_set_output(stream);
1760 * Allocate an AST object of the given size.
1762 * @param size the size of the object to allocate
1764 * @return A new allocated object in the AST memeory space.
1766 void *(allocate_ast)(size_t size)
1768 return _allocate_ast(size);