* ada-lang.c (user_select_syms, ada_print_subexp): Pass flags
[platform/upstream/binutils.git] / gdb / cp-valprint.c
1 /* Support for printing C++ values for GDB, the GNU debugger.
2
3    Copyright (C) 1986, 1988-1989, 1991-1997, 2000-2003, 2005-2012 Free
4    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 3 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, see <http://www.gnu.org/licenses/>.  */
20
21 #include "defs.h"
22 #include "gdb_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 #include "cp-abi.h"
35 #include "valprint.h"
36 #include "cp-support.h"
37 #include "language.h"
38 #include "python/python.h"
39 #include "exceptions.h"
40 #include "typeprint.h"
41
42 /* Controls printing of vtbl's.  */
43 static void
44 show_vtblprint (struct ui_file *file, int from_tty,
45                 struct cmd_list_element *c, const char *value)
46 {
47   fprintf_filtered (file, _("\
48 Printing of C++ virtual function tables is %s.\n"),
49                     value);
50 }
51
52 /* Controls looking up an object's derived type using what we find in
53    its vtables.  */
54 static void
55 show_objectprint (struct ui_file *file, int from_tty,
56                   struct cmd_list_element *c,
57                   const char *value)
58 {
59   fprintf_filtered (file, _("\
60 Printing of object's derived type based on vtable info is %s.\n"),
61                     value);
62 }
63
64 static void
65 show_static_field_print (struct ui_file *file, int from_tty,
66                          struct cmd_list_element *c,
67                          const char *value)
68 {
69   fprintf_filtered (file,
70                     _("Printing of C++ static members is %s.\n"),
71                     value);
72 }
73
74
75 static struct obstack dont_print_vb_obstack;
76 static struct obstack dont_print_statmem_obstack;
77 static struct obstack dont_print_stat_array_obstack;
78
79 extern void _initialize_cp_valprint (void);
80
81 static void cp_print_static_field (struct type *, struct value *,
82                                    struct ui_file *, int,
83                                    const struct value_print_options *);
84
85 static void cp_print_value (struct type *, struct type *,
86                             const gdb_byte *, int,
87                             CORE_ADDR, struct ui_file *,
88                             int, const struct value *,
89                             const struct value_print_options *,
90                             struct type **);
91
92
93 /* GCC versions after 2.4.5 use this.  */
94 const char vtbl_ptr_name[] = "__vtbl_ptr_type";
95
96 /* Return truth value for assertion that TYPE is of the type
97    "pointer to virtual function".  */
98
99 int
100 cp_is_vtbl_ptr_type (struct type *type)
101 {
102   const char *typename = type_name_no_tag (type);
103
104   return (typename != NULL && !strcmp (typename, vtbl_ptr_name));
105 }
106
107 /* Return truth value for the assertion that TYPE is of the type
108    "pointer to virtual function table".  */
109
110 int
111 cp_is_vtbl_member (struct type *type)
112 {
113   /* With older versions of g++, the vtbl field pointed to an array of
114      structures.  Nowadays it points directly to the structure.  */
115   if (TYPE_CODE (type) == TYPE_CODE_PTR)
116     {
117       type = TYPE_TARGET_TYPE (type);
118       if (TYPE_CODE (type) == TYPE_CODE_ARRAY)
119         {
120           type = TYPE_TARGET_TYPE (type);
121           if (TYPE_CODE (type) == TYPE_CODE_STRUCT    /* if not using thunks */
122               || TYPE_CODE (type) == TYPE_CODE_PTR)   /* if using thunks */
123             {
124               /* Virtual functions tables are full of pointers
125                  to virtual functions.  */
126               return cp_is_vtbl_ptr_type (type);
127             }
128         }
129       else if (TYPE_CODE (type) == TYPE_CODE_STRUCT)  /* if not using thunks */
130         {
131           return cp_is_vtbl_ptr_type (type);
132         }
133       else if (TYPE_CODE (type) == TYPE_CODE_PTR)     /* if using thunks */
134         {
135           /* The type name of the thunk pointer is NULL when using
136              dwarf2.  We could test for a pointer to a function, but
137              there is no type info for the virtual table either, so it
138              wont help.  */
139           return cp_is_vtbl_ptr_type (type);
140         }
141     }
142   return 0;
143 }
144
145 /* Mutually recursive subroutines of cp_print_value and c_val_print to
146    print out a structure's fields: cp_print_value_fields and
147    cp_print_value.
148
149    TYPE, VALADDR, ADDRESS, STREAM, RECURSE, and OPTIONS have the same
150    meanings as in cp_print_value and c_val_print.
151
152    2nd argument REAL_TYPE is used to carry over the type of the
153    derived class across the recursion to base classes.
154
155    DONT_PRINT is an array of baseclass types that we should not print,
156    or zero if called from top level.  */
157
158 void
159 cp_print_value_fields (struct type *type, struct type *real_type,
160                        const gdb_byte *valaddr, int offset,
161                        CORE_ADDR address, struct ui_file *stream,
162                        int recurse, const struct value *val,
163                        const struct value_print_options *options,
164                        struct type **dont_print_vb,
165                        int dont_print_statmem)
166 {
167   int i, len, n_baseclasses;
168   int fields_seen = 0;
169   static int last_set_recurse = -1;
170
171   CHECK_TYPEDEF (type);
172   
173   if (recurse == 0)
174     {
175       /* Any object can be left on obstacks only during an unexpected
176          error.  */
177
178       if (obstack_object_size (&dont_print_statmem_obstack) > 0)
179         {
180           obstack_free (&dont_print_statmem_obstack, NULL);
181           obstack_begin (&dont_print_statmem_obstack,
182                          32 * sizeof (CORE_ADDR));
183         }
184       if (obstack_object_size (&dont_print_stat_array_obstack) > 0)
185         {
186           obstack_free (&dont_print_stat_array_obstack, NULL);
187           obstack_begin (&dont_print_stat_array_obstack,
188                          32 * sizeof (struct type *));
189         }
190     }
191
192   fprintf_filtered (stream, "{");
193   len = TYPE_NFIELDS (type);
194   n_baseclasses = TYPE_N_BASECLASSES (type);
195
196   /* First, print out baseclasses such that we don't print
197      duplicates of virtual baseclasses.  */
198
199   if (n_baseclasses > 0)
200     cp_print_value (type, real_type, valaddr, 
201                     offset, address, stream,
202                     recurse + 1, val, options,
203                     dont_print_vb);
204
205   /* Second, print out data fields */
206
207   /* If there are no data fields, skip this part */
208   if (len == n_baseclasses || !len)
209     fprintf_filtered (stream, "<No data fields>");
210   else
211     {
212       int statmem_obstack_initial_size = 0;
213       int stat_array_obstack_initial_size = 0;
214       struct type *vptr_basetype = NULL;
215       int vptr_fieldno;
216
217       if (dont_print_statmem == 0)
218         {
219           statmem_obstack_initial_size =
220             obstack_object_size (&dont_print_statmem_obstack);
221
222           if (last_set_recurse != recurse)
223             {
224               stat_array_obstack_initial_size =
225                 obstack_object_size (&dont_print_stat_array_obstack);
226
227               last_set_recurse = recurse;
228             }
229         }
230
231       vptr_fieldno = get_vptr_fieldno (type, &vptr_basetype);
232       for (i = n_baseclasses; i < len; i++)
233         {
234           /* If requested, skip printing of static fields.  */
235           if (!options->static_field_print
236               && field_is_static (&TYPE_FIELD (type, i)))
237             continue;
238
239           if (fields_seen)
240             fprintf_filtered (stream, ", ");
241           else if (n_baseclasses > 0)
242             {
243               if (options->pretty)
244                 {
245                   fprintf_filtered (stream, "\n");
246                   print_spaces_filtered (2 + 2 * recurse, stream);
247                   fputs_filtered ("members of ", stream);
248                   fputs_filtered (type_name_no_tag (type), stream);
249                   fputs_filtered (": ", stream);
250                 }
251             }
252           fields_seen = 1;
253
254           if (options->pretty)
255             {
256               fprintf_filtered (stream, "\n");
257               print_spaces_filtered (2 + 2 * recurse, stream);
258             }
259           else
260             {
261               wrap_here (n_spaces (2 + 2 * recurse));
262             }
263           if (options->inspect_it)
264             {
265               if (TYPE_CODE (TYPE_FIELD_TYPE (type, i)) == TYPE_CODE_PTR)
266                 fputs_filtered ("\"( ptr \"", stream);
267               else
268                 fputs_filtered ("\"( nodef \"", stream);
269               if (field_is_static (&TYPE_FIELD (type, i)))
270                 fputs_filtered ("static ", stream);
271               fprintf_symbol_filtered (stream,
272                                        TYPE_FIELD_NAME (type, i),
273                                        current_language->la_language,
274                                        DMGL_PARAMS | DMGL_ANSI);
275               fputs_filtered ("\" \"", stream);
276               fprintf_symbol_filtered (stream,
277                                        TYPE_FIELD_NAME (type, i),
278                                        current_language->la_language,
279                                        DMGL_PARAMS | DMGL_ANSI);
280               fputs_filtered ("\") \"", stream);
281             }
282           else
283             {
284               annotate_field_begin (TYPE_FIELD_TYPE (type, i));
285
286               if (field_is_static (&TYPE_FIELD (type, i)))
287                 fputs_filtered ("static ", stream);
288               fprintf_symbol_filtered (stream,
289                                        TYPE_FIELD_NAME (type, i),
290                                        current_language->la_language,
291                                        DMGL_PARAMS | DMGL_ANSI);
292               annotate_field_name_end ();
293               /* Do not print leading '=' in case of anonymous
294                  unions.  */
295               if (strcmp (TYPE_FIELD_NAME (type, i), ""))
296                 fputs_filtered (" = ", stream);
297               annotate_field_value ();
298             }
299
300           if (!field_is_static (&TYPE_FIELD (type, i))
301               && TYPE_FIELD_PACKED (type, i))
302             {
303               struct value *v;
304
305               /* Bitfields require special handling, especially due to
306                  byte order problems.  */
307               if (TYPE_FIELD_IGNORE (type, i))
308                 {
309                   fputs_filtered ("<optimized out or zero length>", stream);
310                 }
311               else if (value_bits_synthetic_pointer (val,
312                                                      TYPE_FIELD_BITPOS (type,
313                                                                         i),
314                                                      TYPE_FIELD_BITSIZE (type,
315                                                                          i)))
316                 {
317                   fputs_filtered (_("<synthetic pointer>"), stream);
318                 }
319               else if (!value_bits_valid (val,
320                                           TYPE_FIELD_BITPOS (type, i),
321                                           TYPE_FIELD_BITSIZE (type, i)))
322                 {
323                   val_print_optimized_out (stream);
324                 }
325               else
326                 {
327                   struct value_print_options opts = *options;
328
329                   opts.deref_ref = 0;
330
331                   v = value_field_bitfield (type, i, valaddr, offset, val);
332
333                   common_val_print (v, stream, recurse + 1, &opts,
334                                     current_language);
335                 }
336             }
337           else
338             {
339               if (TYPE_FIELD_IGNORE (type, i))
340                 {
341                   fputs_filtered ("<optimized out or zero length>",
342                                   stream);
343                 }
344               else if (field_is_static (&TYPE_FIELD (type, i)))
345                 {
346                   volatile struct gdb_exception ex;
347                   struct value *v = NULL;
348
349                   TRY_CATCH (ex, RETURN_MASK_ERROR)
350                     {
351                       v = value_static_field (type, i);
352                     }
353
354                   if (ex.reason < 0)
355                     fprintf_filtered (stream,
356                                       _("<error reading variable: %s>"),
357                                       ex.message);
358                   else if (v == NULL)
359                     val_print_optimized_out (stream);
360                   else
361                     cp_print_static_field (TYPE_FIELD_TYPE (type, i),
362                                            v, stream, recurse + 1,
363                                            options);
364                 }
365               else if (i == vptr_fieldno && type == vptr_basetype)
366                 {
367                   int i_offset = offset + TYPE_FIELD_BITPOS (type, i) / 8;
368                   struct type *i_type = TYPE_FIELD_TYPE (type, i);
369
370                   if (valprint_check_validity (stream, i_type, i_offset, val))
371                     {
372                       CORE_ADDR addr;
373                       
374                       addr = extract_typed_address (valaddr + i_offset, i_type);
375                       print_function_pointer_address (options,
376                                                       get_type_arch (type),
377                                                       addr, stream);
378                     }
379                 }
380               else
381                 {
382                   struct value_print_options opts = *options;
383
384                   opts.deref_ref = 0;
385                   val_print (TYPE_FIELD_TYPE (type, i),
386                              valaddr, 
387                              offset + TYPE_FIELD_BITPOS (type, i) / 8,
388                              address,
389                              stream, recurse + 1, val, &opts,
390                              current_language);
391                 }
392             }
393           annotate_field_end ();
394         }
395
396       if (dont_print_statmem == 0)
397         {
398           int obstack_final_size =
399            obstack_object_size (&dont_print_statmem_obstack);
400
401           if (obstack_final_size > statmem_obstack_initial_size)
402             {
403               /* In effect, a pop of the printed-statics stack.  */
404
405               void *free_to_ptr =
406                 obstack_next_free (&dont_print_statmem_obstack) -
407                 (obstack_final_size - statmem_obstack_initial_size);
408
409               obstack_free (&dont_print_statmem_obstack,
410                             free_to_ptr);
411             }
412
413           if (last_set_recurse != recurse)
414             {
415               int obstack_final_size =
416                 obstack_object_size (&dont_print_stat_array_obstack);
417               
418               if (obstack_final_size > stat_array_obstack_initial_size)
419                 {
420                   void *free_to_ptr =
421                     obstack_next_free (&dont_print_stat_array_obstack)
422                     - (obstack_final_size
423                        - stat_array_obstack_initial_size);
424
425                   obstack_free (&dont_print_stat_array_obstack,
426                                 free_to_ptr);
427                 }
428               last_set_recurse = -1;
429             }
430         }
431
432       if (options->pretty)
433         {
434           fprintf_filtered (stream, "\n");
435           print_spaces_filtered (2 * recurse, stream);
436         }
437     }                           /* if there are data fields */
438
439   fprintf_filtered (stream, "}");
440 }
441
442 /* Like cp_print_value_fields, but find the runtime type of the object
443    and pass it as the `real_type' argument to cp_print_value_fields.
444    This function is a hack to work around the fact that
445    common_val_print passes the embedded offset to val_print, but not
446    the enclosing type.  */
447
448 void
449 cp_print_value_fields_rtti (struct type *type,
450                             const gdb_byte *valaddr, int offset,
451                             CORE_ADDR address,
452                             struct ui_file *stream, int recurse,
453                             const struct value *val,
454                             const struct value_print_options *options,
455                             struct type **dont_print_vb, 
456                             int dont_print_statmem)
457 {
458   struct type *real_type = NULL;
459
460   /* We require all bits to be valid in order to attempt a
461      conversion.  */
462   if (value_bits_valid (val, TARGET_CHAR_BIT * offset,
463                         TARGET_CHAR_BIT * TYPE_LENGTH (type)))
464     {
465       struct value *value;
466       int full, top, using_enc;
467
468       /* Ugh, we have to convert back to a value here.  */
469       value = value_from_contents_and_address (type, valaddr + offset,
470                                                address + offset);
471       /* We don't actually care about most of the result here -- just
472          the type.  We already have the correct offset, due to how
473          val_print was initially called.  */
474       real_type = value_rtti_type (value, &full, &top, &using_enc);
475     }
476
477   if (!real_type)
478     real_type = type;
479
480   cp_print_value_fields (type, real_type, valaddr, offset,
481                          address, stream, recurse, val, options,
482                          dont_print_vb, dont_print_statmem);
483 }
484
485 /* Special val_print routine to avoid printing multiple copies of
486    virtual baseclasses.  */
487
488 static void
489 cp_print_value (struct type *type, struct type *real_type,
490                 const gdb_byte *valaddr, int offset,
491                 CORE_ADDR address, struct ui_file *stream,
492                 int recurse, const struct value *val,
493                 const struct value_print_options *options,
494                 struct type **dont_print_vb)
495 {
496   struct type **last_dont_print
497     = (struct type **) obstack_next_free (&dont_print_vb_obstack);
498   struct obstack tmp_obstack = dont_print_vb_obstack;
499   int i, n_baseclasses = TYPE_N_BASECLASSES (type);
500   int thisoffset;
501   struct type *thistype;
502
503   if (dont_print_vb == 0)
504     {
505       /* If we're at top level, carve out a completely fresh chunk of
506          the obstack and use that until this particular invocation
507          returns.  */
508       /* Bump up the high-water mark.  Now alpha is omega.  */
509       obstack_finish (&dont_print_vb_obstack);
510     }
511
512   for (i = 0; i < n_baseclasses; i++)
513     {
514       int boffset = 0;
515       int skip;
516       struct type *baseclass = check_typedef (TYPE_BASECLASS (type, i));
517       const char *basename = TYPE_NAME (baseclass);
518       const gdb_byte *base_valaddr = NULL;
519       const struct value *base_val = NULL;
520       volatile struct gdb_exception ex;
521
522       if (BASETYPE_VIA_VIRTUAL (type, i))
523         {
524           struct type **first_dont_print
525             = (struct type **) obstack_base (&dont_print_vb_obstack);
526
527           int j = (struct type **)
528             obstack_next_free (&dont_print_vb_obstack) - first_dont_print;
529
530           while (--j >= 0)
531             if (baseclass == first_dont_print[j])
532               goto flush_it;
533
534           obstack_ptr_grow (&dont_print_vb_obstack, baseclass);
535         }
536
537       thisoffset = offset;
538       thistype = real_type;
539
540       TRY_CATCH (ex, RETURN_MASK_ERROR)
541         {
542           boffset = baseclass_offset (type, i, valaddr, offset, address, val);
543         }
544       if (ex.reason < 0 && ex.error == NOT_AVAILABLE_ERROR)
545         skip = -1;
546       else if (ex.reason < 0)
547         skip = 1;
548       else
549         {
550           skip = 0;
551
552           if (BASETYPE_VIA_VIRTUAL (type, i))
553             {
554               /* The virtual base class pointer might have been
555                  clobbered by the user program. Make sure that it
556                  still points to a valid memory location.  */
557
558               if ((boffset + offset) < 0
559                   || (boffset + offset) >= TYPE_LENGTH (real_type))
560                 {
561                   gdb_byte *buf;
562                   struct cleanup *back_to;
563
564                   buf = xmalloc (TYPE_LENGTH (baseclass));
565                   back_to = make_cleanup (xfree, buf);
566
567                   if (target_read_memory (address + boffset, buf,
568                                           TYPE_LENGTH (baseclass)) != 0)
569                     skip = 1;
570                   base_val = value_from_contents_and_address (baseclass,
571                                                               buf,
572                                                               address + boffset);
573                   thisoffset = 0;
574                   boffset = 0;
575                   thistype = baseclass;
576                   base_valaddr = value_contents_for_printing_const (base_val);
577                   do_cleanups (back_to);
578                 }
579               else
580                 {
581                   base_valaddr = valaddr;
582                   base_val = val;
583                 }
584             }
585           else
586             {
587               base_valaddr = valaddr;
588               base_val = val;
589             }
590         }
591
592       /* Now do the printing.  */
593       if (options->pretty)
594         {
595           fprintf_filtered (stream, "\n");
596           print_spaces_filtered (2 * recurse, stream);
597         }
598       fputs_filtered ("<", stream);
599       /* Not sure what the best notation is in the case where there is
600          no baseclass name.  */
601       fputs_filtered (basename ? basename : "", stream);
602       fputs_filtered ("> = ", stream);
603
604       if (skip < 0)
605         val_print_unavailable (stream);
606       else if (skip > 0)
607         val_print_invalid_address (stream);
608       else
609         {
610           int result = 0;
611
612           /* Attempt to run the Python pretty-printers on the
613              baseclass if possible.  */
614           if (!options->raw)
615             result = apply_val_pretty_printer (baseclass, base_valaddr,
616                                                thisoffset + boffset,
617                                                value_address (base_val),
618                                                stream, recurse, base_val,
619                                                options, current_language);
620
621
622                   
623           if (!result)
624             cp_print_value_fields (baseclass, thistype, base_valaddr,
625                                    thisoffset + boffset,
626                                    value_address (base_val),
627                                    stream, recurse, base_val, options,
628                                    ((struct type **)
629                                     obstack_base (&dont_print_vb_obstack)),
630                                    0);
631         }
632       fputs_filtered (", ", stream);
633
634     flush_it:
635       ;
636     }
637
638   if (dont_print_vb == 0)
639     {
640       /* Free the space used to deal with the printing
641          of this type from top level.  */
642       obstack_free (&dont_print_vb_obstack, last_dont_print);
643       /* Reset watermark so that we can continue protecting
644          ourselves from whatever we were protecting ourselves.  */
645       dont_print_vb_obstack = tmp_obstack;
646     }
647 }
648
649 /* Print value of a static member.  To avoid infinite recursion when
650    printing a class that contains a static instance of the class, we
651    keep the addresses of all printed static member classes in an
652    obstack and refuse to print them more than once.
653
654    VAL contains the value to print, TYPE, STREAM, RECURSE, and OPTIONS
655    have the same meanings as in c_val_print.  */
656
657 static void
658 cp_print_static_field (struct type *type,
659                        struct value *val,
660                        struct ui_file *stream,
661                        int recurse,
662                        const struct value_print_options *options)
663 {
664   struct value_print_options opts;
665   
666   if (TYPE_CODE (type) == TYPE_CODE_STRUCT)
667     {
668       CORE_ADDR *first_dont_print;
669       CORE_ADDR addr;
670       int i;
671
672       first_dont_print
673         = (CORE_ADDR *) obstack_base (&dont_print_statmem_obstack);
674       i = obstack_object_size (&dont_print_statmem_obstack)
675         / sizeof (CORE_ADDR);
676
677       while (--i >= 0)
678         {
679           if (value_address (val) == first_dont_print[i])
680             {
681               fputs_filtered ("<same as static member of an already"
682                               " seen type>",
683                               stream);
684               return;
685             }
686         }
687
688       addr = value_address (val);
689       obstack_grow (&dont_print_statmem_obstack, (char *) &addr,
690                     sizeof (CORE_ADDR));
691       CHECK_TYPEDEF (type);
692       cp_print_value_fields (type, value_enclosing_type (val),
693                              value_contents_for_printing (val),
694                              value_embedded_offset (val), addr,
695                              stream, recurse, val,
696                              options, NULL, 1);
697       return;
698     }
699
700   if (TYPE_CODE (type) == TYPE_CODE_ARRAY)
701     {
702       struct type **first_dont_print;
703       int i;
704       struct type *target_type = TYPE_TARGET_TYPE (type);
705
706       first_dont_print
707         = (struct type **) obstack_base (&dont_print_stat_array_obstack);
708       i = obstack_object_size (&dont_print_stat_array_obstack)
709         / sizeof (struct type *);
710
711       while (--i >= 0)
712         {
713           if (target_type == first_dont_print[i])
714             {
715               fputs_filtered ("<same as static member of an already"
716                               " seen type>",
717                               stream);
718               return;
719             }
720         }
721
722       obstack_grow (&dont_print_stat_array_obstack,
723                     (char *) &target_type,
724                     sizeof (struct type *));
725     }
726
727   opts = *options;
728   opts.deref_ref = 0;
729   val_print (type, value_contents_for_printing (val), 
730              value_embedded_offset (val),
731              value_address (val),
732              stream, recurse, val,
733              &opts, current_language);
734 }
735
736
737 /* Find the field in *DOMAIN, or its non-virtual base classes, with
738    bit offset OFFSET.  Set *DOMAIN to the containing type and *FIELDNO
739    to the containing field number.  If OFFSET is not exactly at the
740    start of some field, set *DOMAIN to NULL.  */
741
742 static void
743 cp_find_class_member (struct type **domain_p, int *fieldno,
744                       LONGEST offset)
745 {
746   struct type *domain;
747   unsigned int i;
748   unsigned len;
749
750   *domain_p = check_typedef (*domain_p);
751   domain = *domain_p;
752   len = TYPE_NFIELDS (domain);
753
754   for (i = TYPE_N_BASECLASSES (domain); i < len; i++)
755     {
756       LONGEST bitpos = TYPE_FIELD_BITPOS (domain, i);
757
758       QUIT;
759       if (offset == bitpos)
760         {
761           *fieldno = i;
762           return;
763         }
764     }
765
766   for (i = 0; i < TYPE_N_BASECLASSES (domain); i++)
767     {
768       LONGEST bitpos = TYPE_FIELD_BITPOS (domain, i);
769       LONGEST bitsize = 8 * TYPE_LENGTH (TYPE_FIELD_TYPE (domain, i));
770
771       if (offset >= bitpos && offset < bitpos + bitsize)
772         {
773           *domain_p = TYPE_FIELD_TYPE (domain, i);
774           cp_find_class_member (domain_p, fieldno, offset - bitpos);
775           return;
776         }
777     }
778
779   *domain_p = NULL;
780 }
781
782 void
783 cp_print_class_member (const gdb_byte *valaddr, struct type *type,
784                        struct ui_file *stream, char *prefix)
785 {
786   enum bfd_endian byte_order = gdbarch_byte_order (get_type_arch (type));
787
788   /* VAL is a byte offset into the structure type DOMAIN.
789      Find the name of the field for that offset and
790      print it.  */
791   struct type *domain = TYPE_DOMAIN_TYPE (type);
792   LONGEST val;
793   unsigned int fieldno;
794
795   val = extract_signed_integer (valaddr,
796                                 TYPE_LENGTH (type),
797                                 byte_order);
798
799   /* Pointers to data members are usually byte offsets into an object.
800      Because a data member can have offset zero, and a NULL pointer to
801      member must be distinct from any valid non-NULL pointer to
802      member, either the value is biased or the NULL value has a
803      special representation; both are permitted by ISO C++.  HP aCC
804      used a bias of 0x20000000; HP cfront used a bias of 1; g++ 3.x
805      and other compilers which use the Itanium ABI use -1 as the NULL
806      value.  GDB only supports that last form; to add support for
807      another form, make this into a cp-abi hook.  */
808
809   if (val == -1)
810     {
811       fprintf_filtered (stream, "NULL");
812       return;
813     }
814
815   cp_find_class_member (&domain, &fieldno, val << 3);
816
817   if (domain != NULL)
818     {
819       const char *name;
820
821       fputs_filtered (prefix, stream);
822       name = type_name_no_tag (domain);
823       if (name)
824         fputs_filtered (name, stream);
825       else
826         c_type_print_base (domain, stream, 0, 0, &type_print_raw_options);
827       fprintf_filtered (stream, "::");
828       fputs_filtered (TYPE_FIELD_NAME (domain, fieldno), stream);
829     }
830   else
831     fprintf_filtered (stream, "%ld", (long) val);
832 }
833
834
835 void
836 _initialize_cp_valprint (void)
837 {
838   add_setshow_boolean_cmd ("static-members", class_support,
839                            &user_print_options.static_field_print, _("\
840 Set printing of C++ static members."), _("\
841 Show printing of C++ static members."), NULL,
842                            NULL,
843                            show_static_field_print,
844                            &setprintlist, &showprintlist);
845
846   add_setshow_boolean_cmd ("vtbl", class_support,
847                            &user_print_options.vtblprint, _("\
848 Set printing of C++ virtual function tables."), _("\
849 Show printing of C++ virtual function tables."), NULL,
850                            NULL,
851                            show_vtblprint,
852                            &setprintlist, &showprintlist);
853
854   add_setshow_boolean_cmd ("object", class_support,
855                            &user_print_options.objectprint, _("\
856 Set printing of object's derived type based on vtable info."), _("\
857 Show printing of object's derived type based on vtable info."), NULL,
858                            NULL,
859                            show_objectprint,
860                            &setprintlist, &showprintlist);
861
862   obstack_begin (&dont_print_stat_array_obstack,
863                  32 * sizeof (struct type *));
864   obstack_begin (&dont_print_statmem_obstack,
865                  32 * sizeof (CORE_ADDR));
866   obstack_begin (&dont_print_vb_obstack,
867                  32 * sizeof (struct type *));
868 }