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 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 static void m2_array (struct type *, struct ui_file *, int, int);
45 static void m2_pointer (struct type *, struct ui_file *, int, int);
46 static void m2_ref (struct type *, struct ui_file *, int, int);
47 static void m2_procedure (struct type *, struct ui_file *, int, int);
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 static void m2_type_name (struct type *type, struct ui_file *stream);
52 static void m2_short_set (struct type *type, struct ui_file *stream,
54 static int m2_long_set (struct type *type, struct ui_file *stream,
56 static void m2_record_fields (struct type *type, struct ui_file *stream,
58 static void m2_unknown (const char *s, struct type *type,
59 struct ui_file *stream, int show, int level);
61 int m2_is_long_set (struct type *type);
62 int m2_is_long_set_of_type (struct type *type, struct type **of_type);
66 m2_print_type (struct type *type, char *varstring, struct ui_file *stream,
79 fputs_filtered (_("<type unknown>"), stream);
83 code = TYPE_CODE (type);
84 switch (TYPE_CODE (type))
87 m2_short_set(type, stream, show, level);
90 case TYPE_CODE_STRUCT:
91 if (m2_long_set (type, stream, show, level))
93 m2_record_fields (type, stream, show, level);
96 case TYPE_CODE_TYPEDEF:
97 m2_typedef (type, stream, show, level);
100 case TYPE_CODE_ARRAY:
101 m2_array (type, stream, show, level);
105 m2_pointer (type, stream, show, level);
109 m2_ref (type, stream, show, level);
112 case TYPE_CODE_METHOD:
113 m2_unknown (_("method"), type, stream, show, level);
117 m2_procedure (type, stream, show, level);
120 case TYPE_CODE_UNION:
121 m2_union (type, stream);
125 m2_enum (type, stream, show, level);
131 case TYPE_CODE_UNDEF:
132 /* i18n: Do not translate the "struct" part! */
133 m2_unknown (_("undef"), type, stream, show, level);
136 case TYPE_CODE_ERROR:
137 m2_unknown (_("error"), type, stream, show, level);
140 case TYPE_CODE_RANGE:
141 m2_range (type, stream, show, level);
144 case TYPE_CODE_TEMPLATE:
148 m2_type_name (type, stream);
154 * m2_type_name - if a, type, has a name then print it.
158 m2_type_name (struct type *type, struct ui_file *stream)
160 if (TYPE_NAME (type) != NULL)
161 fputs_filtered (TYPE_NAME (type), stream);
165 * m2_range - displays a Modula-2 subrange type.
169 m2_range (struct type *type, struct ui_file *stream, int show,
172 if (TYPE_HIGH_BOUND (type) == TYPE_LOW_BOUND (type))
173 m2_print_type (TYPE_DOMAIN_TYPE (type), "", stream, show, level);
176 struct type *target = TYPE_TARGET_TYPE (type);
178 fprintf_filtered (stream, "[");
179 print_type_scalar (target, TYPE_LOW_BOUND (type), stream);
180 fprintf_filtered (stream, "..");
181 print_type_scalar (target, TYPE_HIGH_BOUND (type), stream);
182 fprintf_filtered (stream, "]");
187 m2_typedef (struct type *type, struct ui_file *stream, int show,
190 if (TYPE_NAME (type) != NULL)
192 fputs_filtered (TYPE_NAME (type), stream);
193 fputs_filtered (" = ", stream);
195 m2_print_type (TYPE_TARGET_TYPE (type), "", stream, show, level);
199 * m2_array - prints out a Modula-2 ARRAY ... OF type
202 static void m2_array (struct type *type, struct ui_file *stream,
205 fprintf_filtered (stream, "ARRAY [");
206 if (TYPE_LENGTH (TYPE_TARGET_TYPE (type)) > 0
207 && TYPE_ARRAY_UPPER_BOUND_TYPE (type) != BOUND_CANNOT_BE_DETERMINED)
209 if (TYPE_INDEX_TYPE (type) != 0)
211 m2_print_bounds (TYPE_INDEX_TYPE (type), stream, show, -1, 0);
212 fprintf_filtered (stream, "..");
213 m2_print_bounds (TYPE_INDEX_TYPE (type), stream, show, -1, 1);
216 fprintf_filtered (stream, "%d",
218 / TYPE_LENGTH (TYPE_TARGET_TYPE (type))));
220 fprintf_filtered (stream, "] OF ");
221 m2_print_type (TYPE_TARGET_TYPE (type), "", stream, show, level);
225 m2_pointer (struct type *type, struct ui_file *stream, int show,
228 if (TYPE_CONST (type))
229 fprintf_filtered (stream, "[...] : ");
231 fprintf_filtered (stream, "POINTER TO ");
233 m2_print_type (TYPE_TARGET_TYPE (type), "", stream, show, level);
237 m2_ref (struct type *type, struct ui_file *stream, int show,
240 fprintf_filtered (stream, "VAR");
241 m2_print_type (TYPE_TARGET_TYPE (type), "", stream, show, level);
245 m2_unknown (const char *s, struct type *type, struct ui_file *stream,
248 fprintf_filtered (stream, "%s %s", s, _("is unknown"));
251 static void m2_union (struct type *type, struct ui_file *stream)
253 fprintf_filtered (stream, "union");
257 m2_procedure (struct type *type, struct ui_file *stream,
260 fprintf_filtered (stream, "PROCEDURE ");
261 m2_type_name (type, stream);
262 if (TYPE_CODE (TYPE_TARGET_TYPE (type)) != TYPE_CODE_VOID)
264 int i, len = TYPE_NFIELDS (type);
266 fprintf_filtered (stream, " (");
267 for (i = 0; i < len; i++)
271 fputs_filtered (", ", stream);
274 m2_print_type (TYPE_FIELD_TYPE (type, i), "", stream, -1, 0);
276 if (TYPE_TARGET_TYPE (type) != NULL)
278 fprintf_filtered (stream, " : ");
279 m2_print_type (TYPE_TARGET_TYPE (type), "", stream, 0, 0);
285 m2_print_bounds (struct type *type,
286 struct ui_file *stream, int show, int level,
289 struct type *target = TYPE_TARGET_TYPE (type);
292 target = builtin_type_int;
294 if (TYPE_NFIELDS(type) == 0)
298 print_type_scalar (target, TYPE_HIGH_BOUND (type), stream);
300 print_type_scalar (target, TYPE_LOW_BOUND (type), stream);
304 m2_short_set (struct type *type, struct ui_file *stream, int show, int level)
306 fprintf_filtered(stream, "SET [");
307 m2_print_bounds (TYPE_INDEX_TYPE (type), stream,
310 fprintf_filtered(stream, "..");
311 m2_print_bounds (TYPE_INDEX_TYPE (type), stream,
313 fprintf_filtered(stream, "]");
317 m2_is_long_set (struct type *type)
319 LONGEST previous_high = 0; /* unnecessary initialization
320 keeps gcc -Wall happy */
324 if (TYPE_CODE (type) == TYPE_CODE_STRUCT)
328 * check if all fields of the RECORD are consecutive sets
330 len = TYPE_NFIELDS (type);
331 for (i = TYPE_N_BASECLASSES (type); i < len; i++)
333 if (TYPE_FIELD_TYPE (type, i) == NULL)
335 if (TYPE_CODE (TYPE_FIELD_TYPE (type, i)) != TYPE_CODE_SET)
337 if (TYPE_FIELD_NAME (type, i) != NULL
338 && (strcmp (TYPE_FIELD_NAME (type, i), "") != 0))
340 range = TYPE_INDEX_TYPE (TYPE_FIELD_TYPE (type, i));
341 if ((i > TYPE_N_BASECLASSES (type))
342 && previous_high + 1 != TYPE_LOW_BOUND (range))
344 previous_high = TYPE_HIGH_BOUND (range);
352 * m2_get_discrete_bounds - a wrapper for get_discrete_bounds which
353 * understands that CHARs might be signed.
354 * This should be integrated into gdbtypes.c
355 * inside get_discrete_bounds.
359 m2_get_discrete_bounds (struct type *type, LONGEST *lowp, LONGEST *highp)
361 CHECK_TYPEDEF (type);
362 switch (TYPE_CODE (type))
365 if (TYPE_LENGTH (type) < sizeof (LONGEST))
367 if (!TYPE_UNSIGNED (type))
369 *lowp = -(1 << (TYPE_LENGTH (type) * TARGET_CHAR_BIT - 1));
376 return get_discrete_bounds (type, lowp, highp);
381 * m2_is_long_set_of_type - returns TRUE if the long set was declared as
382 * SET OF <oftype> of_type is assigned to the
387 m2_is_long_set_of_type (struct type *type, struct type **of_type)
395 if (TYPE_CODE (type) == TYPE_CODE_STRUCT)
397 len = TYPE_NFIELDS (type);
398 i = TYPE_N_BASECLASSES (type);
401 range = TYPE_INDEX_TYPE (TYPE_FIELD_TYPE (type, i));
402 target = TYPE_TARGET_TYPE (range);
404 target = builtin_type_int;
406 l1 = TYPE_LOW_BOUND (TYPE_INDEX_TYPE (TYPE_FIELD_TYPE (type, i)));
407 h1 = TYPE_HIGH_BOUND (TYPE_INDEX_TYPE (TYPE_FIELD_TYPE (type, len-1)));
409 if (m2_get_discrete_bounds (target, &l2, &h2) >= 0)
410 return (l1 == l2 && h1 == h2);
411 error (_("long_set failed to find discrete bounds for its subtype"));
414 error (_("expecting long_set"));
419 m2_long_set (struct type *type, struct ui_file *stream, int show, int level)
421 struct type *index_type;
422 struct type *range_type;
423 struct type *of_type;
425 int len = TYPE_NFIELDS (type);
429 if (m2_is_long_set (type))
431 if (TYPE_TAG_NAME (type) != NULL)
433 fputs_filtered (TYPE_TAG_NAME (type), stream);
437 else if (TYPE_NAME (type) != NULL)
439 fputs_filtered (TYPE_NAME (type), stream);
444 if (TYPE_TAG_NAME (type) != NULL || TYPE_NAME (type) != NULL)
445 fputs_filtered (" = ", stream);
447 if (get_long_set_bounds (type, &low, &high))
449 fprintf_filtered(stream, "SET OF ");
450 i = TYPE_N_BASECLASSES (type);
451 if (m2_is_long_set_of_type (type, &of_type))
452 m2_print_type (of_type, "", stream, show - 1, level);
455 fprintf_filtered(stream, "[");
456 m2_print_bounds (TYPE_INDEX_TYPE (TYPE_FIELD_TYPE (type, i)),
457 stream, show - 1, level, 0);
459 fprintf_filtered(stream, "..");
461 m2_print_bounds (TYPE_INDEX_TYPE (TYPE_FIELD_TYPE (type, len-1)),
462 stream, show - 1, level, 1);
463 fprintf_filtered(stream, "]");
467 /* i18n: Do not translate the "SET OF" part! */
468 fprintf_filtered(stream, _("SET OF <unknown>"));
476 m2_record_fields (struct type *type, struct ui_file *stream, int show,
479 /* Print the tag if it exists.
481 if (TYPE_TAG_NAME (type) != NULL)
483 if (strncmp (TYPE_TAG_NAME (type), "$$", 2) != 0)
485 fputs_filtered (TYPE_TAG_NAME (type), stream);
487 fprintf_filtered (stream, " = ");
493 if (TYPE_CODE (type) == DECLARED_TYPE_STRUCT)
494 fprintf_filtered (stream, "RECORD ... END ");
495 else if (TYPE_DECLARED_TYPE (type) == DECLARED_TYPE_UNION)
496 fprintf_filtered (stream, "CASE ... END ");
501 int len = TYPE_NFIELDS (type);
503 if (TYPE_CODE (type) == TYPE_CODE_STRUCT)
504 fprintf_filtered (stream, "RECORD\n");
505 else if (TYPE_CODE (type) == TYPE_CODE_UNION)
506 /* i18n: Do not translate "CASE" and "OF" */
507 fprintf_filtered (stream, _("CASE <variant> OF\n"));
509 for (i = TYPE_N_BASECLASSES (type); i < len; i++)
513 print_spaces_filtered (level + 4, stream);
514 fputs_filtered (TYPE_FIELD_NAME (type, i), stream);
515 fputs_filtered (" : ", stream);
516 m2_print_type (TYPE_FIELD_TYPE (type, i),
518 stream, 0, level + 4);
519 if (TYPE_FIELD_PACKED (type, i))
521 /* It is a bitfield. This code does not attempt
522 to look at the bitpos and reconstruct filler,
523 unnamed fields. This would lead to misleading
524 results if the compiler does not put out fields
525 for such things (I don't know what it does). */
526 fprintf_filtered (stream, " : %d",
527 TYPE_FIELD_BITSIZE (type, i));
529 fprintf_filtered (stream, ";\n");
532 fprintfi_filtered (level, stream, "END ");
537 m2_enum (struct type *type, struct ui_file *stream, int show, int level)
543 /* If we just printed a tag name, no need to print anything else. */
544 if (TYPE_TAG_NAME (type) == NULL)
545 fprintf_filtered (stream, "(...)");
547 else if (show > 0 || TYPE_TAG_NAME (type) == NULL)
549 fprintf_filtered (stream, "(");
550 len = TYPE_NFIELDS (type);
552 for (i = 0; i < len; i++)
556 fprintf_filtered (stream, ", ");
558 fputs_filtered (TYPE_FIELD_NAME (type, i), stream);
559 if (lastval != TYPE_FIELD_BITPOS (type, i))
561 fprintf_filtered (stream, " = %d", TYPE_FIELD_BITPOS (type, i));
562 lastval = TYPE_FIELD_BITPOS (type, i);
566 fprintf_filtered (stream, ")");