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
));
63 /* Return truth value for assertion that TYPE is of the type
64 "pointer to virtual function". */
67 cp_is_vtbl_ptr_type(type
)
70 char *typename
= type_name_no_tag (type
);
71 static const char vtbl_ptr_name
[] =
72 { CPLUS_MARKER
,'v','t','b','l','_','p','t','r','_','t','y','p','e', 0 };
74 return (typename
!= NULL
&& STREQ(typename
, vtbl_ptr_name
));
77 /* Return truth value for the assertion that TYPE is of the type
78 "pointer to virtual function table". */
81 cp_is_vtbl_member(type
)
84 if (TYPE_CODE (type
) == TYPE_CODE_PTR
)
85 type
= TYPE_TARGET_TYPE (type
);
89 if (TYPE_CODE (type
) == TYPE_CODE_ARRAY
90 && TYPE_CODE (TYPE_TARGET_TYPE (type
)) == TYPE_CODE_STRUCT
)
91 /* Virtual functions tables are full of pointers to virtual functions. */
92 return cp_is_vtbl_ptr_type (TYPE_TARGET_TYPE (type
));
96 /* Mutually recursive subroutines of cplus_print_value and c_val_print to
97 print out a structure's fields: cp_print_value_fields and cplus_print_value.
99 TYPE, VALADDR, STREAM, RECURSE, and PRETTY have the
100 same meanings as in cplus_print_value and c_val_print.
102 DONT_PRINT is an array of baseclass types that we
103 should not print, or zero if called from top level. */
106 cp_print_value_fields (type
, valaddr
, stream
, format
, recurse
, pretty
,
113 enum val_prettyprint pretty
;
114 struct type
**dont_print
;
116 int i
, len
, n_baseclasses
;
118 check_stub_type (type
);
120 fprintf_filtered (stream
, "{");
121 len
= TYPE_NFIELDS (type
);
122 n_baseclasses
= TYPE_N_BASECLASSES (type
);
124 /* Print out baseclasses such that we don't print
125 duplicates of virtual baseclasses. */
126 if (n_baseclasses
> 0)
127 cplus_print_value (type
, valaddr
, stream
, format
, recurse
+1, pretty
,
130 if (!len
&& n_baseclasses
== 1)
131 fprintf_filtered (stream
, "<No data fields>");
134 extern int inspect_it
;
137 for (i
= n_baseclasses
; i
< len
; i
++)
139 /* Check if static field */
140 if (TYPE_FIELD_STATIC (type
, i
))
143 fprintf_filtered (stream
, ", ");
144 else if (n_baseclasses
> 0)
148 fprintf_filtered (stream
, "\n");
149 print_spaces_filtered (2 + 2 * recurse
, stream
);
150 fputs_filtered ("members of ", stream
);
151 fputs_filtered (type_name_no_tag (type
), stream
);
152 fputs_filtered (": ", stream
);
159 fprintf_filtered (stream
, "\n");
160 print_spaces_filtered (2 + 2 * recurse
, stream
);
164 wrap_here (n_spaces (2 + 2 * recurse
));
168 if (TYPE_CODE (TYPE_FIELD_TYPE (type
, i
)) == TYPE_CODE_PTR
)
169 fputs_filtered ("\"( ptr \"", stream
);
171 fputs_filtered ("\"( nodef \"", stream
);
172 fprint_symbol (stream
, TYPE_FIELD_NAME (type
, i
));
173 fputs_filtered ("\" \"", stream
);
174 fprint_symbol (stream
, TYPE_FIELD_NAME (type
, i
));
175 fputs_filtered ("\") \"", stream
);
179 fprint_symbol (stream
, TYPE_FIELD_NAME (type
, i
));
180 fputs_filtered (" = ", stream
);
182 if (TYPE_FIELD_PACKED (type
, i
))
186 /* Bitfields require special handling, especially due to byte
188 v
= value_from_longest (TYPE_FIELD_TYPE (type
, i
),
189 unpack_field_as_long (type
, valaddr
, i
));
191 c_val_print (TYPE_FIELD_TYPE (type
, i
), VALUE_CONTENTS (v
), 0,
192 stream
, format
, 0, recurse
+ 1, pretty
);
196 c_val_print (TYPE_FIELD_TYPE (type
, i
),
197 valaddr
+ TYPE_FIELD_BITPOS (type
, i
) / 8,
198 0, stream
, format
, 0, recurse
+ 1, pretty
);
203 fprintf_filtered (stream
, "\n");
204 print_spaces_filtered (2 * recurse
, stream
);
207 fprintf_filtered (stream
, "}");
210 /* Special val_print routine to avoid printing multiple copies of virtual
214 cplus_print_value (type
, valaddr
, stream
, format
, recurse
, pretty
, dont_print
)
220 enum val_prettyprint pretty
;
221 struct type
**dont_print
;
223 struct obstack tmp_obstack
;
224 struct type
**last_dont_print
225 = (struct type
**)obstack_next_free (&dont_print_obstack
);
226 int i
, n_baseclasses
= TYPE_N_BASECLASSES (type
);
230 /* If we're at top level, carve out a completely fresh
231 chunk of the obstack and use that until this particular
232 invocation returns. */
233 tmp_obstack
= dont_print_obstack
;
234 /* Bump up the high-water mark. Now alpha is omega. */
235 obstack_finish (&dont_print_obstack
);
238 for (i
= 0; i
< n_baseclasses
; i
++)
243 if (BASETYPE_VIA_VIRTUAL (type
, i
))
245 struct type
**first_dont_print
246 = (struct type
**)obstack_base (&dont_print_obstack
);
248 int j
= (struct type
**)obstack_next_free (&dont_print_obstack
)
252 if (TYPE_BASECLASS (type
, i
) == first_dont_print
[j
])
255 obstack_ptr_grow (&dont_print_obstack
, TYPE_BASECLASS (type
, i
));
258 /* Fix to use baseclass_offset instead. FIXME */
259 baddr
= baseclass_addr (type
, i
, valaddr
, 0, &err
);
260 if (err
== 0 && baddr
== 0)
261 error ("could not find virtual baseclass `%s'\n",
262 type_name_no_tag (TYPE_BASECLASS (type
, i
)));
266 fprintf_filtered (stream
, "\n");
267 print_spaces_filtered (2 * recurse
, stream
);
269 fputs_filtered ("<", stream
);
270 fputs_filtered (type_name_no_tag (TYPE_BASECLASS (type
, i
)), stream
);
271 fputs_filtered ("> = ", stream
);
273 fprintf_filtered (stream
, "<invalid address 0x%x>", baddr
);
275 cp_print_value_fields (TYPE_BASECLASS (type
, i
), baddr
, stream
, format
,
277 (struct type
**) obstack_base (&dont_print_obstack
));
278 fputs_filtered (", ", stream
);
286 /* Free the space used to deal with the printing
287 of this type from top level. */
288 obstack_free (&dont_print_obstack
, last_dont_print
);
289 /* Reset watermark so that we can continue protecting
290 ourselves from whatever we were protecting ourselves. */
291 dont_print_obstack
= tmp_obstack
;
296 cp_print_class_member (valaddr
, domain
, stream
, prefix
)
303 /* VAL is a byte offset into the structure type DOMAIN.
304 Find the name of the field for that offset and
308 register unsigned int i
;
309 unsigned len
= TYPE_NFIELDS (domain
);
310 /* @@ Make VAL into bit offset */
311 LONGEST val
= unpack_long (builtin_type_int
, valaddr
) << 3;
312 for (i
= TYPE_N_BASECLASSES (domain
); i
< len
; i
++)
314 int bitpos
= TYPE_FIELD_BITPOS (domain
, i
);
318 if (val
< bitpos
&& i
!= 0)
320 /* Somehow pointing into a field. */
322 extra
= (val
- TYPE_FIELD_BITPOS (domain
, i
));
333 fprintf_filtered (stream
, prefix
);
334 name
= type_name_no_tag (domain
);
336 fputs_filtered (name
, stream
);
338 c_type_print_base (domain
, stream
, 0, 0);
339 fprintf_filtered (stream
, "::");
340 fputs_filtered (TYPE_FIELD_NAME (domain
, i
), stream
);
342 fprintf_filtered (stream
, " + %d bytes", extra
);
344 fprintf_filtered (stream
, " (offset in bits)");
347 fprintf_filtered (stream
, "%d", val
>> 3);
351 _initialize_cp_valprint ()
354 (add_set_cmd ("vtbl", class_support
, var_boolean
, (char *)&vtblprint
,
355 "Set printing of C++ virtual function tables.",
360 (add_set_cmd ("object", class_support
, var_boolean
, (char *)&objectprint
,
361 "Set printing of object's derived type based on vtable info.",
365 /* Give people the defaults which they are used to. */
368 obstack_begin (&dont_print_obstack
, 32 * sizeof (struct type
*));