1 /* Support for printing Pascal types for GDB, the GNU debugger.
2 Copyright (C) 2000-2014 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"
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
)
56 code
= TYPE_CODE (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 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
,
209 const struct type_print_options
*flags
)
214 if (TYPE_NAME (type
) && show
<= 0)
219 switch (TYPE_CODE (type
))
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_CODE (TYPE_TARGET_TYPE (type
)) != TYPE_CODE_VOID
)
233 fprintf_filtered (stream
, "function ");
237 fprintf_filtered (stream
, "procedure ");
242 fprintf_filtered (stream
, " ");
243 pascal_type_print_base (TYPE_DOMAIN_TYPE (type
),
244 stream
, 0, passed_a_ptr
, flags
);
245 fprintf_filtered (stream
, "::");
250 pascal_type_print_varspec_prefix (TYPE_TARGET_TYPE (type
), stream
, 0, 1,
252 fprintf_filtered (stream
, "&");
257 fprintf_filtered (stream
, "(");
259 if (TYPE_CODE (TYPE_TARGET_TYPE (type
)) != TYPE_CODE_VOID
)
261 fprintf_filtered (stream
, "function ");
265 fprintf_filtered (stream
, "procedure ");
270 case TYPE_CODE_ARRAY
:
272 fprintf_filtered (stream
, "(");
273 fprintf_filtered (stream
, "array ");
274 if (TYPE_LENGTH (TYPE_TARGET_TYPE (type
)) > 0
275 && !TYPE_ARRAY_UPPER_BOUND_IS_UNDEFINED (type
))
276 fprintf_filtered (stream
, "[%s..%s] ",
277 plongest (TYPE_ARRAY_LOWER_BOUND_VALUE (type
)),
278 plongest (TYPE_ARRAY_UPPER_BOUND_VALUE (type
)));
279 fprintf_filtered (stream
, "of ");
282 case TYPE_CODE_UNDEF
:
283 case TYPE_CODE_STRUCT
:
284 case TYPE_CODE_UNION
:
289 case TYPE_CODE_ERROR
:
293 case TYPE_CODE_RANGE
:
294 case TYPE_CODE_STRING
:
295 case TYPE_CODE_COMPLEX
:
296 case TYPE_CODE_TYPEDEF
:
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_print_func_args (struct type
*type
, struct ui_file
*stream
,
308 const struct type_print_options
*flags
)
310 int i
, len
= TYPE_NFIELDS (type
);
314 fprintf_filtered (stream
, "(");
316 for (i
= 0; i
< len
; i
++)
320 fputs_filtered (", ", stream
);
323 /* Can we find if it is a var parameter ??
324 if ( TYPE_FIELD(type, i) == )
326 fprintf_filtered (stream, "var ");
328 pascal_print_type (TYPE_FIELD_TYPE (type
, i
), "" /* TYPE_FIELD_NAME
330 ,stream
, -1, 0, flags
);
334 fprintf_filtered (stream
, ")");
338 /* Print any array sizes, function arguments or close parentheses
339 needed after the variable name (to describe its type).
340 Args work like pascal_type_print_varspec_prefix. */
343 pascal_type_print_varspec_suffix (struct type
*type
, struct ui_file
*stream
,
344 int show
, int passed_a_ptr
,
346 const struct type_print_options
*flags
)
351 if (TYPE_NAME (type
) && show
<= 0)
356 switch (TYPE_CODE (type
))
358 case TYPE_CODE_ARRAY
:
360 fprintf_filtered (stream
, ")");
363 case TYPE_CODE_METHOD
:
365 fprintf_filtered (stream
, ")");
366 pascal_type_print_method_args ("",
369 if (TYPE_CODE (TYPE_TARGET_TYPE (type
)) != TYPE_CODE_VOID
)
371 fprintf_filtered (stream
, " : ");
372 pascal_type_print_varspec_prefix (TYPE_TARGET_TYPE (type
),
373 stream
, 0, 0, flags
);
374 pascal_type_print_base (TYPE_TARGET_TYPE (type
), stream
, show
, 0,
376 pascal_type_print_varspec_suffix (TYPE_TARGET_TYPE (type
), stream
, 0,
377 passed_a_ptr
, 0, flags
);
383 pascal_type_print_varspec_suffix (TYPE_TARGET_TYPE (type
),
384 stream
, 0, 1, 0, flags
);
389 fprintf_filtered (stream
, ")");
391 pascal_print_func_args (type
, stream
, flags
);
392 if (TYPE_CODE (TYPE_TARGET_TYPE (type
)) != TYPE_CODE_VOID
)
394 fprintf_filtered (stream
, " : ");
395 pascal_type_print_varspec_prefix (TYPE_TARGET_TYPE (type
),
396 stream
, 0, 0, flags
);
397 pascal_type_print_base (TYPE_TARGET_TYPE (type
), stream
, show
, 0,
399 pascal_type_print_varspec_suffix (TYPE_TARGET_TYPE (type
), stream
, 0,
400 passed_a_ptr
, 0, flags
);
404 case TYPE_CODE_UNDEF
:
405 case TYPE_CODE_STRUCT
:
406 case TYPE_CODE_UNION
:
411 case TYPE_CODE_ERROR
:
415 case TYPE_CODE_RANGE
:
416 case TYPE_CODE_STRING
:
417 case TYPE_CODE_COMPLEX
:
418 case TYPE_CODE_TYPEDEF
:
419 /* These types do not need a suffix. They are listed so that
420 gcc -Wall will report types that may not have been considered. */
423 error (_("type not handled in pascal_type_print_varspec_suffix()"));
428 /* Print the name of the type (or the ultimate pointer target,
429 function value or array element), or the description of a
432 SHOW positive means print details about the type (e.g. enum values),
433 and print structure elements passing SHOW - 1 for show.
434 SHOW negative means just print the type name or struct tag if there is one.
435 If there is no name, print something sensible but concise like
437 SHOW zero means just print the type name or struct tag if there is one.
438 If there is no name, print something sensible but not as concise like
439 "struct {int x; int y;}".
441 LEVEL is the number of spaces to indent by.
442 We increase it for some recursive calls. */
445 pascal_type_print_base (struct type
*type
, struct ui_file
*stream
, int show
,
446 int level
, const struct type_print_options
*flags
)
453 s_none
, s_public
, s_private
, s_protected
461 fputs_filtered ("<type unknown>", stream
);
466 if ((TYPE_CODE (type
) == TYPE_CODE_PTR
)
467 && (TYPE_CODE (TYPE_TARGET_TYPE (type
)) == TYPE_CODE_VOID
))
469 fputs_filtered (TYPE_NAME (type
) ? TYPE_NAME (type
) : "pointer",
473 /* When SHOW is zero or less, and there is a valid type name, then always
474 just print the type name directly from the type. */
477 && TYPE_NAME (type
) != NULL
)
479 fputs_filtered (TYPE_NAME (type
), stream
);
483 CHECK_TYPEDEF (type
);
485 switch (TYPE_CODE (type
))
487 case TYPE_CODE_TYPEDEF
:
490 /* case TYPE_CODE_FUNC:
491 case TYPE_CODE_METHOD: */
492 pascal_type_print_base (TYPE_TARGET_TYPE (type
), stream
, show
, level
,
496 case TYPE_CODE_ARRAY
:
497 /* pascal_type_print_varspec_prefix (TYPE_TARGET_TYPE (type),
499 pascal_type_print_base (TYPE_TARGET_TYPE (type),
500 stream, show, level);
501 pascal_type_print_varspec_suffix (TYPE_TARGET_TYPE (type),
503 pascal_print_type (TYPE_TARGET_TYPE (type
), NULL
, stream
, 0, 0, flags
);
507 case TYPE_CODE_METHOD
:
509 pascal_type_print_base (TYPE_TARGET_TYPE (type), stream, show, level);
510 only after args !! */
512 case TYPE_CODE_STRUCT
:
513 if (TYPE_TAG_NAME (type
) != NULL
)
515 fputs_filtered (TYPE_TAG_NAME (type
), stream
);
516 fputs_filtered (" = ", stream
);
518 if (HAVE_CPLUS_STRUCT (type
))
520 fprintf_filtered (stream
, "class ");
524 fprintf_filtered (stream
, "record ");
528 case TYPE_CODE_UNION
:
529 if (TYPE_TAG_NAME (type
) != NULL
)
531 fputs_filtered (TYPE_TAG_NAME (type
), stream
);
532 fputs_filtered (" = ", stream
);
534 fprintf_filtered (stream
, "case <?> of ");
540 /* If we just printed a tag name, no need to print anything else. */
541 if (TYPE_TAG_NAME (type
) == NULL
)
542 fprintf_filtered (stream
, "{...}");
544 else if (show
> 0 || TYPE_TAG_NAME (type
) == NULL
)
546 pascal_type_print_derivation_info (stream
, type
);
548 fprintf_filtered (stream
, "\n");
549 if ((TYPE_NFIELDS (type
) == 0) && (TYPE_NFN_FIELDS (type
) == 0))
551 if (TYPE_STUB (type
))
552 fprintfi_filtered (level
+ 4, stream
, "<incomplete type>\n");
554 fprintfi_filtered (level
+ 4, stream
, "<no data fields>\n");
557 /* Start off with no specific section type, so we can print
558 one for the first field we find, and use that section type
559 thereafter until we find another type. */
561 section_type
= s_none
;
563 /* If there is a base class for this type,
564 do not print the field that it occupies. */
566 len
= TYPE_NFIELDS (type
);
567 for (i
= TYPE_N_BASECLASSES (type
); i
< len
; i
++)
570 /* Don't print out virtual function table. */
571 if ((strncmp (TYPE_FIELD_NAME (type
, i
), "_vptr", 5) == 0)
572 && is_cplus_marker ((TYPE_FIELD_NAME (type
, i
))[5]))
575 /* If this is a pascal object or class we can print the
576 various section labels. */
578 if (HAVE_CPLUS_STRUCT (type
))
580 if (TYPE_FIELD_PROTECTED (type
, i
))
582 if (section_type
!= s_protected
)
584 section_type
= s_protected
;
585 fprintfi_filtered (level
+ 2, stream
,
589 else if (TYPE_FIELD_PRIVATE (type
, i
))
591 if (section_type
!= s_private
)
593 section_type
= s_private
;
594 fprintfi_filtered (level
+ 2, stream
, "private\n");
599 if (section_type
!= s_public
)
601 section_type
= s_public
;
602 fprintfi_filtered (level
+ 2, stream
, "public\n");
607 print_spaces_filtered (level
+ 4, stream
);
608 if (field_is_static (&TYPE_FIELD (type
, i
)))
609 fprintf_filtered (stream
, "static ");
610 pascal_print_type (TYPE_FIELD_TYPE (type
, i
),
611 TYPE_FIELD_NAME (type
, i
),
612 stream
, show
- 1, level
+ 4, flags
);
613 if (!field_is_static (&TYPE_FIELD (type
, i
))
614 && TYPE_FIELD_PACKED (type
, i
))
616 /* It is a bitfield. This code does not attempt
617 to look at the bitpos and reconstruct filler,
618 unnamed fields. This would lead to misleading
619 results if the compiler does not put out fields
620 for such things (I don't know what it does). */
621 fprintf_filtered (stream
, " : %d",
622 TYPE_FIELD_BITSIZE (type
, i
));
624 fprintf_filtered (stream
, ";\n");
627 /* If there are both fields and methods, put a space between. */
628 len
= TYPE_NFN_FIELDS (type
);
629 if (len
&& section_type
!= s_none
)
630 fprintf_filtered (stream
, "\n");
632 /* Object pascal: print out the methods. */
634 for (i
= 0; i
< len
; i
++)
636 struct fn_field
*f
= TYPE_FN_FIELDLIST1 (type
, i
);
637 int j
, len2
= TYPE_FN_FIELDLIST_LENGTH (type
, i
);
638 const char *method_name
= TYPE_FN_FIELDLIST_NAME (type
, i
);
640 /* this is GNU C++ specific
641 how can we know constructor/destructor?
642 It might work for GNU pascal. */
643 for (j
= 0; j
< len2
; j
++)
645 const char *physname
= TYPE_FN_FIELD_PHYSNAME (f
, j
);
647 int is_constructor
= (strncmp (physname
, "__ct__", 6) == 0);
648 int is_destructor
= (strncmp (physname
, "__dt__", 6) == 0);
651 if (TYPE_FN_FIELD_PROTECTED (f
, j
))
653 if (section_type
!= s_protected
)
655 section_type
= s_protected
;
656 fprintfi_filtered (level
+ 2, stream
,
660 else if (TYPE_FN_FIELD_PRIVATE (f
, j
))
662 if (section_type
!= s_private
)
664 section_type
= s_private
;
665 fprintfi_filtered (level
+ 2, stream
, "private\n");
670 if (section_type
!= s_public
)
672 section_type
= s_public
;
673 fprintfi_filtered (level
+ 2, stream
, "public\n");
677 print_spaces_filtered (level
+ 4, stream
);
678 if (TYPE_FN_FIELD_STATIC_P (f
, j
))
679 fprintf_filtered (stream
, "static ");
680 if (TYPE_TARGET_TYPE (TYPE_FN_FIELD_TYPE (f
, j
)) == 0)
682 /* Keep GDB from crashing here. */
683 fprintf_filtered (stream
, "<undefined type> %s;\n",
684 TYPE_FN_FIELD_PHYSNAME (f
, j
));
690 fprintf_filtered (stream
, "constructor ");
692 else if (is_destructor
)
694 fprintf_filtered (stream
, "destructor ");
696 else if (TYPE_TARGET_TYPE (TYPE_FN_FIELD_TYPE (f
, j
)) != 0
697 && TYPE_CODE (TYPE_TARGET_TYPE (
698 TYPE_FN_FIELD_TYPE (f
, j
))) != TYPE_CODE_VOID
)
700 fprintf_filtered (stream
, "function ");
704 fprintf_filtered (stream
, "procedure ");
706 /* This does not work, no idea why !! */
708 pascal_type_print_method_args (physname
,
712 if (TYPE_TARGET_TYPE (TYPE_FN_FIELD_TYPE (f
, j
)) != 0
713 && TYPE_CODE (TYPE_TARGET_TYPE (
714 TYPE_FN_FIELD_TYPE (f
, j
))) != TYPE_CODE_VOID
)
716 fputs_filtered (" : ", stream
);
717 type_print (TYPE_TARGET_TYPE (TYPE_FN_FIELD_TYPE (f
, j
)),
720 if (TYPE_FN_FIELD_VIRTUAL_P (f
, j
))
721 fprintf_filtered (stream
, "; virtual");
723 fprintf_filtered (stream
, ";\n");
726 fprintfi_filtered (level
, stream
, "end");
731 if (TYPE_TAG_NAME (type
) != NULL
)
733 fputs_filtered (TYPE_TAG_NAME (type
), stream
);
735 fputs_filtered (" ", stream
);
737 /* enum is just defined by
738 type enume_name = (enum_member1,enum_member2,...) */
739 fprintf_filtered (stream
, " = ");
743 /* If we just printed a tag name, no need to print anything else. */
744 if (TYPE_TAG_NAME (type
) == NULL
)
745 fprintf_filtered (stream
, "(...)");
747 else if (show
> 0 || TYPE_TAG_NAME (type
) == NULL
)
749 fprintf_filtered (stream
, "(");
750 len
= TYPE_NFIELDS (type
);
752 for (i
= 0; i
< len
; i
++)
756 fprintf_filtered (stream
, ", ");
758 fputs_filtered (TYPE_FIELD_NAME (type
, i
), stream
);
759 if (lastval
!= TYPE_FIELD_ENUMVAL (type
, i
))
761 fprintf_filtered (stream
,
763 plongest (TYPE_FIELD_ENUMVAL (type
, i
)));
764 lastval
= TYPE_FIELD_ENUMVAL (type
, i
);
768 fprintf_filtered (stream
, ")");
773 fprintf_filtered (stream
, "void");
776 case TYPE_CODE_UNDEF
:
777 fprintf_filtered (stream
, "record <unknown>");
780 case TYPE_CODE_ERROR
:
781 fprintf_filtered (stream
, "%s", TYPE_ERROR_NAME (type
));
784 /* this probably does not work for enums. */
785 case TYPE_CODE_RANGE
:
787 struct type
*target
= TYPE_TARGET_TYPE (type
);
789 print_type_scalar (target
, TYPE_LOW_BOUND (type
), stream
);
790 fputs_filtered ("..", stream
);
791 print_type_scalar (target
, TYPE_HIGH_BOUND (type
), stream
);
796 fputs_filtered ("set of ", stream
);
797 pascal_print_type (TYPE_INDEX_TYPE (type
), "", stream
,
798 show
- 1, level
, flags
);
801 case TYPE_CODE_STRING
:
802 fputs_filtered ("String", stream
);
806 /* Handle types not explicitly handled by the other cases,
807 such as fundamental types. For these, just print whatever
808 the type name is, as recorded in the type itself. If there
809 is no type name, then complain. */
810 if (TYPE_NAME (type
) != NULL
)
812 fputs_filtered (TYPE_NAME (type
), stream
);
816 /* At least for dump_symtab, it is important that this not be
818 fprintf_filtered (stream
, "<invalid unnamed pascal type code %d>",
This page took 0.047073 seconds and 5 git commands to generate.