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