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