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