* remote.c (remote_pid_to_str): If printing a process id and we
[binutils-gdb.git] / gdb / m2-typeprint.c
blob8c5d6b4c592b12b5f4645f173bc0fbdd3f2d1974
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, 2009
4 Free Software Foundation, Inc.
6 This file is part of GDB.
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>. */
21 #include "defs.h"
22 #include "gdb_obstack.h"
23 #include "bfd.h" /* Binary File Description */
24 #include "symtab.h"
25 #include "gdbtypes.h"
26 #include "expression.h"
27 #include "value.h"
28 #include "gdbcore.h"
29 #include "m2-lang.h"
30 #include "target.h"
31 #include "language.h"
32 #include "demangle.h"
33 #include "c-lang.h"
34 #include "typeprint.h"
35 #include "cp-abi.h"
37 #include "gdb_string.h"
38 #include <errno.h>
40 static void m2_print_bounds (struct type *type,
41 struct ui_file *stream, int show, int level,
42 int print_high);
44 static void m2_typedef (struct type *, struct ui_file *, int, int);
45 static void m2_array (struct type *, struct ui_file *, int, int);
46 static void m2_pointer (struct type *, struct ui_file *, int, int);
47 static void m2_ref (struct type *, struct ui_file *, int, int);
48 static void m2_procedure (struct type *, struct ui_file *, int, int);
49 static void m2_union (struct type *, struct ui_file *);
50 static void m2_enum (struct type *, struct ui_file *, int, int);
51 static void m2_range (struct type *, struct ui_file *, int, int);
52 static void m2_type_name (struct type *type, struct ui_file *stream);
53 static void m2_short_set (struct type *type, struct ui_file *stream,
54 int show, int level);
55 static int m2_long_set (struct type *type, struct ui_file *stream,
56 int show, int level);
57 static int m2_unbounded_array (struct type *type, struct ui_file *stream,
58 int show, int level);
59 static void m2_record_fields (struct type *type, struct ui_file *stream,
60 int show, int level);
61 static void m2_unknown (const char *s, struct type *type,
62 struct ui_file *stream, int show, int level);
64 int m2_is_long_set (struct type *type);
65 int m2_is_long_set_of_type (struct type *type, struct type **of_type);
66 int m2_is_unbounded_array (struct type *type);
69 void
70 m2_print_type (struct type *type, char *varstring, struct ui_file *stream,
71 int show, int level)
73 enum type_code code;
74 int demangled_args;
76 CHECK_TYPEDEF (type);
78 QUIT;
80 wrap_here (" ");
81 if (type == NULL)
83 fputs_filtered (_("<type unknown>"), stream);
84 return;
87 code = TYPE_CODE (type);
88 switch (TYPE_CODE (type))
90 case TYPE_CODE_SET:
91 m2_short_set(type, stream, show, level);
92 break;
94 case TYPE_CODE_STRUCT:
95 if (m2_long_set (type, stream, show, level)
96 || m2_unbounded_array (type, stream, show, level))
97 break;
98 m2_record_fields (type, stream, show, level);
99 break;
101 case TYPE_CODE_TYPEDEF:
102 m2_typedef (type, stream, show, level);
103 break;
105 case TYPE_CODE_ARRAY:
106 m2_array (type, stream, show, level);
107 break;
109 case TYPE_CODE_PTR:
110 m2_pointer (type, stream, show, level);
111 break;
113 case TYPE_CODE_REF:
114 m2_ref (type, stream, show, level);
115 break;
117 case TYPE_CODE_METHOD:
118 m2_unknown (_("method"), type, stream, show, level);
119 break;
121 case TYPE_CODE_FUNC:
122 m2_procedure (type, stream, show, level);
123 break;
125 case TYPE_CODE_UNION:
126 m2_union (type, stream);
127 break;
129 case TYPE_CODE_ENUM:
130 m2_enum (type, stream, show, level);
131 break;
133 case TYPE_CODE_VOID:
134 break;
136 case TYPE_CODE_UNDEF:
137 /* i18n: Do not translate the "struct" part! */
138 m2_unknown (_("undef"), type, stream, show, level);
139 break;
141 case TYPE_CODE_ERROR:
142 m2_unknown (_("error"), type, stream, show, level);
143 break;
145 case TYPE_CODE_RANGE:
146 m2_range (type, stream, show, level);
147 break;
149 case TYPE_CODE_TEMPLATE:
150 break;
152 default:
153 m2_type_name (type, stream);
154 break;
158 /* Print a typedef using M2 syntax. TYPE is the underlying type.
159 NEW_SYMBOL is the symbol naming the type. STREAM is the stream on
160 which to print. */
162 void
163 m2_print_typedef (struct type *type, struct symbol *new_symbol,
164 struct ui_file *stream)
166 CHECK_TYPEDEF (type);
167 fprintf_filtered (stream, "TYPE ");
168 if (!TYPE_NAME (SYMBOL_TYPE (new_symbol))
169 || strcmp (TYPE_NAME ((SYMBOL_TYPE (new_symbol))),
170 SYMBOL_LINKAGE_NAME (new_symbol)) != 0)
171 fprintf_filtered (stream, "%s = ", SYMBOL_PRINT_NAME (new_symbol));
172 else
173 fprintf_filtered (stream, "<builtin> = ");
174 type_print (type, "", stream, 0);
175 fprintf_filtered (stream, ";\n");
178 /* m2_type_name - if a, type, has a name then print it. */
180 void
181 m2_type_name (struct type *type, struct ui_file *stream)
183 if (TYPE_NAME (type) != NULL)
184 fputs_filtered (TYPE_NAME (type), stream);
187 /* m2_range - displays a Modula-2 subrange type. */
189 void
190 m2_range (struct type *type, struct ui_file *stream, int show,
191 int level)
193 if (TYPE_HIGH_BOUND (type) == TYPE_LOW_BOUND (type))
194 m2_print_type (TYPE_DOMAIN_TYPE (type), "", stream, show, level);
195 else
197 struct type *target = TYPE_TARGET_TYPE (type);
199 fprintf_filtered (stream, "[");
200 print_type_scalar (target, TYPE_LOW_BOUND (type), stream);
201 fprintf_filtered (stream, "..");
202 print_type_scalar (target, TYPE_HIGH_BOUND (type), stream);
203 fprintf_filtered (stream, "]");
207 static void
208 m2_typedef (struct type *type, struct ui_file *stream, int show,
209 int level)
211 if (TYPE_NAME (type) != NULL)
213 fputs_filtered (TYPE_NAME (type), stream);
214 fputs_filtered (" = ", stream);
216 m2_print_type (TYPE_TARGET_TYPE (type), "", stream, show, level);
219 /* m2_array - prints out a Modula-2 ARRAY ... OF type. */
221 static void m2_array (struct type *type, struct ui_file *stream,
222 int show, int level)
224 fprintf_filtered (stream, "ARRAY [");
225 if (TYPE_LENGTH (TYPE_TARGET_TYPE (type)) > 0
226 && !TYPE_ARRAY_UPPER_BOUND_IS_UNDEFINED (type))
228 if (TYPE_INDEX_TYPE (type) != 0)
230 m2_print_bounds (TYPE_INDEX_TYPE (type), stream, show, -1, 0);
231 fprintf_filtered (stream, "..");
232 m2_print_bounds (TYPE_INDEX_TYPE (type), stream, show, -1, 1);
234 else
235 fprintf_filtered (stream, "%d",
236 (TYPE_LENGTH (type)
237 / TYPE_LENGTH (TYPE_TARGET_TYPE (type))));
239 fprintf_filtered (stream, "] OF ");
240 m2_print_type (TYPE_TARGET_TYPE (type), "", stream, show, level);
243 static void
244 m2_pointer (struct type *type, struct ui_file *stream, int show,
245 int level)
247 if (TYPE_CONST (type))
248 fprintf_filtered (stream, "[...] : ");
249 else
250 fprintf_filtered (stream, "POINTER TO ");
252 m2_print_type (TYPE_TARGET_TYPE (type), "", stream, show, level);
255 static void
256 m2_ref (struct type *type, struct ui_file *stream, int show,
257 int level)
259 fprintf_filtered (stream, "VAR");
260 m2_print_type (TYPE_TARGET_TYPE (type), "", stream, show, level);
263 static void
264 m2_unknown (const char *s, struct type *type, struct ui_file *stream,
265 int show, int level)
267 fprintf_filtered (stream, "%s %s", s, _("is unknown"));
270 static void m2_union (struct type *type, struct ui_file *stream)
272 fprintf_filtered (stream, "union");
275 static void
276 m2_procedure (struct type *type, struct ui_file *stream,
277 int show, int level)
279 fprintf_filtered (stream, "PROCEDURE ");
280 m2_type_name (type, stream);
281 if (TYPE_CODE (TYPE_TARGET_TYPE (type)) != TYPE_CODE_VOID)
283 int i, len = TYPE_NFIELDS (type);
285 fprintf_filtered (stream, " (");
286 for (i = 0; i < len; i++)
288 if (i > 0)
290 fputs_filtered (", ", stream);
291 wrap_here (" ");
293 m2_print_type (TYPE_FIELD_TYPE (type, i), "", stream, -1, 0);
295 if (TYPE_TARGET_TYPE (type) != NULL)
297 fprintf_filtered (stream, " : ");
298 m2_print_type (TYPE_TARGET_TYPE (type), "", stream, 0, 0);
303 static void
304 m2_print_bounds (struct type *type,
305 struct ui_file *stream, int show, int level,
306 int print_high)
308 struct type *target = TYPE_TARGET_TYPE (type);
310 if (TYPE_NFIELDS(type) == 0)
311 return;
313 if (print_high)
314 print_type_scalar (target, TYPE_HIGH_BOUND (type), stream);
315 else
316 print_type_scalar (target, TYPE_LOW_BOUND (type), stream);
319 static void
320 m2_short_set (struct type *type, struct ui_file *stream, int show, int level)
322 fprintf_filtered(stream, "SET [");
323 m2_print_bounds (TYPE_INDEX_TYPE (type), stream,
324 show - 1, level, 0);
326 fprintf_filtered(stream, "..");
327 m2_print_bounds (TYPE_INDEX_TYPE (type), stream,
328 show - 1, level, 1);
329 fprintf_filtered(stream, "]");
333 m2_is_long_set (struct type *type)
335 LONGEST previous_high = 0; /* unnecessary initialization
336 keeps gcc -Wall happy */
337 int len, i;
338 struct type *range;
340 if (TYPE_CODE (type) == TYPE_CODE_STRUCT)
343 /* check if all fields of the RECORD are consecutive sets. */
345 len = TYPE_NFIELDS (type);
346 for (i = TYPE_N_BASECLASSES (type); i < len; i++)
348 if (TYPE_FIELD_TYPE (type, i) == NULL)
349 return 0;
350 if (TYPE_CODE (TYPE_FIELD_TYPE (type, i)) != TYPE_CODE_SET)
351 return 0;
352 if (TYPE_FIELD_NAME (type, i) != NULL
353 && (strcmp (TYPE_FIELD_NAME (type, i), "") != 0))
354 return 0;
355 range = TYPE_INDEX_TYPE (TYPE_FIELD_TYPE (type, i));
356 if ((i > TYPE_N_BASECLASSES (type))
357 && previous_high + 1 != TYPE_LOW_BOUND (range))
358 return 0;
359 previous_high = TYPE_HIGH_BOUND (range);
361 return len>0;
363 return 0;
366 /* m2_get_discrete_bounds - a wrapper for get_discrete_bounds which
367 understands that CHARs might be signed.
368 This should be integrated into gdbtypes.c
369 inside get_discrete_bounds. */
371 static int
372 m2_get_discrete_bounds (struct type *type, LONGEST *lowp, LONGEST *highp)
374 CHECK_TYPEDEF (type);
375 switch (TYPE_CODE (type))
377 case TYPE_CODE_CHAR:
378 if (TYPE_LENGTH (type) < sizeof (LONGEST))
380 if (!TYPE_UNSIGNED (type))
382 *lowp = -(1 << (TYPE_LENGTH (type) * TARGET_CHAR_BIT - 1));
383 *highp = -*lowp - 1;
384 return 0;
387 /* fall through */
388 default:
389 return get_discrete_bounds (type, lowp, highp);
393 /* m2_is_long_set_of_type - returns TRUE if the long set was declared as
394 SET OF <oftype> of_type is assigned to the
395 subtype. */
398 m2_is_long_set_of_type (struct type *type, struct type **of_type)
400 int len, i;
401 struct type *range;
402 struct type *target;
403 LONGEST l1, l2;
404 LONGEST h1, h2;
406 if (TYPE_CODE (type) == TYPE_CODE_STRUCT)
408 len = TYPE_NFIELDS (type);
409 i = TYPE_N_BASECLASSES (type);
410 if (len == 0)
411 return 0;
412 range = TYPE_INDEX_TYPE (TYPE_FIELD_TYPE (type, i));
413 target = TYPE_TARGET_TYPE (range);
415 l1 = TYPE_LOW_BOUND (TYPE_INDEX_TYPE (TYPE_FIELD_TYPE (type, i)));
416 h1 = TYPE_HIGH_BOUND (TYPE_INDEX_TYPE (TYPE_FIELD_TYPE (type, len-1)));
417 *of_type = target;
418 if (m2_get_discrete_bounds (target, &l2, &h2) >= 0)
419 return (l1 == l2 && h1 == h2);
420 error (_("long_set failed to find discrete bounds for its subtype"));
421 return 0;
423 error (_("expecting long_set"));
424 return 0;
427 static int
428 m2_long_set (struct type *type, struct ui_file *stream, int show, int level)
430 struct type *index_type;
431 struct type *range_type;
432 struct type *of_type;
433 int i;
434 int len = TYPE_NFIELDS (type);
435 LONGEST low;
436 LONGEST high;
438 if (m2_is_long_set (type))
440 if (TYPE_TAG_NAME (type) != NULL)
442 fputs_filtered (TYPE_TAG_NAME (type), stream);
443 if (show == 0)
444 return 1;
446 else if (TYPE_NAME (type) != NULL)
448 fputs_filtered (TYPE_NAME (type), stream);
449 if (show == 0)
450 return 1;
453 if (TYPE_TAG_NAME (type) != NULL || TYPE_NAME (type) != NULL)
454 fputs_filtered (" = ", stream);
456 if (get_long_set_bounds (type, &low, &high))
458 fprintf_filtered(stream, "SET OF ");
459 i = TYPE_N_BASECLASSES (type);
460 if (m2_is_long_set_of_type (type, &of_type))
461 m2_print_type (of_type, "", stream, show - 1, level);
462 else
464 fprintf_filtered(stream, "[");
465 m2_print_bounds (TYPE_INDEX_TYPE (TYPE_FIELD_TYPE (type, i)),
466 stream, show - 1, level, 0);
468 fprintf_filtered(stream, "..");
470 m2_print_bounds (TYPE_INDEX_TYPE (TYPE_FIELD_TYPE (type, len-1)),
471 stream, show - 1, level, 1);
472 fprintf_filtered(stream, "]");
475 else
476 /* i18n: Do not translate the "SET OF" part! */
477 fprintf_filtered(stream, _("SET OF <unknown>"));
479 return 1;
481 return 0;
484 /* m2_is_unbounded_array - returns TRUE if, type, should be regarded
485 as a Modula-2 unbounded ARRAY type. */
488 m2_is_unbounded_array (struct type *type)
490 if (TYPE_CODE (type) == TYPE_CODE_STRUCT)
493 * check if we have a structure with exactly two fields named
494 * _m2_contents and _m2_high. It also checks to see if the
495 * type of _m2_contents is a pointer. The TYPE_TARGET_TYPE
496 * of the pointer determines the unbounded ARRAY OF type.
498 if (TYPE_NFIELDS (type) != 2)
499 return 0;
500 if (strcmp (TYPE_FIELD_NAME (type, 0), "_m2_contents") != 0)
501 return 0;
502 if (strcmp (TYPE_FIELD_NAME (type, 1), "_m2_high") != 0)
503 return 0;
504 if (TYPE_CODE (TYPE_FIELD_TYPE (type, 0)) != TYPE_CODE_PTR)
505 return 0;
506 return 1;
508 return 0;
511 /* m2_unbounded_array - if the struct type matches a Modula-2 unbounded
512 parameter type then display the type as an
513 ARRAY OF type. Returns TRUE if an unbounded
514 array type was detected. */
516 static int
517 m2_unbounded_array (struct type *type, struct ui_file *stream, int show,
518 int level)
520 if (m2_is_unbounded_array (type))
522 if (show > 0)
524 fputs_filtered ("ARRAY OF ", stream);
525 m2_print_type (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type, 0)),
526 "", stream, 0, level);
528 return 1;
530 return 0;
533 void
534 m2_record_fields (struct type *type, struct ui_file *stream, int show,
535 int level)
537 /* Print the tag if it exists. */
538 if (TYPE_TAG_NAME (type) != NULL)
540 if (strncmp (TYPE_TAG_NAME (type), "$$", 2) != 0)
542 fputs_filtered (TYPE_TAG_NAME (type), stream);
543 if (show > 0)
544 fprintf_filtered (stream, " = ");
547 wrap_here (" ");
548 if (show < 0)
550 if (TYPE_CODE (type) == DECLARED_TYPE_STRUCT)
551 fprintf_filtered (stream, "RECORD ... END ");
552 else if (TYPE_DECLARED_TYPE (type) == DECLARED_TYPE_UNION)
553 fprintf_filtered (stream, "CASE ... END ");
555 else if (show > 0)
557 int i;
558 int len = TYPE_NFIELDS (type);
560 if (TYPE_CODE (type) == TYPE_CODE_STRUCT)
561 fprintf_filtered (stream, "RECORD\n");
562 else if (TYPE_CODE (type) == TYPE_CODE_UNION)
563 /* i18n: Do not translate "CASE" and "OF" */
564 fprintf_filtered (stream, _("CASE <variant> OF\n"));
566 for (i = TYPE_N_BASECLASSES (type); i < len; i++)
568 QUIT;
570 print_spaces_filtered (level + 4, stream);
571 fputs_filtered (TYPE_FIELD_NAME (type, i), stream);
572 fputs_filtered (" : ", stream);
573 m2_print_type (TYPE_FIELD_TYPE (type, i),
575 stream, 0, level + 4);
576 if (TYPE_FIELD_PACKED (type, i))
578 /* It is a bitfield. This code does not attempt
579 to look at the bitpos and reconstruct filler,
580 unnamed fields. This would lead to misleading
581 results if the compiler does not put out fields
582 for such things (I don't know what it does). */
583 fprintf_filtered (stream, " : %d",
584 TYPE_FIELD_BITSIZE (type, i));
586 fprintf_filtered (stream, ";\n");
589 fprintfi_filtered (level, stream, "END ");
593 void
594 m2_enum (struct type *type, struct ui_file *stream, int show, int level)
596 int lastval, i, len;
598 if (show < 0)
600 /* If we just printed a tag name, no need to print anything else. */
601 if (TYPE_TAG_NAME (type) == NULL)
602 fprintf_filtered (stream, "(...)");
604 else if (show > 0 || TYPE_TAG_NAME (type) == NULL)
606 fprintf_filtered (stream, "(");
607 len = TYPE_NFIELDS (type);
608 lastval = 0;
609 for (i = 0; i < len; i++)
611 QUIT;
612 if (i > 0)
613 fprintf_filtered (stream, ", ");
614 wrap_here (" ");
615 fputs_filtered (TYPE_FIELD_NAME (type, i), stream);
616 if (lastval != TYPE_FIELD_BITPOS (type, i))
618 fprintf_filtered (stream, " = %d", TYPE_FIELD_BITPOS (type, i));
619 lastval = TYPE_FIELD_BITPOS (type, i);
621 lastval++;
623 fprintf_filtered (stream, ")");