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