* Makefile.in (GDBvn.texi): Fix match expression to work with
[deliverable/binutils-gdb.git] / gdb / cp-valprint.c
CommitLineData
a8a69e63 1/* Support for printing C++ values for GDB, the GNU debugger.
81afee37 2 Copyright 1986, 1988, 1989, 1991, 1994, 1995, 1996
a1a0d974 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"
5e548861 32#include "c-lang.h"
074d813d 33#include "target.h"
a8a69e63 34
4ef1f467
DT
35/* Indication of presence of HP-compiled object files */
36extern int hp_som_som_object_present; /* defined in symtab.c */
37
38
a8a69e63
FF
39int vtblprint; /* Controls printing of vtbl's */
40int objectprint; /* Controls looking up an object's derived type
41 using what we find in its vtables. */
8d2755a9 42int static_field_print; /* Controls printing of static fields. */
a1a0d974
PS
43
44static struct obstack dont_print_vb_obstack;
45static struct obstack dont_print_statmem_obstack;
46
47static void
48cp_print_static_field PARAMS ((struct type *, value_ptr, GDB_FILE *, int, int,
49 enum val_prettyprint));
a8a69e63
FF
50
51static void
4ef1f467 52cp_print_value PARAMS ((struct type *, struct type *, char *, int, CORE_ADDR, GDB_FILE *,
5e548861 53 int, int, enum val_prettyprint, struct type **));
a8a69e63 54
4ef1f467
DT
55static void
56cp_print_hpacc_virtual_table_entries PARAMS ((struct type *, int *, value_ptr, GDB_FILE *,
57 int, int, enum val_prettyprint));
58
59
c7da3ed3
FF
60void
61cp_print_class_method (valaddr, type, stream)
62 char *valaddr;
63 struct type *type;
199b2450 64 GDB_FILE *stream;
c7da3ed3
FF
65{
66 struct type *domain;
72cd0384
RP
67 struct fn_field *f = NULL;
68 int j = 0;
c7da3ed3
FF
69 int len2;
70 int offset;
71 char *kind = "";
72 CORE_ADDR addr;
73 struct symbol *sym;
74 unsigned len;
75 unsigned int i;
5e548861 76 struct type *target_type = check_typedef (TYPE_TARGET_TYPE (type));
c7da3ed3 77
5e548861 78 domain = TYPE_DOMAIN_TYPE (target_type);
30d20d15
PS
79 if (domain == (struct type *)NULL)
80 {
81 fprintf_filtered (stream, "<unknown>");
82 return;
83 }
c7da3ed3
FF
84 addr = unpack_pointer (lookup_pointer_type (builtin_type_void), valaddr);
85 if (METHOD_PTR_IS_VIRTUAL (addr))
86 {
87 offset = METHOD_PTR_TO_VOFFSET (addr);
88 len = TYPE_NFN_FIELDS (domain);
89 for (i = 0; i < len; i++)
90 {
91 f = TYPE_FN_FIELDLIST1 (domain, i);
92 len2 = TYPE_FN_FIELDLIST_LENGTH (domain, i);
93
94 for (j = 0; j < len2; j++)
95 {
96 QUIT;
97 if (TYPE_FN_FIELD_VOFFSET (f, j) == offset)
98 {
a46d92a7
PS
99 if (TYPE_FN_FIELD_STUB (f, j))
100 check_stub_method (domain, i, j);
c7da3ed3
FF
101 kind = "virtual ";
102 goto common;
103 }
104 }
105 }
106 }
107 else
108 {
109 sym = find_pc_function (addr);
110 if (sym == 0)
111 {
4ef1f467
DT
112 /* 1997-08-01 Currently unsupported with HP aCC */
113 if (hp_som_som_object_present)
114 {
115 fputs_filtered ("?? <not supported with HP aCC>", stream);
116 return;
117 }
c7da3ed3
FF
118 error ("invalid pointer to member function");
119 }
120 len = TYPE_NFN_FIELDS (domain);
121 for (i = 0; i < len; i++)
122 {
123 f = TYPE_FN_FIELDLIST1 (domain, i);
124 len2 = TYPE_FN_FIELDLIST_LENGTH (domain, i);
125
126 for (j = 0; j < len2; j++)
127 {
128 QUIT;
39cb3d04
PS
129 if (TYPE_FN_FIELD_STUB (f, j))
130 check_stub_method (domain, i, j);
c7da3ed3
FF
131 if (STREQ (SYMBOL_NAME (sym), TYPE_FN_FIELD_PHYSNAME (f, j)))
132 {
133 goto common;
134 }
135 }
136 }
137 }
138 common:
139 if (i < len)
140 {
a46d92a7
PS
141 char *demangled_name;
142
c7da3ed3 143 fprintf_filtered (stream, "&");
a46d92a7
PS
144 fprintf_filtered (stream, kind);
145 demangled_name = cplus_demangle (TYPE_FN_FIELD_PHYSNAME (f, j),
146 DMGL_ANSI | DMGL_PARAMS);
147 if (demangled_name == NULL)
148 fprintf_filtered (stream, "<badly mangled name %s>",
149 TYPE_FN_FIELD_PHYSNAME (f, j));
c7da3ed3
FF
150 else
151 {
a46d92a7
PS
152 fputs_filtered (demangled_name, stream);
153 free (demangled_name);
c7da3ed3
FF
154 }
155 }
156 else
157 {
158 fprintf_filtered (stream, "(");
159 type_print (type, "", stream, -1);
160 fprintf_filtered (stream, ") %d", (int) addr >> 3);
161 }
162}
163
36a2283d
PB
164/* This was what it was for gcc 2.4.5 and earlier. */
165static const char vtbl_ptr_name_old[] =
166 { CPLUS_MARKER,'v','t','b','l','_','p','t','r','_','t','y','p','e', 0 };
167/* It was changed to this after 2.4.5. */
168const char vtbl_ptr_name[] =
169 { '_','_','v','t','b','l','_','p','t','r','_','t','y','p','e', 0 };
170
4ef1f467
DT
171/* HP aCC uses different names */
172const char hpacc_vtbl_ptr_name[] =
173 { '_', '_', 'v', 'f', 'p', 0 };
174const char hpacc_vtbl_ptr_type_name[] =
175 { '_', '_', 'v', 'f', 't', 'y', 'p', 0 };
176
177
a8a69e63
FF
178/* Return truth value for assertion that TYPE is of the type
179 "pointer to virtual function". */
180
181int
182cp_is_vtbl_ptr_type(type)
183 struct type *type;
184{
185 char *typename = type_name_no_tag (type);
a8a69e63 186
8f341c15
JK
187 return (typename != NULL
188 && (STREQ (typename, vtbl_ptr_name)
189 || STREQ (typename, vtbl_ptr_name_old)));
a8a69e63
FF
190}
191
192/* Return truth value for the assertion that TYPE is of the type
193 "pointer to virtual function table". */
194
195int
196cp_is_vtbl_member(type)
197 struct type *type;
198{
199 if (TYPE_CODE (type) == TYPE_CODE_PTR)
36a2283d
PB
200 {
201 type = TYPE_TARGET_TYPE (type);
202 if (TYPE_CODE (type) == TYPE_CODE_ARRAY)
203 {
204 type = TYPE_TARGET_TYPE (type);
205 if (TYPE_CODE (type) == TYPE_CODE_STRUCT /* if not using thunks */
206 || TYPE_CODE (type) == TYPE_CODE_PTR) /* if using thunks */
207 {
208 /* Virtual functions tables are full of pointers
209 to virtual functions. */
210 return cp_is_vtbl_ptr_type (type);
211 }
212 }
213 }
a8a69e63
FF
214 return 0;
215}
216
5e548861
PB
217/* Mutually recursive subroutines of cp_print_value and c_val_print to
218 print out a structure's fields: cp_print_value_fields and cp_print_value.
219
220 TYPE, VALADDR, ADDRESS, STREAM, RECURSE, and PRETTY have the
221 same meanings as in cp_print_value and c_val_print.
a8a69e63 222
4ef1f467
DT
223 2nd argument REAL_TYPE is used to carry over the type of the derived
224 class across the recursion to base classes.
225
a8a69e63
FF
226 DONT_PRINT is an array of baseclass types that we
227 should not print, or zero if called from top level. */
228
229void
4ef1f467 230cp_print_value_fields (type, real_type, valaddr, offset, address, stream, format, recurse, pretty,
a1a0d974 231 dont_print_vb, dont_print_statmem)
a8a69e63 232 struct type *type;
4ef1f467 233 struct type *real_type;
a8a69e63 234 char *valaddr;
4ef1f467 235 int offset;
5e548861 236 CORE_ADDR address;
199b2450 237 GDB_FILE *stream;
a8a69e63
FF
238 int format;
239 int recurse;
240 enum val_prettyprint pretty;
a1a0d974
PS
241 struct type **dont_print_vb;
242 int dont_print_statmem;
a8a69e63
FF
243{
244 int i, len, n_baseclasses;
a1a0d974
PS
245 struct obstack tmp_obstack;
246 char *last_dont_print = obstack_next_free (&dont_print_statmem_obstack);
4ef1f467 247 int fields_seen = 0;
a8a69e63 248
5e548861 249 CHECK_TYPEDEF (type);
a8a69e63
FF
250
251 fprintf_filtered (stream, "{");
252 len = TYPE_NFIELDS (type);
253 n_baseclasses = TYPE_N_BASECLASSES (type);
254
4ef1f467 255 /* First, print out baseclasses such that we don't print
a8a69e63 256 duplicates of virtual baseclasses. */
4ef1f467 257
a8a69e63 258 if (n_baseclasses > 0)
4ef1f467 259 cp_print_value (type, real_type, valaddr, offset, address, stream,
5e548861 260 format, recurse+1, pretty, dont_print_vb);
a8a69e63 261
4ef1f467
DT
262 /* Second, print out data fields */
263
264 /* If there are no data fields, or if the only field is the
265 * vtbl pointer, skip this part */
266 if ((len == n_baseclasses) ||
267 ((len - n_baseclasses == 1) &&
268 TYPE_HAS_VTABLE(type) &&
269 STREQN(TYPE_FIELD_NAME (type, n_baseclasses), hpacc_vtbl_ptr_name, 5)) ||
270 !len)
a8a69e63
FF
271 fprintf_filtered (stream, "<No data fields>");
272 else
273 {
274 extern int inspect_it;
a8a69e63 275
a1a0d974
PS
276 if (dont_print_statmem == 0)
277 {
278 /* If we're at top level, carve out a completely fresh
279 chunk of the obstack and use that until this particular
280 invocation returns. */
281 tmp_obstack = dont_print_statmem_obstack;
282 obstack_finish (&dont_print_statmem_obstack);
283 }
284
a8a69e63
FF
285 for (i = n_baseclasses; i < len; i++)
286 {
4c664b8d
PS
287 /* If requested, skip printing of static fields. */
288 if (!static_field_print && TYPE_FIELD_STATIC (type, i))
a8a69e63 289 continue;
4ef1f467
DT
290
291 /* If a vtable pointer appears, we'll print it out later */
292 if (TYPE_HAS_VTABLE(type) && STREQN(TYPE_FIELD_NAME (type, i), hpacc_vtbl_ptr_name, 5))
293 continue;
294
a8a69e63
FF
295 if (fields_seen)
296 fprintf_filtered (stream, ", ");
297 else if (n_baseclasses > 0)
298 {
299 if (pretty)
300 {
301 fprintf_filtered (stream, "\n");
302 print_spaces_filtered (2 + 2 * recurse, stream);
303 fputs_filtered ("members of ", stream);
304 fputs_filtered (type_name_no_tag (type), stream);
305 fputs_filtered (": ", stream);
306 }
307 }
308 fields_seen = 1;
309
310 if (pretty)
311 {
312 fprintf_filtered (stream, "\n");
313 print_spaces_filtered (2 + 2 * recurse, stream);
314 }
315 else
316 {
317 wrap_here (n_spaces (2 + 2 * recurse));
318 }
319 if (inspect_it)
320 {
321 if (TYPE_CODE (TYPE_FIELD_TYPE (type, i)) == TYPE_CODE_PTR)
322 fputs_filtered ("\"( ptr \"", stream);
323 else
324 fputs_filtered ("\"( nodef \"", stream);
4c664b8d
PS
325 if (TYPE_FIELD_STATIC (type, i))
326 fputs_filtered ("static ", stream);
5e81259d
FF
327 fprintf_symbol_filtered (stream, TYPE_FIELD_NAME (type, i),
328 language_cplus,
329 DMGL_PARAMS | DMGL_ANSI);
a8a69e63 330 fputs_filtered ("\" \"", stream);
5e81259d
FF
331 fprintf_symbol_filtered (stream, TYPE_FIELD_NAME (type, i),
332 language_cplus,
333 DMGL_PARAMS | DMGL_ANSI);
a8a69e63
FF
334 fputs_filtered ("\") \"", stream);
335 }
336 else
337 {
1c95d7ab 338 annotate_field_begin (TYPE_FIELD_TYPE (type, i));
da988c20 339
4c664b8d
PS
340 if (TYPE_FIELD_STATIC (type, i))
341 fputs_filtered ("static ", stream);
5e81259d
FF
342 fprintf_symbol_filtered (stream, TYPE_FIELD_NAME (type, i),
343 language_cplus,
344 DMGL_PARAMS | DMGL_ANSI);
1c95d7ab 345 annotate_field_name_end ();
4ef1f467
DT
346 /* do not print leading '=' in case of anonymous unions */
347 if (strcmp (TYPE_FIELD_NAME (type, i), ""))
348 fputs_filtered (" = ", stream);
1c95d7ab 349 annotate_field_value ();
96f7edbd
JK
350 }
351
4c664b8d 352 if (!TYPE_FIELD_STATIC (type, i) && TYPE_FIELD_PACKED (type, i))
a8a69e63 353 {
82a2edfb 354 value_ptr v;
a8a69e63
FF
355
356 /* Bitfields require special handling, especially due to byte
357 order problems. */
024f65b1
KH
358 if (TYPE_FIELD_IGNORE (type, i))
359 {
aa074e84 360 fputs_filtered ("<optimized out or zero length>", stream);
024f65b1
KH
361 }
362 else
363 {
364 v = value_from_longest (TYPE_FIELD_TYPE (type, i),
4ef1f467 365 unpack_field_as_long (type, valaddr + offset, i));
a8a69e63 366
4ef1f467 367 val_print (TYPE_FIELD_TYPE(type, i), VALUE_CONTENTS (v), 0, 0,
dda398c3 368 stream, format, 0, recurse + 1, pretty);
024f65b1 369 }
a8a69e63
FF
370 }
371 else
372 {
024f65b1
KH
373 if (TYPE_FIELD_IGNORE (type, i))
374 {
aa074e84 375 fputs_filtered ("<optimized out or zero length>", stream);
024f65b1 376 }
4c664b8d
PS
377 else if (TYPE_FIELD_STATIC (type, i))
378 {
f7f37388
PB
379 value_ptr v = value_static_field (type, i);
380 if (v == NULL)
4c664b8d
PS
381 fputs_filtered ("<optimized out>", stream);
382 else
f7f37388
PB
383 cp_print_static_field (TYPE_FIELD_TYPE (type, i), v,
384 stream, format, recurse + 1,
385 pretty);
4c664b8d 386 }
024f65b1
KH
387 else
388 {
dda398c3 389 val_print (TYPE_FIELD_TYPE (type, i),
4ef1f467 390 valaddr, offset + TYPE_FIELD_BITPOS (type, i) / 8,
972256e7
PS
391 address + TYPE_FIELD_BITPOS (type, i) / 8,
392 stream, format, 0, recurse + 1, pretty);
024f65b1 393 }
a8a69e63 394 }
1c95d7ab 395 annotate_field_end ();
a8a69e63 396 }
96f7edbd 397
a1a0d974
PS
398 if (dont_print_statmem == 0)
399 {
400 /* Free the space used to deal with the printing
401 of the members from top level. */
402 obstack_free (&dont_print_statmem_obstack, last_dont_print);
403 dont_print_statmem_obstack = tmp_obstack;
404 }
405
a8a69e63
FF
406 if (pretty)
407 {
408 fprintf_filtered (stream, "\n");
409 print_spaces_filtered (2 * recurse, stream);
410 }
4ef1f467
DT
411 } /* if there are data fields */
412 /* Now print out the virtual table pointer if there is one */
413 if (TYPE_HAS_VTABLE(type) && STREQN(TYPE_FIELD_NAME (type, n_baseclasses), hpacc_vtbl_ptr_name, 5))
414 {
415 value_ptr v;
416 /* First get the virtual table pointer and print it out*/
417
418#if 0
419 fputs_filtered ("__vfp = ", stream);
420#endif
421
422 fputs_filtered (", Virtual table at ", stream);
423
424 /* pai: FIXME 32x64 problem? */
425 /* Not sure what the best notation is in the case where there is no
426 baseclass name. */
427 v = value_from_longest (lookup_pointer_type (builtin_type_unsigned_long),
428 * (unsigned long *) (valaddr + offset));
429
430 val_print (VALUE_TYPE (v), VALUE_CONTENTS (v), 0, 0,
431 stream, format, 0, recurse + 1, pretty);
432 fields_seen = 1;
433
434 if (vtblprint)
435 {
436 /* Print out function pointers in vtable. */
437
438 /* FIXME: then-clause is for non-RRBC layout of virtual
439 * table. The RRBC case in the else-clause is yet to be
440 * implemented. The if (1) below should be changed to a
441 * test for whether the executable we have was compiled
442 * with a version of HP aCC that doesn't have RRBC
443 * support. */
444
445 if (1)
446 {
447 /* no RRBC support; function pointers embedded directly in vtable */
448
449 int vfuncs = count_virtual_fns (real_type);
450
451 fputs_filtered (" {", stream);
452
453 /* FIXME : doesn't work at present */
454#if 0
455 fprintf_filtered (stream, "%d entr%s: ", vfuncs, vfuncs == 1 ? "y" : "ies");
456#else
457 fputs_filtered ("not implemented", stream);
458
459
460#endif
461
462 /* recursive function that prints all virtual function entries */
463#if 0
464 cp_print_hpacc_virtual_table_entries (real_type, &vfuncs, v, stream, format, recurse, pretty);
465#endif
466 fputs_filtered ("}", stream);
467 } /* non-RRBC case */
468 else
469 {
470 /* FIXME -- seem comments above */
471 /* RRBC support present; function pointers are found
472 * by indirection through the class segment entries. */
473
474
475 } /* RRBC case */
476 } /* if vtblprint */
477
478 if (pretty)
479 {
480 fprintf_filtered (stream, "\n");
481 print_spaces_filtered (2 * recurse, stream);
482 }
483
484 } /* if vtable exists */
485
a8a69e63
FF
486 fprintf_filtered (stream, "}");
487}
488
489/* Special val_print routine to avoid printing multiple copies of virtual
490 baseclasses. */
491
492static void
4ef1f467 493cp_print_value (type, real_type, valaddr, offset, address, stream, format, recurse, pretty,
5e548861 494 dont_print_vb)
a8a69e63 495 struct type *type;
4ef1f467 496 struct type *real_type;
a8a69e63 497 char *valaddr;
4ef1f467 498 int offset;
5e548861 499 CORE_ADDR address;
199b2450 500 GDB_FILE *stream;
a8a69e63
FF
501 int format;
502 int recurse;
503 enum val_prettyprint pretty;
a1a0d974 504 struct type **dont_print_vb;
a8a69e63
FF
505{
506 struct obstack tmp_obstack;
507 struct type **last_dont_print
a1a0d974 508 = (struct type **)obstack_next_free (&dont_print_vb_obstack);
a8a69e63
FF
509 int i, n_baseclasses = TYPE_N_BASECLASSES (type);
510
a1a0d974 511 if (dont_print_vb == 0)
a8a69e63
FF
512 {
513 /* If we're at top level, carve out a completely fresh
514 chunk of the obstack and use that until this particular
515 invocation returns. */
a1a0d974 516 tmp_obstack = dont_print_vb_obstack;
a8a69e63 517 /* Bump up the high-water mark. Now alpha is omega. */
a1a0d974 518 obstack_finish (&dont_print_vb_obstack);
a8a69e63
FF
519 }
520
521 for (i = 0; i < n_baseclasses; i++)
522 {
5e548861 523 int boffset;
4ef1f467 524 int skip;
5e548861
PB
525 struct type *baseclass = check_typedef (TYPE_BASECLASS (type, i));
526 char *basename = TYPE_NAME (baseclass);
074d813d 527 char *base_valaddr;
a8a69e63
FF
528
529 if (BASETYPE_VIA_VIRTUAL (type, i))
530 {
531 struct type **first_dont_print
a1a0d974 532 = (struct type **)obstack_base (&dont_print_vb_obstack);
a8a69e63 533
a1a0d974 534 int j = (struct type **)obstack_next_free (&dont_print_vb_obstack)
a8a69e63
FF
535 - first_dont_print;
536
537 while (--j >= 0)
5e548861 538 if (baseclass == first_dont_print[j])
a8a69e63
FF
539 goto flush_it;
540
5e548861 541 obstack_ptr_grow (&dont_print_vb_obstack, baseclass);
a8a69e63
FF
542 }
543
4ef1f467
DT
544 if (TYPE_HAS_VTABLE (type) && BASETYPE_VIA_VIRTUAL (type, i))
545 {
546 /* Assume HP/Taligent runtime convention */
547 find_rt_vbase_offset (type, TYPE_BASECLASS (type, i),
548 valaddr, offset, &boffset, &skip);
549 if (skip >= 0)
550 error ("Virtual base class offset not found from vtable while printing");
551 base_valaddr = valaddr;
552 }
553 else
554 {
555 boffset = baseclass_offset (type, i , valaddr + offset, address + offset);
556 skip = ((boffset == -1) || (boffset+offset) < 0 ) ? 1 : -1;
557
558 if (BASETYPE_VIA_VIRTUAL (type, i))
559 {
560 /* The virtual base class pointer might have been clobbered by the
561 user program. Make sure that it still points to a valid memory
562 location. */
563
564 if (boffset != -1 && ((boffset+offset) < 0 || (boffset+offset) >= TYPE_LENGTH (type)))
565 {
566 base_valaddr = (char *) alloca (TYPE_LENGTH (baseclass));
567 if (target_read_memory (address + boffset, base_valaddr,
568 TYPE_LENGTH (baseclass)) != 0)
569 skip = 1;
570 }
571 else
572 base_valaddr = valaddr;
573 }
574 else
575 base_valaddr = valaddr;
576 }
a8a69e63 577
4ef1f467 578 /* now do the printing */
a8a69e63
FF
579 if (pretty)
580 {
581 fprintf_filtered (stream, "\n");
582 print_spaces_filtered (2 * recurse, stream);
583 }
584 fputs_filtered ("<", stream);
1410f5f1
JK
585 /* Not sure what the best notation is in the case where there is no
586 baseclass name. */
587 fputs_filtered (basename ? basename : "", stream);
a8a69e63 588 fputs_filtered ("> = ", stream);
074d813d 589
074d813d 590
4ef1f467 591 if (skip >= 1)
5e548861 592 fprintf_filtered (stream, "<invalid address>");
a8a69e63 593 else
4ef1f467 594 cp_print_value_fields (baseclass, real_type, base_valaddr, offset + boffset, address,
5e548861 595 stream, format, recurse, pretty,
a1a0d974
PS
596 (struct type **) obstack_base (&dont_print_vb_obstack),
597 0);
a8a69e63
FF
598 fputs_filtered (", ", stream);
599
600 flush_it:
601 ;
602 }
603
a1a0d974 604 if (dont_print_vb == 0)
a8a69e63
FF
605 {
606 /* Free the space used to deal with the printing
607 of this type from top level. */
a1a0d974 608 obstack_free (&dont_print_vb_obstack, last_dont_print);
a8a69e63
FF
609 /* Reset watermark so that we can continue protecting
610 ourselves from whatever we were protecting ourselves. */
a1a0d974
PS
611 dont_print_vb_obstack = tmp_obstack;
612 }
613}
614
615/* Print value of a static member.
616 To avoid infinite recursion when printing a class that contains
617 a static instance of the class, we keep the addresses of all printed
618 static member classes in an obstack and refuse to print them more
619 than once.
620
621 VAL contains the value to print, TYPE, STREAM, RECURSE, and PRETTY
622 have the same meanings as in c_val_print. */
623
624static void
625cp_print_static_field (type, val, stream, format, recurse, pretty)
626 struct type *type;
627 value_ptr val;
628 GDB_FILE *stream;
629 int format;
630 int recurse;
631 enum val_prettyprint pretty;
632{
633 if (TYPE_CODE (type) == TYPE_CODE_STRUCT)
634 {
635 CORE_ADDR *first_dont_print;
636 int i;
637
638 first_dont_print
639 = (CORE_ADDR *)obstack_base (&dont_print_statmem_obstack);
640 i = (CORE_ADDR *)obstack_next_free (&dont_print_statmem_obstack)
641 - first_dont_print;
642
643 while (--i >= 0)
644 {
645 if (VALUE_ADDRESS (val) == first_dont_print[i])
646 {
647 fputs_filtered ("<same as static member of an already seen type>",
648 stream);
649 return;
650 }
651 }
652
5579919f 653 obstack_grow (&dont_print_statmem_obstack, (char *) &VALUE_ADDRESS (val),
a1a0d974
PS
654 sizeof (CORE_ADDR));
655
5e548861 656 CHECK_TYPEDEF (type);
4ef1f467 657 cp_print_value_fields (type, type, VALUE_CONTENTS_ALL (val), VALUE_EMBEDDED_OFFSET (val), VALUE_ADDRESS (val),
5e548861 658 stream, format, recurse, pretty, NULL, 1);
a1a0d974 659 return;
a8a69e63 660 }
4ef1f467 661 val_print (type, VALUE_CONTENTS_ALL (val), VALUE_EMBEDDED_OFFSET (val), VALUE_ADDRESS (val),
a1a0d974 662 stream, format, 0, recurse, pretty);
a8a69e63
FF
663}
664
665void
666cp_print_class_member (valaddr, domain, stream, prefix)
667 char *valaddr;
668 struct type *domain;
199b2450 669 GDB_FILE *stream;
a8a69e63
FF
670 char *prefix;
671{
672
673 /* VAL is a byte offset into the structure type DOMAIN.
674 Find the name of the field for that offset and
675 print it. */
676 int extra = 0;
677 int bits = 0;
678 register unsigned int i;
679 unsigned len = TYPE_NFIELDS (domain);
4ef1f467 680
a8a69e63 681 /* @@ Make VAL into bit offset */
4ef1f467
DT
682
683 /* Note: HP aCC generates offsets that are the real byte offsets added
684 to a constant bias 0x20000000 (1 << 29). This constant bias gets
685 shifted out in the code below -- joyous happenstance! */
686
687 /* Note: HP cfront uses a constant bias of 1; if we support this
688 compiler ever, we will have to adjust the computation below */
689
a8a69e63
FF
690 LONGEST val = unpack_long (builtin_type_int, valaddr) << 3;
691 for (i = TYPE_N_BASECLASSES (domain); i < len; i++)
692 {
693 int bitpos = TYPE_FIELD_BITPOS (domain, i);
694 QUIT;
695 if (val == bitpos)
696 break;
697 if (val < bitpos && i != 0)
698 {
699 /* Somehow pointing into a field. */
700 i -= 1;
701 extra = (val - TYPE_FIELD_BITPOS (domain, i));
702 if (extra & 0x7)
703 bits = 1;
704 else
705 extra >>= 3;
706 break;
707 }
708 }
709 if (i < len)
710 {
711 char *name;
712 fprintf_filtered (stream, prefix);
713 name = type_name_no_tag (domain);
714 if (name)
715 fputs_filtered (name, stream);
716 else
717 c_type_print_base (domain, stream, 0, 0);
718 fprintf_filtered (stream, "::");
719 fputs_filtered (TYPE_FIELD_NAME (domain, i), stream);
720 if (extra)
721 fprintf_filtered (stream, " + %d bytes", extra);
722 if (bits)
723 fprintf_filtered (stream, " (offset in bits)");
724 }
725 else
726 fprintf_filtered (stream, "%d", val >> 3);
727}
728
4ef1f467
DT
729
730/* This function prints out virtual table entries for a class; it
731 * recurses on the base classes to find all virtual functions
732 * available in a class.
733 *
734 * pai/1997-05-21 Note: As the name suggests, it's currently
735 * implemented for HP aCC runtime only. g++ objects are handled
736 * differently and I have made no attempt to fold that logic in
737 * here. The runtime layout is different for the two cases. Also,
738 * this currently has only the code for non-RRBC layouts generated by
739 * the HP aCC compiler; RRBC code is stubbed out and will have to be
740 * added later. */
741
742
743static void
744cp_print_hpacc_virtual_table_entries (type, vfuncs, v, stream, format, recurse, pretty)
745 struct type * type;
746 int * vfuncs;
747 value_ptr v;
748 GDB_FILE *stream;
749 int format;
750 int recurse;
751 enum val_prettyprint pretty;
752{
753 int fn, oi;
754
755 /* pai: FIXME this function doesn't work. It should handle a given
756 * virtual function only once (latest redefinition in class hierarchy)
757 */
758
759 /* Recursion on other classes that can share the same vtable */
760 struct type * pbc = primary_base_class (type);
761 if (pbc)
762 cp_print_hpacc_virtual_table_entries (pbc, vfuncs, v, stream, format, recurse, pretty);
763
764 /* Now deal with vfuncs declared in this class */
765 for (fn = 0; fn < TYPE_NFN_FIELDS (type); fn++)
766 for (oi = 0; oi < TYPE_FN_FIELDLIST_LENGTH (type, fn); oi++)
767 if (TYPE_FN_FIELD_VIRTUAL_P (TYPE_FN_FIELDLIST1 (type, fn), oi))
768 {
769 char * vf_name;
770
771 /* virtual function offset */
772 int vx = TYPE_FN_FIELD_VOFFSET (TYPE_FN_FIELDLIST1 (type, fn), oi) - 1;
773
774 /* Get the address of the vfunction entry */
775 value_ptr vf = value_copy (v);
776 if (VALUE_LAZY (vf))
777 (void) value_fetch_lazy (vf);
778 vf->aligner.contents[0] += 4 * (HP_ACC_VFUNC_START + vx); /* adjust by offset */
779 vf = value_ind (vf); /* get the entry */
780 VALUE_TYPE (vf) = VALUE_TYPE (v); /* make it a pointer */
781
782 /* print out the entry */
783 val_print (VALUE_TYPE (vf), VALUE_CONTENTS (vf), 0, 0,
784 stream, format, 0, recurse + 1, pretty);
785 vf_name = cplus_demangle (TYPE_FN_FIELD_PHYSNAME (TYPE_FN_FIELDLIST1 (type, fn), oi),
786 DMGL_ARM); /* pai: (temp) FIXME Maybe this should be DMGL_ANSI */
787 fprintf_filtered (stream, " %s", vf_name);
788 if (--(*vfuncs) > 0)
789 fputs_filtered (", ", stream);
790 }
791}
792
793
794
a8a69e63
FF
795void
796_initialize_cp_valprint ()
797{
4c664b8d
PS
798 add_show_from_set
799 (add_set_cmd ("static-members", class_support, var_boolean,
800 (char *)&static_field_print,
801 "Set printing of C++ static members.",
802 &setprintlist),
803 &showprintlist);
804 /* Turn on printing of static fields. */
805 static_field_print = 1;
806
a8a69e63
FF
807 add_show_from_set
808 (add_set_cmd ("vtbl", class_support, var_boolean, (char *)&vtblprint,
809 "Set printing of C++ virtual function tables.",
810 &setprintlist),
811 &showprintlist);
812
813 add_show_from_set
814 (add_set_cmd ("object", class_support, var_boolean, (char *)&objectprint,
815 "Set printing of object's derived type based on vtable info.",
816 &setprintlist),
817 &showprintlist);
818
819 /* Give people the defaults which they are used to. */
820 objectprint = 0;
821 vtblprint = 0;
a1a0d974
PS
822 obstack_begin (&dont_print_vb_obstack, 32 * sizeof (struct type *));
823 obstack_specify_allocation (&dont_print_statmem_obstack,
824 32 * sizeof (CORE_ADDR), sizeof (CORE_ADDR),
825 xmalloc, free);
a8a69e63 826}
This page took 0.296794 seconds and 4 git commands to generate.