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