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