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