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