Change print_field_values to use value-based API
[deliverable/binutils-gdb.git] / gdb / cp-valprint.c
CommitLineData
c906108c 1/* Support for printing C++ values for GDB, the GNU debugger.
a2bd3dcd 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"
4de283e4
TT
21#include "gdb_obstack.h"
22#include "symtab.h"
23#include "gdbtypes.h"
24#include "expression.h"
25#include "value.h"
26#include "command.h"
27#include "gdbcmd.h"
28#include "demangle.h"
c906108c 29#include "annotate.h"
c906108c 30#include "c-lang.h"
4de283e4 31#include "target.h"
b9d652ac 32#include "cp-abi.h"
4de283e4 33#include "valprint.h"
d3cbe7ef 34#include "cp-support.h"
d55e5aa6 35#include "language.h"
4de283e4 36#include "extension.h"
79d43c61 37#include "typeprint.h"
268a13a5 38#include "gdbsupport/byte-vector.h"
0d12e84c 39#include "gdbarch.h"
7f6aba03 40#include "cli/cli-style.h"
c906108c 41
c906108c
SS
42static struct obstack dont_print_vb_obstack;
43static struct obstack dont_print_statmem_obstack;
ec31cde5 44static struct obstack dont_print_stat_array_obstack;
c906108c 45
6943961c 46static void cp_print_static_field (struct type *, struct value *,
79a45b7d
TT
47 struct ui_file *, int,
48 const struct value_print_options *);
c906108c 49
aff410f1 50static void cp_print_value (struct type *, struct type *,
65408fa6 51 LONGEST,
aff410f1 52 CORE_ADDR, struct ui_file *,
e8b24d9f 53 int, struct value *,
aff410f1
MS
54 const struct value_print_options *,
55 struct type **);
c906108c 56
fbf54e75
TT
57static void cp_print_value (struct value *, struct ui_file *,
58 int, const struct value_print_options *,
59 struct type **);
60
c906108c 61
8343f86c 62/* GCC versions after 2.4.5 use this. */
bad5c026 63const char vtbl_ptr_name[] = "__vtbl_ptr_type";
c906108c 64
c906108c
SS
65/* Return truth value for assertion that TYPE is of the type
66 "pointer to virtual function". */
67
68int
fba45db2 69cp_is_vtbl_ptr_type (struct type *type)
c906108c 70{
a737d952 71 const char *type_name = TYPE_NAME (type);
c906108c 72
fe978cb0 73 return (type_name != NULL && !strcmp (type_name, vtbl_ptr_name));
c906108c
SS
74}
75
76/* Return truth value for the assertion that TYPE is of the type
77 "pointer to virtual function table". */
78
79int
fba45db2 80cp_is_vtbl_member (struct type *type)
c906108c 81{
aff410f1
MS
82 /* With older versions of g++, the vtbl field pointed to an array of
83 structures. Nowadays it points directly to the structure. */
c906108c
SS
84 if (TYPE_CODE (type) == TYPE_CODE_PTR)
85 {
86 type = TYPE_TARGET_TYPE (type);
87 if (TYPE_CODE (type) == TYPE_CODE_ARRAY)
88 {
89 type = TYPE_TARGET_TYPE (type);
aff410f1
MS
90 if (TYPE_CODE (type) == TYPE_CODE_STRUCT /* if not using thunks */
91 || TYPE_CODE (type) == TYPE_CODE_PTR) /* if using thunks */
c906108c
SS
92 {
93 /* Virtual functions tables are full of pointers
aff410f1 94 to virtual functions. */
c906108c
SS
95 return cp_is_vtbl_ptr_type (type);
96 }
97 }
0e5e3ea6
PS
98 else if (TYPE_CODE (type) == TYPE_CODE_STRUCT) /* if not using thunks */
99 {
100 return cp_is_vtbl_ptr_type (type);
101 }
102 else if (TYPE_CODE (type) == TYPE_CODE_PTR) /* if using thunks */
103 {
aff410f1
MS
104 /* The type name of the thunk pointer is NULL when using
105 dwarf2. We could test for a pointer to a function, but
106 there is no type info for the virtual table either, so it
107 wont help. */
0e5e3ea6
PS
108 return cp_is_vtbl_ptr_type (type);
109 }
c906108c
SS
110 }
111 return 0;
112}
113
114/* Mutually recursive subroutines of cp_print_value and c_val_print to
aff410f1
MS
115 print out a structure's fields: cp_print_value_fields and
116 cp_print_value.
c5aa993b 117
aff410f1
MS
118 TYPE, VALADDR, ADDRESS, STREAM, RECURSE, and OPTIONS have the same
119 meanings as in cp_print_value and c_val_print.
c906108c 120
aff410f1
MS
121 2nd argument REAL_TYPE is used to carry over the type of the
122 derived class across the recursion to base classes.
c906108c 123
aff410f1
MS
124 DONT_PRINT is an array of baseclass types that we should not print,
125 or zero if called from top level. */
c906108c
SS
126
127void
a2bd3dcd 128cp_print_value_fields (struct type *type, struct type *real_type,
65408fa6 129 LONGEST offset,
aff410f1 130 CORE_ADDR address, struct ui_file *stream,
e8b24d9f 131 int recurse, struct value *val,
79a45b7d 132 const struct value_print_options *options,
aff410f1
MS
133 struct type **dont_print_vb,
134 int dont_print_statmem)
c906108c
SS
135{
136 int i, len, n_baseclasses;
c906108c 137 int fields_seen = 0;
ec31cde5 138 static int last_set_recurse = -1;
c906108c 139
f168693b 140 type = check_typedef (type);
99903ae3 141
ec31cde5
CM
142 if (recurse == 0)
143 {
aff410f1
MS
144 /* Any object can be left on obstacks only during an unexpected
145 error. */
6036c742 146
ec31cde5 147 if (obstack_object_size (&dont_print_statmem_obstack) > 0)
6036c742
JK
148 {
149 obstack_free (&dont_print_statmem_obstack, NULL);
aff410f1
MS
150 obstack_begin (&dont_print_statmem_obstack,
151 32 * sizeof (CORE_ADDR));
6036c742 152 }
ec31cde5 153 if (obstack_object_size (&dont_print_stat_array_obstack) > 0)
6036c742
JK
154 {
155 obstack_free (&dont_print_stat_array_obstack, NULL);
156 obstack_begin (&dont_print_stat_array_obstack,
157 32 * sizeof (struct type *));
158 }
ec31cde5 159 }
c906108c
SS
160
161 fprintf_filtered (stream, "{");
162 len = TYPE_NFIELDS (type);
163 n_baseclasses = TYPE_N_BASECLASSES (type);
164
165 /* First, print out baseclasses such that we don't print
166 duplicates of virtual baseclasses. */
167
168 if (n_baseclasses > 0)
65408fa6 169 cp_print_value (type, real_type,
aff410f1
MS
170 offset, address, stream,
171 recurse + 1, val, options,
172 dont_print_vb);
c906108c
SS
173
174 /* Second, print out data fields */
175
086280be
UW
176 /* If there are no data fields, skip this part */
177 if (len == n_baseclasses || !len)
7f6aba03 178 fprintf_styled (stream, metadata_style.style (), "<No data fields>");
c906108c
SS
179 else
180 {
241fd515
AM
181 size_t statmem_obstack_initial_size = 0;
182 size_t stat_array_obstack_initial_size = 0;
7977e5d2
TT
183 struct type *vptr_basetype = NULL;
184 int vptr_fieldno;
185
c906108c
SS
186 if (dont_print_statmem == 0)
187 {
f56dcb88 188 statmem_obstack_initial_size =
0b66f317 189 obstack_object_size (&dont_print_statmem_obstack);
ec31cde5
CM
190
191 if (last_set_recurse != recurse)
192 {
f56dcb88
CM
193 stat_array_obstack_initial_size =
194 obstack_object_size (&dont_print_stat_array_obstack);
c5504eaf 195
ec31cde5
CM
196 last_set_recurse = recurse;
197 }
c906108c
SS
198 }
199
7977e5d2 200 vptr_fieldno = get_vptr_fieldno (type, &vptr_basetype);
c906108c
SS
201 for (i = n_baseclasses; i < len; i++)
202 {
65408fa6
YQ
203 const gdb_byte *valaddr = value_contents_for_printing (val);
204
c906108c 205 /* If requested, skip printing of static fields. */
79a45b7d 206 if (!options->static_field_print
d6a843b5 207 && field_is_static (&TYPE_FIELD (type, i)))
c906108c
SS
208 continue;
209
c906108c 210 if (fields_seen)
18c77628
AB
211 {
212 fputs_filtered (",", stream);
213 if (!options->prettyformat)
214 fputs_filtered (" ", stream);
215 }
c906108c
SS
216 else if (n_baseclasses > 0)
217 {
2a998fc0 218 if (options->prettyformat)
c906108c
SS
219 {
220 fprintf_filtered (stream, "\n");
221 print_spaces_filtered (2 + 2 * recurse, stream);
222 fputs_filtered ("members of ", stream);
a737d952 223 fputs_filtered (TYPE_NAME (type), stream);
18c77628 224 fputs_filtered (":", stream);
c906108c
SS
225 }
226 }
227 fields_seen = 1;
228
2a998fc0 229 if (options->prettyformat)
c906108c
SS
230 {
231 fprintf_filtered (stream, "\n");
232 print_spaces_filtered (2 + 2 * recurse, stream);
233 }
c5aa993b 234 else
c906108c
SS
235 {
236 wrap_here (n_spaces (2 + 2 * recurse));
237 }
e93a8774
TT
238
239 annotate_field_begin (TYPE_FIELD_TYPE (type, i));
240
241 if (field_is_static (&TYPE_FIELD (type, i)))
3f0cbb04
TT
242 {
243 fputs_filtered ("static ", stream);
244 fprintf_symbol_filtered (stream,
245 TYPE_FIELD_NAME (type, i),
246 current_language->la_language,
247 DMGL_PARAMS | DMGL_ANSI);
248 }
249 else
250 fputs_styled (TYPE_FIELD_NAME (type, i),
251 variable_name_style.style (), stream);
e93a8774 252 annotate_field_name_end ();
2e62ab40
AB
253
254 /* We tweak various options in a few cases below. */
255 value_print_options options_copy = *options;
256 value_print_options *opts = &options_copy;
257
e93a8774
TT
258 /* Do not print leading '=' in case of anonymous
259 unions. */
260 if (strcmp (TYPE_FIELD_NAME (type, i), ""))
261 fputs_filtered (" = ", stream);
2e62ab40
AB
262 else
263 {
264 /* If this is an anonymous field then we want to consider it
265 as though it is at its parent's depth when it comes to the
266 max print depth. */
267 if (opts->max_depth != -1 && opts->max_depth < INT_MAX)
268 ++opts->max_depth;
269 }
e93a8774 270 annotate_field_value ();
c906108c 271
d6a843b5
JK
272 if (!field_is_static (&TYPE_FIELD (type, i))
273 && TYPE_FIELD_PACKED (type, i))
c906108c 274 {
6943961c 275 struct value *v;
c906108c 276
aff410f1
MS
277 /* Bitfields require special handling, especially due to
278 byte order problems. */
c906108c
SS
279 if (TYPE_FIELD_IGNORE (type, i))
280 {
7f6aba03
TT
281 fputs_styled ("<optimized out or zero length>",
282 metadata_style.style (), stream);
c906108c 283 }
8cf6f0b1
TT
284 else if (value_bits_synthetic_pointer (val,
285 TYPE_FIELD_BITPOS (type,
286 i),
287 TYPE_FIELD_BITSIZE (type,
288 i)))
289 {
7f6aba03
TT
290 fputs_styled (_("<synthetic pointer>"),
291 metadata_style.style (), stream);
8cf6f0b1 292 }
c906108c
SS
293 else
294 {
2e62ab40 295 opts->deref_ref = 0;
5467c6c8
PA
296
297 v = value_field_bitfield (type, i, valaddr, offset, val);
c906108c 298
2e62ab40
AB
299 common_val_print (v, stream, recurse + 1,
300 opts, current_language);
c906108c
SS
301 }
302 }
303 else
304 {
305 if (TYPE_FIELD_IGNORE (type, i))
306 {
7f6aba03
TT
307 fputs_styled ("<optimized out or zero length>",
308 metadata_style.style (), stream);
c906108c 309 }
d6a843b5 310 else if (field_is_static (&TYPE_FIELD (type, i)))
c906108c 311 {
a70b8144 312 try
ee86786c 313 {
4330d61d 314 struct value *v = value_static_field (type, i);
ee86786c 315
4330d61d
TT
316 cp_print_static_field (TYPE_FIELD_TYPE (type, i),
317 v, stream, recurse + 1,
3d507ff2 318 opts);
4330d61d 319 }
230d2906 320 catch (const gdb_exception_error &ex)
492d29ea 321 {
7f6aba03
TT
322 fprintf_styled (stream, metadata_style.style (),
323 _("<error reading variable: %s>"),
324 ex.what ());
492d29ea 325 }
c906108c 326 }
7977e5d2 327 else if (i == vptr_fieldno && type == vptr_basetype)
410528f0 328 {
a72c8f6a
JK
329 int i_offset = offset + TYPE_FIELD_BITPOS (type, i) / 8;
330 struct type *i_type = TYPE_FIELD_TYPE (type, i);
331
332 if (valprint_check_validity (stream, i_type, i_offset, val))
333 {
334 CORE_ADDR addr;
335
64b653ca 336 i_offset += value_embedded_offset (val);
a72c8f6a 337 addr = extract_typed_address (valaddr + i_offset, i_type);
2e62ab40 338 print_function_pointer_address (opts,
edf0c1b7
TT
339 get_type_arch (type),
340 addr, stream);
a72c8f6a 341 }
410528f0 342 }
c906108c
SS
343 else
344 {
2e62ab40 345 opts->deref_ref = 0;
c5aa993b 346 val_print (TYPE_FIELD_TYPE (type, i),
aff410f1 347 offset + TYPE_FIELD_BITPOS (type, i) / 8,
edf3d5f3 348 address,
2e62ab40 349 stream, recurse + 1, val, opts,
d8ca156b 350 current_language);
c906108c
SS
351 }
352 }
353 annotate_field_end ();
354 }
355
356 if (dont_print_statmem == 0)
357 {
241fd515 358 size_t obstack_final_size =
0b66f317
CM
359 obstack_object_size (&dont_print_statmem_obstack);
360
aff410f1
MS
361 if (obstack_final_size > statmem_obstack_initial_size)
362 {
363 /* In effect, a pop of the printed-statics stack. */
a43f3893
PF
364 size_t shrink_bytes
365 = statmem_obstack_initial_size - obstack_final_size;
366 obstack_blank_fast (&dont_print_statmem_obstack, shrink_bytes);
aff410f1 367 }
ec31cde5
CM
368
369 if (last_set_recurse != recurse)
370 {
b926417a 371 obstack_final_size =
f56dcb88
CM
372 obstack_object_size (&dont_print_stat_array_obstack);
373
374 if (obstack_final_size > stat_array_obstack_initial_size)
375 {
376 void *free_to_ptr =
1ae1b8cc 377 (char *) obstack_next_free (&dont_print_stat_array_obstack)
aff410f1
MS
378 - (obstack_final_size
379 - stat_array_obstack_initial_size);
f56dcb88
CM
380
381 obstack_free (&dont_print_stat_array_obstack,
382 free_to_ptr);
383 }
ec31cde5
CM
384 last_set_recurse = -1;
385 }
c906108c
SS
386 }
387
2a998fc0 388 if (options->prettyformat)
c906108c
SS
389 {
390 fprintf_filtered (stream, "\n");
391 print_spaces_filtered (2 * recurse, stream);
392 }
c5aa993b 393 } /* if there are data fields */
c5aa993b 394
c906108c
SS
395 fprintf_filtered (stream, "}");
396}
397
64b653ca
TT
398/* Mutually recursive subroutines of cp_print_value and c_value_print
399 to print out a structure's fields: cp_print_value_fields and
400 cp_print_value.
401
402 VAL, ADDRESS, STREAM, RECURSE, and OPTIONS have the same meanings
403 as in cp_print_value and c_value_print.
404
405 DONT_PRINT is an array of baseclass types that we should not print,
406 or zero if called from top level. */
407
408void
409cp_print_value_fields (struct value *val, struct ui_file *stream,
410 int recurse, const struct value_print_options *options,
411 struct type **dont_print_vb,
412 int dont_print_statmem)
413{
414 int i, len, n_baseclasses;
415 int fields_seen = 0;
416 static int last_set_recurse = -1;
417
418 struct type *type = check_typedef (value_type (val));
64b653ca
TT
419
420 if (recurse == 0)
421 {
422 /* Any object can be left on obstacks only during an unexpected
423 error. */
424
425 if (obstack_object_size (&dont_print_statmem_obstack) > 0)
426 {
427 obstack_free (&dont_print_statmem_obstack, NULL);
428 obstack_begin (&dont_print_statmem_obstack,
429 32 * sizeof (CORE_ADDR));
430 }
431 if (obstack_object_size (&dont_print_stat_array_obstack) > 0)
432 {
433 obstack_free (&dont_print_stat_array_obstack, NULL);
434 obstack_begin (&dont_print_stat_array_obstack,
435 32 * sizeof (struct type *));
436 }
437 }
438
439 fprintf_filtered (stream, "{");
440 len = TYPE_NFIELDS (type);
441 n_baseclasses = TYPE_N_BASECLASSES (type);
442
443 /* First, print out baseclasses such that we don't print
444 duplicates of virtual baseclasses. */
445
446 if (n_baseclasses > 0)
fbf54e75 447 cp_print_value (val, stream, recurse + 1, options, dont_print_vb);
64b653ca
TT
448
449 /* Second, print out data fields */
450
451 /* If there are no data fields, skip this part */
452 if (len == n_baseclasses || !len)
453 fprintf_styled (stream, metadata_style.style (), "<No data fields>");
454 else
455 {
456 size_t statmem_obstack_initial_size = 0;
457 size_t stat_array_obstack_initial_size = 0;
458 struct type *vptr_basetype = NULL;
459 int vptr_fieldno;
460
461 if (dont_print_statmem == 0)
462 {
463 statmem_obstack_initial_size =
464 obstack_object_size (&dont_print_statmem_obstack);
465
466 if (last_set_recurse != recurse)
467 {
468 stat_array_obstack_initial_size =
469 obstack_object_size (&dont_print_stat_array_obstack);
470
471 last_set_recurse = recurse;
472 }
473 }
474
475 vptr_fieldno = get_vptr_fieldno (type, &vptr_basetype);
476 for (i = n_baseclasses; i < len; i++)
477 {
478 const gdb_byte *valaddr = value_contents_for_printing (val);
479
480 /* If requested, skip printing of static fields. */
481 if (!options->static_field_print
482 && field_is_static (&TYPE_FIELD (type, i)))
483 continue;
484
485 if (fields_seen)
486 {
487 fputs_filtered (",", stream);
488 if (!options->prettyformat)
489 fputs_filtered (" ", stream);
490 }
491 else if (n_baseclasses > 0)
492 {
493 if (options->prettyformat)
494 {
495 fprintf_filtered (stream, "\n");
496 print_spaces_filtered (2 + 2 * recurse, stream);
497 fputs_filtered ("members of ", stream);
498 fputs_filtered (TYPE_NAME (type), stream);
499 fputs_filtered (":", stream);
500 }
501 }
502 fields_seen = 1;
503
504 if (options->prettyformat)
505 {
506 fprintf_filtered (stream, "\n");
507 print_spaces_filtered (2 + 2 * recurse, stream);
508 }
509 else
510 {
511 wrap_here (n_spaces (2 + 2 * recurse));
512 }
513
514 annotate_field_begin (TYPE_FIELD_TYPE (type, i));
515
516 if (field_is_static (&TYPE_FIELD (type, i)))
517 {
518 fputs_filtered ("static ", stream);
519 fprintf_symbol_filtered (stream,
520 TYPE_FIELD_NAME (type, i),
521 current_language->la_language,
522 DMGL_PARAMS | DMGL_ANSI);
523 }
524 else
525 fputs_styled (TYPE_FIELD_NAME (type, i),
526 variable_name_style.style (), stream);
527 annotate_field_name_end ();
528
529 /* We tweak various options in a few cases below. */
530 value_print_options options_copy = *options;
531 value_print_options *opts = &options_copy;
532
533 /* Do not print leading '=' in case of anonymous
534 unions. */
535 if (strcmp (TYPE_FIELD_NAME (type, i), ""))
536 fputs_filtered (" = ", stream);
537 else
538 {
539 /* If this is an anonymous field then we want to consider it
540 as though it is at its parent's depth when it comes to the
541 max print depth. */
542 if (opts->max_depth != -1 && opts->max_depth < INT_MAX)
543 ++opts->max_depth;
544 }
545 annotate_field_value ();
546
547 if (!field_is_static (&TYPE_FIELD (type, i))
548 && TYPE_FIELD_PACKED (type, i))
549 {
550 struct value *v;
551
552 /* Bitfields require special handling, especially due to
553 byte order problems. */
554 if (TYPE_FIELD_IGNORE (type, i))
555 {
556 fputs_styled ("<optimized out or zero length>",
557 metadata_style.style (), stream);
558 }
559 else if (value_bits_synthetic_pointer (val,
560 TYPE_FIELD_BITPOS (type,
561 i),
562 TYPE_FIELD_BITSIZE (type,
563 i)))
564 {
565 fputs_styled (_("<synthetic pointer>"),
566 metadata_style.style (), stream);
567 }
568 else
569 {
570 opts->deref_ref = 0;
571
572 v = value_field_bitfield (type, i, valaddr,
573 value_embedded_offset (val), val);
574
575 common_val_print (v, stream, recurse + 1,
576 opts, current_language);
577 }
578 }
579 else
580 {
581 if (TYPE_FIELD_IGNORE (type, i))
582 {
583 fputs_styled ("<optimized out or zero length>",
584 metadata_style.style (), stream);
585 }
586 else if (field_is_static (&TYPE_FIELD (type, i)))
587 {
588 try
589 {
590 struct value *v = value_static_field (type, i);
591
592 cp_print_static_field (TYPE_FIELD_TYPE (type, i),
593 v, stream, recurse + 1,
594 opts);
595 }
596 catch (const gdb_exception_error &ex)
597 {
598 fprintf_styled (stream, metadata_style.style (),
599 _("<error reading variable: %s>"),
600 ex.what ());
601 }
602 }
603 else if (i == vptr_fieldno && type == vptr_basetype)
604 {
605 int i_offset = TYPE_FIELD_BITPOS (type, i) / 8;
606 struct type *i_type = TYPE_FIELD_TYPE (type, i);
607
608 if (valprint_check_validity (stream, i_type, i_offset, val))
609 {
610 CORE_ADDR addr;
611
fbf54e75 612 i_offset += value_embedded_offset (val);
64b653ca
TT
613 addr = extract_typed_address (valaddr + i_offset, i_type);
614 print_function_pointer_address (opts,
615 get_type_arch (type),
616 addr, stream);
617 }
618 }
619 else
620 {
621 struct value *v = value_primitive_field (val, 0, i, type);
622 opts->deref_ref = 0;
623 common_val_print (v, stream, recurse + 1, opts,
624 current_language);
625 }
626 }
627 annotate_field_end ();
628 }
629
630 if (dont_print_statmem == 0)
631 {
632 size_t obstack_final_size =
633 obstack_object_size (&dont_print_statmem_obstack);
634
635 if (obstack_final_size > statmem_obstack_initial_size)
636 {
637 /* In effect, a pop of the printed-statics stack. */
638 size_t shrink_bytes
639 = statmem_obstack_initial_size - obstack_final_size;
640 obstack_blank_fast (&dont_print_statmem_obstack, shrink_bytes);
641 }
642
643 if (last_set_recurse != recurse)
644 {
645 obstack_final_size =
646 obstack_object_size (&dont_print_stat_array_obstack);
647
648 if (obstack_final_size > stat_array_obstack_initial_size)
649 {
650 void *free_to_ptr =
651 (char *) obstack_next_free (&dont_print_stat_array_obstack)
652 - (obstack_final_size
653 - stat_array_obstack_initial_size);
654
655 obstack_free (&dont_print_stat_array_obstack,
656 free_to_ptr);
657 }
658 last_set_recurse = -1;
659 }
660 }
661
662 if (options->prettyformat)
663 {
664 fprintf_filtered (stream, "\n");
665 print_spaces_filtered (2 * recurse, stream);
666 }
667 } /* if there are data fields */
668
669 fprintf_filtered (stream, "}");
670}
671
edf3d5f3
TT
672/* Like cp_print_value_fields, but find the runtime type of the object
673 and pass it as the `real_type' argument to cp_print_value_fields.
674 This function is a hack to work around the fact that
675 common_val_print passes the embedded offset to val_print, but not
676 the enclosing type. */
677
678void
679cp_print_value_fields_rtti (struct type *type,
6b850546 680 const gdb_byte *valaddr, LONGEST offset,
edf3d5f3
TT
681 CORE_ADDR address,
682 struct ui_file *stream, int recurse,
e8b24d9f 683 struct value *val,
edf3d5f3 684 const struct value_print_options *options,
c5504eaf
MS
685 struct type **dont_print_vb,
686 int dont_print_statmem)
edf3d5f3 687{
0e03807e
TT
688 struct type *real_type = NULL;
689
690 /* We require all bits to be valid in order to attempt a
691 conversion. */
9a0dc9e3
PA
692 if (!value_bits_any_optimized_out (val,
693 TARGET_CHAR_BIT * offset,
694 TARGET_CHAR_BIT * TYPE_LENGTH (type)))
0e03807e
TT
695 {
696 struct value *value;
6b850546
DT
697 int full, using_enc;
698 LONGEST top;
0e03807e
TT
699
700 /* Ugh, we have to convert back to a value here. */
701 value = value_from_contents_and_address (type, valaddr + offset,
702 address + offset);
9f1f738a 703 type = value_type (value);
aff410f1
MS
704 /* We don't actually care about most of the result here -- just
705 the type. We already have the correct offset, due to how
706 val_print was initially called. */
0e03807e
TT
707 real_type = value_rtti_type (value, &full, &top, &using_enc);
708 }
709
edf3d5f3
TT
710 if (!real_type)
711 real_type = type;
712
65408fa6 713 cp_print_value_fields (type, real_type, offset,
0e03807e 714 address, stream, recurse, val, options,
edf3d5f3
TT
715 dont_print_vb, dont_print_statmem);
716}
717
64b653ca 718/* Special value_print routine to avoid printing multiple copies of
aff410f1 719 virtual baseclasses. */
c906108c
SS
720
721static void
a2bd3dcd 722cp_print_value (struct type *type, struct type *real_type,
65408fa6 723 LONGEST offset,
aff410f1 724 CORE_ADDR address, struct ui_file *stream,
e8b24d9f 725 int recurse, struct value *val,
79a45b7d
TT
726 const struct value_print_options *options,
727 struct type **dont_print_vb)
c906108c 728{
c906108c 729 struct type **last_dont_print
2c63a960 730 = (struct type **) obstack_next_free (&dont_print_vb_obstack);
c1b6e682 731 struct obstack tmp_obstack = dont_print_vb_obstack;
c906108c 732 int i, n_baseclasses = TYPE_N_BASECLASSES (type);
6b850546 733 LONGEST thisoffset;
b9d652ac 734 struct type *thistype;
65408fa6 735 const gdb_byte *valaddr = value_contents_for_printing (val);
c906108c
SS
736
737 if (dont_print_vb == 0)
738 {
aff410f1
MS
739 /* If we're at top level, carve out a completely fresh chunk of
740 the obstack and use that until this particular invocation
741 returns. */
c906108c
SS
742 /* Bump up the high-water mark. Now alpha is omega. */
743 obstack_finish (&dont_print_vb_obstack);
744 }
745
746 for (i = 0; i < n_baseclasses; i++)
747 {
6b850546 748 LONGEST boffset = 0;
7556d4a4 749 int skip = 0;
c906108c 750 struct type *baseclass = check_typedef (TYPE_BASECLASS (type, i));
0d5cff50 751 const char *basename = TYPE_NAME (baseclass);
e8b24d9f 752 struct value *base_val = NULL;
c906108c
SS
753
754 if (BASETYPE_VIA_VIRTUAL (type, i))
755 {
756 struct type **first_dont_print
2c63a960 757 = (struct type **) obstack_base (&dont_print_vb_obstack);
c906108c 758
aff410f1
MS
759 int j = (struct type **)
760 obstack_next_free (&dont_print_vb_obstack) - first_dont_print;
c906108c
SS
761
762 while (--j >= 0)
763 if (baseclass == first_dont_print[j])
764 goto flush_it;
765
766 obstack_ptr_grow (&dont_print_vb_obstack, baseclass);
767 }
768
b9d652ac
DJ
769 thisoffset = offset;
770 thistype = real_type;
086280be 771
a70b8144 772 try
c5aa993b 773 {
8af8e3bc
PA
774 boffset = baseclass_offset (type, i, valaddr, offset, address, val);
775 }
230d2906 776 catch (const gdb_exception_error &ex)
7556d4a4
PA
777 {
778 if (ex.error == NOT_AVAILABLE_ERROR)
779 skip = -1;
780 else
781 skip = 1;
782 }
c906108c 783
7556d4a4
PA
784 if (skip == 0)
785 {
8af8e3bc 786 if (BASETYPE_VIA_VIRTUAL (type, i))
c5aa993b 787 {
8af8e3bc
PA
788 /* The virtual base class pointer might have been
789 clobbered by the user program. Make sure that it
790 still points to a valid memory location. */
791
792 if ((boffset + offset) < 0
793 || (boffset + offset) >= TYPE_LENGTH (real_type))
794 {
26fcd5d7 795 gdb::byte_vector buf (TYPE_LENGTH (baseclass));
d5161074 796
26fcd5d7 797 if (target_read_memory (address + boffset, buf.data (),
8af8e3bc
PA
798 TYPE_LENGTH (baseclass)) != 0)
799 skip = 1;
800 base_val = value_from_contents_and_address (baseclass,
26fcd5d7 801 buf.data (),
8af8e3bc 802 address + boffset);
9f1f738a 803 baseclass = value_type (base_val);
8af8e3bc
PA
804 thisoffset = 0;
805 boffset = 0;
806 thistype = baseclass;
8af8e3bc
PA
807 }
808 else
809 {
8af8e3bc
PA
810 base_val = val;
811 }
c5aa993b
JM
812 }
813 else
de4127a3 814 {
de4127a3
PA
815 base_val = val;
816 }
c906108c
SS
817 }
818
aff410f1 819 /* Now do the printing. */
2a998fc0 820 if (options->prettyformat)
c906108c
SS
821 {
822 fprintf_filtered (stream, "\n");
823 print_spaces_filtered (2 * recurse, stream);
824 }
825 fputs_filtered ("<", stream);
aff410f1
MS
826 /* Not sure what the best notation is in the case where there is
827 no baseclass name. */
c906108c
SS
828 fputs_filtered (basename ? basename : "", stream);
829 fputs_filtered ("> = ", stream);
830
8af8e3bc
PA
831 if (skip < 0)
832 val_print_unavailable (stream);
833 else if (skip > 0)
834 val_print_invalid_address (stream);
c906108c 835 else
a6bac58e
TT
836 {
837 int result = 0;
838
2e62ab40
AB
839 if (options->max_depth > -1
840 && recurse >= options->max_depth)
841 {
842 const struct language_defn *language = current_language;
843 gdb_assert (language->la_struct_too_deep_ellipsis != NULL);
844 fputs_filtered (language->la_struct_too_deep_ellipsis, stream);
845 }
846 else
847 {
848 /* Attempt to run an extension language pretty-printer on the
849 baseclass if possible. */
850 if (!options->raw)
851 result
852 = apply_ext_lang_val_pretty_printer (baseclass,
853 thisoffset + boffset,
854 value_address (base_val),
855 stream, recurse,
856 base_val, options,
857 current_language);
858
859 if (!result)
860 cp_print_value_fields (baseclass, thistype,
861 thisoffset + boffset,
862 value_address (base_val),
863 stream, recurse, base_val, options,
864 ((struct type **)
865 obstack_base (&dont_print_vb_obstack)),
866 0);
867 }
a6bac58e 868 }
c906108c
SS
869 fputs_filtered (", ", stream);
870
871 flush_it:
872 ;
873 }
874
875 if (dont_print_vb == 0)
876 {
877 /* Free the space used to deal with the printing
c5aa993b 878 of this type from top level. */
c906108c
SS
879 obstack_free (&dont_print_vb_obstack, last_dont_print);
880 /* Reset watermark so that we can continue protecting
c5aa993b 881 ourselves from whatever we were protecting ourselves. */
c906108c
SS
882 dont_print_vb_obstack = tmp_obstack;
883 }
884}
885
fbf54e75
TT
886/* Special val_print routine to avoid printing multiple copies of
887 virtual baseclasses. */
888
889static void
890cp_print_value (struct value *val, struct ui_file *stream,
891 int recurse, const struct value_print_options *options,
892 struct type **dont_print_vb)
893{
894 struct type *type = check_typedef (value_type (val));
895 CORE_ADDR address = value_address (val);
896 struct type **last_dont_print
897 = (struct type **) obstack_next_free (&dont_print_vb_obstack);
898 struct obstack tmp_obstack = dont_print_vb_obstack;
899 int i, n_baseclasses = TYPE_N_BASECLASSES (type);
900 const gdb_byte *valaddr = value_contents_for_printing (val);
901
902 if (dont_print_vb == 0)
903 {
904 /* If we're at top level, carve out a completely fresh chunk of
905 the obstack and use that until this particular invocation
906 returns. */
907 /* Bump up the high-water mark. Now alpha is omega. */
908 obstack_finish (&dont_print_vb_obstack);
909 }
910
911 for (i = 0; i < n_baseclasses; i++)
912 {
913 LONGEST boffset = 0;
914 int skip = 0;
915 struct type *baseclass = check_typedef (TYPE_BASECLASS (type, i));
916 const char *basename = TYPE_NAME (baseclass);
917 struct value *base_val = NULL;
918
919 if (BASETYPE_VIA_VIRTUAL (type, i))
920 {
921 struct type **first_dont_print
922 = (struct type **) obstack_base (&dont_print_vb_obstack);
923
924 int j = (struct type **)
925 obstack_next_free (&dont_print_vb_obstack) - first_dont_print;
926
927 while (--j >= 0)
928 if (baseclass == first_dont_print[j])
929 goto flush_it;
930
931 obstack_ptr_grow (&dont_print_vb_obstack, baseclass);
932 }
933
934 try
935 {
936 boffset = baseclass_offset (type, i, valaddr,
937 value_embedded_offset (val),
938 address, val);
939 }
940 catch (const gdb_exception_error &ex)
941 {
942 if (ex.error == NOT_AVAILABLE_ERROR)
943 skip = -1;
944 else
945 skip = 1;
946 }
947
948 if (skip == 0)
949 {
950 if (BASETYPE_VIA_VIRTUAL (type, i))
951 {
952 /* The virtual base class pointer might have been
953 clobbered by the user program. Make sure that it
954 still points to a valid memory location. */
955
956 if (boffset < 0 || boffset >= TYPE_LENGTH (type))
957 {
958 gdb::byte_vector buf (TYPE_LENGTH (baseclass));
959
960 if (target_read_memory (address + boffset, buf.data (),
961 TYPE_LENGTH (baseclass)) != 0)
962 skip = 1;
963 base_val = value_from_contents_and_address (baseclass,
964 buf.data (),
965 address + boffset);
966 baseclass = value_type (base_val);
967 boffset = 0;
968 }
969 else
970 {
971 base_val = val;
972 }
973 }
974 else
975 {
976 base_val = val;
977 }
978 }
979
980 /* Now do the printing. */
981 if (options->prettyformat)
982 {
983 fprintf_filtered (stream, "\n");
984 print_spaces_filtered (2 * recurse, stream);
985 }
986 fputs_filtered ("<", stream);
987 /* Not sure what the best notation is in the case where there is
988 no baseclass name. */
989 fputs_filtered (basename ? basename : "", stream);
990 fputs_filtered ("> = ", stream);
991
992 if (skip < 0)
993 val_print_unavailable (stream);
994 else if (skip > 0)
995 val_print_invalid_address (stream);
996 else
997 {
998 int result = 0;
999
1000 if (options->max_depth > -1
1001 && recurse >= options->max_depth)
1002 {
1003 const struct language_defn *language = current_language;
1004 gdb_assert (language->la_struct_too_deep_ellipsis != NULL);
1005 fputs_filtered (language->la_struct_too_deep_ellipsis, stream);
1006 }
1007 else
1008 {
1009 /* Attempt to run an extension language pretty-printer on the
1010 baseclass if possible. */
1011 if (!options->raw)
1012 result
1013 = apply_ext_lang_val_pretty_printer (baseclass, boffset,
1014 value_address (base_val),
1015 stream, recurse,
1016 base_val, options,
1017 current_language);
1018
1019 if (!result)
1020 cp_print_value_fields (value_primitive_field (val, 0, i, type),
1021 stream, recurse, options,
1022 ((struct type **)
1023 obstack_base (&dont_print_vb_obstack)),
1024 0);
1025 }
1026 }
1027 fputs_filtered (", ", stream);
1028
1029 flush_it:
1030 ;
1031 }
1032
1033 if (dont_print_vb == 0)
1034 {
1035 /* Free the space used to deal with the printing
1036 of this type from top level. */
1037 obstack_free (&dont_print_vb_obstack, last_dont_print);
1038 /* Reset watermark so that we can continue protecting
1039 ourselves from whatever we were protecting ourselves. */
1040 dont_print_vb_obstack = tmp_obstack;
1041 }
1042}
1043
aff410f1
MS
1044/* Print value of a static member. To avoid infinite recursion when
1045 printing a class that contains a static instance of the class, we
1046 keep the addresses of all printed static member classes in an
1047 obstack and refuse to print them more than once.
c906108c 1048
79a45b7d 1049 VAL contains the value to print, TYPE, STREAM, RECURSE, and OPTIONS
c906108c
SS
1050 have the same meanings as in c_val_print. */
1051
1052static void
2c63a960 1053cp_print_static_field (struct type *type,
6943961c 1054 struct value *val,
2c63a960 1055 struct ui_file *stream,
2c63a960 1056 int recurse,
79a45b7d 1057 const struct value_print_options *options)
c906108c 1058{
79a45b7d 1059 struct value_print_options opts;
686d4def
PA
1060
1061 if (value_entirely_optimized_out (val))
1062 {
1063 val_print_optimized_out (val, stream);
1064 return;
1065 }
1066
79f18731
WP
1067 struct type *real_type = check_typedef (type);
1068 if (TYPE_CODE (real_type) == TYPE_CODE_STRUCT)
c906108c
SS
1069 {
1070 CORE_ADDR *first_dont_print;
42ae5230 1071 CORE_ADDR addr;
c906108c
SS
1072 int i;
1073
1074 first_dont_print
c5aa993b 1075 = (CORE_ADDR *) obstack_base (&dont_print_statmem_obstack);
99903ae3
CM
1076 i = obstack_object_size (&dont_print_statmem_obstack)
1077 / sizeof (CORE_ADDR);
c906108c
SS
1078
1079 while (--i >= 0)
1080 {
42ae5230 1081 if (value_address (val) == first_dont_print[i])
c906108c 1082 {
2dbc041e
TT
1083 fputs_styled (_("<same as static member of an already"
1084 " seen type>"),
1085 metadata_style.style (), stream);
c906108c
SS
1086 return;
1087 }
1088 }
1089
42ae5230
TT
1090 addr = value_address (val);
1091 obstack_grow (&dont_print_statmem_obstack, (char *) &addr,
c906108c 1092 sizeof (CORE_ADDR));
edf3d5f3 1093 cp_print_value_fields (type, value_enclosing_type (val),
42ae5230 1094 value_embedded_offset (val), addr,
aff410f1
MS
1095 stream, recurse, val,
1096 options, NULL, 1);
c906108c
SS
1097 return;
1098 }
79a45b7d 1099
79f18731 1100 if (TYPE_CODE (real_type) == TYPE_CODE_ARRAY)
ec31cde5
CM
1101 {
1102 struct type **first_dont_print;
1103 int i;
1104 struct type *target_type = TYPE_TARGET_TYPE (type);
1105
1106 first_dont_print
1107 = (struct type **) obstack_base (&dont_print_stat_array_obstack);
1108 i = obstack_object_size (&dont_print_stat_array_obstack)
1e9beacb 1109 / sizeof (struct type *);
ec31cde5
CM
1110
1111 while (--i >= 0)
1112 {
1113 if (target_type == first_dont_print[i])
1114 {
2dbc041e
TT
1115 fputs_styled (_("<same as static member of an already"
1116 " seen type>"),
1117 metadata_style.style (), stream);
ec31cde5
CM
1118 return;
1119 }
1120 }
1121
aff410f1
MS
1122 obstack_grow (&dont_print_stat_array_obstack,
1123 (char *) &target_type,
ec31cde5
CM
1124 sizeof (struct type *));
1125 }
1126
79a45b7d
TT
1127 opts = *options;
1128 opts.deref_ref = 0;
410cf315 1129 common_val_print (val, stream, recurse, &opts, current_language);
c906108c
SS
1130}
1131
09e2d7c7
DE
1132/* Find the field in *SELF, or its non-virtual base classes, with
1133 bit offset OFFSET. Set *SELF to the containing type and *FIELDNO
aff410f1 1134 to the containing field number. If OFFSET is not exactly at the
09e2d7c7 1135 start of some field, set *SELF to NULL. */
0d5de010 1136
2c0b251b 1137static void
09e2d7c7 1138cp_find_class_member (struct type **self_p, int *fieldno,
0d5de010
DJ
1139 LONGEST offset)
1140{
09e2d7c7 1141 struct type *self;
0d5de010
DJ
1142 unsigned int i;
1143 unsigned len;
1144
09e2d7c7
DE
1145 *self_p = check_typedef (*self_p);
1146 self = *self_p;
1147 len = TYPE_NFIELDS (self);
0d5de010 1148
09e2d7c7 1149 for (i = TYPE_N_BASECLASSES (self); i < len; i++)
0d5de010 1150 {
09e2d7c7 1151 LONGEST bitpos = TYPE_FIELD_BITPOS (self, i);
0d5de010
DJ
1152
1153 QUIT;
1154 if (offset == bitpos)
1155 {
1156 *fieldno = i;
1157 return;
1158 }
1159 }
1160
09e2d7c7 1161 for (i = 0; i < TYPE_N_BASECLASSES (self); i++)
0d5de010 1162 {
09e2d7c7
DE
1163 LONGEST bitpos = TYPE_FIELD_BITPOS (self, i);
1164 LONGEST bitsize = 8 * TYPE_LENGTH (TYPE_FIELD_TYPE (self, i));
0d5de010
DJ
1165
1166 if (offset >= bitpos && offset < bitpos + bitsize)
1167 {
09e2d7c7
DE
1168 *self_p = TYPE_FIELD_TYPE (self, i);
1169 cp_find_class_member (self_p, fieldno, offset - bitpos);
0d5de010
DJ
1170 return;
1171 }
1172 }
1173
09e2d7c7 1174 *self_p = NULL;
0d5de010
DJ
1175}
1176
c906108c 1177void
ad4820ab 1178cp_print_class_member (const gdb_byte *valaddr, struct type *type,
a121b7c1 1179 struct ui_file *stream, const char *prefix)
c906108c 1180{
34877895 1181 enum bfd_endian byte_order = type_byte_order (type);
e17a4113 1182
09e2d7c7 1183 /* VAL is a byte offset into the structure type SELF_TYPE.
c906108c
SS
1184 Find the name of the field for that offset and
1185 print it. */
09e2d7c7 1186 struct type *self_type = TYPE_SELF_TYPE (type);
e17a4113 1187 LONGEST val;
9f8afa72 1188 int fieldno;
c906108c 1189
aff410f1
MS
1190 val = extract_signed_integer (valaddr,
1191 TYPE_LENGTH (type),
1192 byte_order);
e17a4113 1193
0d5de010
DJ
1194 /* Pointers to data members are usually byte offsets into an object.
1195 Because a data member can have offset zero, and a NULL pointer to
1196 member must be distinct from any valid non-NULL pointer to
1197 member, either the value is biased or the NULL value has a
1198 special representation; both are permitted by ISO C++. HP aCC
1199 used a bias of 0x20000000; HP cfront used a bias of 1; g++ 3.x
1200 and other compilers which use the Itanium ABI use -1 as the NULL
1201 value. GDB only supports that last form; to add support for
1202 another form, make this into a cp-abi hook. */
c906108c 1203
0d5de010 1204 if (val == -1)
c906108c 1205 {
0d5de010
DJ
1206 fprintf_filtered (stream, "NULL");
1207 return;
c906108c 1208 }
0d5de010 1209
09e2d7c7 1210 cp_find_class_member (&self_type, &fieldno, val << 3);
0d5de010 1211
09e2d7c7 1212 if (self_type != NULL)
c906108c 1213 {
0d5cff50 1214 const char *name;
c5504eaf 1215
306d9ac5 1216 fputs_filtered (prefix, stream);
a737d952 1217 name = TYPE_NAME (self_type);
c906108c 1218 if (name)
c5aa993b 1219 fputs_filtered (name, stream);
c906108c 1220 else
09e2d7c7 1221 c_type_print_base (self_type, stream, 0, 0, &type_print_raw_options);
c906108c 1222 fprintf_filtered (stream, "::");
3f0cbb04
TT
1223 fputs_styled (TYPE_FIELD_NAME (self_type, fieldno),
1224 variable_name_style.style (), stream);
c906108c
SS
1225 }
1226 else
0d5de010 1227 fprintf_filtered (stream, "%ld", (long) val);
c906108c
SS
1228}
1229
1230
6c265988 1231void _initialize_cp_valprint ();
c906108c 1232void
6c265988 1233_initialize_cp_valprint ()
c906108c 1234{
aff410f1
MS
1235 obstack_begin (&dont_print_stat_array_obstack,
1236 32 * sizeof (struct type *));
1237 obstack_begin (&dont_print_statmem_obstack,
1238 32 * sizeof (CORE_ADDR));
1239 obstack_begin (&dont_print_vb_obstack,
1240 32 * sizeof (struct type *));
c906108c 1241}
This page took 1.546933 seconds and 4 git commands to generate.