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