Commit | Line | Data |
---|---|---|
c906108c | 1 | /* Language independent support for printing types for GDB, the GNU debugger. |
1bac305b | 2 | |
e2882c85 | 3 | Copyright (C) 1986-2018 Free Software Foundation, Inc. |
c906108c | 4 | |
c5aa993b | 5 | This file is part of GDB. |
c906108c | 6 | |
c5aa993b JM |
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 | |
a9762ec7 | 9 | the Free Software Foundation; either version 3 of the License, or |
c5aa993b | 10 | (at your option) any later version. |
c906108c | 11 | |
c5aa993b JM |
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. | |
c906108c | 16 | |
c5aa993b | 17 | You should have received a copy of the GNU General Public License |
a9762ec7 | 18 | along with this program. If not, see <http://www.gnu.org/licenses/>. */ |
c906108c SS |
19 | |
20 | #include "defs.h" | |
04ea0df1 | 21 | #include "gdb_obstack.h" |
c906108c SS |
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" | |
015a42b4 | 32 | #include "cp-abi.h" |
b9362cc7 | 33 | #include "typeprint.h" |
79a45b7d | 34 | #include "valprint.h" |
53342f27 TT |
35 | #include <ctype.h> |
36 | #include "cli/cli-utils.h" | |
6dddc817 | 37 | #include "extension.h" |
4fc5d43e | 38 | #include "completer.h" |
c906108c | 39 | |
79d43c61 TT |
40 | const struct type_print_options type_print_raw_options = |
41 | { | |
53342f27 TT |
42 | 1, /* raw */ |
43 | 1, /* print_methods */ | |
bd69fc68 | 44 | 1, /* print_typedefs */ |
7c161838 | 45 | 0, /* print_offsets */ |
883fd55a | 46 | 0, /* print_nested_type_limit */ |
18a9fc12 TT |
47 | NULL, /* local_typedefs */ |
48 | NULL, /* global_table */ | |
49 | NULL /* global_printers */ | |
79d43c61 TT |
50 | }; |
51 | ||
52 | /* The default flags for 'ptype' and 'whatis'. */ | |
53 | ||
54 | static struct type_print_options default_ptype_flags = | |
55 | { | |
53342f27 TT |
56 | 0, /* raw */ |
57 | 1, /* print_methods */ | |
bd69fc68 | 58 | 1, /* print_typedefs */ |
7c161838 | 59 | 0, /* print_offsets */ |
883fd55a | 60 | 0, /* print_nested_type_limit */ |
18a9fc12 TT |
61 | NULL, /* local_typedefs */ |
62 | NULL, /* global_table */ | |
63 | NULL /* global_printers */ | |
79d43c61 | 64 | }; |
5c6ce71d | 65 | |
53342f27 TT |
66 | \f |
67 | ||
e0c547d1 TT |
68 | /* See typeprint.h. */ |
69 | ||
70 | const int print_offset_data::indentation = 23; | |
71 | ||
72 | ||
73 | /* See typeprint.h. */ | |
74 | ||
75 | void | |
76 | print_offset_data::maybe_print_hole (struct ui_file *stream, | |
77 | unsigned int bitpos, | |
78 | const char *for_what) | |
79 | { | |
80 | /* We check for END_BITPOS > 0 because there is a specific | |
81 | scenario when END_BITPOS can be zero and BITPOS can be > | |
82 | 0: when we are dealing with a struct/class with a virtual method. | |
83 | Because of the vtable, the first field of the struct/class will | |
84 | have an offset of sizeof (void *) (the size of the vtable). If | |
85 | we do not check for END_BITPOS > 0 here, GDB will report | |
86 | a hole before the first field, which is not accurate. */ | |
87 | if (end_bitpos > 0 && end_bitpos < bitpos) | |
88 | { | |
89 | /* If END_BITPOS is smaller than the current type's | |
90 | bitpos, it means there's a hole in the struct, so we report | |
91 | it here. */ | |
92 | unsigned int hole = bitpos - end_bitpos; | |
93 | unsigned int hole_byte = hole / TARGET_CHAR_BIT; | |
94 | unsigned int hole_bit = hole % TARGET_CHAR_BIT; | |
95 | ||
96 | if (hole_bit > 0) | |
97 | fprintf_filtered (stream, "/* XXX %2u-bit %s */\n", hole_bit, | |
98 | for_what); | |
99 | ||
100 | if (hole_byte > 0) | |
101 | fprintf_filtered (stream, "/* XXX %2u-byte %s */\n", hole_byte, | |
102 | for_what); | |
103 | } | |
104 | } | |
105 | ||
106 | /* See typeprint.h. */ | |
107 | ||
108 | void | |
109 | print_offset_data::update (struct type *type, unsigned int field_idx, | |
110 | struct ui_file *stream) | |
111 | { | |
112 | if (field_is_static (&TYPE_FIELD (type, field_idx))) | |
113 | { | |
114 | print_spaces_filtered (indentation, stream); | |
115 | return; | |
116 | } | |
117 | ||
118 | struct type *ftype = check_typedef (TYPE_FIELD_TYPE (type, field_idx)); | |
119 | if (TYPE_CODE (type) == TYPE_CODE_UNION) | |
120 | { | |
121 | /* Since union fields don't have the concept of offsets, we just | |
122 | print their sizes. */ | |
123 | fprintf_filtered (stream, "/* %4u */", TYPE_LENGTH (ftype)); | |
124 | return; | |
125 | } | |
126 | ||
127 | unsigned int bitpos = TYPE_FIELD_BITPOS (type, field_idx); | |
128 | unsigned int fieldsize_byte = TYPE_LENGTH (ftype); | |
129 | unsigned int fieldsize_bit = fieldsize_byte * TARGET_CHAR_BIT; | |
130 | ||
131 | maybe_print_hole (stream, bitpos, "hole"); | |
132 | ||
133 | if (TYPE_FIELD_PACKED (type, field_idx)) | |
134 | { | |
135 | /* We're dealing with a bitfield. Print how many bits are left | |
136 | to be used. */ | |
137 | unsigned int bitsize = TYPE_FIELD_BITSIZE (type, field_idx); | |
138 | /* The bitpos relative to the beginning of our container | |
139 | field. */ | |
140 | unsigned int relative_bitpos; | |
141 | ||
142 | /* The following was copied from | |
143 | value.c:value_primitive_field. */ | |
144 | if ((bitpos % fieldsize_bit) + bitsize <= fieldsize_bit) | |
145 | relative_bitpos = bitpos % fieldsize_bit; | |
146 | else | |
147 | relative_bitpos = bitpos % TARGET_CHAR_BIT; | |
148 | ||
149 | /* This is the exact offset (in bits) of this bitfield. */ | |
150 | unsigned int bit_offset | |
151 | = (bitpos - relative_bitpos) + offset_bitpos; | |
152 | ||
153 | /* The position of the field, relative to the beginning of the | |
154 | struct, and how many bits are left to be used in this | |
155 | container. */ | |
156 | fprintf_filtered (stream, "/* %4u:%2u", bit_offset / TARGET_CHAR_BIT, | |
157 | fieldsize_bit - (relative_bitpos + bitsize)); | |
158 | fieldsize_bit = bitsize; | |
159 | } | |
160 | else | |
161 | { | |
162 | /* The position of the field, relative to the beginning of the | |
163 | struct. */ | |
164 | fprintf_filtered (stream, "/* %4u", | |
165 | (bitpos + offset_bitpos) / TARGET_CHAR_BIT); | |
166 | ||
167 | fprintf_filtered (stream, " "); | |
168 | } | |
169 | ||
170 | fprintf_filtered (stream, " | %4u */", fieldsize_byte); | |
171 | ||
172 | end_bitpos = bitpos + fieldsize_bit; | |
173 | } | |
174 | ||
175 | /* See typeprint.h. */ | |
176 | ||
177 | void | |
178 | print_offset_data::finish (struct type *type, int level, | |
179 | struct ui_file *stream) | |
180 | { | |
181 | unsigned int bitpos = TYPE_LENGTH (type) * TARGET_CHAR_BIT; | |
182 | maybe_print_hole (stream, bitpos, "padding"); | |
183 | ||
184 | fputs_filtered ("\n", stream); | |
185 | print_spaces_filtered (level + 4 + print_offset_data::indentation, stream); | |
186 | fprintf_filtered (stream, "/* total size (bytes): %4u */\n", | |
187 | TYPE_LENGTH (type)); | |
188 | } | |
189 | ||
190 | \f | |
191 | ||
bd69fc68 TT |
192 | /* A hash function for a typedef_field. */ |
193 | ||
194 | static hashval_t | |
195 | hash_typedef_field (const void *p) | |
196 | { | |
883fd55a | 197 | const struct decl_field *tf = (const struct decl_field *) p; |
bd69fc68 TT |
198 | struct type *t = check_typedef (tf->type); |
199 | ||
200 | return htab_hash_string (TYPE_SAFE_NAME (t)); | |
201 | } | |
202 | ||
203 | /* An equality function for a typedef field. */ | |
204 | ||
205 | static int | |
206 | eq_typedef_field (const void *a, const void *b) | |
207 | { | |
883fd55a KS |
208 | const struct decl_field *tfa = (const struct decl_field *) a; |
209 | const struct decl_field *tfb = (const struct decl_field *) b; | |
bd69fc68 TT |
210 | |
211 | return types_equal (tfa->type, tfb->type); | |
212 | } | |
213 | ||
c819b2c0 | 214 | /* See typeprint.h. */ |
bd69fc68 TT |
215 | |
216 | void | |
c819b2c0 | 217 | typedef_hash_table::recursively_update (struct type *t) |
bd69fc68 TT |
218 | { |
219 | int i; | |
220 | ||
bd69fc68 TT |
221 | for (i = 0; i < TYPE_TYPEDEF_FIELD_COUNT (t); ++i) |
222 | { | |
883fd55a | 223 | struct decl_field *tdef = &TYPE_TYPEDEF_FIELD (t, i); |
bd69fc68 TT |
224 | void **slot; |
225 | ||
c819b2c0 | 226 | slot = htab_find_slot (m_table, tdef, INSERT); |
bd69fc68 TT |
227 | /* Only add a given typedef name once. Really this shouldn't |
228 | happen; but it is safe enough to do the updates breadth-first | |
229 | and thus use the most specific typedef. */ | |
230 | if (*slot == NULL) | |
231 | *slot = tdef; | |
232 | } | |
233 | ||
234 | /* Recurse into superclasses. */ | |
235 | for (i = 0; i < TYPE_N_BASECLASSES (t); ++i) | |
c819b2c0 | 236 | recursively_update (TYPE_BASECLASS (t, i)); |
bd69fc68 TT |
237 | } |
238 | ||
c819b2c0 | 239 | /* See typeprint.h. */ |
bd69fc68 TT |
240 | |
241 | void | |
c819b2c0 | 242 | typedef_hash_table::add_template_parameters (struct type *t) |
bd69fc68 TT |
243 | { |
244 | int i; | |
245 | ||
bd69fc68 TT |
246 | for (i = 0; i < TYPE_N_TEMPLATE_ARGUMENTS (t); ++i) |
247 | { | |
883fd55a | 248 | struct decl_field *tf; |
bd69fc68 TT |
249 | void **slot; |
250 | ||
251 | /* We only want type-valued template parameters in the hash. */ | |
252 | if (SYMBOL_CLASS (TYPE_TEMPLATE_ARGUMENT (t, i)) != LOC_TYPEDEF) | |
253 | continue; | |
254 | ||
c819b2c0 | 255 | tf = XOBNEW (&m_storage, struct decl_field); |
bd69fc68 TT |
256 | tf->name = SYMBOL_LINKAGE_NAME (TYPE_TEMPLATE_ARGUMENT (t, i)); |
257 | tf->type = SYMBOL_TYPE (TYPE_TEMPLATE_ARGUMENT (t, i)); | |
258 | ||
c819b2c0 | 259 | slot = htab_find_slot (m_table, tf, INSERT); |
bd69fc68 TT |
260 | if (*slot == NULL) |
261 | *slot = tf; | |
262 | } | |
263 | } | |
264 | ||
c819b2c0 | 265 | /* See typeprint.h. */ |
bd69fc68 | 266 | |
c819b2c0 | 267 | typedef_hash_table::typedef_hash_table () |
bd69fc68 | 268 | { |
c819b2c0 TT |
269 | m_table = htab_create_alloc (10, hash_typedef_field, eq_typedef_field, |
270 | NULL, xcalloc, xfree); | |
bd69fc68 TT |
271 | } |
272 | ||
273 | /* Free a typedef field table. */ | |
274 | ||
c819b2c0 | 275 | typedef_hash_table::~typedef_hash_table () |
bd69fc68 | 276 | { |
c819b2c0 | 277 | htab_delete (m_table); |
bd69fc68 TT |
278 | } |
279 | ||
c819b2c0 | 280 | /* Helper function for typedef_hash_table::copy. */ |
bd69fc68 TT |
281 | |
282 | static int | |
283 | copy_typedef_hash_element (void **slot, void *nt) | |
284 | { | |
19ba03f4 | 285 | htab_t new_table = (htab_t) nt; |
bd69fc68 TT |
286 | void **new_slot; |
287 | ||
288 | new_slot = htab_find_slot (new_table, *slot, INSERT); | |
289 | if (*new_slot == NULL) | |
290 | *new_slot = *slot; | |
291 | ||
292 | return 1; | |
293 | } | |
294 | ||
c819b2c0 | 295 | /* See typeprint.h. */ |
18a9fc12 | 296 | |
c819b2c0 | 297 | typedef_hash_table::typedef_hash_table (const typedef_hash_table &table) |
18a9fc12 | 298 | { |
c819b2c0 TT |
299 | m_table = htab_create_alloc (10, hash_typedef_field, eq_typedef_field, |
300 | NULL, xcalloc, xfree); | |
301 | htab_traverse_noresize (table.m_table, copy_typedef_hash_element, | |
302 | m_table); | |
18a9fc12 TT |
303 | } |
304 | ||
305 | /* Look up the type T in the global typedef hash. If it is found, | |
306 | return the typedef name. If it is not found, apply the | |
6dddc817 | 307 | type-printers, if any, given by start_script_type_printers and return the |
18a9fc12 TT |
308 | result. A NULL return means that the name was not found. */ |
309 | ||
c819b2c0 TT |
310 | const char * |
311 | typedef_hash_table::find_global_typedef (const struct type_print_options *flags, | |
312 | struct type *t) | |
18a9fc12 TT |
313 | { |
314 | char *applied; | |
315 | void **slot; | |
883fd55a | 316 | struct decl_field tf, *new_tf; |
18a9fc12 TT |
317 | |
318 | if (flags->global_typedefs == NULL) | |
319 | return NULL; | |
320 | ||
321 | tf.name = NULL; | |
322 | tf.type = t; | |
323 | ||
c819b2c0 | 324 | slot = htab_find_slot (flags->global_typedefs->m_table, &tf, INSERT); |
18a9fc12 TT |
325 | if (*slot != NULL) |
326 | { | |
883fd55a | 327 | new_tf = (struct decl_field *) *slot; |
18a9fc12 TT |
328 | return new_tf->name; |
329 | } | |
330 | ||
9b94fcf1 DE |
331 | /* Put an entry into the hash table now, in case |
332 | apply_ext_lang_type_printers recurses. */ | |
c819b2c0 | 333 | new_tf = XOBNEW (&flags->global_typedefs->m_storage, struct decl_field); |
18a9fc12 TT |
334 | new_tf->name = NULL; |
335 | new_tf->type = t; | |
336 | ||
337 | *slot = new_tf; | |
338 | ||
6dddc817 | 339 | applied = apply_ext_lang_type_printers (flags->global_printers, t); |
18a9fc12 TT |
340 | |
341 | if (applied != NULL) | |
342 | { | |
224c3ddb | 343 | new_tf->name |
c819b2c0 | 344 | = (const char *) obstack_copy0 (&flags->global_typedefs->m_storage, |
224c3ddb | 345 | applied, strlen (applied)); |
18a9fc12 TT |
346 | xfree (applied); |
347 | } | |
348 | ||
349 | return new_tf->name; | |
350 | } | |
351 | ||
c819b2c0 | 352 | /* See typeprint.h. */ |
bd69fc68 TT |
353 | |
354 | const char * | |
c819b2c0 TT |
355 | typedef_hash_table::find_typedef (const struct type_print_options *flags, |
356 | struct type *t) | |
bd69fc68 | 357 | { |
18a9fc12 TT |
358 | if (flags->local_typedefs != NULL) |
359 | { | |
883fd55a | 360 | struct decl_field tf, *found; |
bd69fc68 | 361 | |
18a9fc12 TT |
362 | tf.name = NULL; |
363 | tf.type = t; | |
c819b2c0 | 364 | found = (struct decl_field *) htab_find (flags->local_typedefs->m_table, |
883fd55a | 365 | &tf); |
bd69fc68 | 366 | |
18a9fc12 TT |
367 | if (found != NULL) |
368 | return found->name; | |
369 | } | |
bd69fc68 | 370 | |
18a9fc12 | 371 | return find_global_typedef (flags, t); |
bd69fc68 TT |
372 | } |
373 | ||
374 | \f | |
375 | ||
a5238fbc PM |
376 | /* Print a description of a type in the format of a |
377 | typedef for the current language. | |
c378eb4e | 378 | NEW is the new name for a type TYPE. */ |
a5238fbc PM |
379 | |
380 | void | |
fe978cb0 | 381 | typedef_print (struct type *type, struct symbol *newobj, struct ui_file *stream) |
a5238fbc | 382 | { |
fe978cb0 | 383 | LA_PRINT_TYPEDEF (type, newobj, stream); |
5c6ce71d TT |
384 | } |
385 | ||
386 | /* The default way to print a typedef. */ | |
387 | ||
388 | void | |
389 | default_print_typedef (struct type *type, struct symbol *new_symbol, | |
390 | struct ui_file *stream) | |
391 | { | |
392 | error (_("Language not supported.")); | |
a5238fbc PM |
393 | } |
394 | ||
c906108c SS |
395 | /* Print a description of a type TYPE in the form of a declaration of a |
396 | variable named VARSTRING. (VARSTRING is demangled if necessary.) | |
397 | Output goes to STREAM (via stdio). | |
398 | If SHOW is positive, we show the contents of the outermost level | |
399 | of structure even if there is a type name that could be used instead. | |
400 | If SHOW is negative, we never show the details of elements' types. */ | |
401 | ||
402 | void | |
0d5cff50 | 403 | type_print (struct type *type, const char *varstring, struct ui_file *stream, |
fba45db2 | 404 | int show) |
c906108c | 405 | { |
79d43c61 | 406 | LA_PRINT_TYPE (type, varstring, stream, show, 0, &default_ptype_flags); |
c906108c SS |
407 | } |
408 | ||
ae6a3a4c TJB |
409 | /* Print TYPE to a string, returning it. The caller is responsible for |
410 | freeing the string. */ | |
411 | ||
2f408ecb | 412 | std::string |
ae6a3a4c TJB |
413 | type_to_string (struct type *type) |
414 | { | |
492d29ea | 415 | TRY |
ae6a3a4c | 416 | { |
d7e74731 PA |
417 | string_file stb; |
418 | ||
419 | type_print (type, "", &stb, -1); | |
420 | return std::move (stb.string ()); | |
ae6a3a4c | 421 | } |
492d29ea PA |
422 | CATCH (except, RETURN_MASK_ALL) |
423 | { | |
492d29ea PA |
424 | } |
425 | END_CATCH | |
ae6a3a4c | 426 | |
d7e74731 | 427 | return {}; |
ae6a3a4c TJB |
428 | } |
429 | ||
7022349d PA |
430 | /* See typeprint.h. */ |
431 | ||
432 | void | |
433 | type_print_unknown_return_type (struct ui_file *stream) | |
434 | { | |
435 | fprintf_filtered (stream, _("<unknown return type>")); | |
436 | } | |
437 | ||
46a4882b PA |
438 | /* See typeprint.h. */ |
439 | ||
440 | void | |
441 | error_unknown_type (const char *sym_print_name) | |
442 | { | |
443 | error (_("'%s' has unknown type; cast it to its declared type"), | |
444 | sym_print_name); | |
445 | } | |
446 | ||
c906108c SS |
447 | /* Print type of EXP, or last thing in value history if EXP == NULL. |
448 | show is passed to type_print. */ | |
449 | ||
450 | static void | |
0b39b52e | 451 | whatis_exp (const char *exp, int show) |
c906108c | 452 | { |
3d6d86c6 | 453 | struct value *val; |
c5aa993b | 454 | struct type *real_type = NULL; |
070ad9f0 | 455 | struct type *type; |
c906108c | 456 | int full = 0; |
6b850546 | 457 | LONGEST top = -1; |
c906108c | 458 | int using_enc = 0; |
79a45b7d | 459 | struct value_print_options opts; |
53342f27 | 460 | struct type_print_options flags = default_ptype_flags; |
c906108c SS |
461 | |
462 | if (exp) | |
463 | { | |
53342f27 TT |
464 | if (*exp == '/') |
465 | { | |
466 | int seen_one = 0; | |
467 | ||
468 | for (++exp; *exp && !isspace (*exp); ++exp) | |
469 | { | |
470 | switch (*exp) | |
471 | { | |
472 | case 'r': | |
473 | flags.raw = 1; | |
474 | break; | |
475 | case 'm': | |
476 | flags.print_methods = 0; | |
477 | break; | |
478 | case 'M': | |
479 | flags.print_methods = 1; | |
480 | break; | |
481 | case 't': | |
482 | flags.print_typedefs = 0; | |
483 | break; | |
484 | case 'T': | |
485 | flags.print_typedefs = 1; | |
486 | break; | |
7c161838 SDJ |
487 | case 'o': |
488 | { | |
489 | /* Filter out languages which don't implement the | |
490 | feature. */ | |
46afe196 SDJ |
491 | if (show > 0 |
492 | && (current_language->la_language == language_c | |
a33ccfc7 TT |
493 | || current_language->la_language == language_cplus |
494 | || current_language->la_language == language_rust)) | |
7c161838 SDJ |
495 | { |
496 | flags.print_offsets = 1; | |
497 | flags.print_typedefs = 0; | |
498 | flags.print_methods = 0; | |
499 | } | |
500 | break; | |
501 | } | |
53342f27 TT |
502 | default: |
503 | error (_("unrecognized flag '%c'"), *exp); | |
504 | } | |
505 | seen_one = 1; | |
506 | } | |
507 | ||
508 | if (!*exp && !seen_one) | |
509 | error (_("flag expected")); | |
510 | if (!isspace (*exp)) | |
511 | error (_("expected space after format")); | |
512 | exp = skip_spaces (exp); | |
513 | } | |
514 | ||
4d01a485 | 515 | expression_up expr = parse_expression (exp); |
c973d0aa PA |
516 | |
517 | /* The behavior of "whatis" depends on whether the user | |
518 | expression names a type directly, or a language expression | |
519 | (including variable names). If the former, then "whatis" | |
520 | strips one level of typedefs, only. If an expression, | |
521 | "whatis" prints the type of the expression without stripping | |
522 | any typedef level. "ptype" always strips all levels of | |
523 | typedefs. */ | |
524 | if (show == -1 && expr->elts[0].opcode == OP_TYPE) | |
525 | { | |
526 | /* The user expression names a type directly. */ | |
527 | type = expr->elts[1].type; | |
528 | ||
529 | /* If this is a typedef, then find its immediate target. | |
530 | Use check_typedef to resolve stubs, but ignore its result | |
531 | because we do not want to dig past all typedefs. */ | |
532 | check_typedef (type); | |
533 | if (TYPE_CODE (type) == TYPE_CODE_TYPEDEF) | |
534 | type = TYPE_TARGET_TYPE (type); | |
5c319bb2 PA |
535 | |
536 | /* If the expression is actually a type, then there's no | |
537 | value to fetch the dynamic type from. */ | |
538 | val = NULL; | |
c973d0aa PA |
539 | } |
540 | else | |
541 | { | |
542 | /* The user expression names a type indirectly by naming an | |
543 | object or expression of that type. Find that | |
544 | indirectly-named type. */ | |
545 | val = evaluate_type (expr.get ()); | |
546 | type = value_type (val); | |
547 | } | |
c906108c SS |
548 | } |
549 | else | |
c973d0aa PA |
550 | { |
551 | val = access_value_history (0); | |
552 | type = value_type (val); | |
553 | } | |
070ad9f0 | 554 | |
79a45b7d | 555 | get_user_print_options (&opts); |
5c319bb2 | 556 | if (val != NULL && opts.objectprint) |
070ad9f0 | 557 | { |
aa006118 | 558 | if (((TYPE_CODE (type) == TYPE_CODE_PTR) || TYPE_IS_REFERENCE (type)) |
4753d33b | 559 | && (TYPE_CODE (TYPE_TARGET_TYPE (type)) == TYPE_CODE_STRUCT)) |
dfcee124 | 560 | real_type = value_rtti_indirect_type (val, &full, &top, &using_enc); |
4753d33b | 561 | else if (TYPE_CODE (type) == TYPE_CODE_STRUCT) |
41808ebe | 562 | real_type = value_rtti_type (val, &full, &top, &using_enc); |
070ad9f0 | 563 | } |
c906108c | 564 | |
7c161838 SDJ |
565 | if (flags.print_offsets |
566 | && (TYPE_CODE (type) == TYPE_CODE_STRUCT | |
567 | || TYPE_CODE (type) == TYPE_CODE_UNION)) | |
568 | fprintf_filtered (gdb_stdout, "/* offset | size */ "); | |
569 | ||
c906108c SS |
570 | printf_filtered ("type = "); |
571 | ||
c819b2c0 TT |
572 | std::unique_ptr<typedef_hash_table> table_holder; |
573 | std::unique_ptr<ext_lang_type_printers> printer_holder; | |
18a9fc12 | 574 | if (!flags.raw) |
c819b2c0 TT |
575 | { |
576 | table_holder.reset (new typedef_hash_table); | |
577 | flags.global_typedefs = table_holder.get (); | |
578 | ||
579 | printer_holder.reset (new ext_lang_type_printers); | |
580 | flags.global_printers = printer_holder.get (); | |
581 | } | |
18a9fc12 | 582 | |
070ad9f0 DB |
583 | if (real_type) |
584 | { | |
585 | printf_filtered ("/* real type = "); | |
586 | type_print (real_type, "", gdb_stdout, -1); | |
587 | if (! full) | |
588 | printf_filtered (" (incomplete object)"); | |
589 | printf_filtered (" */\n"); | |
590 | } | |
c906108c | 591 | |
53342f27 | 592 | LA_PRINT_TYPE (type, "", gdb_stdout, show, 0, &flags); |
c906108c | 593 | printf_filtered ("\n"); |
c906108c SS |
594 | } |
595 | ||
c906108c | 596 | static void |
0b39b52e | 597 | whatis_command (const char *exp, int from_tty) |
c906108c SS |
598 | { |
599 | /* Most of the time users do not want to see all the fields | |
600 | in a structure. If they do they can use the "ptype" command. | |
601 | Hence the "-1" below. */ | |
602 | whatis_exp (exp, -1); | |
603 | } | |
604 | ||
c906108c SS |
605 | /* TYPENAME is either the name of a type, or an expression. */ |
606 | ||
c906108c | 607 | static void |
0b39b52e | 608 | ptype_command (const char *type_name, int from_tty) |
c906108c | 609 | { |
fe978cb0 | 610 | whatis_exp (type_name, 1); |
c906108c SS |
611 | } |
612 | ||
613 | /* Print integral scalar data VAL, of type TYPE, onto stdio stream STREAM. | |
614 | Used to print data from type structures in a specified type. For example, | |
615 | array bounds may be characters or booleans in some languages, and this | |
616 | allows the ranges to be printed in their "natural" form rather than as | |
617 | decimal integer values. | |
618 | ||
619 | FIXME: This is here simply because only the type printing routines | |
620 | currently use it, and it wasn't clear if it really belonged somewhere | |
621 | else (like printcmd.c). There are a lot of other gdb routines that do | |
622 | something similar, but they are generally concerned with printing values | |
41808ebe | 623 | that come from the inferior in target byte order and target size. */ |
c906108c SS |
624 | |
625 | void | |
fba45db2 | 626 | print_type_scalar (struct type *type, LONGEST val, struct ui_file *stream) |
c906108c SS |
627 | { |
628 | unsigned int i; | |
629 | unsigned len; | |
630 | ||
f168693b | 631 | type = check_typedef (type); |
c906108c SS |
632 | |
633 | switch (TYPE_CODE (type)) | |
634 | { | |
635 | ||
636 | case TYPE_CODE_ENUM: | |
637 | len = TYPE_NFIELDS (type); | |
638 | for (i = 0; i < len; i++) | |
639 | { | |
14e75d8e | 640 | if (TYPE_FIELD_ENUMVAL (type, i) == val) |
c906108c SS |
641 | { |
642 | break; | |
643 | } | |
644 | } | |
645 | if (i < len) | |
646 | { | |
647 | fputs_filtered (TYPE_FIELD_NAME (type, i), stream); | |
648 | } | |
649 | else | |
650 | { | |
651 | print_longest (stream, 'd', 0, val); | |
652 | } | |
653 | break; | |
654 | ||
655 | case TYPE_CODE_INT: | |
656 | print_longest (stream, TYPE_UNSIGNED (type) ? 'u' : 'd', 0, val); | |
657 | break; | |
658 | ||
659 | case TYPE_CODE_CHAR: | |
6c7a06a3 | 660 | LA_PRINT_CHAR ((unsigned char) val, type, stream); |
c906108c SS |
661 | break; |
662 | ||
663 | case TYPE_CODE_BOOL: | |
664 | fprintf_filtered (stream, val ? "TRUE" : "FALSE"); | |
665 | break; | |
666 | ||
667 | case TYPE_CODE_RANGE: | |
668 | print_type_scalar (TYPE_TARGET_TYPE (type), val, stream); | |
669 | return; | |
670 | ||
671 | case TYPE_CODE_UNDEF: | |
672 | case TYPE_CODE_PTR: | |
673 | case TYPE_CODE_ARRAY: | |
674 | case TYPE_CODE_STRUCT: | |
675 | case TYPE_CODE_UNION: | |
676 | case TYPE_CODE_FUNC: | |
677 | case TYPE_CODE_FLT: | |
678 | case TYPE_CODE_VOID: | |
679 | case TYPE_CODE_SET: | |
680 | case TYPE_CODE_STRING: | |
681 | case TYPE_CODE_ERROR: | |
0d5de010 DJ |
682 | case TYPE_CODE_MEMBERPTR: |
683 | case TYPE_CODE_METHODPTR: | |
c906108c SS |
684 | case TYPE_CODE_METHOD: |
685 | case TYPE_CODE_REF: | |
aa006118 | 686 | case TYPE_CODE_RVALUE_REF: |
5c4e30ca | 687 | case TYPE_CODE_NAMESPACE: |
8a3fe4f8 | 688 | error (_("internal error: unhandled type in print_type_scalar")); |
c906108c SS |
689 | break; |
690 | ||
691 | default: | |
8a3fe4f8 | 692 | error (_("Invalid type code in symbol table.")); |
c906108c SS |
693 | } |
694 | gdb_flush (stream); | |
695 | } | |
696 | ||
c906108c SS |
697 | /* Dump details of a type specified either directly or indirectly. |
698 | Uses the same sort of type lookup mechanism as ptype_command() | |
41808ebe | 699 | and whatis_command(). */ |
c906108c SS |
700 | |
701 | void | |
58971144 | 702 | maintenance_print_type (const char *type_name, int from_tty) |
c906108c | 703 | { |
3d6d86c6 | 704 | struct value *val; |
52f0bd74 | 705 | struct type *type; |
c906108c | 706 | |
fe978cb0 | 707 | if (type_name != NULL) |
c5aa993b | 708 | { |
4d01a485 | 709 | expression_up expr = parse_expression (type_name); |
c5aa993b JM |
710 | if (expr->elts[0].opcode == OP_TYPE) |
711 | { | |
c378eb4e | 712 | /* The user expression names a type directly, just use that type. */ |
c5aa993b JM |
713 | type = expr->elts[1].type; |
714 | } | |
715 | else | |
716 | { | |
717 | /* The user expression may name a type indirectly by naming an | |
c378eb4e | 718 | object of that type. Find that indirectly named type. */ |
4d01a485 | 719 | val = evaluate_type (expr.get ()); |
df407dfe | 720 | type = value_type (val); |
c5aa993b JM |
721 | } |
722 | if (type != NULL) | |
723 | { | |
724 | recursive_dump_type (type, 0); | |
725 | } | |
c5aa993b | 726 | } |
c906108c | 727 | } |
c906108c | 728 | \f |
c5aa993b | 729 | |
53342f27 TT |
730 | struct cmd_list_element *setprinttypelist; |
731 | ||
732 | struct cmd_list_element *showprinttypelist; | |
733 | ||
734 | static void | |
981a3fb3 | 735 | set_print_type (const char *arg, int from_tty) |
53342f27 TT |
736 | { |
737 | printf_unfiltered ( | |
738 | "\"set print type\" must be followed by the name of a subcommand.\n"); | |
635c7e8a | 739 | help_list (setprintlist, "set print type ", all_commands, gdb_stdout); |
53342f27 TT |
740 | } |
741 | ||
742 | static void | |
981a3fb3 | 743 | show_print_type (const char *args, int from_tty) |
53342f27 TT |
744 | { |
745 | cmd_show_list (showprinttypelist, from_tty, ""); | |
746 | } | |
747 | ||
748 | static int print_methods = 1; | |
749 | ||
750 | static void | |
eb4c3f4a TT |
751 | set_print_type_methods (const char *args, |
752 | int from_tty, struct cmd_list_element *c) | |
53342f27 TT |
753 | { |
754 | default_ptype_flags.print_methods = print_methods; | |
755 | } | |
756 | ||
757 | static void | |
758 | show_print_type_methods (struct ui_file *file, int from_tty, | |
759 | struct cmd_list_element *c, const char *value) | |
760 | { | |
761 | fprintf_filtered (file, _("Printing of methods defined in a class in %s\n"), | |
762 | value); | |
763 | } | |
764 | ||
765 | static int print_typedefs = 1; | |
766 | ||
767 | static void | |
eb4c3f4a TT |
768 | set_print_type_typedefs (const char *args, |
769 | int from_tty, struct cmd_list_element *c) | |
53342f27 TT |
770 | { |
771 | default_ptype_flags.print_typedefs = print_typedefs; | |
772 | } | |
773 | ||
774 | static void | |
775 | show_print_type_typedefs (struct ui_file *file, int from_tty, | |
776 | struct cmd_list_element *c, const char *value) | |
777 | { | |
778 | fprintf_filtered (file, _("Printing of typedefs defined in a class in %s\n"), | |
779 | value); | |
780 | } | |
781 | ||
883fd55a KS |
782 | /* Limit on the number of nested type definitions to print or -1 to print |
783 | all nested type definitions in a class. By default, we do not print | |
784 | nested definitions. */ | |
785 | ||
786 | static int print_nested_type_limit = 0; | |
787 | ||
788 | /* Set how many nested type definitions should be printed by the type | |
789 | printer. */ | |
790 | ||
791 | static void | |
792 | set_print_type_nested_types (const char *args, int from_tty, | |
793 | struct cmd_list_element *c) | |
794 | { | |
795 | default_ptype_flags.print_nested_type_limit = print_nested_type_limit; | |
796 | } | |
797 | ||
798 | /* Show how many nested type definitions the type printer will print. */ | |
799 | ||
800 | static void | |
801 | show_print_type_nested_types (struct ui_file *file, int from_tty, | |
802 | struct cmd_list_element *c, const char *value) | |
803 | { | |
804 | if (*value == '0') | |
805 | { | |
806 | fprintf_filtered (file, | |
807 | _("Will not print nested types defined in a class\n")); | |
808 | } | |
809 | else | |
810 | { | |
811 | fprintf_filtered (file, | |
812 | _("Will print %s nested types defined in a class\n"), | |
813 | value); | |
814 | } | |
815 | } | |
816 | ||
c906108c | 817 | void |
fba45db2 | 818 | _initialize_typeprint (void) |
c906108c | 819 | { |
4fc5d43e TT |
820 | struct cmd_list_element *c; |
821 | ||
822 | c = add_com ("ptype", class_vars, ptype_command, _("\ | |
1bedd215 | 823 | Print definition of type TYPE.\n\ |
a9375afe DE |
824 | Usage: ptype[/FLAGS] TYPE | EXPRESSION\n\ |
825 | Argument may be any type (for example a type name defined by typedef,\n\ | |
826 | or \"struct STRUCT-TAG\" or \"class CLASS-NAME\" or \"union UNION-TAG\"\n\ | |
827 | or \"enum ENUM-TAG\") or an expression.\n\ | |
11081198 | 828 | The selected stack frame's lexical context is used to look up the name.\n\ |
53342f27 TT |
829 | Contrary to \"whatis\", \"ptype\" always unrolls any typedefs.\n\ |
830 | \n\ | |
831 | Available FLAGS are:\n\ | |
832 | /r print in \"raw\" form; do not substitute typedefs\n\ | |
833 | /m do not print methods defined in a class\n\ | |
834 | /M print methods defined in a class\n\ | |
835 | /t do not print typedefs defined in a class\n\ | |
7c161838 SDJ |
836 | /T print typedefs defined in a class\n\ |
837 | /o print offsets and sizes of fields in a struct (like pahole)\n")); | |
4fc5d43e | 838 | set_cmd_completer (c, expression_completer); |
c906108c | 839 | |
4fc5d43e TT |
840 | c = add_com ("whatis", class_vars, whatis_command, |
841 | _("Print data type of expression EXP.\n\ | |
11081198 | 842 | Only one level of typedefs is unrolled. See also \"ptype\".")); |
4fc5d43e | 843 | set_cmd_completer (c, expression_completer); |
53342f27 TT |
844 | |
845 | add_prefix_cmd ("type", no_class, show_print_type, | |
846 | _("Generic command for showing type-printing settings."), | |
847 | &showprinttypelist, "show print type ", 0, &showprintlist); | |
848 | add_prefix_cmd ("type", no_class, set_print_type, | |
849 | _("Generic command for setting how types print."), | |
850 | &setprinttypelist, "show print type ", 0, &setprintlist); | |
851 | ||
852 | add_setshow_boolean_cmd ("methods", no_class, &print_methods, | |
853 | _("\ | |
854 | Set printing of methods defined in classes."), _("\ | |
855 | Show printing of methods defined in classes."), NULL, | |
856 | set_print_type_methods, | |
857 | show_print_type_methods, | |
858 | &setprinttypelist, &showprinttypelist); | |
859 | add_setshow_boolean_cmd ("typedefs", no_class, &print_typedefs, | |
860 | _("\ | |
861 | Set printing of typedefs defined in classes."), _("\ | |
862 | Show printing of typedefs defined in classes."), NULL, | |
863 | set_print_type_typedefs, | |
864 | show_print_type_typedefs, | |
865 | &setprinttypelist, &showprinttypelist); | |
883fd55a KS |
866 | |
867 | add_setshow_zuinteger_unlimited_cmd ("nested-type-limit", no_class, | |
868 | &print_nested_type_limit, | |
869 | _("\ | |
870 | Set the number of recursive nested type definitions to print \ | |
871 | (\"unlimited\" or -1 to show all)."), _("\ | |
872 | Show the number of recursive nested type definitions to print."), NULL, | |
873 | set_print_type_nested_types, | |
874 | show_print_type_nested_types, | |
875 | &setprinttypelist, &showprinttypelist); | |
c906108c | 876 | } |
3f2f83dd KB |
877 | |
878 | /* Print <not allocated> status to stream STREAM. */ | |
879 | ||
880 | void | |
881 | val_print_not_allocated (struct ui_file *stream) | |
882 | { | |
883 | fprintf_filtered (stream, _("<not allocated>")); | |
884 | } | |
885 | ||
886 | /* Print <not associated> status to stream STREAM. */ | |
887 | ||
888 | void | |
889 | val_print_not_associated (struct ui_file *stream) | |
890 | { | |
891 | fprintf_filtered (stream, _("<not associated>")); | |
892 | } |