gdb/
[deliverable/binutils-gdb.git] / gdb / m2-typeprint.c
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.
4
5 This file is part of GDB.
6
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.
11
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.
16
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/>. */
19
20 #include "defs.h"
21 #include "gdb_obstack.h"
22 #include "bfd.h" /* Binary File Description */
23 #include "symtab.h"
24 #include "gdbtypes.h"
25 #include "expression.h"
26 #include "value.h"
27 #include "gdbcore.h"
28 #include "m2-lang.h"
29 #include "target.h"
30 #include "language.h"
31 #include "demangle.h"
32 #include "c-lang.h"
33 #include "typeprint.h"
34 #include "cp-abi.h"
35
36 #include "gdb_string.h"
37 #include <errno.h>
38
39 static void m2_print_bounds (struct type *type,
40 struct ui_file *stream, int show, int level,
41 int print_high);
42
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,
53 int show, int level);
54 static int m2_long_set (struct type *type, struct ui_file *stream,
55 int show, int level);
56 static int m2_unbounded_array (struct type *type, struct ui_file *stream,
57 int show, int level);
58 static void m2_record_fields (struct type *type, struct ui_file *stream,
59 int show, int level);
60 static void m2_unknown (const char *s, struct type *type,
61 struct ui_file *stream, int show, int level);
62
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);
66
67
68 void
69 m2_print_type (struct type *type, char *varstring, struct ui_file *stream,
70 int show, int level)
71 {
72 enum type_code code;
73 int demangled_args;
74
75 CHECK_TYPEDEF (type);
76
77 QUIT;
78
79 wrap_here (" ");
80 if (type == NULL)
81 {
82 fputs_filtered (_("<type unknown>"), stream);
83 return;
84 }
85
86 code = TYPE_CODE (type);
87 switch (TYPE_CODE (type))
88 {
89 case TYPE_CODE_SET:
90 m2_short_set(type, stream, show, level);
91 break;
92
93 case TYPE_CODE_STRUCT:
94 if (m2_long_set (type, stream, show, level)
95 || m2_unbounded_array (type, stream, show, level))
96 break;
97 m2_record_fields (type, stream, show, level);
98 break;
99
100 case TYPE_CODE_TYPEDEF:
101 m2_typedef (type, stream, show, level);
102 break;
103
104 case TYPE_CODE_ARRAY:
105 m2_array (type, stream, show, level);
106 break;
107
108 case TYPE_CODE_PTR:
109 m2_pointer (type, stream, show, level);
110 break;
111
112 case TYPE_CODE_REF:
113 m2_ref (type, stream, show, level);
114 break;
115
116 case TYPE_CODE_METHOD:
117 m2_unknown (_("method"), type, stream, show, level);
118 break;
119
120 case TYPE_CODE_FUNC:
121 m2_procedure (type, stream, show, level);
122 break;
123
124 case TYPE_CODE_UNION:
125 m2_union (type, stream);
126 break;
127
128 case TYPE_CODE_ENUM:
129 m2_enum (type, stream, show, level);
130 break;
131
132 case TYPE_CODE_VOID:
133 break;
134
135 case TYPE_CODE_UNDEF:
136 /* i18n: Do not translate the "struct" part! */
137 m2_unknown (_("undef"), type, stream, show, level);
138 break;
139
140 case TYPE_CODE_ERROR:
141 m2_unknown (_("error"), type, stream, show, level);
142 break;
143
144 case TYPE_CODE_RANGE:
145 m2_range (type, stream, show, level);
146 break;
147
148 case TYPE_CODE_TEMPLATE:
149 break;
150
151 default:
152 m2_type_name (type, stream);
153 break;
154 }
155 }
156
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
159 which to print. */
160
161 void
162 m2_print_typedef (struct type *type, struct symbol *new_symbol,
163 struct ui_file *stream)
164 {
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));
171 else
172 fprintf_filtered (stream, "<builtin> = ");
173 type_print (type, "", stream, 0);
174 fprintf_filtered (stream, ";\n");
175 }
176
177 /* m2_type_name - if a, type, has a name then print it. */
178
179 void
180 m2_type_name (struct type *type, struct ui_file *stream)
181 {
182 if (TYPE_NAME (type) != NULL)
183 fputs_filtered (TYPE_NAME (type), stream);
184 }
185
186 /* m2_range - displays a Modula-2 subrange type. */
187
188 void
189 m2_range (struct type *type, struct ui_file *stream, int show,
190 int level)
191 {
192 if (TYPE_HIGH_BOUND (type) == TYPE_LOW_BOUND (type))
193 m2_print_type (TYPE_DOMAIN_TYPE (type), "", stream, show, level);
194 else
195 {
196 struct type *target = TYPE_TARGET_TYPE (type);
197
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, "]");
203 }
204 }
205
206 static void
207 m2_typedef (struct type *type, struct ui_file *stream, int show,
208 int level)
209 {
210 if (TYPE_NAME (type) != NULL)
211 {
212 fputs_filtered (TYPE_NAME (type), stream);
213 fputs_filtered (" = ", stream);
214 }
215 m2_print_type (TYPE_TARGET_TYPE (type), "", stream, show, level);
216 }
217
218 /* m2_array - prints out a Modula-2 ARRAY ... OF type. */
219
220 static void m2_array (struct type *type, struct ui_file *stream,
221 int show, int level)
222 {
223 fprintf_filtered (stream, "ARRAY [");
224 if (TYPE_LENGTH (TYPE_TARGET_TYPE (type)) > 0
225 && !TYPE_ARRAY_UPPER_BOUND_IS_UNDEFINED (type))
226 {
227 if (TYPE_INDEX_TYPE (type) != 0)
228 {
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);
232 }
233 else
234 fprintf_filtered (stream, "%d",
235 (TYPE_LENGTH (type)
236 / TYPE_LENGTH (TYPE_TARGET_TYPE (type))));
237 }
238 fprintf_filtered (stream, "] OF ");
239 m2_print_type (TYPE_TARGET_TYPE (type), "", stream, show, level);
240 }
241
242 static void
243 m2_pointer (struct type *type, struct ui_file *stream, int show,
244 int level)
245 {
246 if (TYPE_CONST (type))
247 fprintf_filtered (stream, "[...] : ");
248 else
249 fprintf_filtered (stream, "POINTER TO ");
250
251 m2_print_type (TYPE_TARGET_TYPE (type), "", stream, show, level);
252 }
253
254 static void
255 m2_ref (struct type *type, struct ui_file *stream, int show,
256 int level)
257 {
258 fprintf_filtered (stream, "VAR");
259 m2_print_type (TYPE_TARGET_TYPE (type), "", stream, show, level);
260 }
261
262 static void
263 m2_unknown (const char *s, struct type *type, struct ui_file *stream,
264 int show, int level)
265 {
266 fprintf_filtered (stream, "%s %s", s, _("is unknown"));
267 }
268
269 static void m2_union (struct type *type, struct ui_file *stream)
270 {
271 fprintf_filtered (stream, "union");
272 }
273
274 static void
275 m2_procedure (struct type *type, struct ui_file *stream,
276 int show, int level)
277 {
278 fprintf_filtered (stream, "PROCEDURE ");
279 m2_type_name (type, stream);
280 if (TYPE_CODE (TYPE_TARGET_TYPE (type)) != TYPE_CODE_VOID)
281 {
282 int i, len = TYPE_NFIELDS (type);
283
284 fprintf_filtered (stream, " (");
285 for (i = 0; i < len; i++)
286 {
287 if (i > 0)
288 {
289 fputs_filtered (", ", stream);
290 wrap_here (" ");
291 }
292 m2_print_type (TYPE_FIELD_TYPE (type, i), "", stream, -1, 0);
293 }
294 if (TYPE_TARGET_TYPE (type) != NULL)
295 {
296 fprintf_filtered (stream, " : ");
297 m2_print_type (TYPE_TARGET_TYPE (type), "", stream, 0, 0);
298 }
299 }
300 }
301
302 static void
303 m2_print_bounds (struct type *type,
304 struct ui_file *stream, int show, int level,
305 int print_high)
306 {
307 struct type *target = TYPE_TARGET_TYPE (type);
308
309 if (target == NULL)
310 target = builtin_type_int32;
311
312 if (TYPE_NFIELDS(type) == 0)
313 return;
314
315 if (print_high)
316 print_type_scalar (target, TYPE_HIGH_BOUND (type), stream);
317 else
318 print_type_scalar (target, TYPE_LOW_BOUND (type), stream);
319 }
320
321 static void
322 m2_short_set (struct type *type, struct ui_file *stream, int show, int level)
323 {
324 fprintf_filtered(stream, "SET [");
325 m2_print_bounds (TYPE_INDEX_TYPE (type), stream,
326 show - 1, level, 0);
327
328 fprintf_filtered(stream, "..");
329 m2_print_bounds (TYPE_INDEX_TYPE (type), stream,
330 show - 1, level, 1);
331 fprintf_filtered(stream, "]");
332 }
333
334 int
335 m2_is_long_set (struct type *type)
336 {
337 LONGEST previous_high = 0; /* unnecessary initialization
338 keeps gcc -Wall happy */
339 int len, i;
340 struct type *range;
341
342 if (TYPE_CODE (type) == TYPE_CODE_STRUCT)
343 {
344
345 /* check if all fields of the RECORD are consecutive sets. */
346
347 len = TYPE_NFIELDS (type);
348 for (i = TYPE_N_BASECLASSES (type); i < len; i++)
349 {
350 if (TYPE_FIELD_TYPE (type, i) == NULL)
351 return 0;
352 if (TYPE_CODE (TYPE_FIELD_TYPE (type, i)) != TYPE_CODE_SET)
353 return 0;
354 if (TYPE_FIELD_NAME (type, i) != NULL
355 && (strcmp (TYPE_FIELD_NAME (type, i), "") != 0))
356 return 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))
360 return 0;
361 previous_high = TYPE_HIGH_BOUND (range);
362 }
363 return len>0;
364 }
365 return 0;
366 }
367
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. */
372
373 int
374 m2_get_discrete_bounds (struct type *type, LONGEST *lowp, LONGEST *highp)
375 {
376 CHECK_TYPEDEF (type);
377 switch (TYPE_CODE (type))
378 {
379 case TYPE_CODE_CHAR:
380 if (TYPE_LENGTH (type) < sizeof (LONGEST))
381 {
382 if (!TYPE_UNSIGNED (type))
383 {
384 *lowp = -(1 << (TYPE_LENGTH (type) * TARGET_CHAR_BIT - 1));
385 *highp = -*lowp - 1;
386 return 0;
387 }
388 }
389 /* fall through */
390 default:
391 return get_discrete_bounds (type, lowp, highp);
392 }
393 }
394
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
397 subtype. */
398
399 int
400 m2_is_long_set_of_type (struct type *type, struct type **of_type)
401 {
402 int len, i;
403 struct type *range;
404 struct type *target;
405 LONGEST l1, l2;
406 LONGEST h1, h2;
407
408 if (TYPE_CODE (type) == TYPE_CODE_STRUCT)
409 {
410 len = TYPE_NFIELDS (type);
411 i = TYPE_N_BASECLASSES (type);
412 if (len == 0)
413 return 0;
414 range = TYPE_INDEX_TYPE (TYPE_FIELD_TYPE (type, i));
415 target = TYPE_TARGET_TYPE (range);
416 if (target == NULL)
417 target = builtin_type_int32;
418
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)));
421 *of_type = target;
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"));
425 return 0;
426 }
427 error (_("expecting long_set"));
428 return 0;
429 }
430
431 static int
432 m2_long_set (struct type *type, struct ui_file *stream, int show, int level)
433 {
434 struct type *index_type;
435 struct type *range_type;
436 struct type *of_type;
437 int i;
438 int len = TYPE_NFIELDS (type);
439 LONGEST low;
440 LONGEST high;
441
442 if (m2_is_long_set (type))
443 {
444 if (TYPE_TAG_NAME (type) != NULL)
445 {
446 fputs_filtered (TYPE_TAG_NAME (type), stream);
447 if (show == 0)
448 return 1;
449 }
450 else if (TYPE_NAME (type) != NULL)
451 {
452 fputs_filtered (TYPE_NAME (type), stream);
453 if (show == 0)
454 return 1;
455 }
456
457 if (TYPE_TAG_NAME (type) != NULL || TYPE_NAME (type) != NULL)
458 fputs_filtered (" = ", stream);
459
460 if (get_long_set_bounds (type, &low, &high))
461 {
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);
466 else
467 {
468 fprintf_filtered(stream, "[");
469 m2_print_bounds (TYPE_INDEX_TYPE (TYPE_FIELD_TYPE (type, i)),
470 stream, show - 1, level, 0);
471
472 fprintf_filtered(stream, "..");
473
474 m2_print_bounds (TYPE_INDEX_TYPE (TYPE_FIELD_TYPE (type, len-1)),
475 stream, show - 1, level, 1);
476 fprintf_filtered(stream, "]");
477 }
478 }
479 else
480 /* i18n: Do not translate the "SET OF" part! */
481 fprintf_filtered(stream, _("SET OF <unknown>"));
482
483 return 1;
484 }
485 return 0;
486 }
487
488 /* m2_is_unbounded_array - returns TRUE if, type, should be regarded
489 as a Modula-2 unbounded ARRAY type. */
490
491 int
492 m2_is_unbounded_array (struct type *type)
493 {
494 if (TYPE_CODE (type) == TYPE_CODE_STRUCT)
495 {
496 /*
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.
501 */
502 if (TYPE_NFIELDS (type) != 2)
503 return 0;
504 if (strcmp (TYPE_FIELD_NAME (type, 0), "_m2_contents") != 0)
505 return 0;
506 if (strcmp (TYPE_FIELD_NAME (type, 1), "_m2_high") != 0)
507 return 0;
508 if (TYPE_CODE (TYPE_FIELD_TYPE (type, 0)) != TYPE_CODE_PTR)
509 return 0;
510 return 1;
511 }
512 return 0;
513 }
514
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. */
519
520 static int
521 m2_unbounded_array (struct type *type, struct ui_file *stream, int show,
522 int level)
523 {
524 if (m2_is_unbounded_array (type))
525 {
526 if (show > 0)
527 {
528 fputs_filtered ("ARRAY OF ", stream);
529 m2_print_type (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type, 0)),
530 "", stream, 0, level);
531 }
532 return 1;
533 }
534 return 0;
535 }
536
537 void
538 m2_record_fields (struct type *type, struct ui_file *stream, int show,
539 int level)
540 {
541 /* Print the tag if it exists. */
542 if (TYPE_TAG_NAME (type) != NULL)
543 {
544 if (strncmp (TYPE_TAG_NAME (type), "$$", 2) != 0)
545 {
546 fputs_filtered (TYPE_TAG_NAME (type), stream);
547 if (show > 0)
548 fprintf_filtered (stream, " = ");
549 }
550 }
551 wrap_here (" ");
552 if (show < 0)
553 {
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 ");
558 }
559 else if (show > 0)
560 {
561 int i;
562 int len = TYPE_NFIELDS (type);
563
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"));
569
570 for (i = TYPE_N_BASECLASSES (type); i < len; i++)
571 {
572 QUIT;
573
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),
578 "",
579 stream, 0, level + 4);
580 if (TYPE_FIELD_PACKED (type, i))
581 {
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));
589 }
590 fprintf_filtered (stream, ";\n");
591 }
592
593 fprintfi_filtered (level, stream, "END ");
594 }
595 }
596
597 void
598 m2_enum (struct type *type, struct ui_file *stream, int show, int level)
599 {
600 int lastval, i, len;
601
602 if (show < 0)
603 {
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, "(...)");
607 }
608 else if (show > 0 || TYPE_TAG_NAME (type) == NULL)
609 {
610 fprintf_filtered (stream, "(");
611 len = TYPE_NFIELDS (type);
612 lastval = 0;
613 for (i = 0; i < len; i++)
614 {
615 QUIT;
616 if (i > 0)
617 fprintf_filtered (stream, ", ");
618 wrap_here (" ");
619 fputs_filtered (TYPE_FIELD_NAME (type, i), stream);
620 if (lastval != TYPE_FIELD_BITPOS (type, i))
621 {
622 fprintf_filtered (stream, " = %d", TYPE_FIELD_BITPOS (type, i));
623 lastval = TYPE_FIELD_BITPOS (type, i);
624 }
625 lastval++;
626 }
627 fprintf_filtered (stream, ")");
628 }
629 }
This page took 0.049488 seconds and 5 git commands to generate.