1 /* Generic code for supporting multiple C++ ABI's
3 Copyright (C) 2001-2023 Free Software Foundation, Inc.
5 This file is part of GDB.
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
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
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.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
27 static struct cp_abi_ops
*find_cp_abi (const char *short_name
);
29 static struct cp_abi_ops current_cp_abi
= { "", NULL
};
30 static struct cp_abi_ops auto_cp_abi
= { "auto", NULL
};
33 static struct cp_abi_ops
*cp_abis
[CP_ABI_MAX
];
34 static int num_cp_abis
= 0;
37 is_constructor_name (const char *name
)
39 if ((current_cp_abi
.is_constructor_name
) == NULL
)
40 error (_("ABI doesn't define required function is_constructor_name"));
41 return (*current_cp_abi
.is_constructor_name
) (name
);
45 is_destructor_name (const char *name
)
47 if ((current_cp_abi
.is_destructor_name
) == NULL
)
48 error (_("ABI doesn't define required function is_destructor_name"));
49 return (*current_cp_abi
.is_destructor_name
) (name
);
53 is_vtable_name (const char *name
)
55 if ((current_cp_abi
.is_vtable_name
) == NULL
)
56 error (_("ABI doesn't define required function is_vtable_name"));
57 return (*current_cp_abi
.is_vtable_name
) (name
);
61 is_operator_name (const char *name
)
63 if ((current_cp_abi
.is_operator_name
) == NULL
)
64 error (_("ABI doesn't define required function is_operator_name"));
65 return (*current_cp_abi
.is_operator_name
) (name
);
69 baseclass_offset (struct type
*type
, int index
, const gdb_byte
*valaddr
,
70 LONGEST embedded_offset
, CORE_ADDR address
,
71 const struct value
*val
)
75 gdb_assert (current_cp_abi
.baseclass_offset
!= NULL
);
79 res
= (*current_cp_abi
.baseclass_offset
) (type
, index
, valaddr
,
83 catch (const gdb_exception_error
&ex
)
85 if (ex
.error
!= NOT_AVAILABLE_ERROR
)
88 throw_error (NOT_AVAILABLE_ERROR
,
89 _("Cannot determine virtual baseclass offset "
90 "of incomplete object"));
97 value_virtual_fn_field (struct value
**arg1p
,
98 struct fn_field
*f
, int j
,
99 struct type
*type
, int offset
)
101 if ((current_cp_abi
.virtual_fn_field
) == NULL
)
103 return (*current_cp_abi
.virtual_fn_field
) (arg1p
, f
, j
,
108 value_rtti_type (struct value
*v
, int *full
,
109 LONGEST
*top
, int *using_enc
)
111 struct type
*ret
= NULL
;
113 if ((current_cp_abi
.rtti_type
) == NULL
114 || !HAVE_CPLUS_STRUCT (check_typedef (v
->type ())))
118 ret
= (*current_cp_abi
.rtti_type
) (v
, full
, top
, using_enc
);
120 catch (const gdb_exception_error
&e
)
129 cplus_print_method_ptr (const gdb_byte
*contents
,
131 struct ui_file
*stream
)
133 if (current_cp_abi
.print_method_ptr
== NULL
)
134 error (_("GDB does not support pointers to methods on this target"));
135 (*current_cp_abi
.print_method_ptr
) (contents
, type
, stream
);
139 cplus_method_ptr_size (struct type
*to_type
)
141 if (current_cp_abi
.method_ptr_size
== NULL
)
142 error (_("GDB does not support pointers to methods on this target"));
143 return (*current_cp_abi
.method_ptr_size
) (to_type
);
147 cplus_make_method_ptr (struct type
*type
, gdb_byte
*contents
,
148 CORE_ADDR value
, int is_virtual
)
150 if (current_cp_abi
.make_method_ptr
== NULL
)
151 error (_("GDB does not support pointers to methods on this target"));
152 (*current_cp_abi
.make_method_ptr
) (type
, contents
, value
, is_virtual
);
156 cplus_skip_trampoline (frame_info_ptr frame
,
159 if (current_cp_abi
.skip_trampoline
== NULL
)
161 return (*current_cp_abi
.skip_trampoline
) (frame
, stop_pc
);
165 cplus_method_ptr_to_value (struct value
**this_p
,
166 struct value
*method_ptr
)
168 if (current_cp_abi
.method_ptr_to_value
== NULL
)
169 error (_("GDB does not support pointers to methods on this target"));
170 return (*current_cp_abi
.method_ptr_to_value
) (this_p
, method_ptr
);
176 cplus_print_vtable (struct value
*value
)
178 if (current_cp_abi
.print_vtable
== NULL
)
179 error (_("GDB cannot print the vtable on this target"));
180 (*current_cp_abi
.print_vtable
) (value
);
186 cplus_typeid (struct value
*value
)
188 if (current_cp_abi
.get_typeid
== NULL
)
189 error (_("GDB cannot find the typeid on this target"));
190 return (*current_cp_abi
.get_typeid
) (value
);
196 cplus_typeid_type (struct gdbarch
*gdbarch
)
198 if (current_cp_abi
.get_typeid_type
== NULL
)
199 error (_("GDB cannot find the type for 'typeid' on this target"));
200 return (*current_cp_abi
.get_typeid_type
) (gdbarch
);
206 cplus_type_from_type_info (struct value
*value
)
208 if (current_cp_abi
.get_type_from_type_info
== NULL
)
209 error (_("GDB cannot find the type from a std::type_info on this target"));
210 return (*current_cp_abi
.get_type_from_type_info
) (value
);
216 cplus_typename_from_type_info (struct value
*value
)
218 if (current_cp_abi
.get_typename_from_type_info
== NULL
)
219 error (_("GDB cannot find the type name "
220 "from a std::type_info on this target"));
221 return (*current_cp_abi
.get_typename_from_type_info
) (value
);
226 struct language_pass_by_ref_info
227 cp_pass_by_reference (struct type
*type
)
229 if ((current_cp_abi
.pass_by_reference
) == NULL
)
231 return (*current_cp_abi
.pass_by_reference
) (type
);
234 /* Set the current C++ ABI to SHORT_NAME. */
237 switch_to_cp_abi (const char *short_name
)
239 struct cp_abi_ops
*abi
;
241 abi
= find_cp_abi (short_name
);
245 current_cp_abi
= *abi
;
249 /* Add ABI to the list of supported C++ ABI's. */
252 register_cp_abi (struct cp_abi_ops
*abi
)
254 if (num_cp_abis
== CP_ABI_MAX
)
255 internal_error (_("Too many C++ ABIs, please increase "
256 "CP_ABI_MAX in cp-abi.c"));
258 cp_abis
[num_cp_abis
++] = abi
;
263 /* Set the ABI to use in "auto" mode to SHORT_NAME. */
266 set_cp_abi_as_auto_default (const char *short_name
)
268 struct cp_abi_ops
*abi
= find_cp_abi (short_name
);
271 internal_error (_("Cannot find C++ ABI \"%s\" to set it as auto default."),
274 xfree ((char *) auto_cp_abi
.longname
);
275 xfree ((char *) auto_cp_abi
.doc
);
279 auto_cp_abi
.shortname
= "auto";
280 auto_cp_abi
.longname
= xstrprintf ("currently \"%s\"",
281 abi
->shortname
).release ();
282 auto_cp_abi
.doc
= xstrprintf ("Automatically selected; currently \"%s\"",
283 abi
->shortname
).release ();
285 /* Since we copy the current ABI into current_cp_abi instead of
286 using a pointer, if auto is currently the default, we need to
288 if (strcmp (current_cp_abi
.shortname
, "auto") == 0)
289 switch_to_cp_abi ("auto");
292 /* Return the ABI operations associated with SHORT_NAME. */
294 static struct cp_abi_ops
*
295 find_cp_abi (const char *short_name
)
299 for (i
= 0; i
< num_cp_abis
; i
++)
300 if (strcmp (cp_abis
[i
]->shortname
, short_name
) == 0)
306 /* Display the list of registered C++ ABIs. */
309 list_cp_abis (int from_tty
)
311 struct ui_out
*uiout
= current_uiout
;
314 uiout
->text ("The available C++ ABIs are:\n");
315 ui_out_emit_tuple
tuple_emitter (uiout
, "cp-abi-list");
316 for (i
= 0; i
< num_cp_abis
; i
++)
322 uiout
->field_string ("cp-abi", cp_abis
[i
]->shortname
);
324 padcount
= 16 - 2 - strlen (cp_abis
[i
]->shortname
);
327 pad
[--padcount
] = ' ';
330 uiout
->field_string ("doc", cp_abis
[i
]->doc
);
335 /* Set the current C++ ABI, or display the list of options if no
336 argument is given. */
339 set_cp_abi_cmd (const char *args
, int from_tty
)
343 list_cp_abis (from_tty
);
347 if (!switch_to_cp_abi (args
))
348 error (_("Could not find \"%s\" in ABI list"), args
);
351 /* A completion function for "set cp-abi". */
354 cp_abi_completer (struct cmd_list_element
*ignore
,
355 completion_tracker
&tracker
,
356 const char *text
, const char *word
)
358 static const char **cp_abi_names
;
360 if (cp_abi_names
== NULL
)
364 cp_abi_names
= XNEWVEC (const char *, num_cp_abis
+ 1);
365 for (i
= 0; i
< num_cp_abis
; ++i
)
366 cp_abi_names
[i
] = cp_abis
[i
]->shortname
;
367 cp_abi_names
[i
] = NULL
;
370 complete_on_enum (tracker
, cp_abi_names
, text
, word
);
373 /* Show the currently selected C++ ABI. */
376 show_cp_abi_cmd (const char *args
, int from_tty
)
378 struct ui_out
*uiout
= current_uiout
;
380 uiout
->text ("The currently selected C++ ABI is \"");
382 uiout
->field_string ("cp-abi", current_cp_abi
.shortname
);
383 uiout
->text ("\" (");
384 uiout
->field_string ("longname", current_cp_abi
.longname
);
385 uiout
->text (").\n");
388 void _initialize_cp_abi ();
390 _initialize_cp_abi ()
392 struct cmd_list_element
*c
;
394 register_cp_abi (&auto_cp_abi
);
395 switch_to_cp_abi ("auto");
397 c
= add_cmd ("cp-abi", class_obscure
, set_cp_abi_cmd
, _("\
398 Set the ABI used for inspecting C++ objects.\n\
399 \"set cp-abi\" with no arguments will list the available ABIs."),
401 set_cmd_completer (c
, cp_abi_completer
);
403 add_cmd ("cp-abi", class_obscure
, show_cp_abi_cmd
,
404 _("Show the ABI used for inspecting C++ objects."),