Replace copyreloc-main.c with copyreloc-main.S
[platform/upstream/binutils.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
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   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   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     m2_print_type (TYPE_DOMAIN_TYPE (type), "", stream, show, level,
192                    flags);
193   else
194     {
195       struct type *target = TYPE_TARGET_TYPE (type);
196
197       fprintf_filtered (stream, "[");
198       print_type_scalar (target, TYPE_LOW_BOUND (type), stream);
199       fprintf_filtered (stream, "..");
200       print_type_scalar (target, TYPE_HIGH_BOUND (type), stream);
201       fprintf_filtered (stream, "]");
202     }
203 }
204
205 static void
206 m2_typedef (struct type *type, struct ui_file *stream, int show,
207             int level, const struct type_print_options *flags)
208 {
209   if (TYPE_NAME (type) != NULL)
210     {
211       fputs_filtered (TYPE_NAME (type), stream);
212       fputs_filtered (" = ", stream);
213     }
214   m2_print_type (TYPE_TARGET_TYPE (type), "", stream, show, level, flags);
215 }
216
217 /* m2_array - prints out a Modula-2 ARRAY ... OF type.  */
218
219 static void m2_array (struct type *type, struct ui_file *stream,
220                       int show, int level, const struct type_print_options *flags)
221 {
222   fprintf_filtered (stream, "ARRAY [");
223   if (TYPE_LENGTH (TYPE_TARGET_TYPE (type)) > 0
224       && !TYPE_ARRAY_UPPER_BOUND_IS_UNDEFINED (type))
225     {
226       if (TYPE_INDEX_TYPE (type) != 0)
227         {
228           m2_print_bounds (TYPE_INDEX_TYPE (type), stream, show, -1, 0);
229           fprintf_filtered (stream, "..");
230           m2_print_bounds (TYPE_INDEX_TYPE (type), stream, show, -1, 1);
231         }
232       else
233         fprintf_filtered (stream, "%d",
234                           (TYPE_LENGTH (type)
235                            / TYPE_LENGTH (TYPE_TARGET_TYPE (type))));
236     }
237   fprintf_filtered (stream, "] OF ");
238   m2_print_type (TYPE_TARGET_TYPE (type), "", stream, show, level, flags);
239 }
240
241 static void
242 m2_pointer (struct type *type, struct ui_file *stream, int show,
243             int level, const struct type_print_options *flags)
244 {
245   if (TYPE_CONST (type))
246     fprintf_filtered (stream, "[...] : ");
247   else
248     fprintf_filtered (stream, "POINTER TO ");
249
250   m2_print_type (TYPE_TARGET_TYPE (type), "", stream, show, level, flags);
251 }
252
253 static void
254 m2_ref (struct type *type, struct ui_file *stream, int show,
255         int level, const struct type_print_options *flags)
256 {
257   fprintf_filtered (stream, "VAR");
258   m2_print_type (TYPE_TARGET_TYPE (type), "", stream, show, level, flags);
259 }
260
261 static void
262 m2_unknown (const char *s, struct type *type, struct ui_file *stream,
263             int show, int level)
264 {
265   fprintf_filtered (stream, "%s %s", s, _("is unknown"));
266 }
267
268 static void m2_union (struct type *type, struct ui_file *stream)
269 {
270   fprintf_filtered (stream, "union");
271 }
272
273 static void
274 m2_procedure (struct type *type, struct ui_file *stream,
275               int show, int level, const struct type_print_options *flags)
276 {
277   fprintf_filtered (stream, "PROCEDURE ");
278   m2_type_name (type, stream);
279   if (TYPE_CODE (TYPE_TARGET_TYPE (type)) != TYPE_CODE_VOID)
280     {
281       int i, len = TYPE_NFIELDS (type);
282
283       fprintf_filtered (stream, " (");
284       for (i = 0; i < len; i++)
285         {
286           if (i > 0)
287             {
288               fputs_filtered (", ", stream);
289               wrap_here ("    ");
290             }
291           m2_print_type (TYPE_FIELD_TYPE (type, i), "", stream, -1, 0, flags);
292         }
293       if (TYPE_TARGET_TYPE (type) != NULL)
294         {
295           fprintf_filtered (stream, " : ");
296           m2_print_type (TYPE_TARGET_TYPE (type), "", stream, 0, 0, flags);
297         }
298     }
299 }
300
301 static void
302 m2_print_bounds (struct type *type,
303                  struct ui_file *stream, int show, int level,
304                  int print_high)
305 {
306   struct type *target = TYPE_TARGET_TYPE (type);
307
308   if (TYPE_NFIELDS(type) == 0)
309     return;
310
311   if (print_high)
312     print_type_scalar (target, TYPE_HIGH_BOUND (type), stream);
313   else
314     print_type_scalar (target, TYPE_LOW_BOUND (type), stream);
315 }
316
317 static void
318 m2_short_set (struct type *type, struct ui_file *stream, int show, int level)
319 {
320   fprintf_filtered(stream, "SET [");
321   m2_print_bounds (TYPE_INDEX_TYPE (type), stream,
322                    show - 1, level, 0);
323
324   fprintf_filtered(stream, "..");
325   m2_print_bounds (TYPE_INDEX_TYPE (type), stream,
326                    show - 1, level, 1);
327   fprintf_filtered(stream, "]");
328 }
329
330 int
331 m2_is_long_set (struct type *type)
332 {
333   LONGEST previous_high = 0;  /* Unnecessary initialization
334                                  keeps gcc -Wall happy.  */
335   int len, i;
336   struct type *range;
337
338   if (TYPE_CODE (type) == TYPE_CODE_STRUCT)
339     {
340
341       /* check if all fields of the RECORD are consecutive sets.  */
342
343       len = TYPE_NFIELDS (type);
344       for (i = TYPE_N_BASECLASSES (type); i < len; i++)
345         {
346           if (TYPE_FIELD_TYPE (type, i) == NULL)
347             return 0;
348           if (TYPE_CODE (TYPE_FIELD_TYPE (type, i)) != TYPE_CODE_SET)
349             return 0;
350           if (TYPE_FIELD_NAME (type, i) != NULL
351               && (strcmp (TYPE_FIELD_NAME (type, i), "") != 0))
352             return 0;
353           range = TYPE_INDEX_TYPE (TYPE_FIELD_TYPE (type, i));
354           if ((i > TYPE_N_BASECLASSES (type))
355               && previous_high + 1 != TYPE_LOW_BOUND (range))
356             return 0;
357           previous_high = TYPE_HIGH_BOUND (range);
358         }
359       return len>0;
360     }
361   return 0;
362 }
363
364 /* m2_get_discrete_bounds - a wrapper for get_discrete_bounds which
365                             understands that CHARs might be signed.
366                             This should be integrated into gdbtypes.c
367                             inside get_discrete_bounds.  */
368
369 static int
370 m2_get_discrete_bounds (struct type *type, LONGEST *lowp, LONGEST *highp)
371 {
372   CHECK_TYPEDEF (type);
373   switch (TYPE_CODE (type))
374     {
375     case TYPE_CODE_CHAR:
376       if (TYPE_LENGTH (type) < sizeof (LONGEST))
377         {
378           if (!TYPE_UNSIGNED (type))
379             {
380               *lowp = -(1 << (TYPE_LENGTH (type) * TARGET_CHAR_BIT - 1));
381               *highp = -*lowp - 1;
382               return 0;
383             }
384         }
385       /* fall through */
386     default:
387       return get_discrete_bounds (type, lowp, highp);
388     }
389 }
390
391 /* m2_is_long_set_of_type - returns TRUE if the long set was declared as
392                             SET OF <oftype> of_type is assigned to the
393                             subtype.  */
394
395 int
396 m2_is_long_set_of_type (struct type *type, struct type **of_type)
397 {
398   int len, i;
399   struct type *range;
400   struct type *target;
401   LONGEST l1, l2;
402   LONGEST h1, h2;
403
404   if (TYPE_CODE (type) == TYPE_CODE_STRUCT)
405     {
406       len = TYPE_NFIELDS (type);
407       i = TYPE_N_BASECLASSES (type);
408       if (len == 0)
409         return 0;
410       range = TYPE_INDEX_TYPE (TYPE_FIELD_TYPE (type, i));
411       target = TYPE_TARGET_TYPE (range);
412
413       l1 = TYPE_LOW_BOUND (TYPE_INDEX_TYPE (TYPE_FIELD_TYPE (type, i)));
414       h1 = TYPE_HIGH_BOUND (TYPE_INDEX_TYPE (TYPE_FIELD_TYPE (type, len-1)));
415       *of_type = target;
416       if (m2_get_discrete_bounds (target, &l2, &h2) >= 0)
417         return (l1 == l2 && h1 == h2);
418       error (_("long_set failed to find discrete bounds for its subtype"));
419       return 0;
420     }
421   error (_("expecting long_set"));
422   return 0;
423 }
424
425 static int
426 m2_long_set (struct type *type, struct ui_file *stream, int show, int level,
427              const struct type_print_options *flags)
428 {
429   struct type *of_type;
430   int i;
431   int len = TYPE_NFIELDS (type);
432   LONGEST low;
433   LONGEST high;
434
435   if (m2_is_long_set (type))
436     {
437       if (TYPE_TAG_NAME (type) != NULL)
438         {
439           fputs_filtered (TYPE_TAG_NAME (type), stream);
440           if (show == 0)
441             return 1;
442         }
443       else if (TYPE_NAME (type) != NULL)
444         {
445           fputs_filtered (TYPE_NAME (type), stream);
446           if (show == 0)
447             return 1;
448         }
449
450       if (TYPE_TAG_NAME (type) != NULL || TYPE_NAME (type) != NULL)
451         fputs_filtered (" = ", stream);
452
453       if (get_long_set_bounds (type, &low, &high))
454         {
455           fprintf_filtered(stream, "SET OF ");
456           i = TYPE_N_BASECLASSES (type);
457           if (m2_is_long_set_of_type (type, &of_type))
458             m2_print_type (of_type, "", stream, show - 1, level, flags);
459           else
460             {
461               fprintf_filtered(stream, "[");
462               m2_print_bounds (TYPE_INDEX_TYPE (TYPE_FIELD_TYPE (type, i)),
463                                stream, show - 1, level, 0);
464
465               fprintf_filtered(stream, "..");
466
467               m2_print_bounds (TYPE_INDEX_TYPE (TYPE_FIELD_TYPE (type, len-1)),
468                                stream, show - 1, level, 1);
469               fprintf_filtered(stream, "]");
470             }
471         }
472       else
473         /* i18n: Do not translate the "SET OF" part!  */
474         fprintf_filtered(stream, _("SET OF <unknown>"));
475
476       return 1;
477     }
478   return 0;
479 }
480
481 /* m2_is_unbounded_array - returns TRUE if, type, should be regarded
482                            as a Modula-2 unbounded ARRAY type.  */
483
484 int
485 m2_is_unbounded_array (struct type *type)
486 {
487   if (TYPE_CODE (type) == TYPE_CODE_STRUCT)
488     {
489       /*
490        *  check if we have a structure with exactly two fields named
491        *  _m2_contents and _m2_high.  It also checks to see if the
492        *  type of _m2_contents is a pointer.  The TYPE_TARGET_TYPE
493        *  of the pointer determines the unbounded ARRAY OF type.
494        */
495       if (TYPE_NFIELDS (type) != 2)
496         return 0;
497       if (strcmp (TYPE_FIELD_NAME (type, 0), "_m2_contents") != 0)
498         return 0;
499       if (strcmp (TYPE_FIELD_NAME (type, 1), "_m2_high") != 0)
500         return 0;
501       if (TYPE_CODE (TYPE_FIELD_TYPE (type, 0)) != TYPE_CODE_PTR)
502         return 0;
503       return 1;
504     }
505   return 0;
506 }
507
508 /* m2_unbounded_array - if the struct type matches a Modula-2 unbounded
509                         parameter type then display the type as an
510                         ARRAY OF type.  Returns TRUE if an unbounded
511                         array type was detected.  */
512
513 static int
514 m2_unbounded_array (struct type *type, struct ui_file *stream, int show,
515                     int level, const struct type_print_options *flags)
516 {
517   if (m2_is_unbounded_array (type))
518     {
519       if (show > 0)
520         {
521           fputs_filtered ("ARRAY OF ", stream);
522           m2_print_type (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type, 0)),
523                          "", stream, 0, level, flags);
524         }
525       return 1;
526     }
527   return 0;
528 }
529
530 void
531 m2_record_fields (struct type *type, struct ui_file *stream, int show,
532                   int level, const struct type_print_options *flags)
533 {
534   /* Print the tag if it exists.  */
535   if (TYPE_TAG_NAME (type) != NULL)
536     {
537       if (strncmp (TYPE_TAG_NAME (type), "$$", 2) != 0)
538         {
539           fputs_filtered (TYPE_TAG_NAME (type), stream);
540           if (show > 0)
541             fprintf_filtered (stream, " = ");
542         }
543     }
544   wrap_here ("    ");
545   if (show < 0)
546     {
547       if (TYPE_CODE (type) == TYPE_CODE_STRUCT)
548         fprintf_filtered (stream, "RECORD ... END ");
549       else if (TYPE_CODE (type) == TYPE_CODE_UNION)
550         fprintf_filtered (stream, "CASE ... END ");
551     }
552   else if (show > 0)
553     {
554       int i;
555       int len = TYPE_NFIELDS (type);
556
557       if (TYPE_CODE (type) == TYPE_CODE_STRUCT)
558         fprintf_filtered (stream, "RECORD\n");
559       else if (TYPE_CODE (type) == TYPE_CODE_UNION)
560         /* i18n: Do not translate "CASE" and "OF".  */
561         fprintf_filtered (stream, _("CASE <variant> OF\n"));
562
563       for (i = TYPE_N_BASECLASSES (type); i < len; i++)
564         {
565           QUIT;
566
567           print_spaces_filtered (level + 4, stream);
568           fputs_filtered (TYPE_FIELD_NAME (type, i), stream);
569           fputs_filtered (" : ", stream);
570           m2_print_type (TYPE_FIELD_TYPE (type, i),
571                          "",
572                          stream, 0, level + 4, flags);
573           if (TYPE_FIELD_PACKED (type, i))
574             {
575               /* It is a bitfield.  This code does not attempt
576                  to look at the bitpos and reconstruct filler,
577                  unnamed fields.  This would lead to misleading
578                  results if the compiler does not put out fields
579                  for such things (I don't know what it does).  */
580               fprintf_filtered (stream, " : %d",
581                                 TYPE_FIELD_BITSIZE (type, i));
582             }
583           fprintf_filtered (stream, ";\n");
584         }
585       
586       fprintfi_filtered (level, stream, "END ");
587     }
588 }
589
590 void
591 m2_enum (struct type *type, struct ui_file *stream, int show, int level)
592 {
593   LONGEST lastval;
594   int i, len;
595
596   if (show < 0)
597     {
598       /* If we just printed a tag name, no need to print anything else.  */
599       if (TYPE_TAG_NAME (type) == NULL)
600         fprintf_filtered (stream, "(...)");
601     }
602   else if (show > 0 || TYPE_TAG_NAME (type) == NULL)
603     {
604       fprintf_filtered (stream, "(");
605       len = TYPE_NFIELDS (type);
606       lastval = 0;
607       for (i = 0; i < len; i++)
608         {
609           QUIT;
610           if (i > 0)
611             fprintf_filtered (stream, ", ");
612           wrap_here ("    ");
613           fputs_filtered (TYPE_FIELD_NAME (type, i), stream);
614           if (lastval != TYPE_FIELD_ENUMVAL (type, i))
615             {
616               fprintf_filtered (stream, " = %s",
617                                 plongest (TYPE_FIELD_ENUMVAL (type, i)));
618               lastval = TYPE_FIELD_ENUMVAL (type, i);
619             }
620           lastval++;
621         }
622       fprintf_filtered (stream, ")");
623     }
624 }