1 /* Support for printing Pascal types for GDB, the GNU debugger.
2 Copyright (C) 2000-2023 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 "gdbsupport/gdb_obstack.h"
26 #include "expression.h"
32 #include "typeprint.h"
33 #include "gdb-demangle.h"
35 #include "cli/cli-style.h"
40 pascal_language::print_type (struct type
*type
, const char *varstring
,
41 struct ui_file
*stream
, int show
, int level
,
42 const struct type_print_options
*flags
) const
50 type
= check_typedef (type
);
52 if ((code
== TYPE_CODE_FUNC
53 || code
== TYPE_CODE_METHOD
))
55 type_print_varspec_prefix (type
, stream
, show
, 0, flags
);
58 gdb_puts (varstring
, stream
);
60 if ((varstring
!= NULL
&& *varstring
!= '\0')
61 && !(code
== TYPE_CODE_FUNC
62 || code
== TYPE_CODE_METHOD
))
64 gdb_puts (" : ", stream
);
67 if (!(code
== TYPE_CODE_FUNC
68 || code
== TYPE_CODE_METHOD
))
70 type_print_varspec_prefix (type
, stream
, show
, 0, flags
);
73 type_print_base (type
, stream
, show
, level
, flags
);
74 /* For demangled function names, we have the arglist as part of the name,
75 so don't print an additional pair of ()'s. */
77 demangled_args
= varstring
? strchr (varstring
, '(') != NULL
: 0;
78 type_print_varspec_suffix (type
, stream
, show
, 0, demangled_args
,
86 pascal_language::print_typedef (struct type
*type
, struct symbol
*new_symbol
,
87 struct ui_file
*stream
) const
89 type
= check_typedef (type
);
90 gdb_printf (stream
, "type ");
91 gdb_printf (stream
, "%s = ", new_symbol
->print_name ());
92 type_print (type
, "", stream
, 0);
93 gdb_printf (stream
, ";");
99 pascal_language::type_print_derivation_info (struct ui_file
*stream
,
100 struct type
*type
) const
105 for (i
= 0; i
< TYPE_N_BASECLASSES (type
); i
++)
107 gdb_puts (i
== 0 ? ": " : ", ", stream
);
108 gdb_printf (stream
, "%s%s ",
109 BASETYPE_VIA_PUBLIC (type
, i
) ? "public" : "private",
110 BASETYPE_VIA_VIRTUAL (type
, i
) ? " virtual" : "");
111 name
= TYPE_BASECLASS (type
, i
)->name ();
112 gdb_printf (stream
, "%s", name
? name
: "(null)");
116 gdb_puts (" ", stream
);
123 pascal_language::type_print_method_args (const char *physname
,
124 const char *methodname
,
125 struct ui_file
*stream
) const
127 int is_constructor
= (startswith (physname
, "__ct__"));
128 int is_destructor
= (startswith (physname
, "__dt__"));
130 if (is_constructor
|| is_destructor
)
135 gdb_puts (methodname
, stream
);
137 if (physname
&& (*physname
!= 0))
139 gdb_puts (" (", stream
);
140 /* We must demangle this. */
141 while (isdigit (physname
[0]))
147 while (isdigit (physname
[len
]))
151 i
= strtol (physname
, &argname
, 0);
154 for (j
= 0; j
< i
; ++j
)
155 gdb_putc (physname
[j
], stream
);
158 if (physname
[0] != 0)
160 gdb_puts (", ", stream
);
163 gdb_puts (")", stream
);
170 pascal_language::type_print_varspec_prefix (struct type
*type
,
171 struct ui_file
*stream
,
172 int show
, int passed_a_ptr
,
173 const struct type_print_options
*flags
) const
178 if (type
->name () && show
<= 0)
183 switch (type
->code ())
186 gdb_printf (stream
, "^");
187 type_print_varspec_prefix (type
->target_type (), stream
, 0, 1,
189 break; /* Pointer should be handled normally
192 case TYPE_CODE_METHOD
:
194 gdb_printf (stream
, "(");
195 if (type
->target_type () != NULL
196 && type
->target_type ()->code () != TYPE_CODE_VOID
)
198 gdb_printf (stream
, "function ");
202 gdb_printf (stream
, "procedure ");
207 gdb_printf (stream
, " ");
208 type_print_base (TYPE_SELF_TYPE (type
),
209 stream
, 0, passed_a_ptr
, flags
);
210 gdb_printf (stream
, "::");
215 type_print_varspec_prefix (type
->target_type (), stream
, 0, 1,
217 gdb_printf (stream
, "&");
222 gdb_printf (stream
, "(");
224 if (type
->target_type () != NULL
225 && type
->target_type ()->code () != TYPE_CODE_VOID
)
227 gdb_printf (stream
, "function ");
231 gdb_printf (stream
, "procedure ");
236 case TYPE_CODE_ARRAY
:
238 gdb_printf (stream
, "(");
239 gdb_printf (stream
, "array ");
240 if (type
->target_type ()->length () > 0
241 && type
->bounds ()->high
.is_constant ())
242 gdb_printf (stream
, "[%s..%s] ",
243 plongest (type
->bounds ()->low
.const_val ()),
244 plongest (type
->bounds ()->high
.const_val ()));
245 gdb_printf (stream
, "of ");
253 pascal_language::print_func_args (struct type
*type
, struct ui_file
*stream
,
254 const struct type_print_options
*flags
) const
256 int i
, len
= type
->num_fields ();
260 gdb_printf (stream
, "(");
262 for (i
= 0; i
< len
; i
++)
266 gdb_puts (", ", stream
);
267 stream
->wrap_here (4);
269 /* Can we find if it is a var parameter ??
270 if ( TYPE_FIELD(type, i) == )
272 gdb_printf (stream, "var ");
274 print_type (type
->field (i
).type (), "" /* TYPE_FIELD_NAME
276 ,stream
, -1, 0, flags
);
280 gdb_printf (stream
, ")");
287 pascal_language::type_print_func_varspec_suffix (struct type
*type
,
288 struct ui_file
*stream
,
289 int show
, int passed_a_ptr
,
291 const struct type_print_options
*flags
) const
293 if (type
->target_type () == NULL
294 || type
->target_type ()->code () != TYPE_CODE_VOID
)
296 gdb_printf (stream
, " : ");
297 type_print_varspec_prefix (type
->target_type (),
298 stream
, 0, 0, flags
);
300 if (type
->target_type () == NULL
)
301 type_print_unknown_return_type (stream
);
303 type_print_base (type
->target_type (), stream
, show
, 0,
306 type_print_varspec_suffix (type
->target_type (), stream
, 0,
307 passed_a_ptr
, 0, flags
);
314 pascal_language::type_print_varspec_suffix (struct type
*type
,
315 struct ui_file
*stream
,
316 int show
, int passed_a_ptr
,
318 const struct type_print_options
*flags
) const
323 if (type
->name () && show
<= 0)
328 switch (type
->code ())
330 case TYPE_CODE_ARRAY
:
332 gdb_printf (stream
, ")");
335 case TYPE_CODE_METHOD
:
337 gdb_printf (stream
, ")");
338 type_print_method_args ("", "", stream
);
339 type_print_func_varspec_suffix (type
, stream
, show
,
340 passed_a_ptr
, 0, flags
);
345 type_print_varspec_suffix (type
->target_type (),
346 stream
, 0, 1, 0, flags
);
351 gdb_printf (stream
, ")");
353 print_func_args (type
, stream
, flags
);
354 type_print_func_varspec_suffix (type
, stream
, show
,
355 passed_a_ptr
, 0, flags
);
363 pascal_language::type_print_base (struct type
*type
, struct ui_file
*stream
, int show
,
364 int level
, const struct type_print_options
*flags
) const
371 s_none
, s_public
, s_private
, s_protected
376 stream
->wrap_here (4);
379 fputs_styled ("<type unknown>", metadata_style
.style (), stream
);
384 if ((type
->code () == TYPE_CODE_PTR
)
385 && (type
->target_type ()->code () == TYPE_CODE_VOID
))
387 gdb_puts (type
->name () ? type
->name () : "pointer",
391 /* When SHOW is zero or less, and there is a valid type name, then always
392 just print the type name directly from the type. */
395 && type
->name () != NULL
)
397 gdb_puts (type
->name (), stream
);
401 type
= check_typedef (type
);
403 switch (type
->code ())
405 case TYPE_CODE_TYPEDEF
:
408 type_print_base (type
->target_type (), stream
, show
, level
,
412 case TYPE_CODE_ARRAY
:
413 print_type (type
->target_type (), NULL
, stream
, 0, 0, flags
);
417 case TYPE_CODE_METHOD
:
419 case TYPE_CODE_STRUCT
:
420 if (type
->name () != NULL
)
422 gdb_puts (type
->name (), stream
);
423 gdb_puts (" = ", stream
);
425 if (HAVE_CPLUS_STRUCT (type
))
427 gdb_printf (stream
, "class ");
431 gdb_printf (stream
, "record ");
435 case TYPE_CODE_UNION
:
436 if (type
->name () != NULL
)
438 gdb_puts (type
->name (), stream
);
439 gdb_puts (" = ", stream
);
441 gdb_printf (stream
, "case <?> of ");
444 stream
->wrap_here (4);
447 /* If we just printed a tag name, no need to print anything else. */
448 if (type
->name () == NULL
)
449 gdb_printf (stream
, "{...}");
451 else if (show
> 0 || type
->name () == NULL
)
453 type_print_derivation_info (stream
, type
);
455 gdb_printf (stream
, "\n");
456 if ((type
->num_fields () == 0) && (TYPE_NFN_FIELDS (type
) == 0))
458 if (type
->is_stub ())
459 gdb_printf (stream
, "%*s<incomplete type>\n",
462 gdb_printf (stream
, "%*s<no data fields>\n",
466 /* Start off with no specific section type, so we can print
467 one for the first field we find, and use that section type
468 thereafter until we find another type. */
470 section_type
= s_none
;
472 /* If there is a base class for this type,
473 do not print the field that it occupies. */
475 len
= type
->num_fields ();
476 for (i
= TYPE_N_BASECLASSES (type
); i
< len
; i
++)
479 /* Don't print out virtual function table. */
480 if ((startswith (type
->field (i
).name (), "_vptr"))
481 && is_cplus_marker ((type
->field (i
).name ())[5]))
484 /* If this is a pascal object or class we can print the
485 various section labels. */
487 if (HAVE_CPLUS_STRUCT (type
))
489 if (TYPE_FIELD_PROTECTED (type
, i
))
491 if (section_type
!= s_protected
)
493 section_type
= s_protected
;
494 gdb_printf (stream
, "%*sprotected\n",
498 else if (TYPE_FIELD_PRIVATE (type
, i
))
500 if (section_type
!= s_private
)
502 section_type
= s_private
;
503 gdb_printf (stream
, "%*sprivate\n",
509 if (section_type
!= s_public
)
511 section_type
= s_public
;
512 gdb_printf (stream
, "%*spublic\n",
518 print_spaces (level
+ 4, stream
);
519 if (type
->field (i
).is_static ())
520 gdb_printf (stream
, "static ");
521 print_type (type
->field (i
).type (),
522 type
->field (i
).name (),
523 stream
, show
- 1, level
+ 4, flags
);
524 if (!type
->field (i
).is_static ()
525 && type
->field (i
).is_packed ())
527 /* It is a bitfield. This code does not attempt
528 to look at the bitpos and reconstruct filler,
529 unnamed fields. This would lead to misleading
530 results if the compiler does not put out fields
531 for such things (I don't know what it does). */
532 gdb_printf (stream
, " : %d", type
->field (i
).bitsize ());
534 gdb_printf (stream
, ";\n");
537 /* If there are both fields and methods, put a space between. */
538 len
= TYPE_NFN_FIELDS (type
);
539 if (len
&& section_type
!= s_none
)
540 gdb_printf (stream
, "\n");
542 /* Object pascal: print out the methods. */
544 for (i
= 0; i
< len
; i
++)
546 struct fn_field
*f
= TYPE_FN_FIELDLIST1 (type
, i
);
547 int j
, len2
= TYPE_FN_FIELDLIST_LENGTH (type
, i
);
548 const char *method_name
= TYPE_FN_FIELDLIST_NAME (type
, i
);
550 /* this is GNU C++ specific
551 how can we know constructor/destructor?
552 It might work for GNU pascal. */
553 for (j
= 0; j
< len2
; j
++)
555 const char *physname
= TYPE_FN_FIELD_PHYSNAME (f
, j
);
557 int is_constructor
= (startswith (physname
, "__ct__"));
558 int is_destructor
= (startswith (physname
, "__dt__"));
561 if (TYPE_FN_FIELD_PROTECTED (f
, j
))
563 if (section_type
!= s_protected
)
565 section_type
= s_protected
;
566 gdb_printf (stream
, "%*sprotected\n",
570 else if (TYPE_FN_FIELD_PRIVATE (f
, j
))
572 if (section_type
!= s_private
)
574 section_type
= s_private
;
575 gdb_printf (stream
, "%*sprivate\n",
581 if (section_type
!= s_public
)
583 section_type
= s_public
;
584 gdb_printf (stream
, "%*spublic\n",
589 print_spaces (level
+ 4, stream
);
590 if (TYPE_FN_FIELD_STATIC_P (f
, j
))
591 gdb_printf (stream
, "static ");
592 if (TYPE_FN_FIELD_TYPE (f
, j
)->target_type () == 0)
594 /* Keep GDB from crashing here. */
595 gdb_printf (stream
, "<undefined type> %s;\n",
596 TYPE_FN_FIELD_PHYSNAME (f
, j
));
602 gdb_printf (stream
, "constructor ");
604 else if (is_destructor
)
606 gdb_printf (stream
, "destructor ");
608 else if (TYPE_FN_FIELD_TYPE (f
, j
)->target_type () != 0
609 && (TYPE_FN_FIELD_TYPE(f
, j
)->target_type ()->code ()
612 gdb_printf (stream
, "function ");
616 gdb_printf (stream
, "procedure ");
618 /* This does not work, no idea why !! */
620 type_print_method_args (physname
, method_name
, stream
);
622 if (TYPE_FN_FIELD_TYPE (f
, j
)->target_type () != 0
623 && (TYPE_FN_FIELD_TYPE(f
, j
)->target_type ()->code ()
626 gdb_puts (" : ", stream
);
627 type_print (TYPE_FN_FIELD_TYPE (f
, j
)->target_type (),
630 if (TYPE_FN_FIELD_VIRTUAL_P (f
, j
))
631 gdb_printf (stream
, "; virtual");
633 gdb_printf (stream
, ";\n");
636 gdb_printf (stream
, "%*send", level
, "");
641 if (type
->name () != NULL
)
643 gdb_puts (type
->name (), stream
);
645 gdb_puts (" ", stream
);
647 /* enum is just defined by
648 type enume_name = (enum_member1,enum_member2,...) */
649 gdb_printf (stream
, " = ");
650 stream
->wrap_here (4);
653 /* If we just printed a tag name, no need to print anything else. */
654 if (type
->name () == NULL
)
655 gdb_printf (stream
, "(...)");
657 else if (show
> 0 || type
->name () == NULL
)
659 gdb_printf (stream
, "(");
660 len
= type
->num_fields ();
662 for (i
= 0; i
< len
; i
++)
666 gdb_printf (stream
, ", ");
667 stream
->wrap_here (4);
668 gdb_puts (type
->field (i
).name (), stream
);
669 if (lastval
!= type
->field (i
).loc_enumval ())
673 plongest (type
->field (i
).loc_enumval ()));
674 lastval
= type
->field (i
).loc_enumval ();
678 gdb_printf (stream
, ")");
683 gdb_printf (stream
, "void");
686 case TYPE_CODE_UNDEF
:
687 gdb_printf (stream
, "record <unknown>");
690 case TYPE_CODE_ERROR
:
691 gdb_printf (stream
, "%s", TYPE_ERROR_NAME (type
));
694 /* this probably does not work for enums. */
695 case TYPE_CODE_RANGE
:
697 struct type
*target
= type
->target_type ();
699 print_type_scalar (target
, type
->bounds ()->low
.const_val (), stream
);
700 gdb_puts ("..", stream
);
701 print_type_scalar (target
, type
->bounds ()->high
.const_val (), stream
);
706 gdb_puts ("set of ", stream
);
707 print_type (type
->index_type (), "", stream
,
708 show
- 1, level
, flags
);
711 case TYPE_CODE_STRING
:
712 gdb_puts ("String", stream
);
716 /* Handle types not explicitly handled by the other cases,
717 such as fundamental types. For these, just print whatever
718 the type name is, as recorded in the type itself. If there
719 is no type name, then complain. */
720 if (type
->name () != NULL
)
722 gdb_puts (type
->name (), stream
);
726 /* At least for dump_symtab, it is important that this not be
728 fprintf_styled (stream
, metadata_style
.style (),
729 "<invalid unnamed pascal type code %d>",