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