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",
35 "typdef", "structof", "unionof", "enum",
36 "typeof", "attribute",
40 while ((res
= *ptr
++) != NULL
) {
44 while ((c
= *res
++) != '\0')
54 void show_struct_member(struct symbol
*sym
, void *data
, int flags
)
56 if (flags
& ITERATE_FIRST
)
58 printf("%s", show_token(sym
->token
));
59 if (flags
& ITERATE_LAST
)
65 void show_type_details(unsigned int modifiers
, struct symbol
*sym
)
72 if (sym
== &int_type
) {
73 if (modifiers
& (MOD_CHAR
| MOD_SHORT
| MOD_LONG
))
78 if (sym
== &fp_type
) {
82 if (sym
== &void_type
) {
86 if (sym
== &vector_type
) {
90 if (sym
== &bad_type
) {
94 if (sym
->type
== SYM_STRUCT
) {
95 printf(" struct %s", show_token(sym
->token
));
96 symbol_iterate(sym
->symbol_list
, show_struct_member
, NULL
);
99 if (sym
->type
== SYM_UNION
) {
100 printf(" union %s", show_token(sym
->token
));
101 symbol_iterate(sym
->symbol_list
, show_struct_member
, NULL
);
104 if (sym
->type
== SYM_ENUM
) {
105 printf(" enum %s", show_token(sym
->token
));
109 if (sym
->type
== SYM_PTR
)
111 else if (sym
->type
== SYM_FN
)
114 printf(" strange type %d", sym
->type
);
117 printf(" '%s'", show_token(sym
->token
));
119 printf("%s", modifier_string(sym
->ctype
.modifiers
));
120 show_type_details(sym
->ctype
.modifiers
, sym
->ctype
.base_type
);
123 static void show_one_symbol(struct symbol
*sym
, void *sep
, int flags
)
126 if (!(flags
& ITERATE_LAST
))
127 printf("%s", (const char *)sep
);
130 void show_symbol_list(struct symbol_list
*list
, const char *sep
)
132 symbol_iterate(list
, show_one_symbol
, (void *)sep
);
135 void show_type_list(struct symbol
*sym
)
143 void show_type(struct symbol
*sym
)
145 struct ctype
*ctype
= &sym
->ctype
;
148 printf(" <typeless>");
152 printf("%s", modifier_string(sym
->ctype
.modifiers
));
156 show_type(sym
->ctype
.base_type
);
162 show_type(sym
->ctype
.base_type
);
164 show_symbol_list(sym
->arguments
, ", ");
169 printf("<array of>");
170 show_type(sym
->ctype
.base_type
);
175 show_type_details(ctype
->modifiers
, ctype
->base_type
);
180 void show_symbol(struct symbol
*sym
)
183 printf("<anon symbol>");
188 printf("%s: ", show_token(sym
->token
));
191 show_statement(sym
->stmt
);
195 printf(": %s", show_token(sym
->token
));
202 * Print out a statement
204 void show_statement(struct statement
*stmt
)
207 printf("\t<nostatement>");
210 switch (stmt
->type
) {
213 show_expression(stmt
->expression
);
219 show_symbol_list(stmt
->syms
, "\n\t");
222 show_statement_list(stmt
->stmts
, ";\n");
225 case STMT_EXPRESSION
:
227 show_expression(stmt
->expression
);
231 show_expression(stmt
->if_conditional
);
233 show_statement(stmt
->if_true
);
234 if (stmt
->if_false
) {
236 show_statement(stmt
->if_false
);
240 printf("\tswitch (");
241 show_expression(stmt
->switch_expression
);
243 show_statement(stmt
->switch_statement
);
247 if (!stmt
->case_expression
)
251 show_expression(stmt
->case_expression
);
254 show_expression(stmt
->case_to
);
258 show_statement(stmt
->case_statement
);
267 show_expression(stmt
->e1
);
269 show_statement(stmt
->iterate
);
274 show_statement(stmt
->iterate
);
276 show_expression(stmt
->e1
);
282 show_expression(stmt
->e1
);
284 show_expression(stmt
->e2
);
286 show_expression(stmt
->e3
);
288 show_statement(stmt
->iterate
);
296 static void show_one_statement(struct statement
*stmt
, void *sep
, int flags
)
298 show_statement(stmt
);
299 if (!(flags
& ITERATE_LAST
))
300 printf("%s", (const char *)sep
);
303 void show_statement_list(struct statement_list
*stmt
, const char *sep
)
305 statement_iterate(stmt
, show_one_statement
, (void *)sep
);
309 * Print out an expression
311 void show_expression(struct expression
*expr
)
317 switch (expr
->type
) {
319 show_expression(expr
->left
);
320 printf(" %s ", show_special(expr
->op
));
321 show_expression(expr
->right
);
324 printf("%s<", show_special(expr
->op
));
325 show_expression(expr
->unop
);
329 show_expression(expr
->unop
);
330 printf(" %s ", show_special(expr
->op
));
333 printf("%s", show_token(expr
->token
));
337 warn(expr
->token
, "undefined symbol '%s'", show_token(expr
->token
));
338 printf("<nosymbol>");
341 printf("<%s:", show_token(expr
->symbol
->token
));
342 show_type(expr
->symbol
);
346 show_expression(expr
->deref
);
347 printf("%s", show_special(expr
->op
));
348 printf("%s", show_token(expr
->member
));
352 show_type(expr
->cast_type
);
354 show_expression(expr
->cast_expression
);