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