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