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