* parse.c (write_dollar_variable): New function.
[deliverable/binutils-gdb.git] / gdb / cp-valprint.c
CommitLineData
a8a69e63 1/* Support for printing C++ values for GDB, the GNU debugger.
a1a0d974
PS
2 Copyright 1986, 1988, 1989, 1991, 1994, 1995
3 Free Software Foundation, Inc.
a8a69e63
FF
4
5This file is part of GDB.
6
7This program is free software; you can redistribute it and/or modify
8it under the terms of the GNU General Public License as published by
9the Free Software Foundation; either version 2 of the License, or
10(at your option) any later version.
11
12This program is distributed in the hope that it will be useful,
13but WITHOUT ANY WARRANTY; without even the implied warranty of
14MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15GNU General Public License for more details.
16
17You should have received a copy of the GNU General Public License
18along with this program; if not, write to the Free Software
6c9638b4 19Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
a8a69e63
FF
20
21#include "defs.h"
22#include "obstack.h"
23#include "symtab.h"
24#include "gdbtypes.h"
25#include "expression.h"
26#include "value.h"
27#include "command.h"
28#include "gdbcmd.h"
5e81259d 29#include "demangle.h"
1c95d7ab 30#include "annotate.h"
2b576293 31#include "gdb_string.h"
a8a69e63
FF
32
33int vtblprint; /* Controls printing of vtbl's */
34int objectprint; /* Controls looking up an object's derived type
35 using what we find in its vtables. */
4c664b8d 36static int static_field_print; /* Controls printing of static fields. */
a1a0d974
PS
37
38static struct obstack dont_print_vb_obstack;
39static struct obstack dont_print_statmem_obstack;
40
41static void
42cp_print_static_field PARAMS ((struct type *, value_ptr, GDB_FILE *, int, int,
43 enum val_prettyprint));
a8a69e63
FF
44
45static void
199b2450 46cplus_print_value PARAMS ((struct type *, char *, GDB_FILE *, int, int,
a8a69e63
FF
47 enum val_prettyprint, struct type **));
48
49/* BEGIN-FIXME: Hooks into typeprint.c, find a better home for prototypes. */
50
51extern void
199b2450 52c_type_print_base PARAMS ((struct type *, GDB_FILE *, int, int));
a8a69e63
FF
53
54extern void
199b2450 55c_type_print_varspec_prefix PARAMS ((struct type *, GDB_FILE *, int, int));
a8a69e63
FF
56
57extern void
58cp_type_print_method_args PARAMS ((struct type **, char *, char *, int,
199b2450 59 GDB_FILE *));
a8a69e63 60
a8a69e63
FF
61/* END-FIXME */
62
c7da3ed3
FF
63void
64cp_print_class_method (valaddr, type, stream)
65 char *valaddr;
66 struct type *type;
199b2450 67 GDB_FILE *stream;
c7da3ed3
FF
68{
69 struct type *domain;
72cd0384
RP
70 struct fn_field *f = NULL;
71 int j = 0;
c7da3ed3
FF
72 int len2;
73 int offset;
74 char *kind = "";
75 CORE_ADDR addr;
76 struct symbol *sym;
77 unsigned len;
78 unsigned int i;
79
30d20d15 80 check_stub_type (TYPE_TARGET_TYPE (type));
c7da3ed3 81 domain = TYPE_DOMAIN_TYPE (TYPE_TARGET_TYPE (type));
30d20d15
PS
82 if (domain == (struct type *)NULL)
83 {
84 fprintf_filtered (stream, "<unknown>");
85 return;
86 }
c7da3ed3
FF
87 addr = unpack_pointer (lookup_pointer_type (builtin_type_void), valaddr);
88 if (METHOD_PTR_IS_VIRTUAL (addr))
89 {
90 offset = METHOD_PTR_TO_VOFFSET (addr);
91 len = TYPE_NFN_FIELDS (domain);
92 for (i = 0; i < len; i++)
93 {
94 f = TYPE_FN_FIELDLIST1 (domain, i);
95 len2 = TYPE_FN_FIELDLIST_LENGTH (domain, i);
96
97 for (j = 0; j < len2; j++)
98 {
99 QUIT;
100 if (TYPE_FN_FIELD_VOFFSET (f, j) == offset)
101 {
102 kind = "virtual ";
103 goto common;
104 }
105 }
106 }
107 }
108 else
109 {
110 sym = find_pc_function (addr);
111 if (sym == 0)
112 {
113 error ("invalid pointer to member function");
114 }
115 len = TYPE_NFN_FIELDS (domain);
116 for (i = 0; i < len; i++)
117 {
118 f = TYPE_FN_FIELDLIST1 (domain, i);
119 len2 = TYPE_FN_FIELDLIST_LENGTH (domain, i);
120
121 for (j = 0; j < len2; j++)
122 {
123 QUIT;
39cb3d04
PS
124 if (TYPE_FN_FIELD_STUB (f, j))
125 check_stub_method (domain, i, j);
c7da3ed3
FF
126 if (STREQ (SYMBOL_NAME (sym), TYPE_FN_FIELD_PHYSNAME (f, j)))
127 {
128 goto common;
129 }
130 }
131 }
132 }
133 common:
134 if (i < len)
135 {
136 fprintf_filtered (stream, "&");
137 c_type_print_varspec_prefix (TYPE_FN_FIELD_TYPE (f, j), stream, 0, 0);
199b2450 138 fprintf_unfiltered (stream, kind);
c7da3ed3
FF
139 if (TYPE_FN_FIELD_PHYSNAME (f, j)[0] == '_'
140 && TYPE_FN_FIELD_PHYSNAME (f, j)[1] == CPLUS_MARKER)
141 {
142 cp_type_print_method_args (TYPE_FN_FIELD_ARGS (f, j) + 1, "~",
143 TYPE_FN_FIELDLIST_NAME (domain, i),
144 0, stream);
145 }
146 else
147 {
148 cp_type_print_method_args (TYPE_FN_FIELD_ARGS (f, j), "",
149 TYPE_FN_FIELDLIST_NAME (domain, i),
150 0, stream);
151 }
152 }
153 else
154 {
155 fprintf_filtered (stream, "(");
156 type_print (type, "", stream, -1);
157 fprintf_filtered (stream, ") %d", (int) addr >> 3);
158 }
159}
160
36a2283d
PB
161/* This was what it was for gcc 2.4.5 and earlier. */
162static const char vtbl_ptr_name_old[] =
163 { CPLUS_MARKER,'v','t','b','l','_','p','t','r','_','t','y','p','e', 0 };
164/* It was changed to this after 2.4.5. */
165const char vtbl_ptr_name[] =
166 { '_','_','v','t','b','l','_','p','t','r','_','t','y','p','e', 0 };
167
a8a69e63
FF
168/* Return truth value for assertion that TYPE is of the type
169 "pointer to virtual function". */
170
171int
172cp_is_vtbl_ptr_type(type)
173 struct type *type;
174{
175 char *typename = type_name_no_tag (type);
a8a69e63 176
8f341c15
JK
177 return (typename != NULL
178 && (STREQ (typename, vtbl_ptr_name)
179 || STREQ (typename, vtbl_ptr_name_old)));
a8a69e63
FF
180}
181
182/* Return truth value for the assertion that TYPE is of the type
183 "pointer to virtual function table". */
184
185int
186cp_is_vtbl_member(type)
187 struct type *type;
188{
189 if (TYPE_CODE (type) == TYPE_CODE_PTR)
36a2283d
PB
190 {
191 type = TYPE_TARGET_TYPE (type);
192 if (TYPE_CODE (type) == TYPE_CODE_ARRAY)
193 {
194 type = TYPE_TARGET_TYPE (type);
195 if (TYPE_CODE (type) == TYPE_CODE_STRUCT /* if not using thunks */
196 || TYPE_CODE (type) == TYPE_CODE_PTR) /* if using thunks */
197 {
198 /* Virtual functions tables are full of pointers
199 to virtual functions. */
200 return cp_is_vtbl_ptr_type (type);
201 }
202 }
203 }
a8a69e63
FF
204 return 0;
205}
206
207/* Mutually recursive subroutines of cplus_print_value and c_val_print to
208 print out a structure's fields: cp_print_value_fields and cplus_print_value.
209
210 TYPE, VALADDR, STREAM, RECURSE, and PRETTY have the
211 same meanings as in cplus_print_value and c_val_print.
212
213 DONT_PRINT is an array of baseclass types that we
214 should not print, or zero if called from top level. */
215
216void
217cp_print_value_fields (type, valaddr, stream, format, recurse, pretty,
a1a0d974 218 dont_print_vb, dont_print_statmem)
a8a69e63
FF
219 struct type *type;
220 char *valaddr;
199b2450 221 GDB_FILE *stream;
a8a69e63
FF
222 int format;
223 int recurse;
224 enum val_prettyprint pretty;
a1a0d974
PS
225 struct type **dont_print_vb;
226 int dont_print_statmem;
a8a69e63
FF
227{
228 int i, len, n_baseclasses;
a1a0d974
PS
229 struct obstack tmp_obstack;
230 char *last_dont_print = obstack_next_free (&dont_print_statmem_obstack);
a8a69e63
FF
231
232 check_stub_type (type);
233
234 fprintf_filtered (stream, "{");
235 len = TYPE_NFIELDS (type);
236 n_baseclasses = TYPE_N_BASECLASSES (type);
237
238 /* Print out baseclasses such that we don't print
239 duplicates of virtual baseclasses. */
240 if (n_baseclasses > 0)
241 cplus_print_value (type, valaddr, stream, format, recurse+1, pretty,
a1a0d974 242 dont_print_vb);
a8a69e63
FF
243
244 if (!len && n_baseclasses == 1)
245 fprintf_filtered (stream, "<No data fields>");
246 else
247 {
248 extern int inspect_it;
249 int fields_seen = 0;
250
a1a0d974
PS
251 if (dont_print_statmem == 0)
252 {
253 /* If we're at top level, carve out a completely fresh
254 chunk of the obstack and use that until this particular
255 invocation returns. */
256 tmp_obstack = dont_print_statmem_obstack;
257 obstack_finish (&dont_print_statmem_obstack);
258 }
259
a8a69e63
FF
260 for (i = n_baseclasses; i < len; i++)
261 {
4c664b8d
PS
262 /* If requested, skip printing of static fields. */
263 if (!static_field_print && TYPE_FIELD_STATIC (type, i))
a8a69e63
FF
264 continue;
265 if (fields_seen)
266 fprintf_filtered (stream, ", ");
267 else if (n_baseclasses > 0)
268 {
269 if (pretty)
270 {
271 fprintf_filtered (stream, "\n");
272 print_spaces_filtered (2 + 2 * recurse, stream);
273 fputs_filtered ("members of ", stream);
274 fputs_filtered (type_name_no_tag (type), stream);
275 fputs_filtered (": ", stream);
276 }
277 }
278 fields_seen = 1;
279
280 if (pretty)
281 {
282 fprintf_filtered (stream, "\n");
283 print_spaces_filtered (2 + 2 * recurse, stream);
284 }
285 else
286 {
287 wrap_here (n_spaces (2 + 2 * recurse));
288 }
289 if (inspect_it)
290 {
291 if (TYPE_CODE (TYPE_FIELD_TYPE (type, i)) == TYPE_CODE_PTR)
292 fputs_filtered ("\"( ptr \"", stream);
293 else
294 fputs_filtered ("\"( nodef \"", stream);
4c664b8d
PS
295 if (TYPE_FIELD_STATIC (type, i))
296 fputs_filtered ("static ", stream);
5e81259d
FF
297 fprintf_symbol_filtered (stream, TYPE_FIELD_NAME (type, i),
298 language_cplus,
299 DMGL_PARAMS | DMGL_ANSI);
a8a69e63 300 fputs_filtered ("\" \"", stream);
5e81259d
FF
301 fprintf_symbol_filtered (stream, TYPE_FIELD_NAME (type, i),
302 language_cplus,
303 DMGL_PARAMS | DMGL_ANSI);
a8a69e63
FF
304 fputs_filtered ("\") \"", stream);
305 }
306 else
307 {
1c95d7ab 308 annotate_field_begin (TYPE_FIELD_TYPE (type, i));
da988c20 309
4c664b8d
PS
310 if (TYPE_FIELD_STATIC (type, i))
311 fputs_filtered ("static ", stream);
5e81259d
FF
312 fprintf_symbol_filtered (stream, TYPE_FIELD_NAME (type, i),
313 language_cplus,
314 DMGL_PARAMS | DMGL_ANSI);
1c95d7ab 315 annotate_field_name_end ();
a8a69e63 316 fputs_filtered (" = ", stream);
1c95d7ab 317 annotate_field_value ();
96f7edbd
JK
318 }
319
4c664b8d 320 if (!TYPE_FIELD_STATIC (type, i) && TYPE_FIELD_PACKED (type, i))
a8a69e63 321 {
82a2edfb 322 value_ptr v;
a8a69e63
FF
323
324 /* Bitfields require special handling, especially due to byte
325 order problems. */
024f65b1
KH
326 if (TYPE_FIELD_IGNORE (type, i))
327 {
aa074e84 328 fputs_filtered ("<optimized out or zero length>", stream);
024f65b1
KH
329 }
330 else
331 {
332 v = value_from_longest (TYPE_FIELD_TYPE (type, i),
a8a69e63
FF
333 unpack_field_as_long (type, valaddr, i));
334
dda398c3
JK
335 val_print (TYPE_FIELD_TYPE(type, i), VALUE_CONTENTS (v), 0,
336 stream, format, 0, recurse + 1, pretty);
024f65b1 337 }
a8a69e63
FF
338 }
339 else
340 {
024f65b1
KH
341 if (TYPE_FIELD_IGNORE (type, i))
342 {
aa074e84 343 fputs_filtered ("<optimized out or zero length>", stream);
024f65b1 344 }
4c664b8d
PS
345 else if (TYPE_FIELD_STATIC (type, i))
346 {
347 value_ptr v;
348 char *phys_name = TYPE_FIELD_STATIC_PHYSNAME (type, i);
349 struct symbol *sym =
350 lookup_symbol (phys_name, 0, VAR_NAMESPACE, 0, NULL);
351 if (sym == NULL)
352 fputs_filtered ("<optimized out>", stream);
353 else
354 {
355 v = value_at (TYPE_FIELD_TYPE (type, i),
356 (CORE_ADDR)SYMBOL_BLOCK_VALUE (sym));
a1a0d974
PS
357 cp_print_static_field (TYPE_FIELD_TYPE (type, i), v,
358 stream, format, recurse + 1,
359 pretty);
4c664b8d
PS
360 }
361 }
024f65b1
KH
362 else
363 {
dda398c3
JK
364 val_print (TYPE_FIELD_TYPE (type, i),
365 valaddr + TYPE_FIELD_BITPOS (type, i) / 8,
366 0, stream, format, 0, recurse + 1, pretty);
024f65b1 367 }
a8a69e63 368 }
1c95d7ab 369 annotate_field_end ();
a8a69e63 370 }
96f7edbd 371
a1a0d974
PS
372 if (dont_print_statmem == 0)
373 {
374 /* Free the space used to deal with the printing
375 of the members from top level. */
376 obstack_free (&dont_print_statmem_obstack, last_dont_print);
377 dont_print_statmem_obstack = tmp_obstack;
378 }
379
a8a69e63
FF
380 if (pretty)
381 {
382 fprintf_filtered (stream, "\n");
383 print_spaces_filtered (2 * recurse, stream);
384 }
385 }
386 fprintf_filtered (stream, "}");
387}
388
389/* Special val_print routine to avoid printing multiple copies of virtual
390 baseclasses. */
391
392static void
a1a0d974
PS
393cplus_print_value (type, valaddr, stream, format, recurse, pretty,
394 dont_print_vb)
a8a69e63
FF
395 struct type *type;
396 char *valaddr;
199b2450 397 GDB_FILE *stream;
a8a69e63
FF
398 int format;
399 int recurse;
400 enum val_prettyprint pretty;
a1a0d974 401 struct type **dont_print_vb;
a8a69e63
FF
402{
403 struct obstack tmp_obstack;
404 struct type **last_dont_print
a1a0d974 405 = (struct type **)obstack_next_free (&dont_print_vb_obstack);
a8a69e63
FF
406 int i, n_baseclasses = TYPE_N_BASECLASSES (type);
407
a1a0d974 408 if (dont_print_vb == 0)
a8a69e63
FF
409 {
410 /* If we're at top level, carve out a completely fresh
411 chunk of the obstack and use that until this particular
412 invocation returns. */
a1a0d974 413 tmp_obstack = dont_print_vb_obstack;
a8a69e63 414 /* Bump up the high-water mark. Now alpha is omega. */
a1a0d974 415 obstack_finish (&dont_print_vb_obstack);
a8a69e63
FF
416 }
417
418 for (i = 0; i < n_baseclasses; i++)
419 {
5e678752
JK
420 /* FIXME-32x64--assumes that a target pointer can fit in a char *.
421 Fix it by nuking baseclass_addr. */
a8a69e63
FF
422 char *baddr;
423 int err;
2d2fc7e4
JK
424 char *basename;
425
426 check_stub_type (TYPE_BASECLASS (type, i));
427 basename = TYPE_NAME (TYPE_BASECLASS (type, i));
a8a69e63
FF
428
429 if (BASETYPE_VIA_VIRTUAL (type, i))
430 {
431 struct type **first_dont_print
a1a0d974 432 = (struct type **)obstack_base (&dont_print_vb_obstack);
a8a69e63 433
a1a0d974 434 int j = (struct type **)obstack_next_free (&dont_print_vb_obstack)
a8a69e63
FF
435 - first_dont_print;
436
437 while (--j >= 0)
438 if (TYPE_BASECLASS (type, i) == first_dont_print[j])
439 goto flush_it;
440
a1a0d974 441 obstack_ptr_grow (&dont_print_vb_obstack, TYPE_BASECLASS (type, i));
a8a69e63
FF
442 }
443
444 /* Fix to use baseclass_offset instead. FIXME */
445 baddr = baseclass_addr (type, i, valaddr, 0, &err);
446 if (err == 0 && baddr == 0)
1410f5f1
JK
447 error ("could not find virtual baseclass %s\n",
448 basename ? basename : "");
a8a69e63
FF
449
450 if (pretty)
451 {
452 fprintf_filtered (stream, "\n");
453 print_spaces_filtered (2 * recurse, stream);
454 }
455 fputs_filtered ("<", stream);
1410f5f1
JK
456 /* Not sure what the best notation is in the case where there is no
457 baseclass name. */
458 fputs_filtered (basename ? basename : "", stream);
a8a69e63
FF
459 fputs_filtered ("> = ", stream);
460 if (err != 0)
833e0d94
JK
461 {
462 fprintf_filtered (stream, "<invalid address ");
d24c0599 463 print_address_numeric ((CORE_ADDR) baddr, 1, stream);
833e0d94
JK
464 fprintf_filtered (stream, ">");
465 }
a8a69e63
FF
466 else
467 cp_print_value_fields (TYPE_BASECLASS (type, i), baddr, stream, format,
468 recurse, pretty,
a1a0d974
PS
469 (struct type **) obstack_base (&dont_print_vb_obstack),
470 0);
a8a69e63
FF
471 fputs_filtered (", ", stream);
472
473 flush_it:
474 ;
475 }
476
a1a0d974 477 if (dont_print_vb == 0)
a8a69e63
FF
478 {
479 /* Free the space used to deal with the printing
480 of this type from top level. */
a1a0d974 481 obstack_free (&dont_print_vb_obstack, last_dont_print);
a8a69e63
FF
482 /* Reset watermark so that we can continue protecting
483 ourselves from whatever we were protecting ourselves. */
a1a0d974
PS
484 dont_print_vb_obstack = tmp_obstack;
485 }
486}
487
488/* Print value of a static member.
489 To avoid infinite recursion when printing a class that contains
490 a static instance of the class, we keep the addresses of all printed
491 static member classes in an obstack and refuse to print them more
492 than once.
493
494 VAL contains the value to print, TYPE, STREAM, RECURSE, and PRETTY
495 have the same meanings as in c_val_print. */
496
497static void
498cp_print_static_field (type, val, stream, format, recurse, pretty)
499 struct type *type;
500 value_ptr val;
501 GDB_FILE *stream;
502 int format;
503 int recurse;
504 enum val_prettyprint pretty;
505{
506 if (TYPE_CODE (type) == TYPE_CODE_STRUCT)
507 {
508 CORE_ADDR *first_dont_print;
509 int i;
510
511 first_dont_print
512 = (CORE_ADDR *)obstack_base (&dont_print_statmem_obstack);
513 i = (CORE_ADDR *)obstack_next_free (&dont_print_statmem_obstack)
514 - first_dont_print;
515
516 while (--i >= 0)
517 {
518 if (VALUE_ADDRESS (val) == first_dont_print[i])
519 {
520 fputs_filtered ("<same as static member of an already seen type>",
521 stream);
522 return;
523 }
524 }
525
526 obstack_grow (&dont_print_statmem_obstack, &VALUE_ADDRESS (val),
527 sizeof (CORE_ADDR));
528
529 check_stub_type (type);
530 cp_print_value_fields (type, VALUE_CONTENTS (val),
531 stream, format, recurse, pretty,
532 NULL, 1);
533 return;
a8a69e63 534 }
a1a0d974
PS
535 val_print (type, VALUE_CONTENTS (val), VALUE_ADDRESS (val),
536 stream, format, 0, recurse, pretty);
a8a69e63
FF
537}
538
539void
540cp_print_class_member (valaddr, domain, stream, prefix)
541 char *valaddr;
542 struct type *domain;
199b2450 543 GDB_FILE *stream;
a8a69e63
FF
544 char *prefix;
545{
546
547 /* VAL is a byte offset into the structure type DOMAIN.
548 Find the name of the field for that offset and
549 print it. */
550 int extra = 0;
551 int bits = 0;
552 register unsigned int i;
553 unsigned len = TYPE_NFIELDS (domain);
554 /* @@ Make VAL into bit offset */
555 LONGEST val = unpack_long (builtin_type_int, valaddr) << 3;
556 for (i = TYPE_N_BASECLASSES (domain); i < len; i++)
557 {
558 int bitpos = TYPE_FIELD_BITPOS (domain, i);
559 QUIT;
560 if (val == bitpos)
561 break;
562 if (val < bitpos && i != 0)
563 {
564 /* Somehow pointing into a field. */
565 i -= 1;
566 extra = (val - TYPE_FIELD_BITPOS (domain, i));
567 if (extra & 0x7)
568 bits = 1;
569 else
570 extra >>= 3;
571 break;
572 }
573 }
574 if (i < len)
575 {
576 char *name;
577 fprintf_filtered (stream, prefix);
578 name = type_name_no_tag (domain);
579 if (name)
580 fputs_filtered (name, stream);
581 else
582 c_type_print_base (domain, stream, 0, 0);
583 fprintf_filtered (stream, "::");
584 fputs_filtered (TYPE_FIELD_NAME (domain, i), stream);
585 if (extra)
586 fprintf_filtered (stream, " + %d bytes", extra);
587 if (bits)
588 fprintf_filtered (stream, " (offset in bits)");
589 }
590 else
591 fprintf_filtered (stream, "%d", val >> 3);
592}
593
594void
595_initialize_cp_valprint ()
596{
4c664b8d
PS
597 add_show_from_set
598 (add_set_cmd ("static-members", class_support, var_boolean,
599 (char *)&static_field_print,
600 "Set printing of C++ static members.",
601 &setprintlist),
602 &showprintlist);
603 /* Turn on printing of static fields. */
604 static_field_print = 1;
605
a8a69e63
FF
606 add_show_from_set
607 (add_set_cmd ("vtbl", class_support, var_boolean, (char *)&vtblprint,
608 "Set printing of C++ virtual function tables.",
609 &setprintlist),
610 &showprintlist);
611
612 add_show_from_set
613 (add_set_cmd ("object", class_support, var_boolean, (char *)&objectprint,
614 "Set printing of object's derived type based on vtable info.",
615 &setprintlist),
616 &showprintlist);
617
618 /* Give people the defaults which they are used to. */
619 objectprint = 0;
620 vtblprint = 0;
a1a0d974
PS
621 obstack_begin (&dont_print_vb_obstack, 32 * sizeof (struct type *));
622 obstack_specify_allocation (&dont_print_statmem_obstack,
623 32 * sizeof (CORE_ADDR), sizeof (CORE_ADDR),
624 xmalloc, free);
a8a69e63 625}
This page took 0.172523 seconds and 4 git commands to generate.