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