* symtab.h (lookup_symbol_in_language): Remove SYMTAB 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,
193                              TYPE_DOMAIN_TYPE (type),
194                              stream, "&");
195       break;
196
197     case TYPE_CODE_METHODPTR:
198       cplus_print_method_ptr (valaddr + embedded_offset, type, stream);
199       break;
200
201     case TYPE_CODE_PTR:
202       if (format && format != 's')
203         {
204           print_scalar_formatted (valaddr + embedded_offset, type, format, 0, stream);
205           break;
206         }
207       if (vtblprint && cp_is_vtbl_ptr_type (type))
208         {
209           /* Print the unmangled name if desired.  */
210           /* Print vtable entry - we only get here if we ARE using
211              -fvtable_thunks.  (Otherwise, look under TYPE_CODE_STRUCT.) */
212           CORE_ADDR addr
213             = extract_typed_address (valaddr + embedded_offset, type);
214           print_function_pointer_address (addr, stream);
215           break;
216         }
217       elttype = check_typedef (TYPE_TARGET_TYPE (type));
218         {
219           addr = unpack_pointer (type, valaddr + embedded_offset);
220         print_unpacked_pointer:
221
222           if (TYPE_CODE (elttype) == TYPE_CODE_FUNC)
223             {
224               /* Try to print what function it points to.  */
225               print_function_pointer_address (addr, stream);
226               /* Return value is irrelevant except for string pointers.  */
227               return (0);
228             }
229
230           if (addressprint)
231             fputs_filtered (paddress (addr), stream);
232
233           /* For a pointer to a textual type, also print the string
234              pointed to, unless pointer is null.  */
235           /* FIXME: need to handle wchar_t here... */
236
237           if (textual_element_type (elttype, format) && addr != 0)
238             {
239               i = val_print_string (addr, -1, TYPE_LENGTH (elttype), stream);
240             }
241           else if (cp_is_vtbl_member (type))
242             {
243               /* print vtbl's nicely */
244               CORE_ADDR vt_address = unpack_pointer (type, valaddr + embedded_offset);
245
246               struct minimal_symbol *msymbol =
247               lookup_minimal_symbol_by_pc (vt_address);
248               if ((msymbol != NULL) &&
249                   (vt_address == SYMBOL_VALUE_ADDRESS (msymbol)))
250                 {
251                   fputs_filtered (" <", stream);
252                   fputs_filtered (SYMBOL_PRINT_NAME (msymbol), stream);
253                   fputs_filtered (">", stream);
254                 }
255               if (vt_address && vtblprint)
256                 {
257                   struct value *vt_val;
258                   struct symbol *wsym = (struct symbol *) NULL;
259                   struct type *wtype;
260                   struct block *block = (struct block *) NULL;
261                   int is_this_fld;
262
263                   if (msymbol != NULL)
264                     wsym = lookup_symbol (DEPRECATED_SYMBOL_NAME (msymbol), block,
265                                           VAR_DOMAIN, &is_this_fld);
266
267                   if (wsym)
268                     {
269                       wtype = SYMBOL_TYPE (wsym);
270                     }
271                   else
272                     {
273                       wtype = TYPE_TARGET_TYPE (type);
274                     }
275                   vt_val = value_at (wtype, vt_address);
276                   common_val_print (vt_val, stream, format,
277                                     deref_ref, recurse + 1, pretty,
278                                     current_language);
279                   if (pretty)
280                     {
281                       fprintf_filtered (stream, "\n");
282                       print_spaces_filtered (2 + 2 * recurse, stream);
283                     }
284                 }
285             }
286
287           /* Return number of characters printed, including the terminating
288              '\0' if we reached the end.  val_print_string takes care including
289              the terminating '\0' if necessary.  */
290           return i;
291         }
292       break;
293
294     case TYPE_CODE_REF:
295       elttype = check_typedef (TYPE_TARGET_TYPE (type));
296       if (addressprint)
297         {
298           CORE_ADDR addr
299             = extract_typed_address (valaddr + embedded_offset, type);
300           fprintf_filtered (stream, "@");
301           fputs_filtered (paddress (addr), stream);
302           if (deref_ref)
303             fputs_filtered (": ", stream);
304         }
305       /* De-reference the reference.  */
306       if (deref_ref)
307         {
308           if (TYPE_CODE (elttype) != TYPE_CODE_UNDEF)
309             {
310               struct value *deref_val =
311               value_at
312               (TYPE_TARGET_TYPE (type),
313                unpack_pointer (lookup_pointer_type (builtin_type_void),
314                                valaddr + embedded_offset));
315               common_val_print (deref_val, stream, format, deref_ref,
316                                 recurse, pretty, current_language);
317             }
318           else
319             fputs_filtered ("???", stream);
320         }
321       break;
322
323     case TYPE_CODE_UNION:
324       if (recurse && !unionprint)
325         {
326           fprintf_filtered (stream, "{...}");
327           break;
328         }
329       /* Fall through.  */
330     case TYPE_CODE_STRUCT:
331       /*FIXME: Abstract this away */
332       if (vtblprint && cp_is_vtbl_ptr_type (type))
333         {
334           /* Print the unmangled name if desired.  */
335           /* Print vtable entry - we only get here if NOT using
336              -fvtable_thunks.  (Otherwise, look under TYPE_CODE_PTR.) */
337           int offset = (embedded_offset +
338                         TYPE_FIELD_BITPOS (type, VTBL_FNADDR_OFFSET) / 8);
339           struct type *field_type = TYPE_FIELD_TYPE (type, VTBL_FNADDR_OFFSET);
340           CORE_ADDR addr
341             = extract_typed_address (valaddr + offset, field_type);
342
343           print_function_pointer_address (addr, stream);
344         }
345       else
346         cp_print_value_fields (type, type, valaddr, embedded_offset, address, stream, format,
347                                recurse, pretty, NULL, 0);
348       break;
349
350     case TYPE_CODE_ENUM:
351       if (format)
352         {
353           print_scalar_formatted (valaddr + embedded_offset, type, format, 0, stream);
354           break;
355         }
356       len = TYPE_NFIELDS (type);
357       val = unpack_long (type, valaddr + embedded_offset);
358       for (i = 0; i < len; i++)
359         {
360           QUIT;
361           if (val == TYPE_FIELD_BITPOS (type, i))
362             {
363               break;
364             }
365         }
366       if (i < len)
367         {
368           fputs_filtered (TYPE_FIELD_NAME (type, i), stream);
369         }
370       else
371         {
372           print_longest (stream, 'd', 0, val);
373         }
374       break;
375
376     case TYPE_CODE_FLAGS:
377       if (format)
378           print_scalar_formatted (valaddr + embedded_offset, type, format, 0, stream);
379       else
380         val_print_type_code_flags (type, valaddr + embedded_offset, stream);
381       break;
382
383     case TYPE_CODE_FUNC:
384     case TYPE_CODE_METHOD:
385       if (format)
386         {
387           print_scalar_formatted (valaddr + embedded_offset, type, format, 0, stream);
388           break;
389         }
390       /* FIXME, we should consider, at least for ANSI C language, eliminating
391          the distinction made between FUNCs and POINTERs to FUNCs.  */
392       fprintf_filtered (stream, "{");
393       type_print (type, "", stream, -1);
394       fprintf_filtered (stream, "} ");
395       /* Try to print what function it points to, and its address.  */
396       print_address_demangle (address, stream, demangle);
397       break;
398
399     case TYPE_CODE_BOOL:
400       format = format ? format : output_format;
401       if (format)
402         print_scalar_formatted (valaddr + embedded_offset, type, format, 0, stream);
403       else
404         {
405           val = unpack_long (type, valaddr + embedded_offset);
406           if (val == 0)
407             fputs_filtered ("false", stream);
408           else if (val == 1)
409             fputs_filtered ("true", stream);
410           else
411             print_longest (stream, 'd', 0, val);
412         }
413       break;
414
415     case TYPE_CODE_RANGE:
416       /* FIXME: create_range_type does not set the unsigned bit in a
417          range type (I think it probably should copy it from the target
418          type), so we won't print values which are too large to
419          fit in a signed integer correctly.  */
420       /* FIXME: Doesn't handle ranges of enums correctly.  (Can't just
421          print with the target type, though, because the size of our type
422          and the target type might differ).  */
423       /* FALLTHROUGH */
424
425     case TYPE_CODE_INT:
426       format = format ? format : output_format;
427       if (format)
428         {
429           print_scalar_formatted (valaddr + embedded_offset, type, format, 0, stream);
430         }
431       else
432         {
433           val_print_type_code_int (type, valaddr + embedded_offset, stream);
434           /* C and C++ has no single byte int type, char is used instead.
435              Since we don't know whether the value is really intended to
436              be used as an integer or a character, print the character
437              equivalent as well.  */
438           if (textual_element_type (type, format))
439             {
440               fputs_filtered (" ", stream);
441               LA_PRINT_CHAR ((unsigned char) unpack_long (type, valaddr + embedded_offset),
442                              stream);
443             }
444         }
445       break;
446
447     case TYPE_CODE_CHAR:
448       format = format ? format : output_format;
449       if (format)
450         {
451           print_scalar_formatted (valaddr + embedded_offset, type, format, 0, stream);
452         }
453       else
454         {
455           val = unpack_long (type, valaddr + embedded_offset);
456           if (TYPE_UNSIGNED (type))
457             fprintf_filtered (stream, "%u", (unsigned int) val);
458           else
459             fprintf_filtered (stream, "%d", (int) val);
460           fputs_filtered (" ", stream);
461           LA_PRINT_CHAR ((unsigned char) val, stream);
462         }
463       break;
464
465     case TYPE_CODE_FLT:
466       if (format)
467         {
468           print_scalar_formatted (valaddr + embedded_offset, type, format, 0, stream);
469         }
470       else
471         {
472           print_floating (valaddr + embedded_offset, type, stream);
473         }
474       break;
475
476     case TYPE_CODE_DECFLOAT:
477       if (format)
478         print_scalar_formatted (valaddr + embedded_offset, type, format, 0, stream);
479       else
480         print_decimal_floating (valaddr + embedded_offset, type, stream);
481       break;
482
483     case TYPE_CODE_VOID:
484       fprintf_filtered (stream, "void");
485       break;
486
487     case TYPE_CODE_ERROR:
488       fprintf_filtered (stream, _("<error type>"));
489       break;
490
491     case TYPE_CODE_UNDEF:
492       /* This happens (without TYPE_FLAG_STUB set) on systems which don't use
493          dbx xrefs (NO_DBX_XREFS in gcc) if a file has a "struct foo *bar"
494          and no complete type for struct foo in that file.  */
495       fprintf_filtered (stream, _("<incomplete type>"));
496       break;
497
498     case TYPE_CODE_COMPLEX:
499       if (format)
500         print_scalar_formatted (valaddr + embedded_offset,
501                                 TYPE_TARGET_TYPE (type),
502                                 format, 0, stream);
503       else
504         print_floating (valaddr + embedded_offset, TYPE_TARGET_TYPE (type),
505                         stream);
506       fprintf_filtered (stream, " + ");
507       if (format)
508         print_scalar_formatted (valaddr + embedded_offset
509                                 + TYPE_LENGTH (TYPE_TARGET_TYPE (type)),
510                                 TYPE_TARGET_TYPE (type),
511                                 format, 0, stream);
512       else
513         print_floating (valaddr + embedded_offset
514                         + TYPE_LENGTH (TYPE_TARGET_TYPE (type)),
515                         TYPE_TARGET_TYPE (type),
516                         stream);
517       fprintf_filtered (stream, " * I");
518       break;
519
520     default:
521       error (_("Invalid C/C++ type code %d in symbol table."), TYPE_CODE (type));
522     }
523   gdb_flush (stream);
524   return (0);
525 }
526 \f
527 int
528 c_value_print (struct value *val, struct ui_file *stream, int format,
529                enum val_prettyprint pretty)
530 {
531   struct type *type, *real_type;
532   int full, top, using_enc;
533
534   /* If it is a pointer, indicate what it points to.
535
536      Print type also if it is a reference.
537
538      C++: if it is a member pointer, we will take care
539      of that when we print it.  */
540
541   type = check_typedef (value_type (val));
542
543   if (TYPE_CODE (type) == TYPE_CODE_PTR
544       || TYPE_CODE (type) == TYPE_CODE_REF)
545     {
546       /* Hack:  remove (char *) for char strings.  Their
547          type is indicated by the quoted string anyway.
548          (Don't use textual_element_type here; quoted strings
549          are always exactly (char *).  */
550       if (TYPE_CODE (type) == TYPE_CODE_PTR
551           && TYPE_NAME (type) == NULL
552           && TYPE_NAME (TYPE_TARGET_TYPE (type)) != NULL
553           && strcmp (TYPE_NAME (TYPE_TARGET_TYPE (type)), "char") == 0)
554         {
555           /* Print nothing */
556         }
557       else if (objectprint && (TYPE_CODE (TYPE_TARGET_TYPE (type)) == TYPE_CODE_CLASS))
558         {
559
560           if (TYPE_CODE(type) == TYPE_CODE_REF)
561             {
562               /* Copy value, change to pointer, so we don't get an
563                * error about a non-pointer type in value_rtti_target_type
564                */
565               struct value *temparg;
566               temparg=value_copy(val);
567               deprecated_set_value_type (temparg, lookup_pointer_type (TYPE_TARGET_TYPE(type)));
568               val=temparg;
569             }
570           /* Pointer to class, check real type of object */
571           fprintf_filtered (stream, "(");
572           real_type = value_rtti_target_type (val, &full, &top, &using_enc);
573           if (real_type)
574             {
575               /* RTTI entry found */
576               if (TYPE_CODE (type) == TYPE_CODE_PTR)
577                 {
578                   /* create a pointer type pointing to the real type */
579                   type = lookup_pointer_type (real_type);
580                 }
581               else
582                 {
583                   /* create a reference type referencing the real type */
584                   type = lookup_reference_type (real_type);
585                 }
586               /* JYG: Need to adjust pointer value. */
587               /* NOTE: cagney/2005-01-02: THIS IS BOGUS.  */
588               value_contents_writeable (val)[0] -= top;
589
590               /* Note: When we look up RTTI entries, we don't get any 
591                  information on const or volatile attributes */
592             }
593           type_print (type, "", stream, -1);
594           fprintf_filtered (stream, ") ");
595         }
596       else
597         {
598           /* normal case */
599           fprintf_filtered (stream, "(");
600           type_print (value_type (val), "", stream, -1);
601           fprintf_filtered (stream, ") ");
602         }
603     }
604
605   if (!value_initialized (val))
606     fprintf_filtered (stream, " [uninitialized] ");
607
608   if (objectprint && (TYPE_CODE (type) == TYPE_CODE_CLASS))
609     {
610       /* Attempt to determine real type of object */
611       real_type = value_rtti_type (val, &full, &top, &using_enc);
612       if (real_type)
613         {
614           /* We have RTTI information, so use it */
615           val = value_full_object (val, real_type, full, top, using_enc);
616           fprintf_filtered (stream, "(%s%s) ",
617                             TYPE_NAME (real_type),
618                             full ? "" : _(" [incomplete object]"));
619           /* Print out object: enclosing type is same as real_type if full */
620           return val_print (value_enclosing_type (val),
621                             value_contents_all (val), 0,
622                             VALUE_ADDRESS (val), stream, format, 1, 0,
623                             pretty, current_language);
624           /* Note: When we look up RTTI entries, we don't get any information on
625              const or volatile attributes */
626         }
627       else if (type != check_typedef (value_enclosing_type (val)))
628         {
629           /* No RTTI information, so let's do our best */
630           fprintf_filtered (stream, "(%s ?) ",
631                             TYPE_NAME (value_enclosing_type (val)));
632           return val_print (value_enclosing_type (val),
633                             value_contents_all (val), 0,
634                             VALUE_ADDRESS (val), stream, format, 1, 0,
635                             pretty, current_language);
636         }
637       /* Otherwise, we end up at the return outside this "if" */
638     }
639
640   return val_print (type, value_contents_all (val),
641                     value_embedded_offset (val),
642                     VALUE_ADDRESS (val) + value_offset (val),
643                     stream, format, 1, 0, pretty, current_language);
644 }