* valprint.c (val_print_string): Don't print leading space.
[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-2001, 2003, 2005-2012 Free
4    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 3 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, see <http://www.gnu.org/licenses/>.  */
20
21 #include "defs.h"
22 #include "gdb_string.h"
23 #include "symtab.h"
24 #include "gdbtypes.h"
25 #include "expression.h"
26 #include "value.h"
27 #include "valprint.h"
28 #include "language.h"
29 #include "c-lang.h"
30 #include "cp-abi.h"
31 #include "target.h"
32 \f
33
34 /* A helper for c_textual_element_type.  This checks the name of the
35    typedef.  This is bogus but it isn't apparent that the compiler
36    provides us the help we may need.  */
37
38 static int
39 textual_name (const char *name)
40 {
41   return (!strcmp (name, "wchar_t")
42           || !strcmp (name, "char16_t")
43           || !strcmp (name, "char32_t"));
44 }
45
46 /* Apply a heuristic to decide whether an array of TYPE or a pointer
47    to TYPE should be printed as a textual string.  Return non-zero if
48    it should, or zero if it should be treated as an array of integers
49    or pointer to integers.  FORMAT is the current format letter, or 0
50    if none.
51
52    We guess that "char" is a character.  Explicitly signed and
53    unsigned character types are also characters.  Integer data from
54    vector types is not.  The user can override this by using the /s
55    format letter.  */
56
57 int
58 c_textual_element_type (struct type *type, char format)
59 {
60   struct type *true_type, *iter_type;
61
62   if (format != 0 && format != 's')
63     return 0;
64
65   /* We also rely on this for its side effect of setting up all the
66      typedef pointers.  */
67   true_type = check_typedef (type);
68
69   /* TYPE_CODE_CHAR is always textual.  */
70   if (TYPE_CODE (true_type) == TYPE_CODE_CHAR)
71     return 1;
72
73   /* Any other character-like types must be integral.  */
74   if (TYPE_CODE (true_type) != TYPE_CODE_INT)
75     return 0;
76
77   /* We peel typedefs one by one, looking for a match.  */
78   iter_type = type;
79   while (iter_type)
80     {
81       /* Check the name of the type.  */
82       if (TYPE_NAME (iter_type) && textual_name (TYPE_NAME (iter_type)))
83         return 1;
84
85       if (TYPE_CODE (iter_type) != TYPE_CODE_TYPEDEF)
86         break;
87
88       /* Peel a single typedef.  If the typedef doesn't have a target
89          type, we use check_typedef and hope the result is ok -- it
90          might be for C++, where wchar_t is a built-in type.  */
91       if (TYPE_TARGET_TYPE (iter_type))
92         iter_type = TYPE_TARGET_TYPE (iter_type);
93       else
94         iter_type = check_typedef (iter_type);
95     }
96
97   if (format == 's')
98     {
99       /* Print this as a string if we can manage it.  For now, no wide
100          character support.  */
101       if (TYPE_CODE (true_type) == TYPE_CODE_INT
102           && TYPE_LENGTH (true_type) == 1)
103         return 1;
104     }
105   else
106     {
107       /* If a one-byte TYPE_CODE_INT is missing the not-a-character
108          flag, then we treat it as text; otherwise, we assume it's
109          being used as data.  */
110       if (TYPE_CODE (true_type) == TYPE_CODE_INT
111           && TYPE_LENGTH (true_type) == 1
112           && !TYPE_NOTTEXT (true_type))
113         return 1;
114     }
115
116   return 0;
117 }
118
119 /* Decorations for C.  */
120
121 static const struct generic_val_print_decorations c_decorations =
122 {
123   "",
124   " + ",
125   " * I",
126   "true",
127   "false",
128   "void"
129 };
130
131 /* See val_print for a description of the various parameters of this
132    function; they are identical.  */
133
134 void
135 c_val_print (struct type *type, const gdb_byte *valaddr,
136              int embedded_offset, CORE_ADDR address,
137              struct ui_file *stream, int recurse,
138              const struct value *original_value,
139              const struct value_print_options *options)
140 {
141   struct gdbarch *gdbarch = get_type_arch (type);
142   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
143   unsigned int i = 0;   /* Number of characters printed.  */
144   unsigned len;
145   struct type *elttype, *unresolved_elttype;
146   struct type *unresolved_type = type;
147   unsigned eltlen;
148   LONGEST val;
149   CORE_ADDR addr;
150
151   CHECK_TYPEDEF (type);
152   switch (TYPE_CODE (type))
153     {
154     case TYPE_CODE_ARRAY:
155       unresolved_elttype = TYPE_TARGET_TYPE (type);
156       elttype = check_typedef (unresolved_elttype);
157       if (TYPE_LENGTH (type) > 0 && TYPE_LENGTH (unresolved_elttype) > 0)
158         {
159           LONGEST low_bound, high_bound;
160
161           if (!get_array_bounds (type, &low_bound, &high_bound))
162             error (_("Could not determine the array high bound"));
163
164           eltlen = TYPE_LENGTH (elttype);
165           len = high_bound - low_bound + 1;
166           if (options->prettyprint_arrays)
167             {
168               print_spaces_filtered (2 + 2 * recurse, stream);
169             }
170
171           /* Print arrays of textual chars with a string syntax, as
172              long as the entire array is valid.  */
173           if (c_textual_element_type (unresolved_elttype,
174                                       options->format)
175               && value_bytes_available (original_value, embedded_offset,
176                                         TYPE_LENGTH (type))
177               && value_bits_valid (original_value,
178                                    TARGET_CHAR_BIT * embedded_offset,
179                                    TARGET_CHAR_BIT * TYPE_LENGTH (type)))
180             {
181               /* If requested, look for the first null char and only
182                  print elements up to it.  */
183               if (options->stop_print_at_null)
184                 {
185                   unsigned int temp_len;
186
187                   for (temp_len = 0;
188                        (temp_len < len
189                         && temp_len < options->print_max
190                         && extract_unsigned_integer (valaddr + embedded_offset
191                                                      + temp_len * eltlen,
192                                                      eltlen, byte_order) != 0);
193                        ++temp_len)
194                     ;
195                   len = temp_len;
196                 }
197
198               LA_PRINT_STRING (stream, unresolved_elttype,
199                                valaddr + embedded_offset, len,
200                                NULL, 0, options);
201               i = len;
202             }
203           else
204             {
205               fprintf_filtered (stream, "{");
206               /* If this is a virtual function table, print the 0th
207                  entry specially, and the rest of the members
208                  normally.  */
209               if (cp_is_vtbl_ptr_type (elttype))
210                 {
211                   i = 1;
212                   fprintf_filtered (stream, _("%d vtable entries"),
213                                     len - 1);
214                 }
215               else
216                 {
217                   i = 0;
218                 }
219               val_print_array_elements (type, valaddr, embedded_offset,
220                                         address, stream,
221                                         recurse, original_value, options, i);
222               fprintf_filtered (stream, "}");
223             }
224           break;
225         }
226       /* Array of unspecified length: treat like pointer to first
227          elt.  */
228       addr = address + embedded_offset;
229       goto print_unpacked_pointer;
230
231     case TYPE_CODE_METHODPTR:
232       cplus_print_method_ptr (valaddr + embedded_offset, type, stream);
233       break;
234
235     case TYPE_CODE_PTR:
236       if (options->format && options->format != 's')
237         {
238           val_print_scalar_formatted (type, valaddr, embedded_offset,
239                                       original_value, options, 0, stream);
240           break;
241         }
242       if (options->vtblprint && cp_is_vtbl_ptr_type (type))
243         {
244           /* Print the unmangled name if desired.  */
245           /* Print vtable entry - we only get here if we ARE using
246              -fvtable_thunks.  (Otherwise, look under
247              TYPE_CODE_STRUCT.)  */
248           CORE_ADDR addr
249             = extract_typed_address (valaddr + embedded_offset, type);
250
251           print_function_pointer_address (options, gdbarch, addr, stream);
252           break;
253         }
254       unresolved_elttype = TYPE_TARGET_TYPE (type);
255       elttype = check_typedef (unresolved_elttype);
256         {
257           int want_space;
258
259           addr = unpack_pointer (type, valaddr + embedded_offset);
260         print_unpacked_pointer:
261
262           want_space = 0;
263
264           if (TYPE_CODE (elttype) == TYPE_CODE_FUNC)
265             {
266               /* Try to print what function it points to.  */
267               print_function_pointer_address (options, gdbarch, addr, stream);
268               return;
269             }
270
271           if (options->addressprint)
272             {
273               fputs_filtered (paddress (gdbarch, addr), stream);
274               want_space = 1;
275             }
276
277           /* For a pointer to a textual type, also print the string
278              pointed to, unless pointer is null.  */
279
280           if (c_textual_element_type (unresolved_elttype,
281                                       options->format)
282               && addr != 0)
283             {
284               if (want_space)
285                 fputs_filtered (" ", stream);
286               i = val_print_string (unresolved_elttype, NULL,
287                                     addr, -1,
288                                     stream, options);
289             }
290           else if (cp_is_vtbl_member (type))
291             {
292               /* Print vtbl's nicely.  */
293               CORE_ADDR vt_address = unpack_pointer (type,
294                                                      valaddr
295                                                      + embedded_offset);
296               struct minimal_symbol *msymbol =
297               lookup_minimal_symbol_by_pc (vt_address);
298
299               if ((msymbol != NULL)
300                   && (vt_address == SYMBOL_VALUE_ADDRESS (msymbol)))
301                 {
302                   if (want_space)
303                     fputs_filtered (" ", stream);
304                   fputs_filtered (" <", stream);
305                   fputs_filtered (SYMBOL_PRINT_NAME (msymbol), stream);
306                   fputs_filtered (">", stream);
307                   want_space = 1;
308                 }
309
310               if (vt_address && options->vtblprint)
311                 {
312                   struct value *vt_val;
313                   struct symbol *wsym = (struct symbol *) NULL;
314                   struct type *wtype;
315                   struct block *block = (struct block *) NULL;
316                   int is_this_fld;
317
318                   if (want_space)
319                     fputs_filtered (" ", stream);
320
321                   if (msymbol != NULL)
322                     wsym = lookup_symbol (SYMBOL_LINKAGE_NAME (msymbol),
323                                           block, VAR_DOMAIN,
324                                           &is_this_fld);
325
326                   if (wsym)
327                     {
328                       wtype = SYMBOL_TYPE (wsym);
329                     }
330                   else
331                     {
332                       wtype = unresolved_elttype;
333                     }
334                   vt_val = value_at (wtype, vt_address);
335                   common_val_print (vt_val, stream, recurse + 1,
336                                     options, current_language);
337                   if (options->pretty)
338                     {
339                       fprintf_filtered (stream, "\n");
340                       print_spaces_filtered (2 + 2 * recurse, stream);
341                     }
342                 }
343             }
344           return;
345         }
346       break;
347
348     case TYPE_CODE_UNION:
349       if (recurse && !options->unionprint)
350         {
351           fprintf_filtered (stream, "{...}");
352           break;
353         }
354       /* Fall through.  */
355     case TYPE_CODE_STRUCT:
356       /*FIXME: Abstract this away.  */
357       if (options->vtblprint && cp_is_vtbl_ptr_type (type))
358         {
359           /* Print the unmangled name if desired.  */
360           /* Print vtable entry - we only get here if NOT using
361              -fvtable_thunks.  (Otherwise, look under
362              TYPE_CODE_PTR.)  */
363           int offset = (embedded_offset
364                         + TYPE_FIELD_BITPOS (type,
365                                              VTBL_FNADDR_OFFSET) / 8);
366           struct type *field_type = TYPE_FIELD_TYPE (type,
367                                                      VTBL_FNADDR_OFFSET);
368           CORE_ADDR addr
369             = extract_typed_address (valaddr + offset, field_type);
370
371           print_function_pointer_address (options, gdbarch, addr, stream);
372         }
373       else
374         cp_print_value_fields_rtti (type, valaddr,
375                                     embedded_offset, address,
376                                     stream, recurse,
377                                     original_value, options,
378                                     NULL, 0);
379       break;
380
381     case TYPE_CODE_INT:
382       if (options->format || options->output_format)
383         {
384           struct value_print_options opts = *options;
385
386           opts.format = (options->format ? options->format
387                          : options->output_format);
388           val_print_scalar_formatted (type, valaddr, embedded_offset,
389                                       original_value, &opts, 0, stream);
390         }
391       else
392         {
393           val_print_type_code_int (type, valaddr + embedded_offset,
394                                    stream);
395           /* C and C++ has no single byte int type, char is used
396              instead.  Since we don't know whether the value is really
397              intended to be used as an integer or a character, print
398              the character equivalent as well.  */
399           if (c_textual_element_type (unresolved_type, options->format))
400             {
401               fputs_filtered (" ", stream);
402               LA_PRINT_CHAR (unpack_long (type, valaddr + embedded_offset),
403                              unresolved_type, stream);
404             }
405         }
406       break;
407
408     case TYPE_CODE_MEMBERPTR:
409       if (!options->format)
410         {
411           cp_print_class_member (valaddr + embedded_offset, type, stream, "&");
412           break;
413         }
414       /* FALLTHROUGH */
415
416     case TYPE_CODE_REF:
417     case TYPE_CODE_ENUM:
418     case TYPE_CODE_FLAGS:
419     case TYPE_CODE_FUNC:
420     case TYPE_CODE_METHOD:
421     case TYPE_CODE_BOOL:
422     case TYPE_CODE_RANGE:
423     case TYPE_CODE_FLT:
424     case TYPE_CODE_DECFLOAT:
425     case TYPE_CODE_VOID:
426     case TYPE_CODE_ERROR:
427     case TYPE_CODE_UNDEF:
428     case TYPE_CODE_COMPLEX:
429     case TYPE_CODE_CHAR:
430     default:
431       generic_val_print (type, valaddr, embedded_offset, address,
432                          stream, recurse, original_value, options,
433                          &c_decorations);
434       break;
435     }
436   gdb_flush (stream);
437 }
438 \f
439 void
440 c_value_print (struct value *val, struct ui_file *stream, 
441                const struct value_print_options *options)
442 {
443   struct type *type, *real_type, *val_type;
444   int full, top, using_enc;
445   struct value_print_options opts = *options;
446
447   opts.deref_ref = 1;
448
449   /* If it is a pointer, indicate what it points to.
450
451      Print type also if it is a reference.
452
453      C++: if it is a member pointer, we will take care
454      of that when we print it.  */
455
456   /* Preserve the original type before stripping typedefs.  We prefer
457      to pass down the original type when possible, but for local
458      checks it is better to look past the typedefs.  */
459   val_type = value_type (val);
460   type = check_typedef (val_type);
461
462   if (TYPE_CODE (type) == TYPE_CODE_PTR
463       || TYPE_CODE (type) == TYPE_CODE_REF)
464     {
465       /* Hack:  remove (char *) for char strings.  Their
466          type is indicated by the quoted string anyway.
467          (Don't use c_textual_element_type here; quoted strings
468          are always exactly (char *), (wchar_t *), or the like.  */
469       if (TYPE_CODE (val_type) == TYPE_CODE_PTR
470           && TYPE_NAME (val_type) == NULL
471           && TYPE_NAME (TYPE_TARGET_TYPE (val_type)) != NULL
472           && (strcmp (TYPE_NAME (TYPE_TARGET_TYPE (val_type)),
473                       "char") == 0
474               || textual_name (TYPE_NAME (TYPE_TARGET_TYPE (val_type)))))
475         {
476           /* Print nothing.  */
477         }
478       else if (options->objectprint
479                && (TYPE_CODE (TYPE_TARGET_TYPE (type)) == TYPE_CODE_CLASS))
480         {
481
482           if (TYPE_CODE(type) == TYPE_CODE_REF)
483             {
484               /* Copy value, change to pointer, so we don't get an
485                  error about a non-pointer type in
486                  value_rtti_target_type.  */
487               struct value *temparg;
488               temparg=value_copy(val);
489               deprecated_set_value_type
490                 (temparg, lookup_pointer_type (TYPE_TARGET_TYPE (type)));
491               val = temparg;
492             }
493           /* Pointer to class, check real type of object.  */
494           fprintf_filtered (stream, "(");
495
496           if (value_entirely_available (val))
497             {
498               real_type = value_rtti_indirect_type (val, &full, &top,
499                                                     &using_enc);
500               if (real_type)
501                 {
502                   /* RTTI entry found.  */
503                   type = real_type;
504
505                   /* Need to adjust pointer value.  */
506                   val = value_from_pointer (type, value_as_address (val) - top);
507
508                   /* Note: When we look up RTTI entries, we don't get
509                      any information on const or volatile
510                      attributes.  */
511                 }
512             }
513           type_print (type, "", stream, -1);
514           fprintf_filtered (stream, ") ");
515           val_type = type;
516         }
517       else
518         {
519           /* normal case */
520           fprintf_filtered (stream, "(");
521           type_print (value_type (val), "", stream, -1);
522           fprintf_filtered (stream, ") ");
523         }
524     }
525
526   if (!value_initialized (val))
527     fprintf_filtered (stream, " [uninitialized] ");
528
529   if (options->objectprint && (TYPE_CODE (type) == TYPE_CODE_CLASS))
530     {
531       /* Attempt to determine real type of object.  */
532       real_type = value_rtti_type (val, &full, &top, &using_enc);
533       if (real_type)
534         {
535           /* We have RTTI information, so use it.  */
536           val = value_full_object (val, real_type, 
537                                    full, top, using_enc);
538           fprintf_filtered (stream, "(%s%s) ",
539                             TYPE_NAME (real_type),
540                             full ? "" : _(" [incomplete object]"));
541           /* Print out object: enclosing type is same as real_type if
542              full.  */
543           val_print (value_enclosing_type (val),
544                      value_contents_for_printing (val), 0,
545                      value_address (val), stream, 0,
546                      val, &opts, current_language);
547           return;
548           /* Note: When we look up RTTI entries, we don't get any
549              information on const or volatile attributes.  */
550         }
551       else if (type != check_typedef (value_enclosing_type (val)))
552         {
553           /* No RTTI information, so let's do our best.  */
554           fprintf_filtered (stream, "(%s ?) ",
555                             TYPE_NAME (value_enclosing_type (val)));
556           val_print (value_enclosing_type (val),
557                      value_contents_for_printing (val), 0,
558                      value_address (val), stream, 0,
559                      val, &opts, current_language);
560           return;
561         }
562       /* Otherwise, we end up at the return outside this "if".  */
563     }
564
565   val_print (val_type, value_contents_for_printing (val),
566              value_embedded_offset (val),
567              value_address (val),
568              stream, 0,
569              val, &opts, current_language);
570 }