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