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