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