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