* arm-tdep.c: Include "observer.h".
[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,
7b6bb8da 4 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011
4c38e0a4 5 Free Software Foundation, Inc.
14f9c5c9 6
a9762ec7 7 This file is part of GDB.
14f9c5c9 8
a9762ec7
JB
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3 of the License, or
12 (at your option) any later version.
14f9c5c9 13
a9762ec7
JB
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
14f9c5c9 18
a9762ec7
JB
19 You should have received a copy of the GNU General Public License
20 along with this program. If not, see <http://www.gnu.org/licenses/>. */
14f9c5c9 21
14f9c5c9 22#include "defs.h"
12c89474 23#include <ctype.h>
4c4b4cd2 24#include "gdb_string.h"
14f9c5c9
AS
25#include "symtab.h"
26#include "gdbtypes.h"
27#include "expression.h"
28#include "value.h"
29#include "demangle.h"
30#include "valprint.h"
31#include "language.h"
32#include "annotate.h"
33#include "ada-lang.h"
34#include "c-lang.h"
04714b91 35#include "infcall.h"
60250e8b 36#include "exceptions.h"
8ca1c40e 37#include "objfiles.h"
14f9c5c9 38
490f124f
PA
39static void print_record (struct type *, const gdb_byte *, int,
40 struct ui_file *,
0e03807e
TT
41 int,
42 const struct value *,
43 const struct value_print_options *);
14f9c5c9 44
fc1a4b47 45static int print_field_values (struct type *, const gdb_byte *,
490f124f 46 int,
79a45b7d 47 struct ui_file *, int,
0e03807e 48 const struct value *,
79a45b7d 49 const struct value_print_options *,
490f124f 50 int, struct type *, int);
14f9c5c9 51
d2e4a39e 52static void adjust_type_signedness (struct type *);
14f9c5c9 53
fc1a4b47 54static int ada_val_print_1 (struct type *, const gdb_byte *, int, CORE_ADDR,
79a45b7d 55 struct ui_file *, int,
0e03807e 56 const struct value *,
79a45b7d 57 const struct value_print_options *);
14f9c5c9
AS
58\f
59
4c4b4cd2 60/* Make TYPE unsigned if its range of values includes no negatives. */
d2e4a39e 61static void
4dc81987 62adjust_type_signedness (struct type *type)
14f9c5c9 63{
d2e4a39e 64 if (type != NULL && TYPE_CODE (type) == TYPE_CODE_RANGE
14f9c5c9 65 && TYPE_LOW_BOUND (type) >= 0)
876cecd0 66 TYPE_UNSIGNED (type) = 1;
d2e4a39e 67}
14f9c5c9 68
e936309c
JB
69/* Assuming TYPE is a simple array type, prints its lower bound on STREAM,
70 if non-standard (i.e., other than 1 for numbers, other than lower bound
71 of index type for enumerated type). Returns 1 if something printed,
72 otherwise 0. */
14f9c5c9 73
d2e4a39e 74static int
79a45b7d
TT
75print_optional_low_bound (struct ui_file *stream, struct type *type,
76 const struct value_print_options *options)
14f9c5c9
AS
77{
78 struct type *index_type;
df178451
PM
79 LONGEST low_bound;
80 LONGEST high_bound;
14f9c5c9 81
79a45b7d 82 if (options->print_array_indexes)
14f9c5c9 83 return 0;
e79af960 84
e936309c
JB
85 if (!get_array_bounds (type, &low_bound, &high_bound))
86 return 0;
87
88 /* If this is an empty array, then don't print the lower bound.
89 That would be confusing, because we would print the lower bound,
90 followed by... nothing! */
91 if (low_bound > high_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 {
690cc4eb
PH
108 case TYPE_CODE_BOOL:
109 if (low_bound == 0)
110 return 0;
111 break;
d2e4a39e
AS
112 case TYPE_CODE_ENUM:
113 if (low_bound == TYPE_FIELD_BITPOS (index_type, 0))
114 return 0;
115 break;
116 case TYPE_CODE_UNDEF:
7c964f07 117 index_type = NULL;
d2e4a39e
AS
118 /* FALL THROUGH */
119 default:
120 if (low_bound == 1)
121 return 0;
122 break;
123 }
14f9c5c9 124
df178451 125 ada_print_scalar (index_type, low_bound, stream);
14f9c5c9
AS
126 fprintf_filtered (stream, " => ");
127 return 1;
128}
129
130/* Version of val_print_array_elements for GNAT-style packed arrays.
131 Prints elements of packed array of type TYPE at bit offset
79a45b7d 132 BITOFFSET from VALADDR on STREAM. Formats according to OPTIONS and
4c4b4cd2 133 separates with commas. RECURSE is the recursion (nesting) level.
79a45b7d 134 TYPE must have been decoded (as by ada_coerce_to_simple_array). */
14f9c5c9
AS
135
136static void
fc1a4b47 137val_print_packed_array_elements (struct type *type, const gdb_byte *valaddr,
490f124f 138 int offset,
ebf56fd3 139 int bitoffset, struct ui_file *stream,
79a45b7d 140 int recurse,
0e03807e 141 const struct value *val,
79a45b7d 142 const struct value_print_options *options)
14f9c5c9
AS
143{
144 unsigned int i;
145 unsigned int things_printed = 0;
146 unsigned len;
e79af960 147 struct type *elttype, *index_type;
14f9c5c9 148 unsigned eltlen;
14f9c5c9 149 unsigned long bitsize = TYPE_FIELD_BITSIZE (type, 0);
d2e4a39e 150 struct value *mark = value_mark ();
e79af960 151 LONGEST low = 0;
d2e4a39e 152
14f9c5c9
AS
153 elttype = TYPE_TARGET_TYPE (type);
154 eltlen = TYPE_LENGTH (check_typedef (elttype));
e79af960 155 index_type = TYPE_INDEX_TYPE (type);
14f9c5c9
AS
156
157 {
e79af960 158 LONGEST high;
5b4ee69b 159
262452ec 160 if (get_discrete_bounds (index_type, &low, &high) < 0)
14f9c5c9
AS
161 len = 1;
162 else
163 len = high - low + 1;
164 }
165
166 i = 0;
167 annotate_array_section_begin (i, elttype);
168
79a45b7d 169 while (i < len && things_printed < options->print_max)
14f9c5c9
AS
170 {
171 struct value *v0, *v1;
172 int i0;
173
174 if (i != 0)
175 {
79a45b7d 176 if (options->prettyprint_arrays)
14f9c5c9
AS
177 {
178 fprintf_filtered (stream, ",\n");
179 print_spaces_filtered (2 + 2 * recurse, stream);
180 }
181 else
182 {
183 fprintf_filtered (stream, ", ");
184 }
185 }
186 wrap_here (n_spaces (2 + 2 * recurse));
79a45b7d 187 maybe_print_array_index (index_type, i + low, stream, options);
14f9c5c9
AS
188
189 i0 = i;
490f124f 190 v0 = ada_value_primitive_packed_val (NULL, valaddr + offset,
14f9c5c9
AS
191 (i0 * bitsize) / HOST_CHAR_BIT,
192 (i0 * bitsize) % HOST_CHAR_BIT,
193 bitsize, elttype);
194 while (1)
195 {
196 i += 1;
197 if (i >= len)
198 break;
490f124f 199 v1 = ada_value_primitive_packed_val (NULL, valaddr + offset,
14f9c5c9
AS
200 (i * bitsize) / HOST_CHAR_BIT,
201 (i * bitsize) % HOST_CHAR_BIT,
202 bitsize, elttype);
0fd88904 203 if (memcmp (value_contents (v0), value_contents (v1), eltlen) != 0)
14f9c5c9
AS
204 break;
205 }
206
79a45b7d 207 if (i - i0 > options->repeat_count_threshold)
14f9c5c9 208 {
79a45b7d 209 struct value_print_options opts = *options;
5b4ee69b 210
79a45b7d 211 opts.deref_ref = 0;
490f124f
PA
212 val_print (elttype, value_contents_for_printing (v0),
213 value_embedded_offset (v0), 0, stream,
a491d753 214 recurse + 1, v0, &opts, current_language);
14f9c5c9 215 annotate_elt_rep (i - i0);
edefbb7c 216 fprintf_filtered (stream, _(" <repeats %u times>"), i - i0);
14f9c5c9
AS
217 annotate_elt_rep_end ();
218
219 }
220 else
221 {
222 int j;
79a45b7d 223 struct value_print_options opts = *options;
5b4ee69b 224
79a45b7d 225 opts.deref_ref = 0;
14f9c5c9
AS
226 for (j = i0; j < i; j += 1)
227 {
d2e4a39e 228 if (j > i0)
14f9c5c9 229 {
79a45b7d 230 if (options->prettyprint_arrays)
14f9c5c9
AS
231 {
232 fprintf_filtered (stream, ",\n");
233 print_spaces_filtered (2 + 2 * recurse, stream);
234 }
235 else
236 {
237 fprintf_filtered (stream, ", ");
238 }
239 wrap_here (n_spaces (2 + 2 * recurse));
e79af960 240 maybe_print_array_index (index_type, j + low,
79a45b7d 241 stream, options);
14f9c5c9 242 }
490f124f
PA
243 val_print (elttype, value_contents_for_printing (v0),
244 value_embedded_offset (v0), 0, stream,
a491d753 245 recurse + 1, v0, &opts, current_language);
14f9c5c9
AS
246 annotate_elt ();
247 }
248 }
249 things_printed += i - i0;
250 }
251 annotate_array_section_end ();
252 if (i < len)
253 {
254 fprintf_filtered (stream, "...");
255 }
256
257 value_free_to_mark (mark);
258}
259
d2e4a39e 260static struct type *
fc1a4b47 261printable_val_type (struct type *type, const gdb_byte *valaddr)
14f9c5c9 262{
1ed6ede0 263 return ada_to_fixed_type (ada_aligned_type (type), valaddr, 0, NULL, 1);
14f9c5c9
AS
264}
265
266/* Print the character C on STREAM as part of the contents of a literal
267 string whose delimiter is QUOTER. TYPE_LEN is the length in bytes
4ffa5a33 268 of the character. */
14f9c5c9
AS
269
270void
6c7a06a3
TT
271ada_emit_char (int c, struct type *type, struct ui_file *stream,
272 int quoter, int type_len)
14f9c5c9 273{
4ffa5a33
JB
274 /* If this character fits in the normal ASCII range, and is
275 a printable character, then print the character as if it was
276 an ASCII character, even if this is a wide character.
277 The UCHAR_MAX check is necessary because the isascii function
278 requires that its argument have a value of an unsigned char,
279 or EOF (EOF is obviously not printable). */
280 if (c <= UCHAR_MAX && isascii (c) && isprint (c))
14f9c5c9
AS
281 {
282 if (c == quoter && c == '"')
529cad9c 283 fprintf_filtered (stream, "\"\"");
14f9c5c9
AS
284 else
285 fprintf_filtered (stream, "%c", c);
286 }
287 else
d2e4a39e 288 fprintf_filtered (stream, "[\"%0*x\"]", type_len * 2, c);
14f9c5c9
AS
289}
290
4ffa5a33
JB
291/* Character #I of STRING, given that TYPE_LEN is the size in bytes
292 of a character. */
14f9c5c9
AS
293
294static int
e17a4113
UW
295char_at (const gdb_byte *string, int i, int type_len,
296 enum bfd_endian byte_order)
14f9c5c9
AS
297{
298 if (type_len == 1)
299 return string[i];
d2e4a39e 300 else
4ffa5a33
JB
301 return (int) extract_unsigned_integer (string + type_len * i,
302 type_len, byte_order);
14f9c5c9
AS
303}
304
0963b4bd 305/* Wrapper around memcpy to make it legal argument to ui_file_put. */
4c4b4cd2
PH
306static void
307ui_memcpy (void *dest, const char *buffer, long len)
308{
309 memcpy (dest, buffer, (size_t) len);
310 ((char *) dest)[len] = '\0';
311}
312
313/* Print a floating-point value of type TYPE, pointed to in GDB by
314 VALADDR, on STREAM. Use Ada formatting conventions: there must be
315 a decimal point, and at least one digit before and after the
316 point. We use GNAT format for NaNs and infinities. */
317static void
fc1a4b47 318ada_print_floating (const gdb_byte *valaddr, struct type *type,
a2bd3dcd 319 struct ui_file *stream)
4c4b4cd2
PH
320{
321 char buffer[64];
322 char *s, *result;
323 int len;
324 struct ui_file *tmp_stream = mem_fileopen ();
325 struct cleanup *cleanups = make_cleanup_ui_file_delete (tmp_stream);
326
327 print_floating (valaddr, type, tmp_stream);
328 ui_file_put (tmp_stream, ui_memcpy, buffer);
329 do_cleanups (cleanups);
330
331 result = buffer;
332 len = strlen (result);
333
334 /* Modify for Ada rules. */
606b8d1a 335
c3e5cd34
PH
336 s = strstr (result, "inf");
337 if (s == NULL)
338 s = strstr (result, "Inf");
339 if (s == NULL)
340 s = strstr (result, "INF");
341 if (s != NULL)
4c4b4cd2 342 strcpy (s, "Inf");
c3e5cd34
PH
343
344 if (s == NULL)
4c4b4cd2 345 {
c3e5cd34
PH
346 s = strstr (result, "nan");
347 if (s == NULL)
348 s = strstr (result, "NaN");
349 if (s == NULL)
350 s = strstr (result, "Nan");
351 if (s != NULL)
352 {
353 s[0] = s[2] = 'N';
354 if (result[0] == '-')
355 result += 1;
356 }
4c4b4cd2 357 }
c3e5cd34
PH
358
359 if (s == NULL && strchr (result, '.') == NULL)
4c4b4cd2 360 {
c3e5cd34
PH
361 s = strchr (result, 'e');
362 if (s == NULL)
4c4b4cd2
PH
363 fprintf_filtered (stream, "%s.0", result);
364 else
365 fprintf_filtered (stream, "%.*s.0%s", (int) (s-result), result, s);
366 return;
367 }
368 fprintf_filtered (stream, "%s", result);
369}
370
14f9c5c9 371void
6c7a06a3 372ada_printchar (int c, struct type *type, struct ui_file *stream)
14f9c5c9
AS
373{
374 fputs_filtered ("'", stream);
447b483c 375 ada_emit_char (c, type, stream, '\'', TYPE_LENGTH (type));
14f9c5c9
AS
376 fputs_filtered ("'", stream);
377}
378
379/* [From print_type_scalar in typeprint.c]. Print VAL on STREAM in a
7c964f07
UW
380 form appropriate for TYPE, if non-NULL. If TYPE is NULL, print VAL
381 like a default signed integer. */
14f9c5c9
AS
382
383void
ebf56fd3 384ada_print_scalar (struct type *type, LONGEST val, struct ui_file *stream)
14f9c5c9
AS
385{
386 unsigned int i;
387 unsigned len;
388
7c964f07
UW
389 if (!type)
390 {
391 print_longest (stream, 'd', 0, val);
392 return;
393 }
394
61ee279c 395 type = ada_check_typedef (type);
14f9c5c9
AS
396
397 switch (TYPE_CODE (type))
398 {
399
400 case TYPE_CODE_ENUM:
401 len = TYPE_NFIELDS (type);
402 for (i = 0; i < len; i++)
403 {
404 if (TYPE_FIELD_BITPOS (type, i) == val)
405 {
406 break;
407 }
408 }
409 if (i < len)
410 {
411 fputs_filtered (ada_enum_name (TYPE_FIELD_NAME (type, i)), stream);
412 }
413 else
414 {
415 print_longest (stream, 'd', 0, val);
416 }
417 break;
418
419 case TYPE_CODE_INT:
420 print_longest (stream, TYPE_UNSIGNED (type) ? 'u' : 'd', 0, val);
421 break;
422
423 case TYPE_CODE_CHAR:
10d44370 424 LA_PRINT_CHAR (val, type, stream);
14f9c5c9
AS
425 break;
426
427 case TYPE_CODE_BOOL:
428 fprintf_filtered (stream, val ? "true" : "false");
429 break;
430
431 case TYPE_CODE_RANGE:
432 ada_print_scalar (TYPE_TARGET_TYPE (type), val, stream);
433 return;
434
435 case TYPE_CODE_UNDEF:
436 case TYPE_CODE_PTR:
437 case TYPE_CODE_ARRAY:
438 case TYPE_CODE_STRUCT:
439 case TYPE_CODE_UNION:
440 case TYPE_CODE_FUNC:
441 case TYPE_CODE_FLT:
442 case TYPE_CODE_VOID:
443 case TYPE_CODE_SET:
444 case TYPE_CODE_STRING:
445 case TYPE_CODE_ERROR:
0d5de010
DJ
446 case TYPE_CODE_MEMBERPTR:
447 case TYPE_CODE_METHODPTR:
14f9c5c9
AS
448 case TYPE_CODE_METHOD:
449 case TYPE_CODE_REF:
edefbb7c 450 warning (_("internal error: unhandled type in ada_print_scalar"));
14f9c5c9
AS
451 break;
452
453 default:
edefbb7c 454 error (_("Invalid type code in symbol table."));
14f9c5c9
AS
455 }
456 gdb_flush (stream);
457}
458
459/* Print the character string STRING, printing at most LENGTH characters.
460 Printing stops early if the number hits print_max; repeat counts
461 are printed as appropriate. Print ellipses at the end if we
9a153e0b
JB
462 had to stop before printing LENGTH characters, or if FORCE_ELLIPSES.
463 TYPE_LEN is the length (1 or 2) of the character type. */
14f9c5c9
AS
464
465static void
6c7a06a3 466printstr (struct ui_file *stream, struct type *elttype, const gdb_byte *string,
79a45b7d
TT
467 unsigned int length, int force_ellipses, int type_len,
468 const struct value_print_options *options)
14f9c5c9 469{
e17a4113 470 enum bfd_endian byte_order = gdbarch_byte_order (get_type_arch (elttype));
14f9c5c9
AS
471 unsigned int i;
472 unsigned int things_printed = 0;
473 int in_quotes = 0;
474 int need_comma = 0;
475
476 if (length == 0)
477 {
478 fputs_filtered ("\"\"", stream);
479 return;
480 }
481
79a45b7d 482 for (i = 0; i < length && things_printed < options->print_max; i += 1)
14f9c5c9
AS
483 {
484 /* Position of the character we are examining
d2e4a39e 485 to see whether it is repeated. */
14f9c5c9
AS
486 unsigned int rep1;
487 /* Number of repetitions we have detected so far. */
488 unsigned int reps;
489
490 QUIT;
491
492 if (need_comma)
493 {
494 fputs_filtered (", ", stream);
495 need_comma = 0;
496 }
497
498 rep1 = i + 1;
499 reps = 1;
c3e5cd34 500 while (rep1 < length
e17a4113
UW
501 && char_at (string, rep1, type_len, byte_order)
502 == char_at (string, i, type_len, byte_order))
14f9c5c9
AS
503 {
504 rep1 += 1;
505 reps += 1;
506 }
507
79a45b7d 508 if (reps > options->repeat_count_threshold)
14f9c5c9
AS
509 {
510 if (in_quotes)
511 {
79a45b7d 512 if (options->inspect_it)
14f9c5c9
AS
513 fputs_filtered ("\\\", ", stream);
514 else
515 fputs_filtered ("\", ", stream);
516 in_quotes = 0;
517 }
518 fputs_filtered ("'", stream);
e17a4113
UW
519 ada_emit_char (char_at (string, i, type_len, byte_order),
520 elttype, stream, '\'', type_len);
14f9c5c9 521 fputs_filtered ("'", stream);
edefbb7c 522 fprintf_filtered (stream, _(" <repeats %u times>"), reps);
14f9c5c9 523 i = rep1 - 1;
79a45b7d 524 things_printed += options->repeat_count_threshold;
14f9c5c9
AS
525 need_comma = 1;
526 }
527 else
528 {
529 if (!in_quotes)
530 {
79a45b7d 531 if (options->inspect_it)
14f9c5c9
AS
532 fputs_filtered ("\\\"", stream);
533 else
534 fputs_filtered ("\"", stream);
535 in_quotes = 1;
536 }
e17a4113
UW
537 ada_emit_char (char_at (string, i, type_len, byte_order),
538 elttype, stream, '"', type_len);
14f9c5c9
AS
539 things_printed += 1;
540 }
541 }
542
543 /* Terminate the quotes if necessary. */
544 if (in_quotes)
545 {
79a45b7d 546 if (options->inspect_it)
14f9c5c9
AS
547 fputs_filtered ("\\\"", stream);
548 else
549 fputs_filtered ("\"", stream);
550 }
551
552 if (force_ellipses || i < length)
553 fputs_filtered ("...", stream);
554}
555
556void
0963b4bd
MS
557ada_printstr (struct ui_file *stream, struct type *type,
558 const gdb_byte *string, unsigned int length,
559 const char *encoding, int force_ellipses,
79a45b7d 560 const struct value_print_options *options)
14f9c5c9 561{
6c7a06a3
TT
562 printstr (stream, type, string, length, force_ellipses, TYPE_LENGTH (type),
563 options);
14f9c5c9
AS
564}
565
566
567/* Print data of type TYPE located at VALADDR (within GDB), which came from
568 the inferior at address ADDRESS, onto stdio stream STREAM according to
79a45b7d 569 OPTIONS. The data at VALADDR is in target byte order.
14f9c5c9
AS
570
571 If the data is printed as a string, returns the number of string characters
572 printed.
573
14f9c5c9 574 RECURSE indicates the amount of indentation to supply before
79a45b7d 575 continuation lines; this amount is roughly twice the value of RECURSE. */
14f9c5c9
AS
576
577int
490f124f 578ada_val_print (struct type *type, const gdb_byte *valaddr,
a2bd3dcd 579 int embedded_offset, CORE_ADDR address,
79a45b7d 580 struct ui_file *stream, int recurse,
0e03807e 581 const struct value *val,
79a45b7d 582 const struct value_print_options *options)
14f9c5c9 583{
0e03807e
TT
584 volatile struct gdb_exception except;
585 int result = 0;
14f9c5c9 586
490f124f 587 /* XXX: this catches QUIT/ctrl-c as well. Isn't that busted? */
0e03807e
TT
588 TRY_CATCH (except, RETURN_MASK_ALL)
589 {
490f124f 590 result = ada_val_print_1 (type, valaddr, embedded_offset, address,
0e03807e
TT
591 stream, recurse, val, options);
592 }
5b4ee69b 593
0e03807e
TT
594 if (except.reason < 0)
595 result = 0;
596
597 return result;
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,
490f124f
PA
607 int offset, CORE_ADDR address,
608 struct ui_file *stream, int recurse,
0e03807e 609 const struct value *val,
79a45b7d 610 const struct value_print_options *options)
e936309c 611{
e17a4113 612 enum bfd_endian byte_order = gdbarch_byte_order (get_type_arch (type));
e936309c 613 struct type *elttype = TYPE_TARGET_TYPE (type);
e936309c
JB
614 int result = 0;
615
e936309c 616 /* For an array of chars, print with string syntax. */
79a45b7d
TT
617 if (ada_is_string_type (type)
618 && (options->format == 0 || options->format == 's'))
e936309c 619 {
9addb3b9
JB
620 unsigned int eltlen;
621 unsigned int len;
622
623 if (elttype == NULL)
624 eltlen = 0;
625 else
626 eltlen = TYPE_LENGTH (elttype);
627 if (eltlen == 0)
628 len = 0;
629 else
630 len = TYPE_LENGTH (type) / eltlen;
631
79a45b7d 632 if (options->prettyprint_arrays)
e936309c
JB
633 print_spaces_filtered (2 + 2 * recurse, stream);
634
635 /* If requested, look for the first null char and only print
636 elements up to it. */
79a45b7d 637 if (options->stop_print_at_null)
e936309c
JB
638 {
639 int temp_len;
640
641 /* Look for a NULL char. */
642 for (temp_len = 0;
643 (temp_len < len
79a45b7d 644 && temp_len < options->print_max
490f124f
PA
645 && char_at (valaddr + offset,
646 temp_len, eltlen, byte_order) != 0);
e936309c
JB
647 temp_len += 1);
648 len = temp_len;
649 }
650
490f124f 651 printstr (stream, elttype, valaddr + offset, len, 0, eltlen, options);
e936309c
JB
652 result = len;
653 }
654 else
655 {
656 fprintf_filtered (stream, "(");
79a45b7d 657 print_optional_low_bound (stream, type, options);
e936309c 658 if (TYPE_FIELD_BITSIZE (type, 0) > 0)
490f124f
PA
659 val_print_packed_array_elements (type, valaddr, offset,
660 0, stream, recurse, val, options);
e936309c 661 else
490f124f
PA
662 val_print_array_elements (type, valaddr, offset, address,
663 stream, recurse, val, options, 0);
e936309c
JB
664 fprintf_filtered (stream, ")");
665 }
666
667 return result;
668}
669
14f9c5c9 670/* See the comment on ada_val_print. This function differs in that it
e936309c 671 does not catch evaluation errors (leaving that to ada_val_print). */
14f9c5c9
AS
672
673static int
490f124f
PA
674ada_val_print_1 (struct type *type, const gdb_byte *valaddr,
675 int offset, CORE_ADDR address,
79a45b7d 676 struct ui_file *stream, int recurse,
0e03807e 677 const struct value *original_value,
79a45b7d 678 const struct value_print_options *options)
14f9c5c9
AS
679{
680 unsigned int len;
681 int i;
682 struct type *elttype;
14f9c5c9 683 LONGEST val;
490f124f 684 int offset_aligned;
14f9c5c9 685
61ee279c 686 type = ada_check_typedef (type);
14f9c5c9 687
ad82864c
JB
688 if (ada_is_array_descriptor_type (type)
689 || ada_is_constrained_packed_array_type (type))
14f9c5c9
AS
690 {
691 int retn;
d2e4a39e
AS
692 struct value *mark = value_mark ();
693 struct value *val;
5b4ee69b 694
490f124f 695 val = value_from_contents_and_address (type, valaddr + offset, address);
720d1a40
JB
696 if (TYPE_CODE (type) == TYPE_CODE_TYPEDEF) /* array access type. */
697 val = ada_coerce_to_simple_array_ptr (val);
698 else
699 val = ada_coerce_to_simple_array (val);
14f9c5c9
AS
700 if (val == NULL)
701 {
9e87a409
JB
702 gdb_assert (TYPE_CODE (type) == TYPE_CODE_TYPEDEF);
703 fprintf_filtered (stream, "0x0");
14f9c5c9
AS
704 retn = 0;
705 }
706 else
490f124f
PA
707 retn = ada_val_print_1 (value_type (val),
708 value_contents_for_printing (val),
709 value_embedded_offset (val),
0e03807e 710 value_address (val), stream, recurse,
a491d753 711 val, options);
14f9c5c9
AS
712 value_free_to_mark (mark);
713 return retn;
714 }
715
490f124f
PA
716 offset_aligned = offset + ada_aligned_value_addr (type, valaddr) - valaddr;
717 type = printable_val_type (type, valaddr + offset_aligned);
14f9c5c9
AS
718
719 switch (TYPE_CODE (type))
720 {
721 default:
490f124f 722 return c_val_print (type, valaddr, offset, address, stream,
0e03807e 723 recurse, original_value, options);
14f9c5c9 724
4c4b4cd2
PH
725 case TYPE_CODE_PTR:
726 {
490f124f 727 int ret = c_val_print (type, valaddr, offset, address,
0e03807e 728 stream, recurse, original_value, options);
5b4ee69b 729
4c4b4cd2
PH
730 if (ada_is_tag_type (type))
731 {
490f124f
PA
732 struct value *val =
733 value_from_contents_and_address (type,
734 valaddr + offset_aligned,
735 address + offset_aligned);
4c4b4cd2 736 const char *name = ada_tag_name (val);
5b4ee69b 737
4c4b4cd2
PH
738 if (name != NULL)
739 fprintf_filtered (stream, " (%s)", name);
740 return 0;
741 }
742 return ret;
743 }
744
14f9c5c9
AS
745 case TYPE_CODE_INT:
746 case TYPE_CODE_RANGE:
747 if (ada_is_fixed_point_type (type))
748 {
490f124f 749 LONGEST v = unpack_long (type, valaddr + offset_aligned);
14f9c5c9
AS
750 int len = TYPE_LENGTH (type);
751
752 fprintf_filtered (stream, len < 4 ? "%.11g" : "%.17g",
753 (double) ada_fixed_to_float (type, v));
754 return 0;
755 }
14f9c5c9
AS
756 else if (TYPE_CODE (type) == TYPE_CODE_RANGE)
757 {
d2e4a39e 758 struct type *target_type = TYPE_TARGET_TYPE (type);
5b4ee69b 759
14f9c5c9
AS
760 if (TYPE_LENGTH (type) != TYPE_LENGTH (target_type))
761 {
762 /* Obscure case of range type that has different length from
d2e4a39e
AS
763 its base type. Perform a conversion, or we will get a
764 nonsense value. Actually, we could use the same
4c4b4cd2 765 code regardless of lengths; I'm just avoiding a cast. */
490f124f
PA
766 struct value *v1
767 = value_from_contents_and_address (type, valaddr + offset, 0);
768 struct value *v = value_cast (target_type, v1);
769
770 return ada_val_print_1 (target_type,
771 value_contents_for_printing (v),
772 value_embedded_offset (v), 0,
a491d753 773 stream, recurse + 1, v, options);
14f9c5c9
AS
774 }
775 else
d2e4a39e 776 return ada_val_print_1 (TYPE_TARGET_TYPE (type),
490f124f 777 valaddr, offset,
0963b4bd
MS
778 address, stream, recurse,
779 original_value, options);
14f9c5c9 780 }
d2e4a39e 781 else
14f9c5c9 782 {
79a45b7d
TT
783 int format = (options->format ? options->format
784 : options->output_format);
5b4ee69b 785
14f9c5c9
AS
786 if (format)
787 {
79a45b7d 788 struct value_print_options opts = *options;
5b4ee69b 789
79a45b7d 790 opts.format = format;
ab2188aa
PA
791 val_print_scalar_formatted (type, valaddr, offset_aligned,
792 original_value, &opts, 0, stream);
14f9c5c9 793 }
50810684 794 else if (ada_is_system_address_type (type))
4c4b4cd2
PH
795 {
796 /* FIXME: We want to print System.Address variables using
797 the same format as for any access type. But for some
798 reason GNAT encodes the System.Address type as an int,
799 so we have to work-around this deficiency by handling
50810684 800 System.Address values as a special case. */
8ca1c40e 801
50810684 802 struct gdbarch *gdbarch = get_type_arch (type);
8ca1c40e 803 struct type *ptr_type = builtin_type (gdbarch)->builtin_data_ptr;
490f124f
PA
804 CORE_ADDR addr = extract_typed_address (valaddr + offset_aligned,
805 ptr_type);
8ca1c40e 806
4c4b4cd2
PH
807 fprintf_filtered (stream, "(");
808 type_print (type, "", stream, -1);
809 fprintf_filtered (stream, ") ");
5af949e3 810 fputs_filtered (paddress (gdbarch, addr), stream);
4c4b4cd2 811 }
14f9c5c9
AS
812 else
813 {
490f124f 814 val_print_type_code_int (type, valaddr + offset_aligned, stream);
14f9c5c9
AS
815 if (ada_is_character_type (type))
816 {
490f124f
PA
817 LONGEST c;
818
14f9c5c9 819 fputs_filtered (" ", stream);
490f124f
PA
820 c = unpack_long (type, valaddr + offset_aligned);
821 ada_printchar (c, type, stream);
14f9c5c9
AS
822 }
823 }
824 return 0;
825 }
826
827 case TYPE_CODE_ENUM:
79a45b7d 828 if (options->format)
14f9c5c9 829 {
ab2188aa
PA
830 val_print_scalar_formatted (type, valaddr, offset_aligned,
831 original_value, options, 0, stream);
14f9c5c9
AS
832 break;
833 }
834 len = TYPE_NFIELDS (type);
490f124f 835 val = unpack_long (type, valaddr + offset_aligned);
14f9c5c9
AS
836 for (i = 0; i < len; i++)
837 {
838 QUIT;
839 if (val == TYPE_FIELD_BITPOS (type, i))
840 {
841 break;
842 }
843 }
844 if (i < len)
845 {
d2e4a39e 846 const char *name = ada_enum_name (TYPE_FIELD_NAME (type, i));
5b4ee69b 847
d2e4a39e 848 if (name[0] == '\'')
14f9c5c9
AS
849 fprintf_filtered (stream, "%ld %s", (long) val, name);
850 else
851 fputs_filtered (name, stream);
852 }
853 else
854 {
855 print_longest (stream, 'd', 0, val);
856 }
857 break;
d2e4a39e 858
4f2aea11 859 case TYPE_CODE_FLAGS:
79a45b7d 860 if (options->format)
ab2188aa
PA
861 val_print_scalar_formatted (type, valaddr, offset_aligned,
862 original_value, options, 0, stream);
4f2aea11 863 else
490f124f 864 val_print_type_code_flags (type, valaddr + offset_aligned, stream);
4f2aea11
MK
865 break;
866
4c4b4cd2 867 case TYPE_CODE_FLT:
79a45b7d 868 if (options->format)
490f124f 869 return c_val_print (type, valaddr, offset, address, stream,
0e03807e 870 recurse, original_value, options);
4c4b4cd2 871 else
490f124f 872 ada_print_floating (valaddr + offset, type, stream);
4c4b4cd2
PH
873 break;
874
14f9c5c9
AS
875 case TYPE_CODE_UNION:
876 case TYPE_CODE_STRUCT:
877 if (ada_is_bogus_array_descriptor (type))
878 {
879 fprintf_filtered (stream, "(...?)");
880 return 0;
d2e4a39e 881 }
14f9c5c9
AS
882 else
883 {
490f124f
PA
884 print_record (type, valaddr, offset_aligned,
885 stream, recurse, original_value, options);
14f9c5c9
AS
886 return 0;
887 }
888
889 case TYPE_CODE_ARRAY:
490f124f
PA
890 return ada_val_print_array (type, valaddr, offset_aligned,
891 address, stream, recurse, original_value,
892 options);
14f9c5c9
AS
893
894 case TYPE_CODE_REF:
969a1360
JB
895 /* For references, the debugger is expected to print the value as
896 an address if DEREF_REF is null. But printing an address in place
897 of the object value would be confusing to an Ada programmer.
898 So, for Ada values, we print the actual dereferenced value
899 regardless. */
14f9c5c9 900 elttype = check_typedef (TYPE_TARGET_TYPE (type));
969a1360
JB
901
902 if (TYPE_CODE (elttype) != TYPE_CODE_UNDEF)
903 {
490f124f
PA
904 CORE_ADDR deref_val_int
905 = unpack_pointer (type, valaddr + offset_aligned);
5b4ee69b 906
969a1360
JB
907 if (deref_val_int != 0)
908 {
909 struct value *deref_val =
490f124f 910 ada_value_ind (value_from_pointer
969a1360
JB
911 (lookup_pointer_type (elttype),
912 deref_val_int));
5b4ee69b 913
969a1360 914 val_print (value_type (deref_val),
490f124f
PA
915 value_contents_for_printing (deref_val),
916 value_embedded_offset (deref_val),
42ae5230 917 value_address (deref_val), stream, recurse + 1,
a491d753 918 deref_val, options, current_language);
969a1360
JB
919 }
920 else
921 fputs_filtered ("(null)", stream);
922 }
923 else
924 fputs_filtered ("???", stream);
925
14f9c5c9
AS
926 break;
927 }
4c4b4cd2 928 gdb_flush (stream);
14f9c5c9
AS
929 return 0;
930}
931
932static int
490f124f
PA
933print_variant_part (struct type *type, int field_num,
934 const gdb_byte *valaddr, int offset,
79a45b7d 935 struct ui_file *stream, int recurse,
0e03807e 936 const struct value *val,
0963b4bd
MS
937 const struct value_print_options *options,
938 int comma_needed,
490f124f 939 struct type *outer_type, int outer_offset)
14f9c5c9
AS
940{
941 struct type *var_type = TYPE_FIELD_TYPE (type, field_num);
490f124f
PA
942 int which = ada_which_variant_applies (var_type, outer_type,
943 valaddr + outer_offset);
14f9c5c9
AS
944
945 if (which < 0)
946 return 0;
947 else
d2e4a39e 948 return print_field_values
14f9c5c9 949 (TYPE_FIELD_TYPE (var_type, which),
490f124f
PA
950 valaddr,
951 offset + TYPE_FIELD_BITPOS (type, field_num) / HOST_CHAR_BIT
14f9c5c9 952 + TYPE_FIELD_BITPOS (var_type, which) / HOST_CHAR_BIT,
0e03807e 953 stream, recurse, val, options,
490f124f 954 comma_needed, outer_type, outer_offset);
14f9c5c9
AS
955}
956
957int
79a45b7d
TT
958ada_value_print (struct value *val0, struct ui_file *stream,
959 const struct value_print_options *options)
14f9c5c9 960{
0c3acc09
JB
961 struct value *val = ada_to_fixed_value (val0);
962 CORE_ADDR address = value_address (val);
963 struct type *type = value_type (val);
79a45b7d 964 struct value_print_options opts;
14f9c5c9 965
4c4b4cd2
PH
966 /* If it is a pointer, indicate what it points to. */
967 if (TYPE_CODE (type) == TYPE_CODE_PTR)
14f9c5c9 968 {
4c4b4cd2
PH
969 /* Hack: don't print (char *) for char strings. Their
970 type is indicated by the quoted string anyway. */
971 if (TYPE_LENGTH (TYPE_TARGET_TYPE (type)) != sizeof (char)
972 || TYPE_CODE (TYPE_TARGET_TYPE (type)) != TYPE_CODE_INT
973 || TYPE_UNSIGNED (TYPE_TARGET_TYPE (type)))
14f9c5c9
AS
974 {
975 fprintf_filtered (stream, "(");
976 type_print (type, "", stream, -1);
977 fprintf_filtered (stream, ") ");
978 }
979 }
4c4b4cd2 980 else if (ada_is_array_descriptor_type (type))
14f9c5c9 981 {
720d1a40
JB
982 /* We do not print the type description unless TYPE is an array
983 access type (this is encoded by the compiler as a typedef to
984 a fat pointer - hence the check against TYPE_CODE_TYPEDEF). */
985 if (TYPE_CODE (type) == TYPE_CODE_TYPEDEF)
986 {
987 fprintf_filtered (stream, "(");
988 type_print (type, "", stream, -1);
989 fprintf_filtered (stream, ") ");
990 }
14f9c5c9
AS
991 }
992 else if (ada_is_bogus_array_descriptor (type))
993 {
994 fprintf_filtered (stream, "(");
995 type_print (type, "", stream, -1);
996 fprintf_filtered (stream, ") (...?)");
997 return 0;
998 }
4c4b4cd2 999
79a45b7d
TT
1000 opts = *options;
1001 opts.deref_ref = 1;
490f124f
PA
1002 return (val_print (type, value_contents_for_printing (val),
1003 value_embedded_offset (val), address,
0e03807e 1004 stream, 0, val, &opts, current_language));
14f9c5c9 1005}
d2e4a39e 1006
14f9c5c9 1007static void
fc1a4b47 1008print_record (struct type *type, const gdb_byte *valaddr,
490f124f 1009 int offset,
79a45b7d 1010 struct ui_file *stream, int recurse,
0e03807e 1011 const struct value *val,
79a45b7d 1012 const struct value_print_options *options)
14f9c5c9 1013{
61ee279c 1014 type = ada_check_typedef (type);
14f9c5c9
AS
1015
1016 fprintf_filtered (stream, "(");
1017
490f124f
PA
1018 if (print_field_values (type, valaddr, offset,
1019 stream, recurse, val, options,
1020 0, type, offset) != 0 && options->pretty)
14f9c5c9
AS
1021 {
1022 fprintf_filtered (stream, "\n");
1023 print_spaces_filtered (2 * recurse, stream);
1024 }
1025
1026 fprintf_filtered (stream, ")");
1027}
1028
1029/* Print out fields of value at VALADDR having structure type TYPE.
4c4b4cd2 1030
79a45b7d 1031 TYPE, VALADDR, STREAM, RECURSE, and OPTIONS have the
4c4b4cd2 1032 same meanings as in ada_print_value and ada_val_print.
14f9c5c9
AS
1033
1034 OUTER_TYPE and OUTER_VALADDR give type and address of enclosing record
1035 (used to get discriminant values when printing variant parts).
1036
4c4b4cd2 1037 COMMA_NEEDED is 1 if fields have been printed at the current recursion
14f9c5c9 1038 level, so that a comma is needed before any field printed by this
4c4b4cd2 1039 call.
14f9c5c9 1040
4c4b4cd2 1041 Returns 1 if COMMA_NEEDED or any fields were printed. */
14f9c5c9
AS
1042
1043static int
fc1a4b47 1044print_field_values (struct type *type, const gdb_byte *valaddr,
490f124f 1045 int offset, struct ui_file *stream, int recurse,
0e03807e 1046 const struct value *val,
79a45b7d
TT
1047 const struct value_print_options *options,
1048 int comma_needed,
490f124f 1049 struct type *outer_type, int outer_offset)
14f9c5c9
AS
1050{
1051 int i, len;
1052
1053 len = TYPE_NFIELDS (type);
1054
1055 for (i = 0; i < len; i += 1)
1056 {
1057 if (ada_is_ignored_field (type, i))
d2e4a39e 1058 continue;
14f9c5c9
AS
1059
1060 if (ada_is_wrapper_field (type, i))
1061 {
d2e4a39e 1062 comma_needed =
14f9c5c9 1063 print_field_values (TYPE_FIELD_TYPE (type, i),
490f124f
PA
1064 valaddr,
1065 (offset
1066 + TYPE_FIELD_BITPOS (type, i) / HOST_CHAR_BIT),
0e03807e 1067 stream, recurse, val, options,
490f124f 1068 comma_needed, type, offset);
14f9c5c9
AS
1069 continue;
1070 }
1071 else if (ada_is_variant_part (type, i))
1072 {
1073 comma_needed =
1074 print_variant_part (type, i, valaddr,
490f124f
PA
1075 offset, stream, recurse, val,
1076 options, comma_needed,
1077 outer_type, outer_offset);
14f9c5c9
AS
1078 continue;
1079 }
1080
1081 if (comma_needed)
1082 fprintf_filtered (stream, ", ");
1083 comma_needed = 1;
1084
79a45b7d 1085 if (options->pretty)
14f9c5c9
AS
1086 {
1087 fprintf_filtered (stream, "\n");
1088 print_spaces_filtered (2 + 2 * recurse, stream);
1089 }
d2e4a39e 1090 else
14f9c5c9
AS
1091 {
1092 wrap_here (n_spaces (2 + 2 * recurse));
1093 }
79a45b7d 1094 if (options->inspect_it)
14f9c5c9
AS
1095 {
1096 if (TYPE_CODE (TYPE_FIELD_TYPE (type, i)) == TYPE_CODE_PTR)
1097 fputs_filtered ("\"( ptr \"", stream);
1098 else
1099 fputs_filtered ("\"( nodef \"", stream);
1100 fprintf_symbol_filtered (stream, TYPE_FIELD_NAME (type, i),
1101 language_cplus, DMGL_NO_OPTS);
1102 fputs_filtered ("\" \"", stream);
1103 fprintf_symbol_filtered (stream, TYPE_FIELD_NAME (type, i),
1104 language_cplus, DMGL_NO_OPTS);
1105 fputs_filtered ("\") \"", stream);
1106 }
1107 else
1108 {
1109 annotate_field_begin (TYPE_FIELD_TYPE (type, i));
d2e4a39e 1110 fprintf_filtered (stream, "%.*s",
14f9c5c9
AS
1111 ada_name_prefix_len (TYPE_FIELD_NAME (type, i)),
1112 TYPE_FIELD_NAME (type, i));
1113 annotate_field_name_end ();
1114 fputs_filtered (" => ", stream);
1115 annotate_field_value ();
1116 }
1117
1118 if (TYPE_FIELD_PACKED (type, i))
1119 {
d2e4a39e 1120 struct value *v;
14f9c5c9
AS
1121
1122 /* Bitfields require special handling, especially due to byte
1123 order problems. */
b4ba55a1 1124 if (HAVE_CPLUS_STRUCT (type) && TYPE_FIELD_IGNORE (type, i))
14f9c5c9 1125 {
edefbb7c 1126 fputs_filtered (_("<optimized out or zero length>"), stream);
14f9c5c9
AS
1127 }
1128 else
1129 {
1130 int bit_pos = TYPE_FIELD_BITPOS (type, i);
1131 int bit_size = TYPE_FIELD_BITSIZE (type, i);
79a45b7d 1132 struct value_print_options opts;
d2e4a39e 1133
14f9c5c9 1134 adjust_type_signedness (TYPE_FIELD_TYPE (type, i));
490f124f
PA
1135 v = ada_value_primitive_packed_val
1136 (NULL, valaddr,
1137 offset + bit_pos / HOST_CHAR_BIT,
1138 bit_pos % HOST_CHAR_BIT,
1139 bit_size, TYPE_FIELD_TYPE (type, i));
79a45b7d
TT
1140 opts = *options;
1141 opts.deref_ref = 0;
490f124f
PA
1142 val_print (TYPE_FIELD_TYPE (type, i),
1143 value_contents_for_printing (v),
1144 value_embedded_offset (v), 0,
0e03807e
TT
1145 stream, recurse + 1, v,
1146 &opts, current_language);
14f9c5c9
AS
1147 }
1148 }
1149 else
79a45b7d
TT
1150 {
1151 struct value_print_options opts = *options;
5b4ee69b 1152
79a45b7d
TT
1153 opts.deref_ref = 0;
1154 ada_val_print (TYPE_FIELD_TYPE (type, i),
490f124f
PA
1155 valaddr,
1156 (offset
1157 + TYPE_FIELD_BITPOS (type, i) / HOST_CHAR_BIT),
1158 0, stream, recurse + 1, val, &opts);
79a45b7d 1159 }
14f9c5c9
AS
1160 annotate_field_end ();
1161 }
1162
1163 return comma_needed;
1164}
This page took 0.55991 seconds and 4 git commands to generate.