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