* utils.c (fputs_demangled, fprint_symbol): Remove.
[deliverable/binutils-gdb.git] / gdb / cp-valprint.c
CommitLineData
a8a69e63
FF
1/* Support for printing C++ values for GDB, the GNU debugger.
2 Copyright 1986, 1988, 1989, 1991 Free Software Foundation, Inc.
3
4This file is part of GDB.
5
6This program is free software; you can redistribute it and/or modify
7it under the terms of the GNU General Public License as published by
8the Free Software Foundation; either version 2 of the License, or
9(at your option) any later version.
10
11This program is distributed in the hope that it will be useful,
12but WITHOUT ANY WARRANTY; without even the implied warranty of
13MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14GNU General Public License for more details.
15
16You should have received a copy of the GNU General Public License
17along with this program; if not, write to the Free Software
18Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
19
20#include "defs.h"
21#include "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"
5e81259d 28#include "demangle.h"
a8a69e63
FF
29
30int vtblprint; /* Controls printing of vtbl's */
31int objectprint; /* Controls looking up an object's derived type
32 using what we find in its vtables. */
33struct obstack dont_print_obstack;
34
35static void
36cplus_print_value PARAMS ((struct type *, char *, FILE *, int, int,
37 enum val_prettyprint, struct type **));
38
39/* BEGIN-FIXME: Hooks into typeprint.c, find a better home for prototypes. */
40
41extern void
42c_type_print_base PARAMS ((struct type *, FILE *, int, int));
43
44extern void
45c_type_print_varspec_prefix PARAMS ((struct type *, FILE *, int, int));
46
47extern void
48cp_type_print_method_args PARAMS ((struct type **, char *, char *, int,
49 FILE *));
50
51extern struct obstack dont_print_obstack;
52
53/* END-FIXME */
54
55
56/* BEGIN-FIXME: Hooks into c-valprint.c */
57
58extern int
59c_val_print PARAMS ((struct type *, char *, CORE_ADDR, FILE *, int, int, int,
60 enum val_prettyprint));
61/* END-FIXME */
62
63
c7da3ed3
FF
64void
65cp_print_class_method (valaddr, type, stream)
66 char *valaddr;
67 struct type *type;
68 FILE *stream;
69{
70 struct type *domain;
71 struct fn_field *f;
72 int j;
73 int len2;
74 int offset;
75 char *kind = "";
76 CORE_ADDR addr;
77 struct symbol *sym;
78 unsigned len;
79 unsigned int i;
80
81 domain = TYPE_DOMAIN_TYPE (TYPE_TARGET_TYPE (type));
82 addr = unpack_pointer (lookup_pointer_type (builtin_type_void), valaddr);
83 if (METHOD_PTR_IS_VIRTUAL (addr))
84 {
85 offset = METHOD_PTR_TO_VOFFSET (addr);
86 len = TYPE_NFN_FIELDS (domain);
87 for (i = 0; i < len; i++)
88 {
89 f = TYPE_FN_FIELDLIST1 (domain, i);
90 len2 = TYPE_FN_FIELDLIST_LENGTH (domain, i);
91
92 for (j = 0; j < len2; j++)
93 {
94 QUIT;
95 if (TYPE_FN_FIELD_VOFFSET (f, j) == offset)
96 {
97 kind = "virtual ";
98 goto common;
99 }
100 }
101 }
102 }
103 else
104 {
105 sym = find_pc_function (addr);
106 if (sym == 0)
107 {
108 error ("invalid pointer to member function");
109 }
110 len = TYPE_NFN_FIELDS (domain);
111 for (i = 0; i < len; i++)
112 {
113 f = TYPE_FN_FIELDLIST1 (domain, i);
114 len2 = TYPE_FN_FIELDLIST_LENGTH (domain, i);
115
116 for (j = 0; j < len2; j++)
117 {
118 QUIT;
119 if (STREQ (SYMBOL_NAME (sym), TYPE_FN_FIELD_PHYSNAME (f, j)))
120 {
121 goto common;
122 }
123 }
124 }
125 }
126 common:
127 if (i < len)
128 {
129 fprintf_filtered (stream, "&");
130 c_type_print_varspec_prefix (TYPE_FN_FIELD_TYPE (f, j), stream, 0, 0);
131 fprintf (stream, kind);
132 if (TYPE_FN_FIELD_PHYSNAME (f, j)[0] == '_'
133 && TYPE_FN_FIELD_PHYSNAME (f, j)[1] == CPLUS_MARKER)
134 {
135 cp_type_print_method_args (TYPE_FN_FIELD_ARGS (f, j) + 1, "~",
136 TYPE_FN_FIELDLIST_NAME (domain, i),
137 0, stream);
138 }
139 else
140 {
141 cp_type_print_method_args (TYPE_FN_FIELD_ARGS (f, j), "",
142 TYPE_FN_FIELDLIST_NAME (domain, i),
143 0, stream);
144 }
145 }
146 else
147 {
148 fprintf_filtered (stream, "(");
149 type_print (type, "", stream, -1);
150 fprintf_filtered (stream, ") %d", (int) addr >> 3);
151 }
152}
153
a8a69e63
FF
154/* Return truth value for assertion that TYPE is of the type
155 "pointer to virtual function". */
156
157int
158cp_is_vtbl_ptr_type(type)
159 struct type *type;
160{
161 char *typename = type_name_no_tag (type);
162 static const char vtbl_ptr_name[] =
163 { CPLUS_MARKER,'v','t','b','l','_','p','t','r','_','t','y','p','e', 0 };
164
2e4964ad 165 return (typename != NULL && STREQ(typename, vtbl_ptr_name));
a8a69e63
FF
166}
167
168/* Return truth value for the assertion that TYPE is of the type
169 "pointer to virtual function table". */
170
171int
172cp_is_vtbl_member(type)
173 struct type *type;
174{
175 if (TYPE_CODE (type) == TYPE_CODE_PTR)
176 type = TYPE_TARGET_TYPE (type);
177 else
178 return 0;
179
180 if (TYPE_CODE (type) == TYPE_CODE_ARRAY
181 && TYPE_CODE (TYPE_TARGET_TYPE (type)) == TYPE_CODE_STRUCT)
182 /* Virtual functions tables are full of pointers to virtual functions. */
183 return cp_is_vtbl_ptr_type (TYPE_TARGET_TYPE (type));
184 return 0;
185}
186
187/* Mutually recursive subroutines of cplus_print_value and c_val_print to
188 print out a structure's fields: cp_print_value_fields and cplus_print_value.
189
190 TYPE, VALADDR, STREAM, RECURSE, and PRETTY have the
191 same meanings as in cplus_print_value and c_val_print.
192
193 DONT_PRINT is an array of baseclass types that we
194 should not print, or zero if called from top level. */
195
196void
197cp_print_value_fields (type, valaddr, stream, format, recurse, pretty,
198 dont_print)
199 struct type *type;
200 char *valaddr;
201 FILE *stream;
202 int format;
203 int recurse;
204 enum val_prettyprint pretty;
205 struct type **dont_print;
206{
207 int i, len, n_baseclasses;
208
209 check_stub_type (type);
210
211 fprintf_filtered (stream, "{");
212 len = TYPE_NFIELDS (type);
213 n_baseclasses = TYPE_N_BASECLASSES (type);
214
215 /* Print out baseclasses such that we don't print
216 duplicates of virtual baseclasses. */
217 if (n_baseclasses > 0)
218 cplus_print_value (type, valaddr, stream, format, recurse+1, pretty,
219 dont_print);
220
221 if (!len && n_baseclasses == 1)
222 fprintf_filtered (stream, "<No data fields>");
223 else
224 {
225 extern int inspect_it;
226 int fields_seen = 0;
227
228 for (i = n_baseclasses; i < len; i++)
229 {
230 /* Check if static field */
231 if (TYPE_FIELD_STATIC (type, i))
232 continue;
233 if (fields_seen)
234 fprintf_filtered (stream, ", ");
235 else if (n_baseclasses > 0)
236 {
237 if (pretty)
238 {
239 fprintf_filtered (stream, "\n");
240 print_spaces_filtered (2 + 2 * recurse, stream);
241 fputs_filtered ("members of ", stream);
242 fputs_filtered (type_name_no_tag (type), stream);
243 fputs_filtered (": ", stream);
244 }
245 }
246 fields_seen = 1;
247
248 if (pretty)
249 {
250 fprintf_filtered (stream, "\n");
251 print_spaces_filtered (2 + 2 * recurse, stream);
252 }
253 else
254 {
255 wrap_here (n_spaces (2 + 2 * recurse));
256 }
257 if (inspect_it)
258 {
259 if (TYPE_CODE (TYPE_FIELD_TYPE (type, i)) == TYPE_CODE_PTR)
260 fputs_filtered ("\"( ptr \"", stream);
261 else
262 fputs_filtered ("\"( nodef \"", stream);
5e81259d
FF
263 fprintf_symbol_filtered (stream, TYPE_FIELD_NAME (type, i),
264 language_cplus,
265 DMGL_PARAMS | DMGL_ANSI);
a8a69e63 266 fputs_filtered ("\" \"", stream);
5e81259d
FF
267 fprintf_symbol_filtered (stream, TYPE_FIELD_NAME (type, i),
268 language_cplus,
269 DMGL_PARAMS | DMGL_ANSI);
a8a69e63
FF
270 fputs_filtered ("\") \"", stream);
271 }
272 else
273 {
5e81259d
FF
274 fprintf_symbol_filtered (stream, TYPE_FIELD_NAME (type, i),
275 language_cplus,
276 DMGL_PARAMS | DMGL_ANSI);
a8a69e63
FF
277 fputs_filtered (" = ", stream);
278 }
279 if (TYPE_FIELD_PACKED (type, i))
280 {
281 value v;
282
283 /* Bitfields require special handling, especially due to byte
284 order problems. */
285 v = value_from_longest (TYPE_FIELD_TYPE (type, i),
286 unpack_field_as_long (type, valaddr, i));
287
288 c_val_print (TYPE_FIELD_TYPE (type, i), VALUE_CONTENTS (v), 0,
289 stream, format, 0, recurse + 1, pretty);
290 }
291 else
292 {
293 c_val_print (TYPE_FIELD_TYPE (type, i),
294 valaddr + TYPE_FIELD_BITPOS (type, i) / 8,
295 0, stream, format, 0, recurse + 1, pretty);
296 }
297 }
298 if (pretty)
299 {
300 fprintf_filtered (stream, "\n");
301 print_spaces_filtered (2 * recurse, stream);
302 }
303 }
304 fprintf_filtered (stream, "}");
305}
306
307/* Special val_print routine to avoid printing multiple copies of virtual
308 baseclasses. */
309
310static void
311cplus_print_value (type, valaddr, stream, format, recurse, pretty, dont_print)
312 struct type *type;
313 char *valaddr;
314 FILE *stream;
315 int format;
316 int recurse;
317 enum val_prettyprint pretty;
318 struct type **dont_print;
319{
320 struct obstack tmp_obstack;
321 struct type **last_dont_print
322 = (struct type **)obstack_next_free (&dont_print_obstack);
323 int i, n_baseclasses = TYPE_N_BASECLASSES (type);
324
325 if (dont_print == 0)
326 {
327 /* If we're at top level, carve out a completely fresh
328 chunk of the obstack and use that until this particular
329 invocation returns. */
330 tmp_obstack = dont_print_obstack;
331 /* Bump up the high-water mark. Now alpha is omega. */
332 obstack_finish (&dont_print_obstack);
333 }
334
335 for (i = 0; i < n_baseclasses; i++)
336 {
337 char *baddr;
338 int err;
339
340 if (BASETYPE_VIA_VIRTUAL (type, i))
341 {
342 struct type **first_dont_print
343 = (struct type **)obstack_base (&dont_print_obstack);
344
345 int j = (struct type **)obstack_next_free (&dont_print_obstack)
346 - first_dont_print;
347
348 while (--j >= 0)
349 if (TYPE_BASECLASS (type, i) == first_dont_print[j])
350 goto flush_it;
351
352 obstack_ptr_grow (&dont_print_obstack, TYPE_BASECLASS (type, i));
353 }
354
355 /* Fix to use baseclass_offset instead. FIXME */
356 baddr = baseclass_addr (type, i, valaddr, 0, &err);
357 if (err == 0 && baddr == 0)
358 error ("could not find virtual baseclass `%s'\n",
359 type_name_no_tag (TYPE_BASECLASS (type, i)));
360
361 if (pretty)
362 {
363 fprintf_filtered (stream, "\n");
364 print_spaces_filtered (2 * recurse, stream);
365 }
366 fputs_filtered ("<", stream);
367 fputs_filtered (type_name_no_tag (TYPE_BASECLASS (type, i)), stream);
368 fputs_filtered ("> = ", stream);
369 if (err != 0)
370 fprintf_filtered (stream, "<invalid address 0x%x>", baddr);
371 else
372 cp_print_value_fields (TYPE_BASECLASS (type, i), baddr, stream, format,
373 recurse, pretty,
374 (struct type **) obstack_base (&dont_print_obstack));
375 fputs_filtered (", ", stream);
376
377 flush_it:
378 ;
379 }
380
381 if (dont_print == 0)
382 {
383 /* Free the space used to deal with the printing
384 of this type from top level. */
385 obstack_free (&dont_print_obstack, last_dont_print);
386 /* Reset watermark so that we can continue protecting
387 ourselves from whatever we were protecting ourselves. */
388 dont_print_obstack = tmp_obstack;
389 }
390}
391
392void
393cp_print_class_member (valaddr, domain, stream, prefix)
394 char *valaddr;
395 struct type *domain;
396 FILE *stream;
397 char *prefix;
398{
399
400 /* VAL is a byte offset into the structure type DOMAIN.
401 Find the name of the field for that offset and
402 print it. */
403 int extra = 0;
404 int bits = 0;
405 register unsigned int i;
406 unsigned len = TYPE_NFIELDS (domain);
407 /* @@ Make VAL into bit offset */
408 LONGEST val = unpack_long (builtin_type_int, valaddr) << 3;
409 for (i = TYPE_N_BASECLASSES (domain); i < len; i++)
410 {
411 int bitpos = TYPE_FIELD_BITPOS (domain, i);
412 QUIT;
413 if (val == bitpos)
414 break;
415 if (val < bitpos && i != 0)
416 {
417 /* Somehow pointing into a field. */
418 i -= 1;
419 extra = (val - TYPE_FIELD_BITPOS (domain, i));
420 if (extra & 0x7)
421 bits = 1;
422 else
423 extra >>= 3;
424 break;
425 }
426 }
427 if (i < len)
428 {
429 char *name;
430 fprintf_filtered (stream, prefix);
431 name = type_name_no_tag (domain);
432 if (name)
433 fputs_filtered (name, stream);
434 else
435 c_type_print_base (domain, stream, 0, 0);
436 fprintf_filtered (stream, "::");
437 fputs_filtered (TYPE_FIELD_NAME (domain, i), stream);
438 if (extra)
439 fprintf_filtered (stream, " + %d bytes", extra);
440 if (bits)
441 fprintf_filtered (stream, " (offset in bits)");
442 }
443 else
444 fprintf_filtered (stream, "%d", val >> 3);
445}
446
447void
448_initialize_cp_valprint ()
449{
450 add_show_from_set
451 (add_set_cmd ("vtbl", class_support, var_boolean, (char *)&vtblprint,
452 "Set printing of C++ virtual function tables.",
453 &setprintlist),
454 &showprintlist);
455
456 add_show_from_set
457 (add_set_cmd ("object", class_support, var_boolean, (char *)&objectprint,
458 "Set printing of object's derived type based on vtable info.",
459 &setprintlist),
460 &showprintlist);
461
462 /* Give people the defaults which they are used to. */
463 objectprint = 0;
464 vtblprint = 0;
465 obstack_begin (&dont_print_obstack, 32 * sizeof (struct type *));
466}
This page took 0.050012 seconds and 4 git commands to generate.