1 /* Support for printing Pascal types for GDB, the GNU debugger.
3 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
21 /* This file is derived from p-typeprint.c */
25 #include "bfd.h" /* Binary File Description */
28 #include "expression.h"
36 #include "typeprint.h"
38 #include "gdb_string.h"
42 static void pascal_type_print_args (struct type
*, struct ui_file
*);
44 static void pascal_type_print_varspec_suffix (struct type
*, struct ui_file
*, int, int, int);
46 static void pascal_type_print_derivation_info (struct ui_file
*, struct type
*);
48 void pascal_type_print_varspec_prefix (struct type
*, struct ui_file
*, int, int);
51 /* LEVEL is the depth to indent lines by. */
54 pascal_print_type (struct type
*type
, char *varstring
, struct ui_file
*stream
,
57 register enum type_code code
;
60 code
= TYPE_CODE (type
);
65 if ((code
== TYPE_CODE_FUNC
||
66 code
== TYPE_CODE_METHOD
))
68 pascal_type_print_varspec_prefix (type
, stream
, show
, 0);
71 fputs_filtered (varstring
, stream
);
73 if ((varstring
!= NULL
&& *varstring
!= '\0') &&
74 !(code
== TYPE_CODE_FUNC
||
75 code
== TYPE_CODE_METHOD
))
77 fputs_filtered (" : ", stream
);
80 if (!(code
== TYPE_CODE_FUNC
||
81 code
== TYPE_CODE_METHOD
))
83 pascal_type_print_varspec_prefix (type
, stream
, show
, 0);
86 pascal_type_print_base (type
, stream
, show
, level
);
87 /* For demangled function names, we have the arglist as part of the name,
88 so don't print an additional pair of ()'s */
90 demangled_args
= varstring
? strchr (varstring
, '(') != NULL
: 0;
91 pascal_type_print_varspec_suffix (type
, stream
, show
, 0, demangled_args
);
95 /* If TYPE is a derived type, then print out derivation information.
96 Print only the actual base classes of this type, not the base classes
97 of the base classes. I.E. for the derivation hierarchy:
100 class B : public A {int b; };
101 class C : public B {int c; };
103 Print the type of class C as:
109 Not as the following (like gdb used to), which is not legal C++ syntax for
110 derived types and may be confused with the multiple inheritance form:
112 class C : public B : public A {
116 In general, gdb should try to print the types as closely as possible to
117 the form that they appear in the source code. */
120 pascal_type_print_derivation_info (struct ui_file
*stream
, struct type
*type
)
125 for (i
= 0; i
< TYPE_N_BASECLASSES (type
); i
++)
127 fputs_filtered (i
== 0 ? ": " : ", ", stream
);
128 fprintf_filtered (stream
, "%s%s ",
129 BASETYPE_VIA_PUBLIC (type
, i
) ? "public" : "private",
130 BASETYPE_VIA_VIRTUAL (type
, i
) ? " virtual" : "");
131 name
= type_name_no_tag (TYPE_BASECLASS (type
, i
));
132 fprintf_filtered (stream
, "%s", name
? name
: "(null)");
136 fputs_filtered (" ", stream
);
140 /* Print the Pascal method arguments ARGS to the file STREAM. */
143 pascal_type_print_method_args (char *physname
, char *methodname
,
144 struct ui_file
*stream
)
146 int is_constructor
= STREQN (physname
, "__ct__", 6);
147 int is_destructor
= STREQN (physname
, "__dt__", 6);
149 if (is_constructor
|| is_destructor
)
154 fputs_filtered (methodname
, stream
);
156 if (physname
&& (*physname
!= 0))
162 fputs_filtered (" (", stream
);
163 /* we must demangle this */
164 while (isdigit (physname
[0]))
166 while (isdigit (physname
[len
]))
170 i
= strtol (physname
, &argname
, 0);
172 storec
= physname
[i
];
174 fputs_filtered (physname
, stream
);
175 physname
[i
] = storec
;
177 if (physname
[0] != 0)
179 fputs_filtered (", ", stream
);
182 fputs_filtered (")", stream
);
186 /* Print any asterisks or open-parentheses needed before the
187 variable name (to describe its type).
189 On outermost call, pass 0 for PASSED_A_PTR.
190 On outermost call, SHOW > 0 means should ignore
191 any typename for TYPE and show its details.
192 SHOW is always zero on recursive calls. */
195 pascal_type_print_varspec_prefix (struct type
*type
, struct ui_file
*stream
,
196 int show
, int passed_a_ptr
)
202 if (TYPE_NAME (type
) && show
<= 0)
207 switch (TYPE_CODE (type
))
210 fprintf_filtered (stream
, "^");
211 pascal_type_print_varspec_prefix (TYPE_TARGET_TYPE (type
), stream
, 0, 1);
212 break; /* pointer should be handled normally in pascal */
214 case TYPE_CODE_MEMBER
:
216 fprintf_filtered (stream
, "(");
217 pascal_type_print_varspec_prefix (TYPE_TARGET_TYPE (type
), stream
, 0, 0);
218 fprintf_filtered (stream
, " ");
219 name
= type_name_no_tag (TYPE_DOMAIN_TYPE (type
));
221 fputs_filtered (name
, stream
);
223 pascal_type_print_base (TYPE_DOMAIN_TYPE (type
), stream
, 0, passed_a_ptr
);
224 fprintf_filtered (stream
, "::");
227 case TYPE_CODE_METHOD
:
229 fprintf_filtered (stream
, "(");
230 if (TYPE_CODE (TYPE_TARGET_TYPE (type
)) != TYPE_CODE_VOID
)
232 fprintf_filtered (stream
, "function ");
236 fprintf_filtered (stream
, "procedure ");
241 fprintf_filtered (stream
, " ");
242 pascal_type_print_base (TYPE_DOMAIN_TYPE (type
), stream
, 0, passed_a_ptr
);
243 fprintf_filtered (stream
, "::");
248 pascal_type_print_varspec_prefix (TYPE_TARGET_TYPE (type
), stream
, 0, 1);
249 fprintf_filtered (stream
, "&");
254 fprintf_filtered (stream
, "(");
256 if (TYPE_CODE (TYPE_TARGET_TYPE (type
)) != TYPE_CODE_VOID
)
258 fprintf_filtered (stream
, "function ");
262 fprintf_filtered (stream
, "procedure ");
267 case TYPE_CODE_ARRAY
:
269 fprintf_filtered (stream
, "(");
270 fprintf_filtered (stream
, "array ");
271 if (TYPE_LENGTH (type
) >= 0 && TYPE_LENGTH (TYPE_TARGET_TYPE (type
)) > 0
272 && TYPE_ARRAY_UPPER_BOUND_TYPE (type
) != BOUND_CANNOT_BE_DETERMINED
)
273 fprintf_filtered (stream
, "[%d..%d] ",
274 TYPE_ARRAY_LOWER_BOUND_VALUE (type
),
275 TYPE_ARRAY_UPPER_BOUND_VALUE (type
)
277 fprintf_filtered (stream
, "of ");
280 case TYPE_CODE_UNDEF
:
281 case TYPE_CODE_STRUCT
:
282 case TYPE_CODE_UNION
:
287 case TYPE_CODE_ERROR
:
291 case TYPE_CODE_RANGE
:
292 case TYPE_CODE_STRING
:
293 case TYPE_CODE_BITSTRING
:
294 case TYPE_CODE_COMPLEX
:
295 case TYPE_CODE_TYPEDEF
:
296 case TYPE_CODE_TEMPLATE
:
297 /* These types need no prefix. They are listed here so that
298 gcc -Wall will reveal any types that haven't been handled. */
301 error ("type not handled in pascal_type_print_varspec_prefix()");
307 pascal_type_print_args (struct type
*type
, struct ui_file
*stream
)
312 /* fprintf_filtered (stream, "(");
313 no () for procedures !! */
314 args
= TYPE_ARG_TYPES (type
);
317 if ((args
[1] != NULL
&& args
[1]->code
!= TYPE_CODE_VOID
) ||
320 fprintf_filtered (stream
, "(");
324 fprintf_filtered (stream
, "...");
329 args
[i
] != NULL
&& args
[i
]->code
!= TYPE_CODE_VOID
;
332 pascal_print_type (args
[i
], "", stream
, -1, 0);
333 if (args
[i
+ 1] == NULL
)
335 fprintf_filtered (stream
, "...");
337 else if (args
[i
+ 1]->code
!= TYPE_CODE_VOID
)
339 fprintf_filtered (stream
, ",");
344 if ((args
[1] != NULL
&& args
[1]->code
!= TYPE_CODE_VOID
) ||
347 fprintf_filtered (stream
, ")");
353 pascal_print_func_args (struct type
*type
, struct ui_file
*stream
)
355 int i
, len
= TYPE_NFIELDS (type
);
358 fprintf_filtered (stream
, "(");
360 for (i
= 0; i
< len
; i
++)
364 fputs_filtered (", ", stream
);
367 /* can we find if it is a var parameter ??
368 if ( TYPE_FIELD(type, i) == )
370 fprintf_filtered (stream, "var ");
372 pascal_print_type (TYPE_FIELD_TYPE (type
, i
), "" /* TYPE_FIELD_NAME seems invalid ! */
377 fprintf_filtered (stream
, ")");
381 /* Print any array sizes, function arguments or close parentheses
382 needed after the variable name (to describe its type).
383 Args work like pascal_type_print_varspec_prefix. */
386 pascal_type_print_varspec_suffix (struct type
*type
, struct ui_file
*stream
,
387 int show
, int passed_a_ptr
,
393 if (TYPE_NAME (type
) && show
<= 0)
398 switch (TYPE_CODE (type
))
400 case TYPE_CODE_ARRAY
:
402 fprintf_filtered (stream
, ")");
405 case TYPE_CODE_MEMBER
:
407 fprintf_filtered (stream
, ")");
408 pascal_type_print_varspec_suffix (TYPE_TARGET_TYPE (type
), stream
, 0, 0, 0);
411 case TYPE_CODE_METHOD
:
413 fprintf_filtered (stream
, ")");
414 pascal_type_print_method_args ("",
417 /* pascal_type_print_args (type, stream); */
418 if (TYPE_CODE (TYPE_TARGET_TYPE (type
)) != TYPE_CODE_VOID
)
420 fprintf_filtered (stream
, " : ");
421 pascal_type_print_varspec_prefix (TYPE_TARGET_TYPE (type
), stream
, 0, 0);
422 pascal_type_print_base (TYPE_TARGET_TYPE (type
), stream
, show
, 0);
423 pascal_type_print_varspec_suffix (TYPE_TARGET_TYPE (type
), stream
, 0,
430 pascal_type_print_varspec_suffix (TYPE_TARGET_TYPE (type
), stream
, 0, 1, 0);
435 fprintf_filtered (stream
, ")");
437 pascal_print_func_args (type
, stream
);
438 if (TYPE_CODE (TYPE_TARGET_TYPE (type
)) != TYPE_CODE_VOID
)
440 fprintf_filtered (stream
, " : ");
441 pascal_type_print_varspec_prefix (TYPE_TARGET_TYPE (type
), stream
, 0, 0);
442 pascal_type_print_base (TYPE_TARGET_TYPE (type
), stream
, show
, 0);
443 pascal_type_print_varspec_suffix (TYPE_TARGET_TYPE (type
), stream
, 0,
448 case TYPE_CODE_UNDEF
:
449 case TYPE_CODE_STRUCT
:
450 case TYPE_CODE_UNION
:
455 case TYPE_CODE_ERROR
:
459 case TYPE_CODE_RANGE
:
460 case TYPE_CODE_STRING
:
461 case TYPE_CODE_BITSTRING
:
462 case TYPE_CODE_COMPLEX
:
463 case TYPE_CODE_TYPEDEF
:
464 case TYPE_CODE_TEMPLATE
:
465 /* These types do not need a suffix. They are listed so that
466 gcc -Wall will report types that may not have been considered. */
469 error ("type not handled in pascal_type_print_varspec_suffix()");
474 /* Print the name of the type (or the ultimate pointer target,
475 function value or array element), or the description of a
478 SHOW positive means print details about the type (e.g. enum values),
479 and print structure elements passing SHOW - 1 for show.
480 SHOW negative means just print the type name or struct tag if there is one.
481 If there is no name, print something sensible but concise like
483 SHOW zero means just print the type name or struct tag if there is one.
484 If there is no name, print something sensible but not as concise like
485 "struct {int x; int y;}".
487 LEVEL is the number of spaces to indent by.
488 We increase it for some recursive calls. */
491 pascal_type_print_base (struct type
*type
, struct ui_file
*stream
, int show
,
496 register int lastval
;
499 s_none
, s_public
, s_private
, s_protected
507 fputs_filtered ("<type unknown>", stream
);
512 if ((TYPE_CODE (type
) == TYPE_CODE_PTR
) && (TYPE_CODE (TYPE_TARGET_TYPE (type
)) == TYPE_CODE_VOID
))
514 fprintf_filtered (stream
,
515 TYPE_NAME (type
) ? TYPE_NAME (type
) : "pointer");
518 /* When SHOW is zero or less, and there is a valid type name, then always
519 just print the type name directly from the type. */
522 && TYPE_NAME (type
) != NULL
)
524 fputs_filtered (TYPE_NAME (type
), stream
);
528 CHECK_TYPEDEF (type
);
530 switch (TYPE_CODE (type
))
532 case TYPE_CODE_TYPEDEF
:
534 case TYPE_CODE_MEMBER
:
536 /* case TYPE_CODE_FUNC:
537 case TYPE_CODE_METHOD: */
538 pascal_type_print_base (TYPE_TARGET_TYPE (type
), stream
, show
, level
);
541 case TYPE_CODE_ARRAY
:
542 /* pascal_type_print_varspec_prefix (TYPE_TARGET_TYPE (type), stream, 0, 0);
543 pascal_type_print_base (TYPE_TARGET_TYPE (type), stream, show, level);
544 pascal_type_print_varspec_suffix (TYPE_TARGET_TYPE (type), stream, 0, 0, 0); */
545 pascal_print_type (TYPE_TARGET_TYPE (type
), NULL
, stream
, 0, 0);
549 case TYPE_CODE_METHOD
:
551 pascal_type_print_base (TYPE_TARGET_TYPE (type), stream, show, level);
552 only after args !! */
554 case TYPE_CODE_STRUCT
:
555 if (TYPE_TAG_NAME (type
) != NULL
)
557 fputs_filtered (TYPE_TAG_NAME (type
), stream
);
558 fputs_filtered (" = ", stream
);
560 if (HAVE_CPLUS_STRUCT (type
))
562 fprintf_filtered (stream
, "class ");
566 fprintf_filtered (stream
, "record ");
570 case TYPE_CODE_UNION
:
571 if (TYPE_TAG_NAME (type
) != NULL
)
573 fputs_filtered (TYPE_TAG_NAME (type
), stream
);
574 fputs_filtered (" = ", stream
);
576 fprintf_filtered (stream
, "case <?> of ");
582 /* If we just printed a tag name, no need to print anything else. */
583 if (TYPE_TAG_NAME (type
) == NULL
)
584 fprintf_filtered (stream
, "{...}");
586 else if (show
> 0 || TYPE_TAG_NAME (type
) == NULL
)
588 pascal_type_print_derivation_info (stream
, type
);
590 fprintf_filtered (stream
, "\n");
591 if ((TYPE_NFIELDS (type
) == 0) && (TYPE_NFN_FIELDS (type
) == 0))
593 if (TYPE_FLAGS (type
) & TYPE_FLAG_STUB
)
594 fprintfi_filtered (level
+ 4, stream
, "<incomplete type>\n");
596 fprintfi_filtered (level
+ 4, stream
, "<no data fields>\n");
599 /* Start off with no specific section type, so we can print
600 one for the first field we find, and use that section type
601 thereafter until we find another type. */
603 section_type
= s_none
;
605 /* If there is a base class for this type,
606 do not print the field that it occupies. */
608 len
= TYPE_NFIELDS (type
);
609 for (i
= TYPE_N_BASECLASSES (type
); i
< len
; i
++)
612 /* Don't print out virtual function table. */
613 if (STREQN (TYPE_FIELD_NAME (type
, i
), "_vptr", 5)
614 && is_cplus_marker ((TYPE_FIELD_NAME (type
, i
))[5]))
617 /* If this is a pascal object or class we can print the
618 various section labels. */
620 if (HAVE_CPLUS_STRUCT (type
))
622 if (TYPE_FIELD_PROTECTED (type
, i
))
624 if (section_type
!= s_protected
)
626 section_type
= s_protected
;
627 fprintfi_filtered (level
+ 2, stream
,
631 else if (TYPE_FIELD_PRIVATE (type
, i
))
633 if (section_type
!= s_private
)
635 section_type
= s_private
;
636 fprintfi_filtered (level
+ 2, stream
, "private\n");
641 if (section_type
!= s_public
)
643 section_type
= s_public
;
644 fprintfi_filtered (level
+ 2, stream
, "public\n");
649 print_spaces_filtered (level
+ 4, stream
);
650 if (TYPE_FIELD_STATIC (type
, i
))
652 fprintf_filtered (stream
, "static ");
654 pascal_print_type (TYPE_FIELD_TYPE (type
, i
),
655 TYPE_FIELD_NAME (type
, i
),
656 stream
, show
- 1, level
+ 4);
657 if (!TYPE_FIELD_STATIC (type
, i
)
658 && TYPE_FIELD_PACKED (type
, i
))
660 /* It is a bitfield. This code does not attempt
661 to look at the bitpos and reconstruct filler,
662 unnamed fields. This would lead to misleading
663 results if the compiler does not put out fields
664 for such things (I don't know what it does). */
665 fprintf_filtered (stream
, " : %d",
666 TYPE_FIELD_BITSIZE (type
, i
));
668 fprintf_filtered (stream
, ";\n");
671 /* If there are both fields and methods, put a space between. */
672 len
= TYPE_NFN_FIELDS (type
);
673 if (len
&& section_type
!= s_none
)
674 fprintf_filtered (stream
, "\n");
676 /* Pbject pascal: print out the methods */
678 for (i
= 0; i
< len
; i
++)
680 struct fn_field
*f
= TYPE_FN_FIELDLIST1 (type
, i
);
681 int j
, len2
= TYPE_FN_FIELDLIST_LENGTH (type
, i
);
682 char *method_name
= TYPE_FN_FIELDLIST_NAME (type
, i
);
683 char *name
= type_name_no_tag (type
);
684 /* this is GNU C++ specific
685 how can we know constructor/destructor?
686 It might work for GNU pascal */
687 for (j
= 0; j
< len2
; j
++)
689 char *physname
= TYPE_FN_FIELD_PHYSNAME (f
, j
);
691 int is_constructor
= STREQN (physname
, "__ct__", 6);
692 int is_destructor
= STREQN (physname
, "__dt__", 6);
695 if (TYPE_FN_FIELD_PROTECTED (f
, j
))
697 if (section_type
!= s_protected
)
699 section_type
= s_protected
;
700 fprintfi_filtered (level
+ 2, stream
,
704 else if (TYPE_FN_FIELD_PRIVATE (f
, j
))
706 if (section_type
!= s_private
)
708 section_type
= s_private
;
709 fprintfi_filtered (level
+ 2, stream
, "private\n");
714 if (section_type
!= s_public
)
716 section_type
= s_public
;
717 fprintfi_filtered (level
+ 2, stream
, "public\n");
721 print_spaces_filtered (level
+ 4, stream
);
722 if (TYPE_FN_FIELD_STATIC_P (f
, j
))
723 fprintf_filtered (stream
, "static ");
724 if (TYPE_TARGET_TYPE (TYPE_FN_FIELD_TYPE (f
, j
)) == 0)
726 /* Keep GDB from crashing here. */
727 fprintf_filtered (stream
, "<undefined type> %s;\n",
728 TYPE_FN_FIELD_PHYSNAME (f
, j
));
734 fprintf_filtered (stream
, "constructor ");
736 else if (is_destructor
)
738 fprintf_filtered (stream
, "destructor ");
740 else if (TYPE_TARGET_TYPE (TYPE_FN_FIELD_TYPE (f
, j
)) != 0 &&
741 TYPE_CODE (TYPE_TARGET_TYPE (TYPE_FN_FIELD_TYPE (f
, j
))) != TYPE_CODE_VOID
)
743 fprintf_filtered (stream
, "function ");
747 fprintf_filtered (stream
, "procedure ");
749 /* this does not work, no idea why !! */
751 pascal_type_print_method_args (physname
,
755 if (TYPE_TARGET_TYPE (TYPE_FN_FIELD_TYPE (f
, j
)) != 0 &&
756 TYPE_CODE (TYPE_TARGET_TYPE (TYPE_FN_FIELD_TYPE (f
, j
))) != TYPE_CODE_VOID
)
758 fputs_filtered (" : ", stream
);
759 type_print (TYPE_TARGET_TYPE (TYPE_FN_FIELD_TYPE (f
, j
)),
762 if (TYPE_FN_FIELD_VIRTUAL_P (f
, j
))
763 fprintf_filtered (stream
, "; virtual");
765 fprintf_filtered (stream
, ";\n");
768 fprintfi_filtered (level
, stream
, "end");
773 if (TYPE_TAG_NAME (type
) != NULL
)
775 fputs_filtered (TYPE_TAG_NAME (type
), stream
);
777 fputs_filtered (" ", stream
);
779 /* enum is just defined by
780 type enume_name = (enum_member1,enum_member2,...) */
781 fprintf_filtered (stream
, " = ");
785 /* If we just printed a tag name, no need to print anything else. */
786 if (TYPE_TAG_NAME (type
) == NULL
)
787 fprintf_filtered (stream
, "(...)");
789 else if (show
> 0 || TYPE_TAG_NAME (type
) == NULL
)
791 fprintf_filtered (stream
, "(");
792 len
= TYPE_NFIELDS (type
);
794 for (i
= 0; i
< len
; i
++)
798 fprintf_filtered (stream
, ", ");
800 fputs_filtered (TYPE_FIELD_NAME (type
, i
), stream
);
801 if (lastval
!= TYPE_FIELD_BITPOS (type
, i
))
803 fprintf_filtered (stream
, " := %d", TYPE_FIELD_BITPOS (type
, i
));
804 lastval
= TYPE_FIELD_BITPOS (type
, i
);
808 fprintf_filtered (stream
, ")");
813 fprintf_filtered (stream
, "void");
816 case TYPE_CODE_UNDEF
:
817 fprintf_filtered (stream
, "record <unknown>");
820 case TYPE_CODE_ERROR
:
821 fprintf_filtered (stream
, "<unknown type>");
824 /* this probably does not work for enums */
825 case TYPE_CODE_RANGE
:
827 struct type
*target
= TYPE_TARGET_TYPE (type
);
829 target
= builtin_type_long
;
830 print_type_scalar (target
, TYPE_LOW_BOUND (type
), stream
);
831 fputs_filtered ("..", stream
);
832 print_type_scalar (target
, TYPE_HIGH_BOUND (type
), stream
);
837 fputs_filtered ("set of ", stream
);
838 pascal_print_type (TYPE_INDEX_TYPE (type
), "", stream
,
843 /* Handle types not explicitly handled by the other cases,
844 such as fundamental types. For these, just print whatever
845 the type name is, as recorded in the type itself. If there
846 is no type name, then complain. */
847 if (TYPE_NAME (type
) != NULL
)
849 fputs_filtered (TYPE_NAME (type
), stream
);
853 /* At least for dump_symtab, it is important that this not be
855 fprintf_filtered (stream
, "<invalid unnamed pascal type code %d>",