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