gdb, testsuite: Fix return value in gdb.base/foll-fork.exp
[binutils-gdb.git] / gdb / p-typeprint.c
blob95a7f6041c1d641074b4224e0977cfb257f7a7e4
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"
23 #include "bfd.h"
24 #include "symtab.h"
25 #include "gdbtypes.h"
26 #include "expression.h"
27 #include "value.h"
28 #include "gdbcore.h"
29 #include "target.h"
30 #include "language.h"
31 #include "p-lang.h"
32 #include "typeprint.h"
33 #include "gdb-demangle.h"
34 #include <ctype.h>
35 #include "cli/cli-style.h"
37 /* See language.h. */
39 void
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
44 enum type_code code;
45 int demangled_args;
47 code = type->code ();
49 if (show > 0)
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);
57 /* first the name */
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,
80 flags);
84 /* See language.h. */
86 void
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, ";");
97 /* See p-lang.h. */
99 void
100 pascal_language::type_print_derivation_info (struct ui_file *stream,
101 struct type *type) const
103 const char *name;
104 int i;
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)");
115 if (i > 0)
117 gdb_puts (" ", stream);
121 /* See p-lang.h. */
123 void
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)
133 physname += 6;
136 gdb_puts (methodname, stream);
138 if (physname && (*physname != 0))
140 gdb_puts (" (", stream);
141 /* We must demangle this. */
142 while (isdigit (physname[0]))
144 int len = 0;
145 int i, j;
146 char *argname;
148 while (isdigit (physname[len]))
150 len++;
152 i = strtol (physname, &argname, 0);
153 physname += len;
155 for (j = 0; j < i; ++j)
156 gdb_putc (physname[j], stream);
158 physname += i;
159 if (physname[0] != 0)
161 gdb_puts (", ", stream);
164 gdb_puts (")", stream);
168 /* See p-lang.h. */
170 void
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
176 if (type == 0)
177 return;
179 if (type->name () && show <= 0)
180 return;
182 QUIT;
184 switch (type->code ())
186 case TYPE_CODE_PTR:
187 gdb_printf (stream, "^");
188 type_print_varspec_prefix (type->target_type (), stream, 0, 1,
189 flags);
190 break; /* Pointer should be handled normally
191 in pascal. */
193 case TYPE_CODE_METHOD:
194 if (passed_a_ptr)
195 gdb_printf (stream, "(");
196 if (type->target_type () != NULL
197 && type->target_type ()->code () != TYPE_CODE_VOID)
199 gdb_printf (stream, "function ");
201 else
203 gdb_printf (stream, "procedure ");
206 if (passed_a_ptr)
208 gdb_printf (stream, " ");
209 type_print_base (TYPE_SELF_TYPE (type),
210 stream, 0, passed_a_ptr, flags);
211 gdb_printf (stream, "::");
213 break;
215 case TYPE_CODE_REF:
216 type_print_varspec_prefix (type->target_type (), stream, 0, 1,
217 flags);
218 gdb_printf (stream, "&");
219 break;
221 case TYPE_CODE_FUNC:
222 if (passed_a_ptr)
223 gdb_printf (stream, "(");
225 if (type->target_type () != NULL
226 && type->target_type ()->code () != TYPE_CODE_VOID)
228 gdb_printf (stream, "function ");
230 else
232 gdb_printf (stream, "procedure ");
235 break;
237 case TYPE_CODE_ARRAY:
238 if (passed_a_ptr)
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 ");
247 break;
251 /* See p-lang.h. */
253 void
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 ();
259 if (len)
261 gdb_printf (stream, "(");
263 for (i = 0; i < len; i++)
265 if (i > 0)
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 ");
274 } */
275 print_type (type->field (i).type (), "" /* TYPE_FIELD_NAME
276 seems invalid! */
277 ,stream, -1, 0, flags);
279 if (len)
281 gdb_printf (stream, ")");
285 /* See p-lang.h. */
287 void
288 pascal_language::type_print_func_varspec_suffix (struct type *type,
289 struct ui_file *stream,
290 int show, int passed_a_ptr,
291 int demangled_args,
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);
303 else
304 type_print_base (type->target_type (), stream, show, 0,
305 flags);
307 type_print_varspec_suffix (type->target_type (), stream, 0,
308 passed_a_ptr, 0, flags);
312 /* See p-lang.h. */
314 void
315 pascal_language::type_print_varspec_suffix (struct type *type,
316 struct ui_file *stream,
317 int show, int passed_a_ptr,
318 int demangled_args,
319 const struct type_print_options *flags) const
321 if (type == 0)
322 return;
324 if (type->name () && show <= 0)
325 return;
327 QUIT;
329 switch (type->code ())
331 case TYPE_CODE_ARRAY:
332 if (passed_a_ptr)
333 gdb_printf (stream, ")");
334 break;
336 case TYPE_CODE_METHOD:
337 if (passed_a_ptr)
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);
342 break;
344 case TYPE_CODE_PTR:
345 case TYPE_CODE_REF:
346 type_print_varspec_suffix (type->target_type (),
347 stream, 0, 1, 0, flags);
348 break;
350 case TYPE_CODE_FUNC:
351 if (passed_a_ptr)
352 gdb_printf (stream, ")");
353 if (!demangled_args)
354 print_func_args (type, stream, flags);
355 type_print_func_varspec_suffix (type, stream, show,
356 passed_a_ptr, 0, flags);
357 break;
361 /* See p-lang.h. */
363 void
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
367 int i;
368 int len;
369 LONGEST lastval;
370 enum
372 s_none, s_public, s_private, s_protected
374 section_type;
376 QUIT;
377 stream->wrap_here (4);
378 if (type == NULL)
380 fputs_styled ("<type unknown>", metadata_style.style (), stream);
381 return;
384 /* void pointer */
385 if ((type->code () == TYPE_CODE_PTR)
386 && (type->target_type ()->code () == TYPE_CODE_VOID))
388 gdb_puts (type->name () ? type->name () : "pointer",
389 stream);
390 return;
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. */
395 if (show <= 0
396 && type->name () != NULL)
398 gdb_puts (type->name (), stream);
399 return;
402 type = check_typedef (type);
404 switch (type->code ())
406 case TYPE_CODE_TYPEDEF:
407 case TYPE_CODE_PTR:
408 case TYPE_CODE_REF:
409 type_print_base (type->target_type (), stream, show, level,
410 flags);
411 break;
413 case TYPE_CODE_ARRAY:
414 print_type (type->target_type (), NULL, stream, 0, 0, flags);
415 break;
417 case TYPE_CODE_FUNC:
418 case TYPE_CODE_METHOD:
419 break;
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 ");
430 else
432 gdb_printf (stream, "record ");
434 goto struct_union;
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 ");
444 struct_union:
445 stream->wrap_here (4);
446 if (show < 0)
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",
461 level + 4, "");
462 else
463 gdb_printf (stream, "%*s<no data fields>\n",
464 level + 4, "");
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++)
479 QUIT;
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]))
483 continue;
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",
498 level + 2, "");
501 else if (fld.is_private ())
503 if (section_type != s_private)
505 section_type = s_private;
506 gdb_printf (stream, "%*sprivate\n",
507 level + 2, "");
510 else
512 if (section_type != s_public)
514 section_type = s_public;
515 gdb_printf (stream, "%*spublic\n",
516 level + 2, "");
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__"));
563 QUIT;
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",
570 level + 2, "");
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",
579 level + 2, "");
582 else
584 if (section_type != s_public)
586 section_type = s_public;
587 gdb_printf (stream, "%*spublic\n",
588 level + 2, "");
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));
600 break;
603 if (is_constructor)
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 ()
613 != TYPE_CODE_VOID))
615 gdb_printf (stream, "function ");
617 else
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 ()
627 != TYPE_CODE_VOID))
629 gdb_puts (" : ", stream);
630 type_print (TYPE_FN_FIELD_TYPE (f, j)->target_type (),
631 "", stream, -1);
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, "");
641 break;
643 case TYPE_CODE_ENUM:
644 if (type->name () != NULL)
646 gdb_puts (type->name (), stream);
647 if (show > 0)
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);
654 if (show < 0)
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 ();
664 lastval = 0;
665 for (i = 0; i < len; i++)
667 QUIT;
668 if (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 ())
674 gdb_printf (stream,
675 " := %s",
676 plongest (type->field (i).loc_enumval ()));
677 lastval = type->field (i).loc_enumval ();
679 lastval++;
681 gdb_printf (stream, ")");
683 break;
685 case TYPE_CODE_VOID:
686 gdb_printf (stream, "void");
687 break;
689 case TYPE_CODE_UNDEF:
690 gdb_printf (stream, "record <unknown>");
691 break;
693 case TYPE_CODE_ERROR:
694 gdb_printf (stream, "%s", TYPE_ERROR_NAME (type));
695 break;
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);
706 break;
708 case TYPE_CODE_SET:
709 gdb_puts ("set of ", stream);
710 print_type (type->index_type (), "", stream,
711 show - 1, level, flags);
712 break;
714 case TYPE_CODE_STRING:
715 gdb_puts ("String", stream);
716 break;
718 default:
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);
727 else
729 /* At least for dump_symtab, it is important that this not be
730 an error (). */
731 fprintf_styled (stream, metadata_style.style (),
732 "<invalid unnamed pascal type code %d>",
733 type->code ());
735 break;