1 /* Support for printing Ada types for GDB, the GNU debugger.
2 Copyright (C) 1986, 1988, 1989, 1991, 1997, 1998, 1999, 2000, 2001, 2002,
3 2003, 2004, 2007, 2008, 2009 Free Software Foundation, Inc.
5 This file is part of GDB.
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.
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.
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/>. */
21 #include "gdb_obstack.h"
22 #include "bfd.h" /* Binary File Description */
25 #include "expression.h"
34 #include "typeprint.h"
38 #include "gdb_string.h"
41 static int print_record_field_types (struct type
*, struct type
*,
42 struct ui_file
*, int, int);
44 static void print_array_type (struct type
*, struct ui_file
*, int, int);
46 static void print_choices (struct type
*, int, struct ui_file
*,
49 static void print_range (struct type
*, struct ui_file
*);
51 static void print_range_bound (struct type
*, char *, int *,
55 print_dynamic_range_bound (struct type
*, const char *, int,
56 const char *, struct ui_file
*);
58 static void print_range_type_named (char *, struct type
*, struct ui_file
*);
62 static char *name_buffer
;
63 static int name_buffer_len
;
65 /* The (decoded) Ada name of TYPE. This value persists until the
69 decoded_type_name (struct type
*type
)
71 if (ada_type_name (type
) == NULL
)
75 char *raw_name
= ada_type_name (type
);
78 if (name_buffer
== NULL
|| name_buffer_len
<= strlen (raw_name
))
80 name_buffer_len
= 16 + 2 * strlen (raw_name
);
81 name_buffer
= xrealloc (name_buffer
, name_buffer_len
);
83 strcpy (name_buffer
, raw_name
);
85 s
= (char *) strstr (name_buffer
, "___");
89 s
= name_buffer
+ strlen (name_buffer
) - 1;
90 while (s
> name_buffer
&& (s
[0] != '_' || s
[-1] != '_'))
99 for (s
= q
= name_buffer
; *s
!= '\0'; q
+= 1)
101 if (s
[0] == '_' && s
[1] == '_')
117 /* Print TYPE on STREAM, preferably as a range. */
120 print_range (struct type
*type
, struct ui_file
*stream
)
122 switch (TYPE_CODE (type
))
124 case TYPE_CODE_RANGE
:
127 struct type
*target_type
;
128 target_type
= TYPE_TARGET_TYPE (type
);
129 if (target_type
== NULL
)
131 ada_print_scalar (target_type
, ada_discrete_type_low_bound (type
),
133 fprintf_filtered (stream
, " .. ");
134 ada_print_scalar (target_type
, ada_discrete_type_high_bound (type
),
139 fprintf_filtered (stream
, "%.*s",
140 ada_name_prefix_len (TYPE_NAME (type
)),
146 /* Print the number or discriminant bound at BOUNDS+*N on STREAM, and
147 set *N past the bound and its delimiter, if any. */
150 print_range_bound (struct type
*type
, char *bounds
, int *n
,
151 struct ui_file
*stream
)
154 if (ada_scan_number (bounds
, *n
, &B
, n
))
156 /* STABS decodes all range types which bounds are 0 .. -1 as
157 unsigned integers (ie. the type code is TYPE_CODE_INT, not
158 TYPE_CODE_RANGE). Unfortunately, ada_print_scalar() relies
159 on the unsigned flag to determine whether the bound should
160 be printed as a signed or an unsigned value. This causes
161 the upper bound of the 0 .. -1 range types to be printed as
162 a very large unsigned number instead of -1.
163 To workaround this stabs deficiency, we replace the TYPE by NULL
164 to indicate default output when we detect that the bound is negative,
165 and the type is a TYPE_CODE_INT. The bound is negative when
166 'm' is the last character of the number scanned in BOUNDS. */
167 if (bounds
[*n
- 1] == 'm' && TYPE_CODE (type
) == TYPE_CODE_INT
)
169 ada_print_scalar (type
, B
, stream
);
170 if (bounds
[*n
] == '_')
176 char *bound
= bounds
+ *n
;
179 pend
= strstr (bound
, "__");
181 *n
+= bound_len
= strlen (bound
);
184 bound_len
= pend
- bound
;
187 fprintf_filtered (stream
, "%.*s", bound_len
, bound
);
191 /* Assuming NAME[0 .. NAME_LEN-1] is the name of a range type, print
192 the value (if found) of the bound indicated by SUFFIX ("___L" or
193 "___U") according to the ___XD conventions. */
196 print_dynamic_range_bound (struct type
*type
, const char *name
, int name_len
,
197 const char *suffix
, struct ui_file
*stream
)
199 static char *name_buf
= NULL
;
200 static size_t name_buf_len
= 0;
204 GROW_VECT (name_buf
, name_buf_len
, name_len
+ strlen (suffix
) + 1);
205 strncpy (name_buf
, name
, name_len
);
206 strcpy (name_buf
+ name_len
, suffix
);
208 B
= get_int_var_value (name_buf
, &OK
);
210 ada_print_scalar (type
, B
, stream
);
212 fprintf_filtered (stream
, "?");
215 /* Print the range type named NAME. If symbol lookup fails, fall back
216 to ORIG_TYPE as base type. */
219 print_range_type_named (char *name
, struct type
*orig_type
,
220 struct ui_file
*stream
)
222 struct type
*raw_type
= ada_find_any_type (name
);
223 struct type
*base_type
;
226 if (raw_type
== NULL
)
227 raw_type
= orig_type
;
229 if (TYPE_CODE (raw_type
) == TYPE_CODE_RANGE
)
230 base_type
= TYPE_TARGET_TYPE (raw_type
);
232 base_type
= raw_type
;
234 subtype_info
= strstr (name
, "___XD");
235 if (subtype_info
== NULL
)
236 print_range (raw_type
, stream
);
239 int prefix_len
= subtype_info
- name
;
244 bounds_str
= strchr (subtype_info
, '_');
247 if (*subtype_info
== 'L')
249 print_range_bound (base_type
, bounds_str
, &n
, stream
);
253 print_dynamic_range_bound (base_type
, name
, prefix_len
, "___L",
256 fprintf_filtered (stream
, " .. ");
258 if (*subtype_info
== 'U')
259 print_range_bound (base_type
, bounds_str
, &n
, stream
);
261 print_dynamic_range_bound (base_type
, name
, prefix_len
, "___U",
266 /* Print enumerated type TYPE on STREAM. */
269 print_enum_type (struct type
*type
, struct ui_file
*stream
)
271 int len
= TYPE_NFIELDS (type
);
274 fprintf_filtered (stream
, "(");
278 for (i
= 0; i
< len
; i
++)
282 fprintf_filtered (stream
, ", ");
284 fputs_filtered (ada_enum_name (TYPE_FIELD_NAME (type
, i
)), stream
);
285 if (lastval
!= TYPE_FIELD_BITPOS (type
, i
))
287 fprintf_filtered (stream
, " => %d", TYPE_FIELD_BITPOS (type
, i
));
288 lastval
= TYPE_FIELD_BITPOS (type
, i
);
292 fprintf_filtered (stream
, ")");
295 /* Print representation of Ada fixed-point type TYPE on STREAM. */
298 print_fixed_point_type (struct type
*type
, struct ui_file
*stream
)
300 DOUBLEST delta
= ada_delta (type
);
301 DOUBLEST small
= ada_fixed_to_float (type
, 1.0);
304 fprintf_filtered (stream
, "delta ??");
307 fprintf_filtered (stream
, "delta %g", (double) delta
);
309 fprintf_filtered (stream
, " <'small = %g>", (double) small
);
313 /* Print representation of special VAX floating-point type TYPE on STREAM. */
316 print_vax_floating_point_type (struct type
*type
, struct ui_file
*stream
)
318 fprintf_filtered (stream
, "<float format %c>",
319 ada_vax_float_type_suffix (type
));
322 /* Print simple (constrained) array type TYPE on STREAM. LEVEL is the
323 recursion (indentation) level, in case the element type itself has
324 nested structure, and SHOW is the number of levels of internal
325 structure to show (see ada_print_type). */
328 print_array_type (struct type
*type
, struct ui_file
*stream
, int show
,
334 if (ada_is_constrained_packed_array_type (type
))
335 type
= ada_coerce_to_simple_array_type (type
);
338 fprintf_filtered (stream
, "array (");
342 fprintf_filtered (stream
, _("<undecipherable array type>"));
348 fprintf_filtered (stream
, "...");
351 if (ada_is_simple_array_type (type
))
353 struct type
*range_desc_type
=
354 ada_find_parallel_type (type
, "___XA");
355 struct type
*arr_type
;
358 if (range_desc_type
== NULL
)
360 for (arr_type
= type
; TYPE_CODE (arr_type
) == TYPE_CODE_ARRAY
;
361 arr_type
= TYPE_TARGET_TYPE (arr_type
))
363 if (arr_type
!= type
)
364 fprintf_filtered (stream
, ", ");
365 print_range (TYPE_INDEX_TYPE (arr_type
), stream
);
366 if (TYPE_FIELD_BITSIZE (arr_type
, 0) > 0)
367 bitsize
= TYPE_FIELD_BITSIZE (arr_type
, 0);
373 n_indices
= TYPE_NFIELDS (range_desc_type
);
374 for (k
= 0, arr_type
= type
;
376 k
+= 1, arr_type
= TYPE_TARGET_TYPE (arr_type
))
379 fprintf_filtered (stream
, ", ");
380 print_range_type_named (TYPE_FIELD_NAME
381 (range_desc_type
, k
),
382 TYPE_INDEX_TYPE (arr_type
), stream
);
383 if (TYPE_FIELD_BITSIZE (arr_type
, 0) > 0)
384 bitsize
= TYPE_FIELD_BITSIZE (arr_type
, 0);
391 for (i
= i0
= ada_array_arity (type
); i
> 0; i
-= 1)
392 fprintf_filtered (stream
, "%s<>", i
== i0
? "" : ", ");
396 fprintf_filtered (stream
, ") of ");
398 ada_print_type (ada_array_element_type (type
, n_indices
), "", stream
,
399 show
== 0 ? 0 : show
- 1, level
+ 1);
401 fprintf_filtered (stream
, " <packed: %d-bit elements>", bitsize
);
404 /* Print the choices encoded by field FIELD_NUM of variant-part TYPE on
405 STREAM, assuming that VAL_TYPE (if non-NULL) is the type of the values. */
408 print_choices (struct type
*type
, int field_num
, struct ui_file
*stream
,
409 struct type
*val_type
)
413 const char *name
= TYPE_FIELD_NAME (type
, field_num
);
417 /* Skip over leading 'V': NOTE soon to be obsolete. */
420 if (!ada_scan_number (name
, 1, NULL
, &p
))
436 fprintf_filtered (stream
, " | ");
446 if (!ada_scan_number (name
, p
+ 1, &W
, &p
))
448 ada_print_scalar (val_type
, W
, stream
);
454 if (!ada_scan_number (name
, p
+ 1, &L
, &p
)
455 || name
[p
] != 'T' || !ada_scan_number (name
, p
+ 1, &U
, &p
))
457 ada_print_scalar (val_type
, L
, stream
);
458 fprintf_filtered (stream
, " .. ");
459 ada_print_scalar (val_type
, U
, stream
);
463 fprintf_filtered (stream
, "others");
470 fprintf_filtered (stream
, "??");
474 /* Assuming that field FIELD_NUM of TYPE is a VARIANTS field whose
475 discriminant is contained in OUTER_TYPE, print its variants on STREAM.
476 LEVEL is the recursion
477 (indentation) level, in case any of the fields themselves have
478 nested structure, and SHOW is the number of levels of internal structure
479 to show (see ada_print_type). For this purpose, fields nested in a
480 variant part are taken to be at the same level as the fields
481 immediately outside the variant part. */
484 print_variant_clauses (struct type
*type
, int field_num
,
485 struct type
*outer_type
, struct ui_file
*stream
,
489 struct type
*var_type
, *par_type
;
490 struct type
*discr_type
;
492 var_type
= TYPE_FIELD_TYPE (type
, field_num
);
493 discr_type
= ada_variant_discrim_type (var_type
, outer_type
);
495 if (TYPE_CODE (var_type
) == TYPE_CODE_PTR
)
497 var_type
= TYPE_TARGET_TYPE (var_type
);
498 if (var_type
== NULL
|| TYPE_CODE (var_type
) != TYPE_CODE_UNION
)
502 par_type
= ada_find_parallel_type (var_type
, "___XVU");
503 if (par_type
!= NULL
)
506 for (i
= 0; i
< TYPE_NFIELDS (var_type
); i
+= 1)
508 fprintf_filtered (stream
, "\n%*swhen ", level
+ 4, "");
509 print_choices (var_type
, i
, stream
, discr_type
);
510 fprintf_filtered (stream
, " =>");
511 if (print_record_field_types (TYPE_FIELD_TYPE (var_type
, i
),
512 outer_type
, stream
, show
, level
+ 4) <= 0)
513 fprintf_filtered (stream
, " null;");
517 /* Assuming that field FIELD_NUM of TYPE is a variant part whose
518 discriminants are contained in OUTER_TYPE, print a description of it
519 on STREAM. LEVEL is the recursion (indentation) level, in case any of
520 the fields themselves have nested structure, and SHOW is the number of
521 levels of internal structure to show (see ada_print_type). For this
522 purpose, fields nested in a variant part are taken to be at the same
523 level as the fields immediately outside the variant part. */
526 print_variant_part (struct type
*type
, int field_num
, struct type
*outer_type
,
527 struct ui_file
*stream
, int show
, int level
)
529 fprintf_filtered (stream
, "\n%*scase %s is", level
+ 4, "",
530 ada_variant_discrim_name
531 (TYPE_FIELD_TYPE (type
, field_num
)));
532 print_variant_clauses (type
, field_num
, outer_type
, stream
, show
,
534 fprintf_filtered (stream
, "\n%*send case;", level
+ 4, "");
537 /* Print a description on STREAM of the fields in record type TYPE, whose
538 discriminants are in OUTER_TYPE. LEVEL is the recursion (indentation)
539 level, in case any of the fields themselves have nested structure,
540 and SHOW is the number of levels of internal structure to show
541 (see ada_print_type). Does not print parent type information of TYPE.
542 Returns 0 if no fields printed, -1 for an incomplete type, else > 0.
543 Prints each field beginning on a new line, but does not put a new line at
547 print_record_field_types (struct type
*type
, struct type
*outer_type
,
548 struct ui_file
*stream
, int show
, int level
)
553 len
= TYPE_NFIELDS (type
);
555 if (len
== 0 && TYPE_STUB (type
))
558 for (i
= 0; i
< len
; 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
);
567 else if (ada_is_variant_part (type
, i
))
569 print_variant_part (type
, i
, outer_type
, stream
, show
, level
);
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);
579 fprintf_filtered (stream
, ";");
586 /* Print record type TYPE on STREAM. LEVEL is the recursion (indentation)
587 level, in case the element type itself has nested structure, and SHOW is
588 the number of levels of internal structure to show (see ada_print_type). */
591 print_record_type (struct type
*type0
, struct ui_file
*stream
, int show
,
594 struct type
*parent_type
;
597 type
= ada_find_parallel_type (type0
, "___XVE");
601 parent_type
= ada_parent_type (type
);
602 if (ada_type_name (parent_type
) != NULL
)
603 fprintf_filtered (stream
, "new %s with record",
604 decoded_type_name (parent_type
));
605 else if (parent_type
== NULL
&& ada_is_tagged_type (type
, 0))
606 fprintf_filtered (stream
, "tagged record");
608 fprintf_filtered (stream
, "record");
611 fprintf_filtered (stream
, " ... end record");
617 if (parent_type
!= NULL
&& ada_type_name (parent_type
) == NULL
)
618 flds
+= print_record_field_types (parent_type
, parent_type
,
619 stream
, show
, level
);
620 flds
+= print_record_field_types (type
, type
, stream
, show
, level
);
623 fprintf_filtered (stream
, "\n%*send record", level
, "");
625 fprintf_filtered (stream
, _(" <incomplete type> end record"));
627 fprintf_filtered (stream
, " null; end record");
631 /* Print the unchecked union type TYPE in something resembling Ada
632 format on STREAM. LEVEL is the recursion (indentation) level
633 in case the element type itself has nested structure, and SHOW is the
634 number of levels of internal structure to show (see ada_print_type). */
636 print_unchecked_union_type (struct type
*type
, struct ui_file
*stream
,
640 fprintf_filtered (stream
, "record (?) is ... end record");
641 else if (TYPE_NFIELDS (type
) == 0)
642 fprintf_filtered (stream
, "record (?) is null; end record");
647 fprintf_filtered (stream
, "record (?) is\n%*scase ? is", level
+ 4, "");
649 for (i
= 0; i
< TYPE_NFIELDS (type
); i
+= 1)
651 fprintf_filtered (stream
, "\n%*swhen ? =>\n%*s", level
+ 8, "",
653 ada_print_type (TYPE_FIELD_TYPE (type
, i
),
654 TYPE_FIELD_NAME (type
, i
),
655 stream
, show
- 1, level
+ 12);
656 fprintf_filtered (stream
, ";");
659 fprintf_filtered (stream
, "\n%*send case;\n%*send record",
660 level
+ 4, "", level
, "");
666 /* Print function or procedure type TYPE on STREAM. Make it a header
667 for function or procedure NAME if NAME is not null. */
670 print_func_type (struct type
*type
, struct ui_file
*stream
, char *name
)
672 int i
, len
= TYPE_NFIELDS (type
);
674 if (TYPE_CODE (TYPE_TARGET_TYPE (type
)) == TYPE_CODE_VOID
)
675 fprintf_filtered (stream
, "procedure");
677 fprintf_filtered (stream
, "function");
679 if (name
!= NULL
&& name
[0] != '\0')
680 fprintf_filtered (stream
, " %s", name
);
684 fprintf_filtered (stream
, " (");
685 for (i
= 0; i
< len
; i
+= 1)
689 fputs_filtered ("; ", stream
);
692 fprintf_filtered (stream
, "a%d: ", i
+ 1);
693 ada_print_type (TYPE_FIELD_TYPE (type
, i
), "", stream
, -1, 0);
695 fprintf_filtered (stream
, ")");
698 if (TYPE_CODE (TYPE_TARGET_TYPE (type
)) != TYPE_CODE_VOID
)
700 fprintf_filtered (stream
, " return ");
701 ada_print_type (TYPE_TARGET_TYPE (type
), "", stream
, 0, 0);
706 /* Print a description of a type TYPE0.
707 Output goes to STREAM (via stdio).
708 If VARSTRING is a non-empty string, print as an Ada variable/field
710 SHOW+1 is the maximum number of levels of internal type structure
711 to show (this applies to record types, enumerated types, and
713 SHOW is the number of levels of internal type structure to show
714 when there is a type name for the SHOWth deepest level (0th is
716 When SHOW<0, no inner structure is shown.
717 LEVEL indicates level of recursion (for nested definitions). */
720 ada_print_type (struct type
*type0
, char *varstring
, struct ui_file
*stream
,
723 struct type
*type
= ada_check_typedef (ada_get_base_type (type0
));
724 char *type_name
= decoded_type_name (type0
);
725 int is_var_decl
= (varstring
!= NULL
&& varstring
[0] != '\0');
730 fprintf_filtered (stream
, "%.*s: ",
731 ada_name_prefix_len (varstring
), varstring
);
732 fprintf_filtered (stream
, "<null type?>");
737 type
= ada_check_typedef (type
);
739 if (is_var_decl
&& TYPE_CODE (type
) != TYPE_CODE_FUNC
)
740 fprintf_filtered (stream
, "%.*s: ",
741 ada_name_prefix_len (varstring
), varstring
);
743 if (type_name
!= NULL
&& show
<= 0)
745 fprintf_filtered (stream
, "%.*s",
746 ada_name_prefix_len (type_name
), type_name
);
750 if (ada_is_aligner_type (type
))
751 ada_print_type (ada_aligned_type (type
), "", stream
, show
, level
);
752 else if (ada_is_constrained_packed_array_type (type
))
754 if (TYPE_CODE (type
) == TYPE_CODE_PTR
)
756 fprintf_filtered (stream
, "access ");
757 print_array_type (TYPE_TARGET_TYPE (type
), stream
, show
, level
);
761 print_array_type (type
, stream
, show
, level
);
765 switch (TYPE_CODE (type
))
768 fprintf_filtered (stream
, "<");
769 c_print_type (type
, "", stream
, show
, level
);
770 fprintf_filtered (stream
, ">");
773 fprintf_filtered (stream
, "access ");
774 ada_print_type (TYPE_TARGET_TYPE (type
), "", stream
, show
, level
);
777 fprintf_filtered (stream
, "<ref> ");
778 ada_print_type (TYPE_TARGET_TYPE (type
), "", stream
, show
, level
);
780 case TYPE_CODE_ARRAY
:
781 print_array_type (type
, stream
, show
, level
);
784 fprintf_filtered (stream
, "(false, true)");
787 if (ada_is_fixed_point_type (type
))
788 print_fixed_point_type (type
, stream
);
789 else if (ada_is_vax_floating_type (type
))
790 print_vax_floating_point_type (type
, stream
);
793 char *name
= ada_type_name (type
);
794 if (!ada_is_range_type_name (name
))
795 fprintf_filtered (stream
, _("<%d-byte integer>"),
799 fprintf_filtered (stream
, "range ");
800 print_range_type_named (name
, type
, stream
);
804 case TYPE_CODE_RANGE
:
805 if (ada_is_fixed_point_type (type
))
806 print_fixed_point_type (type
, stream
);
807 else if (ada_is_vax_floating_type (type
))
808 print_vax_floating_point_type (type
, stream
);
809 else if (ada_is_modular_type (type
))
810 fprintf_filtered (stream
, "mod %s",
811 int_string (ada_modulus (type
), 10, 0, 0, 1));
814 fprintf_filtered (stream
, "range ");
815 print_range (type
, stream
);
819 fprintf_filtered (stream
, _("<%d-byte float>"), TYPE_LENGTH (type
));
823 fprintf_filtered (stream
, "(...)");
825 print_enum_type (type
, stream
);
827 case TYPE_CODE_STRUCT
:
828 if (ada_is_array_descriptor_type (type
))
829 print_array_type (type
, stream
, show
, level
);
830 else if (ada_is_bogus_array_descriptor (type
))
831 fprintf_filtered (stream
,
832 _("array (?) of ? (<mal-formed descriptor>)"));
834 print_record_type (type
, stream
, show
, level
);
836 case TYPE_CODE_UNION
:
837 print_unchecked_union_type (type
, stream
, show
, level
);
840 print_func_type (type
, stream
, varstring
);
This page took 0.04772 seconds and 4 git commands to generate.