oops - typo correction.
[deliverable/binutils-gdb.git] / gdb / ada-valprint.c
CommitLineData
4c4b4cd2 1/* Support for printing Ada values for GDB, the GNU debugger.
d56612af 2
28e7fd62 3 Copyright (C) 1986-2013 Free Software Foundation, Inc.
14f9c5c9 4
a9762ec7 5 This file is part of GDB.
14f9c5c9 6
a9762ec7
JB
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
14f9c5c9 11
a9762ec7
JB
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
14f9c5c9 16
a9762ec7
JB
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
14f9c5c9 19
14f9c5c9 20#include "defs.h"
12c89474 21#include <ctype.h>
4c4b4cd2 22#include "gdb_string.h"
14f9c5c9
AS
23#include "symtab.h"
24#include "gdbtypes.h"
25#include "expression.h"
26#include "value.h"
27#include "demangle.h"
28#include "valprint.h"
29#include "language.h"
30#include "annotate.h"
31#include "ada-lang.h"
32#include "c-lang.h"
04714b91 33#include "infcall.h"
60250e8b 34#include "exceptions.h"
8ca1c40e 35#include "objfiles.h"
14f9c5c9 36
490f124f
PA
37static void print_record (struct type *, const gdb_byte *, int,
38 struct ui_file *,
0e03807e
TT
39 int,
40 const struct value *,
41 const struct value_print_options *);
14f9c5c9 42
fc1a4b47 43static int print_field_values (struct type *, const gdb_byte *,
490f124f 44 int,
79a45b7d 45 struct ui_file *, int,
0e03807e 46 const struct value *,
79a45b7d 47 const struct value_print_options *,
490f124f 48 int, struct type *, int);
14f9c5c9 49
d2e4a39e 50static void adjust_type_signedness (struct type *);
14f9c5c9 51
d3eab38a
TT
52static void ada_val_print_1 (struct type *, const gdb_byte *, int, CORE_ADDR,
53 struct ui_file *, int,
54 const struct value *,
55 const struct value_print_options *);
14f9c5c9
AS
56\f
57
4c4b4cd2 58/* Make TYPE unsigned if its range of values includes no negatives. */
d2e4a39e 59static void
4dc81987 60adjust_type_signedness (struct type *type)
14f9c5c9 61{
d2e4a39e 62 if (type != NULL && TYPE_CODE (type) == TYPE_CODE_RANGE
14f9c5c9 63 && TYPE_LOW_BOUND (type) >= 0)
876cecd0 64 TYPE_UNSIGNED (type) = 1;
d2e4a39e 65}
14f9c5c9 66
e936309c
JB
67/* Assuming TYPE is a simple array type, prints its lower bound on STREAM,
68 if non-standard (i.e., other than 1 for numbers, other than lower bound
69 of index type for enumerated type). Returns 1 if something printed,
70 otherwise 0. */
14f9c5c9 71
d2e4a39e 72static int
79a45b7d
TT
73print_optional_low_bound (struct ui_file *stream, struct type *type,
74 const struct value_print_options *options)
14f9c5c9
AS
75{
76 struct type *index_type;
df178451
PM
77 LONGEST low_bound;
78 LONGEST high_bound;
14f9c5c9 79
79a45b7d 80 if (options->print_array_indexes)
14f9c5c9 81 return 0;
e79af960 82
e936309c
JB
83 if (!get_array_bounds (type, &low_bound, &high_bound))
84 return 0;
85
86 /* If this is an empty array, then don't print the lower bound.
87 That would be confusing, because we would print the lower bound,
88 followed by... nothing! */
89 if (low_bound > high_bound)
14f9c5c9 90 return 0;
d2e4a39e 91
e79af960
JB
92 index_type = TYPE_INDEX_TYPE (type);
93
fd1b946e
JB
94 if (TYPE_CODE (index_type) == TYPE_CODE_RANGE)
95 {
96 /* We need to know what the base type is, in order to do the
97 appropriate check below. Otherwise, if this is a subrange
98 of an enumerated type, where the underlying value of the
99 first element is typically 0, we might test the low bound
100 against the wrong value. */
101 index_type = TYPE_TARGET_TYPE (index_type);
102 }
103
d2e4a39e
AS
104 switch (TYPE_CODE (index_type))
105 {
690cc4eb
PH
106 case TYPE_CODE_BOOL:
107 if (low_bound == 0)
108 return 0;
109 break;
d2e4a39e 110 case TYPE_CODE_ENUM:
14e75d8e 111 if (low_bound == TYPE_FIELD_ENUMVAL (index_type, 0))
d2e4a39e
AS
112 return 0;
113 break;
114 case TYPE_CODE_UNDEF:
7c964f07 115 index_type = NULL;
d2e4a39e
AS
116 /* FALL THROUGH */
117 default:
118 if (low_bound == 1)
119 return 0;
120 break;
121 }
14f9c5c9 122
df178451 123 ada_print_scalar (index_type, low_bound, stream);
14f9c5c9
AS
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
79a45b7d 130 BITOFFSET from VALADDR on STREAM. Formats according to OPTIONS and
4c4b4cd2 131 separates with commas. RECURSE is the recursion (nesting) level.
79a45b7d 132 TYPE must have been decoded (as by ada_coerce_to_simple_array). */
14f9c5c9
AS
133
134static void
fc1a4b47 135val_print_packed_array_elements (struct type *type, const gdb_byte *valaddr,
490f124f 136 int offset,
ebf56fd3 137 int bitoffset, struct ui_file *stream,
79a45b7d 138 int recurse,
0e03807e 139 const struct value *val,
79a45b7d 140 const struct value_print_options *options)
14f9c5c9
AS
141{
142 unsigned int i;
143 unsigned int things_printed = 0;
144 unsigned len;
e79af960 145 struct type *elttype, *index_type;
14f9c5c9 146 unsigned eltlen;
14f9c5c9 147 unsigned long bitsize = TYPE_FIELD_BITSIZE (type, 0);
d2e4a39e 148 struct value *mark = value_mark ();
e79af960 149 LONGEST low = 0;
d2e4a39e 150
14f9c5c9
AS
151 elttype = TYPE_TARGET_TYPE (type);
152 eltlen = TYPE_LENGTH (check_typedef (elttype));
e79af960 153 index_type = TYPE_INDEX_TYPE (type);
14f9c5c9
AS
154
155 {
e79af960 156 LONGEST high;
5b4ee69b 157
262452ec 158 if (get_discrete_bounds (index_type, &low, &high) < 0)
14f9c5c9
AS
159 len = 1;
160 else
161 len = high - low + 1;
162 }
163
164 i = 0;
165 annotate_array_section_begin (i, elttype);
166
79a45b7d 167 while (i < len && things_printed < options->print_max)
14f9c5c9
AS
168 {
169 struct value *v0, *v1;
170 int i0;
171
172 if (i != 0)
173 {
79a45b7d 174 if (options->prettyprint_arrays)
14f9c5c9
AS
175 {
176 fprintf_filtered (stream, ",\n");
177 print_spaces_filtered (2 + 2 * recurse, stream);
178 }
179 else
180 {
181 fprintf_filtered (stream, ", ");
182 }
183 }
184 wrap_here (n_spaces (2 + 2 * recurse));
79a45b7d 185 maybe_print_array_index (index_type, i + low, stream, options);
14f9c5c9
AS
186
187 i0 = i;
490f124f 188 v0 = ada_value_primitive_packed_val (NULL, valaddr + offset,
14f9c5c9
AS
189 (i0 * bitsize) / HOST_CHAR_BIT,
190 (i0 * bitsize) % HOST_CHAR_BIT,
191 bitsize, elttype);
192 while (1)
193 {
194 i += 1;
195 if (i >= len)
196 break;
490f124f 197 v1 = ada_value_primitive_packed_val (NULL, valaddr + offset,
14f9c5c9
AS
198 (i * bitsize) / HOST_CHAR_BIT,
199 (i * bitsize) % HOST_CHAR_BIT,
200 bitsize, elttype);
c8c1c22f
PA
201 if (!value_available_contents_eq (v0, value_embedded_offset (v0),
202 v1, value_embedded_offset (v1),
203 eltlen))
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 {
14e75d8e 404 if (TYPE_FIELD_ENUMVAL (type, i) == val)
14f9c5c9
AS
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
32b72a42 567/* See val_print for a description of the various parameters of this
d3eab38a 568 function; they are identical. */
14f9c5c9 569
d3eab38a 570void
490f124f 571ada_val_print (struct type *type, const gdb_byte *valaddr,
a2bd3dcd 572 int embedded_offset, CORE_ADDR address,
79a45b7d 573 struct ui_file *stream, int recurse,
0e03807e 574 const struct value *val,
79a45b7d 575 const struct value_print_options *options)
14f9c5c9 576{
0e03807e 577 volatile struct gdb_exception except;
14f9c5c9 578
490f124f 579 /* XXX: this catches QUIT/ctrl-c as well. Isn't that busted? */
0e03807e
TT
580 TRY_CATCH (except, RETURN_MASK_ALL)
581 {
d3eab38a
TT
582 ada_val_print_1 (type, valaddr, embedded_offset, address,
583 stream, recurse, val, options);
0e03807e 584 }
14f9c5c9
AS
585}
586
e936309c 587/* Assuming TYPE is a simple array, print the value of this array located
32b72a42 588 at VALADDR + OFFSET. See ada_val_print for a description of the various
d3eab38a 589 parameters of this function; they are identical. */
e936309c 590
d3eab38a 591static void
e936309c 592ada_val_print_array (struct type *type, const gdb_byte *valaddr,
490f124f
PA
593 int offset, CORE_ADDR address,
594 struct ui_file *stream, int recurse,
0e03807e 595 const struct value *val,
79a45b7d 596 const struct value_print_options *options)
e936309c 597{
e936309c 598 /* For an array of chars, print with string syntax. */
79a45b7d
TT
599 if (ada_is_string_type (type)
600 && (options->format == 0 || options->format == 's'))
e936309c 601 {
dd70cc93
JB
602 enum bfd_endian byte_order = gdbarch_byte_order (get_type_arch (type));
603 struct type *elttype = TYPE_TARGET_TYPE (type);
9addb3b9
JB
604 unsigned int eltlen;
605 unsigned int len;
606
dd70cc93
JB
607 /* We know that ELTTYPE cannot possibly be null, because we found
608 that TYPE is a string-like type. Similarly, the size of ELTTYPE
609 should also be non-null, since it's a character-like type. */
610 gdb_assert (elttype != NULL);
611 gdb_assert (TYPE_LENGTH (elttype) != 0);
612
613 eltlen = TYPE_LENGTH (elttype);
614 len = TYPE_LENGTH (type) / eltlen;
9addb3b9 615
79a45b7d 616 if (options->prettyprint_arrays)
e936309c
JB
617 print_spaces_filtered (2 + 2 * recurse, stream);
618
619 /* If requested, look for the first null char and only print
620 elements up to it. */
79a45b7d 621 if (options->stop_print_at_null)
e936309c
JB
622 {
623 int temp_len;
624
625 /* Look for a NULL char. */
626 for (temp_len = 0;
627 (temp_len < len
79a45b7d 628 && temp_len < options->print_max
490f124f
PA
629 && char_at (valaddr + offset,
630 temp_len, eltlen, byte_order) != 0);
e936309c
JB
631 temp_len += 1);
632 len = temp_len;
633 }
634
490f124f 635 printstr (stream, elttype, valaddr + offset, len, 0, eltlen, options);
e936309c
JB
636 }
637 else
638 {
639 fprintf_filtered (stream, "(");
79a45b7d 640 print_optional_low_bound (stream, type, options);
e936309c 641 if (TYPE_FIELD_BITSIZE (type, 0) > 0)
490f124f
PA
642 val_print_packed_array_elements (type, valaddr, offset,
643 0, stream, recurse, val, options);
e936309c 644 else
490f124f
PA
645 val_print_array_elements (type, valaddr, offset, address,
646 stream, recurse, val, options, 0);
e936309c
JB
647 fprintf_filtered (stream, ")");
648 }
e936309c
JB
649}
650
14f9c5c9 651/* See the comment on ada_val_print. This function differs in that it
e936309c 652 does not catch evaluation errors (leaving that to ada_val_print). */
14f9c5c9 653
d3eab38a 654static void
490f124f
PA
655ada_val_print_1 (struct type *type, const gdb_byte *valaddr,
656 int offset, CORE_ADDR address,
79a45b7d 657 struct ui_file *stream, int recurse,
0e03807e 658 const struct value *original_value,
79a45b7d 659 const struct value_print_options *options)
14f9c5c9 660{
14f9c5c9
AS
661 int i;
662 struct type *elttype;
490f124f 663 int offset_aligned;
14f9c5c9 664
61ee279c 665 type = ada_check_typedef (type);
14f9c5c9 666
ad82864c 667 if (ada_is_array_descriptor_type (type)
d2d43431
JB
668 || (ada_is_constrained_packed_array_type (type)
669 && TYPE_CODE (type) != TYPE_CODE_PTR))
14f9c5c9 670 {
d2e4a39e
AS
671 struct value *mark = value_mark ();
672 struct value *val;
5b4ee69b 673
490f124f 674 val = value_from_contents_and_address (type, valaddr + offset, address);
2d4a02ee
JB
675 /* If this is a reference, coerce it now. This helps taking care
676 of the case where ADDRESS is meaningless because original_value
677 was not an lval. */
678 val = coerce_ref (val);
720d1a40
JB
679 if (TYPE_CODE (type) == TYPE_CODE_TYPEDEF) /* array access type. */
680 val = ada_coerce_to_simple_array_ptr (val);
681 else
682 val = ada_coerce_to_simple_array (val);
14f9c5c9
AS
683 if (val == NULL)
684 {
9e87a409
JB
685 gdb_assert (TYPE_CODE (type) == TYPE_CODE_TYPEDEF);
686 fprintf_filtered (stream, "0x0");
14f9c5c9
AS
687 }
688 else
d3eab38a
TT
689 ada_val_print_1 (value_type (val),
690 value_contents_for_printing (val),
691 value_embedded_offset (val),
692 value_address (val), stream, recurse,
693 val, options);
14f9c5c9 694 value_free_to_mark (mark);
d3eab38a 695 return;
14f9c5c9
AS
696 }
697
490f124f
PA
698 offset_aligned = offset + ada_aligned_value_addr (type, valaddr) - valaddr;
699 type = printable_val_type (type, valaddr + offset_aligned);
14f9c5c9
AS
700
701 switch (TYPE_CODE (type))
702 {
703 default:
d3eab38a
TT
704 c_val_print (type, valaddr, offset, address, stream,
705 recurse, original_value, options);
706 break;
14f9c5c9 707
4c4b4cd2
PH
708 case TYPE_CODE_PTR:
709 {
d3eab38a
TT
710 c_val_print (type, valaddr, offset, address,
711 stream, recurse, original_value, options);
5b4ee69b 712
4c4b4cd2
PH
713 if (ada_is_tag_type (type))
714 {
490f124f
PA
715 struct value *val =
716 value_from_contents_and_address (type,
717 valaddr + offset_aligned,
718 address + offset_aligned);
4c4b4cd2 719 const char *name = ada_tag_name (val);
5b4ee69b 720
4c4b4cd2
PH
721 if (name != NULL)
722 fprintf_filtered (stream, " (%s)", name);
d3eab38a
TT
723 }
724 return;
4c4b4cd2
PH
725 }
726
14f9c5c9
AS
727 case TYPE_CODE_INT:
728 case TYPE_CODE_RANGE:
729 if (ada_is_fixed_point_type (type))
730 {
490f124f 731 LONGEST v = unpack_long (type, valaddr + offset_aligned);
14f9c5c9 732
744a8059 733 fprintf_filtered (stream, TYPE_LENGTH (type) < 4 ? "%.11g" : "%.17g",
14f9c5c9 734 (double) ada_fixed_to_float (type, v));
d3eab38a 735 return;
14f9c5c9 736 }
14f9c5c9
AS
737 else if (TYPE_CODE (type) == TYPE_CODE_RANGE)
738 {
d2e4a39e 739 struct type *target_type = TYPE_TARGET_TYPE (type);
5b4ee69b 740
14f9c5c9
AS
741 if (TYPE_LENGTH (type) != TYPE_LENGTH (target_type))
742 {
743 /* Obscure case of range type that has different length from
d2e4a39e
AS
744 its base type. Perform a conversion, or we will get a
745 nonsense value. Actually, we could use the same
4c4b4cd2 746 code regardless of lengths; I'm just avoiding a cast. */
490f124f
PA
747 struct value *v1
748 = value_from_contents_and_address (type, valaddr + offset, 0);
749 struct value *v = value_cast (target_type, v1);
750
d3eab38a
TT
751 ada_val_print_1 (target_type,
752 value_contents_for_printing (v),
753 value_embedded_offset (v), 0,
754 stream, recurse + 1, v, options);
14f9c5c9
AS
755 }
756 else
d3eab38a
TT
757 ada_val_print_1 (TYPE_TARGET_TYPE (type),
758 valaddr, offset,
759 address, stream, recurse,
760 original_value, options);
761 return;
14f9c5c9 762 }
d2e4a39e 763 else
14f9c5c9 764 {
79a45b7d
TT
765 int format = (options->format ? options->format
766 : options->output_format);
5b4ee69b 767
14f9c5c9
AS
768 if (format)
769 {
79a45b7d 770 struct value_print_options opts = *options;
5b4ee69b 771
79a45b7d 772 opts.format = format;
ab2188aa
PA
773 val_print_scalar_formatted (type, valaddr, offset_aligned,
774 original_value, &opts, 0, stream);
14f9c5c9 775 }
50810684 776 else if (ada_is_system_address_type (type))
4c4b4cd2
PH
777 {
778 /* FIXME: We want to print System.Address variables using
779 the same format as for any access type. But for some
780 reason GNAT encodes the System.Address type as an int,
781 so we have to work-around this deficiency by handling
50810684 782 System.Address values as a special case. */
8ca1c40e 783
50810684 784 struct gdbarch *gdbarch = get_type_arch (type);
8ca1c40e 785 struct type *ptr_type = builtin_type (gdbarch)->builtin_data_ptr;
490f124f
PA
786 CORE_ADDR addr = extract_typed_address (valaddr + offset_aligned,
787 ptr_type);
8ca1c40e 788
4c4b4cd2
PH
789 fprintf_filtered (stream, "(");
790 type_print (type, "", stream, -1);
791 fprintf_filtered (stream, ") ");
5af949e3 792 fputs_filtered (paddress (gdbarch, addr), stream);
4c4b4cd2 793 }
14f9c5c9
AS
794 else
795 {
490f124f 796 val_print_type_code_int (type, valaddr + offset_aligned, stream);
14f9c5c9
AS
797 if (ada_is_character_type (type))
798 {
490f124f
PA
799 LONGEST c;
800
14f9c5c9 801 fputs_filtered (" ", stream);
490f124f
PA
802 c = unpack_long (type, valaddr + offset_aligned);
803 ada_printchar (c, type, stream);
14f9c5c9
AS
804 }
805 }
d3eab38a 806 return;
14f9c5c9
AS
807 }
808
809 case TYPE_CODE_ENUM:
6c88661c
JB
810 {
811 unsigned int len;
812 LONGEST val;
5b4ee69b 813
6c88661c
JB
814 if (options->format)
815 {
816 val_print_scalar_formatted (type, valaddr, offset_aligned,
817 original_value, options, 0, stream);
818 break;
819 }
820 len = TYPE_NFIELDS (type);
821 val = unpack_long (type, valaddr + offset_aligned);
822 for (i = 0; i < len; i++)
823 {
824 QUIT;
14e75d8e 825 if (val == TYPE_FIELD_ENUMVAL (type, i))
6c88661c
JB
826 {
827 break;
828 }
829 }
830 if (i < len)
831 {
832 const char *name = ada_enum_name (TYPE_FIELD_NAME (type, i));
833
834 if (name[0] == '\'')
835 fprintf_filtered (stream, "%ld %s", (long) val, name);
836 else
837 fputs_filtered (name, stream);
838 }
839 else
840 {
841 print_longest (stream, 'd', 0, val);
842 }
843 break;
844 }
d2e4a39e 845
4c4b4cd2 846 case TYPE_CODE_FLT:
79a45b7d 847 if (options->format)
d3eab38a
TT
848 {
849 c_val_print (type, valaddr, offset, address, stream,
850 recurse, original_value, options);
851 return;
852 }
4c4b4cd2 853 else
490f124f 854 ada_print_floating (valaddr + offset, type, stream);
4c4b4cd2
PH
855 break;
856
14f9c5c9
AS
857 case TYPE_CODE_UNION:
858 case TYPE_CODE_STRUCT:
859 if (ada_is_bogus_array_descriptor (type))
860 {
861 fprintf_filtered (stream, "(...?)");
d3eab38a 862 return;
d2e4a39e 863 }
14f9c5c9
AS
864 else
865 {
490f124f
PA
866 print_record (type, valaddr, offset_aligned,
867 stream, recurse, original_value, options);
d3eab38a 868 return;
14f9c5c9
AS
869 }
870
871 case TYPE_CODE_ARRAY:
d3eab38a
TT
872 ada_val_print_array (type, valaddr, offset_aligned,
873 address, stream, recurse, original_value,
874 options);
875 return;
14f9c5c9
AS
876
877 case TYPE_CODE_REF:
969a1360
JB
878 /* For references, the debugger is expected to print the value as
879 an address if DEREF_REF is null. But printing an address in place
880 of the object value would be confusing to an Ada programmer.
881 So, for Ada values, we print the actual dereferenced value
882 regardless. */
14f9c5c9 883 elttype = check_typedef (TYPE_TARGET_TYPE (type));
969a1360
JB
884
885 if (TYPE_CODE (elttype) != TYPE_CODE_UNDEF)
886 {
a471c594
JK
887 CORE_ADDR deref_val_int;
888 struct value *deref_val;
5b4ee69b 889
a471c594
JK
890 deref_val = coerce_ref_if_computed (original_value);
891 if (deref_val)
892 {
b50d69b5
JG
893 if (ada_is_tagged_type (value_type (deref_val), 1))
894 deref_val = ada_tag_value_at_base_address (deref_val);
895
a471c594
JK
896 common_val_print (deref_val, stream, recurse + 1, options,
897 current_language);
898 break;
899 }
900
901 deref_val_int = unpack_pointer (type, valaddr + offset_aligned);
969a1360
JB
902 if (deref_val_int != 0)
903 {
6c88661c 904 deref_val =
490f124f 905 ada_value_ind (value_from_pointer
969a1360
JB
906 (lookup_pointer_type (elttype),
907 deref_val_int));
5b4ee69b 908
b50d69b5
JG
909 if (ada_is_tagged_type (value_type (deref_val), 1))
910 deref_val = ada_tag_value_at_base_address (deref_val);
911
969a1360 912 val_print (value_type (deref_val),
490f124f
PA
913 value_contents_for_printing (deref_val),
914 value_embedded_offset (deref_val),
42ae5230 915 value_address (deref_val), stream, recurse + 1,
a491d753 916 deref_val, options, current_language);
969a1360
JB
917 }
918 else
919 fputs_filtered ("(null)", stream);
920 }
921 else
922 fputs_filtered ("???", stream);
923
14f9c5c9
AS
924 break;
925 }
4c4b4cd2 926 gdb_flush (stream);
14f9c5c9
AS
927}
928
929static int
490f124f
PA
930print_variant_part (struct type *type, int field_num,
931 const gdb_byte *valaddr, int offset,
79a45b7d 932 struct ui_file *stream, int recurse,
0e03807e 933 const struct value *val,
0963b4bd
MS
934 const struct value_print_options *options,
935 int comma_needed,
490f124f 936 struct type *outer_type, int outer_offset)
14f9c5c9
AS
937{
938 struct type *var_type = TYPE_FIELD_TYPE (type, field_num);
490f124f
PA
939 int which = ada_which_variant_applies (var_type, outer_type,
940 valaddr + outer_offset);
14f9c5c9
AS
941
942 if (which < 0)
943 return 0;
944 else
d2e4a39e 945 return print_field_values
14f9c5c9 946 (TYPE_FIELD_TYPE (var_type, which),
490f124f
PA
947 valaddr,
948 offset + TYPE_FIELD_BITPOS (type, field_num) / HOST_CHAR_BIT
14f9c5c9 949 + TYPE_FIELD_BITPOS (var_type, which) / HOST_CHAR_BIT,
0e03807e 950 stream, recurse, val, options,
490f124f 951 comma_needed, outer_type, outer_offset);
14f9c5c9
AS
952}
953
8e069a98 954void
79a45b7d
TT
955ada_value_print (struct value *val0, struct ui_file *stream,
956 const struct value_print_options *options)
14f9c5c9 957{
0c3acc09
JB
958 struct value *val = ada_to_fixed_value (val0);
959 CORE_ADDR address = value_address (val);
f5aa6869 960 struct type *type = ada_check_typedef (value_type (val));
79a45b7d 961 struct value_print_options opts;
14f9c5c9 962
4c4b4cd2
PH
963 /* If it is a pointer, indicate what it points to. */
964 if (TYPE_CODE (type) == TYPE_CODE_PTR)
14f9c5c9 965 {
4c4b4cd2
PH
966 /* Hack: don't print (char *) for char strings. Their
967 type is indicated by the quoted string anyway. */
968 if (TYPE_LENGTH (TYPE_TARGET_TYPE (type)) != sizeof (char)
969 || TYPE_CODE (TYPE_TARGET_TYPE (type)) != TYPE_CODE_INT
970 || TYPE_UNSIGNED (TYPE_TARGET_TYPE (type)))
14f9c5c9
AS
971 {
972 fprintf_filtered (stream, "(");
973 type_print (type, "", stream, -1);
974 fprintf_filtered (stream, ") ");
975 }
976 }
4c4b4cd2 977 else if (ada_is_array_descriptor_type (type))
14f9c5c9 978 {
720d1a40
JB
979 /* We do not print the type description unless TYPE is an array
980 access type (this is encoded by the compiler as a typedef to
981 a fat pointer - hence the check against TYPE_CODE_TYPEDEF). */
982 if (TYPE_CODE (type) == TYPE_CODE_TYPEDEF)
983 {
984 fprintf_filtered (stream, "(");
985 type_print (type, "", stream, -1);
986 fprintf_filtered (stream, ") ");
987 }
14f9c5c9
AS
988 }
989 else if (ada_is_bogus_array_descriptor (type))
990 {
991 fprintf_filtered (stream, "(");
992 type_print (type, "", stream, -1);
993 fprintf_filtered (stream, ") (...?)");
8e069a98 994 return;
14f9c5c9 995 }
4c4b4cd2 996
79a45b7d
TT
997 opts = *options;
998 opts.deref_ref = 1;
8e069a98
TT
999 val_print (type, value_contents_for_printing (val),
1000 value_embedded_offset (val), address,
1001 stream, 0, val, &opts, current_language);
14f9c5c9 1002}
d2e4a39e 1003
14f9c5c9 1004static void
fc1a4b47 1005print_record (struct type *type, const gdb_byte *valaddr,
490f124f 1006 int offset,
79a45b7d 1007 struct ui_file *stream, int recurse,
0e03807e 1008 const struct value *val,
79a45b7d 1009 const struct value_print_options *options)
14f9c5c9 1010{
61ee279c 1011 type = ada_check_typedef (type);
14f9c5c9
AS
1012
1013 fprintf_filtered (stream, "(");
1014
490f124f
PA
1015 if (print_field_values (type, valaddr, offset,
1016 stream, recurse, val, options,
1017 0, type, offset) != 0 && options->pretty)
14f9c5c9
AS
1018 {
1019 fprintf_filtered (stream, "\n");
1020 print_spaces_filtered (2 * recurse, stream);
1021 }
1022
1023 fprintf_filtered (stream, ")");
1024}
1025
32b72a42 1026/* Print out fields of value at VALADDR + OFFSET having structure type TYPE.
4c4b4cd2 1027
32b72a42
PA
1028 TYPE, VALADDR, OFFSET, STREAM, RECURSE, and OPTIONS have the same
1029 meanings as in ada_print_value and ada_val_print.
14f9c5c9 1030
32b72a42
PA
1031 OUTER_TYPE and OUTER_OFFSET give type and address of enclosing
1032 record (used to get discriminant values when printing variant
1033 parts).
14f9c5c9 1034
4c4b4cd2 1035 COMMA_NEEDED is 1 if fields have been printed at the current recursion
14f9c5c9 1036 level, so that a comma is needed before any field printed by this
4c4b4cd2 1037 call.
14f9c5c9 1038
4c4b4cd2 1039 Returns 1 if COMMA_NEEDED or any fields were printed. */
14f9c5c9
AS
1040
1041static int
fc1a4b47 1042print_field_values (struct type *type, const gdb_byte *valaddr,
490f124f 1043 int offset, struct ui_file *stream, int recurse,
0e03807e 1044 const struct value *val,
79a45b7d
TT
1045 const struct value_print_options *options,
1046 int comma_needed,
490f124f 1047 struct type *outer_type, int outer_offset)
14f9c5c9
AS
1048{
1049 int i, len;
1050
1051 len = TYPE_NFIELDS (type);
1052
1053 for (i = 0; i < len; i += 1)
1054 {
1055 if (ada_is_ignored_field (type, i))
d2e4a39e 1056 continue;
14f9c5c9
AS
1057
1058 if (ada_is_wrapper_field (type, i))
1059 {
d2e4a39e 1060 comma_needed =
14f9c5c9 1061 print_field_values (TYPE_FIELD_TYPE (type, i),
490f124f
PA
1062 valaddr,
1063 (offset
1064 + TYPE_FIELD_BITPOS (type, i) / HOST_CHAR_BIT),
0e03807e 1065 stream, recurse, val, options,
490f124f 1066 comma_needed, type, offset);
14f9c5c9
AS
1067 continue;
1068 }
1069 else if (ada_is_variant_part (type, i))
1070 {
1071 comma_needed =
1072 print_variant_part (type, i, valaddr,
490f124f
PA
1073 offset, stream, recurse, val,
1074 options, comma_needed,
1075 outer_type, outer_offset);
14f9c5c9
AS
1076 continue;
1077 }
1078
1079 if (comma_needed)
1080 fprintf_filtered (stream, ", ");
1081 comma_needed = 1;
1082
79a45b7d 1083 if (options->pretty)
14f9c5c9
AS
1084 {
1085 fprintf_filtered (stream, "\n");
1086 print_spaces_filtered (2 + 2 * recurse, stream);
1087 }
d2e4a39e 1088 else
14f9c5c9
AS
1089 {
1090 wrap_here (n_spaces (2 + 2 * recurse));
1091 }
79a45b7d 1092 if (options->inspect_it)
14f9c5c9
AS
1093 {
1094 if (TYPE_CODE (TYPE_FIELD_TYPE (type, i)) == TYPE_CODE_PTR)
1095 fputs_filtered ("\"( ptr \"", stream);
1096 else
1097 fputs_filtered ("\"( nodef \"", stream);
1098 fprintf_symbol_filtered (stream, TYPE_FIELD_NAME (type, i),
1099 language_cplus, DMGL_NO_OPTS);
1100 fputs_filtered ("\" \"", stream);
1101 fprintf_symbol_filtered (stream, TYPE_FIELD_NAME (type, i),
1102 language_cplus, DMGL_NO_OPTS);
1103 fputs_filtered ("\") \"", stream);
1104 }
1105 else
1106 {
1107 annotate_field_begin (TYPE_FIELD_TYPE (type, i));
d2e4a39e 1108 fprintf_filtered (stream, "%.*s",
14f9c5c9
AS
1109 ada_name_prefix_len (TYPE_FIELD_NAME (type, i)),
1110 TYPE_FIELD_NAME (type, i));
1111 annotate_field_name_end ();
1112 fputs_filtered (" => ", stream);
1113 annotate_field_value ();
1114 }
1115
1116 if (TYPE_FIELD_PACKED (type, i))
1117 {
d2e4a39e 1118 struct value *v;
14f9c5c9
AS
1119
1120 /* Bitfields require special handling, especially due to byte
1121 order problems. */
b4ba55a1 1122 if (HAVE_CPLUS_STRUCT (type) && TYPE_FIELD_IGNORE (type, i))
14f9c5c9 1123 {
edefbb7c 1124 fputs_filtered (_("<optimized out or zero length>"), stream);
14f9c5c9
AS
1125 }
1126 else
1127 {
1128 int bit_pos = TYPE_FIELD_BITPOS (type, i);
1129 int bit_size = TYPE_FIELD_BITSIZE (type, i);
79a45b7d 1130 struct value_print_options opts;
d2e4a39e 1131
14f9c5c9 1132 adjust_type_signedness (TYPE_FIELD_TYPE (type, i));
490f124f
PA
1133 v = ada_value_primitive_packed_val
1134 (NULL, valaddr,
1135 offset + bit_pos / HOST_CHAR_BIT,
1136 bit_pos % HOST_CHAR_BIT,
1137 bit_size, TYPE_FIELD_TYPE (type, i));
79a45b7d
TT
1138 opts = *options;
1139 opts.deref_ref = 0;
490f124f
PA
1140 val_print (TYPE_FIELD_TYPE (type, i),
1141 value_contents_for_printing (v),
1142 value_embedded_offset (v), 0,
0e03807e
TT
1143 stream, recurse + 1, v,
1144 &opts, current_language);
14f9c5c9
AS
1145 }
1146 }
1147 else
79a45b7d
TT
1148 {
1149 struct value_print_options opts = *options;
5b4ee69b 1150
79a45b7d
TT
1151 opts.deref_ref = 0;
1152 ada_val_print (TYPE_FIELD_TYPE (type, i),
490f124f
PA
1153 valaddr,
1154 (offset
1155 + TYPE_FIELD_BITPOS (type, i) / HOST_CHAR_BIT),
1156 0, stream, recurse + 1, val, &opts);
79a45b7d 1157 }
14f9c5c9
AS
1158 annotate_field_end ();
1159 }
1160
1161 return comma_needed;
1162}
This page took 0.835904 seconds and 4 git commands to generate.