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"
29 int vtblprint; /* Controls printing of vtbl's */
30 int objectprint; /* Controls looking up an object's derived type
31 using what we find in its vtables. */
32 struct obstack dont_print_obstack;
35 cplus_print_value PARAMS ((struct type *, char *, FILE *, int, int,
36 enum val_prettyprint, struct type **));
38 /* BEGIN-FIXME: Hooks into typeprint.c, find a better home for prototypes. */
41 c_type_print_base PARAMS ((struct type *, FILE *, int, int));
44 c_type_print_varspec_prefix PARAMS ((struct type *, FILE *, int, int));
47 cp_type_print_method_args PARAMS ((struct type **, char *, char *, int,
50 extern struct obstack dont_print_obstack;
55 /* BEGIN-FIXME: Hooks into c-valprint.c */
58 c_val_print PARAMS ((struct type *, char *, CORE_ADDR, FILE *, int, int, int,
59 enum val_prettyprint));
64 cp_print_class_method (valaddr, type, stream)
80 domain = TYPE_DOMAIN_TYPE (TYPE_TARGET_TYPE (type));
81 addr = unpack_pointer (lookup_pointer_type (builtin_type_void), valaddr);
82 if (METHOD_PTR_IS_VIRTUAL (addr))
84 offset = METHOD_PTR_TO_VOFFSET (addr);
85 len = TYPE_NFN_FIELDS (domain);
86 for (i = 0; i < len; i++)
88 f = TYPE_FN_FIELDLIST1 (domain, i);
89 len2 = TYPE_FN_FIELDLIST_LENGTH (domain, i);
91 for (j = 0; j < len2; j++)
94 if (TYPE_FN_FIELD_VOFFSET (f, j) == offset)
104 sym = find_pc_function (addr);
107 error ("invalid pointer to member function");
109 len = TYPE_NFN_FIELDS (domain);
110 for (i = 0; i < len; i++)
112 f = TYPE_FN_FIELDLIST1 (domain, i);
113 len2 = TYPE_FN_FIELDLIST_LENGTH (domain, i);
115 for (j = 0; j < len2; j++)
118 if (STREQ (SYMBOL_NAME (sym), TYPE_FN_FIELD_PHYSNAME (f, j)))
128 fprintf_filtered (stream, "&");
129 c_type_print_varspec_prefix (TYPE_FN_FIELD_TYPE (f, j), stream, 0, 0);
130 fprintf (stream, kind);
131 if (TYPE_FN_FIELD_PHYSNAME (f, j)[0] == '_'
132 && TYPE_FN_FIELD_PHYSNAME (f, j)[1] == CPLUS_MARKER)
134 cp_type_print_method_args (TYPE_FN_FIELD_ARGS (f, j) + 1, "~",
135 TYPE_FN_FIELDLIST_NAME (domain, i),
140 cp_type_print_method_args (TYPE_FN_FIELD_ARGS (f, j), "",
141 TYPE_FN_FIELDLIST_NAME (domain, i),
147 fprintf_filtered (stream, "(");
148 type_print (type, "", stream, -1);
149 fprintf_filtered (stream, ") %d", (int) addr >> 3);
153 /* Return truth value for assertion that TYPE is of the type
154 "pointer to virtual function". */
157 cp_is_vtbl_ptr_type(type)
160 char *typename = type_name_no_tag (type);
161 static const char vtbl_ptr_name[] =
162 { CPLUS_MARKER,'v','t','b','l','_','p','t','r','_','t','y','p','e', 0 };
164 return (typename != NULL && STREQ(typename, vtbl_ptr_name));
167 /* Return truth value for the assertion that TYPE is of the type
168 "pointer to virtual function table". */
171 cp_is_vtbl_member(type)
174 if (TYPE_CODE (type) == TYPE_CODE_PTR)
175 type = TYPE_TARGET_TYPE (type);
179 if (TYPE_CODE (type) == TYPE_CODE_ARRAY
180 && TYPE_CODE (TYPE_TARGET_TYPE (type)) == TYPE_CODE_STRUCT)
181 /* Virtual functions tables are full of pointers to virtual functions. */
182 return cp_is_vtbl_ptr_type (TYPE_TARGET_TYPE (type));
186 /* Mutually recursive subroutines of cplus_print_value and c_val_print to
187 print out a structure's fields: cp_print_value_fields and cplus_print_value.
189 TYPE, VALADDR, STREAM, RECURSE, and PRETTY have the
190 same meanings as in cplus_print_value and c_val_print.
192 DONT_PRINT is an array of baseclass types that we
193 should not print, or zero if called from top level. */
196 cp_print_value_fields (type, valaddr, stream, format, recurse, pretty,
203 enum val_prettyprint pretty;
204 struct type **dont_print;
206 int i, len, n_baseclasses;
208 check_stub_type (type);
210 fprintf_filtered (stream, "{");
211 len = TYPE_NFIELDS (type);
212 n_baseclasses = TYPE_N_BASECLASSES (type);
214 /* Print out baseclasses such that we don't print
215 duplicates of virtual baseclasses. */
216 if (n_baseclasses > 0)
217 cplus_print_value (type, valaddr, stream, format, recurse+1, pretty,
220 if (!len && n_baseclasses == 1)
221 fprintf_filtered (stream, "<No data fields>");
224 extern int inspect_it;
227 for (i = n_baseclasses; i < len; i++)
229 /* Check if static field */
230 if (TYPE_FIELD_STATIC (type, i))
233 fprintf_filtered (stream, ", ");
234 else if (n_baseclasses > 0)
238 fprintf_filtered (stream, "\n");
239 print_spaces_filtered (2 + 2 * recurse, stream);
240 fputs_filtered ("members of ", stream);
241 fputs_filtered (type_name_no_tag (type), stream);
242 fputs_filtered (": ", stream);
249 fprintf_filtered (stream, "\n");
250 print_spaces_filtered (2 + 2 * recurse, stream);
254 wrap_here (n_spaces (2 + 2 * recurse));
258 if (TYPE_CODE (TYPE_FIELD_TYPE (type, i)) == TYPE_CODE_PTR)
259 fputs_filtered ("\"( ptr \"", stream);
261 fputs_filtered ("\"( nodef \"", stream);
262 fprint_symbol (stream, TYPE_FIELD_NAME (type, i));
263 fputs_filtered ("\" \"", stream);
264 fprint_symbol (stream, TYPE_FIELD_NAME (type, i));
265 fputs_filtered ("\") \"", stream);
269 fprint_symbol (stream, TYPE_FIELD_NAME (type, i));
270 fputs_filtered (" = ", stream);
272 if (TYPE_FIELD_PACKED (type, i))
276 /* Bitfields require special handling, especially due to byte
278 v = value_from_longest (TYPE_FIELD_TYPE (type, i),
279 unpack_field_as_long (type, valaddr, i));
281 c_val_print (TYPE_FIELD_TYPE (type, i), VALUE_CONTENTS (v), 0,
282 stream, format, 0, recurse + 1, pretty);
286 c_val_print (TYPE_FIELD_TYPE (type, i),
287 valaddr + TYPE_FIELD_BITPOS (type, i) / 8,
288 0, stream, format, 0, recurse + 1, pretty);
293 fprintf_filtered (stream, "\n");
294 print_spaces_filtered (2 * recurse, stream);
297 fprintf_filtered (stream, "}");
300 /* Special val_print routine to avoid printing multiple copies of virtual
304 cplus_print_value (type, valaddr, stream, format, recurse, pretty, dont_print)
310 enum val_prettyprint pretty;
311 struct type **dont_print;
313 struct obstack tmp_obstack;
314 struct type **last_dont_print
315 = (struct type **)obstack_next_free (&dont_print_obstack);
316 int i, n_baseclasses = TYPE_N_BASECLASSES (type);
320 /* If we're at top level, carve out a completely fresh
321 chunk of the obstack and use that until this particular
322 invocation returns. */
323 tmp_obstack = dont_print_obstack;
324 /* Bump up the high-water mark. Now alpha is omega. */
325 obstack_finish (&dont_print_obstack);
328 for (i = 0; i < n_baseclasses; i++)
333 if (BASETYPE_VIA_VIRTUAL (type, i))
335 struct type **first_dont_print
336 = (struct type **)obstack_base (&dont_print_obstack);
338 int j = (struct type **)obstack_next_free (&dont_print_obstack)
342 if (TYPE_BASECLASS (type, i) == first_dont_print[j])
345 obstack_ptr_grow (&dont_print_obstack, TYPE_BASECLASS (type, i));
348 /* Fix to use baseclass_offset instead. FIXME */
349 baddr = baseclass_addr (type, i, valaddr, 0, &err);
350 if (err == 0 && baddr == 0)
351 error ("could not find virtual baseclass `%s'\n",
352 type_name_no_tag (TYPE_BASECLASS (type, i)));
356 fprintf_filtered (stream, "\n");
357 print_spaces_filtered (2 * recurse, stream);
359 fputs_filtered ("<", stream);
360 fputs_filtered (type_name_no_tag (TYPE_BASECLASS (type, i)), stream);
361 fputs_filtered ("> = ", stream);
363 fprintf_filtered (stream, "<invalid address 0x%x>", baddr);
365 cp_print_value_fields (TYPE_BASECLASS (type, i), baddr, stream, format,
367 (struct type **) obstack_base (&dont_print_obstack));
368 fputs_filtered (", ", stream);
376 /* Free the space used to deal with the printing
377 of this type from top level. */
378 obstack_free (&dont_print_obstack, last_dont_print);
379 /* Reset watermark so that we can continue protecting
380 ourselves from whatever we were protecting ourselves. */
381 dont_print_obstack = tmp_obstack;
386 cp_print_class_member (valaddr, domain, stream, prefix)
393 /* VAL is a byte offset into the structure type DOMAIN.
394 Find the name of the field for that offset and
398 register unsigned int i;
399 unsigned len = TYPE_NFIELDS (domain);
400 /* @@ Make VAL into bit offset */
401 LONGEST val = unpack_long (builtin_type_int, valaddr) << 3;
402 for (i = TYPE_N_BASECLASSES (domain); i < len; i++)
404 int bitpos = TYPE_FIELD_BITPOS (domain, i);
408 if (val < bitpos && i != 0)
410 /* Somehow pointing into a field. */
412 extra = (val - TYPE_FIELD_BITPOS (domain, i));
423 fprintf_filtered (stream, prefix);
424 name = type_name_no_tag (domain);
426 fputs_filtered (name, stream);
428 c_type_print_base (domain, stream, 0, 0);
429 fprintf_filtered (stream, "::");
430 fputs_filtered (TYPE_FIELD_NAME (domain, i), stream);
432 fprintf_filtered (stream, " + %d bytes", extra);
434 fprintf_filtered (stream, " (offset in bits)");
437 fprintf_filtered (stream, "%d", val >> 3);
441 _initialize_cp_valprint ()
444 (add_set_cmd ("vtbl", class_support, var_boolean, (char *)&vtblprint,
445 "Set printing of C++ virtual function tables.",
450 (add_set_cmd ("object", class_support, var_boolean, (char *)&objectprint,
451 "Set printing of object's derived type based on vtable info.",
455 /* Give people the defaults which they are used to. */
458 obstack_begin (&dont_print_obstack, 32 * sizeof (struct type *));