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
));
108 show_type(sym
->ctype
.base_type
);
114 show_type(sym
->ctype
.base_type
);
116 show_symbol_list(sym
->arguments
, ", ");
121 printf("<array of>");
122 show_type(sym
->ctype
.base_type
);
127 printf("%s", type_string(ctype
->modifiers
, ctype
->base_type
));
132 void show_symbol(struct symbol
*sym
)
135 printf("<anon symbol>");
140 printf("%s: ", show_token(sym
->token
));
143 show_statement(sym
->stmt
);
147 printf(": %s", show_token(sym
->token
));
154 * Print out a statement
156 void show_statement(struct statement
*stmt
)
159 printf("\t<nostatement>");
162 switch (stmt
->type
) {
165 show_expression(stmt
->expression
);
171 show_symbol_list(stmt
->syms
, "\n\t");
174 show_statement_list(stmt
->stmts
, ";\n");
177 case STMT_EXPRESSION
:
179 show_expression(stmt
->expression
);
183 show_expression(stmt
->if_conditional
);
185 show_statement(stmt
->if_true
);
186 if (stmt
->if_false
) {
188 show_statement(stmt
->if_false
);
192 printf("\tswitch (");
193 show_expression(stmt
->switch_expression
);
195 show_statement(stmt
->switch_statement
);
199 if (!stmt
->case_expression
)
203 show_expression(stmt
->case_expression
);
206 show_expression(stmt
->case_to
);
210 show_statement(stmt
->case_statement
);
219 show_expression(stmt
->e1
);
221 show_statement(stmt
->iterate
);
226 show_statement(stmt
->iterate
);
228 show_expression(stmt
->e1
);
234 show_expression(stmt
->e1
);
236 show_expression(stmt
->e2
);
238 show_expression(stmt
->e3
);
240 show_statement(stmt
->iterate
);
248 static void show_one_statement(struct statement
*stmt
, void *sep
, int flags
)
250 show_statement(stmt
);
251 if (!(flags
& ITERATE_LAST
))
252 printf("%s", (const char *)sep
);
255 void show_statement_list(struct statement_list
*stmt
, const char *sep
)
257 statement_iterate(stmt
, show_one_statement
, (void *)sep
);
261 * Print out an expression
263 void show_expression(struct expression
*expr
)
269 switch (expr
->type
) {
271 show_expression(expr
->left
);
272 printf(" %s ", show_special(expr
->op
));
273 show_expression(expr
->right
);
276 printf("%s<", show_special(expr
->op
));
277 show_expression(expr
->unop
);
281 show_expression(expr
->unop
);
282 printf(" %s ", show_special(expr
->op
));
285 printf("%s", show_token(expr
->token
));
289 warn(expr
->token
, "undefined symbol '%s'", show_token(expr
->token
));
290 printf("<nosymbol>");
293 printf("<%s:", show_token(expr
->symbol
->token
));
294 show_type(expr
->symbol
);
298 show_expression(expr
->deref
);
299 printf("%s", show_special(expr
->op
));
300 printf("%s", show_token(expr
->member
));
304 show_type(expr
->cast_type
);
306 show_expression(expr
->cast_expression
);