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