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