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