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