* c-exp.y (exp:STRING): Convert C strings into array-of-char
[deliverable/binutils-gdb.git] / gdb / c-valprint.c
CommitLineData
a8a69e63
FF
1/* Support for printing C values for GDB, the GNU debugger.
2 Copyright 1986, 1988, 1989, 1991 Free Software Foundation, Inc.
3
4This file is part of GDB.
5
6This program is free software; you can redistribute it and/or modify
7it under the terms of the GNU General Public License as published by
8the Free Software Foundation; either version 2 of the License, or
9(at your option) any later version.
10
11This program is distributed in the hope that it will be useful,
12but WITHOUT ANY WARRANTY; without even the implied warranty of
13MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14GNU General Public License for more details.
15
16You should have received a copy of the GNU General Public License
17along with this program; if not, write to the Free Software
18Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
19
20#include "defs.h"
21#include "symtab.h"
22#include "gdbtypes.h"
23#include "expression.h"
24#include "value.h"
25#include "demangle.h"
26#include "valprint.h"
27#include "language.h"
28
29/* BEGIN-FIXME */
30
31extern int vtblprint; /* Controls printing of vtbl's */
32extern int demangle; /* whether to print C++ syms raw or src-form */
33
34extern void
35cp_print_class_member PARAMS ((char *, struct type *, FILE *, char *));
36
c7da3ed3
FF
37extern void
38cp_print_class_method PARAMS ((char *, struct type *, FILE *));
a8a69e63
FF
39
40extern void
41cp_print_value_fields PARAMS ((struct type *, char *, FILE *, int, int,
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
56c_type_print_varspec_prefix PARAMS ((struct type *, FILE *, int, int));
57
58extern void
59cp_type_print_method_args PARAMS ((struct type **, char *, char *, int,
60 FILE *));
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;
86 FILE *stream;
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;
97 unsigned char c;
c4413e2c 98 CORE_ADDR addr;
a8a69e63
FF
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 }
a8a69e63
FF
112 /* For an array of chars, print with string syntax. */
113 if (eltlen == 1 && TYPE_CODE (elttype) == TYPE_CODE_INT
c4413e2c 114 && (format == 0 || format == 's'))
a8a69e63 115 {
c4413e2c
FF
116 if (addressprint && format != 's')
117 {
118 fprintf_filtered (stream, "0x%x ", address);
119 }
a8a69e63
FF
120 LA_PRINT_STRING (stream, valaddr, len, 0);
121 }
122 else
123 {
c4413e2c 124 fprintf_filtered (stream, "{");
a8a69e63
FF
125 /* If this is a virtual function table, print the 0th
126 entry specially, and the rest of the members normally. */
127 if (cp_is_vtbl_ptr_type (elttype))
128 {
129 i = 1;
130 fprintf_filtered (stream, "%d vtable entries", len - 1);
131 }
132 else
133 {
134 i = 0;
135 }
136 val_print_array_elements (type, valaddr, address, stream,
137 format, deref_ref, recurse, pretty, i);
c4413e2c 138 fprintf_filtered (stream, "}");
a8a69e63 139 }
a8a69e63
FF
140 break;
141 }
142 /* Array of unspecified length: treat like pointer to first elt. */
143 valaddr = (char *) &address;
c4413e2c 144 /* FALL THROUGH */
a8a69e63
FF
145
146 case TYPE_CODE_PTR:
147 if (format && format != 's')
148 {
149 print_scalar_formatted (valaddr, type, format, 0, stream);
150 break;
151 }
152 if (TYPE_CODE (TYPE_TARGET_TYPE (type)) == TYPE_CODE_METHOD)
153 {
c7da3ed3 154 cp_print_class_method (valaddr, type, stream);
a8a69e63
FF
155 }
156 else if (TYPE_CODE (TYPE_TARGET_TYPE (type)) == TYPE_CODE_MEMBER)
157 {
158 cp_print_class_member (valaddr,
159 TYPE_DOMAIN_TYPE (TYPE_TARGET_TYPE (type)),
160 stream, "&");
161 }
162 else
163 {
c4413e2c 164 addr = unpack_pointer (type, valaddr);
a8a69e63
FF
165 elttype = TYPE_TARGET_TYPE (type);
166
167 if (TYPE_CODE (elttype) == TYPE_CODE_FUNC)
168 {
169 /* Try to print what function it points to. */
170 print_address_demangle (addr, stream, demangle);
171 /* Return value is irrelevant except for string pointers. */
172 return (0);
173 }
174
175 if (addressprint && format != 's')
176 {
177 fprintf_filtered (stream, "0x%x", addr);
178 }
179
180 /* For a pointer to char or unsigned char, also print the string
181 pointed to, unless pointer is null. */
c4413e2c
FF
182 if (TYPE_LENGTH (elttype) == 1
183 && TYPE_CODE (elttype) == TYPE_CODE_INT
184 && (format == 0 || format == 's')
185 && addr != 0)
a8a69e63 186 {
c4413e2c 187 i = val_print_string (addr, 0, stream);
a8a69e63
FF
188 }
189 else if (cp_is_vtbl_member(type))
190 {
191 /* print vtbl's nicely */
192 CORE_ADDR vt_address = unpack_pointer (type, valaddr);
193
194 struct minimal_symbol *msymbol =
195 lookup_minimal_symbol_by_pc (vt_address);
2e4964ad
FF
196 if ((msymbol != NULL) &&
197 (vt_address == SYMBOL_VALUE_ADDRESS (msymbol)))
a8a69e63
FF
198 {
199 fputs_filtered (" <", stream);
2e4964ad 200 fputs_filtered (SYMBOL_SOURCE_NAME (msymbol), stream);
a8a69e63
FF
201 fputs_filtered (">", stream);
202 }
203 if (vtblprint)
204 {
205 value vt_val;
206
207 vt_val = value_at (TYPE_TARGET_TYPE (type), vt_address);
208 val_print (VALUE_TYPE (vt_val), VALUE_CONTENTS (vt_val),
209 VALUE_ADDRESS (vt_val), stream, format,
210 deref_ref, recurse + 1, pretty);
211 if (pretty)
212 {
213 fprintf_filtered (stream, "\n");
214 print_spaces_filtered (2 + 2 * recurse, stream);
215 }
216 }
217 }
218
219 /* Return number of characters printed, plus one for the
220 terminating null if we have "reached the end". */
221 return (i + (print_max && i != print_max));
222 }
223 break;
224
225 case TYPE_CODE_MEMBER:
226 error ("not implemented: member type in c_val_print");
227 break;
228
229 case TYPE_CODE_REF:
230 if (TYPE_CODE (TYPE_TARGET_TYPE (type)) == TYPE_CODE_MEMBER)
231 {
232 cp_print_class_member (valaddr,
233 TYPE_DOMAIN_TYPE (TYPE_TARGET_TYPE (type)),
234 stream, "");
235 break;
236 }
237 if (addressprint)
238 {
239 fprintf_filtered (stream, "@0x%lx",
240 unpack_long (builtin_type_int, valaddr));
241 if (deref_ref)
242 fputs_filtered (": ", stream);
243 }
244 /* De-reference the reference. */
245 if (deref_ref)
246 {
247 if (TYPE_CODE (TYPE_TARGET_TYPE (type)) != TYPE_CODE_UNDEF)
248 {
249 value deref_val =
250 value_at
251 (TYPE_TARGET_TYPE (type),
252 unpack_pointer (lookup_pointer_type (builtin_type_void),
253 valaddr));
254 val_print (VALUE_TYPE (deref_val),
255 VALUE_CONTENTS (deref_val),
256 VALUE_ADDRESS (deref_val), stream, format,
257 deref_ref, recurse + 1, pretty);
258 }
259 else
260 fputs_filtered ("???", stream);
261 }
262 break;
263
264 case TYPE_CODE_UNION:
265 if (recurse && !unionprint)
266 {
267 fprintf_filtered (stream, "{...}");
268 break;
269 }
270 /* Fall through. */
271 case TYPE_CODE_STRUCT:
272 if (vtblprint && cp_is_vtbl_ptr_type(type))
273 {
274 /* Print the unmangled name if desired. */
275 print_address_demangle(*((int *) (valaddr + /* FIXME bytesex */
276 TYPE_FIELD_BITPOS (type, VTBL_FNADDR_OFFSET) / 8)),
277 stream, demangle);
278 break;
279 }
280 cp_print_value_fields (type, valaddr, stream, format, recurse, pretty,
281 0);
282 break;
283
284 case TYPE_CODE_ENUM:
285 if (format)
286 {
287 print_scalar_formatted (valaddr, type, format, 0, stream);
288 break;
289 }
290 len = TYPE_NFIELDS (type);
291 val = unpack_long (builtin_type_int, valaddr);
292 for (i = 0; i < len; i++)
293 {
294 QUIT;
295 if (val == TYPE_FIELD_BITPOS (type, i))
296 {
297 break;
298 }
299 }
300 if (i < len)
301 {
302 fputs_filtered (TYPE_FIELD_NAME (type, i), stream);
303 }
304 else
305 {
306#ifdef LONG_LONG
307 fprintf_filtered (stream, "%lld", val);
308#else
309 fprintf_filtered (stream, "%ld", val);
310#endif
311 }
312 break;
313
314 case TYPE_CODE_FUNC:
315 if (format)
316 {
317 print_scalar_formatted (valaddr, type, format, 0, stream);
318 break;
319 }
320 /* FIXME, we should consider, at least for ANSI C language, eliminating
321 the distinction made between FUNCs and POINTERs to FUNCs. */
322 fprintf_filtered (stream, "{");
323 type_print (type, "", stream, -1);
324 fprintf_filtered (stream, "} ");
325 /* Try to print what function it points to, and its address. */
326 print_address_demangle (address, stream, demangle);
327 break;
328
329 case TYPE_CODE_INT:
330 format = format ? format : output_format;
331 if (format)
332 {
333 print_scalar_formatted (valaddr, type, format, 0, stream);
334 }
335 else
336 {
337 val_print_type_code_int (type, valaddr, stream);
338 /* C and C++ has no single byte int type, char is used instead.
339 Since we don't know whether the value is really intended to
340 be used as an integer or a character, print the character
341 equivalent as well. */
342 if (TYPE_LENGTH (type) == 1)
343 {
344 fputs_filtered (" ", stream);
345 LA_PRINT_CHAR ((unsigned char) unpack_long (type, valaddr),
346 stream);
347 }
348 }
349 break;
350
351 case TYPE_CODE_CHAR:
352 format = format ? format : output_format;
353 if (format)
354 {
355 print_scalar_formatted (valaddr, type, format, 0, stream);
356 }
357 else
358 {
359 fprintf_filtered (stream, TYPE_UNSIGNED (type) ? "%u" : "%d",
360 unpack_long (type, valaddr));
361 fputs_filtered (" ", stream);
362 LA_PRINT_CHAR ((unsigned char) unpack_long (type, valaddr), stream);
363 }
364 break;
365
366 case TYPE_CODE_FLT:
367 if (format)
368 {
369 print_scalar_formatted (valaddr, type, format, 0, stream);
370 }
371 else
372 {
373 print_floating (valaddr, type, stream);
374 }
375 break;
376
377 case TYPE_CODE_VOID:
378 fprintf_filtered (stream, "void");
379 break;
380
381 case TYPE_CODE_ERROR:
382 fprintf_filtered (stream, "<error type>");
383 break;
384
385 case TYPE_CODE_RANGE:
386 /* FIXME, we should not ever have to print one of these yet. */
387 fprintf_filtered (stream, "<range type>");
388 break;
389
390 case TYPE_CODE_UNDEF:
391 /* This happens (without TYPE_FLAG_STUB set) on systems which don't use
392 dbx xrefs (NO_DBX_XREFS in gcc) if a file has a "struct foo *bar"
393 and no complete type for struct foo in that file. */
394 fprintf_filtered (stream, "<incomplete type>");
395 break;
396
397 default:
398 error ("Invalid C/C++ type code %d in symbol table.", TYPE_CODE (type));
399 }
400 fflush (stream);
401 return (0);
402}
This page took 0.03986 seconds and 4 git commands to generate.