1 /* Generic code for supporting multiple C++ ABI's
3 Copyright (C) 2001-2019 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/>. */
22 /* Local non-gdb includes. */
29 static struct cp_abi_ops
*find_cp_abi (const char *short_name
);
31 static struct cp_abi_ops current_cp_abi
= { "", NULL
};
32 static struct cp_abi_ops auto_cp_abi
= { "auto", NULL
};
35 static struct cp_abi_ops
*cp_abis
[CP_ABI_MAX
];
36 static int num_cp_abis
= 0;
39 is_constructor_name (const char *name
)
41 if ((current_cp_abi
.is_constructor_name
) == NULL
)
42 error (_("ABI doesn't define required function is_constructor_name"));
43 return (*current_cp_abi
.is_constructor_name
) (name
);
47 is_destructor_name (const char *name
)
49 if ((current_cp_abi
.is_destructor_name
) == NULL
)
50 error (_("ABI doesn't define required function is_destructor_name"));
51 return (*current_cp_abi
.is_destructor_name
) (name
);
55 is_vtable_name (const char *name
)
57 if ((current_cp_abi
.is_vtable_name
) == NULL
)
58 error (_("ABI doesn't define required function is_vtable_name"));
59 return (*current_cp_abi
.is_vtable_name
) (name
);
63 is_operator_name (const char *name
)
65 if ((current_cp_abi
.is_operator_name
) == NULL
)
66 error (_("ABI doesn't define required function is_operator_name"));
67 return (*current_cp_abi
.is_operator_name
) (name
);
71 baseclass_offset (struct type
*type
, int index
, const gdb_byte
*valaddr
,
72 LONGEST embedded_offset
, CORE_ADDR address
,
73 const struct value
*val
)
77 gdb_assert (current_cp_abi
.baseclass_offset
!= NULL
);
81 res
= (*current_cp_abi
.baseclass_offset
) (type
, index
, valaddr
,
85 CATCH (ex
, RETURN_MASK_ERROR
)
87 if (ex
.error
!= NOT_AVAILABLE_ERROR
)
90 throw_error (NOT_AVAILABLE_ERROR
,
91 _("Cannot determine virtual baseclass offset "
92 "of incomplete object"));
100 value_virtual_fn_field (struct value
**arg1p
,
101 struct fn_field
*f
, int j
,
102 struct type
*type
, int offset
)
104 if ((current_cp_abi
.virtual_fn_field
) == NULL
)
106 return (*current_cp_abi
.virtual_fn_field
) (arg1p
, f
, j
,
111 value_rtti_type (struct value
*v
, int *full
,
112 LONGEST
*top
, int *using_enc
)
114 struct type
*ret
= NULL
;
116 if ((current_cp_abi
.rtti_type
) == NULL
)
120 ret
= (*current_cp_abi
.rtti_type
) (v
, full
, top
, using_enc
);
122 CATCH (e
, RETURN_MASK_ERROR
)
132 cplus_print_method_ptr (const gdb_byte
*contents
,
134 struct ui_file
*stream
)
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
);
142 cplus_method_ptr_size (struct type
*to_type
)
144 if (current_cp_abi
.method_ptr_size
== NULL
)
145 error (_("GDB does not support pointers to methods on this target"));
146 return (*current_cp_abi
.method_ptr_size
) (to_type
);
150 cplus_make_method_ptr (struct type
*type
, gdb_byte
*contents
,
151 CORE_ADDR value
, int is_virtual
)
153 if (current_cp_abi
.make_method_ptr
== NULL
)
154 error (_("GDB does not support pointers to methods on this target"));
155 (*current_cp_abi
.make_method_ptr
) (type
, contents
, value
, is_virtual
);
159 cplus_skip_trampoline (struct frame_info
*frame
,
162 if (current_cp_abi
.skip_trampoline
== NULL
)
164 return (*current_cp_abi
.skip_trampoline
) (frame
, stop_pc
);
168 cplus_method_ptr_to_value (struct value
**this_p
,
169 struct value
*method_ptr
)
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
);
179 cplus_print_vtable (struct value
*value
)
181 if (current_cp_abi
.print_vtable
== NULL
)
182 error (_("GDB cannot print the vtable on this target"));
183 (*current_cp_abi
.print_vtable
) (value
);
189 cplus_typeid (struct value
*value
)
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
);
199 cplus_typeid_type (struct gdbarch
*gdbarch
)
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
);
209 cplus_type_from_type_info (struct value
*value
)
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
);
219 cplus_typename_from_type_info (struct value
*value
)
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
);
228 cp_pass_by_reference (struct type
*type
)
230 if ((current_cp_abi
.pass_by_reference
) == NULL
)
232 return (*current_cp_abi
.pass_by_reference
) (type
);
235 /* Set the current C++ ABI to SHORT_NAME. */
238 switch_to_cp_abi (const char *short_name
)
240 struct cp_abi_ops
*abi
;
242 abi
= find_cp_abi (short_name
);
246 current_cp_abi
= *abi
;
250 /* Add ABI to the list of supported C++ ABI's. */
253 register_cp_abi (struct cp_abi_ops
*abi
)
255 if (num_cp_abis
== CP_ABI_MAX
)
256 internal_error (__FILE__
, __LINE__
,
257 _("Too many C++ ABIs, please increase "
258 "CP_ABI_MAX in cp-abi.c"));
260 cp_abis
[num_cp_abis
++] = abi
;
265 /* Set the ABI to use in "auto" mode to SHORT_NAME. */
268 set_cp_abi_as_auto_default (const char *short_name
)
270 char *new_longname
, *new_doc
;
271 struct cp_abi_ops
*abi
= find_cp_abi (short_name
);
274 internal_error (__FILE__
, __LINE__
,
275 _("Cannot find C++ ABI \"%s\" to set it as auto default."),
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
);
285 auto_cp_abi
.shortname
= "auto";
286 new_longname
= xstrprintf ("currently \"%s\"", abi
->shortname
);
287 auto_cp_abi
.longname
= new_longname
;
289 new_doc
= xstrprintf ("Automatically selected; currently \"%s\"",
291 auto_cp_abi
.doc
= new_doc
;
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
296 if (strcmp (current_cp_abi
.shortname
, "auto") == 0)
297 switch_to_cp_abi ("auto");
300 /* Return the ABI operations associated with SHORT_NAME. */
302 static struct cp_abi_ops
*
303 find_cp_abi (const char *short_name
)
307 for (i
= 0; i
< num_cp_abis
; i
++)
308 if (strcmp (cp_abis
[i
]->shortname
, short_name
) == 0)
314 /* Display the list of registered C++ ABIs. */
317 list_cp_abis (int from_tty
)
319 struct ui_out
*uiout
= current_uiout
;
322 uiout
->text ("The available C++ ABIs are:\n");
323 ui_out_emit_tuple
tuple_emitter (uiout
, "cp-abi-list");
324 for (i
= 0; i
< num_cp_abis
; i
++)
330 uiout
->field_string ("cp-abi", cp_abis
[i
]->shortname
);
332 padcount
= 16 - 2 - strlen (cp_abis
[i
]->shortname
);
335 pad
[--padcount
] = ' ';
338 uiout
->field_string ("doc", cp_abis
[i
]->doc
);
343 /* Set the current C++ ABI, or display the list of options if no
344 argument is given. */
347 set_cp_abi_cmd (const char *args
, int from_tty
)
351 list_cp_abis (from_tty
);
355 if (!switch_to_cp_abi (args
))
356 error (_("Could not find \"%s\" in ABI list"), args
);
359 /* A completion function for "set cp-abi". */
362 cp_abi_completer (struct cmd_list_element
*ignore
,
363 completion_tracker
&tracker
,
364 const char *text
, const char *word
)
366 static const char **cp_abi_names
;
368 if (cp_abi_names
== NULL
)
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
;
375 cp_abi_names
[i
] = NULL
;
378 complete_on_enum (tracker
, cp_abi_names
, text
, word
);
381 /* Show the currently selected C++ ABI. */
384 show_cp_abi_cmd (const char *args
, int from_tty
)
386 struct ui_out
*uiout
= current_uiout
;
388 uiout
->text ("The currently selected C++ ABI is \"");
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");
397 _initialize_cp_abi (void)
399 struct cmd_list_element
*c
;
401 register_cp_abi (&auto_cp_abi
);
402 switch_to_cp_abi ("auto");
404 c
= add_cmd ("cp-abi", class_obscure
, set_cp_abi_cmd
, _("\
405 Set the ABI used for inspecting C++ objects.\n\
406 \"set cp-abi\" with no arguments will list the available ABIs."),
408 set_cmd_completer (c
, cp_abi_completer
);
410 add_cmd ("cp-abi", class_obscure
, show_cp_abi_cmd
,
411 _("Show the ABI used for inspecting C++ objects."),