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