1 /* Support for printing Pascal types for GDB, the GNU debugger.
2 Copyright (C) 2000-2024 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 */
21 #include "event-top.h"
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 if (varstring
!= nullptr)
59 gdb_puts (varstring
, stream
);
61 if ((varstring
!= NULL
&& *varstring
!= '\0')
62 && !(code
== TYPE_CODE_FUNC
63 || code
== TYPE_CODE_METHOD
))
65 gdb_puts (" : ", stream
);
68 if (!(code
== TYPE_CODE_FUNC
69 || code
== TYPE_CODE_METHOD
))
71 type_print_varspec_prefix (type
, stream
, show
, 0, flags
);
74 type_print_base (type
, stream
, show
, level
, flags
);
75 /* For demangled function names, we have the arglist as part of the name,
76 so don't print an additional pair of ()'s. */
78 demangled_args
= varstring
? strchr (varstring
, '(') != NULL
: 0;
79 type_print_varspec_suffix (type
, stream
, show
, 0, demangled_args
,
87 pascal_language::print_typedef (struct type
*type
, struct symbol
*new_symbol
,
88 struct ui_file
*stream
) const
90 type
= check_typedef (type
);
91 gdb_printf (stream
, "type ");
92 gdb_printf (stream
, "%s = ", new_symbol
->print_name ());
93 type_print (type
, "", stream
, 0);
94 gdb_printf (stream
, ";");
100 pascal_language::type_print_derivation_info (struct ui_file
*stream
,
101 struct type
*type
) const
106 for (i
= 0; i
< TYPE_N_BASECLASSES (type
); i
++)
108 gdb_puts (i
== 0 ? ": " : ", ", stream
);
109 gdb_printf (stream
, "%s%s ",
110 BASETYPE_VIA_PUBLIC (type
, i
) ? "public" : "private",
111 BASETYPE_VIA_VIRTUAL (type
, i
) ? " virtual" : "");
112 name
= TYPE_BASECLASS (type
, i
)->name ();
113 gdb_printf (stream
, "%s", name
? name
: "(null)");
117 gdb_puts (" ", stream
);
124 pascal_language::type_print_method_args (const char *physname
,
125 const char *methodname
,
126 struct ui_file
*stream
) const
128 int is_constructor
= (startswith (physname
, "__ct__"));
129 int is_destructor
= (startswith (physname
, "__dt__"));
131 if (is_constructor
|| is_destructor
)
136 gdb_puts (methodname
, stream
);
138 if (physname
&& (*physname
!= 0))
140 gdb_puts (" (", stream
);
141 /* We must demangle this. */
142 while (isdigit (physname
[0]))
148 while (isdigit (physname
[len
]))
152 i
= strtol (physname
, &argname
, 0);
155 for (j
= 0; j
< i
; ++j
)
156 gdb_putc (physname
[j
], stream
);
159 if (physname
[0] != 0)
161 gdb_puts (", ", stream
);
164 gdb_puts (")", stream
);
171 pascal_language::type_print_varspec_prefix (struct type
*type
,
172 struct ui_file
*stream
,
173 int show
, int passed_a_ptr
,
174 const struct type_print_options
*flags
) const
179 if (type
->name () && show
<= 0)
184 switch (type
->code ())
187 gdb_printf (stream
, "^");
188 type_print_varspec_prefix (type
->target_type (), stream
, 0, 1,
190 break; /* Pointer should be handled normally
193 case TYPE_CODE_METHOD
:
195 gdb_printf (stream
, "(");
196 if (type
->target_type () != NULL
197 && type
->target_type ()->code () != TYPE_CODE_VOID
)
199 gdb_printf (stream
, "function ");
203 gdb_printf (stream
, "procedure ");
208 gdb_printf (stream
, " ");
209 type_print_base (TYPE_SELF_TYPE (type
),
210 stream
, 0, passed_a_ptr
, flags
);
211 gdb_printf (stream
, "::");
216 type_print_varspec_prefix (type
->target_type (), stream
, 0, 1,
218 gdb_printf (stream
, "&");
223 gdb_printf (stream
, "(");
225 if (type
->target_type () != NULL
226 && type
->target_type ()->code () != TYPE_CODE_VOID
)
228 gdb_printf (stream
, "function ");
232 gdb_printf (stream
, "procedure ");
237 case TYPE_CODE_ARRAY
:
239 gdb_printf (stream
, "(");
240 gdb_printf (stream
, "array ");
241 if (type
->target_type ()->length () > 0
242 && type
->bounds ()->high
.is_constant ())
243 gdb_printf (stream
, "[%s..%s] ",
244 plongest (type
->bounds ()->low
.const_val ()),
245 plongest (type
->bounds ()->high
.const_val ()));
246 gdb_printf (stream
, "of ");
254 pascal_language::print_func_args (struct type
*type
, struct ui_file
*stream
,
255 const struct type_print_options
*flags
) const
257 int i
, len
= type
->num_fields ();
261 gdb_printf (stream
, "(");
263 for (i
= 0; i
< len
; i
++)
267 gdb_puts (", ", stream
);
268 stream
->wrap_here (4);
270 /* Can we find if it is a var parameter ??
271 if ( TYPE_FIELD(type, i) == )
273 gdb_printf (stream, "var ");
275 print_type (type
->field (i
).type (), "" /* TYPE_FIELD_NAME
277 ,stream
, -1, 0, flags
);
281 gdb_printf (stream
, ")");
288 pascal_language::type_print_func_varspec_suffix (struct type
*type
,
289 struct ui_file
*stream
,
290 int show
, int passed_a_ptr
,
292 const struct type_print_options
*flags
) const
294 if (type
->target_type () == NULL
295 || type
->target_type ()->code () != TYPE_CODE_VOID
)
297 gdb_printf (stream
, " : ");
298 type_print_varspec_prefix (type
->target_type (),
299 stream
, 0, 0, flags
);
301 if (type
->target_type () == NULL
)
302 type_print_unknown_return_type (stream
);
304 type_print_base (type
->target_type (), stream
, show
, 0,
307 type_print_varspec_suffix (type
->target_type (), stream
, 0,
308 passed_a_ptr
, 0, flags
);
315 pascal_language::type_print_varspec_suffix (struct type
*type
,
316 struct ui_file
*stream
,
317 int show
, int passed_a_ptr
,
319 const struct type_print_options
*flags
) const
324 if (type
->name () && show
<= 0)
329 switch (type
->code ())
331 case TYPE_CODE_ARRAY
:
333 gdb_printf (stream
, ")");
336 case TYPE_CODE_METHOD
:
338 gdb_printf (stream
, ")");
339 type_print_method_args ("", "", stream
);
340 type_print_func_varspec_suffix (type
, stream
, show
,
341 passed_a_ptr
, 0, flags
);
346 type_print_varspec_suffix (type
->target_type (),
347 stream
, 0, 1, 0, flags
);
352 gdb_printf (stream
, ")");
354 print_func_args (type
, stream
, flags
);
355 type_print_func_varspec_suffix (type
, stream
, show
,
356 passed_a_ptr
, 0, flags
);
364 pascal_language::type_print_base (struct type
*type
, struct ui_file
*stream
, int show
,
365 int level
, const struct type_print_options
*flags
) const
372 s_none
, s_public
, s_private
, s_protected
377 stream
->wrap_here (4);
380 fputs_styled ("<type unknown>", metadata_style
.style (), stream
);
385 if ((type
->code () == TYPE_CODE_PTR
)
386 && (type
->target_type ()->code () == TYPE_CODE_VOID
))
388 gdb_puts (type
->name () ? type
->name () : "pointer",
392 /* When SHOW is zero or less, and there is a valid type name, then always
393 just print the type name directly from the type. */
396 && type
->name () != NULL
)
398 gdb_puts (type
->name (), stream
);
402 type
= check_typedef (type
);
404 switch (type
->code ())
406 case TYPE_CODE_TYPEDEF
:
409 type_print_base (type
->target_type (), stream
, show
, level
,
413 case TYPE_CODE_ARRAY
:
414 print_type (type
->target_type (), NULL
, stream
, 0, 0, flags
);
418 case TYPE_CODE_METHOD
:
420 case TYPE_CODE_STRUCT
:
421 if (type
->name () != NULL
)
423 gdb_puts (type
->name (), stream
);
424 gdb_puts (" = ", stream
);
426 if (HAVE_CPLUS_STRUCT (type
))
428 gdb_printf (stream
, "class ");
432 gdb_printf (stream
, "record ");
436 case TYPE_CODE_UNION
:
437 if (type
->name () != NULL
)
439 gdb_puts (type
->name (), stream
);
440 gdb_puts (" = ", stream
);
442 gdb_printf (stream
, "case <?> of ");
445 stream
->wrap_here (4);
448 /* If we just printed a tag name, no need to print anything else. */
449 if (type
->name () == NULL
)
450 gdb_printf (stream
, "{...}");
452 else if (show
> 0 || type
->name () == NULL
)
454 type_print_derivation_info (stream
, type
);
456 gdb_printf (stream
, "\n");
457 if ((type
->num_fields () == 0) && (TYPE_NFN_FIELDS (type
) == 0))
459 if (type
->is_stub ())
460 gdb_printf (stream
, "%*s<incomplete type>\n",
463 gdb_printf (stream
, "%*s<no data fields>\n",
467 /* Start off with no specific section type, so we can print
468 one for the first field we find, and use that section type
469 thereafter until we find another type. */
471 section_type
= s_none
;
473 /* If there is a base class for this type,
474 do not print the field that it occupies. */
476 len
= type
->num_fields ();
477 for (i
= TYPE_N_BASECLASSES (type
); i
< len
; i
++)
480 /* Don't print out virtual function table. */
481 if ((startswith (type
->field (i
).name (), "_vptr"))
482 && is_cplus_marker ((type
->field (i
).name ())[5]))
485 /* If this is a pascal object or class we can print the
486 various section labels. */
488 if (HAVE_CPLUS_STRUCT (type
))
490 field
&fld
= type
->field (i
);
492 if (fld
.is_protected ())
494 if (section_type
!= s_protected
)
496 section_type
= s_protected
;
497 gdb_printf (stream
, "%*sprotected\n",
501 else if (fld
.is_private ())
503 if (section_type
!= s_private
)
505 section_type
= s_private
;
506 gdb_printf (stream
, "%*sprivate\n",
512 if (section_type
!= s_public
)
514 section_type
= s_public
;
515 gdb_printf (stream
, "%*spublic\n",
521 print_spaces (level
+ 4, stream
);
522 if (type
->field (i
).is_static ())
523 gdb_printf (stream
, "static ");
524 print_type (type
->field (i
).type (),
525 type
->field (i
).name (),
526 stream
, show
- 1, level
+ 4, flags
);
527 if (!type
->field (i
).is_static ()
528 && type
->field (i
).is_packed ())
530 /* It is a bitfield. This code does not attempt
531 to look at the bitpos and reconstruct filler,
532 unnamed fields. This would lead to misleading
533 results if the compiler does not put out fields
534 for such things (I don't know what it does). */
535 gdb_printf (stream
, " : %d", type
->field (i
).bitsize ());
537 gdb_printf (stream
, ";\n");
540 /* If there are both fields and methods, put a space between. */
541 len
= TYPE_NFN_FIELDS (type
);
542 if (len
&& section_type
!= s_none
)
543 gdb_printf (stream
, "\n");
545 /* Object pascal: print out the methods. */
547 for (i
= 0; i
< len
; i
++)
549 struct fn_field
*f
= TYPE_FN_FIELDLIST1 (type
, i
);
550 int j
, len2
= TYPE_FN_FIELDLIST_LENGTH (type
, i
);
551 const char *method_name
= TYPE_FN_FIELDLIST_NAME (type
, i
);
553 /* this is GNU C++ specific
554 how can we know constructor/destructor?
555 It might work for GNU pascal. */
556 for (j
= 0; j
< len2
; j
++)
558 const char *physname
= TYPE_FN_FIELD_PHYSNAME (f
, j
);
560 int is_constructor
= (startswith (physname
, "__ct__"));
561 int is_destructor
= (startswith (physname
, "__dt__"));
564 if (TYPE_FN_FIELD_PROTECTED (f
, j
))
566 if (section_type
!= s_protected
)
568 section_type
= s_protected
;
569 gdb_printf (stream
, "%*sprotected\n",
573 else if (TYPE_FN_FIELD_PRIVATE (f
, j
))
575 if (section_type
!= s_private
)
577 section_type
= s_private
;
578 gdb_printf (stream
, "%*sprivate\n",
584 if (section_type
!= s_public
)
586 section_type
= s_public
;
587 gdb_printf (stream
, "%*spublic\n",
592 print_spaces (level
+ 4, stream
);
593 if (TYPE_FN_FIELD_STATIC_P (f
, j
))
594 gdb_printf (stream
, "static ");
595 if (TYPE_FN_FIELD_TYPE (f
, j
)->target_type () == 0)
597 /* Keep GDB from crashing here. */
598 gdb_printf (stream
, "<undefined type> %s;\n",
599 TYPE_FN_FIELD_PHYSNAME (f
, j
));
605 gdb_printf (stream
, "constructor ");
607 else if (is_destructor
)
609 gdb_printf (stream
, "destructor ");
611 else if (TYPE_FN_FIELD_TYPE (f
, j
)->target_type () != 0
612 && (TYPE_FN_FIELD_TYPE(f
, j
)->target_type ()->code ()
615 gdb_printf (stream
, "function ");
619 gdb_printf (stream
, "procedure ");
621 /* This does not work, no idea why !! */
623 type_print_method_args (physname
, method_name
, stream
);
625 if (TYPE_FN_FIELD_TYPE (f
, j
)->target_type () != 0
626 && (TYPE_FN_FIELD_TYPE(f
, j
)->target_type ()->code ()
629 gdb_puts (" : ", stream
);
630 type_print (TYPE_FN_FIELD_TYPE (f
, j
)->target_type (),
633 if (TYPE_FN_FIELD_VIRTUAL_P (f
, j
))
634 gdb_printf (stream
, "; virtual");
636 gdb_printf (stream
, ";\n");
639 gdb_printf (stream
, "%*send", level
, "");
644 if (type
->name () != NULL
)
646 gdb_puts (type
->name (), stream
);
648 gdb_puts (" ", stream
);
650 /* enum is just defined by
651 type enume_name = (enum_member1,enum_member2,...) */
652 gdb_printf (stream
, " = ");
653 stream
->wrap_here (4);
656 /* If we just printed a tag name, no need to print anything else. */
657 if (type
->name () == NULL
)
658 gdb_printf (stream
, "(...)");
660 else if (show
> 0 || type
->name () == NULL
)
662 gdb_printf (stream
, "(");
663 len
= type
->num_fields ();
665 for (i
= 0; i
< len
; i
++)
669 gdb_printf (stream
, ", ");
670 stream
->wrap_here (4);
671 gdb_puts (type
->field (i
).name (), stream
);
672 if (lastval
!= type
->field (i
).loc_enumval ())
676 plongest (type
->field (i
).loc_enumval ()));
677 lastval
= type
->field (i
).loc_enumval ();
681 gdb_printf (stream
, ")");
686 gdb_printf (stream
, "void");
689 case TYPE_CODE_UNDEF
:
690 gdb_printf (stream
, "record <unknown>");
693 case TYPE_CODE_ERROR
:
694 gdb_printf (stream
, "%s", TYPE_ERROR_NAME (type
));
697 /* this probably does not work for enums. */
698 case TYPE_CODE_RANGE
:
700 struct type
*target
= type
->target_type ();
702 print_type_scalar (target
, type
->bounds ()->low
.const_val (), stream
);
703 gdb_puts ("..", stream
);
704 print_type_scalar (target
, type
->bounds ()->high
.const_val (), stream
);
709 gdb_puts ("set of ", stream
);
710 print_type (type
->index_type (), "", stream
,
711 show
- 1, level
, flags
);
714 case TYPE_CODE_STRING
:
715 gdb_puts ("String", stream
);
719 /* Handle types not explicitly handled by the other cases,
720 such as fundamental types. For these, just print whatever
721 the type name is, as recorded in the type itself. If there
722 is no type name, then complain. */
723 if (type
->name () != NULL
)
725 gdb_puts (type
->name (), stream
);
729 /* At least for dump_symtab, it is important that this not be
731 fprintf_styled (stream
, metadata_style
.style (),
732 "<invalid unnamed pascal type code %d>",