Include string.h in common-defs.h
[binutils-gdb.git] / gdb / m2-typeprint.c
1 /* Support for printing Modula 2 types for GDB, the GNU debugger.
2 Copyright (C) 1986-2014 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 #include <errno.h>
35
36 static void m2_print_bounds (struct type *type,
37 struct ui_file *stream, int show, int level,
38 int print_high);
39
40 static void m2_typedef (struct type *, struct ui_file *, int, int,
41 const struct type_print_options *);
42 static void m2_array (struct type *, struct ui_file *, int, int,
43 const struct type_print_options *);
44 static void m2_pointer (struct type *, struct ui_file *, int, int,
45 const struct type_print_options *);
46 static void m2_ref (struct type *, struct ui_file *, int, int,
47 const struct type_print_options *);
48 static void m2_procedure (struct type *, struct ui_file *, int, int,
49 const struct type_print_options *);
50 static void m2_union (struct type *, struct ui_file *);
51 static void m2_enum (struct type *, struct ui_file *, int, int);
52 static void m2_range (struct type *, struct ui_file *, int, int,
53 const struct type_print_options *);
54 static void m2_type_name (struct type *type, struct ui_file *stream);
55 static void m2_short_set (struct type *type, struct ui_file *stream,
56 int show, int level);
57 static int m2_long_set (struct type *type, struct ui_file *stream,
58 int show, int level, const struct type_print_options *flags);
59 static int m2_unbounded_array (struct type *type, struct ui_file *stream,
60 int show, int level,
61 const struct type_print_options *flags);
62 static void m2_record_fields (struct type *type, struct ui_file *stream,
63 int show, int level, const struct type_print_options *flags);
64 static void m2_unknown (const char *s, struct type *type,
65 struct ui_file *stream, int show, int level);
66
67 int m2_is_long_set (struct type *type);
68 int m2_is_long_set_of_type (struct type *type, struct type **of_type);
69 int m2_is_unbounded_array (struct type *type);
70
71
72 void
73 m2_print_type (struct type *type, const char *varstring,
74 struct ui_file *stream,
75 int show, int level,
76 const struct type_print_options *flags)
77 {
78 CHECK_TYPEDEF (type);
79
80 QUIT;
81
82 wrap_here (" ");
83 if (type == NULL)
84 {
85 fputs_filtered (_("<type unknown>"), stream);
86 return;
87 }
88
89 switch (TYPE_CODE (type))
90 {
91 case TYPE_CODE_SET:
92 m2_short_set(type, stream, show, level);
93 break;
94
95 case TYPE_CODE_STRUCT:
96 if (m2_long_set (type, stream, show, level, flags)
97 || m2_unbounded_array (type, stream, show, level, flags))
98 break;
99 m2_record_fields (type, stream, show, level, flags);
100 break;
101
102 case TYPE_CODE_TYPEDEF:
103 m2_typedef (type, stream, show, level, flags);
104 break;
105
106 case TYPE_CODE_ARRAY:
107 m2_array (type, stream, show, level, flags);
108 break;
109
110 case TYPE_CODE_PTR:
111 m2_pointer (type, stream, show, level, flags);
112 break;
113
114 case TYPE_CODE_REF:
115 m2_ref (type, stream, show, level, flags);
116 break;
117
118 case TYPE_CODE_METHOD:
119 m2_unknown (_("method"), type, stream, show, level);
120 break;
121
122 case TYPE_CODE_FUNC:
123 m2_procedure (type, stream, show, level, flags);
124 break;
125
126 case TYPE_CODE_UNION:
127 m2_union (type, stream);
128 break;
129
130 case TYPE_CODE_ENUM:
131 m2_enum (type, stream, show, level);
132 break;
133
134 case TYPE_CODE_VOID:
135 break;
136
137 case TYPE_CODE_UNDEF:
138 /* i18n: Do not translate the "struct" part! */
139 m2_unknown (_("undef"), type, stream, show, level);
140 break;
141
142 case TYPE_CODE_ERROR:
143 m2_unknown (_("error"), type, stream, show, level);
144 break;
145
146 case TYPE_CODE_RANGE:
147 m2_range (type, stream, show, level, flags);
148 break;
149
150 default:
151 m2_type_name (type, stream);
152 break;
153 }
154 }
155
156 /* Print a typedef using M2 syntax. TYPE is the underlying type.
157 NEW_SYMBOL is the symbol naming the type. STREAM is the stream on
158 which to print. */
159
160 void
161 m2_print_typedef (struct type *type, struct symbol *new_symbol,
162 struct ui_file *stream)
163 {
164 CHECK_TYPEDEF (type);
165 fprintf_filtered (stream, "TYPE ");
166 if (!TYPE_NAME (SYMBOL_TYPE (new_symbol))
167 || strcmp (TYPE_NAME ((SYMBOL_TYPE (new_symbol))),
168 SYMBOL_LINKAGE_NAME (new_symbol)) != 0)
169 fprintf_filtered (stream, "%s = ", SYMBOL_PRINT_NAME (new_symbol));
170 else
171 fprintf_filtered (stream, "<builtin> = ");
172 type_print (type, "", stream, 0);
173 fprintf_filtered (stream, ";\n");
174 }
175
176 /* m2_type_name - if a, type, has a name then print it. */
177
178 void
179 m2_type_name (struct type *type, struct ui_file *stream)
180 {
181 if (TYPE_NAME (type) != NULL)
182 fputs_filtered (TYPE_NAME (type), stream);
183 }
184
185 /* m2_range - displays a Modula-2 subrange type. */
186
187 void
188 m2_range (struct type *type, struct ui_file *stream, int show,
189 int level, const struct type_print_options *flags)
190 {
191 if (TYPE_HIGH_BOUND (type) == TYPE_LOW_BOUND (type))
192 m2_print_type (TYPE_DOMAIN_TYPE (type), "", stream, show, level,
193 flags);
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, const struct type_print_options *flags)
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, flags);
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, const struct type_print_options *flags)
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, flags);
240 }
241
242 static void
243 m2_pointer (struct type *type, struct ui_file *stream, int show,
244 int level, const struct type_print_options *flags)
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, flags);
252 }
253
254 static void
255 m2_ref (struct type *type, struct ui_file *stream, int show,
256 int level, const struct type_print_options *flags)
257 {
258 fprintf_filtered (stream, "VAR");
259 m2_print_type (TYPE_TARGET_TYPE (type), "", stream, show, level, flags);
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, const struct type_print_options *flags)
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, flags);
293 }
294 if (TYPE_TARGET_TYPE (type) != NULL)
295 {
296 fprintf_filtered (stream, " : ");
297 m2_print_type (TYPE_TARGET_TYPE (type), "", stream, 0, 0, flags);
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 (TYPE_NFIELDS(type) == 0)
310 return;
311
312 if (print_high)
313 print_type_scalar (target, TYPE_HIGH_BOUND (type), stream);
314 else
315 print_type_scalar (target, TYPE_LOW_BOUND (type), stream);
316 }
317
318 static void
319 m2_short_set (struct type *type, struct ui_file *stream, int show, int level)
320 {
321 fprintf_filtered(stream, "SET [");
322 m2_print_bounds (TYPE_INDEX_TYPE (type), stream,
323 show - 1, level, 0);
324
325 fprintf_filtered(stream, "..");
326 m2_print_bounds (TYPE_INDEX_TYPE (type), stream,
327 show - 1, level, 1);
328 fprintf_filtered(stream, "]");
329 }
330
331 int
332 m2_is_long_set (struct type *type)
333 {
334 LONGEST previous_high = 0; /* Unnecessary initialization
335 keeps gcc -Wall happy. */
336 int len, i;
337 struct type *range;
338
339 if (TYPE_CODE (type) == TYPE_CODE_STRUCT)
340 {
341
342 /* check if all fields of the RECORD are consecutive sets. */
343
344 len = TYPE_NFIELDS (type);
345 for (i = TYPE_N_BASECLASSES (type); i < len; i++)
346 {
347 if (TYPE_FIELD_TYPE (type, i) == NULL)
348 return 0;
349 if (TYPE_CODE (TYPE_FIELD_TYPE (type, i)) != TYPE_CODE_SET)
350 return 0;
351 if (TYPE_FIELD_NAME (type, i) != NULL
352 && (strcmp (TYPE_FIELD_NAME (type, i), "") != 0))
353 return 0;
354 range = TYPE_INDEX_TYPE (TYPE_FIELD_TYPE (type, i));
355 if ((i > TYPE_N_BASECLASSES (type))
356 && previous_high + 1 != TYPE_LOW_BOUND (range))
357 return 0;
358 previous_high = TYPE_HIGH_BOUND (range);
359 }
360 return len>0;
361 }
362 return 0;
363 }
364
365 /* m2_get_discrete_bounds - a wrapper for get_discrete_bounds which
366 understands that CHARs might be signed.
367 This should be integrated into gdbtypes.c
368 inside get_discrete_bounds. */
369
370 static int
371 m2_get_discrete_bounds (struct type *type, LONGEST *lowp, LONGEST *highp)
372 {
373 CHECK_TYPEDEF (type);
374 switch (TYPE_CODE (type))
375 {
376 case TYPE_CODE_CHAR:
377 if (TYPE_LENGTH (type) < sizeof (LONGEST))
378 {
379 if (!TYPE_UNSIGNED (type))
380 {
381 *lowp = -(1 << (TYPE_LENGTH (type) * TARGET_CHAR_BIT - 1));
382 *highp = -*lowp - 1;
383 return 0;
384 }
385 }
386 /* fall through */
387 default:
388 return get_discrete_bounds (type, lowp, highp);
389 }
390 }
391
392 /* m2_is_long_set_of_type - returns TRUE if the long set was declared as
393 SET OF <oftype> of_type is assigned to the
394 subtype. */
395
396 int
397 m2_is_long_set_of_type (struct type *type, struct type **of_type)
398 {
399 int len, i;
400 struct type *range;
401 struct type *target;
402 LONGEST l1, l2;
403 LONGEST h1, h2;
404
405 if (TYPE_CODE (type) == TYPE_CODE_STRUCT)
406 {
407 len = TYPE_NFIELDS (type);
408 i = TYPE_N_BASECLASSES (type);
409 if (len == 0)
410 return 0;
411 range = TYPE_INDEX_TYPE (TYPE_FIELD_TYPE (type, i));
412 target = TYPE_TARGET_TYPE (range);
413
414 l1 = TYPE_LOW_BOUND (TYPE_INDEX_TYPE (TYPE_FIELD_TYPE (type, i)));
415 h1 = TYPE_HIGH_BOUND (TYPE_INDEX_TYPE (TYPE_FIELD_TYPE (type, len-1)));
416 *of_type = target;
417 if (m2_get_discrete_bounds (target, &l2, &h2) >= 0)
418 return (l1 == l2 && h1 == h2);
419 error (_("long_set failed to find discrete bounds for its subtype"));
420 return 0;
421 }
422 error (_("expecting long_set"));
423 return 0;
424 }
425
426 static int
427 m2_long_set (struct type *type, struct ui_file *stream, int show, int level,
428 const struct type_print_options *flags)
429 {
430 struct type *of_type;
431 int i;
432 int len = TYPE_NFIELDS (type);
433 LONGEST low;
434 LONGEST high;
435
436 if (m2_is_long_set (type))
437 {
438 if (TYPE_TAG_NAME (type) != NULL)
439 {
440 fputs_filtered (TYPE_TAG_NAME (type), stream);
441 if (show == 0)
442 return 1;
443 }
444 else if (TYPE_NAME (type) != NULL)
445 {
446 fputs_filtered (TYPE_NAME (type), stream);
447 if (show == 0)
448 return 1;
449 }
450
451 if (TYPE_TAG_NAME (type) != NULL || TYPE_NAME (type) != NULL)
452 fputs_filtered (" = ", stream);
453
454 if (get_long_set_bounds (type, &low, &high))
455 {
456 fprintf_filtered(stream, "SET OF ");
457 i = TYPE_N_BASECLASSES (type);
458 if (m2_is_long_set_of_type (type, &of_type))
459 m2_print_type (of_type, "", stream, show - 1, level, flags);
460 else
461 {
462 fprintf_filtered(stream, "[");
463 m2_print_bounds (TYPE_INDEX_TYPE (TYPE_FIELD_TYPE (type, i)),
464 stream, show - 1, level, 0);
465
466 fprintf_filtered(stream, "..");
467
468 m2_print_bounds (TYPE_INDEX_TYPE (TYPE_FIELD_TYPE (type, len-1)),
469 stream, show - 1, level, 1);
470 fprintf_filtered(stream, "]");
471 }
472 }
473 else
474 /* i18n: Do not translate the "SET OF" part! */
475 fprintf_filtered(stream, _("SET OF <unknown>"));
476
477 return 1;
478 }
479 return 0;
480 }
481
482 /* m2_is_unbounded_array - returns TRUE if, type, should be regarded
483 as a Modula-2 unbounded ARRAY type. */
484
485 int
486 m2_is_unbounded_array (struct type *type)
487 {
488 if (TYPE_CODE (type) == TYPE_CODE_STRUCT)
489 {
490 /*
491 * check if we have a structure with exactly two fields named
492 * _m2_contents and _m2_high. It also checks to see if the
493 * type of _m2_contents is a pointer. The TYPE_TARGET_TYPE
494 * of the pointer determines the unbounded ARRAY OF type.
495 */
496 if (TYPE_NFIELDS (type) != 2)
497 return 0;
498 if (strcmp (TYPE_FIELD_NAME (type, 0), "_m2_contents") != 0)
499 return 0;
500 if (strcmp (TYPE_FIELD_NAME (type, 1), "_m2_high") != 0)
501 return 0;
502 if (TYPE_CODE (TYPE_FIELD_TYPE (type, 0)) != TYPE_CODE_PTR)
503 return 0;
504 return 1;
505 }
506 return 0;
507 }
508
509 /* m2_unbounded_array - if the struct type matches a Modula-2 unbounded
510 parameter type then display the type as an
511 ARRAY OF type. Returns TRUE if an unbounded
512 array type was detected. */
513
514 static int
515 m2_unbounded_array (struct type *type, struct ui_file *stream, int show,
516 int level, const struct type_print_options *flags)
517 {
518 if (m2_is_unbounded_array (type))
519 {
520 if (show > 0)
521 {
522 fputs_filtered ("ARRAY OF ", stream);
523 m2_print_type (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type, 0)),
524 "", stream, 0, level, flags);
525 }
526 return 1;
527 }
528 return 0;
529 }
530
531 void
532 m2_record_fields (struct type *type, struct ui_file *stream, int show,
533 int level, const struct type_print_options *flags)
534 {
535 /* Print the tag if it exists. */
536 if (TYPE_TAG_NAME (type) != NULL)
537 {
538 if (strncmp (TYPE_TAG_NAME (type), "$$", 2) != 0)
539 {
540 fputs_filtered (TYPE_TAG_NAME (type), stream);
541 if (show > 0)
542 fprintf_filtered (stream, " = ");
543 }
544 }
545 wrap_here (" ");
546 if (show < 0)
547 {
548 if (TYPE_CODE (type) == TYPE_CODE_STRUCT)
549 fprintf_filtered (stream, "RECORD ... END ");
550 else if (TYPE_CODE (type) == TYPE_CODE_UNION)
551 fprintf_filtered (stream, "CASE ... END ");
552 }
553 else if (show > 0)
554 {
555 int i;
556 int len = TYPE_NFIELDS (type);
557
558 if (TYPE_CODE (type) == TYPE_CODE_STRUCT)
559 fprintf_filtered (stream, "RECORD\n");
560 else if (TYPE_CODE (type) == TYPE_CODE_UNION)
561 /* i18n: Do not translate "CASE" and "OF". */
562 fprintf_filtered (stream, _("CASE <variant> OF\n"));
563
564 for (i = TYPE_N_BASECLASSES (type); i < len; i++)
565 {
566 QUIT;
567
568 print_spaces_filtered (level + 4, stream);
569 fputs_filtered (TYPE_FIELD_NAME (type, i), stream);
570 fputs_filtered (" : ", stream);
571 m2_print_type (TYPE_FIELD_TYPE (type, i),
572 "",
573 stream, 0, level + 4, flags);
574 if (TYPE_FIELD_PACKED (type, i))
575 {
576 /* It is a bitfield. This code does not attempt
577 to look at the bitpos and reconstruct filler,
578 unnamed fields. This would lead to misleading
579 results if the compiler does not put out fields
580 for such things (I don't know what it does). */
581 fprintf_filtered (stream, " : %d",
582 TYPE_FIELD_BITSIZE (type, i));
583 }
584 fprintf_filtered (stream, ";\n");
585 }
586
587 fprintfi_filtered (level, stream, "END ");
588 }
589 }
590
591 void
592 m2_enum (struct type *type, struct ui_file *stream, int show, int level)
593 {
594 LONGEST lastval;
595 int i, len;
596
597 if (show < 0)
598 {
599 /* If we just printed a tag name, no need to print anything else. */
600 if (TYPE_TAG_NAME (type) == NULL)
601 fprintf_filtered (stream, "(...)");
602 }
603 else if (show > 0 || TYPE_TAG_NAME (type) == NULL)
604 {
605 fprintf_filtered (stream, "(");
606 len = TYPE_NFIELDS (type);
607 lastval = 0;
608 for (i = 0; i < len; i++)
609 {
610 QUIT;
611 if (i > 0)
612 fprintf_filtered (stream, ", ");
613 wrap_here (" ");
614 fputs_filtered (TYPE_FIELD_NAME (type, i), stream);
615 if (lastval != TYPE_FIELD_ENUMVAL (type, i))
616 {
617 fprintf_filtered (stream, " = %s",
618 plongest (TYPE_FIELD_ENUMVAL (type, i)));
619 lastval = TYPE_FIELD_ENUMVAL (type, i);
620 }
621 lastval++;
622 }
623 fprintf_filtered (stream, ")");
624 }
625 }