Convert ada_value_print to value-based API
[deliverable/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 type *, const gdb_byte *,
34 int,
35 struct ui_file *, int,
36 struct value *,
37 const struct value_print_options *,
38 int, struct type *, int,
39 const struct language_defn *);
40 \f
41
42 /* Make TYPE unsigned if its range of values includes no negatives. */
43 static void
44 adjust_type_signedness (struct type *type)
45 {
46 if (type != NULL && TYPE_CODE (type) == TYPE_CODE_RANGE
47 && TYPE_LOW_BOUND (type) >= 0)
48 TYPE_UNSIGNED (type) = 1;
49 }
50
51 /* Assuming TYPE is a simple array type, prints its lower bound on STREAM,
52 if non-standard (i.e., other than 1 for numbers, other than lower bound
53 of index type for enumerated type). Returns 1 if something printed,
54 otherwise 0. */
55
56 static int
57 print_optional_low_bound (struct ui_file *stream, struct type *type,
58 const struct value_print_options *options)
59 {
60 struct type *index_type;
61 LONGEST low_bound;
62 LONGEST high_bound;
63
64 if (options->print_array_indexes)
65 return 0;
66
67 if (!get_array_bounds (type, &low_bound, &high_bound))
68 return 0;
69
70 /* If this is an empty array, then don't print the lower bound.
71 That would be confusing, because we would print the lower bound,
72 followed by... nothing! */
73 if (low_bound > high_bound)
74 return 0;
75
76 index_type = TYPE_INDEX_TYPE (type);
77
78 while (TYPE_CODE (index_type) == TYPE_CODE_RANGE)
79 {
80 /* We need to know what the base type is, in order to do the
81 appropriate check below. Otherwise, if this is a subrange
82 of an enumerated type, where the underlying value of the
83 first element is typically 0, we might test the low bound
84 against the wrong value. */
85 index_type = TYPE_TARGET_TYPE (index_type);
86 }
87
88 /* Don't print the lower bound if it's the default one. */
89 switch (TYPE_CODE (index_type))
90 {
91 case TYPE_CODE_BOOL:
92 case TYPE_CODE_CHAR:
93 if (low_bound == 0)
94 return 0;
95 break;
96 case TYPE_CODE_ENUM:
97 if (low_bound == TYPE_FIELD_ENUMVAL (index_type, 0))
98 return 0;
99 break;
100 case TYPE_CODE_UNDEF:
101 index_type = NULL;
102 /* FALL THROUGH */
103 default:
104 if (low_bound == 1)
105 return 0;
106 break;
107 }
108
109 ada_print_scalar (index_type, low_bound, stream);
110 fprintf_filtered (stream, " => ");
111 return 1;
112 }
113
114 /* Version of val_print_array_elements for GNAT-style packed arrays.
115 Prints elements of packed array of type TYPE at bit offset
116 BITOFFSET from VALADDR on STREAM. Formats according to OPTIONS and
117 separates with commas. RECURSE is the recursion (nesting) level.
118 TYPE must have been decoded (as by ada_coerce_to_simple_array). */
119
120 static void
121 val_print_packed_array_elements (struct type *type, const gdb_byte *valaddr,
122 int offset,
123 int bitoffset, struct ui_file *stream,
124 int recurse,
125 struct value *val,
126 const struct value_print_options *options)
127 {
128 unsigned int i;
129 unsigned int things_printed = 0;
130 unsigned len;
131 struct type *elttype, *index_type;
132 unsigned long bitsize = TYPE_FIELD_BITSIZE (type, 0);
133 struct value *mark = value_mark ();
134 LONGEST low = 0;
135
136 elttype = TYPE_TARGET_TYPE (type);
137 index_type = TYPE_INDEX_TYPE (type);
138
139 {
140 LONGEST high;
141 struct type *base_index_type;
142
143 if (get_discrete_bounds (index_type, &low, &high) < 0)
144 len = 1;
145 else
146 len = high - low + 1;
147
148 if (TYPE_CODE (index_type) == TYPE_CODE_RANGE)
149 base_index_type = TYPE_TARGET_TYPE (index_type);
150 else
151 base_index_type = index_type;
152
153 if (TYPE_CODE (base_index_type) == TYPE_CODE_ENUM)
154 {
155 LONGEST low_pos, high_pos;
156
157 /* Non-contiguous enumerations types can by used as index types
158 so the array length is computed from the positions of the
159 first and last literal in the enumeration type, and not from
160 the values of these literals. */
161
162 if (!discrete_position (base_index_type, low, &low_pos)
163 || !discrete_position (base_index_type, high, &high_pos))
164 {
165 warning (_("unable to get positions in array, use bounds instead"));
166 low_pos = low;
167 high_pos = high;
168 }
169
170 /* The array length should normally be HIGH_POS - LOW_POS + 1.
171 But in Ada we allow LOW_POS to be greater than HIGH_POS for
172 empty arrays. In that situation, the array length is just zero,
173 not negative! */
174
175 if (low_pos > high_pos)
176 len = 0;
177 else
178 len = high_pos - low_pos + 1;
179 }
180 }
181
182 i = 0;
183 annotate_array_section_begin (i, elttype);
184
185 while (i < len && things_printed < options->print_max)
186 {
187 struct value *v0, *v1;
188 int i0;
189
190 if (i != 0)
191 {
192 if (options->prettyformat_arrays)
193 {
194 fprintf_filtered (stream, ",\n");
195 print_spaces_filtered (2 + 2 * recurse, stream);
196 }
197 else
198 {
199 fprintf_filtered (stream, ", ");
200 }
201 }
202 wrap_here (n_spaces (2 + 2 * recurse));
203 maybe_print_array_index (index_type, i + low, stream, options);
204
205 i0 = i;
206 v0 = ada_value_primitive_packed_val (NULL, valaddr + offset,
207 (i0 * bitsize) / HOST_CHAR_BIT,
208 (i0 * bitsize) % HOST_CHAR_BIT,
209 bitsize, elttype);
210 while (1)
211 {
212 i += 1;
213 if (i >= len)
214 break;
215 v1 = ada_value_primitive_packed_val (NULL, valaddr + offset,
216 (i * bitsize) / HOST_CHAR_BIT,
217 (i * bitsize) % HOST_CHAR_BIT,
218 bitsize, elttype);
219 if (TYPE_LENGTH (check_typedef (value_type (v0)))
220 != TYPE_LENGTH (check_typedef (value_type (v1))))
221 break;
222 if (!value_contents_eq (v0, value_embedded_offset (v0),
223 v1, value_embedded_offset (v1),
224 TYPE_LENGTH (check_typedef (value_type (v0)))))
225 break;
226 }
227
228 if (i - i0 > options->repeat_count_threshold)
229 {
230 struct value_print_options opts = *options;
231
232 opts.deref_ref = 0;
233 val_print (elttype,
234 value_embedded_offset (v0), 0, stream,
235 recurse + 1, v0, &opts, current_language);
236 annotate_elt_rep (i - i0);
237 fprintf_filtered (stream, _(" %p[<repeats %u times>%p]"),
238 metadata_style.style ().ptr (), i - i0, nullptr);
239 annotate_elt_rep_end ();
240
241 }
242 else
243 {
244 int j;
245 struct value_print_options opts = *options;
246
247 opts.deref_ref = 0;
248 for (j = i0; j < i; j += 1)
249 {
250 if (j > i0)
251 {
252 if (options->prettyformat_arrays)
253 {
254 fprintf_filtered (stream, ",\n");
255 print_spaces_filtered (2 + 2 * recurse, stream);
256 }
257 else
258 {
259 fprintf_filtered (stream, ", ");
260 }
261 wrap_here (n_spaces (2 + 2 * recurse));
262 maybe_print_array_index (index_type, j + low,
263 stream, options);
264 }
265 val_print (elttype,
266 value_embedded_offset (v0), 0, stream,
267 recurse + 1, v0, &opts, current_language);
268 annotate_elt ();
269 }
270 }
271 things_printed += i - i0;
272 }
273 annotate_array_section_end ();
274 if (i < len)
275 {
276 fprintf_filtered (stream, "...");
277 }
278
279 value_free_to_mark (mark);
280 }
281
282 static struct type *
283 printable_val_type (struct type *type, const gdb_byte *valaddr)
284 {
285 return ada_to_fixed_type (ada_aligned_type (type), valaddr, 0, NULL, 1);
286 }
287
288 /* Print the character C on STREAM as part of the contents of a literal
289 string whose delimiter is QUOTER. TYPE_LEN is the length in bytes
290 of the character. */
291
292 void
293 ada_emit_char (int c, struct type *type, struct ui_file *stream,
294 int quoter, int type_len)
295 {
296 /* If this character fits in the normal ASCII range, and is
297 a printable character, then print the character as if it was
298 an ASCII character, even if this is a wide character.
299 The UCHAR_MAX check is necessary because the isascii function
300 requires that its argument have a value of an unsigned char,
301 or EOF (EOF is obviously not printable). */
302 if (c <= UCHAR_MAX && isascii (c) && isprint (c))
303 {
304 if (c == quoter && c == '"')
305 fprintf_filtered (stream, "\"\"");
306 else
307 fprintf_filtered (stream, "%c", c);
308 }
309 else
310 fprintf_filtered (stream, "[\"%0*x\"]", type_len * 2, c);
311 }
312
313 /* Character #I of STRING, given that TYPE_LEN is the size in bytes
314 of a character. */
315
316 static int
317 char_at (const gdb_byte *string, int i, int type_len,
318 enum bfd_endian byte_order)
319 {
320 if (type_len == 1)
321 return string[i];
322 else
323 return (int) extract_unsigned_integer (string + type_len * i,
324 type_len, byte_order);
325 }
326
327 /* Print a floating-point value of type TYPE, pointed to in GDB by
328 VALADDR, on STREAM. Use Ada formatting conventions: there must be
329 a decimal point, and at least one digit before and after the
330 point. We use the GNAT format for NaNs and infinities. */
331
332 static void
333 ada_print_floating (const gdb_byte *valaddr, struct type *type,
334 struct ui_file *stream)
335 {
336 string_file tmp_stream;
337
338 print_floating (valaddr, type, &tmp_stream);
339
340 std::string &s = tmp_stream.string ();
341 size_t skip_count = 0;
342
343 /* Modify for Ada rules. */
344
345 size_t pos = s.find ("inf");
346 if (pos == std::string::npos)
347 pos = s.find ("Inf");
348 if (pos == std::string::npos)
349 pos = s.find ("INF");
350 if (pos != std::string::npos)
351 s.replace (pos, 3, "Inf");
352
353 if (pos == std::string::npos)
354 {
355 pos = s.find ("nan");
356 if (pos == std::string::npos)
357 pos = s.find ("NaN");
358 if (pos == std::string::npos)
359 pos = s.find ("Nan");
360 if (pos != std::string::npos)
361 {
362 s[pos] = s[pos + 2] = 'N';
363 if (s[0] == '-')
364 skip_count = 1;
365 }
366 }
367
368 if (pos == std::string::npos
369 && s.find ('.') == std::string::npos)
370 {
371 pos = s.find ('e');
372 if (pos == std::string::npos)
373 fprintf_filtered (stream, "%s.0", s.c_str ());
374 else
375 fprintf_filtered (stream, "%.*s.0%s", (int) pos, s.c_str (), &s[pos]);
376 }
377 else
378 fprintf_filtered (stream, "%s", &s[skip_count]);
379 }
380
381 void
382 ada_printchar (int c, struct type *type, struct ui_file *stream)
383 {
384 fputs_filtered ("'", stream);
385 ada_emit_char (c, type, stream, '\'', TYPE_LENGTH (type));
386 fputs_filtered ("'", stream);
387 }
388
389 /* [From print_type_scalar in typeprint.c]. Print VAL on STREAM in a
390 form appropriate for TYPE, if non-NULL. If TYPE is NULL, print VAL
391 like a default signed integer. */
392
393 void
394 ada_print_scalar (struct type *type, LONGEST val, struct ui_file *stream)
395 {
396 unsigned int i;
397 unsigned len;
398
399 if (!type)
400 {
401 print_longest (stream, 'd', 0, val);
402 return;
403 }
404
405 type = ada_check_typedef (type);
406
407 switch (TYPE_CODE (type))
408 {
409
410 case TYPE_CODE_ENUM:
411 len = TYPE_NFIELDS (type);
412 for (i = 0; i < len; i++)
413 {
414 if (TYPE_FIELD_ENUMVAL (type, i) == val)
415 {
416 break;
417 }
418 }
419 if (i < len)
420 {
421 fputs_styled (ada_enum_name (TYPE_FIELD_NAME (type, i)),
422 variable_name_style.style (), stream);
423 }
424 else
425 {
426 print_longest (stream, 'd', 0, val);
427 }
428 break;
429
430 case TYPE_CODE_INT:
431 print_longest (stream, TYPE_UNSIGNED (type) ? 'u' : 'd', 0, val);
432 break;
433
434 case TYPE_CODE_CHAR:
435 LA_PRINT_CHAR (val, type, stream);
436 break;
437
438 case TYPE_CODE_BOOL:
439 fprintf_filtered (stream, val ? "true" : "false");
440 break;
441
442 case TYPE_CODE_RANGE:
443 ada_print_scalar (TYPE_TARGET_TYPE (type), val, stream);
444 return;
445
446 case TYPE_CODE_UNDEF:
447 case TYPE_CODE_PTR:
448 case TYPE_CODE_ARRAY:
449 case TYPE_CODE_STRUCT:
450 case TYPE_CODE_UNION:
451 case TYPE_CODE_FUNC:
452 case TYPE_CODE_FLT:
453 case TYPE_CODE_VOID:
454 case TYPE_CODE_SET:
455 case TYPE_CODE_STRING:
456 case TYPE_CODE_ERROR:
457 case TYPE_CODE_MEMBERPTR:
458 case TYPE_CODE_METHODPTR:
459 case TYPE_CODE_METHOD:
460 case TYPE_CODE_REF:
461 warning (_("internal error: unhandled type in ada_print_scalar"));
462 break;
463
464 default:
465 error (_("Invalid type code in symbol table."));
466 }
467 }
468
469 /* Print the character string STRING, printing at most LENGTH characters.
470 Printing stops early if the number hits print_max; repeat counts
471 are printed as appropriate. Print ellipses at the end if we
472 had to stop before printing LENGTH characters, or if FORCE_ELLIPSES.
473 TYPE_LEN is the length (1 or 2) of the character type. */
474
475 static void
476 printstr (struct ui_file *stream, struct type *elttype, const gdb_byte *string,
477 unsigned int length, int force_ellipses, int type_len,
478 const struct value_print_options *options)
479 {
480 enum bfd_endian byte_order = type_byte_order (elttype);
481 unsigned int i;
482 unsigned int things_printed = 0;
483 int in_quotes = 0;
484 int need_comma = 0;
485
486 if (length == 0)
487 {
488 fputs_filtered ("\"\"", stream);
489 return;
490 }
491
492 for (i = 0; i < length && things_printed < options->print_max; i += 1)
493 {
494 /* Position of the character we are examining
495 to see whether it is repeated. */
496 unsigned int rep1;
497 /* Number of repetitions we have detected so far. */
498 unsigned int reps;
499
500 QUIT;
501
502 if (need_comma)
503 {
504 fputs_filtered (", ", stream);
505 need_comma = 0;
506 }
507
508 rep1 = i + 1;
509 reps = 1;
510 while (rep1 < length
511 && char_at (string, rep1, type_len, byte_order)
512 == char_at (string, i, type_len, byte_order))
513 {
514 rep1 += 1;
515 reps += 1;
516 }
517
518 if (reps > options->repeat_count_threshold)
519 {
520 if (in_quotes)
521 {
522 fputs_filtered ("\", ", stream);
523 in_quotes = 0;
524 }
525 fputs_filtered ("'", stream);
526 ada_emit_char (char_at (string, i, type_len, byte_order),
527 elttype, stream, '\'', type_len);
528 fputs_filtered ("'", stream);
529 fprintf_filtered (stream, _(" %p[<repeats %u times>%p]"),
530 metadata_style.style ().ptr (), reps, nullptr);
531 i = rep1 - 1;
532 things_printed += options->repeat_count_threshold;
533 need_comma = 1;
534 }
535 else
536 {
537 if (!in_quotes)
538 {
539 fputs_filtered ("\"", stream);
540 in_quotes = 1;
541 }
542 ada_emit_char (char_at (string, i, type_len, byte_order),
543 elttype, stream, '"', type_len);
544 things_printed += 1;
545 }
546 }
547
548 /* Terminate the quotes if necessary. */
549 if (in_quotes)
550 fputs_filtered ("\"", stream);
551
552 if (force_ellipses || i < length)
553 fputs_filtered ("...", stream);
554 }
555
556 void
557 ada_printstr (struct ui_file *stream, struct type *type,
558 const gdb_byte *string, unsigned int length,
559 const char *encoding, int force_ellipses,
560 const struct value_print_options *options)
561 {
562 printstr (stream, type, string, length, force_ellipses, TYPE_LENGTH (type),
563 options);
564 }
565
566 static int
567 print_variant_part (struct type *type, int field_num,
568 const gdb_byte *valaddr, int offset,
569 struct ui_file *stream, int recurse,
570 struct value *val,
571 const struct value_print_options *options,
572 int comma_needed,
573 struct type *outer_type, int outer_offset,
574 const struct language_defn *language)
575 {
576 struct type *var_type = TYPE_FIELD_TYPE (type, field_num);
577 int which = ada_which_variant_applies (var_type, outer_type,
578 valaddr + outer_offset);
579
580 if (which < 0)
581 return 0;
582 else
583 return print_field_values
584 (TYPE_FIELD_TYPE (var_type, which),
585 valaddr,
586 offset + TYPE_FIELD_BITPOS (type, field_num) / HOST_CHAR_BIT
587 + TYPE_FIELD_BITPOS (var_type, which) / HOST_CHAR_BIT,
588 stream, recurse, val, options,
589 comma_needed, outer_type, outer_offset, language);
590 }
591
592 /* Print out fields of value at VALADDR + OFFSET having structure type TYPE.
593
594 TYPE, VALADDR, OFFSET, STREAM, RECURSE, and OPTIONS have the same
595 meanings as in ada_print_value and ada_val_print.
596
597 OUTER_TYPE and OUTER_OFFSET give type and address of enclosing
598 record (used to get discriminant values when printing variant
599 parts).
600
601 COMMA_NEEDED is 1 if fields have been printed at the current recursion
602 level, so that a comma is needed before any field printed by this
603 call.
604
605 Returns 1 if COMMA_NEEDED or any fields were printed. */
606
607 static int
608 print_field_values (struct type *type, const gdb_byte *valaddr,
609 int offset, struct ui_file *stream, int recurse,
610 struct value *val,
611 const struct value_print_options *options,
612 int comma_needed,
613 struct type *outer_type, int outer_offset,
614 const struct language_defn *language)
615 {
616 int i, len;
617
618 len = TYPE_NFIELDS (type);
619
620 for (i = 0; i < len; i += 1)
621 {
622 if (ada_is_ignored_field (type, i))
623 continue;
624
625 if (ada_is_wrapper_field (type, i))
626 {
627 comma_needed =
628 print_field_values (TYPE_FIELD_TYPE (type, i),
629 valaddr,
630 (offset
631 + TYPE_FIELD_BITPOS (type, i) / HOST_CHAR_BIT),
632 stream, recurse, val, options,
633 comma_needed, type, offset, language);
634 continue;
635 }
636 else if (ada_is_variant_part (type, i))
637 {
638 comma_needed =
639 print_variant_part (type, i, valaddr,
640 offset, stream, recurse, val,
641 options, comma_needed,
642 outer_type, outer_offset, language);
643 continue;
644 }
645
646 if (comma_needed)
647 fprintf_filtered (stream, ", ");
648 comma_needed = 1;
649
650 if (options->prettyformat)
651 {
652 fprintf_filtered (stream, "\n");
653 print_spaces_filtered (2 + 2 * recurse, stream);
654 }
655 else
656 {
657 wrap_here (n_spaces (2 + 2 * recurse));
658 }
659
660 annotate_field_begin (TYPE_FIELD_TYPE (type, i));
661 fprintf_filtered (stream, "%.*s",
662 ada_name_prefix_len (TYPE_FIELD_NAME (type, i)),
663 TYPE_FIELD_NAME (type, i));
664 annotate_field_name_end ();
665 fputs_filtered (" => ", stream);
666 annotate_field_value ();
667
668 if (TYPE_FIELD_PACKED (type, i))
669 {
670 /* Bitfields require special handling, especially due to byte
671 order problems. */
672 if (HAVE_CPLUS_STRUCT (type) && TYPE_FIELD_IGNORE (type, i))
673 {
674 fputs_styled (_("<optimized out or zero length>"),
675 metadata_style.style (), stream);
676 }
677 else
678 {
679 struct value *v;
680 int bit_pos = TYPE_FIELD_BITPOS (type, i);
681 int bit_size = TYPE_FIELD_BITSIZE (type, i);
682 struct value_print_options opts;
683
684 adjust_type_signedness (TYPE_FIELD_TYPE (type, i));
685 v = ada_value_primitive_packed_val
686 (NULL, valaddr,
687 offset + bit_pos / HOST_CHAR_BIT,
688 bit_pos % HOST_CHAR_BIT,
689 bit_size, TYPE_FIELD_TYPE (type, i));
690 opts = *options;
691 opts.deref_ref = 0;
692 val_print (TYPE_FIELD_TYPE (type, i),
693 value_embedded_offset (v), 0,
694 stream, recurse + 1, v,
695 &opts, language);
696 }
697 }
698 else
699 {
700 struct value_print_options opts = *options;
701
702 opts.deref_ref = 0;
703 val_print (TYPE_FIELD_TYPE (type, i),
704 (offset + TYPE_FIELD_BITPOS (type, i) / HOST_CHAR_BIT),
705 0, stream, recurse + 1, val, &opts, language);
706 }
707 annotate_field_end ();
708 }
709
710 return comma_needed;
711 }
712
713 /* Implement Ada val_print'ing for the case where TYPE is
714 a TYPE_CODE_ARRAY of characters. */
715
716 static void
717 ada_val_print_string (struct type *type, const gdb_byte *valaddr,
718 int offset, int offset_aligned, CORE_ADDR address,
719 struct ui_file *stream, int recurse,
720 struct value *original_value,
721 const struct value_print_options *options)
722 {
723 enum bfd_endian byte_order = type_byte_order (type);
724 struct type *elttype = TYPE_TARGET_TYPE (type);
725 unsigned int eltlen;
726 unsigned int len;
727
728 /* We know that ELTTYPE cannot possibly be null, because we assume
729 that we're called only when TYPE is a string-like type.
730 Similarly, the size of ELTTYPE should also be non-null, since
731 it's a character-like type. */
732 gdb_assert (elttype != NULL);
733 gdb_assert (TYPE_LENGTH (elttype) != 0);
734
735 eltlen = TYPE_LENGTH (elttype);
736 len = TYPE_LENGTH (type) / eltlen;
737
738 if (options->prettyformat_arrays)
739 print_spaces_filtered (2 + 2 * recurse, stream);
740
741 /* If requested, look for the first null char and only print
742 elements up to it. */
743 if (options->stop_print_at_null)
744 {
745 int temp_len;
746
747 /* Look for a NULL char. */
748 for (temp_len = 0;
749 (temp_len < len
750 && temp_len < options->print_max
751 && char_at (valaddr + offset_aligned,
752 temp_len, eltlen, byte_order) != 0);
753 temp_len += 1);
754 len = temp_len;
755 }
756
757 printstr (stream, elttype, valaddr + offset_aligned, len, 0,
758 eltlen, options);
759 }
760
761 /* Implement Ada val_print-ing for GNAT arrays (Eg. fat pointers,
762 thin pointers, etc). */
763
764 static void
765 ada_val_print_gnat_array (struct value *val,
766 struct ui_file *stream, int recurse,
767 const struct value_print_options *options)
768 {
769 scoped_value_mark free_values;
770
771 struct type *type = ada_check_typedef (value_type (val));
772
773 /* If this is a reference, coerce it now. This helps taking care
774 of the case where ADDRESS is meaningless because original_value
775 was not an lval. */
776 val = coerce_ref (val);
777 if (TYPE_CODE (type) == TYPE_CODE_TYPEDEF) /* array access type. */
778 val = ada_coerce_to_simple_array_ptr (val);
779 else
780 val = ada_coerce_to_simple_array (val);
781 if (val == NULL)
782 {
783 gdb_assert (TYPE_CODE (type) == TYPE_CODE_TYPEDEF);
784 fprintf_filtered (stream, "0x0");
785 }
786 else
787 common_val_print (val, stream, recurse, options,
788 language_def (language_ada));
789 }
790
791 /* Implement Ada val_print'ing for the case where TYPE is
792 a TYPE_CODE_PTR. */
793
794 static void
795 ada_val_print_ptr (struct type *type, const gdb_byte *valaddr,
796 int offset, int offset_aligned, CORE_ADDR address,
797 struct ui_file *stream, int recurse,
798 struct value *original_value,
799 const struct value_print_options *options)
800 {
801 val_print (type, offset, address, stream, recurse,
802 original_value, options, language_def (language_c));
803
804 if (ada_is_tag_type (type))
805 {
806 struct value *val =
807 value_from_contents_and_address (type,
808 valaddr + offset_aligned,
809 address + offset_aligned);
810 const char *name = ada_tag_name (val);
811
812 if (name != NULL)
813 fprintf_filtered (stream, " (%s)", name);
814 }
815 }
816
817 /* Implement Ada value_print'ing for the case where TYPE is a
818 TYPE_CODE_PTR. */
819
820 static void
821 ada_value_print_ptr (struct value *val,
822 struct ui_file *stream, int recurse,
823 const struct value_print_options *options)
824 {
825 common_val_print (val, stream, recurse, options, language_def (language_c));
826
827 struct type *type = ada_check_typedef (value_type (val));
828 if (ada_is_tag_type (type))
829 {
830 const char *name = ada_tag_name (val);
831
832 if (name != NULL)
833 fprintf_filtered (stream, " (%s)", name);
834 }
835 }
836
837 /* Implement Ada val_print'ing for the case where TYPE is
838 a TYPE_CODE_INT or TYPE_CODE_RANGE. */
839
840 static void
841 ada_val_print_num (struct type *type, const gdb_byte *valaddr,
842 int offset, int offset_aligned, CORE_ADDR address,
843 struct ui_file *stream, int recurse,
844 struct value *original_value,
845 const struct value_print_options *options)
846 {
847 if (ada_is_fixed_point_type (type))
848 {
849 struct value *scale = ada_scaling_factor (type);
850 struct value *v = value_from_contents (type, valaddr + offset_aligned);
851 v = value_cast (value_type (scale), v);
852 v = value_binop (v, scale, BINOP_MUL);
853
854 const char *fmt = TYPE_LENGTH (type) < 4 ? "%.11g" : "%.17g";
855 std::string str
856 = target_float_to_string (value_contents (v), value_type (v), fmt);
857 fputs_filtered (str.c_str (), stream);
858 return;
859 }
860 else if (TYPE_CODE (type) == TYPE_CODE_RANGE
861 && (TYPE_CODE (TYPE_TARGET_TYPE (type)) == TYPE_CODE_ENUM
862 || TYPE_CODE (TYPE_TARGET_TYPE (type)) == TYPE_CODE_BOOL
863 || TYPE_CODE (TYPE_TARGET_TYPE (type)) == TYPE_CODE_CHAR))
864 {
865 /* For enum-valued ranges, we want to recurse, because we'll end
866 up printing the constant's name rather than its numeric
867 value. Character and fixed-point types are also printed
868 differently, so recuse for those as well. */
869 struct type *target_type = TYPE_TARGET_TYPE (type);
870
871 if (TYPE_LENGTH (type) != TYPE_LENGTH (target_type))
872 {
873 /* Obscure case of range type that has different length from
874 its base type. Perform a conversion, or we will get a
875 nonsense value. Actually, we could use the same
876 code regardless of lengths; I'm just avoiding a cast. */
877 struct value *v1
878 = value_from_contents_and_address (type, valaddr + offset, 0);
879 struct value *v = value_cast (target_type, v1);
880
881 val_print (target_type,
882 value_embedded_offset (v), 0, stream,
883 recurse + 1, v, options,
884 language_def (language_ada));
885 }
886 else
887 val_print (TYPE_TARGET_TYPE (type), offset,
888 address, stream, recurse, original_value,
889 options, language_def (language_ada));
890 return;
891 }
892 else
893 {
894 int format = (options->format ? options->format
895 : options->output_format);
896
897 if (format)
898 {
899 struct value_print_options opts = *options;
900
901 opts.format = format;
902 val_print_scalar_formatted (type, offset_aligned,
903 original_value, &opts, 0, stream);
904 }
905 else if (ada_is_system_address_type (type))
906 {
907 /* FIXME: We want to print System.Address variables using
908 the same format as for any access type. But for some
909 reason GNAT encodes the System.Address type as an int,
910 so we have to work-around this deficiency by handling
911 System.Address values as a special case. */
912
913 struct gdbarch *gdbarch = get_type_arch (type);
914 struct type *ptr_type = builtin_type (gdbarch)->builtin_data_ptr;
915 CORE_ADDR addr = extract_typed_address (valaddr + offset_aligned,
916 ptr_type);
917
918 fprintf_filtered (stream, "(");
919 type_print (type, "", stream, -1);
920 fprintf_filtered (stream, ") ");
921 fputs_filtered (paddress (gdbarch, addr), stream);
922 }
923 else
924 {
925 val_print_scalar_formatted (type, offset_aligned,
926 original_value, options, 0, stream);
927 if (ada_is_character_type (type))
928 {
929 LONGEST c;
930
931 fputs_filtered (" ", stream);
932 c = unpack_long (type, valaddr + offset_aligned);
933 ada_printchar (c, type, stream);
934 }
935 }
936 return;
937 }
938 }
939
940 /* Implement Ada val_print'ing for the case where TYPE is
941 a TYPE_CODE_INT or TYPE_CODE_RANGE. */
942
943 static void
944 ada_value_print_num (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 const gdb_byte *valaddr = value_contents_for_printing (val);
949
950 if (ada_is_fixed_point_type (type))
951 {
952 struct value *scale = ada_scaling_factor (type);
953 val = value_cast (value_type (scale), val);
954 val = value_binop (val, scale, BINOP_MUL);
955
956 const char *fmt = TYPE_LENGTH (type) < 4 ? "%.11g" : "%.17g";
957 std::string str
958 = target_float_to_string (value_contents (val), value_type (val), fmt);
959 fputs_filtered (str.c_str (), stream);
960 return;
961 }
962 else if (TYPE_CODE (type) == TYPE_CODE_RANGE
963 && (TYPE_CODE (TYPE_TARGET_TYPE (type)) == TYPE_CODE_ENUM
964 || TYPE_CODE (TYPE_TARGET_TYPE (type)) == TYPE_CODE_BOOL
965 || TYPE_CODE (TYPE_TARGET_TYPE (type)) == TYPE_CODE_CHAR))
966 {
967 /* For enum-valued ranges, we want to recurse, because we'll end
968 up printing the constant's name rather than its numeric
969 value. Character and fixed-point types are also printed
970 differently, so recuse for those as well. */
971 struct type *target_type = TYPE_TARGET_TYPE (type);
972 val = value_cast (target_type, val);
973 common_val_print (val, stream, recurse + 1, options,
974 language_def (language_ada));
975 return;
976 }
977 else
978 {
979 int format = (options->format ? options->format
980 : options->output_format);
981
982 if (format)
983 {
984 struct value_print_options opts = *options;
985
986 opts.format = format;
987 value_print_scalar_formatted (val, &opts, 0, stream);
988 }
989 else if (ada_is_system_address_type (type))
990 {
991 /* FIXME: We want to print System.Address variables using
992 the same format as for any access type. But for some
993 reason GNAT encodes the System.Address type as an int,
994 so we have to work-around this deficiency by handling
995 System.Address values as a special case. */
996
997 struct gdbarch *gdbarch = get_type_arch (type);
998 struct type *ptr_type = builtin_type (gdbarch)->builtin_data_ptr;
999 CORE_ADDR addr = extract_typed_address (valaddr, ptr_type);
1000
1001 fprintf_filtered (stream, "(");
1002 type_print (type, "", stream, -1);
1003 fprintf_filtered (stream, ") ");
1004 fputs_filtered (paddress (gdbarch, addr), stream);
1005 }
1006 else
1007 {
1008 value_print_scalar_formatted (val, options, 0, stream);
1009 if (ada_is_character_type (type))
1010 {
1011 LONGEST c;
1012
1013 fputs_filtered (" ", stream);
1014 c = unpack_long (type, valaddr);
1015 ada_printchar (c, type, stream);
1016 }
1017 }
1018 return;
1019 }
1020 }
1021
1022 /* Implement Ada val_print'ing for the case where TYPE is
1023 a TYPE_CODE_ENUM. */
1024
1025 static void
1026 ada_val_print_enum (struct type *type, const gdb_byte *valaddr,
1027 int offset, int offset_aligned, CORE_ADDR address,
1028 struct ui_file *stream, int recurse,
1029 struct value *original_value,
1030 const struct value_print_options *options)
1031 {
1032 int i;
1033 unsigned int len;
1034 LONGEST val;
1035
1036 if (options->format)
1037 {
1038 val_print_scalar_formatted (type, offset_aligned,
1039 original_value, options, 0, stream);
1040 return;
1041 }
1042
1043 len = TYPE_NFIELDS (type);
1044 val = unpack_long (type, valaddr + offset_aligned);
1045 for (i = 0; i < len; i++)
1046 {
1047 QUIT;
1048 if (val == TYPE_FIELD_ENUMVAL (type, i))
1049 break;
1050 }
1051
1052 if (i < len)
1053 {
1054 const char *name = ada_enum_name (TYPE_FIELD_NAME (type, i));
1055
1056 if (name[0] == '\'')
1057 fprintf_filtered (stream, "%ld %ps", (long) val,
1058 styled_string (variable_name_style.style (),
1059 name));
1060 else
1061 fputs_styled (name, variable_name_style.style (), stream);
1062 }
1063 else
1064 print_longest (stream, 'd', 0, val);
1065 }
1066
1067 /* Implement Ada val_print'ing for the case where TYPE is
1068 a TYPE_CODE_FLT. */
1069
1070 static void
1071 ada_val_print_flt (struct type *type, const gdb_byte *valaddr,
1072 int offset, int offset_aligned, CORE_ADDR address,
1073 struct ui_file *stream, int recurse,
1074 struct value *original_value,
1075 const struct value_print_options *options)
1076 {
1077 if (options->format)
1078 {
1079 val_print (type, offset, address, stream, recurse,
1080 original_value, options, language_def (language_c));
1081 return;
1082 }
1083
1084 ada_print_floating (valaddr + offset, type, stream);
1085 }
1086
1087 /* Implement Ada val_print'ing for the case where TYPE is
1088 a TYPE_CODE_STRUCT or TYPE_CODE_UNION. */
1089
1090 static void
1091 ada_val_print_struct_union
1092 (struct type *type, const gdb_byte *valaddr, int offset,
1093 int offset_aligned, CORE_ADDR address, struct ui_file *stream,
1094 int recurse, struct value *original_value,
1095 const struct value_print_options *options)
1096 {
1097 if (ada_is_bogus_array_descriptor (type))
1098 {
1099 fprintf_filtered (stream, "(...?)");
1100 return;
1101 }
1102
1103 fprintf_filtered (stream, "(");
1104
1105 if (print_field_values (type, valaddr, offset_aligned,
1106 stream, recurse, original_value, options,
1107 0, type, offset_aligned,
1108 language_def (language_ada)) != 0
1109 && options->prettyformat)
1110 {
1111 fprintf_filtered (stream, "\n");
1112 print_spaces_filtered (2 * recurse, stream);
1113 }
1114
1115 fprintf_filtered (stream, ")");
1116 }
1117
1118 /* Implement Ada val_print'ing for the case where TYPE is
1119 a TYPE_CODE_ARRAY. */
1120
1121 static void
1122 ada_val_print_array (struct type *type, const gdb_byte *valaddr,
1123 int offset, int offset_aligned, CORE_ADDR address,
1124 struct ui_file *stream, int recurse,
1125 struct value *original_value,
1126 const struct value_print_options *options)
1127 {
1128 /* For an array of characters, print with string syntax. */
1129 if (ada_is_string_type (type)
1130 && (options->format == 0 || options->format == 's'))
1131 {
1132 ada_val_print_string (type, valaddr, offset, offset_aligned,
1133 address, stream, recurse, original_value,
1134 options);
1135 return;
1136 }
1137
1138 fprintf_filtered (stream, "(");
1139 print_optional_low_bound (stream, type, options);
1140 if (TYPE_FIELD_BITSIZE (type, 0) > 0)
1141 val_print_packed_array_elements (type, valaddr, offset_aligned,
1142 0, stream, recurse,
1143 original_value, options);
1144 else
1145 val_print_array_elements (type, offset_aligned, address,
1146 stream, recurse, original_value,
1147 options, 0);
1148 fprintf_filtered (stream, ")");
1149 }
1150
1151 /* Implement Ada val_print'ing for the case where TYPE is
1152 a TYPE_CODE_REF. */
1153
1154 static void
1155 ada_val_print_ref (struct type *type, const gdb_byte *valaddr,
1156 int offset, int offset_aligned, CORE_ADDR address,
1157 struct ui_file *stream, int recurse,
1158 struct value *original_value,
1159 const struct value_print_options *options)
1160 {
1161 /* For references, the debugger is expected to print the value as
1162 an address if DEREF_REF is null. But printing an address in place
1163 of the object value would be confusing to an Ada programmer.
1164 So, for Ada values, we print the actual dereferenced value
1165 regardless. */
1166 struct type *elttype = check_typedef (TYPE_TARGET_TYPE (type));
1167 struct value *deref_val;
1168 CORE_ADDR deref_val_int;
1169
1170 if (TYPE_CODE (elttype) == TYPE_CODE_UNDEF)
1171 {
1172 fputs_styled ("<ref to undefined type>", metadata_style.style (),
1173 stream);
1174 return;
1175 }
1176
1177 deref_val = coerce_ref_if_computed (original_value);
1178 if (deref_val)
1179 {
1180 if (ada_is_tagged_type (value_type (deref_val), 1))
1181 deref_val = ada_tag_value_at_base_address (deref_val);
1182
1183 common_val_print (deref_val, stream, recurse + 1, options,
1184 language_def (language_ada));
1185 return;
1186 }
1187
1188 deref_val_int = unpack_pointer (type, valaddr + offset_aligned);
1189 if (deref_val_int == 0)
1190 {
1191 fputs_filtered ("(null)", stream);
1192 return;
1193 }
1194
1195 deref_val
1196 = ada_value_ind (value_from_pointer (lookup_pointer_type (elttype),
1197 deref_val_int));
1198 if (ada_is_tagged_type (value_type (deref_val), 1))
1199 deref_val = ada_tag_value_at_base_address (deref_val);
1200
1201 /* Make sure that the object does not have an unreasonable size
1202 before trying to print it. This can happen for instance with
1203 references to dynamic objects whose contents is uninitialized
1204 (Eg: an array whose bounds are not set yet). */
1205 ada_ensure_varsize_limit (value_type (deref_val));
1206
1207 if (value_lazy (deref_val))
1208 value_fetch_lazy (deref_val);
1209
1210 common_val_print (deref_val, stream, recurse + 1,
1211 options, language_def (language_ada));
1212 }
1213
1214 /* See the comment on ada_val_print. This function differs in that it
1215 does not catch evaluation errors (leaving that to ada_val_print). */
1216
1217 static void
1218 ada_val_print_1 (struct type *type,
1219 int offset, CORE_ADDR address,
1220 struct ui_file *stream, int recurse,
1221 struct value *original_value,
1222 const struct value_print_options *options)
1223 {
1224 int offset_aligned;
1225 const gdb_byte *valaddr = value_contents_for_printing (original_value);
1226
1227 type = ada_check_typedef (type);
1228
1229 if (ada_is_array_descriptor_type (type)
1230 || (ada_is_constrained_packed_array_type (type)
1231 && TYPE_CODE (type) != TYPE_CODE_PTR))
1232 {
1233 struct value *val = value_from_contents_and_address (type,
1234 valaddr + offset,
1235 address);
1236 ada_val_print_gnat_array (val, stream, recurse, options);
1237 return;
1238 }
1239
1240 offset_aligned = offset + ada_aligned_value_addr (type, valaddr) - valaddr;
1241 type = printable_val_type (type, valaddr + offset_aligned);
1242 type = resolve_dynamic_type (type, valaddr + offset_aligned,
1243 address + offset_aligned);
1244
1245 switch (TYPE_CODE (type))
1246 {
1247 default:
1248 val_print (type, offset, address, stream, recurse,
1249 original_value, options, language_def (language_c));
1250 break;
1251
1252 case TYPE_CODE_PTR:
1253 ada_val_print_ptr (type, valaddr, offset, offset_aligned,
1254 address, stream, recurse, original_value,
1255 options);
1256 break;
1257
1258 case TYPE_CODE_INT:
1259 case TYPE_CODE_RANGE:
1260 ada_val_print_num (type, valaddr, offset, offset_aligned,
1261 address, stream, recurse, original_value,
1262 options);
1263 break;
1264
1265 case TYPE_CODE_ENUM:
1266 ada_val_print_enum (type, valaddr, offset, offset_aligned,
1267 address, stream, recurse, original_value,
1268 options);
1269 break;
1270
1271 case TYPE_CODE_FLT:
1272 ada_val_print_flt (type, valaddr, offset, offset_aligned,
1273 address, stream, recurse, original_value,
1274 options);
1275 break;
1276
1277 case TYPE_CODE_UNION:
1278 case TYPE_CODE_STRUCT:
1279 ada_val_print_struct_union (type, valaddr, offset, offset_aligned,
1280 address, stream, recurse,
1281 original_value, options);
1282 break;
1283
1284 case TYPE_CODE_ARRAY:
1285 ada_val_print_array (type, valaddr, offset, offset_aligned,
1286 address, stream, recurse, original_value,
1287 options);
1288 return;
1289
1290 case TYPE_CODE_REF:
1291 ada_val_print_ref (type, valaddr, offset, offset_aligned,
1292 address, stream, recurse, original_value,
1293 options);
1294 break;
1295 }
1296 }
1297
1298 /* See val_print for a description of the various parameters of this
1299 function; they are identical. */
1300
1301 void
1302 ada_val_print (struct type *type,
1303 int embedded_offset, CORE_ADDR address,
1304 struct ui_file *stream, int recurse,
1305 struct value *val,
1306 const struct value_print_options *options)
1307 {
1308 try
1309 {
1310 ada_val_print_1 (type, embedded_offset, address,
1311 stream, recurse, val, options);
1312 }
1313 catch (const gdb_exception_error &except)
1314 {
1315 fprintf_styled (stream, metadata_style.style (),
1316 _("<error reading variable: %s>"),
1317 except.what ());
1318 }
1319 }
1320
1321 /* See the comment on ada_value_print. This function differs in that
1322 it does not catch evaluation errors (leaving that to
1323 ada_value_print). */
1324
1325 static void
1326 ada_value_print_1 (struct value *val, struct ui_file *stream, int recurse,
1327 const struct value_print_options *options)
1328 {
1329 struct type *type = ada_check_typedef (value_type (val));
1330
1331 if (ada_is_array_descriptor_type (type)
1332 || (ada_is_constrained_packed_array_type (type)
1333 && TYPE_CODE (type) != TYPE_CODE_PTR))
1334 {
1335 ada_val_print_gnat_array (val, stream, recurse, options);
1336 return;
1337 }
1338
1339 val = ada_to_fixed_value (val);
1340 type = value_type (val);
1341 struct type *saved_type = type;
1342
1343 const gdb_byte *valaddr = value_contents_for_printing (val);
1344 CORE_ADDR address = value_address (val);
1345 type = ada_check_typedef (resolve_dynamic_type (type, valaddr, address));
1346 if (type != saved_type)
1347 {
1348 val = value_copy (val);
1349 deprecated_set_value_type (val, type);
1350 }
1351
1352 switch (TYPE_CODE (type))
1353 {
1354 default:
1355 common_val_print (val, stream, recurse, options,
1356 language_def (language_c));
1357 break;
1358
1359 case TYPE_CODE_PTR:
1360 ada_value_print_ptr (val, stream, recurse, options);
1361 break;
1362
1363 case TYPE_CODE_INT:
1364 case TYPE_CODE_RANGE:
1365 ada_value_print_num (val, stream, recurse, options);
1366 break;
1367
1368 case TYPE_CODE_ENUM:
1369 ada_val_print_enum (type, valaddr, 0, 0,
1370 address, stream, recurse, val,
1371 options);
1372 break;
1373
1374 case TYPE_CODE_FLT:
1375 if (options->format)
1376 {
1377 common_val_print (val, stream, recurse, options,
1378 language_def (language_c));
1379 break;
1380 }
1381
1382 ada_print_floating (valaddr, type, stream);
1383 break;
1384
1385 case TYPE_CODE_UNION:
1386 case TYPE_CODE_STRUCT:
1387 ada_val_print_struct_union (type, valaddr, 0, 0,
1388 address, stream, recurse,
1389 val, options);
1390 break;
1391
1392 case TYPE_CODE_ARRAY:
1393 ada_val_print_array (type, valaddr, 0, 0,
1394 address, stream, recurse, val,
1395 options);
1396 return;
1397
1398 case TYPE_CODE_REF:
1399 ada_val_print_ref (type, valaddr, 0, 0,
1400 address, stream, recurse, val,
1401 options);
1402 break;
1403 }
1404 }
1405
1406 /* See ada-lang.h. */
1407
1408 void
1409 ada_value_print_inner (struct value *val, struct ui_file *stream,
1410 int recurse,
1411 const struct value_print_options *options)
1412 {
1413 try
1414 {
1415 ada_value_print_1 (val, stream, recurse, options);
1416 }
1417 catch (const gdb_exception_error &except)
1418 {
1419 fprintf_styled (stream, metadata_style.style (),
1420 _("<error reading variable: %s>"),
1421 except.what ());
1422 }
1423 }
1424
1425 void
1426 ada_value_print (struct value *val0, struct ui_file *stream,
1427 const struct value_print_options *options)
1428 {
1429 struct value *val = ada_to_fixed_value (val0);
1430 struct type *type = ada_check_typedef (value_type (val));
1431 struct value_print_options opts;
1432
1433 /* If it is a pointer, indicate what it points to. */
1434 if (TYPE_CODE (type) == TYPE_CODE_PTR)
1435 {
1436 /* Hack: don't print (char *) for char strings. Their
1437 type is indicated by the quoted string anyway. */
1438 if (TYPE_LENGTH (TYPE_TARGET_TYPE (type)) != sizeof (char)
1439 || TYPE_CODE (TYPE_TARGET_TYPE (type)) != TYPE_CODE_INT
1440 || TYPE_UNSIGNED (TYPE_TARGET_TYPE (type)))
1441 {
1442 fprintf_filtered (stream, "(");
1443 type_print (type, "", stream, -1);
1444 fprintf_filtered (stream, ") ");
1445 }
1446 }
1447 else if (ada_is_array_descriptor_type (type))
1448 {
1449 /* We do not print the type description unless TYPE is an array
1450 access type (this is encoded by the compiler as a typedef to
1451 a fat pointer - hence the check against TYPE_CODE_TYPEDEF). */
1452 if (TYPE_CODE (type) == TYPE_CODE_TYPEDEF)
1453 {
1454 fprintf_filtered (stream, "(");
1455 type_print (type, "", stream, -1);
1456 fprintf_filtered (stream, ") ");
1457 }
1458 }
1459 else if (ada_is_bogus_array_descriptor (type))
1460 {
1461 fprintf_filtered (stream, "(");
1462 type_print (type, "", stream, -1);
1463 fprintf_filtered (stream, ") (...?)");
1464 return;
1465 }
1466
1467 opts = *options;
1468 opts.deref_ref = 1;
1469 common_val_print (val, stream, 0, &opts, current_language);
1470 }
This page took 0.093042 seconds and 4 git commands to generate.