1 /* Support for printing Modula 2 types for GDB, the GNU debugger.
2 Copyright (C) 1986, 1988, 1989, 1991, 1992, 1995, 2000, 2001, 2002, 2003,
3 2004, 2005, 2006, 2007 Free 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 2 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, write to the Free Software
19 Foundation, Inc., 51 Franklin Street, Fifth Floor,
20 Boston, MA 02110-1301, USA. */
23 #include "gdb_obstack.h"
24 #include "bfd.h" /* Binary File Description */
27 #include "expression.h"
35 #include "typeprint.h"
38 #include "gdb_string.h"
41 static void m2_print_bounds (struct type
*type
,
42 struct ui_file
*stream
, int show
, int level
,
45 static void m2_typedef (struct type
*, struct ui_file
*, int, int);
46 static void m2_array (struct type
*, struct ui_file
*, int, int);
47 static void m2_pointer (struct type
*, struct ui_file
*, int, int);
48 static void m2_ref (struct type
*, struct ui_file
*, int, int);
49 static void m2_procedure (struct type
*, struct ui_file
*, int, int);
50 static void m2_union (struct type
*, struct ui_file
*);
51 static void m2_enum (struct type
*, struct ui_file
*, int, int);
52 static void m2_range (struct type
*, struct ui_file
*, int, int);
53 static void m2_type_name (struct type
*type
, struct ui_file
*stream
);
54 static void m2_short_set (struct type
*type
, struct ui_file
*stream
,
56 static int m2_long_set (struct type
*type
, struct ui_file
*stream
,
58 static void m2_record_fields (struct type
*type
, struct ui_file
*stream
,
60 static void m2_unknown (const char *s
, struct type
*type
,
61 struct ui_file
*stream
, int show
, int level
);
63 int m2_is_long_set (struct type
*type
);
64 int m2_is_long_set_of_type (struct type
*type
, struct type
**of_type
);
68 m2_print_type (struct type
*type
, char *varstring
, struct ui_file
*stream
,
81 fputs_filtered (_("<type unknown>"), stream
);
85 code
= TYPE_CODE (type
);
86 switch (TYPE_CODE (type
))
89 m2_short_set(type
, stream
, show
, level
);
92 case TYPE_CODE_STRUCT
:
93 if (m2_long_set (type
, stream
, show
, level
))
95 m2_record_fields (type
, stream
, show
, level
);
98 case TYPE_CODE_TYPEDEF
:
99 m2_typedef (type
, stream
, show
, level
);
102 case TYPE_CODE_ARRAY
:
103 m2_array (type
, stream
, show
, level
);
107 m2_pointer (type
, stream
, show
, level
);
111 m2_ref (type
, stream
, show
, level
);
114 case TYPE_CODE_METHOD
:
115 m2_unknown (_("method"), type
, stream
, show
, level
);
119 m2_procedure (type
, stream
, show
, level
);
122 case TYPE_CODE_UNION
:
123 m2_union (type
, stream
);
127 m2_enum (type
, stream
, show
, level
);
133 case TYPE_CODE_UNDEF
:
134 /* i18n: Do not translate the "struct" part! */
135 m2_unknown (_("undef"), type
, stream
, show
, level
);
138 case TYPE_CODE_ERROR
:
139 m2_unknown (_("error"), type
, stream
, show
, level
);
142 case TYPE_CODE_RANGE
:
143 m2_range (type
, stream
, show
, level
);
146 case TYPE_CODE_TEMPLATE
:
150 m2_type_name (type
, stream
);
156 * m2_type_name - if a, type, has a name then print it.
160 m2_type_name (struct type
*type
, struct ui_file
*stream
)
162 if (TYPE_NAME (type
) != NULL
)
163 fputs_filtered (TYPE_NAME (type
), stream
);
167 * m2_range - displays a Modula-2 subrange type.
171 m2_range (struct type
*type
, struct ui_file
*stream
, int show
,
174 if (TYPE_HIGH_BOUND (type
) == TYPE_LOW_BOUND (type
))
175 m2_print_type (TYPE_DOMAIN_TYPE (type
), "", stream
, show
, level
);
178 struct type
*target
= TYPE_TARGET_TYPE (type
);
180 fprintf_filtered (stream
, "[");
181 print_type_scalar (target
, TYPE_LOW_BOUND (type
), stream
);
182 fprintf_filtered (stream
, "..");
183 print_type_scalar (target
, TYPE_HIGH_BOUND (type
), stream
);
184 fprintf_filtered (stream
, "]");
189 m2_typedef (struct type
*type
, struct ui_file
*stream
, int show
,
192 if (TYPE_NAME (type
) != NULL
)
194 fputs_filtered (TYPE_NAME (type
), stream
);
195 fputs_filtered (" = ", stream
);
197 m2_print_type (TYPE_TARGET_TYPE (type
), "", stream
, show
, level
);
201 * m2_array - prints out a Modula-2 ARRAY ... OF type
204 static void m2_array (struct type
*type
, struct ui_file
*stream
,
207 fprintf_filtered (stream
, "ARRAY [");
208 if (TYPE_LENGTH (TYPE_TARGET_TYPE (type
)) > 0
209 && TYPE_ARRAY_UPPER_BOUND_TYPE (type
) != BOUND_CANNOT_BE_DETERMINED
)
211 if (TYPE_INDEX_TYPE (type
) != 0)
213 m2_print_bounds (TYPE_INDEX_TYPE (type
), stream
, show
, -1, 0);
214 fprintf_filtered (stream
, "..");
215 m2_print_bounds (TYPE_INDEX_TYPE (type
), stream
, show
, -1, 1);
218 fprintf_filtered (stream
, "%d",
220 / TYPE_LENGTH (TYPE_TARGET_TYPE (type
))));
222 fprintf_filtered (stream
, "] OF ");
223 m2_print_type (TYPE_TARGET_TYPE (type
), "", stream
, show
, level
);
227 m2_pointer (struct type
*type
, struct ui_file
*stream
, int show
,
230 if (TYPE_CONST (type
))
231 fprintf_filtered (stream
, "[...] : ");
233 fprintf_filtered (stream
, "POINTER TO ");
235 m2_print_type (TYPE_TARGET_TYPE (type
), "", stream
, show
, level
);
239 m2_ref (struct type
*type
, struct ui_file
*stream
, int show
,
242 fprintf_filtered (stream
, "VAR");
243 m2_print_type (TYPE_TARGET_TYPE (type
), "", stream
, show
, level
);
247 m2_unknown (const char *s
, struct type
*type
, struct ui_file
*stream
,
250 fprintf_filtered (stream
, "%s %s", s
, _("is unknown"));
253 static void m2_union (struct type
*type
, struct ui_file
*stream
)
255 fprintf_filtered (stream
, "union");
259 m2_procedure (struct type
*type
, struct ui_file
*stream
,
262 fprintf_filtered (stream
, "PROCEDURE ");
263 m2_type_name (type
, stream
);
264 if (TYPE_CODE (TYPE_TARGET_TYPE (type
)) != TYPE_CODE_VOID
)
266 int i
, len
= TYPE_NFIELDS (type
);
268 fprintf_filtered (stream
, " (");
269 for (i
= 0; i
< len
; i
++)
273 fputs_filtered (", ", stream
);
276 m2_print_type (TYPE_FIELD_TYPE (type
, i
), "", stream
, -1, 0);
278 if (TYPE_TARGET_TYPE (type
) != NULL
)
280 fprintf_filtered (stream
, " : ");
281 m2_print_type (TYPE_TARGET_TYPE (type
), "", stream
, 0, 0);
287 m2_print_bounds (struct type
*type
,
288 struct ui_file
*stream
, int show
, int level
,
291 struct type
*target
= TYPE_TARGET_TYPE (type
);
294 target
= builtin_type_int
;
296 if (TYPE_NFIELDS(type
) == 0)
300 print_type_scalar (target
, TYPE_HIGH_BOUND (type
), stream
);
302 print_type_scalar (target
, TYPE_LOW_BOUND (type
), stream
);
306 m2_short_set (struct type
*type
, struct ui_file
*stream
, int show
, int level
)
308 fprintf_filtered(stream
, "SET [");
309 m2_print_bounds (TYPE_INDEX_TYPE (type
), stream
,
312 fprintf_filtered(stream
, "..");
313 m2_print_bounds (TYPE_INDEX_TYPE (type
), stream
,
315 fprintf_filtered(stream
, "]");
319 m2_is_long_set (struct type
*type
)
321 LONGEST previous_high
= 0; /* unnecessary initialization
322 keeps gcc -Wall happy */
326 if (TYPE_CODE (type
) == TYPE_CODE_STRUCT
)
330 * check if all fields of the RECORD are consecutive sets
332 len
= TYPE_NFIELDS (type
);
333 for (i
= TYPE_N_BASECLASSES (type
); i
< len
; i
++)
335 if (TYPE_FIELD_TYPE (type
, i
) == NULL
)
337 if (TYPE_CODE (TYPE_FIELD_TYPE (type
, i
)) != TYPE_CODE_SET
)
339 if (TYPE_FIELD_NAME (type
, i
) != NULL
340 && (strcmp (TYPE_FIELD_NAME (type
, i
), "") != 0))
342 range
= TYPE_INDEX_TYPE (TYPE_FIELD_TYPE (type
, i
));
343 if ((i
> TYPE_N_BASECLASSES (type
))
344 && previous_high
+ 1 != TYPE_LOW_BOUND (range
))
346 previous_high
= TYPE_HIGH_BOUND (range
);
354 * m2_get_discrete_bounds - a wrapper for get_discrete_bounds which
355 * understands that CHARs might be signed.
356 * This should be integrated into gdbtypes.c
357 * inside get_discrete_bounds.
361 m2_get_discrete_bounds (struct type
*type
, LONGEST
*lowp
, LONGEST
*highp
)
363 CHECK_TYPEDEF (type
);
364 switch (TYPE_CODE (type
))
367 if (TYPE_LENGTH (type
) < sizeof (LONGEST
))
369 if (!TYPE_UNSIGNED (type
))
371 *lowp
= -(1 << (TYPE_LENGTH (type
) * TARGET_CHAR_BIT
- 1));
378 return get_discrete_bounds (type
, lowp
, highp
);
383 * m2_is_long_set_of_type - returns TRUE if the long set was declared as
384 * SET OF <oftype> of_type is assigned to the
389 m2_is_long_set_of_type (struct type
*type
, struct type
**of_type
)
397 if (TYPE_CODE (type
) == TYPE_CODE_STRUCT
)
399 len
= TYPE_NFIELDS (type
);
400 i
= TYPE_N_BASECLASSES (type
);
403 range
= TYPE_INDEX_TYPE (TYPE_FIELD_TYPE (type
, i
));
404 target
= TYPE_TARGET_TYPE (range
);
406 target
= builtin_type_int
;
408 l1
= TYPE_LOW_BOUND (TYPE_INDEX_TYPE (TYPE_FIELD_TYPE (type
, i
)));
409 h1
= TYPE_HIGH_BOUND (TYPE_INDEX_TYPE (TYPE_FIELD_TYPE (type
, len
-1)));
411 if (m2_get_discrete_bounds (target
, &l2
, &h2
) >= 0)
412 return (l1
== l2
&& h1
== h2
);
413 error (_("long_set failed to find discrete bounds for its subtype"));
416 error (_("expecting long_set"));
421 m2_long_set (struct type
*type
, struct ui_file
*stream
, int show
, int level
)
423 struct type
*index_type
;
424 struct type
*range_type
;
425 struct type
*of_type
;
427 int len
= TYPE_NFIELDS (type
);
431 if (m2_is_long_set (type
))
433 if (TYPE_TAG_NAME (type
) != NULL
)
435 fputs_filtered (TYPE_TAG_NAME (type
), stream
);
439 else if (TYPE_NAME (type
) != NULL
)
441 fputs_filtered (TYPE_NAME (type
), stream
);
446 if (TYPE_TAG_NAME (type
) != NULL
|| TYPE_NAME (type
) != NULL
)
447 fputs_filtered (" = ", stream
);
449 if (get_long_set_bounds (type
, &low
, &high
))
451 fprintf_filtered(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
);
457 fprintf_filtered(stream
, "[");
458 m2_print_bounds (TYPE_INDEX_TYPE (TYPE_FIELD_TYPE (type
, i
)),
459 stream
, show
- 1, level
, 0);
461 fprintf_filtered(stream
, "..");
463 m2_print_bounds (TYPE_INDEX_TYPE (TYPE_FIELD_TYPE (type
, len
-1)),
464 stream
, show
- 1, level
, 1);
465 fprintf_filtered(stream
, "]");
469 /* i18n: Do not translate the "SET OF" part! */
470 fprintf_filtered(stream
, _("SET OF <unknown>"));
478 m2_record_fields (struct type
*type
, struct ui_file
*stream
, int show
,
481 /* Print the tag if it exists.
483 if (TYPE_TAG_NAME (type
) != NULL
)
485 if (strncmp (TYPE_TAG_NAME (type
), "$$", 2) != 0)
487 fputs_filtered (TYPE_TAG_NAME (type
), stream
);
489 fprintf_filtered (stream
, " = ");
495 if (TYPE_CODE (type
) == DECLARED_TYPE_STRUCT
)
496 fprintf_filtered (stream
, "RECORD ... END ");
497 else if (TYPE_DECLARED_TYPE (type
) == DECLARED_TYPE_UNION
)
498 fprintf_filtered (stream
, "CASE ... END ");
503 int len
= TYPE_NFIELDS (type
);
505 if (TYPE_CODE (type
) == TYPE_CODE_STRUCT
)
506 fprintf_filtered (stream
, "RECORD\n");
507 else if (TYPE_CODE (type
) == TYPE_CODE_UNION
)
508 /* i18n: Do not translate "CASE" and "OF" */
509 fprintf_filtered (stream
, _("CASE <variant> OF\n"));
511 for (i
= TYPE_N_BASECLASSES (type
); i
< len
; i
++)
515 print_spaces_filtered (level
+ 4, stream
);
516 fputs_filtered (TYPE_FIELD_NAME (type
, i
), stream
);
517 fputs_filtered (" : ", stream
);
518 m2_print_type (TYPE_FIELD_TYPE (type
, i
),
520 stream
, 0, level
+ 4);
521 if (TYPE_FIELD_PACKED (type
, i
))
523 /* It is a bitfield. This code does not attempt
524 to look at the bitpos and reconstruct filler,
525 unnamed fields. This would lead to misleading
526 results if the compiler does not put out fields
527 for such things (I don't know what it does). */
528 fprintf_filtered (stream
, " : %d",
529 TYPE_FIELD_BITSIZE (type
, i
));
531 fprintf_filtered (stream
, ";\n");
534 fprintfi_filtered (level
, stream
, "END ");
539 m2_enum (struct type
*type
, struct ui_file
*stream
, int show
, int level
)
545 /* If we just printed a tag name, no need to print anything else. */
546 if (TYPE_TAG_NAME (type
) == NULL
)
547 fprintf_filtered (stream
, "(...)");
549 else if (show
> 0 || TYPE_TAG_NAME (type
) == NULL
)
551 fprintf_filtered (stream
, "(");
552 len
= TYPE_NFIELDS (type
);
554 for (i
= 0; i
< len
; i
++)
558 fprintf_filtered (stream
, ", ");
560 fputs_filtered (TYPE_FIELD_NAME (type
, i
), stream
);
561 if (lastval
!= TYPE_FIELD_BITPOS (type
, i
))
563 fprintf_filtered (stream
, " = %d", TYPE_FIELD_BITPOS (type
, i
));
564 lastval
= TYPE_FIELD_BITPOS (type
, i
);
568 fprintf_filtered (stream
, ")");