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