Commit | Line | Data |
---|---|---|
c906108c | 1 | /* Support for printing C++ values for GDB, the GNU debugger. |
b6ba6518 | 2 | Copyright 1986, 1988, 1989, 1991, 1992, 1993, 1994, 1995, 1996, 1997, |
0e5e3ea6 | 3 | 2000, 2001, 2002 |
c906108c SS |
4 | Free Software Foundation, Inc. |
5 | ||
c5aa993b | 6 | This file is part of GDB. |
c906108c | 7 | |
c5aa993b JM |
8 | This program is free software; you can redistribute it and/or modify |
9 | it under the terms of the GNU General Public License as published by | |
10 | the Free Software Foundation; either version 2 of the License, or | |
11 | (at your option) any later version. | |
c906108c | 12 | |
c5aa993b JM |
13 | This program is distributed in the hope that it will be useful, |
14 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
16 | GNU General Public License for more details. | |
c906108c | 17 | |
c5aa993b JM |
18 | You should have received a copy of the GNU General Public License |
19 | along with this program; if not, write to the Free Software | |
20 | Foundation, Inc., 59 Temple Place - Suite 330, | |
21 | Boston, MA 02111-1307, USA. */ | |
c906108c SS |
22 | |
23 | #include "defs.h" | |
04ea0df1 | 24 | #include "gdb_obstack.h" |
c906108c SS |
25 | #include "symtab.h" |
26 | #include "gdbtypes.h" | |
27 | #include "expression.h" | |
28 | #include "value.h" | |
29 | #include "command.h" | |
30 | #include "gdbcmd.h" | |
31 | #include "demangle.h" | |
32 | #include "annotate.h" | |
33 | #include "gdb_string.h" | |
34 | #include "c-lang.h" | |
35 | #include "target.h" | |
b9d652ac | 36 | #include "cp-abi.h" |
c906108c | 37 | |
c5aa993b JM |
38 | /* Indication of presence of HP-compiled object files */ |
39 | extern int hp_som_som_object_present; /* defined in symtab.c */ | |
c906108c SS |
40 | |
41 | ||
42 | int vtblprint; /* Controls printing of vtbl's */ | |
43 | int objectprint; /* Controls looking up an object's derived type | |
44 | using what we find in its vtables. */ | |
c5aa993b | 45 | int static_field_print; /* Controls printing of static fields. */ |
c906108c SS |
46 | |
47 | static struct obstack dont_print_vb_obstack; | |
48 | static struct obstack dont_print_statmem_obstack; | |
49 | ||
a14ed312 | 50 | extern void _initialize_cp_valprint (void); |
392a587b | 51 | |
6943961c | 52 | static void cp_print_static_field (struct type *, struct value *, |
d9fcf2fb JM |
53 | struct ui_file *, int, int, |
54 | enum val_prettyprint); | |
c906108c | 55 | |
d9fcf2fb JM |
56 | static void cp_print_value (struct type *, struct type *, char *, int, |
57 | CORE_ADDR, struct ui_file *, int, int, | |
58 | enum val_prettyprint, struct type **); | |
c906108c | 59 | |
d9fcf2fb | 60 | static void cp_print_hpacc_virtual_table_entries (struct type *, int *, |
6943961c | 61 | struct value *, |
d9fcf2fb JM |
62 | struct ui_file *, int, |
63 | int, | |
64 | enum val_prettyprint); | |
c906108c SS |
65 | |
66 | ||
67 | void | |
2c63a960 JB |
68 | cp_print_class_method (char *valaddr, |
69 | struct type *type, | |
70 | struct ui_file *stream) | |
c906108c SS |
71 | { |
72 | struct type *domain; | |
73 | struct fn_field *f = NULL; | |
74 | int j = 0; | |
75 | int len2; | |
76 | int offset; | |
77 | char *kind = ""; | |
78 | CORE_ADDR addr; | |
79 | struct symbol *sym; | |
80 | unsigned len; | |
81 | unsigned int i; | |
82 | struct type *target_type = check_typedef (TYPE_TARGET_TYPE (type)); | |
83 | ||
84 | domain = TYPE_DOMAIN_TYPE (target_type); | |
c5aa993b | 85 | if (domain == (struct type *) NULL) |
c906108c SS |
86 | { |
87 | fprintf_filtered (stream, "<unknown>"); | |
88 | return; | |
89 | } | |
90 | addr = unpack_pointer (lookup_pointer_type (builtin_type_void), valaddr); | |
91 | if (METHOD_PTR_IS_VIRTUAL (addr)) | |
92 | { | |
93 | offset = METHOD_PTR_TO_VOFFSET (addr); | |
94 | len = TYPE_NFN_FIELDS (domain); | |
95 | for (i = 0; i < len; i++) | |
96 | { | |
97 | f = TYPE_FN_FIELDLIST1 (domain, i); | |
98 | len2 = TYPE_FN_FIELDLIST_LENGTH (domain, i); | |
c5aa993b | 99 | |
de17c821 | 100 | check_stub_method_group (domain, i); |
c906108c SS |
101 | for (j = 0; j < len2; j++) |
102 | { | |
c906108c SS |
103 | if (TYPE_FN_FIELD_VOFFSET (f, j) == offset) |
104 | { | |
c906108c SS |
105 | kind = "virtual "; |
106 | goto common; | |
107 | } | |
108 | } | |
109 | } | |
110 | } | |
111 | else | |
112 | { | |
113 | sym = find_pc_function (addr); | |
114 | if (sym == 0) | |
115 | { | |
c5aa993b JM |
116 | /* 1997-08-01 Currently unsupported with HP aCC */ |
117 | if (hp_som_som_object_present) | |
118 | { | |
119 | fputs_filtered ("?? <not supported with HP aCC>", stream); | |
120 | return; | |
121 | } | |
c906108c SS |
122 | error ("invalid pointer to member function"); |
123 | } | |
124 | len = TYPE_NFN_FIELDS (domain); | |
125 | for (i = 0; i < len; i++) | |
126 | { | |
127 | f = TYPE_FN_FIELDLIST1 (domain, i); | |
128 | len2 = TYPE_FN_FIELDLIST_LENGTH (domain, i); | |
c5aa993b | 129 | |
1b2ef1b6 | 130 | check_stub_method_group (domain, i); |
c906108c SS |
131 | for (j = 0; j < len2; j++) |
132 | { | |
c906108c | 133 | if (STREQ (SYMBOL_NAME (sym), TYPE_FN_FIELD_PHYSNAME (f, j))) |
de17c821 | 134 | goto common; |
c906108c SS |
135 | } |
136 | } | |
137 | } | |
2c63a960 | 138 | common: |
c906108c SS |
139 | if (i < len) |
140 | { | |
141 | char *demangled_name; | |
142 | ||
143 | fprintf_filtered (stream, "&"); | |
144 | fprintf_filtered (stream, kind); | |
145 | demangled_name = cplus_demangle (TYPE_FN_FIELD_PHYSNAME (f, j), | |
146 | DMGL_ANSI | DMGL_PARAMS); | |
147 | if (demangled_name == NULL) | |
148 | fprintf_filtered (stream, "<badly mangled name %s>", | |
149 | TYPE_FN_FIELD_PHYSNAME (f, j)); | |
150 | else | |
151 | { | |
152 | fputs_filtered (demangled_name, stream); | |
b8c9b27d | 153 | xfree (demangled_name); |
c906108c SS |
154 | } |
155 | } | |
156 | else | |
157 | { | |
158 | fprintf_filtered (stream, "("); | |
159 | type_print (type, "", stream, -1); | |
160 | fprintf_filtered (stream, ") %d", (int) addr >> 3); | |
161 | } | |
162 | } | |
163 | ||
164 | /* This was what it was for gcc 2.4.5 and earlier. */ | |
165 | static const char vtbl_ptr_name_old[] = | |
2c63a960 JB |
166 | { |
167 | CPLUS_MARKER, 'v', 't', 'b', 'l', '_', 'p', 't', 'r', '_', | |
168 | 't', 'y', 'p', 'e', 0 | |
169 | }; | |
170 | ||
c906108c | 171 | /* It was changed to this after 2.4.5. */ |
2c63a960 | 172 | const char vtbl_ptr_name[] = "__vtbl_ptr_type"; |
c906108c SS |
173 | |
174 | /* HP aCC uses different names */ | |
2c63a960 JB |
175 | const char hpacc_vtbl_ptr_name[] = "__vfp"; |
176 | const char hpacc_vtbl_ptr_type_name[] = "__vftyp"; | |
c906108c SS |
177 | |
178 | ||
179 | /* Return truth value for assertion that TYPE is of the type | |
180 | "pointer to virtual function". */ | |
181 | ||
182 | int | |
fba45db2 | 183 | cp_is_vtbl_ptr_type (struct type *type) |
c906108c SS |
184 | { |
185 | char *typename = type_name_no_tag (type); | |
186 | ||
187 | return (typename != NULL | |
188 | && (STREQ (typename, vtbl_ptr_name) | |
189 | || STREQ (typename, vtbl_ptr_name_old))); | |
190 | } | |
191 | ||
192 | /* Return truth value for the assertion that TYPE is of the type | |
193 | "pointer to virtual function table". */ | |
194 | ||
195 | int | |
fba45db2 | 196 | cp_is_vtbl_member (struct type *type) |
c906108c | 197 | { |
0e5e3ea6 PS |
198 | /* With older versions of g++, the vtbl field pointed to an array |
199 | of structures. Nowadays it points directly to the structure. */ | |
c906108c SS |
200 | if (TYPE_CODE (type) == TYPE_CODE_PTR) |
201 | { | |
202 | type = TYPE_TARGET_TYPE (type); | |
203 | if (TYPE_CODE (type) == TYPE_CODE_ARRAY) | |
204 | { | |
205 | type = TYPE_TARGET_TYPE (type); | |
c5aa993b JM |
206 | if (TYPE_CODE (type) == TYPE_CODE_STRUCT /* if not using thunks */ |
207 | || TYPE_CODE (type) == TYPE_CODE_PTR) /* if using thunks */ | |
c906108c SS |
208 | { |
209 | /* Virtual functions tables are full of pointers | |
c5aa993b | 210 | to virtual functions. */ |
c906108c SS |
211 | return cp_is_vtbl_ptr_type (type); |
212 | } | |
213 | } | |
0e5e3ea6 PS |
214 | else if (TYPE_CODE (type) == TYPE_CODE_STRUCT) /* if not using thunks */ |
215 | { | |
216 | return cp_is_vtbl_ptr_type (type); | |
217 | } | |
218 | else if (TYPE_CODE (type) == TYPE_CODE_PTR) /* if using thunks */ | |
219 | { | |
220 | /* The type name of the thunk pointer is NULL when using dwarf2. | |
221 | We could test for a pointer to a function, but there is | |
222 | no type info for the virtual table either, so it wont help. */ | |
223 | return cp_is_vtbl_ptr_type (type); | |
224 | } | |
c906108c SS |
225 | } |
226 | return 0; | |
227 | } | |
228 | ||
229 | /* Mutually recursive subroutines of cp_print_value and c_val_print to | |
230 | print out a structure's fields: cp_print_value_fields and cp_print_value. | |
c5aa993b | 231 | |
c906108c SS |
232 | TYPE, VALADDR, ADDRESS, STREAM, RECURSE, and PRETTY have the |
233 | same meanings as in cp_print_value and c_val_print. | |
234 | ||
235 | 2nd argument REAL_TYPE is used to carry over the type of the derived | |
236 | class across the recursion to base classes. | |
237 | ||
238 | DONT_PRINT is an array of baseclass types that we | |
239 | should not print, or zero if called from top level. */ | |
240 | ||
241 | void | |
fba45db2 KB |
242 | cp_print_value_fields (struct type *type, struct type *real_type, char *valaddr, |
243 | int offset, CORE_ADDR address, struct ui_file *stream, | |
244 | int format, int recurse, enum val_prettyprint pretty, | |
245 | struct type **dont_print_vb, int dont_print_statmem) | |
c906108c SS |
246 | { |
247 | int i, len, n_baseclasses; | |
248 | struct obstack tmp_obstack; | |
249 | char *last_dont_print = obstack_next_free (&dont_print_statmem_obstack); | |
250 | int fields_seen = 0; | |
251 | ||
252 | CHECK_TYPEDEF (type); | |
253 | ||
254 | fprintf_filtered (stream, "{"); | |
255 | len = TYPE_NFIELDS (type); | |
256 | n_baseclasses = TYPE_N_BASECLASSES (type); | |
257 | ||
258 | /* First, print out baseclasses such that we don't print | |
259 | duplicates of virtual baseclasses. */ | |
260 | ||
261 | if (n_baseclasses > 0) | |
262 | cp_print_value (type, real_type, valaddr, offset, address, stream, | |
c5aa993b | 263 | format, recurse + 1, pretty, dont_print_vb); |
c906108c SS |
264 | |
265 | /* Second, print out data fields */ | |
266 | ||
267 | /* If there are no data fields, or if the only field is the | |
c5aa993b | 268 | * vtbl pointer, skip this part */ |
2c63a960 JB |
269 | if ((len == n_baseclasses) |
270 | || ((len - n_baseclasses == 1) | |
271 | && TYPE_HAS_VTABLE (type) | |
272 | && STREQN (TYPE_FIELD_NAME (type, n_baseclasses), | |
273 | hpacc_vtbl_ptr_name, 5)) | |
274 | || !len) | |
c906108c SS |
275 | fprintf_filtered (stream, "<No data fields>"); |
276 | else | |
277 | { | |
278 | extern int inspect_it; | |
279 | ||
280 | if (dont_print_statmem == 0) | |
281 | { | |
282 | /* If we're at top level, carve out a completely fresh | |
283 | chunk of the obstack and use that until this particular | |
284 | invocation returns. */ | |
285 | tmp_obstack = dont_print_statmem_obstack; | |
286 | obstack_finish (&dont_print_statmem_obstack); | |
287 | } | |
288 | ||
289 | for (i = n_baseclasses; i < len; i++) | |
290 | { | |
291 | /* If requested, skip printing of static fields. */ | |
292 | if (!static_field_print && TYPE_FIELD_STATIC (type, i)) | |
293 | continue; | |
294 | ||
c5aa993b | 295 | /* If a vtable pointer appears, we'll print it out later */ |
2c63a960 JB |
296 | if (TYPE_HAS_VTABLE (type) |
297 | && STREQN (TYPE_FIELD_NAME (type, i), hpacc_vtbl_ptr_name, 5)) | |
c5aa993b JM |
298 | continue; |
299 | ||
c906108c SS |
300 | if (fields_seen) |
301 | fprintf_filtered (stream, ", "); | |
302 | else if (n_baseclasses > 0) | |
303 | { | |
304 | if (pretty) | |
305 | { | |
306 | fprintf_filtered (stream, "\n"); | |
307 | print_spaces_filtered (2 + 2 * recurse, stream); | |
308 | fputs_filtered ("members of ", stream); | |
309 | fputs_filtered (type_name_no_tag (type), stream); | |
310 | fputs_filtered (": ", stream); | |
311 | } | |
312 | } | |
313 | fields_seen = 1; | |
314 | ||
315 | if (pretty) | |
316 | { | |
317 | fprintf_filtered (stream, "\n"); | |
318 | print_spaces_filtered (2 + 2 * recurse, stream); | |
319 | } | |
c5aa993b | 320 | else |
c906108c SS |
321 | { |
322 | wrap_here (n_spaces (2 + 2 * recurse)); | |
323 | } | |
324 | if (inspect_it) | |
325 | { | |
326 | if (TYPE_CODE (TYPE_FIELD_TYPE (type, i)) == TYPE_CODE_PTR) | |
327 | fputs_filtered ("\"( ptr \"", stream); | |
328 | else | |
329 | fputs_filtered ("\"( nodef \"", stream); | |
330 | if (TYPE_FIELD_STATIC (type, i)) | |
331 | fputs_filtered ("static ", stream); | |
332 | fprintf_symbol_filtered (stream, TYPE_FIELD_NAME (type, i), | |
333 | language_cplus, | |
334 | DMGL_PARAMS | DMGL_ANSI); | |
335 | fputs_filtered ("\" \"", stream); | |
336 | fprintf_symbol_filtered (stream, TYPE_FIELD_NAME (type, i), | |
337 | language_cplus, | |
338 | DMGL_PARAMS | DMGL_ANSI); | |
339 | fputs_filtered ("\") \"", stream); | |
340 | } | |
341 | else | |
342 | { | |
343 | annotate_field_begin (TYPE_FIELD_TYPE (type, i)); | |
344 | ||
345 | if (TYPE_FIELD_STATIC (type, i)) | |
346 | fputs_filtered ("static ", stream); | |
347 | fprintf_symbol_filtered (stream, TYPE_FIELD_NAME (type, i), | |
348 | language_cplus, | |
349 | DMGL_PARAMS | DMGL_ANSI); | |
350 | annotate_field_name_end (); | |
351 | /* do not print leading '=' in case of anonymous unions */ | |
352 | if (strcmp (TYPE_FIELD_NAME (type, i), "")) | |
353 | fputs_filtered (" = ", stream); | |
354 | annotate_field_value (); | |
355 | } | |
356 | ||
357 | if (!TYPE_FIELD_STATIC (type, i) && TYPE_FIELD_PACKED (type, i)) | |
358 | { | |
6943961c | 359 | struct value *v; |
c906108c SS |
360 | |
361 | /* Bitfields require special handling, especially due to byte | |
c5aa993b | 362 | order problems. */ |
c906108c SS |
363 | if (TYPE_FIELD_IGNORE (type, i)) |
364 | { | |
c5aa993b | 365 | fputs_filtered ("<optimized out or zero length>", stream); |
c906108c SS |
366 | } |
367 | else | |
368 | { | |
2c63a960 JB |
369 | v = value_from_longest |
370 | (TYPE_FIELD_TYPE (type, i), | |
371 | unpack_field_as_long (type, valaddr + offset, i)); | |
c906108c | 372 | |
2c63a960 JB |
373 | val_print (TYPE_FIELD_TYPE (type, i), VALUE_CONTENTS (v), |
374 | 0, 0, stream, format, 0, recurse + 1, pretty); | |
c906108c SS |
375 | } |
376 | } | |
377 | else | |
378 | { | |
379 | if (TYPE_FIELD_IGNORE (type, i)) | |
380 | { | |
c5aa993b | 381 | fputs_filtered ("<optimized out or zero length>", stream); |
c906108c SS |
382 | } |
383 | else if (TYPE_FIELD_STATIC (type, i)) | |
384 | { | |
6943961c | 385 | struct value *v = value_static_field (type, i); |
c906108c SS |
386 | if (v == NULL) |
387 | fputs_filtered ("<optimized out>", stream); | |
388 | else | |
389 | cp_print_static_field (TYPE_FIELD_TYPE (type, i), v, | |
390 | stream, format, recurse + 1, | |
391 | pretty); | |
392 | } | |
393 | else | |
394 | { | |
c5aa993b | 395 | val_print (TYPE_FIELD_TYPE (type, i), |
2c63a960 | 396 | valaddr, offset + TYPE_FIELD_BITPOS (type, i) / 8, |
c5aa993b JM |
397 | address + TYPE_FIELD_BITPOS (type, i) / 8, |
398 | stream, format, 0, recurse + 1, pretty); | |
c906108c SS |
399 | } |
400 | } | |
401 | annotate_field_end (); | |
402 | } | |
403 | ||
404 | if (dont_print_statmem == 0) | |
405 | { | |
406 | /* Free the space used to deal with the printing | |
407 | of the members from top level. */ | |
408 | obstack_free (&dont_print_statmem_obstack, last_dont_print); | |
409 | dont_print_statmem_obstack = tmp_obstack; | |
410 | } | |
411 | ||
412 | if (pretty) | |
413 | { | |
414 | fprintf_filtered (stream, "\n"); | |
415 | print_spaces_filtered (2 * recurse, stream); | |
416 | } | |
c5aa993b JM |
417 | } /* if there are data fields */ |
418 | /* Now print out the virtual table pointer if there is one */ | |
2c63a960 JB |
419 | if (TYPE_HAS_VTABLE (type) |
420 | && STREQN (TYPE_FIELD_NAME (type, n_baseclasses), | |
421 | hpacc_vtbl_ptr_name, | |
422 | 5)) | |
c906108c | 423 | { |
6943961c | 424 | struct value *v; |
c5aa993b | 425 | /* First get the virtual table pointer and print it out */ |
c906108c SS |
426 | |
427 | #if 0 | |
428 | fputs_filtered ("__vfp = ", stream); | |
429 | #endif | |
430 | ||
431 | fputs_filtered (", Virtual table at ", stream); | |
432 | ||
433 | /* pai: FIXME 32x64 problem? */ | |
434 | /* Not sure what the best notation is in the case where there is no | |
435 | baseclass name. */ | |
4478b372 | 436 | v = value_from_pointer (lookup_pointer_type (builtin_type_unsigned_long), |
c5aa993b | 437 | *(unsigned long *) (valaddr + offset)); |
c906108c SS |
438 | |
439 | val_print (VALUE_TYPE (v), VALUE_CONTENTS (v), 0, 0, | |
c5aa993b | 440 | stream, format, 0, recurse + 1, pretty); |
c906108c SS |
441 | fields_seen = 1; |
442 | ||
443 | if (vtblprint) | |
c5aa993b JM |
444 | { |
445 | /* Print out function pointers in vtable. */ | |
c906108c | 446 | |
c5aa993b JM |
447 | /* FIXME: then-clause is for non-RRBC layout of virtual |
448 | * table. The RRBC case in the else-clause is yet to be | |
449 | * implemented. The if (1) below should be changed to a | |
450 | * test for whether the executable we have was compiled | |
451 | * with a version of HP aCC that doesn't have RRBC | |
452 | * support. */ | |
c906108c | 453 | |
c5aa993b JM |
454 | if (1) |
455 | { | |
2c63a960 JB |
456 | /* no RRBC support; function pointers embedded directly |
457 | in vtable */ | |
c906108c | 458 | |
c5aa993b | 459 | int vfuncs = count_virtual_fns (real_type); |
c906108c | 460 | |
c5aa993b | 461 | fputs_filtered (" {", stream); |
c906108c | 462 | |
c5aa993b | 463 | /* FIXME : doesn't work at present */ |
c906108c | 464 | #if 0 |
2c63a960 JB |
465 | fprintf_filtered (stream, "%d entr%s: ", vfuncs, |
466 | vfuncs == 1 ? "y" : "ies"); | |
c906108c | 467 | #else |
c5aa993b | 468 | fputs_filtered ("not implemented", stream); |
c906108c SS |
469 | |
470 | ||
471 | #endif | |
472 | ||
c5aa993b | 473 | /* recursive function that prints all virtual function entries */ |
c906108c | 474 | #if 0 |
2c63a960 JB |
475 | cp_print_hpacc_virtual_table_entries (real_type, &vfuncs, v, |
476 | stream, format, recurse, | |
477 | pretty); | |
c906108c | 478 | #endif |
c5aa993b JM |
479 | fputs_filtered ("}", stream); |
480 | } /* non-RRBC case */ | |
481 | else | |
482 | { | |
4d33f415 | 483 | /* FIXME -- see comments above */ |
c5aa993b JM |
484 | /* RRBC support present; function pointers are found |
485 | * by indirection through the class segment entries. */ | |
486 | ||
487 | ||
488 | } /* RRBC case */ | |
489 | } /* if vtblprint */ | |
c906108c SS |
490 | |
491 | if (pretty) | |
492 | { | |
493 | fprintf_filtered (stream, "\n"); | |
494 | print_spaces_filtered (2 * recurse, stream); | |
495 | } | |
496 | ||
c5aa993b JM |
497 | } /* if vtable exists */ |
498 | ||
c906108c SS |
499 | fprintf_filtered (stream, "}"); |
500 | } | |
501 | ||
502 | /* Special val_print routine to avoid printing multiple copies of virtual | |
503 | baseclasses. */ | |
504 | ||
505 | static void | |
fba45db2 KB |
506 | cp_print_value (struct type *type, struct type *real_type, char *valaddr, |
507 | int offset, CORE_ADDR address, struct ui_file *stream, | |
508 | int format, int recurse, enum val_prettyprint pretty, | |
509 | struct type **dont_print_vb) | |
c906108c SS |
510 | { |
511 | struct obstack tmp_obstack; | |
512 | struct type **last_dont_print | |
2c63a960 | 513 | = (struct type **) obstack_next_free (&dont_print_vb_obstack); |
c906108c | 514 | int i, n_baseclasses = TYPE_N_BASECLASSES (type); |
b9d652ac DJ |
515 | int thisoffset; |
516 | struct type *thistype; | |
c906108c SS |
517 | |
518 | if (dont_print_vb == 0) | |
519 | { | |
520 | /* If we're at top level, carve out a completely fresh | |
c5aa993b JM |
521 | chunk of the obstack and use that until this particular |
522 | invocation returns. */ | |
c906108c SS |
523 | tmp_obstack = dont_print_vb_obstack; |
524 | /* Bump up the high-water mark. Now alpha is omega. */ | |
525 | obstack_finish (&dont_print_vb_obstack); | |
526 | } | |
527 | ||
528 | for (i = 0; i < n_baseclasses; i++) | |
529 | { | |
530 | int boffset; | |
531 | int skip; | |
532 | struct type *baseclass = check_typedef (TYPE_BASECLASS (type, i)); | |
533 | char *basename = TYPE_NAME (baseclass); | |
534 | char *base_valaddr; | |
535 | ||
536 | if (BASETYPE_VIA_VIRTUAL (type, i)) | |
537 | { | |
538 | struct type **first_dont_print | |
2c63a960 | 539 | = (struct type **) obstack_base (&dont_print_vb_obstack); |
c906108c | 540 | |
c5aa993b | 541 | int j = (struct type **) obstack_next_free (&dont_print_vb_obstack) |
2c63a960 | 542 | - first_dont_print; |
c906108c SS |
543 | |
544 | while (--j >= 0) | |
545 | if (baseclass == first_dont_print[j]) | |
546 | goto flush_it; | |
547 | ||
548 | obstack_ptr_grow (&dont_print_vb_obstack, baseclass); | |
549 | } | |
550 | ||
b9d652ac DJ |
551 | thisoffset = offset; |
552 | thistype = real_type; | |
c906108c | 553 | if (TYPE_HAS_VTABLE (type) && BASETYPE_VIA_VIRTUAL (type, i)) |
c5aa993b JM |
554 | { |
555 | /* Assume HP/Taligent runtime convention */ | |
556 | find_rt_vbase_offset (type, TYPE_BASECLASS (type, i), | |
557 | valaddr, offset, &boffset, &skip); | |
558 | if (skip >= 0) | |
2c63a960 JB |
559 | error ("Virtual base class offset not found from vtable while" |
560 | " printing"); | |
c5aa993b JM |
561 | base_valaddr = valaddr; |
562 | } | |
c906108c | 563 | else |
c5aa993b | 564 | { |
2c63a960 JB |
565 | boffset = baseclass_offset (type, i, |
566 | valaddr + offset, | |
567 | address + offset); | |
c5aa993b | 568 | skip = ((boffset == -1) || (boffset + offset) < 0) ? 1 : -1; |
c906108c | 569 | |
c5aa993b JM |
570 | if (BASETYPE_VIA_VIRTUAL (type, i)) |
571 | { | |
2c63a960 JB |
572 | /* The virtual base class pointer might have been |
573 | clobbered by the user program. Make sure that it | |
574 | still points to a valid memory location. */ | |
c906108c | 575 | |
2c63a960 JB |
576 | if (boffset != -1 |
577 | && ((boffset + offset) < 0 | |
578 | || (boffset + offset) >= TYPE_LENGTH (type))) | |
c5aa993b | 579 | { |
34c0bd93 | 580 | /* FIXME (alloca): unsafe if baseclass is really really large. */ |
c5aa993b | 581 | base_valaddr = (char *) alloca (TYPE_LENGTH (baseclass)); |
b9d652ac | 582 | if (target_read_memory (address + offset + boffset, base_valaddr, |
c5aa993b JM |
583 | TYPE_LENGTH (baseclass)) != 0) |
584 | skip = 1; | |
b9d652ac DJ |
585 | thisoffset = 0; |
586 | boffset = 0; | |
587 | thistype = baseclass; | |
c5aa993b JM |
588 | } |
589 | else | |
590 | base_valaddr = valaddr; | |
591 | } | |
592 | else | |
593 | base_valaddr = valaddr; | |
c906108c SS |
594 | } |
595 | ||
596 | /* now do the printing */ | |
597 | if (pretty) | |
598 | { | |
599 | fprintf_filtered (stream, "\n"); | |
600 | print_spaces_filtered (2 * recurse, stream); | |
601 | } | |
602 | fputs_filtered ("<", stream); | |
603 | /* Not sure what the best notation is in the case where there is no | |
c5aa993b | 604 | baseclass name. */ |
c906108c SS |
605 | fputs_filtered (basename ? basename : "", stream); |
606 | fputs_filtered ("> = ", stream); | |
607 | ||
608 | ||
609 | if (skip >= 1) | |
610 | fprintf_filtered (stream, "<invalid address>"); | |
611 | else | |
b9d652ac DJ |
612 | cp_print_value_fields (baseclass, thistype, base_valaddr, |
613 | thisoffset + boffset, address, stream, format, | |
2c63a960 JB |
614 | recurse, pretty, |
615 | ((struct type **) | |
616 | obstack_base (&dont_print_vb_obstack)), | |
c906108c SS |
617 | 0); |
618 | fputs_filtered (", ", stream); | |
619 | ||
620 | flush_it: | |
621 | ; | |
622 | } | |
623 | ||
624 | if (dont_print_vb == 0) | |
625 | { | |
626 | /* Free the space used to deal with the printing | |
c5aa993b | 627 | of this type from top level. */ |
c906108c SS |
628 | obstack_free (&dont_print_vb_obstack, last_dont_print); |
629 | /* Reset watermark so that we can continue protecting | |
c5aa993b | 630 | ourselves from whatever we were protecting ourselves. */ |
c906108c SS |
631 | dont_print_vb_obstack = tmp_obstack; |
632 | } | |
633 | } | |
634 | ||
635 | /* Print value of a static member. | |
636 | To avoid infinite recursion when printing a class that contains | |
637 | a static instance of the class, we keep the addresses of all printed | |
638 | static member classes in an obstack and refuse to print them more | |
639 | than once. | |
640 | ||
641 | VAL contains the value to print, TYPE, STREAM, RECURSE, and PRETTY | |
642 | have the same meanings as in c_val_print. */ | |
643 | ||
644 | static void | |
2c63a960 | 645 | cp_print_static_field (struct type *type, |
6943961c | 646 | struct value *val, |
2c63a960 JB |
647 | struct ui_file *stream, |
648 | int format, | |
649 | int recurse, | |
650 | enum val_prettyprint pretty) | |
c906108c SS |
651 | { |
652 | if (TYPE_CODE (type) == TYPE_CODE_STRUCT) | |
653 | { | |
654 | CORE_ADDR *first_dont_print; | |
655 | int i; | |
656 | ||
657 | first_dont_print | |
c5aa993b JM |
658 | = (CORE_ADDR *) obstack_base (&dont_print_statmem_obstack); |
659 | i = (CORE_ADDR *) obstack_next_free (&dont_print_statmem_obstack) | |
c906108c SS |
660 | - first_dont_print; |
661 | ||
662 | while (--i >= 0) | |
663 | { | |
664 | if (VALUE_ADDRESS (val) == first_dont_print[i]) | |
665 | { | |
2c63a960 JB |
666 | fputs_filtered ("<same as static member of an already" |
667 | " seen type>", | |
c906108c SS |
668 | stream); |
669 | return; | |
670 | } | |
671 | } | |
672 | ||
673 | obstack_grow (&dont_print_statmem_obstack, (char *) &VALUE_ADDRESS (val), | |
674 | sizeof (CORE_ADDR)); | |
675 | ||
676 | CHECK_TYPEDEF (type); | |
2c63a960 JB |
677 | cp_print_value_fields (type, type, VALUE_CONTENTS_ALL (val), |
678 | VALUE_EMBEDDED_OFFSET (val), VALUE_ADDRESS (val), | |
c906108c SS |
679 | stream, format, recurse, pretty, NULL, 1); |
680 | return; | |
681 | } | |
2c63a960 JB |
682 | val_print (type, VALUE_CONTENTS_ALL (val), |
683 | VALUE_EMBEDDED_OFFSET (val), VALUE_ADDRESS (val), | |
c906108c SS |
684 | stream, format, 0, recurse, pretty); |
685 | } | |
686 | ||
687 | void | |
fba45db2 KB |
688 | cp_print_class_member (char *valaddr, struct type *domain, |
689 | struct ui_file *stream, char *prefix) | |
c906108c | 690 | { |
c5aa993b | 691 | |
c906108c SS |
692 | /* VAL is a byte offset into the structure type DOMAIN. |
693 | Find the name of the field for that offset and | |
694 | print it. */ | |
695 | int extra = 0; | |
696 | int bits = 0; | |
697 | register unsigned int i; | |
698 | unsigned len = TYPE_NFIELDS (domain); | |
699 | ||
700 | /* @@ Make VAL into bit offset */ | |
701 | ||
702 | /* Note: HP aCC generates offsets that are the real byte offsets added | |
703 | to a constant bias 0x20000000 (1 << 29). This constant bias gets | |
704 | shifted out in the code below -- joyous happenstance! */ | |
705 | ||
706 | /* Note: HP cfront uses a constant bias of 1; if we support this | |
c5aa993b JM |
707 | compiler ever, we will have to adjust the computation below */ |
708 | ||
c906108c SS |
709 | LONGEST val = unpack_long (builtin_type_int, valaddr) << 3; |
710 | for (i = TYPE_N_BASECLASSES (domain); i < len; i++) | |
711 | { | |
712 | int bitpos = TYPE_FIELD_BITPOS (domain, i); | |
713 | QUIT; | |
714 | if (val == bitpos) | |
715 | break; | |
716 | if (val < bitpos && i != 0) | |
717 | { | |
718 | /* Somehow pointing into a field. */ | |
719 | i -= 1; | |
720 | extra = (val - TYPE_FIELD_BITPOS (domain, i)); | |
721 | if (extra & 0x7) | |
722 | bits = 1; | |
723 | else | |
724 | extra >>= 3; | |
725 | break; | |
726 | } | |
727 | } | |
728 | if (i < len) | |
729 | { | |
730 | char *name; | |
731 | fprintf_filtered (stream, prefix); | |
732 | name = type_name_no_tag (domain); | |
733 | if (name) | |
c5aa993b | 734 | fputs_filtered (name, stream); |
c906108c SS |
735 | else |
736 | c_type_print_base (domain, stream, 0, 0); | |
737 | fprintf_filtered (stream, "::"); | |
738 | fputs_filtered (TYPE_FIELD_NAME (domain, i), stream); | |
739 | if (extra) | |
740 | fprintf_filtered (stream, " + %d bytes", extra); | |
741 | if (bits) | |
742 | fprintf_filtered (stream, " (offset in bits)"); | |
743 | } | |
744 | else | |
d4f3574e | 745 | fprintf_filtered (stream, "%ld", (long) (val >> 3)); |
c906108c SS |
746 | } |
747 | ||
748 | ||
749 | /* This function prints out virtual table entries for a class; it | |
750 | * recurses on the base classes to find all virtual functions | |
751 | * available in a class. | |
752 | * | |
753 | * pai/1997-05-21 Note: As the name suggests, it's currently | |
754 | * implemented for HP aCC runtime only. g++ objects are handled | |
755 | * differently and I have made no attempt to fold that logic in | |
756 | * here. The runtime layout is different for the two cases. Also, | |
757 | * this currently has only the code for non-RRBC layouts generated by | |
758 | * the HP aCC compiler; RRBC code is stubbed out and will have to be | |
759 | * added later. */ | |
c5aa993b | 760 | |
c906108c SS |
761 | |
762 | static void | |
fba45db2 | 763 | cp_print_hpacc_virtual_table_entries (struct type *type, int *vfuncs, |
6943961c | 764 | struct value *v, struct ui_file *stream, |
fba45db2 KB |
765 | int format, int recurse, |
766 | enum val_prettyprint pretty) | |
c906108c SS |
767 | { |
768 | int fn, oi; | |
769 | ||
770 | /* pai: FIXME this function doesn't work. It should handle a given | |
771 | * virtual function only once (latest redefinition in class hierarchy) | |
772 | */ | |
773 | ||
c5aa993b JM |
774 | /* Recursion on other classes that can share the same vtable */ |
775 | struct type *pbc = primary_base_class (type); | |
c906108c | 776 | if (pbc) |
2c63a960 JB |
777 | cp_print_hpacc_virtual_table_entries (pbc, vfuncs, v, stream, format, |
778 | recurse, pretty); | |
c5aa993b | 779 | |
c906108c SS |
780 | /* Now deal with vfuncs declared in this class */ |
781 | for (fn = 0; fn < TYPE_NFN_FIELDS (type); fn++) | |
782 | for (oi = 0; oi < TYPE_FN_FIELDLIST_LENGTH (type, fn); oi++) | |
783 | if (TYPE_FN_FIELD_VIRTUAL_P (TYPE_FN_FIELDLIST1 (type, fn), oi)) | |
c5aa993b JM |
784 | { |
785 | char *vf_name; | |
2c63a960 | 786 | const char *field_physname; |
c5aa993b JM |
787 | |
788 | /* virtual function offset */ | |
2c63a960 JB |
789 | int vx = (TYPE_FN_FIELD_VOFFSET (TYPE_FN_FIELDLIST1 (type, fn), oi) |
790 | - 1); | |
c5aa993b JM |
791 | |
792 | /* Get the address of the vfunction entry */ | |
6943961c | 793 | struct value *vf = value_copy (v); |
c5aa993b JM |
794 | if (VALUE_LAZY (vf)) |
795 | (void) value_fetch_lazy (vf); | |
2c63a960 JB |
796 | /* adjust by offset */ |
797 | vf->aligner.contents[0] += 4 * (HP_ACC_VFUNC_START + vx); | |
c5aa993b JM |
798 | vf = value_ind (vf); /* get the entry */ |
799 | VALUE_TYPE (vf) = VALUE_TYPE (v); /* make it a pointer */ | |
800 | ||
801 | /* print out the entry */ | |
802 | val_print (VALUE_TYPE (vf), VALUE_CONTENTS (vf), 0, 0, | |
803 | stream, format, 0, recurse + 1, pretty); | |
2c63a960 JB |
804 | field_physname |
805 | = TYPE_FN_FIELD_PHYSNAME (TYPE_FN_FIELDLIST1 (type, fn), oi); | |
806 | /* pai: (temp) FIXME Maybe this should be DMGL_ANSI */ | |
807 | vf_name = cplus_demangle (field_physname, DMGL_ARM); | |
c5aa993b JM |
808 | fprintf_filtered (stream, " %s", vf_name); |
809 | if (--(*vfuncs) > 0) | |
810 | fputs_filtered (", ", stream); | |
811 | } | |
c906108c SS |
812 | } |
813 | ||
814 | ||
815 | ||
816 | void | |
fba45db2 | 817 | _initialize_cp_valprint (void) |
c906108c SS |
818 | { |
819 | add_show_from_set | |
820 | (add_set_cmd ("static-members", class_support, var_boolean, | |
c5aa993b | 821 | (char *) &static_field_print, |
c906108c SS |
822 | "Set printing of C++ static members.", |
823 | &setprintlist), | |
824 | &showprintlist); | |
825 | /* Turn on printing of static fields. */ | |
826 | static_field_print = 1; | |
827 | ||
828 | add_show_from_set | |
c5aa993b | 829 | (add_set_cmd ("vtbl", class_support, var_boolean, (char *) &vtblprint, |
c906108c SS |
830 | "Set printing of C++ virtual function tables.", |
831 | &setprintlist), | |
832 | &showprintlist); | |
833 | ||
834 | add_show_from_set | |
c5aa993b JM |
835 | (add_set_cmd ("object", class_support, var_boolean, (char *) &objectprint, |
836 | "Set printing of object's derived type based on vtable info.", | |
c906108c SS |
837 | &setprintlist), |
838 | &showprintlist); | |
839 | ||
840 | /* Give people the defaults which they are used to. */ | |
841 | objectprint = 0; | |
842 | vtblprint = 0; | |
843 | obstack_begin (&dont_print_vb_obstack, 32 * sizeof (struct type *)); | |
844 | obstack_specify_allocation (&dont_print_statmem_obstack, | |
845 | 32 * sizeof (CORE_ADDR), sizeof (CORE_ADDR), | |
b8c9b27d | 846 | xmalloc, xfree); |
c906108c | 847 | } |