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