1 /* Language independent support for printing types for GDB, the GNU debugger.
3 Copyright (C) 1986-2018 Free Software Foundation, Inc.
5 This file is part of GDB.
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 3 of the License, or
10 (at your option) any later version.
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.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
21 #include "gdb_obstack.h"
22 #include "bfd.h" /* Binary File Description */
25 #include "expression.h"
33 #include "typeprint.h"
36 #include "cli/cli-utils.h"
37 #include "extension.h"
38 #include "completer.h"
40 const struct type_print_options type_print_raw_options
=
43 1, /* print_methods */
44 1, /* print_typedefs */
45 0, /* print_offsets */
46 0, /* print_nested_type_limit */
47 NULL
, /* local_typedefs */
48 NULL
, /* global_table */
49 NULL
/* global_printers */
52 /* The default flags for 'ptype' and 'whatis'. */
54 static struct type_print_options default_ptype_flags
=
57 1, /* print_methods */
58 1, /* print_typedefs */
59 0, /* print_offsets */
60 0, /* print_nested_type_limit */
61 NULL
, /* local_typedefs */
62 NULL
, /* global_table */
63 NULL
/* global_printers */
68 /* A hash function for a typedef_field. */
71 hash_typedef_field (const void *p
)
73 const struct decl_field
*tf
= (const struct decl_field
*) p
;
74 struct type
*t
= check_typedef (tf
->type
);
76 return htab_hash_string (TYPE_SAFE_NAME (t
));
79 /* An equality function for a typedef field. */
82 eq_typedef_field (const void *a
, const void *b
)
84 const struct decl_field
*tfa
= (const struct decl_field
*) a
;
85 const struct decl_field
*tfb
= (const struct decl_field
*) b
;
87 return types_equal (tfa
->type
, tfb
->type
);
90 /* See typeprint.h. */
93 typedef_hash_table::recursively_update (struct type
*t
)
97 for (i
= 0; i
< TYPE_TYPEDEF_FIELD_COUNT (t
); ++i
)
99 struct decl_field
*tdef
= &TYPE_TYPEDEF_FIELD (t
, i
);
102 slot
= htab_find_slot (m_table
, tdef
, INSERT
);
103 /* Only add a given typedef name once. Really this shouldn't
104 happen; but it is safe enough to do the updates breadth-first
105 and thus use the most specific typedef. */
110 /* Recurse into superclasses. */
111 for (i
= 0; i
< TYPE_N_BASECLASSES (t
); ++i
)
112 recursively_update (TYPE_BASECLASS (t
, i
));
115 /* See typeprint.h. */
118 typedef_hash_table::add_template_parameters (struct type
*t
)
122 for (i
= 0; i
< TYPE_N_TEMPLATE_ARGUMENTS (t
); ++i
)
124 struct decl_field
*tf
;
127 /* We only want type-valued template parameters in the hash. */
128 if (SYMBOL_CLASS (TYPE_TEMPLATE_ARGUMENT (t
, i
)) != LOC_TYPEDEF
)
131 tf
= XOBNEW (&m_storage
, struct decl_field
);
132 tf
->name
= SYMBOL_LINKAGE_NAME (TYPE_TEMPLATE_ARGUMENT (t
, i
));
133 tf
->type
= SYMBOL_TYPE (TYPE_TEMPLATE_ARGUMENT (t
, i
));
135 slot
= htab_find_slot (m_table
, tf
, INSERT
);
141 /* See typeprint.h. */
143 typedef_hash_table::typedef_hash_table ()
145 m_table
= htab_create_alloc (10, hash_typedef_field
, eq_typedef_field
,
146 NULL
, xcalloc
, xfree
);
149 /* Free a typedef field table. */
151 typedef_hash_table::~typedef_hash_table ()
153 htab_delete (m_table
);
156 /* Helper function for typedef_hash_table::copy. */
159 copy_typedef_hash_element (void **slot
, void *nt
)
161 htab_t new_table
= (htab_t
) nt
;
164 new_slot
= htab_find_slot (new_table
, *slot
, INSERT
);
165 if (*new_slot
== NULL
)
171 /* See typeprint.h. */
173 typedef_hash_table::typedef_hash_table (const typedef_hash_table
&table
)
175 m_table
= htab_create_alloc (10, hash_typedef_field
, eq_typedef_field
,
176 NULL
, xcalloc
, xfree
);
177 htab_traverse_noresize (table
.m_table
, copy_typedef_hash_element
,
181 /* Look up the type T in the global typedef hash. If it is found,
182 return the typedef name. If it is not found, apply the
183 type-printers, if any, given by start_script_type_printers and return the
184 result. A NULL return means that the name was not found. */
187 typedef_hash_table::find_global_typedef (const struct type_print_options
*flags
,
192 struct decl_field tf
, *new_tf
;
194 if (flags
->global_typedefs
== NULL
)
200 slot
= htab_find_slot (flags
->global_typedefs
->m_table
, &tf
, INSERT
);
203 new_tf
= (struct decl_field
*) *slot
;
207 /* Put an entry into the hash table now, in case
208 apply_ext_lang_type_printers recurses. */
209 new_tf
= XOBNEW (&flags
->global_typedefs
->m_storage
, struct decl_field
);
215 applied
= apply_ext_lang_type_printers (flags
->global_printers
, t
);
220 = (const char *) obstack_copy0 (&flags
->global_typedefs
->m_storage
,
221 applied
, strlen (applied
));
228 /* See typeprint.h. */
231 typedef_hash_table::find_typedef (const struct type_print_options
*flags
,
234 if (flags
->local_typedefs
!= NULL
)
236 struct decl_field tf
, *found
;
240 found
= (struct decl_field
*) htab_find (flags
->local_typedefs
->m_table
,
247 return find_global_typedef (flags
, t
);
252 /* Print a description of a type in the format of a
253 typedef for the current language.
254 NEW is the new name for a type TYPE. */
257 typedef_print (struct type
*type
, struct symbol
*newobj
, struct ui_file
*stream
)
259 LA_PRINT_TYPEDEF (type
, newobj
, stream
);
262 /* The default way to print a typedef. */
265 default_print_typedef (struct type
*type
, struct symbol
*new_symbol
,
266 struct ui_file
*stream
)
268 error (_("Language not supported."));
271 /* Print a description of a type TYPE in the form of a declaration of a
272 variable named VARSTRING. (VARSTRING is demangled if necessary.)
273 Output goes to STREAM (via stdio).
274 If SHOW is positive, we show the contents of the outermost level
275 of structure even if there is a type name that could be used instead.
276 If SHOW is negative, we never show the details of elements' types. */
279 type_print (struct type
*type
, const char *varstring
, struct ui_file
*stream
,
282 LA_PRINT_TYPE (type
, varstring
, stream
, show
, 0, &default_ptype_flags
);
285 /* Print TYPE to a string, returning it. The caller is responsible for
286 freeing the string. */
289 type_to_string (struct type
*type
)
295 type_print (type
, "", &stb
, -1);
296 return std::move (stb
.string ());
298 CATCH (except
, RETURN_MASK_ALL
)
306 /* See typeprint.h. */
309 type_print_unknown_return_type (struct ui_file
*stream
)
311 fprintf_filtered (stream
, _("<unknown return type>"));
314 /* See typeprint.h. */
317 error_unknown_type (const char *sym_print_name
)
319 error (_("'%s' has unknown type; cast it to its declared type"),
323 /* Print type of EXP, or last thing in value history if EXP == NULL.
324 show is passed to type_print. */
327 whatis_exp (const char *exp
, int show
)
330 struct type
*real_type
= NULL
;
335 struct value_print_options opts
;
336 struct type_print_options flags
= default_ptype_flags
;
344 for (++exp
; *exp
&& !isspace (*exp
); ++exp
)
352 flags
.print_methods
= 0;
355 flags
.print_methods
= 1;
358 flags
.print_typedefs
= 0;
361 flags
.print_typedefs
= 1;
365 /* Filter out languages which don't implement the
368 && (current_language
->la_language
== language_c
369 || current_language
->la_language
== language_cplus
))
371 flags
.print_offsets
= 1;
372 flags
.print_typedefs
= 0;
373 flags
.print_methods
= 0;
378 error (_("unrecognized flag '%c'"), *exp
);
383 if (!*exp
&& !seen_one
)
384 error (_("flag expected"));
386 error (_("expected space after format"));
387 exp
= skip_spaces (exp
);
390 expression_up expr
= parse_expression (exp
);
392 /* The behavior of "whatis" depends on whether the user
393 expression names a type directly, or a language expression
394 (including variable names). If the former, then "whatis"
395 strips one level of typedefs, only. If an expression,
396 "whatis" prints the type of the expression without stripping
397 any typedef level. "ptype" always strips all levels of
399 if (show
== -1 && expr
->elts
[0].opcode
== OP_TYPE
)
401 /* The user expression names a type directly. */
402 type
= expr
->elts
[1].type
;
404 /* If this is a typedef, then find its immediate target.
405 Use check_typedef to resolve stubs, but ignore its result
406 because we do not want to dig past all typedefs. */
407 check_typedef (type
);
408 if (TYPE_CODE (type
) == TYPE_CODE_TYPEDEF
)
409 type
= TYPE_TARGET_TYPE (type
);
411 /* If the expression is actually a type, then there's no
412 value to fetch the dynamic type from. */
417 /* The user expression names a type indirectly by naming an
418 object or expression of that type. Find that
419 indirectly-named type. */
420 val
= evaluate_type (expr
.get ());
421 type
= value_type (val
);
426 val
= access_value_history (0);
427 type
= value_type (val
);
430 get_user_print_options (&opts
);
431 if (val
!= NULL
&& opts
.objectprint
)
433 if (((TYPE_CODE (type
) == TYPE_CODE_PTR
) || TYPE_IS_REFERENCE (type
))
434 && (TYPE_CODE (TYPE_TARGET_TYPE (type
)) == TYPE_CODE_STRUCT
))
435 real_type
= value_rtti_indirect_type (val
, &full
, &top
, &using_enc
);
436 else if (TYPE_CODE (type
) == TYPE_CODE_STRUCT
)
437 real_type
= value_rtti_type (val
, &full
, &top
, &using_enc
);
440 if (flags
.print_offsets
441 && (TYPE_CODE (type
) == TYPE_CODE_STRUCT
442 || TYPE_CODE (type
) == TYPE_CODE_UNION
))
443 fprintf_filtered (gdb_stdout
, "/* offset | size */ ");
445 printf_filtered ("type = ");
447 std::unique_ptr
<typedef_hash_table
> table_holder
;
448 std::unique_ptr
<ext_lang_type_printers
> printer_holder
;
451 table_holder
.reset (new typedef_hash_table
);
452 flags
.global_typedefs
= table_holder
.get ();
454 printer_holder
.reset (new ext_lang_type_printers
);
455 flags
.global_printers
= printer_holder
.get ();
460 printf_filtered ("/* real type = ");
461 type_print (real_type
, "", gdb_stdout
, -1);
463 printf_filtered (" (incomplete object)");
464 printf_filtered (" */\n");
467 LA_PRINT_TYPE (type
, "", gdb_stdout
, show
, 0, &flags
);
468 printf_filtered ("\n");
472 whatis_command (const char *exp
, int from_tty
)
474 /* Most of the time users do not want to see all the fields
475 in a structure. If they do they can use the "ptype" command.
476 Hence the "-1" below. */
477 whatis_exp (exp
, -1);
480 /* TYPENAME is either the name of a type, or an expression. */
483 ptype_command (const char *type_name
, int from_tty
)
485 whatis_exp (type_name
, 1);
488 /* Print integral scalar data VAL, of type TYPE, onto stdio stream STREAM.
489 Used to print data from type structures in a specified type. For example,
490 array bounds may be characters or booleans in some languages, and this
491 allows the ranges to be printed in their "natural" form rather than as
492 decimal integer values.
494 FIXME: This is here simply because only the type printing routines
495 currently use it, and it wasn't clear if it really belonged somewhere
496 else (like printcmd.c). There are a lot of other gdb routines that do
497 something similar, but they are generally concerned with printing values
498 that come from the inferior in target byte order and target size. */
501 print_type_scalar (struct type
*type
, LONGEST val
, struct ui_file
*stream
)
506 type
= check_typedef (type
);
508 switch (TYPE_CODE (type
))
512 len
= TYPE_NFIELDS (type
);
513 for (i
= 0; i
< len
; i
++)
515 if (TYPE_FIELD_ENUMVAL (type
, i
) == val
)
522 fputs_filtered (TYPE_FIELD_NAME (type
, i
), stream
);
526 print_longest (stream
, 'd', 0, val
);
531 print_longest (stream
, TYPE_UNSIGNED (type
) ? 'u' : 'd', 0, val
);
535 LA_PRINT_CHAR ((unsigned char) val
, type
, stream
);
539 fprintf_filtered (stream
, val
? "TRUE" : "FALSE");
542 case TYPE_CODE_RANGE
:
543 print_type_scalar (TYPE_TARGET_TYPE (type
), val
, stream
);
546 case TYPE_CODE_UNDEF
:
548 case TYPE_CODE_ARRAY
:
549 case TYPE_CODE_STRUCT
:
550 case TYPE_CODE_UNION
:
555 case TYPE_CODE_STRING
:
556 case TYPE_CODE_ERROR
:
557 case TYPE_CODE_MEMBERPTR
:
558 case TYPE_CODE_METHODPTR
:
559 case TYPE_CODE_METHOD
:
561 case TYPE_CODE_RVALUE_REF
:
562 case TYPE_CODE_NAMESPACE
:
563 error (_("internal error: unhandled type in print_type_scalar"));
567 error (_("Invalid type code in symbol table."));
572 /* Dump details of a type specified either directly or indirectly.
573 Uses the same sort of type lookup mechanism as ptype_command()
574 and whatis_command(). */
577 maintenance_print_type (const char *type_name
, int from_tty
)
582 if (type_name
!= NULL
)
584 expression_up expr
= parse_expression (type_name
);
585 if (expr
->elts
[0].opcode
== OP_TYPE
)
587 /* The user expression names a type directly, just use that type. */
588 type
= expr
->elts
[1].type
;
592 /* The user expression may name a type indirectly by naming an
593 object of that type. Find that indirectly named type. */
594 val
= evaluate_type (expr
.get ());
595 type
= value_type (val
);
599 recursive_dump_type (type
, 0);
605 struct cmd_list_element
*setprinttypelist
;
607 struct cmd_list_element
*showprinttypelist
;
610 set_print_type (const char *arg
, int from_tty
)
613 "\"set print type\" must be followed by the name of a subcommand.\n");
614 help_list (setprintlist
, "set print type ", all_commands
, gdb_stdout
);
618 show_print_type (const char *args
, int from_tty
)
620 cmd_show_list (showprinttypelist
, from_tty
, "");
623 static int print_methods
= 1;
626 set_print_type_methods (const char *args
,
627 int from_tty
, struct cmd_list_element
*c
)
629 default_ptype_flags
.print_methods
= print_methods
;
633 show_print_type_methods (struct ui_file
*file
, int from_tty
,
634 struct cmd_list_element
*c
, const char *value
)
636 fprintf_filtered (file
, _("Printing of methods defined in a class in %s\n"),
640 static int print_typedefs
= 1;
643 set_print_type_typedefs (const char *args
,
644 int from_tty
, struct cmd_list_element
*c
)
646 default_ptype_flags
.print_typedefs
= print_typedefs
;
650 show_print_type_typedefs (struct ui_file
*file
, int from_tty
,
651 struct cmd_list_element
*c
, const char *value
)
653 fprintf_filtered (file
, _("Printing of typedefs defined in a class in %s\n"),
657 /* Limit on the number of nested type definitions to print or -1 to print
658 all nested type definitions in a class. By default, we do not print
659 nested definitions. */
661 static int print_nested_type_limit
= 0;
663 /* Set how many nested type definitions should be printed by the type
667 set_print_type_nested_types (const char *args
, int from_tty
,
668 struct cmd_list_element
*c
)
670 default_ptype_flags
.print_nested_type_limit
= print_nested_type_limit
;
673 /* Show how many nested type definitions the type printer will print. */
676 show_print_type_nested_types (struct ui_file
*file
, int from_tty
,
677 struct cmd_list_element
*c
, const char *value
)
681 fprintf_filtered (file
,
682 _("Will not print nested types defined in a class\n"));
686 fprintf_filtered (file
,
687 _("Will print %s nested types defined in a class\n"),
693 _initialize_typeprint (void)
695 struct cmd_list_element
*c
;
697 c
= add_com ("ptype", class_vars
, ptype_command
, _("\
698 Print definition of type TYPE.\n\
699 Usage: ptype[/FLAGS] TYPE | EXPRESSION\n\
700 Argument may be any type (for example a type name defined by typedef,\n\
701 or \"struct STRUCT-TAG\" or \"class CLASS-NAME\" or \"union UNION-TAG\"\n\
702 or \"enum ENUM-TAG\") or an expression.\n\
703 The selected stack frame's lexical context is used to look up the name.\n\
704 Contrary to \"whatis\", \"ptype\" always unrolls any typedefs.\n\
706 Available FLAGS are:\n\
707 /r print in \"raw\" form; do not substitute typedefs\n\
708 /m do not print methods defined in a class\n\
709 /M print methods defined in a class\n\
710 /t do not print typedefs defined in a class\n\
711 /T print typedefs defined in a class\n\
712 /o print offsets and sizes of fields in a struct (like pahole)\n"));
713 set_cmd_completer (c
, expression_completer
);
715 c
= add_com ("whatis", class_vars
, whatis_command
,
716 _("Print data type of expression EXP.\n\
717 Only one level of typedefs is unrolled. See also \"ptype\"."));
718 set_cmd_completer (c
, expression_completer
);
720 add_prefix_cmd ("type", no_class
, show_print_type
,
721 _("Generic command for showing type-printing settings."),
722 &showprinttypelist
, "show print type ", 0, &showprintlist
);
723 add_prefix_cmd ("type", no_class
, set_print_type
,
724 _("Generic command for setting how types print."),
725 &setprinttypelist
, "show print type ", 0, &setprintlist
);
727 add_setshow_boolean_cmd ("methods", no_class
, &print_methods
,
729 Set printing of methods defined in classes."), _("\
730 Show printing of methods defined in classes."), NULL
,
731 set_print_type_methods
,
732 show_print_type_methods
,
733 &setprinttypelist
, &showprinttypelist
);
734 add_setshow_boolean_cmd ("typedefs", no_class
, &print_typedefs
,
736 Set printing of typedefs defined in classes."), _("\
737 Show printing of typedefs defined in classes."), NULL
,
738 set_print_type_typedefs
,
739 show_print_type_typedefs
,
740 &setprinttypelist
, &showprinttypelist
);
742 add_setshow_zuinteger_unlimited_cmd ("nested-type-limit", no_class
,
743 &print_nested_type_limit
,
745 Set the number of recursive nested type definitions to print \
746 (\"unlimited\" or -1 to show all)."), _("\
747 Show the number of recursive nested type definitions to print."), NULL
,
748 set_print_type_nested_types
,
749 show_print_type_nested_types
,
750 &setprinttypelist
, &showprinttypelist
);
753 /* Print <not allocated> status to stream STREAM. */
756 val_print_not_allocated (struct ui_file
*stream
)
758 fprintf_filtered (stream
, _("<not allocated>"));
761 /* Print <not associated> status to stream STREAM. */
764 val_print_not_associated (struct ui_file
*stream
)
766 fprintf_filtered (stream
, _("<not associated>"));