]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gdb/cp-valprint.c
Change Ada catchpoints to be bp_catchpoint
[thirdparty/binutils-gdb.git] / gdb / cp-valprint.c
CommitLineData
c906108c 1/* Support for printing C++ values for GDB, the GNU debugger.
a2bd3dcd 2
42a4f53d 3 Copyright (C) 1986-2019 Free Software Foundation, Inc.
c906108c 4
c5aa993b 5 This file is part of GDB.
c906108c 6
c5aa993b
JM
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
a9762ec7 9 the Free Software Foundation; either version 3 of the License, or
c5aa993b 10 (at your option) any later version.
c906108c 11
c5aa993b
JM
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
c906108c 16
c5aa993b 17 You should have received a copy of the GNU General Public License
a9762ec7 18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
c906108c
SS
19
20#include "defs.h"
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"
c906108c 39
c906108c
SS
40static struct obstack dont_print_vb_obstack;
41static struct obstack dont_print_statmem_obstack;
ec31cde5 42static struct obstack dont_print_stat_array_obstack;
c906108c 43
6943961c 44static void cp_print_static_field (struct type *, struct value *,
79a45b7d
TT
45 struct ui_file *, int,
46 const struct value_print_options *);
c906108c 47
aff410f1 48static void cp_print_value (struct type *, struct type *,
65408fa6 49 LONGEST,
aff410f1 50 CORE_ADDR, struct ui_file *,
e8b24d9f 51 int, struct value *,
aff410f1
MS
52 const struct value_print_options *,
53 struct type **);
c906108c 54
c906108c 55
8343f86c 56/* GCC versions after 2.4.5 use this. */
ddb6d633 57extern const char vtbl_ptr_name[] = "__vtbl_ptr_type";
c906108c 58
c906108c
SS
59/* Return truth value for assertion that TYPE is of the type
60 "pointer to virtual function". */
61
62int
fba45db2 63cp_is_vtbl_ptr_type (struct type *type)
c906108c 64{
a737d952 65 const char *type_name = TYPE_NAME (type);
c906108c 66
fe978cb0 67 return (type_name != NULL && !strcmp (type_name, vtbl_ptr_name));
c906108c
SS
68}
69
70/* Return truth value for the assertion that TYPE is of the type
71 "pointer to virtual function table". */
72
73int
fba45db2 74cp_is_vtbl_member (struct type *type)
c906108c 75{
aff410f1
MS
76 /* With older versions of g++, the vtbl field pointed to an array of
77 structures. Nowadays it points directly to the structure. */
c906108c
SS
78 if (TYPE_CODE (type) == TYPE_CODE_PTR)
79 {
80 type = TYPE_TARGET_TYPE (type);
81 if (TYPE_CODE (type) == TYPE_CODE_ARRAY)
82 {
83 type = TYPE_TARGET_TYPE (type);
aff410f1
MS
84 if (TYPE_CODE (type) == TYPE_CODE_STRUCT /* if not using thunks */
85 || TYPE_CODE (type) == TYPE_CODE_PTR) /* if using thunks */
c906108c
SS
86 {
87 /* Virtual functions tables are full of pointers
aff410f1 88 to virtual functions. */
c906108c
SS
89 return cp_is_vtbl_ptr_type (type);
90 }
91 }
0e5e3ea6
PS
92 else if (TYPE_CODE (type) == TYPE_CODE_STRUCT) /* if not using thunks */
93 {
94 return cp_is_vtbl_ptr_type (type);
95 }
96 else if (TYPE_CODE (type) == TYPE_CODE_PTR) /* if using thunks */
97 {
aff410f1
MS
98 /* The type name of the thunk pointer is NULL when using
99 dwarf2. We could test for a pointer to a function, but
100 there is no type info for the virtual table either, so it
101 wont help. */
0e5e3ea6
PS
102 return cp_is_vtbl_ptr_type (type);
103 }
c906108c
SS
104 }
105 return 0;
106}
107
108/* Mutually recursive subroutines of cp_print_value and c_val_print to
aff410f1
MS
109 print out a structure's fields: cp_print_value_fields and
110 cp_print_value.
c5aa993b 111
aff410f1
MS
112 TYPE, VALADDR, ADDRESS, STREAM, RECURSE, and OPTIONS have the same
113 meanings as in cp_print_value and c_val_print.
c906108c 114
aff410f1
MS
115 2nd argument REAL_TYPE is used to carry over the type of the
116 derived class across the recursion to base classes.
c906108c 117
aff410f1
MS
118 DONT_PRINT is an array of baseclass types that we should not print,
119 or zero if called from top level. */
c906108c
SS
120
121void
a2bd3dcd 122cp_print_value_fields (struct type *type, struct type *real_type,
65408fa6 123 LONGEST offset,
aff410f1 124 CORE_ADDR address, struct ui_file *stream,
e8b24d9f 125 int recurse, struct value *val,
79a45b7d 126 const struct value_print_options *options,
aff410f1
MS
127 struct type **dont_print_vb,
128 int dont_print_statmem)
c906108c
SS
129{
130 int i, len, n_baseclasses;
c906108c 131 int fields_seen = 0;
ec31cde5 132 static int last_set_recurse = -1;
c906108c 133
f168693b 134 type = check_typedef (type);
99903ae3 135
ec31cde5
CM
136 if (recurse == 0)
137 {
aff410f1
MS
138 /* Any object can be left on obstacks only during an unexpected
139 error. */
6036c742 140
ec31cde5 141 if (obstack_object_size (&dont_print_statmem_obstack) > 0)
6036c742
JK
142 {
143 obstack_free (&dont_print_statmem_obstack, NULL);
aff410f1
MS
144 obstack_begin (&dont_print_statmem_obstack,
145 32 * sizeof (CORE_ADDR));
6036c742 146 }
ec31cde5 147 if (obstack_object_size (&dont_print_stat_array_obstack) > 0)
6036c742
JK
148 {
149 obstack_free (&dont_print_stat_array_obstack, NULL);
150 obstack_begin (&dont_print_stat_array_obstack,
151 32 * sizeof (struct type *));
152 }
ec31cde5 153 }
c906108c
SS
154
155 fprintf_filtered (stream, "{");
156 len = TYPE_NFIELDS (type);
157 n_baseclasses = TYPE_N_BASECLASSES (type);
158
159 /* First, print out baseclasses such that we don't print
160 duplicates of virtual baseclasses. */
161
162 if (n_baseclasses > 0)
65408fa6 163 cp_print_value (type, real_type,
aff410f1
MS
164 offset, address, stream,
165 recurse + 1, val, options,
166 dont_print_vb);
c906108c
SS
167
168 /* Second, print out data fields */
169
086280be
UW
170 /* If there are no data fields, skip this part */
171 if (len == n_baseclasses || !len)
c906108c
SS
172 fprintf_filtered (stream, "<No data fields>");
173 else
174 {
241fd515
AM
175 size_t statmem_obstack_initial_size = 0;
176 size_t stat_array_obstack_initial_size = 0;
7977e5d2
TT
177 struct type *vptr_basetype = NULL;
178 int vptr_fieldno;
179
c906108c
SS
180 if (dont_print_statmem == 0)
181 {
f56dcb88 182 statmem_obstack_initial_size =
0b66f317 183 obstack_object_size (&dont_print_statmem_obstack);
ec31cde5
CM
184
185 if (last_set_recurse != recurse)
186 {
f56dcb88
CM
187 stat_array_obstack_initial_size =
188 obstack_object_size (&dont_print_stat_array_obstack);
c5504eaf 189
ec31cde5
CM
190 last_set_recurse = recurse;
191 }
c906108c
SS
192 }
193
7977e5d2 194 vptr_fieldno = get_vptr_fieldno (type, &vptr_basetype);
c906108c
SS
195 for (i = n_baseclasses; i < len; i++)
196 {
65408fa6
YQ
197 const gdb_byte *valaddr = value_contents_for_printing (val);
198
c906108c 199 /* If requested, skip printing of static fields. */
79a45b7d 200 if (!options->static_field_print
d6a843b5 201 && field_is_static (&TYPE_FIELD (type, i)))
c906108c
SS
202 continue;
203
c906108c 204 if (fields_seen)
18c77628
AB
205 {
206 fputs_filtered (",", stream);
207 if (!options->prettyformat)
208 fputs_filtered (" ", stream);
209 }
c906108c
SS
210 else if (n_baseclasses > 0)
211 {
2a998fc0 212 if (options->prettyformat)
c906108c
SS
213 {
214 fprintf_filtered (stream, "\n");
215 print_spaces_filtered (2 + 2 * recurse, stream);
216 fputs_filtered ("members of ", stream);
a737d952 217 fputs_filtered (TYPE_NAME (type), stream);
18c77628 218 fputs_filtered (":", stream);
c906108c
SS
219 }
220 }
221 fields_seen = 1;
222
2a998fc0 223 if (options->prettyformat)
c906108c
SS
224 {
225 fprintf_filtered (stream, "\n");
226 print_spaces_filtered (2 + 2 * recurse, stream);
227 }
c5aa993b 228 else
c906108c
SS
229 {
230 wrap_here (n_spaces (2 + 2 * recurse));
231 }
e93a8774
TT
232
233 annotate_field_begin (TYPE_FIELD_TYPE (type, i));
234
235 if (field_is_static (&TYPE_FIELD (type, i)))
236 fputs_filtered ("static ", stream);
237 fprintf_symbol_filtered (stream,
238 TYPE_FIELD_NAME (type, i),
239 current_language->la_language,
240 DMGL_PARAMS | DMGL_ANSI);
241 annotate_field_name_end ();
2e62ab40
AB
242
243 /* We tweak various options in a few cases below. */
244 value_print_options options_copy = *options;
245 value_print_options *opts = &options_copy;
246
e93a8774
TT
247 /* Do not print leading '=' in case of anonymous
248 unions. */
249 if (strcmp (TYPE_FIELD_NAME (type, i), ""))
250 fputs_filtered (" = ", stream);
2e62ab40
AB
251 else
252 {
253 /* If this is an anonymous field then we want to consider it
254 as though it is at its parent's depth when it comes to the
255 max print depth. */
256 if (opts->max_depth != -1 && opts->max_depth < INT_MAX)
257 ++opts->max_depth;
258 }
e93a8774 259 annotate_field_value ();
c906108c 260
d6a843b5
JK
261 if (!field_is_static (&TYPE_FIELD (type, i))
262 && TYPE_FIELD_PACKED (type, i))
c906108c 263 {
6943961c 264 struct value *v;
c906108c 265
aff410f1
MS
266 /* Bitfields require special handling, especially due to
267 byte order problems. */
c906108c
SS
268 if (TYPE_FIELD_IGNORE (type, i))
269 {
c5aa993b 270 fputs_filtered ("<optimized out or zero length>", stream);
c906108c 271 }
8cf6f0b1
TT
272 else if (value_bits_synthetic_pointer (val,
273 TYPE_FIELD_BITPOS (type,
274 i),
275 TYPE_FIELD_BITSIZE (type,
276 i)))
277 {
278 fputs_filtered (_("<synthetic pointer>"), stream);
279 }
c906108c
SS
280 else
281 {
2e62ab40 282 opts->deref_ref = 0;
5467c6c8
PA
283
284 v = value_field_bitfield (type, i, valaddr, offset, val);
c906108c 285
2e62ab40
AB
286 common_val_print (v, stream, recurse + 1,
287 opts, current_language);
c906108c
SS
288 }
289 }
290 else
291 {
292 if (TYPE_FIELD_IGNORE (type, i))
293 {
aff410f1
MS
294 fputs_filtered ("<optimized out or zero length>",
295 stream);
c906108c 296 }
d6a843b5 297 else if (field_is_static (&TYPE_FIELD (type, i)))
c906108c 298 {
a70b8144 299 try
ee86786c 300 {
4330d61d 301 struct value *v = value_static_field (type, i);
ee86786c 302
4330d61d
TT
303 cp_print_static_field (TYPE_FIELD_TYPE (type, i),
304 v, stream, recurse + 1,
3d507ff2 305 opts);
4330d61d 306 }
230d2906 307 catch (const gdb_exception_error &ex)
492d29ea
PA
308 {
309 fprintf_filtered (stream,
310 _("<error reading variable: %s>"),
3d6e9d23 311 ex.what ());
492d29ea 312 }
c906108c 313 }
7977e5d2 314 else if (i == vptr_fieldno && type == vptr_basetype)
410528f0 315 {
a72c8f6a
JK
316 int i_offset = offset + TYPE_FIELD_BITPOS (type, i) / 8;
317 struct type *i_type = TYPE_FIELD_TYPE (type, i);
318
319 if (valprint_check_validity (stream, i_type, i_offset, val))
320 {
321 CORE_ADDR addr;
322
323 addr = extract_typed_address (valaddr + i_offset, i_type);
2e62ab40 324 print_function_pointer_address (opts,
edf0c1b7
TT
325 get_type_arch (type),
326 addr, stream);
a72c8f6a 327 }
410528f0 328 }
c906108c
SS
329 else
330 {
2e62ab40 331 opts->deref_ref = 0;
c5aa993b 332 val_print (TYPE_FIELD_TYPE (type, i),
aff410f1 333 offset + TYPE_FIELD_BITPOS (type, i) / 8,
edf3d5f3 334 address,
2e62ab40 335 stream, recurse + 1, val, opts,
d8ca156b 336 current_language);
c906108c
SS
337 }
338 }
339 annotate_field_end ();
340 }
341
342 if (dont_print_statmem == 0)
343 {
241fd515 344 size_t obstack_final_size =
0b66f317
CM
345 obstack_object_size (&dont_print_statmem_obstack);
346
aff410f1
MS
347 if (obstack_final_size > statmem_obstack_initial_size)
348 {
349 /* In effect, a pop of the printed-statics stack. */
a43f3893
PF
350 size_t shrink_bytes
351 = statmem_obstack_initial_size - obstack_final_size;
352 obstack_blank_fast (&dont_print_statmem_obstack, shrink_bytes);
aff410f1 353 }
ec31cde5
CM
354
355 if (last_set_recurse != recurse)
356 {
b926417a 357 obstack_final_size =
f56dcb88
CM
358 obstack_object_size (&dont_print_stat_array_obstack);
359
360 if (obstack_final_size > stat_array_obstack_initial_size)
361 {
362 void *free_to_ptr =
1ae1b8cc 363 (char *) obstack_next_free (&dont_print_stat_array_obstack)
aff410f1
MS
364 - (obstack_final_size
365 - stat_array_obstack_initial_size);
f56dcb88
CM
366
367 obstack_free (&dont_print_stat_array_obstack,
368 free_to_ptr);
369 }
ec31cde5
CM
370 last_set_recurse = -1;
371 }
c906108c
SS
372 }
373
2a998fc0 374 if (options->prettyformat)
c906108c
SS
375 {
376 fprintf_filtered (stream, "\n");
377 print_spaces_filtered (2 * recurse, stream);
378 }
c5aa993b 379 } /* if there are data fields */
c5aa993b 380
c906108c
SS
381 fprintf_filtered (stream, "}");
382}
383
edf3d5f3
TT
384/* Like cp_print_value_fields, but find the runtime type of the object
385 and pass it as the `real_type' argument to cp_print_value_fields.
386 This function is a hack to work around the fact that
387 common_val_print passes the embedded offset to val_print, but not
388 the enclosing type. */
389
390void
391cp_print_value_fields_rtti (struct type *type,
6b850546 392 const gdb_byte *valaddr, LONGEST offset,
edf3d5f3
TT
393 CORE_ADDR address,
394 struct ui_file *stream, int recurse,
e8b24d9f 395 struct value *val,
edf3d5f3 396 const struct value_print_options *options,
c5504eaf
MS
397 struct type **dont_print_vb,
398 int dont_print_statmem)
edf3d5f3 399{
0e03807e
TT
400 struct type *real_type = NULL;
401
402 /* We require all bits to be valid in order to attempt a
403 conversion. */
9a0dc9e3
PA
404 if (!value_bits_any_optimized_out (val,
405 TARGET_CHAR_BIT * offset,
406 TARGET_CHAR_BIT * TYPE_LENGTH (type)))
0e03807e
TT
407 {
408 struct value *value;
6b850546
DT
409 int full, using_enc;
410 LONGEST top;
0e03807e
TT
411
412 /* Ugh, we have to convert back to a value here. */
413 value = value_from_contents_and_address (type, valaddr + offset,
414 address + offset);
9f1f738a 415 type = value_type (value);
aff410f1
MS
416 /* We don't actually care about most of the result here -- just
417 the type. We already have the correct offset, due to how
418 val_print was initially called. */
0e03807e
TT
419 real_type = value_rtti_type (value, &full, &top, &using_enc);
420 }
421
edf3d5f3
TT
422 if (!real_type)
423 real_type = type;
424
65408fa6 425 cp_print_value_fields (type, real_type, offset,
0e03807e 426 address, stream, recurse, val, options,
edf3d5f3
TT
427 dont_print_vb, dont_print_statmem);
428}
429
aff410f1
MS
430/* Special val_print routine to avoid printing multiple copies of
431 virtual baseclasses. */
c906108c
SS
432
433static void
a2bd3dcd 434cp_print_value (struct type *type, struct type *real_type,
65408fa6 435 LONGEST offset,
aff410f1 436 CORE_ADDR address, struct ui_file *stream,
e8b24d9f 437 int recurse, struct value *val,
79a45b7d
TT
438 const struct value_print_options *options,
439 struct type **dont_print_vb)
c906108c 440{
c906108c 441 struct type **last_dont_print
2c63a960 442 = (struct type **) obstack_next_free (&dont_print_vb_obstack);
c1b6e682 443 struct obstack tmp_obstack = dont_print_vb_obstack;
c906108c 444 int i, n_baseclasses = TYPE_N_BASECLASSES (type);
6b850546 445 LONGEST thisoffset;
b9d652ac 446 struct type *thistype;
65408fa6 447 const gdb_byte *valaddr = value_contents_for_printing (val);
c906108c
SS
448
449 if (dont_print_vb == 0)
450 {
aff410f1
MS
451 /* If we're at top level, carve out a completely fresh chunk of
452 the obstack and use that until this particular invocation
453 returns. */
c906108c
SS
454 /* Bump up the high-water mark. Now alpha is omega. */
455 obstack_finish (&dont_print_vb_obstack);
456 }
457
458 for (i = 0; i < n_baseclasses; i++)
459 {
6b850546 460 LONGEST boffset = 0;
7556d4a4 461 int skip = 0;
c906108c 462 struct type *baseclass = check_typedef (TYPE_BASECLASS (type, i));
0d5cff50 463 const char *basename = TYPE_NAME (baseclass);
e8b24d9f 464 struct value *base_val = NULL;
c906108c
SS
465
466 if (BASETYPE_VIA_VIRTUAL (type, i))
467 {
468 struct type **first_dont_print
2c63a960 469 = (struct type **) obstack_base (&dont_print_vb_obstack);
c906108c 470
aff410f1
MS
471 int j = (struct type **)
472 obstack_next_free (&dont_print_vb_obstack) - first_dont_print;
c906108c
SS
473
474 while (--j >= 0)
475 if (baseclass == first_dont_print[j])
476 goto flush_it;
477
478 obstack_ptr_grow (&dont_print_vb_obstack, baseclass);
479 }
480
b9d652ac
DJ
481 thisoffset = offset;
482 thistype = real_type;
086280be 483
a70b8144 484 try
c5aa993b 485 {
8af8e3bc
PA
486 boffset = baseclass_offset (type, i, valaddr, offset, address, val);
487 }
230d2906 488 catch (const gdb_exception_error &ex)
7556d4a4
PA
489 {
490 if (ex.error == NOT_AVAILABLE_ERROR)
491 skip = -1;
492 else
493 skip = 1;
494 }
c906108c 495
7556d4a4
PA
496 if (skip == 0)
497 {
8af8e3bc 498 if (BASETYPE_VIA_VIRTUAL (type, i))
c5aa993b 499 {
8af8e3bc
PA
500 /* The virtual base class pointer might have been
501 clobbered by the user program. Make sure that it
502 still points to a valid memory location. */
503
504 if ((boffset + offset) < 0
505 || (boffset + offset) >= TYPE_LENGTH (real_type))
506 {
26fcd5d7 507 gdb::byte_vector buf (TYPE_LENGTH (baseclass));
d5161074 508
26fcd5d7 509 if (target_read_memory (address + boffset, buf.data (),
8af8e3bc
PA
510 TYPE_LENGTH (baseclass)) != 0)
511 skip = 1;
512 base_val = value_from_contents_and_address (baseclass,
26fcd5d7 513 buf.data (),
8af8e3bc 514 address + boffset);
9f1f738a 515 baseclass = value_type (base_val);
8af8e3bc
PA
516 thisoffset = 0;
517 boffset = 0;
518 thistype = baseclass;
8af8e3bc
PA
519 }
520 else
521 {
8af8e3bc
PA
522 base_val = val;
523 }
c5aa993b
JM
524 }
525 else
de4127a3 526 {
de4127a3
PA
527 base_val = val;
528 }
c906108c
SS
529 }
530
aff410f1 531 /* Now do the printing. */
2a998fc0 532 if (options->prettyformat)
c906108c
SS
533 {
534 fprintf_filtered (stream, "\n");
535 print_spaces_filtered (2 * recurse, stream);
536 }
537 fputs_filtered ("<", stream);
aff410f1
MS
538 /* Not sure what the best notation is in the case where there is
539 no baseclass name. */
c906108c
SS
540 fputs_filtered (basename ? basename : "", stream);
541 fputs_filtered ("> = ", stream);
542
8af8e3bc
PA
543 if (skip < 0)
544 val_print_unavailable (stream);
545 else if (skip > 0)
546 val_print_invalid_address (stream);
c906108c 547 else
a6bac58e
TT
548 {
549 int result = 0;
550
2e62ab40
AB
551 if (options->max_depth > -1
552 && recurse >= options->max_depth)
553 {
554 const struct language_defn *language = current_language;
555 gdb_assert (language->la_struct_too_deep_ellipsis != NULL);
556 fputs_filtered (language->la_struct_too_deep_ellipsis, stream);
557 }
558 else
559 {
560 /* Attempt to run an extension language pretty-printer on the
561 baseclass if possible. */
562 if (!options->raw)
563 result
564 = apply_ext_lang_val_pretty_printer (baseclass,
565 thisoffset + boffset,
566 value_address (base_val),
567 stream, recurse,
568 base_val, options,
569 current_language);
570
571 if (!result)
572 cp_print_value_fields (baseclass, thistype,
573 thisoffset + boffset,
574 value_address (base_val),
575 stream, recurse, base_val, options,
576 ((struct type **)
577 obstack_base (&dont_print_vb_obstack)),
578 0);
579 }
a6bac58e 580 }
c906108c
SS
581 fputs_filtered (", ", stream);
582
583 flush_it:
584 ;
585 }
586
587 if (dont_print_vb == 0)
588 {
589 /* Free the space used to deal with the printing
c5aa993b 590 of this type from top level. */
c906108c
SS
591 obstack_free (&dont_print_vb_obstack, last_dont_print);
592 /* Reset watermark so that we can continue protecting
c5aa993b 593 ourselves from whatever we were protecting ourselves. */
c906108c
SS
594 dont_print_vb_obstack = tmp_obstack;
595 }
596}
597
aff410f1
MS
598/* Print value of a static member. To avoid infinite recursion when
599 printing a class that contains a static instance of the class, we
600 keep the addresses of all printed static member classes in an
601 obstack and refuse to print them more than once.
c906108c 602
79a45b7d 603 VAL contains the value to print, TYPE, STREAM, RECURSE, and OPTIONS
c906108c
SS
604 have the same meanings as in c_val_print. */
605
606static void
2c63a960 607cp_print_static_field (struct type *type,
6943961c 608 struct value *val,
2c63a960 609 struct ui_file *stream,
2c63a960 610 int recurse,
79a45b7d 611 const struct value_print_options *options)
c906108c 612{
79a45b7d 613 struct value_print_options opts;
686d4def
PA
614
615 if (value_entirely_optimized_out (val))
616 {
617 val_print_optimized_out (val, stream);
618 return;
619 }
620
79f18731
WP
621 struct type *real_type = check_typedef (type);
622 if (TYPE_CODE (real_type) == TYPE_CODE_STRUCT)
c906108c
SS
623 {
624 CORE_ADDR *first_dont_print;
42ae5230 625 CORE_ADDR addr;
c906108c
SS
626 int i;
627
628 first_dont_print
c5aa993b 629 = (CORE_ADDR *) obstack_base (&dont_print_statmem_obstack);
99903ae3
CM
630 i = obstack_object_size (&dont_print_statmem_obstack)
631 / sizeof (CORE_ADDR);
c906108c
SS
632
633 while (--i >= 0)
634 {
42ae5230 635 if (value_address (val) == first_dont_print[i])
c906108c 636 {
2c63a960
JB
637 fputs_filtered ("<same as static member of an already"
638 " seen type>",
c906108c
SS
639 stream);
640 return;
641 }
642 }
643
42ae5230
TT
644 addr = value_address (val);
645 obstack_grow (&dont_print_statmem_obstack, (char *) &addr,
c906108c 646 sizeof (CORE_ADDR));
edf3d5f3 647 cp_print_value_fields (type, value_enclosing_type (val),
42ae5230 648 value_embedded_offset (val), addr,
aff410f1
MS
649 stream, recurse, val,
650 options, NULL, 1);
c906108c
SS
651 return;
652 }
79a45b7d 653
79f18731 654 if (TYPE_CODE (real_type) == TYPE_CODE_ARRAY)
ec31cde5
CM
655 {
656 struct type **first_dont_print;
657 int i;
658 struct type *target_type = TYPE_TARGET_TYPE (type);
659
660 first_dont_print
661 = (struct type **) obstack_base (&dont_print_stat_array_obstack);
662 i = obstack_object_size (&dont_print_stat_array_obstack)
1e9beacb 663 / sizeof (struct type *);
ec31cde5
CM
664
665 while (--i >= 0)
666 {
667 if (target_type == first_dont_print[i])
668 {
669 fputs_filtered ("<same as static member of an already"
670 " seen type>",
671 stream);
672 return;
673 }
674 }
675
aff410f1
MS
676 obstack_grow (&dont_print_stat_array_obstack,
677 (char *) &target_type,
ec31cde5
CM
678 sizeof (struct type *));
679 }
680
79a45b7d
TT
681 opts = *options;
682 opts.deref_ref = 0;
e8b24d9f 683 val_print (type,
aff410f1
MS
684 value_embedded_offset (val),
685 value_address (val),
686 stream, recurse, val,
687 &opts, current_language);
c906108c
SS
688}
689
09e2d7c7
DE
690/* Find the field in *SELF, or its non-virtual base classes, with
691 bit offset OFFSET. Set *SELF to the containing type and *FIELDNO
aff410f1 692 to the containing field number. If OFFSET is not exactly at the
09e2d7c7 693 start of some field, set *SELF to NULL. */
0d5de010 694
2c0b251b 695static void
09e2d7c7 696cp_find_class_member (struct type **self_p, int *fieldno,
0d5de010
DJ
697 LONGEST offset)
698{
09e2d7c7 699 struct type *self;
0d5de010
DJ
700 unsigned int i;
701 unsigned len;
702
09e2d7c7
DE
703 *self_p = check_typedef (*self_p);
704 self = *self_p;
705 len = TYPE_NFIELDS (self);
0d5de010 706
09e2d7c7 707 for (i = TYPE_N_BASECLASSES (self); i < len; i++)
0d5de010 708 {
09e2d7c7 709 LONGEST bitpos = TYPE_FIELD_BITPOS (self, i);
0d5de010
DJ
710
711 QUIT;
712 if (offset == bitpos)
713 {
714 *fieldno = i;
715 return;
716 }
717 }
718
09e2d7c7 719 for (i = 0; i < TYPE_N_BASECLASSES (self); i++)
0d5de010 720 {
09e2d7c7
DE
721 LONGEST bitpos = TYPE_FIELD_BITPOS (self, i);
722 LONGEST bitsize = 8 * TYPE_LENGTH (TYPE_FIELD_TYPE (self, i));
0d5de010
DJ
723
724 if (offset >= bitpos && offset < bitpos + bitsize)
725 {
09e2d7c7
DE
726 *self_p = TYPE_FIELD_TYPE (self, i);
727 cp_find_class_member (self_p, fieldno, offset - bitpos);
0d5de010
DJ
728 return;
729 }
730 }
731
09e2d7c7 732 *self_p = NULL;
0d5de010
DJ
733}
734
c906108c 735void
ad4820ab 736cp_print_class_member (const gdb_byte *valaddr, struct type *type,
a121b7c1 737 struct ui_file *stream, const char *prefix)
c906108c 738{
e17a4113
UW
739 enum bfd_endian byte_order = gdbarch_byte_order (get_type_arch (type));
740
09e2d7c7 741 /* VAL is a byte offset into the structure type SELF_TYPE.
c906108c
SS
742 Find the name of the field for that offset and
743 print it. */
09e2d7c7 744 struct type *self_type = TYPE_SELF_TYPE (type);
e17a4113 745 LONGEST val;
9f8afa72 746 int fieldno;
c906108c 747
aff410f1
MS
748 val = extract_signed_integer (valaddr,
749 TYPE_LENGTH (type),
750 byte_order);
e17a4113 751
0d5de010
DJ
752 /* Pointers to data members are usually byte offsets into an object.
753 Because a data member can have offset zero, and a NULL pointer to
754 member must be distinct from any valid non-NULL pointer to
755 member, either the value is biased or the NULL value has a
756 special representation; both are permitted by ISO C++. HP aCC
757 used a bias of 0x20000000; HP cfront used a bias of 1; g++ 3.x
758 and other compilers which use the Itanium ABI use -1 as the NULL
759 value. GDB only supports that last form; to add support for
760 another form, make this into a cp-abi hook. */
c906108c 761
0d5de010 762 if (val == -1)
c906108c 763 {
0d5de010
DJ
764 fprintf_filtered (stream, "NULL");
765 return;
c906108c 766 }
0d5de010 767
09e2d7c7 768 cp_find_class_member (&self_type, &fieldno, val << 3);
0d5de010 769
09e2d7c7 770 if (self_type != NULL)
c906108c 771 {
0d5cff50 772 const char *name;
c5504eaf 773
306d9ac5 774 fputs_filtered (prefix, stream);
a737d952 775 name = TYPE_NAME (self_type);
c906108c 776 if (name)
c5aa993b 777 fputs_filtered (name, stream);
c906108c 778 else
09e2d7c7 779 c_type_print_base (self_type, stream, 0, 0, &type_print_raw_options);
c906108c 780 fprintf_filtered (stream, "::");
09e2d7c7 781 fputs_filtered (TYPE_FIELD_NAME (self_type, fieldno), stream);
c906108c
SS
782 }
783 else
0d5de010 784 fprintf_filtered (stream, "%ld", (long) val);
c906108c
SS
785}
786
787
c906108c 788void
fba45db2 789_initialize_cp_valprint (void)
c906108c 790{
aff410f1
MS
791 obstack_begin (&dont_print_stat_array_obstack,
792 32 * sizeof (struct type *));
793 obstack_begin (&dont_print_statmem_obstack,
794 32 * sizeof (CORE_ADDR));
795 obstack_begin (&dont_print_vb_obstack,
796 32 * sizeof (struct type *));
c906108c 797}