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