1 /* Support for printing Modula 2 types for GDB, the GNU debugger.
2 Copyright (C) 1986, 1988-1989, 1991-1992, 1995, 2000-2012 Free
3 Software Foundation, Inc.
5 This file is part of GDB.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
21 #include "gdb_obstack.h"
22 #include "bfd.h" /* Binary File Description */
25 #include "expression.h"
33 #include "typeprint.h"
36 #include "gdb_string.h"
39 static void m2_print_bounds (struct type
*type
,
40 struct ui_file
*stream
, int show
, int level
,
43 static void m2_typedef (struct type
*, struct ui_file
*, int, int,
44 const struct type_print_options
*);
45 static void m2_array (struct type
*, struct ui_file
*, int, int,
46 const struct type_print_options
*);
47 static void m2_pointer (struct type
*, struct ui_file
*, int, int,
48 const struct type_print_options
*);
49 static void m2_ref (struct type
*, struct ui_file
*, int, int,
50 const struct type_print_options
*);
51 static void m2_procedure (struct type
*, struct ui_file
*, int, int,
52 const struct type_print_options
*);
53 static void m2_union (struct type
*, struct ui_file
*);
54 static void m2_enum (struct type
*, struct ui_file
*, int, int);
55 static void m2_range (struct type
*, struct ui_file
*, int, int,
56 const struct type_print_options
*);
57 static void m2_type_name (struct type
*type
, struct ui_file
*stream
);
58 static void m2_short_set (struct type
*type
, struct ui_file
*stream
,
60 static int m2_long_set (struct type
*type
, struct ui_file
*stream
,
61 int show
, int level
, const struct type_print_options
*flags
);
62 static int m2_unbounded_array (struct type
*type
, struct ui_file
*stream
,
64 const struct type_print_options
*flags
);
65 static void m2_record_fields (struct type
*type
, struct ui_file
*stream
,
66 int show
, int level
, const struct type_print_options
*flags
);
67 static void m2_unknown (const char *s
, struct type
*type
,
68 struct ui_file
*stream
, int show
, int level
);
70 int m2_is_long_set (struct type
*type
);
71 int m2_is_long_set_of_type (struct type
*type
, struct type
**of_type
);
72 int m2_is_unbounded_array (struct type
*type
);
76 m2_print_type (struct type
*type
, const char *varstring
,
77 struct ui_file
*stream
,
79 const struct type_print_options
*flags
)
90 fputs_filtered (_("<type unknown>"), stream
);
94 code
= TYPE_CODE (type
);
95 switch (TYPE_CODE (type
))
98 m2_short_set(type
, stream
, show
, level
);
101 case TYPE_CODE_STRUCT
:
102 if (m2_long_set (type
, stream
, show
, level
, flags
)
103 || m2_unbounded_array (type
, stream
, show
, level
, flags
))
105 m2_record_fields (type
, stream
, show
, level
, flags
);
108 case TYPE_CODE_TYPEDEF
:
109 m2_typedef (type
, stream
, show
, level
, flags
);
112 case TYPE_CODE_ARRAY
:
113 m2_array (type
, stream
, show
, level
, flags
);
117 m2_pointer (type
, stream
, show
, level
, flags
);
121 m2_ref (type
, stream
, show
, level
, flags
);
124 case TYPE_CODE_METHOD
:
125 m2_unknown (_("method"), type
, stream
, show
, level
);
129 m2_procedure (type
, stream
, show
, level
, flags
);
132 case TYPE_CODE_UNION
:
133 m2_union (type
, stream
);
137 m2_enum (type
, stream
, show
, level
);
143 case TYPE_CODE_UNDEF
:
144 /* i18n: Do not translate the "struct" part! */
145 m2_unknown (_("undef"), type
, stream
, show
, level
);
148 case TYPE_CODE_ERROR
:
149 m2_unknown (_("error"), type
, stream
, show
, level
);
152 case TYPE_CODE_RANGE
:
153 m2_range (type
, stream
, show
, level
, flags
);
157 m2_type_name (type
, stream
);
162 /* Print a typedef using M2 syntax. TYPE is the underlying type.
163 NEW_SYMBOL is the symbol naming the type. STREAM is the stream on
167 m2_print_typedef (struct type
*type
, struct symbol
*new_symbol
,
168 struct ui_file
*stream
)
170 CHECK_TYPEDEF (type
);
171 fprintf_filtered (stream
, "TYPE ");
172 if (!TYPE_NAME (SYMBOL_TYPE (new_symbol
))
173 || strcmp (TYPE_NAME ((SYMBOL_TYPE (new_symbol
))),
174 SYMBOL_LINKAGE_NAME (new_symbol
)) != 0)
175 fprintf_filtered (stream
, "%s = ", SYMBOL_PRINT_NAME (new_symbol
));
177 fprintf_filtered (stream
, "<builtin> = ");
178 type_print (type
, "", stream
, 0);
179 fprintf_filtered (stream
, ";\n");
182 /* m2_type_name - if a, type, has a name then print it. */
185 m2_type_name (struct type
*type
, struct ui_file
*stream
)
187 if (TYPE_NAME (type
) != NULL
)
188 fputs_filtered (TYPE_NAME (type
), stream
);
191 /* m2_range - displays a Modula-2 subrange type. */
194 m2_range (struct type
*type
, struct ui_file
*stream
, int show
,
195 int level
, const struct type_print_options
*flags
)
197 if (TYPE_HIGH_BOUND (type
) == TYPE_LOW_BOUND (type
))
198 m2_print_type (TYPE_DOMAIN_TYPE (type
), "", stream
, show
, level
,
202 struct type
*target
= TYPE_TARGET_TYPE (type
);
204 fprintf_filtered (stream
, "[");
205 print_type_scalar (target
, TYPE_LOW_BOUND (type
), stream
);
206 fprintf_filtered (stream
, "..");
207 print_type_scalar (target
, TYPE_HIGH_BOUND (type
), stream
);
208 fprintf_filtered (stream
, "]");
213 m2_typedef (struct type
*type
, struct ui_file
*stream
, int show
,
214 int level
, const struct type_print_options
*flags
)
216 if (TYPE_NAME (type
) != NULL
)
218 fputs_filtered (TYPE_NAME (type
), stream
);
219 fputs_filtered (" = ", stream
);
221 m2_print_type (TYPE_TARGET_TYPE (type
), "", stream
, show
, level
, flags
);
224 /* m2_array - prints out a Modula-2 ARRAY ... OF type. */
226 static void m2_array (struct type
*type
, struct ui_file
*stream
,
227 int show
, int level
, const struct type_print_options
*flags
)
229 fprintf_filtered (stream
, "ARRAY [");
230 if (TYPE_LENGTH (TYPE_TARGET_TYPE (type
)) > 0
231 && !TYPE_ARRAY_UPPER_BOUND_IS_UNDEFINED (type
))
233 if (TYPE_INDEX_TYPE (type
) != 0)
235 m2_print_bounds (TYPE_INDEX_TYPE (type
), stream
, show
, -1, 0);
236 fprintf_filtered (stream
, "..");
237 m2_print_bounds (TYPE_INDEX_TYPE (type
), stream
, show
, -1, 1);
240 fprintf_filtered (stream
, "%d",
242 / TYPE_LENGTH (TYPE_TARGET_TYPE (type
))));
244 fprintf_filtered (stream
, "] OF ");
245 m2_print_type (TYPE_TARGET_TYPE (type
), "", stream
, show
, level
, flags
);
249 m2_pointer (struct type
*type
, struct ui_file
*stream
, int show
,
250 int level
, const struct type_print_options
*flags
)
252 if (TYPE_CONST (type
))
253 fprintf_filtered (stream
, "[...] : ");
255 fprintf_filtered (stream
, "POINTER TO ");
257 m2_print_type (TYPE_TARGET_TYPE (type
), "", stream
, show
, level
, flags
);
261 m2_ref (struct type
*type
, struct ui_file
*stream
, int show
,
262 int level
, const struct type_print_options
*flags
)
264 fprintf_filtered (stream
, "VAR");
265 m2_print_type (TYPE_TARGET_TYPE (type
), "", stream
, show
, level
, flags
);
269 m2_unknown (const char *s
, struct type
*type
, struct ui_file
*stream
,
272 fprintf_filtered (stream
, "%s %s", s
, _("is unknown"));
275 static void m2_union (struct type
*type
, struct ui_file
*stream
)
277 fprintf_filtered (stream
, "union");
281 m2_procedure (struct type
*type
, struct ui_file
*stream
,
282 int show
, int level
, const struct type_print_options
*flags
)
284 fprintf_filtered (stream
, "PROCEDURE ");
285 m2_type_name (type
, stream
);
286 if (TYPE_CODE (TYPE_TARGET_TYPE (type
)) != TYPE_CODE_VOID
)
288 int i
, len
= TYPE_NFIELDS (type
);
290 fprintf_filtered (stream
, " (");
291 for (i
= 0; i
< len
; i
++)
295 fputs_filtered (", ", stream
);
298 m2_print_type (TYPE_FIELD_TYPE (type
, i
), "", stream
, -1, 0, flags
);
300 if (TYPE_TARGET_TYPE (type
) != NULL
)
302 fprintf_filtered (stream
, " : ");
303 m2_print_type (TYPE_TARGET_TYPE (type
), "", stream
, 0, 0, flags
);
309 m2_print_bounds (struct type
*type
,
310 struct ui_file
*stream
, int show
, int level
,
313 struct type
*target
= TYPE_TARGET_TYPE (type
);
315 if (TYPE_NFIELDS(type
) == 0)
319 print_type_scalar (target
, TYPE_HIGH_BOUND (type
), stream
);
321 print_type_scalar (target
, TYPE_LOW_BOUND (type
), stream
);
325 m2_short_set (struct type
*type
, struct ui_file
*stream
, int show
, int level
)
327 fprintf_filtered(stream
, "SET [");
328 m2_print_bounds (TYPE_INDEX_TYPE (type
), stream
,
331 fprintf_filtered(stream
, "..");
332 m2_print_bounds (TYPE_INDEX_TYPE (type
), stream
,
334 fprintf_filtered(stream
, "]");
338 m2_is_long_set (struct type
*type
)
340 LONGEST previous_high
= 0; /* Unnecessary initialization
341 keeps gcc -Wall happy. */
345 if (TYPE_CODE (type
) == TYPE_CODE_STRUCT
)
348 /* check if all fields of the RECORD are consecutive sets. */
350 len
= TYPE_NFIELDS (type
);
351 for (i
= TYPE_N_BASECLASSES (type
); i
< len
; i
++)
353 if (TYPE_FIELD_TYPE (type
, i
) == NULL
)
355 if (TYPE_CODE (TYPE_FIELD_TYPE (type
, i
)) != TYPE_CODE_SET
)
357 if (TYPE_FIELD_NAME (type
, i
) != NULL
358 && (strcmp (TYPE_FIELD_NAME (type
, i
), "") != 0))
360 range
= TYPE_INDEX_TYPE (TYPE_FIELD_TYPE (type
, i
));
361 if ((i
> TYPE_N_BASECLASSES (type
))
362 && previous_high
+ 1 != TYPE_LOW_BOUND (range
))
364 previous_high
= TYPE_HIGH_BOUND (range
);
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. */
377 m2_get_discrete_bounds (struct type
*type
, LONGEST
*lowp
, LONGEST
*highp
)
379 CHECK_TYPEDEF (type
);
380 switch (TYPE_CODE (type
))
383 if (TYPE_LENGTH (type
) < sizeof (LONGEST
))
385 if (!TYPE_UNSIGNED (type
))
387 *lowp
= -(1 << (TYPE_LENGTH (type
) * TARGET_CHAR_BIT
- 1));
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
403 m2_is_long_set_of_type (struct type
*type
, struct type
**of_type
)
411 if (TYPE_CODE (type
) == TYPE_CODE_STRUCT
)
413 len
= TYPE_NFIELDS (type
);
414 i
= TYPE_N_BASECLASSES (type
);
417 range
= TYPE_INDEX_TYPE (TYPE_FIELD_TYPE (type
, i
));
418 target
= TYPE_TARGET_TYPE (range
);
420 l1
= TYPE_LOW_BOUND (TYPE_INDEX_TYPE (TYPE_FIELD_TYPE (type
, i
)));
421 h1
= TYPE_HIGH_BOUND (TYPE_INDEX_TYPE (TYPE_FIELD_TYPE (type
, len
-1)));
423 if (m2_get_discrete_bounds (target
, &l2
, &h2
) >= 0)
424 return (l1
== l2
&& h1
== h2
);
425 error (_("long_set failed to find discrete bounds for its subtype"));
428 error (_("expecting long_set"));
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
;
438 int len
= TYPE_NFIELDS (type
);
442 if (m2_is_long_set (type
))
444 if (TYPE_TAG_NAME (type
) != NULL
)
446 fputs_filtered (TYPE_TAG_NAME (type
), stream
);
450 else if (TYPE_NAME (type
) != NULL
)
452 fputs_filtered (TYPE_NAME (type
), stream
);
457 if (TYPE_TAG_NAME (type
) != NULL
|| TYPE_NAME (type
) != NULL
)
458 fputs_filtered (" = ", stream
);
460 if (get_long_set_bounds (type
, &low
, &high
))
462 fprintf_filtered(stream
, "SET OF ");
463 i
= TYPE_N_BASECLASSES (type
);
464 if (m2_is_long_set_of_type (type
, &of_type
))
465 m2_print_type (of_type
, "", stream
, show
- 1, level
, flags
);
468 fprintf_filtered(stream
, "[");
469 m2_print_bounds (TYPE_INDEX_TYPE (TYPE_FIELD_TYPE (type
, i
)),
470 stream
, show
- 1, level
, 0);
472 fprintf_filtered(stream
, "..");
474 m2_print_bounds (TYPE_INDEX_TYPE (TYPE_FIELD_TYPE (type
, len
-1)),
475 stream
, show
- 1, level
, 1);
476 fprintf_filtered(stream
, "]");
480 /* i18n: Do not translate the "SET OF" part! */
481 fprintf_filtered(stream
, _("SET OF <unknown>"));
488 /* m2_is_unbounded_array - returns TRUE if, type, should be regarded
489 as a Modula-2 unbounded ARRAY type. */
492 m2_is_unbounded_array (struct type
*type
)
494 if (TYPE_CODE (type
) == TYPE_CODE_STRUCT
)
497 * check if we have a structure with exactly two fields named
498 * _m2_contents and _m2_high. It also checks to see if the
499 * type of _m2_contents is a pointer. The TYPE_TARGET_TYPE
500 * of the pointer determines the unbounded ARRAY OF type.
502 if (TYPE_NFIELDS (type
) != 2)
504 if (strcmp (TYPE_FIELD_NAME (type
, 0), "_m2_contents") != 0)
506 if (strcmp (TYPE_FIELD_NAME (type
, 1), "_m2_high") != 0)
508 if (TYPE_CODE (TYPE_FIELD_TYPE (type
, 0)) != TYPE_CODE_PTR
)
515 /* m2_unbounded_array - if the struct type matches a Modula-2 unbounded
516 parameter type then display the type as an
517 ARRAY OF type. Returns TRUE if an unbounded
518 array type was detected. */
521 m2_unbounded_array (struct type
*type
, struct ui_file
*stream
, int show
,
522 int level
, const struct type_print_options
*flags
)
524 if (m2_is_unbounded_array (type
))
528 fputs_filtered ("ARRAY OF ", stream
);
529 m2_print_type (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type
, 0)),
530 "", stream
, 0, level
, flags
);
538 m2_record_fields (struct type
*type
, struct ui_file
*stream
, int show
,
539 int level
, const struct type_print_options
*flags
)
541 /* Print the tag if it exists. */
542 if (TYPE_TAG_NAME (type
) != NULL
)
544 if (strncmp (TYPE_TAG_NAME (type
), "$$", 2) != 0)
546 fputs_filtered (TYPE_TAG_NAME (type
), stream
);
548 fprintf_filtered (stream
, " = ");
554 if (TYPE_CODE (type
) == TYPE_CODE_STRUCT
)
555 fprintf_filtered (stream
, "RECORD ... END ");
556 else if (TYPE_CODE (type
) == TYPE_CODE_UNION
)
557 fprintf_filtered (stream
, "CASE ... END ");
562 int len
= TYPE_NFIELDS (type
);
564 if (TYPE_CODE (type
) == TYPE_CODE_STRUCT
)
565 fprintf_filtered (stream
, "RECORD\n");
566 else if (TYPE_CODE (type
) == TYPE_CODE_UNION
)
567 /* i18n: Do not translate "CASE" and "OF". */
568 fprintf_filtered (stream
, _("CASE <variant> OF\n"));
570 for (i
= TYPE_N_BASECLASSES (type
); i
< len
; i
++)
574 print_spaces_filtered (level
+ 4, stream
);
575 fputs_filtered (TYPE_FIELD_NAME (type
, i
), stream
);
576 fputs_filtered (" : ", stream
);
577 m2_print_type (TYPE_FIELD_TYPE (type
, i
),
579 stream
, 0, level
+ 4, flags
);
580 if (TYPE_FIELD_PACKED (type
, i
))
582 /* It is a bitfield. This code does not attempt
583 to look at the bitpos and reconstruct filler,
584 unnamed fields. This would lead to misleading
585 results if the compiler does not put out fields
586 for such things (I don't know what it does). */
587 fprintf_filtered (stream
, " : %d",
588 TYPE_FIELD_BITSIZE (type
, i
));
590 fprintf_filtered (stream
, ";\n");
593 fprintfi_filtered (level
, stream
, "END ");
598 m2_enum (struct type
*type
, struct ui_file
*stream
, int show
, int level
)
605 /* If we just printed a tag name, no need to print anything else. */
606 if (TYPE_TAG_NAME (type
) == NULL
)
607 fprintf_filtered (stream
, "(...)");
609 else if (show
> 0 || TYPE_TAG_NAME (type
) == NULL
)
611 fprintf_filtered (stream
, "(");
612 len
= TYPE_NFIELDS (type
);
614 for (i
= 0; i
< len
; i
++)
618 fprintf_filtered (stream
, ", ");
620 fputs_filtered (TYPE_FIELD_NAME (type
, i
), stream
);
621 if (lastval
!= TYPE_FIELD_ENUMVAL (type
, i
))
623 fprintf_filtered (stream
, " = %s",
624 plongest (TYPE_FIELD_ENUMVAL (type
, i
)));
625 lastval
= TYPE_FIELD_ENUMVAL (type
, i
);
629 fprintf_filtered (stream
, ")");
This page took 0.047011 seconds and 4 git commands to generate.