1 /* Support for printing C and C++ types for GDB, the GNU debugger.
2 Copyright (C) 1986-2013 Free Software Foundation, Inc.
4 This file is part of GDB.
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <http://www.gnu.org/licenses/>. */
20 #include "gdb_obstack.h"
21 #include "bfd.h" /* Binary File Description. */
24 #include "expression.h"
31 #include "typeprint.h"
34 #include "gdb_string.h"
36 #include "cp-support.h"
38 static void c_type_print_varspec_prefix (struct type
*,
41 const struct type_print_options
*);
43 /* Print "const", "volatile", or address space modifiers. */
44 static void c_type_print_modifier (struct type
*,
49 /* A callback function for cp_canonicalize_string_full that uses
50 find_typedef_in_hash. */
53 find_typedef_for_canonicalize (struct type
*t
, void *data
)
55 return find_typedef_in_hash (data
, t
);
58 /* Print NAME on STREAM. If the 'raw' field of FLAGS is not set,
59 canonicalize NAME using the local typedefs first. */
62 print_name_maybe_canonical (const char *name
,
63 const struct type_print_options
*flags
,
64 struct ui_file
*stream
)
69 s
= cp_canonicalize_string_full (name
,
70 find_typedef_for_canonicalize
,
73 fputs_filtered (s
? s
: name
, stream
);
79 /* LEVEL is the depth to indent lines by. */
82 c_print_type (struct type
*type
,
83 const char *varstring
,
84 struct ui_file
*stream
,
86 const struct type_print_options
*flags
)
91 const char *local_name
;
96 local_name
= find_typedef_in_hash (flags
, type
);
97 if (local_name
!= NULL
)
99 fputs_filtered (local_name
, stream
);
100 if (varstring
!= NULL
&& *varstring
!= '\0')
101 fputs_filtered (" ", stream
);
105 c_type_print_base (type
, stream
, show
, level
, flags
);
106 code
= TYPE_CODE (type
);
107 if ((varstring
!= NULL
&& *varstring
!= '\0')
108 /* Need a space if going to print stars or brackets;
109 but not if we will print just a type name. */
110 || ((show
> 0 || TYPE_NAME (type
) == 0)
111 && (code
== TYPE_CODE_PTR
|| code
== TYPE_CODE_FUNC
112 || code
== TYPE_CODE_METHOD
113 || (code
== TYPE_CODE_ARRAY
114 && !TYPE_VECTOR (type
))
115 || code
== TYPE_CODE_MEMBERPTR
116 || code
== TYPE_CODE_METHODPTR
117 || code
== TYPE_CODE_REF
)))
118 fputs_filtered (" ", stream
);
119 need_post_space
= (varstring
!= NULL
&& strcmp (varstring
, "") != 0);
120 c_type_print_varspec_prefix (type
, stream
, show
, 0, need_post_space
,
124 if (varstring
!= NULL
)
126 fputs_filtered (varstring
, stream
);
128 /* For demangled function names, we have the arglist as part of
129 the name, so don't print an additional pair of ()'s. */
130 if (local_name
== NULL
)
132 demangled_args
= strchr (varstring
, '(') != NULL
;
133 c_type_print_varspec_suffix (type
, stream
, show
,
140 /* Print a typedef using C syntax. TYPE is the underlying type.
141 NEW_SYMBOL is the symbol naming the type. STREAM is the stream on
145 c_print_typedef (struct type
*type
,
146 struct symbol
*new_symbol
,
147 struct ui_file
*stream
)
149 CHECK_TYPEDEF (type
);
150 fprintf_filtered (stream
, "typedef ");
151 type_print (type
, "", stream
, 0);
152 if (TYPE_NAME ((SYMBOL_TYPE (new_symbol
))) == 0
153 || strcmp (TYPE_NAME ((SYMBOL_TYPE (new_symbol
))),
154 SYMBOL_LINKAGE_NAME (new_symbol
)) != 0
155 || TYPE_CODE (SYMBOL_TYPE (new_symbol
)) == TYPE_CODE_TYPEDEF
)
156 fprintf_filtered (stream
, " %s", SYMBOL_PRINT_NAME (new_symbol
));
157 fprintf_filtered (stream
, ";\n");
160 /* If TYPE is a derived type, then print out derivation information.
161 Print only the actual base classes of this type, not the base
162 classes of the base classes. I.e. for the derivation hierarchy:
165 class B : public A {int b; };
166 class C : public B {int c; };
168 Print the type of class C as:
174 Not as the following (like gdb used to), which is not legal C++
175 syntax for derived types and may be confused with the multiple
178 class C : public B : public A {
182 In general, gdb should try to print the types as closely as
183 possible to the form that they appear in the source code. */
186 cp_type_print_derivation_info (struct ui_file
*stream
,
188 const struct type_print_options
*flags
)
193 for (i
= 0; i
< TYPE_N_BASECLASSES (type
); i
++)
196 fputs_filtered (i
== 0 ? ": " : ", ", stream
);
197 fprintf_filtered (stream
, "%s%s ",
198 BASETYPE_VIA_PUBLIC (type
, i
)
199 ? "public" : (TYPE_FIELD_PROTECTED (type
, i
)
200 ? "protected" : "private"),
201 BASETYPE_VIA_VIRTUAL (type
, i
) ? " virtual" : "");
202 name
= type_name_no_tag (TYPE_BASECLASS (type
, i
));
204 print_name_maybe_canonical (name
, flags
, stream
);
206 fprintf_filtered (stream
, "(null)");
210 fputs_filtered (" ", stream
);
214 /* Print the C++ method arguments ARGS to the file STREAM. */
217 cp_type_print_method_args (struct type
*mtype
, const char *prefix
,
218 const char *varstring
, int staticp
,
219 struct ui_file
*stream
,
220 const struct type_print_options
*flags
)
222 struct field
*args
= TYPE_FIELDS (mtype
);
223 int nargs
= TYPE_NFIELDS (mtype
);
224 int varargs
= TYPE_VARARGS (mtype
);
227 fprintf_symbol_filtered (stream
, prefix
,
228 language_cplus
, DMGL_ANSI
);
229 fprintf_symbol_filtered (stream
, varstring
,
230 language_cplus
, DMGL_ANSI
);
231 fputs_filtered ("(", stream
);
233 /* Skip the class variable. */
239 c_print_type (args
[i
++].type
, "", stream
, 0, 0, flags
);
241 if (i
== nargs
&& varargs
)
242 fprintf_filtered (stream
, ", ...");
245 fprintf_filtered (stream
, ", ");
251 fprintf_filtered (stream
, "...");
252 else if (current_language
->la_language
== language_cplus
)
253 fprintf_filtered (stream
, "void");
255 fprintf_filtered (stream
, ")");
257 /* For non-static methods, read qualifiers from the type of
263 gdb_assert (nargs
> 0);
264 gdb_assert (TYPE_CODE (args
[0].type
) == TYPE_CODE_PTR
);
265 domain
= TYPE_TARGET_TYPE (args
[0].type
);
267 if (TYPE_CONST (domain
))
268 fprintf_filtered (stream
, " const");
270 if (TYPE_VOLATILE (domain
))
271 fprintf_filtered (stream
, " volatile");
273 if (TYPE_RESTRICT (domain
))
274 fprintf_filtered (stream
, " restrict");
279 /* Print any asterisks or open-parentheses needed before the
280 variable name (to describe its type).
282 On outermost call, pass 0 for PASSED_A_PTR.
283 On outermost call, SHOW > 0 means should ignore
284 any typename for TYPE and show its details.
285 SHOW is always zero on recursive calls.
287 NEED_POST_SPACE is non-zero when a space will be be needed
288 between a trailing qualifier and a field, variable, or function
292 c_type_print_varspec_prefix (struct type
*type
,
293 struct ui_file
*stream
,
294 int show
, int passed_a_ptr
,
296 const struct type_print_options
*flags
)
303 if (TYPE_NAME (type
) && show
<= 0)
308 switch (TYPE_CODE (type
))
311 c_type_print_varspec_prefix (TYPE_TARGET_TYPE (type
),
312 stream
, show
, 1, 1, flags
);
313 fprintf_filtered (stream
, "*");
314 c_type_print_modifier (type
, stream
, 1, need_post_space
);
317 case TYPE_CODE_MEMBERPTR
:
318 c_type_print_varspec_prefix (TYPE_TARGET_TYPE (type
),
319 stream
, show
, 0, 0, flags
);
320 name
= type_name_no_tag (TYPE_DOMAIN_TYPE (type
));
322 print_name_maybe_canonical (name
, flags
, stream
);
324 c_type_print_base (TYPE_DOMAIN_TYPE (type
),
325 stream
, -1, passed_a_ptr
, flags
);
326 fprintf_filtered (stream
, "::*");
329 case TYPE_CODE_METHODPTR
:
330 c_type_print_varspec_prefix (TYPE_TARGET_TYPE (type
),
331 stream
, show
, 0, 0, flags
);
332 fprintf_filtered (stream
, "(");
333 name
= type_name_no_tag (TYPE_DOMAIN_TYPE (type
));
335 print_name_maybe_canonical (name
, flags
, stream
);
337 c_type_print_base (TYPE_DOMAIN_TYPE (type
),
338 stream
, -1, passed_a_ptr
, flags
);
339 fprintf_filtered (stream
, "::*");
343 c_type_print_varspec_prefix (TYPE_TARGET_TYPE (type
),
344 stream
, show
, 1, 0, flags
);
345 fprintf_filtered (stream
, "&");
346 c_type_print_modifier (type
, stream
, 1, need_post_space
);
349 case TYPE_CODE_METHOD
:
351 c_type_print_varspec_prefix (TYPE_TARGET_TYPE (type
),
352 stream
, show
, 0, 0, flags
);
354 fprintf_filtered (stream
, "(");
357 case TYPE_CODE_ARRAY
:
358 c_type_print_varspec_prefix (TYPE_TARGET_TYPE (type
),
359 stream
, show
, 0, 0, flags
);
361 fprintf_filtered (stream
, "(");
364 case TYPE_CODE_TYPEDEF
:
365 c_type_print_varspec_prefix (TYPE_TARGET_TYPE (type
),
366 stream
, show
, passed_a_ptr
, 0, flags
);
369 case TYPE_CODE_UNDEF
:
370 case TYPE_CODE_STRUCT
:
371 case TYPE_CODE_UNION
:
376 case TYPE_CODE_ERROR
:
380 case TYPE_CODE_RANGE
:
381 case TYPE_CODE_STRING
:
382 case TYPE_CODE_COMPLEX
:
383 case TYPE_CODE_NAMESPACE
:
384 case TYPE_CODE_DECFLOAT
:
385 /* These types need no prefix. They are listed here so that
386 gcc -Wall will reveal any types that haven't been handled. */
389 error (_("type not handled in c_type_print_varspec_prefix()"));
394 /* Print out "const" and "volatile" attributes,
395 and address space id if present.
396 TYPE is a pointer to the type being printed out.
397 STREAM is the output destination.
398 NEED_PRE_SPACE = 1 indicates an initial white space is needed.
399 NEED_POST_SPACE = 1 indicates a final white space is needed. */
402 c_type_print_modifier (struct type
*type
, struct ui_file
*stream
,
403 int need_pre_space
, int need_post_space
)
405 int did_print_modifier
= 0;
406 const char *address_space_id
;
408 /* We don't print `const' qualifiers for references --- since all
409 operators affect the thing referenced, not the reference itself,
410 every reference is `const'. */
411 if (TYPE_CONST (type
)
412 && TYPE_CODE (type
) != TYPE_CODE_REF
)
415 fprintf_filtered (stream
, " ");
416 fprintf_filtered (stream
, "const");
417 did_print_modifier
= 1;
420 if (TYPE_VOLATILE (type
))
422 if (did_print_modifier
|| need_pre_space
)
423 fprintf_filtered (stream
, " ");
424 fprintf_filtered (stream
, "volatile");
425 did_print_modifier
= 1;
428 if (TYPE_RESTRICT (type
))
430 if (did_print_modifier
|| need_pre_space
)
431 fprintf_filtered (stream
, " ");
432 fprintf_filtered (stream
, "restrict");
433 did_print_modifier
= 1;
436 address_space_id
= address_space_int_to_name (get_type_arch (type
),
437 TYPE_INSTANCE_FLAGS (type
));
438 if (address_space_id
)
440 if (did_print_modifier
|| need_pre_space
)
441 fprintf_filtered (stream
, " ");
442 fprintf_filtered (stream
, "@%s", address_space_id
);
443 did_print_modifier
= 1;
446 if (did_print_modifier
&& need_post_space
)
447 fprintf_filtered (stream
, " ");
451 /* Print out the arguments of TYPE, which should have TYPE_CODE_METHOD
452 or TYPE_CODE_FUNC, to STREAM. Artificial arguments, such as "this"
453 in non-static methods, are displayed if LINKAGE_NAME is zero. If
454 LINKAGE_NAME is non-zero and LANGUAGE is language_cplus the topmost
455 parameter types get removed their possible const and volatile qualifiers to
456 match demangled linkage name parameters part of such function type.
457 LANGUAGE is the language in which TYPE was defined. This is a necessary
458 evil since this code is used by the C, C++, and Java backends. */
461 c_type_print_args (struct type
*type
, struct ui_file
*stream
,
462 int linkage_name
, enum language language
,
463 const struct type_print_options
*flags
)
468 fprintf_filtered (stream
, "(");
470 for (i
= 0; i
< TYPE_NFIELDS (type
); i
++)
472 struct type
*param_type
;
474 if (TYPE_FIELD_ARTIFICIAL (type
, i
) && linkage_name
)
479 fprintf_filtered (stream
, ", ");
483 param_type
= TYPE_FIELD_TYPE (type
, i
);
485 if (language
== language_cplus
&& linkage_name
)
487 /* C++ standard, 13.1 Overloadable declarations, point 3, item:
488 - Parameter declarations that differ only in the presence or
489 absence of const and/or volatile are equivalent.
491 And the const/volatile qualifiers are not present in the mangled
492 names as produced by GCC. */
494 param_type
= make_cv_type (0, 0, param_type
, NULL
);
497 if (language
== language_java
)
498 java_print_type (param_type
, "", stream
, -1, 0, flags
);
500 c_print_type (param_type
, "", stream
, -1, 0, flags
);
504 if (printed_any
&& TYPE_VARARGS (type
))
506 /* Print out a trailing ellipsis for varargs functions. Ignore
507 TYPE_VARARGS if the function has no named arguments; that
508 represents unprototyped (K&R style) C functions. */
509 if (printed_any
&& TYPE_VARARGS (type
))
511 fprintf_filtered (stream
, ", ");
513 fprintf_filtered (stream
, "...");
516 else if (!printed_any
517 && ((TYPE_PROTOTYPED (type
) && language
!= language_java
)
518 || language
== language_cplus
))
519 fprintf_filtered (stream
, "void");
521 fprintf_filtered (stream
, ")");
524 /* Return true iff the j'th overloading of the i'th method of TYPE
525 is a type conversion operator, like `operator int () { ... }'.
526 When listing a class's methods, we don't print the return type of
530 is_type_conversion_operator (struct type
*type
, int i
, int j
)
532 /* I think the whole idea of recognizing type conversion operators
533 by their name is pretty terrible. But I don't think our present
534 data structure gives us any other way to tell. If you know of
535 some other way, feel free to rewrite this function. */
536 const char *name
= TYPE_FN_FIELDLIST_NAME (type
, i
);
538 if (strncmp (name
, "operator", 8) != 0)
542 if (! strchr (" \t\f\n\r", *name
))
545 while (strchr (" \t\f\n\r", *name
))
548 if (!('a' <= *name
&& *name
<= 'z')
549 && !('A' <= *name
&& *name
<= 'Z')
551 /* If this doesn't look like the start of an identifier, then it
552 isn't a type conversion operator. */
554 else if (strncmp (name
, "new", 3) == 0)
556 else if (strncmp (name
, "delete", 6) == 0)
559 /* If it doesn't look like new or delete, it's a type conversion
563 /* Is that really the end of the name? */
564 if (('a' <= *name
&& *name
<= 'z')
565 || ('A' <= *name
&& *name
<= 'Z')
566 || ('0' <= *name
&& *name
<= '9')
568 /* No, so the identifier following "operator" must be a type name,
569 and this is a type conversion operator. */
572 /* That was indeed the end of the name, so it was `operator new' or
573 `operator delete', neither of which are type conversion
578 /* Given a C++ qualified identifier QID, strip off the qualifiers,
579 yielding the unqualified name. The return value is a pointer into
582 It's a pity we don't have this information in some more structured
583 form. Even the author of this function feels that writing little
584 parsers like this everywhere is stupid. */
587 remove_qualifiers (char *qid
)
589 int quoted
= 0; /* Zero if we're not in quotes;
590 '"' if we're in a double-quoted string;
591 '\'' if we're in a single-quoted string. */
592 int depth
= 0; /* Number of unclosed parens we've seen. */
593 char *parenstack
= (char *) alloca (strlen (qid
));
595 char *last
= 0; /* The character after the rightmost
596 `::' token we've seen so far. */
598 for (scan
= qid
; *scan
; scan
++)
604 else if (*scan
== '\\' && *(scan
+ 1))
607 else if (scan
[0] == ':' && scan
[1] == ':')
609 /* If we're inside parenthesis (i.e., an argument list) or
610 angle brackets (i.e., a list of template arguments), then
611 we don't record the position of this :: token, since it's
612 not relevant to the top-level structure we're trying to
620 else if (*scan
== '"' || *scan
== '\'')
622 else if (*scan
== '(')
623 parenstack
[depth
++] = ')';
624 else if (*scan
== '[')
625 parenstack
[depth
++] = ']';
626 /* We're going to treat <> as a pair of matching characters,
627 since we're more likely to see those in template id's than
628 real less-than characters. What a crock. */
629 else if (*scan
== '<')
630 parenstack
[depth
++] = '>';
631 else if (*scan
== ')' || *scan
== ']' || *scan
== '>')
633 if (depth
> 0 && parenstack
[depth
- 1] == *scan
)
637 /* We're going to do a little error recovery here. If
638 we don't find a match for *scan on the paren stack,
639 but there is something lower on the stack that does
640 match, we pop the stack to that point. */
643 for (i
= depth
- 1; i
>= 0; i
--)
644 if (parenstack
[i
] == *scan
)
656 /* We didn't find any :: tokens at the top level, so declare the
657 whole thing an unqualified identifier. */
661 /* Print any array sizes, function arguments or close parentheses
662 needed after the variable name (to describe its type).
663 Args work like c_type_print_varspec_prefix. */
666 c_type_print_varspec_suffix (struct type
*type
,
667 struct ui_file
*stream
,
668 int show
, int passed_a_ptr
,
670 const struct type_print_options
*flags
)
675 if (TYPE_NAME (type
) && show
<= 0)
680 switch (TYPE_CODE (type
))
682 case TYPE_CODE_ARRAY
:
684 LONGEST low_bound
, high_bound
;
685 int is_vector
= TYPE_VECTOR (type
);
688 fprintf_filtered (stream
, ")");
690 fprintf_filtered (stream
, (is_vector
?
691 " __attribute__ ((vector_size(" : "["));
692 if (get_array_bounds (type
, &low_bound
, &high_bound
))
693 fprintf_filtered (stream
, "%s",
694 plongest (high_bound
- low_bound
+ 1));
695 fprintf_filtered (stream
, (is_vector
? ")))" : "]"));
697 c_type_print_varspec_suffix (TYPE_TARGET_TYPE (type
), stream
,
702 case TYPE_CODE_MEMBERPTR
:
703 c_type_print_varspec_suffix (TYPE_TARGET_TYPE (type
), stream
,
707 case TYPE_CODE_METHODPTR
:
708 fprintf_filtered (stream
, ")");
709 c_type_print_varspec_suffix (TYPE_TARGET_TYPE (type
), stream
,
715 c_type_print_varspec_suffix (TYPE_TARGET_TYPE (type
), stream
,
719 case TYPE_CODE_METHOD
:
722 fprintf_filtered (stream
, ")");
724 c_type_print_args (type
, stream
, 0, current_language
->la_language
,
726 c_type_print_varspec_suffix (TYPE_TARGET_TYPE (type
), stream
,
727 show
, passed_a_ptr
, 0, flags
);
730 case TYPE_CODE_TYPEDEF
:
731 c_type_print_varspec_suffix (TYPE_TARGET_TYPE (type
), stream
,
732 show
, passed_a_ptr
, 0, flags
);
735 case TYPE_CODE_UNDEF
:
736 case TYPE_CODE_STRUCT
:
737 case TYPE_CODE_UNION
:
742 case TYPE_CODE_ERROR
:
746 case TYPE_CODE_RANGE
:
747 case TYPE_CODE_STRING
:
748 case TYPE_CODE_COMPLEX
:
749 case TYPE_CODE_NAMESPACE
:
750 case TYPE_CODE_DECFLOAT
:
751 /* These types do not need a suffix. They are listed so that
752 gcc -Wall will report types that may not have been
756 error (_("type not handled in c_type_print_varspec_suffix()"));
761 /* A helper for c_type_print_base that displays template
762 parameters and their bindings, if needed.
764 TABLE is the local bindings table to use. If NULL, no printing is
765 done. Note that, at this point, TABLE won't have any useful
766 information in it -- but it is also used as a flag to
767 print_name_maybe_canonical to activate searching the global typedef
770 TYPE is the type whose template arguments are being displayed.
772 STREAM is the stream on which to print. */
775 c_type_print_template_args (const struct type_print_options
*flags
,
776 struct type
*type
, struct ui_file
*stream
)
783 for (i
= 0; i
< TYPE_N_TEMPLATE_ARGUMENTS (type
); ++i
)
785 struct symbol
*sym
= TYPE_TEMPLATE_ARGUMENT (type
, i
);
787 if (SYMBOL_CLASS (sym
) != LOC_TYPEDEF
)
793 fprintf_filtered (stream
, _("[with %s = "),
794 SYMBOL_LINKAGE_NAME (sym
));
799 fputs_filtered (", ", stream
);
801 fprintf_filtered (stream
, "%s = ", SYMBOL_LINKAGE_NAME (sym
));
804 c_print_type (SYMBOL_TYPE (sym
), "", stream
, -1, 0, flags
);
808 fputs_filtered (_("] "), stream
);
811 /* Print the name of the type (or the ultimate pointer target,
812 function value or array element), or the description of a structure
815 SHOW positive means print details about the type (e.g. enum
816 values), and print structure elements passing SHOW - 1 for show.
818 SHOW negative means just print the type name or struct tag if there
819 is one. If there is no name, print something sensible but concise
822 SHOW zero means just print the type name or struct tag if there is
823 one. If there is no name, print something sensible but not as
824 concise like "struct {int x; int y;}".
826 LEVEL is the number of spaces to indent by.
827 We increase it for some recursive calls. */
830 c_type_print_base (struct type
*type
, struct ui_file
*stream
,
831 int show
, int level
, const struct type_print_options
*flags
)
837 s_none
, s_public
, s_private
, s_protected
840 int need_access_label
= 0;
847 fputs_filtered (_("<type unknown>"), stream
);
851 /* When SHOW is zero or less, and there is a valid type name, then
852 always just print the type name directly from the type. */
853 /* If we have "typedef struct foo {. . .} bar;" do we want to print
854 it as "struct foo" or as "bar"? Pick the latter, because C++
855 folk tend to expect things like "class5 *foo" rather than "struct
859 && TYPE_NAME (type
) != NULL
)
861 c_type_print_modifier (type
, stream
, 0, 1);
862 print_name_maybe_canonical (TYPE_NAME (type
), flags
, stream
);
866 CHECK_TYPEDEF (type
);
868 switch (TYPE_CODE (type
))
870 case TYPE_CODE_TYPEDEF
:
871 /* If we get here, the typedef doesn't have a name, and we
872 couldn't resolve TYPE_TARGET_TYPE. Not much we can do. */
873 gdb_assert (TYPE_NAME (type
) == NULL
);
874 gdb_assert (TYPE_TARGET_TYPE (type
) == NULL
);
875 fprintf_filtered (stream
, _("<unnamed typedef>"));
878 case TYPE_CODE_ARRAY
:
880 case TYPE_CODE_MEMBERPTR
:
883 case TYPE_CODE_METHOD
:
884 case TYPE_CODE_METHODPTR
:
885 c_type_print_base (TYPE_TARGET_TYPE (type
),
886 stream
, show
, level
, flags
);
889 case TYPE_CODE_STRUCT
:
890 case TYPE_CODE_UNION
:
892 struct type_print_options local_flags
= *flags
;
893 struct type_print_options semi_local_flags
= *flags
;
894 struct cleanup
*local_cleanups
= make_cleanup (null_cleanup
, NULL
);
896 local_flags
.local_typedefs
= NULL
;
897 semi_local_flags
.local_typedefs
= NULL
;
901 if (flags
->local_typedefs
)
902 local_flags
.local_typedefs
903 = copy_typedef_hash (flags
->local_typedefs
);
905 local_flags
.local_typedefs
= create_typedef_hash ();
907 make_cleanup_free_typedef_hash (local_flags
.local_typedefs
);
910 c_type_print_modifier (type
, stream
, 0, 1);
911 if (TYPE_CODE (type
) == TYPE_CODE_UNION
)
912 fprintf_filtered (stream
, "union ");
913 else if (TYPE_DECLARED_CLASS (type
))
914 fprintf_filtered (stream
, "class ");
916 fprintf_filtered (stream
, "struct ");
918 /* Print the tag if it exists. The HP aCC compiler emits a
919 spurious "{unnamed struct}"/"{unnamed union}"/"{unnamed
920 enum}" tag for unnamed struct/union/enum's, which we don't
922 if (TYPE_TAG_NAME (type
) != NULL
923 && strncmp (TYPE_TAG_NAME (type
), "{unnamed", 8))
925 /* When printing the tag name, we are still effectively
926 printing in the outer context, hence the use of FLAGS
928 print_name_maybe_canonical (TYPE_TAG_NAME (type
), flags
, stream
);
930 fputs_filtered (" ", stream
);
935 /* If we just printed a tag name, no need to print anything
937 if (TYPE_TAG_NAME (type
) == NULL
)
938 fprintf_filtered (stream
, "{...}");
940 else if (show
> 0 || TYPE_TAG_NAME (type
) == NULL
)
942 struct type
*basetype
;
945 c_type_print_template_args (&local_flags
, type
, stream
);
947 /* Add in template parameters when printing derivation info. */
948 add_template_parameters (local_flags
.local_typedefs
, type
);
949 cp_type_print_derivation_info (stream
, type
, &local_flags
);
951 /* This holds just the global typedefs and the template
953 semi_local_flags
.local_typedefs
954 = copy_typedef_hash (local_flags
.local_typedefs
);
955 if (semi_local_flags
.local_typedefs
)
956 make_cleanup_free_typedef_hash (semi_local_flags
.local_typedefs
);
958 /* Now add in the local typedefs. */
959 recursively_update_typedef_hash (local_flags
.local_typedefs
, type
);
961 fprintf_filtered (stream
, "{\n");
962 if (TYPE_NFIELDS (type
) == 0 && TYPE_NFN_FIELDS (type
) == 0
963 && TYPE_TYPEDEF_FIELD_COUNT (type
) == 0)
965 if (TYPE_STUB (type
))
966 fprintfi_filtered (level
+ 4, stream
,
967 _("<incomplete type>\n"));
969 fprintfi_filtered (level
+ 4, stream
,
970 _("<no data fields>\n"));
973 /* Start off with no specific section type, so we can print
974 one for the first field we find, and use that section type
975 thereafter until we find another type. */
977 section_type
= s_none
;
979 /* For a class, if all members are private, there's no need
980 for a "private:" label; similarly, for a struct or union
981 masquerading as a class, if all members are public, there's
982 no need for a "public:" label. */
984 if (TYPE_DECLARED_CLASS (type
))
987 len
= TYPE_NFIELDS (type
);
988 for (i
= TYPE_N_BASECLASSES (type
); i
< len
; i
++)
989 if (!TYPE_FIELD_PRIVATE (type
, i
))
991 need_access_label
= 1;
995 if (!need_access_label
)
997 len2
= TYPE_NFN_FIELDS (type
);
998 for (j
= 0; j
< len2
; j
++)
1000 len
= TYPE_FN_FIELDLIST_LENGTH (type
, j
);
1001 for (i
= 0; i
< len
; i
++)
1002 if (!TYPE_FN_FIELD_PRIVATE (TYPE_FN_FIELDLIST1 (type
,
1005 need_access_label
= 1;
1008 if (need_access_label
)
1016 len
= TYPE_NFIELDS (type
);
1017 for (i
= TYPE_N_BASECLASSES (type
); i
< len
; i
++)
1018 if (TYPE_FIELD_PRIVATE (type
, i
)
1019 || TYPE_FIELD_PROTECTED (type
, i
))
1021 need_access_label
= 1;
1025 if (!need_access_label
)
1027 len2
= TYPE_NFN_FIELDS (type
);
1028 for (j
= 0; j
< len2
; j
++)
1031 len
= TYPE_FN_FIELDLIST_LENGTH (type
, j
);
1032 for (i
= 0; i
< len
; i
++)
1033 if (TYPE_FN_FIELD_PROTECTED (TYPE_FN_FIELDLIST1 (type
,
1035 || TYPE_FN_FIELD_PRIVATE (TYPE_FN_FIELDLIST1 (type
,
1039 need_access_label
= 1;
1042 if (need_access_label
)
1048 /* If there is a base class for this type,
1049 do not print the field that it occupies. */
1051 len
= TYPE_NFIELDS (type
);
1052 vptr_fieldno
= get_vptr_fieldno (type
, &basetype
);
1053 for (i
= TYPE_N_BASECLASSES (type
); i
< len
; i
++)
1057 /* If we have a virtual table pointer, omit it. Even if
1058 virtual table pointers are not specifically marked in
1059 the debug info, they should be artificial. */
1060 if ((i
== vptr_fieldno
&& type
== basetype
)
1061 || TYPE_FIELD_ARTIFICIAL (type
, i
))
1064 if (need_access_label
)
1066 if (TYPE_FIELD_PROTECTED (type
, i
))
1068 if (section_type
!= s_protected
)
1070 section_type
= s_protected
;
1071 fprintfi_filtered (level
+ 2, stream
,
1075 else if (TYPE_FIELD_PRIVATE (type
, i
))
1077 if (section_type
!= s_private
)
1079 section_type
= s_private
;
1080 fprintfi_filtered (level
+ 2, stream
,
1086 if (section_type
!= s_public
)
1088 section_type
= s_public
;
1089 fprintfi_filtered (level
+ 2, stream
,
1095 print_spaces_filtered (level
+ 4, stream
);
1096 if (field_is_static (&TYPE_FIELD (type
, i
)))
1097 fprintf_filtered (stream
, "static ");
1098 c_print_type (TYPE_FIELD_TYPE (type
, i
),
1099 TYPE_FIELD_NAME (type
, i
),
1100 stream
, show
- 1, level
+ 4,
1102 if (!field_is_static (&TYPE_FIELD (type
, i
))
1103 && TYPE_FIELD_PACKED (type
, i
))
1105 /* It is a bitfield. This code does not attempt
1106 to look at the bitpos and reconstruct filler,
1107 unnamed fields. This would lead to misleading
1108 results if the compiler does not put out fields
1109 for such things (I don't know what it does). */
1110 fprintf_filtered (stream
, " : %d",
1111 TYPE_FIELD_BITSIZE (type
, i
));
1113 fprintf_filtered (stream
, ";\n");
1116 /* If there are both fields and methods, put a blank line
1117 between them. Make sure to count only method that we
1118 will display; artificial methods will be hidden. */
1119 len
= TYPE_NFN_FIELDS (type
);
1120 if (!flags
->print_methods
)
1123 for (i
= 0; i
< len
; i
++)
1125 struct fn_field
*f
= TYPE_FN_FIELDLIST1 (type
, i
);
1126 int len2
= TYPE_FN_FIELDLIST_LENGTH (type
, i
);
1129 for (j
= 0; j
< len2
; j
++)
1130 if (!TYPE_FN_FIELD_ARTIFICIAL (f
, j
))
1133 if (real_len
> 0 && section_type
!= s_none
)
1134 fprintf_filtered (stream
, "\n");
1136 /* C++: print out the methods. */
1137 for (i
= 0; i
< len
; i
++)
1139 struct fn_field
*f
= TYPE_FN_FIELDLIST1 (type
, i
);
1140 int j
, len2
= TYPE_FN_FIELDLIST_LENGTH (type
, i
);
1141 const char *method_name
= TYPE_FN_FIELDLIST_NAME (type
, i
);
1142 const char *name
= type_name_no_tag (type
);
1143 int is_constructor
= name
&& strcmp (method_name
,
1146 for (j
= 0; j
< len2
; j
++)
1148 const char *mangled_name
;
1149 char *demangled_name
;
1150 struct cleanup
*inner_cleanup
;
1151 const char *physname
= TYPE_FN_FIELD_PHYSNAME (f
, j
);
1152 int is_full_physname_constructor
=
1153 TYPE_FN_FIELD_CONSTRUCTOR (f
, j
)
1154 || is_constructor_name (physname
)
1155 || is_destructor_name (physname
)
1156 || method_name
[0] == '~';
1158 /* Do not print out artificial methods. */
1159 if (TYPE_FN_FIELD_ARTIFICIAL (f
, j
))
1162 inner_cleanup
= make_cleanup (null_cleanup
, NULL
);
1165 if (TYPE_FN_FIELD_PROTECTED (f
, j
))
1167 if (section_type
!= s_protected
)
1169 section_type
= s_protected
;
1170 fprintfi_filtered (level
+ 2, stream
,
1174 else if (TYPE_FN_FIELD_PRIVATE (f
, j
))
1176 if (section_type
!= s_private
)
1178 section_type
= s_private
;
1179 fprintfi_filtered (level
+ 2, stream
,
1185 if (section_type
!= s_public
)
1187 section_type
= s_public
;
1188 fprintfi_filtered (level
+ 2, stream
,
1193 print_spaces_filtered (level
+ 4, stream
);
1194 if (TYPE_FN_FIELD_VIRTUAL_P (f
, j
))
1195 fprintf_filtered (stream
, "virtual ");
1196 else if (TYPE_FN_FIELD_STATIC_P (f
, j
))
1197 fprintf_filtered (stream
, "static ");
1198 if (TYPE_TARGET_TYPE (TYPE_FN_FIELD_TYPE (f
, j
)) == 0)
1200 /* Keep GDB from crashing here. */
1201 fprintf_filtered (stream
,
1202 _("<undefined type> %s;\n"),
1203 TYPE_FN_FIELD_PHYSNAME (f
, j
));
1206 else if (!is_constructor
/* Constructors don't
1209 && !is_full_physname_constructor
/* " " */
1210 && !is_type_conversion_operator (type
, i
, j
))
1212 c_print_type (TYPE_TARGET_TYPE (TYPE_FN_FIELD_TYPE (f
, j
)),
1215 fputs_filtered (" ", stream
);
1217 if (TYPE_FN_FIELD_STUB (f
, j
))
1221 /* Build something we can demangle. */
1222 tem
= gdb_mangle_name (type
, i
, j
);
1223 make_cleanup (xfree
, tem
);
1227 mangled_name
= TYPE_FN_FIELD_PHYSNAME (f
, j
);
1230 gdb_demangle (mangled_name
,
1231 DMGL_ANSI
| DMGL_PARAMS
);
1232 if (demangled_name
== NULL
)
1234 /* In some cases (for instance with the HP
1235 demangling), if a function has more than 10
1236 arguments, the demangling will fail.
1237 Let's try to reconstruct the function
1238 signature from the symbol information. */
1239 if (!TYPE_FN_FIELD_STUB (f
, j
))
1241 int staticp
= TYPE_FN_FIELD_STATIC_P (f
, j
);
1242 struct type
*mtype
= TYPE_FN_FIELD_TYPE (f
, j
);
1244 cp_type_print_method_args (mtype
,
1248 stream
, &local_flags
);
1251 fprintf_filtered (stream
,
1252 _("<badly mangled name '%s'>"),
1258 char *demangled_no_class
1259 = remove_qualifiers (demangled_name
);
1261 /* Get rid of the `static' appended by the
1263 p
= strstr (demangled_no_class
, " static");
1266 int length
= p
- demangled_no_class
;
1267 char *demangled_no_static
;
1270 = (char *) xmalloc (length
+ 1);
1271 strncpy (demangled_no_static
,
1272 demangled_no_class
, length
);
1273 *(demangled_no_static
+ length
) = '\0';
1274 fputs_filtered (demangled_no_static
, stream
);
1275 xfree (demangled_no_static
);
1278 fputs_filtered (demangled_no_class
, stream
);
1279 xfree (demangled_name
);
1282 do_cleanups (inner_cleanup
);
1284 fprintf_filtered (stream
, ";\n");
1288 /* Print typedefs defined in this class. */
1290 if (TYPE_TYPEDEF_FIELD_COUNT (type
) != 0 && flags
->print_typedefs
)
1292 if (TYPE_NFIELDS (type
) != 0 || TYPE_NFN_FIELDS (type
) != 0)
1293 fprintf_filtered (stream
, "\n");
1295 for (i
= 0; i
< TYPE_TYPEDEF_FIELD_COUNT (type
); i
++)
1297 struct type
*target
= TYPE_TYPEDEF_FIELD_TYPE (type
, i
);
1299 /* Dereference the typedef declaration itself. */
1300 gdb_assert (TYPE_CODE (target
) == TYPE_CODE_TYPEDEF
);
1301 target
= TYPE_TARGET_TYPE (target
);
1303 print_spaces_filtered (level
+ 4, stream
);
1304 fprintf_filtered (stream
, "typedef ");
1306 /* We want to print typedefs with substitutions
1307 from the template parameters or globally-known
1308 typedefs but not local typedefs. */
1309 c_print_type (target
,
1310 TYPE_TYPEDEF_FIELD_NAME (type
, i
),
1311 stream
, show
- 1, level
+ 4,
1313 fprintf_filtered (stream
, ";\n");
1317 fprintfi_filtered (level
, stream
, "}");
1320 do_cleanups (local_cleanups
);
1324 case TYPE_CODE_ENUM
:
1325 c_type_print_modifier (type
, stream
, 0, 1);
1326 fprintf_filtered (stream
, "enum ");
1327 /* Print the tag name if it exists.
1328 The aCC compiler emits a spurious
1329 "{unnamed struct}"/"{unnamed union}"/"{unnamed enum}"
1330 tag for unnamed struct/union/enum's, which we don't
1332 if (TYPE_TAG_NAME (type
) != NULL
1333 && strncmp (TYPE_TAG_NAME (type
), "{unnamed", 8))
1335 print_name_maybe_canonical (TYPE_TAG_NAME (type
), flags
, stream
);
1337 fputs_filtered (" ", stream
);
1343 /* If we just printed a tag name, no need to print anything
1345 if (TYPE_TAG_NAME (type
) == NULL
)
1346 fprintf_filtered (stream
, "{...}");
1348 else if (show
> 0 || TYPE_TAG_NAME (type
) == NULL
)
1350 LONGEST lastval
= 0;
1352 fprintf_filtered (stream
, "{");
1353 len
= TYPE_NFIELDS (type
);
1354 for (i
= 0; i
< len
; i
++)
1358 fprintf_filtered (stream
, ", ");
1360 fputs_filtered (TYPE_FIELD_NAME (type
, i
), stream
);
1361 if (lastval
!= TYPE_FIELD_ENUMVAL (type
, i
))
1363 fprintf_filtered (stream
, " = %s",
1364 plongest (TYPE_FIELD_ENUMVAL (type
, i
)));
1365 lastval
= TYPE_FIELD_ENUMVAL (type
, i
);
1369 fprintf_filtered (stream
, "}");
1373 case TYPE_CODE_VOID
:
1374 fprintf_filtered (stream
, "void");
1377 case TYPE_CODE_UNDEF
:
1378 fprintf_filtered (stream
, _("struct <unknown>"));
1381 case TYPE_CODE_ERROR
:
1382 fprintf_filtered (stream
, "%s", TYPE_ERROR_NAME (type
));
1385 case TYPE_CODE_RANGE
:
1386 /* This should not occur. */
1387 fprintf_filtered (stream
, _("<range type>"));
1390 case TYPE_CODE_NAMESPACE
:
1391 fputs_filtered ("namespace ", stream
);
1392 fputs_filtered (TYPE_TAG_NAME (type
), stream
);
1396 /* Handle types not explicitly handled by the other cases, such
1397 as fundamental types. For these, just print whatever the
1398 type name is, as recorded in the type itself. If there is no
1399 type name, then complain. */
1400 if (TYPE_NAME (type
) != NULL
)
1402 c_type_print_modifier (type
, stream
, 0, 1);
1403 print_name_maybe_canonical (TYPE_NAME (type
), flags
, stream
);
1407 /* At least for dump_symtab, it is important that this not
1409 fprintf_filtered (stream
, _("<invalid type code %d>"),