]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - gdb/ada-valprint.c
Turn value_contents_eq into a method
[thirdparty/binutils-gdb.git] / gdb / ada-valprint.c
1 /* Support for printing Ada values for GDB, the GNU debugger.
2
3 Copyright (C) 1986-2023 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 #include "defs.h"
21 #include <ctype.h>
22 #include "gdbtypes.h"
23 #include "expression.h"
24 #include "value.h"
25 #include "valprint.h"
26 #include "language.h"
27 #include "annotate.h"
28 #include "ada-lang.h"
29 #include "target-float.h"
30 #include "cli/cli-style.h"
31 #include "gdbarch.h"
32
33 static int print_field_values (struct value *, struct value *,
34 struct ui_file *, int,
35 const struct value_print_options *,
36 int, const struct language_defn *);
37
38 \f
39
40 /* Make TYPE unsigned if its range of values includes no negatives. */
41 static void
42 adjust_type_signedness (struct type *type)
43 {
44 if (type != NULL && type->code () == TYPE_CODE_RANGE
45 && type->bounds ()->low.const_val () >= 0)
46 type->set_is_unsigned (true);
47 }
48
49 /* Assuming TYPE is a simple array type, prints its lower bound on STREAM,
50 if non-standard (i.e., other than 1 for numbers, other than lower bound
51 of index type for enumerated type). Returns 1 if something printed,
52 otherwise 0. */
53
54 static int
55 print_optional_low_bound (struct ui_file *stream, struct type *type,
56 const struct value_print_options *options)
57 {
58 struct type *index_type;
59 LONGEST low_bound;
60 LONGEST high_bound;
61
62 if (options->print_array_indexes)
63 return 0;
64
65 if (!get_array_bounds (type, &low_bound, &high_bound))
66 return 0;
67
68 /* If this is an empty array, then don't print the lower bound.
69 That would be confusing, because we would print the lower bound,
70 followed by... nothing! */
71 if (low_bound > high_bound)
72 return 0;
73
74 index_type = type->index_type ();
75
76 while (index_type->code () == TYPE_CODE_RANGE)
77 {
78 /* We need to know what the base type is, in order to do the
79 appropriate check below. Otherwise, if this is a subrange
80 of an enumerated type, where the underlying value of the
81 first element is typically 0, we might test the low bound
82 against the wrong value. */
83 index_type = index_type->target_type ();
84 }
85
86 /* Don't print the lower bound if it's the default one. */
87 switch (index_type->code ())
88 {
89 case TYPE_CODE_BOOL:
90 case TYPE_CODE_CHAR:
91 if (low_bound == 0)
92 return 0;
93 break;
94 case TYPE_CODE_ENUM:
95 if (low_bound == 0)
96 return 0;
97 low_bound = index_type->field (low_bound).loc_enumval ();
98 break;
99 case TYPE_CODE_UNDEF:
100 index_type = NULL;
101 /* FALL THROUGH */
102 default:
103 if (low_bound == 1)
104 return 0;
105 break;
106 }
107
108 ada_print_scalar (index_type, low_bound, stream);
109 gdb_printf (stream, " => ");
110 return 1;
111 }
112
113 /* Version of val_print_array_elements for GNAT-style packed arrays.
114 Prints elements of packed array of type TYPE from VALADDR on
115 STREAM. Formats according to OPTIONS and separates with commas.
116 RECURSE is the recursion (nesting) level. TYPE must have been
117 decoded (as by ada_coerce_to_simple_array). */
118
119 static void
120 val_print_packed_array_elements (struct type *type, const gdb_byte *valaddr,
121 int offset, struct ui_file *stream,
122 int recurse,
123 const struct value_print_options *options)
124 {
125 unsigned int i;
126 unsigned int things_printed = 0;
127 unsigned len;
128 struct type *elttype, *index_type;
129 unsigned long bitsize = TYPE_FIELD_BITSIZE (type, 0);
130 LONGEST low = 0;
131
132 scoped_value_mark mark;
133
134 elttype = type->target_type ();
135 index_type = type->index_type ();
136
137 {
138 LONGEST high;
139
140 if (!get_discrete_bounds (index_type, &low, &high))
141 len = 1;
142 else if (low > high)
143 {
144 /* The array length should normally be HIGH_POS - LOW_POS + 1.
145 But in Ada we allow LOW_POS to be greater than HIGH_POS for
146 empty arrays. In that situation, the array length is just zero,
147 not negative! */
148 len = 0;
149 }
150 else
151 len = high - low + 1;
152 }
153
154 if (index_type->code () == TYPE_CODE_RANGE)
155 index_type = index_type->target_type ();
156
157 i = 0;
158 annotate_array_section_begin (i, elttype);
159
160 while (i < len && things_printed < options->print_max)
161 {
162 struct value *v0, *v1;
163 int i0;
164
165 if (i != 0)
166 {
167 if (options->prettyformat_arrays)
168 {
169 gdb_printf (stream, ",\n");
170 print_spaces (2 + 2 * recurse, stream);
171 }
172 else
173 {
174 gdb_printf (stream, ", ");
175 }
176 }
177 else if (options->prettyformat_arrays)
178 {
179 gdb_printf (stream, "\n");
180 print_spaces (2 + 2 * recurse, stream);
181 }
182 stream->wrap_here (2 + 2 * recurse);
183 maybe_print_array_index (index_type, i + low, stream, options);
184
185 i0 = i;
186 v0 = ada_value_primitive_packed_val (NULL, valaddr + offset,
187 (i0 * bitsize) / HOST_CHAR_BIT,
188 (i0 * bitsize) % HOST_CHAR_BIT,
189 bitsize, elttype);
190 while (1)
191 {
192 i += 1;
193 if (i >= len)
194 break;
195 v1 = ada_value_primitive_packed_val (NULL, valaddr + offset,
196 (i * bitsize) / HOST_CHAR_BIT,
197 (i * bitsize) % HOST_CHAR_BIT,
198 bitsize, elttype);
199 if (check_typedef (v0->type ())->length ()
200 != check_typedef (v1->type ())->length ())
201 break;
202 if (!v0->contents_eq (v0->embedded_offset (),
203 v1, v1->embedded_offset (),
204 check_typedef (v0->type ())->length ()))
205 break;
206 }
207
208 if (i - i0 > options->repeat_count_threshold)
209 {
210 struct value_print_options opts = *options;
211
212 opts.deref_ref = false;
213 common_val_print (v0, stream, recurse + 1, &opts, current_language);
214 annotate_elt_rep (i - i0);
215 gdb_printf (stream, _(" %p[<repeats %u times>%p]"),
216 metadata_style.style ().ptr (), i - i0, nullptr);
217 annotate_elt_rep_end ();
218
219 }
220 else
221 {
222 int j;
223 struct value_print_options opts = *options;
224
225 opts.deref_ref = false;
226 for (j = i0; j < i; j += 1)
227 {
228 if (j > i0)
229 {
230 if (options->prettyformat_arrays)
231 {
232 gdb_printf (stream, ",\n");
233 print_spaces (2 + 2 * recurse, stream);
234 }
235 else
236 {
237 gdb_printf (stream, ", ");
238 }
239 stream->wrap_here (2 + 2 * recurse);
240 maybe_print_array_index (index_type, j + low,
241 stream, options);
242 }
243 common_val_print (v0, stream, recurse + 1, &opts,
244 current_language);
245 annotate_elt ();
246 }
247 }
248 things_printed += i - i0;
249 }
250 annotate_array_section_end ();
251 if (i < len)
252 {
253 gdb_printf (stream, "...");
254 }
255 }
256
257 /* Print the character C on STREAM as part of the contents of a literal
258 string whose delimiter is QUOTER. TYPE_LEN is the length in bytes
259 of the character. */
260
261 void
262 ada_emit_char (int c, struct type *type, struct ui_file *stream,
263 int quoter, int type_len)
264 {
265 /* If this character fits in the normal ASCII range, and is
266 a printable character, then print the character as if it was
267 an ASCII character, even if this is a wide character.
268 The UCHAR_MAX check is necessary because the isascii function
269 requires that its argument have a value of an unsigned char,
270 or EOF (EOF is obviously not printable). */
271 if (c <= UCHAR_MAX && isascii (c) && isprint (c))
272 {
273 if (c == quoter && c == '"')
274 gdb_printf (stream, "\"\"");
275 else
276 gdb_printf (stream, "%c", c);
277 }
278 else
279 {
280 /* Follow GNAT's lead here and only use 6 digits for
281 wide_wide_character. */
282 gdb_printf (stream, "[\"%0*x\"]", std::min (6, type_len * 2), c);
283 }
284 }
285
286 /* Character #I of STRING, given that TYPE_LEN is the size in bytes
287 of a character. */
288
289 static int
290 char_at (const gdb_byte *string, int i, int type_len,
291 enum bfd_endian byte_order)
292 {
293 if (type_len == 1)
294 return string[i];
295 else
296 return (int) extract_unsigned_integer (string + type_len * i,
297 type_len, byte_order);
298 }
299
300 /* Print a floating-point value of type TYPE, pointed to in GDB by
301 VALADDR, on STREAM. Use Ada formatting conventions: there must be
302 a decimal point, and at least one digit before and after the
303 point. We use the GNAT format for NaNs and infinities. */
304
305 static void
306 ada_print_floating (const gdb_byte *valaddr, struct type *type,
307 struct ui_file *stream)
308 {
309 string_file tmp_stream;
310
311 print_floating (valaddr, type, &tmp_stream);
312
313 std::string s = tmp_stream.release ();
314 size_t skip_count = 0;
315
316 /* Don't try to modify a result representing an error. */
317 if (s[0] == '<')
318 {
319 gdb_puts (s.c_str (), stream);
320 return;
321 }
322
323 /* Modify for Ada rules. */
324
325 size_t pos = s.find ("inf");
326 if (pos == std::string::npos)
327 pos = s.find ("Inf");
328 if (pos == std::string::npos)
329 pos = s.find ("INF");
330 if (pos != std::string::npos)
331 s.replace (pos, 3, "Inf");
332
333 if (pos == std::string::npos)
334 {
335 pos = s.find ("nan");
336 if (pos == std::string::npos)
337 pos = s.find ("NaN");
338 if (pos == std::string::npos)
339 pos = s.find ("Nan");
340 if (pos != std::string::npos)
341 {
342 s[pos] = s[pos + 2] = 'N';
343 if (s[0] == '-')
344 skip_count = 1;
345 }
346 }
347
348 if (pos == std::string::npos
349 && s.find ('.') == std::string::npos)
350 {
351 pos = s.find ('e');
352 if (pos == std::string::npos)
353 gdb_printf (stream, "%s.0", s.c_str ());
354 else
355 gdb_printf (stream, "%.*s.0%s", (int) pos, s.c_str (), &s[pos]);
356 }
357 else
358 gdb_printf (stream, "%s", &s[skip_count]);
359 }
360
361 void
362 ada_printchar (int c, struct type *type, struct ui_file *stream)
363 {
364 gdb_puts ("'", stream);
365 ada_emit_char (c, type, stream, '\'', type->length ());
366 gdb_puts ("'", stream);
367 }
368
369 /* [From print_type_scalar in typeprint.c]. Print VAL on STREAM in a
370 form appropriate for TYPE, if non-NULL. If TYPE is NULL, print VAL
371 like a default signed integer. */
372
373 void
374 ada_print_scalar (struct type *type, LONGEST val, struct ui_file *stream)
375 {
376 unsigned int i;
377 unsigned len;
378
379 if (!type)
380 {
381 print_longest (stream, 'd', 0, val);
382 return;
383 }
384
385 type = ada_check_typedef (type);
386
387 switch (type->code ())
388 {
389
390 case TYPE_CODE_ENUM:
391 len = type->num_fields ();
392 for (i = 0; i < len; i++)
393 {
394 if (type->field (i).loc_enumval () == val)
395 {
396 break;
397 }
398 }
399 if (i < len)
400 {
401 fputs_styled (ada_enum_name (type->field (i).name ()),
402 variable_name_style.style (), stream);
403 }
404 else
405 {
406 print_longest (stream, 'd', 0, val);
407 }
408 break;
409
410 case TYPE_CODE_INT:
411 print_longest (stream, type->is_unsigned () ? 'u' : 'd', 0, val);
412 break;
413
414 case TYPE_CODE_CHAR:
415 current_language->printchar (val, type, stream);
416 break;
417
418 case TYPE_CODE_BOOL:
419 gdb_printf (stream, val ? "true" : "false");
420 break;
421
422 case TYPE_CODE_RANGE:
423 ada_print_scalar (type->target_type (), val, stream);
424 return;
425
426 case TYPE_CODE_UNDEF:
427 case TYPE_CODE_PTR:
428 case TYPE_CODE_ARRAY:
429 case TYPE_CODE_STRUCT:
430 case TYPE_CODE_UNION:
431 case TYPE_CODE_FUNC:
432 case TYPE_CODE_FLT:
433 case TYPE_CODE_VOID:
434 case TYPE_CODE_SET:
435 case TYPE_CODE_STRING:
436 case TYPE_CODE_ERROR:
437 case TYPE_CODE_MEMBERPTR:
438 case TYPE_CODE_METHODPTR:
439 case TYPE_CODE_METHOD:
440 case TYPE_CODE_REF:
441 warning (_("internal error: unhandled type in ada_print_scalar"));
442 break;
443
444 default:
445 error (_("Invalid type code in symbol table."));
446 }
447 }
448
449 /* Print the character string STRING, printing at most LENGTH characters.
450 Printing stops early if the number hits print_max; repeat counts
451 are printed as appropriate. Print ellipses at the end if we
452 had to stop before printing LENGTH characters, or if FORCE_ELLIPSES.
453 TYPE_LEN is the length (1 or 2) of the character type. */
454
455 static void
456 printstr (struct ui_file *stream, struct type *elttype, const gdb_byte *string,
457 unsigned int length, int force_ellipses, int type_len,
458 const struct value_print_options *options)
459 {
460 enum bfd_endian byte_order = type_byte_order (elttype);
461 unsigned int i;
462 unsigned int things_printed = 0;
463 int in_quotes = 0;
464 int need_comma = 0;
465
466 if (length == 0)
467 {
468 gdb_puts ("\"\"", stream);
469 return;
470 }
471
472 unsigned int print_max_chars = get_print_max_chars (options);
473 for (i = 0; i < length && things_printed < print_max_chars; i += 1)
474 {
475 /* Position of the character we are examining
476 to see whether it is repeated. */
477 unsigned int rep1;
478 /* Number of repetitions we have detected so far. */
479 unsigned int reps;
480
481 QUIT;
482
483 if (need_comma)
484 {
485 gdb_puts (", ", stream);
486 need_comma = 0;
487 }
488
489 rep1 = i + 1;
490 reps = 1;
491 while (rep1 < length
492 && char_at (string, rep1, type_len, byte_order)
493 == char_at (string, i, type_len, byte_order))
494 {
495 rep1 += 1;
496 reps += 1;
497 }
498
499 if (reps > options->repeat_count_threshold)
500 {
501 if (in_quotes)
502 {
503 gdb_puts ("\", ", stream);
504 in_quotes = 0;
505 }
506 gdb_puts ("'", stream);
507 ada_emit_char (char_at (string, i, type_len, byte_order),
508 elttype, stream, '\'', type_len);
509 gdb_puts ("'", stream);
510 gdb_printf (stream, _(" %p[<repeats %u times>%p]"),
511 metadata_style.style ().ptr (), reps, nullptr);
512 i = rep1 - 1;
513 things_printed += options->repeat_count_threshold;
514 need_comma = 1;
515 }
516 else
517 {
518 if (!in_quotes)
519 {
520 gdb_puts ("\"", stream);
521 in_quotes = 1;
522 }
523 ada_emit_char (char_at (string, i, type_len, byte_order),
524 elttype, stream, '"', type_len);
525 things_printed += 1;
526 }
527 }
528
529 /* Terminate the quotes if necessary. */
530 if (in_quotes)
531 gdb_puts ("\"", stream);
532
533 if (force_ellipses || i < length)
534 gdb_puts ("...", stream);
535 }
536
537 void
538 ada_printstr (struct ui_file *stream, struct type *type,
539 const gdb_byte *string, unsigned int length,
540 const char *encoding, int force_ellipses,
541 const struct value_print_options *options)
542 {
543 printstr (stream, type, string, length, force_ellipses, type->length (),
544 options);
545 }
546
547 static int
548 print_variant_part (struct value *value, int field_num,
549 struct value *outer_value,
550 struct ui_file *stream, int recurse,
551 const struct value_print_options *options,
552 int comma_needed,
553 const struct language_defn *language)
554 {
555 struct type *type = value->type ();
556 struct type *var_type = type->field (field_num).type ();
557 int which = ada_which_variant_applies (var_type, outer_value);
558
559 if (which < 0)
560 return 0;
561
562 struct value *variant_field = value_field (value, field_num);
563 struct value *active_component = value_field (variant_field, which);
564 return print_field_values (active_component, outer_value, stream, recurse,
565 options, comma_needed, language);
566 }
567
568 /* Print out fields of VALUE.
569
570 STREAM, RECURSE, and OPTIONS have the same meanings as in
571 ada_print_value and ada_value_print.
572
573 OUTER_VALUE gives the enclosing record (used to get discriminant
574 values when printing variant parts).
575
576 COMMA_NEEDED is 1 if fields have been printed at the current recursion
577 level, so that a comma is needed before any field printed by this
578 call.
579
580 Returns 1 if COMMA_NEEDED or any fields were printed. */
581
582 static int
583 print_field_values (struct value *value, struct value *outer_value,
584 struct ui_file *stream, int recurse,
585 const struct value_print_options *options,
586 int comma_needed,
587 const struct language_defn *language)
588 {
589 int i, len;
590
591 struct type *type = value->type ();
592 len = type->num_fields ();
593
594 for (i = 0; i < len; i += 1)
595 {
596 if (ada_is_ignored_field (type, i))
597 continue;
598
599 if (ada_is_wrapper_field (type, i))
600 {
601 struct value *field_val = ada_value_primitive_field (value, 0,
602 i, type);
603 comma_needed =
604 print_field_values (field_val, field_val,
605 stream, recurse, options,
606 comma_needed, language);
607 continue;
608 }
609 else if (ada_is_variant_part (type, i))
610 {
611 comma_needed =
612 print_variant_part (value, i, outer_value, stream, recurse,
613 options, comma_needed, language);
614 continue;
615 }
616
617 if (comma_needed)
618 gdb_printf (stream, ", ");
619 comma_needed = 1;
620
621 if (options->prettyformat)
622 {
623 gdb_printf (stream, "\n");
624 print_spaces (2 + 2 * recurse, stream);
625 }
626 else
627 {
628 stream->wrap_here (2 + 2 * recurse);
629 }
630
631 annotate_field_begin (type->field (i).type ());
632 gdb_printf (stream, "%.*s",
633 ada_name_prefix_len (type->field (i).name ()),
634 type->field (i).name ());
635 annotate_field_name_end ();
636 gdb_puts (" => ", stream);
637 annotate_field_value ();
638
639 if (TYPE_FIELD_PACKED (type, i))
640 {
641 /* Bitfields require special handling, especially due to byte
642 order problems. */
643 if (HAVE_CPLUS_STRUCT (type) && TYPE_FIELD_IGNORE (type, i))
644 {
645 fputs_styled (_("<optimized out or zero length>"),
646 metadata_style.style (), stream);
647 }
648 else
649 {
650 struct value *v;
651 int bit_pos = type->field (i).loc_bitpos ();
652 int bit_size = TYPE_FIELD_BITSIZE (type, i);
653 struct value_print_options opts;
654
655 adjust_type_signedness (type->field (i).type ());
656 v = ada_value_primitive_packed_val
657 (value, nullptr,
658 bit_pos / HOST_CHAR_BIT,
659 bit_pos % HOST_CHAR_BIT,
660 bit_size, type->field (i).type ());
661 opts = *options;
662 opts.deref_ref = false;
663 common_val_print (v, stream, recurse + 1, &opts, language);
664 }
665 }
666 else
667 {
668 struct value_print_options opts = *options;
669
670 opts.deref_ref = false;
671
672 struct value *v = value_field (value, i);
673 common_val_print (v, stream, recurse + 1, &opts, language);
674 }
675 annotate_field_end ();
676 }
677
678 return comma_needed;
679 }
680
681 /* Implement Ada val_print'ing for the case where TYPE is
682 a TYPE_CODE_ARRAY of characters. */
683
684 static void
685 ada_val_print_string (struct type *type, const gdb_byte *valaddr,
686 int offset_aligned,
687 struct ui_file *stream, int recurse,
688 const struct value_print_options *options)
689 {
690 enum bfd_endian byte_order = type_byte_order (type);
691 struct type *elttype = type->target_type ();
692 unsigned int eltlen;
693 unsigned int len;
694
695 /* We know that ELTTYPE cannot possibly be null, because we assume
696 that we're called only when TYPE is a string-like type.
697 Similarly, the size of ELTTYPE should also be non-null, since
698 it's a character-like type. */
699 gdb_assert (elttype != NULL);
700 gdb_assert (elttype->length () != 0);
701
702 eltlen = elttype->length ();
703 len = type->length () / eltlen;
704
705 /* If requested, look for the first null char and only print
706 elements up to it. */
707 if (options->stop_print_at_null)
708 {
709 unsigned int print_max_chars = get_print_max_chars (options);
710 int temp_len;
711
712 /* Look for a NULL char. */
713 for (temp_len = 0;
714 (temp_len < len
715 && temp_len < print_max_chars
716 && char_at (valaddr + offset_aligned,
717 temp_len, eltlen, byte_order) != 0);
718 temp_len += 1);
719 len = temp_len;
720 }
721
722 printstr (stream, elttype, valaddr + offset_aligned, len, 0,
723 eltlen, options);
724 }
725
726 /* Implement Ada value_print'ing for the case where TYPE is a
727 TYPE_CODE_PTR. */
728
729 static void
730 ada_value_print_ptr (struct value *val,
731 struct ui_file *stream, int recurse,
732 const struct value_print_options *options)
733 {
734 if (!options->format
735 && val->type ()->target_type ()->code () == TYPE_CODE_INT
736 && val->type ()->target_type ()->length () == 0)
737 {
738 gdb_puts ("null", stream);
739 return;
740 }
741
742 common_val_print (val, stream, recurse, options, language_def (language_c));
743
744 struct type *type = ada_check_typedef (val->type ());
745 if (ada_is_tag_type (type))
746 {
747 gdb::unique_xmalloc_ptr<char> name = ada_tag_name (val);
748
749 if (name != NULL)
750 gdb_printf (stream, " (%s)", name.get ());
751 }
752 }
753
754 /* Implement Ada val_print'ing for the case where TYPE is
755 a TYPE_CODE_INT or TYPE_CODE_RANGE. */
756
757 static void
758 ada_value_print_num (struct value *val, struct ui_file *stream, int recurse,
759 const struct value_print_options *options)
760 {
761 struct type *type = ada_check_typedef (val->type ());
762 const gdb_byte *valaddr = value_contents_for_printing (val).data ();
763
764 if (type->code () == TYPE_CODE_RANGE
765 && (type->target_type ()->code () == TYPE_CODE_ENUM
766 || type->target_type ()->code () == TYPE_CODE_BOOL
767 || type->target_type ()->code () == TYPE_CODE_CHAR))
768 {
769 /* For enum-valued ranges, we want to recurse, because we'll end
770 up printing the constant's name rather than its numeric
771 value. Character and fixed-point types are also printed
772 differently, so recuse for those as well. */
773 struct type *target_type = type->target_type ();
774 val = value_cast (target_type, val);
775 common_val_print (val, stream, recurse + 1, options,
776 language_def (language_ada));
777 return;
778 }
779 else
780 {
781 int format = (options->format ? options->format
782 : options->output_format);
783
784 if (format)
785 {
786 struct value_print_options opts = *options;
787
788 opts.format = format;
789 value_print_scalar_formatted (val, &opts, 0, stream);
790 }
791 else if (ada_is_system_address_type (type))
792 {
793 /* FIXME: We want to print System.Address variables using
794 the same format as for any access type. But for some
795 reason GNAT encodes the System.Address type as an int,
796 so we have to work-around this deficiency by handling
797 System.Address values as a special case. */
798
799 struct gdbarch *gdbarch = type->arch ();
800 struct type *ptr_type = builtin_type (gdbarch)->builtin_data_ptr;
801 CORE_ADDR addr = extract_typed_address (valaddr, ptr_type);
802
803 gdb_printf (stream, "(");
804 type_print (type, "", stream, -1);
805 gdb_printf (stream, ") ");
806 gdb_puts (paddress (gdbarch, addr), stream);
807 }
808 else
809 {
810 value_print_scalar_formatted (val, options, 0, stream);
811 if (ada_is_character_type (type))
812 {
813 LONGEST c;
814
815 gdb_puts (" ", stream);
816 c = unpack_long (type, valaddr);
817 ada_printchar (c, type, stream);
818 }
819 }
820 return;
821 }
822 }
823
824 /* Implement Ada val_print'ing for the case where TYPE is
825 a TYPE_CODE_ENUM. */
826
827 static void
828 ada_val_print_enum (struct value *value, struct ui_file *stream, int recurse,
829 const struct value_print_options *options)
830 {
831 int i;
832 unsigned int len;
833 LONGEST val;
834
835 if (options->format)
836 {
837 value_print_scalar_formatted (value, options, 0, stream);
838 return;
839 }
840
841 struct type *type = ada_check_typedef (value->type ());
842 const gdb_byte *valaddr = value_contents_for_printing (value).data ();
843 int offset_aligned = ada_aligned_value_addr (type, valaddr) - valaddr;
844
845 len = type->num_fields ();
846 val = unpack_long (type, valaddr + offset_aligned);
847 for (i = 0; i < len; i++)
848 {
849 QUIT;
850 if (val == type->field (i).loc_enumval ())
851 break;
852 }
853
854 if (i < len)
855 {
856 const char *name = ada_enum_name (type->field (i).name ());
857
858 if (name[0] == '\'')
859 gdb_printf (stream, "%ld %ps", (long) val,
860 styled_string (variable_name_style.style (),
861 name));
862 else
863 fputs_styled (name, variable_name_style.style (), stream);
864 }
865 else
866 print_longest (stream, 'd', 0, val);
867 }
868
869 /* Implement Ada val_print'ing for the case where the type is
870 TYPE_CODE_STRUCT or TYPE_CODE_UNION. */
871
872 static void
873 ada_val_print_struct_union (struct value *value,
874 struct ui_file *stream,
875 int recurse,
876 const struct value_print_options *options)
877 {
878 if (ada_is_bogus_array_descriptor (value->type ()))
879 {
880 gdb_printf (stream, "(...?)");
881 return;
882 }
883
884 gdb_printf (stream, "(");
885
886 if (print_field_values (value, value, stream, recurse, options,
887 0, language_def (language_ada)) != 0
888 && options->prettyformat)
889 {
890 gdb_printf (stream, "\n");
891 print_spaces (2 * recurse, stream);
892 }
893
894 gdb_printf (stream, ")");
895 }
896
897 /* Implement Ada value_print'ing for the case where TYPE is a
898 TYPE_CODE_ARRAY. */
899
900 static void
901 ada_value_print_array (struct value *val, struct ui_file *stream, int recurse,
902 const struct value_print_options *options)
903 {
904 struct type *type = ada_check_typedef (val->type ());
905
906 /* For an array of characters, print with string syntax. */
907 if (ada_is_string_type (type)
908 && (options->format == 0 || options->format == 's'))
909 {
910 const gdb_byte *valaddr = value_contents_for_printing (val).data ();
911 int offset_aligned = ada_aligned_value_addr (type, valaddr) - valaddr;
912
913 ada_val_print_string (type, valaddr, offset_aligned, stream, recurse,
914 options);
915 return;
916 }
917
918 gdb_printf (stream, "(");
919 print_optional_low_bound (stream, type, options);
920
921 if (value_entirely_optimized_out (val))
922 val_print_optimized_out (val, stream);
923 else if (TYPE_FIELD_BITSIZE (type, 0) > 0)
924 {
925 const gdb_byte *valaddr = value_contents_for_printing (val).data ();
926 int offset_aligned = ada_aligned_value_addr (type, valaddr) - valaddr;
927 val_print_packed_array_elements (type, valaddr, offset_aligned,
928 stream, recurse, options);
929 }
930 else
931 value_print_array_elements (val, stream, recurse, options, 0);
932 gdb_printf (stream, ")");
933 }
934
935 /* Implement Ada val_print'ing for the case where TYPE is
936 a TYPE_CODE_REF. */
937
938 static void
939 ada_val_print_ref (struct type *type, const gdb_byte *valaddr,
940 int offset, int offset_aligned, CORE_ADDR address,
941 struct ui_file *stream, int recurse,
942 struct value *original_value,
943 const struct value_print_options *options)
944 {
945 /* For references, the debugger is expected to print the value as
946 an address if DEREF_REF is null. But printing an address in place
947 of the object value would be confusing to an Ada programmer.
948 So, for Ada values, we print the actual dereferenced value
949 regardless. */
950 struct type *elttype = check_typedef (type->target_type ());
951 struct value *deref_val;
952 CORE_ADDR deref_val_int;
953
954 if (elttype->code () == TYPE_CODE_UNDEF)
955 {
956 fputs_styled ("<ref to undefined type>", metadata_style.style (),
957 stream);
958 return;
959 }
960
961 deref_val = coerce_ref_if_computed (original_value);
962 if (deref_val)
963 {
964 if (ada_is_tagged_type (deref_val->type (), 1))
965 deref_val = ada_tag_value_at_base_address (deref_val);
966
967 common_val_print (deref_val, stream, recurse + 1, options,
968 language_def (language_ada));
969 return;
970 }
971
972 deref_val_int = unpack_pointer (type, valaddr + offset_aligned);
973 if (deref_val_int == 0)
974 {
975 gdb_puts ("(null)", stream);
976 return;
977 }
978
979 deref_val
980 = ada_value_ind (value_from_pointer (lookup_pointer_type (elttype),
981 deref_val_int));
982 if (ada_is_tagged_type (deref_val->type (), 1))
983 deref_val = ada_tag_value_at_base_address (deref_val);
984
985 if (deref_val->lazy ())
986 deref_val->fetch_lazy ();
987
988 common_val_print (deref_val, stream, recurse + 1,
989 options, language_def (language_ada));
990 }
991
992 /* See the comment on ada_value_print. This function differs in that
993 it does not catch evaluation errors (leaving that to its
994 caller). */
995
996 void
997 ada_value_print_inner (struct value *val, struct ui_file *stream, int recurse,
998 const struct value_print_options *options)
999 {
1000 struct type *type = ada_check_typedef (val->type ());
1001
1002 if (ada_is_array_descriptor_type (type)
1003 || (ada_is_constrained_packed_array_type (type)
1004 && type->code () != TYPE_CODE_PTR))
1005 {
1006 /* If this is a reference, coerce it now. This helps taking
1007 care of the case where ADDRESS is meaningless because
1008 original_value was not an lval. */
1009 val = coerce_ref (val);
1010 val = ada_get_decoded_value (val);
1011 if (val == nullptr)
1012 {
1013 gdb_assert (type->code () == TYPE_CODE_TYPEDEF);
1014 gdb_printf (stream, "0x0");
1015 return;
1016 }
1017 }
1018 else
1019 val = ada_to_fixed_value (val);
1020
1021 type = val->type ();
1022 struct type *saved_type = type;
1023
1024 const gdb_byte *valaddr = value_contents_for_printing (val).data ();
1025 CORE_ADDR address = val->address ();
1026 gdb::array_view<const gdb_byte> view
1027 = gdb::make_array_view (valaddr, type->length ());
1028 type = ada_check_typedef (resolve_dynamic_type (type, view, address));
1029 if (type != saved_type)
1030 {
1031 val = value_copy (val);
1032 val->deprecated_set_type (type);
1033 }
1034
1035 if (is_fixed_point_type (type))
1036 type = type->fixed_point_type_base_type ();
1037
1038 switch (type->code ())
1039 {
1040 default:
1041 common_val_print (val, stream, recurse, options,
1042 language_def (language_c));
1043 break;
1044
1045 case TYPE_CODE_PTR:
1046 ada_value_print_ptr (val, stream, recurse, options);
1047 break;
1048
1049 case TYPE_CODE_INT:
1050 case TYPE_CODE_RANGE:
1051 ada_value_print_num (val, stream, recurse, options);
1052 break;
1053
1054 case TYPE_CODE_ENUM:
1055 ada_val_print_enum (val, stream, recurse, options);
1056 break;
1057
1058 case TYPE_CODE_FLT:
1059 if (options->format)
1060 {
1061 common_val_print (val, stream, recurse, options,
1062 language_def (language_c));
1063 break;
1064 }
1065
1066 ada_print_floating (valaddr, type, stream);
1067 break;
1068
1069 case TYPE_CODE_UNION:
1070 case TYPE_CODE_STRUCT:
1071 ada_val_print_struct_union (val, stream, recurse, options);
1072 break;
1073
1074 case TYPE_CODE_ARRAY:
1075 ada_value_print_array (val, stream, recurse, options);
1076 return;
1077
1078 case TYPE_CODE_REF:
1079 ada_val_print_ref (type, valaddr, 0, 0,
1080 address, stream, recurse, val,
1081 options);
1082 break;
1083 }
1084 }
1085
1086 void
1087 ada_value_print (struct value *val0, struct ui_file *stream,
1088 const struct value_print_options *options)
1089 {
1090 struct value *val = ada_to_fixed_value (val0);
1091 struct type *type = ada_check_typedef (val->type ());
1092 struct value_print_options opts;
1093
1094 /* If it is a pointer, indicate what it points to; but not for
1095 "void *" pointers. */
1096 if (type->code () == TYPE_CODE_PTR
1097 && !(type->target_type ()->code () == TYPE_CODE_INT
1098 && type->target_type ()->length () == 0))
1099 {
1100 /* Hack: don't print (char *) for char strings. Their
1101 type is indicated by the quoted string anyway. */
1102 if (type->target_type ()->length () != sizeof (char)
1103 || type->target_type ()->code () != TYPE_CODE_INT
1104 || type->target_type ()->is_unsigned ())
1105 {
1106 gdb_printf (stream, "(");
1107 type_print (type, "", stream, -1);
1108 gdb_printf (stream, ") ");
1109 }
1110 }
1111 else if (ada_is_array_descriptor_type (type))
1112 {
1113 /* We do not print the type description unless TYPE is an array
1114 access type (this is encoded by the compiler as a typedef to
1115 a fat pointer - hence the check against TYPE_CODE_TYPEDEF). */
1116 if (type->code () == TYPE_CODE_TYPEDEF)
1117 {
1118 gdb_printf (stream, "(");
1119 type_print (type, "", stream, -1);
1120 gdb_printf (stream, ") ");
1121 }
1122 }
1123 else if (ada_is_bogus_array_descriptor (type))
1124 {
1125 gdb_printf (stream, "(");
1126 type_print (type, "", stream, -1);
1127 gdb_printf (stream, ") (...?)");
1128 return;
1129 }
1130
1131 opts = *options;
1132 opts.deref_ref = true;
1133 common_val_print (val, stream, 0, &opts, current_language);
1134 }