1 /* Support for printing C++ values for GDB, the GNU debugger.
2 Copyright 1986, 1988, 1989, 1991 Free Software Foundation, Inc.
4 This file is part of GDB.
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
24 #include "expression.h"
30 int vtblprint; /* Controls printing of vtbl's */
31 int objectprint; /* Controls looking up an object's derived type
32 using what we find in its vtables. */
33 struct obstack dont_print_obstack;
36 cplus_print_value PARAMS ((struct type *, char *, GDB_FILE *, int, int,
37 enum val_prettyprint, struct type **));
39 /* BEGIN-FIXME: Hooks into typeprint.c, find a better home for prototypes. */
42 c_type_print_base PARAMS ((struct type *, GDB_FILE *, int, int));
45 c_type_print_varspec_prefix PARAMS ((struct type *, GDB_FILE *, int, int));
48 cp_type_print_method_args PARAMS ((struct type **, char *, char *, int,
51 extern struct obstack dont_print_obstack;
56 /* BEGIN-FIXME: Hooks into c-valprint.c */
59 c_val_print PARAMS ((struct type *, char *, CORE_ADDR, GDB_FILE *, int, int, int,
60 enum val_prettyprint));
65 cp_print_class_method (valaddr, type, stream)
71 struct fn_field *f = NULL;
81 check_stub_type (TYPE_TARGET_TYPE (type));
82 domain = TYPE_DOMAIN_TYPE (TYPE_TARGET_TYPE (type));
83 if (domain == (struct type *)NULL)
85 fprintf_filtered (stream, "<unknown>");
88 addr = unpack_pointer (lookup_pointer_type (builtin_type_void), valaddr);
89 if (METHOD_PTR_IS_VIRTUAL (addr))
91 offset = METHOD_PTR_TO_VOFFSET (addr);
92 len = TYPE_NFN_FIELDS (domain);
93 for (i = 0; i < len; i++)
95 f = TYPE_FN_FIELDLIST1 (domain, i);
96 len2 = TYPE_FN_FIELDLIST_LENGTH (domain, i);
98 for (j = 0; j < len2; j++)
101 if (TYPE_FN_FIELD_VOFFSET (f, j) == offset)
111 sym = find_pc_function (addr);
114 error ("invalid pointer to member function");
116 len = TYPE_NFN_FIELDS (domain);
117 for (i = 0; i < len; i++)
119 f = TYPE_FN_FIELDLIST1 (domain, i);
120 len2 = TYPE_FN_FIELDLIST_LENGTH (domain, i);
122 for (j = 0; j < len2; j++)
125 if (TYPE_FN_FIELD_STUB (f, j))
126 check_stub_method (domain, i, j);
127 if (STREQ (SYMBOL_NAME (sym), TYPE_FN_FIELD_PHYSNAME (f, j)))
137 fprintf_filtered (stream, "&");
138 c_type_print_varspec_prefix (TYPE_FN_FIELD_TYPE (f, j), stream, 0, 0);
139 fprintf_unfiltered (stream, kind);
140 if (TYPE_FN_FIELD_PHYSNAME (f, j)[0] == '_'
141 && TYPE_FN_FIELD_PHYSNAME (f, j)[1] == CPLUS_MARKER)
143 cp_type_print_method_args (TYPE_FN_FIELD_ARGS (f, j) + 1, "~",
144 TYPE_FN_FIELDLIST_NAME (domain, i),
149 cp_type_print_method_args (TYPE_FN_FIELD_ARGS (f, j), "",
150 TYPE_FN_FIELDLIST_NAME (domain, i),
156 fprintf_filtered (stream, "(");
157 type_print (type, "", stream, -1);
158 fprintf_filtered (stream, ") %d", (int) addr >> 3);
162 /* Return truth value for assertion that TYPE is of the type
163 "pointer to virtual function". */
166 cp_is_vtbl_ptr_type(type)
169 char *typename = type_name_no_tag (type);
170 /* This was what it was for gcc 2.4.5 and earlier. */
171 static const char vtbl_ptr_name_old[] =
172 { CPLUS_MARKER,'v','t','b','l','_','p','t','r','_','t','y','p','e', 0 };
173 /* It was changed to this after 2.4.5. */
174 static const char vtbl_ptr_name[] =
175 { '_','_','v','t','b','l','_','p','t','r','_','t','y','p','e', 0 };
177 return (typename != NULL
178 && (STREQ (typename, vtbl_ptr_name)
179 || STREQ (typename, vtbl_ptr_name_old)));
182 /* Return truth value for the assertion that TYPE is of the type
183 "pointer to virtual function table". */
186 cp_is_vtbl_member(type)
189 if (TYPE_CODE (type) == TYPE_CODE_PTR)
190 type = TYPE_TARGET_TYPE (type);
194 if (TYPE_CODE (type) == TYPE_CODE_ARRAY
195 && TYPE_CODE (TYPE_TARGET_TYPE (type)) == TYPE_CODE_STRUCT)
196 /* Virtual functions tables are full of pointers to virtual functions. */
197 return cp_is_vtbl_ptr_type (TYPE_TARGET_TYPE (type));
201 /* Mutually recursive subroutines of cplus_print_value and c_val_print to
202 print out a structure's fields: cp_print_value_fields and cplus_print_value.
204 TYPE, VALADDR, STREAM, RECURSE, and PRETTY have the
205 same meanings as in cplus_print_value and c_val_print.
207 DONT_PRINT is an array of baseclass types that we
208 should not print, or zero if called from top level. */
211 cp_print_value_fields (type, valaddr, stream, format, recurse, pretty,
218 enum val_prettyprint pretty;
219 struct type **dont_print;
221 int i, len, n_baseclasses;
223 check_stub_type (type);
225 fprintf_filtered (stream, "{");
226 len = TYPE_NFIELDS (type);
227 n_baseclasses = TYPE_N_BASECLASSES (type);
229 /* Print out baseclasses such that we don't print
230 duplicates of virtual baseclasses. */
231 if (n_baseclasses > 0)
232 cplus_print_value (type, valaddr, stream, format, recurse+1, pretty,
235 if (!len && n_baseclasses == 1)
236 fprintf_filtered (stream, "<No data fields>");
239 extern int inspect_it;
242 for (i = n_baseclasses; i < len; i++)
244 /* Check if static field */
245 if (TYPE_FIELD_STATIC (type, i))
248 fprintf_filtered (stream, ", ");
249 else if (n_baseclasses > 0)
253 fprintf_filtered (stream, "\n");
254 print_spaces_filtered (2 + 2 * recurse, stream);
255 fputs_filtered ("members of ", stream);
256 fputs_filtered (type_name_no_tag (type), stream);
257 fputs_filtered (": ", stream);
264 fprintf_filtered (stream, "\n");
265 print_spaces_filtered (2 + 2 * recurse, stream);
269 wrap_here (n_spaces (2 + 2 * recurse));
273 if (TYPE_CODE (TYPE_FIELD_TYPE (type, i)) == TYPE_CODE_PTR)
274 fputs_filtered ("\"( ptr \"", stream);
276 fputs_filtered ("\"( nodef \"", stream);
277 fprintf_symbol_filtered (stream, TYPE_FIELD_NAME (type, i),
279 DMGL_PARAMS | DMGL_ANSI);
280 fputs_filtered ("\" \"", stream);
281 fprintf_symbol_filtered (stream, TYPE_FIELD_NAME (type, i),
283 DMGL_PARAMS | DMGL_ANSI);
284 fputs_filtered ("\") \"", stream);
288 fprintf_symbol_filtered (stream, TYPE_FIELD_NAME (type, i),
290 DMGL_PARAMS | DMGL_ANSI);
291 fputs_filtered (" = ", stream);
293 if (TYPE_FIELD_PACKED (type, i))
297 /* Bitfields require special handling, especially due to byte
299 if (TYPE_FIELD_IGNORE (type, i))
301 fputs_filtered ("<optimized out or zero length>", stream);
305 v = value_from_longest (TYPE_FIELD_TYPE (type, i),
306 unpack_field_as_long (type, valaddr, i));
308 c_val_print (TYPE_FIELD_TYPE(type, i), VALUE_CONTENTS (v), 0,
309 stream, format, 0, recurse + 1, pretty);
314 if (TYPE_FIELD_IGNORE (type, i))
316 fputs_filtered ("<optimized out or zero length>", stream);
320 c_val_print (TYPE_FIELD_TYPE (type, i),
321 valaddr + TYPE_FIELD_BITPOS (type, i) / 8,
322 0, stream, format, 0, recurse + 1, pretty);
328 fprintf_filtered (stream, "\n");
329 print_spaces_filtered (2 * recurse, stream);
332 fprintf_filtered (stream, "}");
335 /* Special val_print routine to avoid printing multiple copies of virtual
339 cplus_print_value (type, valaddr, stream, format, recurse, pretty, dont_print)
345 enum val_prettyprint pretty;
346 struct type **dont_print;
348 struct obstack tmp_obstack;
349 struct type **last_dont_print
350 = (struct type **)obstack_next_free (&dont_print_obstack);
351 int i, n_baseclasses = TYPE_N_BASECLASSES (type);
355 /* If we're at top level, carve out a completely fresh
356 chunk of the obstack and use that until this particular
357 invocation returns. */
358 tmp_obstack = dont_print_obstack;
359 /* Bump up the high-water mark. Now alpha is omega. */
360 obstack_finish (&dont_print_obstack);
363 for (i = 0; i < n_baseclasses; i++)
367 char *basename = TYPE_NAME (TYPE_BASECLASS (type, i));
369 if (BASETYPE_VIA_VIRTUAL (type, i))
371 struct type **first_dont_print
372 = (struct type **)obstack_base (&dont_print_obstack);
374 int j = (struct type **)obstack_next_free (&dont_print_obstack)
378 if (TYPE_BASECLASS (type, i) == first_dont_print[j])
381 obstack_ptr_grow (&dont_print_obstack, TYPE_BASECLASS (type, i));
384 /* Fix to use baseclass_offset instead. FIXME */
385 baddr = baseclass_addr (type, i, valaddr, 0, &err);
386 if (err == 0 && baddr == 0)
387 error ("could not find virtual baseclass %s\n",
388 basename ? basename : "");
392 fprintf_filtered (stream, "\n");
393 print_spaces_filtered (2 * recurse, stream);
395 fputs_filtered ("<", stream);
396 /* Not sure what the best notation is in the case where there is no
398 fputs_filtered (basename ? basename : "", stream);
399 fputs_filtered ("> = ", stream);
401 fprintf_filtered (stream,
402 "<invalid address 0x%lx>", (unsigned long) baddr);
404 cp_print_value_fields (TYPE_BASECLASS (type, i), baddr, stream, format,
406 (struct type **) obstack_base (&dont_print_obstack));
407 fputs_filtered (", ", stream);
415 /* Free the space used to deal with the printing
416 of this type from top level. */
417 obstack_free (&dont_print_obstack, last_dont_print);
418 /* Reset watermark so that we can continue protecting
419 ourselves from whatever we were protecting ourselves. */
420 dont_print_obstack = tmp_obstack;
425 cp_print_class_member (valaddr, domain, stream, prefix)
432 /* VAL is a byte offset into the structure type DOMAIN.
433 Find the name of the field for that offset and
437 register unsigned int i;
438 unsigned len = TYPE_NFIELDS (domain);
439 /* @@ Make VAL into bit offset */
440 LONGEST val = unpack_long (builtin_type_int, valaddr) << 3;
441 for (i = TYPE_N_BASECLASSES (domain); i < len; i++)
443 int bitpos = TYPE_FIELD_BITPOS (domain, i);
447 if (val < bitpos && i != 0)
449 /* Somehow pointing into a field. */
451 extra = (val - TYPE_FIELD_BITPOS (domain, i));
462 fprintf_filtered (stream, prefix);
463 name = type_name_no_tag (domain);
465 fputs_filtered (name, stream);
467 c_type_print_base (domain, stream, 0, 0);
468 fprintf_filtered (stream, "::");
469 fputs_filtered (TYPE_FIELD_NAME (domain, i), stream);
471 fprintf_filtered (stream, " + %d bytes", extra);
473 fprintf_filtered (stream, " (offset in bits)");
476 fprintf_filtered (stream, "%d", val >> 3);
480 _initialize_cp_valprint ()
483 (add_set_cmd ("vtbl", class_support, var_boolean, (char *)&vtblprint,
484 "Set printing of C++ virtual function tables.",
489 (add_set_cmd ("object", class_support, var_boolean, (char *)&objectprint,
490 "Set printing of object's derived type based on vtable info.",
494 /* Give people the defaults which they are used to. */
497 obstack_begin (&dont_print_obstack, 32 * sizeof (struct type *));