s390-vregs.exp: Avoid compile errors with older GCCs and on 31-bit targets
[binutils-gdb.git] / gdb / c-typeprint.c
blob421b72014865e17521b142f907aeb12a88546a79
1 /* Support for printing C and C++ types for GDB, the GNU debugger.
2 Copyright (C) 1986-2015 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/>. */
19 #include "defs.h"
20 #include "gdb_obstack.h"
21 #include "bfd.h" /* Binary File Description. */
22 #include "symtab.h"
23 #include "gdbtypes.h"
24 #include "expression.h"
25 #include "value.h"
26 #include "gdbcore.h"
27 #include "target.h"
28 #include "language.h"
29 #include "demangle.h"
30 #include "c-lang.h"
31 #include "typeprint.h"
32 #include "cp-abi.h"
33 #include "jv-lang.h"
34 #include "cp-support.h"
36 static void c_type_print_varspec_prefix (struct type *,
37 struct ui_file *,
38 int, int, int,
39 const struct type_print_options *);
41 /* Print "const", "volatile", or address space modifiers. */
42 static void c_type_print_modifier (struct type *,
43 struct ui_file *,
44 int, int);
47 /* A callback function for cp_canonicalize_string_full that uses
48 find_typedef_in_hash. */
50 static const char *
51 find_typedef_for_canonicalize (struct type *t, void *data)
53 return find_typedef_in_hash (data, t);
56 /* Print NAME on STREAM. If the 'raw' field of FLAGS is not set,
57 canonicalize NAME using the local typedefs first. */
59 static void
60 print_name_maybe_canonical (const char *name,
61 const struct type_print_options *flags,
62 struct ui_file *stream)
64 char *s = NULL;
66 if (!flags->raw)
67 s = cp_canonicalize_string_full (name,
68 find_typedef_for_canonicalize,
69 (void *) flags);
71 fputs_filtered (s ? s : name, stream);
72 xfree (s);
77 /* LEVEL is the depth to indent lines by. */
79 void
80 c_print_type (struct type *type,
81 const char *varstring,
82 struct ui_file *stream,
83 int show, int level,
84 const struct type_print_options *flags)
86 enum type_code code;
87 int demangled_args;
88 int need_post_space;
89 const char *local_name;
91 if (show > 0)
92 CHECK_TYPEDEF (type);
94 local_name = find_typedef_in_hash (flags, type);
95 if (local_name != NULL)
97 fputs_filtered (local_name, stream);
98 if (varstring != NULL && *varstring != '\0')
99 fputs_filtered (" ", stream);
101 else
103 c_type_print_base (type, stream, show, level, flags);
104 code = TYPE_CODE (type);
105 if ((varstring != NULL && *varstring != '\0')
106 /* Need a space if going to print stars or brackets;
107 but not if we will print just a type name. */
108 || ((show > 0 || TYPE_NAME (type) == 0)
109 && (code == TYPE_CODE_PTR || code == TYPE_CODE_FUNC
110 || code == TYPE_CODE_METHOD
111 || (code == TYPE_CODE_ARRAY
112 && !TYPE_VECTOR (type))
113 || code == TYPE_CODE_MEMBERPTR
114 || code == TYPE_CODE_METHODPTR
115 || code == TYPE_CODE_REF)))
116 fputs_filtered (" ", stream);
117 need_post_space = (varstring != NULL && strcmp (varstring, "") != 0);
118 c_type_print_varspec_prefix (type, stream, show, 0, need_post_space,
119 flags);
122 if (varstring != NULL)
124 fputs_filtered (varstring, stream);
126 /* For demangled function names, we have the arglist as part of
127 the name, so don't print an additional pair of ()'s. */
128 if (local_name == NULL)
130 demangled_args = strchr (varstring, '(') != NULL;
131 c_type_print_varspec_suffix (type, stream, show,
132 0, demangled_args,
133 flags);
138 /* Print a typedef using C syntax. TYPE is the underlying type.
139 NEW_SYMBOL is the symbol naming the type. STREAM is the stream on
140 which to print. */
142 void
143 c_print_typedef (struct type *type,
144 struct symbol *new_symbol,
145 struct ui_file *stream)
147 CHECK_TYPEDEF (type);
148 fprintf_filtered (stream, "typedef ");
149 type_print (type, "", stream, 0);
150 if (TYPE_NAME ((SYMBOL_TYPE (new_symbol))) == 0
151 || strcmp (TYPE_NAME ((SYMBOL_TYPE (new_symbol))),
152 SYMBOL_LINKAGE_NAME (new_symbol)) != 0
153 || TYPE_CODE (SYMBOL_TYPE (new_symbol)) == TYPE_CODE_TYPEDEF)
154 fprintf_filtered (stream, " %s", SYMBOL_PRINT_NAME (new_symbol));
155 fprintf_filtered (stream, ";\n");
158 /* If TYPE is a derived type, then print out derivation information.
159 Print only the actual base classes of this type, not the base
160 classes of the base classes. I.e. for the derivation hierarchy:
162 class A { int a; };
163 class B : public A {int b; };
164 class C : public B {int c; };
166 Print the type of class C as:
168 class C : public B {
169 int c;
172 Not as the following (like gdb used to), which is not legal C++
173 syntax for derived types and may be confused with the multiple
174 inheritance form:
176 class C : public B : public A {
177 int c;
180 In general, gdb should try to print the types as closely as
181 possible to the form that they appear in the source code. */
183 static void
184 cp_type_print_derivation_info (struct ui_file *stream,
185 struct type *type,
186 const struct type_print_options *flags)
188 const char *name;
189 int i;
191 for (i = 0; i < TYPE_N_BASECLASSES (type); i++)
193 wrap_here (" ");
194 fputs_filtered (i == 0 ? ": " : ", ", stream);
195 fprintf_filtered (stream, "%s%s ",
196 BASETYPE_VIA_PUBLIC (type, i)
197 ? "public" : (TYPE_FIELD_PROTECTED (type, i)
198 ? "protected" : "private"),
199 BASETYPE_VIA_VIRTUAL (type, i) ? " virtual" : "");
200 name = type_name_no_tag (TYPE_BASECLASS (type, i));
201 if (name)
202 print_name_maybe_canonical (name, flags, stream);
203 else
204 fprintf_filtered (stream, "(null)");
206 if (i > 0)
208 fputs_filtered (" ", stream);
212 /* Print the C++ method arguments ARGS to the file STREAM. */
214 static void
215 cp_type_print_method_args (struct type *mtype, const char *prefix,
216 const char *varstring, int staticp,
217 struct ui_file *stream,
218 const struct type_print_options *flags)
220 struct field *args = TYPE_FIELDS (mtype);
221 int nargs = TYPE_NFIELDS (mtype);
222 int varargs = TYPE_VARARGS (mtype);
223 int i;
225 fprintf_symbol_filtered (stream, prefix,
226 language_cplus, DMGL_ANSI);
227 fprintf_symbol_filtered (stream, varstring,
228 language_cplus, DMGL_ANSI);
229 fputs_filtered ("(", stream);
231 /* Skip the class variable. */
232 i = staticp ? 0 : 1;
233 if (nargs > i)
235 while (i < nargs)
237 c_print_type (args[i++].type, "", stream, 0, 0, flags);
239 if (i == nargs && varargs)
240 fprintf_filtered (stream, ", ...");
241 else if (i < nargs)
243 fprintf_filtered (stream, ", ");
244 wrap_here (" ");
248 else if (varargs)
249 fprintf_filtered (stream, "...");
250 else if (current_language->la_language == language_cplus)
251 fprintf_filtered (stream, "void");
253 fprintf_filtered (stream, ")");
255 /* For non-static methods, read qualifiers from the type of
256 THIS. */
257 if (!staticp)
259 struct type *domain;
261 gdb_assert (nargs > 0);
262 gdb_assert (TYPE_CODE (args[0].type) == TYPE_CODE_PTR);
263 domain = TYPE_TARGET_TYPE (args[0].type);
265 if (TYPE_CONST (domain))
266 fprintf_filtered (stream, " const");
268 if (TYPE_VOLATILE (domain))
269 fprintf_filtered (stream, " volatile");
271 if (TYPE_RESTRICT (domain))
272 fprintf_filtered (stream, " restrict");
274 if (TYPE_ATOMIC (domain))
275 fprintf_filtered (stream, " _Atomic");
280 /* Print any asterisks or open-parentheses needed before the
281 variable name (to describe its type).
283 On outermost call, pass 0 for PASSED_A_PTR.
284 On outermost call, SHOW > 0 means should ignore
285 any typename for TYPE and show its details.
286 SHOW is always zero on recursive calls.
288 NEED_POST_SPACE is non-zero when a space will be be needed
289 between a trailing qualifier and a field, variable, or function
290 name. */
292 static void
293 c_type_print_varspec_prefix (struct type *type,
294 struct ui_file *stream,
295 int show, int passed_a_ptr,
296 int need_post_space,
297 const struct type_print_options *flags)
299 const char *name;
301 if (type == 0)
302 return;
304 if (TYPE_NAME (type) && show <= 0)
305 return;
307 QUIT;
309 switch (TYPE_CODE (type))
311 case TYPE_CODE_PTR:
312 c_type_print_varspec_prefix (TYPE_TARGET_TYPE (type),
313 stream, show, 1, 1, flags);
314 fprintf_filtered (stream, "*");
315 c_type_print_modifier (type, stream, 1, need_post_space);
316 break;
318 case TYPE_CODE_MEMBERPTR:
319 c_type_print_varspec_prefix (TYPE_TARGET_TYPE (type),
320 stream, show, 0, 0, flags);
321 name = type_name_no_tag (TYPE_SELF_TYPE (type));
322 if (name)
323 print_name_maybe_canonical (name, flags, stream);
324 else
325 c_type_print_base (TYPE_SELF_TYPE (type),
326 stream, -1, passed_a_ptr, flags);
327 fprintf_filtered (stream, "::*");
328 break;
330 case TYPE_CODE_METHODPTR:
331 c_type_print_varspec_prefix (TYPE_TARGET_TYPE (type),
332 stream, show, 0, 0, flags);
333 fprintf_filtered (stream, "(");
334 name = type_name_no_tag (TYPE_SELF_TYPE (type));
335 if (name)
336 print_name_maybe_canonical (name, flags, stream);
337 else
338 c_type_print_base (TYPE_SELF_TYPE (type),
339 stream, -1, passed_a_ptr, flags);
340 fprintf_filtered (stream, "::*");
341 break;
343 case TYPE_CODE_REF:
344 c_type_print_varspec_prefix (TYPE_TARGET_TYPE (type),
345 stream, show, 1, 0, flags);
346 fprintf_filtered (stream, "&");
347 c_type_print_modifier (type, stream, 1, need_post_space);
348 break;
350 case TYPE_CODE_METHOD:
351 case TYPE_CODE_FUNC:
352 c_type_print_varspec_prefix (TYPE_TARGET_TYPE (type),
353 stream, show, 0, 0, flags);
354 if (passed_a_ptr)
355 fprintf_filtered (stream, "(");
356 break;
358 case TYPE_CODE_ARRAY:
359 c_type_print_varspec_prefix (TYPE_TARGET_TYPE (type),
360 stream, show, 0, 0, flags);
361 if (passed_a_ptr)
362 fprintf_filtered (stream, "(");
363 break;
365 case TYPE_CODE_TYPEDEF:
366 c_type_print_varspec_prefix (TYPE_TARGET_TYPE (type),
367 stream, show, passed_a_ptr, 0, flags);
368 break;
370 case TYPE_CODE_UNDEF:
371 case TYPE_CODE_STRUCT:
372 case TYPE_CODE_UNION:
373 case TYPE_CODE_ENUM:
374 case TYPE_CODE_INT:
375 case TYPE_CODE_FLT:
376 case TYPE_CODE_VOID:
377 case TYPE_CODE_ERROR:
378 case TYPE_CODE_CHAR:
379 case TYPE_CODE_BOOL:
380 case TYPE_CODE_SET:
381 case TYPE_CODE_RANGE:
382 case TYPE_CODE_STRING:
383 case TYPE_CODE_COMPLEX:
384 case TYPE_CODE_NAMESPACE:
385 case TYPE_CODE_DECFLOAT:
386 /* These types need no prefix. They are listed here so that
387 gcc -Wall will reveal any types that haven't been handled. */
388 break;
389 default:
390 error (_("type not handled in c_type_print_varspec_prefix()"));
391 break;
395 /* Print out "const" and "volatile" attributes,
396 and address space id if present.
397 TYPE is a pointer to the type being printed out.
398 STREAM is the output destination.
399 NEED_PRE_SPACE = 1 indicates an initial white space is needed.
400 NEED_POST_SPACE = 1 indicates a final white space is needed. */
402 static void
403 c_type_print_modifier (struct type *type, struct ui_file *stream,
404 int need_pre_space, int need_post_space)
406 int did_print_modifier = 0;
407 const char *address_space_id;
409 /* We don't print `const' qualifiers for references --- since all
410 operators affect the thing referenced, not the reference itself,
411 every reference is `const'. */
412 if (TYPE_CONST (type)
413 && TYPE_CODE (type) != TYPE_CODE_REF)
415 if (need_pre_space)
416 fprintf_filtered (stream, " ");
417 fprintf_filtered (stream, "const");
418 did_print_modifier = 1;
421 if (TYPE_VOLATILE (type))
423 if (did_print_modifier || need_pre_space)
424 fprintf_filtered (stream, " ");
425 fprintf_filtered (stream, "volatile");
426 did_print_modifier = 1;
429 if (TYPE_RESTRICT (type))
431 if (did_print_modifier || need_pre_space)
432 fprintf_filtered (stream, " ");
433 fprintf_filtered (stream, "restrict");
434 did_print_modifier = 1;
437 if (TYPE_ATOMIC (type))
439 if (did_print_modifier || need_pre_space)
440 fprintf_filtered (stream, " ");
441 fprintf_filtered (stream, "_Atomic");
442 did_print_modifier = 1;
445 address_space_id = address_space_int_to_name (get_type_arch (type),
446 TYPE_INSTANCE_FLAGS (type));
447 if (address_space_id)
449 if (did_print_modifier || need_pre_space)
450 fprintf_filtered (stream, " ");
451 fprintf_filtered (stream, "@%s", address_space_id);
452 did_print_modifier = 1;
455 if (did_print_modifier && need_post_space)
456 fprintf_filtered (stream, " ");
460 /* Print out the arguments of TYPE, which should have TYPE_CODE_METHOD
461 or TYPE_CODE_FUNC, to STREAM. Artificial arguments, such as "this"
462 in non-static methods, are displayed if LINKAGE_NAME is zero. If
463 LINKAGE_NAME is non-zero and LANGUAGE is language_cplus the topmost
464 parameter types get removed their possible const and volatile qualifiers to
465 match demangled linkage name parameters part of such function type.
466 LANGUAGE is the language in which TYPE was defined. This is a necessary
467 evil since this code is used by the C, C++, and Java backends. */
469 void
470 c_type_print_args (struct type *type, struct ui_file *stream,
471 int linkage_name, enum language language,
472 const struct type_print_options *flags)
474 int i;
475 int printed_any = 0;
477 fprintf_filtered (stream, "(");
479 for (i = 0; i < TYPE_NFIELDS (type); i++)
481 struct type *param_type;
483 if (TYPE_FIELD_ARTIFICIAL (type, i) && linkage_name)
484 continue;
486 if (printed_any)
488 fprintf_filtered (stream, ", ");
489 wrap_here (" ");
492 param_type = TYPE_FIELD_TYPE (type, i);
494 if (language == language_cplus && linkage_name)
496 /* C++ standard, 13.1 Overloadable declarations, point 3, item:
497 - Parameter declarations that differ only in the presence or
498 absence of const and/or volatile are equivalent.
500 And the const/volatile qualifiers are not present in the mangled
501 names as produced by GCC. */
503 param_type = make_cv_type (0, 0, param_type, NULL);
506 if (language == language_java)
507 java_print_type (param_type, "", stream, -1, 0, flags);
508 else
509 c_print_type (param_type, "", stream, -1, 0, flags);
510 printed_any = 1;
513 if (printed_any && TYPE_VARARGS (type))
515 /* Print out a trailing ellipsis for varargs functions. Ignore
516 TYPE_VARARGS if the function has no named arguments; that
517 represents unprototyped (K&R style) C functions. */
518 if (printed_any && TYPE_VARARGS (type))
520 fprintf_filtered (stream, ", ");
521 wrap_here (" ");
522 fprintf_filtered (stream, "...");
525 else if (!printed_any
526 && ((TYPE_PROTOTYPED (type) && language != language_java)
527 || language == language_cplus))
528 fprintf_filtered (stream, "void");
530 fprintf_filtered (stream, ")");
533 /* Return true iff the j'th overloading of the i'th method of TYPE
534 is a type conversion operator, like `operator int () { ... }'.
535 When listing a class's methods, we don't print the return type of
536 such operators. */
538 static int
539 is_type_conversion_operator (struct type *type, int i, int j)
541 /* I think the whole idea of recognizing type conversion operators
542 by their name is pretty terrible. But I don't think our present
543 data structure gives us any other way to tell. If you know of
544 some other way, feel free to rewrite this function. */
545 const char *name = TYPE_FN_FIELDLIST_NAME (type, i);
547 if (!startswith (name, "operator"))
548 return 0;
550 name += 8;
551 if (! strchr (" \t\f\n\r", *name))
552 return 0;
554 while (strchr (" \t\f\n\r", *name))
555 name++;
557 if (!('a' <= *name && *name <= 'z')
558 && !('A' <= *name && *name <= 'Z')
559 && *name != '_')
560 /* If this doesn't look like the start of an identifier, then it
561 isn't a type conversion operator. */
562 return 0;
563 else if (startswith (name, "new"))
564 name += 3;
565 else if (startswith (name, "delete"))
566 name += 6;
567 else
568 /* If it doesn't look like new or delete, it's a type conversion
569 operator. */
570 return 1;
572 /* Is that really the end of the name? */
573 if (('a' <= *name && *name <= 'z')
574 || ('A' <= *name && *name <= 'Z')
575 || ('0' <= *name && *name <= '9')
576 || *name == '_')
577 /* No, so the identifier following "operator" must be a type name,
578 and this is a type conversion operator. */
579 return 1;
581 /* That was indeed the end of the name, so it was `operator new' or
582 `operator delete', neither of which are type conversion
583 operators. */
584 return 0;
587 /* Given a C++ qualified identifier QID, strip off the qualifiers,
588 yielding the unqualified name. The return value is a pointer into
589 the original string.
591 It's a pity we don't have this information in some more structured
592 form. Even the author of this function feels that writing little
593 parsers like this everywhere is stupid. */
595 static char *
596 remove_qualifiers (char *qid)
598 int quoted = 0; /* Zero if we're not in quotes;
599 '"' if we're in a double-quoted string;
600 '\'' if we're in a single-quoted string. */
601 int depth = 0; /* Number of unclosed parens we've seen. */
602 char *parenstack = (char *) alloca (strlen (qid));
603 char *scan;
604 char *last = 0; /* The character after the rightmost
605 `::' token we've seen so far. */
607 for (scan = qid; *scan; scan++)
609 if (quoted)
611 if (*scan == quoted)
612 quoted = 0;
613 else if (*scan == '\\' && *(scan + 1))
614 scan++;
616 else if (scan[0] == ':' && scan[1] == ':')
618 /* If we're inside parenthesis (i.e., an argument list) or
619 angle brackets (i.e., a list of template arguments), then
620 we don't record the position of this :: token, since it's
621 not relevant to the top-level structure we're trying to
622 operate on. */
623 if (depth == 0)
625 last = scan + 2;
626 scan++;
629 else if (*scan == '"' || *scan == '\'')
630 quoted = *scan;
631 else if (*scan == '(')
632 parenstack[depth++] = ')';
633 else if (*scan == '[')
634 parenstack[depth++] = ']';
635 /* We're going to treat <> as a pair of matching characters,
636 since we're more likely to see those in template id's than
637 real less-than characters. What a crock. */
638 else if (*scan == '<')
639 parenstack[depth++] = '>';
640 else if (*scan == ')' || *scan == ']' || *scan == '>')
642 if (depth > 0 && parenstack[depth - 1] == *scan)
643 depth--;
644 else
646 /* We're going to do a little error recovery here. If
647 we don't find a match for *scan on the paren stack,
648 but there is something lower on the stack that does
649 match, we pop the stack to that point. */
650 int i;
652 for (i = depth - 1; i >= 0; i--)
653 if (parenstack[i] == *scan)
655 depth = i;
656 break;
662 if (last)
663 return last;
664 else
665 /* We didn't find any :: tokens at the top level, so declare the
666 whole thing an unqualified identifier. */
667 return qid;
670 /* Print any array sizes, function arguments or close parentheses
671 needed after the variable name (to describe its type).
672 Args work like c_type_print_varspec_prefix. */
674 void
675 c_type_print_varspec_suffix (struct type *type,
676 struct ui_file *stream,
677 int show, int passed_a_ptr,
678 int demangled_args,
679 const struct type_print_options *flags)
681 if (type == 0)
682 return;
684 if (TYPE_NAME (type) && show <= 0)
685 return;
687 QUIT;
689 switch (TYPE_CODE (type))
691 case TYPE_CODE_ARRAY:
693 LONGEST low_bound, high_bound;
694 int is_vector = TYPE_VECTOR (type);
696 if (passed_a_ptr)
697 fprintf_filtered (stream, ")");
699 fprintf_filtered (stream, (is_vector ?
700 " __attribute__ ((vector_size(" : "["));
701 /* Bounds are not yet resolved, print a bounds placeholder instead. */
702 if (TYPE_HIGH_BOUND_KIND (TYPE_INDEX_TYPE (type)) == PROP_LOCEXPR
703 || TYPE_HIGH_BOUND_KIND (TYPE_INDEX_TYPE (type)) == PROP_LOCLIST)
704 fprintf_filtered (stream, "variable length");
705 else if (get_array_bounds (type, &low_bound, &high_bound))
706 fprintf_filtered (stream, "%s",
707 plongest (high_bound - low_bound + 1));
708 fprintf_filtered (stream, (is_vector ? ")))" : "]"));
710 c_type_print_varspec_suffix (TYPE_TARGET_TYPE (type), stream,
711 show, 0, 0, flags);
713 break;
715 case TYPE_CODE_MEMBERPTR:
716 c_type_print_varspec_suffix (TYPE_TARGET_TYPE (type), stream,
717 show, 0, 0, flags);
718 break;
720 case TYPE_CODE_METHODPTR:
721 fprintf_filtered (stream, ")");
722 c_type_print_varspec_suffix (TYPE_TARGET_TYPE (type), stream,
723 show, 0, 0, flags);
724 break;
726 case TYPE_CODE_PTR:
727 case TYPE_CODE_REF:
728 c_type_print_varspec_suffix (TYPE_TARGET_TYPE (type), stream,
729 show, 1, 0, flags);
730 break;
732 case TYPE_CODE_METHOD:
733 case TYPE_CODE_FUNC:
734 if (passed_a_ptr)
735 fprintf_filtered (stream, ")");
736 if (!demangled_args)
737 c_type_print_args (type, stream, 0, current_language->la_language,
738 flags);
739 c_type_print_varspec_suffix (TYPE_TARGET_TYPE (type), stream,
740 show, passed_a_ptr, 0, flags);
741 break;
743 case TYPE_CODE_TYPEDEF:
744 c_type_print_varspec_suffix (TYPE_TARGET_TYPE (type), stream,
745 show, passed_a_ptr, 0, flags);
746 break;
748 case TYPE_CODE_UNDEF:
749 case TYPE_CODE_STRUCT:
750 case TYPE_CODE_UNION:
751 case TYPE_CODE_ENUM:
752 case TYPE_CODE_INT:
753 case TYPE_CODE_FLT:
754 case TYPE_CODE_VOID:
755 case TYPE_CODE_ERROR:
756 case TYPE_CODE_CHAR:
757 case TYPE_CODE_BOOL:
758 case TYPE_CODE_SET:
759 case TYPE_CODE_RANGE:
760 case TYPE_CODE_STRING:
761 case TYPE_CODE_COMPLEX:
762 case TYPE_CODE_NAMESPACE:
763 case TYPE_CODE_DECFLOAT:
764 /* These types do not need a suffix. They are listed so that
765 gcc -Wall will report types that may not have been
766 considered. */
767 break;
768 default:
769 error (_("type not handled in c_type_print_varspec_suffix()"));
770 break;
774 /* A helper for c_type_print_base that displays template
775 parameters and their bindings, if needed.
777 TABLE is the local bindings table to use. If NULL, no printing is
778 done. Note that, at this point, TABLE won't have any useful
779 information in it -- but it is also used as a flag to
780 print_name_maybe_canonical to activate searching the global typedef
781 table.
783 TYPE is the type whose template arguments are being displayed.
785 STREAM is the stream on which to print. */
787 static void
788 c_type_print_template_args (const struct type_print_options *flags,
789 struct type *type, struct ui_file *stream)
791 int first = 1, i;
793 if (flags->raw)
794 return;
796 for (i = 0; i < TYPE_N_TEMPLATE_ARGUMENTS (type); ++i)
798 struct symbol *sym = TYPE_TEMPLATE_ARGUMENT (type, i);
800 if (SYMBOL_CLASS (sym) != LOC_TYPEDEF)
801 continue;
803 if (first)
805 wrap_here (" ");
806 fprintf_filtered (stream, _("[with %s = "),
807 SYMBOL_LINKAGE_NAME (sym));
808 first = 0;
810 else
812 fputs_filtered (", ", stream);
813 wrap_here (" ");
814 fprintf_filtered (stream, "%s = ", SYMBOL_LINKAGE_NAME (sym));
817 c_print_type (SYMBOL_TYPE (sym), "", stream, -1, 0, flags);
820 if (!first)
821 fputs_filtered (_("] "), stream);
824 /* Print the name of the type (or the ultimate pointer target,
825 function value or array element), or the description of a structure
826 or union.
828 SHOW positive means print details about the type (e.g. enum
829 values), and print structure elements passing SHOW - 1 for show.
831 SHOW negative means just print the type name or struct tag if there
832 is one. If there is no name, print something sensible but concise
833 like "struct {...}".
835 SHOW zero means just print the type name or struct tag if there is
836 one. If there is no name, print something sensible but not as
837 concise like "struct {int x; int y;}".
839 LEVEL is the number of spaces to indent by.
840 We increase it for some recursive calls. */
842 void
843 c_type_print_base (struct type *type, struct ui_file *stream,
844 int show, int level, const struct type_print_options *flags)
846 int i;
847 int len, real_len;
848 enum
850 s_none, s_public, s_private, s_protected
852 section_type;
853 int need_access_label = 0;
854 int j, len2;
856 QUIT;
858 if (type == NULL)
860 fputs_filtered (_("<type unknown>"), stream);
861 return;
864 /* When SHOW is zero or less, and there is a valid type name, then
865 always just print the type name directly from the type. */
866 /* If we have "typedef struct foo {. . .} bar;" do we want to print
867 it as "struct foo" or as "bar"? Pick the latter, because C++
868 folk tend to expect things like "class5 *foo" rather than "struct
869 class5 *foo". */
871 if (show <= 0
872 && TYPE_NAME (type) != NULL)
874 c_type_print_modifier (type, stream, 0, 1);
875 print_name_maybe_canonical (TYPE_NAME (type), flags, stream);
876 return;
879 CHECK_TYPEDEF (type);
881 switch (TYPE_CODE (type))
883 case TYPE_CODE_TYPEDEF:
884 /* If we get here, the typedef doesn't have a name, and we
885 couldn't resolve TYPE_TARGET_TYPE. Not much we can do. */
886 gdb_assert (TYPE_NAME (type) == NULL);
887 gdb_assert (TYPE_TARGET_TYPE (type) == NULL);
888 fprintf_filtered (stream, _("<unnamed typedef>"));
889 break;
891 case TYPE_CODE_ARRAY:
892 case TYPE_CODE_PTR:
893 case TYPE_CODE_MEMBERPTR:
894 case TYPE_CODE_REF:
895 case TYPE_CODE_FUNC:
896 case TYPE_CODE_METHOD:
897 case TYPE_CODE_METHODPTR:
898 c_type_print_base (TYPE_TARGET_TYPE (type),
899 stream, show, level, flags);
900 break;
902 case TYPE_CODE_STRUCT:
903 case TYPE_CODE_UNION:
905 struct type_print_options local_flags = *flags;
906 struct type_print_options semi_local_flags = *flags;
907 struct cleanup *local_cleanups = make_cleanup (null_cleanup, NULL);
909 local_flags.local_typedefs = NULL;
910 semi_local_flags.local_typedefs = NULL;
912 if (!flags->raw)
914 if (flags->local_typedefs)
915 local_flags.local_typedefs
916 = copy_typedef_hash (flags->local_typedefs);
917 else
918 local_flags.local_typedefs = create_typedef_hash ();
920 make_cleanup_free_typedef_hash (local_flags.local_typedefs);
923 c_type_print_modifier (type, stream, 0, 1);
924 if (TYPE_CODE (type) == TYPE_CODE_UNION)
925 fprintf_filtered (stream, "union ");
926 else if (TYPE_DECLARED_CLASS (type))
927 fprintf_filtered (stream, "class ");
928 else
929 fprintf_filtered (stream, "struct ");
931 /* Print the tag if it exists. The HP aCC compiler emits a
932 spurious "{unnamed struct}"/"{unnamed union}"/"{unnamed
933 enum}" tag for unnamed struct/union/enum's, which we don't
934 want to print. */
935 if (TYPE_TAG_NAME (type) != NULL
936 && !startswith (TYPE_TAG_NAME (type), "{unnamed"))
938 /* When printing the tag name, we are still effectively
939 printing in the outer context, hence the use of FLAGS
940 here. */
941 print_name_maybe_canonical (TYPE_TAG_NAME (type), flags, stream);
942 if (show > 0)
943 fputs_filtered (" ", stream);
946 if (show < 0)
948 /* If we just printed a tag name, no need to print anything
949 else. */
950 if (TYPE_TAG_NAME (type) == NULL)
951 fprintf_filtered (stream, "{...}");
953 else if (show > 0 || TYPE_TAG_NAME (type) == NULL)
955 struct type *basetype;
956 int vptr_fieldno;
958 c_type_print_template_args (&local_flags, type, stream);
960 /* Add in template parameters when printing derivation info. */
961 add_template_parameters (local_flags.local_typedefs, type);
962 cp_type_print_derivation_info (stream, type, &local_flags);
964 /* This holds just the global typedefs and the template
965 parameters. */
966 semi_local_flags.local_typedefs
967 = copy_typedef_hash (local_flags.local_typedefs);
968 if (semi_local_flags.local_typedefs)
969 make_cleanup_free_typedef_hash (semi_local_flags.local_typedefs);
971 /* Now add in the local typedefs. */
972 recursively_update_typedef_hash (local_flags.local_typedefs, type);
974 fprintf_filtered (stream, "{\n");
975 if (TYPE_NFIELDS (type) == 0 && TYPE_NFN_FIELDS (type) == 0
976 && TYPE_TYPEDEF_FIELD_COUNT (type) == 0)
978 if (TYPE_STUB (type))
979 fprintfi_filtered (level + 4, stream,
980 _("<incomplete type>\n"));
981 else
982 fprintfi_filtered (level + 4, stream,
983 _("<no data fields>\n"));
986 /* Start off with no specific section type, so we can print
987 one for the first field we find, and use that section type
988 thereafter until we find another type. */
990 section_type = s_none;
992 /* For a class, if all members are private, there's no need
993 for a "private:" label; similarly, for a struct or union
994 masquerading as a class, if all members are public, there's
995 no need for a "public:" label. */
997 if (TYPE_DECLARED_CLASS (type))
999 QUIT;
1000 len = TYPE_NFIELDS (type);
1001 for (i = TYPE_N_BASECLASSES (type); i < len; i++)
1002 if (!TYPE_FIELD_PRIVATE (type, i))
1004 need_access_label = 1;
1005 break;
1007 QUIT;
1008 if (!need_access_label)
1010 len2 = TYPE_NFN_FIELDS (type);
1011 for (j = 0; j < len2; j++)
1013 len = TYPE_FN_FIELDLIST_LENGTH (type, j);
1014 for (i = 0; i < len; i++)
1015 if (!TYPE_FN_FIELD_PRIVATE (TYPE_FN_FIELDLIST1 (type,
1016 j), i))
1018 need_access_label = 1;
1019 break;
1021 if (need_access_label)
1022 break;
1026 else
1028 QUIT;
1029 len = TYPE_NFIELDS (type);
1030 for (i = TYPE_N_BASECLASSES (type); i < len; i++)
1031 if (TYPE_FIELD_PRIVATE (type, i)
1032 || TYPE_FIELD_PROTECTED (type, i))
1034 need_access_label = 1;
1035 break;
1037 QUIT;
1038 if (!need_access_label)
1040 len2 = TYPE_NFN_FIELDS (type);
1041 for (j = 0; j < len2; j++)
1043 QUIT;
1044 len = TYPE_FN_FIELDLIST_LENGTH (type, j);
1045 for (i = 0; i < len; i++)
1046 if (TYPE_FN_FIELD_PROTECTED (TYPE_FN_FIELDLIST1 (type,
1047 j), i)
1048 || TYPE_FN_FIELD_PRIVATE (TYPE_FN_FIELDLIST1 (type,
1052 need_access_label = 1;
1053 break;
1055 if (need_access_label)
1056 break;
1061 /* If there is a base class for this type,
1062 do not print the field that it occupies. */
1064 len = TYPE_NFIELDS (type);
1065 vptr_fieldno = get_vptr_fieldno (type, &basetype);
1066 for (i = TYPE_N_BASECLASSES (type); i < len; i++)
1068 QUIT;
1070 /* If we have a virtual table pointer, omit it. Even if
1071 virtual table pointers are not specifically marked in
1072 the debug info, they should be artificial. */
1073 if ((i == vptr_fieldno && type == basetype)
1074 || TYPE_FIELD_ARTIFICIAL (type, i))
1075 continue;
1077 if (need_access_label)
1079 if (TYPE_FIELD_PROTECTED (type, i))
1081 if (section_type != s_protected)
1083 section_type = s_protected;
1084 fprintfi_filtered (level + 2, stream,
1085 "protected:\n");
1088 else if (TYPE_FIELD_PRIVATE (type, i))
1090 if (section_type != s_private)
1092 section_type = s_private;
1093 fprintfi_filtered (level + 2, stream,
1094 "private:\n");
1097 else
1099 if (section_type != s_public)
1101 section_type = s_public;
1102 fprintfi_filtered (level + 2, stream,
1103 "public:\n");
1108 print_spaces_filtered (level + 4, stream);
1109 if (field_is_static (&TYPE_FIELD (type, i)))
1110 fprintf_filtered (stream, "static ");
1111 c_print_type (TYPE_FIELD_TYPE (type, i),
1112 TYPE_FIELD_NAME (type, i),
1113 stream, show - 1, level + 4,
1114 &local_flags);
1115 if (!field_is_static (&TYPE_FIELD (type, i))
1116 && TYPE_FIELD_PACKED (type, i))
1118 /* It is a bitfield. This code does not attempt
1119 to look at the bitpos and reconstruct filler,
1120 unnamed fields. This would lead to misleading
1121 results if the compiler does not put out fields
1122 for such things (I don't know what it does). */
1123 fprintf_filtered (stream, " : %d",
1124 TYPE_FIELD_BITSIZE (type, i));
1126 fprintf_filtered (stream, ";\n");
1129 /* If there are both fields and methods, put a blank line
1130 between them. Make sure to count only method that we
1131 will display; artificial methods will be hidden. */
1132 len = TYPE_NFN_FIELDS (type);
1133 if (!flags->print_methods)
1134 len = 0;
1135 real_len = 0;
1136 for (i = 0; i < len; i++)
1138 struct fn_field *f = TYPE_FN_FIELDLIST1 (type, i);
1139 int len2 = TYPE_FN_FIELDLIST_LENGTH (type, i);
1140 int j;
1142 for (j = 0; j < len2; j++)
1143 if (!TYPE_FN_FIELD_ARTIFICIAL (f, j))
1144 real_len++;
1146 if (real_len > 0 && section_type != s_none)
1147 fprintf_filtered (stream, "\n");
1149 /* C++: print out the methods. */
1150 for (i = 0; i < len; i++)
1152 struct fn_field *f = TYPE_FN_FIELDLIST1 (type, i);
1153 int j, len2 = TYPE_FN_FIELDLIST_LENGTH (type, i);
1154 const char *method_name = TYPE_FN_FIELDLIST_NAME (type, i);
1155 const char *name = type_name_no_tag (type);
1156 int is_constructor = name && strcmp (method_name,
1157 name) == 0;
1159 for (j = 0; j < len2; j++)
1161 const char *mangled_name;
1162 char *demangled_name;
1163 struct cleanup *inner_cleanup;
1164 const char *physname = TYPE_FN_FIELD_PHYSNAME (f, j);
1165 int is_full_physname_constructor =
1166 TYPE_FN_FIELD_CONSTRUCTOR (f, j)
1167 || is_constructor_name (physname)
1168 || is_destructor_name (physname)
1169 || method_name[0] == '~';
1171 /* Do not print out artificial methods. */
1172 if (TYPE_FN_FIELD_ARTIFICIAL (f, j))
1173 continue;
1175 inner_cleanup = make_cleanup (null_cleanup, NULL);
1177 QUIT;
1178 if (TYPE_FN_FIELD_PROTECTED (f, j))
1180 if (section_type != s_protected)
1182 section_type = s_protected;
1183 fprintfi_filtered (level + 2, stream,
1184 "protected:\n");
1187 else if (TYPE_FN_FIELD_PRIVATE (f, j))
1189 if (section_type != s_private)
1191 section_type = s_private;
1192 fprintfi_filtered (level + 2, stream,
1193 "private:\n");
1196 else
1198 if (section_type != s_public)
1200 section_type = s_public;
1201 fprintfi_filtered (level + 2, stream,
1202 "public:\n");
1206 print_spaces_filtered (level + 4, stream);
1207 if (TYPE_FN_FIELD_VIRTUAL_P (f, j))
1208 fprintf_filtered (stream, "virtual ");
1209 else if (TYPE_FN_FIELD_STATIC_P (f, j))
1210 fprintf_filtered (stream, "static ");
1211 if (TYPE_TARGET_TYPE (TYPE_FN_FIELD_TYPE (f, j)) == 0)
1213 /* Keep GDB from crashing here. */
1214 fprintf_filtered (stream,
1215 _("<undefined type> %s;\n"),
1216 TYPE_FN_FIELD_PHYSNAME (f, j));
1217 break;
1219 else if (!is_constructor /* Constructors don't
1220 have declared
1221 types. */
1222 && !is_full_physname_constructor /* " " */
1223 && !is_type_conversion_operator (type, i, j))
1225 c_print_type (TYPE_TARGET_TYPE (TYPE_FN_FIELD_TYPE (f, j)),
1226 "", stream, -1, 0,
1227 &local_flags);
1228 fputs_filtered (" ", stream);
1230 if (TYPE_FN_FIELD_STUB (f, j))
1232 char *tem;
1234 /* Build something we can demangle. */
1235 tem = gdb_mangle_name (type, i, j);
1236 make_cleanup (xfree, tem);
1237 mangled_name = tem;
1239 else
1240 mangled_name = TYPE_FN_FIELD_PHYSNAME (f, j);
1242 demangled_name =
1243 gdb_demangle (mangled_name,
1244 DMGL_ANSI | DMGL_PARAMS);
1245 if (demangled_name == NULL)
1247 /* In some cases (for instance with the HP
1248 demangling), if a function has more than 10
1249 arguments, the demangling will fail.
1250 Let's try to reconstruct the function
1251 signature from the symbol information. */
1252 if (!TYPE_FN_FIELD_STUB (f, j))
1254 int staticp = TYPE_FN_FIELD_STATIC_P (f, j);
1255 struct type *mtype = TYPE_FN_FIELD_TYPE (f, j);
1257 cp_type_print_method_args (mtype,
1259 method_name,
1260 staticp,
1261 stream, &local_flags);
1263 else
1264 fprintf_filtered (stream,
1265 _("<badly mangled name '%s'>"),
1266 mangled_name);
1268 else
1270 char *p;
1271 char *demangled_no_class
1272 = remove_qualifiers (demangled_name);
1274 /* Get rid of the `static' appended by the
1275 demangler. */
1276 p = strstr (demangled_no_class, " static");
1277 if (p != NULL)
1279 int length = p - demangled_no_class;
1280 char *demangled_no_static;
1282 demangled_no_static
1283 = (char *) xmalloc (length + 1);
1284 strncpy (demangled_no_static,
1285 demangled_no_class, length);
1286 *(demangled_no_static + length) = '\0';
1287 fputs_filtered (demangled_no_static, stream);
1288 xfree (demangled_no_static);
1290 else
1291 fputs_filtered (demangled_no_class, stream);
1292 xfree (demangled_name);
1295 do_cleanups (inner_cleanup);
1297 fprintf_filtered (stream, ";\n");
1301 /* Print typedefs defined in this class. */
1303 if (TYPE_TYPEDEF_FIELD_COUNT (type) != 0 && flags->print_typedefs)
1305 if (TYPE_NFIELDS (type) != 0 || TYPE_NFN_FIELDS (type) != 0)
1306 fprintf_filtered (stream, "\n");
1308 for (i = 0; i < TYPE_TYPEDEF_FIELD_COUNT (type); i++)
1310 struct type *target = TYPE_TYPEDEF_FIELD_TYPE (type, i);
1312 /* Dereference the typedef declaration itself. */
1313 gdb_assert (TYPE_CODE (target) == TYPE_CODE_TYPEDEF);
1314 target = TYPE_TARGET_TYPE (target);
1316 print_spaces_filtered (level + 4, stream);
1317 fprintf_filtered (stream, "typedef ");
1319 /* We want to print typedefs with substitutions
1320 from the template parameters or globally-known
1321 typedefs but not local typedefs. */
1322 c_print_type (target,
1323 TYPE_TYPEDEF_FIELD_NAME (type, i),
1324 stream, show - 1, level + 4,
1325 &semi_local_flags);
1326 fprintf_filtered (stream, ";\n");
1330 fprintfi_filtered (level, stream, "}");
1333 do_cleanups (local_cleanups);
1335 break;
1337 case TYPE_CODE_ENUM:
1338 c_type_print_modifier (type, stream, 0, 1);
1339 fprintf_filtered (stream, "enum ");
1340 if (TYPE_DECLARED_CLASS (type))
1341 fprintf_filtered (stream, "class ");
1342 /* Print the tag name if it exists.
1343 The aCC compiler emits a spurious
1344 "{unnamed struct}"/"{unnamed union}"/"{unnamed enum}"
1345 tag for unnamed struct/union/enum's, which we don't
1346 want to print. */
1347 if (TYPE_TAG_NAME (type) != NULL
1348 && !startswith (TYPE_TAG_NAME (type), "{unnamed"))
1350 print_name_maybe_canonical (TYPE_TAG_NAME (type), flags, stream);
1351 if (show > 0)
1352 fputs_filtered (" ", stream);
1355 wrap_here (" ");
1356 if (show < 0)
1358 /* If we just printed a tag name, no need to print anything
1359 else. */
1360 if (TYPE_TAG_NAME (type) == NULL)
1361 fprintf_filtered (stream, "{...}");
1363 else if (show > 0 || TYPE_TAG_NAME (type) == NULL)
1365 LONGEST lastval = 0;
1367 /* We can't handle this case perfectly, as DWARF does not
1368 tell us whether or not the underlying type was specified
1369 in the source (and other debug formats don't provide this
1370 at all). We choose to print the underlying type, if it
1371 has a name, when in C++ on the theory that it's better to
1372 print too much than too little; but conversely not to
1373 print something egregiously outside the current
1374 language's syntax. */
1375 if (current_language->la_language == language_cplus
1376 && TYPE_TARGET_TYPE (type) != NULL)
1378 struct type *underlying = check_typedef (TYPE_TARGET_TYPE (type));
1380 if (TYPE_NAME (underlying) != NULL)
1381 fprintf_filtered (stream, ": %s ", TYPE_NAME (underlying));
1384 fprintf_filtered (stream, "{");
1385 len = TYPE_NFIELDS (type);
1386 for (i = 0; i < len; i++)
1388 QUIT;
1389 if (i)
1390 fprintf_filtered (stream, ", ");
1391 wrap_here (" ");
1392 fputs_filtered (TYPE_FIELD_NAME (type, i), stream);
1393 if (lastval != TYPE_FIELD_ENUMVAL (type, i))
1395 fprintf_filtered (stream, " = %s",
1396 plongest (TYPE_FIELD_ENUMVAL (type, i)));
1397 lastval = TYPE_FIELD_ENUMVAL (type, i);
1399 lastval++;
1401 fprintf_filtered (stream, "}");
1403 break;
1405 case TYPE_CODE_VOID:
1406 fprintf_filtered (stream, "void");
1407 break;
1409 case TYPE_CODE_UNDEF:
1410 fprintf_filtered (stream, _("struct <unknown>"));
1411 break;
1413 case TYPE_CODE_ERROR:
1414 fprintf_filtered (stream, "%s", TYPE_ERROR_NAME (type));
1415 break;
1417 case TYPE_CODE_RANGE:
1418 /* This should not occur. */
1419 fprintf_filtered (stream, _("<range type>"));
1420 break;
1422 case TYPE_CODE_NAMESPACE:
1423 fputs_filtered ("namespace ", stream);
1424 fputs_filtered (TYPE_TAG_NAME (type), stream);
1425 break;
1427 default:
1428 /* Handle types not explicitly handled by the other cases, such
1429 as fundamental types. For these, just print whatever the
1430 type name is, as recorded in the type itself. If there is no
1431 type name, then complain. */
1432 if (TYPE_NAME (type) != NULL)
1434 c_type_print_modifier (type, stream, 0, 1);
1435 print_name_maybe_canonical (TYPE_NAME (type), flags, stream);
1437 else
1439 /* At least for dump_symtab, it is important that this not
1440 be an error (). */
1441 fprintf_filtered (stream, _("<invalid type code %d>"),
1442 TYPE_CODE (type));
1444 break;