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