1 /* Support for printing Ada types for GDB, the GNU debugger.
2 Copyright (C) 1986-2014 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/>. */
20 #include "gdb_obstack.h"
21 #include "bfd.h" /* Binary File Description */
24 #include "expression.h"
33 #include "typeprint.h"
37 static int print_selected_record_field_types (struct type *, struct type *,
39 struct ui_file *, int, int,
40 const struct type_print_options *);
42 static int print_record_field_types (struct type *, struct type *,
43 struct ui_file *, int, int,
44 const struct type_print_options *);
48 static char *name_buffer;
49 static int name_buffer_len;
51 /* The (decoded) Ada name of TYPE. This value persists until the
55 decoded_type_name (struct type *type)
57 if (ada_type_name (type) == NULL)
61 const char *raw_name = ada_type_name (type);
64 if (name_buffer == NULL || name_buffer_len <= strlen (raw_name))
66 name_buffer_len = 16 + 2 * strlen (raw_name);
67 name_buffer = xrealloc (name_buffer, name_buffer_len);
69 strcpy (name_buffer, raw_name);
71 s = (char *) strstr (name_buffer, "___");
75 s = name_buffer + strlen (name_buffer) - 1;
76 while (s > name_buffer && (s[0] != '_' || s[-1] != '_'))
85 for (s = q = name_buffer; *s != '\0'; q += 1)
87 if (s[0] == '_' && s[1] == '_')
103 /* Return nonzero if TYPE is a subrange type, and its bounds
104 are identical to the bounds of its subtype. */
107 type_is_full_subrange_of_target_type (struct type *type)
109 struct type *subtype;
111 if (TYPE_CODE (type) != TYPE_CODE_RANGE)
114 subtype = TYPE_TARGET_TYPE (type);
118 if (ada_discrete_type_low_bound (type)
119 != ada_discrete_type_low_bound (subtype))
122 if (ada_discrete_type_high_bound (type)
123 != ada_discrete_type_high_bound (subtype))
129 /* Print TYPE on STREAM, preferably as a range if BOUNDS_PREFERED_P
133 print_range (struct type *type, struct ui_file *stream,
134 int bounds_prefered_p)
136 if (!bounds_prefered_p)
138 /* Try stripping all TYPE_CODE_RANGE layers whose bounds
139 are identical to the bounds of their subtype. When
140 the bounds of both types match, it can allow us to
141 print a range using the name of its base type, which
142 is easier to read. For instance, we would print...
144 array (character) of ...
148 array ('["00"]' .. '["ff"]') of ... */
149 while (type_is_full_subrange_of_target_type (type))
150 type = TYPE_TARGET_TYPE (type);
153 switch (TYPE_CODE (type))
155 case TYPE_CODE_RANGE:
158 struct type *target_type;
160 target_type = TYPE_TARGET_TYPE (type);
161 if (target_type == NULL)
163 ada_print_scalar (target_type, ada_discrete_type_low_bound (type),
165 fprintf_filtered (stream, " .. ");
166 ada_print_scalar (target_type, ada_discrete_type_high_bound (type),
171 fprintf_filtered (stream, "%.*s",
172 ada_name_prefix_len (TYPE_NAME (type)),
178 /* Print the number or discriminant bound at BOUNDS+*N on STREAM, and
179 set *N past the bound and its delimiter, if any. */
182 print_range_bound (struct type *type, char *bounds, int *n,
183 struct ui_file *stream)
187 if (ada_scan_number (bounds, *n, &B, n))
189 /* STABS decodes all range types which bounds are 0 .. -1 as
190 unsigned integers (ie. the type code is TYPE_CODE_INT, not
191 TYPE_CODE_RANGE). Unfortunately, ada_print_scalar() relies
192 on the unsigned flag to determine whether the bound should
193 be printed as a signed or an unsigned value. This causes
194 the upper bound of the 0 .. -1 range types to be printed as
195 a very large unsigned number instead of -1.
196 To workaround this stabs deficiency, we replace the TYPE by NULL
197 to indicate default output when we detect that the bound is negative,
198 and the type is a TYPE_CODE_INT. The bound is negative when
199 'm' is the last character of the number scanned in BOUNDS. */
200 if (bounds[*n - 1] == 'm' && TYPE_CODE (type) == TYPE_CODE_INT)
202 ada_print_scalar (type, B, stream);
203 if (bounds[*n] == '_')
209 char *bound = bounds + *n;
212 pend = strstr (bound, "__");
214 *n += bound_len = strlen (bound);
217 bound_len = pend - bound;
220 fprintf_filtered (stream, "%.*s", bound_len, bound);
224 /* Assuming NAME[0 .. NAME_LEN-1] is the name of a range type, print
225 the value (if found) of the bound indicated by SUFFIX ("___L" or
226 "___U") according to the ___XD conventions. */
229 print_dynamic_range_bound (struct type *type, const char *name, int name_len,
230 const char *suffix, struct ui_file *stream)
232 static char *name_buf = NULL;
233 static size_t name_buf_len = 0;
237 GROW_VECT (name_buf, name_buf_len, name_len + strlen (suffix) + 1);
238 strncpy (name_buf, name, name_len);
239 strcpy (name_buf + name_len, suffix);
241 B = get_int_var_value (name_buf, &OK);
243 ada_print_scalar (type, B, stream);
245 fprintf_filtered (stream, "?");
248 /* Print RAW_TYPE as a range type, using any bound information
249 following the GNAT encoding (if available).
251 If BOUNDS_PREFERED_P is nonzero, force the printing of the range
252 using its bounds. Otherwise, try printing the range without
253 printing the value of the bounds, if possible (this is only
254 considered a hint, not a guaranty). */
257 print_range_type (struct type *raw_type, struct ui_file *stream,
258 int bounds_prefered_p)
261 struct type *base_type;
262 const char *subtype_info;
264 gdb_assert (raw_type != NULL);
265 name = TYPE_NAME (raw_type);
266 gdb_assert (name != NULL);
268 if (TYPE_CODE (raw_type) == TYPE_CODE_RANGE)
269 base_type = TYPE_TARGET_TYPE (raw_type);
271 base_type = raw_type;
273 subtype_info = strstr (name, "___XD");
274 if (subtype_info == NULL)
275 print_range (raw_type, stream, bounds_prefered_p);
278 int prefix_len = subtype_info - name;
283 bounds_str = strchr (subtype_info, '_');
286 if (*subtype_info == 'L')
288 print_range_bound (base_type, bounds_str, &n, stream);
292 print_dynamic_range_bound (base_type, name, prefix_len, "___L",
295 fprintf_filtered (stream, " .. ");
297 if (*subtype_info == 'U')
298 print_range_bound (base_type, bounds_str, &n, stream);
300 print_dynamic_range_bound (base_type, name, prefix_len, "___U",
305 /* Print enumerated type TYPE on STREAM. */
308 print_enum_type (struct type *type, struct ui_file *stream)
310 int len = TYPE_NFIELDS (type);
314 fprintf_filtered (stream, "(");
318 for (i = 0; i < len; i++)
322 fprintf_filtered (stream, ", ");
324 fputs_filtered (ada_enum_name (TYPE_FIELD_NAME (type, i)), stream);
325 if (lastval != TYPE_FIELD_ENUMVAL (type, i))
327 fprintf_filtered (stream, " => %s",
328 plongest (TYPE_FIELD_ENUMVAL (type, i)));
329 lastval = TYPE_FIELD_ENUMVAL (type, i);
333 fprintf_filtered (stream, ")");
336 /* Print representation of Ada fixed-point type TYPE on STREAM. */
339 print_fixed_point_type (struct type *type, struct ui_file *stream)
341 DOUBLEST delta = ada_delta (type);
342 DOUBLEST small = ada_fixed_to_float (type, 1.0);
345 fprintf_filtered (stream, "delta ??");
348 fprintf_filtered (stream, "delta %g", (double) delta);
350 fprintf_filtered (stream, " <'small = %g>", (double) small);
354 /* Print simple (constrained) array type TYPE on STREAM. LEVEL is the
355 recursion (indentation) level, in case the element type itself has
356 nested structure, and SHOW is the number of levels of internal
357 structure to show (see ada_print_type). */
360 print_array_type (struct type *type, struct ui_file *stream, int show,
361 int level, const struct type_print_options *flags)
366 if (ada_is_constrained_packed_array_type (type))
367 type = ada_coerce_to_simple_array_type (type);
370 fprintf_filtered (stream, "array (");
374 fprintf_filtered (stream, _("<undecipherable array type>"));
379 if (ada_is_simple_array_type (type))
381 struct type *range_desc_type;
382 struct type *arr_type;
384 range_desc_type = ada_find_parallel_type (type, "___XA");
385 ada_fixup_array_indexes_type (range_desc_type);
388 if (range_desc_type == NULL)
390 for (arr_type = type; TYPE_CODE (arr_type) == TYPE_CODE_ARRAY;
391 arr_type = TYPE_TARGET_TYPE (arr_type))
393 if (arr_type != type)
394 fprintf_filtered (stream, ", ");
395 print_range (TYPE_INDEX_TYPE (arr_type), stream,
396 0 /* bounds_prefered_p */);
397 if (TYPE_FIELD_BITSIZE (arr_type, 0) > 0)
398 bitsize = TYPE_FIELD_BITSIZE (arr_type, 0);
405 n_indices = TYPE_NFIELDS (range_desc_type);
406 for (k = 0, arr_type = type;
408 k += 1, arr_type = TYPE_TARGET_TYPE (arr_type))
411 fprintf_filtered (stream, ", ");
412 print_range_type (TYPE_FIELD_TYPE (range_desc_type, k),
413 stream, 0 /* bounds_prefered_p */);
414 if (TYPE_FIELD_BITSIZE (arr_type, 0) > 0)
415 bitsize = TYPE_FIELD_BITSIZE (arr_type, 0);
423 for (i = i0 = ada_array_arity (type); i > 0; i -= 1)
424 fprintf_filtered (stream, "%s<>", i == i0 ? "" : ", ");
427 fprintf_filtered (stream, ") of ");
429 ada_print_type (ada_array_element_type (type, n_indices), "", stream,
430 show == 0 ? 0 : show - 1, level + 1, flags);
432 fprintf_filtered (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_Variant). */
443 print_choices (struct type *type, int field_num, struct ui_file *stream,
444 struct type *val_type)
448 const char *name = TYPE_FIELD_NAME (type, field_num);
452 /* Skip over leading 'V': NOTE soon to be obsolete. */
455 if (!ada_scan_number (name, 1, NULL, &p))
469 fprintf_filtered (stream, " =>");
475 fprintf_filtered (stream, " | ");
486 if (!ada_scan_number (name, p + 1, &W, &p))
488 ada_print_scalar (val_type, W, stream);
495 if (!ada_scan_number (name, p + 1, &L, &p)
496 || name[p] != 'T' || !ada_scan_number (name, p + 1, &U, &p))
498 ada_print_scalar (val_type, L, stream);
499 fprintf_filtered (stream, " .. ");
500 ada_print_scalar (val_type, U, stream);
504 fprintf_filtered (stream, "others");
511 fprintf_filtered (stream, "?? =>");
515 /* Assuming that field FIELD_NUM of TYPE represents variants whose
516 discriminant is contained in OUTER_TYPE, print its components on STREAM.
517 LEVEL is the recursion (indentation) level, in case any of the fields
518 themselves have nested structure, and SHOW is the number of levels of
519 internal structure to show (see ada_print_type). For this purpose,
520 fields nested in a variant part are taken to be at the same level as
521 the fields immediately outside the variant part. */
524 print_variant_clauses (struct type *type, int field_num,
525 struct type *outer_type, struct ui_file *stream,
527 const struct type_print_options *flags)
530 struct type *var_type, *par_type;
531 struct type *discr_type;
533 var_type = TYPE_FIELD_TYPE (type, field_num);
534 discr_type = ada_variant_discrim_type (var_type, outer_type);
536 if (TYPE_CODE (var_type) == TYPE_CODE_PTR)
538 var_type = TYPE_TARGET_TYPE (var_type);
539 if (var_type == NULL || TYPE_CODE (var_type) != TYPE_CODE_UNION)
543 par_type = ada_find_parallel_type (var_type, "___XVU");
544 if (par_type != NULL)
547 for (i = 0; i < TYPE_NFIELDS (var_type); i += 1)
549 fprintf_filtered (stream, "\n%*swhen ", level + 4, "");
550 if (print_choices (var_type, i, stream, discr_type))
552 if (print_record_field_types (TYPE_FIELD_TYPE (var_type, i),
553 outer_type, stream, show, level + 4,
556 fprintf_filtered (stream, " null;");
559 print_selected_record_field_types (var_type, outer_type, i, i,
560 stream, show, level + 4, flags);
564 /* Assuming that field FIELD_NUM of TYPE is a variant part whose
565 discriminants are contained in OUTER_TYPE, print a description of it
566 on STREAM. LEVEL is the recursion (indentation) level, in case any of
567 the fields themselves have nested structure, and SHOW is the number of
568 levels of internal structure to show (see ada_print_type). For this
569 purpose, fields nested in a variant part are taken to be at the same
570 level as the fields immediately outside the variant part. */
573 print_variant_part (struct type *type, int field_num, struct type *outer_type,
574 struct ui_file *stream, int show, int level,
575 const struct type_print_options *flags)
577 fprintf_filtered (stream, "\n%*scase %s is", level + 4, "",
578 ada_variant_discrim_name
579 (TYPE_FIELD_TYPE (type, field_num)));
580 print_variant_clauses (type, field_num, outer_type, stream, show,
582 fprintf_filtered (stream, "\n%*send case;", level + 4, "");
585 /* Print a description on STREAM of the fields FLD0 through FLD1 in
586 record or union type TYPE, whose discriminants are in OUTER_TYPE.
587 LEVEL is the recursion (indentation) level, in case any of the
588 fields themselves have nested structure, and SHOW is the number of
589 levels of internal structure to show (see ada_print_type). Does
590 not print parent type information of TYPE. Returns 0 if no fields
591 printed, -1 for an incomplete type, else > 0. Prints each field
592 beginning on a new line, but does not put a new line at end. */
595 print_selected_record_field_types (struct type *type, struct type *outer_type,
597 struct ui_file *stream, int show, int level,
598 const struct type_print_options *flags)
604 if (fld0 > fld1 && TYPE_STUB (type))
607 for (i = fld0; i <= fld1; i += 1)
611 if (ada_is_parent_field (type, i) || ada_is_ignored_field (type, i))
613 else if (ada_is_wrapper_field (type, i))
614 flds += print_record_field_types (TYPE_FIELD_TYPE (type, i), type,
615 stream, show, level, flags);
616 else if (ada_is_variant_part (type, i))
618 print_variant_part (type, i, outer_type, stream, show, level, flags);
624 fprintf_filtered (stream, "\n%*s", level + 4, "");
625 ada_print_type (TYPE_FIELD_TYPE (type, i),
626 TYPE_FIELD_NAME (type, i),
627 stream, show - 1, level + 4, flags);
628 fprintf_filtered (stream, ";");
635 /* Print a description on STREAM of all fields of record or union type
636 TYPE, as for print_selected_record_field_types, above. */
639 print_record_field_types (struct type *type, struct type *outer_type,
640 struct ui_file *stream, int show, int level,
641 const struct type_print_options *flags)
643 return print_selected_record_field_types (type, outer_type,
644 0, TYPE_NFIELDS (type) - 1,
645 stream, show, level, flags);
649 /* Print record type TYPE on STREAM. LEVEL is the recursion (indentation)
650 level, in case the element type itself has nested structure, and SHOW is
651 the number of levels of internal structure to show (see ada_print_type). */
654 print_record_type (struct type *type0, struct ui_file *stream, int show,
655 int level, const struct type_print_options *flags)
657 struct type *parent_type;
660 type = ada_find_parallel_type (type0, "___XVE");
664 parent_type = ada_parent_type (type);
665 if (ada_type_name (parent_type) != NULL)
667 const char *parent_name = decoded_type_name (parent_type);
669 /* If we fail to decode the parent type name, then use the parent
670 type name as is. Not pretty, but should never happen except
671 when the debugging info is incomplete or incorrect. This
672 prevents a crash trying to print a NULL pointer. */
673 if (parent_name == NULL)
674 parent_name = ada_type_name (parent_type);
675 fprintf_filtered (stream, "new %s with record", parent_name);
677 else if (parent_type == NULL && ada_is_tagged_type (type, 0))
678 fprintf_filtered (stream, "tagged record");
680 fprintf_filtered (stream, "record");
683 fprintf_filtered (stream, " ... end record");
689 if (parent_type != NULL && ada_type_name (parent_type) == NULL)
690 flds += print_record_field_types (parent_type, parent_type,
691 stream, show, level, flags);
692 flds += print_record_field_types (type, type, stream, show, level,
696 fprintf_filtered (stream, "\n%*send record", level, "");
698 fprintf_filtered (stream, _(" <incomplete type> end record"));
700 fprintf_filtered (stream, " null; end record");
704 /* Print the unchecked union type TYPE in something resembling Ada
705 format on STREAM. LEVEL is the recursion (indentation) level
706 in case the element type itself has nested structure, and SHOW is the
707 number of levels of internal structure to show (see ada_print_type). */
709 print_unchecked_union_type (struct type *type, struct ui_file *stream,
711 const struct type_print_options *flags)
714 fprintf_filtered (stream, "record (?) is ... end record");
715 else if (TYPE_NFIELDS (type) == 0)
716 fprintf_filtered (stream, "record (?) is null; end record");
721 fprintf_filtered (stream, "record (?) is\n%*scase ? is", level + 4, "");
723 for (i = 0; i < TYPE_NFIELDS (type); i += 1)
725 fprintf_filtered (stream, "\n%*swhen ? =>\n%*s", level + 8, "",
727 ada_print_type (TYPE_FIELD_TYPE (type, i),
728 TYPE_FIELD_NAME (type, i),
729 stream, show - 1, level + 12, flags);
730 fprintf_filtered (stream, ";");
733 fprintf_filtered (stream, "\n%*send case;\n%*send record",
734 level + 4, "", level, "");
740 /* Print function or procedure type TYPE on STREAM. Make it a header
741 for function or procedure NAME if NAME is not null. */
744 print_func_type (struct type *type, struct ui_file *stream, const char *name,
745 const struct type_print_options *flags)
747 int i, len = TYPE_NFIELDS (type);
749 if (TYPE_CODE (TYPE_TARGET_TYPE (type)) == TYPE_CODE_VOID)
750 fprintf_filtered (stream, "procedure");
752 fprintf_filtered (stream, "function");
754 if (name != NULL && name[0] != '\0')
755 fprintf_filtered (stream, " %s", name);
759 fprintf_filtered (stream, " (");
760 for (i = 0; i < len; i += 1)
764 fputs_filtered ("; ", stream);
767 fprintf_filtered (stream, "a%d: ", i + 1);
768 ada_print_type (TYPE_FIELD_TYPE (type, i), "", stream, -1, 0,
771 fprintf_filtered (stream, ")");
774 if (TYPE_CODE (TYPE_TARGET_TYPE (type)) != TYPE_CODE_VOID)
776 fprintf_filtered (stream, " return ");
777 ada_print_type (TYPE_TARGET_TYPE (type), "", stream, 0, 0, flags);
782 /* Print a description of a type TYPE0.
783 Output goes to STREAM (via stdio).
784 If VARSTRING is a non-empty string, print as an Ada variable/field
786 SHOW+1 is the maximum number of levels of internal type structure
787 to show (this applies to record types, enumerated types, and
789 SHOW is the number of levels of internal type structure to show
790 when there is a type name for the SHOWth deepest level (0th is
792 When SHOW<0, no inner structure is shown.
793 LEVEL indicates level of recursion (for nested definitions). */
796 ada_print_type (struct type *type0, const char *varstring,
797 struct ui_file *stream, int show, int level,
798 const struct type_print_options *flags)
800 struct type *type = ada_check_typedef (ada_get_base_type (type0));
801 char *type_name = decoded_type_name (type0);
802 int is_var_decl = (varstring != NULL && varstring[0] != '\0');
807 fprintf_filtered (stream, "%.*s: ",
808 ada_name_prefix_len (varstring), varstring);
809 fprintf_filtered (stream, "<null type?>");
814 type = ada_check_typedef (type);
816 if (is_var_decl && TYPE_CODE (type) != TYPE_CODE_FUNC)
817 fprintf_filtered (stream, "%.*s: ",
818 ada_name_prefix_len (varstring), varstring);
820 if (type_name != NULL && show <= 0 && !ada_is_aligner_type (type))
822 fprintf_filtered (stream, "%.*s",
823 ada_name_prefix_len (type_name), type_name);
827 if (ada_is_aligner_type (type))
828 ada_print_type (ada_aligned_type (type), "", stream, show, level, flags);
829 else if (ada_is_constrained_packed_array_type (type)
830 && TYPE_CODE (type) != TYPE_CODE_PTR)
831 print_array_type (type, stream, show, level, flags);
833 switch (TYPE_CODE (type))
836 fprintf_filtered (stream, "<");
837 c_print_type (type, "", stream, show, level, flags);
838 fprintf_filtered (stream, ">");
841 case TYPE_CODE_TYPEDEF:
842 fprintf_filtered (stream, "access ");
843 ada_print_type (TYPE_TARGET_TYPE (type), "", stream, show, level,
847 fprintf_filtered (stream, "<ref> ");
848 ada_print_type (TYPE_TARGET_TYPE (type), "", stream, show, level,
851 case TYPE_CODE_ARRAY:
852 print_array_type (type, stream, show, level, flags);
855 fprintf_filtered (stream, "(false, true)");
858 if (ada_is_fixed_point_type (type))
859 print_fixed_point_type (type, stream);
862 const char *name = ada_type_name (type);
864 if (!ada_is_range_type_name (name))
865 fprintf_filtered (stream, _("<%d-byte integer>"),
869 fprintf_filtered (stream, "range ");
870 print_range_type (type, stream, 1 /* bounds_prefered_p */);
874 case TYPE_CODE_RANGE:
875 if (ada_is_fixed_point_type (type))
876 print_fixed_point_type (type, stream);
877 else if (ada_is_modular_type (type))
878 fprintf_filtered (stream, "mod %s",
879 int_string (ada_modulus (type), 10, 0, 0, 1));
882 fprintf_filtered (stream, "range ");
883 print_range (type, stream, 1 /* bounds_prefered_p */);
887 fprintf_filtered (stream, _("<%d-byte float>"), TYPE_LENGTH (type));
891 fprintf_filtered (stream, "(...)");
893 print_enum_type (type, stream);
895 case TYPE_CODE_STRUCT:
896 if (ada_is_array_descriptor_type (type))
897 print_array_type (type, stream, show, level, flags);
898 else if (ada_is_bogus_array_descriptor (type))
899 fprintf_filtered (stream,
900 _("array (?) of ? (<mal-formed descriptor>)"));
902 print_record_type (type, stream, show, level, flags);
904 case TYPE_CODE_UNION:
905 print_unchecked_union_type (type, stream, show, level, flags);
908 print_func_type (type, stream, varstring, flags);
913 /* Implement the la_print_typedef language method for Ada. */
916 ada_print_typedef (struct type *type, struct symbol *new_symbol,
917 struct ui_file *stream)
919 type = ada_check_typedef (type);
920 ada_print_type (type, "", stream, 0, 0, &type_print_raw_options);
921 fprintf_filtered (stream, "\n");