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