1 /* Support for printing Ada types for GDB, the GNU debugger.
2 Copyright 1986, 1988, 1989, 1991, 1997, 1998, 1999, 2000,
3 2001, 2002, 2003, 2004 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 2 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, write to the Free Software
19 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
22 #include "gdb_obstack.h"
23 #include "bfd.h" /* Binary File Description */
26 #include "expression.h"
35 #include "typeprint.h"
39 #include "gdb_string.h"
42 static int print_record_field_types (struct type *, struct type *,
43 struct ui_file *, int, int);
45 static void print_array_type (struct type *, struct ui_file *, int, int);
47 static void print_choices (struct type *, int, struct ui_file *,
50 static void print_range (struct type *, struct ui_file *);
52 static void print_range_bound (struct type *, char *, int *,
56 print_dynamic_range_bound (struct type *, const char *, int,
57 const char *, struct ui_file *);
59 static void print_range_type_named (char *, struct ui_file *);
63 static char *name_buffer;
64 static int name_buffer_len;
66 /* The (decoded) Ada name of TYPE. This value persists until the
70 decoded_type_name (struct type *type)
72 if (ada_type_name (type) == NULL)
76 char *raw_name = ada_type_name (type);
79 if (name_buffer == NULL || name_buffer_len <= strlen (raw_name))
81 name_buffer_len = 16 + 2 * strlen (raw_name);
82 name_buffer = xrealloc (name_buffer, name_buffer_len);
84 strcpy (name_buffer, raw_name);
86 s = (char *) strstr (name_buffer, "___");
90 s = name_buffer + strlen (name_buffer) - 1;
91 while (s > name_buffer && (s[0] != '_' || s[-1] != '_'))
100 for (s = q = name_buffer; *s != '\0'; q += 1)
102 if (s[0] == '_' && s[1] == '_')
119 /* Print a description of a type in the format of a
120 typedef for the current language.
121 NEW is the new name for a type TYPE. */
124 ada_typedef_print (struct type *type, struct symbol *new,
125 struct ui_file *stream)
127 /* XXX: type_sprint */
128 fprintf_filtered (stream, "type %.*s is ",
129 ada_name_prefix_len (SYMBOL_PRINT_NAME (new)),
130 SYMBOL_PRINT_NAME (new));
131 type_print (type, "", stream, 1);
134 /* Print range type TYPE on STREAM. */
137 print_range (struct type *type, struct ui_file *stream)
139 struct type *target_type;
140 target_type = TYPE_TARGET_TYPE (type);
141 if (target_type == NULL)
144 switch (TYPE_CODE (target_type))
146 case TYPE_CODE_RANGE:
153 target_type = builtin_type_int;
157 if (TYPE_NFIELDS (type) < 2)
159 /* A range needs at least 2 bounds to be printed. If there are less
160 than 2, just print the type name instead of the range itself.
161 This check handles cases such as characters, for example.
163 Note that if the name is not defined, then we don't print anything.
165 fprintf_filtered (stream, "%.*s",
166 ada_name_prefix_len (TYPE_NAME (type)),
171 /* We extract the range type bounds respectively from the first element
172 and the last element of the type->fields array */
173 const LONGEST lower_bound = (LONGEST) TYPE_LOW_BOUND (type);
174 const LONGEST upper_bound =
175 (LONGEST) TYPE_FIELD_BITPOS (type, TYPE_NFIELDS (type) - 1);
177 ada_print_scalar (target_type, lower_bound, stream);
178 fprintf_filtered (stream, " .. ");
179 ada_print_scalar (target_type, upper_bound, stream);
183 /* Print the number or discriminant bound at BOUNDS+*N on STREAM, and
184 set *N past the bound and its delimiter, if any. */
187 print_range_bound (struct type *type, char *bounds, int *n,
188 struct ui_file *stream)
191 if (ada_scan_number (bounds, *n, &B, n))
193 /* STABS decodes all range types which bounds are 0 .. -1 as
194 unsigned integers (ie. the type code is TYPE_CODE_INT, not
195 TYPE_CODE_RANGE). Unfortunately, ada_print_scalar() relies
196 on the unsigned flag to determine whether the bound should
197 be printed as a signed or an unsigned value. This causes
198 the upper bound of the 0 .. -1 range types to be printed as
199 a very large unsigned number instead of -1.
200 To workaround this stabs deficiency, we replace the TYPE by
201 builtin_type_long when we detect that the bound is negative,
202 and the type is a TYPE_CODE_INT. The bound is negative when
203 'm' is the last character of the number scanned in BOUNDS. */
204 if (bounds[*n - 1] == 'm' && TYPE_CODE (type) == TYPE_CODE_INT)
205 type = builtin_type_long;
206 ada_print_scalar (type, B, stream);
207 if (bounds[*n] == '_')
213 char *bound = bounds + *n;
216 pend = strstr (bound, "__");
218 *n += bound_len = strlen (bound);
221 bound_len = pend - bound;
224 fprintf_filtered (stream, "%.*s", bound_len, bound);
228 /* Assuming NAME[0 .. NAME_LEN-1] is the name of a range type, print
229 the value (if found) of the bound indicated by SUFFIX ("___L" or
230 "___U") according to the ___XD conventions. */
233 print_dynamic_range_bound (struct type *type, const char *name, int name_len,
234 const char *suffix, struct ui_file *stream)
236 static char *name_buf = NULL;
237 static size_t name_buf_len = 0;
241 GROW_VECT (name_buf, name_buf_len, name_len + strlen (suffix) + 1);
242 strncpy (name_buf, name, name_len);
243 strcpy (name_buf + name_len, suffix);
245 B = get_int_var_value (name_buf, &OK);
247 ada_print_scalar (type, B, stream);
249 fprintf_filtered (stream, "?");
252 /* Print the range type named NAME. */
255 print_range_type_named (char *name, struct ui_file *stream)
257 struct type *raw_type = ada_find_any_type (name);
258 struct type *base_type;
261 if (raw_type == NULL)
262 base_type = builtin_type_int;
263 else if (TYPE_CODE (raw_type) == TYPE_CODE_RANGE)
264 base_type = TYPE_TARGET_TYPE (raw_type);
266 base_type = raw_type;
268 subtype_info = strstr (name, "___XD");
269 if (subtype_info == NULL && raw_type == NULL)
270 fprintf_filtered (stream, "? .. ?");
271 else if (subtype_info == NULL)
272 print_range (raw_type, stream);
275 int prefix_len = subtype_info - name;
280 bounds_str = strchr (subtype_info, '_');
283 if (*subtype_info == 'L')
285 print_range_bound (base_type, bounds_str, &n, stream);
289 print_dynamic_range_bound (base_type, name, prefix_len, "___L",
292 fprintf_filtered (stream, " .. ");
294 if (*subtype_info == 'U')
295 print_range_bound (base_type, bounds_str, &n, stream);
297 print_dynamic_range_bound (base_type, name, prefix_len, "___U",
302 /* Print enumerated type TYPE on STREAM. */
305 print_enum_type (struct type *type, struct ui_file *stream)
307 int len = TYPE_NFIELDS (type);
310 fprintf_filtered (stream, "(");
314 for (i = 0; i < len; i++)
318 fprintf_filtered (stream, ", ");
320 fputs_filtered (ada_enum_name (TYPE_FIELD_NAME (type, i)), stream);
321 if (lastval != TYPE_FIELD_BITPOS (type, i))
323 fprintf_filtered (stream, " => %d", TYPE_FIELD_BITPOS (type, i));
324 lastval = TYPE_FIELD_BITPOS (type, i);
328 fprintf_filtered (stream, ")");
331 /* Print representation of Ada fixed-point type TYPE on STREAM. */
334 print_fixed_point_type (struct type *type, struct ui_file *stream)
336 DOUBLEST delta = ada_delta (type);
337 DOUBLEST small = ada_fixed_to_float (type, 1.0);
340 fprintf_filtered (stream, "delta ??");
343 fprintf_filtered (stream, "delta %g", (double) delta);
345 fprintf_filtered (stream, " <'small = %g>", (double) small);
349 /* Print representation of special VAX floating-point type TYPE on STREAM. */
352 print_vax_floating_point_type (struct type *type, struct ui_file *stream)
354 fprintf_filtered (stream, "<float format %c>",
355 ada_vax_float_type_suffix (type));
358 /* Print simple (constrained) array type TYPE on STREAM. LEVEL is the
359 recursion (indentation) level, in case the element type itself has
360 nested structure, and SHOW is the number of levels of internal
361 structure to show (see ada_print_type). */
364 print_array_type (struct type *type, struct ui_file *stream, int show,
371 fprintf_filtered (stream, "array (");
375 fprintf_filtered (stream, "...");
378 if (ada_is_packed_array_type (type))
379 type = ada_coerce_to_simple_array_type (type);
382 fprintf_filtered (stream, "<undecipherable array type>");
385 if (ada_is_simple_array_type (type))
387 struct type *range_desc_type =
388 ada_find_parallel_type (type, "___XA");
389 struct type *arr_type;
392 if (range_desc_type == NULL)
394 for (arr_type = type; TYPE_CODE (arr_type) == TYPE_CODE_ARRAY;
395 arr_type = TYPE_TARGET_TYPE (arr_type))
397 if (arr_type != type)
398 fprintf_filtered (stream, ", ");
399 print_range (TYPE_INDEX_TYPE (arr_type), stream);
400 if (TYPE_FIELD_BITSIZE (arr_type, 0) > 0)
401 bitsize = TYPE_FIELD_BITSIZE (arr_type, 0);
407 n_indices = TYPE_NFIELDS (range_desc_type);
408 for (k = 0, arr_type = type;
410 k += 1, arr_type = TYPE_TARGET_TYPE (arr_type))
413 fprintf_filtered (stream, ", ");
414 print_range_type_named (TYPE_FIELD_NAME
415 (range_desc_type, k), stream);
416 if (TYPE_FIELD_BITSIZE (arr_type, 0) > 0)
417 bitsize = TYPE_FIELD_BITSIZE (arr_type, 0);
424 for (i = i0 = ada_array_arity (type); i > 0; i -= 1)
425 fprintf_filtered (stream, "%s<>", i == i0 ? "" : ", ");
429 fprintf_filtered (stream, ") of ");
431 ada_print_type (ada_array_element_type (type, n_indices), "", stream,
432 show == 0 ? 0 : show - 1, level + 1);
434 fprintf_filtered (stream, " <packed: %d-bit elements>", bitsize);
437 /* Print the choices encoded by field FIELD_NUM of variant-part TYPE on
438 STREAM, assuming the VAL_TYPE is the type of the values. */
441 print_choices (struct type *type, int field_num, struct ui_file *stream,
442 struct type *val_type)
446 const char *name = TYPE_FIELD_NAME (type, field_num);
450 /* Skip over leading 'V': NOTE soon to be obsolete. */
453 if (!ada_scan_number (name, 1, NULL, &p))
469 fprintf_filtered (stream, " | ");
479 if (!ada_scan_number (name, p + 1, &W, &p))
481 ada_print_scalar (val_type, W, stream);
487 if (!ada_scan_number (name, p + 1, &L, &p)
488 || name[p] != 'T' || !ada_scan_number (name, p + 1, &U, &p))
490 ada_print_scalar (val_type, L, stream);
491 fprintf_filtered (stream, " .. ");
492 ada_print_scalar (val_type, U, stream);
496 fprintf_filtered (stream, "others");
503 fprintf_filtered (stream, "??");
507 /* Assuming that field FIELD_NUM of TYPE is a VARIANTS field whose
508 discriminant is contained in OUTER_TYPE, print its variants on STREAM.
509 LEVEL is the recursion
510 (indentation) level, in case any of the fields themselves have
511 nested structure, and SHOW is the number of levels of internal structure
512 to show (see ada_print_type). For this purpose, fields nested in a
513 variant part are taken to be at the same level as the fields
514 immediately outside the variant part. */
517 print_variant_clauses (struct type *type, int field_num,
518 struct type *outer_type, struct ui_file *stream,
522 struct type *var_type, *par_type;
523 struct type *discr_type;
525 var_type = TYPE_FIELD_TYPE (type, field_num);
526 discr_type = ada_variant_discrim_type (var_type, outer_type);
528 if (TYPE_CODE (var_type) == TYPE_CODE_PTR)
530 var_type = TYPE_TARGET_TYPE (var_type);
531 if (var_type == NULL || TYPE_CODE (var_type) != TYPE_CODE_UNION)
535 par_type = ada_find_parallel_type (var_type, "___XVU");
536 if (par_type != NULL)
539 for (i = 0; i < TYPE_NFIELDS (var_type); i += 1)
541 fprintf_filtered (stream, "\n%*swhen ", level + 4, "");
542 print_choices (var_type, i, stream, discr_type);
543 fprintf_filtered (stream, " =>");
544 if (print_record_field_types (TYPE_FIELD_TYPE (var_type, i),
545 outer_type, stream, show, level + 4) <= 0)
546 fprintf_filtered (stream, " null;");
550 /* Assuming that field FIELD_NUM of TYPE is a variant part whose
551 discriminants are contained in OUTER_TYPE, print a description of it
552 on STREAM. LEVEL is the recursion (indentation) level, in case any of
553 the fields themselves have nested structure, and SHOW is the number of
554 levels of internal structure to show (see ada_print_type). For this
555 purpose, fields nested in a variant part are taken to be at the same
556 level as the fields immediately outside the variant part. */
559 print_variant_part (struct type *type, int field_num, struct type *outer_type,
560 struct ui_file *stream, int show, int level)
562 fprintf_filtered (stream, "\n%*scase %s is", level + 4, "",
563 ada_variant_discrim_name
564 (TYPE_FIELD_TYPE (type, field_num)));
565 print_variant_clauses (type, field_num, outer_type, stream, show,
567 fprintf_filtered (stream, "\n%*send case;", level + 4, "");
570 /* Print a description on STREAM of the fields in record type TYPE, whose
571 discriminants are in OUTER_TYPE. LEVEL is the recursion (indentation)
572 level, in case any of the fields themselves have nested structure,
573 and SHOW is the number of levels of internal structure to show
574 (see ada_print_type). Does not print parent type information of TYPE.
575 Returns 0 if no fields printed, -1 for an incomplete type, else > 0.
576 Prints each field beginning on a new line, but does not put a new line at
580 print_record_field_types (struct type *type, struct type *outer_type,
581 struct ui_file *stream, int show, int level)
586 len = TYPE_NFIELDS (type);
588 if (len == 0 && (TYPE_FLAGS (type) & TYPE_FLAG_STUB) != 0)
591 for (i = 0; i < len; i += 1)
595 if (ada_is_parent_field (type, i) || ada_is_ignored_field (type, i))
597 else if (ada_is_wrapper_field (type, i))
598 flds += print_record_field_types (TYPE_FIELD_TYPE (type, i), type,
599 stream, show, level);
600 else if (ada_is_variant_part (type, i))
602 print_variant_part (type, i, outer_type, stream, show, level);
608 fprintf_filtered (stream, "\n%*s", level + 4, "");
609 ada_print_type (TYPE_FIELD_TYPE (type, i),
610 TYPE_FIELD_NAME (type, i),
611 stream, show - 1, level + 4);
612 fprintf_filtered (stream, ";");
619 /* Print record type TYPE on STREAM. LEVEL is the recursion (indentation)
620 level, in case the element type itself has nested structure, and SHOW is
621 the number of levels of internal structure to show (see ada_print_type). */
624 print_record_type (struct type *type0, struct ui_file *stream, int show,
627 struct type *parent_type;
630 type = ada_find_parallel_type (type0, "___XVE");
634 parent_type = ada_parent_type (type);
635 if (ada_type_name (parent_type) != NULL)
636 fprintf_filtered (stream, "new %s with record",
637 decoded_type_name (parent_type));
638 else if (parent_type == NULL && ada_is_tagged_type (type, 0))
639 fprintf_filtered (stream, "tagged record");
641 fprintf_filtered (stream, "record");
644 fprintf_filtered (stream, " ... end record");
650 if (parent_type != NULL && ada_type_name (parent_type) == NULL)
651 flds += print_record_field_types (parent_type, parent_type,
652 stream, show, level);
653 flds += print_record_field_types (type, type, stream, show, level);
656 fprintf_filtered (stream, "\n%*send record", level, "");
658 fprintf_filtered (stream, _(" <incomplete type> end record"));
660 fprintf_filtered (stream, " null; end record");
664 /* Print the unchecked union type TYPE in something resembling Ada
665 format on STREAM. LEVEL is the recursion (indentation) level
666 in case the element type itself has nested structure, and SHOW is the
667 number of levels of internal structure to show (see ada_print_type). */
669 print_unchecked_union_type (struct type *type, struct ui_file *stream,
673 fprintf_filtered (stream, "record (?) is ... end record");
674 else if (TYPE_NFIELDS (type) == 0)
675 fprintf_filtered (stream, "record (?) is null; end record");
680 fprintf_filtered (stream, "record (?) is\n%*scase ? is", level + 4, "");
682 for (i = 0; i < TYPE_NFIELDS (type); i += 1)
684 fprintf_filtered (stream, "\n%*swhen ? =>\n%*s", level + 8, "",
686 ada_print_type (TYPE_FIELD_TYPE (type, i),
687 TYPE_FIELD_NAME (type, i),
688 stream, show - 1, level + 12);
689 fprintf_filtered (stream, ";");
692 fprintf_filtered (stream, "\n%*send case;\n%*send record",
693 level + 4, "", level, "");
699 /* Print function or procedure type TYPE on STREAM. Make it a header
700 for function or procedure NAME if NAME is not null. */
703 print_func_type (struct type *type, struct ui_file *stream, char *name)
705 int i, len = TYPE_NFIELDS (type);
707 if (TYPE_CODE (TYPE_TARGET_TYPE (type)) == TYPE_CODE_VOID)
708 fprintf_filtered (stream, "procedure");
710 fprintf_filtered (stream, "function");
712 if (name != NULL && name[0] != '\0')
713 fprintf_filtered (stream, " %s", name);
717 fprintf_filtered (stream, " (");
718 for (i = 0; i < len; i += 1)
722 fputs_filtered ("; ", stream);
725 fprintf_filtered (stream, "a%d: ", i + 1);
726 ada_print_type (TYPE_FIELD_TYPE (type, i), "", stream, -1, 0);
728 fprintf_filtered (stream, ")");
731 if (TYPE_CODE (TYPE_TARGET_TYPE (type)) != TYPE_CODE_VOID)
733 fprintf_filtered (stream, " return ");
734 ada_print_type (TYPE_TARGET_TYPE (type), "", stream, 0, 0);
739 /* Print a description of a type TYPE0.
740 Output goes to STREAM (via stdio).
741 If VARSTRING is a non-empty string, print as an Ada variable/field
743 SHOW+1 is the maximum number of levels of internal type structure
744 to show (this applies to record types, enumerated types, and
746 SHOW is the number of levels of internal type structure to show
747 when there is a type name for the SHOWth deepest level (0th is
749 When SHOW<0, no inner structure is shown.
750 LEVEL indicates level of recursion (for nested definitions). */
753 ada_print_type (struct type *type0, char *varstring, struct ui_file *stream,
756 struct type *type = ada_check_typedef (ada_get_base_type (type0));
757 char *type_name = decoded_type_name (type);
758 int is_var_decl = (varstring != NULL && varstring[0] != '\0');
763 fprintf_filtered (stream, "%.*s: ",
764 ada_name_prefix_len (varstring), varstring);
765 fprintf_filtered (stream, "<null type?>");
770 type = ada_check_typedef (type);
772 if (is_var_decl && TYPE_CODE (type) != TYPE_CODE_FUNC)
773 fprintf_filtered (stream, "%.*s: ",
774 ada_name_prefix_len (varstring), varstring);
776 if (type_name != NULL && show <= 0)
778 fprintf_filtered (stream, "%.*s",
779 ada_name_prefix_len (type_name), type_name);
783 if (ada_is_aligner_type (type))
784 ada_print_type (ada_aligned_type (type), "", stream, show, level);
785 else if (ada_is_packed_array_type (type))
786 print_array_type (type, stream, show, level);
788 switch (TYPE_CODE (type))
791 fprintf_filtered (stream, "<");
792 c_print_type (type, "", stream, show, level);
793 fprintf_filtered (stream, ">");
796 fprintf_filtered (stream, "access ");
797 ada_print_type (TYPE_TARGET_TYPE (type), "", stream, show, level);
800 fprintf_filtered (stream, "<ref> ");
801 ada_print_type (TYPE_TARGET_TYPE (type), "", stream, show, level);
803 case TYPE_CODE_ARRAY:
804 print_array_type (type, stream, show, level);
807 if (ada_is_fixed_point_type (type))
808 print_fixed_point_type (type, stream);
809 else if (ada_is_vax_floating_type (type))
810 print_vax_floating_point_type (type, stream);
813 char *name = ada_type_name (type);
814 if (!ada_is_range_type_name (name))
815 fprintf_filtered (stream, "<%d-byte integer>",
819 fprintf_filtered (stream, "range ");
820 print_range_type_named (name, stream);
824 case TYPE_CODE_RANGE:
825 if (ada_is_fixed_point_type (type))
826 print_fixed_point_type (type, stream);
827 else if (ada_is_vax_floating_type (type))
828 print_vax_floating_point_type (type, stream);
829 else if (ada_is_modular_type (type))
830 fprintf_filtered (stream, "mod %ld", (long) ada_modulus (type));
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);
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);
855 case TYPE_CODE_UNION:
856 print_unchecked_union_type (type, stream, show, level);
859 print_func_type (type, stream, varstring);