* NEWS: Mention pointer to member improvements.
[external/binutils.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,
3                  2002, 2003, 2004, 2005, 2006
4    Free Software Foundation, Inc.
5
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
10    the Free Software Foundation; either version 2 of the License, or
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
19    along with this program; if not, write to the Free Software
20    Foundation, Inc., 51 Franklin Street, Fifth Floor,
21    Boston, MA 02110-1301, USA.  */
22
23 #include "defs.h"
24 #include "gdb_obstack.h"
25 #include "bfd.h"                /* Binary File Description */
26 #include "symtab.h"
27 #include "gdbtypes.h"
28 #include "expression.h"
29 #include "value.h"
30 #include "gdbcore.h"
31 #include "m2-lang.h"
32 #include "target.h"
33 #include "language.h"
34 #include "demangle.h"
35 #include "c-lang.h"
36 #include "typeprint.h"
37 #include "cp-abi.h"
38
39 #include "gdb_string.h"
40 #include <errno.h>
41
42 static void m2_print_bounds (struct type *type,
43                              struct ui_file *stream, int show, int level,
44                              int print_high);
45
46 static void m2_typedef (struct type *, struct ui_file *, int, int);
47 static void m2_array (struct type *, struct ui_file *, int, int);
48 static void m2_pointer (struct type *, struct ui_file *, int, int);
49 static void m2_ref (struct type *, struct ui_file *, int, int);
50 static void m2_procedure (struct type *, struct ui_file *, int, int);
51 static void m2_union (struct type *, struct ui_file *);
52 static void m2_enum (struct type *, struct ui_file *, int, int);
53 static void m2_range (struct type *, struct ui_file *, int, int);
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);
59 static void m2_record_fields (struct type *type, struct ui_file *stream,
60                               int show, int level);
61 static void m2_unknown (const char *s, struct type *type,
62                         struct ui_file *stream, int show, int level);
63
64 int m2_is_long_set (struct type *type);
65 int m2_is_long_set_of_type (struct type *type, struct type **of_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   code = TYPE_CODE (type);
77
78   QUIT;
79
80   wrap_here ("    ");
81   if (type == NULL)
82     {
83       fputs_filtered (_("<type unknown>"), stream);
84       return;
85     }
86
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         break;
96       m2_record_fields (type, stream, show, level);
97       break;
98
99     case TYPE_CODE_TYPEDEF:
100       m2_typedef (type, stream, show, level);
101       break;
102
103     case TYPE_CODE_ARRAY:
104       m2_array (type, stream, show, level);
105       break;
106
107     case TYPE_CODE_PTR:
108       m2_pointer (type, stream, show, level);
109       break;
110
111     case TYPE_CODE_REF:
112       m2_ref (type, stream, show, level);
113       break;
114
115     case TYPE_CODE_METHOD:
116       m2_unknown (_("method"), type, stream, show, level);
117       break;
118
119     case TYPE_CODE_FUNC:
120       m2_procedure (type, stream, show, level);
121       break;
122
123     case TYPE_CODE_UNION:
124       m2_union (type, stream);
125       break;
126
127     case TYPE_CODE_ENUM:
128       m2_enum (type, stream, show, level);
129       break;
130
131     case TYPE_CODE_VOID:
132       break;
133
134     case TYPE_CODE_UNDEF:
135       /* i18n: Do not translate the "struct" part! */
136       m2_unknown (_("undef"), type, stream, show, level);
137       break;
138
139     case TYPE_CODE_ERROR:
140       m2_unknown (_("error"), type, stream, show, level);
141       break;
142
143     case TYPE_CODE_RANGE:
144       m2_range (type, stream, show, level);
145       break;
146
147     case TYPE_CODE_TEMPLATE:
148       break;
149
150     default:
151       m2_type_name (type, stream);
152       break;
153     }
154 }
155
156 /*
157  *  m2_type_name - if a, type, has a name then print it.
158  */
159
160 void
161 m2_type_name (struct type *type, struct ui_file *stream)
162 {
163   if (TYPE_NAME (type) != NULL)
164     fputs_filtered (TYPE_NAME (type), stream);
165 }
166
167 /*
168  *  m2_range - displays a Modula-2 subrange type.
169  */
170
171 void
172 m2_range (struct type *type, struct ui_file *stream, int show,
173           int level)
174 {
175   if (TYPE_HIGH_BOUND (type) == TYPE_LOW_BOUND (type))
176     m2_print_type (TYPE_DOMAIN_TYPE (type), "", stream, show, level);
177   else
178     {
179       struct type *target = TYPE_TARGET_TYPE (type);
180
181       fprintf_filtered (stream, "[");
182       print_type_scalar (target, TYPE_LOW_BOUND (type), stream);
183       fprintf_filtered (stream, "..");
184       print_type_scalar (target, TYPE_HIGH_BOUND (type), stream);
185       fprintf_filtered (stream, "]");
186     }
187 }
188
189 static void
190 m2_typedef (struct type *type, struct ui_file *stream, int show,
191             int level)
192 {
193   if (TYPE_NAME (type) != NULL)
194     {
195       fputs_filtered (TYPE_NAME (type), stream);
196       fputs_filtered (" = ", stream);
197     }
198   m2_print_type (TYPE_TARGET_TYPE (type), "", stream, show, level);
199 }
200
201 /*
202  *  m2_array - prints out a Modula-2 ARRAY ... OF type
203  */
204
205 static void m2_array (struct type *type, struct ui_file *stream,
206                       int show, int level)
207 {
208   fprintf_filtered (stream, "ARRAY [");
209   if (TYPE_LENGTH (type) >= 0 && TYPE_LENGTH (TYPE_TARGET_TYPE (type)) > 0
210       && TYPE_ARRAY_UPPER_BOUND_TYPE (type) != BOUND_CANNOT_BE_DETERMINED)
211     {
212       if (TYPE_INDEX_TYPE (type) != 0)
213         {
214           m2_print_bounds (TYPE_INDEX_TYPE (type), stream, show, -1, 0);
215           fprintf_filtered (stream, "..");
216           m2_print_bounds (TYPE_INDEX_TYPE (type), stream, show, -1, 1);
217         }
218       else
219         fprintf_filtered (stream, "%d",
220                           (TYPE_LENGTH (type)
221                            / TYPE_LENGTH (TYPE_TARGET_TYPE (type))));
222     }
223   fprintf_filtered (stream, "] OF ");
224   m2_print_type (TYPE_TARGET_TYPE (type), "", stream, show, level);
225 }
226
227 static void
228 m2_pointer (struct type *type, struct ui_file *stream, int show,
229             int level)
230 {
231   if (TYPE_CONST (type))
232     fprintf_filtered (stream, "[...] : ");
233   else
234     fprintf_filtered (stream, "POINTER TO ");
235
236   m2_print_type (TYPE_TARGET_TYPE (type), "", stream, show, level);
237 }
238
239 static void
240 m2_ref (struct type *type, struct ui_file *stream, int show,
241         int level)
242 {
243   fprintf_filtered (stream, "VAR");
244   m2_print_type (TYPE_TARGET_TYPE (type), "", stream, show, level);
245 }
246
247 static void
248 m2_unknown (const char *s, struct type *type, struct ui_file *stream,
249             int show, int level)
250 {
251   fprintf_filtered (stream, "%s %s", s, _("is unknown"));
252 }
253
254 static void m2_union (struct type *type, struct ui_file *stream)
255 {
256   fprintf_filtered (stream, "union");
257 }
258
259 static void
260 m2_procedure (struct type *type, struct ui_file *stream,
261               int show, int level)
262 {
263   fprintf_filtered (stream, "PROCEDURE ");
264   m2_type_name (type, stream);
265   if (TYPE_CODE (TYPE_TARGET_TYPE (type)) != TYPE_CODE_VOID)
266     {
267       int i, len = TYPE_NFIELDS (type);
268
269       fprintf_filtered (stream, " (");
270       for (i = 0; i < len; i++)
271         {
272           if (i > 0)
273             {
274               fputs_filtered (", ", stream);
275               wrap_here ("    ");
276             }
277           m2_print_type (TYPE_FIELD_TYPE (type, i), "", stream, -1, 0);
278         }
279       if (TYPE_TARGET_TYPE (type) != NULL)
280         {
281           fprintf_filtered (stream, " : ");
282           m2_print_type (TYPE_TARGET_TYPE (type), "", stream, 0, 0);
283         }
284     }
285 }
286
287 static void
288 m2_print_bounds (struct type *type,
289                  struct ui_file *stream, int show, int level,
290                  int print_high)
291 {
292   struct type *target = TYPE_TARGET_TYPE (type);
293
294   if (target == NULL)
295     target = builtin_type_int;
296
297   if (TYPE_NFIELDS(type) == 0)
298     return;
299
300   if (print_high)
301     print_type_scalar (target, TYPE_HIGH_BOUND (type), stream);
302   else
303     print_type_scalar (target, TYPE_LOW_BOUND (type), stream);
304 }
305
306 static void
307 m2_short_set (struct type *type, struct ui_file *stream, int show, int level)
308 {
309   fprintf_filtered(stream, "SET [");
310   m2_print_bounds (TYPE_INDEX_TYPE (type), stream,
311                    show - 1, level, 0);
312
313   fprintf_filtered(stream, "..");
314   m2_print_bounds (TYPE_INDEX_TYPE (type), stream,
315                    show - 1, level, 1);
316   fprintf_filtered(stream, "]");
317 }
318
319 int
320 m2_is_long_set (struct type *type)
321 {
322   LONGEST previous_high = 0;  /* unnecessary initialization
323                                  keeps gcc -Wall happy */
324   int len, i;
325   struct type *range;
326
327   if (TYPE_CODE (type) == TYPE_CODE_STRUCT)
328     {
329
330       /*
331        *  check if all fields of the RECORD are consecutive sets
332        */
333       len = TYPE_NFIELDS (type);
334       for (i = TYPE_N_BASECLASSES (type); i < len; i++)
335         {
336           if (TYPE_FIELD_TYPE (type, i) == NULL)
337             return 0;
338           if (TYPE_CODE (TYPE_FIELD_TYPE (type, i)) != TYPE_CODE_SET)
339             return 0;
340           if (TYPE_FIELD_NAME (type, i) != NULL
341               && (strcmp (TYPE_FIELD_NAME (type, i), "") != 0))
342             return 0;
343           range = TYPE_INDEX_TYPE (TYPE_FIELD_TYPE (type, i));
344           if ((i > TYPE_N_BASECLASSES (type))
345               && previous_high + 1 != TYPE_LOW_BOUND (range))
346             return 0;
347           previous_high = TYPE_HIGH_BOUND (range);
348         }
349       return len>0;
350     }
351   return 0;
352 }
353
354 /*
355  *  m2_get_discrete_bounds - a wrapper for get_discrete_bounds which
356  *                           understands that CHARs might be signed.
357  *                           This should be integrated into gdbtypes.c
358  *                           inside get_discrete_bounds.
359  */
360
361 int
362 m2_get_discrete_bounds (struct type *type, LONGEST *lowp, LONGEST *highp)
363 {
364   CHECK_TYPEDEF (type);
365   switch (TYPE_CODE (type))
366     {
367     case TYPE_CODE_CHAR:
368       if (TYPE_LENGTH (type) < sizeof (LONGEST))
369         {
370           if (!TYPE_UNSIGNED (type))
371             {
372               *lowp = -(1 << (TYPE_LENGTH (type) * TARGET_CHAR_BIT - 1));
373               *highp = -*lowp - 1;
374               return 0;
375             }
376         }
377       /* fall through */
378     default:
379       return get_discrete_bounds (type, lowp, highp);
380     }
381 }
382
383 /*
384  *  m2_is_long_set_of_type - returns TRUE if the long set was declared as
385  *                           SET OF <oftype> of_type is assigned to the
386  *                           subtype.
387  */
388
389 int
390 m2_is_long_set_of_type (struct type *type, struct type **of_type)
391 {
392   int len, i;
393   struct type *range;
394   struct type *target;
395   LONGEST l1, l2;
396   LONGEST h1, h2;
397
398   if (TYPE_CODE (type) == TYPE_CODE_STRUCT)
399     {
400       len = TYPE_NFIELDS (type);
401       i = TYPE_N_BASECLASSES (type);
402       if (len == 0)
403         return 0;
404       range = TYPE_INDEX_TYPE (TYPE_FIELD_TYPE (type, i));
405       target = TYPE_TARGET_TYPE (range);
406       if (target == NULL)
407         target = builtin_type_int;
408
409       l1 = TYPE_LOW_BOUND (TYPE_INDEX_TYPE (TYPE_FIELD_TYPE (type, i)));
410       h1 = TYPE_HIGH_BOUND (TYPE_INDEX_TYPE (TYPE_FIELD_TYPE (type, len-1)));
411       *of_type = target;
412       if (m2_get_discrete_bounds (target, &l2, &h2) >= 0)
413         return (l1 == l2 && h1 == h2);
414       error (_("long_set failed to find discrete bounds for its subtype"));
415       return 0;
416     }
417   error (_("expecting long_set"));
418   return 0;
419 }
420
421 static int
422 m2_long_set (struct type *type, struct ui_file *stream, int show, int level)
423 {
424   struct type *index_type;
425   struct type *range_type;
426   struct type *of_type;
427   int i;
428   int len = TYPE_NFIELDS (type);
429   LONGEST low;
430   LONGEST high;
431
432   if (m2_is_long_set (type))
433     {
434       if (TYPE_TAG_NAME (type) != NULL)
435         {
436           fputs_filtered (TYPE_TAG_NAME (type), stream);
437           if (show == 0)
438             return 1;
439         }
440       else if (TYPE_NAME (type) != NULL)
441         {
442           fputs_filtered (TYPE_NAME (type), stream);
443           if (show == 0)
444             return 1;
445         }
446
447       if (TYPE_TAG_NAME (type) != NULL || TYPE_NAME (type) != NULL)
448         fputs_filtered (" = ", stream);
449
450       if (get_long_set_bounds (type, &low, &high))
451         {
452           fprintf_filtered(stream, "SET OF ");
453           i = TYPE_N_BASECLASSES (type);
454           if (m2_is_long_set_of_type (type, &of_type))
455             m2_print_type (of_type, "", stream, show - 1, level);
456           else
457             {
458               fprintf_filtered(stream, "[");
459               m2_print_bounds (TYPE_INDEX_TYPE (TYPE_FIELD_TYPE (type, i)),
460                                stream, show - 1, level, 0);
461
462               fprintf_filtered(stream, "..");
463
464               m2_print_bounds (TYPE_INDEX_TYPE (TYPE_FIELD_TYPE (type, len-1)),
465                                stream, show - 1, level, 1);
466               fprintf_filtered(stream, "]");
467             }
468         }
469       else
470         /* i18n: Do not translate the "SET OF" part! */
471         fprintf_filtered(stream, _("SET OF <unknown>"));
472
473       return 1;
474     }
475   return 0;
476 }
477
478 void
479 m2_record_fields (struct type *type, struct ui_file *stream, int show,
480                   int level)
481 {
482   /* Print the tag if it exists. 
483    */
484   if (TYPE_TAG_NAME (type) != NULL)
485     {
486       if (strncmp (TYPE_TAG_NAME (type), "$$", 2) != 0)
487         {
488           fputs_filtered (TYPE_TAG_NAME (type), stream);
489           if (show > 0)
490             fprintf_filtered (stream, " = ");
491         }
492     }
493   wrap_here ("    ");
494   if (show < 0)
495     {
496       if (TYPE_CODE (type) == DECLARED_TYPE_STRUCT)
497         fprintf_filtered (stream, "RECORD ... END ");
498       else if (TYPE_DECLARED_TYPE (type) == DECLARED_TYPE_UNION)
499         fprintf_filtered (stream, "CASE ... END ");
500     }
501   else if (show > 0)
502     {
503       int i;
504       int len = TYPE_NFIELDS (type);
505
506       if (TYPE_CODE (type) == TYPE_CODE_STRUCT)
507         fprintf_filtered (stream, "RECORD\n");
508       else if (TYPE_CODE (type) == TYPE_CODE_UNION)
509         /* i18n: Do not translate "CASE" and "OF" */
510         fprintf_filtered (stream, _("CASE <variant> OF\n"));
511
512       for (i = TYPE_N_BASECLASSES (type); i < len; i++)
513         {
514           QUIT;
515
516           print_spaces_filtered (level + 4, stream);
517           fputs_filtered (TYPE_FIELD_NAME (type, i), stream);
518           fputs_filtered (" : ", stream);
519           m2_print_type (TYPE_FIELD_TYPE (type, i),
520                          "",
521                          stream, 0, level + 4);
522           if (TYPE_FIELD_PACKED (type, i))
523             {
524               /* It is a bitfield.  This code does not attempt
525                  to look at the bitpos and reconstruct filler,
526                  unnamed fields.  This would lead to misleading
527                  results if the compiler does not put out fields
528                  for such things (I don't know what it does).  */
529               fprintf_filtered (stream, " : %d",
530                                 TYPE_FIELD_BITSIZE (type, i));
531             }
532           fprintf_filtered (stream, ";\n");
533         }
534       
535       fprintfi_filtered (level, stream, "END ");
536     }
537 }
538
539 void
540 m2_enum (struct type *type, struct ui_file *stream, int show, int level)
541 {
542   int lastval, i, len;
543
544   if (show < 0)
545     {
546       /* If we just printed a tag name, no need to print anything else.  */
547       if (TYPE_TAG_NAME (type) == NULL)
548         fprintf_filtered (stream, "(...)");
549     }
550   else if (show > 0 || TYPE_TAG_NAME (type) == NULL)
551     {
552       fprintf_filtered (stream, "(");
553       len = TYPE_NFIELDS (type);
554       lastval = 0;
555       for (i = 0; i < len; i++)
556         {
557           QUIT;
558           if (i > 0)
559             fprintf_filtered (stream, ", ");
560           wrap_here ("    ");
561           fputs_filtered (TYPE_FIELD_NAME (type, i), stream);
562           if (lastval != TYPE_FIELD_BITPOS (type, i))
563             {
564               fprintf_filtered (stream, " = %d", TYPE_FIELD_BITPOS (type, i));
565               lastval = TYPE_FIELD_BITPOS (type, i);
566             }
567           lastval++;
568         }
569       fprintf_filtered (stream, ")");
570     }
571 }