1 /* Support for printing Pascal types for GDB, the GNU debugger.
2 Copyright (C) 2000-2013 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"
38 static void pascal_type_print_varspec_suffix (struct type
*, struct ui_file
*,
40 const struct type_print_options
*);
42 static void pascal_type_print_derivation_info (struct ui_file
*,
47 /* LEVEL is the depth to indent lines by. */
50 pascal_print_type (struct type
*type
, const char *varstring
,
51 struct ui_file
*stream
, int show
, int level
,
52 const struct type_print_options
*flags
)
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, flags
);
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, flags
);
83 pascal_type_print_base (type
, stream
, show
, level
, flags
);
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
,
93 /* Print a typedef using Pascal syntax. TYPE is the underlying type.
94 NEW_SYMBOL is the symbol naming the type. STREAM is the stream on
98 pascal_print_typedef (struct type
*type
, struct symbol
*new_symbol
,
99 struct ui_file
*stream
)
101 CHECK_TYPEDEF (type
);
102 fprintf_filtered (stream
, "type ");
103 fprintf_filtered (stream
, "%s = ", SYMBOL_PRINT_NAME (new_symbol
));
104 type_print (type
, "", stream
, 0);
105 fprintf_filtered (stream
, ";\n");
108 /* If TYPE is a derived type, then print out derivation information.
109 Print only the actual base classes of this type, not the base classes
110 of the base classes. I.e. for the derivation hierarchy:
113 class B : public A {int b; };
114 class C : public B {int c; };
116 Print the type of class C as:
122 Not as the following (like gdb used to), which is not legal C++ syntax for
123 derived types and may be confused with the multiple inheritance form:
125 class C : public B : public A {
129 In general, gdb should try to print the types as closely as possible to
130 the form that they appear in the source code. */
133 pascal_type_print_derivation_info (struct ui_file
*stream
, struct type
*type
)
138 for (i
= 0; i
< TYPE_N_BASECLASSES (type
); i
++)
140 fputs_filtered (i
== 0 ? ": " : ", ", stream
);
141 fprintf_filtered (stream
, "%s%s ",
142 BASETYPE_VIA_PUBLIC (type
, i
) ? "public" : "private",
143 BASETYPE_VIA_VIRTUAL (type
, i
) ? " virtual" : "");
144 name
= type_name_no_tag (TYPE_BASECLASS (type
, i
));
145 fprintf_filtered (stream
, "%s", name
? name
: "(null)");
149 fputs_filtered (" ", stream
);
153 /* Print the Pascal method arguments ARGS to the file STREAM. */
156 pascal_type_print_method_args (const char *physname
, const char *methodname
,
157 struct ui_file
*stream
)
159 int is_constructor
= (strncmp (physname
, "__ct__", 6) == 0);
160 int is_destructor
= (strncmp (physname
, "__dt__", 6) == 0);
162 if (is_constructor
|| is_destructor
)
167 fputs_filtered (methodname
, stream
);
169 if (physname
&& (*physname
!= 0))
171 fputs_filtered (" (", stream
);
172 /* We must demangle this. */
173 while (isdigit (physname
[0]))
179 while (isdigit (physname
[len
]))
183 i
= strtol (physname
, &argname
, 0);
186 for (j
= 0; j
< i
; ++j
)
187 fputc_filtered (physname
[j
], stream
);
190 if (physname
[0] != 0)
192 fputs_filtered (", ", stream
);
195 fputs_filtered (")", stream
);
199 /* Print any asterisks or open-parentheses needed before the
200 variable name (to describe its type).
202 On outermost call, pass 0 for PASSED_A_PTR.
203 On outermost call, SHOW > 0 means should ignore
204 any typename for TYPE and show its details.
205 SHOW is always zero on recursive calls. */
208 pascal_type_print_varspec_prefix (struct type
*type
, struct ui_file
*stream
,
209 int show
, int passed_a_ptr
,
210 const struct type_print_options
*flags
)
215 if (TYPE_NAME (type
) && show
<= 0)
220 switch (TYPE_CODE (type
))
223 fprintf_filtered (stream
, "^");
224 pascal_type_print_varspec_prefix (TYPE_TARGET_TYPE (type
), stream
, 0, 1,
226 break; /* Pointer should be handled normally
229 case TYPE_CODE_METHOD
:
231 fprintf_filtered (stream
, "(");
232 if (TYPE_CODE (TYPE_TARGET_TYPE (type
)) != TYPE_CODE_VOID
)
234 fprintf_filtered (stream
, "function ");
238 fprintf_filtered (stream
, "procedure ");
243 fprintf_filtered (stream
, " ");
244 pascal_type_print_base (TYPE_DOMAIN_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_CODE (TYPE_TARGET_TYPE (type
)) != TYPE_CODE_VOID
)
262 fprintf_filtered (stream
, "function ");
266 fprintf_filtered (stream
, "procedure ");
271 case TYPE_CODE_ARRAY
:
273 fprintf_filtered (stream
, "(");
274 fprintf_filtered (stream
, "array ");
275 if (TYPE_LENGTH (TYPE_TARGET_TYPE (type
)) > 0
276 && !TYPE_ARRAY_UPPER_BOUND_IS_UNDEFINED (type
))
277 fprintf_filtered (stream
, "[%s..%s] ",
278 plongest (TYPE_ARRAY_LOWER_BOUND_VALUE (type
)),
279 plongest (TYPE_ARRAY_UPPER_BOUND_VALUE (type
)));
280 fprintf_filtered (stream
, "of ");
283 case TYPE_CODE_UNDEF
:
284 case TYPE_CODE_STRUCT
:
285 case TYPE_CODE_UNION
:
290 case TYPE_CODE_ERROR
:
294 case TYPE_CODE_RANGE
:
295 case TYPE_CODE_STRING
:
296 case TYPE_CODE_COMPLEX
:
297 case TYPE_CODE_TYPEDEF
:
298 /* These types need no prefix. They are listed here so that
299 gcc -Wall will reveal any types that haven't been handled. */
302 error (_("type not handled in pascal_type_print_varspec_prefix()"));
308 pascal_print_func_args (struct type
*type
, struct ui_file
*stream
,
309 const struct type_print_options
*flags
)
311 int i
, len
= TYPE_NFIELDS (type
);
315 fprintf_filtered (stream
, "(");
317 for (i
= 0; i
< len
; i
++)
321 fputs_filtered (", ", stream
);
324 /* Can we find if it is a var parameter ??
325 if ( TYPE_FIELD(type, i) == )
327 fprintf_filtered (stream, "var ");
329 pascal_print_type (TYPE_FIELD_TYPE (type
, i
), "" /* TYPE_FIELD_NAME
331 ,stream
, -1, 0, flags
);
335 fprintf_filtered (stream
, ")");
339 /* Print any array sizes, function arguments or close parentheses
340 needed after the variable name (to describe its type).
341 Args work like pascal_type_print_varspec_prefix. */
344 pascal_type_print_varspec_suffix (struct type
*type
, struct ui_file
*stream
,
345 int show
, int passed_a_ptr
,
347 const struct type_print_options
*flags
)
352 if (TYPE_NAME (type
) && show
<= 0)
357 switch (TYPE_CODE (type
))
359 case TYPE_CODE_ARRAY
:
361 fprintf_filtered (stream
, ")");
364 case TYPE_CODE_METHOD
:
366 fprintf_filtered (stream
, ")");
367 pascal_type_print_method_args ("",
370 if (TYPE_CODE (TYPE_TARGET_TYPE (type
)) != TYPE_CODE_VOID
)
372 fprintf_filtered (stream
, " : ");
373 pascal_type_print_varspec_prefix (TYPE_TARGET_TYPE (type
),
374 stream
, 0, 0, flags
);
375 pascal_type_print_base (TYPE_TARGET_TYPE (type
), stream
, show
, 0,
377 pascal_type_print_varspec_suffix (TYPE_TARGET_TYPE (type
), stream
, 0,
378 passed_a_ptr
, 0, flags
);
384 pascal_type_print_varspec_suffix (TYPE_TARGET_TYPE (type
),
385 stream
, 0, 1, 0, flags
);
390 fprintf_filtered (stream
, ")");
392 pascal_print_func_args (type
, stream
, flags
);
393 if (TYPE_CODE (TYPE_TARGET_TYPE (type
)) != TYPE_CODE_VOID
)
395 fprintf_filtered (stream
, " : ");
396 pascal_type_print_varspec_prefix (TYPE_TARGET_TYPE (type
),
397 stream
, 0, 0, flags
);
398 pascal_type_print_base (TYPE_TARGET_TYPE (type
), stream
, show
, 0,
400 pascal_type_print_varspec_suffix (TYPE_TARGET_TYPE (type
), stream
, 0,
401 passed_a_ptr
, 0, flags
);
405 case TYPE_CODE_UNDEF
:
406 case TYPE_CODE_STRUCT
:
407 case TYPE_CODE_UNION
:
412 case TYPE_CODE_ERROR
:
416 case TYPE_CODE_RANGE
:
417 case TYPE_CODE_STRING
:
418 case TYPE_CODE_COMPLEX
:
419 case TYPE_CODE_TYPEDEF
:
420 /* These types do not need a suffix. They are listed so that
421 gcc -Wall will report types that may not have been considered. */
424 error (_("type not handled in pascal_type_print_varspec_suffix()"));
429 /* Print the name of the type (or the ultimate pointer target,
430 function value or array element), or the description of a
433 SHOW positive means print details about the type (e.g. enum values),
434 and print structure elements passing SHOW - 1 for show.
435 SHOW negative means just print the type name or struct tag if there is one.
436 If there is no name, print something sensible but concise like
438 SHOW zero means just print the type name or struct tag if there is one.
439 If there is no name, print something sensible but not as concise like
440 "struct {int x; int y;}".
442 LEVEL is the number of spaces to indent by.
443 We increase it for some recursive calls. */
446 pascal_type_print_base (struct type
*type
, struct ui_file
*stream
, int show
,
447 int level
, const struct type_print_options
*flags
)
454 s_none
, s_public
, s_private
, s_protected
462 fputs_filtered ("<type unknown>", stream
);
467 if ((TYPE_CODE (type
) == TYPE_CODE_PTR
)
468 && (TYPE_CODE (TYPE_TARGET_TYPE (type
)) == TYPE_CODE_VOID
))
470 fputs_filtered (TYPE_NAME (type
) ? TYPE_NAME (type
) : "pointer",
474 /* When SHOW is zero or less, and there is a valid type name, then always
475 just print the type name directly from the type. */
478 && TYPE_NAME (type
) != NULL
)
480 fputs_filtered (TYPE_NAME (type
), stream
);
484 CHECK_TYPEDEF (type
);
486 switch (TYPE_CODE (type
))
488 case TYPE_CODE_TYPEDEF
:
491 /* case TYPE_CODE_FUNC:
492 case TYPE_CODE_METHOD: */
493 pascal_type_print_base (TYPE_TARGET_TYPE (type
), stream
, show
, level
,
497 case TYPE_CODE_ARRAY
:
498 /* pascal_type_print_varspec_prefix (TYPE_TARGET_TYPE (type),
500 pascal_type_print_base (TYPE_TARGET_TYPE (type),
501 stream, show, level);
502 pascal_type_print_varspec_suffix (TYPE_TARGET_TYPE (type),
504 pascal_print_type (TYPE_TARGET_TYPE (type
), NULL
, stream
, 0, 0, flags
);
508 case TYPE_CODE_METHOD
:
510 pascal_type_print_base (TYPE_TARGET_TYPE (type), stream, show, level);
511 only after args !! */
513 case TYPE_CODE_STRUCT
:
514 if (TYPE_TAG_NAME (type
) != NULL
)
516 fputs_filtered (TYPE_TAG_NAME (type
), stream
);
517 fputs_filtered (" = ", stream
);
519 if (HAVE_CPLUS_STRUCT (type
))
521 fprintf_filtered (stream
, "class ");
525 fprintf_filtered (stream
, "record ");
529 case TYPE_CODE_UNION
:
530 if (TYPE_TAG_NAME (type
) != NULL
)
532 fputs_filtered (TYPE_TAG_NAME (type
), stream
);
533 fputs_filtered (" = ", stream
);
535 fprintf_filtered (stream
, "case <?> of ");
541 /* If we just printed a tag name, no need to print anything else. */
542 if (TYPE_TAG_NAME (type
) == NULL
)
543 fprintf_filtered (stream
, "{...}");
545 else if (show
> 0 || TYPE_TAG_NAME (type
) == NULL
)
547 pascal_type_print_derivation_info (stream
, type
);
549 fprintf_filtered (stream
, "\n");
550 if ((TYPE_NFIELDS (type
) == 0) && (TYPE_NFN_FIELDS (type
) == 0))
552 if (TYPE_STUB (type
))
553 fprintfi_filtered (level
+ 4, stream
, "<incomplete type>\n");
555 fprintfi_filtered (level
+ 4, stream
, "<no data fields>\n");
558 /* Start off with no specific section type, so we can print
559 one for the first field we find, and use that section type
560 thereafter until we find another type. */
562 section_type
= s_none
;
564 /* If there is a base class for this type,
565 do not print the field that it occupies. */
567 len
= TYPE_NFIELDS (type
);
568 for (i
= TYPE_N_BASECLASSES (type
); i
< len
; i
++)
571 /* Don't print out virtual function table. */
572 if ((strncmp (TYPE_FIELD_NAME (type
, i
), "_vptr", 5) == 0)
573 && is_cplus_marker ((TYPE_FIELD_NAME (type
, i
))[5]))
576 /* If this is a pascal object or class we can print the
577 various section labels. */
579 if (HAVE_CPLUS_STRUCT (type
))
581 if (TYPE_FIELD_PROTECTED (type
, i
))
583 if (section_type
!= s_protected
)
585 section_type
= s_protected
;
586 fprintfi_filtered (level
+ 2, stream
,
590 else if (TYPE_FIELD_PRIVATE (type
, i
))
592 if (section_type
!= s_private
)
594 section_type
= s_private
;
595 fprintfi_filtered (level
+ 2, stream
, "private\n");
600 if (section_type
!= s_public
)
602 section_type
= s_public
;
603 fprintfi_filtered (level
+ 2, stream
, "public\n");
608 print_spaces_filtered (level
+ 4, stream
);
609 if (field_is_static (&TYPE_FIELD (type
, i
)))
610 fprintf_filtered (stream
, "static ");
611 pascal_print_type (TYPE_FIELD_TYPE (type
, i
),
612 TYPE_FIELD_NAME (type
, i
),
613 stream
, show
- 1, level
+ 4, flags
);
614 if (!field_is_static (&TYPE_FIELD (type
, i
))
615 && TYPE_FIELD_PACKED (type
, i
))
617 /* It is a bitfield. This code does not attempt
618 to look at the bitpos and reconstruct filler,
619 unnamed fields. This would lead to misleading
620 results if the compiler does not put out fields
621 for such things (I don't know what it does). */
622 fprintf_filtered (stream
, " : %d",
623 TYPE_FIELD_BITSIZE (type
, i
));
625 fprintf_filtered (stream
, ";\n");
628 /* If there are both fields and methods, put a space between. */
629 len
= TYPE_NFN_FIELDS (type
);
630 if (len
&& section_type
!= s_none
)
631 fprintf_filtered (stream
, "\n");
633 /* Object pascal: print out the methods. */
635 for (i
= 0; i
< len
; i
++)
637 struct fn_field
*f
= TYPE_FN_FIELDLIST1 (type
, i
);
638 int j
, len2
= TYPE_FN_FIELDLIST_LENGTH (type
, i
);
639 const char *method_name
= TYPE_FN_FIELDLIST_NAME (type
, i
);
641 /* this is GNU C++ specific
642 how can we know constructor/destructor?
643 It might work for GNU pascal. */
644 for (j
= 0; j
< len2
; j
++)
646 const char *physname
= TYPE_FN_FIELD_PHYSNAME (f
, j
);
648 int is_constructor
= (strncmp (physname
, "__ct__", 6) == 0);
649 int is_destructor
= (strncmp (physname
, "__dt__", 6) == 0);
652 if (TYPE_FN_FIELD_PROTECTED (f
, j
))
654 if (section_type
!= s_protected
)
656 section_type
= s_protected
;
657 fprintfi_filtered (level
+ 2, stream
,
661 else if (TYPE_FN_FIELD_PRIVATE (f
, j
))
663 if (section_type
!= s_private
)
665 section_type
= s_private
;
666 fprintfi_filtered (level
+ 2, stream
, "private\n");
671 if (section_type
!= s_public
)
673 section_type
= s_public
;
674 fprintfi_filtered (level
+ 2, stream
, "public\n");
678 print_spaces_filtered (level
+ 4, stream
);
679 if (TYPE_FN_FIELD_STATIC_P (f
, j
))
680 fprintf_filtered (stream
, "static ");
681 if (TYPE_TARGET_TYPE (TYPE_FN_FIELD_TYPE (f
, j
)) == 0)
683 /* Keep GDB from crashing here. */
684 fprintf_filtered (stream
, "<undefined type> %s;\n",
685 TYPE_FN_FIELD_PHYSNAME (f
, j
));
691 fprintf_filtered (stream
, "constructor ");
693 else if (is_destructor
)
695 fprintf_filtered (stream
, "destructor ");
697 else if (TYPE_TARGET_TYPE (TYPE_FN_FIELD_TYPE (f
, j
)) != 0
698 && TYPE_CODE (TYPE_TARGET_TYPE (
699 TYPE_FN_FIELD_TYPE (f
, j
))) != TYPE_CODE_VOID
)
701 fprintf_filtered (stream
, "function ");
705 fprintf_filtered (stream
, "procedure ");
707 /* This does not work, no idea why !! */
709 pascal_type_print_method_args (physname
,
713 if (TYPE_TARGET_TYPE (TYPE_FN_FIELD_TYPE (f
, j
)) != 0
714 && TYPE_CODE (TYPE_TARGET_TYPE (
715 TYPE_FN_FIELD_TYPE (f
, j
))) != TYPE_CODE_VOID
)
717 fputs_filtered (" : ", stream
);
718 type_print (TYPE_TARGET_TYPE (TYPE_FN_FIELD_TYPE (f
, j
)),
721 if (TYPE_FN_FIELD_VIRTUAL_P (f
, j
))
722 fprintf_filtered (stream
, "; virtual");
724 fprintf_filtered (stream
, ";\n");
727 fprintfi_filtered (level
, stream
, "end");
732 if (TYPE_TAG_NAME (type
) != NULL
)
734 fputs_filtered (TYPE_TAG_NAME (type
), stream
);
736 fputs_filtered (" ", stream
);
738 /* enum is just defined by
739 type enume_name = (enum_member1,enum_member2,...) */
740 fprintf_filtered (stream
, " = ");
744 /* If we just printed a tag name, no need to print anything else. */
745 if (TYPE_TAG_NAME (type
) == NULL
)
746 fprintf_filtered (stream
, "(...)");
748 else if (show
> 0 || TYPE_TAG_NAME (type
) == NULL
)
750 fprintf_filtered (stream
, "(");
751 len
= TYPE_NFIELDS (type
);
753 for (i
= 0; i
< len
; i
++)
757 fprintf_filtered (stream
, ", ");
759 fputs_filtered (TYPE_FIELD_NAME (type
, i
), stream
);
760 if (lastval
!= TYPE_FIELD_ENUMVAL (type
, i
))
762 fprintf_filtered (stream
,
764 plongest (TYPE_FIELD_ENUMVAL (type
, i
)));
765 lastval
= TYPE_FIELD_ENUMVAL (type
, i
);
769 fprintf_filtered (stream
, ")");
774 fprintf_filtered (stream
, "void");
777 case TYPE_CODE_UNDEF
:
778 fprintf_filtered (stream
, "record <unknown>");
781 case TYPE_CODE_ERROR
:
782 fprintf_filtered (stream
, "%s", TYPE_ERROR_NAME (type
));
785 /* this probably does not work for enums. */
786 case TYPE_CODE_RANGE
:
788 struct type
*target
= TYPE_TARGET_TYPE (type
);
790 print_type_scalar (target
, TYPE_LOW_BOUND (type
), stream
);
791 fputs_filtered ("..", stream
);
792 print_type_scalar (target
, TYPE_HIGH_BOUND (type
), stream
);
797 fputs_filtered ("set of ", stream
);
798 pascal_print_type (TYPE_INDEX_TYPE (type
), "", stream
,
799 show
- 1, level
, flags
);
802 case TYPE_CODE_STRING
:
803 fputs_filtered ("String", stream
);
807 /* Handle types not explicitly handled by the other cases,
808 such as fundamental types. For these, just print whatever
809 the type name is, as recorded in the type itself. If there
810 is no type name, then complain. */
811 if (TYPE_NAME (type
) != NULL
)
813 fputs_filtered (TYPE_NAME (type
), stream
);
817 /* At least for dump_symtab, it is important that this not be
819 fprintf_filtered (stream
, "<invalid unnamed pascal type code %d>",
This page took 0.045319 seconds and 5 git commands to generate.