Update copyright year range in all GDB files
[deliverable/binutils-gdb.git] / gdb / m2-typeprint.c
1 /* Support for printing Modula 2 types for GDB, the GNU debugger.
2 Copyright (C) 1986-2018 Free Software Foundation, Inc.
3
4 This file is part of GDB.
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <http://www.gnu.org/licenses/>. */
18
19 #include "defs.h"
20 #include "gdb_obstack.h"
21 #include "bfd.h" /* Binary File Description */
22 #include "symtab.h"
23 #include "gdbtypes.h"
24 #include "expression.h"
25 #include "value.h"
26 #include "gdbcore.h"
27 #include "m2-lang.h"
28 #include "target.h"
29 #include "language.h"
30 #include "demangle.h"
31 #include "c-lang.h"
32 #include "typeprint.h"
33 #include "cp-abi.h"
34
35 static void m2_print_bounds (struct type *type,
36 struct ui_file *stream, int show, int level,
37 int print_high);
38
39 static void m2_typedef (struct type *, struct ui_file *, int, int,
40 const struct type_print_options *);
41 static void m2_array (struct type *, struct ui_file *, int, int,
42 const struct type_print_options *);
43 static void m2_pointer (struct type *, struct ui_file *, int, int,
44 const struct type_print_options *);
45 static void m2_ref (struct type *, struct ui_file *, int, int,
46 const struct type_print_options *);
47 static void m2_procedure (struct type *, struct ui_file *, int, int,
48 const struct type_print_options *);
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 const struct type_print_options *);
53 static void m2_type_name (struct type *type, struct ui_file *stream);
54 static void m2_short_set (struct type *type, struct ui_file *stream,
55 int show, int level);
56 static int m2_long_set (struct type *type, struct ui_file *stream,
57 int show, int level, const struct type_print_options *flags);
58 static int m2_unbounded_array (struct type *type, struct ui_file *stream,
59 int show, int level,
60 const struct type_print_options *flags);
61 static void m2_record_fields (struct type *type, struct ui_file *stream,
62 int show, int level, const struct type_print_options *flags);
63 static void m2_unknown (const char *s, struct type *type,
64 struct ui_file *stream, int show, int level);
65
66 int m2_is_long_set (struct type *type);
67 int m2_is_long_set_of_type (struct type *type, struct type **of_type);
68 int m2_is_unbounded_array (struct type *type);
69
70
71 void
72 m2_print_type (struct type *type, const char *varstring,
73 struct ui_file *stream,
74 int show, int level,
75 const struct type_print_options *flags)
76 {
77 type = check_typedef (type);
78
79 QUIT;
80
81 wrap_here (" ");
82 if (type == NULL)
83 {
84 fputs_filtered (_("<type unknown>"), stream);
85 return;
86 }
87
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:
95 if (m2_long_set (type, stream, show, level, flags)
96 || m2_unbounded_array (type, stream, show, level, flags))
97 break;
98 m2_record_fields (type, stream, show, level, flags);
99 break;
100
101 case TYPE_CODE_TYPEDEF:
102 m2_typedef (type, stream, show, level, flags);
103 break;
104
105 case TYPE_CODE_ARRAY:
106 m2_array (type, stream, show, level, flags);
107 break;
108
109 case TYPE_CODE_PTR:
110 m2_pointer (type, stream, show, level, flags);
111 break;
112
113 case TYPE_CODE_REF:
114 m2_ref (type, stream, show, level, flags);
115 break;
116
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, flags);
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, flags);
147 break;
148
149 default:
150 m2_type_name (type, stream);
151 break;
152 }
153 }
154
155 /* Print a typedef using M2 syntax. TYPE is the underlying type.
156 NEW_SYMBOL is the symbol naming the type. STREAM is the stream on
157 which to print. */
158
159 void
160 m2_print_typedef (struct type *type, struct symbol *new_symbol,
161 struct ui_file *stream)
162 {
163 type = check_typedef (type);
164 fprintf_filtered (stream, "TYPE ");
165 if (!TYPE_NAME (SYMBOL_TYPE (new_symbol))
166 || strcmp (TYPE_NAME ((SYMBOL_TYPE (new_symbol))),
167 SYMBOL_LINKAGE_NAME (new_symbol)) != 0)
168 fprintf_filtered (stream, "%s = ", SYMBOL_PRINT_NAME (new_symbol));
169 else
170 fprintf_filtered (stream, "<builtin> = ");
171 type_print (type, "", stream, 0);
172 fprintf_filtered (stream, ";\n");
173 }
174
175 /* m2_type_name - if a, type, has a name then print it. */
176
177 void
178 m2_type_name (struct type *type, struct ui_file *stream)
179 {
180 if (TYPE_NAME (type) != NULL)
181 fputs_filtered (TYPE_NAME (type), stream);
182 }
183
184 /* m2_range - displays a Modula-2 subrange type. */
185
186 void
187 m2_range (struct type *type, struct ui_file *stream, int show,
188 int level, const struct type_print_options *flags)
189 {
190 if (TYPE_HIGH_BOUND (type) == TYPE_LOW_BOUND (type))
191 {
192 /* FIXME: TYPE_TARGET_TYPE used to be TYPE_DOMAIN_TYPE but that was
193 wrong. Not sure if TYPE_TARGET_TYPE is correct though. */
194 m2_print_type (TYPE_TARGET_TYPE (type), "", stream, show, level,
195 flags);
196 }
197 else
198 {
199 struct type *target = TYPE_TARGET_TYPE (type);
200
201 fprintf_filtered (stream, "[");
202 print_type_scalar (target, TYPE_LOW_BOUND (type), stream);
203 fprintf_filtered (stream, "..");
204 print_type_scalar (target, TYPE_HIGH_BOUND (type), stream);
205 fprintf_filtered (stream, "]");
206 }
207 }
208
209 static void
210 m2_typedef (struct type *type, struct ui_file *stream, int show,
211 int level, const struct type_print_options *flags)
212 {
213 if (TYPE_NAME (type) != NULL)
214 {
215 fputs_filtered (TYPE_NAME (type), stream);
216 fputs_filtered (" = ", stream);
217 }
218 m2_print_type (TYPE_TARGET_TYPE (type), "", stream, show, level, flags);
219 }
220
221 /* m2_array - prints out a Modula-2 ARRAY ... OF type. */
222
223 static void m2_array (struct type *type, struct ui_file *stream,
224 int show, int level, const struct type_print_options *flags)
225 {
226 fprintf_filtered (stream, "ARRAY [");
227 if (TYPE_LENGTH (TYPE_TARGET_TYPE (type)) > 0
228 && !TYPE_ARRAY_UPPER_BOUND_IS_UNDEFINED (type))
229 {
230 if (TYPE_INDEX_TYPE (type) != 0)
231 {
232 m2_print_bounds (TYPE_INDEX_TYPE (type), stream, show, -1, 0);
233 fprintf_filtered (stream, "..");
234 m2_print_bounds (TYPE_INDEX_TYPE (type), stream, show, -1, 1);
235 }
236 else
237 fprintf_filtered (stream, "%d",
238 (TYPE_LENGTH (type)
239 / TYPE_LENGTH (TYPE_TARGET_TYPE (type))));
240 }
241 fprintf_filtered (stream, "] OF ");
242 m2_print_type (TYPE_TARGET_TYPE (type), "", stream, show, level, flags);
243 }
244
245 static void
246 m2_pointer (struct type *type, struct ui_file *stream, int show,
247 int level, const struct type_print_options *flags)
248 {
249 if (TYPE_CONST (type))
250 fprintf_filtered (stream, "[...] : ");
251 else
252 fprintf_filtered (stream, "POINTER TO ");
253
254 m2_print_type (TYPE_TARGET_TYPE (type), "", stream, show, level, flags);
255 }
256
257 static void
258 m2_ref (struct type *type, struct ui_file *stream, int show,
259 int level, const struct type_print_options *flags)
260 {
261 fprintf_filtered (stream, "VAR");
262 m2_print_type (TYPE_TARGET_TYPE (type), "", stream, show, level, flags);
263 }
264
265 static void
266 m2_unknown (const char *s, struct type *type, struct ui_file *stream,
267 int show, int level)
268 {
269 fprintf_filtered (stream, "%s %s", s, _("is unknown"));
270 }
271
272 static void m2_union (struct type *type, struct ui_file *stream)
273 {
274 fprintf_filtered (stream, "union");
275 }
276
277 static void
278 m2_procedure (struct type *type, struct ui_file *stream,
279 int show, int level, const struct type_print_options *flags)
280 {
281 fprintf_filtered (stream, "PROCEDURE ");
282 m2_type_name (type, stream);
283 if (TYPE_TARGET_TYPE (type) == NULL
284 || TYPE_CODE (TYPE_TARGET_TYPE (type)) != TYPE_CODE_VOID)
285 {
286 int i, len = TYPE_NFIELDS (type);
287
288 fprintf_filtered (stream, " (");
289 for (i = 0; i < len; i++)
290 {
291 if (i > 0)
292 {
293 fputs_filtered (", ", stream);
294 wrap_here (" ");
295 }
296 m2_print_type (TYPE_FIELD_TYPE (type, i), "", stream, -1, 0, flags);
297 }
298 fprintf_filtered (stream, ") : ");
299 if (TYPE_TARGET_TYPE (type) != NULL)
300 m2_print_type (TYPE_TARGET_TYPE (type), "", stream, 0, 0, flags);
301 else
302 type_print_unknown_return_type (stream);
303 }
304 }
305
306 static void
307 m2_print_bounds (struct type *type,
308 struct ui_file *stream, int show, int level,
309 int print_high)
310 {
311 struct type *target = TYPE_TARGET_TYPE (type);
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
322 static void
323 m2_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
335 int
336 m2_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
346 /* check if all fields of the RECORD are consecutive sets. */
347
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
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. */
373
374 static int
375 m2_get_discrete_bounds (struct type *type, LONGEST *lowp, LONGEST *highp)
376 {
377 type = 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
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. */
399
400 int
401 m2_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
418 l1 = TYPE_LOW_BOUND (TYPE_INDEX_TYPE (TYPE_FIELD_TYPE (type, i)));
419 h1 = TYPE_HIGH_BOUND (TYPE_INDEX_TYPE (TYPE_FIELD_TYPE (type, len-1)));
420 *of_type = target;
421 if (m2_get_discrete_bounds (target, &l2, &h2) >= 0)
422 return (l1 == l2 && h1 == h2);
423 error (_("long_set failed to find discrete bounds for its subtype"));
424 return 0;
425 }
426 error (_("expecting long_set"));
427 return 0;
428 }
429
430 static int
431 m2_long_set (struct type *type, struct ui_file *stream, int show, int level,
432 const struct type_print_options *flags)
433 {
434 struct type *of_type;
435 int i;
436 int len = TYPE_NFIELDS (type);
437 LONGEST low;
438 LONGEST high;
439
440 if (m2_is_long_set (type))
441 {
442 if (TYPE_TAG_NAME (type) != NULL)
443 {
444 fputs_filtered (TYPE_TAG_NAME (type), stream);
445 if (show == 0)
446 return 1;
447 }
448 else if (TYPE_NAME (type) != NULL)
449 {
450 fputs_filtered (TYPE_NAME (type), stream);
451 if (show == 0)
452 return 1;
453 }
454
455 if (TYPE_TAG_NAME (type) != NULL || TYPE_NAME (type) != NULL)
456 fputs_filtered (" = ", stream);
457
458 if (get_long_set_bounds (type, &low, &high))
459 {
460 fprintf_filtered(stream, "SET OF ");
461 i = TYPE_N_BASECLASSES (type);
462 if (m2_is_long_set_of_type (type, &of_type))
463 m2_print_type (of_type, "", stream, show - 1, level, flags);
464 else
465 {
466 fprintf_filtered(stream, "[");
467 m2_print_bounds (TYPE_INDEX_TYPE (TYPE_FIELD_TYPE (type, i)),
468 stream, show - 1, level, 0);
469
470 fprintf_filtered(stream, "..");
471
472 m2_print_bounds (TYPE_INDEX_TYPE (TYPE_FIELD_TYPE (type, len-1)),
473 stream, show - 1, level, 1);
474 fprintf_filtered(stream, "]");
475 }
476 }
477 else
478 /* i18n: Do not translate the "SET OF" part! */
479 fprintf_filtered(stream, _("SET OF <unknown>"));
480
481 return 1;
482 }
483 return 0;
484 }
485
486 /* m2_is_unbounded_array - returns TRUE if, type, should be regarded
487 as a Modula-2 unbounded ARRAY type. */
488
489 int
490 m2_is_unbounded_array (struct type *type)
491 {
492 if (TYPE_CODE (type) == TYPE_CODE_STRUCT)
493 {
494 /*
495 * check if we have a structure with exactly two fields named
496 * _m2_contents and _m2_high. It also checks to see if the
497 * type of _m2_contents is a pointer. The TYPE_TARGET_TYPE
498 * of the pointer determines the unbounded ARRAY OF type.
499 */
500 if (TYPE_NFIELDS (type) != 2)
501 return 0;
502 if (strcmp (TYPE_FIELD_NAME (type, 0), "_m2_contents") != 0)
503 return 0;
504 if (strcmp (TYPE_FIELD_NAME (type, 1), "_m2_high") != 0)
505 return 0;
506 if (TYPE_CODE (TYPE_FIELD_TYPE (type, 0)) != TYPE_CODE_PTR)
507 return 0;
508 return 1;
509 }
510 return 0;
511 }
512
513 /* m2_unbounded_array - if the struct type matches a Modula-2 unbounded
514 parameter type then display the type as an
515 ARRAY OF type. Returns TRUE if an unbounded
516 array type was detected. */
517
518 static int
519 m2_unbounded_array (struct type *type, struct ui_file *stream, int show,
520 int level, const struct type_print_options *flags)
521 {
522 if (m2_is_unbounded_array (type))
523 {
524 if (show > 0)
525 {
526 fputs_filtered ("ARRAY OF ", stream);
527 m2_print_type (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type, 0)),
528 "", stream, 0, level, flags);
529 }
530 return 1;
531 }
532 return 0;
533 }
534
535 void
536 m2_record_fields (struct type *type, struct ui_file *stream, int show,
537 int level, const struct type_print_options *flags)
538 {
539 /* Print the tag if it exists. */
540 if (TYPE_TAG_NAME (type) != NULL)
541 {
542 if (!startswith (TYPE_TAG_NAME (type), "$$"))
543 {
544 fputs_filtered (TYPE_TAG_NAME (type), stream);
545 if (show > 0)
546 fprintf_filtered (stream, " = ");
547 }
548 }
549 wrap_here (" ");
550 if (show < 0)
551 {
552 if (TYPE_CODE (type) == TYPE_CODE_STRUCT)
553 fprintf_filtered (stream, "RECORD ... END ");
554 else if (TYPE_CODE (type) == TYPE_CODE_UNION)
555 fprintf_filtered (stream, "CASE ... END ");
556 }
557 else if (show > 0)
558 {
559 int i;
560 int len = TYPE_NFIELDS (type);
561
562 if (TYPE_CODE (type) == TYPE_CODE_STRUCT)
563 fprintf_filtered (stream, "RECORD\n");
564 else if (TYPE_CODE (type) == TYPE_CODE_UNION)
565 /* i18n: Do not translate "CASE" and "OF". */
566 fprintf_filtered (stream, _("CASE <variant> OF\n"));
567
568 for (i = TYPE_N_BASECLASSES (type); i < len; i++)
569 {
570 QUIT;
571
572 print_spaces_filtered (level + 4, stream);
573 fputs_filtered (TYPE_FIELD_NAME (type, i), stream);
574 fputs_filtered (" : ", stream);
575 m2_print_type (TYPE_FIELD_TYPE (type, i),
576 "",
577 stream, 0, level + 4, flags);
578 if (TYPE_FIELD_PACKED (type, i))
579 {
580 /* It is a bitfield. This code does not attempt
581 to look at the bitpos and reconstruct filler,
582 unnamed fields. This would lead to misleading
583 results if the compiler does not put out fields
584 for such things (I don't know what it does). */
585 fprintf_filtered (stream, " : %d",
586 TYPE_FIELD_BITSIZE (type, i));
587 }
588 fprintf_filtered (stream, ";\n");
589 }
590
591 fprintfi_filtered (level, stream, "END ");
592 }
593 }
594
595 void
596 m2_enum (struct type *type, struct ui_file *stream, int show, int level)
597 {
598 LONGEST lastval;
599 int i, len;
600
601 if (show < 0)
602 {
603 /* If we just printed a tag name, no need to print anything else. */
604 if (TYPE_TAG_NAME (type) == NULL)
605 fprintf_filtered (stream, "(...)");
606 }
607 else if (show > 0 || TYPE_TAG_NAME (type) == NULL)
608 {
609 fprintf_filtered (stream, "(");
610 len = TYPE_NFIELDS (type);
611 lastval = 0;
612 for (i = 0; i < len; i++)
613 {
614 QUIT;
615 if (i > 0)
616 fprintf_filtered (stream, ", ");
617 wrap_here (" ");
618 fputs_filtered (TYPE_FIELD_NAME (type, i), stream);
619 if (lastval != TYPE_FIELD_ENUMVAL (type, i))
620 {
621 fprintf_filtered (stream, " = %s",
622 plongest (TYPE_FIELD_ENUMVAL (type, i)));
623 lastval = TYPE_FIELD_ENUMVAL (type, i);
624 }
625 lastval++;
626 }
627 fprintf_filtered (stream, ")");
628 }
629 }
This page took 0.047399 seconds and 5 git commands to generate.