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