(Changes from Daniel Berlin, with revisions by Jim Blandy.)
[external/binutils.git] / gdb / c-valprint.c
1 /* Support for printing C values for GDB, the GNU debugger.
2    Copyright 1986, 1988, 1989, 1991, 1992, 1993, 1994, 1995, 1996, 1997,
3    1998, 1999, 2000
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 "symtab.h"
25 #include "gdbtypes.h"
26 #include "expression.h"
27 #include "value.h"
28 #include "valprint.h"
29 #include "language.h"
30 #include "c-lang.h"
31 #include "cp-abi.h"
32 \f
33
34 /* Print function pointer with inferior address ADDRESS onto stdio
35    stream STREAM.  */
36
37 static void
38 print_function_pointer_address (CORE_ADDR address, struct ui_file *stream)
39 {
40   CORE_ADDR func_addr = CONVERT_FROM_FUNC_PTR_ADDR (address);
41
42   /* If the function pointer is represented by a description, print the
43      address of the description.  */
44   if (addressprint && func_addr != address)
45     {
46       fputs_filtered ("@", stream);
47       print_address_numeric (address, 1, stream);
48       fputs_filtered (": ", stream);
49     }
50   print_address_demangle (func_addr, stream, demangle);
51 }
52
53
54 /* Print data of type TYPE located at VALADDR (within GDB), which came from
55    the inferior at address ADDRESS, onto stdio stream STREAM according to
56    FORMAT (a letter or 0 for natural format).  The data at VALADDR is in
57    target byte order.
58
59    If the data are a string pointer, returns the number of string characters
60    printed.
61
62    If DEREF_REF is nonzero, then dereference references, otherwise just print
63    them like pointers.
64
65    The PRETTY parameter controls prettyprinting.  */
66
67 int
68 c_val_print (struct type *type, char *valaddr, int embedded_offset,
69              CORE_ADDR address, struct ui_file *stream, int format,
70              int deref_ref, int recurse, enum val_prettyprint pretty)
71 {
72   register unsigned int i = 0;  /* Number of characters printed */
73   unsigned len;
74   struct type *elttype;
75   unsigned eltlen;
76   LONGEST val;
77   CORE_ADDR addr;
78
79   CHECK_TYPEDEF (type);
80   switch (TYPE_CODE (type))
81     {
82     case TYPE_CODE_ARRAY:
83       elttype = check_typedef (TYPE_TARGET_TYPE (type));
84       if (TYPE_LENGTH (type) > 0 && TYPE_LENGTH (TYPE_TARGET_TYPE (type)) > 0)
85         {
86           eltlen = TYPE_LENGTH (elttype);
87           len = TYPE_LENGTH (type) / eltlen;
88           if (prettyprint_arrays)
89             {
90               print_spaces_filtered (2 + 2 * recurse, stream);
91             }
92           /* For an array of chars, print with string syntax.  */
93           if (eltlen == 1 &&
94               ((TYPE_CODE (elttype) == TYPE_CODE_INT)
95                || ((current_language->la_language == language_m2)
96                    && (TYPE_CODE (elttype) == TYPE_CODE_CHAR)))
97               && (format == 0 || format == 's'))
98             {
99               /* If requested, look for the first null char and only print
100                  elements up to it.  */
101               if (stop_print_at_null)
102                 {
103                   unsigned int temp_len;
104
105                   /* Look for a NULL char. */
106                   for (temp_len = 0;
107                        (valaddr + embedded_offset)[temp_len]
108                        && temp_len < len && temp_len < print_max;
109                        temp_len++);
110                   len = temp_len;
111                 }
112
113               LA_PRINT_STRING (stream, valaddr + embedded_offset, len, eltlen, 0);
114               i = len;
115             }
116           else
117             {
118               fprintf_filtered (stream, "{");
119               /* If this is a virtual function table, print the 0th
120                  entry specially, and the rest of the members normally.  */
121               if (cp_is_vtbl_ptr_type (elttype))
122                 {
123                   i = 1;
124                   fprintf_filtered (stream, "%d vtable entries", len - 1);
125                 }
126               else
127                 {
128                   i = 0;
129                 }
130               val_print_array_elements (type, valaddr + embedded_offset, address, stream,
131                                      format, deref_ref, recurse, pretty, i);
132               fprintf_filtered (stream, "}");
133             }
134           break;
135         }
136       /* Array of unspecified length: treat like pointer to first elt.  */
137       addr = address;
138       goto print_unpacked_pointer;
139
140     case TYPE_CODE_PTR:
141       if (format && format != 's')
142         {
143           print_scalar_formatted (valaddr + embedded_offset, type, format, 0, stream);
144           break;
145         }
146       if (vtblprint && cp_is_vtbl_ptr_type (type))
147         {
148           /* Print the unmangled name if desired.  */
149           /* Print vtable entry - we only get here if we ARE using
150              -fvtable_thunks.  (Otherwise, look under TYPE_CODE_STRUCT.) */
151           CORE_ADDR addr
152             = extract_typed_address (valaddr + embedded_offset, type);
153           print_function_pointer_address (addr, stream);
154           break;
155         }
156       elttype = check_typedef (TYPE_TARGET_TYPE (type));
157       if (TYPE_CODE (elttype) == TYPE_CODE_METHOD)
158         {
159           cp_print_class_method (valaddr + embedded_offset, type, stream);
160         }
161       else if (TYPE_CODE (elttype) == TYPE_CODE_MEMBER)
162         {
163           cp_print_class_member (valaddr + embedded_offset,
164                                  TYPE_DOMAIN_TYPE (TYPE_TARGET_TYPE (type)),
165                                  stream, "&");
166         }
167       else
168         {
169           addr = unpack_pointer (type, valaddr + embedded_offset);
170         print_unpacked_pointer:
171           elttype = check_typedef (TYPE_TARGET_TYPE (type));
172
173           if (TYPE_CODE (elttype) == TYPE_CODE_FUNC)
174             {
175               /* Try to print what function it points to.  */
176               print_function_pointer_address (addr, stream);
177               /* Return value is irrelevant except for string pointers.  */
178               return (0);
179             }
180
181           if (addressprint && format != 's')
182             {
183               print_address_numeric (addr, 1, stream);
184             }
185
186           /* For a pointer to char or unsigned char, also print the string
187              pointed to, unless pointer is null.  */
188           /* FIXME: need to handle wchar_t here... */
189
190           if (TYPE_LENGTH (elttype) == 1
191               && TYPE_CODE (elttype) == TYPE_CODE_INT
192               && (format == 0 || format == 's')
193               && addr != 0)
194             {
195               i = val_print_string (addr, -1, TYPE_LENGTH (elttype), stream);
196             }
197           else if (cp_is_vtbl_member (type))
198             {
199               /* print vtbl's nicely */
200               CORE_ADDR vt_address = unpack_pointer (type, valaddr + embedded_offset);
201
202               struct minimal_symbol *msymbol =
203               lookup_minimal_symbol_by_pc (vt_address);
204               if ((msymbol != NULL) &&
205                   (vt_address == SYMBOL_VALUE_ADDRESS (msymbol)))
206                 {
207                   fputs_filtered (" <", stream);
208                   fputs_filtered (SYMBOL_SOURCE_NAME (msymbol), stream);
209                   fputs_filtered (">", stream);
210                 }
211               if (vt_address && vtblprint)
212                 {
213                   value_ptr vt_val;
214                   struct symbol *wsym = (struct symbol *) NULL;
215                   struct type *wtype;
216                   struct symtab *s;
217                   struct block *block = (struct block *) NULL;
218                   int is_this_fld;
219
220                   if (msymbol != NULL)
221                     wsym = lookup_symbol (SYMBOL_NAME (msymbol), block,
222                                           VAR_NAMESPACE, &is_this_fld, &s);
223
224                   if (wsym)
225                     {
226                       wtype = SYMBOL_TYPE (wsym);
227                     }
228                   else
229                     {
230                       wtype = TYPE_TARGET_TYPE (type);
231                     }
232                   vt_val = value_at (wtype, vt_address, NULL);
233                   val_print (VALUE_TYPE (vt_val), VALUE_CONTENTS (vt_val), 0,
234                              VALUE_ADDRESS (vt_val), stream, format,
235                              deref_ref, recurse + 1, pretty);
236                   if (pretty)
237                     {
238                       fprintf_filtered (stream, "\n");
239                       print_spaces_filtered (2 + 2 * recurse, stream);
240                     }
241                 }
242             }
243
244           /* Return number of characters printed, including the terminating
245              '\0' if we reached the end.  val_print_string takes care including
246              the terminating '\0' if necessary.  */
247           return i;
248         }
249       break;
250
251     case TYPE_CODE_MEMBER:
252       error ("not implemented: member type in c_val_print");
253       break;
254
255     case TYPE_CODE_REF:
256       elttype = check_typedef (TYPE_TARGET_TYPE (type));
257       if (TYPE_CODE (elttype) == TYPE_CODE_MEMBER)
258         {
259           cp_print_class_member (valaddr + embedded_offset,
260                                  TYPE_DOMAIN_TYPE (elttype),
261                                  stream, "");
262           break;
263         }
264       if (addressprint)
265         {
266           CORE_ADDR addr
267             = extract_typed_address (valaddr + embedded_offset, type);
268           fprintf_filtered (stream, "@");
269           print_address_numeric (addr, 1, stream);
270           if (deref_ref)
271             fputs_filtered (": ", stream);
272         }
273       /* De-reference the reference.  */
274       if (deref_ref)
275         {
276           if (TYPE_CODE (elttype) != TYPE_CODE_UNDEF)
277             {
278               value_ptr deref_val =
279               value_at
280               (TYPE_TARGET_TYPE (type),
281                unpack_pointer (lookup_pointer_type (builtin_type_void),
282                                valaddr + embedded_offset),
283                NULL);
284               val_print (VALUE_TYPE (deref_val),
285                          VALUE_CONTENTS (deref_val),
286                          0,
287                          VALUE_ADDRESS (deref_val),
288                          stream,
289                          format,
290                          deref_ref,
291                          recurse,
292                          pretty);
293             }
294           else
295             fputs_filtered ("???", stream);
296         }
297       break;
298
299     case TYPE_CODE_UNION:
300       if (recurse && !unionprint)
301         {
302           fprintf_filtered (stream, "{...}");
303           break;
304         }
305       /* Fall through.  */
306     case TYPE_CODE_STRUCT:
307       /*FIXME: Abstract this away */
308       if (vtblprint && cp_is_vtbl_ptr_type (type))
309         {
310           /* Print the unmangled name if desired.  */
311           /* Print vtable entry - we only get here if NOT using
312              -fvtable_thunks.  (Otherwise, look under TYPE_CODE_PTR.) */
313           int offset = (embedded_offset +
314                         TYPE_FIELD_BITPOS (type, VTBL_FNADDR_OFFSET) / 8);
315           struct type *field_type = TYPE_FIELD_TYPE (type, VTBL_FNADDR_OFFSET);
316           CORE_ADDR addr
317             = extract_typed_address (valaddr + offset, field_type);
318
319           print_function_pointer_address (addr, stream);
320         }
321       else
322         cp_print_value_fields (type, type, valaddr, embedded_offset, address, stream, format,
323                                recurse, pretty, NULL, 0);
324       break;
325
326     case TYPE_CODE_ENUM:
327       if (format)
328         {
329           print_scalar_formatted (valaddr + embedded_offset, type, format, 0, stream);
330           break;
331         }
332       len = TYPE_NFIELDS (type);
333       val = unpack_long (type, valaddr + embedded_offset);
334       for (i = 0; i < len; i++)
335         {
336           QUIT;
337           if (val == TYPE_FIELD_BITPOS (type, i))
338             {
339               break;
340             }
341         }
342       if (i < len)
343         {
344           fputs_filtered (TYPE_FIELD_NAME (type, i), stream);
345         }
346       else
347         {
348           print_longest (stream, 'd', 0, val);
349         }
350       break;
351
352     case TYPE_CODE_FUNC:
353       if (format)
354         {
355           print_scalar_formatted (valaddr + embedded_offset, type, format, 0, stream);
356           break;
357         }
358       /* FIXME, we should consider, at least for ANSI C language, eliminating
359          the distinction made between FUNCs and POINTERs to FUNCs.  */
360       fprintf_filtered (stream, "{");
361       type_print (type, "", stream, -1);
362       fprintf_filtered (stream, "} ");
363       /* Try to print what function it points to, and its address.  */
364       print_address_demangle (address, stream, demangle);
365       break;
366
367     case TYPE_CODE_BOOL:
368       format = format ? format : output_format;
369       if (format)
370         print_scalar_formatted (valaddr + embedded_offset, type, format, 0, stream);
371       else
372         {
373           val = unpack_long (type, valaddr + embedded_offset);
374           if (val == 0)
375             fputs_filtered ("false", stream);
376           else if (val == 1)
377             fputs_filtered ("true", stream);
378           else
379             print_longest (stream, 'd', 0, val);
380         }
381       break;
382
383     case TYPE_CODE_RANGE:
384       /* FIXME: create_range_type does not set the unsigned bit in a
385          range type (I think it probably should copy it from the target
386          type), so we won't print values which are too large to
387          fit in a signed integer correctly.  */
388       /* FIXME: Doesn't handle ranges of enums correctly.  (Can't just
389          print with the target type, though, because the size of our type
390          and the target type might differ).  */
391       /* FALLTHROUGH */
392
393     case TYPE_CODE_INT:
394       format = format ? format : output_format;
395       if (format)
396         {
397           print_scalar_formatted (valaddr + embedded_offset, type, format, 0, stream);
398         }
399       else
400         {
401           val_print_type_code_int (type, valaddr + embedded_offset, stream);
402           /* C and C++ has no single byte int type, char is used instead.
403              Since we don't know whether the value is really intended to
404              be used as an integer or a character, print the character
405              equivalent as well. */
406           if (TYPE_LENGTH (type) == 1)
407             {
408               fputs_filtered (" ", stream);
409               LA_PRINT_CHAR ((unsigned char) unpack_long (type, valaddr + embedded_offset),
410                              stream);
411             }
412         }
413       break;
414
415     case TYPE_CODE_CHAR:
416       format = format ? format : output_format;
417       if (format)
418         {
419           print_scalar_formatted (valaddr + embedded_offset, type, format, 0, stream);
420         }
421       else
422         {
423           val = unpack_long (type, valaddr + embedded_offset);
424           if (TYPE_UNSIGNED (type))
425             fprintf_filtered (stream, "%u", (unsigned int) val);
426           else
427             fprintf_filtered (stream, "%d", (int) val);
428           fputs_filtered (" ", stream);
429           LA_PRINT_CHAR ((unsigned char) val, stream);
430         }
431       break;
432
433     case TYPE_CODE_FLT:
434       if (format)
435         {
436           print_scalar_formatted (valaddr + embedded_offset, type, format, 0, stream);
437         }
438       else
439         {
440           print_floating (valaddr + embedded_offset, type, stream);
441         }
442       break;
443
444     case TYPE_CODE_METHOD:
445       cp_print_class_method (valaddr + embedded_offset, lookup_pointer_type (type), stream);
446       break;
447
448     case TYPE_CODE_VOID:
449       fprintf_filtered (stream, "void");
450       break;
451
452     case TYPE_CODE_ERROR:
453       fprintf_filtered (stream, "<error type>");
454       break;
455
456     case TYPE_CODE_UNDEF:
457       /* This happens (without TYPE_FLAG_STUB set) on systems which don't use
458          dbx xrefs (NO_DBX_XREFS in gcc) if a file has a "struct foo *bar"
459          and no complete type for struct foo in that file.  */
460       fprintf_filtered (stream, "<incomplete type>");
461       break;
462
463     default:
464       error ("Invalid C/C++ type code %d in symbol table.", TYPE_CODE (type));
465     }
466   gdb_flush (stream);
467   return (0);
468 }
469 \f
470 int
471 c_value_print (value_ptr val, struct ui_file *stream, int format,
472                enum val_prettyprint pretty)
473 {
474   struct type *type = VALUE_TYPE (val);
475   struct type *real_type;
476   int full, top, using_enc;
477
478   /* If it is a pointer, indicate what it points to.
479
480      Print type also if it is a reference.
481
482      C++: if it is a member pointer, we will take care
483      of that when we print it.  */
484   if (TYPE_CODE (type) == TYPE_CODE_PTR ||
485       TYPE_CODE (type) == TYPE_CODE_REF)
486     {
487       /* Hack:  remove (char *) for char strings.  Their
488          type is indicated by the quoted string anyway. */
489       if (TYPE_CODE (type) == TYPE_CODE_PTR &&
490           TYPE_NAME (type) == NULL &&
491           TYPE_NAME (TYPE_TARGET_TYPE (type)) != NULL &&
492           STREQ (TYPE_NAME (TYPE_TARGET_TYPE (type)), "char"))
493         {
494           /* Print nothing */
495         }
496       else if (objectprint && (TYPE_CODE (TYPE_TARGET_TYPE (type)) == TYPE_CODE_CLASS))
497         {
498
499           if (TYPE_CODE(type) == TYPE_CODE_REF)
500             {
501               /* Copy value, change to pointer, so we don't get an
502                * error about a non-pointer type in value_rtti_target_type
503                */
504               value_ptr temparg;
505               temparg=value_copy(val);
506               VALUE_TYPE (temparg) = lookup_pointer_type(TYPE_TARGET_TYPE(type));
507               val=temparg;
508             }
509           /* Pointer to class, check real type of object */
510           fprintf_filtered (stream, "(");
511           real_type = value_rtti_target_type (val, &full, &top, &using_enc);
512           if (real_type)
513             {
514               /* RTTI entry found */
515               if (TYPE_CODE (type) == TYPE_CODE_PTR)
516                 {
517                   /* create a pointer type pointing to the real type */
518                   type = lookup_pointer_type (real_type);
519                 }
520               else
521                 {
522                   /* create a reference type referencing the real type */
523                   type = lookup_reference_type (real_type);
524                 }
525               /* JYG: Need to adjust pointer value. */
526               val->aligner.contents[0] -= top;
527
528               /* Note: When we look up RTTI entries, we don't get any 
529                  information on const or volatile attributes */
530             }
531           type_print (type, "", stream, -1);
532           fprintf_filtered (stream, ") ");
533         }
534       else
535         {
536           /* normal case */
537           fprintf_filtered (stream, "(");
538           type_print (type, "", stream, -1);
539           fprintf_filtered (stream, ") ");
540         }
541     }
542   if (objectprint && (TYPE_CODE (VALUE_TYPE (val)) == TYPE_CODE_CLASS))
543     {
544       /* Attempt to determine real type of object */
545       real_type = value_rtti_type (val, &full, &top, &using_enc);
546       if (real_type)
547         {
548           /* We have RTTI information, so use it */
549           val = value_full_object (val, real_type, full, top, using_enc);
550           fprintf_filtered (stream, "(%s%s) ",
551                             TYPE_NAME (real_type),
552                             full ? "" : " [incomplete object]");
553           /* Print out object: enclosing type is same as real_type if full */
554           return val_print (VALUE_ENCLOSING_TYPE (val), VALUE_CONTENTS_ALL (val), 0,
555                          VALUE_ADDRESS (val), stream, format, 1, 0, pretty);
556           /* Note: When we look up RTTI entries, we don't get any information on
557              const or volatile attributes */
558         }
559       else if (type != VALUE_ENCLOSING_TYPE (val))
560         {
561           /* No RTTI information, so let's do our best */
562           fprintf_filtered (stream, "(%s ?) ",
563                             TYPE_NAME (VALUE_ENCLOSING_TYPE (val)));
564           return val_print (VALUE_ENCLOSING_TYPE (val), VALUE_CONTENTS_ALL (val), 0,
565                          VALUE_ADDRESS (val), stream, format, 1, 0, pretty);
566         }
567       /* Otherwise, we end up at the return outside this "if" */
568     }
569
570   return val_print (type, VALUE_CONTENTS_ALL (val), VALUE_EMBEDDED_OFFSET (val),
571                     VALUE_ADDRESS (val),
572                     stream, format, 1, 0, pretty);
573 }