1 /* Support for printing Pascal types for GDB, the GNU debugger.
2 Copyright (C) 2000-2002, 2006-2012 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"
34 #include "gdb_string.h"
38 static void pascal_type_print_varspec_suffix (struct type
*, struct ui_file
*,
41 static void pascal_type_print_derivation_info (struct ui_file
*,
44 void pascal_type_print_varspec_prefix (struct type
*, struct ui_file
*,
48 /* LEVEL is the depth to indent lines by. */
51 pascal_print_type (struct type
*type
, const char *varstring
,
52 struct ui_file
*stream
, int show
, int level
)
57 code
= TYPE_CODE (type
);
62 if ((code
== TYPE_CODE_FUNC
63 || code
== TYPE_CODE_METHOD
))
65 pascal_type_print_varspec_prefix (type
, stream
, show
, 0);
68 fputs_filtered (varstring
, stream
);
70 if ((varstring
!= NULL
&& *varstring
!= '\0')
71 && !(code
== TYPE_CODE_FUNC
72 || code
== TYPE_CODE_METHOD
))
74 fputs_filtered (" : ", stream
);
77 if (!(code
== TYPE_CODE_FUNC
78 || code
== TYPE_CODE_METHOD
))
80 pascal_type_print_varspec_prefix (type
, stream
, show
, 0);
83 pascal_type_print_base (type
, stream
, show
, level
);
84 /* For demangled function names, we have the arglist as part of the name,
85 so don't print an additional pair of ()'s. */
87 demangled_args
= varstring
? strchr (varstring
, '(') != NULL
: 0;
88 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 CHECK_TYPEDEF (type
);
101 fprintf_filtered (stream
, "type ");
102 fprintf_filtered (stream
, "%s = ", SYMBOL_PRINT_NAME (new_symbol
));
103 type_print (type
, "", stream
, 0);
104 fprintf_filtered (stream
, ";\n");
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_name_no_tag (TYPE_BASECLASS (type
, i
));
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
= (strncmp (physname
, "__ct__", 6) == 0);
159 int is_destructor
= (strncmp (physname
, "__dt__", 6) == 0);
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
)
213 if (TYPE_NAME (type
) && show
<= 0)
218 switch (TYPE_CODE (type
))
221 fprintf_filtered (stream
, "^");
222 pascal_type_print_varspec_prefix (TYPE_TARGET_TYPE (type
), stream
, 0, 1);
223 break; /* Pointer should be handled normally
226 case TYPE_CODE_METHOD
:
228 fprintf_filtered (stream
, "(");
229 if (TYPE_CODE (TYPE_TARGET_TYPE (type
)) != TYPE_CODE_VOID
)
231 fprintf_filtered (stream
, "function ");
235 fprintf_filtered (stream
, "procedure ");
240 fprintf_filtered (stream
, " ");
241 pascal_type_print_base (TYPE_DOMAIN_TYPE (type
),
242 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_TARGET_TYPE (type
)) > 0
272 && !TYPE_ARRAY_UPPER_BOUND_IS_UNDEFINED (type
))
273 fprintf_filtered (stream
, "[%s..%s] ",
274 plongest (TYPE_ARRAY_LOWER_BOUND_VALUE (type
)),
275 plongest (TYPE_ARRAY_UPPER_BOUND_VALUE (type
)));
276 fprintf_filtered (stream
, "of ");
279 case TYPE_CODE_UNDEF
:
280 case TYPE_CODE_STRUCT
:
281 case TYPE_CODE_UNION
:
286 case TYPE_CODE_ERROR
:
290 case TYPE_CODE_RANGE
:
291 case TYPE_CODE_STRING
:
292 case TYPE_CODE_BITSTRING
:
293 case TYPE_CODE_COMPLEX
:
294 case TYPE_CODE_TYPEDEF
:
295 /* These types need no prefix. They are listed here so that
296 gcc -Wall will reveal any types that haven't been handled. */
299 error (_("type not handled in pascal_type_print_varspec_prefix()"));
305 pascal_print_func_args (struct type
*type
, struct ui_file
*stream
)
307 int i
, len
= TYPE_NFIELDS (type
);
311 fprintf_filtered (stream
, "(");
313 for (i
= 0; i
< len
; i
++)
317 fputs_filtered (", ", stream
);
320 /* Can we find if it is a var parameter ??
321 if ( TYPE_FIELD(type, i) == )
323 fprintf_filtered (stream, "var ");
325 pascal_print_type (TYPE_FIELD_TYPE (type
, i
), "" /* TYPE_FIELD_NAME
331 fprintf_filtered (stream
, ")");
335 /* Print any array sizes, function arguments or close parentheses
336 needed after the variable name (to describe its type).
337 Args work like pascal_type_print_varspec_prefix. */
340 pascal_type_print_varspec_suffix (struct type
*type
, struct ui_file
*stream
,
341 int show
, int passed_a_ptr
,
347 if (TYPE_NAME (type
) && show
<= 0)
352 switch (TYPE_CODE (type
))
354 case TYPE_CODE_ARRAY
:
356 fprintf_filtered (stream
, ")");
359 case TYPE_CODE_METHOD
:
361 fprintf_filtered (stream
, ")");
362 pascal_type_print_method_args ("",
365 if (TYPE_CODE (TYPE_TARGET_TYPE (type
)) != TYPE_CODE_VOID
)
367 fprintf_filtered (stream
, " : ");
368 pascal_type_print_varspec_prefix (TYPE_TARGET_TYPE (type
),
370 pascal_type_print_base (TYPE_TARGET_TYPE (type
), stream
, show
, 0);
371 pascal_type_print_varspec_suffix (TYPE_TARGET_TYPE (type
), stream
, 0,
378 pascal_type_print_varspec_suffix (TYPE_TARGET_TYPE (type
),
384 fprintf_filtered (stream
, ")");
386 pascal_print_func_args (type
, stream
);
387 if (TYPE_CODE (TYPE_TARGET_TYPE (type
)) != TYPE_CODE_VOID
)
389 fprintf_filtered (stream
, " : ");
390 pascal_type_print_varspec_prefix (TYPE_TARGET_TYPE (type
),
392 pascal_type_print_base (TYPE_TARGET_TYPE (type
), stream
, show
, 0);
393 pascal_type_print_varspec_suffix (TYPE_TARGET_TYPE (type
), stream
, 0,
398 case TYPE_CODE_UNDEF
:
399 case TYPE_CODE_STRUCT
:
400 case TYPE_CODE_UNION
:
405 case TYPE_CODE_ERROR
:
409 case TYPE_CODE_RANGE
:
410 case TYPE_CODE_STRING
:
411 case TYPE_CODE_BITSTRING
:
412 case TYPE_CODE_COMPLEX
:
413 case TYPE_CODE_TYPEDEF
:
414 /* These types do not need a suffix. They are listed so that
415 gcc -Wall will report types that may not have been considered. */
418 error (_("type not handled in pascal_type_print_varspec_suffix()"));
423 /* Print the name of the type (or the ultimate pointer target,
424 function value or array element), or the description of a
427 SHOW positive means print details about the type (e.g. enum values),
428 and print structure elements passing SHOW - 1 for show.
429 SHOW negative means just print the type name or struct tag if there is one.
430 If there is no name, print something sensible but concise like
432 SHOW zero means just print the type name or struct tag if there is one.
433 If there is no name, print something sensible but not as concise like
434 "struct {int x; int y;}".
436 LEVEL is the number of spaces to indent by.
437 We increase it for some recursive calls. */
440 pascal_type_print_base (struct type
*type
, struct ui_file
*stream
, int show
,
448 s_none
, s_public
, s_private
, s_protected
456 fputs_filtered ("<type unknown>", stream
);
461 if ((TYPE_CODE (type
) == TYPE_CODE_PTR
)
462 && (TYPE_CODE (TYPE_TARGET_TYPE (type
)) == TYPE_CODE_VOID
))
464 fputs_filtered (TYPE_NAME (type
) ? TYPE_NAME (type
) : "pointer",
468 /* When SHOW is zero or less, and there is a valid type name, then always
469 just print the type name directly from the type. */
472 && TYPE_NAME (type
) != NULL
)
474 fputs_filtered (TYPE_NAME (type
), stream
);
478 CHECK_TYPEDEF (type
);
480 switch (TYPE_CODE (type
))
482 case TYPE_CODE_TYPEDEF
:
485 /* case TYPE_CODE_FUNC:
486 case TYPE_CODE_METHOD: */
487 pascal_type_print_base (TYPE_TARGET_TYPE (type
), stream
, show
, level
);
490 case TYPE_CODE_ARRAY
:
491 /* pascal_type_print_varspec_prefix (TYPE_TARGET_TYPE (type),
493 pascal_type_print_base (TYPE_TARGET_TYPE (type),
494 stream, show, level);
495 pascal_type_print_varspec_suffix (TYPE_TARGET_TYPE (type),
497 pascal_print_type (TYPE_TARGET_TYPE (type
), NULL
, stream
, 0, 0);
501 case TYPE_CODE_METHOD
:
503 pascal_type_print_base (TYPE_TARGET_TYPE (type), stream, show, level);
504 only after args !! */
506 case TYPE_CODE_STRUCT
:
507 if (TYPE_TAG_NAME (type
) != NULL
)
509 fputs_filtered (TYPE_TAG_NAME (type
), stream
);
510 fputs_filtered (" = ", stream
);
512 if (HAVE_CPLUS_STRUCT (type
))
514 fprintf_filtered (stream
, "class ");
518 fprintf_filtered (stream
, "record ");
522 case TYPE_CODE_UNION
:
523 if (TYPE_TAG_NAME (type
) != NULL
)
525 fputs_filtered (TYPE_TAG_NAME (type
), stream
);
526 fputs_filtered (" = ", stream
);
528 fprintf_filtered (stream
, "case <?> of ");
534 /* If we just printed a tag name, no need to print anything else. */
535 if (TYPE_TAG_NAME (type
) == NULL
)
536 fprintf_filtered (stream
, "{...}");
538 else if (show
> 0 || TYPE_TAG_NAME (type
) == NULL
)
540 pascal_type_print_derivation_info (stream
, type
);
542 fprintf_filtered (stream
, "\n");
543 if ((TYPE_NFIELDS (type
) == 0) && (TYPE_NFN_FIELDS (type
) == 0))
545 if (TYPE_STUB (type
))
546 fprintfi_filtered (level
+ 4, stream
, "<incomplete type>\n");
548 fprintfi_filtered (level
+ 4, stream
, "<no data fields>\n");
551 /* Start off with no specific section type, so we can print
552 one for the first field we find, and use that section type
553 thereafter until we find another type. */
555 section_type
= s_none
;
557 /* If there is a base class for this type,
558 do not print the field that it occupies. */
560 len
= TYPE_NFIELDS (type
);
561 for (i
= TYPE_N_BASECLASSES (type
); i
< len
; i
++)
564 /* Don't print out virtual function table. */
565 if ((strncmp (TYPE_FIELD_NAME (type
, i
), "_vptr", 5) == 0)
566 && is_cplus_marker ((TYPE_FIELD_NAME (type
, i
))[5]))
569 /* If this is a pascal object or class we can print the
570 various section labels. */
572 if (HAVE_CPLUS_STRUCT (type
))
574 if (TYPE_FIELD_PROTECTED (type
, i
))
576 if (section_type
!= s_protected
)
578 section_type
= s_protected
;
579 fprintfi_filtered (level
+ 2, stream
,
583 else if (TYPE_FIELD_PRIVATE (type
, i
))
585 if (section_type
!= s_private
)
587 section_type
= s_private
;
588 fprintfi_filtered (level
+ 2, stream
, "private\n");
593 if (section_type
!= s_public
)
595 section_type
= s_public
;
596 fprintfi_filtered (level
+ 2, stream
, "public\n");
601 print_spaces_filtered (level
+ 4, stream
);
602 if (field_is_static (&TYPE_FIELD (type
, i
)))
603 fprintf_filtered (stream
, "static ");
604 pascal_print_type (TYPE_FIELD_TYPE (type
, i
),
605 TYPE_FIELD_NAME (type
, i
),
606 stream
, show
- 1, level
+ 4);
607 if (!field_is_static (&TYPE_FIELD (type
, i
))
608 && TYPE_FIELD_PACKED (type
, i
))
610 /* It is a bitfield. This code does not attempt
611 to look at the bitpos and reconstruct filler,
612 unnamed fields. This would lead to misleading
613 results if the compiler does not put out fields
614 for such things (I don't know what it does). */
615 fprintf_filtered (stream
, " : %d",
616 TYPE_FIELD_BITSIZE (type
, i
));
618 fprintf_filtered (stream
, ";\n");
621 /* If there are both fields and methods, put a space between. */
622 len
= TYPE_NFN_FIELDS (type
);
623 if (len
&& section_type
!= s_none
)
624 fprintf_filtered (stream
, "\n");
626 /* Object pascal: print out the methods. */
628 for (i
= 0; i
< len
; i
++)
630 struct fn_field
*f
= TYPE_FN_FIELDLIST1 (type
, i
);
631 int j
, len2
= TYPE_FN_FIELDLIST_LENGTH (type
, i
);
632 const char *method_name
= TYPE_FN_FIELDLIST_NAME (type
, i
);
634 /* this is GNU C++ specific
635 how can we know constructor/destructor?
636 It might work for GNU pascal. */
637 for (j
= 0; j
< len2
; j
++)
639 const char *physname
= TYPE_FN_FIELD_PHYSNAME (f
, j
);
641 int is_constructor
= (strncmp (physname
, "__ct__", 6) == 0);
642 int is_destructor
= (strncmp (physname
, "__dt__", 6) == 0);
645 if (TYPE_FN_FIELD_PROTECTED (f
, j
))
647 if (section_type
!= s_protected
)
649 section_type
= s_protected
;
650 fprintfi_filtered (level
+ 2, stream
,
654 else if (TYPE_FN_FIELD_PRIVATE (f
, j
))
656 if (section_type
!= s_private
)
658 section_type
= s_private
;
659 fprintfi_filtered (level
+ 2, stream
, "private\n");
664 if (section_type
!= s_public
)
666 section_type
= s_public
;
667 fprintfi_filtered (level
+ 2, stream
, "public\n");
671 print_spaces_filtered (level
+ 4, stream
);
672 if (TYPE_FN_FIELD_STATIC_P (f
, j
))
673 fprintf_filtered (stream
, "static ");
674 if (TYPE_TARGET_TYPE (TYPE_FN_FIELD_TYPE (f
, j
)) == 0)
676 /* Keep GDB from crashing here. */
677 fprintf_filtered (stream
, "<undefined type> %s;\n",
678 TYPE_FN_FIELD_PHYSNAME (f
, j
));
684 fprintf_filtered (stream
, "constructor ");
686 else if (is_destructor
)
688 fprintf_filtered (stream
, "destructor ");
690 else if (TYPE_TARGET_TYPE (TYPE_FN_FIELD_TYPE (f
, j
)) != 0
691 && TYPE_CODE (TYPE_TARGET_TYPE (
692 TYPE_FN_FIELD_TYPE (f
, j
))) != TYPE_CODE_VOID
)
694 fprintf_filtered (stream
, "function ");
698 fprintf_filtered (stream
, "procedure ");
700 /* This does not work, no idea why !! */
702 pascal_type_print_method_args (physname
,
706 if (TYPE_TARGET_TYPE (TYPE_FN_FIELD_TYPE (f
, j
)) != 0
707 && TYPE_CODE (TYPE_TARGET_TYPE (
708 TYPE_FN_FIELD_TYPE (f
, j
))) != TYPE_CODE_VOID
)
710 fputs_filtered (" : ", stream
);
711 type_print (TYPE_TARGET_TYPE (TYPE_FN_FIELD_TYPE (f
, j
)),
714 if (TYPE_FN_FIELD_VIRTUAL_P (f
, j
))
715 fprintf_filtered (stream
, "; virtual");
717 fprintf_filtered (stream
, ";\n");
720 fprintfi_filtered (level
, stream
, "end");
725 if (TYPE_TAG_NAME (type
) != NULL
)
727 fputs_filtered (TYPE_TAG_NAME (type
), stream
);
729 fputs_filtered (" ", stream
);
731 /* enum is just defined by
732 type enume_name = (enum_member1,enum_member2,...) */
733 fprintf_filtered (stream
, " = ");
737 /* If we just printed a tag name, no need to print anything else. */
738 if (TYPE_TAG_NAME (type
) == NULL
)
739 fprintf_filtered (stream
, "(...)");
741 else if (show
> 0 || TYPE_TAG_NAME (type
) == NULL
)
743 fprintf_filtered (stream
, "(");
744 len
= TYPE_NFIELDS (type
);
746 for (i
= 0; i
< len
; i
++)
750 fprintf_filtered (stream
, ", ");
752 fputs_filtered (TYPE_FIELD_NAME (type
, i
), stream
);
753 if (lastval
!= TYPE_FIELD_ENUMVAL (type
, i
))
755 fprintf_filtered (stream
,
757 plongest (TYPE_FIELD_ENUMVAL (type
, i
)));
758 lastval
= TYPE_FIELD_ENUMVAL (type
, i
);
762 fprintf_filtered (stream
, ")");
767 fprintf_filtered (stream
, "void");
770 case TYPE_CODE_UNDEF
:
771 fprintf_filtered (stream
, "record <unknown>");
774 case TYPE_CODE_ERROR
:
775 fprintf_filtered (stream
, "%s", TYPE_ERROR_NAME (type
));
778 /* this probably does not work for enums. */
779 case TYPE_CODE_RANGE
:
781 struct type
*target
= TYPE_TARGET_TYPE (type
);
783 print_type_scalar (target
, TYPE_LOW_BOUND (type
), stream
);
784 fputs_filtered ("..", stream
);
785 print_type_scalar (target
, TYPE_HIGH_BOUND (type
), stream
);
790 fputs_filtered ("set of ", stream
);
791 pascal_print_type (TYPE_INDEX_TYPE (type
), "", stream
,
795 case TYPE_CODE_BITSTRING
:
796 fputs_filtered ("BitString", stream
);
799 case TYPE_CODE_STRING
:
800 fputs_filtered ("String", stream
);
804 /* Handle types not explicitly handled by the other cases,
805 such as fundamental types. For these, just print whatever
806 the type name is, as recorded in the type itself. If there
807 is no type name, then complain. */
808 if (TYPE_NAME (type
) != NULL
)
810 fputs_filtered (TYPE_NAME (type
), stream
);
814 /* At least for dump_symtab, it is important that this not be
816 fprintf_filtered (stream
, "<invalid unnamed pascal type code %d>",
This page took 0.046526 seconds and 4 git commands to generate.