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