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
)
173 if (sym
->type
!= SYM_NODE
)
176 type
= sym
->ctype
.base_type
;
181 * Show actual implementation information
183 switch (type
->type
) {
185 symbol_iterate(type
->symbol_list
, show_struct_member
, NULL
);
189 symbol_iterate(type
->symbol_list
, show_struct_member
, NULL
);
194 show_symbol_list(type
->arguments
, ", ");
198 show_statement(type
->stmt
);
202 printf("[%d]\n", type
->array_size
);
211 * Print out a statement
213 void show_statement(struct statement
*stmt
)
217 switch (stmt
->type
) {
220 show_expression(stmt
->expression
);
226 show_symbol_list(stmt
->syms
, "\n\t");
229 show_statement_list(stmt
->stmts
, ";\n");
232 case STMT_EXPRESSION
:
234 show_expression(stmt
->expression
);
238 show_expression(stmt
->if_conditional
);
240 show_statement(stmt
->if_true
);
241 if (stmt
->if_false
) {
243 show_statement(stmt
->if_false
);
247 printf("\tswitch (");
248 show_expression(stmt
->switch_expression
);
250 show_statement(stmt
->switch_statement
);
254 if (!stmt
->case_expression
)
258 show_expression(stmt
->case_expression
);
261 show_expression(stmt
->case_to
);
265 show_statement(stmt
->case_statement
);
272 case STMT_ITERATOR
: {
273 struct statement
*pre_statement
= stmt
->iterator_pre_statement
;
274 struct expression
*pre_condition
= stmt
->iterator_pre_condition
;
275 struct statement
*statement
= stmt
->iterator_statement
;
276 struct statement
*post_statement
= stmt
->iterator_post_statement
;
277 struct expression
*post_condition
= stmt
->iterator_post_condition
;
280 * THIS IS ONLY APPROXIMATE!
282 * Real iterators are more generic than
283 * any of for/while/do-while, and can't
284 * be printed out as C without goto's
286 if (post_statement
|| !post_condition
) {
288 show_statement(pre_statement
);
290 show_expression(pre_condition
);
292 show_statement(post_statement
);
294 show_statement(statement
);
295 } else if (pre_condition
) {
297 show_statement(pre_statement
);
301 show_expression(pre_condition
);
303 show_statement(statement
);
306 show_statement(pre_statement
);
310 show_statement(statement
);
312 show_expression(post_condition
);
323 static void show_one_statement(struct statement
*stmt
, void *sep
, int flags
)
325 show_statement(stmt
);
326 if (!(flags
& ITERATE_LAST
))
327 printf("%s", (const char *)sep
);
330 void show_statement_list(struct statement_list
*stmt
, const char *sep
)
332 statement_iterate(stmt
, show_one_statement
, (void *)sep
);
335 static void show_size(struct symbol
*sym
)
338 printf("%d:%d", sym
->bit_size
, sym
->alignment
);
342 * Print out an expression
344 void show_expression(struct expression
*expr
)
350 show_size(expr
->ctype
);
351 show_type(expr
->ctype
);
353 switch (expr
->type
) {
355 show_expression(expr
->left
);
356 printf(" %s ", show_special(expr
->op
));
357 show_expression(expr
->right
);
360 printf("%s<", show_special(expr
->op
));
361 show_expression(expr
->unop
);
365 show_expression(expr
->unop
);
366 printf(" %s ", show_special(expr
->op
));
369 printf("%s", show_token(expr
->token
));
373 warn(expr
->token
, "undefined symbol '%s'", show_token(expr
->token
));
374 printf("<nosymbol>");
377 printf("<%s:", show_token(expr
->symbol
->token
));
378 show_type(expr
->symbol
->ctype
.base_type
);
382 show_expression(expr
->deref
);
383 printf("%s", show_special(expr
->op
));
384 printf("%s", show_token(expr
->member
));
388 show_type(expr
->cast_type
);
390 show_expression(expr
->cast_expression
);
393 printf("(%lld)", expr
->value
);
398 show_type(expr
->cast_type
);
400 show_expression(expr
->cast_expression
);