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"
40 static int print_selected_record_field_types (struct type
*, struct type
*,
42 struct ui_file
*, int, int,
43 const struct type_print_options
*);
45 static int print_record_field_types (struct type
*, struct type
*,
46 struct ui_file
*, int, int,
47 const struct type_print_options
*);
51 static char *name_buffer
;
52 static int name_buffer_len
;
54 /* The (decoded) Ada name of TYPE. This value persists until the
58 decoded_type_name (struct type
*type
)
60 if (ada_type_name (type
) == NULL
)
64 const char *raw_name
= ada_type_name (type
);
67 if (name_buffer
== NULL
|| name_buffer_len
<= strlen (raw_name
))
69 name_buffer_len
= 16 + 2 * strlen (raw_name
);
70 name_buffer
= xrealloc (name_buffer
, name_buffer_len
);
72 strcpy (name_buffer
, raw_name
);
74 s
= (char *) strstr (name_buffer
, "___");
78 s
= name_buffer
+ strlen (name_buffer
) - 1;
79 while (s
> name_buffer
&& (s
[0] != '_' || s
[-1] != '_'))
88 for (s
= q
= name_buffer
; *s
!= '\0'; q
+= 1)
90 if (s
[0] == '_' && s
[1] == '_')
106 /* Print TYPE on STREAM, preferably as a range. */
109 print_range (struct type
*type
, struct ui_file
*stream
)
111 switch (TYPE_CODE (type
))
113 case TYPE_CODE_RANGE
:
116 struct type
*target_type
;
118 target_type
= TYPE_TARGET_TYPE (type
);
119 if (target_type
== NULL
)
121 ada_print_scalar (target_type
, ada_discrete_type_low_bound (type
),
123 fprintf_filtered (stream
, " .. ");
124 ada_print_scalar (target_type
, ada_discrete_type_high_bound (type
),
129 fprintf_filtered (stream
, "%.*s",
130 ada_name_prefix_len (TYPE_NAME (type
)),
136 /* Print the number or discriminant bound at BOUNDS+*N on STREAM, and
137 set *N past the bound and its delimiter, if any. */
140 print_range_bound (struct type
*type
, char *bounds
, int *n
,
141 struct ui_file
*stream
)
145 if (ada_scan_number (bounds
, *n
, &B
, n
))
147 /* STABS decodes all range types which bounds are 0 .. -1 as
148 unsigned integers (ie. the type code is TYPE_CODE_INT, not
149 TYPE_CODE_RANGE). Unfortunately, ada_print_scalar() relies
150 on the unsigned flag to determine whether the bound should
151 be printed as a signed or an unsigned value. This causes
152 the upper bound of the 0 .. -1 range types to be printed as
153 a very large unsigned number instead of -1.
154 To workaround this stabs deficiency, we replace the TYPE by NULL
155 to indicate default output when we detect that the bound is negative,
156 and the type is a TYPE_CODE_INT. The bound is negative when
157 'm' is the last character of the number scanned in BOUNDS. */
158 if (bounds
[*n
- 1] == 'm' && TYPE_CODE (type
) == TYPE_CODE_INT
)
160 ada_print_scalar (type
, B
, stream
);
161 if (bounds
[*n
] == '_')
167 char *bound
= bounds
+ *n
;
170 pend
= strstr (bound
, "__");
172 *n
+= bound_len
= strlen (bound
);
175 bound_len
= pend
- bound
;
178 fprintf_filtered (stream
, "%.*s", bound_len
, bound
);
182 /* Assuming NAME[0 .. NAME_LEN-1] is the name of a range type, print
183 the value (if found) of the bound indicated by SUFFIX ("___L" or
184 "___U") according to the ___XD conventions. */
187 print_dynamic_range_bound (struct type
*type
, const char *name
, int name_len
,
188 const char *suffix
, struct ui_file
*stream
)
190 static char *name_buf
= NULL
;
191 static size_t name_buf_len
= 0;
195 GROW_VECT (name_buf
, name_buf_len
, name_len
+ strlen (suffix
) + 1);
196 strncpy (name_buf
, name
, name_len
);
197 strcpy (name_buf
+ name_len
, suffix
);
199 B
= get_int_var_value (name_buf
, &OK
);
201 ada_print_scalar (type
, B
, stream
);
203 fprintf_filtered (stream
, "?");
206 /* Print RAW_TYPE as a range type, using any bound information
207 following the GNAT encoding (if available). */
210 print_range_type (struct type
*raw_type
, struct ui_file
*stream
)
213 struct type
*base_type
;
214 const char *subtype_info
;
216 gdb_assert (raw_type
!= NULL
);
217 name
= TYPE_NAME (raw_type
);
218 gdb_assert (name
!= NULL
);
220 if (TYPE_CODE (raw_type
) == TYPE_CODE_RANGE
)
221 base_type
= TYPE_TARGET_TYPE (raw_type
);
223 base_type
= raw_type
;
225 subtype_info
= strstr (name
, "___XD");
226 if (subtype_info
== NULL
)
227 print_range (raw_type
, stream
);
230 int prefix_len
= subtype_info
- name
;
235 bounds_str
= strchr (subtype_info
, '_');
238 if (*subtype_info
== 'L')
240 print_range_bound (base_type
, bounds_str
, &n
, stream
);
244 print_dynamic_range_bound (base_type
, name
, prefix_len
, "___L",
247 fprintf_filtered (stream
, " .. ");
249 if (*subtype_info
== 'U')
250 print_range_bound (base_type
, bounds_str
, &n
, stream
);
252 print_dynamic_range_bound (base_type
, name
, prefix_len
, "___U",
257 /* Print enumerated type TYPE on STREAM. */
260 print_enum_type (struct type
*type
, struct ui_file
*stream
)
262 int len
= TYPE_NFIELDS (type
);
266 fprintf_filtered (stream
, "(");
270 for (i
= 0; i
< len
; i
++)
274 fprintf_filtered (stream
, ", ");
276 fputs_filtered (ada_enum_name (TYPE_FIELD_NAME (type
, i
)), stream
);
277 if (lastval
!= TYPE_FIELD_ENUMVAL (type
, i
))
279 fprintf_filtered (stream
, " => %s",
280 plongest (TYPE_FIELD_ENUMVAL (type
, i
)));
281 lastval
= TYPE_FIELD_ENUMVAL (type
, i
);
285 fprintf_filtered (stream
, ")");
288 /* Print representation of Ada fixed-point type TYPE on STREAM. */
291 print_fixed_point_type (struct type
*type
, struct ui_file
*stream
)
293 DOUBLEST delta
= ada_delta (type
);
294 DOUBLEST small
= ada_fixed_to_float (type
, 1.0);
297 fprintf_filtered (stream
, "delta ??");
300 fprintf_filtered (stream
, "delta %g", (double) delta
);
302 fprintf_filtered (stream
, " <'small = %g>", (double) small
);
306 /* Print simple (constrained) array type TYPE on STREAM. LEVEL is the
307 recursion (indentation) level, in case the element type itself has
308 nested structure, and SHOW is the number of levels of internal
309 structure to show (see ada_print_type). */
312 print_array_type (struct type
*type
, struct ui_file
*stream
, int show
,
313 int level
, const struct type_print_options
*flags
)
318 if (ada_is_constrained_packed_array_type (type
))
319 type
= ada_coerce_to_simple_array_type (type
);
322 fprintf_filtered (stream
, "array (");
326 fprintf_filtered (stream
, _("<undecipherable array type>"));
331 if (ada_is_simple_array_type (type
))
333 struct type
*range_desc_type
;
334 struct type
*arr_type
;
336 range_desc_type
= ada_find_parallel_type (type
, "___XA");
337 ada_fixup_array_indexes_type (range_desc_type
);
340 if (range_desc_type
== NULL
)
342 for (arr_type
= type
; TYPE_CODE (arr_type
) == TYPE_CODE_ARRAY
;
343 arr_type
= TYPE_TARGET_TYPE (arr_type
))
345 if (arr_type
!= type
)
346 fprintf_filtered (stream
, ", ");
347 print_range (TYPE_INDEX_TYPE (arr_type
), stream
);
348 if (TYPE_FIELD_BITSIZE (arr_type
, 0) > 0)
349 bitsize
= TYPE_FIELD_BITSIZE (arr_type
, 0);
356 n_indices
= TYPE_NFIELDS (range_desc_type
);
357 for (k
= 0, arr_type
= type
;
359 k
+= 1, arr_type
= TYPE_TARGET_TYPE (arr_type
))
362 fprintf_filtered (stream
, ", ");
363 print_range_type (TYPE_FIELD_TYPE (range_desc_type
, k
),
365 if (TYPE_FIELD_BITSIZE (arr_type
, 0) > 0)
366 bitsize
= TYPE_FIELD_BITSIZE (arr_type
, 0);
374 for (i
= i0
= ada_array_arity (type
); i
> 0; i
-= 1)
375 fprintf_filtered (stream
, "%s<>", i
== i0
? "" : ", ");
378 fprintf_filtered (stream
, ") of ");
380 ada_print_type (ada_array_element_type (type
, n_indices
), "", stream
,
381 show
== 0 ? 0 : show
- 1, level
+ 1, flags
);
383 fprintf_filtered (stream
, " <packed: %d-bit elements>", bitsize
);
386 /* Print the choices encoded by field FIELD_NUM of variant-part TYPE on
387 STREAM, assuming that VAL_TYPE (if non-NULL) is the type of the
388 values. Return non-zero if the field is an encoding of
389 discriminant values, as in a standard variant record, and 0 if the
390 field is not so encoded (as happens with single-component variants
391 in types annotated with pragma Unchecked_Variant). */
394 print_choices (struct type
*type
, int field_num
, struct ui_file
*stream
,
395 struct type
*val_type
)
399 const char *name
= TYPE_FIELD_NAME (type
, field_num
);
403 /* Skip over leading 'V': NOTE soon to be obsolete. */
406 if (!ada_scan_number (name
, 1, NULL
, &p
))
420 fprintf_filtered (stream
, " =>");
426 fprintf_filtered (stream
, " | ");
437 if (!ada_scan_number (name
, p
+ 1, &W
, &p
))
439 ada_print_scalar (val_type
, W
, stream
);
446 if (!ada_scan_number (name
, p
+ 1, &L
, &p
)
447 || name
[p
] != 'T' || !ada_scan_number (name
, p
+ 1, &U
, &p
))
449 ada_print_scalar (val_type
, L
, stream
);
450 fprintf_filtered (stream
, " .. ");
451 ada_print_scalar (val_type
, U
, stream
);
455 fprintf_filtered (stream
, "others");
462 fprintf_filtered (stream
, "?? =>");
466 /* Assuming that field FIELD_NUM of TYPE represents variants whose
467 discriminant is contained in OUTER_TYPE, print its components on STREAM.
468 LEVEL is the recursion (indentation) level, in case any of the fields
469 themselves have nested structure, and SHOW is the number of levels of
470 internal structure to show (see ada_print_type). For this purpose,
471 fields nested in a variant part are taken to be at the same level as
472 the fields immediately outside the variant part. */
475 print_variant_clauses (struct type
*type
, int field_num
,
476 struct type
*outer_type
, struct ui_file
*stream
,
478 const struct type_print_options
*flags
)
481 struct type
*var_type
, *par_type
;
482 struct type
*discr_type
;
484 var_type
= TYPE_FIELD_TYPE (type
, field_num
);
485 discr_type
= ada_variant_discrim_type (var_type
, outer_type
);
487 if (TYPE_CODE (var_type
) == TYPE_CODE_PTR
)
489 var_type
= TYPE_TARGET_TYPE (var_type
);
490 if (var_type
== NULL
|| TYPE_CODE (var_type
) != TYPE_CODE_UNION
)
494 par_type
= ada_find_parallel_type (var_type
, "___XVU");
495 if (par_type
!= NULL
)
498 for (i
= 0; i
< TYPE_NFIELDS (var_type
); i
+= 1)
500 fprintf_filtered (stream
, "\n%*swhen ", level
+ 4, "");
501 if (print_choices (var_type
, i
, stream
, discr_type
))
503 if (print_record_field_types (TYPE_FIELD_TYPE (var_type
, i
),
504 outer_type
, stream
, show
, level
+ 4,
507 fprintf_filtered (stream
, " null;");
510 print_selected_record_field_types (var_type
, outer_type
, i
, i
,
511 stream
, show
, level
+ 4, flags
);
515 /* Assuming that field FIELD_NUM of TYPE is a variant part whose
516 discriminants are contained in OUTER_TYPE, print a description of it
517 on STREAM. LEVEL is the recursion (indentation) level, in case any of
518 the fields themselves have nested structure, and SHOW is the number of
519 levels of internal structure to show (see ada_print_type). For this
520 purpose, fields nested in a variant part are taken to be at the same
521 level as the fields immediately outside the variant part. */
524 print_variant_part (struct type
*type
, int field_num
, struct type
*outer_type
,
525 struct ui_file
*stream
, int show
, int level
,
526 const struct type_print_options
*flags
)
528 fprintf_filtered (stream
, "\n%*scase %s is", level
+ 4, "",
529 ada_variant_discrim_name
530 (TYPE_FIELD_TYPE (type
, field_num
)));
531 print_variant_clauses (type
, field_num
, outer_type
, stream
, show
,
533 fprintf_filtered (stream
, "\n%*send case;", level
+ 4, "");
536 /* Print a description on STREAM of the fields FLD0 through FLD1 in
537 record or union type TYPE, whose discriminants are in OUTER_TYPE.
538 LEVEL is the recursion (indentation) level, in case any of the
539 fields themselves have nested structure, and SHOW is the number of
540 levels of internal structure to show (see ada_print_type). Does
541 not print parent type information of TYPE. Returns 0 if no fields
542 printed, -1 for an incomplete type, else > 0. Prints each field
543 beginning on a new line, but does not put a new line at end. */
546 print_selected_record_field_types (struct type
*type
, struct type
*outer_type
,
548 struct ui_file
*stream
, int show
, int level
,
549 const struct type_print_options
*flags
)
555 if (fld0
> fld1
&& TYPE_STUB (type
))
558 for (i
= fld0
; i
<= fld1
; i
+= 1)
562 if (ada_is_parent_field (type
, i
) || ada_is_ignored_field (type
, i
))
564 else if (ada_is_wrapper_field (type
, i
))
565 flds
+= print_record_field_types (TYPE_FIELD_TYPE (type
, i
), type
,
566 stream
, show
, level
, flags
);
567 else if (ada_is_variant_part (type
, i
))
569 print_variant_part (type
, i
, outer_type
, stream
, show
, level
, flags
);
575 fprintf_filtered (stream
, "\n%*s", level
+ 4, "");
576 ada_print_type (TYPE_FIELD_TYPE (type
, i
),
577 TYPE_FIELD_NAME (type
, i
),
578 stream
, show
- 1, level
+ 4, flags
);
579 fprintf_filtered (stream
, ";");
586 /* Print a description on STREAM of all fields of record or union type
587 TYPE, as for print_selected_record_field_types, above. */
590 print_record_field_types (struct type
*type
, struct type
*outer_type
,
591 struct ui_file
*stream
, int show
, int level
,
592 const struct type_print_options
*flags
)
594 return print_selected_record_field_types (type
, outer_type
,
595 0, TYPE_NFIELDS (type
) - 1,
596 stream
, show
, level
, flags
);
600 /* Print record type TYPE on STREAM. LEVEL is the recursion (indentation)
601 level, in case the element type itself has nested structure, and SHOW is
602 the number of levels of internal structure to show (see ada_print_type). */
605 print_record_type (struct type
*type0
, struct ui_file
*stream
, int show
,
606 int level
, const struct type_print_options
*flags
)
608 struct type
*parent_type
;
611 type
= ada_find_parallel_type (type0
, "___XVE");
615 parent_type
= ada_parent_type (type
);
616 if (ada_type_name (parent_type
) != NULL
)
618 const char *parent_name
= decoded_type_name (parent_type
);
620 /* If we fail to decode the parent type name, then use the parent
621 type name as is. Not pretty, but should never happen except
622 when the debugging info is incomplete or incorrect. This
623 prevents a crash trying to print a NULL pointer. */
624 if (parent_name
== NULL
)
625 parent_name
= ada_type_name (parent_type
);
626 fprintf_filtered (stream
, "new %s with record", parent_name
);
628 else if (parent_type
== NULL
&& ada_is_tagged_type (type
, 0))
629 fprintf_filtered (stream
, "tagged record");
631 fprintf_filtered (stream
, "record");
634 fprintf_filtered (stream
, " ... end record");
640 if (parent_type
!= NULL
&& ada_type_name (parent_type
) == NULL
)
641 flds
+= print_record_field_types (parent_type
, parent_type
,
642 stream
, show
, level
, flags
);
643 flds
+= print_record_field_types (type
, type
, stream
, show
, level
,
647 fprintf_filtered (stream
, "\n%*send record", level
, "");
649 fprintf_filtered (stream
, _(" <incomplete type> end record"));
651 fprintf_filtered (stream
, " null; end record");
655 /* Print the unchecked union type TYPE in something resembling Ada
656 format on STREAM. LEVEL is the recursion (indentation) level
657 in case the element type itself has nested structure, and SHOW is the
658 number of levels of internal structure to show (see ada_print_type). */
660 print_unchecked_union_type (struct type
*type
, struct ui_file
*stream
,
662 const struct type_print_options
*flags
)
665 fprintf_filtered (stream
, "record (?) is ... end record");
666 else if (TYPE_NFIELDS (type
) == 0)
667 fprintf_filtered (stream
, "record (?) is null; end record");
672 fprintf_filtered (stream
, "record (?) is\n%*scase ? is", level
+ 4, "");
674 for (i
= 0; i
< TYPE_NFIELDS (type
); i
+= 1)
676 fprintf_filtered (stream
, "\n%*swhen ? =>\n%*s", level
+ 8, "",
678 ada_print_type (TYPE_FIELD_TYPE (type
, i
),
679 TYPE_FIELD_NAME (type
, i
),
680 stream
, show
- 1, level
+ 12, flags
);
681 fprintf_filtered (stream
, ";");
684 fprintf_filtered (stream
, "\n%*send case;\n%*send record",
685 level
+ 4, "", level
, "");
691 /* Print function or procedure type TYPE on STREAM. Make it a header
692 for function or procedure NAME if NAME is not null. */
695 print_func_type (struct type
*type
, struct ui_file
*stream
, const char *name
,
696 const struct type_print_options
*flags
)
698 int i
, len
= TYPE_NFIELDS (type
);
700 if (TYPE_CODE (TYPE_TARGET_TYPE (type
)) == TYPE_CODE_VOID
)
701 fprintf_filtered (stream
, "procedure");
703 fprintf_filtered (stream
, "function");
705 if (name
!= NULL
&& name
[0] != '\0')
706 fprintf_filtered (stream
, " %s", name
);
710 fprintf_filtered (stream
, " (");
711 for (i
= 0; i
< len
; i
+= 1)
715 fputs_filtered ("; ", stream
);
718 fprintf_filtered (stream
, "a%d: ", i
+ 1);
719 ada_print_type (TYPE_FIELD_TYPE (type
, i
), "", stream
, -1, 0,
722 fprintf_filtered (stream
, ")");
725 if (TYPE_CODE (TYPE_TARGET_TYPE (type
)) != TYPE_CODE_VOID
)
727 fprintf_filtered (stream
, " return ");
728 ada_print_type (TYPE_TARGET_TYPE (type
), "", stream
, 0, 0, flags
);
733 /* Print a description of a type TYPE0.
734 Output goes to STREAM (via stdio).
735 If VARSTRING is a non-empty string, print as an Ada variable/field
737 SHOW+1 is the maximum number of levels of internal type structure
738 to show (this applies to record types, enumerated types, and
740 SHOW is the number of levels of internal type structure to show
741 when there is a type name for the SHOWth deepest level (0th is
743 When SHOW<0, no inner structure is shown.
744 LEVEL indicates level of recursion (for nested definitions). */
747 ada_print_type (struct type
*type0
, const char *varstring
,
748 struct ui_file
*stream
, int show
, int level
,
749 const struct type_print_options
*flags
)
751 struct type
*type
= ada_check_typedef (ada_get_base_type (type0
));
752 char *type_name
= decoded_type_name (type0
);
753 int is_var_decl
= (varstring
!= NULL
&& varstring
[0] != '\0');
758 fprintf_filtered (stream
, "%.*s: ",
759 ada_name_prefix_len (varstring
), varstring
);
760 fprintf_filtered (stream
, "<null type?>");
765 type
= ada_check_typedef (type
);
767 if (is_var_decl
&& TYPE_CODE (type
) != TYPE_CODE_FUNC
)
768 fprintf_filtered (stream
, "%.*s: ",
769 ada_name_prefix_len (varstring
), varstring
);
771 if (type_name
!= NULL
&& show
<= 0 && !ada_is_aligner_type (type
))
773 fprintf_filtered (stream
, "%.*s",
774 ada_name_prefix_len (type_name
), type_name
);
778 if (ada_is_aligner_type (type
))
779 ada_print_type (ada_aligned_type (type
), "", stream
, show
, level
, flags
);
780 else if (ada_is_constrained_packed_array_type (type
)
781 && TYPE_CODE (type
) != TYPE_CODE_PTR
)
782 print_array_type (type
, stream
, show
, level
, flags
);
784 switch (TYPE_CODE (type
))
787 fprintf_filtered (stream
, "<");
788 c_print_type (type
, "", stream
, show
, level
, flags
);
789 fprintf_filtered (stream
, ">");
792 case TYPE_CODE_TYPEDEF
:
793 fprintf_filtered (stream
, "access ");
794 ada_print_type (TYPE_TARGET_TYPE (type
), "", stream
, show
, level
,
798 fprintf_filtered (stream
, "<ref> ");
799 ada_print_type (TYPE_TARGET_TYPE (type
), "", stream
, show
, level
,
802 case TYPE_CODE_ARRAY
:
803 print_array_type (type
, stream
, show
, level
, flags
);
806 fprintf_filtered (stream
, "(false, true)");
809 if (ada_is_fixed_point_type (type
))
810 print_fixed_point_type (type
, stream
);
813 const char *name
= ada_type_name (type
);
815 if (!ada_is_range_type_name (name
))
816 fprintf_filtered (stream
, _("<%d-byte integer>"),
820 fprintf_filtered (stream
, "range ");
821 print_range_type (type
, stream
);
825 case TYPE_CODE_RANGE
:
826 if (ada_is_fixed_point_type (type
))
827 print_fixed_point_type (type
, stream
);
828 else if (ada_is_modular_type (type
))
829 fprintf_filtered (stream
, "mod %s",
830 int_string (ada_modulus (type
), 10, 0, 0, 1));
833 fprintf_filtered (stream
, "range ");
834 print_range (type
, stream
);
838 fprintf_filtered (stream
, _("<%d-byte float>"), TYPE_LENGTH (type
));
842 fprintf_filtered (stream
, "(...)");
844 print_enum_type (type
, stream
);
846 case TYPE_CODE_STRUCT
:
847 if (ada_is_array_descriptor_type (type
))
848 print_array_type (type
, stream
, show
, level
, flags
);
849 else if (ada_is_bogus_array_descriptor (type
))
850 fprintf_filtered (stream
,
851 _("array (?) of ? (<mal-formed descriptor>)"));
853 print_record_type (type
, stream
, show
, level
, flags
);
855 case TYPE_CODE_UNION
:
856 print_unchecked_union_type (type
, stream
, show
, level
, flags
);
859 print_func_type (type
, stream
, varstring
, flags
);
864 /* Implement the la_print_typedef language method for Ada. */
867 ada_print_typedef (struct type
*type
, struct symbol
*new_symbol
,
868 struct ui_file
*stream
)
870 type
= ada_check_typedef (type
);
871 ada_print_type (type
, "", stream
, 0, 0, &type_print_raw_options
);
872 fprintf_filtered (stream
, "\n");
This page took 0.04967 seconds and 5 git commands to generate.