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