Commit | Line | Data |
---|---|---|
373a8247 | 1 | /* Support for printing Pascal values for GDB, the GNU debugger. |
5bcca90b | 2 | |
61baf725 | 3 | Copyright (C) 2000-2017 Free Software Foundation, Inc. |
373a8247 PM |
4 | |
5 | This file is part of GDB. | |
6 | ||
7 | This program is free software; you can redistribute it and/or modify | |
8 | it under the terms of the GNU General Public License as published by | |
a9762ec7 | 9 | the Free Software Foundation; either version 3 of the License, or |
373a8247 PM |
10 | (at your option) any later version. |
11 | ||
12 | This program is distributed in the hope that it will be useful, | |
13 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 | GNU General Public License for more details. | |
16 | ||
17 | You should have received a copy of the GNU General Public License | |
a9762ec7 | 18 | along with this program. If not, see <http://www.gnu.org/licenses/>. */ |
373a8247 PM |
19 | |
20 | /* This file is derived from c-valprint.c */ | |
21 | ||
22 | #include "defs.h" | |
04ea0df1 | 23 | #include "gdb_obstack.h" |
373a8247 PM |
24 | #include "symtab.h" |
25 | #include "gdbtypes.h" | |
26 | #include "expression.h" | |
27 | #include "value.h" | |
28 | #include "command.h" | |
29 | #include "gdbcmd.h" | |
30 | #include "gdbcore.h" | |
31 | #include "demangle.h" | |
32 | #include "valprint.h" | |
3172dc30 | 33 | #include "typeprint.h" |
373a8247 PM |
34 | #include "language.h" |
35 | #include "target.h" | |
36 | #include "annotate.h" | |
37 | #include "p-lang.h" | |
eb43544b | 38 | #include "cp-abi.h" |
d3cbe7ef | 39 | #include "cp-support.h" |
77e371c0 | 40 | #include "objfiles.h" |
49663d05 | 41 | #include "common/byte-vector.h" |
373a8247 PM |
42 | \f |
43 | ||
e88acd96 TT |
44 | /* Decorations for Pascal. */ |
45 | ||
46 | static const struct generic_val_print_decorations p_decorations = | |
47 | { | |
48 | "", | |
49 | " + ", | |
50 | " * I", | |
51 | "true", | |
52 | "false", | |
00272ec4 TT |
53 | "void", |
54 | "{", | |
55 | "}" | |
e88acd96 TT |
56 | }; |
57 | ||
32b72a42 | 58 | /* See val_print for a description of the various parameters of this |
d3eab38a | 59 | function; they are identical. */ |
373a8247 | 60 | |
d3eab38a | 61 | void |
e8b24d9f | 62 | pascal_val_print (struct type *type, |
a2bd3dcd | 63 | int embedded_offset, CORE_ADDR address, |
79a45b7d | 64 | struct ui_file *stream, int recurse, |
e8b24d9f | 65 | struct value *original_value, |
79a45b7d | 66 | const struct value_print_options *options) |
373a8247 | 67 | { |
5af949e3 | 68 | struct gdbarch *gdbarch = get_type_arch (type); |
e17a4113 | 69 | enum bfd_endian byte_order = gdbarch_byte_order (gdbarch); |
52f0bd74 | 70 | unsigned int i = 0; /* Number of characters printed */ |
373a8247 | 71 | unsigned len; |
df178451 | 72 | LONGEST low_bound, high_bound; |
373a8247 PM |
73 | struct type *elttype; |
74 | unsigned eltlen; | |
5598ce11 | 75 | int length_pos, length_size, string_pos; |
6c7a06a3 | 76 | struct type *char_type; |
373a8247 | 77 | CORE_ADDR addr; |
b012acdd | 78 | int want_space = 0; |
e8b24d9f | 79 | const gdb_byte *valaddr = value_contents_for_printing (original_value); |
373a8247 | 80 | |
f168693b | 81 | type = check_typedef (type); |
373a8247 PM |
82 | switch (TYPE_CODE (type)) |
83 | { | |
84 | case TYPE_CODE_ARRAY: | |
6ced1581 | 85 | if (get_array_bounds (type, &low_bound, &high_bound)) |
373a8247 | 86 | { |
8148cf8d | 87 | len = high_bound - low_bound + 1; |
373a8247 PM |
88 | elttype = check_typedef (TYPE_TARGET_TYPE (type)); |
89 | eltlen = TYPE_LENGTH (elttype); | |
2a998fc0 | 90 | if (options->prettyformat_arrays) |
373a8247 PM |
91 | { |
92 | print_spaces_filtered (2 + 2 * recurse, stream); | |
93 | } | |
6e354e5e PM |
94 | /* If 's' format is used, try to print out as string. |
95 | If no format is given, print as string if element type | |
96 | is of TYPE_CODE_CHAR and element size is 1,2 or 4. */ | |
97 | if (options->format == 's' | |
98 | || ((eltlen == 1 || eltlen == 2 || eltlen == 4) | |
99 | && TYPE_CODE (elttype) == TYPE_CODE_CHAR | |
100 | && options->format == 0)) | |
373a8247 PM |
101 | { |
102 | /* If requested, look for the first null char and only print | |
103 | elements up to it. */ | |
79a45b7d | 104 | if (options->stop_print_at_null) |
373a8247 PM |
105 | { |
106 | unsigned int temp_len; | |
107 | ||
0df8b418 | 108 | /* Look for a NULL char. */ |
373a8247 | 109 | for (temp_len = 0; |
58159490 | 110 | extract_unsigned_integer (valaddr + embedded_offset + |
e17a4113 UW |
111 | temp_len * eltlen, eltlen, |
112 | byte_order) | |
79a45b7d | 113 | && temp_len < len && temp_len < options->print_max; |
373a8247 PM |
114 | temp_len++); |
115 | len = temp_len; | |
116 | } | |
117 | ||
6c7a06a3 | 118 | LA_PRINT_STRING (stream, TYPE_TARGET_TYPE (type), |
be759fcf | 119 | valaddr + embedded_offset, len, NULL, 0, |
6c7a06a3 | 120 | options); |
373a8247 PM |
121 | i = len; |
122 | } | |
123 | else | |
124 | { | |
125 | fprintf_filtered (stream, "{"); | |
126 | /* If this is a virtual function table, print the 0th | |
127 | entry specially, and the rest of the members normally. */ | |
128 | if (pascal_object_is_vtbl_ptr_type (elttype)) | |
129 | { | |
130 | i = 1; | |
131 | fprintf_filtered (stream, "%d vtable entries", len - 1); | |
132 | } | |
133 | else | |
134 | { | |
135 | i = 0; | |
136 | } | |
e8b24d9f | 137 | val_print_array_elements (type, embedded_offset, |
3e43a32a MS |
138 | address, stream, recurse, |
139 | original_value, options, i); | |
373a8247 PM |
140 | fprintf_filtered (stream, "}"); |
141 | } | |
142 | break; | |
143 | } | |
144 | /* Array of unspecified length: treat like pointer to first elt. */ | |
13163d80 | 145 | addr = address + embedded_offset; |
373a8247 PM |
146 | goto print_unpacked_pointer; |
147 | ||
148 | case TYPE_CODE_PTR: | |
79a45b7d | 149 | if (options->format && options->format != 's') |
373a8247 | 150 | { |
e8b24d9f | 151 | val_print_scalar_formatted (type, embedded_offset, |
ab2188aa | 152 | original_value, options, 0, stream); |
373a8247 PM |
153 | break; |
154 | } | |
79a45b7d | 155 | if (options->vtblprint && pascal_object_is_vtbl_ptr_type (type)) |
373a8247 PM |
156 | { |
157 | /* Print the unmangled name if desired. */ | |
158 | /* Print vtable entry - we only get here if we ARE using | |
0df8b418 | 159 | -fvtable_thunks. (Otherwise, look under TYPE_CODE_STRUCT.) */ |
b276f1bb | 160 | /* Extract the address, assume that it is unsigned. */ |
e17a4113 UW |
161 | addr = extract_unsigned_integer (valaddr + embedded_offset, |
162 | TYPE_LENGTH (type), byte_order); | |
edf0c1b7 | 163 | print_address_demangle (options, gdbarch, addr, stream, demangle); |
373a8247 PM |
164 | break; |
165 | } | |
91e8df85 | 166 | check_typedef (TYPE_TARGET_TYPE (type)); |
e13eedd5 PM |
167 | |
168 | addr = unpack_pointer (type, valaddr + embedded_offset); | |
169 | print_unpacked_pointer: | |
170 | elttype = check_typedef (TYPE_TARGET_TYPE (type)); | |
171 | ||
172 | if (TYPE_CODE (elttype) == TYPE_CODE_FUNC) | |
373a8247 | 173 | { |
e13eedd5 | 174 | /* Try to print what function it points to. */ |
edf0c1b7 | 175 | print_address_demangle (options, gdbarch, addr, stream, demangle); |
d3eab38a | 176 | return; |
e13eedd5 | 177 | } |
373a8247 | 178 | |
e13eedd5 PM |
179 | if (options->addressprint && options->format != 's') |
180 | { | |
181 | fputs_filtered (paddress (gdbarch, addr), stream); | |
b012acdd | 182 | want_space = 1; |
e13eedd5 | 183 | } |
373a8247 | 184 | |
e13eedd5 PM |
185 | /* For a pointer to char or unsigned char, also print the string |
186 | pointed to, unless pointer is null. */ | |
187 | if (((TYPE_LENGTH (elttype) == 1 | |
188 | && (TYPE_CODE (elttype) == TYPE_CODE_INT | |
189 | || TYPE_CODE (elttype) == TYPE_CODE_CHAR)) | |
190 | || ((TYPE_LENGTH (elttype) == 2 || TYPE_LENGTH (elttype) == 4) | |
191 | && TYPE_CODE (elttype) == TYPE_CODE_CHAR)) | |
192 | && (options->format == 0 || options->format == 's') | |
193 | && addr != 0) | |
194 | { | |
b012acdd TT |
195 | if (want_space) |
196 | fputs_filtered (" ", stream); | |
0df8b418 | 197 | /* No wide string yet. */ |
09ca9e2e | 198 | i = val_print_string (elttype, NULL, addr, -1, stream, options); |
e13eedd5 | 199 | } |
0df8b418 | 200 | /* Also for pointers to pascal strings. */ |
e13eedd5 PM |
201 | /* Note: this is Free Pascal specific: |
202 | as GDB does not recognize stabs pascal strings | |
203 | Pascal strings are mapped to records | |
0df8b418 | 204 | with lowercase names PM. */ |
e13eedd5 PM |
205 | if (is_pascal_string_type (elttype, &length_pos, &length_size, |
206 | &string_pos, &char_type, NULL) | |
207 | && addr != 0) | |
208 | { | |
209 | ULONGEST string_length; | |
7c543f7b | 210 | gdb_byte *buffer; |
ad3bbd48 | 211 | |
b012acdd TT |
212 | if (want_space) |
213 | fputs_filtered (" ", stream); | |
7c543f7b | 214 | buffer = (gdb_byte *) xmalloc (length_size); |
e13eedd5 PM |
215 | read_memory (addr + length_pos, buffer, length_size); |
216 | string_length = extract_unsigned_integer (buffer, length_size, | |
217 | byte_order); | |
218 | xfree (buffer); | |
09ca9e2e TT |
219 | i = val_print_string (char_type, NULL, |
220 | addr + string_pos, string_length, | |
221 | stream, options); | |
e13eedd5 PM |
222 | } |
223 | else if (pascal_object_is_vtbl_member (type)) | |
224 | { | |
0df8b418 | 225 | /* Print vtbl's nicely. */ |
3e43a32a MS |
226 | CORE_ADDR vt_address = unpack_pointer (type, |
227 | valaddr + embedded_offset); | |
7cbd4a93 | 228 | struct bound_minimal_symbol msymbol = |
ad3bbd48 MS |
229 | lookup_minimal_symbol_by_pc (vt_address); |
230 | ||
9cb709b6 TT |
231 | /* If 'symbol_print' is set, we did the work above. */ |
232 | if (!options->symbol_print | |
7cbd4a93 | 233 | && (msymbol.minsym != NULL) |
77e371c0 | 234 | && (vt_address == BMSYMBOL_VALUE_ADDRESS (msymbol))) |
373a8247 | 235 | { |
b012acdd TT |
236 | if (want_space) |
237 | fputs_filtered (" ", stream); | |
238 | fputs_filtered ("<", stream); | |
efd66ac6 | 239 | fputs_filtered (MSYMBOL_PRINT_NAME (msymbol.minsym), stream); |
e13eedd5 | 240 | fputs_filtered (">", stream); |
b012acdd | 241 | want_space = 1; |
373a8247 | 242 | } |
e13eedd5 | 243 | if (vt_address && options->vtblprint) |
373a8247 | 244 | { |
e13eedd5 | 245 | struct value *vt_val; |
be903358 | 246 | struct symbol *wsym = NULL; |
e13eedd5 | 247 | struct type *wtype; |
be903358 | 248 | struct block *block = NULL; |
1993b719 | 249 | struct field_of_this_result is_this_fld; |
373a8247 | 250 | |
b012acdd TT |
251 | if (want_space) |
252 | fputs_filtered (" ", stream); | |
253 | ||
7cbd4a93 | 254 | if (msymbol.minsym != NULL) |
efd66ac6 | 255 | wsym = lookup_symbol (MSYMBOL_LINKAGE_NAME (msymbol.minsym), |
7cbd4a93 | 256 | block, |
d12307c1 | 257 | VAR_DOMAIN, &is_this_fld).symbol; |
e13eedd5 PM |
258 | |
259 | if (wsym) | |
373a8247 | 260 | { |
e13eedd5 | 261 | wtype = SYMBOL_TYPE (wsym); |
373a8247 | 262 | } |
e13eedd5 | 263 | else |
373a8247 | 264 | { |
e13eedd5 PM |
265 | wtype = TYPE_TARGET_TYPE (type); |
266 | } | |
267 | vt_val = value_at (wtype, vt_address); | |
268 | common_val_print (vt_val, stream, recurse + 1, options, | |
269 | current_language); | |
2a998fc0 | 270 | if (options->prettyformat) |
e13eedd5 PM |
271 | { |
272 | fprintf_filtered (stream, "\n"); | |
273 | print_spaces_filtered (2 + 2 * recurse, stream); | |
373a8247 PM |
274 | } |
275 | } | |
373a8247 | 276 | } |
e13eedd5 | 277 | |
d3eab38a | 278 | return; |
373a8247 | 279 | |
373a8247 | 280 | case TYPE_CODE_REF: |
e88acd96 TT |
281 | case TYPE_CODE_ENUM: |
282 | case TYPE_CODE_FLAGS: | |
283 | case TYPE_CODE_FUNC: | |
284 | case TYPE_CODE_RANGE: | |
285 | case TYPE_CODE_INT: | |
286 | case TYPE_CODE_FLT: | |
287 | case TYPE_CODE_VOID: | |
288 | case TYPE_CODE_ERROR: | |
289 | case TYPE_CODE_UNDEF: | |
290 | case TYPE_CODE_BOOL: | |
291 | case TYPE_CODE_CHAR: | |
e8b24d9f | 292 | generic_val_print (type, embedded_offset, address, |
e88acd96 TT |
293 | stream, recurse, original_value, options, |
294 | &p_decorations); | |
373a8247 PM |
295 | break; |
296 | ||
297 | case TYPE_CODE_UNION: | |
79a45b7d | 298 | if (recurse && !options->unionprint) |
373a8247 PM |
299 | { |
300 | fprintf_filtered (stream, "{...}"); | |
301 | break; | |
302 | } | |
303 | /* Fall through. */ | |
304 | case TYPE_CODE_STRUCT: | |
79a45b7d | 305 | if (options->vtblprint && pascal_object_is_vtbl_ptr_type (type)) |
373a8247 PM |
306 | { |
307 | /* Print the unmangled name if desired. */ | |
308 | /* Print vtable entry - we only get here if NOT using | |
0df8b418 | 309 | -fvtable_thunks. (Otherwise, look under TYPE_CODE_PTR.) */ |
b276f1bb AC |
310 | /* Extract the address, assume that it is unsigned. */ |
311 | print_address_demangle | |
edf0c1b7 | 312 | (options, gdbarch, |
3e43a32a MS |
313 | extract_unsigned_integer (valaddr + embedded_offset |
314 | + TYPE_FIELD_BITPOS (type, | |
315 | VTBL_FNADDR_OFFSET) / 8, | |
316 | TYPE_LENGTH (TYPE_FIELD_TYPE (type, | |
317 | VTBL_FNADDR_OFFSET)), | |
318 | byte_order), | |
b276f1bb | 319 | stream, demangle); |
373a8247 PM |
320 | } |
321 | else | |
322 | { | |
5598ce11 | 323 | if (is_pascal_string_type (type, &length_pos, &length_size, |
6c7a06a3 | 324 | &string_pos, &char_type, NULL)) |
373a8247 | 325 | { |
3e43a32a MS |
326 | len = extract_unsigned_integer (valaddr + embedded_offset |
327 | + length_pos, length_size, | |
328 | byte_order); | |
6ced1581 | 329 | LA_PRINT_STRING (stream, char_type, |
be759fcf PM |
330 | valaddr + embedded_offset + string_pos, |
331 | len, NULL, 0, options); | |
373a8247 PM |
332 | } |
333 | else | |
490f124f | 334 | pascal_object_print_value_fields (type, valaddr, embedded_offset, |
3e43a32a MS |
335 | address, stream, recurse, |
336 | original_value, options, | |
337 | NULL, 0); | |
373a8247 PM |
338 | } |
339 | break; | |
340 | ||
373a8247 PM |
341 | case TYPE_CODE_SET: |
342 | elttype = TYPE_INDEX_TYPE (type); | |
f168693b | 343 | elttype = check_typedef (elttype); |
74a9bb82 | 344 | if (TYPE_STUB (elttype)) |
373a8247 PM |
345 | { |
346 | fprintf_filtered (stream, "<incomplete type>"); | |
347 | gdb_flush (stream); | |
348 | break; | |
349 | } | |
350 | else | |
351 | { | |
352 | struct type *range = elttype; | |
353 | LONGEST low_bound, high_bound; | |
354 | int i; | |
373a8247 PM |
355 | int need_comma = 0; |
356 | ||
6b1755ce | 357 | fputs_filtered ("[", stream); |
373a8247 PM |
358 | |
359 | i = get_discrete_bounds (range, &low_bound, &high_bound); | |
7a081a30 PM |
360 | if (low_bound == 0 && high_bound == -1 && TYPE_LENGTH (type) > 0) |
361 | { | |
362 | /* If we know the size of the set type, we can figure out the | |
363 | maximum value. */ | |
364 | i = 0; | |
365 | high_bound = TYPE_LENGTH (type) * TARGET_CHAR_BIT - 1; | |
366 | TYPE_HIGH_BOUND (range) = high_bound; | |
367 | } | |
373a8247 PM |
368 | maybe_bad_bstring: |
369 | if (i < 0) | |
370 | { | |
371 | fputs_filtered ("<error value>", stream); | |
372 | goto done; | |
373 | } | |
374 | ||
375 | for (i = low_bound; i <= high_bound; i++) | |
376 | { | |
3e43a32a MS |
377 | int element = value_bit_index (type, |
378 | valaddr + embedded_offset, i); | |
ad3bbd48 | 379 | |
373a8247 PM |
380 | if (element < 0) |
381 | { | |
382 | i = element; | |
383 | goto maybe_bad_bstring; | |
384 | } | |
6b1755ce | 385 | if (element) |
373a8247 PM |
386 | { |
387 | if (need_comma) | |
388 | fputs_filtered (", ", stream); | |
389 | print_type_scalar (range, i, stream); | |
390 | need_comma = 1; | |
391 | ||
3e43a32a MS |
392 | if (i + 1 <= high_bound |
393 | && value_bit_index (type, | |
394 | valaddr + embedded_offset, ++i)) | |
373a8247 PM |
395 | { |
396 | int j = i; | |
ad3bbd48 | 397 | |
373a8247 PM |
398 | fputs_filtered ("..", stream); |
399 | while (i + 1 <= high_bound | |
3e43a32a MS |
400 | && value_bit_index (type, |
401 | valaddr + embedded_offset, | |
402 | ++i)) | |
373a8247 PM |
403 | j = i; |
404 | print_type_scalar (range, j, stream); | |
405 | } | |
406 | } | |
407 | } | |
408 | done: | |
6b1755ce | 409 | fputs_filtered ("]", stream); |
373a8247 PM |
410 | } |
411 | break; | |
412 | ||
373a8247 | 413 | default: |
3e43a32a MS |
414 | error (_("Invalid pascal type code %d in symbol table."), |
415 | TYPE_CODE (type)); | |
373a8247 PM |
416 | } |
417 | gdb_flush (stream); | |
373a8247 PM |
418 | } |
419 | \f | |
8e069a98 | 420 | void |
79a45b7d TT |
421 | pascal_value_print (struct value *val, struct ui_file *stream, |
422 | const struct value_print_options *options) | |
373a8247 | 423 | { |
df407dfe | 424 | struct type *type = value_type (val); |
278582cb PM |
425 | struct value_print_options opts = *options; |
426 | ||
427 | opts.deref_ref = 1; | |
373a8247 PM |
428 | |
429 | /* If it is a pointer, indicate what it points to. | |
430 | ||
431 | Print type also if it is a reference. | |
432 | ||
433 | Object pascal: if it is a member pointer, we will take care | |
434 | of that when we print it. */ | |
b20a3440 PM |
435 | if (TYPE_CODE (type) == TYPE_CODE_PTR |
436 | || TYPE_CODE (type) == TYPE_CODE_REF) | |
373a8247 PM |
437 | { |
438 | /* Hack: remove (char *) for char strings. Their | |
0df8b418 | 439 | type is indicated by the quoted string anyway. */ |
6ced1581 | 440 | if (TYPE_CODE (type) == TYPE_CODE_PTR |
b20a3440 PM |
441 | && TYPE_NAME (type) == NULL |
442 | && TYPE_NAME (TYPE_TARGET_TYPE (type)) != NULL | |
6314a349 | 443 | && strcmp (TYPE_NAME (TYPE_TARGET_TYPE (type)), "char") == 0) |
373a8247 | 444 | { |
0df8b418 | 445 | /* Print nothing. */ |
373a8247 PM |
446 | } |
447 | else | |
448 | { | |
449 | fprintf_filtered (stream, "("); | |
450 | type_print (type, "", stream, -1); | |
451 | fprintf_filtered (stream, ") "); | |
452 | } | |
453 | } | |
8e069a98 | 454 | common_val_print (val, stream, 0, &opts, current_language); |
373a8247 PM |
455 | } |
456 | ||
457 | ||
920d2a44 AC |
458 | static void |
459 | show_pascal_static_field_print (struct ui_file *file, int from_tty, | |
460 | struct cmd_list_element *c, const char *value) | |
461 | { | |
462 | fprintf_filtered (file, _("Printing of pascal static members is %s.\n"), | |
463 | value); | |
464 | } | |
373a8247 PM |
465 | |
466 | static struct obstack dont_print_vb_obstack; | |
467 | static struct obstack dont_print_statmem_obstack; | |
468 | ||
806048c6 | 469 | static void pascal_object_print_static_field (struct value *, |
79a45b7d TT |
470 | struct ui_file *, int, |
471 | const struct value_print_options *); | |
373a8247 | 472 | |
fc1a4b47 | 473 | static void pascal_object_print_value (struct type *, const gdb_byte *, |
6b850546 | 474 | LONGEST, |
79a45b7d | 475 | CORE_ADDR, struct ui_file *, int, |
e8b24d9f | 476 | struct value *, |
79a45b7d | 477 | const struct value_print_options *, |
a2bd3dcd | 478 | struct type **); |
373a8247 | 479 | |
373a8247 PM |
480 | /* It was changed to this after 2.4.5. */ |
481 | const char pascal_vtbl_ptr_name[] = | |
482 | {'_', '_', 'v', 't', 'b', 'l', '_', 'p', 't', 'r', '_', 't', 'y', 'p', 'e', 0}; | |
483 | ||
484 | /* Return truth value for assertion that TYPE is of the type | |
485 | "pointer to virtual function". */ | |
486 | ||
487 | int | |
fba45db2 | 488 | pascal_object_is_vtbl_ptr_type (struct type *type) |
373a8247 | 489 | { |
fe978cb0 | 490 | const char *type_name = type_name_no_tag (type); |
373a8247 | 491 | |
fe978cb0 PA |
492 | return (type_name != NULL |
493 | && strcmp (type_name, pascal_vtbl_ptr_name) == 0); | |
373a8247 PM |
494 | } |
495 | ||
496 | /* Return truth value for the assertion that TYPE is of the type | |
497 | "pointer to virtual function table". */ | |
498 | ||
499 | int | |
fba45db2 | 500 | pascal_object_is_vtbl_member (struct type *type) |
373a8247 PM |
501 | { |
502 | if (TYPE_CODE (type) == TYPE_CODE_PTR) | |
503 | { | |
504 | type = TYPE_TARGET_TYPE (type); | |
505 | if (TYPE_CODE (type) == TYPE_CODE_ARRAY) | |
506 | { | |
507 | type = TYPE_TARGET_TYPE (type); | |
0df8b418 MS |
508 | if (TYPE_CODE (type) == TYPE_CODE_STRUCT /* If not using |
509 | thunks. */ | |
510 | || TYPE_CODE (type) == TYPE_CODE_PTR) /* If using thunks. */ | |
373a8247 PM |
511 | { |
512 | /* Virtual functions tables are full of pointers | |
0df8b418 | 513 | to virtual functions. */ |
373a8247 PM |
514 | return pascal_object_is_vtbl_ptr_type (type); |
515 | } | |
516 | } | |
517 | } | |
518 | return 0; | |
519 | } | |
520 | ||
a2bd3dcd AC |
521 | /* Mutually recursive subroutines of pascal_object_print_value and |
522 | c_val_print to print out a structure's fields: | |
523 | pascal_object_print_value_fields and pascal_object_print_value. | |
373a8247 | 524 | |
79a45b7d | 525 | TYPE, VALADDR, ADDRESS, STREAM, RECURSE, and OPTIONS have the |
373a8247 PM |
526 | same meanings as in pascal_object_print_value and c_val_print. |
527 | ||
528 | DONT_PRINT is an array of baseclass types that we | |
529 | should not print, or zero if called from top level. */ | |
530 | ||
531 | void | |
fc1a4b47 | 532 | pascal_object_print_value_fields (struct type *type, const gdb_byte *valaddr, |
6b850546 | 533 | LONGEST offset, |
fba45db2 | 534 | CORE_ADDR address, struct ui_file *stream, |
79a45b7d | 535 | int recurse, |
e8b24d9f | 536 | struct value *val, |
79a45b7d | 537 | const struct value_print_options *options, |
fba45db2 KB |
538 | struct type **dont_print_vb, |
539 | int dont_print_statmem) | |
373a8247 PM |
540 | { |
541 | int i, len, n_baseclasses; | |
79f33898 SM |
542 | char *last_dont_print |
543 | = (char *) obstack_next_free (&dont_print_statmem_obstack); | |
373a8247 | 544 | |
f168693b | 545 | type = check_typedef (type); |
373a8247 PM |
546 | |
547 | fprintf_filtered (stream, "{"); | |
548 | len = TYPE_NFIELDS (type); | |
549 | n_baseclasses = TYPE_N_BASECLASSES (type); | |
550 | ||
551 | /* Print out baseclasses such that we don't print | |
552 | duplicates of virtual baseclasses. */ | |
553 | if (n_baseclasses > 0) | |
490f124f PA |
554 | pascal_object_print_value (type, valaddr, offset, address, |
555 | stream, recurse + 1, val, | |
556 | options, dont_print_vb); | |
373a8247 PM |
557 | |
558 | if (!len && n_baseclasses == 1) | |
559 | fprintf_filtered (stream, "<No data fields>"); | |
560 | else | |
561 | { | |
c1b6e682 | 562 | struct obstack tmp_obstack = dont_print_statmem_obstack; |
373a8247 PM |
563 | int fields_seen = 0; |
564 | ||
565 | if (dont_print_statmem == 0) | |
566 | { | |
567 | /* If we're at top level, carve out a completely fresh | |
568 | chunk of the obstack and use that until this particular | |
569 | invocation returns. */ | |
373a8247 PM |
570 | obstack_finish (&dont_print_statmem_obstack); |
571 | } | |
572 | ||
573 | for (i = n_baseclasses; i < len; i++) | |
574 | { | |
575 | /* If requested, skip printing of static fields. */ | |
79a45b7d | 576 | if (!options->pascal_static_field_print |
d6a843b5 | 577 | && field_is_static (&TYPE_FIELD (type, i))) |
373a8247 PM |
578 | continue; |
579 | if (fields_seen) | |
580 | fprintf_filtered (stream, ", "); | |
581 | else if (n_baseclasses > 0) | |
582 | { | |
2a998fc0 | 583 | if (options->prettyformat) |
373a8247 PM |
584 | { |
585 | fprintf_filtered (stream, "\n"); | |
586 | print_spaces_filtered (2 + 2 * recurse, stream); | |
587 | fputs_filtered ("members of ", stream); | |
588 | fputs_filtered (type_name_no_tag (type), stream); | |
589 | fputs_filtered (": ", stream); | |
590 | } | |
591 | } | |
592 | fields_seen = 1; | |
593 | ||
2a998fc0 | 594 | if (options->prettyformat) |
373a8247 PM |
595 | { |
596 | fprintf_filtered (stream, "\n"); | |
597 | print_spaces_filtered (2 + 2 * recurse, stream); | |
598 | } | |
599 | else | |
600 | { | |
601 | wrap_here (n_spaces (2 + 2 * recurse)); | |
602 | } | |
e93a8774 TT |
603 | |
604 | annotate_field_begin (TYPE_FIELD_TYPE (type, i)); | |
605 | ||
606 | if (field_is_static (&TYPE_FIELD (type, i))) | |
607 | fputs_filtered ("static ", stream); | |
608 | fprintf_symbol_filtered (stream, TYPE_FIELD_NAME (type, i), | |
609 | language_cplus, | |
610 | DMGL_PARAMS | DMGL_ANSI); | |
611 | annotate_field_name_end (); | |
612 | fputs_filtered (" = ", stream); | |
613 | annotate_field_value (); | |
373a8247 | 614 | |
d6a843b5 JK |
615 | if (!field_is_static (&TYPE_FIELD (type, i)) |
616 | && TYPE_FIELD_PACKED (type, i)) | |
373a8247 | 617 | { |
6943961c | 618 | struct value *v; |
373a8247 PM |
619 | |
620 | /* Bitfields require special handling, especially due to byte | |
621 | order problems. */ | |
622 | if (TYPE_FIELD_IGNORE (type, i)) | |
623 | { | |
624 | fputs_filtered ("<optimized out or zero length>", stream); | |
625 | } | |
8cf6f0b1 TT |
626 | else if (value_bits_synthetic_pointer (val, |
627 | TYPE_FIELD_BITPOS (type, | |
628 | i), | |
629 | TYPE_FIELD_BITSIZE (type, | |
630 | i))) | |
631 | { | |
632 | fputs_filtered (_("<synthetic pointer>"), stream); | |
633 | } | |
373a8247 PM |
634 | else |
635 | { | |
79a45b7d | 636 | struct value_print_options opts = *options; |
ad3bbd48 | 637 | |
5467c6c8 | 638 | v = value_field_bitfield (type, i, valaddr, offset, val); |
373a8247 | 639 | |
79a45b7d TT |
640 | opts.deref_ref = 0; |
641 | common_val_print (v, stream, recurse + 1, &opts, | |
642 | current_language); | |
373a8247 PM |
643 | } |
644 | } | |
645 | else | |
646 | { | |
647 | if (TYPE_FIELD_IGNORE (type, i)) | |
648 | { | |
649 | fputs_filtered ("<optimized out or zero length>", stream); | |
650 | } | |
d6a843b5 | 651 | else if (field_is_static (&TYPE_FIELD (type, i))) |
373a8247 | 652 | { |
3e43a32a | 653 | /* struct value *v = value_static_field (type, i); |
0df8b418 | 654 | v4.17 specific. */ |
6943961c | 655 | struct value *v; |
ad3bbd48 | 656 | |
5467c6c8 | 657 | v = value_field_bitfield (type, i, valaddr, offset, val); |
373a8247 PM |
658 | |
659 | if (v == NULL) | |
901461f8 | 660 | val_print_optimized_out (NULL, stream); |
373a8247 | 661 | else |
79a45b7d TT |
662 | pascal_object_print_static_field (v, stream, recurse + 1, |
663 | options); | |
373a8247 PM |
664 | } |
665 | else | |
666 | { | |
79a45b7d | 667 | struct value_print_options opts = *options; |
ad3bbd48 | 668 | |
79a45b7d | 669 | opts.deref_ref = 0; |
373a8247 PM |
670 | /* val_print (TYPE_FIELD_TYPE (type, i), |
671 | valaddr + TYPE_FIELD_BITPOS (type, i) / 8, | |
672 | address + TYPE_FIELD_BITPOS (type, i) / 8, 0, | |
673 | stream, format, 0, recurse + 1, pretty); */ | |
674 | val_print (TYPE_FIELD_TYPE (type, i), | |
e8b24d9f | 675 | offset + TYPE_FIELD_BITPOS (type, i) / 8, |
490f124f | 676 | address, stream, recurse + 1, val, &opts, |
d8ca156b | 677 | current_language); |
373a8247 PM |
678 | } |
679 | } | |
680 | annotate_field_end (); | |
681 | } | |
682 | ||
683 | if (dont_print_statmem == 0) | |
684 | { | |
685 | /* Free the space used to deal with the printing | |
686 | of the members from top level. */ | |
687 | obstack_free (&dont_print_statmem_obstack, last_dont_print); | |
688 | dont_print_statmem_obstack = tmp_obstack; | |
689 | } | |
690 | ||
2a998fc0 | 691 | if (options->prettyformat) |
373a8247 PM |
692 | { |
693 | fprintf_filtered (stream, "\n"); | |
694 | print_spaces_filtered (2 * recurse, stream); | |
695 | } | |
696 | } | |
697 | fprintf_filtered (stream, "}"); | |
698 | } | |
699 | ||
700 | /* Special val_print routine to avoid printing multiple copies of virtual | |
701 | baseclasses. */ | |
702 | ||
7080f20f | 703 | static void |
fc1a4b47 | 704 | pascal_object_print_value (struct type *type, const gdb_byte *valaddr, |
6b850546 | 705 | LONGEST offset, |
a2bd3dcd | 706 | CORE_ADDR address, struct ui_file *stream, |
79a45b7d | 707 | int recurse, |
e8b24d9f | 708 | struct value *val, |
79a45b7d | 709 | const struct value_print_options *options, |
fba45db2 | 710 | struct type **dont_print_vb) |
373a8247 | 711 | { |
373a8247 | 712 | struct type **last_dont_print |
ad3bbd48 | 713 | = (struct type **) obstack_next_free (&dont_print_vb_obstack); |
c1b6e682 | 714 | struct obstack tmp_obstack = dont_print_vb_obstack; |
373a8247 PM |
715 | int i, n_baseclasses = TYPE_N_BASECLASSES (type); |
716 | ||
717 | if (dont_print_vb == 0) | |
718 | { | |
719 | /* If we're at top level, carve out a completely fresh | |
720 | chunk of the obstack and use that until this particular | |
721 | invocation returns. */ | |
373a8247 PM |
722 | /* Bump up the high-water mark. Now alpha is omega. */ |
723 | obstack_finish (&dont_print_vb_obstack); | |
724 | } | |
725 | ||
726 | for (i = 0; i < n_baseclasses; i++) | |
727 | { | |
6b850546 | 728 | LONGEST boffset = 0; |
373a8247 | 729 | struct type *baseclass = check_typedef (TYPE_BASECLASS (type, i)); |
0d5cff50 | 730 | const char *basename = type_name_no_tag (baseclass); |
8af8e3bc | 731 | const gdb_byte *base_valaddr = NULL; |
6b850546 | 732 | LONGEST thisoffset; |
8af8e3bc | 733 | int skip = 0; |
49663d05 | 734 | gdb::byte_vector buf; |
373a8247 PM |
735 | |
736 | if (BASETYPE_VIA_VIRTUAL (type, i)) | |
737 | { | |
738 | struct type **first_dont_print | |
ad3bbd48 | 739 | = (struct type **) obstack_base (&dont_print_vb_obstack); |
373a8247 PM |
740 | |
741 | int j = (struct type **) obstack_next_free (&dont_print_vb_obstack) | |
ad3bbd48 | 742 | - first_dont_print; |
373a8247 PM |
743 | |
744 | while (--j >= 0) | |
745 | if (baseclass == first_dont_print[j]) | |
746 | goto flush_it; | |
747 | ||
748 | obstack_ptr_grow (&dont_print_vb_obstack, baseclass); | |
749 | } | |
750 | ||
490f124f PA |
751 | thisoffset = offset; |
752 | ||
492d29ea | 753 | TRY |
8af8e3bc PA |
754 | { |
755 | boffset = baseclass_offset (type, i, valaddr, offset, address, val); | |
756 | } | |
492d29ea | 757 | CATCH (ex, RETURN_MASK_ERROR) |
8af8e3bc | 758 | { |
7556d4a4 PA |
759 | if (ex.error == NOT_AVAILABLE_ERROR) |
760 | skip = -1; | |
761 | else | |
762 | skip = 1; | |
763 | } | |
492d29ea | 764 | END_CATCH |
8af8e3bc | 765 | |
7556d4a4 PA |
766 | if (skip == 0) |
767 | { | |
8af8e3bc PA |
768 | /* The virtual base class pointer might have been clobbered by the |
769 | user program. Make sure that it still points to a valid memory | |
770 | location. */ | |
771 | ||
772 | if (boffset < 0 || boffset >= TYPE_LENGTH (type)) | |
773 | { | |
49663d05 | 774 | buf.resize (TYPE_LENGTH (baseclass)); |
6c18f3e0 | 775 | |
49663d05 TT |
776 | base_valaddr = buf.data (); |
777 | if (target_read_memory (address + boffset, buf.data (), | |
8af8e3bc PA |
778 | TYPE_LENGTH (baseclass)) != 0) |
779 | skip = 1; | |
780 | address = address + boffset; | |
781 | thisoffset = 0; | |
782 | boffset = 0; | |
783 | } | |
784 | else | |
785 | base_valaddr = valaddr; | |
786 | } | |
373a8247 | 787 | |
2a998fc0 | 788 | if (options->prettyformat) |
373a8247 PM |
789 | { |
790 | fprintf_filtered (stream, "\n"); | |
791 | print_spaces_filtered (2 * recurse, stream); | |
792 | } | |
793 | fputs_filtered ("<", stream); | |
794 | /* Not sure what the best notation is in the case where there is no | |
795 | baseclass name. */ | |
796 | ||
797 | fputs_filtered (basename ? basename : "", stream); | |
798 | fputs_filtered ("> = ", stream); | |
799 | ||
8af8e3bc PA |
800 | if (skip < 0) |
801 | val_print_unavailable (stream); | |
802 | else if (skip > 0) | |
803 | val_print_invalid_address (stream); | |
373a8247 | 804 | else |
3e43a32a | 805 | pascal_object_print_value_fields (baseclass, base_valaddr, |
490f124f PA |
806 | thisoffset + boffset, address, |
807 | stream, recurse, val, options, | |
373a8247 PM |
808 | (struct type **) obstack_base (&dont_print_vb_obstack), |
809 | 0); | |
810 | fputs_filtered (", ", stream); | |
811 | ||
812 | flush_it: | |
813 | ; | |
814 | } | |
815 | ||
816 | if (dont_print_vb == 0) | |
817 | { | |
818 | /* Free the space used to deal with the printing | |
819 | of this type from top level. */ | |
820 | obstack_free (&dont_print_vb_obstack, last_dont_print); | |
821 | /* Reset watermark so that we can continue protecting | |
822 | ourselves from whatever we were protecting ourselves. */ | |
823 | dont_print_vb_obstack = tmp_obstack; | |
824 | } | |
825 | } | |
826 | ||
827 | /* Print value of a static member. | |
828 | To avoid infinite recursion when printing a class that contains | |
829 | a static instance of the class, we keep the addresses of all printed | |
830 | static member classes in an obstack and refuse to print them more | |
831 | than once. | |
832 | ||
79a45b7d | 833 | VAL contains the value to print, STREAM, RECURSE, and OPTIONS |
373a8247 PM |
834 | have the same meanings as in c_val_print. */ |
835 | ||
836 | static void | |
806048c6 | 837 | pascal_object_print_static_field (struct value *val, |
79a45b7d TT |
838 | struct ui_file *stream, |
839 | int recurse, | |
840 | const struct value_print_options *options) | |
373a8247 | 841 | { |
806048c6 | 842 | struct type *type = value_type (val); |
79a45b7d | 843 | struct value_print_options opts; |
806048c6 | 844 | |
686d4def PA |
845 | if (value_entirely_optimized_out (val)) |
846 | { | |
847 | val_print_optimized_out (val, stream); | |
848 | return; | |
849 | } | |
850 | ||
373a8247 PM |
851 | if (TYPE_CODE (type) == TYPE_CODE_STRUCT) |
852 | { | |
42ae5230 | 853 | CORE_ADDR *first_dont_print, addr; |
373a8247 PM |
854 | int i; |
855 | ||
856 | first_dont_print | |
857 | = (CORE_ADDR *) obstack_base (&dont_print_statmem_obstack); | |
858 | i = (CORE_ADDR *) obstack_next_free (&dont_print_statmem_obstack) | |
859 | - first_dont_print; | |
860 | ||
861 | while (--i >= 0) | |
862 | { | |
42ae5230 | 863 | if (value_address (val) == first_dont_print[i]) |
373a8247 | 864 | { |
3e43a32a MS |
865 | fputs_filtered ("\ |
866 | <same as static member of an already seen type>", | |
373a8247 PM |
867 | stream); |
868 | return; | |
869 | } | |
870 | } | |
871 | ||
42ae5230 TT |
872 | addr = value_address (val); |
873 | obstack_grow (&dont_print_statmem_obstack, (char *) &addr, | |
373a8247 PM |
874 | sizeof (CORE_ADDR)); |
875 | ||
f168693b | 876 | type = check_typedef (type); |
490f124f PA |
877 | pascal_object_print_value_fields (type, |
878 | value_contents_for_printing (val), | |
879 | value_embedded_offset (val), | |
880 | addr, | |
881 | stream, recurse, | |
882 | val, options, NULL, 1); | |
373a8247 PM |
883 | return; |
884 | } | |
79a45b7d TT |
885 | |
886 | opts = *options; | |
887 | opts.deref_ref = 0; | |
888 | common_val_print (val, stream, recurse, &opts, current_language); | |
373a8247 PM |
889 | } |
890 | ||
373a8247 | 891 | void |
fba45db2 | 892 | _initialize_pascal_valprint (void) |
373a8247 | 893 | { |
5bf193a2 | 894 | add_setshow_boolean_cmd ("pascal_static-members", class_support, |
79a45b7d | 895 | &user_print_options.pascal_static_field_print, _("\ |
5bf193a2 AC |
896 | Set printing of pascal static members."), _("\ |
897 | Show printing of pascal static members."), NULL, | |
898 | NULL, | |
920d2a44 | 899 | show_pascal_static_field_print, |
5bf193a2 | 900 | &setprintlist, &showprintlist); |
373a8247 | 901 | } |