Update gdb.ada/variant_record_packed_array
[deliverable/binutils-gdb.git] / gdb / cp-valprint.c
CommitLineData
c906108c 1/* Support for printing C++ values for GDB, the GNU debugger.
a2bd3dcd 2
0b302171
JB
3 Copyright (C) 1986, 1988-1989, 1991-1997, 2000-2003, 2005-2012 Free
4 Software Foundation, Inc.
c906108c 5
c5aa993b 6 This file is part of GDB.
c906108c 7
c5aa993b
JM
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
a9762ec7 10 the Free Software Foundation; either version 3 of the License, or
c5aa993b 11 (at your option) any later version.
c906108c 12
c5aa993b
JM
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
c906108c 17
c5aa993b 18 You should have received a copy of the GNU General Public License
a9762ec7 19 along with this program. If not, see <http://www.gnu.org/licenses/>. */
c906108c
SS
20
21#include "defs.h"
04ea0df1 22#include "gdb_obstack.h"
c906108c
SS
23#include "symtab.h"
24#include "gdbtypes.h"
25#include "expression.h"
26#include "value.h"
27#include "command.h"
28#include "gdbcmd.h"
29#include "demangle.h"
30#include "annotate.h"
31#include "gdb_string.h"
32#include "c-lang.h"
33#include "target.h"
b9d652ac 34#include "cp-abi.h"
973177d3 35#include "valprint.h"
d3cbe7ef 36#include "cp-support.h"
cf309262 37#include "language.h"
a6bac58e 38#include "python/python.h"
8af8e3bc 39#include "exceptions.h"
79d43c61 40#include "typeprint.h"
c906108c 41
aff410f1 42/* Controls printing of vtbl's. */
920d2a44
AC
43static void
44show_vtblprint (struct ui_file *file, int from_tty,
45 struct cmd_list_element *c, const char *value)
46{
47 fprintf_filtered (file, _("\
48Printing of C++ virtual function tables is %s.\n"),
49 value);
50}
51
52/* Controls looking up an object's derived type using what we find in
53 its vtables. */
920d2a44
AC
54static void
55show_objectprint (struct ui_file *file, int from_tty,
56 struct cmd_list_element *c,
57 const char *value)
58{
59 fprintf_filtered (file, _("\
60Printing of object's derived type based on vtable info is %s.\n"),
61 value);
62}
63
920d2a44
AC
64static void
65show_static_field_print (struct ui_file *file, int from_tty,
aff410f1
MS
66 struct cmd_list_element *c,
67 const char *value)
920d2a44 68{
aff410f1
MS
69 fprintf_filtered (file,
70 _("Printing of C++ static members is %s.\n"),
920d2a44
AC
71 value);
72}
73
c906108c
SS
74
75static struct obstack dont_print_vb_obstack;
76static struct obstack dont_print_statmem_obstack;
ec31cde5 77static struct obstack dont_print_stat_array_obstack;
c906108c 78
a14ed312 79extern void _initialize_cp_valprint (void);
392a587b 80
6943961c 81static void cp_print_static_field (struct type *, struct value *,
79a45b7d
TT
82 struct ui_file *, int,
83 const struct value_print_options *);
c906108c 84
aff410f1
MS
85static void cp_print_value (struct type *, struct type *,
86 const gdb_byte *, int,
87 CORE_ADDR, struct ui_file *,
88 int, const struct value *,
89 const struct value_print_options *,
90 struct type **);
c906108c 91
c906108c 92
8343f86c 93/* GCC versions after 2.4.5 use this. */
2c63a960 94const char vtbl_ptr_name[] = "__vtbl_ptr_type";
c906108c 95
c906108c
SS
96/* Return truth value for assertion that TYPE is of the type
97 "pointer to virtual function". */
98
99int
fba45db2 100cp_is_vtbl_ptr_type (struct type *type)
c906108c 101{
0d5cff50 102 const char *typename = type_name_no_tag (type);
c906108c 103
8343f86c 104 return (typename != NULL && !strcmp (typename, vtbl_ptr_name));
c906108c
SS
105}
106
107/* Return truth value for the assertion that TYPE is of the type
108 "pointer to virtual function table". */
109
110int
fba45db2 111cp_is_vtbl_member (struct type *type)
c906108c 112{
aff410f1
MS
113 /* With older versions of g++, the vtbl field pointed to an array of
114 structures. Nowadays it points directly to the structure. */
c906108c
SS
115 if (TYPE_CODE (type) == TYPE_CODE_PTR)
116 {
117 type = TYPE_TARGET_TYPE (type);
118 if (TYPE_CODE (type) == TYPE_CODE_ARRAY)
119 {
120 type = TYPE_TARGET_TYPE (type);
aff410f1
MS
121 if (TYPE_CODE (type) == TYPE_CODE_STRUCT /* if not using thunks */
122 || TYPE_CODE (type) == TYPE_CODE_PTR) /* if using thunks */
c906108c
SS
123 {
124 /* Virtual functions tables are full of pointers
aff410f1 125 to virtual functions. */
c906108c
SS
126 return cp_is_vtbl_ptr_type (type);
127 }
128 }
0e5e3ea6
PS
129 else if (TYPE_CODE (type) == TYPE_CODE_STRUCT) /* if not using thunks */
130 {
131 return cp_is_vtbl_ptr_type (type);
132 }
133 else if (TYPE_CODE (type) == TYPE_CODE_PTR) /* if using thunks */
134 {
aff410f1
MS
135 /* The type name of the thunk pointer is NULL when using
136 dwarf2. We could test for a pointer to a function, but
137 there is no type info for the virtual table either, so it
138 wont help. */
0e5e3ea6
PS
139 return cp_is_vtbl_ptr_type (type);
140 }
c906108c
SS
141 }
142 return 0;
143}
144
145/* Mutually recursive subroutines of cp_print_value and c_val_print to
aff410f1
MS
146 print out a structure's fields: cp_print_value_fields and
147 cp_print_value.
c5aa993b 148
aff410f1
MS
149 TYPE, VALADDR, ADDRESS, STREAM, RECURSE, and OPTIONS have the same
150 meanings as in cp_print_value and c_val_print.
c906108c 151
aff410f1
MS
152 2nd argument REAL_TYPE is used to carry over the type of the
153 derived class across the recursion to base classes.
c906108c 154
aff410f1
MS
155 DONT_PRINT is an array of baseclass types that we should not print,
156 or zero if called from top level. */
c906108c
SS
157
158void
a2bd3dcd 159cp_print_value_fields (struct type *type, struct type *real_type,
aff410f1
MS
160 const gdb_byte *valaddr, int offset,
161 CORE_ADDR address, struct ui_file *stream,
162 int recurse, const struct value *val,
79a45b7d 163 const struct value_print_options *options,
aff410f1
MS
164 struct type **dont_print_vb,
165 int dont_print_statmem)
c906108c
SS
166{
167 int i, len, n_baseclasses;
c906108c 168 int fields_seen = 0;
ec31cde5 169 static int last_set_recurse = -1;
c906108c
SS
170
171 CHECK_TYPEDEF (type);
99903ae3 172
ec31cde5
CM
173 if (recurse == 0)
174 {
aff410f1
MS
175 /* Any object can be left on obstacks only during an unexpected
176 error. */
6036c742 177
ec31cde5 178 if (obstack_object_size (&dont_print_statmem_obstack) > 0)
6036c742
JK
179 {
180 obstack_free (&dont_print_statmem_obstack, NULL);
aff410f1
MS
181 obstack_begin (&dont_print_statmem_obstack,
182 32 * sizeof (CORE_ADDR));
6036c742 183 }
ec31cde5 184 if (obstack_object_size (&dont_print_stat_array_obstack) > 0)
6036c742
JK
185 {
186 obstack_free (&dont_print_stat_array_obstack, NULL);
187 obstack_begin (&dont_print_stat_array_obstack,
188 32 * sizeof (struct type *));
189 }
ec31cde5 190 }
c906108c
SS
191
192 fprintf_filtered (stream, "{");
193 len = TYPE_NFIELDS (type);
194 n_baseclasses = TYPE_N_BASECLASSES (type);
195
196 /* First, print out baseclasses such that we don't print
197 duplicates of virtual baseclasses. */
198
199 if (n_baseclasses > 0)
aff410f1
MS
200 cp_print_value (type, real_type, valaddr,
201 offset, address, stream,
202 recurse + 1, val, options,
203 dont_print_vb);
c906108c
SS
204
205 /* Second, print out data fields */
206
086280be
UW
207 /* If there are no data fields, skip this part */
208 if (len == n_baseclasses || !len)
c906108c
SS
209 fprintf_filtered (stream, "<No data fields>");
210 else
211 {
f56dcb88
CM
212 int statmem_obstack_initial_size = 0;
213 int stat_array_obstack_initial_size = 0;
7977e5d2
TT
214 struct type *vptr_basetype = NULL;
215 int vptr_fieldno;
216
c906108c
SS
217 if (dont_print_statmem == 0)
218 {
f56dcb88 219 statmem_obstack_initial_size =
0b66f317 220 obstack_object_size (&dont_print_statmem_obstack);
ec31cde5
CM
221
222 if (last_set_recurse != recurse)
223 {
f56dcb88
CM
224 stat_array_obstack_initial_size =
225 obstack_object_size (&dont_print_stat_array_obstack);
c5504eaf 226
ec31cde5
CM
227 last_set_recurse = recurse;
228 }
c906108c
SS
229 }
230
7977e5d2 231 vptr_fieldno = get_vptr_fieldno (type, &vptr_basetype);
c906108c
SS
232 for (i = n_baseclasses; i < len; i++)
233 {
234 /* If requested, skip printing of static fields. */
79a45b7d 235 if (!options->static_field_print
d6a843b5 236 && field_is_static (&TYPE_FIELD (type, i)))
c906108c
SS
237 continue;
238
c906108c
SS
239 if (fields_seen)
240 fprintf_filtered (stream, ", ");
241 else if (n_baseclasses > 0)
242 {
79a45b7d 243 if (options->pretty)
c906108c
SS
244 {
245 fprintf_filtered (stream, "\n");
246 print_spaces_filtered (2 + 2 * recurse, stream);
247 fputs_filtered ("members of ", stream);
248 fputs_filtered (type_name_no_tag (type), stream);
249 fputs_filtered (": ", stream);
250 }
251 }
252 fields_seen = 1;
253
79a45b7d 254 if (options->pretty)
c906108c
SS
255 {
256 fprintf_filtered (stream, "\n");
257 print_spaces_filtered (2 + 2 * recurse, stream);
258 }
c5aa993b 259 else
c906108c
SS
260 {
261 wrap_here (n_spaces (2 + 2 * recurse));
262 }
79a45b7d 263 if (options->inspect_it)
c906108c
SS
264 {
265 if (TYPE_CODE (TYPE_FIELD_TYPE (type, i)) == TYPE_CODE_PTR)
266 fputs_filtered ("\"( ptr \"", stream);
267 else
268 fputs_filtered ("\"( nodef \"", stream);
d6a843b5 269 if (field_is_static (&TYPE_FIELD (type, i)))
c906108c 270 fputs_filtered ("static ", stream);
aff410f1
MS
271 fprintf_symbol_filtered (stream,
272 TYPE_FIELD_NAME (type, i),
cf309262 273 current_language->la_language,
c906108c
SS
274 DMGL_PARAMS | DMGL_ANSI);
275 fputs_filtered ("\" \"", stream);
aff410f1
MS
276 fprintf_symbol_filtered (stream,
277 TYPE_FIELD_NAME (type, i),
cf309262 278 current_language->la_language,
c906108c
SS
279 DMGL_PARAMS | DMGL_ANSI);
280 fputs_filtered ("\") \"", stream);
281 }
282 else
283 {
284 annotate_field_begin (TYPE_FIELD_TYPE (type, i));
285
d6a843b5 286 if (field_is_static (&TYPE_FIELD (type, i)))
c906108c 287 fputs_filtered ("static ", stream);
aff410f1
MS
288 fprintf_symbol_filtered (stream,
289 TYPE_FIELD_NAME (type, i),
cf309262 290 current_language->la_language,
c906108c
SS
291 DMGL_PARAMS | DMGL_ANSI);
292 annotate_field_name_end ();
aff410f1
MS
293 /* Do not print leading '=' in case of anonymous
294 unions. */
c906108c
SS
295 if (strcmp (TYPE_FIELD_NAME (type, i), ""))
296 fputs_filtered (" = ", stream);
297 annotate_field_value ();
298 }
299
d6a843b5
JK
300 if (!field_is_static (&TYPE_FIELD (type, i))
301 && TYPE_FIELD_PACKED (type, i))
c906108c 302 {
6943961c 303 struct value *v;
c906108c 304
aff410f1
MS
305 /* Bitfields require special handling, especially due to
306 byte order problems. */
c906108c
SS
307 if (TYPE_FIELD_IGNORE (type, i))
308 {
c5aa993b 309 fputs_filtered ("<optimized out or zero length>", stream);
c906108c 310 }
8cf6f0b1
TT
311 else if (value_bits_synthetic_pointer (val,
312 TYPE_FIELD_BITPOS (type,
313 i),
314 TYPE_FIELD_BITSIZE (type,
315 i)))
316 {
317 fputs_filtered (_("<synthetic pointer>"), stream);
318 }
aff410f1
MS
319 else if (!value_bits_valid (val,
320 TYPE_FIELD_BITPOS (type, i),
0e03807e
TT
321 TYPE_FIELD_BITSIZE (type, i)))
322 {
585fdaa1 323 val_print_optimized_out (stream);
0e03807e 324 }
c906108c
SS
325 else
326 {
79a45b7d 327 struct value_print_options opts = *options;
c5504eaf 328
79a45b7d 329 opts.deref_ref = 0;
5467c6c8
PA
330
331 v = value_field_bitfield (type, i, valaddr, offset, val);
c906108c 332
79a45b7d 333 common_val_print (v, stream, recurse + 1, &opts,
d8ca156b 334 current_language);
c906108c
SS
335 }
336 }
337 else
338 {
339 if (TYPE_FIELD_IGNORE (type, i))
340 {
aff410f1
MS
341 fputs_filtered ("<optimized out or zero length>",
342 stream);
c906108c 343 }
d6a843b5 344 else if (field_is_static (&TYPE_FIELD (type, i)))
c906108c 345 {
ee86786c
TT
346 volatile struct gdb_exception ex;
347 struct value *v = NULL;
348
349 TRY_CATCH (ex, RETURN_MASK_ERROR)
350 {
351 v = value_static_field (type, i);
352 }
353
354 if (ex.reason < 0)
355 fprintf_filtered (stream,
356 _("<error reading variable: %s>"),
357 ex.message);
358 else if (v == NULL)
585fdaa1 359 val_print_optimized_out (stream);
c906108c 360 else
aff410f1
MS
361 cp_print_static_field (TYPE_FIELD_TYPE (type, i),
362 v, stream, recurse + 1,
363 options);
c906108c 364 }
7977e5d2 365 else if (i == vptr_fieldno && type == vptr_basetype)
410528f0 366 {
a72c8f6a
JK
367 int i_offset = offset + TYPE_FIELD_BITPOS (type, i) / 8;
368 struct type *i_type = TYPE_FIELD_TYPE (type, i);
369
370 if (valprint_check_validity (stream, i_type, i_offset, val))
371 {
372 CORE_ADDR addr;
373
374 addr = extract_typed_address (valaddr + i_offset, i_type);
edf0c1b7
TT
375 print_function_pointer_address (options,
376 get_type_arch (type),
377 addr, stream);
a72c8f6a 378 }
410528f0 379 }
c906108c
SS
380 else
381 {
79a45b7d 382 struct value_print_options opts = *options;
c5504eaf 383
79a45b7d 384 opts.deref_ref = 0;
c5aa993b 385 val_print (TYPE_FIELD_TYPE (type, i),
aff410f1
MS
386 valaddr,
387 offset + TYPE_FIELD_BITPOS (type, i) / 8,
edf3d5f3 388 address,
0e03807e 389 stream, recurse + 1, val, &opts,
d8ca156b 390 current_language);
c906108c
SS
391 }
392 }
393 annotate_field_end ();
394 }
395
396 if (dont_print_statmem == 0)
397 {
0b66f317
CM
398 int obstack_final_size =
399 obstack_object_size (&dont_print_statmem_obstack);
400
aff410f1
MS
401 if (obstack_final_size > statmem_obstack_initial_size)
402 {
403 /* In effect, a pop of the printed-statics stack. */
0b66f317 404
aff410f1
MS
405 void *free_to_ptr =
406 obstack_next_free (&dont_print_statmem_obstack) -
407 (obstack_final_size - statmem_obstack_initial_size);
0b66f317 408
aff410f1
MS
409 obstack_free (&dont_print_statmem_obstack,
410 free_to_ptr);
411 }
ec31cde5
CM
412
413 if (last_set_recurse != recurse)
414 {
f56dcb88
CM
415 int obstack_final_size =
416 obstack_object_size (&dont_print_stat_array_obstack);
417
418 if (obstack_final_size > stat_array_obstack_initial_size)
419 {
420 void *free_to_ptr =
aff410f1
MS
421 obstack_next_free (&dont_print_stat_array_obstack)
422 - (obstack_final_size
423 - stat_array_obstack_initial_size);
f56dcb88
CM
424
425 obstack_free (&dont_print_stat_array_obstack,
426 free_to_ptr);
427 }
ec31cde5
CM
428 last_set_recurse = -1;
429 }
c906108c
SS
430 }
431
79a45b7d 432 if (options->pretty)
c906108c
SS
433 {
434 fprintf_filtered (stream, "\n");
435 print_spaces_filtered (2 * recurse, stream);
436 }
c5aa993b 437 } /* if there are data fields */
c5aa993b 438
c906108c
SS
439 fprintf_filtered (stream, "}");
440}
441
edf3d5f3
TT
442/* Like cp_print_value_fields, but find the runtime type of the object
443 and pass it as the `real_type' argument to cp_print_value_fields.
444 This function is a hack to work around the fact that
445 common_val_print passes the embedded offset to val_print, but not
446 the enclosing type. */
447
448void
449cp_print_value_fields_rtti (struct type *type,
450 const gdb_byte *valaddr, int offset,
451 CORE_ADDR address,
452 struct ui_file *stream, int recurse,
0e03807e 453 const struct value *val,
edf3d5f3 454 const struct value_print_options *options,
c5504eaf
MS
455 struct type **dont_print_vb,
456 int dont_print_statmem)
edf3d5f3 457{
0e03807e
TT
458 struct type *real_type = NULL;
459
460 /* We require all bits to be valid in order to attempt a
461 conversion. */
462 if (value_bits_valid (val, TARGET_CHAR_BIT * offset,
463 TARGET_CHAR_BIT * TYPE_LENGTH (type)))
464 {
465 struct value *value;
466 int full, top, using_enc;
467
468 /* Ugh, we have to convert back to a value here. */
469 value = value_from_contents_and_address (type, valaddr + offset,
470 address + offset);
aff410f1
MS
471 /* We don't actually care about most of the result here -- just
472 the type. We already have the correct offset, due to how
473 val_print was initially called. */
0e03807e
TT
474 real_type = value_rtti_type (value, &full, &top, &using_enc);
475 }
476
edf3d5f3
TT
477 if (!real_type)
478 real_type = type;
479
480 cp_print_value_fields (type, real_type, valaddr, offset,
0e03807e 481 address, stream, recurse, val, options,
edf3d5f3
TT
482 dont_print_vb, dont_print_statmem);
483}
484
aff410f1
MS
485/* Special val_print routine to avoid printing multiple copies of
486 virtual baseclasses. */
c906108c
SS
487
488static void
a2bd3dcd 489cp_print_value (struct type *type, struct type *real_type,
aff410f1
MS
490 const gdb_byte *valaddr, int offset,
491 CORE_ADDR address, struct ui_file *stream,
492 int recurse, const struct value *val,
79a45b7d
TT
493 const struct value_print_options *options,
494 struct type **dont_print_vb)
c906108c 495{
c906108c 496 struct type **last_dont_print
2c63a960 497 = (struct type **) obstack_next_free (&dont_print_vb_obstack);
c1b6e682 498 struct obstack tmp_obstack = dont_print_vb_obstack;
c906108c 499 int i, n_baseclasses = TYPE_N_BASECLASSES (type);
b9d652ac
DJ
500 int thisoffset;
501 struct type *thistype;
c906108c
SS
502
503 if (dont_print_vb == 0)
504 {
aff410f1
MS
505 /* If we're at top level, carve out a completely fresh chunk of
506 the obstack and use that until this particular invocation
507 returns. */
c906108c
SS
508 /* Bump up the high-water mark. Now alpha is omega. */
509 obstack_finish (&dont_print_vb_obstack);
510 }
511
512 for (i = 0; i < n_baseclasses; i++)
513 {
8af8e3bc 514 int boffset = 0;
c906108c
SS
515 int skip;
516 struct type *baseclass = check_typedef (TYPE_BASECLASS (type, i));
0d5cff50 517 const char *basename = TYPE_NAME (baseclass);
8af8e3bc
PA
518 const gdb_byte *base_valaddr = NULL;
519 const struct value *base_val = NULL;
520 volatile struct gdb_exception ex;
c906108c
SS
521
522 if (BASETYPE_VIA_VIRTUAL (type, i))
523 {
524 struct type **first_dont_print
2c63a960 525 = (struct type **) obstack_base (&dont_print_vb_obstack);
c906108c 526
aff410f1
MS
527 int j = (struct type **)
528 obstack_next_free (&dont_print_vb_obstack) - first_dont_print;
c906108c
SS
529
530 while (--j >= 0)
531 if (baseclass == first_dont_print[j])
532 goto flush_it;
533
534 obstack_ptr_grow (&dont_print_vb_obstack, baseclass);
535 }
536
b9d652ac
DJ
537 thisoffset = offset;
538 thistype = real_type;
086280be 539
8af8e3bc 540 TRY_CATCH (ex, RETURN_MASK_ERROR)
c5aa993b 541 {
8af8e3bc
PA
542 boffset = baseclass_offset (type, i, valaddr, offset, address, val);
543 }
544 if (ex.reason < 0 && ex.error == NOT_AVAILABLE_ERROR)
545 skip = -1;
546 else if (ex.reason < 0)
547 skip = 1;
548 else
549 {
550 skip = 0;
c906108c 551
8af8e3bc 552 if (BASETYPE_VIA_VIRTUAL (type, i))
c5aa993b 553 {
8af8e3bc
PA
554 /* The virtual base class pointer might have been
555 clobbered by the user program. Make sure that it
556 still points to a valid memory location. */
557
558 if ((boffset + offset) < 0
559 || (boffset + offset) >= TYPE_LENGTH (real_type))
560 {
d5161074
SP
561 gdb_byte *buf;
562 struct cleanup *back_to;
563
564 buf = xmalloc (TYPE_LENGTH (baseclass));
565 back_to = make_cleanup (xfree, buf);
8af8e3bc
PA
566
567 if (target_read_memory (address + boffset, buf,
568 TYPE_LENGTH (baseclass)) != 0)
569 skip = 1;
570 base_val = value_from_contents_and_address (baseclass,
571 buf,
572 address + boffset);
573 thisoffset = 0;
574 boffset = 0;
575 thistype = baseclass;
576 base_valaddr = value_contents_for_printing_const (base_val);
d5161074 577 do_cleanups (back_to);
8af8e3bc
PA
578 }
579 else
580 {
581 base_valaddr = valaddr;
582 base_val = val;
583 }
c5aa993b
JM
584 }
585 else
de4127a3
PA
586 {
587 base_valaddr = valaddr;
588 base_val = val;
589 }
c906108c
SS
590 }
591
aff410f1 592 /* Now do the printing. */
79a45b7d 593 if (options->pretty)
c906108c
SS
594 {
595 fprintf_filtered (stream, "\n");
596 print_spaces_filtered (2 * recurse, stream);
597 }
598 fputs_filtered ("<", stream);
aff410f1
MS
599 /* Not sure what the best notation is in the case where there is
600 no baseclass name. */
c906108c
SS
601 fputs_filtered (basename ? basename : "", stream);
602 fputs_filtered ("> = ", stream);
603
8af8e3bc
PA
604 if (skip < 0)
605 val_print_unavailable (stream);
606 else if (skip > 0)
607 val_print_invalid_address (stream);
c906108c 608 else
a6bac58e
TT
609 {
610 int result = 0;
611
612 /* Attempt to run the Python pretty-printers on the
613 baseclass if possible. */
614 if (!options->raw)
615 result = apply_val_pretty_printer (baseclass, base_valaddr,
616 thisoffset + boffset,
de4127a3
PA
617 value_address (base_val),
618 stream, recurse, base_val,
619 options, current_language);
620
621
a6bac58e
TT
622
623 if (!result)
624 cp_print_value_fields (baseclass, thistype, base_valaddr,
de4127a3
PA
625 thisoffset + boffset,
626 value_address (base_val),
627 stream, recurse, base_val, options,
a6bac58e
TT
628 ((struct type **)
629 obstack_base (&dont_print_vb_obstack)),
630 0);
631 }
c906108c
SS
632 fputs_filtered (", ", stream);
633
634 flush_it:
635 ;
636 }
637
638 if (dont_print_vb == 0)
639 {
640 /* Free the space used to deal with the printing
c5aa993b 641 of this type from top level. */
c906108c
SS
642 obstack_free (&dont_print_vb_obstack, last_dont_print);
643 /* Reset watermark so that we can continue protecting
c5aa993b 644 ourselves from whatever we were protecting ourselves. */
c906108c
SS
645 dont_print_vb_obstack = tmp_obstack;
646 }
647}
648
aff410f1
MS
649/* Print value of a static member. To avoid infinite recursion when
650 printing a class that contains a static instance of the class, we
651 keep the addresses of all printed static member classes in an
652 obstack and refuse to print them more than once.
c906108c 653
79a45b7d 654 VAL contains the value to print, TYPE, STREAM, RECURSE, and OPTIONS
c906108c
SS
655 have the same meanings as in c_val_print. */
656
657static void
2c63a960 658cp_print_static_field (struct type *type,
6943961c 659 struct value *val,
2c63a960 660 struct ui_file *stream,
2c63a960 661 int recurse,
79a45b7d 662 const struct value_print_options *options)
c906108c 663{
79a45b7d 664 struct value_print_options opts;
ec31cde5 665
c906108c
SS
666 if (TYPE_CODE (type) == TYPE_CODE_STRUCT)
667 {
668 CORE_ADDR *first_dont_print;
42ae5230 669 CORE_ADDR addr;
c906108c
SS
670 int i;
671
672 first_dont_print
c5aa993b 673 = (CORE_ADDR *) obstack_base (&dont_print_statmem_obstack);
99903ae3
CM
674 i = obstack_object_size (&dont_print_statmem_obstack)
675 / sizeof (CORE_ADDR);
c906108c
SS
676
677 while (--i >= 0)
678 {
42ae5230 679 if (value_address (val) == first_dont_print[i])
c906108c 680 {
2c63a960
JB
681 fputs_filtered ("<same as static member of an already"
682 " seen type>",
c906108c
SS
683 stream);
684 return;
685 }
686 }
687
42ae5230
TT
688 addr = value_address (val);
689 obstack_grow (&dont_print_statmem_obstack, (char *) &addr,
c906108c 690 sizeof (CORE_ADDR));
c906108c 691 CHECK_TYPEDEF (type);
edf3d5f3 692 cp_print_value_fields (type, value_enclosing_type (val),
0e03807e 693 value_contents_for_printing (val),
42ae5230 694 value_embedded_offset (val), addr,
aff410f1
MS
695 stream, recurse, val,
696 options, NULL, 1);
c906108c
SS
697 return;
698 }
79a45b7d 699
ec31cde5
CM
700 if (TYPE_CODE (type) == TYPE_CODE_ARRAY)
701 {
702 struct type **first_dont_print;
703 int i;
704 struct type *target_type = TYPE_TARGET_TYPE (type);
705
706 first_dont_print
707 = (struct type **) obstack_base (&dont_print_stat_array_obstack);
708 i = obstack_object_size (&dont_print_stat_array_obstack)
1e9beacb 709 / sizeof (struct type *);
ec31cde5
CM
710
711 while (--i >= 0)
712 {
713 if (target_type == first_dont_print[i])
714 {
715 fputs_filtered ("<same as static member of an already"
716 " seen type>",
717 stream);
718 return;
719 }
720 }
721
aff410f1
MS
722 obstack_grow (&dont_print_stat_array_obstack,
723 (char *) &target_type,
ec31cde5
CM
724 sizeof (struct type *));
725 }
726
79a45b7d
TT
727 opts = *options;
728 opts.deref_ref = 0;
0e03807e 729 val_print (type, value_contents_for_printing (val),
aff410f1
MS
730 value_embedded_offset (val),
731 value_address (val),
732 stream, recurse, val,
733 &opts, current_language);
c906108c
SS
734}
735
0d5de010 736
aff410f1
MS
737/* Find the field in *DOMAIN, or its non-virtual base classes, with
738 bit offset OFFSET. Set *DOMAIN to the containing type and *FIELDNO
739 to the containing field number. If OFFSET is not exactly at the
740 start of some field, set *DOMAIN to NULL. */
0d5de010 741
2c0b251b 742static void
0d5de010
DJ
743cp_find_class_member (struct type **domain_p, int *fieldno,
744 LONGEST offset)
745{
746 struct type *domain;
747 unsigned int i;
748 unsigned len;
749
750 *domain_p = check_typedef (*domain_p);
751 domain = *domain_p;
752 len = TYPE_NFIELDS (domain);
753
754 for (i = TYPE_N_BASECLASSES (domain); i < len; i++)
755 {
756 LONGEST bitpos = TYPE_FIELD_BITPOS (domain, i);
757
758 QUIT;
759 if (offset == bitpos)
760 {
761 *fieldno = i;
762 return;
763 }
764 }
765
766 for (i = 0; i < TYPE_N_BASECLASSES (domain); i++)
767 {
768 LONGEST bitpos = TYPE_FIELD_BITPOS (domain, i);
769 LONGEST bitsize = 8 * TYPE_LENGTH (TYPE_FIELD_TYPE (domain, i));
770
771 if (offset >= bitpos && offset < bitpos + bitsize)
772 {
773 *domain_p = TYPE_FIELD_TYPE (domain, i);
774 cp_find_class_member (domain_p, fieldno, offset - bitpos);
775 return;
776 }
777 }
778
779 *domain_p = NULL;
780}
781
c906108c 782void
ad4820ab 783cp_print_class_member (const gdb_byte *valaddr, struct type *type,
fba45db2 784 struct ui_file *stream, char *prefix)
c906108c 785{
e17a4113
UW
786 enum bfd_endian byte_order = gdbarch_byte_order (get_type_arch (type));
787
c906108c
SS
788 /* VAL is a byte offset into the structure type DOMAIN.
789 Find the name of the field for that offset and
790 print it. */
ad4820ab 791 struct type *domain = TYPE_DOMAIN_TYPE (type);
e17a4113 792 LONGEST val;
0d5de010 793 unsigned int fieldno;
c906108c 794
aff410f1
MS
795 val = extract_signed_integer (valaddr,
796 TYPE_LENGTH (type),
797 byte_order);
e17a4113 798
0d5de010
DJ
799 /* Pointers to data members are usually byte offsets into an object.
800 Because a data member can have offset zero, and a NULL pointer to
801 member must be distinct from any valid non-NULL pointer to
802 member, either the value is biased or the NULL value has a
803 special representation; both are permitted by ISO C++. HP aCC
804 used a bias of 0x20000000; HP cfront used a bias of 1; g++ 3.x
805 and other compilers which use the Itanium ABI use -1 as the NULL
806 value. GDB only supports that last form; to add support for
807 another form, make this into a cp-abi hook. */
c906108c 808
0d5de010 809 if (val == -1)
c906108c 810 {
0d5de010
DJ
811 fprintf_filtered (stream, "NULL");
812 return;
c906108c 813 }
0d5de010
DJ
814
815 cp_find_class_member (&domain, &fieldno, val << 3);
816
817 if (domain != NULL)
c906108c 818 {
0d5cff50 819 const char *name;
c5504eaf 820
306d9ac5 821 fputs_filtered (prefix, stream);
c906108c
SS
822 name = type_name_no_tag (domain);
823 if (name)
c5aa993b 824 fputs_filtered (name, stream);
c906108c 825 else
79d43c61 826 c_type_print_base (domain, stream, 0, 0, &type_print_raw_options);
c906108c 827 fprintf_filtered (stream, "::");
0d5de010 828 fputs_filtered (TYPE_FIELD_NAME (domain, fieldno), stream);
c906108c
SS
829 }
830 else
0d5de010 831 fprintf_filtered (stream, "%ld", (long) val);
c906108c
SS
832}
833
834
c906108c 835void
fba45db2 836_initialize_cp_valprint (void)
c906108c 837{
5bf193a2 838 add_setshow_boolean_cmd ("static-members", class_support,
79a45b7d 839 &user_print_options.static_field_print, _("\
5bf193a2
AC
840Set printing of C++ static members."), _("\
841Show printing of C++ static members."), NULL,
842 NULL,
920d2a44 843 show_static_field_print,
5bf193a2 844 &setprintlist, &showprintlist);
c906108c 845
79a45b7d
TT
846 add_setshow_boolean_cmd ("vtbl", class_support,
847 &user_print_options.vtblprint, _("\
5bf193a2
AC
848Set printing of C++ virtual function tables."), _("\
849Show printing of C++ virtual function tables."), NULL,
850 NULL,
920d2a44 851 show_vtblprint,
5bf193a2
AC
852 &setprintlist, &showprintlist);
853
79a45b7d
TT
854 add_setshow_boolean_cmd ("object", class_support,
855 &user_print_options.objectprint, _("\
5bf193a2
AC
856Set printing of object's derived type based on vtable info."), _("\
857Show printing of object's derived type based on vtable info."), NULL,
858 NULL,
920d2a44 859 show_objectprint,
5bf193a2 860 &setprintlist, &showprintlist);
c906108c 861
aff410f1
MS
862 obstack_begin (&dont_print_stat_array_obstack,
863 32 * sizeof (struct type *));
864 obstack_begin (&dont_print_statmem_obstack,
865 32 * sizeof (CORE_ADDR));
866 obstack_begin (&dont_print_vb_obstack,
867 32 * sizeof (struct type *));
c906108c 868}
This page took 0.944175 seconds and 4 git commands to generate.