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