Home | History | Annotate | Line # | Download | only in gdb
ada-valprint.c revision 1.11
      1 /* Support for printing Ada values for GDB, the GNU debugger.
      2 
      3    Copyright (C) 1986-2024 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 <ctype.h>
     21 #include "event-top.h"
     22 #include "extract-store-integer.h"
     23 #include "gdbtypes.h"
     24 #include "expression.h"
     25 #include "value.h"
     26 #include "valprint.h"
     27 #include "language.h"
     28 #include "annotate.h"
     29 #include "ada-lang.h"
     30 #include "target-float.h"
     31 #include "cli/cli-style.h"
     32 #include "gdbarch.h"
     33 
     34 static int print_field_values (struct value *, struct value *,
     35 			       struct ui_file *, int,
     36 			       const struct value_print_options *,
     37 			       int, const struct language_defn *);
     38 
     39 
     40 
     42 /* Assuming TYPE is a simple array type, prints its lower bound on STREAM,
     43    if non-standard (i.e., other than 1 for numbers, other than lower bound
     44    of index type for enumerated type).  Returns 1 if something printed,
     45    otherwise 0.  */
     46 
     47 static int
     48 print_optional_low_bound (struct ui_file *stream, struct type *type,
     49 			  const struct value_print_options *options)
     50 {
     51   struct type *index_type;
     52   LONGEST low_bound;
     53   LONGEST high_bound;
     54 
     55   if (options->print_array_indexes)
     56     return 0;
     57 
     58   if (!get_array_bounds (type, &low_bound, &high_bound))
     59     return 0;
     60 
     61   /* If this is an empty array, then don't print the lower bound.
     62      That would be confusing, because we would print the lower bound,
     63      followed by... nothing!  */
     64   if (low_bound > high_bound)
     65     return 0;
     66 
     67   index_type = type->index_type ();
     68 
     69   while (index_type->code () == TYPE_CODE_RANGE)
     70     {
     71       /* We need to know what the base type is, in order to do the
     72 	 appropriate check below.  Otherwise, if this is a subrange
     73 	 of an enumerated type, where the underlying value of the
     74 	 first element is typically 0, we might test the low bound
     75 	 against the wrong value.  */
     76       index_type = index_type->target_type ();
     77     }
     78 
     79   /* Don't print the lower bound if it's the default one.  */
     80   switch (index_type->code ())
     81     {
     82     case TYPE_CODE_BOOL:
     83     case TYPE_CODE_CHAR:
     84       if (low_bound == 0)
     85 	return 0;
     86       break;
     87     case TYPE_CODE_ENUM:
     88       if (low_bound == 0)
     89 	return 0;
     90       low_bound = index_type->field (low_bound).loc_enumval ();
     91       break;
     92     case TYPE_CODE_UNDEF:
     93       index_type = NULL;
     94       [[fallthrough]];
     95     default:
     96       if (low_bound == 1)
     97 	return 0;
     98       break;
     99     }
    100 
    101   ada_print_scalar (index_type, low_bound, stream);
    102   gdb_printf (stream, " => ");
    103   return 1;
    104 }
    105 
    106 /*  Version of val_print_array_elements for GNAT-style packed arrays.
    107     Prints elements of packed array of type TYPE from VALADDR on
    108     STREAM.  Formats according to OPTIONS and separates with commas.
    109     RECURSE is the recursion (nesting) level.  TYPE must have been
    110     decoded (as by ada_coerce_to_simple_array).  */
    111 
    112 static void
    113 val_print_packed_array_elements (struct type *type, const gdb_byte *valaddr,
    114 				 int offset, struct ui_file *stream,
    115 				 int recurse,
    116 				 const struct value_print_options *options)
    117 {
    118   unsigned int i;
    119   unsigned int things_printed = 0;
    120   unsigned len;
    121   struct type *elttype, *index_type;
    122   unsigned long bitsize = type->field (0).bitsize ();
    123   LONGEST low = 0;
    124 
    125   scoped_value_mark mark;
    126 
    127   elttype = type->target_type ();
    128   index_type = type->index_type ();
    129 
    130   {
    131     LONGEST high;
    132 
    133     if (!get_discrete_bounds (index_type, &low, &high))
    134       len = 1;
    135     else if (low > high)
    136       {
    137 	/* The array length should normally be HIGH_POS - LOW_POS + 1.
    138 	   But in Ada we allow LOW_POS to be greater than HIGH_POS for
    139 	   empty arrays.  In that situation, the array length is just zero,
    140 	   not negative!  */
    141 	len = 0;
    142       }
    143     else
    144       len = high - low + 1;
    145   }
    146 
    147   if (index_type->code () == TYPE_CODE_RANGE)
    148     index_type = index_type->target_type ();
    149 
    150   i = 0;
    151   annotate_array_section_begin (i, elttype);
    152 
    153   while (i < len && things_printed < options->print_max)
    154     {
    155       /* Both this outer loop and the inner loop that checks for
    156 	 duplicates may allocate many values.  To avoid using too much
    157 	 memory, both spots release values as they work.  */
    158       scoped_value_mark outer_free_values;
    159 
    160       struct value *v0, *v1;
    161       int i0;
    162 
    163       if (i != 0)
    164 	{
    165 	  if (options->prettyformat_arrays)
    166 	    {
    167 	      gdb_printf (stream, ",\n");
    168 	      print_spaces (2 + 2 * recurse, stream);
    169 	    }
    170 	  else
    171 	    {
    172 	      gdb_printf (stream, ", ");
    173 	    }
    174 	}
    175       else if (options->prettyformat_arrays)
    176 	{
    177 	  gdb_printf (stream, "\n");
    178 	  print_spaces (2 + 2 * recurse, stream);
    179 	}
    180       stream->wrap_here (2 + 2 * recurse);
    181       maybe_print_array_index (index_type, i + low, stream, options);
    182 
    183       i0 = i;
    184       v0 = ada_value_primitive_packed_val (NULL, valaddr + offset,
    185 					   (i0 * bitsize) / HOST_CHAR_BIT,
    186 					   (i0 * bitsize) % HOST_CHAR_BIT,
    187 					   bitsize, elttype);
    188       while (1)
    189 	{
    190 	  /* Make sure to free any values in the inner loop.  */
    191 	  scoped_value_mark free_values;
    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 (v0->type ())->length ()
    201 	      != check_typedef (v1->type ())->length ())
    202 	    break;
    203 	  if (!v0->contents_eq (v0->embedded_offset (),
    204 				v1, v1->embedded_offset (),
    205 				check_typedef (v0->type ())->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 = false;
    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 = false;
    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 ((unsigned char)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   if (!type)
    378     {
    379       print_longest (stream, 'd', 0, val);
    380       return;
    381     }
    382 
    383   type = ada_check_typedef (type);
    384 
    385   switch (type->code ())
    386     {
    387 
    388     case TYPE_CODE_ENUM:
    389       {
    390 	std::optional<LONGEST> posn = discrete_position (type, val);
    391 	if (posn.has_value ())
    392 	  fputs_styled (ada_enum_name (type->field (*posn).name ()),
    393 			variable_name_style.style (), stream);
    394 	else
    395 	  print_longest (stream, 'd', 0, val);
    396       }
    397       break;
    398 
    399     case TYPE_CODE_INT:
    400       print_longest (stream, type->is_unsigned () ? 'u' : 'd', 0, val);
    401       break;
    402 
    403     case TYPE_CODE_CHAR:
    404       current_language->printchar (val, type, stream);
    405       break;
    406 
    407     case TYPE_CODE_BOOL:
    408       gdb_printf (stream, val ? "true" : "false");
    409       break;
    410 
    411     case TYPE_CODE_RANGE:
    412       ada_print_scalar (type->target_type (), val, stream);
    413       return;
    414 
    415     case TYPE_CODE_UNDEF:
    416     case TYPE_CODE_PTR:
    417     case TYPE_CODE_ARRAY:
    418     case TYPE_CODE_STRUCT:
    419     case TYPE_CODE_UNION:
    420     case TYPE_CODE_FUNC:
    421     case TYPE_CODE_FLT:
    422     case TYPE_CODE_VOID:
    423     case TYPE_CODE_SET:
    424     case TYPE_CODE_STRING:
    425     case TYPE_CODE_ERROR:
    426     case TYPE_CODE_MEMBERPTR:
    427     case TYPE_CODE_METHODPTR:
    428     case TYPE_CODE_METHOD:
    429     case TYPE_CODE_REF:
    430       warning (_("internal error: unhandled type in ada_print_scalar"));
    431       break;
    432 
    433     default:
    434       error (_("Invalid type code in symbol table."));
    435     }
    436 }
    437 
    438 /* Print the character string STRING, printing at most LENGTH characters.
    439    Printing stops early if the number hits print_max; repeat counts
    440    are printed as appropriate.  Print ellipses at the end if we
    441    had to stop before printing LENGTH characters, or if FORCE_ELLIPSES.
    442    TYPE_LEN is the length (1 or 2) of the character type.  */
    443 
    444 static void
    445 printstr (struct ui_file *stream, struct type *elttype, const gdb_byte *string,
    446 	  unsigned int length, int force_ellipses, int type_len,
    447 	  const struct value_print_options *options)
    448 {
    449   enum bfd_endian byte_order = type_byte_order (elttype);
    450   unsigned int i;
    451   unsigned int things_printed = 0;
    452   int in_quotes = 0;
    453   int need_comma = 0;
    454 
    455   if (length == 0)
    456     {
    457       gdb_puts ("\"\"", stream);
    458       return;
    459     }
    460 
    461   unsigned int print_max_chars = get_print_max_chars (options);
    462   for (i = 0; i < length && things_printed < print_max_chars; i += 1)
    463     {
    464       /* Position of the character we are examining
    465 	 to see whether it is repeated.  */
    466       unsigned int rep1;
    467       /* Number of repetitions we have detected so far.  */
    468       unsigned int reps;
    469 
    470       QUIT;
    471 
    472       if (need_comma)
    473 	{
    474 	  gdb_puts (", ", stream);
    475 	  need_comma = 0;
    476 	}
    477 
    478       rep1 = i + 1;
    479       reps = 1;
    480       while (rep1 < length
    481 	     && char_at (string, rep1, type_len, byte_order)
    482 		== char_at (string, i, type_len, byte_order))
    483 	{
    484 	  rep1 += 1;
    485 	  reps += 1;
    486 	}
    487 
    488       if (reps > options->repeat_count_threshold)
    489 	{
    490 	  if (in_quotes)
    491 	    {
    492 	      gdb_puts ("\", ", stream);
    493 	      in_quotes = 0;
    494 	    }
    495 	  gdb_puts ("'", stream);
    496 	  ada_emit_char (char_at (string, i, type_len, byte_order),
    497 			 elttype, stream, '\'', type_len);
    498 	  gdb_puts ("'", stream);
    499 	  gdb_printf (stream, _(" %p[<repeats %u times>%p]"),
    500 		      metadata_style.style ().ptr (), reps, nullptr);
    501 	  i = rep1 - 1;
    502 	  things_printed += options->repeat_count_threshold;
    503 	  need_comma = 1;
    504 	}
    505       else
    506 	{
    507 	  if (!in_quotes)
    508 	    {
    509 	      gdb_puts ("\"", stream);
    510 	      in_quotes = 1;
    511 	    }
    512 	  ada_emit_char (char_at (string, i, type_len, byte_order),
    513 			 elttype, stream, '"', type_len);
    514 	  things_printed += 1;
    515 	}
    516     }
    517 
    518   /* Terminate the quotes if necessary.  */
    519   if (in_quotes)
    520     gdb_puts ("\"", stream);
    521 
    522   if (force_ellipses || i < length)
    523     gdb_puts ("...", stream);
    524 }
    525 
    526 void
    527 ada_printstr (struct ui_file *stream, struct type *type,
    528 	      const gdb_byte *string, unsigned int length,
    529 	      const char *encoding, int force_ellipses,
    530 	      const struct value_print_options *options)
    531 {
    532   printstr (stream, type, string, length, force_ellipses, type->length (),
    533 	    options);
    534 }
    535 
    536 static int
    537 print_variant_part (struct value *value, int field_num,
    538 		    struct value *outer_value,
    539 		    struct ui_file *stream, int recurse,
    540 		    const struct value_print_options *options,
    541 		    int comma_needed,
    542 		    const struct language_defn *language)
    543 {
    544   struct type *type = value->type ();
    545   struct type *var_type = type->field (field_num).type ();
    546   int which = ada_which_variant_applies (var_type, outer_value);
    547 
    548   if (which < 0)
    549     return 0;
    550 
    551   struct value *variant_field = value_field (value, field_num);
    552   struct value *active_component = value_field (variant_field, which);
    553   return print_field_values (active_component, outer_value, stream, recurse,
    554 			     options, comma_needed, language);
    555 }
    556 
    557 /* Print out fields of VALUE.
    558 
    559    STREAM, RECURSE, and OPTIONS have the same meanings as in
    560    ada_print_value and ada_value_print.
    561 
    562    OUTER_VALUE gives the enclosing record (used to get discriminant
    563    values when printing variant parts).
    564 
    565    COMMA_NEEDED is 1 if fields have been printed at the current recursion
    566    level, so that a comma is needed before any field printed by this
    567    call.
    568 
    569    Returns 1 if COMMA_NEEDED or any fields were printed.  */
    570 
    571 static int
    572 print_field_values (struct value *value, struct value *outer_value,
    573 		    struct ui_file *stream, int recurse,
    574 		    const struct value_print_options *options,
    575 		    int comma_needed,
    576 		    const struct language_defn *language)
    577 {
    578   int i, len;
    579 
    580   struct type *type = value->type ();
    581   len = type->num_fields ();
    582 
    583   for (i = 0; i < len; i += 1)
    584     {
    585       if (ada_is_ignored_field (type, i))
    586 	continue;
    587 
    588       if (ada_is_wrapper_field (type, i))
    589 	{
    590 	  struct value *field_val = ada_value_primitive_field (value, 0,
    591 							       i, type);
    592 	  comma_needed =
    593 	    print_field_values (field_val, field_val,
    594 				stream, recurse, options,
    595 				comma_needed, language);
    596 	  continue;
    597 	}
    598       else if (ada_is_variant_part (type, i))
    599 	{
    600 	  comma_needed =
    601 	    print_variant_part (value, i, outer_value, stream, recurse,
    602 				options, comma_needed, language);
    603 	  continue;
    604 	}
    605 
    606       if (comma_needed)
    607 	gdb_printf (stream, ", ");
    608       comma_needed = 1;
    609 
    610       if (options->prettyformat)
    611 	{
    612 	  gdb_printf (stream, "\n");
    613 	  print_spaces (2 + 2 * recurse, stream);
    614 	}
    615       else
    616 	{
    617 	  stream->wrap_here (2 + 2 * recurse);
    618 	}
    619 
    620       annotate_field_begin (type->field (i).type ());
    621       gdb_printf (stream, "%.*s",
    622 		  ada_name_prefix_len (type->field (i).name ()),
    623 		  type->field (i).name ());
    624       annotate_field_name_end ();
    625       gdb_puts (" => ", stream);
    626       annotate_field_value ();
    627 
    628       if (type->field (i).is_packed ())
    629 	{
    630 	  /* Bitfields require special handling, especially due to byte
    631 	     order problems.  */
    632 	  if (type->field (i).is_ignored ())
    633 	    {
    634 	      fputs_styled (_("<optimized out or zero length>"),
    635 			    metadata_style.style (), stream);
    636 	    }
    637 	  else
    638 	    {
    639 	      struct value *v;
    640 	      int bit_pos = type->field (i).loc_bitpos ();
    641 	      int bit_size = type->field (i).bitsize ();
    642 	      struct value_print_options opts;
    643 
    644 	      v = ada_value_primitive_packed_val
    645 		    (value, nullptr,
    646 		     bit_pos / HOST_CHAR_BIT,
    647 		     bit_pos % HOST_CHAR_BIT,
    648 		     bit_size, type->field (i).type ());
    649 	      opts = *options;
    650 	      opts.deref_ref = false;
    651 	      common_val_print (v, stream, recurse + 1, &opts, language);
    652 	    }
    653 	}
    654       else
    655 	{
    656 	  struct value_print_options opts = *options;
    657 
    658 	  opts.deref_ref = false;
    659 
    660 	  struct value *v = value_field (value, i);
    661 	  common_val_print (v, stream, recurse + 1, &opts, language);
    662 	}
    663       annotate_field_end ();
    664     }
    665 
    666   return comma_needed;
    667 }
    668 
    669 /* Implement Ada val_print'ing for the case where TYPE is
    670    a TYPE_CODE_ARRAY of characters.  */
    671 
    672 static void
    673 ada_val_print_string (struct type *type, const gdb_byte *valaddr,
    674 		      int offset_aligned,
    675 		      struct ui_file *stream, int recurse,
    676 		      const struct value_print_options *options)
    677 {
    678   enum bfd_endian byte_order = type_byte_order (type);
    679   struct type *elttype = type->target_type ();
    680   unsigned int eltlen;
    681   unsigned int len;
    682 
    683   /* We know that ELTTYPE cannot possibly be null, because we assume
    684      that we're called only when TYPE is a string-like type.
    685      Similarly, the size of ELTTYPE should also be non-null, since
    686      it's a character-like type.  */
    687   gdb_assert (elttype != NULL);
    688   gdb_assert (elttype->length () != 0);
    689 
    690   eltlen = elttype->length ();
    691   len = type->length () / eltlen;
    692 
    693   /* If requested, look for the first null char and only print
    694      elements up to it.  */
    695   if (options->stop_print_at_null)
    696     {
    697       unsigned int print_max_chars = get_print_max_chars (options);
    698       int temp_len;
    699 
    700       /* Look for a NULL char.  */
    701       for (temp_len = 0;
    702 	   (temp_len < len
    703 	    && temp_len < print_max_chars
    704 	    && char_at (valaddr + offset_aligned,
    705 			temp_len, eltlen, byte_order) != 0);
    706 	   temp_len += 1);
    707       len = temp_len;
    708     }
    709 
    710   printstr (stream, elttype, valaddr + offset_aligned, len, 0,
    711 	    eltlen, options);
    712 }
    713 
    714 /* Implement Ada value_print'ing for the case where TYPE is a
    715    TYPE_CODE_PTR.  */
    716 
    717 static void
    718 ada_value_print_ptr (struct value *val,
    719 		     struct ui_file *stream, int recurse,
    720 		     const struct value_print_options *options)
    721 {
    722   if (!options->format
    723       && val->type ()->target_type ()->code () == TYPE_CODE_INT
    724       && val->type ()->target_type ()->length () == 0)
    725     {
    726       gdb_puts ("null", stream);
    727       return;
    728     }
    729 
    730   common_val_print (val, stream, recurse, options, language_def (language_c));
    731 
    732   struct type *type = ada_check_typedef (val->type ());
    733   if (ada_is_tag_type (type))
    734     {
    735       gdb::unique_xmalloc_ptr<char> name = ada_tag_name (val);
    736 
    737       if (name != NULL)
    738 	gdb_printf (stream, " (%s)", name.get ());
    739     }
    740 }
    741 
    742 /* Implement Ada val_print'ing for the case where TYPE is
    743    a TYPE_CODE_INT or TYPE_CODE_RANGE.  */
    744 
    745 static void
    746 ada_value_print_num (struct value *val, struct ui_file *stream, int recurse,
    747 		     const struct value_print_options *options)
    748 {
    749   struct type *type = ada_check_typedef (val->type ());
    750   const gdb_byte *valaddr = val->contents_for_printing ().data ();
    751 
    752   if (type->code () == TYPE_CODE_RANGE
    753       && (type->target_type ()->code () == TYPE_CODE_ENUM
    754 	  || type->target_type ()->code () == TYPE_CODE_BOOL
    755 	  || type->target_type ()->code () == TYPE_CODE_CHAR))
    756     {
    757       /* For enum-valued ranges, we want to recurse, because we'll end
    758 	 up printing the constant's name rather than its numeric
    759 	 value.  Character and fixed-point types are also printed
    760 	 differently, so recurse for those as well.  */
    761       struct type *target_type = type->target_type ();
    762       val = value_cast (target_type, val);
    763       common_val_print (val, stream, recurse + 1, options,
    764 			language_def (language_ada));
    765       return;
    766     }
    767   else
    768     {
    769       int format = (options->format ? options->format
    770 		    : options->output_format);
    771 
    772       if (format)
    773 	{
    774 	  struct value_print_options opts = *options;
    775 
    776 	  opts.format = format;
    777 	  value_print_scalar_formatted (val, &opts, 0, stream);
    778 	}
    779       else if (ada_is_system_address_type (type))
    780 	{
    781 	  /* FIXME: We want to print System.Address variables using
    782 	     the same format as for any access type.  But for some
    783 	     reason GNAT encodes the System.Address type as an int,
    784 	     so we have to work-around this deficiency by handling
    785 	     System.Address values as a special case.  */
    786 
    787 	  struct gdbarch *gdbarch = type->arch ();
    788 	  struct type *ptr_type = builtin_type (gdbarch)->builtin_data_ptr;
    789 	  CORE_ADDR addr = extract_typed_address (valaddr, ptr_type);
    790 
    791 	  gdb_printf (stream, "(");
    792 	  type_print (type, "", stream, -1);
    793 	  gdb_printf (stream, ") ");
    794 	  gdb_puts (paddress (gdbarch, addr), stream);
    795 	}
    796       else
    797 	{
    798 	  value_print_scalar_formatted (val, options, 0, stream);
    799 	  if (ada_is_character_type (type))
    800 	    {
    801 	      LONGEST c;
    802 
    803 	      gdb_puts (" ", stream);
    804 	      c = unpack_long (type, valaddr);
    805 	      ada_printchar (c, type, stream);
    806 	    }
    807 	}
    808       return;
    809     }
    810 }
    811 
    812 /* Implement Ada val_print'ing for the case where TYPE is
    813    a TYPE_CODE_ENUM.  */
    814 
    815 static void
    816 ada_val_print_enum (struct value *value, struct ui_file *stream, int recurse,
    817 		    const struct value_print_options *options)
    818 {
    819   LONGEST val;
    820 
    821   if (options->format)
    822     {
    823       value_print_scalar_formatted (value, options, 0, stream);
    824       return;
    825     }
    826 
    827   struct type *type = ada_check_typedef (value->type ());
    828   const gdb_byte *valaddr = value->contents_for_printing ().data ();
    829   int offset_aligned = ada_aligned_value_addr (type, valaddr) - valaddr;
    830 
    831   val = unpack_long (type, valaddr + offset_aligned);
    832   std::optional<LONGEST> posn = discrete_position (type, val);
    833   if (posn.has_value ())
    834     {
    835       const char *name = ada_enum_name (type->field (*posn).name ());
    836 
    837       if (name[0] == '\'')
    838 	gdb_printf (stream, "%ld %ps", (long) val,
    839 		    styled_string (variable_name_style.style (),
    840 				   name));
    841       else
    842 	fputs_styled (name, variable_name_style.style (), stream);
    843     }
    844   else
    845     print_longest (stream, 'd', 0, val);
    846 }
    847 
    848 /* Implement Ada val_print'ing for the case where the type is
    849    TYPE_CODE_STRUCT or TYPE_CODE_UNION.  */
    850 
    851 static void
    852 ada_val_print_struct_union (struct value *value,
    853 			    struct ui_file *stream,
    854 			    int recurse,
    855 			    const struct value_print_options *options)
    856 {
    857   gdb_printf (stream, "(");
    858 
    859   if (print_field_values (value, value, stream, recurse, options,
    860 			  0, language_def (language_ada)) != 0
    861       && options->prettyformat)
    862     {
    863       gdb_printf (stream, "\n");
    864       print_spaces (2 * recurse, stream);
    865     }
    866 
    867   gdb_printf (stream, ")");
    868 }
    869 
    870 /* Implement Ada value_print'ing for the case where TYPE is a
    871    TYPE_CODE_ARRAY.  */
    872 
    873 static void
    874 ada_value_print_array (struct value *val, struct ui_file *stream, int recurse,
    875 		       const struct value_print_options *options)
    876 {
    877   struct type *type = ada_check_typedef (val->type ());
    878 
    879   /* For an array of characters, print with string syntax.  */
    880   if (ada_is_string_type (type)
    881       && (options->format == 0 || options->format == 's'))
    882     {
    883       const gdb_byte *valaddr = val->contents_for_printing ().data ();
    884       int offset_aligned = ada_aligned_value_addr (type, valaddr) - valaddr;
    885 
    886       ada_val_print_string (type, valaddr, offset_aligned, stream, recurse,
    887 			    options);
    888       return;
    889     }
    890 
    891   gdb_printf (stream, "(");
    892   print_optional_low_bound (stream, type, options);
    893 
    894   if (val->entirely_optimized_out ())
    895     val_print_optimized_out (val, stream);
    896   else if (type->field (0).bitsize () > 0)
    897     {
    898       const gdb_byte *valaddr = val->contents_for_printing ().data ();
    899       int offset_aligned = ada_aligned_value_addr (type, valaddr) - valaddr;
    900       val_print_packed_array_elements (type, valaddr, offset_aligned,
    901 				       stream, recurse, options);
    902     }
    903   else
    904     value_print_array_elements (val, stream, recurse, options, 0);
    905   gdb_printf (stream, ")");
    906 }
    907 
    908 /* Implement Ada val_print'ing for the case where TYPE is
    909    a TYPE_CODE_REF.  */
    910 
    911 static void
    912 ada_val_print_ref (struct type *type, const gdb_byte *valaddr,
    913 		   int offset, int offset_aligned, CORE_ADDR address,
    914 		   struct ui_file *stream, int recurse,
    915 		   struct value *original_value,
    916 		   const struct value_print_options *options)
    917 {
    918   /* For references, the debugger is expected to print the value as
    919      an address if DEREF_REF is null.  But printing an address in place
    920      of the object value would be confusing to an Ada programmer.
    921      So, for Ada values, we print the actual dereferenced value
    922      regardless.  */
    923   struct type *elttype = check_typedef (type->target_type ());
    924   struct value *deref_val;
    925   CORE_ADDR deref_val_int;
    926 
    927   if (elttype->code () == TYPE_CODE_UNDEF)
    928     {
    929       fputs_styled ("<ref to undefined type>", metadata_style.style (),
    930 		    stream);
    931       return;
    932     }
    933 
    934   deref_val = coerce_ref_if_computed (original_value);
    935   if (deref_val)
    936     {
    937       if (ada_is_tagged_type (deref_val->type (), 1))
    938 	deref_val = ada_tag_value_at_base_address (deref_val);
    939 
    940       common_val_print (deref_val, stream, recurse + 1, options,
    941 			language_def (language_ada));
    942       return;
    943     }
    944 
    945   deref_val_int = unpack_pointer (type, valaddr + offset_aligned);
    946   if (deref_val_int == 0)
    947     {
    948       gdb_puts ("(null)", stream);
    949       return;
    950     }
    951 
    952   deref_val
    953     = ada_value_ind (value_from_pointer (lookup_pointer_type (elttype),
    954 					 deref_val_int));
    955   if (ada_is_tagged_type (deref_val->type (), 1))
    956     deref_val = ada_tag_value_at_base_address (deref_val);
    957 
    958   if (deref_val->lazy ())
    959     deref_val->fetch_lazy ();
    960 
    961   common_val_print (deref_val, stream, recurse + 1,
    962 		    options, language_def (language_ada));
    963 }
    964 
    965 /* See the comment on ada_value_print.  This function differs in that
    966    it does not catch evaluation errors (leaving that to its
    967    caller).  */
    968 
    969 void
    970 ada_value_print_inner (struct value *val, struct ui_file *stream, int recurse,
    971 		       const struct value_print_options *options)
    972 {
    973   struct type *type = ada_check_typedef (val->type ());
    974 
    975   if (ada_is_array_descriptor_type (type)
    976       || (ada_is_constrained_packed_array_type (type)
    977 	  && type->code () != TYPE_CODE_PTR))
    978     {
    979       /* If this is a reference, coerce it now.  This helps taking
    980 	 care of the case where ADDRESS is meaningless because
    981 	 original_value was not an lval.  */
    982       val = coerce_ref (val);
    983       val = ada_get_decoded_value (val);
    984       if (val == nullptr)
    985 	{
    986 	  gdb_assert (type->code () == TYPE_CODE_TYPEDEF);
    987 	  gdb_printf (stream, "0x0");
    988 	  return;
    989 	}
    990     }
    991   else
    992     val = ada_to_fixed_value (val);
    993 
    994   type = val->type ();
    995   struct type *saved_type = type;
    996 
    997   const gdb_byte *valaddr = val->contents_for_printing ().data ();
    998   CORE_ADDR address = val->address ();
    999   gdb::array_view<const gdb_byte> view
   1000     = gdb::make_array_view (valaddr, type->length ());
   1001   type = ada_check_typedef (resolve_dynamic_type (type, view, address));
   1002   if (type != saved_type)
   1003     {
   1004       val = val->copy ();
   1005       val->deprecated_set_type (type);
   1006     }
   1007 
   1008   if (is_fixed_point_type (type))
   1009     type = type->fixed_point_type_base_type ();
   1010 
   1011   switch (type->code ())
   1012     {
   1013     default:
   1014       common_val_print (val, stream, recurse, options,
   1015 			language_def (language_c));
   1016       break;
   1017 
   1018     case TYPE_CODE_PTR:
   1019       ada_value_print_ptr (val, stream, recurse, options);
   1020       break;
   1021 
   1022     case TYPE_CODE_INT:
   1023     case TYPE_CODE_RANGE:
   1024       ada_value_print_num (val, stream, recurse, options);
   1025       break;
   1026 
   1027     case TYPE_CODE_ENUM:
   1028       ada_val_print_enum (val, stream, recurse, options);
   1029       break;
   1030 
   1031     case TYPE_CODE_FLT:
   1032       if (options->format)
   1033 	{
   1034 	  common_val_print (val, stream, recurse, options,
   1035 			    language_def (language_c));
   1036 	  break;
   1037 	}
   1038 
   1039       ada_print_floating (valaddr, type, stream);
   1040       break;
   1041 
   1042     case TYPE_CODE_UNION:
   1043     case TYPE_CODE_STRUCT:
   1044       ada_val_print_struct_union (val, stream, recurse, options);
   1045       break;
   1046 
   1047     case TYPE_CODE_ARRAY:
   1048       ada_value_print_array (val, stream, recurse, options);
   1049       return;
   1050 
   1051     case TYPE_CODE_REF:
   1052       ada_val_print_ref (type, valaddr, 0, 0,
   1053 			 address, stream, recurse, val,
   1054 			 options);
   1055       break;
   1056     }
   1057 }
   1058 
   1059 void
   1060 ada_value_print (struct value *val0, struct ui_file *stream,
   1061 		 const struct value_print_options *options)
   1062 {
   1063   struct value *val = ada_to_fixed_value (val0);
   1064   struct type *type = ada_check_typedef (val->type ());
   1065   struct value_print_options opts;
   1066 
   1067   /* If it is a pointer, indicate what it points to; but not for
   1068      "void *" pointers.  */
   1069   if (type->code () == TYPE_CODE_PTR
   1070       && !(type->target_type ()->code () == TYPE_CODE_INT
   1071 	   && type->target_type ()->length () == 0))
   1072     {
   1073       /* Hack:  don't print (char *) for char strings.  Their
   1074 	 type is indicated by the quoted string anyway.  */
   1075       if (type->target_type ()->length () != sizeof (char)
   1076 	  || type->target_type ()->code () != TYPE_CODE_INT
   1077 	  || type->target_type ()->is_unsigned ())
   1078 	{
   1079 	  gdb_printf (stream, "(");
   1080 	  type_print (type, "", stream, -1);
   1081 	  gdb_printf (stream, ") ");
   1082 	}
   1083     }
   1084   else if (ada_is_array_descriptor_type (type))
   1085     {
   1086       /* We do not print the type description unless TYPE is an array
   1087 	 access type (this is encoded by the compiler as a typedef to
   1088 	 a fat pointer - hence the check against TYPE_CODE_TYPEDEF).  */
   1089       if (type->code () == TYPE_CODE_TYPEDEF)
   1090 	{
   1091 	  gdb_printf (stream, "(");
   1092 	  type_print (type, "", stream, -1);
   1093 	  gdb_printf (stream, ") ");
   1094 	}
   1095     }
   1096 
   1097   opts = *options;
   1098   opts.deref_ref = true;
   1099   common_val_print (val, stream, 0, &opts, current_language);
   1100 }
   1101