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