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
) {
43 while ((c
= *res
++) != '\0')
53 void show_struct_member(struct symbol
*sym
, void *data
, int flags
)
55 if (flags
& ITERATE_FIRST
)
57 printf("%s:%d:%d at offset %ld", show_token(sym
->token
), sym
->bit_size
, sym
->alignment
, sym
->offset
);
58 if (flags
& ITERATE_LAST
)
64 static void show_one_symbol(struct symbol
*sym
, void *sep
, int flags
)
67 if (!(flags
& ITERATE_LAST
))
68 printf("%s", (const char *)sep
);
71 void show_symbol_list(struct symbol_list
*list
, const char *sep
)
73 symbol_iterate(list
, show_one_symbol
, (void *)sep
);
76 void show_type_list(struct symbol
*sym
)
84 void show_type(struct symbol
*sym
)
87 static struct ctype_name
{
91 { & char_ctype
, "char" },
92 { &uchar_ctype
, "unsigned char" },
93 { & short_ctype
, "short" },
94 { &ushort_ctype
, "unsigned short" },
95 { & int_ctype
, "int" },
96 { &uint_ctype
, "unsigned int" },
97 { & long_ctype
, "long" },
98 { &ulong_ctype
, "unsigned long" },
99 { & llong_ctype
, "long long" },
100 { &ullong_ctype
, "unsigned long long" },
102 { &void_ctype
, "void" },
103 { &bool_ctype
, "bool" },
104 { &string_ctype
, "string" },
106 { &float_ctype
, "float" },
107 { &double_ctype
, "double" },
108 { &ldouble_ctype
,"long double" },
114 for (i
= 0; i
< sizeof(typenames
)/sizeof(typenames
[0]); i
++) {
115 if (typenames
[i
].sym
== sym
) {
116 printf("%s", typenames
[i
].name
);
121 printf("%s", modifier_string(sym
->ctype
.modifiers
));
125 show_type(sym
->ctype
.base_type
);
130 show_type(sym
->ctype
.base_type
);
136 show_type(sym
->ctype
.base_type
);
141 printf("struct %s", show_token(sym
->token
));
145 printf("union %s", show_token(sym
->token
));
149 printf("enum %s", show_token(sym
->token
));
153 struct symbol
*type
= sym
->ctype
.base_type
;
158 printf(": %s", show_token(sym
->token
));
163 printf("strange type %d '%s' of type ", sym
->type
, show_token(sym
->token
));
164 show_type(sym
->ctype
.base_type
);
169 void show_symbol(struct symbol
*sym
)
174 * Show actual implementation information
178 symbol_iterate(sym
->symbol_list
, show_struct_member
, NULL
);
182 symbol_iterate(sym
->symbol_list
, show_struct_member
, NULL
);
187 show_symbol_list(sym
->arguments
, ", ");
189 show_statement(sym
->stmt
);
198 * Print out a statement
200 void show_statement(struct statement
*stmt
)
204 switch (stmt
->type
) {
207 show_expression(stmt
->expression
);
213 show_symbol_list(stmt
->syms
, "\n\t");
216 show_statement_list(stmt
->stmts
, ";\n");
219 case STMT_EXPRESSION
:
221 show_expression(stmt
->expression
);
225 show_expression(stmt
->if_conditional
);
227 show_statement(stmt
->if_true
);
228 if (stmt
->if_false
) {
230 show_statement(stmt
->if_false
);
234 printf("\tswitch (");
235 show_expression(stmt
->switch_expression
);
237 show_statement(stmt
->switch_statement
);
241 if (!stmt
->case_expression
)
245 show_expression(stmt
->case_expression
);
248 show_expression(stmt
->case_to
);
252 show_statement(stmt
->case_statement
);
259 case STMT_ITERATOR
: {
260 struct statement
*pre_statement
= stmt
->iterator_pre_statement
;
261 struct expression
*pre_condition
= stmt
->iterator_pre_condition
;
262 struct statement
*statement
= stmt
->iterator_statement
;
263 struct statement
*post_statement
= stmt
->iterator_post_statement
;
264 struct expression
*post_condition
= stmt
->iterator_post_condition
;
267 * THIS IS ONLY APPROXIMATE!
269 * Real iterators are more generic than
270 * any of for/while/do-while, and can't
271 * be printed out as C without goto's
273 if (post_statement
|| !post_condition
) {
275 show_statement(pre_statement
);
277 show_expression(pre_condition
);
279 show_statement(post_statement
);
281 show_statement(statement
);
282 } else if (pre_condition
) {
284 show_statement(pre_statement
);
288 show_expression(pre_condition
);
290 show_statement(statement
);
293 show_statement(pre_statement
);
297 show_statement(statement
);
299 show_expression(post_condition
);
310 static void show_one_statement(struct statement
*stmt
, void *sep
, int flags
)
312 show_statement(stmt
);
313 if (!(flags
& ITERATE_LAST
))
314 printf("%s", (const char *)sep
);
317 void show_statement_list(struct statement_list
*stmt
, const char *sep
)
319 statement_iterate(stmt
, show_one_statement
, (void *)sep
);
322 static void show_size(struct symbol
*sym
)
325 printf("%d:%d", sym
->bit_size
, sym
->alignment
);
329 * Print out an expression
331 void show_expression(struct expression
*expr
)
337 show_size(expr
->ctype
);
338 show_type(expr
->ctype
);
340 switch (expr
->type
) {
342 show_expression(expr
->left
);
343 printf(" %s ", show_special(expr
->op
));
344 show_expression(expr
->right
);
347 printf("%s<", show_special(expr
->op
));
348 show_expression(expr
->unop
);
352 show_expression(expr
->unop
);
353 printf(" %s ", show_special(expr
->op
));
356 printf("%s", show_token(expr
->token
));
360 warn(expr
->token
, "undefined symbol '%s'", show_token(expr
->token
));
361 printf("<nosymbol>");
364 printf("<%s:", show_token(expr
->symbol
->token
));
365 show_type(expr
->symbol
->ctype
.base_type
);
369 show_expression(expr
->deref
);
370 printf("%s", show_special(expr
->op
));
371 printf("%s", show_token(expr
->member
));
375 show_type(expr
->cast_type
);
377 show_expression(expr
->cast_expression
);
380 printf("(%lld)", expr
->value
);