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