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 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 range type TYPE on STREAM. */
120 print_range (struct type
*type
, struct ui_file
*stream
)
122 struct type
*target_type
;
123 target_type
= TYPE_TARGET_TYPE (type
);
124 if (target_type
== NULL
)
127 switch (TYPE_CODE (target_type
))
129 case TYPE_CODE_RANGE
:
136 target_type
= builtin_type_int32
;
140 if (TYPE_NFIELDS (type
) < 2)
142 /* A range needs at least 2 bounds to be printed. If there are less
143 than 2, just print the type name instead of the range itself.
144 This check handles cases such as characters, for example.
146 If the name is not defined, then we don't print anything.
148 fprintf_filtered (stream
, "%.*s",
149 ada_name_prefix_len (TYPE_NAME (type
)),
154 /* We extract the range type bounds respectively from the first element
155 and the last element of the type->fields array */
156 const LONGEST lower_bound
= (LONGEST
) TYPE_LOW_BOUND (type
);
157 const LONGEST upper_bound
= (TYPE_CODE (type
) == TYPE_CODE_RANGE
158 ? (LONGEST
) TYPE_HIGH_BOUND (type
)
159 : (LONGEST
) TYPE_FIELD_BITPOS (type
, TYPE_NFIELDS (type
) - 1));
161 ada_print_scalar (target_type
, lower_bound
, stream
);
162 fprintf_filtered (stream
, " .. ");
163 ada_print_scalar (target_type
, upper_bound
, stream
);
167 /* Print the number or discriminant bound at BOUNDS+*N on STREAM, and
168 set *N past the bound and its delimiter, if any. */
171 print_range_bound (struct type
*type
, char *bounds
, int *n
,
172 struct ui_file
*stream
)
175 if (ada_scan_number (bounds
, *n
, &B
, n
))
177 /* STABS decodes all range types which bounds are 0 .. -1 as
178 unsigned integers (ie. the type code is TYPE_CODE_INT, not
179 TYPE_CODE_RANGE). Unfortunately, ada_print_scalar() relies
180 on the unsigned flag to determine whether the bound should
181 be printed as a signed or an unsigned value. This causes
182 the upper bound of the 0 .. -1 range types to be printed as
183 a very large unsigned number instead of -1.
184 To workaround this stabs deficiency, we replace the TYPE by
185 builtin_type_int32 when we detect that the bound is negative,
186 and the type is a TYPE_CODE_INT. The bound is negative when
187 'm' is the last character of the number scanned in BOUNDS. */
188 if (bounds
[*n
- 1] == 'm' && TYPE_CODE (type
) == TYPE_CODE_INT
)
189 type
= builtin_type_int32
;
190 ada_print_scalar (type
, B
, stream
);
191 if (bounds
[*n
] == '_')
197 char *bound
= bounds
+ *n
;
200 pend
= strstr (bound
, "__");
202 *n
+= bound_len
= strlen (bound
);
205 bound_len
= pend
- bound
;
208 fprintf_filtered (stream
, "%.*s", bound_len
, bound
);
212 /* Assuming NAME[0 .. NAME_LEN-1] is the name of a range type, print
213 the value (if found) of the bound indicated by SUFFIX ("___L" or
214 "___U") according to the ___XD conventions. */
217 print_dynamic_range_bound (struct type
*type
, const char *name
, int name_len
,
218 const char *suffix
, struct ui_file
*stream
)
220 static char *name_buf
= NULL
;
221 static size_t name_buf_len
= 0;
225 GROW_VECT (name_buf
, name_buf_len
, name_len
+ strlen (suffix
) + 1);
226 strncpy (name_buf
, name
, name_len
);
227 strcpy (name_buf
+ name_len
, suffix
);
229 B
= get_int_var_value (name_buf
, &OK
);
231 ada_print_scalar (type
, B
, stream
);
233 fprintf_filtered (stream
, "?");
236 /* Print the range type named NAME. */
239 print_range_type_named (char *name
, struct ui_file
*stream
)
241 struct type
*raw_type
= ada_find_any_type (name
);
242 struct type
*base_type
;
245 if (raw_type
== NULL
)
246 base_type
= builtin_type_int32
;
247 else if (TYPE_CODE (raw_type
) == TYPE_CODE_RANGE
)
248 base_type
= TYPE_TARGET_TYPE (raw_type
);
250 base_type
= raw_type
;
252 subtype_info
= strstr (name
, "___XD");
253 if (subtype_info
== NULL
&& raw_type
== NULL
)
254 fprintf_filtered (stream
, "? .. ?");
255 else if (subtype_info
== NULL
)
256 print_range (raw_type
, stream
);
259 int prefix_len
= subtype_info
- name
;
264 bounds_str
= strchr (subtype_info
, '_');
267 if (*subtype_info
== 'L')
269 print_range_bound (base_type
, bounds_str
, &n
, stream
);
273 print_dynamic_range_bound (base_type
, name
, prefix_len
, "___L",
276 fprintf_filtered (stream
, " .. ");
278 if (*subtype_info
== 'U')
279 print_range_bound (base_type
, bounds_str
, &n
, stream
);
281 print_dynamic_range_bound (base_type
, name
, prefix_len
, "___U",
286 /* Print enumerated type TYPE on STREAM. */
289 print_enum_type (struct type
*type
, struct ui_file
*stream
)
291 int len
= TYPE_NFIELDS (type
);
294 fprintf_filtered (stream
, "(");
298 for (i
= 0; i
< len
; i
++)
302 fprintf_filtered (stream
, ", ");
304 fputs_filtered (ada_enum_name (TYPE_FIELD_NAME (type
, i
)), stream
);
305 if (lastval
!= TYPE_FIELD_BITPOS (type
, i
))
307 fprintf_filtered (stream
, " => %d", TYPE_FIELD_BITPOS (type
, i
));
308 lastval
= TYPE_FIELD_BITPOS (type
, i
);
312 fprintf_filtered (stream
, ")");
315 /* Print representation of Ada fixed-point type TYPE on STREAM. */
318 print_fixed_point_type (struct type
*type
, struct ui_file
*stream
)
320 DOUBLEST delta
= ada_delta (type
);
321 DOUBLEST small
= ada_fixed_to_float (type
, 1.0);
324 fprintf_filtered (stream
, "delta ??");
327 fprintf_filtered (stream
, "delta %g", (double) delta
);
329 fprintf_filtered (stream
, " <'small = %g>", (double) small
);
333 /* Print representation of special VAX floating-point type TYPE on STREAM. */
336 print_vax_floating_point_type (struct type
*type
, struct ui_file
*stream
)
338 fprintf_filtered (stream
, "<float format %c>",
339 ada_vax_float_type_suffix (type
));
342 /* Print simple (constrained) array type TYPE on STREAM. LEVEL is the
343 recursion (indentation) level, in case the element type itself has
344 nested structure, and SHOW is the number of levels of internal
345 structure to show (see ada_print_type). */
348 print_array_type (struct type
*type
, struct ui_file
*stream
, int show
,
354 if (ada_is_packed_array_type (type
))
355 type
= ada_coerce_to_simple_array_type (type
);
358 fprintf_filtered (stream
, "array (");
362 fprintf_filtered (stream
, "...");
367 fprintf_filtered (stream
, _("<undecipherable array type>"));
370 if (ada_is_simple_array_type (type
))
372 struct type
*range_desc_type
=
373 ada_find_parallel_type (type
, "___XA");
374 struct type
*arr_type
;
377 if (range_desc_type
== NULL
)
379 for (arr_type
= type
; TYPE_CODE (arr_type
) == TYPE_CODE_ARRAY
;
380 arr_type
= TYPE_TARGET_TYPE (arr_type
))
382 if (arr_type
!= type
)
383 fprintf_filtered (stream
, ", ");
384 print_range (TYPE_INDEX_TYPE (arr_type
), stream
);
385 if (TYPE_FIELD_BITSIZE (arr_type
, 0) > 0)
386 bitsize
= TYPE_FIELD_BITSIZE (arr_type
, 0);
392 n_indices
= TYPE_NFIELDS (range_desc_type
);
393 for (k
= 0, arr_type
= type
;
395 k
+= 1, arr_type
= TYPE_TARGET_TYPE (arr_type
))
398 fprintf_filtered (stream
, ", ");
399 print_range_type_named (TYPE_FIELD_NAME
400 (range_desc_type
, k
), stream
);
401 if (TYPE_FIELD_BITSIZE (arr_type
, 0) > 0)
402 bitsize
= TYPE_FIELD_BITSIZE (arr_type
, 0);
409 for (i
= i0
= ada_array_arity (type
); i
> 0; i
-= 1)
410 fprintf_filtered (stream
, "%s<>", i
== i0
? "" : ", ");
414 fprintf_filtered (stream
, ") of ");
416 ada_print_type (ada_array_element_type (type
, n_indices
), "", stream
,
417 show
== 0 ? 0 : show
- 1, level
+ 1);
419 fprintf_filtered (stream
, " <packed: %d-bit elements>", bitsize
);
422 /* Print the choices encoded by field FIELD_NUM of variant-part TYPE on
423 STREAM, assuming the VAL_TYPE is the type of the values. */
426 print_choices (struct type
*type
, int field_num
, struct ui_file
*stream
,
427 struct type
*val_type
)
431 const char *name
= TYPE_FIELD_NAME (type
, field_num
);
435 /* Skip over leading 'V': NOTE soon to be obsolete. */
438 if (!ada_scan_number (name
, 1, NULL
, &p
))
454 fprintf_filtered (stream
, " | ");
464 if (!ada_scan_number (name
, p
+ 1, &W
, &p
))
466 ada_print_scalar (val_type
, W
, stream
);
472 if (!ada_scan_number (name
, p
+ 1, &L
, &p
)
473 || name
[p
] != 'T' || !ada_scan_number (name
, p
+ 1, &U
, &p
))
475 ada_print_scalar (val_type
, L
, stream
);
476 fprintf_filtered (stream
, " .. ");
477 ada_print_scalar (val_type
, U
, stream
);
481 fprintf_filtered (stream
, "others");
488 fprintf_filtered (stream
, "??");
492 /* Assuming that field FIELD_NUM of TYPE is a VARIANTS field whose
493 discriminant is contained in OUTER_TYPE, print its variants on STREAM.
494 LEVEL is the recursion
495 (indentation) level, in case any of the fields themselves have
496 nested structure, and SHOW is the number of levels of internal structure
497 to show (see ada_print_type). For this purpose, fields nested in a
498 variant part are taken to be at the same level as the fields
499 immediately outside the variant part. */
502 print_variant_clauses (struct type
*type
, int field_num
,
503 struct type
*outer_type
, struct ui_file
*stream
,
507 struct type
*var_type
, *par_type
;
508 struct type
*discr_type
;
510 var_type
= TYPE_FIELD_TYPE (type
, field_num
);
511 discr_type
= ada_variant_discrim_type (var_type
, outer_type
);
513 if (TYPE_CODE (var_type
) == TYPE_CODE_PTR
)
515 var_type
= TYPE_TARGET_TYPE (var_type
);
516 if (var_type
== NULL
|| TYPE_CODE (var_type
) != TYPE_CODE_UNION
)
520 par_type
= ada_find_parallel_type (var_type
, "___XVU");
521 if (par_type
!= NULL
)
524 for (i
= 0; i
< TYPE_NFIELDS (var_type
); i
+= 1)
526 fprintf_filtered (stream
, "\n%*swhen ", level
+ 4, "");
527 print_choices (var_type
, i
, stream
, discr_type
);
528 fprintf_filtered (stream
, " =>");
529 if (print_record_field_types (TYPE_FIELD_TYPE (var_type
, i
),
530 outer_type
, stream
, show
, level
+ 4) <= 0)
531 fprintf_filtered (stream
, " null;");
535 /* Assuming that field FIELD_NUM of TYPE is a variant part whose
536 discriminants are contained in OUTER_TYPE, print a description of it
537 on STREAM. LEVEL is the recursion (indentation) level, in case any of
538 the fields themselves have nested structure, and SHOW is the number of
539 levels of internal structure to show (see ada_print_type). For this
540 purpose, fields nested in a variant part are taken to be at the same
541 level as the fields immediately outside the variant part. */
544 print_variant_part (struct type
*type
, int field_num
, struct type
*outer_type
,
545 struct ui_file
*stream
, int show
, int level
)
547 fprintf_filtered (stream
, "\n%*scase %s is", level
+ 4, "",
548 ada_variant_discrim_name
549 (TYPE_FIELD_TYPE (type
, field_num
)));
550 print_variant_clauses (type
, field_num
, outer_type
, stream
, show
,
552 fprintf_filtered (stream
, "\n%*send case;", level
+ 4, "");
555 /* Print a description on STREAM of the fields in record type TYPE, whose
556 discriminants are in OUTER_TYPE. LEVEL is the recursion (indentation)
557 level, in case any of the fields themselves have nested structure,
558 and SHOW is the number of levels of internal structure to show
559 (see ada_print_type). Does not print parent type information of TYPE.
560 Returns 0 if no fields printed, -1 for an incomplete type, else > 0.
561 Prints each field beginning on a new line, but does not put a new line at
565 print_record_field_types (struct type
*type
, struct type
*outer_type
,
566 struct ui_file
*stream
, int show
, int level
)
571 len
= TYPE_NFIELDS (type
);
573 if (len
== 0 && TYPE_STUB (type
))
576 for (i
= 0; i
< len
; i
+= 1)
580 if (ada_is_parent_field (type
, i
) || ada_is_ignored_field (type
, i
))
582 else if (ada_is_wrapper_field (type
, i
))
583 flds
+= print_record_field_types (TYPE_FIELD_TYPE (type
, i
), type
,
584 stream
, show
, level
);
585 else if (ada_is_variant_part (type
, i
))
587 print_variant_part (type
, i
, outer_type
, stream
, show
, level
);
593 fprintf_filtered (stream
, "\n%*s", level
+ 4, "");
594 ada_print_type (TYPE_FIELD_TYPE (type
, i
),
595 TYPE_FIELD_NAME (type
, i
),
596 stream
, show
- 1, level
+ 4);
597 fprintf_filtered (stream
, ";");
604 /* Print record type TYPE on STREAM. LEVEL is the recursion (indentation)
605 level, in case the element type itself has nested structure, and SHOW is
606 the number of levels of internal structure to show (see ada_print_type). */
609 print_record_type (struct type
*type0
, struct ui_file
*stream
, int show
,
612 struct type
*parent_type
;
615 type
= ada_find_parallel_type (type0
, "___XVE");
619 parent_type
= ada_parent_type (type
);
620 if (ada_type_name (parent_type
) != NULL
)
621 fprintf_filtered (stream
, "new %s with record",
622 decoded_type_name (parent_type
));
623 else if (parent_type
== NULL
&& ada_is_tagged_type (type
, 0))
624 fprintf_filtered (stream
, "tagged record");
626 fprintf_filtered (stream
, "record");
629 fprintf_filtered (stream
, " ... end record");
635 if (parent_type
!= NULL
&& ada_type_name (parent_type
) == NULL
)
636 flds
+= print_record_field_types (parent_type
, parent_type
,
637 stream
, show
, level
);
638 flds
+= print_record_field_types (type
, type
, stream
, show
, level
);
641 fprintf_filtered (stream
, "\n%*send record", level
, "");
643 fprintf_filtered (stream
, _(" <incomplete type> end record"));
645 fprintf_filtered (stream
, " null; end record");
649 /* Print the unchecked union type TYPE in something resembling Ada
650 format on STREAM. LEVEL is the recursion (indentation) level
651 in case the element type itself has nested structure, and SHOW is the
652 number of levels of internal structure to show (see ada_print_type). */
654 print_unchecked_union_type (struct type
*type
, struct ui_file
*stream
,
658 fprintf_filtered (stream
, "record (?) is ... end record");
659 else if (TYPE_NFIELDS (type
) == 0)
660 fprintf_filtered (stream
, "record (?) is null; end record");
665 fprintf_filtered (stream
, "record (?) is\n%*scase ? is", level
+ 4, "");
667 for (i
= 0; i
< TYPE_NFIELDS (type
); i
+= 1)
669 fprintf_filtered (stream
, "\n%*swhen ? =>\n%*s", level
+ 8, "",
671 ada_print_type (TYPE_FIELD_TYPE (type
, i
),
672 TYPE_FIELD_NAME (type
, i
),
673 stream
, show
- 1, level
+ 12);
674 fprintf_filtered (stream
, ";");
677 fprintf_filtered (stream
, "\n%*send case;\n%*send record",
678 level
+ 4, "", level
, "");
684 /* Print function or procedure type TYPE on STREAM. Make it a header
685 for function or procedure NAME if NAME is not null. */
688 print_func_type (struct type
*type
, struct ui_file
*stream
, char *name
)
690 int i
, len
= TYPE_NFIELDS (type
);
692 if (TYPE_CODE (TYPE_TARGET_TYPE (type
)) == TYPE_CODE_VOID
)
693 fprintf_filtered (stream
, "procedure");
695 fprintf_filtered (stream
, "function");
697 if (name
!= NULL
&& name
[0] != '\0')
698 fprintf_filtered (stream
, " %s", name
);
702 fprintf_filtered (stream
, " (");
703 for (i
= 0; i
< len
; i
+= 1)
707 fputs_filtered ("; ", stream
);
710 fprintf_filtered (stream
, "a%d: ", i
+ 1);
711 ada_print_type (TYPE_FIELD_TYPE (type
, i
), "", stream
, -1, 0);
713 fprintf_filtered (stream
, ")");
716 if (TYPE_CODE (TYPE_TARGET_TYPE (type
)) != TYPE_CODE_VOID
)
718 fprintf_filtered (stream
, " return ");
719 ada_print_type (TYPE_TARGET_TYPE (type
), "", stream
, 0, 0);
724 /* Print a description of a type TYPE0.
725 Output goes to STREAM (via stdio).
726 If VARSTRING is a non-empty string, print as an Ada variable/field
728 SHOW+1 is the maximum number of levels of internal type structure
729 to show (this applies to record types, enumerated types, and
731 SHOW is the number of levels of internal type structure to show
732 when there is a type name for the SHOWth deepest level (0th is
734 When SHOW<0, no inner structure is shown.
735 LEVEL indicates level of recursion (for nested definitions). */
738 ada_print_type (struct type
*type0
, char *varstring
, struct ui_file
*stream
,
741 struct type
*type
= ada_check_typedef (ada_get_base_type (type0
));
742 char *type_name
= decoded_type_name (type0
);
743 int is_var_decl
= (varstring
!= NULL
&& varstring
[0] != '\0');
748 fprintf_filtered (stream
, "%.*s: ",
749 ada_name_prefix_len (varstring
), varstring
);
750 fprintf_filtered (stream
, "<null type?>");
755 type
= ada_check_typedef (type
);
757 if (is_var_decl
&& TYPE_CODE (type
) != TYPE_CODE_FUNC
)
758 fprintf_filtered (stream
, "%.*s: ",
759 ada_name_prefix_len (varstring
), varstring
);
761 if (type_name
!= NULL
&& show
<= 0)
763 fprintf_filtered (stream
, "%.*s",
764 ada_name_prefix_len (type_name
), type_name
);
768 if (ada_is_aligner_type (type
))
769 ada_print_type (ada_aligned_type (type
), "", stream
, show
, level
);
770 else if (ada_is_packed_array_type (type
))
772 if (TYPE_CODE (type
) == TYPE_CODE_PTR
)
774 fprintf_filtered (stream
, "access ");
775 print_array_type (TYPE_TARGET_TYPE (type
), stream
, show
, level
);
779 print_array_type (type
, stream
, show
, level
);
783 switch (TYPE_CODE (type
))
786 fprintf_filtered (stream
, "<");
787 c_print_type (type
, "", stream
, show
, level
);
788 fprintf_filtered (stream
, ">");
791 fprintf_filtered (stream
, "access ");
792 ada_print_type (TYPE_TARGET_TYPE (type
), "", stream
, show
, level
);
795 fprintf_filtered (stream
, "<ref> ");
796 ada_print_type (TYPE_TARGET_TYPE (type
), "", stream
, show
, level
);
798 case TYPE_CODE_ARRAY
:
799 print_array_type (type
, stream
, show
, level
);
802 fprintf_filtered (stream
, "(false, true)");
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
);
811 char *name
= ada_type_name (type
);
812 if (!ada_is_range_type_name (name
))
813 fprintf_filtered (stream
, _("<%d-byte integer>"),
817 fprintf_filtered (stream
, "range ");
818 print_range_type_named (name
, stream
);
822 case TYPE_CODE_RANGE
:
823 if (ada_is_fixed_point_type (type
))
824 print_fixed_point_type (type
, stream
);
825 else if (ada_is_vax_floating_type (type
))
826 print_vax_floating_point_type (type
, stream
);
827 else if (ada_is_modular_type (type
))
828 fprintf_filtered (stream
, "mod %s",
829 int_string (ada_modulus (type
), 10, 0, 0, 1));
832 fprintf_filtered (stream
, "range ");
833 print_range (type
, stream
);
837 fprintf_filtered (stream
, _("<%d-byte float>"), TYPE_LENGTH (type
));
841 fprintf_filtered (stream
, "(...)");
843 print_enum_type (type
, stream
);
845 case TYPE_CODE_STRUCT
:
846 if (ada_is_array_descriptor_type (type
))
847 print_array_type (type
, stream
, show
, level
);
848 else if (ada_is_bogus_array_descriptor (type
))
849 fprintf_filtered (stream
,
850 _("array (?) of ? (<mal-formed descriptor>)"));
852 print_record_type (type
, stream
, show
, level
);
854 case TYPE_CODE_UNION
:
855 print_unchecked_union_type (type
, stream
, show
, level
);
858 print_func_type (type
, stream
, varstring
);
This page took 0.047571 seconds and 4 git commands to generate.