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