This commit was generated by cvs2svn to track changes on a CVS vendor
[platform/upstream/binutils.git] / gdb / ch-valprint.c
1 /* Support for printing Chill values for GDB, the GNU debugger.
2    Copyright 1986, 1988, 1989, 1991-1994, 2000
3    Free Software Foundation, Inc.
4
5    This file is part of GDB.
6
7    This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 2 of the License, or
10    (at your option) any later version.
11
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16
17    You should have received a copy of the GNU General Public License
18    along with this program; if not, write to the Free Software
19    Foundation, Inc., 59 Temple Place - Suite 330,
20    Boston, MA 02111-1307, USA.  */
21
22 #include "defs.h"
23 #include "obstack.h"
24 #include "symtab.h"
25 #include "gdbtypes.h"
26 #include "valprint.h"
27 #include "expression.h"
28 #include "value.h"
29 #include "language.h"
30 #include "demangle.h"
31 #include "c-lang.h"             /* For c_val_print */
32 #include "typeprint.h"
33 #include "ch-lang.h"
34 #include "annotate.h"
35
36 static void chill_print_value_fields (struct type *, char *,
37                                       struct ui_file *, int, int,
38                                       enum val_prettyprint, struct type **);
39
40 static void chill_print_type_scalar (struct type *, LONGEST,
41                                      struct ui_file *);
42
43 static void chill_val_print_array_elements (struct type *, char *,
44                                             CORE_ADDR, struct ui_file *,
45                                             int, int, int,
46                                             enum val_prettyprint);
47 \f
48
49 /* Print integral scalar data VAL, of type TYPE, onto stdio stream STREAM.
50    Used to print data from type structures in a specified type.  For example,
51    array bounds may be characters or booleans in some languages, and this
52    allows the ranges to be printed in their "natural" form rather than as
53    decimal integer values. */
54
55 static void
56 chill_print_type_scalar (type, val, stream)
57      struct type *type;
58      LONGEST val;
59      struct ui_file *stream;
60 {
61   switch (TYPE_CODE (type))
62     {
63     case TYPE_CODE_RANGE:
64       if (TYPE_TARGET_TYPE (type))
65         {
66           chill_print_type_scalar (TYPE_TARGET_TYPE (type), val, stream);
67           return;
68         }
69       break;
70     case TYPE_CODE_UNDEF:
71     case TYPE_CODE_PTR:
72     case TYPE_CODE_ARRAY:
73     case TYPE_CODE_STRUCT:
74     case TYPE_CODE_UNION:
75     case TYPE_CODE_ENUM:
76     case TYPE_CODE_FUNC:
77     case TYPE_CODE_INT:
78     case TYPE_CODE_FLT:
79     case TYPE_CODE_VOID:
80     case TYPE_CODE_SET:
81     case TYPE_CODE_STRING:
82     case TYPE_CODE_BITSTRING:
83     case TYPE_CODE_ERROR:
84     case TYPE_CODE_MEMBER:
85     case TYPE_CODE_METHOD:
86     case TYPE_CODE_REF:
87     case TYPE_CODE_CHAR:
88     case TYPE_CODE_BOOL:
89     case TYPE_CODE_COMPLEX:
90     case TYPE_CODE_TYPEDEF:
91     default:
92       break;
93     }
94   print_type_scalar (type, val, stream);
95 }
96 \f
97 /* Print the elements of an array.
98    Similar to val_print_array_elements, but prints
99    element indexes (in Chill syntax). */
100
101 static void
102 chill_val_print_array_elements (type, valaddr, address, stream,
103                                 format, deref_ref, recurse, pretty)
104      struct type *type;
105      char *valaddr;
106      CORE_ADDR address;
107      struct ui_file *stream;
108      int format;
109      int deref_ref;
110      int recurse;
111      enum val_prettyprint pretty;
112 {
113   unsigned int i = 0;
114   unsigned int things_printed = 0;
115   unsigned len;
116   struct type *elttype;
117   struct type *range_type = TYPE_FIELD_TYPE (type, 0);
118   struct type *index_type = TYPE_TARGET_TYPE (range_type);
119   unsigned eltlen;
120   /* Position of the array element we are examining to see
121      whether it is repeated.  */
122   unsigned int rep1;
123   /* Number of repetitions we have detected so far.  */
124   unsigned int reps;
125   LONGEST low_bound = TYPE_FIELD_BITPOS (range_type, 0);
126
127   elttype = check_typedef (TYPE_TARGET_TYPE (type));
128   eltlen = TYPE_LENGTH (elttype);
129   len = TYPE_LENGTH (type) / eltlen;
130
131   annotate_array_section_begin (i, elttype);
132
133   for (; i < len && things_printed < print_max; i++)
134     {
135       if (i != 0)
136         {
137           if (prettyprint_arrays)
138             {
139               fprintf_filtered (stream, ",\n");
140               print_spaces_filtered (2 + 2 * recurse, stream);
141             }
142           else
143             {
144               fprintf_filtered (stream, ", ");
145             }
146         }
147       wrap_here (n_spaces (2 + 2 * recurse));
148
149       rep1 = i + 1;
150       reps = 1;
151       while ((rep1 < len) &&
152              !memcmp (valaddr + i * eltlen, valaddr + rep1 * eltlen, eltlen))
153         {
154           ++reps;
155           ++rep1;
156         }
157
158       fputs_filtered ("(", stream);
159       chill_print_type_scalar (index_type, low_bound + i, stream);
160       if (reps > 1)
161         {
162           fputs_filtered (":", stream);
163           chill_print_type_scalar (index_type, low_bound + i + reps - 1,
164                                    stream);
165           fputs_filtered ("): ", stream);
166           val_print (elttype, valaddr + i * eltlen, 0, 0, stream, format,
167                      deref_ref, recurse + 1, pretty);
168
169           i = rep1 - 1;
170           things_printed += 1;
171         }
172       else
173         {
174           fputs_filtered ("): ", stream);
175           val_print (elttype, valaddr + i * eltlen, 0, 0, stream, format,
176                      deref_ref, recurse + 1, pretty);
177           annotate_elt ();
178           things_printed++;
179         }
180     }
181   annotate_array_section_end ();
182   if (i < len)
183     {
184       fprintf_filtered (stream, "...");
185     }
186 }
187
188 /* Print data of type TYPE located at VALADDR (within GDB), which came from
189    the inferior at address ADDRESS, onto stdio stream STREAM according to
190    FORMAT (a letter or 0 for natural format).  The data at VALADDR is in
191    target byte order.
192
193    If the data are a string pointer, returns the number of string characters
194    printed.
195
196    If DEREF_REF is nonzero, then dereference references, otherwise just print
197    them like pointers.
198
199    The PRETTY parameter controls prettyprinting.  */
200
201 int
202 chill_val_print (type, valaddr, embedded_offset, address,
203                  stream, format, deref_ref, recurse, pretty)
204      struct type *type;
205      char *valaddr;
206      int embedded_offset;
207      CORE_ADDR address;
208      struct ui_file *stream;
209      int format;
210      int deref_ref;
211      int recurse;
212      enum val_prettyprint pretty;
213 {
214   LONGEST val;
215   unsigned int i = 0;           /* Number of characters printed.  */
216   struct type *elttype;
217   CORE_ADDR addr;
218
219   CHECK_TYPEDEF (type);
220
221   switch (TYPE_CODE (type))
222     {
223     case TYPE_CODE_ARRAY:
224       if (TYPE_LENGTH (type) > 0 && TYPE_LENGTH (TYPE_TARGET_TYPE (type)) > 0)
225         {
226           if (prettyprint_arrays)
227             {
228               print_spaces_filtered (2 + 2 * recurse, stream);
229             }
230           fprintf_filtered (stream, "[");
231           chill_val_print_array_elements (type, valaddr, address, stream,
232                                         format, deref_ref, recurse, pretty);
233           fprintf_filtered (stream, "]");
234         }
235       else
236         {
237           error ("unimplemented in chill_val_print; unspecified array length");
238         }
239       break;
240
241     case TYPE_CODE_INT:
242       format = format ? format : output_format;
243       if (format)
244         {
245           print_scalar_formatted (valaddr, type, format, 0, stream);
246         }
247       else
248         {
249           val_print_type_code_int (type, valaddr, stream);
250         }
251       break;
252
253     case TYPE_CODE_CHAR:
254       format = format ? format : output_format;
255       if (format)
256         {
257           print_scalar_formatted (valaddr, type, format, 0, stream);
258         }
259       else
260         {
261           LA_PRINT_CHAR ((unsigned char) unpack_long (type, valaddr),
262                          stream);
263         }
264       break;
265
266     case TYPE_CODE_FLT:
267       if (format)
268         {
269           print_scalar_formatted (valaddr, type, format, 0, stream);
270         }
271       else
272         {
273           print_floating (valaddr, type, stream);
274         }
275       break;
276
277     case TYPE_CODE_BOOL:
278       format = format ? format : output_format;
279       if (format)
280         {
281           print_scalar_formatted (valaddr, type, format, 0, stream);
282         }
283       else
284         {
285           /* FIXME: Why is this using builtin_type_chill_bool not type?  */
286           val = unpack_long (builtin_type_chill_bool, valaddr);
287           fprintf_filtered (stream, val ? "TRUE" : "FALSE");
288         }
289       break;
290
291     case TYPE_CODE_UNDEF:
292       /* This happens (without TYPE_FLAG_STUB set) on systems which don't use
293          dbx xrefs (NO_DBX_XREFS in gcc) if a file has a "struct foo *bar"
294          and no complete type for struct foo in that file.  */
295       fprintf_filtered (stream, "<incomplete type>");
296       break;
297
298     case TYPE_CODE_PTR:
299       if (format && format != 's')
300         {
301           print_scalar_formatted (valaddr, type, format, 0, stream);
302           break;
303         }
304       addr = unpack_pointer (type, valaddr);
305       elttype = check_typedef (TYPE_TARGET_TYPE (type));
306
307       /* We assume a NULL pointer is all zeros ... */
308       if (addr == 0)
309         {
310           fputs_filtered ("NULL", stream);
311           return 0;
312         }
313
314       if (TYPE_CODE (elttype) == TYPE_CODE_FUNC)
315         {
316           /* Try to print what function it points to.  */
317           print_address_demangle (addr, stream, demangle);
318           /* Return value is irrelevant except for string pointers.  */
319           return (0);
320         }
321       if (addressprint && format != 's')
322         {
323           print_address_numeric (addr, 1, stream);
324         }
325
326       /* For a pointer to char or unsigned char, also print the string
327          pointed to, unless pointer is null.  */
328       if (TYPE_LENGTH (elttype) == 1
329           && TYPE_CODE (elttype) == TYPE_CODE_CHAR
330           && (format == 0 || format == 's')
331           && addr != 0
332           &&                    /* If print_max is UINT_MAX, the alloca below will fail.
333                                    In that case don't try to print the string.  */
334           print_max < UINT_MAX)
335         i = val_print_string (addr, -1, TYPE_LENGTH (elttype), stream);
336
337       /* Return number of characters printed, plus one for the
338          terminating null if we have "reached the end".  */
339       return (i + (print_max && i != print_max));
340       break;
341
342     case TYPE_CODE_STRING:
343       i = TYPE_LENGTH (type);
344       LA_PRINT_STRING (stream, valaddr, i, 1, 0);
345       /* Return number of characters printed, plus one for the terminating
346          null if we have "reached the end".  */
347       return (i + (print_max && i != print_max));
348       break;
349
350     case TYPE_CODE_BITSTRING:
351     case TYPE_CODE_SET:
352       elttype = TYPE_INDEX_TYPE (type);
353       CHECK_TYPEDEF (elttype);
354       if (TYPE_FLAGS (elttype) & TYPE_FLAG_STUB)
355         {
356           fprintf_filtered (stream, "<incomplete type>");
357           gdb_flush (stream);
358           break;
359         }
360       {
361         struct type *range = elttype;
362         LONGEST low_bound, high_bound;
363         int i;
364         int is_bitstring = TYPE_CODE (type) == TYPE_CODE_BITSTRING;
365         int need_comma = 0;
366
367         if (is_bitstring)
368           fputs_filtered ("B'", stream);
369         else
370           fputs_filtered ("[", stream);
371
372         i = get_discrete_bounds (range, &low_bound, &high_bound);
373       maybe_bad_bstring:
374         if (i < 0)
375           {
376             fputs_filtered ("<error value>", stream);
377             goto done;
378           }
379
380         for (i = low_bound; i <= high_bound; i++)
381           {
382             int element = value_bit_index (type, valaddr, i);
383             if (element < 0)
384               {
385                 i = element;
386                 goto maybe_bad_bstring;
387               }
388             if (is_bitstring)
389               fprintf_filtered (stream, "%d", element);
390             else if (element)
391               {
392                 if (need_comma)
393                   fputs_filtered (", ", stream);
394                 chill_print_type_scalar (range, (LONGEST) i, stream);
395                 need_comma = 1;
396
397                 /* Look for a continuous range of true elements. */
398                 if (i + 1 <= high_bound && value_bit_index (type, valaddr, ++i))
399                   {
400                     int j = i;  /* j is the upper bound so far of the range */
401                     fputs_filtered (":", stream);
402                     while (i + 1 <= high_bound
403                            && value_bit_index (type, valaddr, ++i))
404                       j = i;
405                     chill_print_type_scalar (range, (LONGEST) j, stream);
406                   }
407               }
408           }
409       done:
410         if (is_bitstring)
411           fputs_filtered ("'", stream);
412         else
413           fputs_filtered ("]", stream);
414       }
415       break;
416
417     case TYPE_CODE_STRUCT:
418       if (chill_varying_type (type))
419         {
420           struct type *inner = check_typedef (TYPE_FIELD_TYPE (type, 1));
421           long length = unpack_long (TYPE_FIELD_TYPE (type, 0), valaddr);
422           char *data_addr = valaddr + TYPE_FIELD_BITPOS (type, 1) / 8;
423
424           switch (TYPE_CODE (inner))
425             {
426             case TYPE_CODE_STRING:
427               if (length > TYPE_LENGTH (type) - 2)
428                 {
429                   fprintf_filtered (stream,
430                         "<dynamic length %ld > static length %d> *invalid*",
431                                     length, TYPE_LENGTH (type));
432
433                   /* Don't print the string; doing so might produce a
434                      segfault.  */
435                   return length;
436                 }
437               LA_PRINT_STRING (stream, data_addr, length, 1, 0);
438               return length;
439             default:
440               break;
441             }
442         }
443       chill_print_value_fields (type, valaddr, stream, format, recurse, pretty,
444                                 0);
445       break;
446
447     case TYPE_CODE_REF:
448       if (addressprint)
449         {
450           fprintf_filtered (stream, "LOC(");
451           print_address_numeric
452             (extract_address (valaddr, TARGET_PTR_BIT / HOST_CHAR_BIT),
453              1,
454              stream);
455           fprintf_filtered (stream, ")");
456           if (deref_ref)
457             fputs_filtered (": ", stream);
458         }
459       /* De-reference the reference.  */
460       if (deref_ref)
461         {
462           if (TYPE_CODE (TYPE_TARGET_TYPE (type)) != TYPE_CODE_UNDEF)
463             {
464               value_ptr deref_val =
465               value_at
466               (TYPE_TARGET_TYPE (type),
467                unpack_pointer (lookup_pointer_type (builtin_type_void),
468                                valaddr),
469                NULL);
470               val_print (VALUE_TYPE (deref_val),
471                          VALUE_CONTENTS (deref_val),
472                          0,
473                          VALUE_ADDRESS (deref_val), stream, format,
474                          deref_ref, recurse + 1, pretty);
475             }
476           else
477             fputs_filtered ("???", stream);
478         }
479       break;
480
481     case TYPE_CODE_ENUM:
482       c_val_print (type, valaddr, 0, address, stream, format,
483                    deref_ref, recurse, pretty);
484       break;
485
486     case TYPE_CODE_RANGE:
487       if (TYPE_TARGET_TYPE (type))
488         chill_val_print (TYPE_TARGET_TYPE (type), valaddr, 0, address, stream,
489                          format, deref_ref, recurse, pretty);
490       break;
491
492     case TYPE_CODE_MEMBER:
493     case TYPE_CODE_UNION:
494     case TYPE_CODE_FUNC:
495     case TYPE_CODE_VOID:
496     case TYPE_CODE_ERROR:
497     default:
498       /* Let's defer printing to the C printer, rather than
499          print an error message.  FIXME! */
500       c_val_print (type, valaddr, 0, address, stream, format,
501                    deref_ref, recurse, pretty);
502     }
503   gdb_flush (stream);
504   return (0);
505 }
506
507 /* Mutually recursive subroutines of cplus_print_value and c_val_print to
508    print out a structure's fields: cp_print_value_fields and cplus_print_value.
509
510    TYPE, VALADDR, STREAM, RECURSE, and PRETTY have the
511    same meanings as in cplus_print_value and c_val_print.
512
513    DONT_PRINT is an array of baseclass types that we
514    should not print, or zero if called from top level.  */
515
516 static void
517 chill_print_value_fields (type, valaddr, stream, format, recurse, pretty,
518                           dont_print)
519      struct type *type;
520      char *valaddr;
521      struct ui_file *stream;
522      int format;
523      int recurse;
524      enum val_prettyprint pretty;
525      struct type **dont_print;
526 {
527   int i, len;
528   int fields_seen = 0;
529
530   CHECK_TYPEDEF (type);
531
532   fprintf_filtered (stream, "[");
533   len = TYPE_NFIELDS (type);
534   if (len == 0)
535     {
536       fprintf_filtered (stream, "<No data fields>");
537     }
538   else
539     {
540       for (i = 0; i < len; i++)
541         {
542           if (fields_seen)
543             {
544               fprintf_filtered (stream, ", ");
545             }
546           fields_seen = 1;
547           if (pretty)
548             {
549               fprintf_filtered (stream, "\n");
550               print_spaces_filtered (2 + 2 * recurse, stream);
551             }
552           else
553             {
554               wrap_here (n_spaces (2 + 2 * recurse));
555             }
556           fputs_filtered (".", stream);
557           fprintf_symbol_filtered (stream, TYPE_FIELD_NAME (type, i),
558                                    language_chill, DMGL_NO_OPTS);
559           fputs_filtered (": ", stream);
560           if (TYPE_FIELD_PACKED (type, i))
561             {
562               value_ptr v;
563
564               /* Bitfields require special handling, especially due to byte
565                  order problems.  */
566               v = value_from_longest (TYPE_FIELD_TYPE (type, i),
567                                    unpack_field_as_long (type, valaddr, i));
568
569               chill_val_print (TYPE_FIELD_TYPE (type, i), VALUE_CONTENTS (v), 0, 0,
570                                stream, format, 0, recurse + 1, pretty);
571             }
572           else
573             {
574               chill_val_print (TYPE_FIELD_TYPE (type, i),
575                                valaddr + TYPE_FIELD_BITPOS (type, i) / 8, 0,
576                                0, stream, format, 0, recurse + 1, pretty);
577             }
578         }
579       if (pretty)
580         {
581           fprintf_filtered (stream, "\n");
582           print_spaces_filtered (2 * recurse, stream);
583         }
584     }
585   fprintf_filtered (stream, "]");
586 }
587 \f
588 int
589 chill_value_print (val, stream, format, pretty)
590      value_ptr val;
591      struct ui_file *stream;
592      int format;
593      enum val_prettyprint pretty;
594 {
595   struct type *type = VALUE_TYPE (val);
596   struct type *real_type = check_typedef (type);
597
598   /* If it is a pointer, indicate what it points to.
599
600      Print type also if it is a reference. */
601
602   if (TYPE_CODE (real_type) == TYPE_CODE_PTR ||
603       TYPE_CODE (real_type) == TYPE_CODE_REF)
604     {
605       char *valaddr = VALUE_CONTENTS (val);
606       CORE_ADDR addr = unpack_pointer (type, valaddr);
607       if (TYPE_CODE (type) != TYPE_CODE_PTR || addr != 0)
608         {
609           int i;
610           char *name = TYPE_NAME (type);
611           if (name)
612             fputs_filtered (name, stream);
613           else if (TYPE_CODE (TYPE_TARGET_TYPE (type)) == TYPE_CODE_VOID)
614             fputs_filtered ("PTR", stream);
615           else
616             {
617               fprintf_filtered (stream, "(");
618               type_print (type, "", stream, -1);
619               fprintf_filtered (stream, ")");
620             }
621           fprintf_filtered (stream, "(");
622           i = val_print (type, valaddr, 0, VALUE_ADDRESS (val),
623                          stream, format, 1, 0, pretty);
624           fprintf_filtered (stream, ")");
625           return i;
626         }
627     }
628   return (val_print (type, VALUE_CONTENTS (val), 0,
629                      VALUE_ADDRESS (val), stream, format, 1, 0, pretty));
630 }