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