1 /* Support for printing Pascal types for GDB, the GNU debugger.
2 Copyright (C) 2000-2015 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"
36 static void pascal_type_print_varspec_suffix (struct type
*, struct ui_file
*,
38 const struct type_print_options
*);
40 static void pascal_type_print_derivation_info (struct ui_file
*,
45 /* LEVEL is the depth to indent lines by. */
48 pascal_print_type (struct type
*type
, const char *varstring
,
49 struct ui_file
*stream
, int show
, int level
,
50 const struct type_print_options
*flags
)
55 code
= TYPE_CODE (type
);
60 if ((code
== TYPE_CODE_FUNC
61 || code
== TYPE_CODE_METHOD
))
63 pascal_type_print_varspec_prefix (type
, stream
, show
, 0, flags
);
66 fputs_filtered (varstring
, stream
);
68 if ((varstring
!= NULL
&& *varstring
!= '\0')
69 && !(code
== TYPE_CODE_FUNC
70 || code
== TYPE_CODE_METHOD
))
72 fputs_filtered (" : ", stream
);
75 if (!(code
== TYPE_CODE_FUNC
76 || code
== TYPE_CODE_METHOD
))
78 pascal_type_print_varspec_prefix (type
, stream
, show
, 0, flags
);
81 pascal_type_print_base (type
, stream
, show
, level
, flags
);
82 /* For demangled function names, we have the arglist as part of the name,
83 so don't print an additional pair of ()'s. */
85 demangled_args
= varstring
? strchr (varstring
, '(') != NULL
: 0;
86 pascal_type_print_varspec_suffix (type
, stream
, show
, 0, demangled_args
,
91 /* Print a typedef using Pascal syntax. TYPE is the underlying type.
92 NEW_SYMBOL is the symbol naming the type. STREAM is the stream on
96 pascal_print_typedef (struct type
*type
, struct symbol
*new_symbol
,
97 struct ui_file
*stream
)
100 fprintf_filtered (stream
, "type ");
101 fprintf_filtered (stream
, "%s = ", SYMBOL_PRINT_NAME (new_symbol
));
102 type_print (type
, "", stream
, 0);
103 fprintf_filtered (stream
, ";\n");
106 /* If TYPE is a derived type, then print out derivation information.
107 Print only the actual base classes of this type, not the base classes
108 of the base classes. I.e. for the derivation hierarchy:
111 class B : public A {int b; };
112 class C : public B {int c; };
114 Print the type of class C as:
120 Not as the following (like gdb used to), which is not legal C++ syntax for
121 derived types and may be confused with the multiple inheritance form:
123 class C : public B : public A {
127 In general, gdb should try to print the types as closely as possible to
128 the form that they appear in the source code. */
131 pascal_type_print_derivation_info (struct ui_file
*stream
, struct type
*type
)
136 for (i
= 0; i
< TYPE_N_BASECLASSES (type
); i
++)
138 fputs_filtered (i
== 0 ? ": " : ", ", stream
);
139 fprintf_filtered (stream
, "%s%s ",
140 BASETYPE_VIA_PUBLIC (type
, i
) ? "public" : "private",
141 BASETYPE_VIA_VIRTUAL (type
, i
) ? " virtual" : "");
142 name
= type_name_no_tag (TYPE_BASECLASS (type
, i
));
143 fprintf_filtered (stream
, "%s", name
? name
: "(null)");
147 fputs_filtered (" ", stream
);
151 /* Print the Pascal method arguments ARGS to the file STREAM. */
154 pascal_type_print_method_args (const char *physname
, const char *methodname
,
155 struct ui_file
*stream
)
157 int is_constructor
= (strncmp (physname
, "__ct__", 6) == 0);
158 int is_destructor
= (strncmp (physname
, "__dt__", 6) == 0);
160 if (is_constructor
|| is_destructor
)
165 fputs_filtered (methodname
, stream
);
167 if (physname
&& (*physname
!= 0))
169 fputs_filtered (" (", stream
);
170 /* We must demangle this. */
171 while (isdigit (physname
[0]))
177 while (isdigit (physname
[len
]))
181 i
= strtol (physname
, &argname
, 0);
184 for (j
= 0; j
< i
; ++j
)
185 fputc_filtered (physname
[j
], stream
);
188 if (physname
[0] != 0)
190 fputs_filtered (", ", stream
);
193 fputs_filtered (")", stream
);
197 /* Print any asterisks or open-parentheses needed before the
198 variable name (to describe its type).
200 On outermost call, pass 0 for PASSED_A_PTR.
201 On outermost call, SHOW > 0 means should ignore
202 any typename for TYPE and show its details.
203 SHOW is always zero on recursive calls. */
206 pascal_type_print_varspec_prefix (struct type
*type
, struct ui_file
*stream
,
207 int show
, int passed_a_ptr
,
208 const struct type_print_options
*flags
)
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,
224 break; /* Pointer should be handled normally
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_SELF_TYPE (type
),
243 stream
, 0, passed_a_ptr
, flags
);
244 fprintf_filtered (stream
, "::");
249 pascal_type_print_varspec_prefix (TYPE_TARGET_TYPE (type
), stream
, 0, 1,
251 fprintf_filtered (stream
, "&");
256 fprintf_filtered (stream
, "(");
258 if (TYPE_CODE (TYPE_TARGET_TYPE (type
)) != TYPE_CODE_VOID
)
260 fprintf_filtered (stream
, "function ");
264 fprintf_filtered (stream
, "procedure ");
269 case TYPE_CODE_ARRAY
:
271 fprintf_filtered (stream
, "(");
272 fprintf_filtered (stream
, "array ");
273 if (TYPE_LENGTH (TYPE_TARGET_TYPE (type
)) > 0
274 && !TYPE_ARRAY_UPPER_BOUND_IS_UNDEFINED (type
))
275 fprintf_filtered (stream
, "[%s..%s] ",
276 plongest (TYPE_ARRAY_LOWER_BOUND_VALUE (type
)),
277 plongest (TYPE_ARRAY_UPPER_BOUND_VALUE (type
)));
278 fprintf_filtered (stream
, "of ");
281 case TYPE_CODE_UNDEF
:
282 case TYPE_CODE_STRUCT
:
283 case TYPE_CODE_UNION
:
288 case TYPE_CODE_ERROR
:
292 case TYPE_CODE_RANGE
:
293 case TYPE_CODE_STRING
:
294 case TYPE_CODE_COMPLEX
:
295 case TYPE_CODE_TYPEDEF
:
296 /* These types need no prefix. They are listed here so that
297 gcc -Wall will reveal any types that haven't been handled. */
300 error (_("type not handled in pascal_type_print_varspec_prefix()"));
306 pascal_print_func_args (struct type
*type
, struct ui_file
*stream
,
307 const struct type_print_options
*flags
)
309 int i
, len
= TYPE_NFIELDS (type
);
313 fprintf_filtered (stream
, "(");
315 for (i
= 0; i
< len
; i
++)
319 fputs_filtered (", ", stream
);
322 /* Can we find if it is a var parameter ??
323 if ( TYPE_FIELD(type, i) == )
325 fprintf_filtered (stream, "var ");
327 pascal_print_type (TYPE_FIELD_TYPE (type
, i
), "" /* TYPE_FIELD_NAME
329 ,stream
, -1, 0, flags
);
333 fprintf_filtered (stream
, ")");
337 /* Print any array sizes, function arguments or close parentheses
338 needed after the variable name (to describe its type).
339 Args work like pascal_type_print_varspec_prefix. */
342 pascal_type_print_varspec_suffix (struct type
*type
, struct ui_file
*stream
,
343 int show
, int passed_a_ptr
,
345 const struct type_print_options
*flags
)
350 if (TYPE_NAME (type
) && show
<= 0)
355 switch (TYPE_CODE (type
))
357 case TYPE_CODE_ARRAY
:
359 fprintf_filtered (stream
, ")");
362 case TYPE_CODE_METHOD
:
364 fprintf_filtered (stream
, ")");
365 pascal_type_print_method_args ("",
368 if (TYPE_CODE (TYPE_TARGET_TYPE (type
)) != TYPE_CODE_VOID
)
370 fprintf_filtered (stream
, " : ");
371 pascal_type_print_varspec_prefix (TYPE_TARGET_TYPE (type
),
372 stream
, 0, 0, flags
);
373 pascal_type_print_base (TYPE_TARGET_TYPE (type
), stream
, show
, 0,
375 pascal_type_print_varspec_suffix (TYPE_TARGET_TYPE (type
), stream
, 0,
376 passed_a_ptr
, 0, flags
);
382 pascal_type_print_varspec_suffix (TYPE_TARGET_TYPE (type
),
383 stream
, 0, 1, 0, flags
);
388 fprintf_filtered (stream
, ")");
390 pascal_print_func_args (type
, stream
, flags
);
391 if (TYPE_CODE (TYPE_TARGET_TYPE (type
)) != TYPE_CODE_VOID
)
393 fprintf_filtered (stream
, " : ");
394 pascal_type_print_varspec_prefix (TYPE_TARGET_TYPE (type
),
395 stream
, 0, 0, flags
);
396 pascal_type_print_base (TYPE_TARGET_TYPE (type
), stream
, show
, 0,
398 pascal_type_print_varspec_suffix (TYPE_TARGET_TYPE (type
), stream
, 0,
399 passed_a_ptr
, 0, flags
);
403 case TYPE_CODE_UNDEF
:
404 case TYPE_CODE_STRUCT
:
405 case TYPE_CODE_UNION
:
410 case TYPE_CODE_ERROR
:
414 case TYPE_CODE_RANGE
:
415 case TYPE_CODE_STRING
:
416 case TYPE_CODE_COMPLEX
:
417 case TYPE_CODE_TYPEDEF
:
418 /* These types do not need a suffix. They are listed so that
419 gcc -Wall will report types that may not have been considered. */
422 error (_("type not handled in pascal_type_print_varspec_suffix()"));
427 /* Print the name of the type (or the ultimate pointer target,
428 function value or array element), or the description of a
431 SHOW positive means print details about the type (e.g. enum values),
432 and print structure elements passing SHOW - 1 for show.
433 SHOW negative means just print the type name or struct tag if there is one.
434 If there is no name, print something sensible but concise like
436 SHOW zero means just print the type name or struct tag if there is one.
437 If there is no name, print something sensible but not as concise like
438 "struct {int x; int y;}".
440 LEVEL is the number of spaces to indent by.
441 We increase it for some recursive calls. */
444 pascal_type_print_base (struct type
*type
, struct ui_file
*stream
, int show
,
445 int level
, const struct type_print_options
*flags
)
452 s_none
, s_public
, s_private
, s_protected
460 fputs_filtered ("<type unknown>", stream
);
465 if ((TYPE_CODE (type
) == TYPE_CODE_PTR
)
466 && (TYPE_CODE (TYPE_TARGET_TYPE (type
)) == TYPE_CODE_VOID
))
468 fputs_filtered (TYPE_NAME (type
) ? TYPE_NAME (type
) : "pointer",
472 /* When SHOW is zero or less, and there is a valid type name, then always
473 just print the type name directly from the type. */
476 && TYPE_NAME (type
) != NULL
)
478 fputs_filtered (TYPE_NAME (type
), stream
);
482 CHECK_TYPEDEF (type
);
484 switch (TYPE_CODE (type
))
486 case TYPE_CODE_TYPEDEF
:
489 /* case TYPE_CODE_FUNC:
490 case TYPE_CODE_METHOD: */
491 pascal_type_print_base (TYPE_TARGET_TYPE (type
), stream
, show
, level
,
495 case TYPE_CODE_ARRAY
:
496 /* pascal_type_print_varspec_prefix (TYPE_TARGET_TYPE (type),
498 pascal_type_print_base (TYPE_TARGET_TYPE (type),
499 stream, show, level);
500 pascal_type_print_varspec_suffix (TYPE_TARGET_TYPE (type),
502 pascal_print_type (TYPE_TARGET_TYPE (type
), NULL
, stream
, 0, 0, flags
);
506 case TYPE_CODE_METHOD
:
508 pascal_type_print_base (TYPE_TARGET_TYPE (type), stream, show, level);
509 only after args !! */
511 case TYPE_CODE_STRUCT
:
512 if (TYPE_TAG_NAME (type
) != NULL
)
514 fputs_filtered (TYPE_TAG_NAME (type
), stream
);
515 fputs_filtered (" = ", stream
);
517 if (HAVE_CPLUS_STRUCT (type
))
519 fprintf_filtered (stream
, "class ");
523 fprintf_filtered (stream
, "record ");
527 case TYPE_CODE_UNION
:
528 if (TYPE_TAG_NAME (type
) != NULL
)
530 fputs_filtered (TYPE_TAG_NAME (type
), stream
);
531 fputs_filtered (" = ", stream
);
533 fprintf_filtered (stream
, "case <?> of ");
539 /* If we just printed a tag name, no need to print anything else. */
540 if (TYPE_TAG_NAME (type
) == NULL
)
541 fprintf_filtered (stream
, "{...}");
543 else if (show
> 0 || TYPE_TAG_NAME (type
) == NULL
)
545 pascal_type_print_derivation_info (stream
, type
);
547 fprintf_filtered (stream
, "\n");
548 if ((TYPE_NFIELDS (type
) == 0) && (TYPE_NFN_FIELDS (type
) == 0))
550 if (TYPE_STUB (type
))
551 fprintfi_filtered (level
+ 4, stream
, "<incomplete type>\n");
553 fprintfi_filtered (level
+ 4, stream
, "<no data fields>\n");
556 /* Start off with no specific section type, so we can print
557 one for the first field we find, and use that section type
558 thereafter until we find another type. */
560 section_type
= s_none
;
562 /* If there is a base class for this type,
563 do not print the field that it occupies. */
565 len
= TYPE_NFIELDS (type
);
566 for (i
= TYPE_N_BASECLASSES (type
); i
< len
; i
++)
569 /* Don't print out virtual function table. */
570 if ((strncmp (TYPE_FIELD_NAME (type
, i
), "_vptr", 5) == 0)
571 && is_cplus_marker ((TYPE_FIELD_NAME (type
, i
))[5]))
574 /* If this is a pascal object or class we can print the
575 various section labels. */
577 if (HAVE_CPLUS_STRUCT (type
))
579 if (TYPE_FIELD_PROTECTED (type
, i
))
581 if (section_type
!= s_protected
)
583 section_type
= s_protected
;
584 fprintfi_filtered (level
+ 2, stream
,
588 else if (TYPE_FIELD_PRIVATE (type
, i
))
590 if (section_type
!= s_private
)
592 section_type
= s_private
;
593 fprintfi_filtered (level
+ 2, stream
, "private\n");
598 if (section_type
!= s_public
)
600 section_type
= s_public
;
601 fprintfi_filtered (level
+ 2, stream
, "public\n");
606 print_spaces_filtered (level
+ 4, stream
);
607 if (field_is_static (&TYPE_FIELD (type
, i
)))
608 fprintf_filtered (stream
, "static ");
609 pascal_print_type (TYPE_FIELD_TYPE (type
, i
),
610 TYPE_FIELD_NAME (type
, i
),
611 stream
, show
- 1, level
+ 4, flags
);
612 if (!field_is_static (&TYPE_FIELD (type
, i
))
613 && TYPE_FIELD_PACKED (type
, i
))
615 /* It is a bitfield. This code does not attempt
616 to look at the bitpos and reconstruct filler,
617 unnamed fields. This would lead to misleading
618 results if the compiler does not put out fields
619 for such things (I don't know what it does). */
620 fprintf_filtered (stream
, " : %d",
621 TYPE_FIELD_BITSIZE (type
, i
));
623 fprintf_filtered (stream
, ";\n");
626 /* If there are both fields and methods, put a space between. */
627 len
= TYPE_NFN_FIELDS (type
);
628 if (len
&& section_type
!= s_none
)
629 fprintf_filtered (stream
, "\n");
631 /* Object pascal: print out the methods. */
633 for (i
= 0; i
< len
; i
++)
635 struct fn_field
*f
= TYPE_FN_FIELDLIST1 (type
, i
);
636 int j
, len2
= TYPE_FN_FIELDLIST_LENGTH (type
, i
);
637 const char *method_name
= TYPE_FN_FIELDLIST_NAME (type
, i
);
639 /* this is GNU C++ specific
640 how can we know constructor/destructor?
641 It might work for GNU pascal. */
642 for (j
= 0; j
< len2
; j
++)
644 const char *physname
= TYPE_FN_FIELD_PHYSNAME (f
, j
);
646 int is_constructor
= (strncmp (physname
, "__ct__", 6) == 0);
647 int is_destructor
= (strncmp (physname
, "__dt__", 6) == 0);
650 if (TYPE_FN_FIELD_PROTECTED (f
, j
))
652 if (section_type
!= s_protected
)
654 section_type
= s_protected
;
655 fprintfi_filtered (level
+ 2, stream
,
659 else if (TYPE_FN_FIELD_PRIVATE (f
, j
))
661 if (section_type
!= s_private
)
663 section_type
= s_private
;
664 fprintfi_filtered (level
+ 2, stream
, "private\n");
669 if (section_type
!= s_public
)
671 section_type
= s_public
;
672 fprintfi_filtered (level
+ 2, stream
, "public\n");
676 print_spaces_filtered (level
+ 4, stream
);
677 if (TYPE_FN_FIELD_STATIC_P (f
, j
))
678 fprintf_filtered (stream
, "static ");
679 if (TYPE_TARGET_TYPE (TYPE_FN_FIELD_TYPE (f
, j
)) == 0)
681 /* Keep GDB from crashing here. */
682 fprintf_filtered (stream
, "<undefined type> %s;\n",
683 TYPE_FN_FIELD_PHYSNAME (f
, j
));
689 fprintf_filtered (stream
, "constructor ");
691 else if (is_destructor
)
693 fprintf_filtered (stream
, "destructor ");
695 else if (TYPE_TARGET_TYPE (TYPE_FN_FIELD_TYPE (f
, j
)) != 0
696 && TYPE_CODE (TYPE_TARGET_TYPE (
697 TYPE_FN_FIELD_TYPE (f
, j
))) != TYPE_CODE_VOID
)
699 fprintf_filtered (stream
, "function ");
703 fprintf_filtered (stream
, "procedure ");
705 /* This does not work, no idea why !! */
707 pascal_type_print_method_args (physname
,
711 if (TYPE_TARGET_TYPE (TYPE_FN_FIELD_TYPE (f
, j
)) != 0
712 && TYPE_CODE (TYPE_TARGET_TYPE (
713 TYPE_FN_FIELD_TYPE (f
, j
))) != TYPE_CODE_VOID
)
715 fputs_filtered (" : ", stream
);
716 type_print (TYPE_TARGET_TYPE (TYPE_FN_FIELD_TYPE (f
, j
)),
719 if (TYPE_FN_FIELD_VIRTUAL_P (f
, j
))
720 fprintf_filtered (stream
, "; virtual");
722 fprintf_filtered (stream
, ";\n");
725 fprintfi_filtered (level
, stream
, "end");
730 if (TYPE_TAG_NAME (type
) != NULL
)
732 fputs_filtered (TYPE_TAG_NAME (type
), stream
);
734 fputs_filtered (" ", stream
);
736 /* enum is just defined by
737 type enume_name = (enum_member1,enum_member2,...) */
738 fprintf_filtered (stream
, " = ");
742 /* If we just printed a tag name, no need to print anything else. */
743 if (TYPE_TAG_NAME (type
) == NULL
)
744 fprintf_filtered (stream
, "(...)");
746 else if (show
> 0 || TYPE_TAG_NAME (type
) == NULL
)
748 fprintf_filtered (stream
, "(");
749 len
= TYPE_NFIELDS (type
);
751 for (i
= 0; i
< len
; i
++)
755 fprintf_filtered (stream
, ", ");
757 fputs_filtered (TYPE_FIELD_NAME (type
, i
), stream
);
758 if (lastval
!= TYPE_FIELD_ENUMVAL (type
, i
))
760 fprintf_filtered (stream
,
762 plongest (TYPE_FIELD_ENUMVAL (type
, i
)));
763 lastval
= TYPE_FIELD_ENUMVAL (type
, i
);
767 fprintf_filtered (stream
, ")");
772 fprintf_filtered (stream
, "void");
775 case TYPE_CODE_UNDEF
:
776 fprintf_filtered (stream
, "record <unknown>");
779 case TYPE_CODE_ERROR
:
780 fprintf_filtered (stream
, "%s", TYPE_ERROR_NAME (type
));
783 /* this probably does not work for enums. */
784 case TYPE_CODE_RANGE
:
786 struct type
*target
= TYPE_TARGET_TYPE (type
);
788 print_type_scalar (target
, TYPE_LOW_BOUND (type
), stream
);
789 fputs_filtered ("..", stream
);
790 print_type_scalar (target
, TYPE_HIGH_BOUND (type
), stream
);
795 fputs_filtered ("set of ", stream
);
796 pascal_print_type (TYPE_INDEX_TYPE (type
), "", stream
,
797 show
- 1, level
, flags
);
800 case TYPE_CODE_STRING
:
801 fputs_filtered ("String", stream
);
805 /* Handle types not explicitly handled by the other cases,
806 such as fundamental types. For these, just print whatever
807 the type name is, as recorded in the type itself. If there
808 is no type name, then complain. */
809 if (TYPE_NAME (type
) != NULL
)
811 fputs_filtered (TYPE_NAME (type
), stream
);
815 /* At least for dump_symtab, it is important that this not be
817 fprintf_filtered (stream
, "<invalid unnamed pascal type code %d>",
This page took 0.057605 seconds and 4 git commands to generate.