gdb
[deliverable/binutils-gdb.git] / gdb / m2-typeprint.c
CommitLineData
c906108c 1/* Support for printing Modula 2 types for GDB, the GNU debugger.
6aba47ca 2 Copyright (C) 1986, 1988, 1989, 1991, 1992, 1995, 2000, 2001, 2002, 2003,
0fb0cc75
JB
3 2004, 2005, 2006, 2007, 2008, 2009
4 Free Software Foundation, Inc.
c906108c 5
c5aa993b
JM
6 This file is part of GDB.
7
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
a9762ec7 10 the Free Software Foundation; either version 3 of the License, or
c5aa993b
JM
11 (at your option) any later version.
12
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.
17
18 You should have received a copy of the GNU General Public License
a9762ec7 19 along with this program. If not, see <http://www.gnu.org/licenses/>. */
c906108c
SS
20
21#include "defs.h"
72019c9c 22#include "gdb_obstack.h"
c906108c
SS
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"
c906108c 29#include "m2-lang.h"
72019c9c
GM
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"
36
37#include "gdb_string.h"
c906108c
SS
38#include <errno.h>
39
72019c9c
GM
40static void m2_print_bounds (struct type *type,
41 struct ui_file *stream, int show, int level,
42 int print_high);
43
44static void m2_typedef (struct type *, struct ui_file *, int, int);
45static void m2_array (struct type *, struct ui_file *, int, int);
46static void m2_pointer (struct type *, struct ui_file *, int, int);
47static void m2_ref (struct type *, struct ui_file *, int, int);
48static void m2_procedure (struct type *, struct ui_file *, int, int);
49static void m2_union (struct type *, struct ui_file *);
50static void m2_enum (struct type *, struct ui_file *, int, int);
51static void m2_range (struct type *, struct ui_file *, int, int);
52static void m2_type_name (struct type *type, struct ui_file *stream);
53static void m2_short_set (struct type *type, struct ui_file *stream,
54 int show, int level);
55static int m2_long_set (struct type *type, struct ui_file *stream,
56 int show, int level);
844781a1
GM
57static int m2_unbounded_array (struct type *type, struct ui_file *stream,
58 int show, int level);
72019c9c
GM
59static void m2_record_fields (struct type *type, struct ui_file *stream,
60 int show, int level);
61static void m2_unknown (const char *s, struct type *type,
62 struct ui_file *stream, int show, int level);
63
64int m2_is_long_set (struct type *type);
65int m2_is_long_set_of_type (struct type *type, struct type **of_type);
844781a1 66int m2_is_unbounded_array (struct type *type);
72019c9c
GM
67
68
c906108c 69void
fba45db2
KB
70m2_print_type (struct type *type, char *varstring, struct ui_file *stream,
71 int show, int level)
c906108c 72{
72019c9c
GM
73 enum type_code code;
74 int demangled_args;
75
76 CHECK_TYPEDEF (type);
72019c9c
GM
77
78 QUIT;
79
80 wrap_here (" ");
81 if (type == NULL)
82 {
83 fputs_filtered (_("<type unknown>"), stream);
84 return;
85 }
86
f08312c2 87 code = TYPE_CODE (type);
72019c9c
GM
88 switch (TYPE_CODE (type))
89 {
90 case TYPE_CODE_SET:
91 m2_short_set(type, stream, show, level);
92 break;
93
94 case TYPE_CODE_STRUCT:
844781a1
GM
95 if (m2_long_set (type, stream, show, level)
96 || m2_unbounded_array (type, stream, show, level))
72019c9c
GM
97 break;
98 m2_record_fields (type, stream, show, level);
99 break;
100
101 case TYPE_CODE_TYPEDEF:
102 m2_typedef (type, stream, show, level);
103 break;
104
105 case TYPE_CODE_ARRAY:
106 m2_array (type, stream, show, level);
107 break;
108
109 case TYPE_CODE_PTR:
110 m2_pointer (type, stream, show, level);
111 break;
112
113 case TYPE_CODE_REF:
114 m2_ref (type, stream, show, level);
115 break;
116
72019c9c
GM
117 case TYPE_CODE_METHOD:
118 m2_unknown (_("method"), type, stream, show, level);
119 break;
120
121 case TYPE_CODE_FUNC:
122 m2_procedure (type, stream, show, level);
123 break;
124
125 case TYPE_CODE_UNION:
126 m2_union (type, stream);
127 break;
128
129 case TYPE_CODE_ENUM:
130 m2_enum (type, stream, show, level);
131 break;
132
133 case TYPE_CODE_VOID:
134 break;
135
136 case TYPE_CODE_UNDEF:
137 /* i18n: Do not translate the "struct" part! */
138 m2_unknown (_("undef"), type, stream, show, level);
139 break;
140
141 case TYPE_CODE_ERROR:
142 m2_unknown (_("error"), type, stream, show, level);
143 break;
144
145 case TYPE_CODE_RANGE:
146 m2_range (type, stream, show, level);
147 break;
148
149 case TYPE_CODE_TEMPLATE:
150 break;
151
152 default:
153 m2_type_name (type, stream);
154 break;
155 }
156}
157
5c6ce71d
TT
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. */
161
162void
163m2_print_typedef (struct type *type, struct symbol *new_symbol,
164 struct ui_file *stream)
165{
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");
176}
177
844781a1 178/* m2_type_name - if a, type, has a name then print it. */
72019c9c
GM
179
180void
181m2_type_name (struct type *type, struct ui_file *stream)
182{
183 if (TYPE_NAME (type) != NULL)
184 fputs_filtered (TYPE_NAME (type), stream);
185}
186
844781a1 187/* m2_range - displays a Modula-2 subrange type. */
72019c9c
GM
188
189void
190m2_range (struct type *type, struct ui_file *stream, int show,
191 int level)
192{
193 if (TYPE_HIGH_BOUND (type) == TYPE_LOW_BOUND (type))
194 m2_print_type (TYPE_DOMAIN_TYPE (type), "", stream, show, level);
195 else
196 {
197 struct type *target = TYPE_TARGET_TYPE (type);
198
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, "]");
204 }
205}
206
207static void
208m2_typedef (struct type *type, struct ui_file *stream, int show,
209 int level)
210{
211 if (TYPE_NAME (type) != NULL)
212 {
213 fputs_filtered (TYPE_NAME (type), stream);
214 fputs_filtered (" = ", stream);
215 }
216 m2_print_type (TYPE_TARGET_TYPE (type), "", stream, show, level);
217}
218
844781a1 219/* m2_array - prints out a Modula-2 ARRAY ... OF type. */
72019c9c
GM
220
221static void m2_array (struct type *type, struct ui_file *stream,
222 int show, int level)
223{
224 fprintf_filtered (stream, "ARRAY [");
d5d6fca5 225 if (TYPE_LENGTH (TYPE_TARGET_TYPE (type)) > 0
d78df370 226 && !TYPE_ARRAY_UPPER_BOUND_IS_UNDEFINED (type))
72019c9c
GM
227 {
228 if (TYPE_INDEX_TYPE (type) != 0)
229 {
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);
233 }
234 else
235 fprintf_filtered (stream, "%d",
236 (TYPE_LENGTH (type)
237 / TYPE_LENGTH (TYPE_TARGET_TYPE (type))));
238 }
239 fprintf_filtered (stream, "] OF ");
240 m2_print_type (TYPE_TARGET_TYPE (type), "", stream, show, level);
241}
242
243static void
244m2_pointer (struct type *type, struct ui_file *stream, int show,
245 int level)
246{
247 if (TYPE_CONST (type))
248 fprintf_filtered (stream, "[...] : ");
249 else
250 fprintf_filtered (stream, "POINTER TO ");
251
252 m2_print_type (TYPE_TARGET_TYPE (type), "", stream, show, level);
253}
254
255static void
256m2_ref (struct type *type, struct ui_file *stream, int show,
257 int level)
258{
259 fprintf_filtered (stream, "VAR");
260 m2_print_type (TYPE_TARGET_TYPE (type), "", stream, show, level);
261}
262
263static void
264m2_unknown (const char *s, struct type *type, struct ui_file *stream,
265 int show, int level)
266{
267 fprintf_filtered (stream, "%s %s", s, _("is unknown"));
268}
269
270static void m2_union (struct type *type, struct ui_file *stream)
271{
272 fprintf_filtered (stream, "union");
273}
274
275static void
276m2_procedure (struct type *type, struct ui_file *stream,
277 int show, int level)
278{
279 fprintf_filtered (stream, "PROCEDURE ");
280 m2_type_name (type, stream);
281 if (TYPE_CODE (TYPE_TARGET_TYPE (type)) != TYPE_CODE_VOID)
282 {
283 int i, len = TYPE_NFIELDS (type);
284
285 fprintf_filtered (stream, " (");
286 for (i = 0; i < len; i++)
287 {
288 if (i > 0)
289 {
290 fputs_filtered (", ", stream);
291 wrap_here (" ");
292 }
293 m2_print_type (TYPE_FIELD_TYPE (type, i), "", stream, -1, 0);
294 }
295 if (TYPE_TARGET_TYPE (type) != NULL)
296 {
297 fprintf_filtered (stream, " : ");
298 m2_print_type (TYPE_TARGET_TYPE (type), "", stream, 0, 0);
299 }
300 }
301}
302
303static void
304m2_print_bounds (struct type *type,
305 struct ui_file *stream, int show, int level,
306 int print_high)
307{
308 struct type *target = TYPE_TARGET_TYPE (type);
309
310 if (target == NULL)
6d84d3d8 311 target = builtin_type_int32;
72019c9c
GM
312
313 if (TYPE_NFIELDS(type) == 0)
314 return;
315
316 if (print_high)
317 print_type_scalar (target, TYPE_HIGH_BOUND (type), stream);
318 else
319 print_type_scalar (target, TYPE_LOW_BOUND (type), stream);
320}
321
322static void
323m2_short_set (struct type *type, struct ui_file *stream, int show, int level)
324{
325 fprintf_filtered(stream, "SET [");
326 m2_print_bounds (TYPE_INDEX_TYPE (type), stream,
327 show - 1, level, 0);
328
329 fprintf_filtered(stream, "..");
330 m2_print_bounds (TYPE_INDEX_TYPE (type), stream,
331 show - 1, level, 1);
332 fprintf_filtered(stream, "]");
333}
334
335int
336m2_is_long_set (struct type *type)
337{
338 LONGEST previous_high = 0; /* unnecessary initialization
339 keeps gcc -Wall happy */
340 int len, i;
341 struct type *range;
342
343 if (TYPE_CODE (type) == TYPE_CODE_STRUCT)
344 {
345
844781a1
GM
346 /* check if all fields of the RECORD are consecutive sets. */
347
72019c9c
GM
348 len = TYPE_NFIELDS (type);
349 for (i = TYPE_N_BASECLASSES (type); i < len; i++)
350 {
351 if (TYPE_FIELD_TYPE (type, i) == NULL)
352 return 0;
353 if (TYPE_CODE (TYPE_FIELD_TYPE (type, i)) != TYPE_CODE_SET)
354 return 0;
355 if (TYPE_FIELD_NAME (type, i) != NULL
356 && (strcmp (TYPE_FIELD_NAME (type, i), "") != 0))
357 return 0;
358 range = TYPE_INDEX_TYPE (TYPE_FIELD_TYPE (type, i));
359 if ((i > TYPE_N_BASECLASSES (type))
360 && previous_high + 1 != TYPE_LOW_BOUND (range))
361 return 0;
362 previous_high = TYPE_HIGH_BOUND (range);
363 }
364 return len>0;
365 }
366 return 0;
367}
368
844781a1
GM
369/* m2_get_discrete_bounds - a wrapper for get_discrete_bounds which
370 understands that CHARs might be signed.
371 This should be integrated into gdbtypes.c
372 inside get_discrete_bounds. */
72019c9c 373
2c0b251b 374static int
72019c9c
GM
375m2_get_discrete_bounds (struct type *type, LONGEST *lowp, LONGEST *highp)
376{
377 CHECK_TYPEDEF (type);
378 switch (TYPE_CODE (type))
379 {
380 case TYPE_CODE_CHAR:
381 if (TYPE_LENGTH (type) < sizeof (LONGEST))
382 {
383 if (!TYPE_UNSIGNED (type))
384 {
385 *lowp = -(1 << (TYPE_LENGTH (type) * TARGET_CHAR_BIT - 1));
386 *highp = -*lowp - 1;
387 return 0;
388 }
389 }
390 /* fall through */
391 default:
392 return get_discrete_bounds (type, lowp, highp);
393 }
394}
395
844781a1
GM
396/* m2_is_long_set_of_type - returns TRUE if the long set was declared as
397 SET OF <oftype> of_type is assigned to the
398 subtype. */
72019c9c
GM
399
400int
401m2_is_long_set_of_type (struct type *type, struct type **of_type)
402{
403 int len, i;
404 struct type *range;
405 struct type *target;
406 LONGEST l1, l2;
407 LONGEST h1, h2;
408
409 if (TYPE_CODE (type) == TYPE_CODE_STRUCT)
410 {
411 len = TYPE_NFIELDS (type);
412 i = TYPE_N_BASECLASSES (type);
413 if (len == 0)
414 return 0;
415 range = TYPE_INDEX_TYPE (TYPE_FIELD_TYPE (type, i));
416 target = TYPE_TARGET_TYPE (range);
417 if (target == NULL)
6d84d3d8 418 target = builtin_type_int32;
72019c9c
GM
419
420 l1 = TYPE_LOW_BOUND (TYPE_INDEX_TYPE (TYPE_FIELD_TYPE (type, i)));
421 h1 = TYPE_HIGH_BOUND (TYPE_INDEX_TYPE (TYPE_FIELD_TYPE (type, len-1)));
422 *of_type = target;
423 if (m2_get_discrete_bounds (target, &l2, &h2) >= 0)
424 return (l1 == l2 && h1 == h2);
425 error (_("long_set failed to find discrete bounds for its subtype"));
426 return 0;
427 }
428 error (_("expecting long_set"));
429 return 0;
430}
431
432static int
433m2_long_set (struct type *type, struct ui_file *stream, int show, int level)
434{
435 struct type *index_type;
436 struct type *range_type;
437 struct type *of_type;
438 int i;
439 int len = TYPE_NFIELDS (type);
440 LONGEST low;
441 LONGEST high;
442
443 if (m2_is_long_set (type))
444 {
445 if (TYPE_TAG_NAME (type) != NULL)
446 {
447 fputs_filtered (TYPE_TAG_NAME (type), stream);
448 if (show == 0)
449 return 1;
450 }
451 else if (TYPE_NAME (type) != NULL)
452 {
453 fputs_filtered (TYPE_NAME (type), stream);
454 if (show == 0)
455 return 1;
456 }
457
458 if (TYPE_TAG_NAME (type) != NULL || TYPE_NAME (type) != NULL)
459 fputs_filtered (" = ", stream);
460
461 if (get_long_set_bounds (type, &low, &high))
462 {
463 fprintf_filtered(stream, "SET OF ");
464 i = TYPE_N_BASECLASSES (type);
465 if (m2_is_long_set_of_type (type, &of_type))
466 m2_print_type (of_type, "", stream, show - 1, level);
467 else
468 {
469 fprintf_filtered(stream, "[");
470 m2_print_bounds (TYPE_INDEX_TYPE (TYPE_FIELD_TYPE (type, i)),
471 stream, show - 1, level, 0);
472
473 fprintf_filtered(stream, "..");
474
475 m2_print_bounds (TYPE_INDEX_TYPE (TYPE_FIELD_TYPE (type, len-1)),
476 stream, show - 1, level, 1);
477 fprintf_filtered(stream, "]");
478 }
479 }
480 else
481 /* i18n: Do not translate the "SET OF" part! */
482 fprintf_filtered(stream, _("SET OF <unknown>"));
483
484 return 1;
485 }
486 return 0;
487}
488
844781a1
GM
489/* m2_is_unbounded_array - returns TRUE if, type, should be regarded
490 as a Modula-2 unbounded ARRAY type. */
491
492int
493m2_is_unbounded_array (struct type *type)
494{
495 if (TYPE_CODE (type) == TYPE_CODE_STRUCT)
496 {
497 /*
498 * check if we have a structure with exactly two fields named
499 * _m2_contents and _m2_high. It also checks to see if the
500 * type of _m2_contents is a pointer. The TYPE_TARGET_TYPE
501 * of the pointer determines the unbounded ARRAY OF type.
502 */
503 if (TYPE_NFIELDS (type) != 2)
504 return 0;
505 if (strcmp (TYPE_FIELD_NAME (type, 0), "_m2_contents") != 0)
506 return 0;
507 if (strcmp (TYPE_FIELD_NAME (type, 1), "_m2_high") != 0)
508 return 0;
509 if (TYPE_CODE (TYPE_FIELD_TYPE (type, 0)) != TYPE_CODE_PTR)
510 return 0;
511 return 1;
512 }
513 return 0;
514}
515
516/* m2_unbounded_array - if the struct type matches a Modula-2 unbounded
517 parameter type then display the type as an
518 ARRAY OF type. Returns TRUE if an unbounded
519 array type was detected. */
520
521static int
522m2_unbounded_array (struct type *type, struct ui_file *stream, int show,
523 int level)
524{
525 if (m2_is_unbounded_array (type))
526 {
527 if (show > 0)
528 {
529 fputs_filtered ("ARRAY OF ", stream);
530 m2_print_type (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type, 0)),
531 "", stream, 0, level);
532 }
533 return 1;
534 }
535 return 0;
536}
537
72019c9c
GM
538void
539m2_record_fields (struct type *type, struct ui_file *stream, int show,
540 int level)
541{
844781a1 542 /* Print the tag if it exists. */
72019c9c
GM
543 if (TYPE_TAG_NAME (type) != NULL)
544 {
545 if (strncmp (TYPE_TAG_NAME (type), "$$", 2) != 0)
546 {
547 fputs_filtered (TYPE_TAG_NAME (type), stream);
548 if (show > 0)
549 fprintf_filtered (stream, " = ");
550 }
551 }
552 wrap_here (" ");
553 if (show < 0)
554 {
555 if (TYPE_CODE (type) == DECLARED_TYPE_STRUCT)
556 fprintf_filtered (stream, "RECORD ... END ");
557 else if (TYPE_DECLARED_TYPE (type) == DECLARED_TYPE_UNION)
558 fprintf_filtered (stream, "CASE ... END ");
559 }
560 else if (show > 0)
561 {
5648af48
JB
562 int i;
563 int len = TYPE_NFIELDS (type);
564
72019c9c
GM
565 if (TYPE_CODE (type) == TYPE_CODE_STRUCT)
566 fprintf_filtered (stream, "RECORD\n");
567 else if (TYPE_CODE (type) == TYPE_CODE_UNION)
568 /* i18n: Do not translate "CASE" and "OF" */
569 fprintf_filtered (stream, _("CASE <variant> OF\n"));
72019c9c
GM
570
571 for (i = TYPE_N_BASECLASSES (type); i < len; i++)
572 {
573 QUIT;
574
575 print_spaces_filtered (level + 4, stream);
576 fputs_filtered (TYPE_FIELD_NAME (type, i), stream);
577 fputs_filtered (" : ", stream);
578 m2_print_type (TYPE_FIELD_TYPE (type, i),
579 "",
580 stream, 0, level + 4);
581 if (TYPE_FIELD_PACKED (type, i))
582 {
583 /* It is a bitfield. This code does not attempt
584 to look at the bitpos and reconstruct filler,
585 unnamed fields. This would lead to misleading
586 results if the compiler does not put out fields
587 for such things (I don't know what it does). */
588 fprintf_filtered (stream, " : %d",
589 TYPE_FIELD_BITSIZE (type, i));
590 }
591 fprintf_filtered (stream, ";\n");
592 }
593
594 fprintfi_filtered (level, stream, "END ");
595 }
596}
597
598void
599m2_enum (struct type *type, struct ui_file *stream, int show, int level)
600{
601 int lastval, i, len;
c906108c 602
72019c9c
GM
603 if (show < 0)
604 {
605 /* If we just printed a tag name, no need to print anything else. */
606 if (TYPE_TAG_NAME (type) == NULL)
607 fprintf_filtered (stream, "(...)");
608 }
609 else if (show > 0 || TYPE_TAG_NAME (type) == NULL)
610 {
611 fprintf_filtered (stream, "(");
612 len = TYPE_NFIELDS (type);
613 lastval = 0;
614 for (i = 0; i < len; i++)
615 {
616 QUIT;
617 if (i > 0)
618 fprintf_filtered (stream, ", ");
619 wrap_here (" ");
620 fputs_filtered (TYPE_FIELD_NAME (type, i), stream);
621 if (lastval != TYPE_FIELD_BITPOS (type, i))
622 {
623 fprintf_filtered (stream, " = %d", TYPE_FIELD_BITPOS (type, i));
624 lastval = TYPE_FIELD_BITPOS (type, i);
625 }
626 lastval++;
627 }
628 fprintf_filtered (stream, ")");
629 }
c906108c 630}
This page took 0.718255 seconds and 4 git commands to generate.