* cp-abi.h (cplus_method_ptr_size): Add TO_TYPE parameter.
[external/binutils.git] / gdb / c-valprint.c
1 /* Support for printing C values for GDB, the GNU debugger.
2
3    Copyright (C) 1986, 1988, 1989, 1991, 1992, 1993, 1994, 1995, 1996, 1997,
4    1998, 1999, 2000, 2001, 2003, 2005, 2006, 2007, 2008
5    Free Software Foundation, Inc.
6
7    This file is part of GDB.
8
9    This program is free software; you can redistribute it and/or modify
10    it under the terms of the GNU General Public License as published by
11    the Free Software Foundation; either version 3 of the License, or
12    (at your option) any later version.
13
14    This program is distributed in the hope that it will be useful,
15    but WITHOUT ANY WARRANTY; without even the implied warranty of
16    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17    GNU General Public License for more details.
18
19    You should have received a copy of the GNU General Public License
20    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
21
22 #include "defs.h"
23 #include "gdb_string.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 #include "target.h"
33 \f
34
35 /* Print function pointer with inferior address ADDRESS onto stdio
36    stream STREAM.  */
37
38 static void
39 print_function_pointer_address (CORE_ADDR address, struct ui_file *stream)
40 {
41   CORE_ADDR func_addr = gdbarch_convert_from_func_ptr_addr (current_gdbarch,
42                                                             address,
43                                                             &current_target);
44
45   /* If the function pointer is represented by a description, print the
46      address of the description.  */
47   if (addressprint && func_addr != address)
48     {
49       fputs_filtered ("@", stream);
50       fputs_filtered (paddress (address), stream);
51       fputs_filtered (": ", stream);
52     }
53   print_address_demangle (func_addr, stream, demangle);
54 }
55
56
57 /* Apply a heuristic to decide whether an array of TYPE or a pointer
58    to TYPE should be printed as a textual string.  Return non-zero if
59    it should, or zero if it should be treated as an array of integers
60    or pointer to integers.  FORMAT is the current format letter,
61    or 0 if none.
62
63    We guess that "char" is a character.  Explicitly signed and
64    unsigned character types are also characters.  Integer data from
65    vector types is not.  The user can override this by using the /s
66    format letter.  */
67
68 static int
69 textual_element_type (struct type *type, char format)
70 {
71   struct type *true_type = check_typedef (type);
72
73   if (format != 0 && format != 's')
74     return 0;
75
76   /* TYPE_CODE_CHAR is always textual.  */
77   if (TYPE_CODE (true_type) == TYPE_CODE_CHAR)
78     return 1;
79
80   if (format == 's')
81     {
82       /* Print this as a string if we can manage it.  For now, no
83          wide character support.  */
84       if (TYPE_CODE (true_type) == TYPE_CODE_INT
85           && TYPE_LENGTH (true_type) == 1)
86         return 1;
87     }
88   else
89     {
90       /* If a one-byte TYPE_CODE_INT is missing the not-a-character
91          flag, then we treat it as text; otherwise, we assume it's
92          being used as data.  */
93       if (TYPE_CODE (true_type) == TYPE_CODE_INT
94           && TYPE_LENGTH (true_type) == 1
95           && !TYPE_NOTTEXT (true_type))
96         return 1;
97     }
98
99   return 0;
100 }
101
102
103 /* Print data of type TYPE located at VALADDR (within GDB), which came from
104    the inferior at address ADDRESS, onto stdio stream STREAM according to
105    FORMAT (a letter or 0 for natural format).  The data at VALADDR is in
106    target byte order.
107
108    If the data are a string pointer, returns the number of string characters
109    printed.
110
111    If DEREF_REF is nonzero, then dereference references, otherwise just print
112    them like pointers.
113
114    The PRETTY parameter controls prettyprinting.  */
115
116 int
117 c_val_print (struct type *type, const gdb_byte *valaddr, int embedded_offset,
118              CORE_ADDR address, struct ui_file *stream, int format,
119              int deref_ref, int recurse, enum val_prettyprint pretty)
120 {
121   unsigned int i = 0;   /* Number of characters printed */
122   unsigned len;
123   struct type *elttype;
124   unsigned eltlen;
125   LONGEST val;
126   CORE_ADDR addr;
127
128   CHECK_TYPEDEF (type);
129   switch (TYPE_CODE (type))
130     {
131     case TYPE_CODE_ARRAY:
132       elttype = check_typedef (TYPE_TARGET_TYPE (type));
133       if (TYPE_LENGTH (type) > 0 && TYPE_LENGTH (TYPE_TARGET_TYPE (type)) > 0)
134         {
135           eltlen = TYPE_LENGTH (elttype);
136           len = TYPE_LENGTH (type) / eltlen;
137           if (prettyprint_arrays)
138             {
139               print_spaces_filtered (2 + 2 * recurse, stream);
140             }
141
142           /* Print arrays of textual chars with a string syntax.  */
143           if (textual_element_type (elttype, format))
144             {
145               /* If requested, look for the first null char and only print
146                  elements up to it.  */
147               if (stop_print_at_null)
148                 {
149                   unsigned int temp_len;
150
151                   /* Look for a NULL char. */
152                   for (temp_len = 0;
153                        (valaddr + embedded_offset)[temp_len]
154                        && temp_len < len && temp_len < print_max;
155                        temp_len++);
156                   len = temp_len;
157                 }
158
159               LA_PRINT_STRING (stream, valaddr + embedded_offset, len, eltlen, 0);
160               i = len;
161             }
162           else
163             {
164               fprintf_filtered (stream, "{");
165               /* If this is a virtual function table, print the 0th
166                  entry specially, and the rest of the members normally.  */
167               if (cp_is_vtbl_ptr_type (elttype))
168                 {
169                   i = 1;
170                   fprintf_filtered (stream, _("%d vtable entries"), len - 1);
171                 }
172               else
173                 {
174                   i = 0;
175                 }
176               val_print_array_elements (type, valaddr + embedded_offset, address, stream,
177                                      format, deref_ref, recurse, pretty, i);
178               fprintf_filtered (stream, "}");
179             }
180           break;
181         }
182       /* Array of unspecified length: treat like pointer to first elt.  */
183       addr = address;
184       goto print_unpacked_pointer;
185
186     case TYPE_CODE_MEMBERPTR:
187       if (format)
188         {
189           print_scalar_formatted (valaddr + embedded_offset, type, format, 0, stream);
190           break;
191         }
192       cp_print_class_member (valaddr + embedded_offset, type, stream, "&");
193       break;
194
195     case TYPE_CODE_METHODPTR:
196       cplus_print_method_ptr (valaddr + embedded_offset, type, stream);
197       break;
198
199     case TYPE_CODE_PTR:
200       if (format && format != 's')
201         {
202           print_scalar_formatted (valaddr + embedded_offset, type, format, 0, stream);
203           break;
204         }
205       if (vtblprint && cp_is_vtbl_ptr_type (type))
206         {
207           /* Print the unmangled name if desired.  */
208           /* Print vtable entry - we only get here if we ARE using
209              -fvtable_thunks.  (Otherwise, look under TYPE_CODE_STRUCT.) */
210           CORE_ADDR addr
211             = extract_typed_address (valaddr + embedded_offset, type);
212           print_function_pointer_address (addr, stream);
213           break;
214         }
215       elttype = check_typedef (TYPE_TARGET_TYPE (type));
216         {
217           addr = unpack_pointer (type, valaddr + embedded_offset);
218         print_unpacked_pointer:
219
220           if (TYPE_CODE (elttype) == TYPE_CODE_FUNC)
221             {
222               /* Try to print what function it points to.  */
223               print_function_pointer_address (addr, stream);
224               /* Return value is irrelevant except for string pointers.  */
225               return (0);
226             }
227
228           if (addressprint)
229             fputs_filtered (paddress (addr), stream);
230
231           /* For a pointer to a textual type, also print the string
232              pointed to, unless pointer is null.  */
233           /* FIXME: need to handle wchar_t here... */
234
235           if (textual_element_type (elttype, format) && addr != 0)
236             {
237               i = val_print_string (addr, -1, TYPE_LENGTH (elttype), stream);
238             }
239           else if (cp_is_vtbl_member (type))
240             {
241               /* print vtbl's nicely */
242               CORE_ADDR vt_address = unpack_pointer (type, valaddr + embedded_offset);
243
244               struct minimal_symbol *msymbol =
245               lookup_minimal_symbol_by_pc (vt_address);
246               if ((msymbol != NULL) &&
247                   (vt_address == SYMBOL_VALUE_ADDRESS (msymbol)))
248                 {
249                   fputs_filtered (" <", stream);
250                   fputs_filtered (SYMBOL_PRINT_NAME (msymbol), stream);
251                   fputs_filtered (">", stream);
252                 }
253               if (vt_address && vtblprint)
254                 {
255                   struct value *vt_val;
256                   struct symbol *wsym = (struct symbol *) NULL;
257                   struct type *wtype;
258                   struct block *block = (struct block *) NULL;
259                   int is_this_fld;
260
261                   if (msymbol != NULL)
262                     wsym = lookup_symbol (SYMBOL_LINKAGE_NAME (msymbol), block,
263                                           VAR_DOMAIN, &is_this_fld);
264
265                   if (wsym)
266                     {
267                       wtype = SYMBOL_TYPE (wsym);
268                     }
269                   else
270                     {
271                       wtype = TYPE_TARGET_TYPE (type);
272                     }
273                   vt_val = value_at (wtype, vt_address);
274                   common_val_print (vt_val, stream, format,
275                                     deref_ref, recurse + 1, pretty,
276                                     current_language);
277                   if (pretty)
278                     {
279                       fprintf_filtered (stream, "\n");
280                       print_spaces_filtered (2 + 2 * recurse, stream);
281                     }
282                 }
283             }
284
285           /* Return number of characters printed, including the terminating
286              '\0' if we reached the end.  val_print_string takes care including
287              the terminating '\0' if necessary.  */
288           return i;
289         }
290       break;
291
292     case TYPE_CODE_REF:
293       elttype = check_typedef (TYPE_TARGET_TYPE (type));
294       if (addressprint)
295         {
296           CORE_ADDR addr
297             = extract_typed_address (valaddr + embedded_offset, type);
298           fprintf_filtered (stream, "@");
299           fputs_filtered (paddress (addr), stream);
300           if (deref_ref)
301             fputs_filtered (": ", stream);
302         }
303       /* De-reference the reference.  */
304       if (deref_ref)
305         {
306           if (TYPE_CODE (elttype) != TYPE_CODE_UNDEF)
307             {
308               struct value *deref_val =
309               value_at
310               (TYPE_TARGET_TYPE (type),
311                unpack_pointer (type, valaddr + embedded_offset));
312               common_val_print (deref_val, stream, format, deref_ref,
313                                 recurse, pretty, current_language);
314             }
315           else
316             fputs_filtered ("???", stream);
317         }
318       break;
319
320     case TYPE_CODE_UNION:
321       if (recurse && !unionprint)
322         {
323           fprintf_filtered (stream, "{...}");
324           break;
325         }
326       /* Fall through.  */
327     case TYPE_CODE_STRUCT:
328       /*FIXME: Abstract this away */
329       if (vtblprint && cp_is_vtbl_ptr_type (type))
330         {
331           /* Print the unmangled name if desired.  */
332           /* Print vtable entry - we only get here if NOT using
333              -fvtable_thunks.  (Otherwise, look under TYPE_CODE_PTR.) */
334           int offset = (embedded_offset +
335                         TYPE_FIELD_BITPOS (type, VTBL_FNADDR_OFFSET) / 8);
336           struct type *field_type = TYPE_FIELD_TYPE (type, VTBL_FNADDR_OFFSET);
337           CORE_ADDR addr
338             = extract_typed_address (valaddr + offset, field_type);
339
340           print_function_pointer_address (addr, stream);
341         }
342       else
343         cp_print_value_fields (type, type, valaddr, embedded_offset, address, stream, format,
344                                recurse, pretty, NULL, 0);
345       break;
346
347     case TYPE_CODE_ENUM:
348       if (format)
349         {
350           print_scalar_formatted (valaddr + embedded_offset, type, format, 0, stream);
351           break;
352         }
353       len = TYPE_NFIELDS (type);
354       val = unpack_long (type, valaddr + embedded_offset);
355       for (i = 0; i < len; i++)
356         {
357           QUIT;
358           if (val == TYPE_FIELD_BITPOS (type, i))
359             {
360               break;
361             }
362         }
363       if (i < len)
364         {
365           fputs_filtered (TYPE_FIELD_NAME (type, i), stream);
366         }
367       else
368         {
369           print_longest (stream, 'd', 0, val);
370         }
371       break;
372
373     case TYPE_CODE_FLAGS:
374       if (format)
375           print_scalar_formatted (valaddr + embedded_offset, type, format, 0, stream);
376       else
377         val_print_type_code_flags (type, valaddr + embedded_offset, stream);
378       break;
379
380     case TYPE_CODE_FUNC:
381     case TYPE_CODE_METHOD:
382       if (format)
383         {
384           print_scalar_formatted (valaddr + embedded_offset, type, format, 0, stream);
385           break;
386         }
387       /* FIXME, we should consider, at least for ANSI C language, eliminating
388          the distinction made between FUNCs and POINTERs to FUNCs.  */
389       fprintf_filtered (stream, "{");
390       type_print (type, "", stream, -1);
391       fprintf_filtered (stream, "} ");
392       /* Try to print what function it points to, and its address.  */
393       print_address_demangle (address, stream, demangle);
394       break;
395
396     case TYPE_CODE_BOOL:
397       format = format ? format : output_format;
398       if (format)
399         print_scalar_formatted (valaddr + embedded_offset, type, format, 0, stream);
400       else
401         {
402           val = unpack_long (type, valaddr + embedded_offset);
403           if (val == 0)
404             fputs_filtered ("false", stream);
405           else if (val == 1)
406             fputs_filtered ("true", stream);
407           else
408             print_longest (stream, 'd', 0, val);
409         }
410       break;
411
412     case TYPE_CODE_RANGE:
413       /* FIXME: create_range_type does not set the unsigned bit in a
414          range type (I think it probably should copy it from the target
415          type), so we won't print values which are too large to
416          fit in a signed integer correctly.  */
417       /* FIXME: Doesn't handle ranges of enums correctly.  (Can't just
418          print with the target type, though, because the size of our type
419          and the target type might differ).  */
420       /* FALLTHROUGH */
421
422     case TYPE_CODE_INT:
423       format = format ? format : output_format;
424       if (format)
425         {
426           print_scalar_formatted (valaddr + embedded_offset, type, format, 0, stream);
427         }
428       else
429         {
430           val_print_type_code_int (type, valaddr + embedded_offset, stream);
431           /* C and C++ has no single byte int type, char is used instead.
432              Since we don't know whether the value is really intended to
433              be used as an integer or a character, print the character
434              equivalent as well.  */
435           if (textual_element_type (type, format))
436             {
437               fputs_filtered (" ", stream);
438               LA_PRINT_CHAR ((unsigned char) unpack_long (type, valaddr + embedded_offset),
439                              stream);
440             }
441         }
442       break;
443
444     case TYPE_CODE_CHAR:
445       format = format ? format : output_format;
446       if (format)
447         {
448           print_scalar_formatted (valaddr + embedded_offset, type, format, 0, stream);
449         }
450       else
451         {
452           val = unpack_long (type, valaddr + embedded_offset);
453           if (TYPE_UNSIGNED (type))
454             fprintf_filtered (stream, "%u", (unsigned int) val);
455           else
456             fprintf_filtered (stream, "%d", (int) val);
457           fputs_filtered (" ", stream);
458           LA_PRINT_CHAR ((unsigned char) val, stream);
459         }
460       break;
461
462     case TYPE_CODE_FLT:
463       if (format)
464         {
465           print_scalar_formatted (valaddr + embedded_offset, type, format, 0, stream);
466         }
467       else
468         {
469           print_floating (valaddr + embedded_offset, type, stream);
470         }
471       break;
472
473     case TYPE_CODE_DECFLOAT:
474       if (format)
475         print_scalar_formatted (valaddr + embedded_offset, type, format, 0, stream);
476       else
477         print_decimal_floating (valaddr + embedded_offset, type, stream);
478       break;
479
480     case TYPE_CODE_VOID:
481       fprintf_filtered (stream, "void");
482       break;
483
484     case TYPE_CODE_ERROR:
485       fprintf_filtered (stream, _("<error type>"));
486       break;
487
488     case TYPE_CODE_UNDEF:
489       /* This happens (without TYPE_FLAG_STUB set) on systems which don't use
490          dbx xrefs (NO_DBX_XREFS in gcc) if a file has a "struct foo *bar"
491          and no complete type for struct foo in that file.  */
492       fprintf_filtered (stream, _("<incomplete type>"));
493       break;
494
495     case TYPE_CODE_COMPLEX:
496       if (format)
497         print_scalar_formatted (valaddr + embedded_offset,
498                                 TYPE_TARGET_TYPE (type),
499                                 format, 0, stream);
500       else
501         print_floating (valaddr + embedded_offset, TYPE_TARGET_TYPE (type),
502                         stream);
503       fprintf_filtered (stream, " + ");
504       if (format)
505         print_scalar_formatted (valaddr + embedded_offset
506                                 + TYPE_LENGTH (TYPE_TARGET_TYPE (type)),
507                                 TYPE_TARGET_TYPE (type),
508                                 format, 0, stream);
509       else
510         print_floating (valaddr + embedded_offset
511                         + TYPE_LENGTH (TYPE_TARGET_TYPE (type)),
512                         TYPE_TARGET_TYPE (type),
513                         stream);
514       fprintf_filtered (stream, " * I");
515       break;
516
517     default:
518       error (_("Invalid C/C++ type code %d in symbol table."), TYPE_CODE (type));
519     }
520   gdb_flush (stream);
521   return (0);
522 }
523 \f
524 int
525 c_value_print (struct value *val, struct ui_file *stream, int format,
526                enum val_prettyprint pretty)
527 {
528   struct type *type, *real_type;
529   int full, top, using_enc;
530
531   /* If it is a pointer, indicate what it points to.
532
533      Print type also if it is a reference.
534
535      C++: if it is a member pointer, we will take care
536      of that when we print it.  */
537
538   type = check_typedef (value_type (val));
539
540   if (TYPE_CODE (type) == TYPE_CODE_PTR
541       || TYPE_CODE (type) == TYPE_CODE_REF)
542     {
543       /* Hack:  remove (char *) for char strings.  Their
544          type is indicated by the quoted string anyway.
545          (Don't use textual_element_type here; quoted strings
546          are always exactly (char *).  */
547       if (TYPE_CODE (type) == TYPE_CODE_PTR
548           && TYPE_NAME (type) == NULL
549           && TYPE_NAME (TYPE_TARGET_TYPE (type)) != NULL
550           && strcmp (TYPE_NAME (TYPE_TARGET_TYPE (type)), "char") == 0)
551         {
552           /* Print nothing */
553         }
554       else if (objectprint && (TYPE_CODE (TYPE_TARGET_TYPE (type)) == TYPE_CODE_CLASS))
555         {
556
557           if (TYPE_CODE(type) == TYPE_CODE_REF)
558             {
559               /* Copy value, change to pointer, so we don't get an
560                * error about a non-pointer type in value_rtti_target_type
561                */
562               struct value *temparg;
563               temparg=value_copy(val);
564               deprecated_set_value_type (temparg, lookup_pointer_type (TYPE_TARGET_TYPE(type)));
565               val=temparg;
566             }
567           /* Pointer to class, check real type of object */
568           fprintf_filtered (stream, "(");
569           real_type = value_rtti_target_type (val, &full, &top, &using_enc);
570           if (real_type)
571             {
572               /* RTTI entry found */
573               if (TYPE_CODE (type) == TYPE_CODE_PTR)
574                 {
575                   /* create a pointer type pointing to the real type */
576                   type = lookup_pointer_type (real_type);
577                 }
578               else
579                 {
580                   /* create a reference type referencing the real type */
581                   type = lookup_reference_type (real_type);
582                 }
583               /* JYG: Need to adjust pointer value. */
584               /* NOTE: cagney/2005-01-02: THIS IS BOGUS.  */
585               value_contents_writeable (val)[0] -= top;
586
587               /* Note: When we look up RTTI entries, we don't get any 
588                  information on const or volatile attributes */
589             }
590           type_print (type, "", stream, -1);
591           fprintf_filtered (stream, ") ");
592         }
593       else
594         {
595           /* normal case */
596           fprintf_filtered (stream, "(");
597           type_print (value_type (val), "", stream, -1);
598           fprintf_filtered (stream, ") ");
599         }
600     }
601
602   if (!value_initialized (val))
603     fprintf_filtered (stream, " [uninitialized] ");
604
605   if (objectprint && (TYPE_CODE (type) == TYPE_CODE_CLASS))
606     {
607       /* Attempt to determine real type of object */
608       real_type = value_rtti_type (val, &full, &top, &using_enc);
609       if (real_type)
610         {
611           /* We have RTTI information, so use it */
612           val = value_full_object (val, real_type, full, top, using_enc);
613           fprintf_filtered (stream, "(%s%s) ",
614                             TYPE_NAME (real_type),
615                             full ? "" : _(" [incomplete object]"));
616           /* Print out object: enclosing type is same as real_type if full */
617           return val_print (value_enclosing_type (val),
618                             value_contents_all (val), 0,
619                             VALUE_ADDRESS (val), stream, format, 1, 0,
620                             pretty, current_language);
621           /* Note: When we look up RTTI entries, we don't get any information on
622              const or volatile attributes */
623         }
624       else if (type != check_typedef (value_enclosing_type (val)))
625         {
626           /* No RTTI information, so let's do our best */
627           fprintf_filtered (stream, "(%s ?) ",
628                             TYPE_NAME (value_enclosing_type (val)));
629           return val_print (value_enclosing_type (val),
630                             value_contents_all (val), 0,
631                             VALUE_ADDRESS (val), stream, format, 1, 0,
632                             pretty, current_language);
633         }
634       /* Otherwise, we end up at the return outside this "if" */
635     }
636
637   return val_print (type, value_contents_all (val),
638                     value_embedded_offset (val),
639                     VALUE_ADDRESS (val) + value_offset (val),
640                     stream, format, 1, 0, pretty, current_language);
641 }