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