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