1 /* Support for printing Pascal types for GDB, the GNU debugger.
2 Copyright (C) 2000-2020 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/>. */
19 /* This file is derived from p-typeprint.c */
22 #include "gdb_obstack.h"
23 #include "bfd.h" /* Binary File Description */
26 #include "expression.h"
32 #include "typeprint.h"
33 #include "gdb-demangle.h"
35 #include "cli/cli-style.h"
37 static void pascal_type_print_varspec_suffix (struct type
*, struct ui_file
*,
39 const struct type_print_options
*);
41 static void pascal_type_print_derivation_info (struct ui_file
*,
46 /* LEVEL is the depth to indent lines by. */
49 pascal_print_type (struct type
*type
, const char *varstring
,
50 struct ui_file
*stream
, int show
, int level
,
51 const struct type_print_options
*flags
)
59 type
= check_typedef (type
);
61 if ((code
== TYPE_CODE_FUNC
62 || code
== TYPE_CODE_METHOD
))
64 pascal_type_print_varspec_prefix (type
, stream
, show
, 0, flags
);
67 fputs_filtered (varstring
, stream
);
69 if ((varstring
!= NULL
&& *varstring
!= '\0')
70 && !(code
== TYPE_CODE_FUNC
71 || code
== TYPE_CODE_METHOD
))
73 fputs_filtered (" : ", stream
);
76 if (!(code
== TYPE_CODE_FUNC
77 || code
== TYPE_CODE_METHOD
))
79 pascal_type_print_varspec_prefix (type
, stream
, show
, 0, flags
);
82 pascal_type_print_base (type
, stream
, show
, level
, flags
);
83 /* For demangled function names, we have the arglist as part of the name,
84 so don't print an additional pair of ()'s. */
86 demangled_args
= varstring
? strchr (varstring
, '(') != NULL
: 0;
87 pascal_type_print_varspec_suffix (type
, stream
, show
, 0, demangled_args
,
92 /* Print a typedef using Pascal syntax. TYPE is the underlying type.
93 NEW_SYMBOL is the symbol naming the type. STREAM is the stream on
97 pascal_print_typedef (struct type
*type
, struct symbol
*new_symbol
,
98 struct ui_file
*stream
)
100 type
= check_typedef (type
);
101 fprintf_filtered (stream
, "type ");
102 fprintf_filtered (stream
, "%s = ", new_symbol
->print_name ());
103 type_print (type
, "", stream
, 0);
104 fprintf_filtered (stream
, ";");
107 /* If TYPE is a derived type, then print out derivation information.
108 Print only the actual base classes of this type, not the base classes
109 of the base classes. I.e. for the derivation hierarchy:
112 class B : public A {int b; };
113 class C : public B {int c; };
115 Print the type of class C as:
121 Not as the following (like gdb used to), which is not legal C++ syntax for
122 derived types and may be confused with the multiple inheritance form:
124 class C : public B : public A {
128 In general, gdb should try to print the types as closely as possible to
129 the form that they appear in the source code. */
132 pascal_type_print_derivation_info (struct ui_file
*stream
, struct type
*type
)
137 for (i
= 0; i
< TYPE_N_BASECLASSES (type
); i
++)
139 fputs_filtered (i
== 0 ? ": " : ", ", stream
);
140 fprintf_filtered (stream
, "%s%s ",
141 BASETYPE_VIA_PUBLIC (type
, i
) ? "public" : "private",
142 BASETYPE_VIA_VIRTUAL (type
, i
) ? " virtual" : "");
143 name
= TYPE_BASECLASS (type
, i
)->name ();
144 fprintf_filtered (stream
, "%s", name
? name
: "(null)");
148 fputs_filtered (" ", stream
);
152 /* Print the Pascal method arguments ARGS to the file STREAM. */
155 pascal_type_print_method_args (const char *physname
, const char *methodname
,
156 struct ui_file
*stream
)
158 int is_constructor
= (startswith (physname
, "__ct__"));
159 int is_destructor
= (startswith (physname
, "__dt__"));
161 if (is_constructor
|| is_destructor
)
166 fputs_filtered (methodname
, stream
);
168 if (physname
&& (*physname
!= 0))
170 fputs_filtered (" (", stream
);
171 /* We must demangle this. */
172 while (isdigit (physname
[0]))
178 while (isdigit (physname
[len
]))
182 i
= strtol (physname
, &argname
, 0);
185 for (j
= 0; j
< i
; ++j
)
186 fputc_filtered (physname
[j
], stream
);
189 if (physname
[0] != 0)
191 fputs_filtered (", ", stream
);
194 fputs_filtered (")", stream
);
198 /* Print any asterisks or open-parentheses needed before the
199 variable name (to describe its type).
201 On outermost call, pass 0 for PASSED_A_PTR.
202 On outermost call, SHOW > 0 means should ignore
203 any typename for TYPE and show its details.
204 SHOW is always zero on recursive calls. */
207 pascal_type_print_varspec_prefix (struct type
*type
, struct ui_file
*stream
,
208 int show
, int passed_a_ptr
,
209 const struct type_print_options
*flags
)
214 if (type
->name () && show
<= 0)
219 switch (type
->code ())
222 fprintf_filtered (stream
, "^");
223 pascal_type_print_varspec_prefix (TYPE_TARGET_TYPE (type
), stream
, 0, 1,
225 break; /* Pointer should be handled normally
228 case TYPE_CODE_METHOD
:
230 fprintf_filtered (stream
, "(");
231 if (TYPE_TARGET_TYPE (type
) != NULL
232 && TYPE_TARGET_TYPE (type
)->code () != TYPE_CODE_VOID
)
234 fprintf_filtered (stream
, "function ");
238 fprintf_filtered (stream
, "procedure ");
243 fprintf_filtered (stream
, " ");
244 pascal_type_print_base (TYPE_SELF_TYPE (type
),
245 stream
, 0, passed_a_ptr
, flags
);
246 fprintf_filtered (stream
, "::");
251 pascal_type_print_varspec_prefix (TYPE_TARGET_TYPE (type
), stream
, 0, 1,
253 fprintf_filtered (stream
, "&");
258 fprintf_filtered (stream
, "(");
260 if (TYPE_TARGET_TYPE (type
) != NULL
261 && TYPE_TARGET_TYPE (type
)->code () != TYPE_CODE_VOID
)
263 fprintf_filtered (stream
, "function ");
267 fprintf_filtered (stream
, "procedure ");
272 case TYPE_CODE_ARRAY
:
274 fprintf_filtered (stream
, "(");
275 fprintf_filtered (stream
, "array ");
276 if (TYPE_LENGTH (TYPE_TARGET_TYPE (type
)) > 0
277 && type
->bounds ()->high
.kind () != PROP_UNDEFINED
)
278 fprintf_filtered (stream
, "[%s..%s] ",
279 plongest (type
->bounds ()->low
.const_val ()),
280 plongest (type
->bounds ()->high
.const_val ()));
281 fprintf_filtered (stream
, "of ");
284 case TYPE_CODE_UNDEF
:
285 case TYPE_CODE_STRUCT
:
286 case TYPE_CODE_UNION
:
291 case TYPE_CODE_ERROR
:
295 case TYPE_CODE_RANGE
:
296 case TYPE_CODE_STRING
:
297 case TYPE_CODE_COMPLEX
:
298 case TYPE_CODE_TYPEDEF
:
299 case TYPE_CODE_FIXED_POINT
:
300 /* These types need no prefix. They are listed here so that
301 gcc -Wall will reveal any types that haven't been handled. */
304 error (_("type not handled in pascal_type_print_varspec_prefix()"));
310 pascal_print_func_args (struct type
*type
, struct ui_file
*stream
,
311 const struct type_print_options
*flags
)
313 int i
, len
= type
->num_fields ();
317 fprintf_filtered (stream
, "(");
319 for (i
= 0; i
< len
; i
++)
323 fputs_filtered (", ", stream
);
326 /* Can we find if it is a var parameter ??
327 if ( TYPE_FIELD(type, i) == )
329 fprintf_filtered (stream, "var ");
331 pascal_print_type (type
->field (i
).type (), "" /* TYPE_FIELD_NAME
333 ,stream
, -1, 0, flags
);
337 fprintf_filtered (stream
, ")");
341 /* Helper for pascal_type_print_varspec_suffix to print the suffix of
342 a function or method. */
345 pascal_type_print_func_varspec_suffix (struct type
*type
, struct ui_file
*stream
,
346 int show
, int passed_a_ptr
,
348 const struct type_print_options
*flags
)
350 if (TYPE_TARGET_TYPE (type
) == NULL
351 || TYPE_TARGET_TYPE (type
)->code () != TYPE_CODE_VOID
)
353 fprintf_filtered (stream
, " : ");
354 pascal_type_print_varspec_prefix (TYPE_TARGET_TYPE (type
),
355 stream
, 0, 0, flags
);
357 if (TYPE_TARGET_TYPE (type
) == NULL
)
358 type_print_unknown_return_type (stream
);
360 pascal_type_print_base (TYPE_TARGET_TYPE (type
), stream
, show
, 0,
363 pascal_type_print_varspec_suffix (TYPE_TARGET_TYPE (type
), stream
, 0,
364 passed_a_ptr
, 0, flags
);
368 /* Print any array sizes, function arguments or close parentheses
369 needed after the variable name (to describe its type).
370 Args work like pascal_type_print_varspec_prefix. */
373 pascal_type_print_varspec_suffix (struct type
*type
, struct ui_file
*stream
,
374 int show
, int passed_a_ptr
,
376 const struct type_print_options
*flags
)
381 if (type
->name () && show
<= 0)
386 switch (type
->code ())
388 case TYPE_CODE_ARRAY
:
390 fprintf_filtered (stream
, ")");
393 case TYPE_CODE_METHOD
:
395 fprintf_filtered (stream
, ")");
396 pascal_type_print_method_args ("",
399 pascal_type_print_func_varspec_suffix (type
, stream
, show
,
400 passed_a_ptr
, 0, flags
);
405 pascal_type_print_varspec_suffix (TYPE_TARGET_TYPE (type
),
406 stream
, 0, 1, 0, flags
);
411 fprintf_filtered (stream
, ")");
413 pascal_print_func_args (type
, stream
, flags
);
414 pascal_type_print_func_varspec_suffix (type
, stream
, show
,
415 passed_a_ptr
, 0, flags
);
418 case TYPE_CODE_UNDEF
:
419 case TYPE_CODE_STRUCT
:
420 case TYPE_CODE_UNION
:
425 case TYPE_CODE_ERROR
:
429 case TYPE_CODE_RANGE
:
430 case TYPE_CODE_STRING
:
431 case TYPE_CODE_COMPLEX
:
432 case TYPE_CODE_TYPEDEF
:
433 case TYPE_CODE_FIXED_POINT
:
434 /* These types do not need a suffix. They are listed so that
435 gcc -Wall will report types that may not have been considered. */
438 error (_("type not handled in pascal_type_print_varspec_suffix()"));
443 /* Print the name of the type (or the ultimate pointer target,
444 function value or array element), or the description of a
447 SHOW positive means print details about the type (e.g. enum values),
448 and print structure elements passing SHOW - 1 for show.
449 SHOW negative means just print the type name or struct tag if there is one.
450 If there is no name, print something sensible but concise like
452 SHOW zero means just print the type name or struct tag if there is one.
453 If there is no name, print something sensible but not as concise like
454 "struct {int x; int y;}".
456 LEVEL is the number of spaces to indent by.
457 We increase it for some recursive calls. */
460 pascal_type_print_base (struct type
*type
, struct ui_file
*stream
, int show
,
461 int level
, const struct type_print_options
*flags
)
468 s_none
, s_public
, s_private
, s_protected
476 fputs_styled ("<type unknown>", metadata_style
.style (), stream
);
481 if ((type
->code () == TYPE_CODE_PTR
)
482 && (TYPE_TARGET_TYPE (type
)->code () == TYPE_CODE_VOID
))
484 fputs_filtered (type
->name () ? type
->name () : "pointer",
488 /* When SHOW is zero or less, and there is a valid type name, then always
489 just print the type name directly from the type. */
492 && type
->name () != NULL
)
494 fputs_filtered (type
->name (), stream
);
498 type
= check_typedef (type
);
500 switch (type
->code ())
502 case TYPE_CODE_TYPEDEF
:
505 /* case TYPE_CODE_FUNC:
506 case TYPE_CODE_METHOD: */
507 pascal_type_print_base (TYPE_TARGET_TYPE (type
), stream
, show
, level
,
511 case TYPE_CODE_ARRAY
:
512 /* pascal_type_print_varspec_prefix (TYPE_TARGET_TYPE (type),
514 pascal_type_print_base (TYPE_TARGET_TYPE (type),
515 stream, show, level);
516 pascal_type_print_varspec_suffix (TYPE_TARGET_TYPE (type),
518 pascal_print_type (TYPE_TARGET_TYPE (type
), NULL
, stream
, 0, 0, flags
);
522 case TYPE_CODE_METHOD
:
524 pascal_type_print_base (TYPE_TARGET_TYPE (type), stream, show, level);
525 only after args !! */
527 case TYPE_CODE_STRUCT
:
528 if (type
->name () != NULL
)
530 fputs_filtered (type
->name (), stream
);
531 fputs_filtered (" = ", stream
);
533 if (HAVE_CPLUS_STRUCT (type
))
535 fprintf_filtered (stream
, "class ");
539 fprintf_filtered (stream
, "record ");
543 case TYPE_CODE_UNION
:
544 if (type
->name () != NULL
)
546 fputs_filtered (type
->name (), stream
);
547 fputs_filtered (" = ", stream
);
549 fprintf_filtered (stream
, "case <?> of ");
555 /* If we just printed a tag name, no need to print anything else. */
556 if (type
->name () == NULL
)
557 fprintf_filtered (stream
, "{...}");
559 else if (show
> 0 || type
->name () == NULL
)
561 pascal_type_print_derivation_info (stream
, type
);
563 fprintf_filtered (stream
, "\n");
564 if ((type
->num_fields () == 0) && (TYPE_NFN_FIELDS (type
) == 0))
566 if (type
->is_stub ())
567 fprintfi_filtered (level
+ 4, stream
, "<incomplete type>\n");
569 fprintfi_filtered (level
+ 4, stream
, "<no data fields>\n");
572 /* Start off with no specific section type, so we can print
573 one for the first field we find, and use that section type
574 thereafter until we find another type. */
576 section_type
= s_none
;
578 /* If there is a base class for this type,
579 do not print the field that it occupies. */
581 len
= type
->num_fields ();
582 for (i
= TYPE_N_BASECLASSES (type
); i
< len
; i
++)
585 /* Don't print out virtual function table. */
586 if ((startswith (TYPE_FIELD_NAME (type
, i
), "_vptr"))
587 && is_cplus_marker ((TYPE_FIELD_NAME (type
, i
))[5]))
590 /* If this is a pascal object or class we can print the
591 various section labels. */
593 if (HAVE_CPLUS_STRUCT (type
))
595 if (TYPE_FIELD_PROTECTED (type
, i
))
597 if (section_type
!= s_protected
)
599 section_type
= s_protected
;
600 fprintfi_filtered (level
+ 2, stream
,
604 else if (TYPE_FIELD_PRIVATE (type
, i
))
606 if (section_type
!= s_private
)
608 section_type
= s_private
;
609 fprintfi_filtered (level
+ 2, stream
, "private\n");
614 if (section_type
!= s_public
)
616 section_type
= s_public
;
617 fprintfi_filtered (level
+ 2, stream
, "public\n");
622 print_spaces_filtered (level
+ 4, stream
);
623 if (field_is_static (&type
->field (i
)))
624 fprintf_filtered (stream
, "static ");
625 pascal_print_type (type
->field (i
).type (),
626 TYPE_FIELD_NAME (type
, i
),
627 stream
, show
- 1, level
+ 4, flags
);
628 if (!field_is_static (&type
->field (i
))
629 && TYPE_FIELD_PACKED (type
, i
))
631 /* It is a bitfield. This code does not attempt
632 to look at the bitpos and reconstruct filler,
633 unnamed fields. This would lead to misleading
634 results if the compiler does not put out fields
635 for such things (I don't know what it does). */
636 fprintf_filtered (stream
, " : %d",
637 TYPE_FIELD_BITSIZE (type
, i
));
639 fprintf_filtered (stream
, ";\n");
642 /* If there are both fields and methods, put a space between. */
643 len
= TYPE_NFN_FIELDS (type
);
644 if (len
&& section_type
!= s_none
)
645 fprintf_filtered (stream
, "\n");
647 /* Object pascal: print out the methods. */
649 for (i
= 0; i
< len
; i
++)
651 struct fn_field
*f
= TYPE_FN_FIELDLIST1 (type
, i
);
652 int j
, len2
= TYPE_FN_FIELDLIST_LENGTH (type
, i
);
653 const char *method_name
= TYPE_FN_FIELDLIST_NAME (type
, i
);
655 /* this is GNU C++ specific
656 how can we know constructor/destructor?
657 It might work for GNU pascal. */
658 for (j
= 0; j
< len2
; j
++)
660 const char *physname
= TYPE_FN_FIELD_PHYSNAME (f
, j
);
662 int is_constructor
= (startswith (physname
, "__ct__"));
663 int is_destructor
= (startswith (physname
, "__dt__"));
666 if (TYPE_FN_FIELD_PROTECTED (f
, j
))
668 if (section_type
!= s_protected
)
670 section_type
= s_protected
;
671 fprintfi_filtered (level
+ 2, stream
,
675 else if (TYPE_FN_FIELD_PRIVATE (f
, j
))
677 if (section_type
!= s_private
)
679 section_type
= s_private
;
680 fprintfi_filtered (level
+ 2, stream
, "private\n");
685 if (section_type
!= s_public
)
687 section_type
= s_public
;
688 fprintfi_filtered (level
+ 2, stream
, "public\n");
692 print_spaces_filtered (level
+ 4, stream
);
693 if (TYPE_FN_FIELD_STATIC_P (f
, j
))
694 fprintf_filtered (stream
, "static ");
695 if (TYPE_TARGET_TYPE (TYPE_FN_FIELD_TYPE (f
, j
)) == 0)
697 /* Keep GDB from crashing here. */
698 fprintf_filtered (stream
, "<undefined type> %s;\n",
699 TYPE_FN_FIELD_PHYSNAME (f
, j
));
705 fprintf_filtered (stream
, "constructor ");
707 else if (is_destructor
)
709 fprintf_filtered (stream
, "destructor ");
711 else if (TYPE_TARGET_TYPE (TYPE_FN_FIELD_TYPE (f
, j
)) != 0
712 && TYPE_TARGET_TYPE (TYPE_FN_FIELD_TYPE(f
, j
))->code () != TYPE_CODE_VOID
)
714 fprintf_filtered (stream
, "function ");
718 fprintf_filtered (stream
, "procedure ");
720 /* This does not work, no idea why !! */
722 pascal_type_print_method_args (physname
,
726 if (TYPE_TARGET_TYPE (TYPE_FN_FIELD_TYPE (f
, j
)) != 0
727 && TYPE_TARGET_TYPE (TYPE_FN_FIELD_TYPE(f
, j
))->code () != TYPE_CODE_VOID
)
729 fputs_filtered (" : ", stream
);
730 type_print (TYPE_TARGET_TYPE (TYPE_FN_FIELD_TYPE (f
, j
)),
733 if (TYPE_FN_FIELD_VIRTUAL_P (f
, j
))
734 fprintf_filtered (stream
, "; virtual");
736 fprintf_filtered (stream
, ";\n");
739 fprintfi_filtered (level
, stream
, "end");
744 if (type
->name () != NULL
)
746 fputs_filtered (type
->name (), stream
);
748 fputs_filtered (" ", stream
);
750 /* enum is just defined by
751 type enume_name = (enum_member1,enum_member2,...) */
752 fprintf_filtered (stream
, " = ");
756 /* If we just printed a tag name, no need to print anything else. */
757 if (type
->name () == NULL
)
758 fprintf_filtered (stream
, "(...)");
760 else if (show
> 0 || type
->name () == NULL
)
762 fprintf_filtered (stream
, "(");
763 len
= type
->num_fields ();
765 for (i
= 0; i
< len
; i
++)
769 fprintf_filtered (stream
, ", ");
771 fputs_filtered (TYPE_FIELD_NAME (type
, i
), stream
);
772 if (lastval
!= TYPE_FIELD_ENUMVAL (type
, i
))
774 fprintf_filtered (stream
,
776 plongest (TYPE_FIELD_ENUMVAL (type
, i
)));
777 lastval
= TYPE_FIELD_ENUMVAL (type
, i
);
781 fprintf_filtered (stream
, ")");
786 fprintf_filtered (stream
, "void");
789 case TYPE_CODE_UNDEF
:
790 fprintf_filtered (stream
, "record <unknown>");
793 case TYPE_CODE_ERROR
:
794 fprintf_filtered (stream
, "%s", TYPE_ERROR_NAME (type
));
797 /* this probably does not work for enums. */
798 case TYPE_CODE_RANGE
:
800 struct type
*target
= TYPE_TARGET_TYPE (type
);
802 print_type_scalar (target
, type
->bounds ()->low
.const_val (), stream
);
803 fputs_filtered ("..", stream
);
804 print_type_scalar (target
, type
->bounds ()->high
.const_val (), stream
);
809 fputs_filtered ("set of ", stream
);
810 pascal_print_type (type
->index_type (), "", stream
,
811 show
- 1, level
, flags
);
814 case TYPE_CODE_STRING
:
815 fputs_filtered ("String", stream
);
819 /* Handle types not explicitly handled by the other cases,
820 such as fundamental types. For these, just print whatever
821 the type name is, as recorded in the type itself. If there
822 is no type name, then complain. */
823 if (type
->name () != NULL
)
825 fputs_filtered (type
->name (), stream
);
829 /* At least for dump_symtab, it is important that this not be
831 fprintf_styled (stream
, metadata_style
.style (),
832 "<invalid unnamed pascal type code %d>",