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