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