[python] Add gdb.Type.name attribute.
[deliverable/binutils-gdb.git] / gdb / ada-valprint.c
CommitLineData
4c4b4cd2 1/* Support for printing Ada values for GDB, the GNU debugger.
d56612af 2
ecd75fc8 3 Copyright (C) 1986-2014 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>
0e9f083f 22#include <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 {
2a998fc0 174 if (options->prettyformat_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 {
2a998fc0 230 if (options->prettyformat_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;
4c4b4cd2
PH
323 struct ui_file *tmp_stream = mem_fileopen ();
324 struct cleanup *cleanups = make_cleanup_ui_file_delete (tmp_stream);
325
326 print_floating (valaddr, type, tmp_stream);
327 ui_file_put (tmp_stream, ui_memcpy, buffer);
328 do_cleanups (cleanups);
329
330 result = buffer;
4c4b4cd2
PH
331
332 /* Modify for Ada rules. */
606b8d1a 333
c3e5cd34
PH
334 s = strstr (result, "inf");
335 if (s == NULL)
336 s = strstr (result, "Inf");
337 if (s == NULL)
338 s = strstr (result, "INF");
339 if (s != NULL)
4c4b4cd2 340 strcpy (s, "Inf");
c3e5cd34
PH
341
342 if (s == NULL)
4c4b4cd2 343 {
c3e5cd34
PH
344 s = strstr (result, "nan");
345 if (s == NULL)
346 s = strstr (result, "NaN");
347 if (s == NULL)
348 s = strstr (result, "Nan");
349 if (s != NULL)
350 {
351 s[0] = s[2] = 'N';
352 if (result[0] == '-')
353 result += 1;
354 }
4c4b4cd2 355 }
c3e5cd34
PH
356
357 if (s == NULL && strchr (result, '.') == NULL)
4c4b4cd2 358 {
c3e5cd34
PH
359 s = strchr (result, 'e');
360 if (s == NULL)
4c4b4cd2
PH
361 fprintf_filtered (stream, "%s.0", result);
362 else
363 fprintf_filtered (stream, "%.*s.0%s", (int) (s-result), result, s);
364 return;
365 }
366 fprintf_filtered (stream, "%s", result);
367}
368
14f9c5c9 369void
6c7a06a3 370ada_printchar (int c, struct type *type, struct ui_file *stream)
14f9c5c9
AS
371{
372 fputs_filtered ("'", stream);
447b483c 373 ada_emit_char (c, type, stream, '\'', TYPE_LENGTH (type));
14f9c5c9
AS
374 fputs_filtered ("'", stream);
375}
376
377/* [From print_type_scalar in typeprint.c]. Print VAL on STREAM in a
7c964f07
UW
378 form appropriate for TYPE, if non-NULL. If TYPE is NULL, print VAL
379 like a default signed integer. */
14f9c5c9
AS
380
381void
ebf56fd3 382ada_print_scalar (struct type *type, LONGEST val, struct ui_file *stream)
14f9c5c9
AS
383{
384 unsigned int i;
385 unsigned len;
386
7c964f07
UW
387 if (!type)
388 {
389 print_longest (stream, 'd', 0, val);
390 return;
391 }
392
61ee279c 393 type = ada_check_typedef (type);
14f9c5c9
AS
394
395 switch (TYPE_CODE (type))
396 {
397
398 case TYPE_CODE_ENUM:
399 len = TYPE_NFIELDS (type);
400 for (i = 0; i < len; i++)
401 {
14e75d8e 402 if (TYPE_FIELD_ENUMVAL (type, i) == val)
14f9c5c9
AS
403 {
404 break;
405 }
406 }
407 if (i < len)
408 {
409 fputs_filtered (ada_enum_name (TYPE_FIELD_NAME (type, i)), stream);
410 }
411 else
412 {
413 print_longest (stream, 'd', 0, val);
414 }
415 break;
416
417 case TYPE_CODE_INT:
418 print_longest (stream, TYPE_UNSIGNED (type) ? 'u' : 'd', 0, val);
419 break;
420
421 case TYPE_CODE_CHAR:
10d44370 422 LA_PRINT_CHAR (val, type, stream);
14f9c5c9
AS
423 break;
424
425 case TYPE_CODE_BOOL:
426 fprintf_filtered (stream, val ? "true" : "false");
427 break;
428
429 case TYPE_CODE_RANGE:
430 ada_print_scalar (TYPE_TARGET_TYPE (type), val, stream);
431 return;
432
433 case TYPE_CODE_UNDEF:
434 case TYPE_CODE_PTR:
435 case TYPE_CODE_ARRAY:
436 case TYPE_CODE_STRUCT:
437 case TYPE_CODE_UNION:
438 case TYPE_CODE_FUNC:
439 case TYPE_CODE_FLT:
440 case TYPE_CODE_VOID:
441 case TYPE_CODE_SET:
442 case TYPE_CODE_STRING:
443 case TYPE_CODE_ERROR:
0d5de010
DJ
444 case TYPE_CODE_MEMBERPTR:
445 case TYPE_CODE_METHODPTR:
14f9c5c9
AS
446 case TYPE_CODE_METHOD:
447 case TYPE_CODE_REF:
edefbb7c 448 warning (_("internal error: unhandled type in ada_print_scalar"));
14f9c5c9
AS
449 break;
450
451 default:
edefbb7c 452 error (_("Invalid type code in symbol table."));
14f9c5c9
AS
453 }
454 gdb_flush (stream);
455}
456
457/* Print the character string STRING, printing at most LENGTH characters.
458 Printing stops early if the number hits print_max; repeat counts
459 are printed as appropriate. Print ellipses at the end if we
9a153e0b
JB
460 had to stop before printing LENGTH characters, or if FORCE_ELLIPSES.
461 TYPE_LEN is the length (1 or 2) of the character type. */
14f9c5c9
AS
462
463static void
6c7a06a3 464printstr (struct ui_file *stream, struct type *elttype, const gdb_byte *string,
79a45b7d
TT
465 unsigned int length, int force_ellipses, int type_len,
466 const struct value_print_options *options)
14f9c5c9 467{
e17a4113 468 enum bfd_endian byte_order = gdbarch_byte_order (get_type_arch (elttype));
14f9c5c9
AS
469 unsigned int i;
470 unsigned int things_printed = 0;
471 int in_quotes = 0;
472 int need_comma = 0;
473
474 if (length == 0)
475 {
476 fputs_filtered ("\"\"", stream);
477 return;
478 }
479
79a45b7d 480 for (i = 0; i < length && things_printed < options->print_max; i += 1)
14f9c5c9
AS
481 {
482 /* Position of the character we are examining
d2e4a39e 483 to see whether it is repeated. */
14f9c5c9
AS
484 unsigned int rep1;
485 /* Number of repetitions we have detected so far. */
486 unsigned int reps;
487
488 QUIT;
489
490 if (need_comma)
491 {
492 fputs_filtered (", ", stream);
493 need_comma = 0;
494 }
495
496 rep1 = i + 1;
497 reps = 1;
c3e5cd34 498 while (rep1 < length
e17a4113
UW
499 && char_at (string, rep1, type_len, byte_order)
500 == char_at (string, i, type_len, byte_order))
14f9c5c9
AS
501 {
502 rep1 += 1;
503 reps += 1;
504 }
505
79a45b7d 506 if (reps > options->repeat_count_threshold)
14f9c5c9
AS
507 {
508 if (in_quotes)
509 {
e93a8774 510 fputs_filtered ("\", ", stream);
14f9c5c9
AS
511 in_quotes = 0;
512 }
513 fputs_filtered ("'", stream);
e17a4113
UW
514 ada_emit_char (char_at (string, i, type_len, byte_order),
515 elttype, stream, '\'', type_len);
14f9c5c9 516 fputs_filtered ("'", stream);
edefbb7c 517 fprintf_filtered (stream, _(" <repeats %u times>"), reps);
14f9c5c9 518 i = rep1 - 1;
79a45b7d 519 things_printed += options->repeat_count_threshold;
14f9c5c9
AS
520 need_comma = 1;
521 }
522 else
523 {
524 if (!in_quotes)
525 {
e93a8774 526 fputs_filtered ("\"", stream);
14f9c5c9
AS
527 in_quotes = 1;
528 }
e17a4113
UW
529 ada_emit_char (char_at (string, i, type_len, byte_order),
530 elttype, stream, '"', type_len);
14f9c5c9
AS
531 things_printed += 1;
532 }
533 }
534
535 /* Terminate the quotes if necessary. */
536 if (in_quotes)
e93a8774 537 fputs_filtered ("\"", stream);
14f9c5c9
AS
538
539 if (force_ellipses || i < length)
540 fputs_filtered ("...", stream);
541}
542
543void
0963b4bd
MS
544ada_printstr (struct ui_file *stream, struct type *type,
545 const gdb_byte *string, unsigned int length,
546 const char *encoding, int force_ellipses,
79a45b7d 547 const struct value_print_options *options)
14f9c5c9 548{
6c7a06a3
TT
549 printstr (stream, type, string, length, force_ellipses, TYPE_LENGTH (type),
550 options);
14f9c5c9
AS
551}
552
553
32b72a42 554/* See val_print for a description of the various parameters of this
d3eab38a 555 function; they are identical. */
14f9c5c9 556
d3eab38a 557void
490f124f 558ada_val_print (struct type *type, const gdb_byte *valaddr,
a2bd3dcd 559 int embedded_offset, CORE_ADDR address,
79a45b7d 560 struct ui_file *stream, int recurse,
0e03807e 561 const struct value *val,
79a45b7d 562 const struct value_print_options *options)
14f9c5c9 563{
0e03807e 564 volatile struct gdb_exception except;
14f9c5c9 565
490f124f 566 /* XXX: this catches QUIT/ctrl-c as well. Isn't that busted? */
0e03807e
TT
567 TRY_CATCH (except, RETURN_MASK_ALL)
568 {
d3eab38a
TT
569 ada_val_print_1 (type, valaddr, embedded_offset, address,
570 stream, recurse, val, options);
0e03807e 571 }
14f9c5c9
AS
572}
573
e936309c 574/* Assuming TYPE is a simple array, print the value of this array located
32b72a42 575 at VALADDR + OFFSET. See ada_val_print for a description of the various
d3eab38a 576 parameters of this function; they are identical. */
e936309c 577
d3eab38a 578static void
e936309c 579ada_val_print_array (struct type *type, const gdb_byte *valaddr,
490f124f
PA
580 int offset, CORE_ADDR address,
581 struct ui_file *stream, int recurse,
0e03807e 582 const struct value *val,
79a45b7d 583 const struct value_print_options *options)
e936309c 584{
e936309c 585 /* For an array of chars, print with string syntax. */
79a45b7d
TT
586 if (ada_is_string_type (type)
587 && (options->format == 0 || options->format == 's'))
e936309c 588 {
dd70cc93
JB
589 enum bfd_endian byte_order = gdbarch_byte_order (get_type_arch (type));
590 struct type *elttype = TYPE_TARGET_TYPE (type);
9addb3b9
JB
591 unsigned int eltlen;
592 unsigned int len;
593
dd70cc93
JB
594 /* We know that ELTTYPE cannot possibly be null, because we found
595 that TYPE is a string-like type. Similarly, the size of ELTTYPE
596 should also be non-null, since it's a character-like type. */
597 gdb_assert (elttype != NULL);
598 gdb_assert (TYPE_LENGTH (elttype) != 0);
599
600 eltlen = TYPE_LENGTH (elttype);
601 len = TYPE_LENGTH (type) / eltlen;
9addb3b9 602
2a998fc0 603 if (options->prettyformat_arrays)
e936309c
JB
604 print_spaces_filtered (2 + 2 * recurse, stream);
605
606 /* If requested, look for the first null char and only print
607 elements up to it. */
79a45b7d 608 if (options->stop_print_at_null)
e936309c
JB
609 {
610 int temp_len;
611
612 /* Look for a NULL char. */
613 for (temp_len = 0;
614 (temp_len < len
79a45b7d 615 && temp_len < options->print_max
490f124f
PA
616 && char_at (valaddr + offset,
617 temp_len, eltlen, byte_order) != 0);
e936309c
JB
618 temp_len += 1);
619 len = temp_len;
620 }
621
490f124f 622 printstr (stream, elttype, valaddr + offset, len, 0, eltlen, options);
e936309c
JB
623 }
624 else
625 {
626 fprintf_filtered (stream, "(");
79a45b7d 627 print_optional_low_bound (stream, type, options);
e936309c 628 if (TYPE_FIELD_BITSIZE (type, 0) > 0)
490f124f
PA
629 val_print_packed_array_elements (type, valaddr, offset,
630 0, stream, recurse, val, options);
e936309c 631 else
490f124f
PA
632 val_print_array_elements (type, valaddr, offset, address,
633 stream, recurse, val, options, 0);
e936309c
JB
634 fprintf_filtered (stream, ")");
635 }
e936309c
JB
636}
637
14f9c5c9 638/* See the comment on ada_val_print. This function differs in that it
e936309c 639 does not catch evaluation errors (leaving that to ada_val_print). */
14f9c5c9 640
d3eab38a 641static void
490f124f
PA
642ada_val_print_1 (struct type *type, const gdb_byte *valaddr,
643 int offset, CORE_ADDR address,
79a45b7d 644 struct ui_file *stream, int recurse,
0e03807e 645 const struct value *original_value,
79a45b7d 646 const struct value_print_options *options)
14f9c5c9 647{
14f9c5c9
AS
648 int i;
649 struct type *elttype;
490f124f 650 int offset_aligned;
14f9c5c9 651
61ee279c 652 type = ada_check_typedef (type);
14f9c5c9 653
ad82864c 654 if (ada_is_array_descriptor_type (type)
d2d43431
JB
655 || (ada_is_constrained_packed_array_type (type)
656 && TYPE_CODE (type) != TYPE_CODE_PTR))
14f9c5c9 657 {
d2e4a39e
AS
658 struct value *mark = value_mark ();
659 struct value *val;
5b4ee69b 660
490f124f 661 val = value_from_contents_and_address (type, valaddr + offset, address);
2d4a02ee
JB
662 /* If this is a reference, coerce it now. This helps taking care
663 of the case where ADDRESS is meaningless because original_value
664 was not an lval. */
665 val = coerce_ref (val);
720d1a40
JB
666 if (TYPE_CODE (type) == TYPE_CODE_TYPEDEF) /* array access type. */
667 val = ada_coerce_to_simple_array_ptr (val);
668 else
669 val = ada_coerce_to_simple_array (val);
14f9c5c9
AS
670 if (val == NULL)
671 {
9e87a409
JB
672 gdb_assert (TYPE_CODE (type) == TYPE_CODE_TYPEDEF);
673 fprintf_filtered (stream, "0x0");
14f9c5c9
AS
674 }
675 else
d3eab38a
TT
676 ada_val_print_1 (value_type (val),
677 value_contents_for_printing (val),
678 value_embedded_offset (val),
679 value_address (val), stream, recurse,
680 val, options);
14f9c5c9 681 value_free_to_mark (mark);
d3eab38a 682 return;
14f9c5c9
AS
683 }
684
490f124f
PA
685 offset_aligned = offset + ada_aligned_value_addr (type, valaddr) - valaddr;
686 type = printable_val_type (type, valaddr + offset_aligned);
14f9c5c9
AS
687
688 switch (TYPE_CODE (type))
689 {
690 default:
d3eab38a
TT
691 c_val_print (type, valaddr, offset, address, stream,
692 recurse, original_value, options);
693 break;
14f9c5c9 694
4c4b4cd2
PH
695 case TYPE_CODE_PTR:
696 {
d3eab38a
TT
697 c_val_print (type, valaddr, offset, address,
698 stream, recurse, original_value, options);
5b4ee69b 699
4c4b4cd2
PH
700 if (ada_is_tag_type (type))
701 {
490f124f
PA
702 struct value *val =
703 value_from_contents_and_address (type,
704 valaddr + offset_aligned,
705 address + offset_aligned);
4c4b4cd2 706 const char *name = ada_tag_name (val);
5b4ee69b 707
4c4b4cd2
PH
708 if (name != NULL)
709 fprintf_filtered (stream, " (%s)", name);
d3eab38a
TT
710 }
711 return;
4c4b4cd2
PH
712 }
713
14f9c5c9
AS
714 case TYPE_CODE_INT:
715 case TYPE_CODE_RANGE:
716 if (ada_is_fixed_point_type (type))
717 {
490f124f 718 LONGEST v = unpack_long (type, valaddr + offset_aligned);
14f9c5c9 719
744a8059 720 fprintf_filtered (stream, TYPE_LENGTH (type) < 4 ? "%.11g" : "%.17g",
14f9c5c9 721 (double) ada_fixed_to_float (type, v));
d3eab38a 722 return;
14f9c5c9 723 }
14f9c5c9
AS
724 else if (TYPE_CODE (type) == TYPE_CODE_RANGE)
725 {
d2e4a39e 726 struct type *target_type = TYPE_TARGET_TYPE (type);
5b4ee69b 727
14f9c5c9
AS
728 if (TYPE_LENGTH (type) != TYPE_LENGTH (target_type))
729 {
730 /* Obscure case of range type that has different length from
d2e4a39e
AS
731 its base type. Perform a conversion, or we will get a
732 nonsense value. Actually, we could use the same
4c4b4cd2 733 code regardless of lengths; I'm just avoiding a cast. */
490f124f
PA
734 struct value *v1
735 = value_from_contents_and_address (type, valaddr + offset, 0);
736 struct value *v = value_cast (target_type, v1);
737
d3eab38a
TT
738 ada_val_print_1 (target_type,
739 value_contents_for_printing (v),
740 value_embedded_offset (v), 0,
741 stream, recurse + 1, v, options);
14f9c5c9
AS
742 }
743 else
d3eab38a
TT
744 ada_val_print_1 (TYPE_TARGET_TYPE (type),
745 valaddr, offset,
746 address, stream, recurse,
747 original_value, options);
748 return;
14f9c5c9 749 }
d2e4a39e 750 else
14f9c5c9 751 {
79a45b7d
TT
752 int format = (options->format ? options->format
753 : options->output_format);
5b4ee69b 754
14f9c5c9
AS
755 if (format)
756 {
79a45b7d 757 struct value_print_options opts = *options;
5b4ee69b 758
79a45b7d 759 opts.format = format;
ab2188aa
PA
760 val_print_scalar_formatted (type, valaddr, offset_aligned,
761 original_value, &opts, 0, stream);
14f9c5c9 762 }
50810684 763 else if (ada_is_system_address_type (type))
4c4b4cd2
PH
764 {
765 /* FIXME: We want to print System.Address variables using
766 the same format as for any access type. But for some
767 reason GNAT encodes the System.Address type as an int,
768 so we have to work-around this deficiency by handling
50810684 769 System.Address values as a special case. */
8ca1c40e 770
50810684 771 struct gdbarch *gdbarch = get_type_arch (type);
8ca1c40e 772 struct type *ptr_type = builtin_type (gdbarch)->builtin_data_ptr;
490f124f
PA
773 CORE_ADDR addr = extract_typed_address (valaddr + offset_aligned,
774 ptr_type);
8ca1c40e 775
4c4b4cd2
PH
776 fprintf_filtered (stream, "(");
777 type_print (type, "", stream, -1);
778 fprintf_filtered (stream, ") ");
5af949e3 779 fputs_filtered (paddress (gdbarch, addr), stream);
4c4b4cd2 780 }
14f9c5c9
AS
781 else
782 {
490f124f 783 val_print_type_code_int (type, valaddr + offset_aligned, stream);
14f9c5c9
AS
784 if (ada_is_character_type (type))
785 {
490f124f
PA
786 LONGEST c;
787
14f9c5c9 788 fputs_filtered (" ", stream);
490f124f
PA
789 c = unpack_long (type, valaddr + offset_aligned);
790 ada_printchar (c, type, stream);
14f9c5c9
AS
791 }
792 }
d3eab38a 793 return;
14f9c5c9
AS
794 }
795
796 case TYPE_CODE_ENUM:
6c88661c
JB
797 {
798 unsigned int len;
799 LONGEST val;
5b4ee69b 800
6c88661c
JB
801 if (options->format)
802 {
803 val_print_scalar_formatted (type, valaddr, offset_aligned,
804 original_value, options, 0, stream);
805 break;
806 }
807 len = TYPE_NFIELDS (type);
808 val = unpack_long (type, valaddr + offset_aligned);
809 for (i = 0; i < len; i++)
810 {
811 QUIT;
14e75d8e 812 if (val == TYPE_FIELD_ENUMVAL (type, i))
6c88661c
JB
813 {
814 break;
815 }
816 }
817 if (i < len)
818 {
819 const char *name = ada_enum_name (TYPE_FIELD_NAME (type, i));
820
821 if (name[0] == '\'')
822 fprintf_filtered (stream, "%ld %s", (long) val, name);
823 else
824 fputs_filtered (name, stream);
825 }
826 else
827 {
828 print_longest (stream, 'd', 0, val);
829 }
830 break;
831 }
d2e4a39e 832
4c4b4cd2 833 case TYPE_CODE_FLT:
79a45b7d 834 if (options->format)
d3eab38a
TT
835 {
836 c_val_print (type, valaddr, offset, address, stream,
837 recurse, original_value, options);
838 return;
839 }
4c4b4cd2 840 else
490f124f 841 ada_print_floating (valaddr + offset, type, stream);
4c4b4cd2
PH
842 break;
843
14f9c5c9
AS
844 case TYPE_CODE_UNION:
845 case TYPE_CODE_STRUCT:
846 if (ada_is_bogus_array_descriptor (type))
847 {
848 fprintf_filtered (stream, "(...?)");
d3eab38a 849 return;
d2e4a39e 850 }
14f9c5c9
AS
851 else
852 {
490f124f
PA
853 print_record (type, valaddr, offset_aligned,
854 stream, recurse, original_value, options);
d3eab38a 855 return;
14f9c5c9
AS
856 }
857
858 case TYPE_CODE_ARRAY:
d3eab38a
TT
859 ada_val_print_array (type, valaddr, offset_aligned,
860 address, stream, recurse, original_value,
861 options);
862 return;
14f9c5c9
AS
863
864 case TYPE_CODE_REF:
969a1360
JB
865 /* For references, the debugger is expected to print the value as
866 an address if DEREF_REF is null. But printing an address in place
867 of the object value would be confusing to an Ada programmer.
868 So, for Ada values, we print the actual dereferenced value
869 regardless. */
14f9c5c9 870 elttype = check_typedef (TYPE_TARGET_TYPE (type));
969a1360
JB
871
872 if (TYPE_CODE (elttype) != TYPE_CODE_UNDEF)
873 {
a471c594
JK
874 CORE_ADDR deref_val_int;
875 struct value *deref_val;
5b4ee69b 876
a471c594
JK
877 deref_val = coerce_ref_if_computed (original_value);
878 if (deref_val)
879 {
b50d69b5
JG
880 if (ada_is_tagged_type (value_type (deref_val), 1))
881 deref_val = ada_tag_value_at_base_address (deref_val);
882
a471c594
JK
883 common_val_print (deref_val, stream, recurse + 1, options,
884 current_language);
885 break;
886 }
887
888 deref_val_int = unpack_pointer (type, valaddr + offset_aligned);
969a1360
JB
889 if (deref_val_int != 0)
890 {
6c88661c 891 deref_val =
490f124f 892 ada_value_ind (value_from_pointer
969a1360
JB
893 (lookup_pointer_type (elttype),
894 deref_val_int));
5b4ee69b 895
b50d69b5
JG
896 if (ada_is_tagged_type (value_type (deref_val), 1))
897 deref_val = ada_tag_value_at_base_address (deref_val);
898
969a1360 899 val_print (value_type (deref_val),
490f124f
PA
900 value_contents_for_printing (deref_val),
901 value_embedded_offset (deref_val),
42ae5230 902 value_address (deref_val), stream, recurse + 1,
a491d753 903 deref_val, options, current_language);
969a1360
JB
904 }
905 else
906 fputs_filtered ("(null)", stream);
907 }
908 else
909 fputs_filtered ("???", stream);
910
14f9c5c9
AS
911 break;
912 }
4c4b4cd2 913 gdb_flush (stream);
14f9c5c9
AS
914}
915
916static int
490f124f
PA
917print_variant_part (struct type *type, int field_num,
918 const gdb_byte *valaddr, int offset,
79a45b7d 919 struct ui_file *stream, int recurse,
0e03807e 920 const struct value *val,
0963b4bd
MS
921 const struct value_print_options *options,
922 int comma_needed,
490f124f 923 struct type *outer_type, int outer_offset)
14f9c5c9
AS
924{
925 struct type *var_type = TYPE_FIELD_TYPE (type, field_num);
490f124f
PA
926 int which = ada_which_variant_applies (var_type, outer_type,
927 valaddr + outer_offset);
14f9c5c9
AS
928
929 if (which < 0)
930 return 0;
931 else
d2e4a39e 932 return print_field_values
14f9c5c9 933 (TYPE_FIELD_TYPE (var_type, which),
490f124f
PA
934 valaddr,
935 offset + TYPE_FIELD_BITPOS (type, field_num) / HOST_CHAR_BIT
14f9c5c9 936 + TYPE_FIELD_BITPOS (var_type, which) / HOST_CHAR_BIT,
0e03807e 937 stream, recurse, val, options,
490f124f 938 comma_needed, outer_type, outer_offset);
14f9c5c9
AS
939}
940
8e069a98 941void
79a45b7d
TT
942ada_value_print (struct value *val0, struct ui_file *stream,
943 const struct value_print_options *options)
14f9c5c9 944{
0c3acc09
JB
945 struct value *val = ada_to_fixed_value (val0);
946 CORE_ADDR address = value_address (val);
f5aa6869 947 struct type *type = ada_check_typedef (value_type (val));
79a45b7d 948 struct value_print_options opts;
14f9c5c9 949
4c4b4cd2
PH
950 /* If it is a pointer, indicate what it points to. */
951 if (TYPE_CODE (type) == TYPE_CODE_PTR)
14f9c5c9 952 {
4c4b4cd2
PH
953 /* Hack: don't print (char *) for char strings. Their
954 type is indicated by the quoted string anyway. */
955 if (TYPE_LENGTH (TYPE_TARGET_TYPE (type)) != sizeof (char)
956 || TYPE_CODE (TYPE_TARGET_TYPE (type)) != TYPE_CODE_INT
957 || TYPE_UNSIGNED (TYPE_TARGET_TYPE (type)))
14f9c5c9
AS
958 {
959 fprintf_filtered (stream, "(");
960 type_print (type, "", stream, -1);
961 fprintf_filtered (stream, ") ");
962 }
963 }
4c4b4cd2 964 else if (ada_is_array_descriptor_type (type))
14f9c5c9 965 {
720d1a40
JB
966 /* We do not print the type description unless TYPE is an array
967 access type (this is encoded by the compiler as a typedef to
968 a fat pointer - hence the check against TYPE_CODE_TYPEDEF). */
969 if (TYPE_CODE (type) == TYPE_CODE_TYPEDEF)
970 {
971 fprintf_filtered (stream, "(");
972 type_print (type, "", stream, -1);
973 fprintf_filtered (stream, ") ");
974 }
14f9c5c9
AS
975 }
976 else if (ada_is_bogus_array_descriptor (type))
977 {
978 fprintf_filtered (stream, "(");
979 type_print (type, "", stream, -1);
980 fprintf_filtered (stream, ") (...?)");
8e069a98 981 return;
14f9c5c9 982 }
4c4b4cd2 983
79a45b7d
TT
984 opts = *options;
985 opts.deref_ref = 1;
8e069a98
TT
986 val_print (type, value_contents_for_printing (val),
987 value_embedded_offset (val), address,
988 stream, 0, val, &opts, current_language);
14f9c5c9 989}
d2e4a39e 990
14f9c5c9 991static void
fc1a4b47 992print_record (struct type *type, const gdb_byte *valaddr,
490f124f 993 int offset,
79a45b7d 994 struct ui_file *stream, int recurse,
0e03807e 995 const struct value *val,
79a45b7d 996 const struct value_print_options *options)
14f9c5c9 997{
61ee279c 998 type = ada_check_typedef (type);
14f9c5c9
AS
999
1000 fprintf_filtered (stream, "(");
1001
490f124f
PA
1002 if (print_field_values (type, valaddr, offset,
1003 stream, recurse, val, options,
2a998fc0 1004 0, type, offset) != 0 && options->prettyformat)
14f9c5c9
AS
1005 {
1006 fprintf_filtered (stream, "\n");
1007 print_spaces_filtered (2 * recurse, stream);
1008 }
1009
1010 fprintf_filtered (stream, ")");
1011}
1012
32b72a42 1013/* Print out fields of value at VALADDR + OFFSET having structure type TYPE.
4c4b4cd2 1014
32b72a42
PA
1015 TYPE, VALADDR, OFFSET, STREAM, RECURSE, and OPTIONS have the same
1016 meanings as in ada_print_value and ada_val_print.
14f9c5c9 1017
32b72a42
PA
1018 OUTER_TYPE and OUTER_OFFSET give type and address of enclosing
1019 record (used to get discriminant values when printing variant
1020 parts).
14f9c5c9 1021
4c4b4cd2 1022 COMMA_NEEDED is 1 if fields have been printed at the current recursion
14f9c5c9 1023 level, so that a comma is needed before any field printed by this
4c4b4cd2 1024 call.
14f9c5c9 1025
4c4b4cd2 1026 Returns 1 if COMMA_NEEDED or any fields were printed. */
14f9c5c9
AS
1027
1028static int
fc1a4b47 1029print_field_values (struct type *type, const gdb_byte *valaddr,
490f124f 1030 int offset, struct ui_file *stream, int recurse,
0e03807e 1031 const struct value *val,
79a45b7d
TT
1032 const struct value_print_options *options,
1033 int comma_needed,
490f124f 1034 struct type *outer_type, int outer_offset)
14f9c5c9
AS
1035{
1036 int i, len;
1037
1038 len = TYPE_NFIELDS (type);
1039
1040 for (i = 0; i < len; i += 1)
1041 {
1042 if (ada_is_ignored_field (type, i))
d2e4a39e 1043 continue;
14f9c5c9
AS
1044
1045 if (ada_is_wrapper_field (type, i))
1046 {
d2e4a39e 1047 comma_needed =
14f9c5c9 1048 print_field_values (TYPE_FIELD_TYPE (type, i),
490f124f
PA
1049 valaddr,
1050 (offset
1051 + TYPE_FIELD_BITPOS (type, i) / HOST_CHAR_BIT),
0e03807e 1052 stream, recurse, val, options,
490f124f 1053 comma_needed, type, offset);
14f9c5c9
AS
1054 continue;
1055 }
1056 else if (ada_is_variant_part (type, i))
1057 {
1058 comma_needed =
1059 print_variant_part (type, i, valaddr,
490f124f
PA
1060 offset, stream, recurse, val,
1061 options, comma_needed,
1062 outer_type, outer_offset);
14f9c5c9
AS
1063 continue;
1064 }
1065
1066 if (comma_needed)
1067 fprintf_filtered (stream, ", ");
1068 comma_needed = 1;
1069
2a998fc0 1070 if (options->prettyformat)
14f9c5c9
AS
1071 {
1072 fprintf_filtered (stream, "\n");
1073 print_spaces_filtered (2 + 2 * recurse, stream);
1074 }
d2e4a39e 1075 else
14f9c5c9
AS
1076 {
1077 wrap_here (n_spaces (2 + 2 * recurse));
1078 }
e93a8774
TT
1079
1080 annotate_field_begin (TYPE_FIELD_TYPE (type, i));
1081 fprintf_filtered (stream, "%.*s",
1082 ada_name_prefix_len (TYPE_FIELD_NAME (type, i)),
1083 TYPE_FIELD_NAME (type, i));
1084 annotate_field_name_end ();
1085 fputs_filtered (" => ", stream);
1086 annotate_field_value ();
14f9c5c9
AS
1087
1088 if (TYPE_FIELD_PACKED (type, i))
1089 {
d2e4a39e 1090 struct value *v;
14f9c5c9
AS
1091
1092 /* Bitfields require special handling, especially due to byte
1093 order problems. */
b4ba55a1 1094 if (HAVE_CPLUS_STRUCT (type) && TYPE_FIELD_IGNORE (type, i))
14f9c5c9 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);
79a45b7d 1102 struct value_print_options opts;
d2e4a39e 1103
14f9c5c9 1104 adjust_type_signedness (TYPE_FIELD_TYPE (type, i));
490f124f
PA
1105 v = ada_value_primitive_packed_val
1106 (NULL, valaddr,
1107 offset + bit_pos / HOST_CHAR_BIT,
1108 bit_pos % HOST_CHAR_BIT,
1109 bit_size, TYPE_FIELD_TYPE (type, i));
79a45b7d
TT
1110 opts = *options;
1111 opts.deref_ref = 0;
490f124f
PA
1112 val_print (TYPE_FIELD_TYPE (type, i),
1113 value_contents_for_printing (v),
1114 value_embedded_offset (v), 0,
0e03807e
TT
1115 stream, recurse + 1, v,
1116 &opts, current_language);
14f9c5c9
AS
1117 }
1118 }
1119 else
79a45b7d
TT
1120 {
1121 struct value_print_options opts = *options;
5b4ee69b 1122
79a45b7d
TT
1123 opts.deref_ref = 0;
1124 ada_val_print (TYPE_FIELD_TYPE (type, i),
490f124f
PA
1125 valaddr,
1126 (offset
1127 + TYPE_FIELD_BITPOS (type, i) / HOST_CHAR_BIT),
1128 0, stream, recurse + 1, val, &opts);
79a45b7d 1129 }
14f9c5c9
AS
1130 annotate_field_end ();
1131 }
1132
1133 return comma_needed;
1134}
This page took 0.768771 seconds and 4 git commands to generate.