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, 2008 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 int m2_unbounded_array (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
);
65 int m2_is_unbounded_array (struct type
*type
);
69 m2_print_type (struct type
*type
, char *varstring
, struct ui_file
*stream
,
82 fputs_filtered (_("<type unknown>"), stream
);
86 code
= TYPE_CODE (type
);
87 switch (TYPE_CODE (type
))
90 m2_short_set(type
, stream
, show
, level
);
93 case TYPE_CODE_STRUCT
:
94 if (m2_long_set (type
, stream
, show
, level
)
95 || m2_unbounded_array (type
, stream
, show
, level
))
97 m2_record_fields (type
, stream
, show
, level
);
100 case TYPE_CODE_TYPEDEF
:
101 m2_typedef (type
, stream
, show
, level
);
104 case TYPE_CODE_ARRAY
:
105 m2_array (type
, stream
, show
, level
);
109 m2_pointer (type
, stream
, show
, level
);
113 m2_ref (type
, stream
, show
, level
);
116 case TYPE_CODE_METHOD
:
117 m2_unknown (_("method"), type
, stream
, show
, level
);
121 m2_procedure (type
, stream
, show
, level
);
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
);
148 case TYPE_CODE_TEMPLATE
:
152 m2_type_name (type
, stream
);
157 /* Print a typedef using M2 syntax. TYPE is the underlying type.
158 NEW_SYMBOL is the symbol naming the type. STREAM is the stream on
162 m2_print_typedef (struct type
*type
, struct symbol
*new_symbol
,
163 struct ui_file
*stream
)
165 CHECK_TYPEDEF (type
);
166 fprintf_filtered (stream
, "TYPE ");
167 if (!TYPE_NAME (SYMBOL_TYPE (new_symbol
))
168 || strcmp (TYPE_NAME ((SYMBOL_TYPE (new_symbol
))),
169 SYMBOL_LINKAGE_NAME (new_symbol
)) != 0)
170 fprintf_filtered (stream
, "%s = ", SYMBOL_PRINT_NAME (new_symbol
));
172 fprintf_filtered (stream
, "<builtin> = ");
173 type_print (type
, "", stream
, 0);
174 fprintf_filtered (stream
, ";\n");
177 /* m2_type_name - if a, type, has a name then print it. */
180 m2_type_name (struct type
*type
, struct ui_file
*stream
)
182 if (TYPE_NAME (type
) != NULL
)
183 fputs_filtered (TYPE_NAME (type
), stream
);
186 /* m2_range - displays a Modula-2 subrange type. */
189 m2_range (struct type
*type
, struct ui_file
*stream
, int show
,
192 if (TYPE_HIGH_BOUND (type
) == TYPE_LOW_BOUND (type
))
193 m2_print_type (TYPE_DOMAIN_TYPE (type
), "", stream
, show
, level
);
196 struct type
*target
= TYPE_TARGET_TYPE (type
);
198 fprintf_filtered (stream
, "[");
199 print_type_scalar (target
, TYPE_LOW_BOUND (type
), stream
);
200 fprintf_filtered (stream
, "..");
201 print_type_scalar (target
, TYPE_HIGH_BOUND (type
), stream
);
202 fprintf_filtered (stream
, "]");
207 m2_typedef (struct type
*type
, struct ui_file
*stream
, int show
,
210 if (TYPE_NAME (type
) != NULL
)
212 fputs_filtered (TYPE_NAME (type
), stream
);
213 fputs_filtered (" = ", stream
);
215 m2_print_type (TYPE_TARGET_TYPE (type
), "", stream
, show
, level
);
218 /* m2_array - prints out a Modula-2 ARRAY ... OF type. */
220 static void m2_array (struct type
*type
, struct ui_file
*stream
,
223 fprintf_filtered (stream
, "ARRAY [");
224 if (TYPE_LENGTH (TYPE_TARGET_TYPE (type
)) > 0
225 && !TYPE_ARRAY_UPPER_BOUND_IS_UNDEFINED (type
))
227 if (TYPE_INDEX_TYPE (type
) != 0)
229 m2_print_bounds (TYPE_INDEX_TYPE (type
), stream
, show
, -1, 0);
230 fprintf_filtered (stream
, "..");
231 m2_print_bounds (TYPE_INDEX_TYPE (type
), stream
, show
, -1, 1);
234 fprintf_filtered (stream
, "%d",
236 / TYPE_LENGTH (TYPE_TARGET_TYPE (type
))));
238 fprintf_filtered (stream
, "] OF ");
239 m2_print_type (TYPE_TARGET_TYPE (type
), "", stream
, show
, level
);
243 m2_pointer (struct type
*type
, struct ui_file
*stream
, int show
,
246 if (TYPE_CONST (type
))
247 fprintf_filtered (stream
, "[...] : ");
249 fprintf_filtered (stream
, "POINTER TO ");
251 m2_print_type (TYPE_TARGET_TYPE (type
), "", stream
, show
, level
);
255 m2_ref (struct type
*type
, struct ui_file
*stream
, int show
,
258 fprintf_filtered (stream
, "VAR");
259 m2_print_type (TYPE_TARGET_TYPE (type
), "", stream
, show
, level
);
263 m2_unknown (const char *s
, struct type
*type
, struct ui_file
*stream
,
266 fprintf_filtered (stream
, "%s %s", s
, _("is unknown"));
269 static void m2_union (struct type
*type
, struct ui_file
*stream
)
271 fprintf_filtered (stream
, "union");
275 m2_procedure (struct type
*type
, struct ui_file
*stream
,
278 fprintf_filtered (stream
, "PROCEDURE ");
279 m2_type_name (type
, stream
);
280 if (TYPE_CODE (TYPE_TARGET_TYPE (type
)) != TYPE_CODE_VOID
)
282 int i
, len
= TYPE_NFIELDS (type
);
284 fprintf_filtered (stream
, " (");
285 for (i
= 0; i
< len
; i
++)
289 fputs_filtered (", ", stream
);
292 m2_print_type (TYPE_FIELD_TYPE (type
, i
), "", stream
, -1, 0);
294 if (TYPE_TARGET_TYPE (type
) != NULL
)
296 fprintf_filtered (stream
, " : ");
297 m2_print_type (TYPE_TARGET_TYPE (type
), "", stream
, 0, 0);
303 m2_print_bounds (struct type
*type
,
304 struct ui_file
*stream
, int show
, int level
,
307 struct type
*target
= TYPE_TARGET_TYPE (type
);
310 target
= builtin_type_int32
;
312 if (TYPE_NFIELDS(type
) == 0)
316 print_type_scalar (target
, TYPE_HIGH_BOUND (type
), stream
);
318 print_type_scalar (target
, TYPE_LOW_BOUND (type
), stream
);
322 m2_short_set (struct type
*type
, struct ui_file
*stream
, int show
, int level
)
324 fprintf_filtered(stream
, "SET [");
325 m2_print_bounds (TYPE_INDEX_TYPE (type
), stream
,
328 fprintf_filtered(stream
, "..");
329 m2_print_bounds (TYPE_INDEX_TYPE (type
), stream
,
331 fprintf_filtered(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
) == TYPE_CODE_STRUCT
)
345 /* check if all fields of the RECORD are consecutive sets. */
347 len
= TYPE_NFIELDS (type
);
348 for (i
= TYPE_N_BASECLASSES (type
); i
< len
; i
++)
350 if (TYPE_FIELD_TYPE (type
, i
) == NULL
)
352 if (TYPE_CODE (TYPE_FIELD_TYPE (type
, i
)) != TYPE_CODE_SET
)
354 if (TYPE_FIELD_NAME (type
, i
) != NULL
355 && (strcmp (TYPE_FIELD_NAME (type
, i
), "") != 0))
357 range
= TYPE_INDEX_TYPE (TYPE_FIELD_TYPE (type
, i
));
358 if ((i
> TYPE_N_BASECLASSES (type
))
359 && previous_high
+ 1 != TYPE_LOW_BOUND (range
))
361 previous_high
= TYPE_HIGH_BOUND (range
);
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 CHECK_TYPEDEF (type
);
377 switch (TYPE_CODE (type
))
380 if (TYPE_LENGTH (type
) < sizeof (LONGEST
))
382 if (!TYPE_UNSIGNED (type
))
384 *lowp
= -(1 << (TYPE_LENGTH (type
) * 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
) == TYPE_CODE_STRUCT
)
410 len
= TYPE_NFIELDS (type
);
411 i
= TYPE_N_BASECLASSES (type
);
414 range
= TYPE_INDEX_TYPE (TYPE_FIELD_TYPE (type
, i
));
415 target
= TYPE_TARGET_TYPE (range
);
417 target
= builtin_type_int32
;
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
)
434 struct type
*index_type
;
435 struct type
*range_type
;
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
);
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
,
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
);
538 m2_record_fields (struct type
*type
, struct ui_file
*stream
, int show
,
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
) == DECLARED_TYPE_STRUCT
)
555 fprintf_filtered (stream
, "RECORD ... END ");
556 else if (TYPE_DECLARED_TYPE (type
) == DECLARED_TYPE_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);
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
)
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_BITPOS (type
, i
))
622 fprintf_filtered (stream
, " = %d", TYPE_FIELD_BITPOS (type
, i
));
623 lastval
= TYPE_FIELD_BITPOS (type
, i
);
627 fprintf_filtered (stream
, ")");
This page took 0.044507 seconds and 4 git commands to generate.