gdb, testsuite: Fix return value in gdb.base/foll-fork.exp
[binutils-gdb.git] / gdb / m2-typeprint.c
blobc0ae72203c32b865cf73a2aeb177b4544e7f8124
1 /* Support for printing Modula 2 types for GDB, the GNU debugger.
2 Copyright (C) 1986-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 #include "event-top.h"
20 #include "language.h"
21 #include "gdbsupport/gdb_obstack.h"
22 #include "bfd.h"
23 #include "symtab.h"
24 #include "gdbtypes.h"
25 #include "expression.h"
26 #include "value.h"
27 #include "gdbcore.h"
28 #include "m2-lang.h"
29 #include "target.h"
30 #include "language.h"
31 #include "demangle.h"
32 #include "c-lang.h"
33 #include "typeprint.h"
34 #include "cp-abi.h"
35 #include "cli/cli-style.h"
37 static void m2_print_bounds (struct type *type,
38 struct ui_file *stream, int show, int level,
39 int print_high);
41 static void m2_typedef (struct type *, struct ui_file *, int, int,
42 const struct type_print_options *);
43 static void m2_array (struct type *, struct ui_file *, int, int,
44 const struct type_print_options *);
45 static void m2_pointer (struct type *, struct ui_file *, int, int,
46 const struct type_print_options *);
47 static void m2_ref (struct type *, struct ui_file *, int, int,
48 const struct type_print_options *);
49 static void m2_procedure (struct type *, struct ui_file *, int, int,
50 const struct type_print_options *);
51 static void m2_union (struct type *, struct ui_file *);
52 static void m2_enum (struct type *, struct ui_file *, int, int);
53 static void m2_range (struct type *, struct ui_file *, int, int,
54 const struct type_print_options *);
55 static void m2_type_name (struct type *type, struct ui_file *stream);
56 static void m2_short_set (struct type *type, struct ui_file *stream,
57 int show, int level);
58 static int m2_long_set (struct type *type, struct ui_file *stream,
59 int show, int level, const struct type_print_options *flags);
60 static int m2_unbounded_array (struct type *type, struct ui_file *stream,
61 int show, int level,
62 const struct type_print_options *flags);
63 static void m2_record_fields (struct type *type, struct ui_file *stream,
64 int show, int level, const struct type_print_options *flags);
65 static void m2_unknown (const char *s, struct type *type,
66 struct ui_file *stream, int show, int level);
68 int m2_is_long_set (struct type *type);
69 int m2_is_long_set_of_type (struct type *type, struct type **of_type);
70 int m2_is_unbounded_array (struct type *type);
73 void
74 m2_print_type (struct type *type, const char *varstring,
75 struct ui_file *stream,
76 int show, int level,
77 const struct type_print_options *flags)
79 type = check_typedef (type);
81 QUIT;
83 stream->wrap_here (4);
84 if (type == NULL)
86 fputs_styled (_("<type unknown>"), metadata_style.style (), stream);
87 return;
90 switch (type->code ())
92 case TYPE_CODE_SET:
93 m2_short_set(type, stream, show, level);
94 break;
96 case TYPE_CODE_STRUCT:
97 if (m2_long_set (type, stream, show, level, flags)
98 || m2_unbounded_array (type, stream, show, level, flags))
99 break;
100 m2_record_fields (type, stream, show, level, flags);
101 break;
103 case TYPE_CODE_TYPEDEF:
104 m2_typedef (type, stream, show, level, flags);
105 break;
107 case TYPE_CODE_ARRAY:
108 m2_array (type, stream, show, level, flags);
109 break;
111 case TYPE_CODE_PTR:
112 m2_pointer (type, stream, show, level, flags);
113 break;
115 case TYPE_CODE_REF:
116 m2_ref (type, stream, show, level, flags);
117 break;
119 case TYPE_CODE_METHOD:
120 m2_unknown (_("method"), type, stream, show, level);
121 break;
123 case TYPE_CODE_FUNC:
124 m2_procedure (type, stream, show, level, flags);
125 break;
127 case TYPE_CODE_UNION:
128 m2_union (type, stream);
129 break;
131 case TYPE_CODE_ENUM:
132 m2_enum (type, stream, show, level);
133 break;
135 case TYPE_CODE_VOID:
136 break;
138 case TYPE_CODE_UNDEF:
139 /* i18n: Do not translate the "struct" part! */
140 m2_unknown (_("undef"), type, stream, show, level);
141 break;
143 case TYPE_CODE_ERROR:
144 m2_unknown (_("error"), type, stream, show, level);
145 break;
147 case TYPE_CODE_RANGE:
148 m2_range (type, stream, show, level, flags);
149 break;
151 default:
152 m2_type_name (type, stream);
153 break;
157 /* Print a typedef using M2 syntax. TYPE is the underlying type.
158 NEW_SYMBOL is the symbol naming the type. STREAM is the stream on
159 which to print. */
161 void
162 m2_language::print_typedef (struct type *type, struct symbol *new_symbol,
163 struct ui_file *stream) const
165 type = check_typedef (type);
166 gdb_printf (stream, "TYPE ");
167 if (!new_symbol->type ()->name ()
168 || strcmp ((new_symbol->type ())->name (),
169 new_symbol->linkage_name ()) != 0)
170 gdb_printf (stream, "%s = ", new_symbol->print_name ());
171 else
172 gdb_printf (stream, "<builtin> = ");
173 type_print (type, "", stream, 0);
174 gdb_printf (stream, ";");
177 /* m2_type_name - if a, type, has a name then print it. */
179 void
180 m2_type_name (struct type *type, struct ui_file *stream)
182 if (type->name () != NULL)
183 gdb_puts (type->name (), stream);
186 /* m2_range - displays a Modula-2 subrange type. */
188 void
189 m2_range (struct type *type, struct ui_file *stream, int show,
190 int level, const struct type_print_options *flags)
192 if (type->bounds ()->high.const_val () == type->bounds ()->low.const_val ())
194 /* FIXME: type::target_type used to be TYPE_DOMAIN_TYPE but that was
195 wrong. Not sure if type::target_type is correct though. */
196 m2_print_type (type->target_type (), "", stream, show, level,
197 flags);
199 else
201 struct type *target = type->target_type ();
203 gdb_printf (stream, "[");
204 print_type_scalar (target, type->bounds ()->low.const_val (), stream);
205 gdb_printf (stream, "..");
206 print_type_scalar (target, type->bounds ()->high.const_val (), stream);
207 gdb_printf (stream, "]");
211 static void
212 m2_typedef (struct type *type, struct ui_file *stream, int show,
213 int level, const struct type_print_options *flags)
215 if (type->name () != NULL)
217 gdb_puts (type->name (), stream);
218 gdb_puts (" = ", stream);
220 m2_print_type (type->target_type (), "", stream, show, level, flags);
223 /* m2_array - prints out a Modula-2 ARRAY ... OF type. */
225 static void m2_array (struct type *type, struct ui_file *stream,
226 int show, int level, const struct type_print_options *flags)
228 gdb_printf (stream, "ARRAY [");
229 if (type->target_type ()->length () > 0
230 && type->bounds ()->high.is_constant ())
232 if (type->index_type () != 0)
234 m2_print_bounds (type->index_type (), stream, show, -1, 0);
235 gdb_printf (stream, "..");
236 m2_print_bounds (type->index_type (), stream, show, -1, 1);
238 else
239 gdb_puts (pulongest ((type->length ()
240 / type->target_type ()->length ())),
241 stream);
243 gdb_printf (stream, "] OF ");
244 m2_print_type (type->target_type (), "", stream, show, level, flags);
247 static void
248 m2_pointer (struct type *type, struct ui_file *stream, int show,
249 int level, const struct type_print_options *flags)
251 if (TYPE_CONST (type))
252 gdb_printf (stream, "[...] : ");
253 else
254 gdb_printf (stream, "POINTER TO ");
256 m2_print_type (type->target_type (), "", stream, show, level, flags);
259 static void
260 m2_ref (struct type *type, struct ui_file *stream, int show,
261 int level, const struct type_print_options *flags)
263 gdb_printf (stream, "VAR");
264 m2_print_type (type->target_type (), "", stream, show, level, flags);
267 static void
268 m2_unknown (const char *s, struct type *type, struct ui_file *stream,
269 int show, int level)
271 gdb_printf (stream, "%s %s", s, _("is unknown"));
274 static void m2_union (struct type *type, struct ui_file *stream)
276 gdb_printf (stream, "union");
279 static void
280 m2_procedure (struct type *type, struct ui_file *stream,
281 int show, int level, const struct type_print_options *flags)
283 gdb_printf (stream, "PROCEDURE ");
284 m2_type_name (type, stream);
285 if (type->target_type () == NULL
286 || type->target_type ()->code () != TYPE_CODE_VOID)
288 int i, len = type->num_fields ();
290 gdb_printf (stream, " (");
291 for (i = 0; i < len; i++)
293 if (i > 0)
295 gdb_puts (", ", stream);
296 stream->wrap_here (4);
298 m2_print_type (type->field (i).type (), "", stream, -1, 0, flags);
300 gdb_printf (stream, ") : ");
301 if (type->target_type () != NULL)
302 m2_print_type (type->target_type (), "", stream, 0, 0, flags);
303 else
304 type_print_unknown_return_type (stream);
308 static void
309 m2_print_bounds (struct type *type,
310 struct ui_file *stream, int show, int level,
311 int print_high)
313 struct type *target = type->target_type ();
315 if (type->num_fields () == 0)
316 return;
318 if (print_high)
319 print_type_scalar (target, type->bounds ()->high.const_val (), stream);
320 else
321 print_type_scalar (target, type->bounds ()->low.const_val (), stream);
324 static void
325 m2_short_set (struct type *type, struct ui_file *stream, int show, int level)
327 gdb_printf(stream, "SET [");
328 m2_print_bounds (type->index_type (), stream,
329 show - 1, level, 0);
331 gdb_printf(stream, "..");
332 m2_print_bounds (type->index_type (), stream,
333 show - 1, level, 1);
334 gdb_printf(stream, "]");
338 m2_is_long_set (struct type *type)
340 LONGEST previous_high = 0; /* Unnecessary initialization
341 keeps gcc -Wall happy. */
342 int len, i;
343 struct type *range;
345 if (type->code () == TYPE_CODE_STRUCT)
348 /* check if all fields of the RECORD are consecutive sets. */
350 len = type->num_fields ();
351 for (i = TYPE_N_BASECLASSES (type); i < len; i++)
353 if (type->field (i).type () == NULL)
354 return 0;
355 if (type->field (i).type ()->code () != TYPE_CODE_SET)
356 return 0;
357 if (type->field (i).name () != NULL
358 && (strcmp (type->field (i).name (), "") != 0))
359 return 0;
360 range = type->field (i).type ()->index_type ();
361 if ((i > TYPE_N_BASECLASSES (type))
362 && previous_high + 1 != range->bounds ()->low.const_val ())
363 return 0;
364 previous_high = range->bounds ()->high.const_val ();
366 return len>0;
368 return 0;
371 /* m2_get_discrete_bounds - a wrapper for get_discrete_bounds which
372 understands that CHARs might be signed.
373 This should be integrated into gdbtypes.c
374 inside get_discrete_bounds. */
376 static bool
377 m2_get_discrete_bounds (struct type *type, LONGEST *lowp, LONGEST *highp)
379 type = check_typedef (type);
380 switch (type->code ())
382 case TYPE_CODE_CHAR:
383 if (type->length () < sizeof (LONGEST))
385 if (!type->is_unsigned ())
387 *lowp = -(1 << (type->length () * TARGET_CHAR_BIT - 1));
388 *highp = -*lowp - 1;
389 return 0;
392 [[fallthrough]];
393 default:
394 return get_discrete_bounds (type, lowp, highp);
398 /* m2_is_long_set_of_type - returns TRUE if the long set was declared as
399 SET OF <oftype> of_type is assigned to the
400 subtype. */
403 m2_is_long_set_of_type (struct type *type, struct type **of_type)
405 int len, i;
406 struct type *range;
407 struct type *target;
408 LONGEST l1, l2;
409 LONGEST h1, h2;
411 if (type->code () == TYPE_CODE_STRUCT)
413 len = type->num_fields ();
414 i = TYPE_N_BASECLASSES (type);
415 if (len == 0)
416 return 0;
417 range = type->field (i).type ()->index_type ();
418 target = range->target_type ();
420 l1 = type->field (i).type ()->bounds ()->low.const_val ();
421 h1 = type->field (len - 1).type ()->bounds ()->high.const_val ();
422 *of_type = target;
423 if (m2_get_discrete_bounds (target, &l2, &h2))
424 return (l1 == l2 && h1 == h2);
425 error (_("long_set failed to find discrete bounds for its subtype"));
426 return 0;
428 error (_("expecting long_set"));
429 return 0;
432 static int
433 m2_long_set (struct type *type, struct ui_file *stream, int show, int level,
434 const struct type_print_options *flags)
436 struct type *of_type;
437 int i;
438 int len = type->num_fields ();
439 LONGEST low;
440 LONGEST high;
442 if (m2_is_long_set (type))
444 if (type->name () != NULL)
446 gdb_puts (type->name (), stream);
447 if (show == 0)
448 return 1;
449 gdb_puts (" = ", stream);
452 if (get_long_set_bounds (type, &low, &high))
454 gdb_printf(stream, "SET OF ");
455 i = TYPE_N_BASECLASSES (type);
456 if (m2_is_long_set_of_type (type, &of_type))
457 m2_print_type (of_type, "", stream, show - 1, level, flags);
458 else
460 gdb_printf(stream, "[");
461 m2_print_bounds (type->field (i).type ()->index_type (),
462 stream, show - 1, level, 0);
464 gdb_printf(stream, "..");
466 m2_print_bounds (type->field (len - 1).type ()->index_type (),
467 stream, show - 1, level, 1);
468 gdb_printf(stream, "]");
471 else
472 /* i18n: Do not translate the "SET OF" part! */
473 gdb_printf(stream, _("SET OF <unknown>"));
475 return 1;
477 return 0;
480 /* m2_is_unbounded_array - returns TRUE if, type, should be regarded
481 as a Modula-2 unbounded ARRAY type. */
484 m2_is_unbounded_array (struct type *type)
486 if (type->code () == TYPE_CODE_STRUCT)
489 * check if we have a structure with exactly two fields named
490 * _m2_contents and _m2_high. It also checks to see if the
491 * type of _m2_contents is a pointer. The type::target_type
492 * of the pointer determines the unbounded ARRAY OF type.
494 if (type->num_fields () != 2)
495 return 0;
496 if (strcmp (type->field (0).name (), "_m2_contents") != 0)
497 return 0;
498 if (strcmp (type->field (1).name (), "_m2_high") != 0)
499 return 0;
500 if (type->field (0).type ()->code () != TYPE_CODE_PTR)
501 return 0;
502 return 1;
504 return 0;
507 /* m2_unbounded_array - if the struct type matches a Modula-2 unbounded
508 parameter type then display the type as an
509 ARRAY OF type. Returns TRUE if an unbounded
510 array type was detected. */
512 static int
513 m2_unbounded_array (struct type *type, struct ui_file *stream, int show,
514 int level, const struct type_print_options *flags)
516 if (m2_is_unbounded_array (type))
518 if (show > 0)
520 gdb_puts ("ARRAY OF ", stream);
521 m2_print_type (type->field (0).type ()->target_type (),
522 "", stream, 0, level, flags);
524 return 1;
526 return 0;
529 void
530 m2_record_fields (struct type *type, struct ui_file *stream, int show,
531 int level, const struct type_print_options *flags)
533 /* Print the tag if it exists. */
534 if (type->name () != NULL)
536 if (!startswith (type->name (), "$$"))
538 gdb_puts (type->name (), stream);
539 if (show > 0)
540 gdb_printf (stream, " = ");
543 stream->wrap_here (4);
544 if (show < 0)
546 if (type->code () == TYPE_CODE_STRUCT)
547 gdb_printf (stream, "RECORD ... END ");
548 else if (type->code () == TYPE_CODE_UNION)
549 gdb_printf (stream, "CASE ... END ");
551 else if (show > 0)
553 int i;
554 int len = type->num_fields ();
556 if (type->code () == TYPE_CODE_STRUCT)
557 gdb_printf (stream, "RECORD\n");
558 else if (type->code () == TYPE_CODE_UNION)
559 /* i18n: Do not translate "CASE" and "OF". */
560 gdb_printf (stream, _("CASE <variant> OF\n"));
562 for (i = TYPE_N_BASECLASSES (type); i < len; i++)
564 QUIT;
566 print_spaces (level + 4, stream);
567 fputs_styled (type->field (i).name (),
568 variable_name_style.style (), stream);
569 gdb_puts (" : ", stream);
570 m2_print_type (type->field (i).type (),
572 stream, 0, level + 4, flags);
573 if (type->field (i).is_packed ())
575 /* It is a bitfield. This code does not attempt
576 to look at the bitpos and reconstruct filler,
577 unnamed fields. This would lead to misleading
578 results if the compiler does not put out fields
579 for such things (I don't know what it does). */
580 gdb_printf (stream, " : %d", type->field (i).bitsize ());
582 gdb_printf (stream, ";\n");
585 gdb_printf (stream, "%*sEND ", level, "");
589 void
590 m2_enum (struct type *type, struct ui_file *stream, int show, int level)
592 LONGEST lastval;
593 int i, len;
595 if (show < 0)
597 /* If we just printed a tag name, no need to print anything else. */
598 if (type->name () == NULL)
599 gdb_printf (stream, "(...)");
601 else if (show > 0 || type->name () == NULL)
603 gdb_printf (stream, "(");
604 len = type->num_fields ();
605 lastval = 0;
606 for (i = 0; i < len; i++)
608 QUIT;
609 if (i > 0)
610 gdb_printf (stream, ", ");
611 stream->wrap_here (4);
612 fputs_styled (type->field (i).name (),
613 variable_name_style.style (), stream);
614 if (lastval != type->field (i).loc_enumval ())
616 gdb_printf (stream, " = %s",
617 plongest (type->field (i).loc_enumval ()));
618 lastval = type->field (i).loc_enumval ();
620 lastval++;
622 gdb_printf (stream, ")");