Change program_space::added_solibs to a std::vector
[deliverable/binutils-gdb.git] / gdb / typeprint.c
1 /* Language independent support for printing types for GDB, the GNU debugger.
2
3 Copyright (C) 1986-2018 Free Software Foundation, Inc.
4
5 This file is part of GDB.
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
19
20 #include "defs.h"
21 #include "gdb_obstack.h"
22 #include "bfd.h" /* Binary File Description */
23 #include "symtab.h"
24 #include "gdbtypes.h"
25 #include "expression.h"
26 #include "value.h"
27 #include "gdbcore.h"
28 #include "command.h"
29 #include "gdbcmd.h"
30 #include "target.h"
31 #include "language.h"
32 #include "cp-abi.h"
33 #include "typeprint.h"
34 #include "valprint.h"
35 #include <ctype.h>
36 #include "cli/cli-utils.h"
37 #include "extension.h"
38 #include "completer.h"
39
40 const struct type_print_options type_print_raw_options =
41 {
42 1, /* raw */
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 */
50 };
51
52 /* The default flags for 'ptype' and 'whatis'. */
53
54 static struct type_print_options default_ptype_flags =
55 {
56 0, /* raw */
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 */
64 };
65
66 \f
67
68 /* A hash function for a typedef_field. */
69
70 static hashval_t
71 hash_typedef_field (const void *p)
72 {
73 const struct decl_field *tf = (const struct decl_field *) p;
74 struct type *t = check_typedef (tf->type);
75
76 return htab_hash_string (TYPE_SAFE_NAME (t));
77 }
78
79 /* An equality function for a typedef field. */
80
81 static int
82 eq_typedef_field (const void *a, const void *b)
83 {
84 const struct decl_field *tfa = (const struct decl_field *) a;
85 const struct decl_field *tfb = (const struct decl_field *) b;
86
87 return types_equal (tfa->type, tfb->type);
88 }
89
90 /* See typeprint.h. */
91
92 void
93 typedef_hash_table::recursively_update (struct type *t)
94 {
95 int i;
96
97 for (i = 0; i < TYPE_TYPEDEF_FIELD_COUNT (t); ++i)
98 {
99 struct decl_field *tdef = &TYPE_TYPEDEF_FIELD (t, i);
100 void **slot;
101
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. */
106 if (*slot == NULL)
107 *slot = tdef;
108 }
109
110 /* Recurse into superclasses. */
111 for (i = 0; i < TYPE_N_BASECLASSES (t); ++i)
112 recursively_update (TYPE_BASECLASS (t, i));
113 }
114
115 /* See typeprint.h. */
116
117 void
118 typedef_hash_table::add_template_parameters (struct type *t)
119 {
120 int i;
121
122 for (i = 0; i < TYPE_N_TEMPLATE_ARGUMENTS (t); ++i)
123 {
124 struct decl_field *tf;
125 void **slot;
126
127 /* We only want type-valued template parameters in the hash. */
128 if (SYMBOL_CLASS (TYPE_TEMPLATE_ARGUMENT (t, i)) != LOC_TYPEDEF)
129 continue;
130
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));
134
135 slot = htab_find_slot (m_table, tf, INSERT);
136 if (*slot == NULL)
137 *slot = tf;
138 }
139 }
140
141 /* See typeprint.h. */
142
143 typedef_hash_table::typedef_hash_table ()
144 {
145 m_table = htab_create_alloc (10, hash_typedef_field, eq_typedef_field,
146 NULL, xcalloc, xfree);
147 }
148
149 /* Free a typedef field table. */
150
151 typedef_hash_table::~typedef_hash_table ()
152 {
153 htab_delete (m_table);
154 }
155
156 /* Helper function for typedef_hash_table::copy. */
157
158 static int
159 copy_typedef_hash_element (void **slot, void *nt)
160 {
161 htab_t new_table = (htab_t) nt;
162 void **new_slot;
163
164 new_slot = htab_find_slot (new_table, *slot, INSERT);
165 if (*new_slot == NULL)
166 *new_slot = *slot;
167
168 return 1;
169 }
170
171 /* See typeprint.h. */
172
173 typedef_hash_table::typedef_hash_table (const typedef_hash_table &table)
174 {
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,
178 m_table);
179 }
180
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. */
185
186 const char *
187 typedef_hash_table::find_global_typedef (const struct type_print_options *flags,
188 struct type *t)
189 {
190 char *applied;
191 void **slot;
192 struct decl_field tf, *new_tf;
193
194 if (flags->global_typedefs == NULL)
195 return NULL;
196
197 tf.name = NULL;
198 tf.type = t;
199
200 slot = htab_find_slot (flags->global_typedefs->m_table, &tf, INSERT);
201 if (*slot != NULL)
202 {
203 new_tf = (struct decl_field *) *slot;
204 return new_tf->name;
205 }
206
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);
210 new_tf->name = NULL;
211 new_tf->type = t;
212
213 *slot = new_tf;
214
215 applied = apply_ext_lang_type_printers (flags->global_printers, t);
216
217 if (applied != NULL)
218 {
219 new_tf->name
220 = (const char *) obstack_copy0 (&flags->global_typedefs->m_storage,
221 applied, strlen (applied));
222 xfree (applied);
223 }
224
225 return new_tf->name;
226 }
227
228 /* See typeprint.h. */
229
230 const char *
231 typedef_hash_table::find_typedef (const struct type_print_options *flags,
232 struct type *t)
233 {
234 if (flags->local_typedefs != NULL)
235 {
236 struct decl_field tf, *found;
237
238 tf.name = NULL;
239 tf.type = t;
240 found = (struct decl_field *) htab_find (flags->local_typedefs->m_table,
241 &tf);
242
243 if (found != NULL)
244 return found->name;
245 }
246
247 return find_global_typedef (flags, t);
248 }
249
250 \f
251
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. */
255
256 void
257 typedef_print (struct type *type, struct symbol *newobj, struct ui_file *stream)
258 {
259 LA_PRINT_TYPEDEF (type, newobj, stream);
260 }
261
262 /* The default way to print a typedef. */
263
264 void
265 default_print_typedef (struct type *type, struct symbol *new_symbol,
266 struct ui_file *stream)
267 {
268 error (_("Language not supported."));
269 }
270
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. */
277
278 void
279 type_print (struct type *type, const char *varstring, struct ui_file *stream,
280 int show)
281 {
282 LA_PRINT_TYPE (type, varstring, stream, show, 0, &default_ptype_flags);
283 }
284
285 /* Print TYPE to a string, returning it. The caller is responsible for
286 freeing the string. */
287
288 std::string
289 type_to_string (struct type *type)
290 {
291 TRY
292 {
293 string_file stb;
294
295 type_print (type, "", &stb, -1);
296 return std::move (stb.string ());
297 }
298 CATCH (except, RETURN_MASK_ALL)
299 {
300 }
301 END_CATCH
302
303 return {};
304 }
305
306 /* See typeprint.h. */
307
308 void
309 type_print_unknown_return_type (struct ui_file *stream)
310 {
311 fprintf_filtered (stream, _("<unknown return type>"));
312 }
313
314 /* See typeprint.h. */
315
316 void
317 error_unknown_type (const char *sym_print_name)
318 {
319 error (_("'%s' has unknown type; cast it to its declared type"),
320 sym_print_name);
321 }
322
323 /* Print type of EXP, or last thing in value history if EXP == NULL.
324 show is passed to type_print. */
325
326 static void
327 whatis_exp (const char *exp, int show)
328 {
329 struct value *val;
330 struct type *real_type = NULL;
331 struct type *type;
332 int full = 0;
333 LONGEST top = -1;
334 int using_enc = 0;
335 struct value_print_options opts;
336 struct type_print_options flags = default_ptype_flags;
337
338 if (exp)
339 {
340 if (*exp == '/')
341 {
342 int seen_one = 0;
343
344 for (++exp; *exp && !isspace (*exp); ++exp)
345 {
346 switch (*exp)
347 {
348 case 'r':
349 flags.raw = 1;
350 break;
351 case 'm':
352 flags.print_methods = 0;
353 break;
354 case 'M':
355 flags.print_methods = 1;
356 break;
357 case 't':
358 flags.print_typedefs = 0;
359 break;
360 case 'T':
361 flags.print_typedefs = 1;
362 break;
363 case 'o':
364 {
365 /* Filter out languages which don't implement the
366 feature. */
367 if (show > 0
368 && (current_language->la_language == language_c
369 || current_language->la_language == language_cplus))
370 {
371 flags.print_offsets = 1;
372 flags.print_typedefs = 0;
373 flags.print_methods = 0;
374 }
375 break;
376 }
377 default:
378 error (_("unrecognized flag '%c'"), *exp);
379 }
380 seen_one = 1;
381 }
382
383 if (!*exp && !seen_one)
384 error (_("flag expected"));
385 if (!isspace (*exp))
386 error (_("expected space after format"));
387 exp = skip_spaces (exp);
388 }
389
390 expression_up expr = parse_expression (exp);
391
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
398 typedefs. */
399 if (show == -1 && expr->elts[0].opcode == OP_TYPE)
400 {
401 /* The user expression names a type directly. */
402 type = expr->elts[1].type;
403
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);
410
411 /* If the expression is actually a type, then there's no
412 value to fetch the dynamic type from. */
413 val = NULL;
414 }
415 else
416 {
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);
422 }
423 }
424 else
425 {
426 val = access_value_history (0);
427 type = value_type (val);
428 }
429
430 get_user_print_options (&opts);
431 if (val != NULL && opts.objectprint)
432 {
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);
438 }
439
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 */ ");
444
445 printf_filtered ("type = ");
446
447 std::unique_ptr<typedef_hash_table> table_holder;
448 std::unique_ptr<ext_lang_type_printers> printer_holder;
449 if (!flags.raw)
450 {
451 table_holder.reset (new typedef_hash_table);
452 flags.global_typedefs = table_holder.get ();
453
454 printer_holder.reset (new ext_lang_type_printers);
455 flags.global_printers = printer_holder.get ();
456 }
457
458 if (real_type)
459 {
460 printf_filtered ("/* real type = ");
461 type_print (real_type, "", gdb_stdout, -1);
462 if (! full)
463 printf_filtered (" (incomplete object)");
464 printf_filtered (" */\n");
465 }
466
467 LA_PRINT_TYPE (type, "", gdb_stdout, show, 0, &flags);
468 printf_filtered ("\n");
469 }
470
471 static void
472 whatis_command (const char *exp, int from_tty)
473 {
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);
478 }
479
480 /* TYPENAME is either the name of a type, or an expression. */
481
482 static void
483 ptype_command (const char *type_name, int from_tty)
484 {
485 whatis_exp (type_name, 1);
486 }
487
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.
493
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. */
499
500 void
501 print_type_scalar (struct type *type, LONGEST val, struct ui_file *stream)
502 {
503 unsigned int i;
504 unsigned len;
505
506 type = check_typedef (type);
507
508 switch (TYPE_CODE (type))
509 {
510
511 case TYPE_CODE_ENUM:
512 len = TYPE_NFIELDS (type);
513 for (i = 0; i < len; i++)
514 {
515 if (TYPE_FIELD_ENUMVAL (type, i) == val)
516 {
517 break;
518 }
519 }
520 if (i < len)
521 {
522 fputs_filtered (TYPE_FIELD_NAME (type, i), stream);
523 }
524 else
525 {
526 print_longest (stream, 'd', 0, val);
527 }
528 break;
529
530 case TYPE_CODE_INT:
531 print_longest (stream, TYPE_UNSIGNED (type) ? 'u' : 'd', 0, val);
532 break;
533
534 case TYPE_CODE_CHAR:
535 LA_PRINT_CHAR ((unsigned char) val, type, stream);
536 break;
537
538 case TYPE_CODE_BOOL:
539 fprintf_filtered (stream, val ? "TRUE" : "FALSE");
540 break;
541
542 case TYPE_CODE_RANGE:
543 print_type_scalar (TYPE_TARGET_TYPE (type), val, stream);
544 return;
545
546 case TYPE_CODE_UNDEF:
547 case TYPE_CODE_PTR:
548 case TYPE_CODE_ARRAY:
549 case TYPE_CODE_STRUCT:
550 case TYPE_CODE_UNION:
551 case TYPE_CODE_FUNC:
552 case TYPE_CODE_FLT:
553 case TYPE_CODE_VOID:
554 case TYPE_CODE_SET:
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:
560 case TYPE_CODE_REF:
561 case TYPE_CODE_RVALUE_REF:
562 case TYPE_CODE_NAMESPACE:
563 error (_("internal error: unhandled type in print_type_scalar"));
564 break;
565
566 default:
567 error (_("Invalid type code in symbol table."));
568 }
569 gdb_flush (stream);
570 }
571
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(). */
575
576 void
577 maintenance_print_type (const char *type_name, int from_tty)
578 {
579 struct value *val;
580 struct type *type;
581
582 if (type_name != NULL)
583 {
584 expression_up expr = parse_expression (type_name);
585 if (expr->elts[0].opcode == OP_TYPE)
586 {
587 /* The user expression names a type directly, just use that type. */
588 type = expr->elts[1].type;
589 }
590 else
591 {
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);
596 }
597 if (type != NULL)
598 {
599 recursive_dump_type (type, 0);
600 }
601 }
602 }
603 \f
604
605 struct cmd_list_element *setprinttypelist;
606
607 struct cmd_list_element *showprinttypelist;
608
609 static void
610 set_print_type (const char *arg, int from_tty)
611 {
612 printf_unfiltered (
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);
615 }
616
617 static void
618 show_print_type (const char *args, int from_tty)
619 {
620 cmd_show_list (showprinttypelist, from_tty, "");
621 }
622
623 static int print_methods = 1;
624
625 static void
626 set_print_type_methods (const char *args,
627 int from_tty, struct cmd_list_element *c)
628 {
629 default_ptype_flags.print_methods = print_methods;
630 }
631
632 static void
633 show_print_type_methods (struct ui_file *file, int from_tty,
634 struct cmd_list_element *c, const char *value)
635 {
636 fprintf_filtered (file, _("Printing of methods defined in a class in %s\n"),
637 value);
638 }
639
640 static int print_typedefs = 1;
641
642 static void
643 set_print_type_typedefs (const char *args,
644 int from_tty, struct cmd_list_element *c)
645 {
646 default_ptype_flags.print_typedefs = print_typedefs;
647 }
648
649 static void
650 show_print_type_typedefs (struct ui_file *file, int from_tty,
651 struct cmd_list_element *c, const char *value)
652 {
653 fprintf_filtered (file, _("Printing of typedefs defined in a class in %s\n"),
654 value);
655 }
656
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. */
660
661 static int print_nested_type_limit = 0;
662
663 /* Set how many nested type definitions should be printed by the type
664 printer. */
665
666 static void
667 set_print_type_nested_types (const char *args, int from_tty,
668 struct cmd_list_element *c)
669 {
670 default_ptype_flags.print_nested_type_limit = print_nested_type_limit;
671 }
672
673 /* Show how many nested type definitions the type printer will print. */
674
675 static void
676 show_print_type_nested_types (struct ui_file *file, int from_tty,
677 struct cmd_list_element *c, const char *value)
678 {
679 if (*value == '0')
680 {
681 fprintf_filtered (file,
682 _("Will not print nested types defined in a class\n"));
683 }
684 else
685 {
686 fprintf_filtered (file,
687 _("Will print %s nested types defined in a class\n"),
688 value);
689 }
690 }
691
692 void
693 _initialize_typeprint (void)
694 {
695 struct cmd_list_element *c;
696
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\
705 \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);
714
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);
719
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);
726
727 add_setshow_boolean_cmd ("methods", no_class, &print_methods,
728 _("\
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,
735 _("\
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);
741
742 add_setshow_zuinteger_unlimited_cmd ("nested-type-limit", no_class,
743 &print_nested_type_limit,
744 _("\
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);
751 }
752
753 /* Print <not allocated> status to stream STREAM. */
754
755 void
756 val_print_not_allocated (struct ui_file *stream)
757 {
758 fprintf_filtered (stream, _("<not allocated>"));
759 }
760
761 /* Print <not associated> status to stream STREAM. */
762
763 void
764 val_print_not_associated (struct ui_file *stream)
765 {
766 fprintf_filtered (stream, _("<not associated>"));
767 }
This page took 0.044795 seconds and 4 git commands to generate.