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