import gdb-2000-02-01 snapshot
[deliverable/binutils-gdb.git] / gdb / c-typeprint.c
1 /* Support for printing C and C++ types for GDB, the GNU debugger.
2 Copyright 1986, 1988, 1989, 1991, 1993-1996, 1998-2000
3 Free Software Foundation, Inc.
4
5 This file is part of GDB.
6
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.
11
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.
16
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. */
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
45 static void cp_type_print_method_args (struct type ** args, char *prefix,
46 char *varstring, int staticp,
47 struct ui_file *stream);
48
49 static void c_type_print_args (struct type *, struct ui_file *);
50
51 static void cp_type_print_derivation_info (struct ui_file *, struct type *);
52
53 void c_type_print_varspec_prefix (struct type *, struct ui_file *, int,
54 int);
55
56 static void c_type_print_cv_qualifier (struct type *, struct ui_file *,
57 int, int);
58 \f
59
60
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)
67 struct type *type;
68 struct symbol *new;
69 struct ui_file *stream;
70 {
71 CHECK_TYPEDEF (type);
72 switch (current_language->la_language)
73 {
74 #ifdef _LANG_c
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));
82 break;
83 #endif
84 #ifdef _LANG_m2
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));
90 else
91 fprintf_filtered (stream, "<builtin> = ");
92 type_print (type, "", stream, 0);
93 break;
94 #endif
95 #ifdef _LANG_chill
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));
101 else
102 fprintf_filtered (stream, "<builtin> = ");
103 type_print (type, "", stream, 0);
104 break;
105 #endif
106 default:
107 error ("Language not supported.");
108 }
109 fprintf_filtered (stream, ";\n");
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 struct ui_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 ||
133 /* Need a space if going to print stars or brackets;
134 but not if we will print just a type name. */
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,
150 so don't print an additional pair of ()'s */
151
152 demangled_args = strchr (varstring, '(') != NULL;
153 c_type_print_varspec_suffix (type, stream, show, 0, demangled_args);
154 }
155 }
156
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
161 class A { int a; };
162 class B : public A {int b; };
163 class C : public B {int c; };
164
165 Print the type of class C as:
166
167 class C : public B {
168 int c;
169 }
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
174 class C : public B : public A {
175 int c;
176 }
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 struct ui_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 ",
196 BASETYPE_VIA_PUBLIC (type, i) ? "public"
197 : (TYPE_FIELD_PROTECTED (type, i) ? "protected" : "private"),
198 BASETYPE_VIA_VIRTUAL (type, i) ? " virtual" : "");
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. */
208
209 static void
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 struct ui_file *stream;
216 {
217 int i;
218
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 {
224 i = !staticp; /* skip the class variable */
225 while (1)
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 }
240 }
241 else if (current_language->la_language == language_cplus)
242 {
243 fprintf_filtered (stream, "void");
244 }
245
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 struct ui_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
291 c_type_print_base (TYPE_DOMAIN_TYPE (type), stream, 0, passed_a_ptr);
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 case TYPE_CODE_TEMPLATE:
342 /* These types need no prefix. They are listed here so that
343 gcc -Wall will reveal any types that haven't been handled. */
344 break;
345 default:
346 error ("type not handled in c_type_print_varspec_prefix()");
347 break;
348 }
349 }
350
351 /* Print out "const" and "volatile" attributes.
352 TYPE is a pointer to the type being printed out.
353 STREAM is the output destination.
354 NEED_SPACE = 1 indicates an initial white space is needed */
355
356 static void
357 c_type_print_cv_qualifier (type, stream, need_pre_space, need_post_space)
358 struct type *type;
359 struct ui_file *stream;
360 int need_pre_space;
361 int need_post_space;
362 {
363 int flag = 0;
364
365 if (TYPE_CONST (type))
366 {
367 if (need_pre_space)
368 fprintf_filtered (stream, " ");
369 fprintf_filtered (stream, "const");
370 flag = 1;
371 }
372
373 if (TYPE_VOLATILE (type))
374 {
375 if (flag || need_pre_space)
376 fprintf_filtered (stream, " ");
377 fprintf_filtered (stream, "volatile");
378 flag = 1;
379 }
380
381 if (flag && need_post_space)
382 fprintf_filtered (stream, " ");
383 }
384
385
386
387
388 static void
389 c_type_print_args (type, stream)
390 struct type *type;
391 struct ui_file *stream;
392 {
393 int i;
394 struct type **args;
395
396 fprintf_filtered (stream, "(");
397 args = TYPE_ARG_TYPES (type);
398 if (args != NULL)
399 {
400 if (args[1] == NULL)
401 {
402 fprintf_filtered (stream, "...");
403 }
404 else if ((args[1]->code == TYPE_CODE_VOID) &&
405 (current_language->la_language == language_cplus))
406 {
407 fprintf_filtered (stream, "void");
408 }
409 else
410 {
411 for (i = 1;
412 args[i] != NULL && args[i]->code != TYPE_CODE_VOID;
413 i++)
414 {
415 c_print_type (args[i], "", stream, -1, 0);
416 if (args[i + 1] == NULL)
417 {
418 fprintf_filtered (stream, "...");
419 }
420 else if (args[i + 1]->code != TYPE_CODE_VOID)
421 {
422 fprintf_filtered (stream, ",");
423 wrap_here (" ");
424 }
425 }
426 }
427 }
428 else if (current_language->la_language == language_cplus)
429 {
430 fprintf_filtered (stream, "void");
431 }
432
433 fprintf_filtered (stream, ")");
434 }
435
436 /* Print any array sizes, function arguments or close parentheses
437 needed after the variable name (to describe its type).
438 Args work like c_type_print_varspec_prefix. */
439
440 void
441 c_type_print_varspec_suffix (type, stream, show, passed_a_ptr, demangled_args)
442 struct type *type;
443 struct ui_file *stream;
444 int show;
445 int passed_a_ptr;
446 int demangled_args;
447 {
448 if (type == 0)
449 return;
450
451 if (TYPE_NAME (type) && show <= 0)
452 return;
453
454 QUIT;
455
456 switch (TYPE_CODE (type))
457 {
458 case TYPE_CODE_ARRAY:
459 if (passed_a_ptr)
460 fprintf_filtered (stream, ")");
461
462 fprintf_filtered (stream, "[");
463 if (TYPE_LENGTH (type) >= 0 && TYPE_LENGTH (TYPE_TARGET_TYPE (type)) > 0
464 && TYPE_ARRAY_UPPER_BOUND_TYPE (type) != BOUND_CANNOT_BE_DETERMINED)
465 fprintf_filtered (stream, "%d",
466 (TYPE_LENGTH (type)
467 / TYPE_LENGTH (TYPE_TARGET_TYPE (type))));
468 fprintf_filtered (stream, "]");
469
470 c_type_print_varspec_suffix (TYPE_TARGET_TYPE (type), stream, 0, 0, 0);
471 break;
472
473 case TYPE_CODE_MEMBER:
474 if (passed_a_ptr)
475 fprintf_filtered (stream, ")");
476 c_type_print_varspec_suffix (TYPE_TARGET_TYPE (type), stream, 0, 0, 0);
477 break;
478
479 case TYPE_CODE_METHOD:
480 if (passed_a_ptr)
481 fprintf_filtered (stream, ")");
482 c_type_print_varspec_suffix (TYPE_TARGET_TYPE (type), stream, 0, 0, 0);
483 if (passed_a_ptr)
484 {
485 c_type_print_args (type, stream);
486 }
487 break;
488
489 case TYPE_CODE_PTR:
490 case TYPE_CODE_REF:
491 c_type_print_varspec_suffix (TYPE_TARGET_TYPE (type), stream, 0, 1, 0);
492 break;
493
494 case TYPE_CODE_FUNC:
495 if (passed_a_ptr)
496 fprintf_filtered (stream, ")");
497 if (!demangled_args)
498 {
499 int i, len = TYPE_NFIELDS (type);
500 fprintf_filtered (stream, "(");
501 if ((len == 0) && (current_language->la_language == language_cplus))
502 {
503 fprintf_filtered (stream, "void");
504 }
505 else
506 for (i = 0; i < len; i++)
507 {
508 if (i > 0)
509 {
510 fputs_filtered (", ", stream);
511 wrap_here (" ");
512 }
513 c_print_type (TYPE_FIELD_TYPE (type, i), "", stream, -1, 0);
514 }
515 fprintf_filtered (stream, ")");
516 }
517 c_type_print_varspec_suffix (TYPE_TARGET_TYPE (type), stream, 0,
518 passed_a_ptr, 0);
519 break;
520
521 case TYPE_CODE_UNDEF:
522 case TYPE_CODE_STRUCT:
523 case TYPE_CODE_UNION:
524 case TYPE_CODE_ENUM:
525 case TYPE_CODE_INT:
526 case TYPE_CODE_FLT:
527 case TYPE_CODE_VOID:
528 case TYPE_CODE_ERROR:
529 case TYPE_CODE_CHAR:
530 case TYPE_CODE_BOOL:
531 case TYPE_CODE_SET:
532 case TYPE_CODE_RANGE:
533 case TYPE_CODE_STRING:
534 case TYPE_CODE_BITSTRING:
535 case TYPE_CODE_COMPLEX:
536 case TYPE_CODE_TYPEDEF:
537 case TYPE_CODE_TEMPLATE:
538 /* These types do not need a suffix. They are listed so that
539 gcc -Wall will report types that may not have been considered. */
540 break;
541 default:
542 error ("type not handled in c_type_print_varspec_suffix()");
543 break;
544 }
545 }
546
547 /* Print the name of the type (or the ultimate pointer target,
548 function value or array element), or the description of a
549 structure or union.
550
551 SHOW positive means print details about the type (e.g. enum values),
552 and print structure elements passing SHOW - 1 for show.
553 SHOW negative means just print the type name or struct tag if there is one.
554 If there is no name, print something sensible but concise like
555 "struct {...}".
556 SHOW zero means just print the type name or struct tag if there is one.
557 If there is no name, print something sensible but not as concise like
558 "struct {int x; int y;}".
559
560 LEVEL is the number of spaces to indent by.
561 We increase it for some recursive calls. */
562
563 void
564 c_type_print_base (type, stream, show, level)
565 struct type *type;
566 struct ui_file *stream;
567 int show;
568 int level;
569 {
570 register int i;
571 register int len;
572 register int lastval;
573 char *mangled_name;
574 char *demangled_name;
575 char *demangled_no_static;
576 enum
577 {
578 s_none, s_public, s_private, s_protected
579 }
580 section_type;
581 int need_access_label = 0;
582 int j, len2;
583
584 QUIT;
585
586 wrap_here (" ");
587 if (type == NULL)
588 {
589 fputs_filtered ("<type unknown>", stream);
590 return;
591 }
592
593 /* When SHOW is zero or less, and there is a valid type name, then always
594 just print the type name directly from the type. */
595 /* If we have "typedef struct foo {. . .} bar;" do we want to print it
596 as "struct foo" or as "bar"? Pick the latter, because C++ folk tend
597 to expect things like "class5 *foo" rather than "struct class5 *foo". */
598
599 if (show <= 0
600 && TYPE_NAME (type) != NULL)
601 {
602 c_type_print_cv_qualifier (type, stream, 0, 1);
603 fputs_filtered (TYPE_NAME (type), stream);
604 return;
605 }
606
607 CHECK_TYPEDEF (type);
608
609 switch (TYPE_CODE (type))
610 {
611 case TYPE_CODE_TYPEDEF:
612 case TYPE_CODE_ARRAY:
613 case TYPE_CODE_PTR:
614 case TYPE_CODE_MEMBER:
615 case TYPE_CODE_REF:
616 case TYPE_CODE_FUNC:
617 case TYPE_CODE_METHOD:
618 c_type_print_base (TYPE_TARGET_TYPE (type), stream, show, level);
619 break;
620
621 case TYPE_CODE_STRUCT:
622 c_type_print_cv_qualifier (type, stream, 0, 1);
623 /* Note TYPE_CODE_STRUCT and TYPE_CODE_CLASS have the same value,
624 * so we use another means for distinguishing them.
625 */
626 if (HAVE_CPLUS_STRUCT (type))
627 {
628 switch (TYPE_DECLARED_TYPE (type))
629 {
630 case DECLARED_TYPE_CLASS:
631 fprintf_filtered (stream, "class ");
632 break;
633 case DECLARED_TYPE_UNION:
634 fprintf_filtered (stream, "union ");
635 break;
636 case DECLARED_TYPE_STRUCT:
637 fprintf_filtered (stream, "struct ");
638 break;
639 default:
640 /* If there is a CPLUS_STRUCT, assume class if not
641 * otherwise specified in the declared_type field.
642 */
643 fprintf_filtered (stream, "class ");
644 break;
645 } /* switch */
646 }
647 else
648 {
649 /* If not CPLUS_STRUCT, then assume it's a C struct */
650 fprintf_filtered (stream, "struct ");
651 }
652 goto struct_union;
653
654 case TYPE_CODE_UNION:
655 c_type_print_cv_qualifier (type, stream, 0, 1);
656 fprintf_filtered (stream, "union ");
657
658 struct_union:
659
660 /* Print the tag if it exists.
661 * The HP aCC compiler emits
662 * a spurious "{unnamed struct}"/"{unnamed union}"/"{unnamed enum}"
663 * tag for unnamed struct/union/enum's, which we don't
664 * want to print.
665 */
666 if (TYPE_TAG_NAME (type) != NULL &&
667 strncmp (TYPE_TAG_NAME (type), "{unnamed", 8))
668 {
669 fputs_filtered (TYPE_TAG_NAME (type), stream);
670 if (show > 0)
671 fputs_filtered (" ", stream);
672 }
673 wrap_here (" ");
674 if (show < 0)
675 {
676 /* If we just printed a tag name, no need to print anything else. */
677 if (TYPE_TAG_NAME (type) == NULL)
678 fprintf_filtered (stream, "{...}");
679 }
680 else if (show > 0 || TYPE_TAG_NAME (type) == NULL)
681 {
682 cp_type_print_derivation_info (stream, type);
683
684 fprintf_filtered (stream, "{\n");
685 if ((TYPE_NFIELDS (type) == 0) && (TYPE_NFN_FIELDS (type) == 0))
686 {
687 if (TYPE_FLAGS (type) & TYPE_FLAG_STUB)
688 fprintfi_filtered (level + 4, stream, "<incomplete type>\n");
689 else
690 fprintfi_filtered (level + 4, stream, "<no data fields>\n");
691 }
692
693 /* Start off with no specific section type, so we can print
694 one for the first field we find, and use that section type
695 thereafter until we find another type. */
696
697 section_type = s_none;
698
699 /* For a class, if all members are private, there's no need
700 for a "private:" label; similarly, for a struct or union
701 masquerading as a class, if all members are public, there's
702 no need for a "public:" label. */
703
704 if ((TYPE_DECLARED_TYPE (type) == DECLARED_TYPE_CLASS) ||
705 (TYPE_DECLARED_TYPE (type) == DECLARED_TYPE_TEMPLATE))
706 {
707 QUIT;
708 len = TYPE_NFIELDS (type);
709 for (i = TYPE_N_BASECLASSES (type); i < len; i++)
710 if (!TYPE_FIELD_PRIVATE (type, i))
711 {
712 need_access_label = 1;
713 break;
714 }
715 QUIT;
716 if (!need_access_label)
717 {
718 len2 = TYPE_NFN_FIELDS (type);
719 for (j = 0; j < len2; j++)
720 {
721 len = TYPE_FN_FIELDLIST_LENGTH (type, j);
722 for (i = 0; i < len; i++)
723 if (!TYPE_FN_FIELD_PRIVATE (TYPE_FN_FIELDLIST1 (type, j), i))
724 {
725 need_access_label = 1;
726 break;
727 }
728 if (need_access_label)
729 break;
730 }
731 }
732 }
733 else if ((TYPE_DECLARED_TYPE (type) == DECLARED_TYPE_STRUCT) ||
734 (TYPE_DECLARED_TYPE (type) == DECLARED_TYPE_UNION))
735 {
736 QUIT;
737 len = TYPE_NFIELDS (type);
738 for (i = TYPE_N_BASECLASSES (type); i < len; i++)
739 if (TYPE_FIELD_PRIVATE (type, i) || TYPE_FIELD_PROTECTED (type, i))
740 {
741 need_access_label = 1;
742 break;
743 }
744 QUIT;
745 if (!need_access_label)
746 {
747 len2 = TYPE_NFN_FIELDS (type);
748 for (j = 0; j < len2; j++)
749 {
750 QUIT;
751 len = TYPE_FN_FIELDLIST_LENGTH (type, j);
752 for (i = 0; i < len; i++)
753 if (TYPE_FN_FIELD_PRIVATE (TYPE_FN_FIELDLIST1 (type, j), i) ||
754 TYPE_FN_FIELD_PROTECTED (TYPE_FN_FIELDLIST1 (type, j), i))
755 {
756 need_access_label = 1;
757 break;
758 }
759 if (need_access_label)
760 break;
761 }
762 }
763 }
764
765 /* If there is a base class for this type,
766 do not print the field that it occupies. */
767
768 len = TYPE_NFIELDS (type);
769 for (i = TYPE_N_BASECLASSES (type); i < len; i++)
770 {
771 QUIT;
772 /* Don't print out virtual function table. */
773 /* HP ANSI C++ case */
774 if (TYPE_HAS_VTABLE (type) && (STREQN (TYPE_FIELD_NAME (type, i), "__vfp", 5)))
775 continue;
776 /* Other compilers */
777 if (STREQN (TYPE_FIELD_NAME (type, i), "_vptr", 5)
778 && is_cplus_marker ((TYPE_FIELD_NAME (type, i))[5]))
779 continue;
780
781 /* If this is a C++ class we can print the various C++ section
782 labels. */
783
784 if (HAVE_CPLUS_STRUCT (type) && need_access_label)
785 {
786 if (TYPE_FIELD_PROTECTED (type, i))
787 {
788 if (section_type != s_protected)
789 {
790 section_type = s_protected;
791 fprintfi_filtered (level + 2, stream,
792 "protected:\n");
793 }
794 }
795 else if (TYPE_FIELD_PRIVATE (type, i))
796 {
797 if (section_type != s_private)
798 {
799 section_type = s_private;
800 fprintfi_filtered (level + 2, stream, "private:\n");
801 }
802 }
803 else
804 {
805 if (section_type != s_public)
806 {
807 section_type = s_public;
808 fprintfi_filtered (level + 2, stream, "public:\n");
809 }
810 }
811 }
812
813 print_spaces_filtered (level + 4, stream);
814 if (TYPE_FIELD_STATIC (type, i))
815 {
816 fprintf_filtered (stream, "static ");
817 }
818 c_print_type (TYPE_FIELD_TYPE (type, i),
819 TYPE_FIELD_NAME (type, i),
820 stream, show - 1, level + 4);
821 if (!TYPE_FIELD_STATIC (type, i)
822 && TYPE_FIELD_PACKED (type, i))
823 {
824 /* It is a bitfield. This code does not attempt
825 to look at the bitpos and reconstruct filler,
826 unnamed fields. This would lead to misleading
827 results if the compiler does not put out fields
828 for such things (I don't know what it does). */
829 fprintf_filtered (stream, " : %d",
830 TYPE_FIELD_BITSIZE (type, i));
831 }
832 fprintf_filtered (stream, ";\n");
833 }
834
835 /* If there are both fields and methods, put a space between. */
836 len = TYPE_NFN_FIELDS (type);
837 if (len && section_type != s_none)
838 fprintf_filtered (stream, "\n");
839
840 /* C++: print out the methods */
841 for (i = 0; i < len; i++)
842 {
843 struct fn_field *f = TYPE_FN_FIELDLIST1 (type, i);
844 int j, len2 = TYPE_FN_FIELDLIST_LENGTH (type, i);
845 char *method_name = TYPE_FN_FIELDLIST_NAME (type, i);
846 char *name = type_name_no_tag (type);
847 int is_constructor = name && STREQ (method_name, name);
848 for (j = 0; j < len2; j++)
849 {
850 char *physname = TYPE_FN_FIELD_PHYSNAME (f, j);
851 int is_full_physname_constructor =
852 ((physname[0] == '_' && physname[1] == '_'
853 && strchr ("0123456789Qt", physname[2]))
854 || STREQN (physname, "__ct__", 6)
855 || DESTRUCTOR_PREFIX_P (physname)
856 || STREQN (physname, "__dt__", 6));
857
858 QUIT;
859 if (TYPE_FN_FIELD_PROTECTED (f, j))
860 {
861 if (section_type != s_protected)
862 {
863 section_type = s_protected;
864 fprintfi_filtered (level + 2, stream,
865 "protected:\n");
866 }
867 }
868 else if (TYPE_FN_FIELD_PRIVATE (f, j))
869 {
870 if (section_type != s_private)
871 {
872 section_type = s_private;
873 fprintfi_filtered (level + 2, stream, "private:\n");
874 }
875 }
876 else
877 {
878 if (section_type != s_public)
879 {
880 section_type = s_public;
881 fprintfi_filtered (level + 2, stream, "public:\n");
882 }
883 }
884
885 print_spaces_filtered (level + 4, stream);
886 if (TYPE_FN_FIELD_VIRTUAL_P (f, j))
887 fprintf_filtered (stream, "virtual ");
888 else if (TYPE_FN_FIELD_STATIC_P (f, j))
889 fprintf_filtered (stream, "static ");
890 if (TYPE_TARGET_TYPE (TYPE_FN_FIELD_TYPE (f, j)) == 0)
891 {
892 /* Keep GDB from crashing here. */
893 fprintf_filtered (stream, "<undefined type> %s;\n",
894 TYPE_FN_FIELD_PHYSNAME (f, j));
895 break;
896 }
897 else if (!is_constructor && /* constructors don't have declared types */
898 !is_full_physname_constructor && /* " " */
899 !strstr (method_name, "operator ")) /* Not a type conversion operator */
900 /* (note space -- other operators don't have it) */
901 {
902 type_print (TYPE_TARGET_TYPE (TYPE_FN_FIELD_TYPE (f, j)),
903 "", stream, -1);
904 fputs_filtered (" ", stream);
905 }
906 if (TYPE_FN_FIELD_STUB (f, j))
907 /* Build something we can demangle. */
908 mangled_name = gdb_mangle_name (type, i, j);
909 else
910 mangled_name = TYPE_FN_FIELD_PHYSNAME (f, j);
911
912 demangled_name =
913 cplus_demangle (mangled_name,
914 DMGL_ANSI | DMGL_PARAMS);
915 if (demangled_name == NULL)
916 {
917 /* in some cases (for instance with the HP demangling),
918 if a function has more than 10 arguments,
919 the demangling will fail.
920 Let's try to reconstruct the function signature from
921 the symbol information */
922 if (!TYPE_FN_FIELD_STUB (f, j))
923 cp_type_print_method_args (TYPE_FN_FIELD_ARGS (f, j), "",
924 method_name,
925 TYPE_FN_FIELD_STATIC_P (f, j),
926 stream);
927 else
928 fprintf_filtered (stream, "<badly mangled name '%s'>",
929 mangled_name);
930 }
931 else
932 {
933 char *p;
934 char *demangled_no_class = strrchr (demangled_name, ':');
935
936 if (demangled_no_class == NULL)
937 demangled_no_class = demangled_name;
938 else
939 {
940 ++demangled_no_class; /* skip over last ':' */
941 }
942 /* get rid of the static word appended by the demangler */
943 p = strstr (demangled_no_class, " static");
944 if (p != NULL)
945 {
946 int length = p - demangled_no_class;
947 demangled_no_static = (char *) xmalloc (length + 1);
948 strncpy (demangled_no_static, demangled_no_class, length);
949 *(demangled_no_static + length) = '\0';
950 fputs_filtered (demangled_no_static, stream);
951 free (demangled_no_static);
952 }
953 else
954 fputs_filtered (demangled_no_class, stream);
955 free (demangled_name);
956 }
957
958 if (TYPE_FN_FIELD_STUB (f, j))
959 free (mangled_name);
960
961 fprintf_filtered (stream, ";\n");
962 }
963 }
964
965 fprintfi_filtered (level, stream, "}");
966
967 if (TYPE_LOCALTYPE_PTR (type) && show >= 0)
968 fprintfi_filtered (level, stream, " (Local at %s:%d)\n",
969 TYPE_LOCALTYPE_FILE (type),
970 TYPE_LOCALTYPE_LINE (type));
971 }
972 if (TYPE_CODE (type) == TYPE_CODE_TEMPLATE)
973 goto go_back;
974 break;
975
976 case TYPE_CODE_ENUM:
977 c_type_print_cv_qualifier (type, stream, 0, 1);
978 /* HP C supports sized enums */
979 if (hp_som_som_object_present)
980 switch (TYPE_LENGTH (type))
981 {
982 case 1:
983 fputs_filtered ("char ", stream);
984 break;
985 case 2:
986 fputs_filtered ("short ", stream);
987 break;
988 default:
989 break;
990 }
991 fprintf_filtered (stream, "enum ");
992 /* Print the tag name if it exists.
993 The aCC compiler emits a spurious
994 "{unnamed struct}"/"{unnamed union}"/"{unnamed enum}"
995 tag for unnamed struct/union/enum's, which we don't
996 want to print. */
997 if (TYPE_TAG_NAME (type) != NULL &&
998 strncmp (TYPE_TAG_NAME (type), "{unnamed", 8))
999 {
1000 fputs_filtered (TYPE_TAG_NAME (type), stream);
1001 if (show > 0)
1002 fputs_filtered (" ", stream);
1003 }
1004
1005 wrap_here (" ");
1006 if (show < 0)
1007 {
1008 /* If we just printed a tag name, no need to print anything else. */
1009 if (TYPE_TAG_NAME (type) == NULL)
1010 fprintf_filtered (stream, "{...}");
1011 }
1012 else if (show > 0 || TYPE_TAG_NAME (type) == NULL)
1013 {
1014 fprintf_filtered (stream, "{");
1015 len = TYPE_NFIELDS (type);
1016 lastval = 0;
1017 for (i = 0; i < len; i++)
1018 {
1019 QUIT;
1020 if (i)
1021 fprintf_filtered (stream, ", ");
1022 wrap_here (" ");
1023 fputs_filtered (TYPE_FIELD_NAME (type, i), stream);
1024 if (lastval != TYPE_FIELD_BITPOS (type, i))
1025 {
1026 fprintf_filtered (stream, " = %d", TYPE_FIELD_BITPOS (type, i));
1027 lastval = TYPE_FIELD_BITPOS (type, i);
1028 }
1029 lastval++;
1030 }
1031 fprintf_filtered (stream, "}");
1032 }
1033 break;
1034
1035 case TYPE_CODE_VOID:
1036 fprintf_filtered (stream, "void");
1037 break;
1038
1039 case TYPE_CODE_UNDEF:
1040 fprintf_filtered (stream, "struct <unknown>");
1041 break;
1042
1043 case TYPE_CODE_ERROR:
1044 fprintf_filtered (stream, "<unknown type>");
1045 break;
1046
1047 case TYPE_CODE_RANGE:
1048 /* This should not occur */
1049 fprintf_filtered (stream, "<range type>");
1050 break;
1051
1052 case TYPE_CODE_TEMPLATE:
1053 /* Called on "ptype t" where "t" is a template.
1054 Prints the template header (with args), e.g.:
1055 template <class T1, class T2> class "
1056 and then merges with the struct/union/class code to
1057 print the rest of the definition. */
1058 c_type_print_cv_qualifier (type, stream, 0, 1);
1059 fprintf_filtered (stream, "template <");
1060 for (i = 0; i < TYPE_NTEMPLATE_ARGS (type); i++)
1061 {
1062 struct template_arg templ_arg;
1063 templ_arg = TYPE_TEMPLATE_ARG (type, i);
1064 fprintf_filtered (stream, "class %s", templ_arg.name);
1065 if (i < TYPE_NTEMPLATE_ARGS (type) - 1)
1066 fprintf_filtered (stream, ", ");
1067 }
1068 fprintf_filtered (stream, "> class ");
1069 /* Yuck, factor this out to a subroutine so we can call
1070 it and return to the point marked with the "goback:" label... - RT */
1071 goto struct_union;
1072 go_back:
1073 if (TYPE_NINSTANTIATIONS (type) > 0)
1074 {
1075 fprintf_filtered (stream, "\ntemplate instantiations:\n");
1076 for (i = 0; i < TYPE_NINSTANTIATIONS (type); i++)
1077 {
1078 fprintf_filtered (stream, " ");
1079 c_type_print_base (TYPE_INSTANTIATION (type, i), stream, 0, level);
1080 if (i < TYPE_NINSTANTIATIONS (type) - 1)
1081 fprintf_filtered (stream, "\n");
1082 }
1083 }
1084 break;
1085
1086 default:
1087 /* Handle types not explicitly handled by the other cases,
1088 such as fundamental types. For these, just print whatever
1089 the type name is, as recorded in the type itself. If there
1090 is no type name, then complain. */
1091 if (TYPE_NAME (type) != NULL)
1092 {
1093 c_type_print_cv_qualifier (type, stream, 0, 1);
1094 fputs_filtered (TYPE_NAME (type), stream);
1095 }
1096 else
1097 {
1098 /* At least for dump_symtab, it is important that this not be
1099 an error (). */
1100 fprintf_filtered (stream, "<invalid type code %d>",
1101 TYPE_CODE (type));
1102 }
1103 break;
1104 }
1105 }
This page took 0.053393 seconds and 4 git commands to generate.