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