Introduce pascal_value_print_inner
[deliverable/binutils-gdb.git] / gdb / p-valprint.c
1 /* Support for printing Pascal values for GDB, the GNU debugger.
2
3 Copyright (C) 2000-2020 Free Software Foundation, Inc.
4
5 This file is part of GDB.
6
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
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
11
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.
16
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
19
20 /* This file is derived from c-valprint.c */
21
22 #include "defs.h"
23 #include "gdb_obstack.h"
24 #include "symtab.h"
25 #include "gdbtypes.h"
26 #include "expression.h"
27 #include "value.h"
28 #include "command.h"
29 #include "gdbcmd.h"
30 #include "gdbcore.h"
31 #include "demangle.h"
32 #include "valprint.h"
33 #include "typeprint.h"
34 #include "language.h"
35 #include "target.h"
36 #include "annotate.h"
37 #include "p-lang.h"
38 #include "cp-abi.h"
39 #include "cp-support.h"
40 #include "objfiles.h"
41 #include "gdbsupport/byte-vector.h"
42 #include "cli/cli-style.h"
43 \f
44
45 static void pascal_object_print_value_fields (struct type *, const gdb_byte *,
46 LONGEST,
47 CORE_ADDR, struct ui_file *,
48 int,
49 struct value *,
50 const struct value_print_options *,
51 struct type **, int);
52
53 /* Decorations for Pascal. */
54
55 static const struct generic_val_print_decorations p_decorations =
56 {
57 "",
58 " + ",
59 " * I",
60 "true",
61 "false",
62 "void",
63 "{",
64 "}"
65 };
66
67 /* See val_print for a description of the various parameters of this
68 function; they are identical. */
69
70 void
71 pascal_val_print (struct type *type,
72 int embedded_offset, CORE_ADDR address,
73 struct ui_file *stream, int recurse,
74 struct value *original_value,
75 const struct value_print_options *options)
76 {
77 struct gdbarch *gdbarch = get_type_arch (type);
78 enum bfd_endian byte_order = type_byte_order (type);
79 unsigned int i = 0; /* Number of characters printed */
80 unsigned len;
81 struct type *elttype;
82 unsigned eltlen;
83 int length_pos, length_size, string_pos;
84 struct type *char_type;
85 CORE_ADDR addr;
86 int want_space = 0;
87 const gdb_byte *valaddr = value_contents_for_printing (original_value);
88
89 type = check_typedef (type);
90 switch (TYPE_CODE (type))
91 {
92 case TYPE_CODE_ARRAY:
93 {
94 LONGEST low_bound, high_bound;
95
96 if (get_array_bounds (type, &low_bound, &high_bound))
97 {
98 len = high_bound - low_bound + 1;
99 elttype = check_typedef (TYPE_TARGET_TYPE (type));
100 eltlen = TYPE_LENGTH (elttype);
101 if (options->prettyformat_arrays)
102 {
103 print_spaces_filtered (2 + 2 * recurse, stream);
104 }
105 /* If 's' format is used, try to print out as string.
106 If no format is given, print as string if element type
107 is of TYPE_CODE_CHAR and element size is 1,2 or 4. */
108 if (options->format == 's'
109 || ((eltlen == 1 || eltlen == 2 || eltlen == 4)
110 && TYPE_CODE (elttype) == TYPE_CODE_CHAR
111 && options->format == 0))
112 {
113 /* If requested, look for the first null char and only print
114 elements up to it. */
115 if (options->stop_print_at_null)
116 {
117 unsigned int temp_len;
118
119 /* Look for a NULL char. */
120 for (temp_len = 0;
121 extract_unsigned_integer (valaddr + embedded_offset +
122 temp_len * eltlen, eltlen,
123 byte_order)
124 && temp_len < len && temp_len < options->print_max;
125 temp_len++);
126 len = temp_len;
127 }
128
129 LA_PRINT_STRING (stream, TYPE_TARGET_TYPE (type),
130 valaddr + embedded_offset, len, NULL, 0,
131 options);
132 i = len;
133 }
134 else
135 {
136 fprintf_filtered (stream, "{");
137 /* If this is a virtual function table, print the 0th
138 entry specially, and the rest of the members normally. */
139 if (pascal_object_is_vtbl_ptr_type (elttype))
140 {
141 i = 1;
142 fprintf_filtered (stream, "%d vtable entries", len - 1);
143 }
144 else
145 {
146 i = 0;
147 }
148 val_print_array_elements (type, embedded_offset,
149 address, stream, recurse,
150 original_value, options, i);
151 fprintf_filtered (stream, "}");
152 }
153 break;
154 }
155 /* Array of unspecified length: treat like pointer to first elt. */
156 addr = address + embedded_offset;
157 }
158 goto print_unpacked_pointer;
159
160 case TYPE_CODE_PTR:
161 if (options->format && options->format != 's')
162 {
163 val_print_scalar_formatted (type, embedded_offset,
164 original_value, options, 0, stream);
165 break;
166 }
167 if (options->vtblprint && pascal_object_is_vtbl_ptr_type (type))
168 {
169 /* Print the unmangled name if desired. */
170 /* Print vtable entry - we only get here if we ARE using
171 -fvtable_thunks. (Otherwise, look under TYPE_CODE_STRUCT.) */
172 /* Extract the address, assume that it is unsigned. */
173 addr = extract_unsigned_integer (valaddr + embedded_offset,
174 TYPE_LENGTH (type), byte_order);
175 print_address_demangle (options, gdbarch, addr, stream, demangle);
176 break;
177 }
178 check_typedef (TYPE_TARGET_TYPE (type));
179
180 addr = unpack_pointer (type, valaddr + embedded_offset);
181 print_unpacked_pointer:
182 elttype = check_typedef (TYPE_TARGET_TYPE (type));
183
184 if (TYPE_CODE (elttype) == TYPE_CODE_FUNC)
185 {
186 /* Try to print what function it points to. */
187 print_address_demangle (options, gdbarch, addr, stream, demangle);
188 return;
189 }
190
191 if (options->addressprint && options->format != 's')
192 {
193 fputs_filtered (paddress (gdbarch, addr), stream);
194 want_space = 1;
195 }
196
197 /* For a pointer to char or unsigned char, also print the string
198 pointed to, unless pointer is null. */
199 if (((TYPE_LENGTH (elttype) == 1
200 && (TYPE_CODE (elttype) == TYPE_CODE_INT
201 || TYPE_CODE (elttype) == TYPE_CODE_CHAR))
202 || ((TYPE_LENGTH (elttype) == 2 || TYPE_LENGTH (elttype) == 4)
203 && TYPE_CODE (elttype) == TYPE_CODE_CHAR))
204 && (options->format == 0 || options->format == 's')
205 && addr != 0)
206 {
207 if (want_space)
208 fputs_filtered (" ", stream);
209 /* No wide string yet. */
210 i = val_print_string (elttype, NULL, addr, -1, stream, options);
211 }
212 /* Also for pointers to pascal strings. */
213 /* Note: this is Free Pascal specific:
214 as GDB does not recognize stabs pascal strings
215 Pascal strings are mapped to records
216 with lowercase names PM. */
217 if (is_pascal_string_type (elttype, &length_pos, &length_size,
218 &string_pos, &char_type, NULL)
219 && addr != 0)
220 {
221 ULONGEST string_length;
222 gdb_byte *buffer;
223
224 if (want_space)
225 fputs_filtered (" ", stream);
226 buffer = (gdb_byte *) xmalloc (length_size);
227 read_memory (addr + length_pos, buffer, length_size);
228 string_length = extract_unsigned_integer (buffer, length_size,
229 byte_order);
230 xfree (buffer);
231 i = val_print_string (char_type, NULL,
232 addr + string_pos, string_length,
233 stream, options);
234 }
235 else if (pascal_object_is_vtbl_member (type))
236 {
237 /* Print vtbl's nicely. */
238 CORE_ADDR vt_address = unpack_pointer (type,
239 valaddr + embedded_offset);
240 struct bound_minimal_symbol msymbol =
241 lookup_minimal_symbol_by_pc (vt_address);
242
243 /* If 'symbol_print' is set, we did the work above. */
244 if (!options->symbol_print
245 && (msymbol.minsym != NULL)
246 && (vt_address == BMSYMBOL_VALUE_ADDRESS (msymbol)))
247 {
248 if (want_space)
249 fputs_filtered (" ", stream);
250 fputs_filtered ("<", stream);
251 fputs_filtered (msymbol.minsym->print_name (), stream);
252 fputs_filtered (">", stream);
253 want_space = 1;
254 }
255 if (vt_address && options->vtblprint)
256 {
257 struct value *vt_val;
258 struct symbol *wsym = NULL;
259 struct type *wtype;
260
261 if (want_space)
262 fputs_filtered (" ", stream);
263
264 if (msymbol.minsym != NULL)
265 {
266 const char *search_name = msymbol.minsym->search_name ();
267 wsym = lookup_symbol_search_name (search_name, NULL,
268 VAR_DOMAIN).symbol;
269 }
270
271 if (wsym)
272 {
273 wtype = SYMBOL_TYPE (wsym);
274 }
275 else
276 {
277 wtype = TYPE_TARGET_TYPE (type);
278 }
279 vt_val = value_at (wtype, vt_address);
280 common_val_print (vt_val, stream, recurse + 1, options,
281 current_language);
282 if (options->prettyformat)
283 {
284 fprintf_filtered (stream, "\n");
285 print_spaces_filtered (2 + 2 * recurse, stream);
286 }
287 }
288 }
289
290 return;
291
292 case TYPE_CODE_REF:
293 case TYPE_CODE_ENUM:
294 case TYPE_CODE_FLAGS:
295 case TYPE_CODE_FUNC:
296 case TYPE_CODE_RANGE:
297 case TYPE_CODE_INT:
298 case TYPE_CODE_FLT:
299 case TYPE_CODE_VOID:
300 case TYPE_CODE_ERROR:
301 case TYPE_CODE_UNDEF:
302 case TYPE_CODE_BOOL:
303 case TYPE_CODE_CHAR:
304 generic_val_print (type, embedded_offset, address,
305 stream, recurse, original_value, options,
306 &p_decorations);
307 break;
308
309 case TYPE_CODE_UNION:
310 if (recurse && !options->unionprint)
311 {
312 fprintf_filtered (stream, "{...}");
313 break;
314 }
315 /* Fall through. */
316 case TYPE_CODE_STRUCT:
317 if (options->vtblprint && pascal_object_is_vtbl_ptr_type (type))
318 {
319 /* Print the unmangled name if desired. */
320 /* Print vtable entry - we only get here if NOT using
321 -fvtable_thunks. (Otherwise, look under TYPE_CODE_PTR.) */
322 /* Extract the address, assume that it is unsigned. */
323 print_address_demangle
324 (options, gdbarch,
325 extract_unsigned_integer (valaddr + embedded_offset
326 + TYPE_FIELD_BITPOS (type,
327 VTBL_FNADDR_OFFSET) / 8,
328 TYPE_LENGTH (TYPE_FIELD_TYPE (type,
329 VTBL_FNADDR_OFFSET)),
330 byte_order),
331 stream, demangle);
332 }
333 else
334 {
335 if (is_pascal_string_type (type, &length_pos, &length_size,
336 &string_pos, &char_type, NULL))
337 {
338 len = extract_unsigned_integer (valaddr + embedded_offset
339 + length_pos, length_size,
340 byte_order);
341 LA_PRINT_STRING (stream, char_type,
342 valaddr + embedded_offset + string_pos,
343 len, NULL, 0, options);
344 }
345 else
346 pascal_object_print_value_fields (type, valaddr, embedded_offset,
347 address, stream, recurse,
348 original_value, options,
349 NULL, 0);
350 }
351 break;
352
353 case TYPE_CODE_SET:
354 elttype = TYPE_INDEX_TYPE (type);
355 elttype = check_typedef (elttype);
356 if (TYPE_STUB (elttype))
357 {
358 fprintf_styled (stream, metadata_style.style (), "<incomplete type>");
359 break;
360 }
361 else
362 {
363 struct type *range = elttype;
364 LONGEST low_bound, high_bound;
365 int need_comma = 0;
366
367 fputs_filtered ("[", stream);
368
369 int bound_info = get_discrete_bounds (range, &low_bound, &high_bound);
370 if (low_bound == 0 && high_bound == -1 && TYPE_LENGTH (type) > 0)
371 {
372 /* If we know the size of the set type, we can figure out the
373 maximum value. */
374 bound_info = 0;
375 high_bound = TYPE_LENGTH (type) * TARGET_CHAR_BIT - 1;
376 TYPE_HIGH_BOUND (range) = high_bound;
377 }
378 maybe_bad_bstring:
379 if (bound_info < 0)
380 {
381 fputs_styled ("<error value>", metadata_style.style (), stream);
382 goto done;
383 }
384
385 for (i = low_bound; i <= high_bound; i++)
386 {
387 int element = value_bit_index (type,
388 valaddr + embedded_offset, i);
389
390 if (element < 0)
391 {
392 i = element;
393 goto maybe_bad_bstring;
394 }
395 if (element)
396 {
397 if (need_comma)
398 fputs_filtered (", ", stream);
399 print_type_scalar (range, i, stream);
400 need_comma = 1;
401
402 if (i + 1 <= high_bound
403 && value_bit_index (type,
404 valaddr + embedded_offset, ++i))
405 {
406 int j = i;
407
408 fputs_filtered ("..", stream);
409 while (i + 1 <= high_bound
410 && value_bit_index (type,
411 valaddr + embedded_offset,
412 ++i))
413 j = i;
414 print_type_scalar (range, j, stream);
415 }
416 }
417 }
418 done:
419 fputs_filtered ("]", stream);
420 }
421 break;
422
423 default:
424 error (_("Invalid pascal type code %d in symbol table."),
425 TYPE_CODE (type));
426 }
427 }
428
429 /* See p-lang.h. */
430
431 void
432 pascal_value_print_inner (struct value *val, struct ui_file *stream,
433 int recurse,
434 const struct value_print_options *options)
435
436 {
437 pascal_val_print (value_type (val), value_embedded_offset (val),
438 value_address (val), stream, recurse, val, options);
439 }
440
441 \f
442 void
443 pascal_value_print (struct value *val, struct ui_file *stream,
444 const struct value_print_options *options)
445 {
446 struct type *type = value_type (val);
447 struct value_print_options opts = *options;
448
449 opts.deref_ref = 1;
450
451 /* If it is a pointer, indicate what it points to.
452
453 Print type also if it is a reference.
454
455 Object pascal: if it is a member pointer, we will take care
456 of that when we print it. */
457 if (TYPE_CODE (type) == TYPE_CODE_PTR
458 || TYPE_CODE (type) == TYPE_CODE_REF)
459 {
460 /* Hack: remove (char *) for char strings. Their
461 type is indicated by the quoted string anyway. */
462 if (TYPE_CODE (type) == TYPE_CODE_PTR
463 && TYPE_NAME (type) == NULL
464 && TYPE_NAME (TYPE_TARGET_TYPE (type)) != NULL
465 && strcmp (TYPE_NAME (TYPE_TARGET_TYPE (type)), "char") == 0)
466 {
467 /* Print nothing. */
468 }
469 else
470 {
471 fprintf_filtered (stream, "(");
472 type_print (type, "", stream, -1);
473 fprintf_filtered (stream, ") ");
474 }
475 }
476 common_val_print (val, stream, 0, &opts, current_language);
477 }
478
479
480 static void
481 show_pascal_static_field_print (struct ui_file *file, int from_tty,
482 struct cmd_list_element *c, const char *value)
483 {
484 fprintf_filtered (file, _("Printing of pascal static members is %s.\n"),
485 value);
486 }
487
488 static struct obstack dont_print_vb_obstack;
489 static struct obstack dont_print_statmem_obstack;
490
491 static void pascal_object_print_static_field (struct value *,
492 struct ui_file *, int,
493 const struct value_print_options *);
494
495 static void pascal_object_print_value (struct type *, const gdb_byte *,
496 LONGEST,
497 CORE_ADDR, struct ui_file *, int,
498 struct value *,
499 const struct value_print_options *,
500 struct type **);
501
502 /* It was changed to this after 2.4.5. */
503 const char pascal_vtbl_ptr_name[] =
504 {'_', '_', 'v', 't', 'b', 'l', '_', 'p', 't', 'r', '_', 't', 'y', 'p', 'e', 0};
505
506 /* Return truth value for assertion that TYPE is of the type
507 "pointer to virtual function". */
508
509 int
510 pascal_object_is_vtbl_ptr_type (struct type *type)
511 {
512 const char *type_name = TYPE_NAME (type);
513
514 return (type_name != NULL
515 && strcmp (type_name, pascal_vtbl_ptr_name) == 0);
516 }
517
518 /* Return truth value for the assertion that TYPE is of the type
519 "pointer to virtual function table". */
520
521 int
522 pascal_object_is_vtbl_member (struct type *type)
523 {
524 if (TYPE_CODE (type) == TYPE_CODE_PTR)
525 {
526 type = TYPE_TARGET_TYPE (type);
527 if (TYPE_CODE (type) == TYPE_CODE_ARRAY)
528 {
529 type = TYPE_TARGET_TYPE (type);
530 if (TYPE_CODE (type) == TYPE_CODE_STRUCT /* If not using
531 thunks. */
532 || TYPE_CODE (type) == TYPE_CODE_PTR) /* If using thunks. */
533 {
534 /* Virtual functions tables are full of pointers
535 to virtual functions. */
536 return pascal_object_is_vtbl_ptr_type (type);
537 }
538 }
539 }
540 return 0;
541 }
542
543 /* Mutually recursive subroutines of pascal_object_print_value and
544 c_val_print to print out a structure's fields:
545 pascal_object_print_value_fields and pascal_object_print_value.
546
547 TYPE, VALADDR, ADDRESS, STREAM, RECURSE, and OPTIONS have the
548 same meanings as in pascal_object_print_value and c_val_print.
549
550 DONT_PRINT is an array of baseclass types that we
551 should not print, or zero if called from top level. */
552
553 static void
554 pascal_object_print_value_fields (struct type *type, const gdb_byte *valaddr,
555 LONGEST offset,
556 CORE_ADDR address, struct ui_file *stream,
557 int recurse,
558 struct value *val,
559 const struct value_print_options *options,
560 struct type **dont_print_vb,
561 int dont_print_statmem)
562 {
563 int i, len, n_baseclasses;
564 char *last_dont_print
565 = (char *) obstack_next_free (&dont_print_statmem_obstack);
566
567 type = check_typedef (type);
568
569 fprintf_filtered (stream, "{");
570 len = TYPE_NFIELDS (type);
571 n_baseclasses = TYPE_N_BASECLASSES (type);
572
573 /* Print out baseclasses such that we don't print
574 duplicates of virtual baseclasses. */
575 if (n_baseclasses > 0)
576 pascal_object_print_value (type, valaddr, offset, address,
577 stream, recurse + 1, val,
578 options, dont_print_vb);
579
580 if (!len && n_baseclasses == 1)
581 fprintf_styled (stream, metadata_style.style (), "<No data fields>");
582 else
583 {
584 struct obstack tmp_obstack = dont_print_statmem_obstack;
585 int fields_seen = 0;
586
587 if (dont_print_statmem == 0)
588 {
589 /* If we're at top level, carve out a completely fresh
590 chunk of the obstack and use that until this particular
591 invocation returns. */
592 obstack_finish (&dont_print_statmem_obstack);
593 }
594
595 for (i = n_baseclasses; i < len; i++)
596 {
597 /* If requested, skip printing of static fields. */
598 if (!options->pascal_static_field_print
599 && field_is_static (&TYPE_FIELD (type, i)))
600 continue;
601 if (fields_seen)
602 fprintf_filtered (stream, ", ");
603 else if (n_baseclasses > 0)
604 {
605 if (options->prettyformat)
606 {
607 fprintf_filtered (stream, "\n");
608 print_spaces_filtered (2 + 2 * recurse, stream);
609 fputs_filtered ("members of ", stream);
610 fputs_filtered (TYPE_NAME (type), stream);
611 fputs_filtered (": ", stream);
612 }
613 }
614 fields_seen = 1;
615
616 if (options->prettyformat)
617 {
618 fprintf_filtered (stream, "\n");
619 print_spaces_filtered (2 + 2 * recurse, stream);
620 }
621 else
622 {
623 wrap_here (n_spaces (2 + 2 * recurse));
624 }
625
626 annotate_field_begin (TYPE_FIELD_TYPE (type, i));
627
628 if (field_is_static (&TYPE_FIELD (type, i)))
629 {
630 fputs_filtered ("static ", stream);
631 fprintf_symbol_filtered (stream,
632 TYPE_FIELD_NAME (type, i),
633 current_language->la_language,
634 DMGL_PARAMS | DMGL_ANSI);
635 }
636 else
637 fputs_styled (TYPE_FIELD_NAME (type, i),
638 variable_name_style.style (), stream);
639 annotate_field_name_end ();
640 fputs_filtered (" = ", stream);
641 annotate_field_value ();
642
643 if (!field_is_static (&TYPE_FIELD (type, i))
644 && TYPE_FIELD_PACKED (type, i))
645 {
646 struct value *v;
647
648 /* Bitfields require special handling, especially due to byte
649 order problems. */
650 if (TYPE_FIELD_IGNORE (type, i))
651 {
652 fputs_styled ("<optimized out or zero length>",
653 metadata_style.style (), stream);
654 }
655 else if (value_bits_synthetic_pointer (val,
656 TYPE_FIELD_BITPOS (type,
657 i),
658 TYPE_FIELD_BITSIZE (type,
659 i)))
660 {
661 fputs_styled (_("<synthetic pointer>"),
662 metadata_style.style (), stream);
663 }
664 else
665 {
666 struct value_print_options opts = *options;
667
668 v = value_field_bitfield (type, i, valaddr, offset, val);
669
670 opts.deref_ref = 0;
671 common_val_print (v, stream, recurse + 1, &opts,
672 current_language);
673 }
674 }
675 else
676 {
677 if (TYPE_FIELD_IGNORE (type, i))
678 {
679 fputs_styled ("<optimized out or zero length>",
680 metadata_style.style (), stream);
681 }
682 else if (field_is_static (&TYPE_FIELD (type, i)))
683 {
684 /* struct value *v = value_static_field (type, i);
685 v4.17 specific. */
686 struct value *v;
687
688 v = value_field_bitfield (type, i, valaddr, offset, val);
689
690 if (v == NULL)
691 val_print_optimized_out (NULL, stream);
692 else
693 pascal_object_print_static_field (v, stream, recurse + 1,
694 options);
695 }
696 else
697 {
698 struct value_print_options opts = *options;
699
700 opts.deref_ref = 0;
701 /* val_print (TYPE_FIELD_TYPE (type, i),
702 valaddr + TYPE_FIELD_BITPOS (type, i) / 8,
703 address + TYPE_FIELD_BITPOS (type, i) / 8, 0,
704 stream, format, 0, recurse + 1, pretty); */
705 val_print (TYPE_FIELD_TYPE (type, i),
706 offset + TYPE_FIELD_BITPOS (type, i) / 8,
707 address, stream, recurse + 1, val, &opts,
708 current_language);
709 }
710 }
711 annotate_field_end ();
712 }
713
714 if (dont_print_statmem == 0)
715 {
716 /* Free the space used to deal with the printing
717 of the members from top level. */
718 obstack_free (&dont_print_statmem_obstack, last_dont_print);
719 dont_print_statmem_obstack = tmp_obstack;
720 }
721
722 if (options->prettyformat)
723 {
724 fprintf_filtered (stream, "\n");
725 print_spaces_filtered (2 * recurse, stream);
726 }
727 }
728 fprintf_filtered (stream, "}");
729 }
730
731 /* Special val_print routine to avoid printing multiple copies of virtual
732 baseclasses. */
733
734 static void
735 pascal_object_print_value (struct type *type, const gdb_byte *valaddr,
736 LONGEST offset,
737 CORE_ADDR address, struct ui_file *stream,
738 int recurse,
739 struct value *val,
740 const struct value_print_options *options,
741 struct type **dont_print_vb)
742 {
743 struct type **last_dont_print
744 = (struct type **) obstack_next_free (&dont_print_vb_obstack);
745 struct obstack tmp_obstack = dont_print_vb_obstack;
746 int i, n_baseclasses = TYPE_N_BASECLASSES (type);
747
748 if (dont_print_vb == 0)
749 {
750 /* If we're at top level, carve out a completely fresh
751 chunk of the obstack and use that until this particular
752 invocation returns. */
753 /* Bump up the high-water mark. Now alpha is omega. */
754 obstack_finish (&dont_print_vb_obstack);
755 }
756
757 for (i = 0; i < n_baseclasses; i++)
758 {
759 LONGEST boffset = 0;
760 struct type *baseclass = check_typedef (TYPE_BASECLASS (type, i));
761 const char *basename = TYPE_NAME (baseclass);
762 const gdb_byte *base_valaddr = NULL;
763 LONGEST thisoffset;
764 int skip = 0;
765 gdb::byte_vector buf;
766
767 if (BASETYPE_VIA_VIRTUAL (type, i))
768 {
769 struct type **first_dont_print
770 = (struct type **) obstack_base (&dont_print_vb_obstack);
771
772 int j = (struct type **) obstack_next_free (&dont_print_vb_obstack)
773 - first_dont_print;
774
775 while (--j >= 0)
776 if (baseclass == first_dont_print[j])
777 goto flush_it;
778
779 obstack_ptr_grow (&dont_print_vb_obstack, baseclass);
780 }
781
782 thisoffset = offset;
783
784 try
785 {
786 boffset = baseclass_offset (type, i, valaddr, offset, address, val);
787 }
788 catch (const gdb_exception_error &ex)
789 {
790 if (ex.error == NOT_AVAILABLE_ERROR)
791 skip = -1;
792 else
793 skip = 1;
794 }
795
796 if (skip == 0)
797 {
798 /* The virtual base class pointer might have been clobbered by the
799 user program. Make sure that it still points to a valid memory
800 location. */
801
802 if (boffset < 0 || boffset >= TYPE_LENGTH (type))
803 {
804 buf.resize (TYPE_LENGTH (baseclass));
805
806 base_valaddr = buf.data ();
807 if (target_read_memory (address + boffset, buf.data (),
808 TYPE_LENGTH (baseclass)) != 0)
809 skip = 1;
810 address = address + boffset;
811 thisoffset = 0;
812 boffset = 0;
813 }
814 else
815 base_valaddr = valaddr;
816 }
817
818 if (options->prettyformat)
819 {
820 fprintf_filtered (stream, "\n");
821 print_spaces_filtered (2 * recurse, stream);
822 }
823 fputs_filtered ("<", stream);
824 /* Not sure what the best notation is in the case where there is no
825 baseclass name. */
826
827 fputs_filtered (basename ? basename : "", stream);
828 fputs_filtered ("> = ", stream);
829
830 if (skip < 0)
831 val_print_unavailable (stream);
832 else if (skip > 0)
833 val_print_invalid_address (stream);
834 else
835 pascal_object_print_value_fields (baseclass, base_valaddr,
836 thisoffset + boffset, address,
837 stream, recurse, val, options,
838 (struct type **) obstack_base (&dont_print_vb_obstack),
839 0);
840 fputs_filtered (", ", stream);
841
842 flush_it:
843 ;
844 }
845
846 if (dont_print_vb == 0)
847 {
848 /* Free the space used to deal with the printing
849 of this type from top level. */
850 obstack_free (&dont_print_vb_obstack, last_dont_print);
851 /* Reset watermark so that we can continue protecting
852 ourselves from whatever we were protecting ourselves. */
853 dont_print_vb_obstack = tmp_obstack;
854 }
855 }
856
857 /* Print value of a static member.
858 To avoid infinite recursion when printing a class that contains
859 a static instance of the class, we keep the addresses of all printed
860 static member classes in an obstack and refuse to print them more
861 than once.
862
863 VAL contains the value to print, STREAM, RECURSE, and OPTIONS
864 have the same meanings as in c_val_print. */
865
866 static void
867 pascal_object_print_static_field (struct value *val,
868 struct ui_file *stream,
869 int recurse,
870 const struct value_print_options *options)
871 {
872 struct type *type = value_type (val);
873 struct value_print_options opts;
874
875 if (value_entirely_optimized_out (val))
876 {
877 val_print_optimized_out (val, stream);
878 return;
879 }
880
881 if (TYPE_CODE (type) == TYPE_CODE_STRUCT)
882 {
883 CORE_ADDR *first_dont_print, addr;
884 int i;
885
886 first_dont_print
887 = (CORE_ADDR *) obstack_base (&dont_print_statmem_obstack);
888 i = (CORE_ADDR *) obstack_next_free (&dont_print_statmem_obstack)
889 - first_dont_print;
890
891 while (--i >= 0)
892 {
893 if (value_address (val) == first_dont_print[i])
894 {
895 fputs_styled (_("\
896 <same as static member of an already seen type>"),
897 metadata_style.style (), stream);
898 return;
899 }
900 }
901
902 addr = value_address (val);
903 obstack_grow (&dont_print_statmem_obstack, (char *) &addr,
904 sizeof (CORE_ADDR));
905
906 type = check_typedef (type);
907 pascal_object_print_value_fields (type,
908 value_contents_for_printing (val),
909 value_embedded_offset (val),
910 addr,
911 stream, recurse,
912 val, options, NULL, 1);
913 return;
914 }
915
916 opts = *options;
917 opts.deref_ref = 0;
918 common_val_print (val, stream, recurse, &opts, current_language);
919 }
920
921 void _initialize_pascal_valprint ();
922 void
923 _initialize_pascal_valprint ()
924 {
925 add_setshow_boolean_cmd ("pascal_static-members", class_support,
926 &user_print_options.pascal_static_field_print, _("\
927 Set printing of pascal static members."), _("\
928 Show printing of pascal static members."), NULL,
929 NULL,
930 show_pascal_static_field_print,
931 &setprintlist, &showprintlist);
932 }
This page took 0.0754089999999999 seconds and 4 git commands to generate.