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