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