* valprint.c (val_print): Update.
[external/binutils.git] / gdb / m2-valprint.c
1 /* Support for printing Modula 2 values for GDB, the GNU debugger.
2
3    Copyright (C) 1986, 1988-1989, 1991-1992, 1996, 1998, 2000, 2005-2012
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 3 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, see <http://www.gnu.org/licenses/>.  */
20
21 #include "defs.h"
22 #include "symtab.h"
23 #include "gdbtypes.h"
24 #include "expression.h"
25 #include "value.h"
26 #include "valprint.h"
27 #include "language.h"
28 #include "typeprint.h"
29 #include "c-lang.h"
30 #include "m2-lang.h"
31 #include "target.h"
32
33 static int print_unpacked_pointer (struct type *type,
34                                    CORE_ADDR address, CORE_ADDR addr,
35                                    const struct value_print_options *options,
36                                    struct ui_file *stream);
37 static void
38 m2_print_array_contents (struct type *type, const gdb_byte *valaddr,
39                          int embedded_offset, CORE_ADDR address,
40                          struct ui_file *stream, int recurse,
41                          const struct value *val,
42                          const struct value_print_options *options,
43                          int len);
44
45
46 /* get_long_set_bounds - assigns the bounds of the long set to low and
47                          high.  */
48
49 int
50 get_long_set_bounds (struct type *type, LONGEST *low, LONGEST *high)
51 {
52   int len, i;
53
54   if (TYPE_CODE (type) == TYPE_CODE_STRUCT)
55     {
56       len = TYPE_NFIELDS (type);
57       i = TYPE_N_BASECLASSES (type);
58       if (len == 0)
59         return 0;
60       *low = TYPE_LOW_BOUND (TYPE_INDEX_TYPE (TYPE_FIELD_TYPE (type, i)));
61       *high = TYPE_HIGH_BOUND (TYPE_INDEX_TYPE (TYPE_FIELD_TYPE (type,
62                                                                  len-1)));
63       return 1;
64     }
65   error (_("expecting long_set"));
66   return 0;
67 }
68
69 static void
70 m2_print_long_set (struct type *type, const gdb_byte *valaddr,
71                    int embedded_offset, CORE_ADDR address,
72                    struct ui_file *stream)
73 {
74   int empty_set        = 1;
75   int element_seen     = 0;
76   LONGEST previous_low = 0;
77   LONGEST previous_high= 0;
78   LONGEST i, low_bound, high_bound;
79   LONGEST field_low, field_high;
80   struct type *range;
81   int len, field;
82   struct type *target;
83   int bitval;
84
85   CHECK_TYPEDEF (type);
86
87   fprintf_filtered (stream, "{");
88   len = TYPE_NFIELDS (type);
89   if (get_long_set_bounds (type, &low_bound, &high_bound))
90     {
91       field = TYPE_N_BASECLASSES (type);
92       range = TYPE_INDEX_TYPE (TYPE_FIELD_TYPE (type, field));
93     }
94   else
95     {
96       fprintf_filtered (stream, " %s }", _("<unknown bounds of set>"));
97       return;
98     }
99
100   target = TYPE_TARGET_TYPE (range);
101
102   if (get_discrete_bounds (range, &field_low, &field_high) >= 0)
103     {
104       for (i = low_bound; i <= high_bound; i++)
105         {
106           bitval = value_bit_index (TYPE_FIELD_TYPE (type, field),
107                                     (TYPE_FIELD_BITPOS (type, field) / 8) +
108                                     valaddr + embedded_offset, i);
109           if (bitval < 0)
110             error (_("bit test is out of range"));
111           else if (bitval > 0)
112             {
113               previous_high = i;
114               if (! element_seen)
115                 {
116                   if (! empty_set)
117                     fprintf_filtered (stream, ", ");
118                   print_type_scalar (target, i, stream);
119                   empty_set    = 0;
120                   element_seen = 1;
121                   previous_low = i;
122                 }
123             }
124           else
125             {
126               /* bit is not set */
127               if (element_seen)
128                 {
129                   if (previous_low+1 < previous_high)
130                     fprintf_filtered (stream, "..");
131                   if (previous_low+1 < previous_high)
132                     print_type_scalar (target, previous_high, stream);
133                   element_seen = 0;
134                 }
135             }
136           if (i == field_high)
137             {
138               field++;
139               if (field == len)
140                 break;
141               range = TYPE_INDEX_TYPE (TYPE_FIELD_TYPE (type, field));
142               if (get_discrete_bounds (range, &field_low, &field_high) < 0)
143                 break;
144               target = TYPE_TARGET_TYPE (range);
145             }
146         }
147       if (element_seen)
148         {
149           if (previous_low+1 < previous_high)
150             {
151               fprintf_filtered (stream, "..");
152               print_type_scalar (target, previous_high, stream);
153             }
154           element_seen = 0;
155         }
156       fprintf_filtered (stream, "}");
157     }
158 }
159
160 static void
161 m2_print_unbounded_array (struct type *type, const gdb_byte *valaddr,
162                           int embedded_offset, CORE_ADDR address,
163                           struct ui_file *stream, int recurse,
164                           const struct value_print_options *options)
165 {
166   struct type *content_type;
167   CORE_ADDR addr;
168   LONGEST len;
169   struct value *val;
170
171   CHECK_TYPEDEF (type);
172   content_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type, 0));
173
174   addr = unpack_pointer (TYPE_FIELD_TYPE (type, 0),
175                          (TYPE_FIELD_BITPOS (type, 0) / 8) +
176                          valaddr + embedded_offset);
177
178   val = value_at_lazy (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type, 0)),
179                        addr);
180   len = unpack_field_as_long (type, valaddr + embedded_offset, 1);
181
182   fprintf_filtered (stream, "{");  
183   m2_print_array_contents (value_type (val),
184                            value_contents_for_printing (val),
185                            value_embedded_offset (val), addr, stream,
186                            recurse, val, options, len);
187   fprintf_filtered (stream, ", HIGH = %d}", (int) len);
188 }
189
190 static int
191 print_unpacked_pointer (struct type *type,
192                         CORE_ADDR address, CORE_ADDR addr,
193                         const struct value_print_options *options,
194                         struct ui_file *stream)
195 {
196   struct gdbarch *gdbarch = get_type_arch (type);
197   struct type *elttype = check_typedef (TYPE_TARGET_TYPE (type));
198
199   if (TYPE_CODE (elttype) == TYPE_CODE_FUNC)
200     {
201       /* Try to print what function it points to.  */
202       print_function_pointer_address (gdbarch, addr, stream,
203                                       options->addressprint);
204       /* Return value is irrelevant except for string pointers.  */
205       return 0;
206     }
207
208   if (options->addressprint && options->format != 's')
209     fputs_filtered (paddress (gdbarch, address), stream);
210
211   /* For a pointer to char or unsigned char, also print the string
212      pointed to, unless pointer is null.  */
213
214   if (TYPE_LENGTH (elttype) == 1
215       && TYPE_CODE (elttype) == TYPE_CODE_INT
216       && (options->format == 0 || options->format == 's')
217       && addr != 0)
218     return val_print_string (TYPE_TARGET_TYPE (type), NULL, addr, -1,
219                              stream, options);
220   
221   return 0;
222 }
223
224 static void
225 print_variable_at_address (struct type *type,
226                            const gdb_byte *valaddr,
227                            struct ui_file *stream,
228                            int recurse,
229                            const struct value_print_options *options)
230 {
231   struct gdbarch *gdbarch = get_type_arch (type);
232   CORE_ADDR addr = unpack_pointer (type, valaddr);
233   struct type *elttype = check_typedef (TYPE_TARGET_TYPE (type));
234
235   fprintf_filtered (stream, "[");
236   fputs_filtered (paddress (gdbarch, addr), stream);
237   fprintf_filtered (stream, "] : ");
238   
239   if (TYPE_CODE (elttype) != TYPE_CODE_UNDEF)
240     {
241       struct value *deref_val =
242         value_at (TYPE_TARGET_TYPE (type), unpack_pointer (type, valaddr));
243
244       common_val_print (deref_val, stream, recurse, options, current_language);
245     }
246   else
247     fputs_filtered ("???", stream);
248 }
249
250
251 /* m2_print_array_contents - prints out the contents of an
252                              array up to a max_print values.
253                              It prints arrays of char as a string
254                              and all other data types as comma
255                              separated values.  */
256
257 static void
258 m2_print_array_contents (struct type *type, const gdb_byte *valaddr,
259                          int embedded_offset, CORE_ADDR address,
260                          struct ui_file *stream, int recurse,
261                          const struct value *val,
262                          const struct value_print_options *options,
263                          int len)
264 {
265   int eltlen;
266   CHECK_TYPEDEF (type);
267
268   if (TYPE_LENGTH (type) > 0)
269     {
270       eltlen = TYPE_LENGTH (type);
271       if (options->prettyprint_arrays)
272         print_spaces_filtered (2 + 2 * recurse, stream);
273       /* For an array of chars, print with string syntax.  */
274       if (eltlen == 1 &&
275           ((TYPE_CODE (type) == TYPE_CODE_INT)
276            || ((current_language->la_language == language_m2)
277                && (TYPE_CODE (type) == TYPE_CODE_CHAR)))
278           && (options->format == 0 || options->format == 's'))
279         val_print_string (type, NULL, address, len+1, stream, options);
280       else
281         {
282           fprintf_filtered (stream, "{");
283           val_print_array_elements (type, valaddr, embedded_offset,
284                                     address, stream, recurse, val,
285                                     options, 0);
286           fprintf_filtered (stream, "}");
287         }
288     }
289 }
290
291
292 /* See val_print for a description of the various parameters of this
293    function; they are identical.  */
294
295 void
296 m2_val_print (struct type *type, const gdb_byte *valaddr, int embedded_offset,
297               CORE_ADDR address, struct ui_file *stream, int recurse,
298               const struct value *original_value,
299               const struct value_print_options *options)
300 {
301   struct gdbarch *gdbarch = get_type_arch (type);
302   unsigned int i = 0;   /* Number of characters printed.  */
303   unsigned len;
304   struct type *elttype;
305   unsigned eltlen;
306   LONGEST val;
307   CORE_ADDR addr;
308
309   CHECK_TYPEDEF (type);
310   switch (TYPE_CODE (type))
311     {
312     case TYPE_CODE_ARRAY:
313       if (TYPE_LENGTH (type) > 0 && TYPE_LENGTH (TYPE_TARGET_TYPE (type)) > 0)
314         {
315           elttype = check_typedef (TYPE_TARGET_TYPE (type));
316           eltlen = TYPE_LENGTH (elttype);
317           len = TYPE_LENGTH (type) / eltlen;
318           if (options->prettyprint_arrays)
319             print_spaces_filtered (2 + 2 * recurse, stream);
320           /* For an array of chars, print with string syntax.  */
321           if (eltlen == 1 &&
322               ((TYPE_CODE (elttype) == TYPE_CODE_INT)
323                || ((current_language->la_language == language_m2)
324                    && (TYPE_CODE (elttype) == TYPE_CODE_CHAR)))
325               && (options->format == 0 || options->format == 's'))
326             {
327               /* If requested, look for the first null char and only print
328                  elements up to it.  */
329               if (options->stop_print_at_null)
330                 {
331                   unsigned int temp_len;
332
333                   /* Look for a NULL char.  */
334                   for (temp_len = 0;
335                        (valaddr + embedded_offset)[temp_len]
336                          && temp_len < len && temp_len < options->print_max;
337                        temp_len++);
338                   len = temp_len;
339                 }
340
341               LA_PRINT_STRING (stream, TYPE_TARGET_TYPE (type),
342                                valaddr + embedded_offset, len, NULL,
343                                0, options);
344               i = len;
345             }
346           else
347             {
348               fprintf_filtered (stream, "{");
349               val_print_array_elements (type, valaddr, embedded_offset,
350                                         address, stream,
351                                         recurse, original_value,
352                                         options, 0);
353               fprintf_filtered (stream, "}");
354             }
355           break;
356         }
357       /* Array of unspecified length: treat like pointer to first elt.  */
358       print_unpacked_pointer (type, address, address, options, stream);
359       break;
360
361     case TYPE_CODE_PTR:
362       if (TYPE_CONST (type))
363         print_variable_at_address (type, valaddr + embedded_offset,
364                                    stream, recurse, options);
365       else if (options->format && options->format != 's')
366         val_print_scalar_formatted (type, valaddr, embedded_offset,
367                                     original_value, options, 0, stream);
368       else
369         {
370           addr = unpack_pointer (type, valaddr + embedded_offset);
371           print_unpacked_pointer (type, addr, address, options, stream);
372         }
373       break;
374
375     case TYPE_CODE_REF:
376       elttype = check_typedef (TYPE_TARGET_TYPE (type));
377       if (options->addressprint)
378         {
379           CORE_ADDR addr
380             = extract_typed_address (valaddr + embedded_offset, type);
381
382           fprintf_filtered (stream, "@");
383           fputs_filtered (paddress (gdbarch, addr), stream);
384           if (options->deref_ref)
385             fputs_filtered (": ", stream);
386         }
387       /* De-reference the reference.  */
388       if (options->deref_ref)
389         {
390           if (TYPE_CODE (elttype) != TYPE_CODE_UNDEF)
391             {
392               struct value *deref_val =
393                 value_at
394                 (TYPE_TARGET_TYPE (type),
395                  unpack_pointer (type, valaddr + embedded_offset));
396
397               common_val_print (deref_val, stream, recurse, options,
398                                 current_language);
399             }
400           else
401             fputs_filtered ("???", stream);
402         }
403       break;
404
405     case TYPE_CODE_UNION:
406       if (recurse && !options->unionprint)
407         {
408           fprintf_filtered (stream, "{...}");
409           break;
410         }
411       /* Fall through.  */
412     case TYPE_CODE_STRUCT:
413       if (m2_is_long_set (type))
414         m2_print_long_set (type, valaddr, embedded_offset, address,
415                            stream);
416       else if (m2_is_unbounded_array (type))
417         m2_print_unbounded_array (type, valaddr, embedded_offset,
418                                   address, stream, recurse, options);
419       else
420         cp_print_value_fields (type, type, valaddr, embedded_offset,
421                                address, stream, recurse, original_value,
422                                options, NULL, 0);
423       break;
424
425     case TYPE_CODE_ENUM:
426       if (options->format)
427         {
428           val_print_scalar_formatted (type, valaddr, embedded_offset,
429                                       original_value, options, 0, stream);
430           break;
431         }
432       len = TYPE_NFIELDS (type);
433       val = unpack_long (type, valaddr + embedded_offset);
434       for (i = 0; i < len; i++)
435         {
436           QUIT;
437           if (val == TYPE_FIELD_BITPOS (type, i))
438             {
439               break;
440             }
441         }
442       if (i < len)
443         {
444           fputs_filtered (TYPE_FIELD_NAME (type, i), stream);
445         }
446       else
447         {
448           print_longest (stream, 'd', 0, val);
449         }
450       break;
451
452     case TYPE_CODE_FUNC:
453       if (options->format)
454         {
455           val_print_scalar_formatted (type, valaddr, embedded_offset,
456                                       original_value, options, 0, stream);
457           break;
458         }
459       /* FIXME, we should consider, at least for ANSI C language, eliminating
460          the distinction made between FUNCs and POINTERs to FUNCs.  */
461       fprintf_filtered (stream, "{");
462       type_print (type, "", stream, -1);
463       fprintf_filtered (stream, "} ");
464       /* Try to print what function it points to, and its address.  */
465       print_address_demangle (gdbarch, address, stream, demangle);
466       break;
467
468     case TYPE_CODE_BOOL:
469       if (options->format || options->output_format)
470         {
471           struct value_print_options opts = *options;
472
473           opts.format = (options->format ? options->format
474                          : options->output_format);
475           val_print_scalar_formatted (type, valaddr, embedded_offset,
476                                       original_value, &opts, 0, stream);
477         }
478       else
479         {
480           val = unpack_long (type, valaddr + embedded_offset);
481           if (val == 0)
482             fputs_filtered ("FALSE", stream);
483           else if (val == 1)
484             fputs_filtered ("TRUE", stream);
485           else
486             fprintf_filtered (stream, "%ld)", (long int) val);
487         }
488       break;
489
490     case TYPE_CODE_RANGE:
491       if (TYPE_LENGTH (type) == TYPE_LENGTH (TYPE_TARGET_TYPE (type)))
492         {
493           m2_val_print (TYPE_TARGET_TYPE (type), valaddr, embedded_offset,
494                         address, stream, recurse, original_value, options);
495           break;
496         }
497       /* FIXME: create_range_type does not set the unsigned bit in a
498          range type (I think it probably should copy it from the target
499          type), so we won't print values which are too large to
500          fit in a signed integer correctly.  */
501       /* FIXME: Doesn't handle ranges of enums correctly.  (Can't just
502          print with the target type, though, because the size of our type
503          and the target type might differ).  */
504       /* FALLTHROUGH */
505
506     case TYPE_CODE_INT:
507       if (options->format || options->output_format)
508         {
509           struct value_print_options opts = *options;
510
511           opts.format = (options->format ? options->format
512                          : options->output_format);
513           val_print_scalar_formatted (type, valaddr, embedded_offset,
514                                       original_value, &opts, 0, stream);
515         }
516       else
517         val_print_type_code_int (type, valaddr + embedded_offset, stream);
518       break;
519
520     case TYPE_CODE_CHAR:
521       if (options->format || options->output_format)
522         {
523           struct value_print_options opts = *options;
524
525           opts.format = (options->format ? options->format
526                          : options->output_format);
527           val_print_scalar_formatted (type, valaddr, embedded_offset,
528                                       original_value, &opts, 0, stream);
529         }
530       else
531         {
532           val = unpack_long (type, valaddr + embedded_offset);
533           if (TYPE_UNSIGNED (type))
534             fprintf_filtered (stream, "%u", (unsigned int) val);
535           else
536             fprintf_filtered (stream, "%d", (int) val);
537           fputs_filtered (" ", stream);
538           LA_PRINT_CHAR ((unsigned char) val, type, stream);
539         }
540       break;
541
542     case TYPE_CODE_FLT:
543       if (options->format)
544         val_print_scalar_formatted (type, valaddr, embedded_offset,
545                                     original_value, options, 0, stream);
546       else
547         print_floating (valaddr + embedded_offset, type, stream);
548       break;
549
550     case TYPE_CODE_METHOD:
551       break;
552
553     case TYPE_CODE_BITSTRING:
554     case TYPE_CODE_SET:
555       elttype = TYPE_INDEX_TYPE (type);
556       CHECK_TYPEDEF (elttype);
557       if (TYPE_STUB (elttype))
558         {
559           fprintf_filtered (stream, _("<incomplete type>"));
560           gdb_flush (stream);
561           break;
562         }
563       else
564         {
565           struct type *range = elttype;
566           LONGEST low_bound, high_bound;
567           int i;
568           int is_bitstring = TYPE_CODE (type) == TYPE_CODE_BITSTRING;
569           int need_comma = 0;
570
571           if (is_bitstring)
572             fputs_filtered ("B'", stream);
573           else
574             fputs_filtered ("{", stream);
575
576           i = get_discrete_bounds (range, &low_bound, &high_bound);
577         maybe_bad_bstring:
578           if (i < 0)
579             {
580               fputs_filtered (_("<error value>"), stream);
581               goto done;
582             }
583
584           for (i = low_bound; i <= high_bound; i++)
585             {
586               int element = value_bit_index (type, valaddr + embedded_offset,
587                                              i);
588
589               if (element < 0)
590                 {
591                   i = element;
592                   goto maybe_bad_bstring;
593                 }
594               if (is_bitstring)
595                 fprintf_filtered (stream, "%d", element);
596               else if (element)
597                 {
598                   if (need_comma)
599                     fputs_filtered (", ", stream);
600                   print_type_scalar (range, i, stream);
601                   need_comma = 1;
602
603                   if (i + 1 <= high_bound
604                       && value_bit_index (type, valaddr + embedded_offset,
605                                           ++i))
606                     {
607                       int j = i;
608
609                       fputs_filtered ("..", stream);
610                       while (i + 1 <= high_bound
611                              && value_bit_index (type,
612                                                  valaddr + embedded_offset,
613                                                  ++i))
614                         j = i;
615                       print_type_scalar (range, j, stream);
616                     }
617                 }
618             }
619         done:
620           if (is_bitstring)
621             fputs_filtered ("'", stream);
622           else
623             fputs_filtered ("}", stream);
624         }
625       break;
626
627     case TYPE_CODE_VOID:
628       fprintf_filtered (stream, "void");
629       break;
630
631     case TYPE_CODE_ERROR:
632       fprintf_filtered (stream, "%s", TYPE_ERROR_NAME (type));
633       break;
634
635     case TYPE_CODE_UNDEF:
636       /* This happens (without TYPE_FLAG_STUB set) on systems which don't use
637          dbx xrefs (NO_DBX_XREFS in gcc) if a file has a "struct foo *bar"
638          and no complete type for struct foo in that file.  */
639       fprintf_filtered (stream, _("<incomplete type>"));
640       break;
641
642     default:
643       error (_("Invalid m2 type code %d in symbol table."), TYPE_CODE (type));
644     }
645   gdb_flush (stream);
646 }