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