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