Commit | Line | Data |
---|---|---|
c906108c | 1 | /* Support for printing C values for GDB, the GNU debugger. |
1bac305b | 2 | |
6aba47ca | 3 | Copyright (C) 1986, 1988, 1989, 1991, 1992, 1993, 1994, 1995, 1996, 1997, |
0fb0cc75 | 4 | 1998, 1999, 2000, 2001, 2003, 2005, 2006, 2007, 2008, 2009 |
4f2aea11 | 5 | Free Software Foundation, Inc. |
c906108c | 6 | |
c5aa993b | 7 | This file is part of GDB. |
c906108c | 8 | |
c5aa993b JM |
9 | This program is free software; you can redistribute it and/or modify |
10 | it under the terms of the GNU General Public License as published by | |
a9762ec7 | 11 | the Free Software Foundation; either version 3 of the License, or |
c5aa993b | 12 | (at your option) any later version. |
c906108c | 13 | |
c5aa993b JM |
14 | This program is distributed in the hope that it will be useful, |
15 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
17 | GNU General Public License for more details. | |
c906108c | 18 | |
c5aa993b | 19 | You should have received a copy of the GNU General Public License |
a9762ec7 | 20 | along with this program. If not, see <http://www.gnu.org/licenses/>. */ |
c906108c SS |
21 | |
22 | #include "defs.h" | |
309367d4 | 23 | #include "gdb_string.h" |
c906108c SS |
24 | #include "symtab.h" |
25 | #include "gdbtypes.h" | |
26 | #include "expression.h" | |
27 | #include "value.h" | |
c906108c SS |
28 | #include "valprint.h" |
29 | #include "language.h" | |
30 | #include "c-lang.h" | |
015a42b4 | 31 | #include "cp-abi.h" |
e2d0e7eb | 32 | #include "target.h" |
c906108c | 33 | \f |
c5aa993b | 34 | |
6e778545 PS |
35 | /* Print function pointer with inferior address ADDRESS onto stdio |
36 | stream STREAM. */ | |
37 | ||
38 | static void | |
79a45b7d TT |
39 | print_function_pointer_address (CORE_ADDR address, struct ui_file *stream, |
40 | int addressprint) | |
6e778545 | 41 | { |
e2d0e7eb AC |
42 | CORE_ADDR func_addr = gdbarch_convert_from_func_ptr_addr (current_gdbarch, |
43 | address, | |
44 | ¤t_target); | |
6e778545 PS |
45 | |
46 | /* If the function pointer is represented by a description, print the | |
47 | address of the description. */ | |
48 | if (addressprint && func_addr != address) | |
49 | { | |
50 | fputs_filtered ("@", stream); | |
ed49a04f | 51 | fputs_filtered (paddress (address), stream); |
6e778545 PS |
52 | fputs_filtered (": ", stream); |
53 | } | |
54 | print_address_demangle (func_addr, stream, demangle); | |
55 | } | |
56 | ||
57 | ||
6c7a06a3 TT |
58 | /* A helper for textual_element_type. This checks the name of the |
59 | typedef. This is bogus but it isn't apparent that the compiler | |
60 | provides us the help we may need. */ | |
61 | ||
62 | static int | |
63 | textual_name (const char *name) | |
64 | { | |
65 | return (!strcmp (name, "wchar_t") | |
66 | || !strcmp (name, "char16_t") | |
67 | || !strcmp (name, "char32_t")); | |
68 | } | |
69 | ||
ea37ba09 DJ |
70 | /* Apply a heuristic to decide whether an array of TYPE or a pointer |
71 | to TYPE should be printed as a textual string. Return non-zero if | |
72 | it should, or zero if it should be treated as an array of integers | |
73 | or pointer to integers. FORMAT is the current format letter, | |
74 | or 0 if none. | |
75 | ||
76 | We guess that "char" is a character. Explicitly signed and | |
77 | unsigned character types are also characters. Integer data from | |
78 | vector types is not. The user can override this by using the /s | |
79 | format letter. */ | |
80 | ||
81 | static int | |
82 | textual_element_type (struct type *type, char format) | |
83 | { | |
84 | struct type *true_type = check_typedef (type); | |
85 | ||
86 | if (format != 0 && format != 's') | |
87 | return 0; | |
88 | ||
89 | /* TYPE_CODE_CHAR is always textual. */ | |
90 | if (TYPE_CODE (true_type) == TYPE_CODE_CHAR) | |
91 | return 1; | |
6c7a06a3 TT |
92 | /* Any other character-like types must be integral. */ |
93 | if (TYPE_CODE (true_type) != TYPE_CODE_INT) | |
94 | return 0; | |
95 | ||
96 | /* Check the names of the type and the typedef. */ | |
97 | if (TYPE_NAME (type) && textual_name (TYPE_NAME (type))) | |
98 | return 1; | |
99 | if (TYPE_NAME (true_type) && textual_name (TYPE_NAME (true_type))) | |
100 | return 1; | |
ea37ba09 DJ |
101 | |
102 | if (format == 's') | |
103 | { | |
104 | /* Print this as a string if we can manage it. For now, no | |
105 | wide character support. */ | |
106 | if (TYPE_CODE (true_type) == TYPE_CODE_INT | |
107 | && TYPE_LENGTH (true_type) == 1) | |
108 | return 1; | |
109 | } | |
110 | else | |
111 | { | |
112 | /* If a one-byte TYPE_CODE_INT is missing the not-a-character | |
113 | flag, then we treat it as text; otherwise, we assume it's | |
114 | being used as data. */ | |
115 | if (TYPE_CODE (true_type) == TYPE_CODE_INT | |
116 | && TYPE_LENGTH (true_type) == 1 | |
117 | && !TYPE_NOTTEXT (true_type)) | |
118 | return 1; | |
119 | } | |
120 | ||
121 | return 0; | |
122 | } | |
123 | ||
124 | ||
c906108c SS |
125 | /* Print data of type TYPE located at VALADDR (within GDB), which came from |
126 | the inferior at address ADDRESS, onto stdio stream STREAM according to | |
79a45b7d | 127 | OPTIONS. The data at VALADDR is in target byte order. |
c906108c SS |
128 | |
129 | If the data are a string pointer, returns the number of string characters | |
79a45b7d | 130 | printed. */ |
c906108c SS |
131 | |
132 | int | |
fc1a4b47 | 133 | c_val_print (struct type *type, const gdb_byte *valaddr, int embedded_offset, |
79a45b7d TT |
134 | CORE_ADDR address, struct ui_file *stream, int recurse, |
135 | const struct value_print_options *options) | |
c906108c | 136 | { |
52f0bd74 | 137 | unsigned int i = 0; /* Number of characters printed */ |
c906108c | 138 | unsigned len; |
6c7a06a3 TT |
139 | struct type *elttype, *unresolved_elttype; |
140 | struct type *unresolved_type = type; | |
c906108c SS |
141 | unsigned eltlen; |
142 | LONGEST val; | |
143 | CORE_ADDR addr; | |
144 | ||
145 | CHECK_TYPEDEF (type); | |
146 | switch (TYPE_CODE (type)) | |
147 | { | |
148 | case TYPE_CODE_ARRAY: | |
6c7a06a3 TT |
149 | unresolved_elttype = TYPE_TARGET_TYPE (type); |
150 | elttype = check_typedef (unresolved_elttype); | |
151 | if (TYPE_LENGTH (type) > 0 && TYPE_LENGTH (unresolved_elttype) > 0) | |
c906108c SS |
152 | { |
153 | eltlen = TYPE_LENGTH (elttype); | |
154 | len = TYPE_LENGTH (type) / eltlen; | |
79a45b7d | 155 | if (options->prettyprint_arrays) |
c906108c SS |
156 | { |
157 | print_spaces_filtered (2 + 2 * recurse, stream); | |
158 | } | |
ea37ba09 DJ |
159 | |
160 | /* Print arrays of textual chars with a string syntax. */ | |
6c7a06a3 | 161 | if (textual_element_type (unresolved_elttype, options->format)) |
c906108c SS |
162 | { |
163 | /* If requested, look for the first null char and only print | |
c5aa993b | 164 | elements up to it. */ |
79a45b7d | 165 | if (options->stop_print_at_null) |
c906108c | 166 | { |
745b8ca0 | 167 | unsigned int temp_len; |
c5aa993b | 168 | |
c906108c | 169 | for (temp_len = 0; |
6c7a06a3 TT |
170 | (temp_len < len |
171 | && temp_len < options->print_max | |
172 | && extract_unsigned_integer (valaddr + embedded_offset | |
173 | + temp_len * eltlen, | |
174 | eltlen) == 0); | |
175 | ++temp_len) | |
176 | ; | |
c906108c SS |
177 | len = temp_len; |
178 | } | |
c5aa993b | 179 | |
6c7a06a3 TT |
180 | LA_PRINT_STRING (stream, unresolved_elttype, |
181 | valaddr + embedded_offset, len, 0, options); | |
c906108c SS |
182 | i = len; |
183 | } | |
184 | else | |
185 | { | |
186 | fprintf_filtered (stream, "{"); | |
187 | /* If this is a virtual function table, print the 0th | |
c5aa993b | 188 | entry specially, and the rest of the members normally. */ |
c906108c SS |
189 | if (cp_is_vtbl_ptr_type (elttype)) |
190 | { | |
191 | i = 1; | |
3d263c1d | 192 | fprintf_filtered (stream, _("%d vtable entries"), len - 1); |
c906108c SS |
193 | } |
194 | else | |
195 | { | |
196 | i = 0; | |
197 | } | |
198 | val_print_array_elements (type, valaddr + embedded_offset, address, stream, | |
79a45b7d | 199 | recurse, options, i); |
c906108c SS |
200 | fprintf_filtered (stream, "}"); |
201 | } | |
202 | break; | |
203 | } | |
204 | /* Array of unspecified length: treat like pointer to first elt. */ | |
205 | addr = address; | |
206 | goto print_unpacked_pointer; | |
207 | ||
0d5de010 | 208 | case TYPE_CODE_MEMBERPTR: |
79a45b7d | 209 | if (options->format) |
0d5de010 | 210 | { |
79a45b7d TT |
211 | print_scalar_formatted (valaddr + embedded_offset, type, |
212 | options, 0, stream); | |
0d5de010 DJ |
213 | break; |
214 | } | |
ad4820ab | 215 | cp_print_class_member (valaddr + embedded_offset, type, stream, "&"); |
0d5de010 DJ |
216 | break; |
217 | ||
218 | case TYPE_CODE_METHODPTR: | |
219 | cplus_print_method_ptr (valaddr + embedded_offset, type, stream); | |
220 | break; | |
221 | ||
c906108c | 222 | case TYPE_CODE_PTR: |
79a45b7d | 223 | if (options->format && options->format != 's') |
c906108c | 224 | { |
79a45b7d TT |
225 | print_scalar_formatted (valaddr + embedded_offset, type, |
226 | options, 0, stream); | |
c906108c SS |
227 | break; |
228 | } | |
79a45b7d | 229 | if (options->vtblprint && cp_is_vtbl_ptr_type (type)) |
c906108c | 230 | { |
c5aa993b | 231 | /* Print the unmangled name if desired. */ |
c906108c SS |
232 | /* Print vtable entry - we only get here if we ARE using |
233 | -fvtable_thunks. (Otherwise, look under TYPE_CODE_STRUCT.) */ | |
4478b372 JB |
234 | CORE_ADDR addr |
235 | = extract_typed_address (valaddr + embedded_offset, type); | |
79a45b7d | 236 | print_function_pointer_address (addr, stream, options->addressprint); |
c906108c SS |
237 | break; |
238 | } | |
6c7a06a3 TT |
239 | unresolved_elttype = TYPE_TARGET_TYPE (type); |
240 | elttype = check_typedef (unresolved_elttype); | |
c906108c SS |
241 | { |
242 | addr = unpack_pointer (type, valaddr + embedded_offset); | |
243 | print_unpacked_pointer: | |
c906108c SS |
244 | |
245 | if (TYPE_CODE (elttype) == TYPE_CODE_FUNC) | |
246 | { | |
247 | /* Try to print what function it points to. */ | |
79a45b7d TT |
248 | print_function_pointer_address (addr, stream, |
249 | options->addressprint); | |
c906108c SS |
250 | /* Return value is irrelevant except for string pointers. */ |
251 | return (0); | |
252 | } | |
253 | ||
79a45b7d | 254 | if (options->addressprint) |
ed49a04f | 255 | fputs_filtered (paddress (addr), stream); |
c906108c | 256 | |
ea37ba09 | 257 | /* For a pointer to a textual type, also print the string |
c906108c | 258 | pointed to, unless pointer is null. */ |
c906108c | 259 | |
6c7a06a3 | 260 | if (textual_element_type (unresolved_elttype, options->format) |
79a45b7d | 261 | && addr != 0) |
c906108c | 262 | { |
6c7a06a3 | 263 | i = val_print_string (unresolved_elttype, addr, -1, stream, |
79a45b7d | 264 | options); |
c906108c | 265 | } |
c5aa993b JM |
266 | else if (cp_is_vtbl_member (type)) |
267 | { | |
c906108c SS |
268 | /* print vtbl's nicely */ |
269 | CORE_ADDR vt_address = unpack_pointer (type, valaddr + embedded_offset); | |
270 | ||
271 | struct minimal_symbol *msymbol = | |
c5aa993b | 272 | lookup_minimal_symbol_by_pc (vt_address); |
c906108c SS |
273 | if ((msymbol != NULL) && |
274 | (vt_address == SYMBOL_VALUE_ADDRESS (msymbol))) | |
275 | { | |
276 | fputs_filtered (" <", stream); | |
de5ad195 | 277 | fputs_filtered (SYMBOL_PRINT_NAME (msymbol), stream); |
c906108c SS |
278 | fputs_filtered (">", stream); |
279 | } | |
79a45b7d | 280 | if (vt_address && options->vtblprint) |
c5aa993b | 281 | { |
6943961c | 282 | struct value *vt_val; |
c5aa993b JM |
283 | struct symbol *wsym = (struct symbol *) NULL; |
284 | struct type *wtype; | |
c5aa993b | 285 | struct block *block = (struct block *) NULL; |
c906108c SS |
286 | int is_this_fld; |
287 | ||
288 | if (msymbol != NULL) | |
3567439c | 289 | wsym = lookup_symbol (SYMBOL_LINKAGE_NAME (msymbol), block, |
2570f2b7 | 290 | VAR_DOMAIN, &is_this_fld); |
c5aa993b | 291 | |
c906108c SS |
292 | if (wsym) |
293 | { | |
c5aa993b | 294 | wtype = SYMBOL_TYPE (wsym); |
c906108c SS |
295 | } |
296 | else | |
297 | { | |
6c7a06a3 | 298 | wtype = unresolved_elttype; |
c906108c | 299 | } |
00a4c844 | 300 | vt_val = value_at (wtype, vt_address); |
79a45b7d | 301 | common_val_print (vt_val, stream, recurse + 1, options, |
d8ca156b | 302 | current_language); |
79a45b7d | 303 | if (options->pretty) |
c906108c SS |
304 | { |
305 | fprintf_filtered (stream, "\n"); | |
306 | print_spaces_filtered (2 + 2 * recurse, stream); | |
307 | } | |
c5aa993b JM |
308 | } |
309 | } | |
c906108c SS |
310 | |
311 | /* Return number of characters printed, including the terminating | |
312 | '\0' if we reached the end. val_print_string takes care including | |
313 | the terminating '\0' if necessary. */ | |
314 | return i; | |
315 | } | |
316 | break; | |
317 | ||
c906108c SS |
318 | case TYPE_CODE_REF: |
319 | elttype = check_typedef (TYPE_TARGET_TYPE (type)); | |
79a45b7d | 320 | if (options->addressprint) |
c5aa993b | 321 | { |
4478b372 JB |
322 | CORE_ADDR addr |
323 | = extract_typed_address (valaddr + embedded_offset, type); | |
c906108c | 324 | fprintf_filtered (stream, "@"); |
ed49a04f | 325 | fputs_filtered (paddress (addr), stream); |
79a45b7d | 326 | if (options->deref_ref) |
c906108c | 327 | fputs_filtered (": ", stream); |
c5aa993b | 328 | } |
c906108c | 329 | /* De-reference the reference. */ |
79a45b7d | 330 | if (options->deref_ref) |
c906108c SS |
331 | { |
332 | if (TYPE_CODE (elttype) != TYPE_CODE_UNDEF) | |
333 | { | |
6943961c | 334 | struct value *deref_val = |
c5aa993b JM |
335 | value_at |
336 | (TYPE_TARGET_TYPE (type), | |
d8631d21 | 337 | unpack_pointer (type, valaddr + embedded_offset)); |
79a45b7d TT |
338 | common_val_print (deref_val, stream, recurse, options, |
339 | current_language); | |
c906108c SS |
340 | } |
341 | else | |
342 | fputs_filtered ("???", stream); | |
343 | } | |
344 | break; | |
345 | ||
346 | case TYPE_CODE_UNION: | |
79a45b7d | 347 | if (recurse && !options->unionprint) |
c906108c SS |
348 | { |
349 | fprintf_filtered (stream, "{...}"); | |
350 | break; | |
351 | } | |
352 | /* Fall through. */ | |
353 | case TYPE_CODE_STRUCT: | |
015a42b4 | 354 | /*FIXME: Abstract this away */ |
79a45b7d | 355 | if (options->vtblprint && cp_is_vtbl_ptr_type (type)) |
c906108c | 356 | { |
c5aa993b | 357 | /* Print the unmangled name if desired. */ |
c906108c SS |
358 | /* Print vtable entry - we only get here if NOT using |
359 | -fvtable_thunks. (Otherwise, look under TYPE_CODE_PTR.) */ | |
4478b372 JB |
360 | int offset = (embedded_offset + |
361 | TYPE_FIELD_BITPOS (type, VTBL_FNADDR_OFFSET) / 8); | |
362 | struct type *field_type = TYPE_FIELD_TYPE (type, VTBL_FNADDR_OFFSET); | |
363 | CORE_ADDR addr | |
364 | = extract_typed_address (valaddr + offset, field_type); | |
365 | ||
79a45b7d | 366 | print_function_pointer_address (addr, stream, options->addressprint); |
c906108c SS |
367 | } |
368 | else | |
79a45b7d TT |
369 | cp_print_value_fields (type, type, valaddr, embedded_offset, address, stream, |
370 | recurse, options, NULL, 0); | |
c906108c SS |
371 | break; |
372 | ||
373 | case TYPE_CODE_ENUM: | |
79a45b7d | 374 | if (options->format) |
c906108c | 375 | { |
79a45b7d TT |
376 | print_scalar_formatted (valaddr + embedded_offset, type, |
377 | options, 0, stream); | |
c906108c SS |
378 | break; |
379 | } | |
380 | len = TYPE_NFIELDS (type); | |
381 | val = unpack_long (type, valaddr + embedded_offset); | |
382 | for (i = 0; i < len; i++) | |
383 | { | |
384 | QUIT; | |
385 | if (val == TYPE_FIELD_BITPOS (type, i)) | |
386 | { | |
387 | break; | |
388 | } | |
389 | } | |
390 | if (i < len) | |
391 | { | |
392 | fputs_filtered (TYPE_FIELD_NAME (type, i), stream); | |
393 | } | |
394 | else | |
395 | { | |
396 | print_longest (stream, 'd', 0, val); | |
397 | } | |
398 | break; | |
399 | ||
4f2aea11 | 400 | case TYPE_CODE_FLAGS: |
79a45b7d TT |
401 | if (options->format) |
402 | print_scalar_formatted (valaddr + embedded_offset, type, | |
403 | options, 0, stream); | |
4f2aea11 MK |
404 | else |
405 | val_print_type_code_flags (type, valaddr + embedded_offset, stream); | |
406 | break; | |
407 | ||
c906108c | 408 | case TYPE_CODE_FUNC: |
0d5de010 | 409 | case TYPE_CODE_METHOD: |
79a45b7d | 410 | if (options->format) |
c906108c | 411 | { |
79a45b7d TT |
412 | print_scalar_formatted (valaddr + embedded_offset, type, |
413 | options, 0, stream); | |
c906108c SS |
414 | break; |
415 | } | |
416 | /* FIXME, we should consider, at least for ANSI C language, eliminating | |
c5aa993b | 417 | the distinction made between FUNCs and POINTERs to FUNCs. */ |
c906108c SS |
418 | fprintf_filtered (stream, "{"); |
419 | type_print (type, "", stream, -1); | |
420 | fprintf_filtered (stream, "} "); | |
421 | /* Try to print what function it points to, and its address. */ | |
422 | print_address_demangle (address, stream, demangle); | |
423 | break; | |
424 | ||
425 | case TYPE_CODE_BOOL: | |
79a45b7d TT |
426 | if (options->format || options->output_format) |
427 | { | |
428 | struct value_print_options opts = *options; | |
429 | opts.format = (options->format ? options->format | |
430 | : options->output_format); | |
431 | print_scalar_formatted (valaddr + embedded_offset, type, | |
432 | &opts, 0, stream); | |
433 | } | |
c906108c SS |
434 | else |
435 | { | |
436 | val = unpack_long (type, valaddr + embedded_offset); | |
437 | if (val == 0) | |
438 | fputs_filtered ("false", stream); | |
439 | else if (val == 1) | |
440 | fputs_filtered ("true", stream); | |
441 | else | |
442 | print_longest (stream, 'd', 0, val); | |
443 | } | |
444 | break; | |
445 | ||
446 | case TYPE_CODE_RANGE: | |
447 | /* FIXME: create_range_type does not set the unsigned bit in a | |
c5aa993b JM |
448 | range type (I think it probably should copy it from the target |
449 | type), so we won't print values which are too large to | |
450 | fit in a signed integer correctly. */ | |
c906108c | 451 | /* FIXME: Doesn't handle ranges of enums correctly. (Can't just |
c5aa993b JM |
452 | print with the target type, though, because the size of our type |
453 | and the target type might differ). */ | |
c906108c SS |
454 | /* FALLTHROUGH */ |
455 | ||
456 | case TYPE_CODE_INT: | |
79a45b7d | 457 | if (options->format || options->output_format) |
c906108c | 458 | { |
79a45b7d TT |
459 | struct value_print_options opts = *options; |
460 | opts.format = (options->format ? options->format | |
461 | : options->output_format); | |
462 | print_scalar_formatted (valaddr + embedded_offset, type, | |
463 | &opts, 0, stream); | |
c906108c SS |
464 | } |
465 | else | |
466 | { | |
467 | val_print_type_code_int (type, valaddr + embedded_offset, stream); | |
468 | /* C and C++ has no single byte int type, char is used instead. | |
469 | Since we don't know whether the value is really intended to | |
470 | be used as an integer or a character, print the character | |
ea37ba09 | 471 | equivalent as well. */ |
6c7a06a3 | 472 | if (textual_element_type (unresolved_type, options->format)) |
c906108c SS |
473 | { |
474 | fputs_filtered (" ", stream); | |
475 | LA_PRINT_CHAR ((unsigned char) unpack_long (type, valaddr + embedded_offset), | |
6c7a06a3 | 476 | unresolved_type, stream); |
c906108c SS |
477 | } |
478 | } | |
479 | break; | |
480 | ||
481 | case TYPE_CODE_CHAR: | |
79a45b7d | 482 | if (options->format || options->output_format) |
c906108c | 483 | { |
79a45b7d TT |
484 | struct value_print_options opts = *options; |
485 | opts.format = (options->format ? options->format | |
486 | : options->output_format); | |
487 | print_scalar_formatted (valaddr + embedded_offset, type, | |
488 | &opts, 0, stream); | |
c906108c SS |
489 | } |
490 | else | |
491 | { | |
96baa820 JM |
492 | val = unpack_long (type, valaddr + embedded_offset); |
493 | if (TYPE_UNSIGNED (type)) | |
494 | fprintf_filtered (stream, "%u", (unsigned int) val); | |
495 | else | |
496 | fprintf_filtered (stream, "%d", (int) val); | |
c906108c | 497 | fputs_filtered (" ", stream); |
6c7a06a3 | 498 | LA_PRINT_CHAR ((unsigned char) val, unresolved_type, stream); |
c906108c SS |
499 | } |
500 | break; | |
501 | ||
502 | case TYPE_CODE_FLT: | |
79a45b7d | 503 | if (options->format) |
c906108c | 504 | { |
79a45b7d TT |
505 | print_scalar_formatted (valaddr + embedded_offset, type, |
506 | options, 0, stream); | |
c906108c SS |
507 | } |
508 | else | |
509 | { | |
510 | print_floating (valaddr + embedded_offset, type, stream); | |
511 | } | |
512 | break; | |
513 | ||
7678ef8f | 514 | case TYPE_CODE_DECFLOAT: |
79a45b7d TT |
515 | if (options->format) |
516 | print_scalar_formatted (valaddr + embedded_offset, type, | |
517 | options, 0, stream); | |
7678ef8f TJB |
518 | else |
519 | print_decimal_floating (valaddr + embedded_offset, type, stream); | |
520 | break; | |
521 | ||
c906108c SS |
522 | case TYPE_CODE_VOID: |
523 | fprintf_filtered (stream, "void"); | |
524 | break; | |
525 | ||
526 | case TYPE_CODE_ERROR: | |
3d263c1d | 527 | fprintf_filtered (stream, _("<error type>")); |
c906108c SS |
528 | break; |
529 | ||
530 | case TYPE_CODE_UNDEF: | |
531 | /* This happens (without TYPE_FLAG_STUB set) on systems which don't use | |
c5aa993b JM |
532 | dbx xrefs (NO_DBX_XREFS in gcc) if a file has a "struct foo *bar" |
533 | and no complete type for struct foo in that file. */ | |
3d263c1d | 534 | fprintf_filtered (stream, _("<incomplete type>")); |
c906108c SS |
535 | break; |
536 | ||
fca9e603 | 537 | case TYPE_CODE_COMPLEX: |
79a45b7d | 538 | if (options->format) |
fca9e603 DJ |
539 | print_scalar_formatted (valaddr + embedded_offset, |
540 | TYPE_TARGET_TYPE (type), | |
79a45b7d | 541 | options, 0, stream); |
fca9e603 DJ |
542 | else |
543 | print_floating (valaddr + embedded_offset, TYPE_TARGET_TYPE (type), | |
544 | stream); | |
545 | fprintf_filtered (stream, " + "); | |
79a45b7d | 546 | if (options->format) |
fca9e603 DJ |
547 | print_scalar_formatted (valaddr + embedded_offset |
548 | + TYPE_LENGTH (TYPE_TARGET_TYPE (type)), | |
549 | TYPE_TARGET_TYPE (type), | |
79a45b7d | 550 | options, 0, stream); |
fca9e603 DJ |
551 | else |
552 | print_floating (valaddr + embedded_offset | |
553 | + TYPE_LENGTH (TYPE_TARGET_TYPE (type)), | |
554 | TYPE_TARGET_TYPE (type), | |
555 | stream); | |
556 | fprintf_filtered (stream, " * I"); | |
557 | break; | |
558 | ||
c906108c | 559 | default: |
3d263c1d | 560 | error (_("Invalid C/C++ type code %d in symbol table."), TYPE_CODE (type)); |
c906108c SS |
561 | } |
562 | gdb_flush (stream); | |
563 | return (0); | |
564 | } | |
565 | \f | |
566 | int | |
79a45b7d TT |
567 | c_value_print (struct value *val, struct ui_file *stream, |
568 | const struct value_print_options *options) | |
c906108c | 569 | { |
6c7a06a3 | 570 | struct type *type, *real_type, *val_type; |
c906108c | 571 | int full, top, using_enc; |
79a45b7d TT |
572 | struct value_print_options opts = *options; |
573 | ||
574 | opts.deref_ref = 1; | |
c5aa993b | 575 | |
c906108c SS |
576 | /* If it is a pointer, indicate what it points to. |
577 | ||
578 | Print type also if it is a reference. | |
579 | ||
580 | C++: if it is a member pointer, we will take care | |
581 | of that when we print it. */ | |
88750304 | 582 | |
6c7a06a3 TT |
583 | /* Preserve the original type before stripping typedefs. We prefer |
584 | to pass down the original type when possible, but for local | |
585 | checks it is better to look past the typedefs. */ | |
586 | val_type = value_type (val); | |
587 | type = check_typedef (val_type); | |
88750304 DJ |
588 | |
589 | if (TYPE_CODE (type) == TYPE_CODE_PTR | |
590 | || TYPE_CODE (type) == TYPE_CODE_REF) | |
c906108c SS |
591 | { |
592 | /* Hack: remove (char *) for char strings. Their | |
ea37ba09 DJ |
593 | type is indicated by the quoted string anyway. |
594 | (Don't use textual_element_type here; quoted strings | |
6c7a06a3 TT |
595 | are always exactly (char *), (wchar_t *), or the like. */ |
596 | if (TYPE_CODE (val_type) == TYPE_CODE_PTR | |
597 | && TYPE_NAME (val_type) == NULL | |
598 | && TYPE_NAME (TYPE_TARGET_TYPE (val_type)) != NULL | |
599 | && (strcmp (TYPE_NAME (TYPE_TARGET_TYPE (val_type)), "char") == 0 | |
600 | || textual_name (TYPE_NAME (TYPE_TARGET_TYPE (val_type))))) | |
c906108c SS |
601 | { |
602 | /* Print nothing */ | |
603 | } | |
79a45b7d TT |
604 | else if (options->objectprint |
605 | && (TYPE_CODE (TYPE_TARGET_TYPE (type)) == TYPE_CODE_CLASS)) | |
c5aa993b | 606 | { |
070ad9f0 DB |
607 | |
608 | if (TYPE_CODE(type) == TYPE_CODE_REF) | |
609 | { | |
610 | /* Copy value, change to pointer, so we don't get an | |
611 | * error about a non-pointer type in value_rtti_target_type | |
612 | */ | |
6943961c | 613 | struct value *temparg; |
070ad9f0 | 614 | temparg=value_copy(val); |
04624583 | 615 | deprecated_set_value_type (temparg, lookup_pointer_type (TYPE_TARGET_TYPE(type))); |
070ad9f0 DB |
616 | val=temparg; |
617 | } | |
c5aa993b | 618 | /* Pointer to class, check real type of object */ |
c906108c | 619 | fprintf_filtered (stream, "("); |
c4093a6a JM |
620 | real_type = value_rtti_target_type (val, &full, &top, &using_enc); |
621 | if (real_type) | |
c5aa993b JM |
622 | { |
623 | /* RTTI entry found */ | |
c4093a6a JM |
624 | if (TYPE_CODE (type) == TYPE_CODE_PTR) |
625 | { | |
626 | /* create a pointer type pointing to the real type */ | |
627 | type = lookup_pointer_type (real_type); | |
628 | } | |
629 | else | |
630 | { | |
631 | /* create a reference type referencing the real type */ | |
632 | type = lookup_reference_type (real_type); | |
633 | } | |
070ad9f0 | 634 | /* JYG: Need to adjust pointer value. */ |
5086187c AC |
635 | /* NOTE: cagney/2005-01-02: THIS IS BOGUS. */ |
636 | value_contents_writeable (val)[0] -= top; | |
070ad9f0 | 637 | |
c4093a6a JM |
638 | /* Note: When we look up RTTI entries, we don't get any |
639 | information on const or volatile attributes */ | |
640 | } | |
641 | type_print (type, "", stream, -1); | |
c906108c | 642 | fprintf_filtered (stream, ") "); |
6c7a06a3 | 643 | val_type = type; |
c5aa993b | 644 | } |
c906108c SS |
645 | else |
646 | { | |
c5aa993b | 647 | /* normal case */ |
c906108c | 648 | fprintf_filtered (stream, "("); |
88750304 | 649 | type_print (value_type (val), "", stream, -1); |
c906108c SS |
650 | fprintf_filtered (stream, ") "); |
651 | } | |
652 | } | |
88750304 | 653 | |
42be36b3 CT |
654 | if (!value_initialized (val)) |
655 | fprintf_filtered (stream, " [uninitialized] "); | |
656 | ||
79a45b7d | 657 | if (options->objectprint && (TYPE_CODE (type) == TYPE_CODE_CLASS)) |
c906108c SS |
658 | { |
659 | /* Attempt to determine real type of object */ | |
660 | real_type = value_rtti_type (val, &full, &top, &using_enc); | |
c5aa993b JM |
661 | if (real_type) |
662 | { | |
663 | /* We have RTTI information, so use it */ | |
664 | val = value_full_object (val, real_type, full, top, using_enc); | |
665 | fprintf_filtered (stream, "(%s%s) ", | |
666 | TYPE_NAME (real_type), | |
3d263c1d | 667 | full ? "" : _(" [incomplete object]")); |
c5aa993b | 668 | /* Print out object: enclosing type is same as real_type if full */ |
46615f07 AC |
669 | return val_print (value_enclosing_type (val), |
670 | value_contents_all (val), 0, | |
79a45b7d TT |
671 | VALUE_ADDRESS (val), stream, 0, |
672 | &opts, current_language); | |
c4093a6a JM |
673 | /* Note: When we look up RTTI entries, we don't get any information on |
674 | const or volatile attributes */ | |
c5aa993b | 675 | } |
88750304 | 676 | else if (type != check_typedef (value_enclosing_type (val))) |
c5aa993b JM |
677 | { |
678 | /* No RTTI information, so let's do our best */ | |
679 | fprintf_filtered (stream, "(%s ?) ", | |
4754a64e | 680 | TYPE_NAME (value_enclosing_type (val))); |
46615f07 AC |
681 | return val_print (value_enclosing_type (val), |
682 | value_contents_all (val), 0, | |
79a45b7d TT |
683 | VALUE_ADDRESS (val), stream, 0, |
684 | &opts, current_language); | |
c5aa993b | 685 | } |
c906108c SS |
686 | /* Otherwise, we end up at the return outside this "if" */ |
687 | } | |
c5aa993b | 688 | |
6c7a06a3 | 689 | return val_print (val_type, value_contents_all (val), |
13c3b5f5 | 690 | value_embedded_offset (val), |
df407dfe | 691 | VALUE_ADDRESS (val) + value_offset (val), |
79a45b7d | 692 | stream, 0, &opts, current_language); |
c906108c | 693 | } |