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