OSDN Git Service

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