* mdebugread.c (parse_symbol, psymtab_to_symtab_1): Initialize
[deliverable/binutils-gdb.git] / gdb / c-valprint.c
CommitLineData
a8a69e63 1/* Support for printing C values for GDB, the GNU debugger.
67e9b3b3 2 Copyright 1986, 1988, 1989, 1991, 1992, 1993, 1994
0568ccb0 3 Free Software Foundation, Inc.
a8a69e63
FF
4
5This file is part of GDB.
6
7This program is free software; you can redistribute it and/or modify
8it under the terms of the GNU General Public License as published by
9the Free Software Foundation; either version 2 of the License, or
10(at your option) any later version.
11
12This program is distributed in the hope that it will be useful,
13but WITHOUT ANY WARRANTY; without even the implied warranty of
14MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15GNU General Public License for more details.
16
17You should have received a copy of the GNU General Public License
18along with this program; if not, write to the Free Software
19Foundation, 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
32extern int vtblprint; /* Controls printing of vtbl's */
a8a69e63
FF
33
34extern void
199b2450 35cp_print_class_member PARAMS ((char *, struct type *, GDB_FILE *, char *));
a8a69e63 36
c7da3ed3 37extern void
199b2450 38cp_print_class_method PARAMS ((char *, struct type *, GDB_FILE *));
a8a69e63
FF
39
40extern void
199b2450 41cp_print_value_fields PARAMS ((struct type *, char *, GDB_FILE *, int, int,
a8a69e63
FF
42 enum val_prettyprint, struct type **));
43
c7da3ed3
FF
44extern int
45cp_is_vtbl_ptr_type PARAMS ((struct type *));
46
a8a69e63
FF
47extern int
48cp_is_vtbl_member PARAMS ((struct type *));
49
50/* END-FIXME */
51
52
53/* BEGIN-FIXME: Hooks into c-typeprint.c */
54
55extern void
199b2450 56c_type_print_varspec_prefix PARAMS ((struct type *, GDB_FILE *, int, int));
a8a69e63
FF
57
58extern void
59cp_type_print_method_args PARAMS ((struct type **, char *, char *, int,
199b2450 60 GDB_FILE *));
a8a69e63
FF
61/* END-FIXME */
62
63
64extern 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
80int
81c_val_print (type, valaddr, address, stream, format, deref_ref, recurse,
82 pretty)
83 struct type *type;
84 char *valaddr;
85 CORE_ADDR address;
199b2450 86 GDB_FILE *stream;
a8a69e63
FF
87 int format;
88 int deref_ref;
89 int recurse;
90 enum val_prettyprint pretty;
91{
c4413e2c 92 register unsigned int i = 0; /* Number of characters printed */
a8a69e63
FF
93 unsigned len;
94 struct type *elttype;
95 unsigned eltlen;
96 LONGEST val;
c4413e2c 97 CORE_ADDR addr;
a8a69e63
FF
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 }
a8a69e63 111 /* For an array of chars, print with string syntax. */
0568ccb0
SS
112 if (eltlen == 1 &&
113 ((TYPE_CODE (elttype) == TYPE_CODE_INT)
114 || ((current_language->la_language == language_m2)
115 && (TYPE_CODE (elttype) == TYPE_CODE_CHAR)))
c4413e2c 116 && (format == 0 || format == 's'))
a8a69e63 117 {
0568ccb0
SS
118 /* If requested, look for the first null char and only print
119 elements up to it. */
120 if (stop_print_at_null)
121 {
122 int temp_len;
123
124 /* Look for a NULL char. */
125 for (temp_len = 0;
126 valaddr[temp_len]
127 && temp_len < len && temp_len < print_max;
128 temp_len++);
129 len = temp_len;
130 }
131
a8a69e63 132 LA_PRINT_STRING (stream, valaddr, len, 0);
ec16f701 133 i = len;
a8a69e63
FF
134 }
135 else
136 {
c4413e2c 137 fprintf_filtered (stream, "{");
a8a69e63
FF
138 /* If this is a virtual function table, print the 0th
139 entry specially, and the rest of the members normally. */
140 if (cp_is_vtbl_ptr_type (elttype))
141 {
142 i = 1;
143 fprintf_filtered (stream, "%d vtable entries", len - 1);
144 }
145 else
146 {
147 i = 0;
148 }
149 val_print_array_elements (type, valaddr, address, stream,
150 format, deref_ref, recurse, pretty, i);
c4413e2c 151 fprintf_filtered (stream, "}");
a8a69e63 152 }
a8a69e63
FF
153 break;
154 }
155 /* Array of unspecified length: treat like pointer to first elt. */
1326dace
JK
156 addr = address;
157 goto print_unpacked_pointer;
a8a69e63
FF
158
159 case TYPE_CODE_PTR:
160 if (format && format != 's')
161 {
162 print_scalar_formatted (valaddr, type, format, 0, stream);
163 break;
164 }
36a2283d
PB
165 if (vtblprint && cp_is_vtbl_ptr_type(type))
166 {
167 /* Print the unmangled name if desired. */
168 /* Print vtable entry - we only get here if we ARE using
169 -fvtable_thunks. (Otherwise, look under TYPE_CODE_STRUCT.) */
170 print_address_demangle(extract_address (valaddr, TYPE_LENGTH (type)),
171 stream, demangle);
172 break;
173 }
a8a69e63
FF
174 if (TYPE_CODE (TYPE_TARGET_TYPE (type)) == TYPE_CODE_METHOD)
175 {
c7da3ed3 176 cp_print_class_method (valaddr, type, stream);
a8a69e63
FF
177 }
178 else if (TYPE_CODE (TYPE_TARGET_TYPE (type)) == TYPE_CODE_MEMBER)
179 {
180 cp_print_class_member (valaddr,
181 TYPE_DOMAIN_TYPE (TYPE_TARGET_TYPE (type)),
182 stream, "&");
183 }
184 else
185 {
c4413e2c 186 addr = unpack_pointer (type, valaddr);
1326dace 187 print_unpacked_pointer:
a8a69e63
FF
188 elttype = TYPE_TARGET_TYPE (type);
189
190 if (TYPE_CODE (elttype) == TYPE_CODE_FUNC)
191 {
192 /* Try to print what function it points to. */
193 print_address_demangle (addr, stream, demangle);
194 /* Return value is irrelevant except for string pointers. */
195 return (0);
196 }
197
198 if (addressprint && format != 's')
199 {
d24c0599 200 print_address_numeric (addr, 1, stream);
a8a69e63
FF
201 }
202
203 /* For a pointer to char or unsigned char, also print the string
204 pointed to, unless pointer is null. */
c4413e2c
FF
205 if (TYPE_LENGTH (elttype) == 1
206 && TYPE_CODE (elttype) == TYPE_CODE_INT
207 && (format == 0 || format == 's')
208 && addr != 0)
a8a69e63 209 {
c4413e2c 210 i = val_print_string (addr, 0, stream);
a8a69e63
FF
211 }
212 else if (cp_is_vtbl_member(type))
213 {
214 /* print vtbl's nicely */
215 CORE_ADDR vt_address = unpack_pointer (type, valaddr);
216
217 struct minimal_symbol *msymbol =
218 lookup_minimal_symbol_by_pc (vt_address);
2e4964ad
FF
219 if ((msymbol != NULL) &&
220 (vt_address == SYMBOL_VALUE_ADDRESS (msymbol)))
a8a69e63
FF
221 {
222 fputs_filtered (" <", stream);
2e4964ad 223 fputs_filtered (SYMBOL_SOURCE_NAME (msymbol), stream);
a8a69e63
FF
224 fputs_filtered (">", stream);
225 }
40f24469 226 if (vt_address && vtblprint)
a8a69e63 227 {
82a2edfb 228 value_ptr vt_val;
1bd97ba8
KH
229 struct symbol *wsym = (struct symbol *)NULL;
230 struct type *wtype;
231 struct symtab *s;
232 struct block *block = (struct block *)NULL;
233 int is_this_fld;
234
40f24469
KH
235 if (msymbol != NULL)
236 wsym = lookup_symbol (SYMBOL_NAME(msymbol), block,
1bd97ba8
KH
237 VAR_NAMESPACE, &is_this_fld, &s);
238
239 if (wsym)
240 {
241 wtype = SYMBOL_TYPE(wsym);
242 }
243 else
244 {
245 wtype = TYPE_TARGET_TYPE(type);
246 }
247 vt_val = value_at (wtype, vt_address);
a8a69e63
FF
248 val_print (VALUE_TYPE (vt_val), VALUE_CONTENTS (vt_val),
249 VALUE_ADDRESS (vt_val), stream, format,
250 deref_ref, recurse + 1, pretty);
251 if (pretty)
252 {
253 fprintf_filtered (stream, "\n");
254 print_spaces_filtered (2 + 2 * recurse, stream);
255 }
256 }
257 }
258
79f3d586
JK
259 /* Return number of characters printed, including the terminating
260 '\0' if we reached the end. val_print_string takes care including
261 the terminating '\0' if necessary. */
262 return i;
a8a69e63
FF
263 }
264 break;
265
266 case TYPE_CODE_MEMBER:
267 error ("not implemented: member type in c_val_print");
268 break;
269
270 case TYPE_CODE_REF:
271 if (TYPE_CODE (TYPE_TARGET_TYPE (type)) == TYPE_CODE_MEMBER)
272 {
273 cp_print_class_member (valaddr,
274 TYPE_DOMAIN_TYPE (TYPE_TARGET_TYPE (type)),
275 stream, "");
276 break;
277 }
278 if (addressprint)
279 {
833e0d94
JK
280 fprintf_filtered (stream, "@");
281 print_address_numeric
282 (extract_address (valaddr,
d24c0599 283 TARGET_PTR_BIT / HOST_CHAR_BIT), 1, stream);
a8a69e63
FF
284 if (deref_ref)
285 fputs_filtered (": ", stream);
286 }
287 /* De-reference the reference. */
288 if (deref_ref)
289 {
290 if (TYPE_CODE (TYPE_TARGET_TYPE (type)) != TYPE_CODE_UNDEF)
291 {
82a2edfb 292 value_ptr deref_val =
a8a69e63
FF
293 value_at
294 (TYPE_TARGET_TYPE (type),
295 unpack_pointer (lookup_pointer_type (builtin_type_void),
296 valaddr));
297 val_print (VALUE_TYPE (deref_val),
298 VALUE_CONTENTS (deref_val),
299 VALUE_ADDRESS (deref_val), stream, format,
300 deref_ref, recurse + 1, pretty);
301 }
302 else
303 fputs_filtered ("???", stream);
304 }
305 break;
306
307 case TYPE_CODE_UNION:
308 if (recurse && !unionprint)
309 {
310 fprintf_filtered (stream, "{...}");
311 break;
312 }
313 /* Fall through. */
314 case TYPE_CODE_STRUCT:
315 if (vtblprint && cp_is_vtbl_ptr_type(type))
316 {
317 /* Print the unmangled name if desired. */
36a2283d
PB
318 /* Print vtable entry - we only get here if NOT using
319 -fvtable_thunks. (Otherwise, look under TYPE_CODE_PTR.) */
a8a69e63
FF
320 print_address_demangle(*((int *) (valaddr + /* FIXME bytesex */
321 TYPE_FIELD_BITPOS (type, VTBL_FNADDR_OFFSET) / 8)),
322 stream, demangle);
323 break;
324 }
325 cp_print_value_fields (type, valaddr, stream, format, recurse, pretty,
326 0);
327 break;
328
329 case TYPE_CODE_ENUM:
330 if (format)
331 {
332 print_scalar_formatted (valaddr, type, format, 0, stream);
333 break;
334 }
335 len = TYPE_NFIELDS (type);
1d9449ab 336 val = unpack_long (type, valaddr);
a8a69e63
FF
337 for (i = 0; i < len; i++)
338 {
339 QUIT;
340 if (val == TYPE_FIELD_BITPOS (type, i))
341 {
342 break;
343 }
344 }
345 if (i < len)
346 {
347 fputs_filtered (TYPE_FIELD_NAME (type, i), stream);
348 }
349 else
350 {
7efb57c3 351 print_longest (stream, 'd', 0, val);
a8a69e63
FF
352 }
353 break;
354
355 case TYPE_CODE_FUNC:
356 if (format)
357 {
358 print_scalar_formatted (valaddr, type, format, 0, stream);
359 break;
360 }
361 /* FIXME, we should consider, at least for ANSI C language, eliminating
362 the distinction made between FUNCs and POINTERs to FUNCs. */
363 fprintf_filtered (stream, "{");
364 type_print (type, "", stream, -1);
365 fprintf_filtered (stream, "} ");
366 /* Try to print what function it points to, and its address. */
367 print_address_demangle (address, stream, demangle);
368 break;
369
7e71985c
JK
370 case TYPE_CODE_BOOL:
371 /* Do something at least vaguely reasonable, for example if the
372 language is set wrong. */
373
85b8aa88
JK
374 case TYPE_CODE_RANGE:
375 /* FIXME: create_range_type does not set the unsigned bit in a
376 range type (I think it probably should copy it from the target
377 type), so we won't print values which are too large to
378 fit in a signed integer correctly. */
379 /* FIXME: Doesn't handle ranges of enums correctly. (Can't just
380 print with the target type, though, because the size of our type
381 and the target type might differ). */
382 /* FALLTHROUGH */
383
a8a69e63
FF
384 case TYPE_CODE_INT:
385 format = format ? format : output_format;
386 if (format)
387 {
388 print_scalar_formatted (valaddr, type, format, 0, stream);
389 }
390 else
391 {
392 val_print_type_code_int (type, valaddr, stream);
393 /* C and C++ has no single byte int type, char is used instead.
394 Since we don't know whether the value is really intended to
395 be used as an integer or a character, print the character
396 equivalent as well. */
397 if (TYPE_LENGTH (type) == 1)
398 {
399 fputs_filtered (" ", stream);
400 LA_PRINT_CHAR ((unsigned char) unpack_long (type, valaddr),
401 stream);
402 }
403 }
404 break;
405
406 case TYPE_CODE_CHAR:
407 format = format ? format : output_format;
408 if (format)
409 {
410 print_scalar_formatted (valaddr, type, format, 0, stream);
411 }
412 else
413 {
414 fprintf_filtered (stream, TYPE_UNSIGNED (type) ? "%u" : "%d",
415 unpack_long (type, valaddr));
416 fputs_filtered (" ", stream);
417 LA_PRINT_CHAR ((unsigned char) unpack_long (type, valaddr), stream);
418 }
419 break;
420
421 case TYPE_CODE_FLT:
422 if (format)
423 {
424 print_scalar_formatted (valaddr, type, format, 0, stream);
425 }
426 else
427 {
428 print_floating (valaddr, type, stream);
429 }
430 break;
431
432 case TYPE_CODE_VOID:
433 fprintf_filtered (stream, "void");
434 break;
435
436 case TYPE_CODE_ERROR:
437 fprintf_filtered (stream, "<error type>");
438 break;
439
a8a69e63
FF
440 case TYPE_CODE_UNDEF:
441 /* This happens (without TYPE_FLAG_STUB set) on systems which don't use
442 dbx xrefs (NO_DBX_XREFS in gcc) if a file has a "struct foo *bar"
443 and no complete type for struct foo in that file. */
444 fprintf_filtered (stream, "<incomplete type>");
445 break;
446
447 default:
448 error ("Invalid C/C++ type code %d in symbol table.", TYPE_CODE (type));
449 }
199b2450 450 gdb_flush (stream);
a8a69e63
FF
451 return (0);
452}
e10cfcaa
PB
453\f
454int
455c_value_print (val, stream, format, pretty)
456 value_ptr val;
457 GDB_FILE *stream;
458 int format;
459 enum val_prettyprint pretty;
460{
461 /* A "repeated" value really contains several values in a row.
462 They are made by the @ operator.
463 Print such values as if they were arrays. */
464
465 if (VALUE_REPEATED (val))
466 {
467 register unsigned int n = VALUE_REPETITIONS (val);
468 register unsigned int typelen = TYPE_LENGTH (VALUE_TYPE (val));
469 fprintf_filtered (stream, "{");
470 /* Print arrays of characters using string syntax. */
471 if (typelen == 1 && TYPE_CODE (VALUE_TYPE (val)) == TYPE_CODE_INT
472 && format == 0)
473 LA_PRINT_STRING (stream, VALUE_CONTENTS (val), n, 0);
474 else
475 {
476 value_print_array_elements (val, stream, format, pretty);
477 }
478 fprintf_filtered (stream, "}");
479 return (n * typelen);
480 }
481 else
482 {
483 struct type *type = VALUE_TYPE (val);
484
485 /* If it is a pointer, indicate what it points to.
486
487 Print type also if it is a reference.
488
489 C++: if it is a member pointer, we will take care
490 of that when we print it. */
491 if (TYPE_CODE (type) == TYPE_CODE_PTR ||
492 TYPE_CODE (type) == TYPE_CODE_REF)
493 {
494 /* Hack: remove (char *) for char strings. Their
495 type is indicated by the quoted string anyway. */
496 if (TYPE_CODE (type) == TYPE_CODE_PTR &&
c50e36c2
PS
497 TYPE_NAME (type) == NULL &&
498 TYPE_NAME (TYPE_TARGET_TYPE (type)) != NULL &&
499 STREQ (TYPE_NAME (TYPE_TARGET_TYPE (type)), "char"))
e10cfcaa
PB
500 {
501 /* Print nothing */
502 }
503 else
504 {
505 fprintf_filtered (stream, "(");
506 type_print (type, "", stream, -1);
507 fprintf_filtered (stream, ") ");
508 }
509 }
510 return (val_print (type, VALUE_CONTENTS (val),
511 VALUE_ADDRESS (val), stream, format, 1, 0, pretty));
512 }
513}
This page took 0.139884 seconds and 4 git commands to generate.