1 /* Language independent support for printing types for GDB, the GNU debugger.
3 Copyright (C) 1986-2017 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 static void ptype_command (char *, int);
42 static void whatis_command (char *, int);
44 static void whatis_exp (char *, int);
46 const struct type_print_options type_print_raw_options
=
49 1, /* print_methods */
50 1, /* print_typedefs */
51 NULL
, /* local_typedefs */
52 NULL
, /* global_table */
53 NULL
/* global_printers */
56 /* The default flags for 'ptype' and 'whatis'. */
58 static struct type_print_options default_ptype_flags
=
61 1, /* print_methods */
62 1, /* print_typedefs */
63 NULL
, /* local_typedefs */
64 NULL
, /* global_table */
65 NULL
/* global_printers */
70 /* A hash table holding typedef_field objects. This is more
71 complicated than an ordinary hash because it must also track the
72 lifetime of some -- but not all -- of the contained objects. */
74 struct typedef_hash_table
76 /* The actual hash table. */
79 /* Storage for typedef_field objects that must be synthesized. */
80 struct obstack storage
;
83 /* A hash function for a typedef_field. */
86 hash_typedef_field (const void *p
)
88 const struct typedef_field
*tf
= (const struct typedef_field
*) p
;
89 struct type
*t
= check_typedef (tf
->type
);
91 return htab_hash_string (TYPE_SAFE_NAME (t
));
94 /* An equality function for a typedef field. */
97 eq_typedef_field (const void *a
, const void *b
)
99 const struct typedef_field
*tfa
= (const struct typedef_field
*) a
;
100 const struct typedef_field
*tfb
= (const struct typedef_field
*) b
;
102 return types_equal (tfa
->type
, tfb
->type
);
105 /* Add typedefs from T to the hash table TABLE. */
108 recursively_update_typedef_hash (struct typedef_hash_table
*table
,
116 for (i
= 0; i
< TYPE_TYPEDEF_FIELD_COUNT (t
); ++i
)
118 struct typedef_field
*tdef
= &TYPE_TYPEDEF_FIELD (t
, i
);
121 slot
= htab_find_slot (table
->table
, tdef
, INSERT
);
122 /* Only add a given typedef name once. Really this shouldn't
123 happen; but it is safe enough to do the updates breadth-first
124 and thus use the most specific typedef. */
129 /* Recurse into superclasses. */
130 for (i
= 0; i
< TYPE_N_BASECLASSES (t
); ++i
)
131 recursively_update_typedef_hash (table
, TYPE_BASECLASS (t
, i
));
134 /* Add template parameters from T to the typedef hash TABLE. */
137 add_template_parameters (struct typedef_hash_table
*table
, struct type
*t
)
144 for (i
= 0; i
< TYPE_N_TEMPLATE_ARGUMENTS (t
); ++i
)
146 struct typedef_field
*tf
;
149 /* We only want type-valued template parameters in the hash. */
150 if (SYMBOL_CLASS (TYPE_TEMPLATE_ARGUMENT (t
, i
)) != LOC_TYPEDEF
)
153 tf
= XOBNEW (&table
->storage
, struct typedef_field
);
154 tf
->name
= SYMBOL_LINKAGE_NAME (TYPE_TEMPLATE_ARGUMENT (t
, i
));
155 tf
->type
= SYMBOL_TYPE (TYPE_TEMPLATE_ARGUMENT (t
, i
));
157 slot
= htab_find_slot (table
->table
, tf
, INSERT
);
163 /* Create a new typedef-lookup hash table. */
165 struct typedef_hash_table
*
166 create_typedef_hash (void)
168 struct typedef_hash_table
*result
;
170 result
= XNEW (struct typedef_hash_table
);
171 result
->table
= htab_create_alloc (10, hash_typedef_field
, eq_typedef_field
,
172 NULL
, xcalloc
, xfree
);
173 obstack_init (&result
->storage
);
178 /* Free a typedef field table. */
181 free_typedef_hash (struct typedef_hash_table
*table
)
185 htab_delete (table
->table
);
186 obstack_free (&table
->storage
, NULL
);
191 /* A cleanup for freeing a typedef_hash_table. */
194 do_free_typedef_hash (void *arg
)
196 free_typedef_hash ((struct typedef_hash_table
*) arg
);
199 /* Return a new cleanup that frees TABLE. */
202 make_cleanup_free_typedef_hash (struct typedef_hash_table
*table
)
204 return make_cleanup (do_free_typedef_hash
, table
);
207 /* Helper function for copy_typedef_hash. */
210 copy_typedef_hash_element (void **slot
, void *nt
)
212 htab_t new_table
= (htab_t
) nt
;
215 new_slot
= htab_find_slot (new_table
, *slot
, INSERT
);
216 if (*new_slot
== NULL
)
222 /* Copy a typedef hash. */
224 struct typedef_hash_table
*
225 copy_typedef_hash (struct typedef_hash_table
*table
)
227 struct typedef_hash_table
*result
;
232 result
= create_typedef_hash ();
233 htab_traverse_noresize (table
->table
, copy_typedef_hash_element
,
238 /* A cleanup to free the global typedef hash. */
241 do_free_global_table (void *arg
)
243 struct type_print_options
*flags
= (struct type_print_options
*) arg
;
245 free_typedef_hash (flags
->global_typedefs
);
246 free_ext_lang_type_printers (flags
->global_printers
);
249 /* Create the global typedef hash. */
251 static struct cleanup
*
252 create_global_typedef_table (struct type_print_options
*flags
)
254 gdb_assert (flags
->global_typedefs
== NULL
&& flags
->global_printers
== NULL
);
255 flags
->global_typedefs
= create_typedef_hash ();
256 flags
->global_printers
= start_ext_lang_type_printers ();
257 return make_cleanup (do_free_global_table
, flags
);
260 /* Look up the type T in the global typedef hash. If it is found,
261 return the typedef name. If it is not found, apply the
262 type-printers, if any, given by start_script_type_printers and return the
263 result. A NULL return means that the name was not found. */
266 find_global_typedef (const struct type_print_options
*flags
,
271 struct typedef_field tf
, *new_tf
;
273 if (flags
->global_typedefs
== NULL
)
279 slot
= htab_find_slot (flags
->global_typedefs
->table
, &tf
, INSERT
);
282 new_tf
= (struct typedef_field
*) *slot
;
286 /* Put an entry into the hash table now, in case
287 apply_ext_lang_type_printers recurses. */
288 new_tf
= XOBNEW (&flags
->global_typedefs
->storage
, struct typedef_field
);
294 applied
= apply_ext_lang_type_printers (flags
->global_printers
, t
);
299 = (const char *) obstack_copy0 (&flags
->global_typedefs
->storage
,
300 applied
, strlen (applied
));
307 /* Look up the type T in the typedef hash table in with FLAGS. If T
308 is in the table, return its short (class-relative) typedef name.
309 Otherwise return NULL. If the table is NULL, this always returns
313 find_typedef_in_hash (const struct type_print_options
*flags
, struct type
*t
)
315 if (flags
->local_typedefs
!= NULL
)
317 struct typedef_field tf
, *found
;
321 found
= (struct typedef_field
*) htab_find (flags
->local_typedefs
->table
,
328 return find_global_typedef (flags
, t
);
333 /* Print a description of a type in the format of a
334 typedef for the current language.
335 NEW is the new name for a type TYPE. */
338 typedef_print (struct type
*type
, struct symbol
*newobj
, struct ui_file
*stream
)
340 LA_PRINT_TYPEDEF (type
, newobj
, stream
);
343 /* The default way to print a typedef. */
346 default_print_typedef (struct type
*type
, struct symbol
*new_symbol
,
347 struct ui_file
*stream
)
349 error (_("Language not supported."));
352 /* Print a description of a type TYPE in the form of a declaration of a
353 variable named VARSTRING. (VARSTRING is demangled if necessary.)
354 Output goes to STREAM (via stdio).
355 If SHOW is positive, we show the contents of the outermost level
356 of structure even if there is a type name that could be used instead.
357 If SHOW is negative, we never show the details of elements' types. */
360 type_print (struct type
*type
, const char *varstring
, struct ui_file
*stream
,
363 LA_PRINT_TYPE (type
, varstring
, stream
, show
, 0, &default_ptype_flags
);
366 /* Print TYPE to a string, returning it. The caller is responsible for
367 freeing the string. */
370 type_to_string (struct type
*type
)
376 type_print (type
, "", &stb
, -1);
377 return std::move (stb
.string ());
379 CATCH (except
, RETURN_MASK_ALL
)
387 /* See typeprint.h. */
390 type_print_unknown_return_type (struct ui_file
*stream
)
392 fprintf_filtered (stream
, _("<unknown return type>"));
395 /* See typeprint.h. */
398 error_unknown_type (const char *sym_print_name
)
400 error (_("'%s' has unknown type; cast it to its declared type"),
404 /* Print type of EXP, or last thing in value history if EXP == NULL.
405 show is passed to type_print. */
408 whatis_exp (char *exp
, int show
)
411 struct cleanup
*old_chain
;
412 struct type
*real_type
= NULL
;
417 struct value_print_options opts
;
418 struct type_print_options flags
= default_ptype_flags
;
420 old_chain
= make_cleanup (null_cleanup
, NULL
);
428 for (++exp
; *exp
&& !isspace (*exp
); ++exp
)
436 flags
.print_methods
= 0;
439 flags
.print_methods
= 1;
442 flags
.print_typedefs
= 0;
445 flags
.print_typedefs
= 1;
448 error (_("unrecognized flag '%c'"), *exp
);
453 if (!*exp
&& !seen_one
)
454 error (_("flag expected"));
456 error (_("expected space after format"));
457 exp
= skip_spaces (exp
);
460 expression_up expr
= parse_expression (exp
);
462 /* The behavior of "whatis" depends on whether the user
463 expression names a type directly, or a language expression
464 (including variable names). If the former, then "whatis"
465 strips one level of typedefs, only. If an expression,
466 "whatis" prints the type of the expression without stripping
467 any typedef level. "ptype" always strips all levels of
469 if (show
== -1 && expr
->elts
[0].opcode
== OP_TYPE
)
471 /* The user expression names a type directly. */
472 type
= expr
->elts
[1].type
;
474 /* If this is a typedef, then find its immediate target.
475 Use check_typedef to resolve stubs, but ignore its result
476 because we do not want to dig past all typedefs. */
477 check_typedef (type
);
478 if (TYPE_CODE (type
) == TYPE_CODE_TYPEDEF
)
479 type
= TYPE_TARGET_TYPE (type
);
483 /* The user expression names a type indirectly by naming an
484 object or expression of that type. Find that
485 indirectly-named type. */
486 val
= evaluate_type (expr
.get ());
487 type
= value_type (val
);
492 val
= access_value_history (0);
493 type
= value_type (val
);
496 get_user_print_options (&opts
);
497 if (opts
.objectprint
)
499 if (((TYPE_CODE (type
) == TYPE_CODE_PTR
) || TYPE_IS_REFERENCE (type
))
500 && (TYPE_CODE (TYPE_TARGET_TYPE (type
)) == TYPE_CODE_STRUCT
))
501 real_type
= value_rtti_indirect_type (val
, &full
, &top
, &using_enc
);
502 else if (TYPE_CODE (type
) == TYPE_CODE_STRUCT
)
503 real_type
= value_rtti_type (val
, &full
, &top
, &using_enc
);
506 printf_filtered ("type = ");
509 create_global_typedef_table (&flags
);
513 printf_filtered ("/* real type = ");
514 type_print (real_type
, "", gdb_stdout
, -1);
516 printf_filtered (" (incomplete object)");
517 printf_filtered (" */\n");
520 LA_PRINT_TYPE (type
, "", gdb_stdout
, show
, 0, &flags
);
521 printf_filtered ("\n");
523 do_cleanups (old_chain
);
527 whatis_command (char *exp
, int from_tty
)
529 /* Most of the time users do not want to see all the fields
530 in a structure. If they do they can use the "ptype" command.
531 Hence the "-1" below. */
532 whatis_exp (exp
, -1);
535 /* TYPENAME is either the name of a type, or an expression. */
538 ptype_command (char *type_name
, int from_tty
)
540 whatis_exp (type_name
, 1);
543 /* Print integral scalar data VAL, of type TYPE, onto stdio stream STREAM.
544 Used to print data from type structures in a specified type. For example,
545 array bounds may be characters or booleans in some languages, and this
546 allows the ranges to be printed in their "natural" form rather than as
547 decimal integer values.
549 FIXME: This is here simply because only the type printing routines
550 currently use it, and it wasn't clear if it really belonged somewhere
551 else (like printcmd.c). There are a lot of other gdb routines that do
552 something similar, but they are generally concerned with printing values
553 that come from the inferior in target byte order and target size. */
556 print_type_scalar (struct type
*type
, LONGEST val
, struct ui_file
*stream
)
561 type
= check_typedef (type
);
563 switch (TYPE_CODE (type
))
567 len
= TYPE_NFIELDS (type
);
568 for (i
= 0; i
< len
; i
++)
570 if (TYPE_FIELD_ENUMVAL (type
, i
) == val
)
577 fputs_filtered (TYPE_FIELD_NAME (type
, i
), stream
);
581 print_longest (stream
, 'd', 0, val
);
586 print_longest (stream
, TYPE_UNSIGNED (type
) ? 'u' : 'd', 0, val
);
590 LA_PRINT_CHAR ((unsigned char) val
, type
, stream
);
594 fprintf_filtered (stream
, val
? "TRUE" : "FALSE");
597 case TYPE_CODE_RANGE
:
598 print_type_scalar (TYPE_TARGET_TYPE (type
), val
, stream
);
601 case TYPE_CODE_UNDEF
:
603 case TYPE_CODE_ARRAY
:
604 case TYPE_CODE_STRUCT
:
605 case TYPE_CODE_UNION
:
610 case TYPE_CODE_STRING
:
611 case TYPE_CODE_ERROR
:
612 case TYPE_CODE_MEMBERPTR
:
613 case TYPE_CODE_METHODPTR
:
614 case TYPE_CODE_METHOD
:
616 case TYPE_CODE_RVALUE_REF
:
617 case TYPE_CODE_NAMESPACE
:
618 error (_("internal error: unhandled type in print_type_scalar"));
622 error (_("Invalid type code in symbol table."));
627 /* Dump details of a type specified either directly or indirectly.
628 Uses the same sort of type lookup mechanism as ptype_command()
629 and whatis_command(). */
632 maintenance_print_type (const char *type_name
, int from_tty
)
637 if (type_name
!= NULL
)
639 expression_up expr
= parse_expression (type_name
);
640 if (expr
->elts
[0].opcode
== OP_TYPE
)
642 /* The user expression names a type directly, just use that type. */
643 type
= expr
->elts
[1].type
;
647 /* The user expression may name a type indirectly by naming an
648 object of that type. Find that indirectly named type. */
649 val
= evaluate_type (expr
.get ());
650 type
= value_type (val
);
654 recursive_dump_type (type
, 0);
660 struct cmd_list_element
*setprinttypelist
;
662 struct cmd_list_element
*showprinttypelist
;
665 set_print_type (char *arg
, int from_tty
)
668 "\"set print type\" must be followed by the name of a subcommand.\n");
669 help_list (setprintlist
, "set print type ", all_commands
, gdb_stdout
);
673 show_print_type (char *args
, int from_tty
)
675 cmd_show_list (showprinttypelist
, from_tty
, "");
678 static int print_methods
= 1;
681 set_print_type_methods (char *args
, int from_tty
, struct cmd_list_element
*c
)
683 default_ptype_flags
.print_methods
= print_methods
;
687 show_print_type_methods (struct ui_file
*file
, int from_tty
,
688 struct cmd_list_element
*c
, const char *value
)
690 fprintf_filtered (file
, _("Printing of methods defined in a class in %s\n"),
694 static int print_typedefs
= 1;
697 set_print_type_typedefs (char *args
, int from_tty
, struct cmd_list_element
*c
)
699 default_ptype_flags
.print_typedefs
= print_typedefs
;
703 show_print_type_typedefs (struct ui_file
*file
, int from_tty
,
704 struct cmd_list_element
*c
, const char *value
)
706 fprintf_filtered (file
, _("Printing of typedefs defined in a class in %s\n"),
711 _initialize_typeprint (void)
713 struct cmd_list_element
*c
;
715 c
= add_com ("ptype", class_vars
, ptype_command
, _("\
716 Print definition of type TYPE.\n\
717 Usage: ptype[/FLAGS] TYPE | EXPRESSION\n\
718 Argument may be any type (for example a type name defined by typedef,\n\
719 or \"struct STRUCT-TAG\" or \"class CLASS-NAME\" or \"union UNION-TAG\"\n\
720 or \"enum ENUM-TAG\") or an expression.\n\
721 The selected stack frame's lexical context is used to look up the name.\n\
722 Contrary to \"whatis\", \"ptype\" always unrolls any typedefs.\n\
724 Available FLAGS are:\n\
725 /r print in \"raw\" form; do not substitute typedefs\n\
726 /m do not print methods defined in a class\n\
727 /M print methods defined in a class\n\
728 /t do not print typedefs defined in a class\n\
729 /T print typedefs defined in a class"));
730 set_cmd_completer (c
, expression_completer
);
732 c
= add_com ("whatis", class_vars
, whatis_command
,
733 _("Print data type of expression EXP.\n\
734 Only one level of typedefs is unrolled. See also \"ptype\"."));
735 set_cmd_completer (c
, expression_completer
);
737 add_prefix_cmd ("type", no_class
, show_print_type
,
738 _("Generic command for showing type-printing settings."),
739 &showprinttypelist
, "show print type ", 0, &showprintlist
);
740 add_prefix_cmd ("type", no_class
, set_print_type
,
741 _("Generic command for setting how types print."),
742 &setprinttypelist
, "show print type ", 0, &setprintlist
);
744 add_setshow_boolean_cmd ("methods", no_class
, &print_methods
,
746 Set printing of methods defined in classes."), _("\
747 Show printing of methods defined in classes."), NULL
,
748 set_print_type_methods
,
749 show_print_type_methods
,
750 &setprinttypelist
, &showprinttypelist
);
751 add_setshow_boolean_cmd ("typedefs", no_class
, &print_typedefs
,
753 Set printing of typedefs defined in classes."), _("\
754 Show printing of typedefs defined in classes."), NULL
,
755 set_print_type_typedefs
,
756 show_print_type_typedefs
,
757 &setprinttypelist
, &showprinttypelist
);
760 /* Print <not allocated> status to stream STREAM. */
763 val_print_not_allocated (struct ui_file
*stream
)
765 fprintf_filtered (stream
, _("<not allocated>"));
768 /* Print <not associated> status to stream STREAM. */
771 val_print_not_associated (struct ui_file
*stream
)
773 fprintf_filtered (stream
, _("<not associated>"));