Fix null pointer dereference in process_debug_info()
[binutils-gdb.git] / gdb / ada-valprint.c
blob6acfb9a48b69676cbb770283578faf472625b2f6
1 /* Support for printing Ada values for GDB, the GNU debugger.
3 Copyright (C) 1986-2024 Free Software Foundation, Inc.
5 This file is part of GDB.
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.
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.
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/>. */
20 #include <ctype.h>
21 #include "gdbtypes.h"
22 #include "expression.h"
23 #include "value.h"
24 #include "valprint.h"
25 #include "language.h"
26 #include "annotate.h"
27 #include "ada-lang.h"
28 #include "target-float.h"
29 #include "cli/cli-style.h"
30 #include "gdbarch.h"
32 static int print_field_values (struct value *, struct value *,
33 struct ui_file *, int,
34 const struct value_print_options *,
35 int, const struct language_defn *);
39 /* Assuming TYPE is a simple array type, prints its lower bound on STREAM,
40 if non-standard (i.e., other than 1 for numbers, other than lower bound
41 of index type for enumerated type). Returns 1 if something printed,
42 otherwise 0. */
44 static int
45 print_optional_low_bound (struct ui_file *stream, struct type *type,
46 const struct value_print_options *options)
48 struct type *index_type;
49 LONGEST low_bound;
50 LONGEST high_bound;
52 if (options->print_array_indexes)
53 return 0;
55 if (!get_array_bounds (type, &low_bound, &high_bound))
56 return 0;
58 /* If this is an empty array, then don't print the lower bound.
59 That would be confusing, because we would print the lower bound,
60 followed by... nothing! */
61 if (low_bound > high_bound)
62 return 0;
64 index_type = type->index_type ();
66 while (index_type->code () == TYPE_CODE_RANGE)
68 /* We need to know what the base type is, in order to do the
69 appropriate check below. Otherwise, if this is a subrange
70 of an enumerated type, where the underlying value of the
71 first element is typically 0, we might test the low bound
72 against the wrong value. */
73 index_type = index_type->target_type ();
76 /* Don't print the lower bound if it's the default one. */
77 switch (index_type->code ())
79 case TYPE_CODE_BOOL:
80 case TYPE_CODE_CHAR:
81 if (low_bound == 0)
82 return 0;
83 break;
84 case TYPE_CODE_ENUM:
85 if (low_bound == 0)
86 return 0;
87 low_bound = index_type->field (low_bound).loc_enumval ();
88 break;
89 case TYPE_CODE_UNDEF:
90 index_type = NULL;
91 [[fallthrough]];
92 default:
93 if (low_bound == 1)
94 return 0;
95 break;
98 ada_print_scalar (index_type, low_bound, stream);
99 gdb_printf (stream, " => ");
100 return 1;
103 /* Version of val_print_array_elements for GNAT-style packed arrays.
104 Prints elements of packed array of type TYPE from VALADDR on
105 STREAM. Formats according to OPTIONS and separates with commas.
106 RECURSE is the recursion (nesting) level. TYPE must have been
107 decoded (as by ada_coerce_to_simple_array). */
109 static void
110 val_print_packed_array_elements (struct type *type, const gdb_byte *valaddr,
111 int offset, struct ui_file *stream,
112 int recurse,
113 const struct value_print_options *options)
115 unsigned int i;
116 unsigned int things_printed = 0;
117 unsigned len;
118 struct type *elttype, *index_type;
119 unsigned long bitsize = type->field (0).bitsize ();
120 LONGEST low = 0;
122 scoped_value_mark mark;
124 elttype = type->target_type ();
125 index_type = type->index_type ();
128 LONGEST high;
130 if (!get_discrete_bounds (index_type, &low, &high))
131 len = 1;
132 else if (low > high)
134 /* The array length should normally be HIGH_POS - LOW_POS + 1.
135 But in Ada we allow LOW_POS to be greater than HIGH_POS for
136 empty arrays. In that situation, the array length is just zero,
137 not negative! */
138 len = 0;
140 else
141 len = high - low + 1;
144 if (index_type->code () == TYPE_CODE_RANGE)
145 index_type = index_type->target_type ();
147 i = 0;
148 annotate_array_section_begin (i, elttype);
150 while (i < len && things_printed < options->print_max)
152 /* Both this outer loop and the inner loop that checks for
153 duplicates may allocate many values. To avoid using too much
154 memory, both spots release values as they work. */
155 scoped_value_mark outer_free_values;
157 struct value *v0, *v1;
158 int i0;
160 if (i != 0)
162 if (options->prettyformat_arrays)
164 gdb_printf (stream, ",\n");
165 print_spaces (2 + 2 * recurse, stream);
167 else
169 gdb_printf (stream, ", ");
172 else if (options->prettyformat_arrays)
174 gdb_printf (stream, "\n");
175 print_spaces (2 + 2 * recurse, stream);
177 stream->wrap_here (2 + 2 * recurse);
178 maybe_print_array_index (index_type, i + low, stream, options);
180 i0 = i;
181 v0 = ada_value_primitive_packed_val (NULL, valaddr + offset,
182 (i0 * bitsize) / HOST_CHAR_BIT,
183 (i0 * bitsize) % HOST_CHAR_BIT,
184 bitsize, elttype);
185 while (1)
187 /* Make sure to free any values in the inner loop. */
188 scoped_value_mark free_values;
190 i += 1;
191 if (i >= len)
192 break;
193 v1 = ada_value_primitive_packed_val (NULL, valaddr + offset,
194 (i * bitsize) / HOST_CHAR_BIT,
195 (i * bitsize) % HOST_CHAR_BIT,
196 bitsize, elttype);
197 if (check_typedef (v0->type ())->length ()
198 != check_typedef (v1->type ())->length ())
199 break;
200 if (!v0->contents_eq (v0->embedded_offset (),
201 v1, v1->embedded_offset (),
202 check_typedef (v0->type ())->length ()))
203 break;
206 if (i - i0 > options->repeat_count_threshold)
208 struct value_print_options opts = *options;
210 opts.deref_ref = false;
211 common_val_print (v0, stream, recurse + 1, &opts, current_language);
212 annotate_elt_rep (i - i0);
213 gdb_printf (stream, _(" %p[<repeats %u times>%p]"),
214 metadata_style.style ().ptr (), i - i0, nullptr);
215 annotate_elt_rep_end ();
218 else
220 int j;
221 struct value_print_options opts = *options;
223 opts.deref_ref = false;
224 for (j = i0; j < i; j += 1)
226 if (j > i0)
228 if (options->prettyformat_arrays)
230 gdb_printf (stream, ",\n");
231 print_spaces (2 + 2 * recurse, stream);
233 else
235 gdb_printf (stream, ", ");
237 stream->wrap_here (2 + 2 * recurse);
238 maybe_print_array_index (index_type, j + low,
239 stream, options);
241 common_val_print (v0, stream, recurse + 1, &opts,
242 current_language);
243 annotate_elt ();
246 things_printed += i - i0;
248 annotate_array_section_end ();
249 if (i < len)
251 gdb_printf (stream, "...");
255 /* Print the character C on STREAM as part of the contents of a literal
256 string whose delimiter is QUOTER. TYPE_LEN is the length in bytes
257 of the character. */
259 void
260 ada_emit_char (int c, struct type *type, struct ui_file *stream,
261 int quoter, int type_len)
263 /* If this character fits in the normal ASCII range, and is
264 a printable character, then print the character as if it was
265 an ASCII character, even if this is a wide character.
266 The UCHAR_MAX check is necessary because the isascii function
267 requires that its argument have a value of an unsigned char,
268 or EOF (EOF is obviously not printable). */
269 if (c <= UCHAR_MAX && isascii (c) && isprint (c))
271 if (c == quoter && c == '"')
272 gdb_printf (stream, "\"\"");
273 else
274 gdb_printf (stream, "%c", c);
276 else
278 /* Follow GNAT's lead here and only use 6 digits for
279 wide_wide_character. */
280 gdb_printf (stream, "[\"%0*x\"]", std::min (6, type_len * 2), c);
284 /* Character #I of STRING, given that TYPE_LEN is the size in bytes
285 of a character. */
287 static int
288 char_at (const gdb_byte *string, int i, int type_len,
289 enum bfd_endian byte_order)
291 if (type_len == 1)
292 return string[i];
293 else
294 return (int) extract_unsigned_integer (string + type_len * i,
295 type_len, byte_order);
298 /* Print a floating-point value of type TYPE, pointed to in GDB by
299 VALADDR, on STREAM. Use Ada formatting conventions: there must be
300 a decimal point, and at least one digit before and after the
301 point. We use the GNAT format for NaNs and infinities. */
303 static void
304 ada_print_floating (const gdb_byte *valaddr, struct type *type,
305 struct ui_file *stream)
307 string_file tmp_stream;
309 print_floating (valaddr, type, &tmp_stream);
311 std::string s = tmp_stream.release ();
312 size_t skip_count = 0;
314 /* Don't try to modify a result representing an error. */
315 if (s[0] == '<')
317 gdb_puts (s.c_str (), stream);
318 return;
321 /* Modify for Ada rules. */
323 size_t pos = s.find ("inf");
324 if (pos == std::string::npos)
325 pos = s.find ("Inf");
326 if (pos == std::string::npos)
327 pos = s.find ("INF");
328 if (pos != std::string::npos)
329 s.replace (pos, 3, "Inf");
331 if (pos == std::string::npos)
333 pos = s.find ("nan");
334 if (pos == std::string::npos)
335 pos = s.find ("NaN");
336 if (pos == std::string::npos)
337 pos = s.find ("Nan");
338 if (pos != std::string::npos)
340 s[pos] = s[pos + 2] = 'N';
341 if (s[0] == '-')
342 skip_count = 1;
346 if (pos == std::string::npos
347 && s.find ('.') == std::string::npos)
349 pos = s.find ('e');
350 if (pos == std::string::npos)
351 gdb_printf (stream, "%s.0", s.c_str ());
352 else
353 gdb_printf (stream, "%.*s.0%s", (int) pos, s.c_str (), &s[pos]);
355 else
356 gdb_printf (stream, "%s", &s[skip_count]);
359 void
360 ada_printchar (int c, struct type *type, struct ui_file *stream)
362 gdb_puts ("'", stream);
363 ada_emit_char (c, type, stream, '\'', type->length ());
364 gdb_puts ("'", stream);
367 /* [From print_type_scalar in typeprint.c]. Print VAL on STREAM in a
368 form appropriate for TYPE, if non-NULL. If TYPE is NULL, print VAL
369 like a default signed integer. */
371 void
372 ada_print_scalar (struct type *type, LONGEST val, struct ui_file *stream)
374 if (!type)
376 print_longest (stream, 'd', 0, val);
377 return;
380 type = ada_check_typedef (type);
382 switch (type->code ())
385 case TYPE_CODE_ENUM:
387 std::optional<LONGEST> posn = discrete_position (type, val);
388 if (posn.has_value ())
389 fputs_styled (ada_enum_name (type->field (*posn).name ()),
390 variable_name_style.style (), stream);
391 else
392 print_longest (stream, 'd', 0, val);
394 break;
396 case TYPE_CODE_INT:
397 print_longest (stream, type->is_unsigned () ? 'u' : 'd', 0, val);
398 break;
400 case TYPE_CODE_CHAR:
401 current_language->printchar (val, type, stream);
402 break;
404 case TYPE_CODE_BOOL:
405 gdb_printf (stream, val ? "true" : "false");
406 break;
408 case TYPE_CODE_RANGE:
409 ada_print_scalar (type->target_type (), val, stream);
410 return;
412 case TYPE_CODE_UNDEF:
413 case TYPE_CODE_PTR:
414 case TYPE_CODE_ARRAY:
415 case TYPE_CODE_STRUCT:
416 case TYPE_CODE_UNION:
417 case TYPE_CODE_FUNC:
418 case TYPE_CODE_FLT:
419 case TYPE_CODE_VOID:
420 case TYPE_CODE_SET:
421 case TYPE_CODE_STRING:
422 case TYPE_CODE_ERROR:
423 case TYPE_CODE_MEMBERPTR:
424 case TYPE_CODE_METHODPTR:
425 case TYPE_CODE_METHOD:
426 case TYPE_CODE_REF:
427 warning (_("internal error: unhandled type in ada_print_scalar"));
428 break;
430 default:
431 error (_("Invalid type code in symbol table."));
435 /* Print the character string STRING, printing at most LENGTH characters.
436 Printing stops early if the number hits print_max; repeat counts
437 are printed as appropriate. Print ellipses at the end if we
438 had to stop before printing LENGTH characters, or if FORCE_ELLIPSES.
439 TYPE_LEN is the length (1 or 2) of the character type. */
441 static void
442 printstr (struct ui_file *stream, struct type *elttype, const gdb_byte *string,
443 unsigned int length, int force_ellipses, int type_len,
444 const struct value_print_options *options)
446 enum bfd_endian byte_order = type_byte_order (elttype);
447 unsigned int i;
448 unsigned int things_printed = 0;
449 int in_quotes = 0;
450 int need_comma = 0;
452 if (length == 0)
454 gdb_puts ("\"\"", stream);
455 return;
458 unsigned int print_max_chars = get_print_max_chars (options);
459 for (i = 0; i < length && things_printed < print_max_chars; i += 1)
461 /* Position of the character we are examining
462 to see whether it is repeated. */
463 unsigned int rep1;
464 /* Number of repetitions we have detected so far. */
465 unsigned int reps;
467 QUIT;
469 if (need_comma)
471 gdb_puts (", ", stream);
472 need_comma = 0;
475 rep1 = i + 1;
476 reps = 1;
477 while (rep1 < length
478 && char_at (string, rep1, type_len, byte_order)
479 == char_at (string, i, type_len, byte_order))
481 rep1 += 1;
482 reps += 1;
485 if (reps > options->repeat_count_threshold)
487 if (in_quotes)
489 gdb_puts ("\", ", stream);
490 in_quotes = 0;
492 gdb_puts ("'", stream);
493 ada_emit_char (char_at (string, i, type_len, byte_order),
494 elttype, stream, '\'', type_len);
495 gdb_puts ("'", stream);
496 gdb_printf (stream, _(" %p[<repeats %u times>%p]"),
497 metadata_style.style ().ptr (), reps, nullptr);
498 i = rep1 - 1;
499 things_printed += options->repeat_count_threshold;
500 need_comma = 1;
502 else
504 if (!in_quotes)
506 gdb_puts ("\"", stream);
507 in_quotes = 1;
509 ada_emit_char (char_at (string, i, type_len, byte_order),
510 elttype, stream, '"', type_len);
511 things_printed += 1;
515 /* Terminate the quotes if necessary. */
516 if (in_quotes)
517 gdb_puts ("\"", stream);
519 if (force_ellipses || i < length)
520 gdb_puts ("...", stream);
523 void
524 ada_printstr (struct ui_file *stream, struct type *type,
525 const gdb_byte *string, unsigned int length,
526 const char *encoding, int force_ellipses,
527 const struct value_print_options *options)
529 printstr (stream, type, string, length, force_ellipses, type->length (),
530 options);
533 static int
534 print_variant_part (struct value *value, int field_num,
535 struct value *outer_value,
536 struct ui_file *stream, int recurse,
537 const struct value_print_options *options,
538 int comma_needed,
539 const struct language_defn *language)
541 struct type *type = value->type ();
542 struct type *var_type = type->field (field_num).type ();
543 int which = ada_which_variant_applies (var_type, outer_value);
545 if (which < 0)
546 return 0;
548 struct value *variant_field = value_field (value, field_num);
549 struct value *active_component = value_field (variant_field, which);
550 return print_field_values (active_component, outer_value, stream, recurse,
551 options, comma_needed, language);
554 /* Print out fields of VALUE.
556 STREAM, RECURSE, and OPTIONS have the same meanings as in
557 ada_print_value and ada_value_print.
559 OUTER_VALUE gives the enclosing record (used to get discriminant
560 values when printing variant parts).
562 COMMA_NEEDED is 1 if fields have been printed at the current recursion
563 level, so that a comma is needed before any field printed by this
564 call.
566 Returns 1 if COMMA_NEEDED or any fields were printed. */
568 static int
569 print_field_values (struct value *value, struct value *outer_value,
570 struct ui_file *stream, int recurse,
571 const struct value_print_options *options,
572 int comma_needed,
573 const struct language_defn *language)
575 int i, len;
577 struct type *type = value->type ();
578 len = type->num_fields ();
580 for (i = 0; i < len; i += 1)
582 if (ada_is_ignored_field (type, i))
583 continue;
585 if (ada_is_wrapper_field (type, i))
587 struct value *field_val = ada_value_primitive_field (value, 0,
588 i, type);
589 comma_needed =
590 print_field_values (field_val, field_val,
591 stream, recurse, options,
592 comma_needed, language);
593 continue;
595 else if (ada_is_variant_part (type, i))
597 comma_needed =
598 print_variant_part (value, i, outer_value, stream, recurse,
599 options, comma_needed, language);
600 continue;
603 if (comma_needed)
604 gdb_printf (stream, ", ");
605 comma_needed = 1;
607 if (options->prettyformat)
609 gdb_printf (stream, "\n");
610 print_spaces (2 + 2 * recurse, stream);
612 else
614 stream->wrap_here (2 + 2 * recurse);
617 annotate_field_begin (type->field (i).type ());
618 gdb_printf (stream, "%.*s",
619 ada_name_prefix_len (type->field (i).name ()),
620 type->field (i).name ());
621 annotate_field_name_end ();
622 gdb_puts (" => ", stream);
623 annotate_field_value ();
625 if (type->field (i).is_packed ())
627 /* Bitfields require special handling, especially due to byte
628 order problems. */
629 if (type->field (i).is_ignored ())
631 fputs_styled (_("<optimized out or zero length>"),
632 metadata_style.style (), stream);
634 else
636 struct value *v;
637 int bit_pos = type->field (i).loc_bitpos ();
638 int bit_size = type->field (i).bitsize ();
639 struct value_print_options opts;
641 v = ada_value_primitive_packed_val
642 (value, nullptr,
643 bit_pos / HOST_CHAR_BIT,
644 bit_pos % HOST_CHAR_BIT,
645 bit_size, type->field (i).type ());
646 opts = *options;
647 opts.deref_ref = false;
648 common_val_print (v, stream, recurse + 1, &opts, language);
651 else
653 struct value_print_options opts = *options;
655 opts.deref_ref = false;
657 struct value *v = value_field (value, i);
658 common_val_print (v, stream, recurse + 1, &opts, language);
660 annotate_field_end ();
663 return comma_needed;
666 /* Implement Ada val_print'ing for the case where TYPE is
667 a TYPE_CODE_ARRAY of characters. */
669 static void
670 ada_val_print_string (struct type *type, const gdb_byte *valaddr,
671 int offset_aligned,
672 struct ui_file *stream, int recurse,
673 const struct value_print_options *options)
675 enum bfd_endian byte_order = type_byte_order (type);
676 struct type *elttype = type->target_type ();
677 unsigned int eltlen;
678 unsigned int len;
680 /* We know that ELTTYPE cannot possibly be null, because we assume
681 that we're called only when TYPE is a string-like type.
682 Similarly, the size of ELTTYPE should also be non-null, since
683 it's a character-like type. */
684 gdb_assert (elttype != NULL);
685 gdb_assert (elttype->length () != 0);
687 eltlen = elttype->length ();
688 len = type->length () / eltlen;
690 /* If requested, look for the first null char and only print
691 elements up to it. */
692 if (options->stop_print_at_null)
694 unsigned int print_max_chars = get_print_max_chars (options);
695 int temp_len;
697 /* Look for a NULL char. */
698 for (temp_len = 0;
699 (temp_len < len
700 && temp_len < print_max_chars
701 && char_at (valaddr + offset_aligned,
702 temp_len, eltlen, byte_order) != 0);
703 temp_len += 1);
704 len = temp_len;
707 printstr (stream, elttype, valaddr + offset_aligned, len, 0,
708 eltlen, options);
711 /* Implement Ada value_print'ing for the case where TYPE is a
712 TYPE_CODE_PTR. */
714 static void
715 ada_value_print_ptr (struct value *val,
716 struct ui_file *stream, int recurse,
717 const struct value_print_options *options)
719 if (!options->format
720 && val->type ()->target_type ()->code () == TYPE_CODE_INT
721 && val->type ()->target_type ()->length () == 0)
723 gdb_puts ("null", stream);
724 return;
727 common_val_print (val, stream, recurse, options, language_def (language_c));
729 struct type *type = ada_check_typedef (val->type ());
730 if (ada_is_tag_type (type))
732 gdb::unique_xmalloc_ptr<char> name = ada_tag_name (val);
734 if (name != NULL)
735 gdb_printf (stream, " (%s)", name.get ());
739 /* Implement Ada val_print'ing for the case where TYPE is
740 a TYPE_CODE_INT or TYPE_CODE_RANGE. */
742 static void
743 ada_value_print_num (struct value *val, struct ui_file *stream, int recurse,
744 const struct value_print_options *options)
746 struct type *type = ada_check_typedef (val->type ());
747 const gdb_byte *valaddr = val->contents_for_printing ().data ();
749 if (type->code () == TYPE_CODE_RANGE
750 && (type->target_type ()->code () == TYPE_CODE_ENUM
751 || type->target_type ()->code () == TYPE_CODE_BOOL
752 || type->target_type ()->code () == TYPE_CODE_CHAR))
754 /* For enum-valued ranges, we want to recurse, because we'll end
755 up printing the constant's name rather than its numeric
756 value. Character and fixed-point types are also printed
757 differently, so recurse for those as well. */
758 struct type *target_type = type->target_type ();
759 val = value_cast (target_type, val);
760 common_val_print (val, stream, recurse + 1, options,
761 language_def (language_ada));
762 return;
764 else
766 int format = (options->format ? options->format
767 : options->output_format);
769 if (format)
771 struct value_print_options opts = *options;
773 opts.format = format;
774 value_print_scalar_formatted (val, &opts, 0, stream);
776 else if (ada_is_system_address_type (type))
778 /* FIXME: We want to print System.Address variables using
779 the same format as for any access type. But for some
780 reason GNAT encodes the System.Address type as an int,
781 so we have to work-around this deficiency by handling
782 System.Address values as a special case. */
784 struct gdbarch *gdbarch = type->arch ();
785 struct type *ptr_type = builtin_type (gdbarch)->builtin_data_ptr;
786 CORE_ADDR addr = extract_typed_address (valaddr, ptr_type);
788 gdb_printf (stream, "(");
789 type_print (type, "", stream, -1);
790 gdb_printf (stream, ") ");
791 gdb_puts (paddress (gdbarch, addr), stream);
793 else
795 value_print_scalar_formatted (val, options, 0, stream);
796 if (ada_is_character_type (type))
798 LONGEST c;
800 gdb_puts (" ", stream);
801 c = unpack_long (type, valaddr);
802 ada_printchar (c, type, stream);
805 return;
809 /* Implement Ada val_print'ing for the case where TYPE is
810 a TYPE_CODE_ENUM. */
812 static void
813 ada_val_print_enum (struct value *value, struct ui_file *stream, int recurse,
814 const struct value_print_options *options)
816 LONGEST val;
818 if (options->format)
820 value_print_scalar_formatted (value, options, 0, stream);
821 return;
824 struct type *type = ada_check_typedef (value->type ());
825 const gdb_byte *valaddr = value->contents_for_printing ().data ();
826 int offset_aligned = ada_aligned_value_addr (type, valaddr) - valaddr;
828 val = unpack_long (type, valaddr + offset_aligned);
829 std::optional<LONGEST> posn = discrete_position (type, val);
830 if (posn.has_value ())
832 const char *name = ada_enum_name (type->field (*posn).name ());
834 if (name[0] == '\'')
835 gdb_printf (stream, "%ld %ps", (long) val,
836 styled_string (variable_name_style.style (),
837 name));
838 else
839 fputs_styled (name, variable_name_style.style (), stream);
841 else
842 print_longest (stream, 'd', 0, val);
845 /* Implement Ada val_print'ing for the case where the type is
846 TYPE_CODE_STRUCT or TYPE_CODE_UNION. */
848 static void
849 ada_val_print_struct_union (struct value *value,
850 struct ui_file *stream,
851 int recurse,
852 const struct value_print_options *options)
854 gdb_printf (stream, "(");
856 if (print_field_values (value, value, stream, recurse, options,
857 0, language_def (language_ada)) != 0
858 && options->prettyformat)
860 gdb_printf (stream, "\n");
861 print_spaces (2 * recurse, stream);
864 gdb_printf (stream, ")");
867 /* Implement Ada value_print'ing for the case where TYPE is a
868 TYPE_CODE_ARRAY. */
870 static void
871 ada_value_print_array (struct value *val, struct ui_file *stream, int recurse,
872 const struct value_print_options *options)
874 struct type *type = ada_check_typedef (val->type ());
876 /* For an array of characters, print with string syntax. */
877 if (ada_is_string_type (type)
878 && (options->format == 0 || options->format == 's'))
880 const gdb_byte *valaddr = val->contents_for_printing ().data ();
881 int offset_aligned = ada_aligned_value_addr (type, valaddr) - valaddr;
883 ada_val_print_string (type, valaddr, offset_aligned, stream, recurse,
884 options);
885 return;
888 gdb_printf (stream, "(");
889 print_optional_low_bound (stream, type, options);
891 if (val->entirely_optimized_out ())
892 val_print_optimized_out (val, stream);
893 else if (type->field (0).bitsize () > 0)
895 const gdb_byte *valaddr = val->contents_for_printing ().data ();
896 int offset_aligned = ada_aligned_value_addr (type, valaddr) - valaddr;
897 val_print_packed_array_elements (type, valaddr, offset_aligned,
898 stream, recurse, options);
900 else
901 value_print_array_elements (val, stream, recurse, options, 0);
902 gdb_printf (stream, ")");
905 /* Implement Ada val_print'ing for the case where TYPE is
906 a TYPE_CODE_REF. */
908 static void
909 ada_val_print_ref (struct type *type, const gdb_byte *valaddr,
910 int offset, int offset_aligned, CORE_ADDR address,
911 struct ui_file *stream, int recurse,
912 struct value *original_value,
913 const struct value_print_options *options)
915 /* For references, the debugger is expected to print the value as
916 an address if DEREF_REF is null. But printing an address in place
917 of the object value would be confusing to an Ada programmer.
918 So, for Ada values, we print the actual dereferenced value
919 regardless. */
920 struct type *elttype = check_typedef (type->target_type ());
921 struct value *deref_val;
922 CORE_ADDR deref_val_int;
924 if (elttype->code () == TYPE_CODE_UNDEF)
926 fputs_styled ("<ref to undefined type>", metadata_style.style (),
927 stream);
928 return;
931 deref_val = coerce_ref_if_computed (original_value);
932 if (deref_val)
934 if (ada_is_tagged_type (deref_val->type (), 1))
935 deref_val = ada_tag_value_at_base_address (deref_val);
937 common_val_print (deref_val, stream, recurse + 1, options,
938 language_def (language_ada));
939 return;
942 deref_val_int = unpack_pointer (type, valaddr + offset_aligned);
943 if (deref_val_int == 0)
945 gdb_puts ("(null)", stream);
946 return;
949 deref_val
950 = ada_value_ind (value_from_pointer (lookup_pointer_type (elttype),
951 deref_val_int));
952 if (ada_is_tagged_type (deref_val->type (), 1))
953 deref_val = ada_tag_value_at_base_address (deref_val);
955 if (deref_val->lazy ())
956 deref_val->fetch_lazy ();
958 common_val_print (deref_val, stream, recurse + 1,
959 options, language_def (language_ada));
962 /* See the comment on ada_value_print. This function differs in that
963 it does not catch evaluation errors (leaving that to its
964 caller). */
966 void
967 ada_value_print_inner (struct value *val, struct ui_file *stream, int recurse,
968 const struct value_print_options *options)
970 struct type *type = ada_check_typedef (val->type ());
972 if (ada_is_array_descriptor_type (type)
973 || (ada_is_constrained_packed_array_type (type)
974 && type->code () != TYPE_CODE_PTR))
976 /* If this is a reference, coerce it now. This helps taking
977 care of the case where ADDRESS is meaningless because
978 original_value was not an lval. */
979 val = coerce_ref (val);
980 val = ada_get_decoded_value (val);
981 if (val == nullptr)
983 gdb_assert (type->code () == TYPE_CODE_TYPEDEF);
984 gdb_printf (stream, "0x0");
985 return;
988 else
989 val = ada_to_fixed_value (val);
991 type = val->type ();
992 struct type *saved_type = type;
994 const gdb_byte *valaddr = val->contents_for_printing ().data ();
995 CORE_ADDR address = val->address ();
996 gdb::array_view<const gdb_byte> view
997 = gdb::make_array_view (valaddr, type->length ());
998 type = ada_check_typedef (resolve_dynamic_type (type, view, address));
999 if (type != saved_type)
1001 val = val->copy ();
1002 val->deprecated_set_type (type);
1005 if (is_fixed_point_type (type))
1006 type = type->fixed_point_type_base_type ();
1008 switch (type->code ())
1010 default:
1011 common_val_print (val, stream, recurse, options,
1012 language_def (language_c));
1013 break;
1015 case TYPE_CODE_PTR:
1016 ada_value_print_ptr (val, stream, recurse, options);
1017 break;
1019 case TYPE_CODE_INT:
1020 case TYPE_CODE_RANGE:
1021 ada_value_print_num (val, stream, recurse, options);
1022 break;
1024 case TYPE_CODE_ENUM:
1025 ada_val_print_enum (val, stream, recurse, options);
1026 break;
1028 case TYPE_CODE_FLT:
1029 if (options->format)
1031 common_val_print (val, stream, recurse, options,
1032 language_def (language_c));
1033 break;
1036 ada_print_floating (valaddr, type, stream);
1037 break;
1039 case TYPE_CODE_UNION:
1040 case TYPE_CODE_STRUCT:
1041 ada_val_print_struct_union (val, stream, recurse, options);
1042 break;
1044 case TYPE_CODE_ARRAY:
1045 ada_value_print_array (val, stream, recurse, options);
1046 return;
1048 case TYPE_CODE_REF:
1049 ada_val_print_ref (type, valaddr, 0, 0,
1050 address, stream, recurse, val,
1051 options);
1052 break;
1056 void
1057 ada_value_print (struct value *val0, struct ui_file *stream,
1058 const struct value_print_options *options)
1060 struct value *val = ada_to_fixed_value (val0);
1061 struct type *type = ada_check_typedef (val->type ());
1062 struct value_print_options opts;
1064 /* If it is a pointer, indicate what it points to; but not for
1065 "void *" pointers. */
1066 if (type->code () == TYPE_CODE_PTR
1067 && !(type->target_type ()->code () == TYPE_CODE_INT
1068 && type->target_type ()->length () == 0))
1070 /* Hack: don't print (char *) for char strings. Their
1071 type is indicated by the quoted string anyway. */
1072 if (type->target_type ()->length () != sizeof (char)
1073 || type->target_type ()->code () != TYPE_CODE_INT
1074 || type->target_type ()->is_unsigned ())
1076 gdb_printf (stream, "(");
1077 type_print (type, "", stream, -1);
1078 gdb_printf (stream, ") ");
1081 else if (ada_is_array_descriptor_type (type))
1083 /* We do not print the type description unless TYPE is an array
1084 access type (this is encoded by the compiler as a typedef to
1085 a fat pointer - hence the check against TYPE_CODE_TYPEDEF). */
1086 if (type->code () == TYPE_CODE_TYPEDEF)
1088 gdb_printf (stream, "(");
1089 type_print (type, "", stream, -1);
1090 gdb_printf (stream, ") ");
1094 opts = *options;
1095 opts.deref_ref = true;
1096 common_val_print (val, stream, 0, &opts, current_language);