daily update
[deliverable/binutils-gdb.git] / gdb / ada-valprint.c
CommitLineData
4c4b4cd2 1/* Support for printing Ada values for GDB, the GNU debugger.
d56612af 2
6aba47ca 3 Copyright (C) 1986, 1988, 1989, 1991, 1992, 1993, 1994, 1997, 2001, 2002,
4c38e0a4
JB
4 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010
5 Free Software Foundation, Inc.
14f9c5c9 6
a9762ec7 7 This file is part of GDB.
14f9c5c9 8
a9762ec7
JB
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3 of the License, or
12 (at your option) any later version.
14f9c5c9 13
a9762ec7
JB
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
14f9c5c9 18
a9762ec7
JB
19 You should have received a copy of the GNU General Public License
20 along with this program. If not, see <http://www.gnu.org/licenses/>. */
14f9c5c9 21
14f9c5c9 22#include "defs.h"
12c89474 23#include <ctype.h>
4c4b4cd2 24#include "gdb_string.h"
14f9c5c9
AS
25#include "symtab.h"
26#include "gdbtypes.h"
27#include "expression.h"
28#include "value.h"
29#include "demangle.h"
30#include "valprint.h"
31#include "language.h"
32#include "annotate.h"
33#include "ada-lang.h"
34#include "c-lang.h"
04714b91 35#include "infcall.h"
60250e8b 36#include "exceptions.h"
8ca1c40e 37#include "objfiles.h"
14f9c5c9 38
fc1a4b47 39static void print_record (struct type *, const gdb_byte *, struct ui_file *,
0e03807e
TT
40 int,
41 const struct value *,
42 const struct value_print_options *);
14f9c5c9 43
fc1a4b47 44static int print_field_values (struct type *, const gdb_byte *,
79a45b7d 45 struct ui_file *, int,
0e03807e 46 const struct value *,
79a45b7d
TT
47 const struct value_print_options *,
48 int, struct type *,
fc1a4b47 49 const gdb_byte *);
14f9c5c9 50
d2e4a39e 51static void adjust_type_signedness (struct type *);
14f9c5c9 52
fc1a4b47 53static int ada_val_print_1 (struct type *, const gdb_byte *, int, CORE_ADDR,
79a45b7d 54 struct ui_file *, int,
0e03807e 55 const struct value *,
79a45b7d 56 const struct value_print_options *);
14f9c5c9
AS
57\f
58
4c4b4cd2 59/* Make TYPE unsigned if its range of values includes no negatives. */
d2e4a39e 60static void
4dc81987 61adjust_type_signedness (struct type *type)
14f9c5c9 62{
d2e4a39e 63 if (type != NULL && TYPE_CODE (type) == TYPE_CODE_RANGE
14f9c5c9 64 && TYPE_LOW_BOUND (type) >= 0)
876cecd0 65 TYPE_UNSIGNED (type) = 1;
d2e4a39e 66}
14f9c5c9 67
e936309c
JB
68/* Assuming TYPE is a simple array type, prints its lower bound on STREAM,
69 if non-standard (i.e., other than 1 for numbers, other than lower bound
70 of index type for enumerated type). Returns 1 if something printed,
71 otherwise 0. */
14f9c5c9 72
d2e4a39e 73static int
79a45b7d
TT
74print_optional_low_bound (struct ui_file *stream, struct type *type,
75 const struct value_print_options *options)
14f9c5c9
AS
76{
77 struct type *index_type;
df178451
PM
78 LONGEST low_bound;
79 LONGEST high_bound;
14f9c5c9 80
79a45b7d 81 if (options->print_array_indexes)
14f9c5c9 82 return 0;
e79af960 83
e936309c
JB
84 if (!get_array_bounds (type, &low_bound, &high_bound))
85 return 0;
86
87 /* If this is an empty array, then don't print the lower bound.
88 That would be confusing, because we would print the lower bound,
89 followed by... nothing! */
90 if (low_bound > high_bound)
14f9c5c9 91 return 0;
d2e4a39e 92
e79af960
JB
93 index_type = TYPE_INDEX_TYPE (type);
94
fd1b946e
JB
95 if (TYPE_CODE (index_type) == TYPE_CODE_RANGE)
96 {
97 /* We need to know what the base type is, in order to do the
98 appropriate check below. Otherwise, if this is a subrange
99 of an enumerated type, where the underlying value of the
100 first element is typically 0, we might test the low bound
101 against the wrong value. */
102 index_type = TYPE_TARGET_TYPE (index_type);
103 }
104
d2e4a39e
AS
105 switch (TYPE_CODE (index_type))
106 {
690cc4eb
PH
107 case TYPE_CODE_BOOL:
108 if (low_bound == 0)
109 return 0;
110 break;
d2e4a39e
AS
111 case TYPE_CODE_ENUM:
112 if (low_bound == TYPE_FIELD_BITPOS (index_type, 0))
113 return 0;
114 break;
115 case TYPE_CODE_UNDEF:
7c964f07 116 index_type = NULL;
d2e4a39e
AS
117 /* FALL THROUGH */
118 default:
119 if (low_bound == 1)
120 return 0;
121 break;
122 }
14f9c5c9 123
df178451 124 ada_print_scalar (index_type, low_bound, stream);
14f9c5c9
AS
125 fprintf_filtered (stream, " => ");
126 return 1;
127}
128
129/* Version of val_print_array_elements for GNAT-style packed arrays.
130 Prints elements of packed array of type TYPE at bit offset
79a45b7d 131 BITOFFSET from VALADDR on STREAM. Formats according to OPTIONS and
4c4b4cd2 132 separates with commas. RECURSE is the recursion (nesting) level.
79a45b7d 133 TYPE must have been decoded (as by ada_coerce_to_simple_array). */
14f9c5c9
AS
134
135static void
fc1a4b47 136val_print_packed_array_elements (struct type *type, const gdb_byte *valaddr,
ebf56fd3 137 int bitoffset, struct ui_file *stream,
79a45b7d 138 int recurse,
0e03807e 139 const struct value *val,
79a45b7d 140 const struct value_print_options *options)
14f9c5c9
AS
141{
142 unsigned int i;
143 unsigned int things_printed = 0;
144 unsigned len;
e79af960 145 struct type *elttype, *index_type;
14f9c5c9 146 unsigned eltlen;
14f9c5c9 147 unsigned long bitsize = TYPE_FIELD_BITSIZE (type, 0);
d2e4a39e 148 struct value *mark = value_mark ();
e79af960 149 LONGEST low = 0;
d2e4a39e 150
14f9c5c9
AS
151 elttype = TYPE_TARGET_TYPE (type);
152 eltlen = TYPE_LENGTH (check_typedef (elttype));
e79af960 153 index_type = TYPE_INDEX_TYPE (type);
14f9c5c9
AS
154
155 {
e79af960 156 LONGEST high;
5b4ee69b 157
262452ec 158 if (get_discrete_bounds (index_type, &low, &high) < 0)
14f9c5c9
AS
159 len = 1;
160 else
161 len = high - low + 1;
162 }
163
164 i = 0;
165 annotate_array_section_begin (i, elttype);
166
79a45b7d 167 while (i < len && things_printed < options->print_max)
14f9c5c9
AS
168 {
169 struct value *v0, *v1;
170 int i0;
171
172 if (i != 0)
173 {
79a45b7d 174 if (options->prettyprint_arrays)
14f9c5c9
AS
175 {
176 fprintf_filtered (stream, ",\n");
177 print_spaces_filtered (2 + 2 * recurse, stream);
178 }
179 else
180 {
181 fprintf_filtered (stream, ", ");
182 }
183 }
184 wrap_here (n_spaces (2 + 2 * recurse));
79a45b7d 185 maybe_print_array_index (index_type, i + low, stream, options);
14f9c5c9
AS
186
187 i0 = i;
d2e4a39e 188 v0 = ada_value_primitive_packed_val (NULL, valaddr,
14f9c5c9
AS
189 (i0 * bitsize) / HOST_CHAR_BIT,
190 (i0 * bitsize) % HOST_CHAR_BIT,
191 bitsize, elttype);
192 while (1)
193 {
194 i += 1;
195 if (i >= len)
196 break;
d2e4a39e 197 v1 = ada_value_primitive_packed_val (NULL, valaddr,
14f9c5c9
AS
198 (i * bitsize) / HOST_CHAR_BIT,
199 (i * bitsize) % HOST_CHAR_BIT,
200 bitsize, elttype);
0fd88904 201 if (memcmp (value_contents (v0), value_contents (v1), eltlen) != 0)
14f9c5c9
AS
202 break;
203 }
204
79a45b7d 205 if (i - i0 > options->repeat_count_threshold)
14f9c5c9 206 {
79a45b7d 207 struct value_print_options opts = *options;
5b4ee69b 208
79a45b7d
TT
209 opts.deref_ref = 0;
210 val_print (elttype, value_contents (v0), 0, 0, stream,
0e03807e 211 recurse + 1, val, &opts, current_language);
14f9c5c9 212 annotate_elt_rep (i - i0);
edefbb7c 213 fprintf_filtered (stream, _(" <repeats %u times>"), i - i0);
14f9c5c9
AS
214 annotate_elt_rep_end ();
215
216 }
217 else
218 {
219 int j;
79a45b7d 220 struct value_print_options opts = *options;
5b4ee69b 221
79a45b7d 222 opts.deref_ref = 0;
14f9c5c9
AS
223 for (j = i0; j < i; j += 1)
224 {
d2e4a39e 225 if (j > i0)
14f9c5c9 226 {
79a45b7d 227 if (options->prettyprint_arrays)
14f9c5c9
AS
228 {
229 fprintf_filtered (stream, ",\n");
230 print_spaces_filtered (2 + 2 * recurse, stream);
231 }
232 else
233 {
234 fprintf_filtered (stream, ", ");
235 }
236 wrap_here (n_spaces (2 + 2 * recurse));
e79af960 237 maybe_print_array_index (index_type, j + low,
79a45b7d 238 stream, options);
14f9c5c9 239 }
79a45b7d 240 val_print (elttype, value_contents (v0), 0, 0, stream,
0e03807e 241 recurse + 1, val, &opts, current_language);
14f9c5c9
AS
242 annotate_elt ();
243 }
244 }
245 things_printed += i - i0;
246 }
247 annotate_array_section_end ();
248 if (i < len)
249 {
250 fprintf_filtered (stream, "...");
251 }
252
253 value_free_to_mark (mark);
254}
255
d2e4a39e 256static struct type *
fc1a4b47 257printable_val_type (struct type *type, const gdb_byte *valaddr)
14f9c5c9 258{
1ed6ede0 259 return ada_to_fixed_type (ada_aligned_type (type), valaddr, 0, NULL, 1);
14f9c5c9
AS
260}
261
262/* Print the character C on STREAM as part of the contents of a literal
263 string whose delimiter is QUOTER. TYPE_LEN is the length in bytes
4c4b4cd2 264 (1 or 2) of the character. */
14f9c5c9
AS
265
266void
6c7a06a3
TT
267ada_emit_char (int c, struct type *type, struct ui_file *stream,
268 int quoter, int type_len)
14f9c5c9
AS
269{
270 if (type_len != 2)
271 type_len = 1;
272
273 c &= (1 << (type_len * TARGET_CHAR_BIT)) - 1;
274
275 if (isascii (c) && isprint (c))
276 {
277 if (c == quoter && c == '"')
529cad9c 278 fprintf_filtered (stream, "\"\"");
14f9c5c9
AS
279 else
280 fprintf_filtered (stream, "%c", c);
281 }
282 else
d2e4a39e 283 fprintf_filtered (stream, "[\"%0*x\"]", type_len * 2, c);
14f9c5c9
AS
284}
285
286/* Character #I of STRING, given that TYPE_LEN is the size in bytes (1
4c4b4cd2 287 or 2) of a character. */
14f9c5c9
AS
288
289static int
e17a4113
UW
290char_at (const gdb_byte *string, int i, int type_len,
291 enum bfd_endian byte_order)
14f9c5c9
AS
292{
293 if (type_len == 1)
294 return string[i];
d2e4a39e 295 else
e17a4113 296 return (int) extract_unsigned_integer (string + 2 * i, 2, byte_order);
14f9c5c9
AS
297}
298
4c4b4cd2
PH
299/* Wrapper around memcpy to make it legal argument to ui_file_put */
300static void
301ui_memcpy (void *dest, const char *buffer, long len)
302{
303 memcpy (dest, buffer, (size_t) len);
304 ((char *) dest)[len] = '\0';
305}
306
307/* Print a floating-point value of type TYPE, pointed to in GDB by
308 VALADDR, on STREAM. Use Ada formatting conventions: there must be
309 a decimal point, and at least one digit before and after the
310 point. We use GNAT format for NaNs and infinities. */
311static void
fc1a4b47 312ada_print_floating (const gdb_byte *valaddr, struct type *type,
a2bd3dcd 313 struct ui_file *stream)
4c4b4cd2
PH
314{
315 char buffer[64];
316 char *s, *result;
317 int len;
318 struct ui_file *tmp_stream = mem_fileopen ();
319 struct cleanup *cleanups = make_cleanup_ui_file_delete (tmp_stream);
320
321 print_floating (valaddr, type, tmp_stream);
322 ui_file_put (tmp_stream, ui_memcpy, buffer);
323 do_cleanups (cleanups);
324
325 result = buffer;
326 len = strlen (result);
327
328 /* Modify for Ada rules. */
606b8d1a 329
c3e5cd34
PH
330 s = strstr (result, "inf");
331 if (s == NULL)
332 s = strstr (result, "Inf");
333 if (s == NULL)
334 s = strstr (result, "INF");
335 if (s != NULL)
4c4b4cd2 336 strcpy (s, "Inf");
c3e5cd34
PH
337
338 if (s == NULL)
4c4b4cd2 339 {
c3e5cd34
PH
340 s = strstr (result, "nan");
341 if (s == NULL)
342 s = strstr (result, "NaN");
343 if (s == NULL)
344 s = strstr (result, "Nan");
345 if (s != NULL)
346 {
347 s[0] = s[2] = 'N';
348 if (result[0] == '-')
349 result += 1;
350 }
4c4b4cd2 351 }
c3e5cd34
PH
352
353 if (s == NULL && strchr (result, '.') == NULL)
4c4b4cd2 354 {
c3e5cd34
PH
355 s = strchr (result, 'e');
356 if (s == NULL)
4c4b4cd2
PH
357 fprintf_filtered (stream, "%s.0", result);
358 else
359 fprintf_filtered (stream, "%.*s.0%s", (int) (s-result), result, s);
360 return;
361 }
362 fprintf_filtered (stream, "%s", result);
363}
364
14f9c5c9 365void
6c7a06a3 366ada_printchar (int c, struct type *type, struct ui_file *stream)
14f9c5c9
AS
367{
368 fputs_filtered ("'", stream);
6c7a06a3 369 ada_emit_char (c, type, stream, '\'', 1);
14f9c5c9
AS
370 fputs_filtered ("'", stream);
371}
372
373/* [From print_type_scalar in typeprint.c]. Print VAL on STREAM in a
7c964f07
UW
374 form appropriate for TYPE, if non-NULL. If TYPE is NULL, print VAL
375 like a default signed integer. */
14f9c5c9
AS
376
377void
ebf56fd3 378ada_print_scalar (struct type *type, LONGEST val, struct ui_file *stream)
14f9c5c9
AS
379{
380 unsigned int i;
381 unsigned len;
382
7c964f07
UW
383 if (!type)
384 {
385 print_longest (stream, 'd', 0, val);
386 return;
387 }
388
61ee279c 389 type = ada_check_typedef (type);
14f9c5c9
AS
390
391 switch (TYPE_CODE (type))
392 {
393
394 case TYPE_CODE_ENUM:
395 len = TYPE_NFIELDS (type);
396 for (i = 0; i < len; i++)
397 {
398 if (TYPE_FIELD_BITPOS (type, i) == val)
399 {
400 break;
401 }
402 }
403 if (i < len)
404 {
405 fputs_filtered (ada_enum_name (TYPE_FIELD_NAME (type, i)), stream);
406 }
407 else
408 {
409 print_longest (stream, 'd', 0, val);
410 }
411 break;
412
413 case TYPE_CODE_INT:
414 print_longest (stream, TYPE_UNSIGNED (type) ? 'u' : 'd', 0, val);
415 break;
416
417 case TYPE_CODE_CHAR:
6c7a06a3 418 LA_PRINT_CHAR ((unsigned char) val, type, stream);
14f9c5c9
AS
419 break;
420
421 case TYPE_CODE_BOOL:
422 fprintf_filtered (stream, val ? "true" : "false");
423 break;
424
425 case TYPE_CODE_RANGE:
426 ada_print_scalar (TYPE_TARGET_TYPE (type), val, stream);
427 return;
428
429 case TYPE_CODE_UNDEF:
430 case TYPE_CODE_PTR:
431 case TYPE_CODE_ARRAY:
432 case TYPE_CODE_STRUCT:
433 case TYPE_CODE_UNION:
434 case TYPE_CODE_FUNC:
435 case TYPE_CODE_FLT:
436 case TYPE_CODE_VOID:
437 case TYPE_CODE_SET:
438 case TYPE_CODE_STRING:
439 case TYPE_CODE_ERROR:
0d5de010
DJ
440 case TYPE_CODE_MEMBERPTR:
441 case TYPE_CODE_METHODPTR:
14f9c5c9
AS
442 case TYPE_CODE_METHOD:
443 case TYPE_CODE_REF:
edefbb7c 444 warning (_("internal error: unhandled type in ada_print_scalar"));
14f9c5c9
AS
445 break;
446
447 default:
edefbb7c 448 error (_("Invalid type code in symbol table."));
14f9c5c9
AS
449 }
450 gdb_flush (stream);
451}
452
453/* Print the character string STRING, printing at most LENGTH characters.
454 Printing stops early if the number hits print_max; repeat counts
455 are printed as appropriate. Print ellipses at the end if we
456 had to stop before printing LENGTH characters, or if
457 FORCE_ELLIPSES. TYPE_LEN is the length (1 or 2) of the character type.
458 */
459
460static void
6c7a06a3 461printstr (struct ui_file *stream, struct type *elttype, const gdb_byte *string,
79a45b7d
TT
462 unsigned int length, int force_ellipses, int type_len,
463 const struct value_print_options *options)
14f9c5c9 464{
e17a4113 465 enum bfd_endian byte_order = gdbarch_byte_order (get_type_arch (elttype));
14f9c5c9
AS
466 unsigned int i;
467 unsigned int things_printed = 0;
468 int in_quotes = 0;
469 int need_comma = 0;
470
471 if (length == 0)
472 {
473 fputs_filtered ("\"\"", stream);
474 return;
475 }
476
79a45b7d 477 for (i = 0; i < length && things_printed < options->print_max; i += 1)
14f9c5c9
AS
478 {
479 /* Position of the character we are examining
d2e4a39e 480 to see whether it is repeated. */
14f9c5c9
AS
481 unsigned int rep1;
482 /* Number of repetitions we have detected so far. */
483 unsigned int reps;
484
485 QUIT;
486
487 if (need_comma)
488 {
489 fputs_filtered (", ", stream);
490 need_comma = 0;
491 }
492
493 rep1 = i + 1;
494 reps = 1;
c3e5cd34 495 while (rep1 < length
e17a4113
UW
496 && char_at (string, rep1, type_len, byte_order)
497 == char_at (string, i, type_len, byte_order))
14f9c5c9
AS
498 {
499 rep1 += 1;
500 reps += 1;
501 }
502
79a45b7d 503 if (reps > options->repeat_count_threshold)
14f9c5c9
AS
504 {
505 if (in_quotes)
506 {
79a45b7d 507 if (options->inspect_it)
14f9c5c9
AS
508 fputs_filtered ("\\\", ", stream);
509 else
510 fputs_filtered ("\", ", stream);
511 in_quotes = 0;
512 }
513 fputs_filtered ("'", stream);
e17a4113
UW
514 ada_emit_char (char_at (string, i, type_len, byte_order),
515 elttype, stream, '\'', type_len);
14f9c5c9 516 fputs_filtered ("'", stream);
edefbb7c 517 fprintf_filtered (stream, _(" <repeats %u times>"), reps);
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 {
79a45b7d 526 if (options->inspect_it)
14f9c5c9
AS
527 fputs_filtered ("\\\"", stream);
528 else
529 fputs_filtered ("\"", stream);
530 in_quotes = 1;
531 }
e17a4113
UW
532 ada_emit_char (char_at (string, i, type_len, byte_order),
533 elttype, stream, '"', type_len);
14f9c5c9
AS
534 things_printed += 1;
535 }
536 }
537
538 /* Terminate the quotes if necessary. */
539 if (in_quotes)
540 {
79a45b7d 541 if (options->inspect_it)
14f9c5c9
AS
542 fputs_filtered ("\\\"", stream);
543 else
544 fputs_filtered ("\"", stream);
545 }
546
547 if (force_ellipses || i < length)
548 fputs_filtered ("...", stream);
549}
550
551void
6c7a06a3 552ada_printstr (struct ui_file *stream, struct type *type, const gdb_byte *string,
be759fcf 553 unsigned int length, const char *encoding, int force_ellipses,
79a45b7d 554 const struct value_print_options *options)
14f9c5c9 555{
6c7a06a3
TT
556 printstr (stream, type, string, length, force_ellipses, TYPE_LENGTH (type),
557 options);
14f9c5c9
AS
558}
559
560
561/* Print data of type TYPE located at VALADDR (within GDB), which came from
562 the inferior at address ADDRESS, onto stdio stream STREAM according to
79a45b7d 563 OPTIONS. The data at VALADDR is in target byte order.
14f9c5c9
AS
564
565 If the data is printed as a string, returns the number of string characters
566 printed.
567
14f9c5c9 568 RECURSE indicates the amount of indentation to supply before
79a45b7d 569 continuation lines; this amount is roughly twice the value of RECURSE. */
14f9c5c9
AS
570
571int
fc1a4b47 572ada_val_print (struct type *type, const gdb_byte *valaddr0,
a2bd3dcd 573 int embedded_offset, CORE_ADDR address,
79a45b7d 574 struct ui_file *stream, int recurse,
0e03807e 575 const struct value *val,
79a45b7d 576 const struct value_print_options *options)
14f9c5c9 577{
0e03807e
TT
578 volatile struct gdb_exception except;
579 int result = 0;
14f9c5c9 580
0e03807e
TT
581 TRY_CATCH (except, RETURN_MASK_ALL)
582 {
583 result = ada_val_print_1 (type, valaddr0, embedded_offset, address,
584 stream, recurse, val, options);
585 }
5b4ee69b 586
0e03807e
TT
587 if (except.reason < 0)
588 result = 0;
589
590 return result;
14f9c5c9
AS
591}
592
e936309c
JB
593/* Assuming TYPE is a simple array, print the value of this array located
594 at VALADDR. See ada_val_print for a description of the various
595 parameters of this function; they are identical. The semantics
596 of the return value is also identical to ada_val_print. */
597
598static int
599ada_val_print_array (struct type *type, const gdb_byte *valaddr,
79a45b7d 600 CORE_ADDR address, struct ui_file *stream, int recurse,
0e03807e 601 const struct value *val,
79a45b7d 602 const struct value_print_options *options)
e936309c 603{
e17a4113 604 enum bfd_endian byte_order = gdbarch_byte_order (get_type_arch (type));
e936309c
JB
605 struct type *elttype = TYPE_TARGET_TYPE (type);
606 unsigned int eltlen;
607 unsigned int len;
608 int result = 0;
609
610 if (elttype == NULL)
611 eltlen = 0;
612 else
613 eltlen = TYPE_LENGTH (elttype);
614 if (eltlen == 0)
615 len = 0;
616 else
617 len = TYPE_LENGTH (type) / eltlen;
618
619 /* For an array of chars, print with string syntax. */
79a45b7d
TT
620 if (ada_is_string_type (type)
621 && (options->format == 0 || options->format == 's'))
e936309c 622 {
79a45b7d 623 if (options->prettyprint_arrays)
e936309c
JB
624 print_spaces_filtered (2 + 2 * recurse, stream);
625
626 /* If requested, look for the first null char and only print
627 elements up to it. */
79a45b7d 628 if (options->stop_print_at_null)
e936309c
JB
629 {
630 int temp_len;
631
632 /* Look for a NULL char. */
633 for (temp_len = 0;
634 (temp_len < len
79a45b7d 635 && temp_len < options->print_max
e17a4113 636 && char_at (valaddr, temp_len, eltlen, byte_order) != 0);
e936309c
JB
637 temp_len += 1);
638 len = temp_len;
639 }
640
6c7a06a3 641 printstr (stream, elttype, valaddr, len, 0, eltlen, options);
e936309c
JB
642 result = len;
643 }
644 else
645 {
646 fprintf_filtered (stream, "(");
79a45b7d 647 print_optional_low_bound (stream, type, options);
e936309c
JB
648 if (TYPE_FIELD_BITSIZE (type, 0) > 0)
649 val_print_packed_array_elements (type, valaddr, 0, stream,
0e03807e 650 recurse, val, options);
e936309c
JB
651 else
652 val_print_array_elements (type, valaddr, address, stream,
0e03807e 653 recurse, val, options, 0);
e936309c
JB
654 fprintf_filtered (stream, ")");
655 }
656
657 return result;
658}
659
14f9c5c9 660/* See the comment on ada_val_print. This function differs in that it
e936309c 661 does not catch evaluation errors (leaving that to ada_val_print). */
14f9c5c9
AS
662
663static int
fc1a4b47 664ada_val_print_1 (struct type *type, const gdb_byte *valaddr0,
a2bd3dcd 665 int embedded_offset, CORE_ADDR address,
79a45b7d 666 struct ui_file *stream, int recurse,
0e03807e 667 const struct value *original_value,
79a45b7d 668 const struct value_print_options *options)
14f9c5c9
AS
669{
670 unsigned int len;
671 int i;
672 struct type *elttype;
14f9c5c9 673 LONGEST val;
fc1a4b47 674 const gdb_byte *valaddr = valaddr0 + embedded_offset;
14f9c5c9 675
61ee279c 676 type = ada_check_typedef (type);
14f9c5c9 677
ad82864c
JB
678 if (ada_is_array_descriptor_type (type)
679 || ada_is_constrained_packed_array_type (type))
14f9c5c9
AS
680 {
681 int retn;
d2e4a39e
AS
682 struct value *mark = value_mark ();
683 struct value *val;
5b4ee69b 684
14f9c5c9
AS
685 val = value_from_contents_and_address (type, valaddr, address);
686 val = ada_coerce_to_simple_array_ptr (val);
687 if (val == NULL)
688 {
689 fprintf_filtered (stream, "(null)");
690 retn = 0;
691 }
692 else
0fd88904 693 retn = ada_val_print_1 (value_type (val), value_contents (val), 0,
0e03807e
TT
694 value_address (val), stream, recurse,
695 NULL, options);
14f9c5c9
AS
696 value_free_to_mark (mark);
697 return retn;
698 }
699
700 valaddr = ada_aligned_value_addr (type, valaddr);
701 embedded_offset -= valaddr - valaddr0 - embedded_offset;
702 type = printable_val_type (type, valaddr);
703
704 switch (TYPE_CODE (type))
705 {
706 default:
d2e4a39e 707 return c_val_print (type, valaddr0, embedded_offset, address, stream,
0e03807e 708 recurse, original_value, options);
14f9c5c9 709
4c4b4cd2
PH
710 case TYPE_CODE_PTR:
711 {
712 int ret = c_val_print (type, valaddr0, embedded_offset, address,
0e03807e 713 stream, recurse, original_value, options);
5b4ee69b 714
4c4b4cd2
PH
715 if (ada_is_tag_type (type))
716 {
717 struct value *val =
718 value_from_contents_and_address (type, valaddr, address);
719 const char *name = ada_tag_name (val);
5b4ee69b 720
4c4b4cd2
PH
721 if (name != NULL)
722 fprintf_filtered (stream, " (%s)", name);
723 return 0;
724 }
725 return ret;
726 }
727
14f9c5c9
AS
728 case TYPE_CODE_INT:
729 case TYPE_CODE_RANGE:
730 if (ada_is_fixed_point_type (type))
731 {
732 LONGEST v = unpack_long (type, valaddr);
733 int len = TYPE_LENGTH (type);
734
735 fprintf_filtered (stream, len < 4 ? "%.11g" : "%.17g",
736 (double) ada_fixed_to_float (type, v));
737 return 0;
738 }
14f9c5c9
AS
739 else if (TYPE_CODE (type) == TYPE_CODE_RANGE)
740 {
d2e4a39e 741 struct type *target_type = TYPE_TARGET_TYPE (type);
5b4ee69b 742
14f9c5c9
AS
743 if (TYPE_LENGTH (type) != TYPE_LENGTH (target_type))
744 {
745 /* Obscure case of range type that has different length from
d2e4a39e
AS
746 its base type. Perform a conversion, or we will get a
747 nonsense value. Actually, we could use the same
4c4b4cd2 748 code regardless of lengths; I'm just avoiding a cast. */
d2e4a39e
AS
749 struct value *v = value_cast (target_type,
750 value_from_contents_and_address
751 (type, valaddr, 0));
5b4ee69b 752
0fd88904 753 return ada_val_print_1 (target_type, value_contents (v), 0, 0,
0e03807e 754 stream, recurse + 1, NULL, options);
14f9c5c9
AS
755 }
756 else
d2e4a39e 757 return ada_val_print_1 (TYPE_TARGET_TYPE (type),
14f9c5c9 758 valaddr0, embedded_offset,
0e03807e 759 address, stream, recurse, original_value, options);
14f9c5c9 760 }
d2e4a39e 761 else
14f9c5c9 762 {
79a45b7d
TT
763 int format = (options->format ? options->format
764 : options->output_format);
5b4ee69b 765
14f9c5c9
AS
766 if (format)
767 {
79a45b7d 768 struct value_print_options opts = *options;
5b4ee69b 769
79a45b7d
TT
770 opts.format = format;
771 print_scalar_formatted (valaddr, type, &opts, 0, stream);
14f9c5c9 772 }
50810684 773 else if (ada_is_system_address_type (type))
4c4b4cd2
PH
774 {
775 /* FIXME: We want to print System.Address variables using
776 the same format as for any access type. But for some
777 reason GNAT encodes the System.Address type as an int,
778 so we have to work-around this deficiency by handling
50810684 779 System.Address values as a special case. */
8ca1c40e 780
50810684 781 struct gdbarch *gdbarch = get_type_arch (type);
8ca1c40e 782 struct type *ptr_type = builtin_type (gdbarch)->builtin_data_ptr;
5af949e3 783 CORE_ADDR addr = extract_typed_address (valaddr, ptr_type);
8ca1c40e 784
4c4b4cd2
PH
785 fprintf_filtered (stream, "(");
786 type_print (type, "", stream, -1);
787 fprintf_filtered (stream, ") ");
5af949e3 788 fputs_filtered (paddress (gdbarch, addr), stream);
4c4b4cd2 789 }
14f9c5c9
AS
790 else
791 {
792 val_print_type_code_int (type, valaddr, stream);
793 if (ada_is_character_type (type))
794 {
795 fputs_filtered (" ", stream);
796 ada_printchar ((unsigned char) unpack_long (type, valaddr),
6c7a06a3 797 type, stream);
14f9c5c9
AS
798 }
799 }
800 return 0;
801 }
802
803 case TYPE_CODE_ENUM:
79a45b7d 804 if (options->format)
14f9c5c9 805 {
79a45b7d 806 print_scalar_formatted (valaddr, type, options, 0, stream);
14f9c5c9
AS
807 break;
808 }
809 len = TYPE_NFIELDS (type);
810 val = unpack_long (type, valaddr);
811 for (i = 0; i < len; i++)
812 {
813 QUIT;
814 if (val == TYPE_FIELD_BITPOS (type, i))
815 {
816 break;
817 }
818 }
819 if (i < len)
820 {
d2e4a39e 821 const char *name = ada_enum_name (TYPE_FIELD_NAME (type, i));
5b4ee69b 822
d2e4a39e 823 if (name[0] == '\'')
14f9c5c9
AS
824 fprintf_filtered (stream, "%ld %s", (long) val, name);
825 else
826 fputs_filtered (name, stream);
827 }
828 else
829 {
830 print_longest (stream, 'd', 0, val);
831 }
832 break;
d2e4a39e 833
4f2aea11 834 case TYPE_CODE_FLAGS:
79a45b7d
TT
835 if (options->format)
836 print_scalar_formatted (valaddr, type, options, 0, stream);
4f2aea11
MK
837 else
838 val_print_type_code_flags (type, valaddr, stream);
839 break;
840
4c4b4cd2 841 case TYPE_CODE_FLT:
79a45b7d 842 if (options->format)
4c4b4cd2 843 return c_val_print (type, valaddr0, embedded_offset, address, stream,
0e03807e 844 recurse, original_value, options);
4c4b4cd2
PH
845 else
846 ada_print_floating (valaddr0 + embedded_offset, type, stream);
847 break;
848
14f9c5c9
AS
849 case TYPE_CODE_UNION:
850 case TYPE_CODE_STRUCT:
851 if (ada_is_bogus_array_descriptor (type))
852 {
853 fprintf_filtered (stream, "(...?)");
854 return 0;
d2e4a39e 855 }
14f9c5c9
AS
856 else
857 {
0e03807e
TT
858 print_record (type, valaddr, stream, recurse, original_value,
859 options);
14f9c5c9
AS
860 return 0;
861 }
862
863 case TYPE_CODE_ARRAY:
79a45b7d 864 return ada_val_print_array (type, valaddr, address, stream,
0e03807e 865 recurse, original_value, options);
14f9c5c9
AS
866
867 case TYPE_CODE_REF:
969a1360
JB
868 /* For references, the debugger is expected to print the value as
869 an address if DEREF_REF is null. But printing an address in place
870 of the object value would be confusing to an Ada programmer.
871 So, for Ada values, we print the actual dereferenced value
872 regardless. */
14f9c5c9 873 elttype = check_typedef (TYPE_TARGET_TYPE (type));
969a1360
JB
874
875 if (TYPE_CODE (elttype) != TYPE_CODE_UNDEF)
876 {
d8631d21 877 LONGEST deref_val_int = (LONGEST) unpack_pointer (type, valaddr);
5b4ee69b 878
969a1360
JB
879 if (deref_val_int != 0)
880 {
881 struct value *deref_val =
882 ada_value_ind (value_from_longest
883 (lookup_pointer_type (elttype),
884 deref_val_int));
5b4ee69b 885
969a1360
JB
886 val_print (value_type (deref_val),
887 value_contents (deref_val), 0,
42ae5230 888 value_address (deref_val), stream, recurse + 1,
0e03807e 889 original_value, options, current_language);
969a1360
JB
890 }
891 else
892 fputs_filtered ("(null)", stream);
893 }
894 else
895 fputs_filtered ("???", stream);
896
14f9c5c9
AS
897 break;
898 }
4c4b4cd2 899 gdb_flush (stream);
14f9c5c9
AS
900 return 0;
901}
902
903static int
fc1a4b47 904print_variant_part (struct type *type, int field_num, const gdb_byte *valaddr,
79a45b7d 905 struct ui_file *stream, int recurse,
0e03807e 906 const struct value *val,
79a45b7d 907 const struct value_print_options *options, int comma_needed,
fc1a4b47 908 struct type *outer_type, const gdb_byte *outer_valaddr)
14f9c5c9
AS
909{
910 struct type *var_type = TYPE_FIELD_TYPE (type, field_num);
d2e4a39e 911 int which = ada_which_variant_applies (var_type, outer_type, outer_valaddr);
14f9c5c9
AS
912
913 if (which < 0)
914 return 0;
915 else
d2e4a39e 916 return print_field_values
14f9c5c9
AS
917 (TYPE_FIELD_TYPE (var_type, which),
918 valaddr + TYPE_FIELD_BITPOS (type, field_num) / HOST_CHAR_BIT
919 + TYPE_FIELD_BITPOS (var_type, which) / HOST_CHAR_BIT,
0e03807e 920 stream, recurse, val, options,
14f9c5c9
AS
921 comma_needed, outer_type, outer_valaddr);
922}
923
924int
79a45b7d
TT
925ada_value_print (struct value *val0, struct ui_file *stream,
926 const struct value_print_options *options)
14f9c5c9 927{
0c3acc09
JB
928 struct value *val = ada_to_fixed_value (val0);
929 CORE_ADDR address = value_address (val);
930 struct type *type = value_type (val);
79a45b7d 931 struct value_print_options opts;
14f9c5c9 932
4c4b4cd2
PH
933 /* If it is a pointer, indicate what it points to. */
934 if (TYPE_CODE (type) == TYPE_CODE_PTR)
14f9c5c9 935 {
4c4b4cd2
PH
936 /* Hack: don't print (char *) for char strings. Their
937 type is indicated by the quoted string anyway. */
938 if (TYPE_LENGTH (TYPE_TARGET_TYPE (type)) != sizeof (char)
939 || TYPE_CODE (TYPE_TARGET_TYPE (type)) != TYPE_CODE_INT
940 || TYPE_UNSIGNED (TYPE_TARGET_TYPE (type)))
14f9c5c9
AS
941 {
942 fprintf_filtered (stream, "(");
943 type_print (type, "", stream, -1);
944 fprintf_filtered (stream, ") ");
945 }
946 }
4c4b4cd2 947 else if (ada_is_array_descriptor_type (type))
14f9c5c9
AS
948 {
949 fprintf_filtered (stream, "(");
950 type_print (type, "", stream, -1);
951 fprintf_filtered (stream, ") ");
952 }
953 else if (ada_is_bogus_array_descriptor (type))
954 {
955 fprintf_filtered (stream, "(");
956 type_print (type, "", stream, -1);
957 fprintf_filtered (stream, ") (...?)");
958 return 0;
959 }
4c4b4cd2 960
79a45b7d
TT
961 opts = *options;
962 opts.deref_ref = 1;
0fd88904 963 return (val_print (type, value_contents (val), 0, address,
0e03807e 964 stream, 0, val, &opts, current_language));
14f9c5c9 965}
d2e4a39e 966
14f9c5c9 967static void
fc1a4b47 968print_record (struct type *type, const gdb_byte *valaddr,
79a45b7d 969 struct ui_file *stream, int recurse,
0e03807e 970 const struct value *val,
79a45b7d 971 const struct value_print_options *options)
14f9c5c9 972{
61ee279c 973 type = ada_check_typedef (type);
14f9c5c9
AS
974
975 fprintf_filtered (stream, "(");
976
0e03807e 977 if (print_field_values (type, valaddr, stream, recurse, val, options,
79a45b7d 978 0, type, valaddr) != 0 && options->pretty)
14f9c5c9
AS
979 {
980 fprintf_filtered (stream, "\n");
981 print_spaces_filtered (2 * recurse, stream);
982 }
983
984 fprintf_filtered (stream, ")");
985}
986
987/* Print out fields of value at VALADDR having structure type TYPE.
4c4b4cd2 988
79a45b7d 989 TYPE, VALADDR, STREAM, RECURSE, and OPTIONS have the
4c4b4cd2 990 same meanings as in ada_print_value and ada_val_print.
14f9c5c9
AS
991
992 OUTER_TYPE and OUTER_VALADDR give type and address of enclosing record
993 (used to get discriminant values when printing variant parts).
994
4c4b4cd2 995 COMMA_NEEDED is 1 if fields have been printed at the current recursion
14f9c5c9 996 level, so that a comma is needed before any field printed by this
4c4b4cd2 997 call.
14f9c5c9 998
4c4b4cd2 999 Returns 1 if COMMA_NEEDED or any fields were printed. */
14f9c5c9
AS
1000
1001static int
fc1a4b47 1002print_field_values (struct type *type, const gdb_byte *valaddr,
79a45b7d 1003 struct ui_file *stream, int recurse,
0e03807e 1004 const struct value *val,
79a45b7d
TT
1005 const struct value_print_options *options,
1006 int comma_needed,
fc1a4b47 1007 struct type *outer_type, const gdb_byte *outer_valaddr)
14f9c5c9
AS
1008{
1009 int i, len;
1010
1011 len = TYPE_NFIELDS (type);
1012
1013 for (i = 0; i < len; i += 1)
1014 {
1015 if (ada_is_ignored_field (type, i))
d2e4a39e 1016 continue;
14f9c5c9
AS
1017
1018 if (ada_is_wrapper_field (type, i))
1019 {
d2e4a39e 1020 comma_needed =
14f9c5c9 1021 print_field_values (TYPE_FIELD_TYPE (type, i),
d2e4a39e 1022 valaddr
14f9c5c9 1023 + TYPE_FIELD_BITPOS (type, i) / HOST_CHAR_BIT,
0e03807e 1024 stream, recurse, val, options,
14f9c5c9
AS
1025 comma_needed, type, valaddr);
1026 continue;
1027 }
1028 else if (ada_is_variant_part (type, i))
1029 {
1030 comma_needed =
1031 print_variant_part (type, i, valaddr,
0e03807e 1032 stream, recurse, val, options, comma_needed,
14f9c5c9
AS
1033 outer_type, outer_valaddr);
1034 continue;
1035 }
1036
1037 if (comma_needed)
1038 fprintf_filtered (stream, ", ");
1039 comma_needed = 1;
1040
79a45b7d 1041 if (options->pretty)
14f9c5c9
AS
1042 {
1043 fprintf_filtered (stream, "\n");
1044 print_spaces_filtered (2 + 2 * recurse, stream);
1045 }
d2e4a39e 1046 else
14f9c5c9
AS
1047 {
1048 wrap_here (n_spaces (2 + 2 * recurse));
1049 }
79a45b7d 1050 if (options->inspect_it)
14f9c5c9
AS
1051 {
1052 if (TYPE_CODE (TYPE_FIELD_TYPE (type, i)) == TYPE_CODE_PTR)
1053 fputs_filtered ("\"( ptr \"", stream);
1054 else
1055 fputs_filtered ("\"( nodef \"", stream);
1056 fprintf_symbol_filtered (stream, TYPE_FIELD_NAME (type, i),
1057 language_cplus, DMGL_NO_OPTS);
1058 fputs_filtered ("\" \"", stream);
1059 fprintf_symbol_filtered (stream, TYPE_FIELD_NAME (type, i),
1060 language_cplus, DMGL_NO_OPTS);
1061 fputs_filtered ("\") \"", stream);
1062 }
1063 else
1064 {
1065 annotate_field_begin (TYPE_FIELD_TYPE (type, i));
d2e4a39e 1066 fprintf_filtered (stream, "%.*s",
14f9c5c9
AS
1067 ada_name_prefix_len (TYPE_FIELD_NAME (type, i)),
1068 TYPE_FIELD_NAME (type, i));
1069 annotate_field_name_end ();
1070 fputs_filtered (" => ", stream);
1071 annotate_field_value ();
1072 }
1073
1074 if (TYPE_FIELD_PACKED (type, i))
1075 {
d2e4a39e 1076 struct value *v;
14f9c5c9
AS
1077
1078 /* Bitfields require special handling, especially due to byte
1079 order problems. */
b4ba55a1 1080 if (HAVE_CPLUS_STRUCT (type) && TYPE_FIELD_IGNORE (type, i))
14f9c5c9 1081 {
edefbb7c 1082 fputs_filtered (_("<optimized out or zero length>"), stream);
14f9c5c9
AS
1083 }
1084 else
1085 {
1086 int bit_pos = TYPE_FIELD_BITPOS (type, i);
1087 int bit_size = TYPE_FIELD_BITSIZE (type, i);
79a45b7d 1088 struct value_print_options opts;
d2e4a39e 1089
14f9c5c9
AS
1090 adjust_type_signedness (TYPE_FIELD_TYPE (type, i));
1091 v = ada_value_primitive_packed_val (NULL, valaddr,
1092 bit_pos / HOST_CHAR_BIT,
1093 bit_pos % HOST_CHAR_BIT,
d2e4a39e 1094 bit_size,
14f9c5c9 1095 TYPE_FIELD_TYPE (type, i));
79a45b7d
TT
1096 opts = *options;
1097 opts.deref_ref = 0;
0fd88904 1098 val_print (TYPE_FIELD_TYPE (type, i), value_contents (v), 0, 0,
0e03807e
TT
1099 stream, recurse + 1, v,
1100 &opts, current_language);
14f9c5c9
AS
1101 }
1102 }
1103 else
79a45b7d
TT
1104 {
1105 struct value_print_options opts = *options;
5b4ee69b 1106
79a45b7d
TT
1107 opts.deref_ref = 0;
1108 ada_val_print (TYPE_FIELD_TYPE (type, i),
1109 valaddr + TYPE_FIELD_BITPOS (type, i) / HOST_CHAR_BIT,
0e03807e 1110 0, 0, stream, recurse + 1, val, &opts);
79a45b7d 1111 }
14f9c5c9
AS
1112 annotate_field_end ();
1113 }
1114
1115 return comma_needed;
1116}
This page took 0.543703 seconds and 4 git commands to generate.