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"
24 #include "expression.h"
30 #include "typeprint.h"
32 #include "cli/cli-style.h"
34 static void m2_print_bounds (struct type
*type
,
35 struct ui_file
*stream
, int show
, int level
,
38 static void m2_typedef (struct type
*, struct ui_file
*, int, int,
39 const struct type_print_options
*);
40 static void m2_array (struct type
*, struct ui_file
*, int, int,
41 const struct type_print_options
*);
42 static void m2_pointer (struct type
*, struct ui_file
*, int, int,
43 const struct type_print_options
*);
44 static void m2_ref (struct type
*, struct ui_file
*, int, int,
45 const struct type_print_options
*);
46 static void m2_procedure (struct type
*, struct ui_file
*, int, int,
47 const struct type_print_options
*);
48 static void m2_union (struct type
*, struct ui_file
*);
49 static void m2_enum (struct type
*, struct ui_file
*, int, int);
50 static void m2_range (struct type
*, struct ui_file
*, int, int,
51 const struct type_print_options
*);
52 static void m2_type_name (struct type
*type
, struct ui_file
*stream
);
53 static void m2_short_set (struct type
*type
, struct ui_file
*stream
,
55 static int m2_long_set (struct type
*type
, struct ui_file
*stream
,
56 int show
, int level
, const struct type_print_options
*flags
);
57 static int m2_unbounded_array (struct type
*type
, struct ui_file
*stream
,
59 const struct type_print_options
*flags
);
60 static void m2_record_fields (struct type
*type
, struct ui_file
*stream
,
61 int show
, int level
, const struct type_print_options
*flags
);
62 static void m2_unknown (const char *s
, struct type
*type
,
63 struct ui_file
*stream
, int show
, int level
);
65 int m2_is_long_set (struct type
*type
);
66 int m2_is_long_set_of_type (struct type
*type
, struct type
**of_type
);
67 int m2_is_unbounded_array (struct type
*type
);
71 m2_print_type (struct type
*type
, const char *varstring
,
72 struct ui_file
*stream
,
74 const struct type_print_options
*flags
)
76 type
= check_typedef (type
);
80 stream
->wrap_here (4);
83 fputs_styled (_("<type unknown>"), metadata_style
.style (), stream
);
87 switch (type
->code ())
90 m2_short_set(type
, stream
, show
, level
);
93 case TYPE_CODE_STRUCT
:
94 if (m2_long_set (type
, stream
, show
, level
, flags
)
95 || m2_unbounded_array (type
, stream
, show
, level
, flags
))
97 m2_record_fields (type
, stream
, show
, level
, flags
);
100 case TYPE_CODE_TYPEDEF
:
101 m2_typedef (type
, stream
, show
, level
, flags
);
104 case TYPE_CODE_ARRAY
:
105 m2_array (type
, stream
, show
, level
, flags
);
109 m2_pointer (type
, stream
, show
, level
, flags
);
113 m2_ref (type
, stream
, show
, level
, flags
);
116 case TYPE_CODE_METHOD
:
117 m2_unknown (_("method"), type
, stream
, show
, level
);
121 m2_procedure (type
, stream
, show
, level
, flags
);
124 case TYPE_CODE_UNION
:
125 m2_union (type
, stream
);
129 m2_enum (type
, stream
, show
, level
);
135 case TYPE_CODE_UNDEF
:
136 /* i18n: Do not translate the "struct" part! */
137 m2_unknown (_("undef"), type
, stream
, show
, level
);
140 case TYPE_CODE_ERROR
:
141 m2_unknown (_("error"), type
, stream
, show
, level
);
144 case TYPE_CODE_RANGE
:
145 m2_range (type
, stream
, show
, level
, flags
);
149 m2_type_name (type
, stream
);
154 /* Print a typedef using M2 syntax. TYPE is the underlying type.
155 NEW_SYMBOL is the symbol naming the type. STREAM is the stream on
159 m2_language::print_typedef (struct type
*type
, struct symbol
*new_symbol
,
160 struct ui_file
*stream
) const
162 type
= check_typedef (type
);
163 gdb_printf (stream
, "TYPE ");
164 if (!new_symbol
->type ()->name ()
165 || strcmp ((new_symbol
->type ())->name (),
166 new_symbol
->linkage_name ()) != 0)
167 gdb_printf (stream
, "%s = ", new_symbol
->print_name ());
169 gdb_printf (stream
, "<builtin> = ");
170 type_print (type
, "", stream
, 0);
171 gdb_printf (stream
, ";");
174 /* m2_type_name - if a, type, has a name then print it. */
177 m2_type_name (struct type
*type
, struct ui_file
*stream
)
179 if (type
->name () != NULL
)
180 gdb_puts (type
->name (), stream
);
183 /* m2_range - displays a Modula-2 subrange type. */
186 m2_range (struct type
*type
, struct ui_file
*stream
, int show
,
187 int level
, const struct type_print_options
*flags
)
189 if (type
->bounds ()->high
.const_val () == type
->bounds ()->low
.const_val ())
191 /* FIXME: type::target_type used to be TYPE_DOMAIN_TYPE but that was
192 wrong. Not sure if type::target_type is correct though. */
193 m2_print_type (type
->target_type (), "", stream
, show
, level
,
198 struct type
*target
= type
->target_type ();
200 gdb_printf (stream
, "[");
201 print_type_scalar (target
, type
->bounds ()->low
.const_val (), stream
);
202 gdb_printf (stream
, "..");
203 print_type_scalar (target
, type
->bounds ()->high
.const_val (), stream
);
204 gdb_printf (stream
, "]");
209 m2_typedef (struct type
*type
, struct ui_file
*stream
, int show
,
210 int level
, const struct type_print_options
*flags
)
212 if (type
->name () != NULL
)
214 gdb_puts (type
->name (), stream
);
215 gdb_puts (" = ", stream
);
217 m2_print_type (type
->target_type (), "", stream
, show
, level
, flags
);
220 /* m2_array - prints out a Modula-2 ARRAY ... OF type. */
222 static void m2_array (struct type
*type
, struct ui_file
*stream
,
223 int show
, int level
, const struct type_print_options
*flags
)
225 gdb_printf (stream
, "ARRAY [");
226 if (type
->target_type ()->length () > 0
227 && type
->bounds ()->high
.is_constant ())
229 if (type
->index_type () != 0)
231 m2_print_bounds (type
->index_type (), stream
, show
, -1, 0);
232 gdb_printf (stream
, "..");
233 m2_print_bounds (type
->index_type (), stream
, show
, -1, 1);
236 gdb_puts (pulongest ((type
->length ()
237 / type
->target_type ()->length ())),
240 gdb_printf (stream
, "] OF ");
241 m2_print_type (type
->target_type (), "", stream
, show
, level
, flags
);
245 m2_pointer (struct type
*type
, struct ui_file
*stream
, int show
,
246 int level
, const struct type_print_options
*flags
)
248 if (TYPE_CONST (type
))
249 gdb_printf (stream
, "[...] : ");
251 gdb_printf (stream
, "POINTER TO ");
253 m2_print_type (type
->target_type (), "", stream
, show
, level
, flags
);
257 m2_ref (struct type
*type
, struct ui_file
*stream
, int show
,
258 int level
, const struct type_print_options
*flags
)
260 gdb_printf (stream
, "VAR");
261 m2_print_type (type
->target_type (), "", stream
, show
, level
, flags
);
265 m2_unknown (const char *s
, struct type
*type
, struct ui_file
*stream
,
268 gdb_printf (stream
, "%s %s", s
, _("is unknown"));
271 static void m2_union (struct type
*type
, struct ui_file
*stream
)
273 gdb_printf (stream
, "union");
277 m2_procedure (struct type
*type
, struct ui_file
*stream
,
278 int show
, int level
, const struct type_print_options
*flags
)
280 gdb_printf (stream
, "PROCEDURE ");
281 m2_type_name (type
, stream
);
282 if (type
->target_type () == NULL
283 || type
->target_type ()->code () != TYPE_CODE_VOID
)
285 int i
, len
= type
->num_fields ();
287 gdb_printf (stream
, " (");
288 for (i
= 0; i
< len
; i
++)
292 gdb_puts (", ", stream
);
293 stream
->wrap_here (4);
295 m2_print_type (type
->field (i
).type (), "", stream
, -1, 0, flags
);
297 gdb_printf (stream
, ") : ");
298 if (type
->target_type () != NULL
)
299 m2_print_type (type
->target_type (), "", stream
, 0, 0, flags
);
301 type_print_unknown_return_type (stream
);
306 m2_print_bounds (struct type
*type
,
307 struct ui_file
*stream
, int show
, int level
,
310 struct type
*target
= type
->target_type ();
312 if (type
->num_fields () == 0)
316 print_type_scalar (target
, type
->bounds ()->high
.const_val (), stream
);
318 print_type_scalar (target
, type
->bounds ()->low
.const_val (), stream
);
322 m2_short_set (struct type
*type
, struct ui_file
*stream
, int show
, int level
)
324 gdb_printf(stream
, "SET [");
325 m2_print_bounds (type
->index_type (), stream
,
328 gdb_printf(stream
, "..");
329 m2_print_bounds (type
->index_type (), stream
,
331 gdb_printf(stream
, "]");
335 m2_is_long_set (struct type
*type
)
337 LONGEST previous_high
= 0; /* Unnecessary initialization
338 keeps gcc -Wall happy. */
342 if (type
->code () == TYPE_CODE_STRUCT
)
345 /* check if all fields of the RECORD are consecutive sets. */
347 len
= type
->num_fields ();
348 for (i
= TYPE_N_BASECLASSES (type
); i
< len
; i
++)
350 if (type
->field (i
).type () == NULL
)
352 if (type
->field (i
).type ()->code () != TYPE_CODE_SET
)
354 if (type
->field (i
).name () != NULL
355 && (strcmp (type
->field (i
).name (), "") != 0))
357 range
= type
->field (i
).type ()->index_type ();
358 if ((i
> TYPE_N_BASECLASSES (type
))
359 && previous_high
+ 1 != range
->bounds ()->low
.const_val ())
361 previous_high
= range
->bounds ()->high
.const_val ();
368 /* m2_get_discrete_bounds - a wrapper for get_discrete_bounds which
369 understands that CHARs might be signed.
370 This should be integrated into gdbtypes.c
371 inside get_discrete_bounds. */
374 m2_get_discrete_bounds (struct type
*type
, LONGEST
*lowp
, LONGEST
*highp
)
376 type
= check_typedef (type
);
377 switch (type
->code ())
380 if (type
->length () < sizeof (LONGEST
))
382 if (!type
->is_unsigned ())
384 *lowp
= -(1 << (type
->length () * TARGET_CHAR_BIT
- 1));
391 return get_discrete_bounds (type
, lowp
, highp
);
395 /* m2_is_long_set_of_type - returns TRUE if the long set was declared as
396 SET OF <oftype> of_type is assigned to the
400 m2_is_long_set_of_type (struct type
*type
, struct type
**of_type
)
408 if (type
->code () == TYPE_CODE_STRUCT
)
410 len
= type
->num_fields ();
411 i
= TYPE_N_BASECLASSES (type
);
414 range
= type
->field (i
).type ()->index_type ();
415 target
= range
->target_type ();
417 l1
= type
->field (i
).type ()->bounds ()->low
.const_val ();
418 h1
= type
->field (len
- 1).type ()->bounds ()->high
.const_val ();
420 if (m2_get_discrete_bounds (target
, &l2
, &h2
))
421 return (l1
== l2
&& h1
== h2
);
422 error (_("long_set failed to find discrete bounds for its subtype"));
425 error (_("expecting long_set"));
430 m2_long_set (struct type
*type
, struct ui_file
*stream
, int show
, int level
,
431 const struct type_print_options
*flags
)
433 struct type
*of_type
;
435 int len
= type
->num_fields ();
439 if (m2_is_long_set (type
))
441 if (type
->name () != NULL
)
443 gdb_puts (type
->name (), stream
);
446 gdb_puts (" = ", stream
);
449 if (get_long_set_bounds (type
, &low
, &high
))
451 gdb_printf(stream
, "SET OF ");
452 i
= TYPE_N_BASECLASSES (type
);
453 if (m2_is_long_set_of_type (type
, &of_type
))
454 m2_print_type (of_type
, "", stream
, show
- 1, level
, flags
);
457 gdb_printf(stream
, "[");
458 m2_print_bounds (type
->field (i
).type ()->index_type (),
459 stream
, show
- 1, level
, 0);
461 gdb_printf(stream
, "..");
463 m2_print_bounds (type
->field (len
- 1).type ()->index_type (),
464 stream
, show
- 1, level
, 1);
465 gdb_printf(stream
, "]");
469 /* i18n: Do not translate the "SET OF" part! */
470 gdb_printf(stream
, _("SET OF <unknown>"));
477 /* m2_is_unbounded_array - returns TRUE if, type, should be regarded
478 as a Modula-2 unbounded ARRAY type. */
481 m2_is_unbounded_array (struct type
*type
)
483 if (type
->code () == TYPE_CODE_STRUCT
)
486 * check if we have a structure with exactly two fields named
487 * _m2_contents and _m2_high. It also checks to see if the
488 * type of _m2_contents is a pointer. The type::target_type
489 * of the pointer determines the unbounded ARRAY OF type.
491 if (type
->num_fields () != 2)
493 if (strcmp (type
->field (0).name (), "_m2_contents") != 0)
495 if (strcmp (type
->field (1).name (), "_m2_high") != 0)
497 if (type
->field (0).type ()->code () != TYPE_CODE_PTR
)
504 /* m2_unbounded_array - if the struct type matches a Modula-2 unbounded
505 parameter type then display the type as an
506 ARRAY OF type. Returns TRUE if an unbounded
507 array type was detected. */
510 m2_unbounded_array (struct type
*type
, struct ui_file
*stream
, int show
,
511 int level
, const struct type_print_options
*flags
)
513 if (m2_is_unbounded_array (type
))
517 gdb_puts ("ARRAY OF ", stream
);
518 m2_print_type (type
->field (0).type ()->target_type (),
519 "", stream
, 0, level
, flags
);
527 m2_record_fields (struct type
*type
, struct ui_file
*stream
, int show
,
528 int level
, const struct type_print_options
*flags
)
530 /* Print the tag if it exists. */
531 if (type
->name () != NULL
)
533 if (!startswith (type
->name (), "$$"))
535 gdb_puts (type
->name (), stream
);
537 gdb_printf (stream
, " = ");
540 stream
->wrap_here (4);
543 if (type
->code () == TYPE_CODE_STRUCT
)
544 gdb_printf (stream
, "RECORD ... END ");
545 else if (type
->code () == TYPE_CODE_UNION
)
546 gdb_printf (stream
, "CASE ... END ");
551 int len
= type
->num_fields ();
553 if (type
->code () == TYPE_CODE_STRUCT
)
554 gdb_printf (stream
, "RECORD\n");
555 else if (type
->code () == TYPE_CODE_UNION
)
556 /* i18n: Do not translate "CASE" and "OF". */
557 gdb_printf (stream
, _("CASE <variant> OF\n"));
559 for (i
= TYPE_N_BASECLASSES (type
); i
< len
; i
++)
563 print_spaces (level
+ 4, stream
);
564 fputs_styled (type
->field (i
).name (),
565 variable_name_style
.style (), stream
);
566 gdb_puts (" : ", stream
);
567 m2_print_type (type
->field (i
).type (),
569 stream
, 0, level
+ 4, flags
);
570 if (type
->field (i
).is_packed ())
572 /* It is a bitfield. This code does not attempt
573 to look at the bitpos and reconstruct filler,
574 unnamed fields. This would lead to misleading
575 results if the compiler does not put out fields
576 for such things (I don't know what it does). */
577 gdb_printf (stream
, " : %d", type
->field (i
).bitsize ());
579 gdb_printf (stream
, ";\n");
582 gdb_printf (stream
, "%*sEND ", level
, "");
587 m2_enum (struct type
*type
, struct ui_file
*stream
, int show
, int level
)
594 /* If we just printed a tag name, no need to print anything else. */
595 if (type
->name () == NULL
)
596 gdb_printf (stream
, "(...)");
598 else if (show
> 0 || type
->name () == NULL
)
600 gdb_printf (stream
, "(");
601 len
= type
->num_fields ();
603 for (i
= 0; i
< len
; i
++)
607 gdb_printf (stream
, ", ");
608 stream
->wrap_here (4);
609 fputs_styled (type
->field (i
).name (),
610 variable_name_style
.style (), stream
);
611 if (lastval
!= type
->field (i
).loc_enumval ())
613 gdb_printf (stream
, " = %s",
614 plongest (type
->field (i
).loc_enumval ()));
615 lastval
= type
->field (i
).loc_enumval ();
619 gdb_printf (stream
, ")");