]>
Commit | Line | Data |
---|---|---|
c906108c SS |
1 | /* Support for printing C and C++ types for GDB, the GNU debugger. |
2 | Copyright 1986, 1988, 1989, 1991, 1993, 1994, 1995, 1996, 1998, 1999 | |
3 | Free Software Foundation, Inc. | |
4 | ||
c5aa993b | 5 | This file is part of GDB. |
c906108c | 6 | |
c5aa993b JM |
7 | This program is free software; you can redistribute it and/or modify |
8 | it under the terms of the GNU General Public License as published by | |
9 | the Free Software Foundation; either version 2 of the License, or | |
10 | (at your option) any later version. | |
c906108c | 11 | |
c5aa993b JM |
12 | This program is distributed in the hope that it will be useful, |
13 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 | GNU General Public License for more details. | |
c906108c | 16 | |
c5aa993b JM |
17 | You should have received a copy of the GNU General Public License |
18 | along with this program; if not, write to the Free Software | |
19 | Foundation, Inc., 59 Temple Place - Suite 330, | |
20 | Boston, MA 02111-1307, USA. */ | |
c906108c SS |
21 | |
22 | #include "defs.h" | |
23 | #include "obstack.h" | |
24 | #include "bfd.h" /* Binary File Description */ | |
25 | #include "symtab.h" | |
26 | #include "gdbtypes.h" | |
27 | #include "expression.h" | |
28 | #include "value.h" | |
29 | #include "gdbcore.h" | |
30 | #include "target.h" | |
31 | #include "command.h" | |
32 | #include "gdbcmd.h" | |
33 | #include "language.h" | |
34 | #include "demangle.h" | |
35 | #include "c-lang.h" | |
36 | #include "typeprint.h" | |
37 | ||
38 | #include "gdb_string.h" | |
39 | #include <errno.h> | |
40 | #include <ctype.h> | |
41 | ||
42 | /* Flag indicating target was compiled by HP compiler */ | |
43 | extern int hp_som_som_object_present; | |
44 | ||
c5aa993b | 45 | static void cp_type_print_method_args PARAMS ((struct type ** args, char *prefix, char *varstring, int staticp, GDB_FILE * stream)); |
392a587b | 46 | |
c906108c SS |
47 | static void |
48 | c_type_print_args PARAMS ((struct type *, GDB_FILE *)); | |
49 | ||
50 | static void | |
51 | cp_type_print_derivation_info PARAMS ((GDB_FILE *, struct type *)); | |
52 | ||
53 | void | |
54 | c_type_print_varspec_prefix PARAMS ((struct type *, GDB_FILE *, int, int)); | |
55 | ||
56 | static void | |
57 | c_type_print_cv_qualifier PARAMS ((struct type *, GDB_FILE *, int, int)); | |
c5aa993b | 58 | \f |
c906108c SS |
59 | |
60 | ||
c906108c SS |
61 | /* Print a description of a type in the format of a |
62 | typedef for the current language. | |
63 | NEW is the new name for a type TYPE. */ | |
64 | ||
65 | void | |
66 | c_typedef_print (type, new, stream) | |
c5aa993b JM |
67 | struct type *type; |
68 | struct symbol *new; | |
69 | GDB_FILE *stream; | |
c906108c SS |
70 | { |
71 | CHECK_TYPEDEF (type); | |
c5aa993b JM |
72 | switch (current_language->la_language) |
73 | { | |
c906108c | 74 | #ifdef _LANG_c |
c5aa993b JM |
75 | case language_c: |
76 | case language_cplus: | |
77 | fprintf_filtered (stream, "typedef "); | |
78 | type_print (type, "", stream, 0); | |
79 | if (TYPE_NAME ((SYMBOL_TYPE (new))) == 0 | |
80 | || !STREQ (TYPE_NAME ((SYMBOL_TYPE (new))), SYMBOL_NAME (new))) | |
81 | fprintf_filtered (stream, " %s", SYMBOL_SOURCE_NAME (new)); | |
c906108c SS |
82 | break; |
83 | #endif | |
84 | #ifdef _LANG_m2 | |
c5aa993b JM |
85 | case language_m2: |
86 | fprintf_filtered (stream, "TYPE "); | |
87 | if (!TYPE_NAME (SYMBOL_TYPE (new)) || | |
88 | !STREQ (TYPE_NAME (SYMBOL_TYPE (new)), SYMBOL_NAME (new))) | |
89 | fprintf_filtered (stream, "%s = ", SYMBOL_SOURCE_NAME (new)); | |
c906108c | 90 | else |
c5aa993b JM |
91 | fprintf_filtered (stream, "<builtin> = "); |
92 | type_print (type, "", stream, 0); | |
c906108c SS |
93 | break; |
94 | #endif | |
95 | #ifdef _LANG_chill | |
c5aa993b JM |
96 | case language_chill: |
97 | fprintf_filtered (stream, "SYNMODE "); | |
98 | if (!TYPE_NAME (SYMBOL_TYPE (new)) || | |
99 | !STREQ (TYPE_NAME (SYMBOL_TYPE (new)), SYMBOL_NAME (new))) | |
100 | fprintf_filtered (stream, "%s = ", SYMBOL_SOURCE_NAME (new)); | |
c906108c | 101 | else |
c5aa993b JM |
102 | fprintf_filtered (stream, "<builtin> = "); |
103 | type_print (type, "", stream, 0); | |
c906108c SS |
104 | break; |
105 | #endif | |
c5aa993b JM |
106 | default: |
107 | error ("Language not supported."); | |
108 | } | |
109 | fprintf_filtered (stream, ";\n"); | |
c906108c SS |
110 | } |
111 | ||
112 | ||
113 | /* LEVEL is the depth to indent lines by. */ | |
114 | ||
115 | void | |
116 | c_print_type (type, varstring, stream, show, level) | |
117 | struct type *type; | |
118 | char *varstring; | |
119 | GDB_FILE *stream; | |
120 | int show; | |
121 | int level; | |
122 | { | |
123 | register enum type_code code; | |
124 | int demangled_args; | |
125 | ||
126 | if (show > 0) | |
127 | CHECK_TYPEDEF (type); | |
128 | ||
129 | c_type_print_base (type, stream, show, level); | |
130 | code = TYPE_CODE (type); | |
131 | if ((varstring != NULL && *varstring != '\0') | |
132 | || | |
c5aa993b JM |
133 | /* Need a space if going to print stars or brackets; |
134 | but not if we will print just a type name. */ | |
c906108c SS |
135 | ((show > 0 || TYPE_NAME (type) == 0) |
136 | && | |
137 | (code == TYPE_CODE_PTR || code == TYPE_CODE_FUNC | |
138 | || code == TYPE_CODE_METHOD | |
139 | || code == TYPE_CODE_ARRAY | |
140 | || code == TYPE_CODE_MEMBER | |
141 | || code == TYPE_CODE_REF))) | |
142 | fputs_filtered (" ", stream); | |
143 | c_type_print_varspec_prefix (type, stream, show, 0); | |
144 | ||
145 | if (varstring != NULL) | |
146 | { | |
147 | fputs_filtered (varstring, stream); | |
148 | ||
149 | /* For demangled function names, we have the arglist as part of the name, | |
c5aa993b | 150 | so don't print an additional pair of ()'s */ |
c906108c | 151 | |
c5aa993b | 152 | demangled_args = strchr (varstring, '(') != NULL; |
c906108c SS |
153 | c_type_print_varspec_suffix (type, stream, show, 0, demangled_args); |
154 | } | |
155 | } | |
c5aa993b | 156 | |
c906108c SS |
157 | /* If TYPE is a derived type, then print out derivation information. |
158 | Print only the actual base classes of this type, not the base classes | |
159 | of the base classes. I.E. for the derivation hierarchy: | |
160 | ||
c5aa993b JM |
161 | class A { int a; }; |
162 | class B : public A {int b; }; | |
163 | class C : public B {int c; }; | |
c906108c SS |
164 | |
165 | Print the type of class C as: | |
166 | ||
c5aa993b JM |
167 | class C : public B { |
168 | int c; | |
169 | } | |
c906108c SS |
170 | |
171 | Not as the following (like gdb used to), which is not legal C++ syntax for | |
172 | derived types and may be confused with the multiple inheritance form: | |
173 | ||
c5aa993b JM |
174 | class C : public B : public A { |
175 | int c; | |
176 | } | |
c906108c SS |
177 | |
178 | In general, gdb should try to print the types as closely as possible to | |
179 | the form that they appear in the source code. | |
180 | Note that in case of protected derivation gcc will not say 'protected' | |
181 | but 'private'. The HP's aCC compiler emits specific information for | |
182 | derivation via protected inheritance, so gdb can print it out */ | |
183 | ||
184 | static void | |
185 | cp_type_print_derivation_info (stream, type) | |
186 | GDB_FILE *stream; | |
187 | struct type *type; | |
188 | { | |
189 | char *name; | |
190 | int i; | |
191 | ||
192 | for (i = 0; i < TYPE_N_BASECLASSES (type); i++) | |
193 | { | |
194 | fputs_filtered (i == 0 ? ": " : ", ", stream); | |
195 | fprintf_filtered (stream, "%s%s ", | |
c5aa993b JM |
196 | BASETYPE_VIA_PUBLIC (type, i) ? "public" |
197 | : (TYPE_FIELD_PROTECTED (type, i) ? "protected" : "private"), | |
198 | BASETYPE_VIA_VIRTUAL (type, i) ? " virtual" : ""); | |
c906108c SS |
199 | name = type_name_no_tag (TYPE_BASECLASS (type, i)); |
200 | fprintf_filtered (stream, "%s", name ? name : "(null)"); | |
201 | } | |
202 | if (i > 0) | |
203 | { | |
204 | fputs_filtered (" ", stream); | |
205 | } | |
206 | } | |
207 | /* Print the C++ method arguments ARGS to the file STREAM. */ | |
c5aa993b | 208 | |
392a587b | 209 | static void |
c906108c SS |
210 | cp_type_print_method_args (args, prefix, varstring, staticp, stream) |
211 | struct type **args; | |
212 | char *prefix; | |
213 | char *varstring; | |
214 | int staticp; | |
215 | GDB_FILE *stream; | |
216 | { | |
217 | int i; | |
c5aa993b | 218 | |
c906108c SS |
219 | fprintf_symbol_filtered (stream, prefix, language_cplus, DMGL_ANSI); |
220 | fprintf_symbol_filtered (stream, varstring, language_cplus, DMGL_ANSI); | |
221 | fputs_filtered ("(", stream); | |
222 | if (args && args[!staticp] && args[!staticp]->code != TYPE_CODE_VOID) | |
223 | { | |
c5aa993b | 224 | i = !staticp; /* skip the class variable */ |
c906108c | 225 | while (1) |
c5aa993b JM |
226 | { |
227 | type_print (args[i++], "", stream, 0); | |
228 | if (!args[i]) | |
229 | { | |
230 | fprintf_filtered (stream, " ..."); | |
231 | break; | |
232 | } | |
233 | else if (args[i]->code != TYPE_CODE_VOID) | |
234 | { | |
235 | fprintf_filtered (stream, ", "); | |
236 | } | |
237 | else | |
238 | break; | |
239 | } | |
c906108c SS |
240 | } |
241 | else if (current_language->la_language == language_cplus) | |
242 | { | |
243 | fprintf_filtered (stream, "void"); | |
244 | } | |
c5aa993b | 245 | |
c906108c SS |
246 | fprintf_filtered (stream, ")"); |
247 | } | |
248 | ||
249 | ||
250 | /* Print any asterisks or open-parentheses needed before the | |
251 | variable name (to describe its type). | |
252 | ||
253 | On outermost call, pass 0 for PASSED_A_PTR. | |
254 | On outermost call, SHOW > 0 means should ignore | |
255 | any typename for TYPE and show its details. | |
256 | SHOW is always zero on recursive calls. */ | |
257 | ||
258 | void | |
259 | c_type_print_varspec_prefix (type, stream, show, passed_a_ptr) | |
260 | struct type *type; | |
261 | GDB_FILE *stream; | |
262 | int show; | |
263 | int passed_a_ptr; | |
264 | { | |
265 | char *name; | |
266 | if (type == 0) | |
267 | return; | |
268 | ||
269 | if (TYPE_NAME (type) && show <= 0) | |
270 | return; | |
271 | ||
272 | QUIT; | |
273 | ||
274 | switch (TYPE_CODE (type)) | |
275 | { | |
276 | case TYPE_CODE_PTR: | |
277 | c_type_print_varspec_prefix (TYPE_TARGET_TYPE (type), stream, 0, 1); | |
278 | fprintf_filtered (stream, "*"); | |
279 | c_type_print_cv_qualifier (type, stream, 1, 0); | |
280 | break; | |
281 | ||
282 | case TYPE_CODE_MEMBER: | |
283 | if (passed_a_ptr) | |
284 | fprintf_filtered (stream, "("); | |
285 | c_type_print_varspec_prefix (TYPE_TARGET_TYPE (type), stream, 0, 0); | |
286 | fprintf_filtered (stream, " "); | |
287 | name = type_name_no_tag (TYPE_DOMAIN_TYPE (type)); | |
288 | if (name) | |
289 | fputs_filtered (name, stream); | |
290 | else | |
c5aa993b | 291 | c_type_print_base (TYPE_DOMAIN_TYPE (type), stream, 0, passed_a_ptr); |
c906108c SS |
292 | fprintf_filtered (stream, "::"); |
293 | break; | |
294 | ||
295 | case TYPE_CODE_METHOD: | |
296 | if (passed_a_ptr) | |
297 | fprintf_filtered (stream, "("); | |
298 | c_type_print_varspec_prefix (TYPE_TARGET_TYPE (type), stream, 0, 0); | |
299 | if (passed_a_ptr) | |
300 | { | |
301 | fprintf_filtered (stream, " "); | |
302 | c_type_print_base (TYPE_DOMAIN_TYPE (type), stream, 0, passed_a_ptr); | |
303 | fprintf_filtered (stream, "::"); | |
304 | } | |
305 | break; | |
306 | ||
307 | case TYPE_CODE_REF: | |
308 | c_type_print_varspec_prefix (TYPE_TARGET_TYPE (type), stream, 0, 1); | |
309 | fprintf_filtered (stream, "&"); | |
310 | c_type_print_cv_qualifier (type, stream, 1, 0); | |
311 | break; | |
312 | ||
313 | case TYPE_CODE_FUNC: | |
314 | c_type_print_varspec_prefix (TYPE_TARGET_TYPE (type), stream, 0, 0); | |
315 | if (passed_a_ptr) | |
316 | fprintf_filtered (stream, "("); | |
317 | break; | |
318 | ||
319 | case TYPE_CODE_ARRAY: | |
320 | c_type_print_varspec_prefix (TYPE_TARGET_TYPE (type), stream, 0, 0); | |
321 | if (passed_a_ptr) | |
322 | fprintf_filtered (stream, "("); | |
323 | break; | |
324 | ||
325 | case TYPE_CODE_UNDEF: | |
326 | case TYPE_CODE_STRUCT: | |
327 | case TYPE_CODE_UNION: | |
328 | case TYPE_CODE_ENUM: | |
329 | case TYPE_CODE_INT: | |
330 | case TYPE_CODE_FLT: | |
331 | case TYPE_CODE_VOID: | |
332 | case TYPE_CODE_ERROR: | |
333 | case TYPE_CODE_CHAR: | |
334 | case TYPE_CODE_BOOL: | |
335 | case TYPE_CODE_SET: | |
336 | case TYPE_CODE_RANGE: | |
337 | case TYPE_CODE_STRING: | |
338 | case TYPE_CODE_BITSTRING: | |
339 | case TYPE_CODE_COMPLEX: | |
340 | case TYPE_CODE_TYPEDEF: | |
341 | /* These types need no prefix. They are listed here so that | |
c5aa993b | 342 | gcc -Wall will reveal any types that haven't been handled. */ |
c906108c SS |
343 | break; |
344 | } | |
345 | } | |
346 | ||
347 | /* Print out "const" and "volatile" attributes. | |
348 | TYPE is a pointer to the type being printed out. | |
349 | STREAM is the output destination. | |
350 | NEED_SPACE = 1 indicates an initial white space is needed */ | |
351 | ||
352 | static void | |
353 | c_type_print_cv_qualifier (type, stream, need_pre_space, need_post_space) | |
c5aa993b JM |
354 | struct type *type; |
355 | GDB_FILE *stream; | |
356 | int need_pre_space; | |
357 | int need_post_space; | |
c906108c SS |
358 | { |
359 | int flag = 0; | |
c5aa993b | 360 | |
c906108c SS |
361 | if (TYPE_CONST (type)) |
362 | { | |
363 | if (need_pre_space) | |
c5aa993b | 364 | fprintf_filtered (stream, " "); |
c906108c SS |
365 | fprintf_filtered (stream, "const"); |
366 | flag = 1; | |
367 | } | |
c5aa993b | 368 | |
c906108c SS |
369 | if (TYPE_VOLATILE (type)) |
370 | { | |
371 | if (flag || need_pre_space) | |
c5aa993b | 372 | fprintf_filtered (stream, " "); |
c906108c SS |
373 | fprintf_filtered (stream, "volatile"); |
374 | flag = 1; | |
375 | } | |
376 | ||
377 | if (flag && need_post_space) | |
378 | fprintf_filtered (stream, " "); | |
379 | } | |
380 | ||
381 | ||
382 | ||
383 | ||
384 | static void | |
385 | c_type_print_args (type, stream) | |
386 | struct type *type; | |
387 | GDB_FILE *stream; | |
388 | { | |
389 | int i; | |
390 | struct type **args; | |
391 | ||
392 | fprintf_filtered (stream, "("); | |
393 | args = TYPE_ARG_TYPES (type); | |
394 | if (args != NULL) | |
395 | { | |
396 | if (args[1] == NULL) | |
397 | { | |
398 | fprintf_filtered (stream, "..."); | |
399 | } | |
400 | else if ((args[1]->code == TYPE_CODE_VOID) && | |
c5aa993b JM |
401 | (current_language->la_language == language_cplus)) |
402 | { | |
403 | fprintf_filtered (stream, "void"); | |
404 | } | |
c906108c SS |
405 | else |
406 | { | |
407 | for (i = 1; | |
408 | args[i] != NULL && args[i]->code != TYPE_CODE_VOID; | |
409 | i++) | |
410 | { | |
411 | c_print_type (args[i], "", stream, -1, 0); | |
c5aa993b | 412 | if (args[i + 1] == NULL) |
c906108c SS |
413 | { |
414 | fprintf_filtered (stream, "..."); | |
415 | } | |
c5aa993b | 416 | else if (args[i + 1]->code != TYPE_CODE_VOID) |
c906108c SS |
417 | { |
418 | fprintf_filtered (stream, ","); | |
419 | wrap_here (" "); | |
420 | } | |
421 | } | |
422 | } | |
423 | } | |
424 | else if (current_language->la_language == language_cplus) | |
425 | { | |
426 | fprintf_filtered (stream, "void"); | |
427 | } | |
c5aa993b | 428 | |
c906108c SS |
429 | fprintf_filtered (stream, ")"); |
430 | } | |
431 | ||
432 | /* Print any array sizes, function arguments or close parentheses | |
433 | needed after the variable name (to describe its type). | |
434 | Args work like c_type_print_varspec_prefix. */ | |
435 | ||
436 | void | |
437 | c_type_print_varspec_suffix (type, stream, show, passed_a_ptr, demangled_args) | |
438 | struct type *type; | |
439 | GDB_FILE *stream; | |
440 | int show; | |
441 | int passed_a_ptr; | |
442 | int demangled_args; | |
443 | { | |
444 | if (type == 0) | |
445 | return; | |
446 | ||
447 | if (TYPE_NAME (type) && show <= 0) | |
448 | return; | |
449 | ||
450 | QUIT; | |
451 | ||
452 | switch (TYPE_CODE (type)) | |
453 | { | |
454 | case TYPE_CODE_ARRAY: | |
455 | if (passed_a_ptr) | |
456 | fprintf_filtered (stream, ")"); | |
c5aa993b | 457 | |
c906108c SS |
458 | fprintf_filtered (stream, "["); |
459 | if (TYPE_LENGTH (type) >= 0 && TYPE_LENGTH (TYPE_TARGET_TYPE (type)) > 0 | |
c5aa993b | 460 | && TYPE_ARRAY_UPPER_BOUND_TYPE (type) != BOUND_CANNOT_BE_DETERMINED) |
c906108c SS |
461 | fprintf_filtered (stream, "%d", |
462 | (TYPE_LENGTH (type) | |
463 | / TYPE_LENGTH (TYPE_TARGET_TYPE (type)))); | |
464 | fprintf_filtered (stream, "]"); | |
c5aa993b | 465 | |
c906108c SS |
466 | c_type_print_varspec_suffix (TYPE_TARGET_TYPE (type), stream, 0, 0, 0); |
467 | break; | |
468 | ||
469 | case TYPE_CODE_MEMBER: | |
470 | if (passed_a_ptr) | |
471 | fprintf_filtered (stream, ")"); | |
472 | c_type_print_varspec_suffix (TYPE_TARGET_TYPE (type), stream, 0, 0, 0); | |
473 | break; | |
474 | ||
475 | case TYPE_CODE_METHOD: | |
476 | if (passed_a_ptr) | |
477 | fprintf_filtered (stream, ")"); | |
478 | c_type_print_varspec_suffix (TYPE_TARGET_TYPE (type), stream, 0, 0, 0); | |
479 | if (passed_a_ptr) | |
480 | { | |
481 | c_type_print_args (type, stream); | |
482 | } | |
483 | break; | |
484 | ||
485 | case TYPE_CODE_PTR: | |
486 | case TYPE_CODE_REF: | |
487 | c_type_print_varspec_suffix (TYPE_TARGET_TYPE (type), stream, 0, 1, 0); | |
488 | break; | |
489 | ||
490 | case TYPE_CODE_FUNC: | |
491 | if (passed_a_ptr) | |
492 | fprintf_filtered (stream, ")"); | |
493 | if (!demangled_args) | |
c5aa993b JM |
494 | { |
495 | int i, len = TYPE_NFIELDS (type); | |
c906108c | 496 | fprintf_filtered (stream, "("); |
c5aa993b JM |
497 | if ((len == 0) && (current_language->la_language == language_cplus)) |
498 | { | |
499 | fprintf_filtered (stream, "void"); | |
500 | } | |
501 | else | |
502 | for (i = 0; i < len; i++) | |
503 | { | |
504 | if (i > 0) | |
505 | { | |
506 | fputs_filtered (", ", stream); | |
507 | wrap_here (" "); | |
508 | } | |
509 | c_print_type (TYPE_FIELD_TYPE (type, i), "", stream, -1, 0); | |
510 | } | |
c906108c SS |
511 | fprintf_filtered (stream, ")"); |
512 | } | |
513 | c_type_print_varspec_suffix (TYPE_TARGET_TYPE (type), stream, 0, | |
514 | passed_a_ptr, 0); | |
515 | break; | |
516 | ||
517 | case TYPE_CODE_UNDEF: | |
518 | case TYPE_CODE_STRUCT: | |
519 | case TYPE_CODE_UNION: | |
520 | case TYPE_CODE_ENUM: | |
521 | case TYPE_CODE_INT: | |
522 | case TYPE_CODE_FLT: | |
523 | case TYPE_CODE_VOID: | |
524 | case TYPE_CODE_ERROR: | |
525 | case TYPE_CODE_CHAR: | |
526 | case TYPE_CODE_BOOL: | |
527 | case TYPE_CODE_SET: | |
528 | case TYPE_CODE_RANGE: | |
529 | case TYPE_CODE_STRING: | |
530 | case TYPE_CODE_BITSTRING: | |
531 | case TYPE_CODE_COMPLEX: | |
532 | case TYPE_CODE_TYPEDEF: | |
533 | /* These types do not need a suffix. They are listed so that | |
c5aa993b | 534 | gcc -Wall will report types that may not have been considered. */ |
c906108c SS |
535 | break; |
536 | } | |
537 | } | |
538 | ||
539 | /* Print the name of the type (or the ultimate pointer target, | |
540 | function value or array element), or the description of a | |
541 | structure or union. | |
542 | ||
543 | SHOW positive means print details about the type (e.g. enum values), | |
544 | and print structure elements passing SHOW - 1 for show. | |
545 | SHOW negative means just print the type name or struct tag if there is one. | |
546 | If there is no name, print something sensible but concise like | |
547 | "struct {...}". | |
548 | SHOW zero means just print the type name or struct tag if there is one. | |
549 | If there is no name, print something sensible but not as concise like | |
550 | "struct {int x; int y;}". | |
551 | ||
552 | LEVEL is the number of spaces to indent by. | |
553 | We increase it for some recursive calls. */ | |
554 | ||
555 | void | |
556 | c_type_print_base (type, stream, show, level) | |
557 | struct type *type; | |
558 | GDB_FILE *stream; | |
559 | int show; | |
560 | int level; | |
561 | { | |
562 | register int i; | |
563 | register int len; | |
564 | register int lastval; | |
565 | char *mangled_name; | |
566 | char *demangled_name; | |
567 | char *demangled_no_static; | |
c5aa993b JM |
568 | enum |
569 | { | |
570 | s_none, s_public, s_private, s_protected | |
571 | } | |
572 | section_type; | |
c906108c SS |
573 | int need_access_label = 0; |
574 | int j, len2; | |
575 | ||
576 | QUIT; | |
577 | ||
578 | wrap_here (" "); | |
579 | if (type == NULL) | |
580 | { | |
581 | fputs_filtered ("<type unknown>", stream); | |
582 | return; | |
583 | } | |
584 | ||
585 | /* When SHOW is zero or less, and there is a valid type name, then always | |
586 | just print the type name directly from the type. */ | |
587 | /* If we have "typedef struct foo {. . .} bar;" do we want to print it | |
588 | as "struct foo" or as "bar"? Pick the latter, because C++ folk tend | |
589 | to expect things like "class5 *foo" rather than "struct class5 *foo". */ | |
590 | ||
591 | if (show <= 0 | |
592 | && TYPE_NAME (type) != NULL) | |
593 | { | |
594 | c_type_print_cv_qualifier (type, stream, 0, 1); | |
595 | fputs_filtered (TYPE_NAME (type), stream); | |
596 | return; | |
597 | } | |
598 | ||
599 | CHECK_TYPEDEF (type); | |
c5aa993b | 600 | |
c906108c SS |
601 | switch (TYPE_CODE (type)) |
602 | { | |
603 | case TYPE_CODE_TYPEDEF: | |
604 | case TYPE_CODE_ARRAY: | |
605 | case TYPE_CODE_PTR: | |
606 | case TYPE_CODE_MEMBER: | |
607 | case TYPE_CODE_REF: | |
608 | case TYPE_CODE_FUNC: | |
609 | case TYPE_CODE_METHOD: | |
610 | c_type_print_base (TYPE_TARGET_TYPE (type), stream, show, level); | |
611 | break; | |
612 | ||
613 | case TYPE_CODE_STRUCT: | |
614 | c_type_print_cv_qualifier (type, stream, 0, 1); | |
615 | /* Note TYPE_CODE_STRUCT and TYPE_CODE_CLASS have the same value, | |
616 | * so we use another means for distinguishing them. | |
617 | */ | |
c5aa993b JM |
618 | if (HAVE_CPLUS_STRUCT (type)) |
619 | { | |
620 | switch (TYPE_DECLARED_TYPE (type)) | |
621 | { | |
622 | case DECLARED_TYPE_CLASS: | |
623 | fprintf_filtered (stream, "class "); | |
624 | break; | |
625 | case DECLARED_TYPE_UNION: | |
626 | fprintf_filtered (stream, "union "); | |
627 | break; | |
628 | case DECLARED_TYPE_STRUCT: | |
629 | fprintf_filtered (stream, "struct "); | |
630 | break; | |
631 | default: | |
632 | /* If there is a CPLUS_STRUCT, assume class if not | |
633 | * otherwise specified in the declared_type field. | |
634 | */ | |
635 | fprintf_filtered (stream, "class "); | |
636 | break; | |
637 | } /* switch */ | |
638 | } | |
639 | else | |
640 | { | |
641 | /* If not CPLUS_STRUCT, then assume it's a C struct */ | |
642 | fprintf_filtered (stream, "struct "); | |
643 | } | |
c906108c SS |
644 | goto struct_union; |
645 | ||
646 | case TYPE_CODE_UNION: | |
647 | c_type_print_cv_qualifier (type, stream, 0, 1); | |
648 | fprintf_filtered (stream, "union "); | |
649 | ||
650 | struct_union: | |
651 | ||
652 | /* Print the tag if it exists. | |
653 | * The HP aCC compiler emits | |
654 | * a spurious "{unnamed struct}"/"{unnamed union}"/"{unnamed enum}" | |
655 | * tag for unnamed struct/union/enum's, which we don't | |
656 | * want to print. | |
657 | */ | |
658 | if (TYPE_TAG_NAME (type) != NULL && | |
c5aa993b | 659 | strncmp (TYPE_TAG_NAME (type), "{unnamed", 8)) |
c906108c SS |
660 | { |
661 | fputs_filtered (TYPE_TAG_NAME (type), stream); | |
662 | if (show > 0) | |
663 | fputs_filtered (" ", stream); | |
664 | } | |
665 | wrap_here (" "); | |
666 | if (show < 0) | |
667 | { | |
668 | /* If we just printed a tag name, no need to print anything else. */ | |
669 | if (TYPE_TAG_NAME (type) == NULL) | |
670 | fprintf_filtered (stream, "{...}"); | |
671 | } | |
672 | else if (show > 0 || TYPE_TAG_NAME (type) == NULL) | |
673 | { | |
674 | cp_type_print_derivation_info (stream, type); | |
c5aa993b | 675 | |
c906108c SS |
676 | fprintf_filtered (stream, "{\n"); |
677 | if ((TYPE_NFIELDS (type) == 0) && (TYPE_NFN_FIELDS (type) == 0)) | |
678 | { | |
679 | if (TYPE_FLAGS (type) & TYPE_FLAG_STUB) | |
680 | fprintfi_filtered (level + 4, stream, "<incomplete type>\n"); | |
681 | else | |
682 | fprintfi_filtered (level + 4, stream, "<no data fields>\n"); | |
683 | } | |
684 | ||
685 | /* Start off with no specific section type, so we can print | |
686 | one for the first field we find, and use that section type | |
687 | thereafter until we find another type. */ | |
688 | ||
689 | section_type = s_none; | |
690 | ||
c5aa993b JM |
691 | /* For a class, if all members are private, there's no need |
692 | for a "private:" label; similarly, for a struct or union | |
693 | masquerading as a class, if all members are public, there's | |
694 | no need for a "public:" label. */ | |
695 | ||
696 | if ((TYPE_DECLARED_TYPE (type) == DECLARED_TYPE_CLASS) || | |
697 | (TYPE_DECLARED_TYPE (type) == DECLARED_TYPE_TEMPLATE)) | |
698 | { | |
699 | QUIT; | |
700 | len = TYPE_NFIELDS (type); | |
701 | for (i = TYPE_N_BASECLASSES (type); i < len; i++) | |
702 | if (!TYPE_FIELD_PRIVATE (type, i)) | |
703 | { | |
704 | need_access_label = 1; | |
705 | break; | |
706 | } | |
707 | QUIT; | |
708 | if (!need_access_label) | |
709 | { | |
710 | len2 = TYPE_NFN_FIELDS (type); | |
711 | for (j = 0; j < len2; j++) | |
712 | { | |
713 | len = TYPE_FN_FIELDLIST_LENGTH (type, j); | |
714 | for (i = 0; i < len; i++) | |
715 | if (!TYPE_FN_FIELD_PRIVATE (TYPE_FN_FIELDLIST1 (type, j), i)) | |
716 | { | |
717 | need_access_label = 1; | |
718 | break; | |
719 | } | |
720 | if (need_access_label) | |
721 | break; | |
722 | } | |
723 | } | |
724 | } | |
725 | else if ((TYPE_DECLARED_TYPE (type) == DECLARED_TYPE_STRUCT) || | |
726 | (TYPE_DECLARED_TYPE (type) == DECLARED_TYPE_UNION)) | |
727 | { | |
728 | QUIT; | |
729 | len = TYPE_NFIELDS (type); | |
730 | for (i = TYPE_N_BASECLASSES (type); i < len; i++) | |
731 | if (TYPE_FIELD_PRIVATE (type, i) || TYPE_FIELD_PROTECTED (type, i)) | |
732 | { | |
733 | need_access_label = 1; | |
734 | break; | |
735 | } | |
736 | QUIT; | |
737 | if (!need_access_label) | |
738 | { | |
739 | len2 = TYPE_NFN_FIELDS (type); | |
740 | for (j = 0; j < len2; j++) | |
741 | { | |
742 | QUIT; | |
743 | len = TYPE_FN_FIELDLIST_LENGTH (type, j); | |
744 | for (i = 0; i < len; i++) | |
745 | if (TYPE_FN_FIELD_PRIVATE (TYPE_FN_FIELDLIST1 (type, j), i) || | |
746 | TYPE_FN_FIELD_PROTECTED (TYPE_FN_FIELDLIST1 (type, j), i)) | |
747 | { | |
748 | need_access_label = 1; | |
749 | break; | |
750 | } | |
751 | if (need_access_label) | |
752 | break; | |
753 | } | |
754 | } | |
755 | } | |
c906108c SS |
756 | |
757 | /* If there is a base class for this type, | |
758 | do not print the field that it occupies. */ | |
759 | ||
760 | len = TYPE_NFIELDS (type); | |
761 | for (i = TYPE_N_BASECLASSES (type); i < len; i++) | |
762 | { | |
763 | QUIT; | |
764 | /* Don't print out virtual function table. */ | |
c5aa993b JM |
765 | /* HP ANSI C++ case */ |
766 | if (TYPE_HAS_VTABLE (type) && (STREQN (TYPE_FIELD_NAME (type, i), "__vfp", 5))) | |
767 | continue; | |
768 | /* Other compilers */ | |
769 | /* pai:: FIXME : check for has_vtable < 0 */ | |
c906108c SS |
770 | if (STREQN (TYPE_FIELD_NAME (type, i), "_vptr", 5) |
771 | && is_cplus_marker ((TYPE_FIELD_NAME (type, i))[5])) | |
772 | continue; | |
773 | ||
774 | /* If this is a C++ class we can print the various C++ section | |
c5aa993b | 775 | labels. */ |
c906108c SS |
776 | |
777 | if (HAVE_CPLUS_STRUCT (type) && need_access_label) | |
778 | { | |
779 | if (TYPE_FIELD_PROTECTED (type, i)) | |
780 | { | |
781 | if (section_type != s_protected) | |
782 | { | |
783 | section_type = s_protected; | |
784 | fprintfi_filtered (level + 2, stream, | |
785 | "protected:\n"); | |
786 | } | |
787 | } | |
788 | else if (TYPE_FIELD_PRIVATE (type, i)) | |
789 | { | |
790 | if (section_type != s_private) | |
791 | { | |
792 | section_type = s_private; | |
793 | fprintfi_filtered (level + 2, stream, "private:\n"); | |
794 | } | |
795 | } | |
796 | else | |
797 | { | |
798 | if (section_type != s_public) | |
799 | { | |
800 | section_type = s_public; | |
801 | fprintfi_filtered (level + 2, stream, "public:\n"); | |
802 | } | |
803 | } | |
804 | } | |
805 | ||
806 | print_spaces_filtered (level + 4, stream); | |
807 | if (TYPE_FIELD_STATIC (type, i)) | |
808 | { | |
809 | fprintf_filtered (stream, "static "); | |
810 | } | |
811 | c_print_type (TYPE_FIELD_TYPE (type, i), | |
812 | TYPE_FIELD_NAME (type, i), | |
813 | stream, show - 1, level + 4); | |
814 | if (!TYPE_FIELD_STATIC (type, i) | |
815 | && TYPE_FIELD_PACKED (type, i)) | |
816 | { | |
817 | /* It is a bitfield. This code does not attempt | |
818 | to look at the bitpos and reconstruct filler, | |
819 | unnamed fields. This would lead to misleading | |
820 | results if the compiler does not put out fields | |
821 | for such things (I don't know what it does). */ | |
822 | fprintf_filtered (stream, " : %d", | |
823 | TYPE_FIELD_BITSIZE (type, i)); | |
824 | } | |
825 | fprintf_filtered (stream, ";\n"); | |
826 | } | |
827 | ||
828 | /* If there are both fields and methods, put a space between. */ | |
829 | len = TYPE_NFN_FIELDS (type); | |
830 | if (len && section_type != s_none) | |
c5aa993b | 831 | fprintf_filtered (stream, "\n"); |
c906108c SS |
832 | |
833 | /* C++: print out the methods */ | |
834 | for (i = 0; i < len; i++) | |
835 | { | |
836 | struct fn_field *f = TYPE_FN_FIELDLIST1 (type, i); | |
837 | int j, len2 = TYPE_FN_FIELDLIST_LENGTH (type, i); | |
838 | char *method_name = TYPE_FN_FIELDLIST_NAME (type, i); | |
839 | char *name = type_name_no_tag (type); | |
c5aa993b | 840 | int is_constructor = name && STREQ (method_name, name); |
c906108c SS |
841 | for (j = 0; j < len2; j++) |
842 | { | |
843 | char *physname = TYPE_FN_FIELD_PHYSNAME (f, j); | |
c5aa993b JM |
844 | int is_full_physname_constructor = |
845 | ((physname[0] == '_' && physname[1] == '_' | |
846 | && strchr ("0123456789Qt", physname[2])) | |
847 | || STREQN (physname, "__ct__", 6) | |
848 | || DESTRUCTOR_PREFIX_P (physname) | |
849 | || STREQN (physname, "__dt__", 6)); | |
c906108c SS |
850 | |
851 | QUIT; | |
852 | if (TYPE_FN_FIELD_PROTECTED (f, j)) | |
853 | { | |
854 | if (section_type != s_protected) | |
855 | { | |
856 | section_type = s_protected; | |
857 | fprintfi_filtered (level + 2, stream, | |
858 | "protected:\n"); | |
859 | } | |
860 | } | |
861 | else if (TYPE_FN_FIELD_PRIVATE (f, j)) | |
862 | { | |
863 | if (section_type != s_private) | |
864 | { | |
865 | section_type = s_private; | |
866 | fprintfi_filtered (level + 2, stream, "private:\n"); | |
867 | } | |
868 | } | |
869 | else | |
870 | { | |
871 | if (section_type != s_public) | |
872 | { | |
873 | section_type = s_public; | |
874 | fprintfi_filtered (level + 2, stream, "public:\n"); | |
875 | } | |
876 | } | |
877 | ||
878 | print_spaces_filtered (level + 4, stream); | |
879 | if (TYPE_FN_FIELD_VIRTUAL_P (f, j)) | |
880 | fprintf_filtered (stream, "virtual "); | |
881 | else if (TYPE_FN_FIELD_STATIC_P (f, j)) | |
882 | fprintf_filtered (stream, "static "); | |
883 | if (TYPE_TARGET_TYPE (TYPE_FN_FIELD_TYPE (f, j)) == 0) | |
884 | { | |
885 | /* Keep GDB from crashing here. */ | |
886 | fprintf_filtered (stream, "<undefined type> %s;\n", | |
c5aa993b | 887 | TYPE_FN_FIELD_PHYSNAME (f, j)); |
c906108c SS |
888 | break; |
889 | } | |
c5aa993b JM |
890 | else if (!is_constructor && /* constructors don't have declared types */ |
891 | !is_full_physname_constructor && /* " " */ | |
892 | !strstr (method_name, "operator ")) /* Not a type conversion operator */ | |
893 | /* (note space -- other operators don't have it) */ | |
c906108c SS |
894 | { |
895 | type_print (TYPE_TARGET_TYPE (TYPE_FN_FIELD_TYPE (f, j)), | |
896 | "", stream, -1); | |
897 | fputs_filtered (" ", stream); | |
898 | } | |
899 | if (TYPE_FN_FIELD_STUB (f, j)) | |
900 | /* Build something we can demangle. */ | |
901 | mangled_name = gdb_mangle_name (type, i, j); | |
902 | else | |
903 | mangled_name = TYPE_FN_FIELD_PHYSNAME (f, j); | |
904 | ||
905 | demangled_name = | |
906 | cplus_demangle (mangled_name, | |
907 | DMGL_ANSI | DMGL_PARAMS); | |
908 | if (demangled_name == NULL) | |
909 | { | |
910 | /* in some cases (for instance with the HP demangling), | |
c5aa993b JM |
911 | if a function has more than 10 arguments, |
912 | the demangling will fail. | |
913 | Let's try to reconstruct the function signature from | |
914 | the symbol information */ | |
c906108c SS |
915 | if (!TYPE_FN_FIELD_STUB (f, j)) |
916 | cp_type_print_method_args (TYPE_FN_FIELD_ARGS (f, j), "", | |
917 | method_name, | |
c5aa993b | 918 | TYPE_FN_FIELD_STATIC_P (f, j), |
c906108c SS |
919 | stream); |
920 | else | |
921 | fprintf_filtered (stream, "<badly mangled name '%s'>", | |
922 | mangled_name); | |
923 | } | |
924 | else | |
925 | { | |
926 | char *p; | |
927 | char *demangled_no_class = demangled_name; | |
c5aa993b | 928 | |
7a292a7a | 929 | while ((p = strchr (demangled_no_class, ':'))) |
c906108c SS |
930 | { |
931 | demangled_no_class = p; | |
932 | if (*++demangled_no_class == ':') | |
933 | ++demangled_no_class; | |
934 | } | |
935 | /* get rid of the static word appended by the demangler */ | |
936 | p = strstr (demangled_no_class, " static"); | |
937 | if (p != NULL) | |
938 | { | |
939 | int length = p - demangled_no_class; | |
940 | demangled_no_static = (char *) xmalloc (length + 1); | |
941 | strncpy (demangled_no_static, demangled_no_class, length); | |
c5aa993b | 942 | *(demangled_no_static + length) = '\0'; |
c906108c SS |
943 | fputs_filtered (demangled_no_static, stream); |
944 | free (demangled_no_static); | |
945 | } | |
946 | else | |
947 | fputs_filtered (demangled_no_class, stream); | |
948 | free (demangled_name); | |
949 | } | |
950 | ||
951 | if (TYPE_FN_FIELD_STUB (f, j)) | |
952 | free (mangled_name); | |
953 | ||
954 | fprintf_filtered (stream, ";\n"); | |
955 | } | |
956 | } | |
957 | ||
c5aa993b JM |
958 | if (TYPE_LOCALTYPE_PTR (type) && show >= 0) |
959 | fprintfi_filtered (level, stream, " (Local at %s:%d)\n", | |
960 | TYPE_LOCALTYPE_FILE (type), | |
961 | TYPE_LOCALTYPE_LINE (type)); | |
c906108c SS |
962 | |
963 | fprintfi_filtered (level, stream, "}"); | |
964 | } | |
c5aa993b JM |
965 | if (TYPE_CODE (type) == TYPE_CODE_TEMPLATE) |
966 | goto go_back; | |
c906108c SS |
967 | break; |
968 | ||
969 | case TYPE_CODE_ENUM: | |
970 | c_type_print_cv_qualifier (type, stream, 0, 1); | |
971 | /* HP C supports sized enums */ | |
972 | if (hp_som_som_object_present) | |
c5aa993b JM |
973 | switch (TYPE_LENGTH (type)) |
974 | { | |
975 | case 1: | |
976 | fputs_filtered ("char ", stream); | |
977 | break; | |
978 | case 2: | |
979 | fputs_filtered ("short ", stream); | |
980 | break; | |
981 | default: | |
982 | break; | |
983 | } | |
984 | fprintf_filtered (stream, "enum "); | |
c906108c SS |
985 | /* Print the tag name if it exists. |
986 | The aCC compiler emits a spurious | |
987 | "{unnamed struct}"/"{unnamed union}"/"{unnamed enum}" | |
988 | tag for unnamed struct/union/enum's, which we don't | |
989 | want to print. */ | |
990 | if (TYPE_TAG_NAME (type) != NULL && | |
c5aa993b | 991 | strncmp (TYPE_TAG_NAME (type), "{unnamed", 8)) |
c906108c SS |
992 | { |
993 | fputs_filtered (TYPE_TAG_NAME (type), stream); | |
994 | if (show > 0) | |
995 | fputs_filtered (" ", stream); | |
996 | } | |
997 | ||
998 | wrap_here (" "); | |
999 | if (show < 0) | |
1000 | { | |
1001 | /* If we just printed a tag name, no need to print anything else. */ | |
1002 | if (TYPE_TAG_NAME (type) == NULL) | |
1003 | fprintf_filtered (stream, "{...}"); | |
1004 | } | |
1005 | else if (show > 0 || TYPE_TAG_NAME (type) == NULL) | |
1006 | { | |
1007 | fprintf_filtered (stream, "{"); | |
1008 | len = TYPE_NFIELDS (type); | |
1009 | lastval = 0; | |
1010 | for (i = 0; i < len; i++) | |
1011 | { | |
1012 | QUIT; | |
c5aa993b JM |
1013 | if (i) |
1014 | fprintf_filtered (stream, ", "); | |
c906108c SS |
1015 | wrap_here (" "); |
1016 | fputs_filtered (TYPE_FIELD_NAME (type, i), stream); | |
1017 | if (lastval != TYPE_FIELD_BITPOS (type, i)) | |
1018 | { | |
1019 | fprintf_filtered (stream, " = %d", TYPE_FIELD_BITPOS (type, i)); | |
1020 | lastval = TYPE_FIELD_BITPOS (type, i); | |
1021 | } | |
1022 | lastval++; | |
1023 | } | |
1024 | fprintf_filtered (stream, "}"); | |
1025 | } | |
1026 | break; | |
1027 | ||
1028 | case TYPE_CODE_VOID: | |
1029 | fprintf_filtered (stream, "void"); | |
1030 | break; | |
1031 | ||
1032 | case TYPE_CODE_UNDEF: | |
1033 | fprintf_filtered (stream, "struct <unknown>"); | |
1034 | break; | |
1035 | ||
1036 | case TYPE_CODE_ERROR: | |
1037 | fprintf_filtered (stream, "<unknown type>"); | |
1038 | break; | |
1039 | ||
1040 | case TYPE_CODE_RANGE: | |
1041 | /* This should not occur */ | |
1042 | fprintf_filtered (stream, "<range type>"); | |
1043 | break; | |
1044 | ||
1045 | case TYPE_CODE_TEMPLATE: | |
1046 | /* Called on "ptype t" where "t" is a template. | |
1047 | Prints the template header (with args), e.g.: | |
c5aa993b | 1048 | template <class T1, class T2> class " |
c906108c SS |
1049 | and then merges with the struct/union/class code to |
1050 | print the rest of the definition. */ | |
1051 | c_type_print_cv_qualifier (type, stream, 0, 1); | |
1052 | fprintf_filtered (stream, "template <"); | |
c5aa993b JM |
1053 | for (i = 0; i < TYPE_NTEMPLATE_ARGS (type); i++) |
1054 | { | |
1055 | struct template_arg templ_arg; | |
1056 | templ_arg = TYPE_TEMPLATE_ARG (type, i); | |
1057 | fprintf_filtered (stream, "class %s", templ_arg.name); | |
1058 | if (i < TYPE_NTEMPLATE_ARGS (type) - 1) | |
1059 | fprintf_filtered (stream, ", "); | |
1060 | } | |
c906108c SS |
1061 | fprintf_filtered (stream, "> class "); |
1062 | /* Yuck, factor this out to a subroutine so we can call | |
1063 | it and return to the point marked with the "goback:" label... - RT */ | |
c5aa993b JM |
1064 | goto struct_union; |
1065 | go_back: | |
1066 | if (TYPE_NINSTANTIATIONS (type) > 0) | |
1067 | { | |
1068 | fprintf_filtered (stream, "\ntemplate instantiations:\n"); | |
1069 | for (i = 0; i < TYPE_NINSTANTIATIONS (type); i++) | |
1070 | { | |
1071 | fprintf_filtered (stream, " "); | |
1072 | c_type_print_base (TYPE_INSTANTIATION (type, i), stream, 0, level); | |
1073 | if (i < TYPE_NINSTANTIATIONS (type) - 1) | |
1074 | fprintf_filtered (stream, "\n"); | |
1075 | } | |
1076 | } | |
c906108c | 1077 | break; |
c5aa993b | 1078 | |
c906108c SS |
1079 | default: |
1080 | /* Handle types not explicitly handled by the other cases, | |
c5aa993b JM |
1081 | such as fundamental types. For these, just print whatever |
1082 | the type name is, as recorded in the type itself. If there | |
1083 | is no type name, then complain. */ | |
c906108c SS |
1084 | if (TYPE_NAME (type) != NULL) |
1085 | { | |
c5aa993b | 1086 | c_type_print_cv_qualifier (type, stream, 0, 1); |
c906108c SS |
1087 | fputs_filtered (TYPE_NAME (type), stream); |
1088 | } | |
1089 | else | |
1090 | { | |
1091 | /* At least for dump_symtab, it is important that this not be | |
1092 | an error (). */ | |
1093 | fprintf_filtered (stream, "<invalid type code %d>", | |
1094 | TYPE_CODE (type)); | |
1095 | } | |
1096 | break; | |
1097 | } | |
1098 | } |