]>
Commit | Line | Data |
---|---|---|
015a42b4 | 1 | /* Generic code for supporting multiple C++ ABI's |
06c4d4dc | 2 | |
42a4f53d | 3 | Copyright (C) 2001-2019 Free Software Foundation, Inc. |
015a42b4 JB |
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 |
015a42b4 JB |
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/>. */ |
015a42b4 JB |
19 | |
20 | #include "defs.h" | |
d55e5aa6 TT |
21 | |
22 | /* Local non-gdb includes. */ | |
fe1f4a5e | 23 | #include "command.h" |
d55e5aa6 | 24 | #include "cp-abi.h" |
fe1f4a5e DJ |
25 | #include "gdbcmd.h" |
26 | #include "ui-out.h" | |
d55e5aa6 TT |
27 | #include "value.h" |
28 | ||
fe1f4a5e | 29 | static struct cp_abi_ops *find_cp_abi (const char *short_name); |
015a42b4 | 30 | |
fe1f4a5e DJ |
31 | static struct cp_abi_ops current_cp_abi = { "", NULL }; |
32 | static struct cp_abi_ops auto_cp_abi = { "auto", NULL }; | |
015a42b4 | 33 | |
fe1f4a5e DJ |
34 | #define CP_ABI_MAX 8 |
35 | static struct cp_abi_ops *cp_abis[CP_ABI_MAX]; | |
36 | static int num_cp_abis = 0; | |
015a42b4 JB |
37 | |
38 | enum ctor_kinds | |
39 | is_constructor_name (const char *name) | |
40 | { | |
41 | if ((current_cp_abi.is_constructor_name) == NULL) | |
8a3fe4f8 | 42 | error (_("ABI doesn't define required function is_constructor_name")); |
015a42b4 JB |
43 | return (*current_cp_abi.is_constructor_name) (name); |
44 | } | |
45 | ||
46 | enum dtor_kinds | |
47 | is_destructor_name (const char *name) | |
48 | { | |
49 | if ((current_cp_abi.is_destructor_name) == NULL) | |
8a3fe4f8 | 50 | error (_("ABI doesn't define required function is_destructor_name")); |
015a42b4 JB |
51 | return (*current_cp_abi.is_destructor_name) (name); |
52 | } | |
53 | ||
54 | int | |
55 | is_vtable_name (const char *name) | |
56 | { | |
57 | if ((current_cp_abi.is_vtable_name) == NULL) | |
8a3fe4f8 | 58 | error (_("ABI doesn't define required function is_vtable_name")); |
015a42b4 JB |
59 | return (*current_cp_abi.is_vtable_name) (name); |
60 | } | |
61 | ||
62 | int | |
63 | is_operator_name (const char *name) | |
64 | { | |
65 | if ((current_cp_abi.is_operator_name) == NULL) | |
8a3fe4f8 | 66 | error (_("ABI doesn't define required function is_operator_name")); |
015a42b4 JB |
67 | return (*current_cp_abi.is_operator_name) (name); |
68 | } | |
69 | ||
1514d34e | 70 | int |
8af8e3bc | 71 | baseclass_offset (struct type *type, int index, const gdb_byte *valaddr, |
6b850546 | 72 | LONGEST embedded_offset, CORE_ADDR address, |
8af8e3bc | 73 | const struct value *val) |
1514d34e | 74 | { |
8af8e3bc PA |
75 | int res = 0; |
76 | ||
77 | gdb_assert (current_cp_abi.baseclass_offset != NULL); | |
78 | ||
492d29ea | 79 | TRY |
8af8e3bc PA |
80 | { |
81 | res = (*current_cp_abi.baseclass_offset) (type, index, valaddr, | |
82 | embedded_offset, | |
83 | address, val); | |
84 | } | |
492d29ea | 85 | CATCH (ex, RETURN_MASK_ERROR) |
7556d4a4 PA |
86 | { |
87 | if (ex.error != NOT_AVAILABLE_ERROR) | |
88 | throw_exception (ex); | |
89 | ||
90 | throw_error (NOT_AVAILABLE_ERROR, | |
91 | _("Cannot determine virtual baseclass offset " | |
92 | "of incomplete object")); | |
93 | } | |
492d29ea | 94 | END_CATCH |
8af8e3bc | 95 | |
7556d4a4 | 96 | return res; |
1514d34e DJ |
97 | } |
98 | ||
e933e538 | 99 | struct value * |
aff410f1 MS |
100 | value_virtual_fn_field (struct value **arg1p, |
101 | struct fn_field *f, int j, | |
fe1f4a5e | 102 | struct type *type, int offset) |
015a42b4 JB |
103 | { |
104 | if ((current_cp_abi.virtual_fn_field) == NULL) | |
105 | return NULL; | |
aff410f1 MS |
106 | return (*current_cp_abi.virtual_fn_field) (arg1p, f, j, |
107 | type, offset); | |
015a42b4 | 108 | } |
1514d34e | 109 | |
015a42b4 | 110 | struct type * |
aff410f1 | 111 | value_rtti_type (struct value *v, int *full, |
6b850546 | 112 | LONGEST *top, int *using_enc) |
015a42b4 | 113 | { |
7093c834 | 114 | struct type *ret = NULL; |
c5504eaf | 115 | |
015a42b4 JB |
116 | if ((current_cp_abi.rtti_type) == NULL) |
117 | return NULL; | |
492d29ea | 118 | TRY |
7093c834 PP |
119 | { |
120 | ret = (*current_cp_abi.rtti_type) (v, full, top, using_enc); | |
121 | } | |
492d29ea PA |
122 | CATCH (e, RETURN_MASK_ERROR) |
123 | { | |
124 | return NULL; | |
125 | } | |
126 | END_CATCH | |
127 | ||
7093c834 | 128 | return ret; |
015a42b4 JB |
129 | } |
130 | ||
0d5de010 | 131 | void |
aff410f1 MS |
132 | cplus_print_method_ptr (const gdb_byte *contents, |
133 | struct type *type, | |
0d5de010 DJ |
134 | struct ui_file *stream) |
135 | { | |
136 | if (current_cp_abi.print_method_ptr == NULL) | |
137 | error (_("GDB does not support pointers to methods on this target")); | |
138 | (*current_cp_abi.print_method_ptr) (contents, type, stream); | |
139 | } | |
140 | ||
141 | int | |
ad4820ab | 142 | cplus_method_ptr_size (struct type *to_type) |
0d5de010 DJ |
143 | { |
144 | if (current_cp_abi.method_ptr_size == NULL) | |
145 | error (_("GDB does not support pointers to methods on this target")); | |
ad4820ab | 146 | return (*current_cp_abi.method_ptr_size) (to_type); |
0d5de010 DJ |
147 | } |
148 | ||
149 | void | |
ad4820ab UW |
150 | cplus_make_method_ptr (struct type *type, gdb_byte *contents, |
151 | CORE_ADDR value, int is_virtual) | |
0d5de010 DJ |
152 | { |
153 | if (current_cp_abi.make_method_ptr == NULL) | |
154 | error (_("GDB does not support pointers to methods on this target")); | |
ad4820ab | 155 | (*current_cp_abi.make_method_ptr) (type, contents, value, is_virtual); |
0d5de010 DJ |
156 | } |
157 | ||
b18be20d | 158 | CORE_ADDR |
aff410f1 MS |
159 | cplus_skip_trampoline (struct frame_info *frame, |
160 | CORE_ADDR stop_pc) | |
b18be20d DJ |
161 | { |
162 | if (current_cp_abi.skip_trampoline == NULL) | |
163 | return 0; | |
52f729a7 | 164 | return (*current_cp_abi.skip_trampoline) (frame, stop_pc); |
b18be20d DJ |
165 | } |
166 | ||
0d5de010 | 167 | struct value * |
aff410f1 MS |
168 | cplus_method_ptr_to_value (struct value **this_p, |
169 | struct value *method_ptr) | |
0d5de010 DJ |
170 | { |
171 | if (current_cp_abi.method_ptr_to_value == NULL) | |
172 | error (_("GDB does not support pointers to methods on this target")); | |
173 | return (*current_cp_abi.method_ptr_to_value) (this_p, method_ptr); | |
174 | } | |
175 | ||
c4aeac85 TT |
176 | /* See cp-abi.h. */ |
177 | ||
178 | void | |
179 | cplus_print_vtable (struct value *value) | |
180 | { | |
181 | if (current_cp_abi.print_vtable == NULL) | |
182 | error (_("GDB cannot print the vtable on this target")); | |
843e694d | 183 | (*current_cp_abi.print_vtable) (value); |
c4aeac85 TT |
184 | } |
185 | ||
6e72ca20 TT |
186 | /* See cp-abi.h. */ |
187 | ||
188 | struct value * | |
189 | cplus_typeid (struct value *value) | |
190 | { | |
191 | if (current_cp_abi.get_typeid == NULL) | |
192 | error (_("GDB cannot find the typeid on this target")); | |
193 | return (*current_cp_abi.get_typeid) (value); | |
194 | } | |
195 | ||
196 | /* See cp-abi.h. */ | |
197 | ||
198 | struct type * | |
199 | cplus_typeid_type (struct gdbarch *gdbarch) | |
200 | { | |
201 | if (current_cp_abi.get_typeid_type == NULL) | |
202 | error (_("GDB cannot find the type for 'typeid' on this target")); | |
203 | return (*current_cp_abi.get_typeid_type) (gdbarch); | |
204 | } | |
205 | ||
72f1fe8a TT |
206 | /* See cp-abi.h. */ |
207 | ||
208 | struct type * | |
209 | cplus_type_from_type_info (struct value *value) | |
210 | { | |
211 | if (current_cp_abi.get_type_from_type_info == NULL) | |
212 | error (_("GDB cannot find the type from a std::type_info on this target")); | |
213 | return (*current_cp_abi.get_type_from_type_info) (value); | |
214 | } | |
215 | ||
cc16e6c9 TT |
216 | /* See cp-abi.h. */ |
217 | ||
2f408ecb | 218 | std::string |
cc16e6c9 TT |
219 | cplus_typename_from_type_info (struct value *value) |
220 | { | |
221 | if (current_cp_abi.get_typename_from_type_info == NULL) | |
222 | error (_("GDB cannot find the type name " | |
223 | "from a std::type_info on this target")); | |
224 | return (*current_cp_abi.get_typename_from_type_info) (value); | |
225 | } | |
226 | ||
41f1b697 DJ |
227 | int |
228 | cp_pass_by_reference (struct type *type) | |
229 | { | |
230 | if ((current_cp_abi.pass_by_reference) == NULL) | |
231 | return 0; | |
232 | return (*current_cp_abi.pass_by_reference) (type); | |
233 | } | |
234 | ||
fe1f4a5e DJ |
235 | /* Set the current C++ ABI to SHORT_NAME. */ |
236 | ||
237 | static int | |
238 | switch_to_cp_abi (const char *short_name) | |
239 | { | |
240 | struct cp_abi_ops *abi; | |
241 | ||
242 | abi = find_cp_abi (short_name); | |
243 | if (abi == NULL) | |
244 | return 0; | |
245 | ||
246 | current_cp_abi = *abi; | |
247 | return 1; | |
248 | } | |
249 | ||
250 | /* Add ABI to the list of supported C++ ABI's. */ | |
251 | ||
015a42b4 | 252 | int |
fe1f4a5e | 253 | register_cp_abi (struct cp_abi_ops *abi) |
015a42b4 | 254 | { |
fe1f4a5e DJ |
255 | if (num_cp_abis == CP_ABI_MAX) |
256 | internal_error (__FILE__, __LINE__, | |
3e43a32a MS |
257 | _("Too many C++ ABIs, please increase " |
258 | "CP_ABI_MAX in cp-abi.c")); | |
fe1f4a5e | 259 | |
015a42b4 JB |
260 | cp_abis[num_cp_abis++] = abi; |
261 | ||
262 | return 1; | |
fe1f4a5e DJ |
263 | } |
264 | ||
265 | /* Set the ABI to use in "auto" mode to SHORT_NAME. */ | |
015a42b4 | 266 | |
fe1f4a5e DJ |
267 | void |
268 | set_cp_abi_as_auto_default (const char *short_name) | |
269 | { | |
270 | char *new_longname, *new_doc; | |
271 | struct cp_abi_ops *abi = find_cp_abi (short_name); | |
272 | ||
273 | if (abi == NULL) | |
274 | internal_error (__FILE__, __LINE__, | |
e2e0b3e5 | 275 | _("Cannot find C++ ABI \"%s\" to set it as auto default."), |
fe1f4a5e DJ |
276 | short_name); |
277 | ||
278 | if (auto_cp_abi.longname != NULL) | |
279 | xfree ((char *) auto_cp_abi.longname); | |
280 | if (auto_cp_abi.doc != NULL) | |
281 | xfree ((char *) auto_cp_abi.doc); | |
282 | ||
283 | auto_cp_abi = *abi; | |
284 | ||
285 | auto_cp_abi.shortname = "auto"; | |
0dfdb8ba | 286 | new_longname = xstrprintf ("currently \"%s\"", abi->shortname); |
fe1f4a5e DJ |
287 | auto_cp_abi.longname = new_longname; |
288 | ||
0dfdb8ba | 289 | new_doc = xstrprintf ("Automatically selected; currently \"%s\"", |
049742da | 290 | abi->shortname); |
fe1f4a5e DJ |
291 | auto_cp_abi.doc = new_doc; |
292 | ||
293 | /* Since we copy the current ABI into current_cp_abi instead of | |
294 | using a pointer, if auto is currently the default, we need to | |
295 | reset it. */ | |
296 | if (strcmp (current_cp_abi.shortname, "auto") == 0) | |
297 | switch_to_cp_abi ("auto"); | |
015a42b4 JB |
298 | } |
299 | ||
fe1f4a5e DJ |
300 | /* Return the ABI operations associated with SHORT_NAME. */ |
301 | ||
302 | static struct cp_abi_ops * | |
303 | find_cp_abi (const char *short_name) | |
015a42b4 JB |
304 | { |
305 | int i; | |
fe1f4a5e | 306 | |
015a42b4 | 307 | for (i = 0; i < num_cp_abis; i++) |
fe1f4a5e DJ |
308 | if (strcmp (cp_abis[i]->shortname, short_name) == 0) |
309 | return cp_abis[i]; | |
310 | ||
311 | return NULL; | |
015a42b4 JB |
312 | } |
313 | ||
fe1f4a5e DJ |
314 | /* Display the list of registered C++ ABIs. */ |
315 | ||
316 | static void | |
317 | list_cp_abis (int from_tty) | |
318 | { | |
79a45e25 | 319 | struct ui_out *uiout = current_uiout; |
fe1f4a5e | 320 | int i; |
fe1f4a5e | 321 | |
112e8700 | 322 | uiout->text ("The available C++ ABIs are:\n"); |
2e783024 | 323 | ui_out_emit_tuple tuple_emitter (uiout, "cp-abi-list"); |
fe1f4a5e DJ |
324 | for (i = 0; i < num_cp_abis; i++) |
325 | { | |
326 | char pad[14]; | |
327 | int padcount; | |
328 | ||
112e8700 SM |
329 | uiout->text (" "); |
330 | uiout->field_string ("cp-abi", cp_abis[i]->shortname); | |
fe1f4a5e DJ |
331 | |
332 | padcount = 16 - 2 - strlen (cp_abis[i]->shortname); | |
333 | pad[padcount] = 0; | |
334 | while (padcount > 0) | |
335 | pad[--padcount] = ' '; | |
112e8700 | 336 | uiout->text (pad); |
fe1f4a5e | 337 | |
112e8700 SM |
338 | uiout->field_string ("doc", cp_abis[i]->doc); |
339 | uiout->text ("\n"); | |
fe1f4a5e | 340 | } |
fe1f4a5e DJ |
341 | } |
342 | ||
343 | /* Set the current C++ ABI, or display the list of options if no | |
344 | argument is given. */ | |
345 | ||
346 | static void | |
4ada038f | 347 | set_cp_abi_cmd (const char *args, int from_tty) |
fe1f4a5e DJ |
348 | { |
349 | if (args == NULL) | |
350 | { | |
351 | list_cp_abis (from_tty); | |
352 | return; | |
353 | } | |
354 | ||
355 | if (!switch_to_cp_abi (args)) | |
8a3fe4f8 | 356 | error (_("Could not find \"%s\" in ABI list"), args); |
fe1f4a5e DJ |
357 | } |
358 | ||
db2b2972 TT |
359 | /* A completion function for "set cp-abi". */ |
360 | ||
eb3ff9a5 | 361 | static void |
db2b2972 | 362 | cp_abi_completer (struct cmd_list_element *ignore, |
eb3ff9a5 | 363 | completion_tracker &tracker, |
6f937416 | 364 | const char *text, const char *word) |
db2b2972 TT |
365 | { |
366 | static const char **cp_abi_names; | |
367 | ||
368 | if (cp_abi_names == NULL) | |
369 | { | |
370 | int i; | |
371 | ||
372 | cp_abi_names = XNEWVEC (const char *, num_cp_abis + 1); | |
373 | for (i = 0; i < num_cp_abis; ++i) | |
374 | cp_abi_names[i] = cp_abis[i]->shortname; | |
6108433d | 375 | cp_abi_names[i] = NULL; |
db2b2972 TT |
376 | } |
377 | ||
eb3ff9a5 | 378 | complete_on_enum (tracker, cp_abi_names, text, word); |
db2b2972 TT |
379 | } |
380 | ||
fe1f4a5e DJ |
381 | /* Show the currently selected C++ ABI. */ |
382 | ||
383 | static void | |
4ada038f | 384 | show_cp_abi_cmd (const char *args, int from_tty) |
fe1f4a5e | 385 | { |
79a45e25 PA |
386 | struct ui_out *uiout = current_uiout; |
387 | ||
112e8700 | 388 | uiout->text ("The currently selected C++ ABI is \""); |
fe1f4a5e | 389 | |
112e8700 SM |
390 | uiout->field_string ("cp-abi", current_cp_abi.shortname); |
391 | uiout->text ("\" ("); | |
392 | uiout->field_string ("longname", current_cp_abi.longname); | |
393 | uiout->text (").\n"); | |
fe1f4a5e DJ |
394 | } |
395 | ||
396 | void | |
397 | _initialize_cp_abi (void) | |
398 | { | |
db2b2972 TT |
399 | struct cmd_list_element *c; |
400 | ||
fe1f4a5e DJ |
401 | register_cp_abi (&auto_cp_abi); |
402 | switch_to_cp_abi ("auto"); | |
403 | ||
db2b2972 | 404 | c = add_cmd ("cp-abi", class_obscure, set_cp_abi_cmd, _("\ |
ac74f770 MS |
405 | Set the ABI used for inspecting C++ objects.\n\ |
406 | \"set cp-abi\" with no arguments will list the available ABIs."), | |
db2b2972 TT |
407 | &setlist); |
408 | set_cmd_completer (c, cp_abi_completer); | |
fe1f4a5e DJ |
409 | |
410 | add_cmd ("cp-abi", class_obscure, show_cp_abi_cmd, | |
aff410f1 MS |
411 | _("Show the ABI used for inspecting C++ objects."), |
412 | &showlist); | |
fe1f4a5e | 413 | } |