1 /* Support for printing Modula 2 types for GDB, the GNU debugger.
2 Copyright (C) 1986-2013 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/>. */
20 #include "gdb_obstack.h"
21 #include "bfd.h" /* Binary File Description */
24 #include "expression.h"
32 #include "typeprint.h"
35 #include "gdb_string.h"
38 static void m2_print_bounds (struct type
*type
,
39 struct ui_file
*stream
, int show
, int level
,
42 static void m2_typedef (struct type
*, struct ui_file
*, int, int,
43 const struct type_print_options
*);
44 static void m2_array (struct type
*, struct ui_file
*, int, int,
45 const struct type_print_options
*);
46 static void m2_pointer (struct type
*, struct ui_file
*, int, int,
47 const struct type_print_options
*);
48 static void m2_ref (struct type
*, struct ui_file
*, int, int,
49 const struct type_print_options
*);
50 static void m2_procedure (struct type
*, struct ui_file
*, int, int,
51 const struct type_print_options
*);
52 static void m2_union (struct type
*, struct ui_file
*);
53 static void m2_enum (struct type
*, struct ui_file
*, int, int);
54 static void m2_range (struct type
*, struct ui_file
*, int, int,
55 const struct type_print_options
*);
56 static void m2_type_name (struct type
*type
, struct ui_file
*stream
);
57 static void m2_short_set (struct type
*type
, struct ui_file
*stream
,
59 static int m2_long_set (struct type
*type
, struct ui_file
*stream
,
60 int show
, int level
, const struct type_print_options
*flags
);
61 static int m2_unbounded_array (struct type
*type
, struct ui_file
*stream
,
63 const struct type_print_options
*flags
);
64 static void m2_record_fields (struct type
*type
, struct ui_file
*stream
,
65 int show
, int level
, const struct type_print_options
*flags
);
66 static void m2_unknown (const char *s
, struct type
*type
,
67 struct ui_file
*stream
, int show
, int level
);
69 int m2_is_long_set (struct type
*type
);
70 int m2_is_long_set_of_type (struct type
*type
, struct type
**of_type
);
71 int m2_is_unbounded_array (struct type
*type
);
75 m2_print_type (struct type
*type
, const char *varstring
,
76 struct ui_file
*stream
,
78 const struct type_print_options
*flags
)
89 fputs_filtered (_("<type unknown>"), stream
);
93 code
= TYPE_CODE (type
);
94 switch (TYPE_CODE (type
))
97 m2_short_set(type
, stream
, show
, level
);
100 case TYPE_CODE_STRUCT
:
101 if (m2_long_set (type
, stream
, show
, level
, flags
)
102 || m2_unbounded_array (type
, stream
, show
, level
, flags
))
104 m2_record_fields (type
, stream
, show
, level
, flags
);
107 case TYPE_CODE_TYPEDEF
:
108 m2_typedef (type
, stream
, show
, level
, flags
);
111 case TYPE_CODE_ARRAY
:
112 m2_array (type
, stream
, show
, level
, flags
);
116 m2_pointer (type
, stream
, show
, level
, flags
);
120 m2_ref (type
, stream
, show
, level
, flags
);
123 case TYPE_CODE_METHOD
:
124 m2_unknown (_("method"), type
, stream
, show
, level
);
128 m2_procedure (type
, stream
, show
, level
, flags
);
131 case TYPE_CODE_UNION
:
132 m2_union (type
, stream
);
136 m2_enum (type
, stream
, show
, level
);
142 case TYPE_CODE_UNDEF
:
143 /* i18n: Do not translate the "struct" part! */
144 m2_unknown (_("undef"), type
, stream
, show
, level
);
147 case TYPE_CODE_ERROR
:
148 m2_unknown (_("error"), type
, stream
, show
, level
);
151 case TYPE_CODE_RANGE
:
152 m2_range (type
, stream
, show
, level
, flags
);
156 m2_type_name (type
, stream
);
161 /* Print a typedef using M2 syntax. TYPE is the underlying type.
162 NEW_SYMBOL is the symbol naming the type. STREAM is the stream on
166 m2_print_typedef (struct type
*type
, struct symbol
*new_symbol
,
167 struct ui_file
*stream
)
169 CHECK_TYPEDEF (type
);
170 fprintf_filtered (stream
, "TYPE ");
171 if (!TYPE_NAME (SYMBOL_TYPE (new_symbol
))
172 || strcmp (TYPE_NAME ((SYMBOL_TYPE (new_symbol
))),
173 SYMBOL_LINKAGE_NAME (new_symbol
)) != 0)
174 fprintf_filtered (stream
, "%s = ", SYMBOL_PRINT_NAME (new_symbol
));
176 fprintf_filtered (stream
, "<builtin> = ");
177 type_print (type
, "", stream
, 0);
178 fprintf_filtered (stream
, ";\n");
181 /* m2_type_name - if a, type, has a name then print it. */
184 m2_type_name (struct type
*type
, struct ui_file
*stream
)
186 if (TYPE_NAME (type
) != NULL
)
187 fputs_filtered (TYPE_NAME (type
), stream
);
190 /* m2_range - displays a Modula-2 subrange type. */
193 m2_range (struct type
*type
, struct ui_file
*stream
, int show
,
194 int level
, const struct type_print_options
*flags
)
196 if (TYPE_HIGH_BOUND (type
) == TYPE_LOW_BOUND (type
))
197 m2_print_type (TYPE_DOMAIN_TYPE (type
), "", stream
, show
, level
,
201 struct type
*target
= TYPE_TARGET_TYPE (type
);
203 fprintf_filtered (stream
, "[");
204 print_type_scalar (target
, TYPE_LOW_BOUND (type
), stream
);
205 fprintf_filtered (stream
, "..");
206 print_type_scalar (target
, TYPE_HIGH_BOUND (type
), stream
);
207 fprintf_filtered (stream
, "]");
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 (type
) != NULL
)
217 fputs_filtered (TYPE_NAME (type
), stream
);
218 fputs_filtered (" = ", stream
);
220 m2_print_type (TYPE_TARGET_TYPE (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 fprintf_filtered (stream
, "ARRAY [");
229 if (TYPE_LENGTH (TYPE_TARGET_TYPE (type
)) > 0
230 && !TYPE_ARRAY_UPPER_BOUND_IS_UNDEFINED (type
))
232 if (TYPE_INDEX_TYPE (type
) != 0)
234 m2_print_bounds (TYPE_INDEX_TYPE (type
), stream
, show
, -1, 0);
235 fprintf_filtered (stream
, "..");
236 m2_print_bounds (TYPE_INDEX_TYPE (type
), stream
, show
, -1, 1);
239 fprintf_filtered (stream
, "%d",
241 / TYPE_LENGTH (TYPE_TARGET_TYPE (type
))));
243 fprintf_filtered (stream
, "] OF ");
244 m2_print_type (TYPE_TARGET_TYPE (type
), "", stream
, show
, level
, flags
);
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 fprintf_filtered (stream
, "[...] : ");
254 fprintf_filtered (stream
, "POINTER TO ");
256 m2_print_type (TYPE_TARGET_TYPE (type
), "", stream
, show
, level
, flags
);
260 m2_ref (struct type
*type
, struct ui_file
*stream
, int show
,
261 int level
, const struct type_print_options
*flags
)
263 fprintf_filtered (stream
, "VAR");
264 m2_print_type (TYPE_TARGET_TYPE (type
), "", stream
, show
, level
, flags
);
268 m2_unknown (const char *s
, struct type
*type
, struct ui_file
*stream
,
271 fprintf_filtered (stream
, "%s %s", s
, _("is unknown"));
274 static void m2_union (struct type
*type
, struct ui_file
*stream
)
276 fprintf_filtered (stream
, "union");
280 m2_procedure (struct type
*type
, struct ui_file
*stream
,
281 int show
, int level
, const struct type_print_options
*flags
)
283 fprintf_filtered (stream
, "PROCEDURE ");
284 m2_type_name (type
, stream
);
285 if (TYPE_CODE (TYPE_TARGET_TYPE (type
)) != TYPE_CODE_VOID
)
287 int i
, len
= TYPE_NFIELDS (type
);
289 fprintf_filtered (stream
, " (");
290 for (i
= 0; i
< len
; i
++)
294 fputs_filtered (", ", stream
);
297 m2_print_type (TYPE_FIELD_TYPE (type
, i
), "", stream
, -1, 0, flags
);
299 if (TYPE_TARGET_TYPE (type
) != NULL
)
301 fprintf_filtered (stream
, " : ");
302 m2_print_type (TYPE_TARGET_TYPE (type
), "", stream
, 0, 0, flags
);
308 m2_print_bounds (struct type
*type
,
309 struct ui_file
*stream
, int show
, int level
,
312 struct type
*target
= TYPE_TARGET_TYPE (type
);
314 if (TYPE_NFIELDS(type
) == 0)
318 print_type_scalar (target
, TYPE_HIGH_BOUND (type
), stream
);
320 print_type_scalar (target
, TYPE_LOW_BOUND (type
), stream
);
324 m2_short_set (struct type
*type
, struct ui_file
*stream
, int show
, int level
)
326 fprintf_filtered(stream
, "SET [");
327 m2_print_bounds (TYPE_INDEX_TYPE (type
), stream
,
330 fprintf_filtered(stream
, "..");
331 m2_print_bounds (TYPE_INDEX_TYPE (type
), stream
,
333 fprintf_filtered(stream
, "]");
337 m2_is_long_set (struct type
*type
)
339 LONGEST previous_high
= 0; /* Unnecessary initialization
340 keeps gcc -Wall happy. */
344 if (TYPE_CODE (type
) == TYPE_CODE_STRUCT
)
347 /* check if all fields of the RECORD are consecutive sets. */
349 len
= TYPE_NFIELDS (type
);
350 for (i
= TYPE_N_BASECLASSES (type
); i
< len
; i
++)
352 if (TYPE_FIELD_TYPE (type
, i
) == NULL
)
354 if (TYPE_CODE (TYPE_FIELD_TYPE (type
, i
)) != TYPE_CODE_SET
)
356 if (TYPE_FIELD_NAME (type
, i
) != NULL
357 && (strcmp (TYPE_FIELD_NAME (type
, i
), "") != 0))
359 range
= TYPE_INDEX_TYPE (TYPE_FIELD_TYPE (type
, i
));
360 if ((i
> TYPE_N_BASECLASSES (type
))
361 && previous_high
+ 1 != TYPE_LOW_BOUND (range
))
363 previous_high
= TYPE_HIGH_BOUND (range
);
370 /* m2_get_discrete_bounds - a wrapper for get_discrete_bounds which
371 understands that CHARs might be signed.
372 This should be integrated into gdbtypes.c
373 inside get_discrete_bounds. */
376 m2_get_discrete_bounds (struct type
*type
, LONGEST
*lowp
, LONGEST
*highp
)
378 CHECK_TYPEDEF (type
);
379 switch (TYPE_CODE (type
))
382 if (TYPE_LENGTH (type
) < sizeof (LONGEST
))
384 if (!TYPE_UNSIGNED (type
))
386 *lowp
= -(1 << (TYPE_LENGTH (type
) * TARGET_CHAR_BIT
- 1));
393 return get_discrete_bounds (type
, lowp
, highp
);
397 /* m2_is_long_set_of_type - returns TRUE if the long set was declared as
398 SET OF <oftype> of_type is assigned to the
402 m2_is_long_set_of_type (struct type
*type
, struct type
**of_type
)
410 if (TYPE_CODE (type
) == TYPE_CODE_STRUCT
)
412 len
= TYPE_NFIELDS (type
);
413 i
= TYPE_N_BASECLASSES (type
);
416 range
= TYPE_INDEX_TYPE (TYPE_FIELD_TYPE (type
, i
));
417 target
= TYPE_TARGET_TYPE (range
);
419 l1
= TYPE_LOW_BOUND (TYPE_INDEX_TYPE (TYPE_FIELD_TYPE (type
, i
)));
420 h1
= TYPE_HIGH_BOUND (TYPE_INDEX_TYPE (TYPE_FIELD_TYPE (type
, len
-1)));
422 if (m2_get_discrete_bounds (target
, &l2
, &h2
) >= 0)
423 return (l1
== l2
&& h1
== h2
);
424 error (_("long_set failed to find discrete bounds for its subtype"));
427 error (_("expecting long_set"));
432 m2_long_set (struct type
*type
, struct ui_file
*stream
, int show
, int level
,
433 const struct type_print_options
*flags
)
435 struct type
*of_type
;
437 int len
= TYPE_NFIELDS (type
);
441 if (m2_is_long_set (type
))
443 if (TYPE_TAG_NAME (type
) != NULL
)
445 fputs_filtered (TYPE_TAG_NAME (type
), stream
);
449 else if (TYPE_NAME (type
) != NULL
)
451 fputs_filtered (TYPE_NAME (type
), stream
);
456 if (TYPE_TAG_NAME (type
) != NULL
|| TYPE_NAME (type
) != NULL
)
457 fputs_filtered (" = ", stream
);
459 if (get_long_set_bounds (type
, &low
, &high
))
461 fprintf_filtered(stream
, "SET OF ");
462 i
= TYPE_N_BASECLASSES (type
);
463 if (m2_is_long_set_of_type (type
, &of_type
))
464 m2_print_type (of_type
, "", stream
, show
- 1, level
, flags
);
467 fprintf_filtered(stream
, "[");
468 m2_print_bounds (TYPE_INDEX_TYPE (TYPE_FIELD_TYPE (type
, i
)),
469 stream
, show
- 1, level
, 0);
471 fprintf_filtered(stream
, "..");
473 m2_print_bounds (TYPE_INDEX_TYPE (TYPE_FIELD_TYPE (type
, len
-1)),
474 stream
, show
- 1, level
, 1);
475 fprintf_filtered(stream
, "]");
479 /* i18n: Do not translate the "SET OF" part! */
480 fprintf_filtered(stream
, _("SET OF <unknown>"));
487 /* m2_is_unbounded_array - returns TRUE if, type, should be regarded
488 as a Modula-2 unbounded ARRAY type. */
491 m2_is_unbounded_array (struct type
*type
)
493 if (TYPE_CODE (type
) == TYPE_CODE_STRUCT
)
496 * check if we have a structure with exactly two fields named
497 * _m2_contents and _m2_high. It also checks to see if the
498 * type of _m2_contents is a pointer. The TYPE_TARGET_TYPE
499 * of the pointer determines the unbounded ARRAY OF type.
501 if (TYPE_NFIELDS (type
) != 2)
503 if (strcmp (TYPE_FIELD_NAME (type
, 0), "_m2_contents") != 0)
505 if (strcmp (TYPE_FIELD_NAME (type
, 1), "_m2_high") != 0)
507 if (TYPE_CODE (TYPE_FIELD_TYPE (type
, 0)) != TYPE_CODE_PTR
)
514 /* m2_unbounded_array - if the struct type matches a Modula-2 unbounded
515 parameter type then display the type as an
516 ARRAY OF type. Returns TRUE if an unbounded
517 array type was detected. */
520 m2_unbounded_array (struct type
*type
, struct ui_file
*stream
, int show
,
521 int level
, const struct type_print_options
*flags
)
523 if (m2_is_unbounded_array (type
))
527 fputs_filtered ("ARRAY OF ", stream
);
528 m2_print_type (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type
, 0)),
529 "", stream
, 0, level
, flags
);
537 m2_record_fields (struct type
*type
, struct ui_file
*stream
, int show
,
538 int level
, const struct type_print_options
*flags
)
540 /* Print the tag if it exists. */
541 if (TYPE_TAG_NAME (type
) != NULL
)
543 if (strncmp (TYPE_TAG_NAME (type
), "$$", 2) != 0)
545 fputs_filtered (TYPE_TAG_NAME (type
), stream
);
547 fprintf_filtered (stream
, " = ");
553 if (TYPE_CODE (type
) == TYPE_CODE_STRUCT
)
554 fprintf_filtered (stream
, "RECORD ... END ");
555 else if (TYPE_CODE (type
) == TYPE_CODE_UNION
)
556 fprintf_filtered (stream
, "CASE ... END ");
561 int len
= TYPE_NFIELDS (type
);
563 if (TYPE_CODE (type
) == TYPE_CODE_STRUCT
)
564 fprintf_filtered (stream
, "RECORD\n");
565 else if (TYPE_CODE (type
) == TYPE_CODE_UNION
)
566 /* i18n: Do not translate "CASE" and "OF". */
567 fprintf_filtered (stream
, _("CASE <variant> OF\n"));
569 for (i
= TYPE_N_BASECLASSES (type
); i
< len
; i
++)
573 print_spaces_filtered (level
+ 4, stream
);
574 fputs_filtered (TYPE_FIELD_NAME (type
, i
), stream
);
575 fputs_filtered (" : ", stream
);
576 m2_print_type (TYPE_FIELD_TYPE (type
, i
),
578 stream
, 0, level
+ 4, flags
);
579 if (TYPE_FIELD_PACKED (type
, i
))
581 /* It is a bitfield. This code does not attempt
582 to look at the bitpos and reconstruct filler,
583 unnamed fields. This would lead to misleading
584 results if the compiler does not put out fields
585 for such things (I don't know what it does). */
586 fprintf_filtered (stream
, " : %d",
587 TYPE_FIELD_BITSIZE (type
, i
));
589 fprintf_filtered (stream
, ";\n");
592 fprintfi_filtered (level
, stream
, "END ");
597 m2_enum (struct type
*type
, struct ui_file
*stream
, int show
, int level
)
604 /* If we just printed a tag name, no need to print anything else. */
605 if (TYPE_TAG_NAME (type
) == NULL
)
606 fprintf_filtered (stream
, "(...)");
608 else if (show
> 0 || TYPE_TAG_NAME (type
) == NULL
)
610 fprintf_filtered (stream
, "(");
611 len
= TYPE_NFIELDS (type
);
613 for (i
= 0; i
< len
; i
++)
617 fprintf_filtered (stream
, ", ");
619 fputs_filtered (TYPE_FIELD_NAME (type
, i
), stream
);
620 if (lastval
!= TYPE_FIELD_ENUMVAL (type
, i
))
622 fprintf_filtered (stream
, " = %s",
623 plongest (TYPE_FIELD_ENUMVAL (type
, i
)));
624 lastval
= TYPE_FIELD_ENUMVAL (type
, i
);
628 fprintf_filtered (stream
, ")");