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