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