* Makefile.in (scm-exp.o, scm-lang.o, scm-valprint.o): Add targets and
[platform/upstream/binutils.git] / gdb / cp-valprint.c
1 /* Support for printing C++ values for GDB, the GNU debugger.
2    Copyright 1986, 1988, 1989, 1991, 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
34 int vtblprint;                  /* Controls printing of vtbl's */
35 int objectprint;                /* Controls looking up an object's derived type
36                                    using what we find in its vtables.  */
37 static int static_field_print;  /* Controls printing of static fields. */
38
39 static struct obstack dont_print_vb_obstack;
40 static struct obstack dont_print_statmem_obstack;
41
42 static void
43 cp_print_static_field PARAMS ((struct type *, value_ptr, GDB_FILE *, int, int,
44                                enum val_prettyprint));
45
46 static void
47 cp_print_value PARAMS ((struct type *, char *, CORE_ADDR, GDB_FILE *,
48                         int, int, enum val_prettyprint, struct type **));
49
50 void
51 cp_print_class_method (valaddr, type, stream)
52      char *valaddr;
53      struct type *type;
54      GDB_FILE *stream;
55 {
56   struct type *domain;
57   struct fn_field *f = NULL;
58   int j = 0;
59   int len2;
60   int offset;
61   char *kind = "";
62   CORE_ADDR addr;
63   struct symbol *sym;
64   unsigned len;
65   unsigned int i;
66   struct type *target_type = check_typedef (TYPE_TARGET_TYPE (type));
67
68   domain = TYPE_DOMAIN_TYPE (target_type);
69   if (domain == (struct type *)NULL)
70     {
71       fprintf_filtered (stream, "<unknown>");
72       return;
73     }
74   addr = unpack_pointer (lookup_pointer_type (builtin_type_void), valaddr);
75   if (METHOD_PTR_IS_VIRTUAL (addr))
76     {
77       offset = METHOD_PTR_TO_VOFFSET (addr);
78       len = TYPE_NFN_FIELDS (domain);
79       for (i = 0; i < len; i++)
80         {
81           f = TYPE_FN_FIELDLIST1 (domain, i);
82           len2 = TYPE_FN_FIELDLIST_LENGTH (domain, i);
83           
84           for (j = 0; j < len2; j++)
85             {
86               QUIT;
87               if (TYPE_FN_FIELD_VOFFSET (f, j) == offset)
88                 {
89                   kind = "virtual ";
90                   goto common;
91                 }
92             }
93         }
94     }
95   else
96     {
97       sym = find_pc_function (addr);
98       if (sym == 0)
99         {
100           error ("invalid pointer to member function");
101         }
102       len = TYPE_NFN_FIELDS (domain);
103       for (i = 0; i < len; i++)
104         {
105           f = TYPE_FN_FIELDLIST1 (domain, i);
106           len2 = TYPE_FN_FIELDLIST_LENGTH (domain, i);
107           
108           for (j = 0; j < len2; j++)
109             {
110               QUIT;
111               if (TYPE_FN_FIELD_STUB (f, j))
112                 check_stub_method (domain, i, j);
113               if (STREQ (SYMBOL_NAME (sym), TYPE_FN_FIELD_PHYSNAME (f, j)))
114                 {
115                   goto common;
116                 }
117             }
118         }
119     }
120   common:
121   if (i < len)
122     {
123       fprintf_filtered (stream, "&");
124       c_type_print_varspec_prefix (TYPE_FN_FIELD_TYPE (f, j), stream, 0, 0);
125       fprintf_unfiltered (stream, kind);
126       if (TYPE_FN_FIELD_PHYSNAME (f, j)[0] == '_'
127           && is_cplus_marker (TYPE_FN_FIELD_PHYSNAME (f, j)[1]))
128         {
129           cp_type_print_method_args (TYPE_FN_FIELD_ARGS (f, j) + 1, "~",
130                                      TYPE_FN_FIELDLIST_NAME (domain, i),
131                                      0, stream);
132         }
133       else
134         {
135           cp_type_print_method_args (TYPE_FN_FIELD_ARGS (f, j), "",
136                                      TYPE_FN_FIELDLIST_NAME (domain, i),
137                                      0, stream);
138         }
139     }
140   else
141     {
142       fprintf_filtered (stream, "(");
143       type_print (type, "", stream, -1);
144       fprintf_filtered (stream, ") %d", (int) addr >> 3);
145     }
146 }
147
148 /* This was what it was for gcc 2.4.5 and earlier.  */
149 static const char vtbl_ptr_name_old[] =
150   { CPLUS_MARKER,'v','t','b','l','_','p','t','r','_','t','y','p','e', 0 };
151 /* It was changed to this after 2.4.5.  */
152 const char vtbl_ptr_name[] =
153   { '_','_','v','t','b','l','_','p','t','r','_','t','y','p','e', 0 };
154
155 /* Return truth value for assertion that TYPE is of the type
156    "pointer to virtual function".  */
157
158 int
159 cp_is_vtbl_ptr_type(type)
160      struct type *type;
161 {
162   char *typename = type_name_no_tag (type);
163
164   return (typename != NULL
165           && (STREQ (typename, vtbl_ptr_name)
166               || STREQ (typename, vtbl_ptr_name_old)));
167 }
168
169 /* Return truth value for the assertion that TYPE is of the type
170    "pointer to virtual function table".  */
171
172 int
173 cp_is_vtbl_member(type)
174      struct type *type;
175 {
176   if (TYPE_CODE (type) == TYPE_CODE_PTR)
177     {
178       type = TYPE_TARGET_TYPE (type);
179       if (TYPE_CODE (type) == TYPE_CODE_ARRAY)
180         {
181           type = TYPE_TARGET_TYPE (type);
182           if (TYPE_CODE (type) == TYPE_CODE_STRUCT /* if not using thunks */
183               || TYPE_CODE (type) == TYPE_CODE_PTR) /* if using thunks */
184             {
185               /* Virtual functions tables are full of pointers
186                  to virtual functions. */
187               return cp_is_vtbl_ptr_type (type);
188             }
189         }
190     }
191   return 0;
192 }
193
194 /* Mutually recursive subroutines of cp_print_value and c_val_print to
195    print out a structure's fields: cp_print_value_fields and cp_print_value.
196   
197    TYPE, VALADDR, ADDRESS, STREAM, RECURSE, and PRETTY have the
198    same meanings as in cp_print_value and c_val_print.
199
200    DONT_PRINT is an array of baseclass types that we
201    should not print, or zero if called from top level.  */
202
203 void
204 cp_print_value_fields (type, valaddr, address, stream, format, recurse, pretty,
205                        dont_print_vb, dont_print_statmem)
206      struct type *type;
207      char *valaddr;
208      CORE_ADDR address;
209      GDB_FILE *stream;
210      int format;
211      int recurse;
212      enum val_prettyprint pretty;
213      struct type **dont_print_vb;
214      int dont_print_statmem;
215 {
216   int i, len, n_baseclasses;
217   struct obstack tmp_obstack;
218   char *last_dont_print = obstack_next_free (&dont_print_statmem_obstack);
219
220   CHECK_TYPEDEF (type);
221
222   fprintf_filtered (stream, "{");
223   len = TYPE_NFIELDS (type);
224   n_baseclasses = TYPE_N_BASECLASSES (type);
225
226   /* Print out baseclasses such that we don't print
227      duplicates of virtual baseclasses.  */
228   if (n_baseclasses > 0)
229     cp_print_value (type, valaddr, address, stream,
230                     format, recurse+1, pretty, dont_print_vb);
231
232   if (!len && n_baseclasses == 1)
233     fprintf_filtered (stream, "<No data fields>");
234   else
235     {
236       extern int inspect_it;
237       int fields_seen = 0;
238
239       if (dont_print_statmem == 0)
240         {
241           /* If we're at top level, carve out a completely fresh
242              chunk of the obstack and use that until this particular
243              invocation returns.  */
244           tmp_obstack = dont_print_statmem_obstack;
245           obstack_finish (&dont_print_statmem_obstack);
246         }
247
248       for (i = n_baseclasses; i < len; i++)
249         {
250           /* If requested, skip printing of static fields.  */
251           if (!static_field_print && TYPE_FIELD_STATIC (type, i))
252             continue;
253           if (fields_seen)
254             fprintf_filtered (stream, ", ");
255           else if (n_baseclasses > 0)
256             {
257               if (pretty)
258                 {
259                   fprintf_filtered (stream, "\n");
260                   print_spaces_filtered (2 + 2 * recurse, stream);
261                   fputs_filtered ("members of ", stream);
262                   fputs_filtered (type_name_no_tag (type), stream);
263                   fputs_filtered (": ", stream);
264                 }
265             }
266           fields_seen = 1;
267
268           if (pretty)
269             {
270               fprintf_filtered (stream, "\n");
271               print_spaces_filtered (2 + 2 * recurse, stream);
272             }
273           else 
274             {
275               wrap_here (n_spaces (2 + 2 * recurse));
276             }
277           if (inspect_it)
278             {
279               if (TYPE_CODE (TYPE_FIELD_TYPE (type, i)) == TYPE_CODE_PTR)
280                 fputs_filtered ("\"( ptr \"", stream);
281               else
282                 fputs_filtered ("\"( nodef \"", stream);
283               if (TYPE_FIELD_STATIC (type, i))
284                 fputs_filtered ("static ", stream);
285               fprintf_symbol_filtered (stream, TYPE_FIELD_NAME (type, i),
286                                        language_cplus,
287                                        DMGL_PARAMS | DMGL_ANSI);
288               fputs_filtered ("\" \"", stream);
289               fprintf_symbol_filtered (stream, TYPE_FIELD_NAME (type, i),
290                                        language_cplus,
291                                        DMGL_PARAMS | DMGL_ANSI);
292               fputs_filtered ("\") \"", stream);
293             }
294           else
295             {
296               annotate_field_begin (TYPE_FIELD_TYPE (type, i));
297
298               if (TYPE_FIELD_STATIC (type, i))
299                 fputs_filtered ("static ", stream);
300               fprintf_symbol_filtered (stream, TYPE_FIELD_NAME (type, i),
301                                        language_cplus,
302                                        DMGL_PARAMS | DMGL_ANSI);
303               annotate_field_name_end ();
304               fputs_filtered (" = ", stream);
305               annotate_field_value ();
306             }
307
308           if (!TYPE_FIELD_STATIC (type, i) && TYPE_FIELD_PACKED (type, i))
309             {
310               value_ptr v;
311
312               /* Bitfields require special handling, especially due to byte
313                  order problems.  */
314               if (TYPE_FIELD_IGNORE (type, i))
315                 {
316                    fputs_filtered ("<optimized out or zero length>", stream);
317                 }
318               else
319                 {
320                    v = value_from_longest (TYPE_FIELD_TYPE (type, i),
321                                    unpack_field_as_long (type, valaddr, i));
322
323                    val_print (TYPE_FIELD_TYPE(type, i), VALUE_CONTENTS (v), 0,
324                               stream, format, 0, recurse + 1, pretty);
325                 }
326             }
327           else
328             {
329               if (TYPE_FIELD_IGNORE (type, i))
330                 {
331                    fputs_filtered ("<optimized out or zero length>", stream);
332                 }
333               else if (TYPE_FIELD_STATIC (type, i))
334                 {
335                   value_ptr v;
336                   char *phys_name = TYPE_FIELD_STATIC_PHYSNAME (type, i);
337                   struct symbol *sym =
338                       lookup_symbol (phys_name, 0, VAR_NAMESPACE, 0, NULL);
339                   if (sym == NULL)
340                     fputs_filtered ("<optimized out>", stream);
341                   else
342                     {
343                       v = value_at (TYPE_FIELD_TYPE (type, i),
344                                     (CORE_ADDR)SYMBOL_BLOCK_VALUE (sym));
345                       cp_print_static_field (TYPE_FIELD_TYPE (type, i), v,
346                                              stream, format, recurse + 1,
347                                              pretty);
348                     }
349                 }
350               else
351                 {
352                    val_print (TYPE_FIELD_TYPE (type, i), 
353                               valaddr + TYPE_FIELD_BITPOS (type, i) / 8,
354                               0, stream, format, 0, recurse + 1, pretty);
355                 }
356             }
357           annotate_field_end ();
358         }
359
360       if (dont_print_statmem == 0)
361         {
362           /* Free the space used to deal with the printing
363              of the members from top level.  */
364           obstack_free (&dont_print_statmem_obstack, last_dont_print);
365           dont_print_statmem_obstack = tmp_obstack;
366         }
367
368       if (pretty)
369         {
370           fprintf_filtered (stream, "\n");
371           print_spaces_filtered (2 * recurse, stream);
372         }
373     }
374   fprintf_filtered (stream, "}");
375 }
376
377 /* Special val_print routine to avoid printing multiple copies of virtual
378    baseclasses.  */
379
380 static void
381 cp_print_value (type, valaddr, address, stream, format, recurse, pretty,
382                 dont_print_vb)
383      struct type *type;
384      char *valaddr;
385      CORE_ADDR address;
386      GDB_FILE *stream;
387      int format;
388      int recurse;
389      enum val_prettyprint pretty;
390      struct type **dont_print_vb;
391 {
392   struct obstack tmp_obstack;
393   struct type **last_dont_print
394     = (struct type **)obstack_next_free (&dont_print_vb_obstack);
395   int i, n_baseclasses = TYPE_N_BASECLASSES (type);
396
397   if (dont_print_vb == 0)
398     {
399       /* If we're at top level, carve out a completely fresh
400          chunk of the obstack and use that until this particular
401          invocation returns.  */
402       tmp_obstack = dont_print_vb_obstack;
403       /* Bump up the high-water mark.  Now alpha is omega.  */
404       obstack_finish (&dont_print_vb_obstack);
405     }
406
407   for (i = 0; i < n_baseclasses; i++)
408     {
409       int boffset;
410       struct type *baseclass = check_typedef (TYPE_BASECLASS (type, i));
411       char *basename = TYPE_NAME (baseclass);
412
413       if (BASETYPE_VIA_VIRTUAL (type, i))
414         {
415           struct type **first_dont_print
416             = (struct type **)obstack_base (&dont_print_vb_obstack);
417
418           int j = (struct type **)obstack_next_free (&dont_print_vb_obstack)
419             - first_dont_print;
420
421           while (--j >= 0)
422             if (baseclass == first_dont_print[j])
423               goto flush_it;
424
425           obstack_ptr_grow (&dont_print_vb_obstack, baseclass);
426         }
427
428       boffset = baseclass_offset (type, i , valaddr, address);
429
430       if (pretty)
431         {
432           fprintf_filtered (stream, "\n");
433           print_spaces_filtered (2 * recurse, stream);
434         }
435       fputs_filtered ("<", stream);
436       /* Not sure what the best notation is in the case where there is no
437          baseclass name.  */
438       fputs_filtered (basename ? basename : "", stream);
439       fputs_filtered ("> = ", stream);
440       if (boffset == -1)
441         fprintf_filtered (stream, "<invalid address>");
442       else
443         cp_print_value_fields (baseclass, valaddr + boffset, address + boffset,
444                                stream, format, recurse, pretty,
445                                (struct type **) obstack_base (&dont_print_vb_obstack),
446                                0);
447       fputs_filtered (", ", stream);
448
449     flush_it:
450       ;
451     }
452
453   if (dont_print_vb == 0)
454     {
455       /* Free the space used to deal with the printing
456          of this type from top level.  */
457       obstack_free (&dont_print_vb_obstack, last_dont_print);
458       /* Reset watermark so that we can continue protecting
459          ourselves from whatever we were protecting ourselves.  */
460       dont_print_vb_obstack = tmp_obstack;
461     }
462 }
463
464 /* Print value of a static member.
465    To avoid infinite recursion when printing a class that contains
466    a static instance of the class, we keep the addresses of all printed
467    static member classes in an obstack and refuse to print them more
468    than once.
469
470    VAL contains the value to print, TYPE, STREAM, RECURSE, and PRETTY
471    have the same meanings as in c_val_print.  */
472
473 static void
474 cp_print_static_field (type, val, stream, format, recurse, pretty)
475      struct type *type;
476      value_ptr val;
477      GDB_FILE *stream;
478      int format;
479      int recurse;
480      enum val_prettyprint pretty;
481 {
482   if (TYPE_CODE (type) == TYPE_CODE_STRUCT)
483     {
484       CORE_ADDR *first_dont_print;
485       int i;
486
487       first_dont_print
488         = (CORE_ADDR *)obstack_base (&dont_print_statmem_obstack);
489       i = (CORE_ADDR *)obstack_next_free (&dont_print_statmem_obstack)
490         - first_dont_print;
491
492       while (--i >= 0)
493         {
494           if (VALUE_ADDRESS (val) == first_dont_print[i])
495             {
496               fputs_filtered ("<same as static member of an already seen type>",
497                               stream);
498               return;
499             }
500         }
501
502       obstack_grow (&dont_print_statmem_obstack, (char *) &VALUE_ADDRESS (val),
503                     sizeof (CORE_ADDR));
504
505       CHECK_TYPEDEF (type);
506       cp_print_value_fields (type, VALUE_CONTENTS (val), VALUE_ADDRESS (val),
507                              stream, format, recurse, pretty, NULL, 1);
508       return;
509     }
510   val_print (type, VALUE_CONTENTS (val), VALUE_ADDRESS (val),
511              stream, format, 0, recurse, pretty);
512 }
513
514 void
515 cp_print_class_member (valaddr, domain, stream, prefix)
516      char *valaddr;
517      struct type *domain;
518      GDB_FILE *stream;
519      char *prefix;
520 {
521   
522   /* VAL is a byte offset into the structure type DOMAIN.
523      Find the name of the field for that offset and
524      print it.  */
525   int extra = 0;
526   int bits = 0;
527   register unsigned int i;
528   unsigned len = TYPE_NFIELDS (domain);
529   /* @@ Make VAL into bit offset */
530   LONGEST val = unpack_long (builtin_type_int, valaddr) << 3;
531   for (i = TYPE_N_BASECLASSES (domain); i < len; i++)
532     {
533       int bitpos = TYPE_FIELD_BITPOS (domain, i);
534       QUIT;
535       if (val == bitpos)
536         break;
537       if (val < bitpos && i != 0)
538         {
539           /* Somehow pointing into a field.  */
540           i -= 1;
541           extra = (val - TYPE_FIELD_BITPOS (domain, i));
542           if (extra & 0x7)
543             bits = 1;
544           else
545             extra >>= 3;
546           break;
547         }
548     }
549   if (i < len)
550     {
551       char *name;
552       fprintf_filtered (stream, prefix);
553       name = type_name_no_tag (domain);
554       if (name)
555         fputs_filtered (name, stream);
556       else
557         c_type_print_base (domain, stream, 0, 0);
558       fprintf_filtered (stream, "::");
559       fputs_filtered (TYPE_FIELD_NAME (domain, i), stream);
560       if (extra)
561         fprintf_filtered (stream, " + %d bytes", extra);
562       if (bits)
563         fprintf_filtered (stream, " (offset in bits)");
564     }
565   else
566     fprintf_filtered (stream, "%d", val >> 3);
567 }
568
569 void
570 _initialize_cp_valprint ()
571 {
572   add_show_from_set
573     (add_set_cmd ("static-members", class_support, var_boolean,
574                   (char *)&static_field_print,
575                   "Set printing of C++ static members.",
576                   &setprintlist),
577      &showprintlist);
578   /* Turn on printing of static fields.  */
579   static_field_print = 1;
580
581   add_show_from_set
582     (add_set_cmd ("vtbl", class_support, var_boolean, (char *)&vtblprint,
583                   "Set printing of C++ virtual function tables.",
584                   &setprintlist),
585      &showprintlist);
586
587   add_show_from_set
588     (add_set_cmd ("object", class_support, var_boolean, (char *)&objectprint,
589           "Set printing of object's derived type based on vtable info.",
590                   &setprintlist),
591      &showprintlist);
592
593   /* Give people the defaults which they are used to.  */
594   objectprint = 0;
595   vtblprint = 0;
596   obstack_begin (&dont_print_vb_obstack, 32 * sizeof (struct type *));
597   obstack_specify_allocation (&dont_print_statmem_obstack,
598                               32 * sizeof (CORE_ADDR), sizeof (CORE_ADDR),
599                               xmalloc, free);
600 }