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