Automatic date update in version.in
[binutils-gdb.git] / gdb / ada-typeprint.c
blobdeead1055dbf4859e3bf930612d4d20dc15baf0a
1 /* Support for printing Ada types for GDB, the GNU debugger.
2 Copyright (C) 1986-2024 Free Software Foundation, Inc.
4 This file is part of GDB.
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <http://www.gnu.org/licenses/>. */
19 #include "bfd.h"
20 #include "gdbtypes.h"
21 #include "value.h"
22 #include "c-lang.h"
23 #include "cli/cli-style.h"
24 #include "typeprint.h"
25 #include "target-float.h"
26 #include "ada-lang.h"
27 #include <ctype.h>
29 static int print_selected_record_field_types (struct type *, struct type *,
30 int, int,
31 struct ui_file *, int, int,
32 const struct type_print_options *);
34 static int print_record_field_types (struct type *, struct type *,
35 struct ui_file *, int, int,
36 const struct type_print_options *);
40 static char *name_buffer;
41 static int name_buffer_len;
43 /* The (decoded) Ada name of TYPE. This value persists until the
44 next call. */
46 static char *
47 decoded_type_name (struct type *type)
49 if (ada_type_name (type) == NULL)
50 return NULL;
51 else
53 const char *raw_name = ada_type_name (type);
54 char *s, *q;
56 if (name_buffer == NULL || name_buffer_len <= strlen (raw_name))
58 name_buffer_len = 16 + 2 * strlen (raw_name);
59 name_buffer = (char *) xrealloc (name_buffer, name_buffer_len);
61 strcpy (name_buffer, raw_name);
63 s = (char *) strstr (name_buffer, "___");
64 if (s != NULL)
65 *s = '\0';
67 s = name_buffer + strlen (name_buffer) - 1;
68 while (s > name_buffer && (s[0] != '_' || s[-1] != '_'))
69 s -= 1;
71 if (s == name_buffer)
72 return name_buffer;
74 if (!islower (s[1]))
75 return NULL;
77 for (s = q = name_buffer; *s != '\0'; q += 1)
79 if (s[0] == '_' && s[1] == '_')
81 *q = '.';
82 s += 2;
84 else
86 *q = *s;
87 s += 1;
90 *q = '\0';
91 return name_buffer;
95 /* Return nonzero if TYPE is a subrange type, and its bounds
96 are identical to the bounds of its subtype. */
98 static int
99 type_is_full_subrange_of_target_type (struct type *type)
101 struct type *subtype;
103 if (type->code () != TYPE_CODE_RANGE)
104 return 0;
106 subtype = type->target_type ();
107 if (subtype == NULL)
108 return 0;
110 if (is_dynamic_type (type))
111 return 0;
113 if (ada_discrete_type_low_bound (type)
114 != ada_discrete_type_low_bound (subtype))
115 return 0;
117 if (ada_discrete_type_high_bound (type)
118 != ada_discrete_type_high_bound (subtype))
119 return 0;
121 return 1;
124 /* Print TYPE on STREAM, preferably as a range if BOUNDS_PREFERRED_P
125 is nonzero. */
127 static void
128 print_range (struct type *type, struct ui_file *stream,
129 int bounds_preferred_p)
131 if (!bounds_preferred_p)
133 /* Try stripping all TYPE_CODE_RANGE layers whose bounds
134 are identical to the bounds of their subtype. When
135 the bounds of both types match, it can allow us to
136 print a range using the name of its base type, which
137 is easier to read. For instance, we would print...
139 array (character) of ...
141 ... instead of...
143 array ('["00"]' .. '["ff"]') of ... */
144 while (type_is_full_subrange_of_target_type (type))
145 type = type->target_type ();
148 switch (type->code ())
150 case TYPE_CODE_RANGE:
151 case TYPE_CODE_ENUM:
153 LONGEST lo = 0, hi = 0; /* init for gcc -Wall */
154 int got_error = 0;
158 lo = ada_discrete_type_low_bound (type);
159 hi = ada_discrete_type_high_bound (type);
161 catch (const gdb_exception_error &e)
163 /* This can happen when the range is dynamic. Sometimes,
164 resolving dynamic property values requires us to have
165 access to an actual object, which is not available
166 when the user is using the "ptype" command on a type.
167 Print the range as an unbounded range. */
168 gdb_printf (stream, "<>");
169 got_error = 1;
172 if (!got_error)
174 ada_print_scalar (type, lo, stream);
175 gdb_printf (stream, " .. ");
176 ada_print_scalar (type, hi, stream);
179 break;
180 default:
181 gdb_printf (stream, "%.*s",
182 ada_name_prefix_len (type->name ()),
183 type->name ());
184 break;
188 /* Print the number or discriminant bound at BOUNDS+*N on STREAM, and
189 set *N past the bound and its delimiter, if any. */
191 static void
192 print_range_bound (struct type *type, const char *bounds, int *n,
193 struct ui_file *stream)
195 LONGEST B;
197 if (ada_scan_number (bounds, *n, &B, n))
199 /* STABS decodes all range types which bounds are 0 .. -1 as
200 unsigned integers (ie. the type code is TYPE_CODE_INT, not
201 TYPE_CODE_RANGE). Unfortunately, ada_print_scalar() relies
202 on the unsigned flag to determine whether the bound should
203 be printed as a signed or an unsigned value. This causes
204 the upper bound of the 0 .. -1 range types to be printed as
205 a very large unsigned number instead of -1.
206 To workaround this stabs deficiency, we replace the TYPE by NULL
207 to indicate default output when we detect that the bound is negative,
208 and the type is a TYPE_CODE_INT. The bound is negative when
209 'm' is the last character of the number scanned in BOUNDS. */
210 if (bounds[*n - 1] == 'm' && type->code () == TYPE_CODE_INT)
211 type = NULL;
212 ada_print_scalar (type, B, stream);
213 if (bounds[*n] == '_')
214 *n += 2;
216 else
218 int bound_len;
219 const char *bound = bounds + *n;
220 const char *pend;
222 pend = strstr (bound, "__");
223 if (pend == NULL)
224 *n += bound_len = strlen (bound);
225 else
227 bound_len = pend - bound;
228 *n += bound_len + 2;
230 gdb_printf (stream, "%.*s", bound_len, bound);
234 /* Assuming NAME[0 .. NAME_LEN-1] is the name of a range type, print
235 the value (if found) of the bound indicated by SUFFIX ("___L" or
236 "___U") according to the ___XD conventions. */
238 static void
239 print_dynamic_range_bound (struct type *type, const char *name, int name_len,
240 const char *suffix, struct ui_file *stream)
242 LONGEST B;
243 std::string name_buf (name, name_len);
244 name_buf += suffix;
246 if (get_int_var_value (name_buf.c_str (), B))
247 ada_print_scalar (type, B, stream);
248 else
249 gdb_printf (stream, "?");
252 /* Print RAW_TYPE as a range type, using any bound information
253 following the GNAT encoding (if available).
255 If BOUNDS_PREFERRED_P is nonzero, force the printing of the range
256 using its bounds. Otherwise, try printing the range without
257 printing the value of the bounds, if possible (this is only
258 considered a hint, not a guaranty). */
260 static void
261 print_range_type (struct type *raw_type, struct ui_file *stream,
262 int bounds_preferred_p)
264 const char *name;
265 struct type *base_type;
266 const char *subtype_info;
268 gdb_assert (raw_type != NULL);
269 name = raw_type->name ();
270 gdb_assert (name != NULL);
272 if (raw_type->code () == TYPE_CODE_RANGE)
273 base_type = raw_type->target_type ();
274 else
275 base_type = raw_type;
277 subtype_info = strstr (name, "___XD");
278 if (subtype_info == NULL)
279 print_range (raw_type, stream, bounds_preferred_p);
280 else
282 int prefix_len = subtype_info - name;
283 const char *bounds_str;
284 int n;
286 subtype_info += 5;
287 bounds_str = strchr (subtype_info, '_');
288 n = 1;
290 if (*subtype_info == 'L')
292 print_range_bound (base_type, bounds_str, &n, stream);
293 subtype_info += 1;
295 else
296 print_dynamic_range_bound (base_type, name, prefix_len, "___L",
297 stream);
299 gdb_printf (stream, " .. ");
301 if (*subtype_info == 'U')
302 print_range_bound (base_type, bounds_str, &n, stream);
303 else
304 print_dynamic_range_bound (base_type, name, prefix_len, "___U",
305 stream);
309 /* Print enumerated type TYPE on STREAM. */
311 static void
312 print_enum_type (struct type *type, struct ui_file *stream)
314 int len = type->num_fields ();
315 int i;
316 LONGEST lastval;
318 gdb_printf (stream, "(");
319 stream->wrap_here (1);
321 lastval = 0;
322 for (i = 0; i < len; i++)
324 QUIT;
325 if (i)
326 gdb_printf (stream, ", ");
327 stream->wrap_here (4);
328 fputs_styled (ada_enum_name (type->field (i).name ()),
329 variable_name_style.style (), stream);
330 if (lastval != type->field (i).loc_enumval ())
332 gdb_printf (stream, " => %s",
333 plongest (type->field (i).loc_enumval ()));
334 lastval = type->field (i).loc_enumval ();
336 lastval += 1;
338 gdb_printf (stream, ")");
341 /* Print simple (constrained) array type TYPE on STREAM. LEVEL is the
342 recursion (indentation) level, in case the element type itself has
343 nested structure, and SHOW is the number of levels of internal
344 structure to show (see ada_print_type). */
346 static void
347 print_array_type (struct type *type, struct ui_file *stream, int show,
348 int level, const struct type_print_options *flags)
350 int bitsize;
351 int n_indices;
352 struct type *elt_type = NULL;
354 if (ada_is_constrained_packed_array_type (type))
355 type = ada_coerce_to_simple_array_type (type);
357 bitsize = 0;
358 gdb_printf (stream, "array (");
360 if (type == NULL)
362 fprintf_styled (stream, metadata_style.style (),
363 _("<undecipherable array type>"));
364 return;
367 n_indices = -1;
368 if (ada_is_simple_array_type (type))
370 struct type *range_desc_type;
371 struct type *arr_type;
373 range_desc_type = ada_find_parallel_type (type, "___XA");
374 ada_fixup_array_indexes_type (range_desc_type);
376 bitsize = 0;
377 if (range_desc_type == NULL)
379 for (arr_type = type; arr_type->code () == TYPE_CODE_ARRAY; )
381 if (arr_type != type)
382 gdb_printf (stream, ", ");
383 print_range (arr_type->index_type (), stream,
384 0 /* bounds_preferred_p */);
385 if (arr_type->field (0).bitsize () > 0)
386 bitsize = arr_type->field (0).bitsize ();
387 /* A multi-dimensional array is represented using a
388 sequence of array types. If one of these types has a
389 name, then it is not another dimension of the outer
390 array, but rather the element type of the outermost
391 array. */
392 arr_type = arr_type->target_type ();
393 if (arr_type->name () != nullptr)
394 break;
397 else
399 int k;
401 n_indices = range_desc_type->num_fields ();
402 for (k = 0, arr_type = type;
403 k < n_indices;
404 k += 1, arr_type = arr_type->target_type ())
406 if (k > 0)
407 gdb_printf (stream, ", ");
408 print_range_type (range_desc_type->field (k).type (),
409 stream, 0 /* bounds_preferred_p */);
410 if (arr_type->field (0).bitsize () > 0)
411 bitsize = arr_type->field (0).bitsize ();
415 else
417 int i, i0;
419 for (i = i0 = ada_array_arity (type); i > 0; i -= 1)
420 gdb_printf (stream, "%s<>", i == i0 ? "" : ", ");
423 elt_type = ada_array_element_type (type, n_indices);
424 gdb_printf (stream, ") of ");
425 stream->wrap_here (0);
426 ada_print_type (elt_type, "", stream, show == 0 ? 0 : show - 1, level + 1,
427 flags);
428 /* Arrays with variable-length elements are never bit-packed in practice but
429 compilers have to describe their stride so that we can properly fetch
430 individual elements. Do not say the array is packed in this case. */
431 if (bitsize > 0 && !is_dynamic_type (elt_type))
432 gdb_printf (stream, " <packed: %d-bit elements>", bitsize);
435 /* Print the choices encoded by field FIELD_NUM of variant-part TYPE on
436 STREAM, assuming that VAL_TYPE (if non-NULL) is the type of the
437 values. Return non-zero if the field is an encoding of
438 discriminant values, as in a standard variant record, and 0 if the
439 field is not so encoded (as happens with single-component variants
440 in types annotated with pragma Unchecked_Union). */
442 static int
443 print_choices (struct type *type, int field_num, struct ui_file *stream,
444 struct type *val_type)
446 int have_output;
447 int p;
448 const char *name = type->field (field_num).name ();
450 have_output = 0;
452 /* Skip over leading 'V': NOTE soon to be obsolete. */
453 if (name[0] == 'V')
455 if (!ada_scan_number (name, 1, NULL, &p))
456 goto Huh;
458 else
459 p = 0;
461 while (1)
463 switch (name[p])
465 default:
466 goto Huh;
467 case '_':
468 case '\0':
469 gdb_printf (stream, " =>");
470 return 1;
471 case 'S':
472 case 'R':
473 case 'O':
474 if (have_output)
475 gdb_printf (stream, " | ");
476 have_output = 1;
477 break;
480 switch (name[p])
482 case 'S':
484 LONGEST W;
486 if (!ada_scan_number (name, p + 1, &W, &p))
487 goto Huh;
488 ada_print_scalar (val_type, W, stream);
489 break;
491 case 'R':
493 LONGEST L, U;
495 if (!ada_scan_number (name, p + 1, &L, &p)
496 || name[p] != 'T' || !ada_scan_number (name, p + 1, &U, &p))
497 goto Huh;
498 ada_print_scalar (val_type, L, stream);
499 gdb_printf (stream, " .. ");
500 ada_print_scalar (val_type, U, stream);
501 break;
503 case 'O':
504 gdb_printf (stream, "others");
505 p += 1;
506 break;
510 Huh:
511 gdb_printf (stream, "? =>");
512 return 0;
515 /* A helper for print_variant_clauses that prints the members of
516 VAR_TYPE. DISCR_TYPE is the type of the discriminant (or nullptr
517 if not available). The discriminant is contained in OUTER_TYPE.
518 STREAM, LEVEL, SHOW, and FLAGS are the same as for
519 ada_print_type. */
521 static void
522 print_variant_clauses (struct type *var_type, struct type *discr_type,
523 struct type *outer_type, struct ui_file *stream,
524 int show, int level,
525 const struct type_print_options *flags)
527 for (int i = 0; i < var_type->num_fields (); i += 1)
529 gdb_printf (stream, "\n%*swhen ", level, "");
530 if (print_choices (var_type, i, stream, discr_type))
532 if (print_record_field_types (var_type->field (i).type (),
533 outer_type, stream, show, level,
534 flags)
535 <= 0)
536 gdb_printf (stream, " null;");
538 else
539 print_selected_record_field_types (var_type, outer_type, i, i,
540 stream, show, level, flags);
544 /* Assuming that field FIELD_NUM of TYPE represents variants whose
545 discriminant is contained in OUTER_TYPE, print its components on STREAM.
546 LEVEL is the recursion (indentation) level, in case any of the fields
547 themselves have nested structure, and SHOW is the number of levels of
548 internal structure to show (see ada_print_type). For this purpose,
549 fields nested in a variant part are taken to be at the same level as
550 the fields immediately outside the variant part. */
552 static void
553 print_variant_clauses (struct type *type, int field_num,
554 struct type *outer_type, struct ui_file *stream,
555 int show, int level,
556 const struct type_print_options *flags)
558 struct type *var_type, *par_type;
559 struct type *discr_type;
561 var_type = type->field (field_num).type ();
562 discr_type = ada_variant_discrim_type (var_type, outer_type);
564 if (var_type->code () == TYPE_CODE_PTR)
566 var_type = var_type->target_type ();
567 if (var_type == NULL || var_type->code () != TYPE_CODE_UNION)
568 return;
571 par_type = ada_find_parallel_type (var_type, "___XVU");
572 if (par_type != NULL)
573 var_type = par_type;
575 print_variant_clauses (var_type, discr_type, outer_type, stream, show,
576 level + 4, flags);
579 /* Assuming that field FIELD_NUM of TYPE is a variant part whose
580 discriminants are contained in OUTER_TYPE, print a description of it
581 on STREAM. LEVEL is the recursion (indentation) level, in case any of
582 the fields themselves have nested structure, and SHOW is the number of
583 levels of internal structure to show (see ada_print_type). For this
584 purpose, fields nested in a variant part are taken to be at the same
585 level as the fields immediately outside the variant part. */
587 static void
588 print_variant_part (struct type *type, int field_num, struct type *outer_type,
589 struct ui_file *stream, int show, int level,
590 const struct type_print_options *flags)
592 const char *variant
593 = ada_variant_discrim_name (type->field (field_num).type ());
594 if (*variant == '\0')
595 variant = "?";
597 gdb_printf (stream, "\n%*scase %s is", level + 4, "", variant);
598 print_variant_clauses (type, field_num, outer_type, stream, show,
599 level + 4, flags);
600 gdb_printf (stream, "\n%*send case;", level + 4, "");
603 /* Print a description on STREAM of the fields FLD0 through FLD1 in
604 record or union type TYPE, whose discriminants are in OUTER_TYPE.
605 LEVEL is the recursion (indentation) level, in case any of the
606 fields themselves have nested structure, and SHOW is the number of
607 levels of internal structure to show (see ada_print_type). Does
608 not print parent type information of TYPE. Returns 0 if no fields
609 printed, -1 for an incomplete type, else > 0. Prints each field
610 beginning on a new line, but does not put a new line at end. */
612 static int
613 print_selected_record_field_types (struct type *type, struct type *outer_type,
614 int fld0, int fld1,
615 struct ui_file *stream, int show, int level,
616 const struct type_print_options *flags)
618 int i, flds;
620 flds = 0;
622 if (fld0 > fld1 && type->is_stub ())
623 return -1;
625 for (i = fld0; i <= fld1; i += 1)
627 QUIT;
629 if (ada_is_parent_field (type, i) || ada_is_ignored_field (type, i))
631 else if (ada_is_wrapper_field (type, i))
632 flds += print_record_field_types (type->field (i).type (), type,
633 stream, show, level, flags);
634 else if (ada_is_variant_part (type, i))
636 print_variant_part (type, i, outer_type, stream, show, level, flags);
637 flds = 1;
639 else
641 flds += 1;
642 gdb_printf (stream, "\n%*s", level + 4, "");
643 ada_print_type (type->field (i).type (),
644 type->field (i).name (),
645 stream, show - 1, level + 4, flags);
646 gdb_printf (stream, ";");
650 return flds;
653 static void print_record_field_types_dynamic
654 (const gdb::array_view<variant_part> &parts,
655 int from, int to, struct type *type, struct ui_file *stream,
656 int show, int level, const struct type_print_options *flags);
658 /* Print the choices encoded by VARIANT on STREAM. LEVEL is the
659 indentation level. The type of the discriminant for VARIANT is
660 given by DISR_TYPE. */
662 static void
663 print_choices (struct type *discr_type, const variant &variant,
664 struct ui_file *stream, int level)
666 gdb_printf (stream, "\n%*swhen ", level, "");
667 if (variant.is_default ())
668 gdb_printf (stream, "others");
669 else
671 bool first = true;
672 for (const discriminant_range &range : variant.discriminants)
674 if (!first)
675 gdb_printf (stream, " | ");
676 first = false;
678 ada_print_scalar (discr_type, range.low, stream);
679 if (range.low != range.high)
680 ada_print_scalar (discr_type, range.high, stream);
684 gdb_printf (stream, " =>");
687 /* Print a single variant part, PART, on STREAM. TYPE is the
688 enclosing type. SHOW, LEVEL, and FLAGS are the usual type-printing
689 settings. This prints information about PART and the fields it
690 controls. It returns the index of the next field that should be
691 shown -- that is, one after the last field printed by this
692 call. */
694 static int
695 print_variant_part (const variant_part &part,
696 struct type *type, struct ui_file *stream,
697 int show, int level,
698 const struct type_print_options *flags)
700 struct type *discr_type = nullptr;
701 const char *name;
702 if (part.discriminant_index == -1)
703 name = "?";
704 else
706 name = type->field (part.discriminant_index).name ();;
707 discr_type = type->field (part.discriminant_index).type ();
710 gdb_printf (stream, "\n%*scase %s is", level + 4, "", name);
712 int last_field = -1;
713 for (const variant &variant : part.variants)
715 print_choices (discr_type, variant, stream, level + 8);
717 if (variant.first_field == variant.last_field)
718 gdb_printf (stream, " null;");
719 else
721 print_record_field_types_dynamic (variant.parts,
722 variant.first_field,
723 variant.last_field, type, stream,
724 show, level + 8, flags);
725 last_field = variant.last_field;
729 gdb_printf (stream, "\n%*send case;", level + 4, "");
731 return last_field;
734 /* Print some fields of TYPE to STREAM. SHOW, LEVEL, and FLAGS are
735 the usual type-printing settings. PARTS is the array of variant
736 parts that correspond to the range of fields to be printed. FROM
737 and TO are the range of fields to print. */
739 static void
740 print_record_field_types_dynamic (const gdb::array_view<variant_part> &parts,
741 int from, int to,
742 struct type *type, struct ui_file *stream,
743 int show, int level,
744 const struct type_print_options *flags)
746 int field = from;
748 for (const variant_part &part : parts)
750 if (part.variants.empty ())
751 continue;
753 /* Print any non-varying fields. */
754 int first_varying = part.variants[0].first_field;
755 print_selected_record_field_types (type, type, field,
756 first_varying - 1, stream,
757 show, level, flags);
759 field = print_variant_part (part, type, stream, show, level, flags);
762 /* Print any trailing fields that we were asked to print. */
763 print_selected_record_field_types (type, type, field, to - 1, stream, show,
764 level, flags);
767 /* Print a description on STREAM of all fields of record or union type
768 TYPE, as for print_selected_record_field_types, above. */
770 static int
771 print_record_field_types (struct type *type, struct type *outer_type,
772 struct ui_file *stream, int show, int level,
773 const struct type_print_options *flags)
775 struct dynamic_prop *prop = type->dyn_prop (DYN_PROP_VARIANT_PARTS);
776 if (prop != nullptr)
778 if (prop->kind () == PROP_TYPE)
780 type = prop->original_type ();
781 prop = type->dyn_prop (DYN_PROP_VARIANT_PARTS);
783 gdb_assert (prop->kind () == PROP_VARIANT_PARTS);
784 print_record_field_types_dynamic (*prop->variant_parts (),
785 0, type->num_fields (),
786 type, stream, show, level, flags);
787 return type->num_fields ();
790 return print_selected_record_field_types (type, outer_type,
791 0, type->num_fields () - 1,
792 stream, show, level, flags);
796 /* Print record type TYPE on STREAM. LEVEL is the recursion (indentation)
797 level, in case the element type itself has nested structure, and SHOW is
798 the number of levels of internal structure to show (see ada_print_type). */
800 static void
801 print_record_type (struct type *type0, struct ui_file *stream, int show,
802 int level, const struct type_print_options *flags)
804 struct type *parent_type;
805 struct type *type;
807 type = ada_find_parallel_type (type0, "___XVE");
808 if (type == NULL)
809 type = type0;
811 parent_type = ada_parent_type (type);
812 if (ada_type_name (parent_type) != NULL)
814 const char *parent_name = decoded_type_name (parent_type);
816 /* If we fail to decode the parent type name, then use the parent
817 type name as is. Not pretty, but should never happen except
818 when the debugging info is incomplete or incorrect. This
819 prevents a crash trying to print a NULL pointer. */
820 if (parent_name == NULL)
821 parent_name = ada_type_name (parent_type);
822 gdb_printf (stream, "new %s with record", parent_name);
824 else if (parent_type == NULL && ada_is_tagged_type (type, 0))
825 gdb_printf (stream, "tagged record");
826 else
827 gdb_printf (stream, "record");
829 if (show < 0)
830 gdb_printf (stream, " ... end record");
831 else
833 int flds;
835 flds = 0;
836 if (parent_type != NULL && ada_type_name (parent_type) == NULL)
837 flds += print_record_field_types (parent_type, parent_type,
838 stream, show, level, flags);
839 flds += print_record_field_types (type, type, stream, show, level,
840 flags);
842 if (flds > 0)
843 gdb_printf (stream, "\n%*send record", level, "");
844 else if (flds < 0)
845 gdb_printf (stream, _(" <incomplete type> end record"));
846 else
847 gdb_printf (stream, " null; end record");
851 /* Print the unchecked union type TYPE in something resembling Ada
852 format on STREAM. LEVEL is the recursion (indentation) level
853 in case the element type itself has nested structure, and SHOW is the
854 number of levels of internal structure to show (see ada_print_type). */
855 static void
856 print_unchecked_union_type (struct type *type, struct ui_file *stream,
857 int show, int level,
858 const struct type_print_options *flags)
860 if (show < 0)
861 gdb_printf (stream, "record (?) is ... end record");
862 else if (type->num_fields () == 0)
863 gdb_printf (stream, "record (?) is null; end record");
864 else
866 gdb_printf (stream, "record (?) is\n%*scase ? is", level + 4, "");
868 print_variant_clauses (type, nullptr, type, stream, show, level + 8, flags);
870 gdb_printf (stream, "\n%*send case;\n%*send record",
871 level + 4, "", level, "");
877 /* Print function or procedure type TYPE on STREAM. Make it a header
878 for function or procedure NAME if NAME is not null. */
880 static void
881 print_func_type (struct type *type, struct ui_file *stream, const char *name,
882 const struct type_print_options *flags)
884 int i, len = type->num_fields ();
886 if (type->target_type () != NULL
887 && type->target_type ()->code () == TYPE_CODE_VOID)
888 gdb_printf (stream, "procedure");
889 else
890 gdb_printf (stream, "function");
892 if (name != NULL && name[0] != '\0')
894 gdb_puts (" ", stream);
895 fputs_styled (name, function_name_style.style (), stream);
898 if (len > 0)
900 gdb_printf (stream, " (");
901 for (i = 0; i < len; i += 1)
903 if (i > 0)
905 gdb_puts ("; ", stream);
906 stream->wrap_here (4);
908 gdb_printf (stream, "a%d: ", i + 1);
909 ada_print_type (type->field (i).type (), "", stream, -1, 0,
910 flags);
912 gdb_printf (stream, ")");
915 if (type->target_type () == NULL)
916 gdb_printf (stream, " return <unknown return type>");
917 else if (type->target_type ()->code () != TYPE_CODE_VOID)
919 gdb_printf (stream, " return ");
920 ada_print_type (type->target_type (), "", stream, 0, 0, flags);
925 /* Print a description of a type TYPE0.
926 Output goes to STREAM (via stdio).
927 If VARSTRING is a non-NULL, non-empty string, print as an Ada
928 variable/field declaration.
929 SHOW+1 is the maximum number of levels of internal type structure
930 to show (this applies to record types, enumerated types, and
931 array types).
932 SHOW is the number of levels of internal type structure to show
933 when there is a type name for the SHOWth deepest level (0th is
934 outer level).
935 When SHOW<0, no inner structure is shown.
936 LEVEL indicates level of recursion (for nested definitions). */
938 void
939 ada_print_type (struct type *type0, const char *varstring,
940 struct ui_file *stream, int show, int level,
941 const struct type_print_options *flags)
943 if (type0->code () == TYPE_CODE_INTERNAL_FUNCTION)
945 c_print_type (type0, "", stream, show, level,
946 language_ada, flags);
947 return;
950 struct type *type = ada_check_typedef (ada_get_base_type (type0));
951 /* If we can decode the original type name, use it. However, there
952 are cases where the original type is an internally-generated type
953 with a name that can't be decoded (and whose encoded name might
954 not actually bear any relation to the type actually declared in
955 the sources). In that case, try using the name of the base type
956 in its place.
958 Note that we looked at the possibility of always using the name
959 of the base type. This does not always work, unfortunately, as
960 there are situations where it's the base type which has an
961 internally-generated name. */
962 const char *type_name = decoded_type_name (type0);
963 if (type_name == nullptr)
964 type_name = decoded_type_name (type);
965 int is_var_decl = (varstring != NULL && varstring[0] != '\0');
967 if (type == NULL)
969 if (is_var_decl)
970 gdb_printf (stream, "%.*s: ",
971 ada_name_prefix_len (varstring), varstring);
972 fprintf_styled (stream, metadata_style.style (), "<null type?>");
973 return;
976 if (is_var_decl && type->code () != TYPE_CODE_FUNC)
977 gdb_printf (stream, "%.*s: ",
978 ada_name_prefix_len (varstring), varstring);
980 if (type_name != NULL && show <= 0 && !ada_is_aligner_type (type))
982 gdb_printf (stream, "%.*s",
983 ada_name_prefix_len (type_name), type_name);
984 return;
987 if (ada_is_aligner_type (type))
988 ada_print_type (ada_aligned_type (type), "", stream, show, level, flags);
989 else if (ada_is_constrained_packed_array_type (type)
990 && type->code () != TYPE_CODE_PTR)
991 print_array_type (type, stream, show, level, flags);
992 else
993 switch (type->code ())
995 default:
996 gdb_printf (stream, "<");
997 c_print_type (type, "", stream, show, level, language_ada, flags);
998 gdb_printf (stream, ">");
999 break;
1000 case TYPE_CODE_PTR:
1001 case TYPE_CODE_TYPEDEF:
1002 /* An __XVL field is not truly a pointer, so don't print
1003 "access" in this case. */
1004 if (type->code () != TYPE_CODE_PTR
1005 || (varstring != nullptr
1006 && strstr (varstring, "___XVL") == nullptr))
1007 gdb_printf (stream, "access ");
1008 ada_print_type (type->target_type (), "", stream, show, level,
1009 flags);
1010 break;
1011 case TYPE_CODE_REF:
1012 gdb_printf (stream, "<ref> ");
1013 ada_print_type (type->target_type (), "", stream, show, level,
1014 flags);
1015 break;
1016 case TYPE_CODE_ARRAY:
1017 print_array_type (type, stream, show, level, flags);
1018 break;
1019 case TYPE_CODE_BOOL:
1020 gdb_printf (stream, "(false, true)");
1021 break;
1022 case TYPE_CODE_INT:
1024 const char *name = ada_type_name (type);
1026 if (!ada_is_range_type_name (name))
1027 fprintf_styled (stream, metadata_style.style (),
1028 _("<%s-byte integer>"),
1029 pulongest (type->length ()));
1030 else
1032 gdb_printf (stream, "range ");
1033 print_range_type (type, stream, 1 /* bounds_preferred_p */);
1036 break;
1037 case TYPE_CODE_RANGE:
1038 if (is_fixed_point_type (type))
1040 gdb_printf (stream, "<");
1041 print_type_fixed_point (type, stream);
1042 gdb_printf (stream, ">");
1044 else if (ada_is_modular_type (type))
1045 gdb_printf (stream, "mod %s",
1046 int_string (ada_modulus (type), 10, 0, 0, 1));
1047 else
1049 gdb_printf (stream, "range ");
1050 print_range (type, stream, 1 /* bounds_preferred_p */);
1052 break;
1053 case TYPE_CODE_FLT:
1054 fprintf_styled (stream, metadata_style.style (),
1055 _("<%s-byte float>"),
1056 pulongest (type->length ()));
1057 break;
1058 case TYPE_CODE_ENUM:
1059 if (show < 0)
1060 gdb_printf (stream, "(...)");
1061 else
1062 print_enum_type (type, stream);
1063 break;
1064 case TYPE_CODE_STRUCT:
1065 if (ada_is_array_descriptor_type (type))
1066 print_array_type (type, stream, show, level, flags);
1067 else
1068 print_record_type (type, stream, show, level, flags);
1069 break;
1070 case TYPE_CODE_UNION:
1071 print_unchecked_union_type (type, stream, show, level, flags);
1072 break;
1073 case TYPE_CODE_FUNC:
1074 print_func_type (type, stream, varstring, flags);
1075 break;
1079 /* Implement the la_print_typedef language method for Ada. */
1081 void
1082 ada_print_typedef (struct type *type, struct symbol *new_symbol,
1083 struct ui_file *stream)
1085 type = ada_check_typedef (type);
1086 ada_print_type (type, "", stream, 0, 0, &type_print_raw_options);