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