Introduce ada_value_print_array
[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
d2e4a39e 277static struct type *
fc1a4b47 278printable_val_type (struct type *type, const gdb_byte *valaddr)
14f9c5c9 279{
1ed6ede0 280 return ada_to_fixed_type (ada_aligned_type (type), valaddr, 0, NULL, 1);
14f9c5c9
AS
281}
282
283/* Print the character C on STREAM as part of the contents of a literal
284 string whose delimiter is QUOTER. TYPE_LEN is the length in bytes
4ffa5a33 285 of the character. */
14f9c5c9
AS
286
287void
6c7a06a3
TT
288ada_emit_char (int c, struct type *type, struct ui_file *stream,
289 int quoter, int type_len)
14f9c5c9 290{
4ffa5a33
JB
291 /* If this character fits in the normal ASCII range, and is
292 a printable character, then print the character as if it was
293 an ASCII character, even if this is a wide character.
294 The UCHAR_MAX check is necessary because the isascii function
295 requires that its argument have a value of an unsigned char,
296 or EOF (EOF is obviously not printable). */
297 if (c <= UCHAR_MAX && isascii (c) && isprint (c))
14f9c5c9
AS
298 {
299 if (c == quoter && c == '"')
529cad9c 300 fprintf_filtered (stream, "\"\"");
14f9c5c9
AS
301 else
302 fprintf_filtered (stream, "%c", c);
303 }
304 else
d2e4a39e 305 fprintf_filtered (stream, "[\"%0*x\"]", type_len * 2, c);
14f9c5c9
AS
306}
307
4ffa5a33
JB
308/* Character #I of STRING, given that TYPE_LEN is the size in bytes
309 of a character. */
14f9c5c9
AS
310
311static int
e17a4113
UW
312char_at (const gdb_byte *string, int i, int type_len,
313 enum bfd_endian byte_order)
14f9c5c9
AS
314{
315 if (type_len == 1)
316 return string[i];
d2e4a39e 317 else
4ffa5a33
JB
318 return (int) extract_unsigned_integer (string + type_len * i,
319 type_len, byte_order);
14f9c5c9
AS
320}
321
4c4b4cd2
PH
322/* Print a floating-point value of type TYPE, pointed to in GDB by
323 VALADDR, on STREAM. Use Ada formatting conventions: there must be
324 a decimal point, and at least one digit before and after the
4fbf5aa5
JB
325 point. We use the GNAT format for NaNs and infinities. */
326
4c4b4cd2 327static void
fc1a4b47 328ada_print_floating (const gdb_byte *valaddr, struct type *type,
a2bd3dcd 329 struct ui_file *stream)
4c4b4cd2 330{
d7e74731 331 string_file tmp_stream;
4c4b4cd2 332
d7e74731 333 print_floating (valaddr, type, &tmp_stream);
77e1c742 334
d7e74731 335 std::string &s = tmp_stream.string ();
77e1c742 336 size_t skip_count = 0;
4c4b4cd2
PH
337
338 /* Modify for Ada rules. */
606b8d1a 339
77e1c742
PA
340 size_t pos = s.find ("inf");
341 if (pos == std::string::npos)
342 pos = s.find ("Inf");
343 if (pos == std::string::npos)
344 pos = s.find ("INF");
345 if (pos != std::string::npos)
346 s.replace (pos, 3, "Inf");
c3e5cd34 347
77e1c742 348 if (pos == std::string::npos)
4c4b4cd2 349 {
77e1c742
PA
350 pos = s.find ("nan");
351 if (pos == std::string::npos)
352 pos = s.find ("NaN");
353 if (pos == std::string::npos)
354 pos = s.find ("Nan");
355 if (pos != std::string::npos)
c3e5cd34 356 {
77e1c742
PA
357 s[pos] = s[pos + 2] = 'N';
358 if (s[0] == '-')
359 skip_count = 1;
c3e5cd34 360 }
4c4b4cd2 361 }
c3e5cd34 362
77e1c742
PA
363 if (pos == std::string::npos
364 && s.find ('.') == std::string::npos)
4c4b4cd2 365 {
77e1c742
PA
366 pos = s.find ('e');
367 if (pos == std::string::npos)
368 fprintf_filtered (stream, "%s.0", s.c_str ());
4c4b4cd2 369 else
77e1c742 370 fprintf_filtered (stream, "%.*s.0%s", (int) pos, s.c_str (), &s[pos]);
4c4b4cd2 371 }
4fbf5aa5 372 else
77e1c742 373 fprintf_filtered (stream, "%s", &s[skip_count]);
4c4b4cd2
PH
374}
375
14f9c5c9 376void
6c7a06a3 377ada_printchar (int c, struct type *type, struct ui_file *stream)
14f9c5c9
AS
378{
379 fputs_filtered ("'", stream);
447b483c 380 ada_emit_char (c, type, stream, '\'', TYPE_LENGTH (type));
14f9c5c9
AS
381 fputs_filtered ("'", stream);
382}
383
384/* [From print_type_scalar in typeprint.c]. Print VAL on STREAM in a
7c964f07
UW
385 form appropriate for TYPE, if non-NULL. If TYPE is NULL, print VAL
386 like a default signed integer. */
14f9c5c9
AS
387
388void
ebf56fd3 389ada_print_scalar (struct type *type, LONGEST val, struct ui_file *stream)
14f9c5c9
AS
390{
391 unsigned int i;
392 unsigned len;
393
7c964f07
UW
394 if (!type)
395 {
396 print_longest (stream, 'd', 0, val);
397 return;
398 }
399
61ee279c 400 type = ada_check_typedef (type);
14f9c5c9
AS
401
402 switch (TYPE_CODE (type))
403 {
404
405 case TYPE_CODE_ENUM:
406 len = TYPE_NFIELDS (type);
407 for (i = 0; i < len; i++)
408 {
14e75d8e 409 if (TYPE_FIELD_ENUMVAL (type, i) == val)
14f9c5c9
AS
410 {
411 break;
412 }
413 }
414 if (i < len)
415 {
3f0cbb04
TT
416 fputs_styled (ada_enum_name (TYPE_FIELD_NAME (type, i)),
417 variable_name_style.style (), stream);
14f9c5c9
AS
418 }
419 else
420 {
421 print_longest (stream, 'd', 0, val);
422 }
423 break;
424
425 case TYPE_CODE_INT:
426 print_longest (stream, TYPE_UNSIGNED (type) ? 'u' : 'd', 0, val);
427 break;
428
429 case TYPE_CODE_CHAR:
10d44370 430 LA_PRINT_CHAR (val, type, stream);
14f9c5c9
AS
431 break;
432
433 case TYPE_CODE_BOOL:
434 fprintf_filtered (stream, val ? "true" : "false");
435 break;
436
437 case TYPE_CODE_RANGE:
438 ada_print_scalar (TYPE_TARGET_TYPE (type), val, stream);
439 return;
440
441 case TYPE_CODE_UNDEF:
442 case TYPE_CODE_PTR:
443 case TYPE_CODE_ARRAY:
444 case TYPE_CODE_STRUCT:
445 case TYPE_CODE_UNION:
446 case TYPE_CODE_FUNC:
447 case TYPE_CODE_FLT:
448 case TYPE_CODE_VOID:
449 case TYPE_CODE_SET:
450 case TYPE_CODE_STRING:
451 case TYPE_CODE_ERROR:
0d5de010
DJ
452 case TYPE_CODE_MEMBERPTR:
453 case TYPE_CODE_METHODPTR:
14f9c5c9
AS
454 case TYPE_CODE_METHOD:
455 case TYPE_CODE_REF:
edefbb7c 456 warning (_("internal error: unhandled type in ada_print_scalar"));
14f9c5c9
AS
457 break;
458
459 default:
edefbb7c 460 error (_("Invalid type code in symbol table."));
14f9c5c9 461 }
14f9c5c9
AS
462}
463
464/* Print the character string STRING, printing at most LENGTH characters.
465 Printing stops early if the number hits print_max; repeat counts
466 are printed as appropriate. Print ellipses at the end if we
9a153e0b
JB
467 had to stop before printing LENGTH characters, or if FORCE_ELLIPSES.
468 TYPE_LEN is the length (1 or 2) of the character type. */
14f9c5c9
AS
469
470static void
6c7a06a3 471printstr (struct ui_file *stream, struct type *elttype, const gdb_byte *string,
79a45b7d
TT
472 unsigned int length, int force_ellipses, int type_len,
473 const struct value_print_options *options)
14f9c5c9 474{
34877895 475 enum bfd_endian byte_order = type_byte_order (elttype);
14f9c5c9
AS
476 unsigned int i;
477 unsigned int things_printed = 0;
478 int in_quotes = 0;
479 int need_comma = 0;
480
481 if (length == 0)
482 {
483 fputs_filtered ("\"\"", stream);
484 return;
485 }
486
79a45b7d 487 for (i = 0; i < length && things_printed < options->print_max; i += 1)
14f9c5c9
AS
488 {
489 /* Position of the character we are examining
d2e4a39e 490 to see whether it is repeated. */
14f9c5c9
AS
491 unsigned int rep1;
492 /* Number of repetitions we have detected so far. */
493 unsigned int reps;
494
495 QUIT;
496
497 if (need_comma)
498 {
499 fputs_filtered (", ", stream);
500 need_comma = 0;
501 }
502
503 rep1 = i + 1;
504 reps = 1;
c3e5cd34 505 while (rep1 < length
e17a4113
UW
506 && char_at (string, rep1, type_len, byte_order)
507 == char_at (string, i, type_len, byte_order))
14f9c5c9
AS
508 {
509 rep1 += 1;
510 reps += 1;
511 }
512
79a45b7d 513 if (reps > options->repeat_count_threshold)
14f9c5c9
AS
514 {
515 if (in_quotes)
516 {
e93a8774 517 fputs_filtered ("\", ", stream);
14f9c5c9
AS
518 in_quotes = 0;
519 }
520 fputs_filtered ("'", stream);
e17a4113
UW
521 ada_emit_char (char_at (string, i, type_len, byte_order),
522 elttype, stream, '\'', type_len);
14f9c5c9 523 fputs_filtered ("'", stream);
7f6aba03
TT
524 fprintf_filtered (stream, _(" %p[<repeats %u times>%p]"),
525 metadata_style.style ().ptr (), reps, nullptr);
14f9c5c9 526 i = rep1 - 1;
79a45b7d 527 things_printed += options->repeat_count_threshold;
14f9c5c9
AS
528 need_comma = 1;
529 }
530 else
531 {
532 if (!in_quotes)
533 {
e93a8774 534 fputs_filtered ("\"", stream);
14f9c5c9
AS
535 in_quotes = 1;
536 }
e17a4113
UW
537 ada_emit_char (char_at (string, i, type_len, byte_order),
538 elttype, stream, '"', type_len);
14f9c5c9
AS
539 things_printed += 1;
540 }
541 }
542
543 /* Terminate the quotes if necessary. */
544 if (in_quotes)
e93a8774 545 fputs_filtered ("\"", stream);
14f9c5c9
AS
546
547 if (force_ellipses || i < length)
548 fputs_filtered ("...", stream);
549}
550
551void
0963b4bd
MS
552ada_printstr (struct ui_file *stream, struct type *type,
553 const gdb_byte *string, unsigned int length,
554 const char *encoding, int force_ellipses,
79a45b7d 555 const struct value_print_options *options)
14f9c5c9 556{
6c7a06a3
TT
557 printstr (stream, type, string, length, force_ellipses, TYPE_LENGTH (type),
558 options);
14f9c5c9
AS
559}
560
bdf779a0
JB
561static int
562print_variant_part (struct type *type, int field_num,
563 const gdb_byte *valaddr, int offset,
564 struct ui_file *stream, int recurse,
e8b24d9f 565 struct value *val,
bdf779a0
JB
566 const struct value_print_options *options,
567 int comma_needed,
8e355c5d
JB
568 struct type *outer_type, int outer_offset,
569 const struct language_defn *language)
bdf779a0
JB
570{
571 struct type *var_type = TYPE_FIELD_TYPE (type, field_num);
572 int which = ada_which_variant_applies (var_type, outer_type,
573 valaddr + outer_offset);
574
575 if (which < 0)
576 return 0;
577 else
578 return print_field_values
579 (TYPE_FIELD_TYPE (var_type, which),
580 valaddr,
581 offset + TYPE_FIELD_BITPOS (type, field_num) / HOST_CHAR_BIT
582 + TYPE_FIELD_BITPOS (var_type, which) / HOST_CHAR_BIT,
583 stream, recurse, val, options,
8e355c5d 584 comma_needed, outer_type, outer_offset, language);
bdf779a0
JB
585}
586
587/* Print out fields of value at VALADDR + OFFSET having structure type TYPE.
588
589 TYPE, VALADDR, OFFSET, STREAM, RECURSE, and OPTIONS have the same
590 meanings as in ada_print_value and ada_val_print.
591
592 OUTER_TYPE and OUTER_OFFSET give type and address of enclosing
593 record (used to get discriminant values when printing variant
594 parts).
595
596 COMMA_NEEDED is 1 if fields have been printed at the current recursion
597 level, so that a comma is needed before any field printed by this
598 call.
599
600 Returns 1 if COMMA_NEEDED or any fields were printed. */
601
602static int
603print_field_values (struct type *type, const gdb_byte *valaddr,
604 int offset, struct ui_file *stream, int recurse,
e8b24d9f 605 struct value *val,
bdf779a0
JB
606 const struct value_print_options *options,
607 int comma_needed,
8e355c5d
JB
608 struct type *outer_type, int outer_offset,
609 const struct language_defn *language)
bdf779a0
JB
610{
611 int i, len;
612
613 len = TYPE_NFIELDS (type);
614
615 for (i = 0; i < len; i += 1)
616 {
617 if (ada_is_ignored_field (type, i))
618 continue;
619
620 if (ada_is_wrapper_field (type, i))
621 {
622 comma_needed =
623 print_field_values (TYPE_FIELD_TYPE (type, i),
624 valaddr,
625 (offset
626 + TYPE_FIELD_BITPOS (type, i) / HOST_CHAR_BIT),
627 stream, recurse, val, options,
8e355c5d 628 comma_needed, type, offset, language);
bdf779a0
JB
629 continue;
630 }
631 else if (ada_is_variant_part (type, i))
632 {
633 comma_needed =
634 print_variant_part (type, i, valaddr,
635 offset, stream, recurse, val,
636 options, comma_needed,
8e355c5d 637 outer_type, outer_offset, language);
bdf779a0
JB
638 continue;
639 }
640
641 if (comma_needed)
642 fprintf_filtered (stream, ", ");
643 comma_needed = 1;
644
645 if (options->prettyformat)
646 {
647 fprintf_filtered (stream, "\n");
648 print_spaces_filtered (2 + 2 * recurse, stream);
649 }
650 else
651 {
652 wrap_here (n_spaces (2 + 2 * recurse));
653 }
654
655 annotate_field_begin (TYPE_FIELD_TYPE (type, i));
656 fprintf_filtered (stream, "%.*s",
657 ada_name_prefix_len (TYPE_FIELD_NAME (type, i)),
658 TYPE_FIELD_NAME (type, i));
659 annotate_field_name_end ();
660 fputs_filtered (" => ", stream);
661 annotate_field_value ();
662
663 if (TYPE_FIELD_PACKED (type, i))
664 {
bdf779a0
JB
665 /* Bitfields require special handling, especially due to byte
666 order problems. */
667 if (HAVE_CPLUS_STRUCT (type) && TYPE_FIELD_IGNORE (type, i))
668 {
7f6aba03
TT
669 fputs_styled (_("<optimized out or zero length>"),
670 metadata_style.style (), stream);
bdf779a0
JB
671 }
672 else
673 {
e8b24d9f 674 struct value *v;
bdf779a0
JB
675 int bit_pos = TYPE_FIELD_BITPOS (type, i);
676 int bit_size = TYPE_FIELD_BITSIZE (type, i);
677 struct value_print_options opts;
678
679 adjust_type_signedness (TYPE_FIELD_TYPE (type, i));
680 v = ada_value_primitive_packed_val
681 (NULL, valaddr,
682 offset + bit_pos / HOST_CHAR_BIT,
683 bit_pos % HOST_CHAR_BIT,
684 bit_size, TYPE_FIELD_TYPE (type, i));
685 opts = *options;
686 opts.deref_ref = 0;
687 val_print (TYPE_FIELD_TYPE (type, i),
bdf779a0
JB
688 value_embedded_offset (v), 0,
689 stream, recurse + 1, v,
8e355c5d 690 &opts, language);
bdf779a0
JB
691 }
692 }
693 else
694 {
695 struct value_print_options opts = *options;
696
697 opts.deref_ref = 0;
e8b24d9f 698 val_print (TYPE_FIELD_TYPE (type, i),
8e355c5d
JB
699 (offset + TYPE_FIELD_BITPOS (type, i) / HOST_CHAR_BIT),
700 0, stream, recurse + 1, val, &opts, language);
bdf779a0
JB
701 }
702 annotate_field_end ();
703 }
704
705 return comma_needed;
706}
707
71855601
JB
708/* Implement Ada val_print'ing for the case where TYPE is
709 a TYPE_CODE_ARRAY of characters. */
710
711static void
712ada_val_print_string (struct type *type, const gdb_byte *valaddr,
b59eac37 713 int offset_aligned,
71855601 714 struct ui_file *stream, int recurse,
71855601
JB
715 const struct value_print_options *options)
716{
34877895 717 enum bfd_endian byte_order = type_byte_order (type);
71855601
JB
718 struct type *elttype = TYPE_TARGET_TYPE (type);
719 unsigned int eltlen;
720 unsigned int len;
721
722 /* We know that ELTTYPE cannot possibly be null, because we assume
723 that we're called only when TYPE is a string-like type.
724 Similarly, the size of ELTTYPE should also be non-null, since
725 it's a character-like type. */
726 gdb_assert (elttype != NULL);
727 gdb_assert (TYPE_LENGTH (elttype) != 0);
728
729 eltlen = TYPE_LENGTH (elttype);
730 len = TYPE_LENGTH (type) / eltlen;
731
732 if (options->prettyformat_arrays)
733 print_spaces_filtered (2 + 2 * recurse, stream);
734
735 /* If requested, look for the first null char and only print
736 elements up to it. */
737 if (options->stop_print_at_null)
738 {
739 int temp_len;
740
741 /* Look for a NULL char. */
742 for (temp_len = 0;
743 (temp_len < len
744 && temp_len < options->print_max
745 && char_at (valaddr + offset_aligned,
746 temp_len, eltlen, byte_order) != 0);
747 temp_len += 1);
748 len = temp_len;
749 }
750
751 printstr (stream, elttype, valaddr + offset_aligned, len, 0,
752 eltlen, options);
753}
754
8004dfd1
JB
755/* Implement Ada val_print-ing for GNAT arrays (Eg. fat pointers,
756 thin pointers, etc). */
757
758static void
5b5e15ec 759ada_val_print_gnat_array (struct value *val,
8004dfd1 760 struct ui_file *stream, int recurse,
2228ef77 761 const struct value_print_options *options)
8004dfd1 762{
5b5e15ec
TT
763 scoped_value_mark free_values;
764
765 struct type *type = ada_check_typedef (value_type (val));
8004dfd1 766
8004dfd1
JB
767 /* If this is a reference, coerce it now. This helps taking care
768 of the case where ADDRESS is meaningless because original_value
769 was not an lval. */
770 val = coerce_ref (val);
771 if (TYPE_CODE (type) == TYPE_CODE_TYPEDEF) /* array access type. */
772 val = ada_coerce_to_simple_array_ptr (val);
773 else
774 val = ada_coerce_to_simple_array (val);
775 if (val == NULL)
776 {
777 gdb_assert (TYPE_CODE (type) == TYPE_CODE_TYPEDEF);
778 fprintf_filtered (stream, "0x0");
779 }
780 else
5b5e15ec
TT
781 common_val_print (val, stream, recurse, options,
782 language_def (language_ada));
8004dfd1
JB
783}
784
785/* Implement Ada val_print'ing for the case where TYPE is
786 a TYPE_CODE_PTR. */
787
788static void
789ada_val_print_ptr (struct type *type, const gdb_byte *valaddr,
790 int offset, int offset_aligned, CORE_ADDR address,
791 struct ui_file *stream, int recurse,
e8b24d9f 792 struct value *original_value,
2228ef77 793 const struct value_print_options *options)
8004dfd1 794{
e8b24d9f 795 val_print (type, offset, address, stream, recurse,
8004dfd1
JB
796 original_value, options, language_def (language_c));
797
798 if (ada_is_tag_type (type))
799 {
800 struct value *val =
801 value_from_contents_and_address (type,
802 valaddr + offset_aligned,
803 address + offset_aligned);
804 const char *name = ada_tag_name (val);
805
806 if (name != NULL)
807 fprintf_filtered (stream, " (%s)", name);
808 }
809}
810
416595d6
TT
811/* Implement Ada value_print'ing for the case where TYPE is a
812 TYPE_CODE_PTR. */
813
814static void
815ada_value_print_ptr (struct value *val,
816 struct ui_file *stream, int recurse,
817 const struct value_print_options *options)
818{
819 common_val_print (val, stream, recurse, options, language_def (language_c));
820
821 struct type *type = ada_check_typedef (value_type (val));
822 if (ada_is_tag_type (type))
823 {
824 const char *name = ada_tag_name (val);
825
826 if (name != NULL)
827 fprintf_filtered (stream, " (%s)", name);
828 }
829}
830
8004dfd1
JB
831/* Implement Ada val_print'ing for the case where TYPE is
832 a TYPE_CODE_INT or TYPE_CODE_RANGE. */
833
834static void
835ada_val_print_num (struct type *type, const gdb_byte *valaddr,
836 int offset, int offset_aligned, CORE_ADDR address,
837 struct ui_file *stream, int recurse,
e8b24d9f 838 struct value *original_value,
2228ef77 839 const struct value_print_options *options)
8004dfd1
JB
840{
841 if (ada_is_fixed_point_type (type))
842 {
50eff16b
UW
843 struct value *scale = ada_scaling_factor (type);
844 struct value *v = value_from_contents (type, valaddr + offset_aligned);
845 v = value_cast (value_type (scale), v);
846 v = value_binop (v, scale, BINOP_MUL);
847
848 const char *fmt = TYPE_LENGTH (type) < 4 ? "%.11g" : "%.17g";
849 std::string str
850 = target_float_to_string (value_contents (v), value_type (v), fmt);
851 fputs_filtered (str.c_str (), stream);
8004dfd1
JB
852 return;
853 }
4e962e74
TT
854 else if (TYPE_CODE (type) == TYPE_CODE_RANGE
855 && (TYPE_CODE (TYPE_TARGET_TYPE (type)) == TYPE_CODE_ENUM
856 || TYPE_CODE (TYPE_TARGET_TYPE (type)) == TYPE_CODE_BOOL
857 || TYPE_CODE (TYPE_TARGET_TYPE (type)) == TYPE_CODE_CHAR))
8004dfd1 858 {
4e962e74
TT
859 /* For enum-valued ranges, we want to recurse, because we'll end
860 up printing the constant's name rather than its numeric
861 value. Character and fixed-point types are also printed
862 differently, so recuse for those as well. */
8004dfd1
JB
863 struct type *target_type = TYPE_TARGET_TYPE (type);
864
865 if (TYPE_LENGTH (type) != TYPE_LENGTH (target_type))
866 {
867 /* Obscure case of range type that has different length from
868 its base type. Perform a conversion, or we will get a
869 nonsense value. Actually, we could use the same
870 code regardless of lengths; I'm just avoiding a cast. */
871 struct value *v1
872 = value_from_contents_and_address (type, valaddr + offset, 0);
873 struct value *v = value_cast (target_type, v1);
874
e8b24d9f 875 val_print (target_type,
8004dfd1 876 value_embedded_offset (v), 0, stream,
2228ef77
XR
877 recurse + 1, v, options,
878 language_def (language_ada));
8004dfd1
JB
879 }
880 else
e8b24d9f 881 val_print (TYPE_TARGET_TYPE (type), offset,
8004dfd1 882 address, stream, recurse, original_value,
2228ef77 883 options, language_def (language_ada));
8004dfd1
JB
884 return;
885 }
886 else
887 {
888 int format = (options->format ? options->format
889 : options->output_format);
890
891 if (format)
892 {
893 struct value_print_options opts = *options;
894
895 opts.format = format;
e8b24d9f 896 val_print_scalar_formatted (type, offset_aligned,
8004dfd1
JB
897 original_value, &opts, 0, stream);
898 }
899 else if (ada_is_system_address_type (type))
900 {
901 /* FIXME: We want to print System.Address variables using
902 the same format as for any access type. But for some
903 reason GNAT encodes the System.Address type as an int,
904 so we have to work-around this deficiency by handling
905 System.Address values as a special case. */
906
907 struct gdbarch *gdbarch = get_type_arch (type);
908 struct type *ptr_type = builtin_type (gdbarch)->builtin_data_ptr;
909 CORE_ADDR addr = extract_typed_address (valaddr + offset_aligned,
910 ptr_type);
911
912 fprintf_filtered (stream, "(");
913 type_print (type, "", stream, -1);
914 fprintf_filtered (stream, ") ");
915 fputs_filtered (paddress (gdbarch, addr), stream);
916 }
917 else
918 {
f12f6bad
TT
919 val_print_scalar_formatted (type, offset_aligned,
920 original_value, options, 0, stream);
8004dfd1
JB
921 if (ada_is_character_type (type))
922 {
923 LONGEST c;
924
925 fputs_filtered (" ", stream);
926 c = unpack_long (type, valaddr + offset_aligned);
927 ada_printchar (c, type, stream);
928 }
929 }
930 return;
931 }
932}
933
39ef85a8
TT
934/* Implement Ada val_print'ing for the case where TYPE is
935 a TYPE_CODE_INT or TYPE_CODE_RANGE. */
936
937static void
938ada_value_print_num (struct value *val, struct ui_file *stream, int recurse,
939 const struct value_print_options *options)
940{
941 struct type *type = ada_check_typedef (value_type (val));
942 const gdb_byte *valaddr = value_contents_for_printing (val);
943
944 if (ada_is_fixed_point_type (type))
945 {
946 struct value *scale = ada_scaling_factor (type);
947 val = value_cast (value_type (scale), val);
948 val = value_binop (val, scale, BINOP_MUL);
949
950 const char *fmt = TYPE_LENGTH (type) < 4 ? "%.11g" : "%.17g";
951 std::string str
952 = target_float_to_string (value_contents (val), value_type (val), fmt);
953 fputs_filtered (str.c_str (), stream);
954 return;
955 }
956 else if (TYPE_CODE (type) == TYPE_CODE_RANGE
957 && (TYPE_CODE (TYPE_TARGET_TYPE (type)) == TYPE_CODE_ENUM
958 || TYPE_CODE (TYPE_TARGET_TYPE (type)) == TYPE_CODE_BOOL
959 || TYPE_CODE (TYPE_TARGET_TYPE (type)) == TYPE_CODE_CHAR))
960 {
961 /* For enum-valued ranges, we want to recurse, because we'll end
962 up printing the constant's name rather than its numeric
963 value. Character and fixed-point types are also printed
964 differently, so recuse for those as well. */
965 struct type *target_type = TYPE_TARGET_TYPE (type);
966 val = value_cast (target_type, val);
967 common_val_print (val, stream, recurse + 1, options,
968 language_def (language_ada));
969 return;
970 }
971 else
972 {
973 int format = (options->format ? options->format
974 : options->output_format);
975
976 if (format)
977 {
978 struct value_print_options opts = *options;
979
980 opts.format = format;
981 value_print_scalar_formatted (val, &opts, 0, stream);
982 }
983 else if (ada_is_system_address_type (type))
984 {
985 /* FIXME: We want to print System.Address variables using
986 the same format as for any access type. But for some
987 reason GNAT encodes the System.Address type as an int,
988 so we have to work-around this deficiency by handling
989 System.Address values as a special case. */
990
991 struct gdbarch *gdbarch = get_type_arch (type);
992 struct type *ptr_type = builtin_type (gdbarch)->builtin_data_ptr;
993 CORE_ADDR addr = extract_typed_address (valaddr, ptr_type);
994
995 fprintf_filtered (stream, "(");
996 type_print (type, "", stream, -1);
997 fprintf_filtered (stream, ") ");
998 fputs_filtered (paddress (gdbarch, addr), stream);
999 }
1000 else
1001 {
1002 value_print_scalar_formatted (val, options, 0, stream);
1003 if (ada_is_character_type (type))
1004 {
1005 LONGEST c;
1006
1007 fputs_filtered (" ", stream);
1008 c = unpack_long (type, valaddr);
1009 ada_printchar (c, type, stream);
1010 }
1011 }
1012 return;
1013 }
1014}
1015
8004dfd1
JB
1016/* Implement Ada val_print'ing for the case where TYPE is
1017 a TYPE_CODE_ENUM. */
1018
1019static void
1020ada_val_print_enum (struct type *type, const gdb_byte *valaddr,
1021 int offset, int offset_aligned, CORE_ADDR address,
1022 struct ui_file *stream, int recurse,
e8b24d9f 1023 struct value *original_value,
2228ef77 1024 const struct value_print_options *options)
8004dfd1
JB
1025{
1026 int i;
1027 unsigned int len;
1028 LONGEST val;
1029
1030 if (options->format)
1031 {
e8b24d9f 1032 val_print_scalar_formatted (type, offset_aligned,
8004dfd1
JB
1033 original_value, options, 0, stream);
1034 return;
1035 }
1036
1037 len = TYPE_NFIELDS (type);
1038 val = unpack_long (type, valaddr + offset_aligned);
1039 for (i = 0; i < len; i++)
1040 {
1041 QUIT;
1042 if (val == TYPE_FIELD_ENUMVAL (type, i))
1043 break;
1044 }
1045
1046 if (i < len)
1047 {
1048 const char *name = ada_enum_name (TYPE_FIELD_NAME (type, i));
1049
1050 if (name[0] == '\'')
3f0cbb04
TT
1051 fprintf_filtered (stream, "%ld %ps", (long) val,
1052 styled_string (variable_name_style.style (),
1053 name));
8004dfd1 1054 else
3f0cbb04 1055 fputs_styled (name, variable_name_style.style (), stream);
8004dfd1
JB
1056 }
1057 else
1058 print_longest (stream, 'd', 0, val);
1059}
1060
1061/* Implement Ada val_print'ing for the case where TYPE is
1062 a TYPE_CODE_FLT. */
1063
1064static void
1065ada_val_print_flt (struct type *type, const gdb_byte *valaddr,
1066 int offset, int offset_aligned, CORE_ADDR address,
1067 struct ui_file *stream, int recurse,
e8b24d9f 1068 struct value *original_value,
2228ef77 1069 const struct value_print_options *options)
8004dfd1
JB
1070{
1071 if (options->format)
1072 {
e8b24d9f 1073 val_print (type, offset, address, stream, recurse,
8004dfd1
JB
1074 original_value, options, language_def (language_c));
1075 return;
1076 }
1077
1078 ada_print_floating (valaddr + offset, type, stream);
1079}
1080
1081/* Implement Ada val_print'ing for the case where TYPE is
1082 a TYPE_CODE_STRUCT or TYPE_CODE_UNION. */
1083
1084static void
1085ada_val_print_struct_union
1086 (struct type *type, const gdb_byte *valaddr, int offset,
1087 int offset_aligned, CORE_ADDR address, struct ui_file *stream,
e8b24d9f 1088 int recurse, struct value *original_value,
2228ef77 1089 const struct value_print_options *options)
8004dfd1
JB
1090{
1091 if (ada_is_bogus_array_descriptor (type))
1092 {
1093 fprintf_filtered (stream, "(...?)");
1094 return;
1095 }
1096
079e4591
JB
1097 fprintf_filtered (stream, "(");
1098
1099 if (print_field_values (type, valaddr, offset_aligned,
1100 stream, recurse, original_value, options,
2228ef77
XR
1101 0, type, offset_aligned,
1102 language_def (language_ada)) != 0
079e4591
JB
1103 && options->prettyformat)
1104 {
1105 fprintf_filtered (stream, "\n");
1106 print_spaces_filtered (2 * recurse, stream);
1107 }
1108
1109 fprintf_filtered (stream, ")");
8004dfd1
JB
1110}
1111
4eb27a30
JB
1112/* Implement Ada val_print'ing for the case where TYPE is
1113 a TYPE_CODE_ARRAY. */
1114
1115static void
1116ada_val_print_array (struct type *type, const gdb_byte *valaddr,
1117 int offset, int offset_aligned, CORE_ADDR address,
1118 struct ui_file *stream, int recurse,
e8b24d9f 1119 struct value *original_value,
4eb27a30
JB
1120 const struct value_print_options *options)
1121{
71855601 1122 /* For an array of characters, print with string syntax. */
4eb27a30
JB
1123 if (ada_is_string_type (type)
1124 && (options->format == 0 || options->format == 's'))
1125 {
b59eac37
TT
1126 ada_val_print_string (type, valaddr, offset_aligned,
1127 stream, recurse, options);
71855601 1128 return;
4eb27a30 1129 }
71855601
JB
1130
1131 fprintf_filtered (stream, "(");
1132 print_optional_low_bound (stream, type, options);
1133 if (TYPE_FIELD_BITSIZE (type, 0) > 0)
1134 val_print_packed_array_elements (type, valaddr, offset_aligned,
b59eac37 1135 stream, recurse, options);
4eb27a30 1136 else
e8b24d9f 1137 val_print_array_elements (type, offset_aligned, address,
71855601
JB
1138 stream, recurse, original_value,
1139 options, 0);
1140 fprintf_filtered (stream, ")");
4eb27a30
JB
1141}
1142
b59eac37
TT
1143/* Implement Ada value_print'ing for the case where TYPE is a
1144 TYPE_CODE_ARRAY. */
1145
1146static void
1147ada_value_print_array (struct value *val, struct ui_file *stream, int recurse,
1148 const struct value_print_options *options)
1149{
1150 struct type *type = ada_check_typedef (value_type (val));
1151
1152 /* For an array of characters, print with string syntax. */
1153 if (ada_is_string_type (type)
1154 && (options->format == 0 || options->format == 's'))
1155 {
1156 const gdb_byte *valaddr = value_contents_for_printing (val);
1157 int offset_aligned = ada_aligned_value_addr (type, valaddr) - valaddr;
1158
1159 ada_val_print_string (type, valaddr, offset_aligned, stream, recurse,
1160 options);
1161 return;
1162 }
1163
1164 fprintf_filtered (stream, "(");
1165 print_optional_low_bound (stream, type, options);
1166 if (TYPE_FIELD_BITSIZE (type, 0) > 0)
1167 {
1168 const gdb_byte *valaddr = value_contents_for_printing (val);
1169 int offset_aligned = ada_aligned_value_addr (type, valaddr) - valaddr;
1170 val_print_packed_array_elements (type, valaddr, offset_aligned,
1171 stream, recurse, options);
1172 }
1173 else
1174 value_print_array_elements (val, stream, recurse, options, 0);
1175 fprintf_filtered (stream, ")");
1176}
1177
8004dfd1
JB
1178/* Implement Ada val_print'ing for the case where TYPE is
1179 a TYPE_CODE_REF. */
1180
1181static void
1182ada_val_print_ref (struct type *type, const gdb_byte *valaddr,
1183 int offset, int offset_aligned, CORE_ADDR address,
1184 struct ui_file *stream, int recurse,
e8b24d9f 1185 struct value *original_value,
2228ef77 1186 const struct value_print_options *options)
8004dfd1
JB
1187{
1188 /* For references, the debugger is expected to print the value as
1189 an address if DEREF_REF is null. But printing an address in place
1190 of the object value would be confusing to an Ada programmer.
1191 So, for Ada values, we print the actual dereferenced value
1192 regardless. */
1193 struct type *elttype = check_typedef (TYPE_TARGET_TYPE (type));
34b27950
JB
1194 struct value *deref_val;
1195 CORE_ADDR deref_val_int;
8004dfd1 1196
34b27950 1197 if (TYPE_CODE (elttype) == TYPE_CODE_UNDEF)
8004dfd1 1198 {
7f6aba03
TT
1199 fputs_styled ("<ref to undefined type>", metadata_style.style (),
1200 stream);
34b27950
JB
1201 return;
1202 }
8004dfd1 1203
34b27950
JB
1204 deref_val = coerce_ref_if_computed (original_value);
1205 if (deref_val)
1206 {
1207 if (ada_is_tagged_type (value_type (deref_val), 1))
1208 deref_val = ada_tag_value_at_base_address (deref_val);
8004dfd1 1209
34b27950 1210 common_val_print (deref_val, stream, recurse + 1, options,
2228ef77 1211 language_def (language_ada));
34b27950
JB
1212 return;
1213 }
8004dfd1 1214
34b27950
JB
1215 deref_val_int = unpack_pointer (type, valaddr + offset_aligned);
1216 if (deref_val_int == 0)
1217 {
1218 fputs_filtered ("(null)", stream);
1219 return;
8004dfd1 1220 }
34b27950
JB
1221
1222 deref_val
1223 = ada_value_ind (value_from_pointer (lookup_pointer_type (elttype),
1224 deref_val_int));
1225 if (ada_is_tagged_type (value_type (deref_val), 1))
1226 deref_val = ada_tag_value_at_base_address (deref_val);
1227
c1b5a1a6
JB
1228 /* Make sure that the object does not have an unreasonable size
1229 before trying to print it. This can happen for instance with
1230 references to dynamic objects whose contents is uninitialized
1231 (Eg: an array whose bounds are not set yet). */
1232 ada_ensure_varsize_limit (value_type (deref_val));
1233
7d45f3df
YQ
1234 if (value_lazy (deref_val))
1235 value_fetch_lazy (deref_val);
1236
2e088f8b
TT
1237 common_val_print (deref_val, stream, recurse + 1,
1238 options, language_def (language_ada));
8004dfd1
JB
1239}
1240
14f9c5c9 1241/* See the comment on ada_val_print. This function differs in that it
e936309c 1242 does not catch evaluation errors (leaving that to ada_val_print). */
14f9c5c9 1243
d3eab38a 1244static void
e8b24d9f 1245ada_val_print_1 (struct type *type,
490f124f 1246 int offset, CORE_ADDR address,
79a45b7d 1247 struct ui_file *stream, int recurse,
e8b24d9f 1248 struct value *original_value,
2228ef77 1249 const struct value_print_options *options)
14f9c5c9 1250{
490f124f 1251 int offset_aligned;
e8b24d9f 1252 const gdb_byte *valaddr = value_contents_for_printing (original_value);
14f9c5c9 1253
61ee279c 1254 type = ada_check_typedef (type);
14f9c5c9 1255
ad82864c 1256 if (ada_is_array_descriptor_type (type)
d2d43431
JB
1257 || (ada_is_constrained_packed_array_type (type)
1258 && TYPE_CODE (type) != TYPE_CODE_PTR))
14f9c5c9 1259 {
5b5e15ec
TT
1260 struct value *val = value_from_contents_and_address (type,
1261 valaddr + offset,
1262 address);
1263 ada_val_print_gnat_array (val, stream, recurse, options);
d3eab38a 1264 return;
14f9c5c9
AS
1265 }
1266
490f124f
PA
1267 offset_aligned = offset + ada_aligned_value_addr (type, valaddr) - valaddr;
1268 type = printable_val_type (type, valaddr + offset_aligned);
62c67f3c
JB
1269 type = resolve_dynamic_type (type, valaddr + offset_aligned,
1270 address + offset_aligned);
14f9c5c9
AS
1271
1272 switch (TYPE_CODE (type))
1273 {
1274 default:
e8b24d9f 1275 val_print (type, offset, address, stream, recurse,
cd1630f9 1276 original_value, options, language_def (language_c));
d3eab38a 1277 break;
14f9c5c9 1278
4c4b4cd2 1279 case TYPE_CODE_PTR:
8004dfd1
JB
1280 ada_val_print_ptr (type, valaddr, offset, offset_aligned,
1281 address, stream, recurse, original_value,
2228ef77 1282 options);
8004dfd1 1283 break;
4c4b4cd2 1284
14f9c5c9
AS
1285 case TYPE_CODE_INT:
1286 case TYPE_CODE_RANGE:
8004dfd1
JB
1287 ada_val_print_num (type, valaddr, offset, offset_aligned,
1288 address, stream, recurse, original_value,
2228ef77 1289 options);
8004dfd1 1290 break;
14f9c5c9
AS
1291
1292 case TYPE_CODE_ENUM:
8004dfd1
JB
1293 ada_val_print_enum (type, valaddr, offset, offset_aligned,
1294 address, stream, recurse, original_value,
2228ef77 1295 options);
8004dfd1 1296 break;
d2e4a39e 1297
4c4b4cd2 1298 case TYPE_CODE_FLT:
8004dfd1
JB
1299 ada_val_print_flt (type, valaddr, offset, offset_aligned,
1300 address, stream, recurse, original_value,
2228ef77 1301 options);
4c4b4cd2
PH
1302 break;
1303
14f9c5c9
AS
1304 case TYPE_CODE_UNION:
1305 case TYPE_CODE_STRUCT:
8004dfd1
JB
1306 ada_val_print_struct_union (type, valaddr, offset, offset_aligned,
1307 address, stream, recurse,
2228ef77 1308 original_value, options);
8004dfd1 1309 break;
14f9c5c9
AS
1310
1311 case TYPE_CODE_ARRAY:
4eb27a30 1312 ada_val_print_array (type, valaddr, offset, offset_aligned,
d3eab38a
TT
1313 address, stream, recurse, original_value,
1314 options);
1315 return;
14f9c5c9
AS
1316
1317 case TYPE_CODE_REF:
8004dfd1
JB
1318 ada_val_print_ref (type, valaddr, offset, offset_aligned,
1319 address, stream, recurse, original_value,
2228ef77 1320 options);
14f9c5c9
AS
1321 break;
1322 }
14f9c5c9
AS
1323}
1324
bdf779a0
JB
1325/* See val_print for a description of the various parameters of this
1326 function; they are identical. */
1327
1328void
e8b24d9f 1329ada_val_print (struct type *type,
bdf779a0
JB
1330 int embedded_offset, CORE_ADDR address,
1331 struct ui_file *stream, int recurse,
e8b24d9f 1332 struct value *val,
bdf779a0 1333 const struct value_print_options *options)
14f9c5c9 1334{
a70b8144 1335 try
bdf779a0 1336 {
e8b24d9f 1337 ada_val_print_1 (type, embedded_offset, address,
2228ef77 1338 stream, recurse, val, options);
bdf779a0 1339 }
230d2906 1340 catch (const gdb_exception_error &except)
492d29ea 1341 {
7f6aba03
TT
1342 fprintf_styled (stream, metadata_style.style (),
1343 _("<error reading variable: %s>"),
1344 except.what ());
492d29ea 1345 }
14f9c5c9
AS
1346}
1347
5b5e15ec
TT
1348/* See the comment on ada_value_print. This function differs in that
1349 it does not catch evaluation errors (leaving that to
1350 ada_value_print). */
1351
1352static void
1353ada_value_print_1 (struct value *val, struct ui_file *stream, int recurse,
1354 const struct value_print_options *options)
1355{
1356 struct type *type = ada_check_typedef (value_type (val));
1357
1358 if (ada_is_array_descriptor_type (type)
1359 || (ada_is_constrained_packed_array_type (type)
1360 && TYPE_CODE (type) != TYPE_CODE_PTR))
1361 {
1362 ada_val_print_gnat_array (val, stream, recurse, options);
1363 return;
1364 }
1365
1366 val = ada_to_fixed_value (val);
1367 type = value_type (val);
1368 struct type *saved_type = type;
1369
1370 const gdb_byte *valaddr = value_contents_for_printing (val);
1371 CORE_ADDR address = value_address (val);
1372 type = ada_check_typedef (resolve_dynamic_type (type, valaddr, address));
1373 if (type != saved_type)
1374 {
1375 val = value_copy (val);
1376 deprecated_set_value_type (val, type);
1377 }
1378
1379 switch (TYPE_CODE (type))
1380 {
1381 default:
1382 common_val_print (val, stream, recurse, options,
1383 language_def (language_c));
1384 break;
1385
1386 case TYPE_CODE_PTR:
416595d6 1387 ada_value_print_ptr (val, stream, recurse, options);
5b5e15ec
TT
1388 break;
1389
1390 case TYPE_CODE_INT:
1391 case TYPE_CODE_RANGE:
39ef85a8 1392 ada_value_print_num (val, stream, recurse, options);
5b5e15ec
TT
1393 break;
1394
1395 case TYPE_CODE_ENUM:
1396 ada_val_print_enum (type, valaddr, 0, 0,
1397 address, stream, recurse, val,
1398 options);
1399 break;
1400
1401 case TYPE_CODE_FLT:
b9fa6e07
TT
1402 if (options->format)
1403 {
1404 common_val_print (val, stream, recurse, options,
1405 language_def (language_c));
1406 break;
1407 }
1408
1409 ada_print_floating (valaddr, type, stream);
5b5e15ec
TT
1410 break;
1411
1412 case TYPE_CODE_UNION:
1413 case TYPE_CODE_STRUCT:
1414 ada_val_print_struct_union (type, valaddr, 0, 0,
1415 address, stream, recurse,
1416 val, options);
1417 break;
1418
1419 case TYPE_CODE_ARRAY:
b59eac37 1420 ada_value_print_array (val, stream, recurse, options);
5b5e15ec
TT
1421 return;
1422
1423 case TYPE_CODE_REF:
1424 ada_val_print_ref (type, valaddr, 0, 0,
1425 address, stream, recurse, val,
1426 options);
1427 break;
1428 }
1429}
1430
26792ee0
TT
1431/* See ada-lang.h. */
1432
1433void
1434ada_value_print_inner (struct value *val, struct ui_file *stream,
1435 int recurse,
1436 const struct value_print_options *options)
1437{
5b5e15ec
TT
1438 try
1439 {
1440 ada_value_print_1 (val, stream, recurse, options);
1441 }
1442 catch (const gdb_exception_error &except)
1443 {
1444 fprintf_styled (stream, metadata_style.style (),
1445 _("<error reading variable: %s>"),
1446 except.what ());
1447 }
26792ee0
TT
1448}
1449
8e069a98 1450void
79a45b7d
TT
1451ada_value_print (struct value *val0, struct ui_file *stream,
1452 const struct value_print_options *options)
14f9c5c9 1453{
0c3acc09 1454 struct value *val = ada_to_fixed_value (val0);
cc330e39 1455 struct type *type = ada_check_typedef (value_type (val));
79a45b7d 1456 struct value_print_options opts;
14f9c5c9 1457
4c4b4cd2
PH
1458 /* If it is a pointer, indicate what it points to. */
1459 if (TYPE_CODE (type) == TYPE_CODE_PTR)
14f9c5c9 1460 {
4c4b4cd2
PH
1461 /* Hack: don't print (char *) for char strings. Their
1462 type is indicated by the quoted string anyway. */
1463 if (TYPE_LENGTH (TYPE_TARGET_TYPE (type)) != sizeof (char)
1464 || TYPE_CODE (TYPE_TARGET_TYPE (type)) != TYPE_CODE_INT
1465 || TYPE_UNSIGNED (TYPE_TARGET_TYPE (type)))
14f9c5c9
AS
1466 {
1467 fprintf_filtered (stream, "(");
1468 type_print (type, "", stream, -1);
1469 fprintf_filtered (stream, ") ");
1470 }
1471 }
4c4b4cd2 1472 else if (ada_is_array_descriptor_type (type))
14f9c5c9 1473 {
720d1a40
JB
1474 /* We do not print the type description unless TYPE is an array
1475 access type (this is encoded by the compiler as a typedef to
1476 a fat pointer - hence the check against TYPE_CODE_TYPEDEF). */
1477 if (TYPE_CODE (type) == TYPE_CODE_TYPEDEF)
1478 {
1479 fprintf_filtered (stream, "(");
1480 type_print (type, "", stream, -1);
1481 fprintf_filtered (stream, ") ");
1482 }
14f9c5c9
AS
1483 }
1484 else if (ada_is_bogus_array_descriptor (type))
1485 {
1486 fprintf_filtered (stream, "(");
1487 type_print (type, "", stream, -1);
1488 fprintf_filtered (stream, ") (...?)");
8e069a98 1489 return;
14f9c5c9 1490 }
4c4b4cd2 1491
79a45b7d
TT
1492 opts = *options;
1493 opts.deref_ref = 1;
03371129 1494 common_val_print (val, stream, 0, &opts, current_language);
14f9c5c9 1495}
This page took 1.294521 seconds and 4 git commands to generate.