* printcmd.c (print_command_1): Annotate the top-level expressions
[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
199b2450 36cplus_print_value PARAMS ((struct type *, char *, GDB_FILE *, int, int,
a8a69e63
FF
37 enum val_prettyprint, struct type **));
38
39/* BEGIN-FIXME: Hooks into typeprint.c, find a better home for prototypes. */
40
41extern void
199b2450 42c_type_print_base PARAMS ((struct type *, GDB_FILE *, int, int));
a8a69e63
FF
43
44extern void
199b2450 45c_type_print_varspec_prefix PARAMS ((struct type *, GDB_FILE *, int, int));
a8a69e63
FF
46
47extern void
48cp_type_print_method_args PARAMS ((struct type **, char *, char *, int,
199b2450 49 GDB_FILE *));
a8a69e63
FF
50
51extern struct obstack dont_print_obstack;
52
53/* END-FIXME */
54
c7da3ed3
FF
55void
56cp_print_class_method (valaddr, type, stream)
57 char *valaddr;
58 struct type *type;
199b2450 59 GDB_FILE *stream;
c7da3ed3
FF
60{
61 struct type *domain;
72cd0384
RP
62 struct fn_field *f = NULL;
63 int j = 0;
c7da3ed3
FF
64 int len2;
65 int offset;
66 char *kind = "";
67 CORE_ADDR addr;
68 struct symbol *sym;
69 unsigned len;
70 unsigned int i;
71
30d20d15 72 check_stub_type (TYPE_TARGET_TYPE (type));
c7da3ed3 73 domain = TYPE_DOMAIN_TYPE (TYPE_TARGET_TYPE (type));
30d20d15
PS
74 if (domain == (struct type *)NULL)
75 {
76 fprintf_filtered (stream, "<unknown>");
77 return;
78 }
c7da3ed3
FF
79 addr = unpack_pointer (lookup_pointer_type (builtin_type_void), valaddr);
80 if (METHOD_PTR_IS_VIRTUAL (addr))
81 {
82 offset = METHOD_PTR_TO_VOFFSET (addr);
83 len = TYPE_NFN_FIELDS (domain);
84 for (i = 0; i < len; i++)
85 {
86 f = TYPE_FN_FIELDLIST1 (domain, i);
87 len2 = TYPE_FN_FIELDLIST_LENGTH (domain, i);
88
89 for (j = 0; j < len2; j++)
90 {
91 QUIT;
92 if (TYPE_FN_FIELD_VOFFSET (f, j) == offset)
93 {
94 kind = "virtual ";
95 goto common;
96 }
97 }
98 }
99 }
100 else
101 {
102 sym = find_pc_function (addr);
103 if (sym == 0)
104 {
105 error ("invalid pointer to member function");
106 }
107 len = TYPE_NFN_FIELDS (domain);
108 for (i = 0; i < len; i++)
109 {
110 f = TYPE_FN_FIELDLIST1 (domain, i);
111 len2 = TYPE_FN_FIELDLIST_LENGTH (domain, i);
112
113 for (j = 0; j < len2; j++)
114 {
115 QUIT;
39cb3d04
PS
116 if (TYPE_FN_FIELD_STUB (f, j))
117 check_stub_method (domain, i, j);
c7da3ed3
FF
118 if (STREQ (SYMBOL_NAME (sym), TYPE_FN_FIELD_PHYSNAME (f, j)))
119 {
120 goto common;
121 }
122 }
123 }
124 }
125 common:
126 if (i < len)
127 {
128 fprintf_filtered (stream, "&");
129 c_type_print_varspec_prefix (TYPE_FN_FIELD_TYPE (f, j), stream, 0, 0);
199b2450 130 fprintf_unfiltered (stream, kind);
c7da3ed3
FF
131 if (TYPE_FN_FIELD_PHYSNAME (f, j)[0] == '_'
132 && TYPE_FN_FIELD_PHYSNAME (f, j)[1] == CPLUS_MARKER)
133 {
134 cp_type_print_method_args (TYPE_FN_FIELD_ARGS (f, j) + 1, "~",
135 TYPE_FN_FIELDLIST_NAME (domain, i),
136 0, stream);
137 }
138 else
139 {
140 cp_type_print_method_args (TYPE_FN_FIELD_ARGS (f, j), "",
141 TYPE_FN_FIELDLIST_NAME (domain, i),
142 0, stream);
143 }
144 }
145 else
146 {
147 fprintf_filtered (stream, "(");
148 type_print (type, "", stream, -1);
149 fprintf_filtered (stream, ") %d", (int) addr >> 3);
150 }
151}
152
a8a69e63
FF
153/* Return truth value for assertion that TYPE is of the type
154 "pointer to virtual function". */
155
156int
157cp_is_vtbl_ptr_type(type)
158 struct type *type;
159{
160 char *typename = type_name_no_tag (type);
8f341c15
JK
161 /* This was what it was for gcc 2.4.5 and earlier. */
162 static const char vtbl_ptr_name_old[] =
a8a69e63 163 { CPLUS_MARKER,'v','t','b','l','_','p','t','r','_','t','y','p','e', 0 };
8f341c15 164 /* It was changed to this after 2.4.5. */
b0dd16a5
KH
165 static const char vtbl_ptr_name[] =
166 { '_','_','v','t','b','l','_','p','t','r','_','t','y','p','e', 0 };
a8a69e63 167
8f341c15
JK
168 return (typename != NULL
169 && (STREQ (typename, vtbl_ptr_name)
170 || STREQ (typename, vtbl_ptr_name_old)));
a8a69e63
FF
171}
172
173/* Return truth value for the assertion that TYPE is of the type
174 "pointer to virtual function table". */
175
176int
177cp_is_vtbl_member(type)
178 struct type *type;
179{
180 if (TYPE_CODE (type) == TYPE_CODE_PTR)
181 type = TYPE_TARGET_TYPE (type);
182 else
183 return 0;
184
185 if (TYPE_CODE (type) == TYPE_CODE_ARRAY
186 && TYPE_CODE (TYPE_TARGET_TYPE (type)) == TYPE_CODE_STRUCT)
187 /* Virtual functions tables are full of pointers to virtual functions. */
188 return cp_is_vtbl_ptr_type (TYPE_TARGET_TYPE (type));
189 return 0;
190}
191
192/* Mutually recursive subroutines of cplus_print_value and c_val_print to
193 print out a structure's fields: cp_print_value_fields and cplus_print_value.
194
195 TYPE, VALADDR, STREAM, RECURSE, and PRETTY have the
196 same meanings as in cplus_print_value and c_val_print.
197
198 DONT_PRINT is an array of baseclass types that we
199 should not print, or zero if called from top level. */
200
201void
202cp_print_value_fields (type, valaddr, stream, format, recurse, pretty,
203 dont_print)
204 struct type *type;
205 char *valaddr;
199b2450 206 GDB_FILE *stream;
a8a69e63
FF
207 int format;
208 int recurse;
209 enum val_prettyprint pretty;
210 struct type **dont_print;
211{
212 int i, len, n_baseclasses;
213
214 check_stub_type (type);
215
216 fprintf_filtered (stream, "{");
217 len = TYPE_NFIELDS (type);
218 n_baseclasses = TYPE_N_BASECLASSES (type);
219
220 /* Print out baseclasses such that we don't print
221 duplicates of virtual baseclasses. */
222 if (n_baseclasses > 0)
223 cplus_print_value (type, valaddr, stream, format, recurse+1, pretty,
224 dont_print);
225
226 if (!len && n_baseclasses == 1)
227 fprintf_filtered (stream, "<No data fields>");
228 else
229 {
230 extern int inspect_it;
231 int fields_seen = 0;
232
233 for (i = n_baseclasses; i < len; i++)
234 {
235 /* Check if static field */
236 if (TYPE_FIELD_STATIC (type, i))
237 continue;
238 if (fields_seen)
239 fprintf_filtered (stream, ", ");
240 else if (n_baseclasses > 0)
241 {
242 if (pretty)
243 {
244 fprintf_filtered (stream, "\n");
245 print_spaces_filtered (2 + 2 * recurse, stream);
246 fputs_filtered ("members of ", stream);
247 fputs_filtered (type_name_no_tag (type), stream);
248 fputs_filtered (": ", stream);
249 }
250 }
251 fields_seen = 1;
252
253 if (pretty)
254 {
255 fprintf_filtered (stream, "\n");
256 print_spaces_filtered (2 + 2 * recurse, stream);
257 }
258 else
259 {
260 wrap_here (n_spaces (2 + 2 * recurse));
261 }
262 if (inspect_it)
263 {
264 if (TYPE_CODE (TYPE_FIELD_TYPE (type, i)) == TYPE_CODE_PTR)
265 fputs_filtered ("\"( ptr \"", stream);
266 else
267 fputs_filtered ("\"( nodef \"", stream);
5e81259d
FF
268 fprintf_symbol_filtered (stream, TYPE_FIELD_NAME (type, i),
269 language_cplus,
270 DMGL_PARAMS | DMGL_ANSI);
a8a69e63 271 fputs_filtered ("\" \"", stream);
5e81259d
FF
272 fprintf_symbol_filtered (stream, TYPE_FIELD_NAME (type, i),
273 language_cplus,
274 DMGL_PARAMS | DMGL_ANSI);
a8a69e63
FF
275 fputs_filtered ("\") \"", stream);
276 }
277 else
278 {
96f7edbd
JK
279 if (annotation_level > 1)
280 printf_filtered ("\n\032\032field-name-begin\n");
5e81259d
FF
281 fprintf_symbol_filtered (stream, TYPE_FIELD_NAME (type, i),
282 language_cplus,
283 DMGL_PARAMS | DMGL_ANSI);
96f7edbd
JK
284 if (annotation_level > 1)
285 printf_filtered ("\n\032\032field-name-end\n");
a8a69e63
FF
286 fputs_filtered (" = ", stream);
287 }
96f7edbd
JK
288
289 if (annotation_level > 1)
290 {
291 printf_filtered ("\n\032\032field-begin ");
292 print_value_flags (TYPE_FIELD_TYPE (type, i));
293 printf_filtered ("\n");
294 }
295
a8a69e63
FF
296 if (TYPE_FIELD_PACKED (type, i))
297 {
82a2edfb 298 value_ptr v;
a8a69e63
FF
299
300 /* Bitfields require special handling, especially due to byte
301 order problems. */
024f65b1
KH
302 if (TYPE_FIELD_IGNORE (type, i))
303 {
aa074e84 304 fputs_filtered ("<optimized out or zero length>", stream);
024f65b1
KH
305 }
306 else
307 {
308 v = value_from_longest (TYPE_FIELD_TYPE (type, i),
a8a69e63
FF
309 unpack_field_as_long (type, valaddr, i));
310
dda398c3
JK
311 val_print (TYPE_FIELD_TYPE(type, i), VALUE_CONTENTS (v), 0,
312 stream, format, 0, recurse + 1, pretty);
024f65b1 313 }
a8a69e63
FF
314 }
315 else
316 {
024f65b1
KH
317 if (TYPE_FIELD_IGNORE (type, i))
318 {
aa074e84 319 fputs_filtered ("<optimized out or zero length>", stream);
024f65b1
KH
320 }
321 else
322 {
dda398c3
JK
323 val_print (TYPE_FIELD_TYPE (type, i),
324 valaddr + TYPE_FIELD_BITPOS (type, i) / 8,
325 0, stream, format, 0, recurse + 1, pretty);
024f65b1 326 }
a8a69e63 327 }
96f7edbd
JK
328 if (annotation_level > 1)
329 printf_filtered ("\n\032\032field-end\n");
a8a69e63 330 }
96f7edbd 331
a8a69e63
FF
332 if (pretty)
333 {
334 fprintf_filtered (stream, "\n");
335 print_spaces_filtered (2 * recurse, stream);
336 }
337 }
338 fprintf_filtered (stream, "}");
339}
340
341/* Special val_print routine to avoid printing multiple copies of virtual
342 baseclasses. */
343
344static void
345cplus_print_value (type, valaddr, stream, format, recurse, pretty, dont_print)
346 struct type *type;
347 char *valaddr;
199b2450 348 GDB_FILE *stream;
a8a69e63
FF
349 int format;
350 int recurse;
351 enum val_prettyprint pretty;
352 struct type **dont_print;
353{
354 struct obstack tmp_obstack;
355 struct type **last_dont_print
356 = (struct type **)obstack_next_free (&dont_print_obstack);
357 int i, n_baseclasses = TYPE_N_BASECLASSES (type);
358
359 if (dont_print == 0)
360 {
361 /* If we're at top level, carve out a completely fresh
362 chunk of the obstack and use that until this particular
363 invocation returns. */
364 tmp_obstack = dont_print_obstack;
365 /* Bump up the high-water mark. Now alpha is omega. */
366 obstack_finish (&dont_print_obstack);
367 }
368
369 for (i = 0; i < n_baseclasses; i++)
370 {
5e678752
JK
371 /* FIXME-32x64--assumes that a target pointer can fit in a char *.
372 Fix it by nuking baseclass_addr. */
a8a69e63
FF
373 char *baddr;
374 int err;
2d2fc7e4
JK
375 char *basename;
376
377 check_stub_type (TYPE_BASECLASS (type, i));
378 basename = TYPE_NAME (TYPE_BASECLASS (type, i));
a8a69e63
FF
379
380 if (BASETYPE_VIA_VIRTUAL (type, i))
381 {
382 struct type **first_dont_print
383 = (struct type **)obstack_base (&dont_print_obstack);
384
385 int j = (struct type **)obstack_next_free (&dont_print_obstack)
386 - first_dont_print;
387
388 while (--j >= 0)
389 if (TYPE_BASECLASS (type, i) == first_dont_print[j])
390 goto flush_it;
391
392 obstack_ptr_grow (&dont_print_obstack, TYPE_BASECLASS (type, i));
393 }
394
395 /* Fix to use baseclass_offset instead. FIXME */
396 baddr = baseclass_addr (type, i, valaddr, 0, &err);
397 if (err == 0 && baddr == 0)
1410f5f1
JK
398 error ("could not find virtual baseclass %s\n",
399 basename ? basename : "");
a8a69e63
FF
400
401 if (pretty)
402 {
403 fprintf_filtered (stream, "\n");
404 print_spaces_filtered (2 * recurse, stream);
405 }
406 fputs_filtered ("<", stream);
1410f5f1
JK
407 /* Not sure what the best notation is in the case where there is no
408 baseclass name. */
409 fputs_filtered (basename ? basename : "", stream);
a8a69e63
FF
410 fputs_filtered ("> = ", stream);
411 if (err != 0)
833e0d94
JK
412 {
413 fprintf_filtered (stream, "<invalid address ");
5e678752 414 print_address_numeric ((CORE_ADDR) baddr, stream);
833e0d94
JK
415 fprintf_filtered (stream, ">");
416 }
a8a69e63
FF
417 else
418 cp_print_value_fields (TYPE_BASECLASS (type, i), baddr, stream, format,
419 recurse, pretty,
420 (struct type **) obstack_base (&dont_print_obstack));
421 fputs_filtered (", ", stream);
422
423 flush_it:
424 ;
425 }
426
427 if (dont_print == 0)
428 {
429 /* Free the space used to deal with the printing
430 of this type from top level. */
431 obstack_free (&dont_print_obstack, last_dont_print);
432 /* Reset watermark so that we can continue protecting
433 ourselves from whatever we were protecting ourselves. */
434 dont_print_obstack = tmp_obstack;
435 }
436}
437
438void
439cp_print_class_member (valaddr, domain, stream, prefix)
440 char *valaddr;
441 struct type *domain;
199b2450 442 GDB_FILE *stream;
a8a69e63
FF
443 char *prefix;
444{
445
446 /* VAL is a byte offset into the structure type DOMAIN.
447 Find the name of the field for that offset and
448 print it. */
449 int extra = 0;
450 int bits = 0;
451 register unsigned int i;
452 unsigned len = TYPE_NFIELDS (domain);
453 /* @@ Make VAL into bit offset */
454 LONGEST val = unpack_long (builtin_type_int, valaddr) << 3;
455 for (i = TYPE_N_BASECLASSES (domain); i < len; i++)
456 {
457 int bitpos = TYPE_FIELD_BITPOS (domain, i);
458 QUIT;
459 if (val == bitpos)
460 break;
461 if (val < bitpos && i != 0)
462 {
463 /* Somehow pointing into a field. */
464 i -= 1;
465 extra = (val - TYPE_FIELD_BITPOS (domain, i));
466 if (extra & 0x7)
467 bits = 1;
468 else
469 extra >>= 3;
470 break;
471 }
472 }
473 if (i < len)
474 {
475 char *name;
476 fprintf_filtered (stream, prefix);
477 name = type_name_no_tag (domain);
478 if (name)
479 fputs_filtered (name, stream);
480 else
481 c_type_print_base (domain, stream, 0, 0);
482 fprintf_filtered (stream, "::");
483 fputs_filtered (TYPE_FIELD_NAME (domain, i), stream);
484 if (extra)
485 fprintf_filtered (stream, " + %d bytes", extra);
486 if (bits)
487 fprintf_filtered (stream, " (offset in bits)");
488 }
489 else
490 fprintf_filtered (stream, "%d", val >> 3);
491}
492
493void
494_initialize_cp_valprint ()
495{
496 add_show_from_set
497 (add_set_cmd ("vtbl", class_support, var_boolean, (char *)&vtblprint,
498 "Set printing of C++ virtual function tables.",
499 &setprintlist),
500 &showprintlist);
501
502 add_show_from_set
503 (add_set_cmd ("object", class_support, var_boolean, (char *)&objectprint,
504 "Set printing of object's derived type based on vtable info.",
505 &setprintlist),
506 &showprintlist);
507
508 /* Give people the defaults which they are used to. */
509 objectprint = 0;
510 vtblprint = 0;
511 obstack_begin (&dont_print_obstack, 32 * sizeof (struct type *));
512}
This page took 0.111381 seconds and 4 git commands to generate.