Home | History | Annotate | Line # | Download | only in gdb
ada-valprint.c revision 1.8
      1 /* Support for printing Ada values for GDB, the GNU debugger.
      2 
      3    Copyright (C) 1986-2019 Free Software Foundation, Inc.
      4 
      5    This file is part of GDB.
      6 
      7    This program is free software; you can redistribute it and/or modify
      8    it under the terms of the GNU General Public License as published by
      9    the Free Software Foundation; either version 3 of the License, or
     10    (at your option) any later version.
     11 
     12    This program is distributed in the hope that it will be useful,
     13    but WITHOUT ANY WARRANTY; without even the implied warranty of
     14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     15    GNU General Public License for more details.
     16 
     17    You should have received a copy of the GNU General Public License
     18    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
     19 
     20 #include "defs.h"
     21 #include <ctype.h>
     22 #include "symtab.h"
     23 #include "gdbtypes.h"
     24 #include "expression.h"
     25 #include "value.h"
     26 #include "demangle.h"
     27 #include "valprint.h"
     28 #include "language.h"
     29 #include "annotate.h"
     30 #include "ada-lang.h"
     31 #include "c-lang.h"
     32 #include "infcall.h"
     33 #include "objfiles.h"
     34 #include "target-float.h"
     35 
     36 static int print_field_values (struct type *, const gdb_byte *,
     37 			       int,
     38 			       struct ui_file *, int,
     39 			       struct value *,
     40 			       const struct value_print_options *,
     41 			       int, struct type *, int,
     42 			       const struct language_defn *);
     43 
     44 
     46 /* Make TYPE unsigned if its range of values includes no negatives.  */
     47 static void
     48 adjust_type_signedness (struct type *type)
     49 {
     50   if (type != NULL && TYPE_CODE (type) == TYPE_CODE_RANGE
     51       && TYPE_LOW_BOUND (type) >= 0)
     52     TYPE_UNSIGNED (type) = 1;
     53 }
     54 
     55 /* Assuming TYPE is a simple array type, prints its lower bound on STREAM,
     56    if non-standard (i.e., other than 1 for numbers, other than lower bound
     57    of index type for enumerated type).  Returns 1 if something printed,
     58    otherwise 0.  */
     59 
     60 static int
     61 print_optional_low_bound (struct ui_file *stream, struct type *type,
     62 			  const struct value_print_options *options)
     63 {
     64   struct type *index_type;
     65   LONGEST low_bound;
     66   LONGEST high_bound;
     67 
     68   if (options->print_array_indexes)
     69     return 0;
     70 
     71   if (!get_array_bounds (type, &low_bound, &high_bound))
     72     return 0;
     73 
     74   /* If this is an empty array, then don't print the lower bound.
     75      That would be confusing, because we would print the lower bound,
     76      followed by... nothing!  */
     77   if (low_bound > high_bound)
     78     return 0;
     79 
     80   index_type = TYPE_INDEX_TYPE (type);
     81 
     82   while (TYPE_CODE (index_type) == TYPE_CODE_RANGE)
     83     {
     84       /* We need to know what the base type is, in order to do the
     85          appropriate check below.  Otherwise, if this is a subrange
     86          of an enumerated type, where the underlying value of the
     87          first element is typically 0, we might test the low bound
     88          against the wrong value.  */
     89       index_type = TYPE_TARGET_TYPE (index_type);
     90     }
     91 
     92   /* Don't print the lower bound if it's the default one.  */
     93   switch (TYPE_CODE (index_type))
     94     {
     95     case TYPE_CODE_BOOL:
     96     case TYPE_CODE_CHAR:
     97       if (low_bound == 0)
     98 	return 0;
     99       break;
    100     case TYPE_CODE_ENUM:
    101       if (low_bound == TYPE_FIELD_ENUMVAL (index_type, 0))
    102 	return 0;
    103       break;
    104     case TYPE_CODE_UNDEF:
    105       index_type = NULL;
    106       /* FALL THROUGH */
    107     default:
    108       if (low_bound == 1)
    109 	return 0;
    110       break;
    111     }
    112 
    113   ada_print_scalar (index_type, low_bound, stream);
    114   fprintf_filtered (stream, " => ");
    115   return 1;
    116 }
    117 
    118 /*  Version of val_print_array_elements for GNAT-style packed arrays.
    119     Prints elements of packed array of type TYPE at bit offset
    120     BITOFFSET from VALADDR on STREAM.  Formats according to OPTIONS and
    121     separates with commas.  RECURSE is the recursion (nesting) level.
    122     TYPE must have been decoded (as by ada_coerce_to_simple_array).  */
    123 
    124 static void
    125 val_print_packed_array_elements (struct type *type, const gdb_byte *valaddr,
    126 				 int offset,
    127 				 int bitoffset, struct ui_file *stream,
    128 				 int recurse,
    129 				 struct value *val,
    130 				 const struct value_print_options *options)
    131 {
    132   unsigned int i;
    133   unsigned int things_printed = 0;
    134   unsigned len;
    135   struct type *elttype, *index_type;
    136   unsigned long bitsize = TYPE_FIELD_BITSIZE (type, 0);
    137   struct value *mark = value_mark ();
    138   LONGEST low = 0;
    139 
    140   elttype = TYPE_TARGET_TYPE (type);
    141   index_type = TYPE_INDEX_TYPE (type);
    142 
    143   {
    144     LONGEST high;
    145     struct type *base_index_type;
    146 
    147     if (get_discrete_bounds (index_type, &low, &high) < 0)
    148       len = 1;
    149     else
    150       len = high - low + 1;
    151 
    152     if (TYPE_CODE (index_type) == TYPE_CODE_RANGE)
    153         base_index_type = TYPE_TARGET_TYPE (index_type);
    154       else
    155         base_index_type = index_type;
    156 
    157     if (TYPE_CODE (base_index_type) == TYPE_CODE_ENUM)
    158       {
    159         LONGEST low_pos, high_pos;
    160 
    161         /* Non-contiguous enumerations types can by used as index types
    162            so the array length is computed from the positions of the
    163            first and last literal in the enumeration type, and not from
    164            the values of these literals.  */
    165 
    166         if (!discrete_position (base_index_type, low, &low_pos)
    167           || !discrete_position (base_index_type, high, &high_pos))
    168           {
    169             warning (_("unable to get positions in array, use bounds instead"));
    170             low_pos = low;
    171             high_pos = high;
    172           }
    173 
    174         /* The array length should normally be HIGH_POS - LOW_POS + 1.
    175            But in Ada we allow LOW_POS to be greater than HIGH_POS for
    176            empty arrays.  In that situation, the array length is just zero,
    177            not negative!  */
    178 
    179         if (low_pos > high_pos)
    180           len = 0;
    181         else
    182           len = high_pos - low_pos + 1;
    183       }
    184   }
    185 
    186   i = 0;
    187   annotate_array_section_begin (i, elttype);
    188 
    189   while (i < len && things_printed < options->print_max)
    190     {
    191       struct value *v0, *v1;
    192       int i0;
    193 
    194       if (i != 0)
    195 	{
    196 	  if (options->prettyformat_arrays)
    197 	    {
    198 	      fprintf_filtered (stream, ",\n");
    199 	      print_spaces_filtered (2 + 2 * recurse, stream);
    200 	    }
    201 	  else
    202 	    {
    203 	      fprintf_filtered (stream, ", ");
    204 	    }
    205 	}
    206       wrap_here (n_spaces (2 + 2 * recurse));
    207       maybe_print_array_index (index_type, i + low, stream, options);
    208 
    209       i0 = i;
    210       v0 = ada_value_primitive_packed_val (NULL, valaddr + offset,
    211 					   (i0 * bitsize) / HOST_CHAR_BIT,
    212 					   (i0 * bitsize) % HOST_CHAR_BIT,
    213 					   bitsize, elttype);
    214       while (1)
    215 	{
    216 	  i += 1;
    217 	  if (i >= len)
    218 	    break;
    219 	  v1 = ada_value_primitive_packed_val (NULL, valaddr + offset,
    220 					       (i * bitsize) / HOST_CHAR_BIT,
    221 					       (i * bitsize) % HOST_CHAR_BIT,
    222 					       bitsize, elttype);
    223 	  if (TYPE_LENGTH (check_typedef (value_type (v0)))
    224 	      != TYPE_LENGTH (check_typedef (value_type (v1))))
    225 	    break;
    226 	  if (!value_contents_eq (v0, value_embedded_offset (v0),
    227 				  v1, value_embedded_offset (v1),
    228 				  TYPE_LENGTH (check_typedef (value_type (v0)))))
    229 	    break;
    230 	}
    231 
    232       if (i - i0 > options->repeat_count_threshold)
    233 	{
    234 	  struct value_print_options opts = *options;
    235 
    236 	  opts.deref_ref = 0;
    237 	  val_print (elttype,
    238 		     value_embedded_offset (v0), 0, stream,
    239 		     recurse + 1, v0, &opts, current_language);
    240 	  annotate_elt_rep (i - i0);
    241 	  fprintf_filtered (stream, _(" <repeats %u times>"), i - i0);
    242 	  annotate_elt_rep_end ();
    243 
    244 	}
    245       else
    246 	{
    247 	  int j;
    248 	  struct value_print_options opts = *options;
    249 
    250 	  opts.deref_ref = 0;
    251 	  for (j = i0; j < i; j += 1)
    252 	    {
    253 	      if (j > i0)
    254 		{
    255 		  if (options->prettyformat_arrays)
    256 		    {
    257 		      fprintf_filtered (stream, ",\n");
    258 		      print_spaces_filtered (2 + 2 * recurse, stream);
    259 		    }
    260 		  else
    261 		    {
    262 		      fprintf_filtered (stream, ", ");
    263 		    }
    264 		  wrap_here (n_spaces (2 + 2 * recurse));
    265 		  maybe_print_array_index (index_type, j + low,
    266 					   stream, options);
    267 		}
    268 	      val_print (elttype,
    269 			 value_embedded_offset (v0), 0, stream,
    270 			 recurse + 1, v0, &opts, current_language);
    271 	      annotate_elt ();
    272 	    }
    273 	}
    274       things_printed += i - i0;
    275     }
    276   annotate_array_section_end ();
    277   if (i < len)
    278     {
    279       fprintf_filtered (stream, "...");
    280     }
    281 
    282   value_free_to_mark (mark);
    283 }
    284 
    285 static struct type *
    286 printable_val_type (struct type *type, const gdb_byte *valaddr)
    287 {
    288   return ada_to_fixed_type (ada_aligned_type (type), valaddr, 0, NULL, 1);
    289 }
    290 
    291 /* Print the character C on STREAM as part of the contents of a literal
    292    string whose delimiter is QUOTER.  TYPE_LEN is the length in bytes
    293    of the character.  */
    294 
    295 void
    296 ada_emit_char (int c, struct type *type, struct ui_file *stream,
    297 	       int quoter, int type_len)
    298 {
    299   /* If this character fits in the normal ASCII range, and is
    300      a printable character, then print the character as if it was
    301      an ASCII character, even if this is a wide character.
    302      The UCHAR_MAX check is necessary because the isascii function
    303      requires that its argument have a value of an unsigned char,
    304      or EOF (EOF is obviously not printable).  */
    305   if (c <= UCHAR_MAX && isascii (c) && isprint (c))
    306     {
    307       if (c == quoter && c == '"')
    308 	fprintf_filtered (stream, "\"\"");
    309       else
    310 	fprintf_filtered (stream, "%c", c);
    311     }
    312   else
    313     fprintf_filtered (stream, "[\"%0*x\"]", type_len * 2, c);
    314 }
    315 
    316 /* Character #I of STRING, given that TYPE_LEN is the size in bytes
    317    of a character.  */
    318 
    319 static int
    320 char_at (const gdb_byte *string, int i, int type_len,
    321 	 enum bfd_endian byte_order)
    322 {
    323   if (type_len == 1)
    324     return string[i];
    325   else
    326     return (int) extract_unsigned_integer (string + type_len * i,
    327                                            type_len, byte_order);
    328 }
    329 
    330 /* Print a floating-point value of type TYPE, pointed to in GDB by
    331    VALADDR, on STREAM.  Use Ada formatting conventions: there must be
    332    a decimal point, and at least one digit before and after the
    333    point.  We use the GNAT format for NaNs and infinities.  */
    334 
    335 static void
    336 ada_print_floating (const gdb_byte *valaddr, struct type *type,
    337 		    struct ui_file *stream)
    338 {
    339   string_file tmp_stream;
    340 
    341   print_floating (valaddr, type, &tmp_stream);
    342 
    343   std::string &s = tmp_stream.string ();
    344   size_t skip_count = 0;
    345 
    346   /* Modify for Ada rules.  */
    347 
    348   size_t pos = s.find ("inf");
    349   if (pos == std::string::npos)
    350     pos = s.find ("Inf");
    351   if (pos == std::string::npos)
    352     pos = s.find ("INF");
    353   if (pos != std::string::npos)
    354     s.replace (pos, 3, "Inf");
    355 
    356   if (pos == std::string::npos)
    357     {
    358       pos = s.find ("nan");
    359       if (pos == std::string::npos)
    360 	pos = s.find ("NaN");
    361       if (pos == std::string::npos)
    362 	pos = s.find ("Nan");
    363       if (pos != std::string::npos)
    364 	{
    365 	  s[pos] = s[pos + 2] = 'N';
    366 	  if (s[0] == '-')
    367 	    skip_count = 1;
    368 	}
    369     }
    370 
    371   if (pos == std::string::npos
    372       && s.find ('.') == std::string::npos)
    373     {
    374       pos = s.find ('e');
    375       if (pos == std::string::npos)
    376 	fprintf_filtered (stream, "%s.0", s.c_str ());
    377       else
    378 	fprintf_filtered (stream, "%.*s.0%s", (int) pos, s.c_str (), &s[pos]);
    379     }
    380   else
    381     fprintf_filtered (stream, "%s", &s[skip_count]);
    382 }
    383 
    384 void
    385 ada_printchar (int c, struct type *type, struct ui_file *stream)
    386 {
    387   fputs_filtered ("'", stream);
    388   ada_emit_char (c, type, stream, '\'', TYPE_LENGTH (type));
    389   fputs_filtered ("'", stream);
    390 }
    391 
    392 /* [From print_type_scalar in typeprint.c].   Print VAL on STREAM in a
    393    form appropriate for TYPE, if non-NULL.  If TYPE is NULL, print VAL
    394    like a default signed integer.  */
    395 
    396 void
    397 ada_print_scalar (struct type *type, LONGEST val, struct ui_file *stream)
    398 {
    399   unsigned int i;
    400   unsigned len;
    401 
    402   if (!type)
    403     {
    404       print_longest (stream, 'd', 0, val);
    405       return;
    406     }
    407 
    408   type = ada_check_typedef (type);
    409 
    410   switch (TYPE_CODE (type))
    411     {
    412 
    413     case TYPE_CODE_ENUM:
    414       len = TYPE_NFIELDS (type);
    415       for (i = 0; i < len; i++)
    416 	{
    417 	  if (TYPE_FIELD_ENUMVAL (type, i) == val)
    418 	    {
    419 	      break;
    420 	    }
    421 	}
    422       if (i < len)
    423 	{
    424 	  fputs_filtered (ada_enum_name (TYPE_FIELD_NAME (type, i)), stream);
    425 	}
    426       else
    427 	{
    428 	  print_longest (stream, 'd', 0, val);
    429 	}
    430       break;
    431 
    432     case TYPE_CODE_INT:
    433       print_longest (stream, TYPE_UNSIGNED (type) ? 'u' : 'd', 0, val);
    434       break;
    435 
    436     case TYPE_CODE_CHAR:
    437       LA_PRINT_CHAR (val, type, stream);
    438       break;
    439 
    440     case TYPE_CODE_BOOL:
    441       fprintf_filtered (stream, val ? "true" : "false");
    442       break;
    443 
    444     case TYPE_CODE_RANGE:
    445       ada_print_scalar (TYPE_TARGET_TYPE (type), val, stream);
    446       return;
    447 
    448     case TYPE_CODE_UNDEF:
    449     case TYPE_CODE_PTR:
    450     case TYPE_CODE_ARRAY:
    451     case TYPE_CODE_STRUCT:
    452     case TYPE_CODE_UNION:
    453     case TYPE_CODE_FUNC:
    454     case TYPE_CODE_FLT:
    455     case TYPE_CODE_VOID:
    456     case TYPE_CODE_SET:
    457     case TYPE_CODE_STRING:
    458     case TYPE_CODE_ERROR:
    459     case TYPE_CODE_MEMBERPTR:
    460     case TYPE_CODE_METHODPTR:
    461     case TYPE_CODE_METHOD:
    462     case TYPE_CODE_REF:
    463       warning (_("internal error: unhandled type in ada_print_scalar"));
    464       break;
    465 
    466     default:
    467       error (_("Invalid type code in symbol table."));
    468     }
    469   gdb_flush (stream);
    470 }
    471 
    472 /* Print the character string STRING, printing at most LENGTH characters.
    473    Printing stops early if the number hits print_max; repeat counts
    474    are printed as appropriate.  Print ellipses at the end if we
    475    had to stop before printing LENGTH characters, or if FORCE_ELLIPSES.
    476    TYPE_LEN is the length (1 or 2) of the character type.  */
    477 
    478 static void
    479 printstr (struct ui_file *stream, struct type *elttype, const gdb_byte *string,
    480 	  unsigned int length, int force_ellipses, int type_len,
    481 	  const struct value_print_options *options)
    482 {
    483   enum bfd_endian byte_order = gdbarch_byte_order (get_type_arch (elttype));
    484   unsigned int i;
    485   unsigned int things_printed = 0;
    486   int in_quotes = 0;
    487   int need_comma = 0;
    488 
    489   if (length == 0)
    490     {
    491       fputs_filtered ("\"\"", stream);
    492       return;
    493     }
    494 
    495   for (i = 0; i < length && things_printed < options->print_max; i += 1)
    496     {
    497       /* Position of the character we are examining
    498          to see whether it is repeated.  */
    499       unsigned int rep1;
    500       /* Number of repetitions we have detected so far.  */
    501       unsigned int reps;
    502 
    503       QUIT;
    504 
    505       if (need_comma)
    506 	{
    507 	  fputs_filtered (", ", stream);
    508 	  need_comma = 0;
    509 	}
    510 
    511       rep1 = i + 1;
    512       reps = 1;
    513       while (rep1 < length
    514 	     && char_at (string, rep1, type_len, byte_order)
    515 		== char_at (string, i, type_len, byte_order))
    516 	{
    517 	  rep1 += 1;
    518 	  reps += 1;
    519 	}
    520 
    521       if (reps > options->repeat_count_threshold)
    522 	{
    523 	  if (in_quotes)
    524 	    {
    525 	      fputs_filtered ("\", ", stream);
    526 	      in_quotes = 0;
    527 	    }
    528 	  fputs_filtered ("'", stream);
    529 	  ada_emit_char (char_at (string, i, type_len, byte_order),
    530 			 elttype, stream, '\'', type_len);
    531 	  fputs_filtered ("'", stream);
    532 	  fprintf_filtered (stream, _(" <repeats %u times>"), reps);
    533 	  i = rep1 - 1;
    534 	  things_printed += options->repeat_count_threshold;
    535 	  need_comma = 1;
    536 	}
    537       else
    538 	{
    539 	  if (!in_quotes)
    540 	    {
    541 	      fputs_filtered ("\"", stream);
    542 	      in_quotes = 1;
    543 	    }
    544 	  ada_emit_char (char_at (string, i, type_len, byte_order),
    545 			 elttype, stream, '"', type_len);
    546 	  things_printed += 1;
    547 	}
    548     }
    549 
    550   /* Terminate the quotes if necessary.  */
    551   if (in_quotes)
    552     fputs_filtered ("\"", stream);
    553 
    554   if (force_ellipses || i < length)
    555     fputs_filtered ("...", stream);
    556 }
    557 
    558 void
    559 ada_printstr (struct ui_file *stream, struct type *type,
    560 	      const gdb_byte *string, unsigned int length,
    561 	      const char *encoding, int force_ellipses,
    562 	      const struct value_print_options *options)
    563 {
    564   printstr (stream, type, string, length, force_ellipses, TYPE_LENGTH (type),
    565 	    options);
    566 }
    567 
    568 static int
    569 print_variant_part (struct type *type, int field_num,
    570 		    const gdb_byte *valaddr, int offset,
    571 		    struct ui_file *stream, int recurse,
    572 		    struct value *val,
    573 		    const struct value_print_options *options,
    574 		    int comma_needed,
    575 		    struct type *outer_type, int outer_offset,
    576 		    const struct language_defn *language)
    577 {
    578   struct type *var_type = TYPE_FIELD_TYPE (type, field_num);
    579   int which = ada_which_variant_applies (var_type, outer_type,
    580 					 valaddr + outer_offset);
    581 
    582   if (which < 0)
    583     return 0;
    584   else
    585     return print_field_values
    586       (TYPE_FIELD_TYPE (var_type, which),
    587        valaddr,
    588        offset + TYPE_FIELD_BITPOS (type, field_num) / HOST_CHAR_BIT
    589        + TYPE_FIELD_BITPOS (var_type, which) / HOST_CHAR_BIT,
    590        stream, recurse, val, options,
    591        comma_needed, outer_type, outer_offset, language);
    592 }
    593 
    594 /* Print out fields of value at VALADDR + OFFSET having structure type TYPE.
    595 
    596    TYPE, VALADDR, OFFSET, STREAM, RECURSE, and OPTIONS have the same
    597    meanings as in ada_print_value and ada_val_print.
    598 
    599    OUTER_TYPE and OUTER_OFFSET give type and address of enclosing
    600    record (used to get discriminant values when printing variant
    601    parts).
    602 
    603    COMMA_NEEDED is 1 if fields have been printed at the current recursion
    604    level, so that a comma is needed before any field printed by this
    605    call.
    606 
    607    Returns 1 if COMMA_NEEDED or any fields were printed.  */
    608 
    609 static int
    610 print_field_values (struct type *type, const gdb_byte *valaddr,
    611 		    int offset, struct ui_file *stream, int recurse,
    612 		    struct value *val,
    613 		    const struct value_print_options *options,
    614 		    int comma_needed,
    615 		    struct type *outer_type, int outer_offset,
    616 		    const struct language_defn *language)
    617 {
    618   int i, len;
    619 
    620   len = TYPE_NFIELDS (type);
    621 
    622   for (i = 0; i < len; i += 1)
    623     {
    624       if (ada_is_ignored_field (type, i))
    625 	continue;
    626 
    627       if (ada_is_wrapper_field (type, i))
    628 	{
    629 	  comma_needed =
    630 	    print_field_values (TYPE_FIELD_TYPE (type, i),
    631 				valaddr,
    632 				(offset
    633 				 + TYPE_FIELD_BITPOS (type, i) / HOST_CHAR_BIT),
    634 				stream, recurse, val, options,
    635 				comma_needed, type, offset, language);
    636 	  continue;
    637 	}
    638       else if (ada_is_variant_part (type, i))
    639 	{
    640 	  comma_needed =
    641 	    print_variant_part (type, i, valaddr,
    642 				offset, stream, recurse, val,
    643 				options, comma_needed,
    644 				outer_type, outer_offset, language);
    645 	  continue;
    646 	}
    647 
    648       if (comma_needed)
    649 	fprintf_filtered (stream, ", ");
    650       comma_needed = 1;
    651 
    652       if (options->prettyformat)
    653 	{
    654 	  fprintf_filtered (stream, "\n");
    655 	  print_spaces_filtered (2 + 2 * recurse, stream);
    656 	}
    657       else
    658 	{
    659 	  wrap_here (n_spaces (2 + 2 * recurse));
    660 	}
    661 
    662       annotate_field_begin (TYPE_FIELD_TYPE (type, i));
    663       fprintf_filtered (stream, "%.*s",
    664 			ada_name_prefix_len (TYPE_FIELD_NAME (type, i)),
    665 			TYPE_FIELD_NAME (type, i));
    666       annotate_field_name_end ();
    667       fputs_filtered (" => ", stream);
    668       annotate_field_value ();
    669 
    670       if (TYPE_FIELD_PACKED (type, i))
    671 	{
    672 	  /* Bitfields require special handling, especially due to byte
    673 	     order problems.  */
    674 	  if (HAVE_CPLUS_STRUCT (type) && TYPE_FIELD_IGNORE (type, i))
    675 	    {
    676 	      fputs_filtered (_("<optimized out or zero length>"), stream);
    677 	    }
    678 	  else
    679 	    {
    680 	      struct value *v;
    681 	      int bit_pos = TYPE_FIELD_BITPOS (type, i);
    682 	      int bit_size = TYPE_FIELD_BITSIZE (type, i);
    683 	      struct value_print_options opts;
    684 
    685 	      adjust_type_signedness (TYPE_FIELD_TYPE (type, i));
    686 	      v = ada_value_primitive_packed_val
    687 		    (NULL, valaddr,
    688 		     offset + bit_pos / HOST_CHAR_BIT,
    689 		     bit_pos % HOST_CHAR_BIT,
    690 		     bit_size, TYPE_FIELD_TYPE (type, i));
    691 	      opts = *options;
    692 	      opts.deref_ref = 0;
    693 	      val_print (TYPE_FIELD_TYPE (type, i),
    694 			 value_embedded_offset (v), 0,
    695 			 stream, recurse + 1, v,
    696 			 &opts, language);
    697 	    }
    698 	}
    699       else
    700 	{
    701 	  struct value_print_options opts = *options;
    702 
    703 	  opts.deref_ref = 0;
    704 	  val_print (TYPE_FIELD_TYPE (type, i),
    705 		     (offset + TYPE_FIELD_BITPOS (type, i) / HOST_CHAR_BIT),
    706 		     0, stream, recurse + 1, val, &opts, language);
    707 	}
    708       annotate_field_end ();
    709     }
    710 
    711   return comma_needed;
    712 }
    713 
    714 /* Implement Ada val_print'ing for the case where TYPE is
    715    a TYPE_CODE_ARRAY of characters.  */
    716 
    717 static void
    718 ada_val_print_string (struct type *type, const gdb_byte *valaddr,
    719 		      int offset, int offset_aligned, CORE_ADDR address,
    720 		      struct ui_file *stream, int recurse,
    721 		      struct value *original_value,
    722 		      const struct value_print_options *options)
    723 {
    724   enum bfd_endian byte_order = gdbarch_byte_order (get_type_arch (type));
    725   struct type *elttype = TYPE_TARGET_TYPE (type);
    726   unsigned int eltlen;
    727   unsigned int len;
    728 
    729   /* We know that ELTTYPE cannot possibly be null, because we assume
    730      that we're called only when TYPE is a string-like type.
    731      Similarly, the size of ELTTYPE should also be non-null, since
    732      it's a character-like type.  */
    733   gdb_assert (elttype != NULL);
    734   gdb_assert (TYPE_LENGTH (elttype) != 0);
    735 
    736   eltlen = TYPE_LENGTH (elttype);
    737   len = TYPE_LENGTH (type) / eltlen;
    738 
    739   if (options->prettyformat_arrays)
    740     print_spaces_filtered (2 + 2 * recurse, stream);
    741 
    742   /* If requested, look for the first null char and only print
    743      elements up to it.  */
    744   if (options->stop_print_at_null)
    745     {
    746       int temp_len;
    747 
    748       /* Look for a NULL char.  */
    749       for (temp_len = 0;
    750 	   (temp_len < len
    751 	    && temp_len < options->print_max
    752 	    && char_at (valaddr + offset_aligned,
    753 			temp_len, eltlen, byte_order) != 0);
    754 	   temp_len += 1);
    755       len = temp_len;
    756     }
    757 
    758   printstr (stream, elttype, valaddr + offset_aligned, len, 0,
    759 	    eltlen, options);
    760 }
    761 
    762 /* Implement Ada val_print-ing for GNAT arrays (Eg. fat pointers,
    763    thin pointers, etc).  */
    764 
    765 static void
    766 ada_val_print_gnat_array (struct type *type, const gdb_byte *valaddr,
    767 			  int offset, CORE_ADDR address,
    768 			  struct ui_file *stream, int recurse,
    769 			  struct value *original_value,
    770 			  const struct value_print_options *options,
    771 			  const struct language_defn *language)
    772 {
    773   struct value *mark = value_mark ();
    774   struct value *val;
    775 
    776   val = value_from_contents_and_address (type, valaddr + offset, address);
    777   /* If this is a reference, coerce it now.  This helps taking care
    778      of the case where ADDRESS is meaningless because original_value
    779      was not an lval.  */
    780   val = coerce_ref (val);
    781   if (TYPE_CODE (type) == TYPE_CODE_TYPEDEF)  /* array access type.  */
    782     val = ada_coerce_to_simple_array_ptr (val);
    783   else
    784     val = ada_coerce_to_simple_array (val);
    785   if (val == NULL)
    786     {
    787       gdb_assert (TYPE_CODE (type) == TYPE_CODE_TYPEDEF);
    788       fprintf_filtered (stream, "0x0");
    789     }
    790   else
    791     val_print (value_type (val),
    792 	       value_embedded_offset (val), value_address (val),
    793 	       stream, recurse, val, options, language);
    794   value_free_to_mark (mark);
    795 }
    796 
    797 /* Implement Ada val_print'ing for the case where TYPE is
    798    a TYPE_CODE_PTR.  */
    799 
    800 static void
    801 ada_val_print_ptr (struct type *type, const gdb_byte *valaddr,
    802 		   int offset, int offset_aligned, CORE_ADDR address,
    803 		   struct ui_file *stream, int recurse,
    804 		   struct value *original_value,
    805 		   const struct value_print_options *options,
    806 		   const struct language_defn *language)
    807 {
    808   val_print (type, offset, address, stream, recurse,
    809 	     original_value, options, language_def (language_c));
    810 
    811   if (ada_is_tag_type (type))
    812     {
    813       struct value *val =
    814 	value_from_contents_and_address (type,
    815 					 valaddr + offset_aligned,
    816 					 address + offset_aligned);
    817       const char *name = ada_tag_name (val);
    818 
    819       if (name != NULL)
    820 	fprintf_filtered (stream, " (%s)", name);
    821     }
    822 }
    823 
    824 /* Implement Ada val_print'ing for the case where TYPE is
    825    a TYPE_CODE_INT or TYPE_CODE_RANGE.  */
    826 
    827 static void
    828 ada_val_print_num (struct type *type, const gdb_byte *valaddr,
    829 		   int offset, int offset_aligned, CORE_ADDR address,
    830 		   struct ui_file *stream, int recurse,
    831 		   struct value *original_value,
    832 		   const struct value_print_options *options,
    833 		   const struct language_defn *language)
    834 {
    835   if (ada_is_fixed_point_type (type))
    836     {
    837       struct value *scale = ada_scaling_factor (type);
    838       struct value *v = value_from_contents (type, valaddr + offset_aligned);
    839       v = value_cast (value_type (scale), v);
    840       v = value_binop (v, scale, BINOP_MUL);
    841 
    842       const char *fmt = TYPE_LENGTH (type) < 4 ? "%.11g" : "%.17g";
    843       std::string str
    844 	= target_float_to_string (value_contents (v), value_type (v), fmt);
    845       fputs_filtered (str.c_str (), stream);
    846       return;
    847     }
    848   else if (TYPE_CODE (type) == TYPE_CODE_RANGE)
    849     {
    850       struct type *target_type = TYPE_TARGET_TYPE (type);
    851 
    852       if (TYPE_LENGTH (type) != TYPE_LENGTH (target_type))
    853 	{
    854 	  /* Obscure case of range type that has different length from
    855 	     its base type.  Perform a conversion, or we will get a
    856 	     nonsense value.  Actually, we could use the same
    857 	     code regardless of lengths; I'm just avoiding a cast.  */
    858 	  struct value *v1
    859 	    = value_from_contents_and_address (type, valaddr + offset, 0);
    860 	  struct value *v = value_cast (target_type, v1);
    861 
    862 	  val_print (target_type,
    863 		     value_embedded_offset (v), 0, stream,
    864 		     recurse + 1, v, options, language);
    865 	}
    866       else
    867 	val_print (TYPE_TARGET_TYPE (type), offset,
    868 		   address, stream, recurse, original_value,
    869 		   options, language);
    870       return;
    871     }
    872   else
    873     {
    874       int format = (options->format ? options->format
    875 		    : options->output_format);
    876 
    877       if (format)
    878 	{
    879 	  struct value_print_options opts = *options;
    880 
    881 	  opts.format = format;
    882 	  val_print_scalar_formatted (type, offset_aligned,
    883 				      original_value, &opts, 0, stream);
    884 	}
    885       else if (ada_is_system_address_type (type))
    886 	{
    887 	  /* FIXME: We want to print System.Address variables using
    888 	     the same format as for any access type.  But for some
    889 	     reason GNAT encodes the System.Address type as an int,
    890 	     so we have to work-around this deficiency by handling
    891 	     System.Address values as a special case.  */
    892 
    893 	  struct gdbarch *gdbarch = get_type_arch (type);
    894 	  struct type *ptr_type = builtin_type (gdbarch)->builtin_data_ptr;
    895 	  CORE_ADDR addr = extract_typed_address (valaddr + offset_aligned,
    896 						  ptr_type);
    897 
    898 	  fprintf_filtered (stream, "(");
    899 	  type_print (type, "", stream, -1);
    900 	  fprintf_filtered (stream, ") ");
    901 	  fputs_filtered (paddress (gdbarch, addr), stream);
    902 	}
    903       else
    904 	{
    905 	  val_print_scalar_formatted (type, offset_aligned,
    906 				      original_value, options, 0, stream);
    907 	  if (ada_is_character_type (type))
    908 	    {
    909 	      LONGEST c;
    910 
    911 	      fputs_filtered (" ", stream);
    912 	      c = unpack_long (type, valaddr + offset_aligned);
    913 	      ada_printchar (c, type, stream);
    914 	    }
    915 	}
    916       return;
    917     }
    918 }
    919 
    920 /* Implement Ada val_print'ing for the case where TYPE is
    921    a TYPE_CODE_ENUM.  */
    922 
    923 static void
    924 ada_val_print_enum (struct type *type, const gdb_byte *valaddr,
    925 		    int offset, int offset_aligned, CORE_ADDR address,
    926 		    struct ui_file *stream, int recurse,
    927 		    struct value *original_value,
    928 		    const struct value_print_options *options,
    929 		    const struct language_defn *language)
    930 {
    931   int i;
    932   unsigned int len;
    933   LONGEST val;
    934 
    935   if (options->format)
    936     {
    937       val_print_scalar_formatted (type, offset_aligned,
    938 				  original_value, options, 0, stream);
    939       return;
    940     }
    941 
    942   len = TYPE_NFIELDS (type);
    943   val = unpack_long (type, valaddr + offset_aligned);
    944   for (i = 0; i < len; i++)
    945     {
    946       QUIT;
    947       if (val == TYPE_FIELD_ENUMVAL (type, i))
    948 	break;
    949     }
    950 
    951   if (i < len)
    952     {
    953       const char *name = ada_enum_name (TYPE_FIELD_NAME (type, i));
    954 
    955       if (name[0] == '\'')
    956 	fprintf_filtered (stream, "%ld %s", (long) val, name);
    957       else
    958 	fputs_filtered (name, stream);
    959     }
    960   else
    961     print_longest (stream, 'd', 0, val);
    962 }
    963 
    964 /* Implement Ada val_print'ing for the case where TYPE is
    965    a TYPE_CODE_FLT.  */
    966 
    967 static void
    968 ada_val_print_flt (struct type *type, const gdb_byte *valaddr,
    969 		   int offset, int offset_aligned, CORE_ADDR address,
    970 		   struct ui_file *stream, int recurse,
    971 		   struct value *original_value,
    972 		   const struct value_print_options *options,
    973 		   const struct language_defn *language)
    974 {
    975   if (options->format)
    976     {
    977       val_print (type, offset, address, stream, recurse,
    978 		 original_value, options, language_def (language_c));
    979       return;
    980     }
    981 
    982   ada_print_floating (valaddr + offset, type, stream);
    983 }
    984 
    985 /* Implement Ada val_print'ing for the case where TYPE is
    986    a TYPE_CODE_STRUCT or TYPE_CODE_UNION.  */
    987 
    988 static void
    989 ada_val_print_struct_union
    990   (struct type *type, const gdb_byte *valaddr, int offset,
    991    int offset_aligned, CORE_ADDR address, struct ui_file *stream,
    992    int recurse, struct value *original_value,
    993    const struct value_print_options *options,
    994    const struct language_defn *language)
    995 {
    996   if (ada_is_bogus_array_descriptor (type))
    997     {
    998       fprintf_filtered (stream, "(...?)");
    999       return;
   1000     }
   1001 
   1002   fprintf_filtered (stream, "(");
   1003 
   1004   if (print_field_values (type, valaddr, offset_aligned,
   1005 			  stream, recurse, original_value, options,
   1006 			  0, type, offset_aligned, language) != 0
   1007       && options->prettyformat)
   1008     {
   1009       fprintf_filtered (stream, "\n");
   1010       print_spaces_filtered (2 * recurse, stream);
   1011     }
   1012 
   1013   fprintf_filtered (stream, ")");
   1014 }
   1015 
   1016 /* Implement Ada val_print'ing for the case where TYPE is
   1017    a TYPE_CODE_ARRAY.  */
   1018 
   1019 static void
   1020 ada_val_print_array (struct type *type, const gdb_byte *valaddr,
   1021 		     int offset, int offset_aligned, CORE_ADDR address,
   1022 		     struct ui_file *stream, int recurse,
   1023 		     struct value *original_value,
   1024 		     const struct value_print_options *options)
   1025 {
   1026   /* For an array of characters, print with string syntax.  */
   1027   if (ada_is_string_type (type)
   1028       && (options->format == 0 || options->format == 's'))
   1029     {
   1030       ada_val_print_string (type, valaddr, offset, offset_aligned,
   1031 			    address, stream, recurse, original_value,
   1032 			    options);
   1033       return;
   1034     }
   1035 
   1036   fprintf_filtered (stream, "(");
   1037   print_optional_low_bound (stream, type, options);
   1038   if (TYPE_FIELD_BITSIZE (type, 0) > 0)
   1039     val_print_packed_array_elements (type, valaddr, offset_aligned,
   1040 				     0, stream, recurse,
   1041 				     original_value, options);
   1042   else
   1043     val_print_array_elements (type, offset_aligned, address,
   1044 			      stream, recurse, original_value,
   1045 			      options, 0);
   1046   fprintf_filtered (stream, ")");
   1047 }
   1048 
   1049 /* Implement Ada val_print'ing for the case where TYPE is
   1050    a TYPE_CODE_REF.  */
   1051 
   1052 static void
   1053 ada_val_print_ref (struct type *type, const gdb_byte *valaddr,
   1054 		   int offset, int offset_aligned, CORE_ADDR address,
   1055 		   struct ui_file *stream, int recurse,
   1056 		   struct value *original_value,
   1057 		   const struct value_print_options *options,
   1058 		   const struct language_defn *language)
   1059 {
   1060   /* For references, the debugger is expected to print the value as
   1061      an address if DEREF_REF is null.  But printing an address in place
   1062      of the object value would be confusing to an Ada programmer.
   1063      So, for Ada values, we print the actual dereferenced value
   1064      regardless.  */
   1065   struct type *elttype = check_typedef (TYPE_TARGET_TYPE (type));
   1066   struct value *deref_val;
   1067   CORE_ADDR deref_val_int;
   1068 
   1069   if (TYPE_CODE (elttype) == TYPE_CODE_UNDEF)
   1070     {
   1071       fputs_filtered ("<ref to undefined type>", stream);
   1072       return;
   1073     }
   1074 
   1075   deref_val = coerce_ref_if_computed (original_value);
   1076   if (deref_val)
   1077     {
   1078       if (ada_is_tagged_type (value_type (deref_val), 1))
   1079 	deref_val = ada_tag_value_at_base_address (deref_val);
   1080 
   1081       common_val_print (deref_val, stream, recurse + 1, options,
   1082 			language);
   1083       return;
   1084     }
   1085 
   1086   deref_val_int = unpack_pointer (type, valaddr + offset_aligned);
   1087   if (deref_val_int == 0)
   1088     {
   1089       fputs_filtered ("(null)", stream);
   1090       return;
   1091     }
   1092 
   1093   deref_val
   1094     = ada_value_ind (value_from_pointer (lookup_pointer_type (elttype),
   1095 					 deref_val_int));
   1096   if (ada_is_tagged_type (value_type (deref_val), 1))
   1097     deref_val = ada_tag_value_at_base_address (deref_val);
   1098 
   1099   /* Make sure that the object does not have an unreasonable size
   1100      before trying to print it.  This can happen for instance with
   1101      references to dynamic objects whose contents is uninitialized
   1102      (Eg: an array whose bounds are not set yet).  */
   1103   ada_ensure_varsize_limit (value_type (deref_val));
   1104 
   1105   if (value_lazy (deref_val))
   1106     value_fetch_lazy (deref_val);
   1107 
   1108   val_print (value_type (deref_val),
   1109 	     value_embedded_offset (deref_val),
   1110 	     value_address (deref_val), stream, recurse + 1,
   1111 	     deref_val, options, language);
   1112 }
   1113 
   1114 /* See the comment on ada_val_print.  This function differs in that it
   1115    does not catch evaluation errors (leaving that to ada_val_print).  */
   1116 
   1117 static void
   1118 ada_val_print_1 (struct type *type,
   1119 		 int offset, CORE_ADDR address,
   1120 		 struct ui_file *stream, int recurse,
   1121 		 struct value *original_value,
   1122 		 const struct value_print_options *options,
   1123 		 const struct language_defn *language)
   1124 {
   1125   int offset_aligned;
   1126   const gdb_byte *valaddr = value_contents_for_printing (original_value);
   1127 
   1128   type = ada_check_typedef (type);
   1129 
   1130   if (ada_is_array_descriptor_type (type)
   1131       || (ada_is_constrained_packed_array_type (type)
   1132 	  && TYPE_CODE (type) != TYPE_CODE_PTR))
   1133     {
   1134       ada_val_print_gnat_array (type, valaddr, offset, address,
   1135 				stream, recurse, original_value,
   1136 				options, language);
   1137       return;
   1138     }
   1139 
   1140   offset_aligned = offset + ada_aligned_value_addr (type, valaddr) - valaddr;
   1141   type = printable_val_type (type, valaddr + offset_aligned);
   1142   type = resolve_dynamic_type (type, valaddr + offset_aligned,
   1143 			       address + offset_aligned);
   1144 
   1145   switch (TYPE_CODE (type))
   1146     {
   1147     default:
   1148       val_print (type, offset, address, stream, recurse,
   1149 		 original_value, options, language_def (language_c));
   1150       break;
   1151 
   1152     case TYPE_CODE_PTR:
   1153       ada_val_print_ptr (type, valaddr, offset, offset_aligned,
   1154 			 address, stream, recurse, original_value,
   1155 			 options, language);
   1156       break;
   1157 
   1158     case TYPE_CODE_INT:
   1159     case TYPE_CODE_RANGE:
   1160       ada_val_print_num (type, valaddr, offset, offset_aligned,
   1161 			 address, stream, recurse, original_value,
   1162 			 options, language);
   1163       break;
   1164 
   1165     case TYPE_CODE_ENUM:
   1166       ada_val_print_enum (type, valaddr, offset, offset_aligned,
   1167 			  address, stream, recurse, original_value,
   1168 			  options, language);
   1169       break;
   1170 
   1171     case TYPE_CODE_FLT:
   1172       ada_val_print_flt (type, valaddr, offset, offset_aligned,
   1173 			 address, stream, recurse, original_value,
   1174 			 options, language);
   1175       break;
   1176 
   1177     case TYPE_CODE_UNION:
   1178     case TYPE_CODE_STRUCT:
   1179       ada_val_print_struct_union (type, valaddr, offset, offset_aligned,
   1180 				  address, stream, recurse,
   1181 				  original_value, options, language);
   1182       break;
   1183 
   1184     case TYPE_CODE_ARRAY:
   1185       ada_val_print_array (type, valaddr, offset, offset_aligned,
   1186 			   address, stream, recurse, original_value,
   1187 			   options);
   1188       return;
   1189 
   1190     case TYPE_CODE_REF:
   1191       ada_val_print_ref (type, valaddr, offset, offset_aligned,
   1192 			 address, stream, recurse, original_value,
   1193 			 options, language);
   1194       break;
   1195     }
   1196 }
   1197 
   1198 /* See val_print for a description of the various parameters of this
   1199    function; they are identical.  */
   1200 
   1201 void
   1202 ada_val_print (struct type *type,
   1203 	       int embedded_offset, CORE_ADDR address,
   1204 	       struct ui_file *stream, int recurse,
   1205 	       struct value *val,
   1206 	       const struct value_print_options *options)
   1207 {
   1208   TRY
   1209     {
   1210       ada_val_print_1 (type, embedded_offset, address,
   1211 		       stream, recurse, val, options,
   1212 		       current_language);
   1213     }
   1214   CATCH (except, RETURN_MASK_ERROR)
   1215     {
   1216       fprintf_filtered (stream, _("<error reading variable: %s>"),
   1217 			except.message);
   1218     }
   1219   END_CATCH
   1220 }
   1221 
   1222 void
   1223 ada_value_print (struct value *val0, struct ui_file *stream,
   1224 		 const struct value_print_options *options)
   1225 {
   1226   struct value *val = ada_to_fixed_value (val0);
   1227   CORE_ADDR address = value_address (val);
   1228   struct type *type = ada_check_typedef (value_type (val));
   1229   struct value_print_options opts;
   1230 
   1231   /* If it is a pointer, indicate what it points to.  */
   1232   if (TYPE_CODE (type) == TYPE_CODE_PTR)
   1233     {
   1234       /* Hack:  don't print (char *) for char strings.  Their
   1235          type is indicated by the quoted string anyway.  */
   1236       if (TYPE_LENGTH (TYPE_TARGET_TYPE (type)) != sizeof (char)
   1237 	  || TYPE_CODE (TYPE_TARGET_TYPE (type)) != TYPE_CODE_INT
   1238 	  || TYPE_UNSIGNED (TYPE_TARGET_TYPE (type)))
   1239 	{
   1240 	  fprintf_filtered (stream, "(");
   1241 	  type_print (type, "", stream, -1);
   1242 	  fprintf_filtered (stream, ") ");
   1243 	}
   1244     }
   1245   else if (ada_is_array_descriptor_type (type))
   1246     {
   1247       /* We do not print the type description unless TYPE is an array
   1248 	 access type (this is encoded by the compiler as a typedef to
   1249 	 a fat pointer - hence the check against TYPE_CODE_TYPEDEF).  */
   1250       if (TYPE_CODE (type) == TYPE_CODE_TYPEDEF)
   1251         {
   1252 	  fprintf_filtered (stream, "(");
   1253 	  type_print (type, "", stream, -1);
   1254 	  fprintf_filtered (stream, ") ");
   1255 	}
   1256     }
   1257   else if (ada_is_bogus_array_descriptor (type))
   1258     {
   1259       fprintf_filtered (stream, "(");
   1260       type_print (type, "", stream, -1);
   1261       fprintf_filtered (stream, ") (...?)");
   1262       return;
   1263     }
   1264 
   1265   opts = *options;
   1266   opts.deref_ref = 1;
   1267   val_print (type,
   1268 	     value_embedded_offset (val), address,
   1269 	     stream, 0, val, &opts, current_language);
   1270 }
   1271