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