2003-07-15 Andrew Cagney <cagney@redhat.com>
[deliverable/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,
22abf04a 3 2000, 2001, 2002, 2003
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"
973177d3 37#include "valprint.h"
c906108c 38
c5aa993b
JM
39/* Indication of presence of HP-compiled object files */
40extern int hp_som_som_object_present; /* defined in symtab.c */
c906108c
SS
41
42
43int vtblprint; /* Controls printing of vtbl's */
44int objectprint; /* Controls looking up an object's derived type
45 using what we find in its vtables. */
c5aa993b 46int static_field_print; /* Controls printing of static fields. */
c906108c
SS
47
48static struct obstack dont_print_vb_obstack;
49static struct obstack dont_print_statmem_obstack;
50
a14ed312 51extern void _initialize_cp_valprint (void);
392a587b 52
6943961c 53static void cp_print_static_field (struct type *, struct value *,
d9fcf2fb
JM
54 struct ui_file *, int, int,
55 enum val_prettyprint);
c906108c 56
d9fcf2fb
JM
57static void cp_print_value (struct type *, struct type *, char *, int,
58 CORE_ADDR, struct ui_file *, int, int,
59 enum val_prettyprint, struct type **);
c906108c 60
d9fcf2fb 61static void cp_print_hpacc_virtual_table_entries (struct type *, int *,
6943961c 62 struct value *,
d9fcf2fb
JM
63 struct ui_file *, int,
64 int,
65 enum val_prettyprint);
c906108c
SS
66
67
68void
2c63a960
JB
69cp_print_class_method (char *valaddr,
70 struct type *type,
71 struct ui_file *stream)
c906108c
SS
72{
73 struct type *domain;
74 struct fn_field *f = NULL;
75 int j = 0;
76 int len2;
77 int offset;
78 char *kind = "";
79 CORE_ADDR addr;
80 struct symbol *sym;
81 unsigned len;
82 unsigned int i;
83 struct type *target_type = check_typedef (TYPE_TARGET_TYPE (type));
84
85 domain = TYPE_DOMAIN_TYPE (target_type);
c5aa993b 86 if (domain == (struct type *) NULL)
c906108c
SS
87 {
88 fprintf_filtered (stream, "<unknown>");
89 return;
90 }
1947a811 91 addr = unpack_pointer (type, valaddr);
c906108c
SS
92 if (METHOD_PTR_IS_VIRTUAL (addr))
93 {
94 offset = METHOD_PTR_TO_VOFFSET (addr);
95 len = TYPE_NFN_FIELDS (domain);
96 for (i = 0; i < len; i++)
97 {
98 f = TYPE_FN_FIELDLIST1 (domain, i);
99 len2 = TYPE_FN_FIELDLIST_LENGTH (domain, i);
c5aa993b 100
de17c821 101 check_stub_method_group (domain, i);
c906108c
SS
102 for (j = 0; j < len2; j++)
103 {
c906108c
SS
104 if (TYPE_FN_FIELD_VOFFSET (f, j) == offset)
105 {
c906108c
SS
106 kind = "virtual ";
107 goto common;
108 }
109 }
110 }
111 }
112 else
113 {
114 sym = find_pc_function (addr);
115 if (sym == 0)
116 {
c5aa993b
JM
117 /* 1997-08-01 Currently unsupported with HP aCC */
118 if (hp_som_som_object_present)
119 {
120 fputs_filtered ("?? <not supported with HP aCC>", stream);
121 return;
122 }
c906108c
SS
123 error ("invalid pointer to member function");
124 }
125 len = TYPE_NFN_FIELDS (domain);
126 for (i = 0; i < len; i++)
127 {
128 f = TYPE_FN_FIELDLIST1 (domain, i);
129 len2 = TYPE_FN_FIELDLIST_LENGTH (domain, i);
c5aa993b 130
1b2ef1b6 131 check_stub_method_group (domain, i);
c906108c
SS
132 for (j = 0; j < len2; j++)
133 {
22abf04a 134 if (strcmp (DEPRECATED_SYMBOL_NAME (sym), TYPE_FN_FIELD_PHYSNAME (f, j))
762f08a3 135 == 0)
de17c821 136 goto common;
c906108c
SS
137 }
138 }
139 }
2c63a960 140 common:
c906108c
SS
141 if (i < len)
142 {
143 char *demangled_name;
144
145 fprintf_filtered (stream, "&");
146 fprintf_filtered (stream, kind);
147 demangled_name = cplus_demangle (TYPE_FN_FIELD_PHYSNAME (f, j),
148 DMGL_ANSI | DMGL_PARAMS);
149 if (demangled_name == NULL)
150 fprintf_filtered (stream, "<badly mangled name %s>",
151 TYPE_FN_FIELD_PHYSNAME (f, j));
152 else
153 {
154 fputs_filtered (demangled_name, stream);
b8c9b27d 155 xfree (demangled_name);
c906108c
SS
156 }
157 }
158 else
159 {
160 fprintf_filtered (stream, "(");
161 type_print (type, "", stream, -1);
162 fprintf_filtered (stream, ") %d", (int) addr >> 3);
163 }
164}
165
8343f86c 166/* GCC versions after 2.4.5 use this. */
2c63a960 167const char vtbl_ptr_name[] = "__vtbl_ptr_type";
c906108c 168
8343f86c 169/* HP aCC uses different names. */
2c63a960
JB
170const char hpacc_vtbl_ptr_name[] = "__vfp";
171const char hpacc_vtbl_ptr_type_name[] = "__vftyp";
c906108c 172
c906108c
SS
173/* Return truth value for assertion that TYPE is of the type
174 "pointer to virtual function". */
175
176int
fba45db2 177cp_is_vtbl_ptr_type (struct type *type)
c906108c
SS
178{
179 char *typename = type_name_no_tag (type);
180
8343f86c 181 return (typename != NULL && !strcmp (typename, vtbl_ptr_name));
c906108c
SS
182}
183
184/* Return truth value for the assertion that TYPE is of the type
185 "pointer to virtual function table". */
186
187int
fba45db2 188cp_is_vtbl_member (struct type *type)
c906108c 189{
0e5e3ea6
PS
190 /* With older versions of g++, the vtbl field pointed to an array
191 of structures. Nowadays it points directly to the structure. */
c906108c
SS
192 if (TYPE_CODE (type) == TYPE_CODE_PTR)
193 {
194 type = TYPE_TARGET_TYPE (type);
195 if (TYPE_CODE (type) == TYPE_CODE_ARRAY)
196 {
197 type = TYPE_TARGET_TYPE (type);
c5aa993b
JM
198 if (TYPE_CODE (type) == TYPE_CODE_STRUCT /* if not using thunks */
199 || TYPE_CODE (type) == TYPE_CODE_PTR) /* if using thunks */
c906108c
SS
200 {
201 /* Virtual functions tables are full of pointers
c5aa993b 202 to virtual functions. */
c906108c
SS
203 return cp_is_vtbl_ptr_type (type);
204 }
205 }
0e5e3ea6
PS
206 else if (TYPE_CODE (type) == TYPE_CODE_STRUCT) /* if not using thunks */
207 {
208 return cp_is_vtbl_ptr_type (type);
209 }
210 else if (TYPE_CODE (type) == TYPE_CODE_PTR) /* if using thunks */
211 {
212 /* The type name of the thunk pointer is NULL when using dwarf2.
213 We could test for a pointer to a function, but there is
214 no type info for the virtual table either, so it wont help. */
215 return cp_is_vtbl_ptr_type (type);
216 }
c906108c
SS
217 }
218 return 0;
219}
220
221/* Mutually recursive subroutines of cp_print_value and c_val_print to
222 print out a structure's fields: cp_print_value_fields and cp_print_value.
c5aa993b 223
c906108c
SS
224 TYPE, VALADDR, ADDRESS, STREAM, RECURSE, and PRETTY have the
225 same meanings as in cp_print_value and c_val_print.
226
227 2nd argument REAL_TYPE is used to carry over the type of the derived
228 class across the recursion to base classes.
229
230 DONT_PRINT is an array of baseclass types that we
231 should not print, or zero if called from top level. */
232
233void
fba45db2
KB
234cp_print_value_fields (struct type *type, struct type *real_type, char *valaddr,
235 int offset, CORE_ADDR address, struct ui_file *stream,
236 int format, int recurse, enum val_prettyprint pretty,
237 struct type **dont_print_vb, int dont_print_statmem)
c906108c
SS
238{
239 int i, len, n_baseclasses;
240 struct obstack tmp_obstack;
241 char *last_dont_print = obstack_next_free (&dont_print_statmem_obstack);
242 int fields_seen = 0;
243
244 CHECK_TYPEDEF (type);
245
246 fprintf_filtered (stream, "{");
247 len = TYPE_NFIELDS (type);
248 n_baseclasses = TYPE_N_BASECLASSES (type);
249
250 /* First, print out baseclasses such that we don't print
251 duplicates of virtual baseclasses. */
252
253 if (n_baseclasses > 0)
254 cp_print_value (type, real_type, valaddr, offset, address, stream,
c5aa993b 255 format, recurse + 1, pretty, dont_print_vb);
c906108c
SS
256
257 /* Second, print out data fields */
258
259 /* If there are no data fields, or if the only field is the
c5aa993b 260 * vtbl pointer, skip this part */
2c63a960
JB
261 if ((len == n_baseclasses)
262 || ((len - n_baseclasses == 1)
263 && TYPE_HAS_VTABLE (type)
5cb316ef
AC
264 && strncmp (TYPE_FIELD_NAME (type, n_baseclasses),
265 hpacc_vtbl_ptr_name, 5) == 0)
2c63a960 266 || !len)
c906108c
SS
267 fprintf_filtered (stream, "<No data fields>");
268 else
269 {
c906108c
SS
270 if (dont_print_statmem == 0)
271 {
272 /* If we're at top level, carve out a completely fresh
273 chunk of the obstack and use that until this particular
274 invocation returns. */
275 tmp_obstack = dont_print_statmem_obstack;
276 obstack_finish (&dont_print_statmem_obstack);
277 }
278
279 for (i = n_baseclasses; i < len; i++)
280 {
281 /* If requested, skip printing of static fields. */
282 if (!static_field_print && TYPE_FIELD_STATIC (type, i))
283 continue;
284
c5aa993b 285 /* If a vtable pointer appears, we'll print it out later */
2c63a960 286 if (TYPE_HAS_VTABLE (type)
5cb316ef
AC
287 && strncmp (TYPE_FIELD_NAME (type, i), hpacc_vtbl_ptr_name,
288 5) == 0)
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 410 if (TYPE_HAS_VTABLE (type)
5cb316ef
AC
411 && strncmp (TYPE_FIELD_NAME (type, n_baseclasses),
412 hpacc_vtbl_ptr_name, 5) == 0)
c906108c 413 {
6943961c 414 struct value *v;
c5aa993b 415 /* First get the virtual table pointer and print it out */
c906108c
SS
416
417#if 0
418 fputs_filtered ("__vfp = ", stream);
419#endif
420
421 fputs_filtered (", Virtual table at ", stream);
422
423 /* pai: FIXME 32x64 problem? */
424 /* Not sure what the best notation is in the case where there is no
425 baseclass name. */
4478b372 426 v = value_from_pointer (lookup_pointer_type (builtin_type_unsigned_long),
c5aa993b 427 *(unsigned long *) (valaddr + offset));
c906108c
SS
428
429 val_print (VALUE_TYPE (v), VALUE_CONTENTS (v), 0, 0,
c5aa993b 430 stream, format, 0, recurse + 1, pretty);
c906108c
SS
431 fields_seen = 1;
432
433 if (vtblprint)
c5aa993b
JM
434 {
435 /* Print out function pointers in vtable. */
c906108c 436
c5aa993b
JM
437 /* FIXME: then-clause is for non-RRBC layout of virtual
438 * table. The RRBC case in the else-clause is yet to be
439 * implemented. The if (1) below should be changed to a
440 * test for whether the executable we have was compiled
441 * with a version of HP aCC that doesn't have RRBC
442 * support. */
c906108c 443
c5aa993b
JM
444 if (1)
445 {
2c63a960
JB
446 /* no RRBC support; function pointers embedded directly
447 in vtable */
c906108c 448
c5aa993b 449 int vfuncs = count_virtual_fns (real_type);
c906108c 450
c5aa993b 451 fputs_filtered (" {", stream);
c906108c 452
c5aa993b 453 /* FIXME : doesn't work at present */
c906108c 454#if 0
2c63a960
JB
455 fprintf_filtered (stream, "%d entr%s: ", vfuncs,
456 vfuncs == 1 ? "y" : "ies");
c906108c 457#else
c5aa993b 458 fputs_filtered ("not implemented", stream);
c906108c
SS
459
460
461#endif
462
c5aa993b 463 /* recursive function that prints all virtual function entries */
c906108c 464#if 0
2c63a960
JB
465 cp_print_hpacc_virtual_table_entries (real_type, &vfuncs, v,
466 stream, format, recurse,
467 pretty);
c906108c 468#endif
c5aa993b
JM
469 fputs_filtered ("}", stream);
470 } /* non-RRBC case */
471 else
472 {
4d33f415 473 /* FIXME -- see comments above */
c5aa993b
JM
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
fba45db2
KB
496cp_print_value (struct type *type, struct type *real_type, char *valaddr,
497 int offset, CORE_ADDR address, struct ui_file *stream,
498 int format, int recurse, enum val_prettyprint pretty,
499 struct type **dont_print_vb)
c906108c
SS
500{
501 struct obstack tmp_obstack;
502 struct type **last_dont_print
2c63a960 503 = (struct type **) obstack_next_free (&dont_print_vb_obstack);
c906108c 504 int i, n_baseclasses = TYPE_N_BASECLASSES (type);
b9d652ac
DJ
505 int thisoffset;
506 struct type *thistype;
c906108c
SS
507
508 if (dont_print_vb == 0)
509 {
510 /* If we're at top level, carve out a completely fresh
c5aa993b
JM
511 chunk of the obstack and use that until this particular
512 invocation returns. */
c906108c
SS
513 tmp_obstack = dont_print_vb_obstack;
514 /* Bump up the high-water mark. Now alpha is omega. */
515 obstack_finish (&dont_print_vb_obstack);
516 }
517
518 for (i = 0; i < n_baseclasses; i++)
519 {
520 int boffset;
521 int skip;
522 struct type *baseclass = check_typedef (TYPE_BASECLASS (type, i));
523 char *basename = TYPE_NAME (baseclass);
524 char *base_valaddr;
525
526 if (BASETYPE_VIA_VIRTUAL (type, i))
527 {
528 struct type **first_dont_print
2c63a960 529 = (struct type **) obstack_base (&dont_print_vb_obstack);
c906108c 530
c5aa993b 531 int j = (struct type **) obstack_next_free (&dont_print_vb_obstack)
2c63a960 532 - first_dont_print;
c906108c
SS
533
534 while (--j >= 0)
535 if (baseclass == first_dont_print[j])
536 goto flush_it;
537
538 obstack_ptr_grow (&dont_print_vb_obstack, baseclass);
539 }
540
b9d652ac
DJ
541 thisoffset = offset;
542 thistype = real_type;
c906108c 543 if (TYPE_HAS_VTABLE (type) && BASETYPE_VIA_VIRTUAL (type, i))
c5aa993b
JM
544 {
545 /* Assume HP/Taligent runtime convention */
546 find_rt_vbase_offset (type, TYPE_BASECLASS (type, i),
547 valaddr, offset, &boffset, &skip);
548 if (skip >= 0)
2c63a960
JB
549 error ("Virtual base class offset not found from vtable while"
550 " printing");
c5aa993b
JM
551 base_valaddr = valaddr;
552 }
c906108c 553 else
c5aa993b 554 {
2c63a960
JB
555 boffset = baseclass_offset (type, i,
556 valaddr + offset,
9e14d721 557 address);
c5aa993b 558 skip = ((boffset == -1) || (boffset + offset) < 0) ? 1 : -1;
c906108c 559
c5aa993b
JM
560 if (BASETYPE_VIA_VIRTUAL (type, i))
561 {
2c63a960
JB
562 /* The virtual base class pointer might have been
563 clobbered by the user program. Make sure that it
564 still points to a valid memory location. */
c906108c 565
2c63a960
JB
566 if (boffset != -1
567 && ((boffset + offset) < 0
568 || (boffset + offset) >= TYPE_LENGTH (type)))
c5aa993b 569 {
34c0bd93 570 /* FIXME (alloca): unsafe if baseclass is really really large. */
c5aa993b 571 base_valaddr = (char *) alloca (TYPE_LENGTH (baseclass));
9e14d721 572 if (target_read_memory (address + boffset, base_valaddr,
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);
2c63a960
JB
669 cp_print_value_fields (type, type, VALUE_CONTENTS_ALL (val),
670 VALUE_EMBEDDED_OFFSET (val), VALUE_ADDRESS (val),
c906108c
SS
671 stream, format, recurse, pretty, NULL, 1);
672 return;
673 }
2c63a960
JB
674 val_print (type, VALUE_CONTENTS_ALL (val),
675 VALUE_EMBEDDED_OFFSET (val), VALUE_ADDRESS (val),
c906108c
SS
676 stream, format, 0, recurse, pretty);
677}
678
679void
fba45db2
KB
680cp_print_class_member (char *valaddr, struct type *domain,
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;
689 register unsigned int i;
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;
723 fprintf_filtered (stream, prefix);
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);
c5aa993b
JM
786 if (VALUE_LAZY (vf))
787 (void) value_fetch_lazy (vf);
2c63a960
JB
788 /* adjust by offset */
789 vf->aligner.contents[0] += 4 * (HP_ACC_VFUNC_START + vx);
c5aa993b
JM
790 vf = value_ind (vf); /* get the entry */
791 VALUE_TYPE (vf) = VALUE_TYPE (v); /* make it a pointer */
792
793 /* print out the entry */
794 val_print (VALUE_TYPE (vf), VALUE_CONTENTS (vf), 0, 0,
795 stream, format, 0, recurse + 1, pretty);
2c63a960
JB
796 field_physname
797 = TYPE_FN_FIELD_PHYSNAME (TYPE_FN_FIELDLIST1 (type, fn), oi);
798 /* pai: (temp) FIXME Maybe this should be DMGL_ANSI */
799 vf_name = cplus_demangle (field_physname, DMGL_ARM);
c5aa993b
JM
800 fprintf_filtered (stream, " %s", vf_name);
801 if (--(*vfuncs) > 0)
802 fputs_filtered (", ", stream);
803 }
c906108c
SS
804}
805
806
807
808void
fba45db2 809_initialize_cp_valprint (void)
c906108c
SS
810{
811 add_show_from_set
812 (add_set_cmd ("static-members", class_support, var_boolean,
c5aa993b 813 (char *) &static_field_print,
c906108c
SS
814 "Set printing of C++ static members.",
815 &setprintlist),
816 &showprintlist);
817 /* Turn on printing of static fields. */
818 static_field_print = 1;
819
820 add_show_from_set
c5aa993b 821 (add_set_cmd ("vtbl", class_support, var_boolean, (char *) &vtblprint,
c906108c
SS
822 "Set printing of C++ virtual function tables.",
823 &setprintlist),
824 &showprintlist);
825
826 add_show_from_set
c5aa993b
JM
827 (add_set_cmd ("object", class_support, var_boolean, (char *) &objectprint,
828 "Set printing of object's derived type based on vtable info.",
c906108c
SS
829 &setprintlist),
830 &showprintlist);
831
832 /* Give people the defaults which they are used to. */
833 objectprint = 0;
834 vtblprint = 0;
835 obstack_begin (&dont_print_vb_obstack, 32 * sizeof (struct type *));
836 obstack_specify_allocation (&dont_print_statmem_obstack,
837 32 * sizeof (CORE_ADDR), sizeof (CORE_ADDR),
b8c9b27d 838 xmalloc, xfree);
c906108c 839}
This page took 0.294502 seconds and 4 git commands to generate.