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