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