4 * Copyright (C) 2003 Linus Torvalds, all rights reserved.
6 * Print out results of parsing for debugging and testing.
21 #include "expression.h"
24 * Symbol type printout. The type system is by far the most
25 * complicated part of C - everything else is trivial.
27 const char *modifier_string(unsigned long mod
)
29 static char buffer
[100];
31 const char *res
,**ptr
, *names
[] = {
32 "auto", "register", "static", "extern",
33 "const", "volatile", "signed", "unsigned",
34 "char", "short", "long", "long",
38 while ((res
= *ptr
++) != NULL
) {
42 while ((c
= *res
++) != '\0')
52 const char *type_string(unsigned int modifiers
, struct symbol
*sym
)
57 if (sym
== &int_type
) {
58 if (modifiers
& (SYM_CHAR
| SYM_SHORT
| SYM_LONG
))
64 if (sym
== &void_type
)
66 if (sym
== &vector_type
)
71 return show_token(sym
->token
);
75 static void show_one_symbol(struct symbol
*sym
, void *sep
, int flags
)
78 if (!(flags
& ITERATE_LAST
))
79 printf("%s", (const char *)sep
);
82 void show_symbol_list(struct symbol_list
*list
, const char *sep
)
84 symbol_iterate(list
, show_one_symbol
, (void *)sep
);
87 void show_type_list(struct symbol
*sym
)
95 void show_type(struct symbol
*sym
)
97 struct ctype
*ctype
= &sym
->ctype
;
100 printf(" <typeless>");
104 printf("%s", modifier_string(sym
->ctype
.modifiers
));
109 show_type(sym
->ctype
.base_type
);
115 show_type(sym
->ctype
.base_type
);
117 show_symbol_list(sym
->arguments
, ", ");
122 printf("<array of>(");
123 show_type(sym
->ctype
.base_type
);
128 printf("%s", type_string(ctype
->modifiers
, ctype
->base_type
));
133 void show_symbol(struct symbol
*sym
)
136 printf("<anon symbol>");
139 printf("Symbol %s: ", show_token(sym
->token
));
144 show_statement(sym
->stmt
);
153 * Print out a statement
155 void show_statement(struct statement
*stmt
)
158 printf("\t<nostatement>");
161 switch (stmt
->type
) {
164 show_expression(stmt
->expression
);
170 show_symbol_list(stmt
->syms
, "\n\t");
173 show_statement_list(stmt
->stmts
, ";\n");
176 case STMT_EXPRESSION
:
178 show_expression(stmt
->expression
);
182 show_expression(stmt
->if_conditional
);
184 show_statement(stmt
->if_true
);
185 if (stmt
->if_false
) {
187 show_statement(stmt
->if_false
);
191 printf("\tswitch (");
192 show_expression(stmt
->switch_expression
);
194 show_statement(stmt
->switch_statement
);
198 if (!stmt
->case_expression
)
202 show_expression(stmt
->case_expression
);
205 show_expression(stmt
->case_to
);
209 show_statement(stmt
->case_statement
);
218 show_expression(stmt
->e1
);
220 show_statement(stmt
->iterate
);
225 show_statement(stmt
->iterate
);
227 show_expression(stmt
->e1
);
233 show_expression(stmt
->e1
);
235 show_expression(stmt
->e2
);
237 show_expression(stmt
->e3
);
239 show_statement(stmt
->iterate
);
247 static void show_one_statement(struct statement
*stmt
, void *sep
, int flags
)
249 show_statement(stmt
);
250 if (!(flags
& ITERATE_LAST
))
251 printf("%s", (const char *)sep
);
254 void show_statement_list(struct statement_list
*stmt
, const char *sep
)
256 statement_iterate(stmt
, show_one_statement
, (void *)sep
);
260 * Print out an expression
262 void show_expression(struct expression
*expr
)
268 switch (expr
->type
) {
270 show_expression(expr
->left
);
271 printf(" %s ", show_special(expr
->op
));
272 show_expression(expr
->right
);
275 printf("%s<", show_special(expr
->op
));
276 show_expression(expr
->unop
);
280 show_expression(expr
->unop
);
281 printf(" %s ", show_special(expr
->op
));
284 printf("%s", show_token(expr
->token
));
288 warn(expr
->token
, "undefined symbol '%s'", show_token(expr
->token
));
289 printf("<nosymbol>");
292 printf("<%s:", show_token(expr
->symbol
->token
));
293 show_type(expr
->symbol
);
297 show_expression(expr
->deref
);
298 printf("%s", show_special(expr
->op
));
299 printf("%s", show_token(expr
->member
));
303 show_type(expr
->cast_type
);
305 show_expression(expr
->cast_expression
);