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
31 #include "adt/error.h"
33 struct obstack ast_obstack;
38 /** If set, implicit casts are printed. */
39 bool print_implicit_casts = false;
41 /** If set parenthesis are printed to indicate operator precedence. */
42 bool print_parenthesis = false;
44 static void print_statement(const statement_t *statement);
45 static void print_expression_prec(const expression_t *expression, unsigned prec);
47 void change_indent(int delta)
53 void print_indent(void)
55 for(int i = 0; i < indent; ++i)
61 PREC_COMMA = 2, /* , left to right */
62 PREC_ASSIGN = 4, /* = += -= *= /= %= <<= >>= &= ^= |= right to left */
63 PREC_COND = 6, /* ?: right to left */
64 PREC_LOG_OR = 8, /* || left to right */
65 PREC_LOG_AND = 10, /* && left to right */
66 PREC_BIT_OR = 12, /* | left to right */
67 PREC_BIT_XOR = 14, /* ^ left to right */
68 PREC_BIT_AND = 16, /* & left to right */
69 PREC_EQ = 18, /* == != left to right */
70 PREC_CMP = 20, /* < <= > >= left to right */
71 PREC_SHF = 22, /* << >> left to right */
72 PREC_PLUS = 24, /* + - left to right */
73 PREC_MUL = 26, /* * / % left to right */
74 PREC_UNARY = 28, /* ! ~ ++ -- + - (type) * & sizeof right to left */
75 PREC_ACCESS = 30, /* () [] -> . left to right */
76 PREC_PRIM = 32, /* primary */
81 * Returns 1 if a given precedence level has right-to-left
82 * associativity, else -1.
84 * @param precedence the operator precedence
86 static int right_to_left(unsigned precedence) {
87 return (precedence == PREC_ASSIGN || precedence == PREC_COND ||
88 precedence == PREC_UNARY) ? 1 : -1;
92 * Return the precedence of an expression given by its kind.
94 * @param kind the expression kind
96 static unsigned get_expression_precedence(expression_kind_t kind)
98 static const unsigned prec[] = {
99 [EXPR_UNKNOWN] = PREC_PRIM,
100 [EXPR_INVALID] = PREC_PRIM,
101 [EXPR_REFERENCE] = PREC_PRIM,
102 [EXPR_CHARACTER_CONSTANT] = PREC_PRIM,
103 [EXPR_WIDE_CHARACTER_CONSTANT] = PREC_PRIM,
104 [EXPR_CONST] = PREC_PRIM,
105 [EXPR_STRING_LITERAL] = PREC_PRIM,
106 [EXPR_WIDE_STRING_LITERAL] = PREC_PRIM,
107 [EXPR_COMPOUND_LITERAL] = PREC_UNARY,
108 [EXPR_CALL] = PREC_PRIM,
109 [EXPR_CONDITIONAL] = PREC_COND,
110 [EXPR_SELECT] = PREC_ACCESS,
111 [EXPR_ARRAY_ACCESS] = PREC_ACCESS,
112 [EXPR_SIZEOF] = PREC_UNARY,
113 [EXPR_CLASSIFY_TYPE] = PREC_UNARY,
114 [EXPR_ALIGNOF] = PREC_UNARY,
116 [EXPR_FUNCTION] = PREC_PRIM,
117 [EXPR_PRETTY_FUNCTION] = PREC_PRIM,
118 [EXPR_BUILTIN_SYMBOL] = PREC_PRIM,
119 [EXPR_BUILTIN_CONSTANT_P] = PREC_PRIM,
120 [EXPR_BUILTIN_PREFETCH] = PREC_PRIM,
121 [EXPR_OFFSETOF] = PREC_PRIM,
122 [EXPR_VA_START] = PREC_PRIM,
123 [EXPR_VA_ARG] = PREC_PRIM,
124 [EXPR_STATEMENT] = PREC_ACCESS,
126 [EXPR_UNARY_NEGATE] = PREC_UNARY,
127 [EXPR_UNARY_PLUS] = PREC_UNARY,
128 [EXPR_UNARY_BITWISE_NEGATE] = PREC_UNARY,
129 [EXPR_UNARY_NOT] = PREC_UNARY,
130 [EXPR_UNARY_DEREFERENCE] = PREC_UNARY,
131 [EXPR_UNARY_TAKE_ADDRESS] = PREC_UNARY,
132 [EXPR_UNARY_POSTFIX_INCREMENT] = PREC_UNARY,
133 [EXPR_UNARY_POSTFIX_DECREMENT] = PREC_UNARY,
134 [EXPR_UNARY_PREFIX_INCREMENT] = PREC_UNARY,
135 [EXPR_UNARY_PREFIX_DECREMENT] = PREC_UNARY,
136 [EXPR_UNARY_CAST] = PREC_UNARY,
137 [EXPR_UNARY_CAST_IMPLICIT] = PREC_UNARY,
138 [EXPR_UNARY_ASSUME] = PREC_PRIM,
139 [EXPR_UNARY_BITFIELD_EXTRACT] = PREC_ACCESS,
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, '"');
320 static void print_wide_string_literal(
321 const wide_string_literal_expression_t *const wstr)
323 print_quoted_wide_string(&wstr->value, '"');
326 static void print_compound_literal(
327 const compound_literal_expression_t *expression)
330 print_type(expression->type);
332 print_initializer(expression->initializer);
336 * Prints a call expression.
338 * @param call the call expression
340 static void print_call_expression(const call_expression_t *call)
342 unsigned prec = get_expression_precedence(call->base.kind);
343 print_expression_prec(call->function, prec);
345 call_argument_t *argument = call->arguments;
347 while(argument != NULL) {
353 print_expression_prec(argument->expression, PREC_COMMA + 1);
355 argument = argument->next;
361 * Prints a binary expression.
363 * @param binexpr the binary expression
365 static void print_binary_expression(const binary_expression_t *binexpr)
367 unsigned prec = get_expression_precedence(binexpr->base.kind);
368 int r2l = right_to_left(prec);
370 if(binexpr->base.kind == EXPR_BINARY_BUILTIN_EXPECT) {
371 fputs("__builtin_expect(", out);
372 print_expression_prec(binexpr->left, prec);
374 print_expression_prec(binexpr->right, prec);
379 print_expression_prec(binexpr->left, prec + r2l);
380 if (binexpr->base.kind != EXPR_BINARY_COMMA) {
383 switch (binexpr->base.kind) {
384 case EXPR_BINARY_COMMA: fputs(",", out); break;
385 case EXPR_BINARY_ASSIGN: fputs("=", out); break;
386 case EXPR_BINARY_ADD: fputs("+", out); break;
387 case EXPR_BINARY_SUB: fputs("-", out); break;
388 case EXPR_BINARY_MUL: fputs("*", out); break;
389 case EXPR_BINARY_MOD: fputs("%", out); break;
390 case EXPR_BINARY_DIV: fputs("/", out); break;
391 case EXPR_BINARY_BITWISE_OR: fputs("|", out); break;
392 case EXPR_BINARY_BITWISE_AND: fputs("&", out); break;
393 case EXPR_BINARY_BITWISE_XOR: fputs("^", out); break;
394 case EXPR_BINARY_LOGICAL_OR: fputs("||", out); break;
395 case EXPR_BINARY_LOGICAL_AND: fputs("&&", out); break;
396 case EXPR_BINARY_NOTEQUAL: fputs("!=", out); break;
397 case EXPR_BINARY_EQUAL: fputs("==", out); break;
398 case EXPR_BINARY_LESS: fputs("<", out); break;
399 case EXPR_BINARY_LESSEQUAL: fputs("<=", out); break;
400 case EXPR_BINARY_GREATER: fputs(">", out); break;
401 case EXPR_BINARY_GREATEREQUAL: fputs(">=", out); break;
402 case EXPR_BINARY_SHIFTLEFT: fputs("<<", out); break;
403 case EXPR_BINARY_SHIFTRIGHT: fputs(">>", out); break;
405 case EXPR_BINARY_ADD_ASSIGN: fputs("+=", out); break;
406 case EXPR_BINARY_SUB_ASSIGN: fputs("-=", out); break;
407 case EXPR_BINARY_MUL_ASSIGN: fputs("*=", out); break;
408 case EXPR_BINARY_MOD_ASSIGN: fputs("%=", out); break;
409 case EXPR_BINARY_DIV_ASSIGN: fputs("/=", out); break;
410 case EXPR_BINARY_BITWISE_OR_ASSIGN: fputs("|=", out); break;
411 case EXPR_BINARY_BITWISE_AND_ASSIGN: fputs("&=", out); break;
412 case EXPR_BINARY_BITWISE_XOR_ASSIGN: fputs("^=", out); break;
413 case EXPR_BINARY_SHIFTLEFT_ASSIGN: fputs("<<=", out); break;
414 case EXPR_BINARY_SHIFTRIGHT_ASSIGN: fputs(">>=", out); break;
415 default: panic("invalid binexpression found");
418 print_expression_prec(binexpr->right, prec - r2l);
422 * Prints an unary expression.
424 * @param unexpr the unary expression
426 static void print_unary_expression(const unary_expression_t *unexpr)
428 unsigned prec = get_expression_precedence(unexpr->base.kind);
429 switch(unexpr->base.kind) {
430 case EXPR_UNARY_NEGATE: fputs("-", out); break;
431 case EXPR_UNARY_PLUS: fputs("+", out); break;
432 case EXPR_UNARY_NOT: fputs("!", out); break;
433 case EXPR_UNARY_BITWISE_NEGATE: fputs("~", out); break;
434 case EXPR_UNARY_PREFIX_INCREMENT: fputs("++", out); break;
435 case EXPR_UNARY_PREFIX_DECREMENT: fputs("--", out); break;
436 case EXPR_UNARY_DEREFERENCE: fputs("*", out); break;
437 case EXPR_UNARY_TAKE_ADDRESS: fputs("&", out); break;
439 case EXPR_UNARY_BITFIELD_EXTRACT:
440 print_expression_prec(unexpr->value, prec);
443 case EXPR_UNARY_POSTFIX_INCREMENT:
444 print_expression_prec(unexpr->value, prec);
447 case EXPR_UNARY_POSTFIX_DECREMENT:
448 print_expression_prec(unexpr->value, prec);
451 case EXPR_UNARY_CAST_IMPLICIT:
452 if(!print_implicit_casts) {
453 print_expression_prec(unexpr->value, prec);
457 case EXPR_UNARY_CAST:
459 print_type(unexpr->base.type);
462 case EXPR_UNARY_ASSUME:
463 fputs("__assume(", out);
464 print_expression_prec(unexpr->value, PREC_COMMA + 1);
468 panic("invalid unary expression found");
470 print_expression_prec(unexpr->value, prec);
474 * Prints a reference expression.
476 * @param ref the reference expression
478 static void print_reference_expression(const reference_expression_t *ref)
480 fprintf(out, "%s", ref->declaration->symbol->string);
484 * Prints an array expression.
486 * @param expression the array expression
488 static void print_array_expression(const array_access_expression_t *expression)
490 unsigned prec = get_expression_precedence(expression->base.kind);
491 if(!expression->flipped) {
492 print_expression_prec(expression->array_ref, prec);
494 print_expression_prec(expression->index, prec);
497 print_expression_prec(expression->index, prec);
499 print_expression_prec(expression->array_ref, prec);
505 * Prints a typeproperty expression (sizeof or __alignof__).
507 * @param expression the type property expression
509 static void print_typeprop_expression(const typeprop_expression_t *expression)
511 if (expression->base.kind == EXPR_SIZEOF) {
512 fputs("sizeof", out);
514 assert(expression->base.kind == EXPR_ALIGNOF);
515 fputs("__alignof__", out);
517 if(expression->tp_expression != NULL) {
518 /* always print the '()' here, sizeof x is right but unusual */
520 print_expression_prec(expression->tp_expression, PREC_ACCESS);
524 print_type(expression->type);
530 * Prints an builtin symbol.
532 * @param expression the builtin symbol expression
534 static void print_builtin_symbol(const builtin_symbol_expression_t *expression)
536 fputs(expression->symbol->string, out);
540 * Prints a builtin constant expression.
542 * @param expression the builtin constant expression
544 static void print_builtin_constant(const builtin_constant_expression_t *expression)
546 fputs("__builtin_constant_p(", out);
547 print_expression_prec(expression->value, PREC_COMMA + 1);
552 * Prints a builtin prefetch expression.
554 * @param expression the builtin prefetch expression
556 static void print_builtin_prefetch(const builtin_prefetch_expression_t *expression)
558 fputs("__builtin_prefetch(", out);
559 print_expression_prec(expression->adr, PREC_COMMA + 1);
560 if (expression->rw) {
562 print_expression_prec(expression->rw, PREC_COMMA + 1);
564 if (expression->locality) {
566 print_expression_prec(expression->locality, PREC_COMMA + 1);
572 * Prints a conditional expression.
574 * @param expression the conditional expression
576 static void print_conditional(const conditional_expression_t *expression)
578 unsigned prec = get_expression_precedence(expression->base.kind);
580 print_expression_prec(expression->condition, prec);
582 print_expression_prec(expression->true_expression, prec);
584 print_expression_prec(expression->false_expression, prec);
589 * Prints a va_start expression.
591 * @param expression the va_start expression
593 static void print_va_start(const va_start_expression_t *const expression)
595 fputs("__builtin_va_start(", out);
596 print_expression_prec(expression->ap, PREC_COMMA + 1);
598 fputs(expression->parameter->symbol->string, out);
603 * Prints a va_arg expression.
605 * @param expression the va_arg expression
607 static void print_va_arg(const va_arg_expression_t *expression)
609 fputs("__builtin_va_arg(", out);
610 print_expression_prec(expression->ap, PREC_COMMA + 1);
612 print_type(expression->base.type);
617 * Prints a select expression (. or ->).
619 * @param expression the select expression
621 static void print_select(const select_expression_t *expression)
623 unsigned prec = get_expression_precedence(expression->base.kind);
624 print_expression_prec(expression->compound, prec);
625 if(is_type_pointer(expression->compound->base.type)) {
630 fputs(expression->symbol->string, out);
634 * Prints a type classify expression.
636 * @param expr the type classify expression
638 static void print_classify_type_expression(
639 const classify_type_expression_t *const expr)
641 fputs("__builtin_classify_type(", out);
642 print_expression_prec(expr->type_expression, PREC_COMMA + 1);
647 * Prints a designator.
649 * @param designator the designator
651 static void print_designator(const designator_t *designator)
653 for ( ; designator != NULL; designator = designator->next) {
654 if (designator->symbol == NULL) {
656 print_expression_prec(designator->array_index, PREC_ACCESS);
660 fputs(designator->symbol->string, out);
666 * Prints an offsetof expression.
668 * @param expression the offset expression
670 static void print_offsetof_expression(const offsetof_expression_t *expression)
672 fputs("__builtin_offsetof", out);
674 print_type(expression->type);
676 print_designator(expression->designator);
681 * Prints a statement expression.
683 * @param expression the statement expression
685 static void print_statement_expression(const statement_expression_t *expression)
688 print_statement(expression->statement);
693 * Prints an expression with parenthesis if needed.
695 * @param expression the expression to print
696 * @param top_prec the precedence of the user of this expression.
698 static void print_expression_prec(const expression_t *expression, unsigned top_prec)
700 unsigned prec = get_expression_precedence(expression->base.kind);
701 if (print_parenthesis && top_prec != PREC_BOTTOM)
705 switch(expression->kind) {
708 fprintf(out, "$invalid expression$");
710 case EXPR_CHARACTER_CONSTANT:
711 print_character_constant(&expression->conste);
713 case EXPR_WIDE_CHARACTER_CONSTANT:
714 print_wide_character_constant(&expression->conste);
717 print_const(&expression->conste);
720 case EXPR_PRETTY_FUNCTION:
721 case EXPR_STRING_LITERAL:
722 print_string_literal(&expression->string);
724 case EXPR_WIDE_STRING_LITERAL:
725 print_wide_string_literal(&expression->wide_string);
727 case EXPR_COMPOUND_LITERAL:
728 print_compound_literal(&expression->compound_literal);
731 print_call_expression(&expression->call);
734 print_binary_expression(&expression->binary);
737 print_reference_expression(&expression->reference);
739 case EXPR_ARRAY_ACCESS:
740 print_array_expression(&expression->array_access);
743 print_unary_expression(&expression->unary);
747 print_typeprop_expression(&expression->typeprop);
749 case EXPR_BUILTIN_SYMBOL:
750 print_builtin_symbol(&expression->builtin_symbol);
752 case EXPR_BUILTIN_CONSTANT_P:
753 print_builtin_constant(&expression->builtin_constant);
755 case EXPR_BUILTIN_PREFETCH:
756 print_builtin_prefetch(&expression->builtin_prefetch);
758 case EXPR_CONDITIONAL:
759 print_conditional(&expression->conditional);
762 print_va_start(&expression->va_starte);
765 print_va_arg(&expression->va_arge);
768 print_select(&expression->select);
770 case EXPR_CLASSIFY_TYPE:
771 print_classify_type_expression(&expression->classify_type);
774 print_offsetof_expression(&expression->offsetofe);
777 print_statement_expression(&expression->statement);
782 fprintf(out, "some expression of type %d", (int) expression->kind);
790 * Print an compound statement.
792 * @param block the compound statement
794 static void print_compound_statement(const compound_statement_t *block)
799 statement_t *statement = block->statements;
800 while(statement != NULL) {
801 if (statement->base.kind == STATEMENT_CASE_LABEL)
804 print_statement(statement);
806 statement = statement->base.next;
814 * Print a return statement.
816 * @param statement the return statement
818 static void print_return_statement(const return_statement_t *statement)
820 fprintf(out, "return ");
821 if(statement->value != NULL)
822 print_expression(statement->value);
827 * Print an expression statement.
829 * @param statement the expression statement
831 static void print_expression_statement(const expression_statement_t *statement)
833 print_expression(statement->expression);
838 * Print a goto statement.
840 * @param statement the goto statement
842 static void print_goto_statement(const goto_statement_t *statement)
844 fprintf(out, "goto ");
845 fputs(statement->label->symbol->string, out);
846 fprintf(stderr, "(%p)", (void*) statement->label);
851 * Print a label statement.
853 * @param statement the label statement
855 static void print_label_statement(const label_statement_t *statement)
857 fprintf(stderr, "(%p)", (void*) statement->label);
858 fprintf(out, "%s:\n", statement->label->symbol->string);
859 print_statement(statement->statement);
863 * Print an if statement.
865 * @param statement the if statement
867 static void print_if_statement(const if_statement_t *statement)
870 print_expression(statement->condition);
872 print_statement(statement->true_statement);
874 if(statement->false_statement != NULL) {
877 print_statement(statement->false_statement);
882 * Print a switch statement.
884 * @param statement the switch statement
886 static void print_switch_statement(const switch_statement_t *statement)
888 fputs("switch (", out);
889 print_expression(statement->expression);
891 print_statement(statement->body);
895 * Print a case label (including the default label).
897 * @param statement the case label statement
899 static void print_case_label(const case_label_statement_t *statement)
901 if(statement->expression == NULL) {
902 fputs("default:\n", out);
905 print_expression(statement->expression);
906 if (statement->end_range != NULL) {
908 print_expression(statement->end_range);
913 if(statement->statement != NULL) {
914 if (statement->statement->base.kind == STATEMENT_CASE_LABEL) {
918 print_statement(statement->statement);
923 * Print a declaration statement.
925 * @param statement the statement
927 static void print_declaration_statement(
928 const declaration_statement_t *statement)
931 declaration_t *declaration = statement->declarations_begin;
932 for( ; declaration != statement->declarations_end->next;
933 declaration = declaration->next) {
934 if(declaration->storage_class == STORAGE_CLASS_ENUM_ENTRY)
942 print_declaration(declaration);
948 * Print a while statement.
950 * @param statement the statement
952 static void print_while_statement(const while_statement_t *statement)
954 fputs("while (", out);
955 print_expression(statement->condition);
957 print_statement(statement->body);
961 * Print a do-while statement.
963 * @param statement the statement
965 static void print_do_while_statement(const do_while_statement_t *statement)
968 print_statement(statement->body);
970 fputs("while (", out);
971 print_expression(statement->condition);
976 * Print a for statement.
978 * @param statement the statement
980 static void print_for_statement(const for_statement_t *statement)
983 if(statement->scope.declarations != NULL) {
984 assert(statement->initialisation == NULL);
985 print_declaration(statement->scope.declarations);
986 if(statement->scope.declarations->next != NULL) {
987 panic("multiple declarations in for statement not supported yet");
991 if(statement->initialisation) {
992 print_expression(statement->initialisation);
996 if(statement->condition != NULL) {
997 print_expression(statement->condition);
1000 if(statement->step != NULL) {
1001 print_expression(statement->step);
1004 print_statement(statement->body);
1008 * Print assembler constraints.
1010 * @param constraints the constraints
1012 static void print_asm_constraints(asm_constraint_t *constraints)
1014 asm_constraint_t *constraint = constraints;
1015 for( ; constraint != NULL; constraint = constraint->next) {
1016 if(constraint != constraints)
1019 if(constraint->symbol) {
1020 fprintf(out, "[%s] ", constraint->symbol->string);
1022 print_quoted_string(&constraint->constraints, '"');
1024 print_expression(constraint->expression);
1030 * Print assembler clobbers.
1032 * @param clobbers the clobbers
1034 static void print_asm_clobbers(asm_clobber_t *clobbers)
1036 asm_clobber_t *clobber = clobbers;
1037 for( ; clobber != NULL; clobber = clobber->next) {
1038 if(clobber != clobbers)
1041 print_quoted_string(&clobber->clobber, '"');
1046 * Print an assembler statement.
1048 * @param statement the statement
1050 static void print_asm_statement(const asm_statement_t *statement)
1053 if(statement->is_volatile) {
1054 fputs("volatile ", out);
1057 print_quoted_string(&statement->asm_text, '"');
1058 if(statement->inputs == NULL && statement->outputs == NULL
1059 && statement->clobbers == NULL)
1060 goto end_of_print_asm_statement;
1063 print_asm_constraints(statement->inputs);
1064 if(statement->outputs == NULL && statement->clobbers == NULL)
1065 goto end_of_print_asm_statement;
1068 print_asm_constraints(statement->outputs);
1069 if(statement->clobbers == NULL)
1070 goto end_of_print_asm_statement;
1073 print_asm_clobbers(statement->clobbers);
1075 end_of_print_asm_statement:
1080 * Print a statement.
1082 * @param statement the statement
1084 void print_statement(const statement_t *statement)
1086 switch(statement->kind) {
1087 case STATEMENT_EMPTY:
1090 case STATEMENT_COMPOUND:
1091 print_compound_statement(&statement->compound);
1093 case STATEMENT_RETURN:
1094 print_return_statement(&statement->returns);
1096 case STATEMENT_EXPRESSION:
1097 print_expression_statement(&statement->expression);
1099 case STATEMENT_LABEL:
1100 print_label_statement(&statement->label);
1102 case STATEMENT_GOTO:
1103 print_goto_statement(&statement->gotos);
1105 case STATEMENT_CONTINUE:
1106 fputs("continue;\n", out);
1108 case STATEMENT_BREAK:
1109 fputs("break;\n", out);
1112 print_if_statement(&statement->ifs);
1114 case STATEMENT_SWITCH:
1115 print_switch_statement(&statement->switchs);
1117 case STATEMENT_CASE_LABEL:
1118 print_case_label(&statement->case_label);
1120 case STATEMENT_DECLARATION:
1121 print_declaration_statement(&statement->declaration);
1123 case STATEMENT_WHILE:
1124 print_while_statement(&statement->whiles);
1126 case STATEMENT_DO_WHILE:
1127 print_do_while_statement(&statement->do_while);
1130 print_for_statement(&statement->fors);
1133 print_asm_statement(&statement->asms);
1135 case STATEMENT_INVALID:
1136 fprintf(out, "$invalid statement$");
1142 * Print a storage class.
1144 * @param storage_class the storage class
1146 static void print_storage_class(storage_class_tag_t storage_class)
1148 switch(storage_class) {
1149 case STORAGE_CLASS_ENUM_ENTRY:
1150 case STORAGE_CLASS_NONE:
1152 case STORAGE_CLASS_TYPEDEF: fputs("typedef ", out); break;
1153 case STORAGE_CLASS_EXTERN: fputs("extern ", out); break;
1154 case STORAGE_CLASS_STATIC: fputs("static ", out); break;
1155 case STORAGE_CLASS_AUTO: fputs("auto ", out); break;
1156 case STORAGE_CLASS_REGISTER: fputs("register ", out); break;
1157 case STORAGE_CLASS_THREAD: fputs("__thread", out); break;
1158 case STORAGE_CLASS_THREAD_EXTERN: fputs("extern __thread", out); break;
1159 case STORAGE_CLASS_THREAD_STATIC: fputs("static __thread", out); break;
1164 * Print an initializer.
1166 * @param initializer the initializer
1168 void print_initializer(const initializer_t *initializer)
1170 if(initializer == NULL) {
1175 switch(initializer->kind) {
1176 case INITIALIZER_VALUE: {
1177 const initializer_value_t *value = &initializer->value;
1178 print_expression(value->value);
1181 case INITIALIZER_LIST: {
1182 assert(initializer->kind == INITIALIZER_LIST);
1184 const initializer_list_t *list = &initializer->list;
1186 for(size_t i = 0 ; i < list->len; ++i) {
1187 const initializer_t *sub_init = list->initializers[i];
1188 print_initializer(list->initializers[i]);
1189 if(i < list->len-1) {
1190 if(sub_init == NULL || sub_init->kind != INITIALIZER_DESIGNATOR)
1197 case INITIALIZER_STRING:
1198 print_quoted_string(&initializer->string.string, '"');
1200 case INITIALIZER_WIDE_STRING:
1201 print_quoted_wide_string(&initializer->wide_string.string, '"');
1203 case INITIALIZER_DESIGNATOR:
1204 print_designator(initializer->designator.designator);
1209 panic("invalid initializer kind found");
1213 * Print microsoft extended declaration modifiers.
1215 static void print_ms_modifiers(const declaration_t *declaration) {
1216 decl_modifiers_t modifiers = declaration->modifiers;
1218 /* DM_FORCEINLINE handled outside. */
1219 if((modifiers & ~DM_FORCEINLINE) != 0 || declaration->alignment != 0 ||
1220 declaration->get_property_sym != NULL || declaration->put_property_sym != NULL) {
1223 fputs("__declspec", out);
1224 if(modifiers & DM_DLLIMPORT) {
1225 fputs(next, out); next = ", "; fputs("dllimport", out);
1227 if(modifiers & DM_DLLEXPORT) {
1228 fputs(next, out); next = ", "; fputs("dllexport", out);
1230 if(modifiers & DM_THREAD) {
1231 fputs(next, out); next = ", "; fputs("thread", out);
1233 if(modifiers & DM_NAKED) {
1234 fputs(next, out); next = ", "; fputs("naked", out);
1236 if(modifiers & DM_THREAD) {
1237 fputs(next, out); next = ", "; fputs("thread", out);
1239 if(modifiers & DM_SELECTANY) {
1240 fputs(next, out); next = ", "; fputs("selectany", out);
1242 if(modifiers & DM_NOTHROW) {
1243 fputs(next, out); next = ", "; fputs("nothrow", out);
1245 if(modifiers & DM_NORETURN) {
1246 fputs(next, out); next = ", "; fputs("noreturn", out);
1248 if(modifiers & DM_NOINLINE) {
1249 fputs(next, out); next = ", "; fputs("noinline", out);
1251 if(modifiers & DM_DEPRECATED) {
1252 fputs(next, out); next = ", "; fputs("deprecated", out);
1253 if(declaration->deprecated_string != NULL)
1254 fprintf(out, "(\"%s\")", declaration->deprecated_string);
1256 if(declaration->alignment != 0) {
1257 fputs(next, out); next = ", "; fprintf(out, "align(%u)", declaration->alignment);
1259 if(modifiers & DM_RESTRICT) {
1260 fputs(next, out); next = ", "; fputs("restrict", out);
1262 if(modifiers & DM_NOALIAS) {
1263 fputs(next, out); next = ", "; fputs("noalias", out);
1265 if(declaration->get_property_sym != NULL || declaration->put_property_sym != NULL) {
1267 fputs(next, out); next = ", "; fprintf(out, "property(");
1268 if(declaration->get_property_sym != NULL) {
1269 fprintf(out, "get=%s", declaration->get_property_sym->string);
1272 if(declaration->put_property_sym != NULL)
1273 fprintf(out, "%sput=%s", comma, declaration->put_property_sym->string);
1281 * Print a declaration in the NORMAL namespace.
1283 * @param declaration the declaration
1285 static void print_normal_declaration(const declaration_t *declaration)
1287 print_storage_class((storage_class_tag_t) declaration->declared_storage_class);
1288 if(declaration->is_inline) {
1289 if(declaration->modifiers & DM_FORCEINLINE)
1290 fputs("__forceinline ", out);
1292 if(declaration->modifiers & DM_MICROSOFT_INLINE)
1293 fputs("__inline ", out);
1295 fputs("inline ", out);
1298 print_ms_modifiers(declaration);
1299 print_type_ext(declaration->type, declaration->symbol,
1300 &declaration->scope);
1302 if(declaration->type->kind == TYPE_FUNCTION) {
1303 if(declaration->init.statement != NULL) {
1305 print_statement(declaration->init.statement);
1308 } else if(declaration->init.initializer != NULL) {
1310 print_initializer(declaration->init.initializer);
1316 * Prints an expression.
1318 * @param expression the expression
1320 void print_expression(const expression_t *expression) {
1321 print_expression_prec(expression, PREC_BOTTOM);
1325 * Print a declaration.
1327 * @param declaration the declaration
1329 void print_declaration(const declaration_t *declaration)
1331 if(declaration->namespc != NAMESPACE_NORMAL &&
1332 declaration->symbol == NULL)
1335 switch(declaration->namespc) {
1336 case NAMESPACE_NORMAL:
1337 print_normal_declaration(declaration);
1339 case NAMESPACE_STRUCT:
1340 fputs("struct ", out);
1341 fputs(declaration->symbol->string, out);
1343 print_compound_definition(declaration);
1346 case NAMESPACE_UNION:
1347 fputs("union ", out);
1348 fputs(declaration->symbol->string, out);
1350 print_compound_definition(declaration);
1353 case NAMESPACE_ENUM:
1354 fputs("enum ", out);
1355 fputs(declaration->symbol->string, out);
1357 print_enum_definition(declaration);
1364 * Print the AST of a translation unit.
1366 * @param unit the translation unit
1368 void print_ast(const translation_unit_t *unit)
1372 declaration_t *declaration = unit->scope.declarations;
1373 for( ; declaration != NULL; declaration = declaration->next) {
1374 if(declaration->storage_class == STORAGE_CLASS_ENUM_ENTRY)
1376 if(declaration->namespc != NAMESPACE_NORMAL &&
1377 declaration->symbol == NULL)
1381 print_declaration(declaration);
1386 bool is_constant_initializer(const initializer_t *initializer)
1388 switch(initializer->kind) {
1389 case INITIALIZER_STRING:
1390 case INITIALIZER_WIDE_STRING:
1391 case INITIALIZER_DESIGNATOR:
1394 case INITIALIZER_VALUE:
1395 return is_constant_expression(initializer->value.value);
1397 case INITIALIZER_LIST:
1398 for(size_t i = 0; i < initializer->list.len; ++i) {
1399 initializer_t *sub_initializer = initializer->list.initializers[i];
1400 if(!is_constant_initializer(sub_initializer))
1405 panic("invalid initializer kind found");
1408 static bool is_object_with_constant_address(const expression_t *expression)
1410 switch(expression->kind) {
1411 case EXPR_UNARY_DEREFERENCE:
1412 return is_address_constant(expression->unary.value);
1415 if(is_type_pointer(expression->select.compound->base.type)) {
1417 return is_address_constant(expression->select.compound);
1419 return is_object_with_constant_address(expression->select.compound);
1423 case EXPR_ARRAY_ACCESS:
1424 return is_constant_expression(expression->array_access.index)
1425 && is_address_constant(expression->array_access.array_ref);
1427 case EXPR_REFERENCE: {
1428 declaration_t *declaration = expression->reference.declaration;
1429 switch((storage_class_tag_t) declaration->storage_class) {
1430 case STORAGE_CLASS_NONE:
1431 case STORAGE_CLASS_EXTERN:
1432 case STORAGE_CLASS_STATIC:
1444 bool is_address_constant(const expression_t *expression)
1446 switch(expression->kind) {
1447 case EXPR_UNARY_TAKE_ADDRESS:
1448 return is_object_with_constant_address(expression->unary.value);
1450 case EXPR_UNARY_CAST:
1451 return is_type_pointer(skip_typeref(expression->base.type))
1452 && (is_constant_expression(expression->unary.value)
1453 || is_address_constant(expression->unary.value));
1455 case EXPR_BINARY_ADD:
1456 case EXPR_BINARY_SUB: {
1457 expression_t *left = expression->binary.left;
1458 expression_t *right = expression->binary.right;
1460 if(is_type_pointer(skip_typeref(left->base.type))) {
1461 return is_address_constant(left) && is_constant_expression(right);
1462 } else if(is_type_pointer(skip_typeref(right->base.type))) {
1463 return is_constant_expression(left) && is_address_constant(right);
1469 case EXPR_REFERENCE: {
1470 declaration_t *declaration = expression->reference.declaration;
1471 type_t *type = skip_typeref(declaration->type);
1472 if(is_type_function(type))
1474 if(is_type_array(type)) {
1475 return is_object_with_constant_address(expression);
1485 bool is_constant_expression(const expression_t *expression)
1487 switch(expression->kind) {
1490 case EXPR_CHARACTER_CONSTANT:
1491 case EXPR_WIDE_CHARACTER_CONSTANT:
1492 case EXPR_STRING_LITERAL:
1493 case EXPR_WIDE_STRING_LITERAL:
1495 case EXPR_CLASSIFY_TYPE:
1497 case EXPR_PRETTY_FUNCTION:
1500 case EXPR_BUILTIN_CONSTANT_P:
1503 case EXPR_BUILTIN_SYMBOL:
1504 case EXPR_BUILTIN_PREFETCH:
1509 case EXPR_STATEMENT:
1510 case EXPR_UNARY_POSTFIX_INCREMENT:
1511 case EXPR_UNARY_POSTFIX_DECREMENT:
1512 case EXPR_UNARY_PREFIX_INCREMENT:
1513 case EXPR_UNARY_PREFIX_DECREMENT:
1514 case EXPR_UNARY_BITFIELD_EXTRACT:
1515 case EXPR_UNARY_ASSUME: /* has VOID type */
1516 case EXPR_UNARY_DEREFERENCE:
1517 case EXPR_UNARY_TAKE_ADDRESS:
1518 case EXPR_BINARY_ASSIGN:
1519 case EXPR_BINARY_MUL_ASSIGN:
1520 case EXPR_BINARY_DIV_ASSIGN:
1521 case EXPR_BINARY_MOD_ASSIGN:
1522 case EXPR_BINARY_ADD_ASSIGN:
1523 case EXPR_BINARY_SUB_ASSIGN:
1524 case EXPR_BINARY_SHIFTLEFT_ASSIGN:
1525 case EXPR_BINARY_SHIFTRIGHT_ASSIGN:
1526 case EXPR_BINARY_BITWISE_AND_ASSIGN:
1527 case EXPR_BINARY_BITWISE_XOR_ASSIGN:
1528 case EXPR_BINARY_BITWISE_OR_ASSIGN:
1529 case EXPR_BINARY_COMMA:
1532 case EXPR_UNARY_NEGATE:
1533 case EXPR_UNARY_PLUS:
1534 case EXPR_UNARY_BITWISE_NEGATE:
1535 case EXPR_UNARY_NOT:
1536 return is_constant_expression(expression->unary.value);
1538 case EXPR_UNARY_CAST:
1539 case EXPR_UNARY_CAST_IMPLICIT:
1540 return is_type_arithmetic(skip_typeref(expression->base.type))
1541 && is_constant_expression(expression->unary.value);
1543 case EXPR_BINARY_ADD:
1544 case EXPR_BINARY_SUB:
1545 case EXPR_BINARY_MUL:
1546 case EXPR_BINARY_DIV:
1547 case EXPR_BINARY_MOD:
1548 case EXPR_BINARY_EQUAL:
1549 case EXPR_BINARY_NOTEQUAL:
1550 case EXPR_BINARY_LESS:
1551 case EXPR_BINARY_LESSEQUAL:
1552 case EXPR_BINARY_GREATER:
1553 case EXPR_BINARY_GREATEREQUAL:
1554 case EXPR_BINARY_BITWISE_AND:
1555 case EXPR_BINARY_BITWISE_OR:
1556 case EXPR_BINARY_BITWISE_XOR:
1557 case EXPR_BINARY_LOGICAL_AND:
1558 case EXPR_BINARY_LOGICAL_OR:
1559 case EXPR_BINARY_SHIFTLEFT:
1560 case EXPR_BINARY_SHIFTRIGHT:
1561 case EXPR_BINARY_BUILTIN_EXPECT:
1562 case EXPR_BINARY_ISGREATER:
1563 case EXPR_BINARY_ISGREATEREQUAL:
1564 case EXPR_BINARY_ISLESS:
1565 case EXPR_BINARY_ISLESSEQUAL:
1566 case EXPR_BINARY_ISLESSGREATER:
1567 case EXPR_BINARY_ISUNORDERED:
1568 return is_constant_expression(expression->binary.left)
1569 && is_constant_expression(expression->binary.right);
1571 case EXPR_COMPOUND_LITERAL:
1572 return is_constant_initializer(expression->compound_literal.initializer);
1574 case EXPR_CONDITIONAL: {
1575 expression_t *condition = expression->conditional.condition;
1576 if(!is_constant_expression(condition))
1579 long val = fold_constant(condition);
1581 return is_constant_expression(expression->conditional.true_expression);
1583 return is_constant_expression(expression->conditional.false_expression);
1586 case EXPR_ARRAY_ACCESS:
1587 return is_constant_expression(expression->array_access.array_ref)
1588 && is_constant_expression(expression->array_access.index);
1590 case EXPR_REFERENCE: {
1591 declaration_t *declaration = expression->reference.declaration;
1592 if(declaration->storage_class == STORAGE_CLASS_ENUM_ENTRY)
1604 panic("invalid expression found (is constant expression)");
1608 * Initialize the AST construction.
1612 obstack_init(&ast_obstack);
1620 obstack_free(&ast_obstack, NULL);
1624 * Set the output stream for the AST printer.
1626 * @param stream the output stream
1628 void ast_set_output(FILE *stream)
1631 type_set_output(stream);
1635 * Allocate an AST object of the given size.
1637 * @param size the size of the object to allocate
1639 * @return A new allocated object in the AST memeory space.
1641 void *(allocate_ast)(size_t size)
1643 return _allocate_ast(size);