Address complaints from gdb_ari.sh:
[deliverable/binutils-gdb.git] / gdb / ada-valprint.c
1 /* Support for printing Ada values for GDB, the GNU debugger.
2 Copyright 1986, 1988, 1989, 1991, 1992, 1993, 1994, 1997, 2001,
3 2002, 2003, 2004.
4 Free Software Foundation, Inc.
5
6 This file is part of GDB.
7
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
12
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
21
22 #include <ctype.h>
23 #include "defs.h"
24 #include "gdb_string.h"
25 #include "symtab.h"
26 #include "gdbtypes.h"
27 #include "expression.h"
28 #include "value.h"
29 #include "demangle.h"
30 #include "valprint.h"
31 #include "language.h"
32 #include "annotate.h"
33 #include "ada-lang.h"
34 #include "c-lang.h"
35 #include "infcall.h"
36
37 /* Encapsulates arguments to ada_val_print. */
38 struct ada_val_print_args
39 {
40 struct type *type;
41 char *valaddr0;
42 int embedded_offset;
43 CORE_ADDR address;
44 struct ui_file *stream;
45 int format;
46 int deref_ref;
47 int recurse;
48 enum val_prettyprint pretty;
49 };
50
51 extern int inspect_it;
52 extern unsigned int repeat_count_threshold;
53
54 static void print_record (struct type *, char *, struct ui_file *, int,
55 int, enum val_prettyprint);
56
57 static int print_field_values (struct type *, char *, struct ui_file *,
58 int, int, enum val_prettyprint,
59 int, struct type *, char *);
60
61 static int print_variant_part (struct type *, int, char *,
62 struct ui_file *, int, int,
63 enum val_prettyprint, int, struct type *,
64 char *);
65
66 static void val_print_packed_array_elements (struct type *, char *valaddr,
67 int, struct ui_file *, int, int,
68 enum val_prettyprint);
69
70 static void adjust_type_signedness (struct type *);
71
72 static int ada_val_print_stub (void *args0);
73
74 static int ada_val_print_1 (struct type *, char *, int, CORE_ADDR,
75 struct ui_file *, int, int, int,
76 enum val_prettyprint);
77 static void ada_print_floating (char *, struct type *, struct ui_file *);
78 \f
79
80 /* Make TYPE unsigned if its range of values includes no negatives. */
81 static void
82 adjust_type_signedness (struct type *type)
83 {
84 if (type != NULL && TYPE_CODE (type) == TYPE_CODE_RANGE
85 && TYPE_LOW_BOUND (type) >= 0)
86 TYPE_FLAGS (type) |= TYPE_FLAG_UNSIGNED;
87 }
88
89 /* Assuming TYPE is a simple array type, prints its lower bound on STREAM,
90 if non-standard (i.e., other than 1 for numbers, other than lower bound
91 of index type for enumerated type). Returns 1 if something printed,
92 otherwise 0. */
93
94 static int
95 print_optional_low_bound (struct ui_file *stream, struct type *type)
96 {
97 struct type *index_type;
98 long low_bound;
99
100 index_type = TYPE_INDEX_TYPE (type);
101 low_bound = 0;
102
103 if (index_type == NULL)
104 return 0;
105 if (TYPE_CODE (index_type) == TYPE_CODE_RANGE)
106 {
107 low_bound = TYPE_LOW_BOUND (index_type);
108 index_type = TYPE_TARGET_TYPE (index_type);
109 }
110 else
111 return 0;
112
113 switch (TYPE_CODE (index_type))
114 {
115 case TYPE_CODE_ENUM:
116 if (low_bound == TYPE_FIELD_BITPOS (index_type, 0))
117 return 0;
118 break;
119 case TYPE_CODE_UNDEF:
120 index_type = builtin_type_long;
121 /* FALL THROUGH */
122 default:
123 if (low_bound == 1)
124 return 0;
125 break;
126 }
127
128 ada_print_scalar (index_type, (LONGEST) low_bound, stream);
129 fprintf_filtered (stream, " => ");
130 return 1;
131 }
132
133 /* Version of val_print_array_elements for GNAT-style packed arrays.
134 Prints elements of packed array of type TYPE at bit offset
135 BITOFFSET from VALADDR on STREAM. Formats according to FORMAT and
136 separates with commas. RECURSE is the recursion (nesting) level.
137 If PRETTY, uses "prettier" format. TYPE must have been decoded (as
138 by ada_coerce_to_simple_array). */
139
140 static void
141 val_print_packed_array_elements (struct type *type, char *valaddr,
142 int bitoffset, struct ui_file *stream,
143 int format, int recurse,
144 enum val_prettyprint pretty)
145 {
146 unsigned int i;
147 unsigned int things_printed = 0;
148 unsigned len;
149 struct type *elttype;
150 unsigned eltlen;
151 unsigned long bitsize = TYPE_FIELD_BITSIZE (type, 0);
152 struct value *mark = value_mark ();
153
154 elttype = TYPE_TARGET_TYPE (type);
155 eltlen = TYPE_LENGTH (check_typedef (elttype));
156
157 {
158 LONGEST low, high;
159 if (get_discrete_bounds (TYPE_FIELD_TYPE (type, 0), &low, &high) < 0)
160 len = 1;
161 else
162 len = high - low + 1;
163 }
164
165 i = 0;
166 annotate_array_section_begin (i, elttype);
167
168 while (i < len && things_printed < print_max)
169 {
170 struct value *v0, *v1;
171 int i0;
172
173 if (i != 0)
174 {
175 if (prettyprint_arrays)
176 {
177 fprintf_filtered (stream, ",\n");
178 print_spaces_filtered (2 + 2 * recurse, stream);
179 }
180 else
181 {
182 fprintf_filtered (stream, ", ");
183 }
184 }
185 wrap_here (n_spaces (2 + 2 * recurse));
186
187 i0 = i;
188 v0 = ada_value_primitive_packed_val (NULL, valaddr,
189 (i0 * bitsize) / HOST_CHAR_BIT,
190 (i0 * bitsize) % HOST_CHAR_BIT,
191 bitsize, elttype);
192 while (1)
193 {
194 i += 1;
195 if (i >= len)
196 break;
197 v1 = ada_value_primitive_packed_val (NULL, valaddr,
198 (i * bitsize) / HOST_CHAR_BIT,
199 (i * bitsize) % HOST_CHAR_BIT,
200 bitsize, elttype);
201 if (memcmp (VALUE_CONTENTS (v0), VALUE_CONTENTS (v1), eltlen) != 0)
202 break;
203 }
204
205 if (i - i0 > repeat_count_threshold)
206 {
207 val_print (elttype, VALUE_CONTENTS (v0), 0, 0, stream, format,
208 0, recurse + 1, pretty);
209 annotate_elt_rep (i - i0);
210 fprintf_filtered (stream, " <repeats %u times>", i - i0);
211 annotate_elt_rep_end ();
212
213 }
214 else
215 {
216 int j;
217 for (j = i0; j < i; j += 1)
218 {
219 if (j > i0)
220 {
221 if (prettyprint_arrays)
222 {
223 fprintf_filtered (stream, ",\n");
224 print_spaces_filtered (2 + 2 * recurse, stream);
225 }
226 else
227 {
228 fprintf_filtered (stream, ", ");
229 }
230 wrap_here (n_spaces (2 + 2 * recurse));
231 }
232 val_print (elttype, VALUE_CONTENTS (v0), 0, 0, stream, format,
233 0, recurse + 1, pretty);
234 annotate_elt ();
235 }
236 }
237 things_printed += i - i0;
238 }
239 annotate_array_section_end ();
240 if (i < len)
241 {
242 fprintf_filtered (stream, "...");
243 }
244
245 value_free_to_mark (mark);
246 }
247
248 static struct type *
249 printable_val_type (struct type *type, char *valaddr)
250 {
251 return ada_to_fixed_type (ada_aligned_type (type), valaddr, 0, NULL);
252 }
253
254 /* Print the character C on STREAM as part of the contents of a literal
255 string whose delimiter is QUOTER. TYPE_LEN is the length in bytes
256 (1 or 2) of the character. */
257
258 void
259 ada_emit_char (int c, struct ui_file *stream, int quoter, int type_len)
260 {
261 if (type_len != 2)
262 type_len = 1;
263
264 c &= (1 << (type_len * TARGET_CHAR_BIT)) - 1;
265
266 if (isascii (c) && isprint (c))
267 {
268 if (c == quoter && c == '"')
269 fprintf_filtered (stream, "[\"%c\"]", quoter);
270 else
271 fprintf_filtered (stream, "%c", c);
272 }
273 else
274 fprintf_filtered (stream, "[\"%0*x\"]", type_len * 2, c);
275 }
276
277 /* Character #I of STRING, given that TYPE_LEN is the size in bytes (1
278 or 2) of a character. */
279
280 static int
281 char_at (char *string, int i, int type_len)
282 {
283 if (type_len == 1)
284 return string[i];
285 else
286 return (int) extract_unsigned_integer (string + 2 * i, 2);
287 }
288
289 /* Wrapper around memcpy to make it legal argument to ui_file_put */
290 static void
291 ui_memcpy (void *dest, const char *buffer, long len)
292 {
293 memcpy (dest, buffer, (size_t) len);
294 ((char *) dest)[len] = '\0';
295 }
296
297 /* Print a floating-point value of type TYPE, pointed to in GDB by
298 VALADDR, on STREAM. Use Ada formatting conventions: there must be
299 a decimal point, and at least one digit before and after the
300 point. We use GNAT format for NaNs and infinities. */
301 static void
302 ada_print_floating (char *valaddr, struct type *type, struct ui_file *stream)
303 {
304 char buffer[64];
305 char *s, *result;
306 int len;
307 struct ui_file *tmp_stream = mem_fileopen ();
308 struct cleanup *cleanups = make_cleanup_ui_file_delete (tmp_stream);
309
310 print_floating (valaddr, type, tmp_stream);
311 ui_file_put (tmp_stream, ui_memcpy, buffer);
312 do_cleanups (cleanups);
313
314 result = buffer;
315 len = strlen (result);
316
317 /* Modify for Ada rules. */
318 if ((s = strstr (result, "inf")) != NULL
319 || (s = strstr (result, "Inf")) != NULL
320 || (s = strstr (result, "INF")) != NULL)
321 strcpy (s, "Inf");
322 else if ((s = strstr (result, "nan")) != NULL
323 || (s = strstr (result, "NaN")) != NULL
324 || (s = strstr (result, "Nan")) != NULL)
325 {
326 s[0] = s[2] = 'N';
327 if (result[0] == '-')
328 result += 1;
329 }
330 else if (strchr (result, '.') == NULL)
331 {
332 if ((s = strchr (result, 'e')) == NULL)
333 fprintf_filtered (stream, "%s.0", result);
334 else
335 fprintf_filtered (stream, "%.*s.0%s", (int) (s-result), result, s);
336 return;
337 }
338 fprintf_filtered (stream, "%s", result);
339 }
340
341 void
342 ada_printchar (int c, struct ui_file *stream)
343 {
344 fputs_filtered ("'", stream);
345 ada_emit_char (c, stream, '\'', 1);
346 fputs_filtered ("'", stream);
347 }
348
349 /* [From print_type_scalar in typeprint.c]. Print VAL on STREAM in a
350 form appropriate for TYPE. */
351
352 void
353 ada_print_scalar (struct type *type, LONGEST val, struct ui_file *stream)
354 {
355 unsigned int i;
356 unsigned len;
357
358 CHECK_TYPEDEF (type);
359
360 switch (TYPE_CODE (type))
361 {
362
363 case TYPE_CODE_ENUM:
364 len = TYPE_NFIELDS (type);
365 for (i = 0; i < len; i++)
366 {
367 if (TYPE_FIELD_BITPOS (type, i) == val)
368 {
369 break;
370 }
371 }
372 if (i < len)
373 {
374 fputs_filtered (ada_enum_name (TYPE_FIELD_NAME (type, i)), stream);
375 }
376 else
377 {
378 print_longest (stream, 'd', 0, val);
379 }
380 break;
381
382 case TYPE_CODE_INT:
383 print_longest (stream, TYPE_UNSIGNED (type) ? 'u' : 'd', 0, val);
384 break;
385
386 case TYPE_CODE_CHAR:
387 LA_PRINT_CHAR ((unsigned char) val, stream);
388 break;
389
390 case TYPE_CODE_BOOL:
391 fprintf_filtered (stream, val ? "true" : "false");
392 break;
393
394 case TYPE_CODE_RANGE:
395 ada_print_scalar (TYPE_TARGET_TYPE (type), val, stream);
396 return;
397
398 case TYPE_CODE_UNDEF:
399 case TYPE_CODE_PTR:
400 case TYPE_CODE_ARRAY:
401 case TYPE_CODE_STRUCT:
402 case TYPE_CODE_UNION:
403 case TYPE_CODE_FUNC:
404 case TYPE_CODE_FLT:
405 case TYPE_CODE_VOID:
406 case TYPE_CODE_SET:
407 case TYPE_CODE_STRING:
408 case TYPE_CODE_ERROR:
409 case TYPE_CODE_MEMBER:
410 case TYPE_CODE_METHOD:
411 case TYPE_CODE_REF:
412 warning ("internal error: unhandled type in ada_print_scalar");
413 break;
414
415 default:
416 error ("Invalid type code in symbol table.");
417 }
418 gdb_flush (stream);
419 }
420
421 /* Print the character string STRING, printing at most LENGTH characters.
422 Printing stops early if the number hits print_max; repeat counts
423 are printed as appropriate. Print ellipses at the end if we
424 had to stop before printing LENGTH characters, or if
425 FORCE_ELLIPSES. TYPE_LEN is the length (1 or 2) of the character type.
426 */
427
428 static void
429 printstr (struct ui_file *stream, char *string, unsigned int length,
430 int force_ellipses, int type_len)
431 {
432 unsigned int i;
433 unsigned int things_printed = 0;
434 int in_quotes = 0;
435 int need_comma = 0;
436
437 if (length == 0)
438 {
439 fputs_filtered ("\"\"", stream);
440 return;
441 }
442
443 for (i = 0; i < length && things_printed < print_max; i += 1)
444 {
445 /* Position of the character we are examining
446 to see whether it is repeated. */
447 unsigned int rep1;
448 /* Number of repetitions we have detected so far. */
449 unsigned int reps;
450
451 QUIT;
452
453 if (need_comma)
454 {
455 fputs_filtered (", ", stream);
456 need_comma = 0;
457 }
458
459 rep1 = i + 1;
460 reps = 1;
461 while (rep1 < length &&
462 char_at (string, rep1, type_len) == char_at (string, i,
463 type_len))
464 {
465 rep1 += 1;
466 reps += 1;
467 }
468
469 if (reps > repeat_count_threshold)
470 {
471 if (in_quotes)
472 {
473 if (inspect_it)
474 fputs_filtered ("\\\", ", stream);
475 else
476 fputs_filtered ("\", ", stream);
477 in_quotes = 0;
478 }
479 fputs_filtered ("'", stream);
480 ada_emit_char (char_at (string, i, type_len), stream, '\'',
481 type_len);
482 fputs_filtered ("'", stream);
483 fprintf_filtered (stream, " <repeats %u times>", reps);
484 i = rep1 - 1;
485 things_printed += repeat_count_threshold;
486 need_comma = 1;
487 }
488 else
489 {
490 if (!in_quotes)
491 {
492 if (inspect_it)
493 fputs_filtered ("\\\"", stream);
494 else
495 fputs_filtered ("\"", stream);
496 in_quotes = 1;
497 }
498 ada_emit_char (char_at (string, i, type_len), stream, '"',
499 type_len);
500 things_printed += 1;
501 }
502 }
503
504 /* Terminate the quotes if necessary. */
505 if (in_quotes)
506 {
507 if (inspect_it)
508 fputs_filtered ("\\\"", stream);
509 else
510 fputs_filtered ("\"", stream);
511 }
512
513 if (force_ellipses || i < length)
514 fputs_filtered ("...", stream);
515 }
516
517 void
518 ada_printstr (struct ui_file *stream, char *string, unsigned int length,
519 int width, int force_ellipses)
520 {
521 printstr (stream, string, length, force_ellipses, width);
522 }
523
524
525 /* Print data of type TYPE located at VALADDR (within GDB), which came from
526 the inferior at address ADDRESS, onto stdio stream STREAM according to
527 FORMAT (a letter as for the printf % codes or 0 for natural format).
528 The data at VALADDR is in target byte order.
529
530 If the data is printed as a string, returns the number of string characters
531 printed.
532
533 If DEREF_REF is nonzero, then dereference references, otherwise just print
534 them like pointers.
535
536 RECURSE indicates the amount of indentation to supply before
537 continuation lines; this amount is roughly twice the value of RECURSE.
538
539 When PRETTY is non-zero, prints record fields on separate lines.
540 (For some reason, the current version of gdb instead uses a global
541 variable---prettyprint_arrays--- to causes a similar effect on
542 arrays.) */
543
544 int
545 ada_val_print (struct type *type, char *valaddr0, int embedded_offset,
546 CORE_ADDR address, struct ui_file *stream, int format,
547 int deref_ref, int recurse, enum val_prettyprint pretty)
548 {
549 struct ada_val_print_args args;
550 args.type = type;
551 args.valaddr0 = valaddr0;
552 args.embedded_offset = embedded_offset;
553 args.address = address;
554 args.stream = stream;
555 args.format = format;
556 args.deref_ref = deref_ref;
557 args.recurse = recurse;
558 args.pretty = pretty;
559
560 return catch_errors (ada_val_print_stub, &args, NULL, RETURN_MASK_ALL);
561 }
562
563 /* Helper for ada_val_print; used as argument to catch_errors to
564 unmarshal the arguments to ada_val_print_1, which does the work. */
565 static int
566 ada_val_print_stub (void *args0)
567 {
568 struct ada_val_print_args *argsp = (struct ada_val_print_args *) args0;
569 return ada_val_print_1 (argsp->type, argsp->valaddr0,
570 argsp->embedded_offset, argsp->address,
571 argsp->stream, argsp->format, argsp->deref_ref,
572 argsp->recurse, argsp->pretty);
573 }
574
575 /* See the comment on ada_val_print. This function differs in that it
576 * does not catch evaluation errors (leaving that to ada_val_print). */
577
578 static int
579 ada_val_print_1 (struct type *type, char *valaddr0, int embedded_offset,
580 CORE_ADDR address, struct ui_file *stream, int format,
581 int deref_ref, int recurse, enum val_prettyprint pretty)
582 {
583 unsigned int len;
584 int i;
585 struct type *elttype;
586 unsigned int eltlen;
587 LONGEST val;
588 char *valaddr = valaddr0 + embedded_offset;
589
590 CHECK_TYPEDEF (type);
591
592 if (ada_is_array_descriptor_type (type) || ada_is_packed_array_type (type))
593 {
594 int retn;
595 struct value *mark = value_mark ();
596 struct value *val;
597 val = value_from_contents_and_address (type, valaddr, address);
598 val = ada_coerce_to_simple_array_ptr (val);
599 if (val == NULL)
600 {
601 fprintf_filtered (stream, "(null)");
602 retn = 0;
603 }
604 else
605 retn = ada_val_print_1 (VALUE_TYPE (val), VALUE_CONTENTS (val), 0,
606 VALUE_ADDRESS (val), stream, format,
607 deref_ref, recurse, pretty);
608 value_free_to_mark (mark);
609 return retn;
610 }
611
612 valaddr = ada_aligned_value_addr (type, valaddr);
613 embedded_offset -= valaddr - valaddr0 - embedded_offset;
614 type = printable_val_type (type, valaddr);
615
616 switch (TYPE_CODE (type))
617 {
618 default:
619 return c_val_print (type, valaddr0, embedded_offset, address, stream,
620 format, deref_ref, recurse, pretty);
621
622 case TYPE_CODE_PTR:
623 {
624 int ret = c_val_print (type, valaddr0, embedded_offset, address,
625 stream, format, deref_ref, recurse, pretty);
626 if (ada_is_tag_type (type))
627 {
628 struct value *val =
629 value_from_contents_and_address (type, valaddr, address);
630 const char *name = ada_tag_name (val);
631 if (name != NULL)
632 fprintf_filtered (stream, " (%s)", name);
633 return 0;
634 }
635 return ret;
636 }
637
638 case TYPE_CODE_INT:
639 case TYPE_CODE_RANGE:
640 if (ada_is_fixed_point_type (type))
641 {
642 LONGEST v = unpack_long (type, valaddr);
643 int len = TYPE_LENGTH (type);
644
645 fprintf_filtered (stream, len < 4 ? "%.11g" : "%.17g",
646 (double) ada_fixed_to_float (type, v));
647 return 0;
648 }
649 else if (ada_is_vax_floating_type (type))
650 {
651 struct value *val =
652 value_from_contents_and_address (type, valaddr, address);
653 struct value *func = ada_vax_float_print_function (type);
654 if (func != 0)
655 {
656 static struct type *parray_of_char = NULL;
657 struct value *printable_val;
658
659 if (parray_of_char == NULL)
660 parray_of_char =
661 make_pointer_type
662 (create_array_type
663 (NULL, builtin_type_char,
664 create_range_type (NULL, builtin_type_int, 0, 32)), NULL);
665
666 printable_val =
667 value_ind (value_cast (parray_of_char,
668 call_function_by_hand (func, 1,
669 &val)));
670
671 fprintf_filtered (stream, "%s", VALUE_CONTENTS (printable_val));
672 return 0;
673 }
674 /* No special printing function. Do as best we can. */
675 }
676 else if (TYPE_CODE (type) == TYPE_CODE_RANGE)
677 {
678 struct type *target_type = TYPE_TARGET_TYPE (type);
679 if (TYPE_LENGTH (type) != TYPE_LENGTH (target_type))
680 {
681 /* Obscure case of range type that has different length from
682 its base type. Perform a conversion, or we will get a
683 nonsense value. Actually, we could use the same
684 code regardless of lengths; I'm just avoiding a cast. */
685 struct value *v = value_cast (target_type,
686 value_from_contents_and_address
687 (type, valaddr, 0));
688 return ada_val_print_1 (target_type, VALUE_CONTENTS (v), 0, 0,
689 stream, format, 0, recurse + 1, pretty);
690 }
691 else
692 return ada_val_print_1 (TYPE_TARGET_TYPE (type),
693 valaddr0, embedded_offset,
694 address, stream, format, deref_ref,
695 recurse, pretty);
696 }
697 else
698 {
699 format = format ? format : output_format;
700 if (format)
701 {
702 print_scalar_formatted (valaddr, type, format, 0, stream);
703 }
704 else if (ada_is_system_address_type (type))
705 {
706 /* FIXME: We want to print System.Address variables using
707 the same format as for any access type. But for some
708 reason GNAT encodes the System.Address type as an int,
709 so we have to work-around this deficiency by handling
710 System.Address values as a special case. */
711 fprintf_filtered (stream, "(");
712 type_print (type, "", stream, -1);
713 fprintf_filtered (stream, ") ");
714 print_address_numeric
715 (extract_typed_address (valaddr, builtin_type_void_data_ptr),
716 1, stream);
717 }
718 else
719 {
720 val_print_type_code_int (type, valaddr, stream);
721 if (ada_is_character_type (type))
722 {
723 fputs_filtered (" ", stream);
724 ada_printchar ((unsigned char) unpack_long (type, valaddr),
725 stream);
726 }
727 }
728 return 0;
729 }
730
731 case TYPE_CODE_ENUM:
732 if (format)
733 {
734 print_scalar_formatted (valaddr, type, format, 0, stream);
735 break;
736 }
737 len = TYPE_NFIELDS (type);
738 val = unpack_long (type, valaddr);
739 for (i = 0; i < len; i++)
740 {
741 QUIT;
742 if (val == TYPE_FIELD_BITPOS (type, i))
743 {
744 break;
745 }
746 }
747 if (i < len)
748 {
749 const char *name = ada_enum_name (TYPE_FIELD_NAME (type, i));
750 if (name[0] == '\'')
751 fprintf_filtered (stream, "%ld %s", (long) val, name);
752 else
753 fputs_filtered (name, stream);
754 }
755 else
756 {
757 print_longest (stream, 'd', 0, val);
758 }
759 break;
760
761 case TYPE_CODE_FLT:
762 if (format)
763 return c_val_print (type, valaddr0, embedded_offset, address, stream,
764 format, deref_ref, recurse, pretty);
765 else
766 ada_print_floating (valaddr0 + embedded_offset, type, stream);
767 break;
768
769 case TYPE_CODE_UNION:
770 case TYPE_CODE_STRUCT:
771 if (ada_is_bogus_array_descriptor (type))
772 {
773 fprintf_filtered (stream, "(...?)");
774 return 0;
775 }
776 else
777 {
778 print_record (type, valaddr, stream, format, recurse, pretty);
779 return 0;
780 }
781
782 case TYPE_CODE_ARRAY:
783 elttype = TYPE_TARGET_TYPE (type);
784 if (elttype == NULL)
785 eltlen = 0;
786 else
787 eltlen = TYPE_LENGTH (elttype);
788 /* FIXME: This doesn't deal with non-empty arrays of
789 0-length items (not a typical case!) */
790 if (eltlen == 0)
791 len = 0;
792 else
793 len = TYPE_LENGTH (type) / eltlen;
794
795 /* For an array of chars, print with string syntax. */
796 if (ada_is_string_type (type) && (format == 0 || format == 's'))
797 {
798 if (prettyprint_arrays)
799 {
800 print_spaces_filtered (2 + 2 * recurse, stream);
801 }
802 /* If requested, look for the first null char and only print
803 elements up to it. */
804 if (stop_print_at_null)
805 {
806 int temp_len;
807
808 /* Look for a NULL char. */
809 for (temp_len = 0;
810 temp_len < len && temp_len < print_max
811 && char_at (valaddr, temp_len, eltlen) != 0;
812 temp_len += 1);
813 len = temp_len;
814 }
815
816 printstr (stream, valaddr, len, 0, eltlen);
817 }
818 else
819 {
820 len = 0;
821 fprintf_filtered (stream, "(");
822 print_optional_low_bound (stream, type);
823 if (TYPE_FIELD_BITSIZE (type, 0) > 0)
824 val_print_packed_array_elements (type, valaddr, 0, stream,
825 format, recurse, pretty);
826 else
827 val_print_array_elements (type, valaddr, address, stream,
828 format, deref_ref, recurse,
829 pretty, 0);
830 fprintf_filtered (stream, ")");
831 }
832 gdb_flush (stream);
833 return len;
834
835 case TYPE_CODE_REF:
836 elttype = check_typedef (TYPE_TARGET_TYPE (type));
837 /* De-reference the reference */
838 if (deref_ref)
839 {
840 if (TYPE_CODE (elttype) != TYPE_CODE_UNDEF)
841 {
842 LONGEST deref_val_int = (LONGEST)
843 unpack_pointer (lookup_pointer_type (builtin_type_void),
844 valaddr);
845 if (deref_val_int != 0)
846 {
847 struct value *deref_val =
848 ada_value_ind (value_from_longest
849 (lookup_pointer_type (elttype),
850 deref_val_int));
851 val_print (VALUE_TYPE (deref_val),
852 VALUE_CONTENTS (deref_val), 0,
853 VALUE_ADDRESS (deref_val), stream, format,
854 deref_ref, recurse + 1, pretty);
855 }
856 else
857 fputs_filtered ("(null)", stream);
858 }
859 else
860 fputs_filtered ("???", stream);
861 }
862 break;
863 }
864 gdb_flush (stream);
865 return 0;
866 }
867
868 static int
869 print_variant_part (struct type *type, int field_num, char *valaddr,
870 struct ui_file *stream, int format, int recurse,
871 enum val_prettyprint pretty, int comma_needed,
872 struct type *outer_type, char *outer_valaddr)
873 {
874 struct type *var_type = TYPE_FIELD_TYPE (type, field_num);
875 int which = ada_which_variant_applies (var_type, outer_type, outer_valaddr);
876
877 if (which < 0)
878 return 0;
879 else
880 return print_field_values
881 (TYPE_FIELD_TYPE (var_type, which),
882 valaddr + TYPE_FIELD_BITPOS (type, field_num) / HOST_CHAR_BIT
883 + TYPE_FIELD_BITPOS (var_type, which) / HOST_CHAR_BIT,
884 stream, format, recurse, pretty,
885 comma_needed, outer_type, outer_valaddr);
886 }
887
888 int
889 ada_value_print (struct value *val0, struct ui_file *stream, int format,
890 enum val_prettyprint pretty)
891 {
892 char *valaddr = VALUE_CONTENTS (val0);
893 CORE_ADDR address = VALUE_ADDRESS (val0) + VALUE_OFFSET (val0);
894 struct type *type =
895 ada_to_fixed_type (VALUE_TYPE (val0), valaddr, address, NULL);
896 struct value *val =
897 value_from_contents_and_address (type, valaddr, address);
898
899 /* If it is a pointer, indicate what it points to. */
900 if (TYPE_CODE (type) == TYPE_CODE_PTR)
901 {
902 /* Hack: don't print (char *) for char strings. Their
903 type is indicated by the quoted string anyway. */
904 if (TYPE_LENGTH (TYPE_TARGET_TYPE (type)) != sizeof (char)
905 || TYPE_CODE (TYPE_TARGET_TYPE (type)) != TYPE_CODE_INT
906 || TYPE_UNSIGNED (TYPE_TARGET_TYPE (type)))
907 {
908 fprintf_filtered (stream, "(");
909 type_print (type, "", stream, -1);
910 fprintf_filtered (stream, ") ");
911 }
912 }
913 else if (ada_is_array_descriptor_type (type))
914 {
915 fprintf_filtered (stream, "(");
916 type_print (type, "", stream, -1);
917 fprintf_filtered (stream, ") ");
918 }
919 else if (ada_is_bogus_array_descriptor (type))
920 {
921 fprintf_filtered (stream, "(");
922 type_print (type, "", stream, -1);
923 fprintf_filtered (stream, ") (...?)");
924 return 0;
925 }
926
927 if (TYPE_CODE (type) == TYPE_CODE_ARRAY
928 && TYPE_LENGTH (TYPE_TARGET_TYPE (type)) == 0
929 && TYPE_CODE (TYPE_INDEX_TYPE (type)) == TYPE_CODE_RANGE)
930 {
931 /* This is an array of zero-length elements, that is an array
932 of null records. This array needs to be printed by hand,
933 as the standard routine to print arrays relies on the size of
934 the array elements to be nonzero. This is because it computes
935 the number of elements in the array by dividing the array size
936 by the array element size. */
937 fprintf_filtered (stream, "(%d .. %d => ())",
938 TYPE_LOW_BOUND (TYPE_INDEX_TYPE (type)),
939 TYPE_HIGH_BOUND (TYPE_INDEX_TYPE (type)));
940 return 0;
941 }
942
943 return (val_print (type, VALUE_CONTENTS (val), 0, address,
944 stream, format, 1, 0, pretty));
945 }
946
947 static void
948 print_record (struct type *type, char *valaddr, struct ui_file *stream,
949 int format, int recurse, enum val_prettyprint pretty)
950 {
951 CHECK_TYPEDEF (type);
952
953 fprintf_filtered (stream, "(");
954
955 if (print_field_values (type, valaddr, stream, format, recurse, pretty,
956 0, type, valaddr) != 0 && pretty)
957 {
958 fprintf_filtered (stream, "\n");
959 print_spaces_filtered (2 * recurse, stream);
960 }
961
962 fprintf_filtered (stream, ")");
963 }
964
965 /* Print out fields of value at VALADDR having structure type TYPE.
966
967 TYPE, VALADDR, STREAM, FORMAT, RECURSE, and PRETTY have the
968 same meanings as in ada_print_value and ada_val_print.
969
970 OUTER_TYPE and OUTER_VALADDR give type and address of enclosing record
971 (used to get discriminant values when printing variant parts).
972
973 COMMA_NEEDED is 1 if fields have been printed at the current recursion
974 level, so that a comma is needed before any field printed by this
975 call.
976
977 Returns 1 if COMMA_NEEDED or any fields were printed. */
978
979 static int
980 print_field_values (struct type *type, char *valaddr, struct ui_file *stream,
981 int format, int recurse, enum val_prettyprint pretty,
982 int comma_needed, struct type *outer_type,
983 char *outer_valaddr)
984 {
985 int i, len;
986
987 len = TYPE_NFIELDS (type);
988
989 for (i = 0; i < len; i += 1)
990 {
991 if (ada_is_ignored_field (type, i))
992 continue;
993
994 if (ada_is_wrapper_field (type, i))
995 {
996 comma_needed =
997 print_field_values (TYPE_FIELD_TYPE (type, i),
998 valaddr
999 + TYPE_FIELD_BITPOS (type, i) / HOST_CHAR_BIT,
1000 stream, format, recurse, pretty,
1001 comma_needed, type, valaddr);
1002 continue;
1003 }
1004 else if (ada_is_variant_part (type, i))
1005 {
1006 comma_needed =
1007 print_variant_part (type, i, valaddr,
1008 stream, format, recurse, pretty, comma_needed,
1009 outer_type, outer_valaddr);
1010 continue;
1011 }
1012
1013 if (comma_needed)
1014 fprintf_filtered (stream, ", ");
1015 comma_needed = 1;
1016
1017 if (pretty)
1018 {
1019 fprintf_filtered (stream, "\n");
1020 print_spaces_filtered (2 + 2 * recurse, stream);
1021 }
1022 else
1023 {
1024 wrap_here (n_spaces (2 + 2 * recurse));
1025 }
1026 if (inspect_it)
1027 {
1028 if (TYPE_CODE (TYPE_FIELD_TYPE (type, i)) == TYPE_CODE_PTR)
1029 fputs_filtered ("\"( ptr \"", stream);
1030 else
1031 fputs_filtered ("\"( nodef \"", stream);
1032 fprintf_symbol_filtered (stream, TYPE_FIELD_NAME (type, i),
1033 language_cplus, DMGL_NO_OPTS);
1034 fputs_filtered ("\" \"", stream);
1035 fprintf_symbol_filtered (stream, TYPE_FIELD_NAME (type, i),
1036 language_cplus, DMGL_NO_OPTS);
1037 fputs_filtered ("\") \"", stream);
1038 }
1039 else
1040 {
1041 annotate_field_begin (TYPE_FIELD_TYPE (type, i));
1042 fprintf_filtered (stream, "%.*s",
1043 ada_name_prefix_len (TYPE_FIELD_NAME (type, i)),
1044 TYPE_FIELD_NAME (type, i));
1045 annotate_field_name_end ();
1046 fputs_filtered (" => ", stream);
1047 annotate_field_value ();
1048 }
1049
1050 if (TYPE_FIELD_PACKED (type, i))
1051 {
1052 struct value *v;
1053
1054 /* Bitfields require special handling, especially due to byte
1055 order problems. */
1056 if (TYPE_CPLUS_SPECIFIC (type) != NULL
1057 && TYPE_FIELD_IGNORE (type, i))
1058 {
1059 fputs_filtered ("<optimized out or zero length>", stream);
1060 }
1061 else
1062 {
1063 int bit_pos = TYPE_FIELD_BITPOS (type, i);
1064 int bit_size = TYPE_FIELD_BITSIZE (type, i);
1065
1066 adjust_type_signedness (TYPE_FIELD_TYPE (type, i));
1067 v = ada_value_primitive_packed_val (NULL, valaddr,
1068 bit_pos / HOST_CHAR_BIT,
1069 bit_pos % HOST_CHAR_BIT,
1070 bit_size,
1071 TYPE_FIELD_TYPE (type, i));
1072 val_print (TYPE_FIELD_TYPE (type, i), VALUE_CONTENTS (v), 0, 0,
1073 stream, format, 0, recurse + 1, pretty);
1074 }
1075 }
1076 else
1077 ada_val_print (TYPE_FIELD_TYPE (type, i),
1078 valaddr + TYPE_FIELD_BITPOS (type, i) / HOST_CHAR_BIT,
1079 0, 0, stream, format, 0, recurse + 1, pretty);
1080 annotate_field_end ();
1081 }
1082
1083 return comma_needed;
1084 }
This page took 0.057768 seconds and 4 git commands to generate.