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