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