1 /* Support for printing Pascal types for GDB, the GNU debugger.
2 Copyright (C) 2000, 2001, 2002, 2006, 2007 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"
34 #include "gdb_string.h"
38 static void pascal_type_print_varspec_suffix (struct type *, struct ui_file *, int, int, int);
40 static void pascal_type_print_derivation_info (struct ui_file *, struct type *);
42 void pascal_type_print_varspec_prefix (struct type *, struct ui_file *, int, int);
45 /* LEVEL is the depth to indent lines by. */
48 pascal_print_type (struct type *type, char *varstring, struct ui_file *stream,
54 code = TYPE_CODE (type);
59 if ((code == TYPE_CODE_FUNC
60 || code == TYPE_CODE_METHOD))
62 pascal_type_print_varspec_prefix (type, stream, show, 0);
65 fputs_filtered (varstring, stream);
67 if ((varstring != NULL && *varstring != '\0')
68 && !(code == TYPE_CODE_FUNC
69 || code == TYPE_CODE_METHOD))
71 fputs_filtered (" : ", stream);
74 if (!(code == TYPE_CODE_FUNC
75 || code == TYPE_CODE_METHOD))
77 pascal_type_print_varspec_prefix (type, stream, show, 0);
80 pascal_type_print_base (type, stream, show, level);
81 /* For demangled function names, we have the arglist as part of the name,
82 so don't print an additional pair of ()'s */
84 demangled_args = varstring ? strchr (varstring, '(') != NULL : 0;
85 pascal_type_print_varspec_suffix (type, stream, show, 0, demangled_args);
89 /* If TYPE is a derived type, then print out derivation information.
90 Print only the actual base classes of this type, not the base classes
91 of the base classes. I.E. for the derivation hierarchy:
94 class B : public A {int b; };
95 class C : public B {int c; };
97 Print the type of class C as:
103 Not as the following (like gdb used to), which is not legal C++ syntax for
104 derived types and may be confused with the multiple inheritance form:
106 class C : public B : public A {
110 In general, gdb should try to print the types as closely as possible to
111 the form that they appear in the source code. */
114 pascal_type_print_derivation_info (struct ui_file *stream, struct type *type)
119 for (i = 0; i < TYPE_N_BASECLASSES (type); i++)
121 fputs_filtered (i == 0 ? ": " : ", ", stream);
122 fprintf_filtered (stream, "%s%s ",
123 BASETYPE_VIA_PUBLIC (type, i) ? "public" : "private",
124 BASETYPE_VIA_VIRTUAL (type, i) ? " virtual" : "");
125 name = type_name_no_tag (TYPE_BASECLASS (type, i));
126 fprintf_filtered (stream, "%s", name ? name : "(null)");
130 fputs_filtered (" ", stream);
134 /* Print the Pascal method arguments ARGS to the file STREAM. */
137 pascal_type_print_method_args (char *physname, char *methodname,
138 struct ui_file *stream)
140 int is_constructor = (strncmp (physname, "__ct__", 6) == 0);
141 int is_destructor = (strncmp (physname, "__dt__", 6) == 0);
143 if (is_constructor || is_destructor)
148 fputs_filtered (methodname, stream);
150 if (physname && (*physname != 0))
156 fputs_filtered (" (", stream);
157 /* we must demangle this */
158 while (isdigit (physname[0]))
160 while (isdigit (physname[len]))
164 i = strtol (physname, &argname, 0);
166 storec = physname[i];
168 fputs_filtered (physname, stream);
169 physname[i] = storec;
171 if (physname[0] != 0)
173 fputs_filtered (", ", stream);
176 fputs_filtered (")", stream);
180 /* Print any asterisks or open-parentheses needed before the
181 variable name (to describe its type).
183 On outermost call, pass 0 for PASSED_A_PTR.
184 On outermost call, SHOW > 0 means should ignore
185 any typename for TYPE and show its details.
186 SHOW is always zero on recursive calls. */
189 pascal_type_print_varspec_prefix (struct type *type, struct ui_file *stream,
190 int show, int passed_a_ptr)
196 if (TYPE_NAME (type) && show <= 0)
201 switch (TYPE_CODE (type))
204 fprintf_filtered (stream, "^");
205 pascal_type_print_varspec_prefix (TYPE_TARGET_TYPE (type), stream, 0, 1);
206 break; /* pointer should be handled normally in pascal */
208 case TYPE_CODE_METHOD:
210 fprintf_filtered (stream, "(");
211 if (TYPE_CODE (TYPE_TARGET_TYPE (type)) != TYPE_CODE_VOID)
213 fprintf_filtered (stream, "function ");
217 fprintf_filtered (stream, "procedure ");
222 fprintf_filtered (stream, " ");
223 pascal_type_print_base (TYPE_DOMAIN_TYPE (type), stream, 0, passed_a_ptr);
224 fprintf_filtered (stream, "::");
229 pascal_type_print_varspec_prefix (TYPE_TARGET_TYPE (type), stream, 0, 1);
230 fprintf_filtered (stream, "&");
235 fprintf_filtered (stream, "(");
237 if (TYPE_CODE (TYPE_TARGET_TYPE (type)) != TYPE_CODE_VOID)
239 fprintf_filtered (stream, "function ");
243 fprintf_filtered (stream, "procedure ");
248 case TYPE_CODE_ARRAY:
250 fprintf_filtered (stream, "(");
251 fprintf_filtered (stream, "array ");
252 if (TYPE_LENGTH (TYPE_TARGET_TYPE (type)) > 0
253 && TYPE_ARRAY_UPPER_BOUND_TYPE (type) != BOUND_CANNOT_BE_DETERMINED)
254 fprintf_filtered (stream, "[%d..%d] ",
255 TYPE_ARRAY_LOWER_BOUND_VALUE (type),
256 TYPE_ARRAY_UPPER_BOUND_VALUE (type)
258 fprintf_filtered (stream, "of ");
261 case TYPE_CODE_UNDEF:
262 case TYPE_CODE_STRUCT:
263 case TYPE_CODE_UNION:
268 case TYPE_CODE_ERROR:
272 case TYPE_CODE_RANGE:
273 case TYPE_CODE_STRING:
274 case TYPE_CODE_BITSTRING:
275 case TYPE_CODE_COMPLEX:
276 case TYPE_CODE_TYPEDEF:
277 case TYPE_CODE_TEMPLATE:
278 /* These types need no prefix. They are listed here so that
279 gcc -Wall will reveal any types that haven't been handled. */
282 error (_("type not handled in pascal_type_print_varspec_prefix()"));
288 pascal_print_func_args (struct type *type, struct ui_file *stream)
290 int i, len = TYPE_NFIELDS (type);
293 fprintf_filtered (stream, "(");
295 for (i = 0; i < len; i++)
299 fputs_filtered (", ", stream);
302 /* can we find if it is a var parameter ??
303 if ( TYPE_FIELD(type, i) == )
305 fprintf_filtered (stream, "var ");
307 pascal_print_type (TYPE_FIELD_TYPE (type, i), "" /* TYPE_FIELD_NAME seems invalid ! */
312 fprintf_filtered (stream, ")");
316 /* Print any array sizes, function arguments or close parentheses
317 needed after the variable name (to describe its type).
318 Args work like pascal_type_print_varspec_prefix. */
321 pascal_type_print_varspec_suffix (struct type *type, struct ui_file *stream,
322 int show, int passed_a_ptr,
328 if (TYPE_NAME (type) && show <= 0)
333 switch (TYPE_CODE (type))
335 case TYPE_CODE_ARRAY:
337 fprintf_filtered (stream, ")");
340 case TYPE_CODE_METHOD:
342 fprintf_filtered (stream, ")");
343 pascal_type_print_method_args ("",
346 if (TYPE_CODE (TYPE_TARGET_TYPE (type)) != TYPE_CODE_VOID)
348 fprintf_filtered (stream, " : ");
349 pascal_type_print_varspec_prefix (TYPE_TARGET_TYPE (type), stream, 0, 0);
350 pascal_type_print_base (TYPE_TARGET_TYPE (type), stream, show, 0);
351 pascal_type_print_varspec_suffix (TYPE_TARGET_TYPE (type), stream, 0,
358 pascal_type_print_varspec_suffix (TYPE_TARGET_TYPE (type), stream, 0, 1, 0);
363 fprintf_filtered (stream, ")");
365 pascal_print_func_args (type, stream);
366 if (TYPE_CODE (TYPE_TARGET_TYPE (type)) != TYPE_CODE_VOID)
368 fprintf_filtered (stream, " : ");
369 pascal_type_print_varspec_prefix (TYPE_TARGET_TYPE (type), stream, 0, 0);
370 pascal_type_print_base (TYPE_TARGET_TYPE (type), stream, show, 0);
371 pascal_type_print_varspec_suffix (TYPE_TARGET_TYPE (type), stream, 0,
376 case TYPE_CODE_UNDEF:
377 case TYPE_CODE_STRUCT:
378 case TYPE_CODE_UNION:
383 case TYPE_CODE_ERROR:
387 case TYPE_CODE_RANGE:
388 case TYPE_CODE_STRING:
389 case TYPE_CODE_BITSTRING:
390 case TYPE_CODE_COMPLEX:
391 case TYPE_CODE_TYPEDEF:
392 case TYPE_CODE_TEMPLATE:
393 /* These types do not need a suffix. They are listed so that
394 gcc -Wall will report types that may not have been considered. */
397 error (_("type not handled in pascal_type_print_varspec_suffix()"));
402 /* Print the name of the type (or the ultimate pointer target,
403 function value or array element), or the description of a
406 SHOW positive means print details about the type (e.g. enum values),
407 and print structure elements passing SHOW - 1 for show.
408 SHOW negative means just print the type name or struct tag if there is one.
409 If there is no name, print something sensible but concise like
411 SHOW zero means just print the type name or struct tag if there is one.
412 If there is no name, print something sensible but not as concise like
413 "struct {int x; int y;}".
415 LEVEL is the number of spaces to indent by.
416 We increase it for some recursive calls. */
419 pascal_type_print_base (struct type *type, struct ui_file *stream, int show,
427 s_none, s_public, s_private, s_protected
435 fputs_filtered ("<type unknown>", stream);
440 if ((TYPE_CODE (type) == TYPE_CODE_PTR) && (TYPE_CODE (TYPE_TARGET_TYPE (type)) == TYPE_CODE_VOID))
442 fputs_filtered (TYPE_NAME (type) ? TYPE_NAME (type) : "pointer",
446 /* When SHOW is zero or less, and there is a valid type name, then always
447 just print the type name directly from the type. */
450 && TYPE_NAME (type) != NULL)
452 fputs_filtered (TYPE_NAME (type), stream);
456 CHECK_TYPEDEF (type);
458 switch (TYPE_CODE (type))
460 case TYPE_CODE_TYPEDEF:
463 /* case TYPE_CODE_FUNC:
464 case TYPE_CODE_METHOD: */
465 pascal_type_print_base (TYPE_TARGET_TYPE (type), stream, show, level);
468 case TYPE_CODE_ARRAY:
469 /* pascal_type_print_varspec_prefix (TYPE_TARGET_TYPE (type), stream, 0, 0);
470 pascal_type_print_base (TYPE_TARGET_TYPE (type), stream, show, level);
471 pascal_type_print_varspec_suffix (TYPE_TARGET_TYPE (type), stream, 0, 0, 0); */
472 pascal_print_type (TYPE_TARGET_TYPE (type), NULL, stream, 0, 0);
476 case TYPE_CODE_METHOD:
478 pascal_type_print_base (TYPE_TARGET_TYPE (type), stream, show, level);
479 only after args !! */
481 case TYPE_CODE_STRUCT:
482 if (TYPE_TAG_NAME (type) != NULL)
484 fputs_filtered (TYPE_TAG_NAME (type), stream);
485 fputs_filtered (" = ", stream);
487 if (HAVE_CPLUS_STRUCT (type))
489 fprintf_filtered (stream, "class ");
493 fprintf_filtered (stream, "record ");
497 case TYPE_CODE_UNION:
498 if (TYPE_TAG_NAME (type) != NULL)
500 fputs_filtered (TYPE_TAG_NAME (type), stream);
501 fputs_filtered (" = ", stream);
503 fprintf_filtered (stream, "case <?> of ");
509 /* If we just printed a tag name, no need to print anything else. */
510 if (TYPE_TAG_NAME (type) == NULL)
511 fprintf_filtered (stream, "{...}");
513 else if (show > 0 || TYPE_TAG_NAME (type) == NULL)
515 pascal_type_print_derivation_info (stream, type);
517 fprintf_filtered (stream, "\n");
518 if ((TYPE_NFIELDS (type) == 0) && (TYPE_NFN_FIELDS (type) == 0))
520 if (TYPE_STUB (type))
521 fprintfi_filtered (level + 4, stream, "<incomplete type>\n");
523 fprintfi_filtered (level + 4, stream, "<no data fields>\n");
526 /* Start off with no specific section type, so we can print
527 one for the first field we find, and use that section type
528 thereafter until we find another type. */
530 section_type = s_none;
532 /* If there is a base class for this type,
533 do not print the field that it occupies. */
535 len = TYPE_NFIELDS (type);
536 for (i = TYPE_N_BASECLASSES (type); i < len; i++)
539 /* Don't print out virtual function table. */
540 if ((strncmp (TYPE_FIELD_NAME (type, i), "_vptr", 5) == 0)
541 && is_cplus_marker ((TYPE_FIELD_NAME (type, i))[5]))
544 /* If this is a pascal object or class we can print the
545 various section labels. */
547 if (HAVE_CPLUS_STRUCT (type))
549 if (TYPE_FIELD_PROTECTED (type, i))
551 if (section_type != s_protected)
553 section_type = s_protected;
554 fprintfi_filtered (level + 2, stream,
558 else if (TYPE_FIELD_PRIVATE (type, i))
560 if (section_type != s_private)
562 section_type = s_private;
563 fprintfi_filtered (level + 2, stream, "private\n");
568 if (section_type != s_public)
570 section_type = s_public;
571 fprintfi_filtered (level + 2, stream, "public\n");
576 print_spaces_filtered (level + 4, stream);
577 if (TYPE_FIELD_STATIC (type, i))
579 fprintf_filtered (stream, "static ");
581 pascal_print_type (TYPE_FIELD_TYPE (type, i),
582 TYPE_FIELD_NAME (type, i),
583 stream, show - 1, level + 4);
584 if (!TYPE_FIELD_STATIC (type, i)
585 && TYPE_FIELD_PACKED (type, i))
587 /* It is a bitfield. This code does not attempt
588 to look at the bitpos and reconstruct filler,
589 unnamed fields. This would lead to misleading
590 results if the compiler does not put out fields
591 for such things (I don't know what it does). */
592 fprintf_filtered (stream, " : %d",
593 TYPE_FIELD_BITSIZE (type, i));
595 fprintf_filtered (stream, ";\n");
598 /* If there are both fields and methods, put a space between. */
599 len = TYPE_NFN_FIELDS (type);
600 if (len && section_type != s_none)
601 fprintf_filtered (stream, "\n");
603 /* Pbject pascal: print out the methods */
605 for (i = 0; i < len; i++)
607 struct fn_field *f = TYPE_FN_FIELDLIST1 (type, i);
608 int j, len2 = TYPE_FN_FIELDLIST_LENGTH (type, i);
609 char *method_name = TYPE_FN_FIELDLIST_NAME (type, i);
610 char *name = type_name_no_tag (type);
611 /* this is GNU C++ specific
612 how can we know constructor/destructor?
613 It might work for GNU pascal */
614 for (j = 0; j < len2; j++)
616 char *physname = TYPE_FN_FIELD_PHYSNAME (f, j);
618 int is_constructor = (strncmp (physname, "__ct__", 6) == 0);
619 int is_destructor = (strncmp (physname, "__dt__", 6) == 0);
622 if (TYPE_FN_FIELD_PROTECTED (f, j))
624 if (section_type != s_protected)
626 section_type = s_protected;
627 fprintfi_filtered (level + 2, stream,
631 else if (TYPE_FN_FIELD_PRIVATE (f, j))
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");
648 print_spaces_filtered (level + 4, stream);
649 if (TYPE_FN_FIELD_STATIC_P (f, j))
650 fprintf_filtered (stream, "static ");
651 if (TYPE_TARGET_TYPE (TYPE_FN_FIELD_TYPE (f, j)) == 0)
653 /* Keep GDB from crashing here. */
654 fprintf_filtered (stream, "<undefined type> %s;\n",
655 TYPE_FN_FIELD_PHYSNAME (f, j));
661 fprintf_filtered (stream, "constructor ");
663 else if (is_destructor)
665 fprintf_filtered (stream, "destructor ");
667 else if (TYPE_TARGET_TYPE (TYPE_FN_FIELD_TYPE (f, j)) != 0
668 && TYPE_CODE (TYPE_TARGET_TYPE (
669 TYPE_FN_FIELD_TYPE (f, j))) != TYPE_CODE_VOID)
671 fprintf_filtered (stream, "function ");
675 fprintf_filtered (stream, "procedure ");
677 /* this does not work, no idea why !! */
679 pascal_type_print_method_args (physname,
683 if (TYPE_TARGET_TYPE (TYPE_FN_FIELD_TYPE (f, j)) != 0
684 && TYPE_CODE (TYPE_TARGET_TYPE (
685 TYPE_FN_FIELD_TYPE (f, j))) != TYPE_CODE_VOID)
687 fputs_filtered (" : ", stream);
688 type_print (TYPE_TARGET_TYPE (TYPE_FN_FIELD_TYPE (f, j)),
691 if (TYPE_FN_FIELD_VIRTUAL_P (f, j))
692 fprintf_filtered (stream, "; virtual");
694 fprintf_filtered (stream, ";\n");
697 fprintfi_filtered (level, stream, "end");
702 if (TYPE_TAG_NAME (type) != NULL)
704 fputs_filtered (TYPE_TAG_NAME (type), stream);
706 fputs_filtered (" ", stream);
708 /* enum is just defined by
709 type enume_name = (enum_member1,enum_member2,...) */
710 fprintf_filtered (stream, " = ");
714 /* If we just printed a tag name, no need to print anything else. */
715 if (TYPE_TAG_NAME (type) == NULL)
716 fprintf_filtered (stream, "(...)");
718 else if (show > 0 || TYPE_TAG_NAME (type) == NULL)
720 fprintf_filtered (stream, "(");
721 len = TYPE_NFIELDS (type);
723 for (i = 0; i < len; i++)
727 fprintf_filtered (stream, ", ");
729 fputs_filtered (TYPE_FIELD_NAME (type, i), stream);
730 if (lastval != TYPE_FIELD_BITPOS (type, i))
732 fprintf_filtered (stream, " := %d", TYPE_FIELD_BITPOS (type, i));
733 lastval = TYPE_FIELD_BITPOS (type, i);
737 fprintf_filtered (stream, ")");
742 fprintf_filtered (stream, "void");
745 case TYPE_CODE_UNDEF:
746 fprintf_filtered (stream, "record <unknown>");
749 case TYPE_CODE_ERROR:
750 fprintf_filtered (stream, "<unknown type>");
753 /* this probably does not work for enums */
754 case TYPE_CODE_RANGE:
756 struct type *target = TYPE_TARGET_TYPE (type);
758 target = builtin_type_long;
759 print_type_scalar (target, TYPE_LOW_BOUND (type), stream);
760 fputs_filtered ("..", stream);
761 print_type_scalar (target, TYPE_HIGH_BOUND (type), stream);
766 fputs_filtered ("set of ", stream);
767 pascal_print_type (TYPE_INDEX_TYPE (type), "", stream,
771 case TYPE_CODE_BITSTRING:
772 fputs_filtered ("BitString", stream);
775 case TYPE_CODE_STRING:
776 fputs_filtered ("String", stream);
780 /* Handle types not explicitly handled by the other cases,
781 such as fundamental types. For these, just print whatever
782 the type name is, as recorded in the type itself. If there
783 is no type name, then complain. */
784 if (TYPE_NAME (type) != NULL)
786 fputs_filtered (TYPE_NAME (type), stream);
790 /* At least for dump_symtab, it is important that this not be
792 fprintf_filtered (stream, "<invalid unnamed pascal type code %d>",