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