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