]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - gdb/cp-abi.c
882c1a03fe291573b72f9fab07033fd99ccc3475
[thirdparty/binutils-gdb.git] / gdb / cp-abi.c
1 /* Generic code for supporting multiple C++ ABI's
2
3 Copyright (C) 2001-2024 Free Software Foundation, Inc.
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
9 the Free Software Foundation; either version 3 of the License, or
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
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
19
20 #include "defs.h"
21 #include "language.h"
22 #include "value.h"
23 #include "cp-abi.h"
24 #include "command.h"
25 #include "gdbcmd.h"
26 #include "ui-out.h"
27 static struct cp_abi_ops *find_cp_abi (const char *short_name);
28
29 static struct cp_abi_ops current_cp_abi = { "", NULL };
30 static struct cp_abi_ops auto_cp_abi = { "auto", NULL };
31
32 #define CP_ABI_MAX 8
33 static struct cp_abi_ops *cp_abis[CP_ABI_MAX];
34 static int num_cp_abis = 0;
35
36 enum ctor_kinds
37 is_constructor_name (const char *name)
38 {
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);
42 }
43
44 enum dtor_kinds
45 is_destructor_name (const char *name)
46 {
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);
50 }
51
52 int
53 is_vtable_name (const char *name)
54 {
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);
58 }
59
60 int
61 is_operator_name (const char *name)
62 {
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);
66 }
67
68 int
69 baseclass_offset (struct type *type, int index, const gdb_byte *valaddr,
70 LONGEST embedded_offset, CORE_ADDR address,
71 const struct value *val)
72 {
73 int res = 0;
74
75 gdb_assert (current_cp_abi.baseclass_offset != NULL);
76
77 try
78 {
79 res = (*current_cp_abi.baseclass_offset) (type, index, valaddr,
80 embedded_offset,
81 address, val);
82 }
83 catch (const gdb_exception_error &ex)
84 {
85 if (ex.error != NOT_AVAILABLE_ERROR)
86 throw;
87
88 throw_error (NOT_AVAILABLE_ERROR,
89 _("Cannot determine virtual baseclass offset "
90 "of incomplete object"));
91 }
92
93 return res;
94 }
95
96 struct value *
97 value_virtual_fn_field (struct value **arg1p,
98 struct fn_field *f, int j,
99 struct type *type, int offset)
100 {
101 if ((current_cp_abi.virtual_fn_field) == NULL)
102 return NULL;
103 return (*current_cp_abi.virtual_fn_field) (arg1p, f, j,
104 type, offset);
105 }
106
107 struct type *
108 value_rtti_type (struct value *v, int *full,
109 LONGEST *top, int *using_enc)
110 {
111 struct type *ret = NULL;
112
113 if ((current_cp_abi.rtti_type) == NULL
114 || !HAVE_CPLUS_STRUCT (check_typedef (v->type ())))
115 return NULL;
116 try
117 {
118 ret = (*current_cp_abi.rtti_type) (v, full, top, using_enc);
119 }
120 catch (const gdb_exception_error &e)
121 {
122 return NULL;
123 }
124
125 return ret;
126 }
127
128 void
129 cplus_print_method_ptr (const gdb_byte *contents,
130 struct type *type,
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
138 int
139 cplus_method_ptr_size (struct type *to_type)
140 {
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);
144 }
145
146 void
147 cplus_make_method_ptr (struct type *type, gdb_byte *contents,
148 CORE_ADDR value, int is_virtual)
149 {
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);
153 }
154
155 CORE_ADDR
156 cplus_skip_trampoline (const frame_info_ptr &frame,
157 CORE_ADDR stop_pc)
158 {
159 if (current_cp_abi.skip_trampoline == NULL)
160 return 0;
161 return (*current_cp_abi.skip_trampoline) (frame, stop_pc);
162 }
163
164 struct value *
165 cplus_method_ptr_to_value (struct value **this_p,
166 struct value *method_ptr)
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
173 /* See cp-abi.h. */
174
175 void
176 cplus_print_vtable (struct value *value)
177 {
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);
181 }
182
183 /* See cp-abi.h. */
184
185 struct value *
186 cplus_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
195 struct type *
196 cplus_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
203 /* See cp-abi.h. */
204
205 struct type *
206 cplus_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
213 /* See cp-abi.h. */
214
215 std::string
216 cplus_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
224 /* See cp-abi.h. */
225
226 struct language_pass_by_ref_info
227 cp_pass_by_reference (struct type *type)
228 {
229 if ((current_cp_abi.pass_by_reference) == NULL)
230 return {};
231 return (*current_cp_abi.pass_by_reference) (type);
232 }
233
234 /* Set the current C++ ABI to SHORT_NAME. */
235
236 static int
237 switch_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
251 int
252 register_cp_abi (struct cp_abi_ops *abi)
253 {
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"));
257
258 cp_abis[num_cp_abis++] = abi;
259
260 return 1;
261 }
262
263 /* Set the ABI to use in "auto" mode to SHORT_NAME. */
264
265 void
266 set_cp_abi_as_auto_default (const char *short_name)
267 {
268 struct cp_abi_ops *abi = find_cp_abi (short_name);
269
270 if (abi == NULL)
271 internal_error (_("Cannot find C++ ABI \"%s\" to set it as auto default."),
272 short_name);
273
274 xfree ((char *) auto_cp_abi.longname);
275 xfree ((char *) auto_cp_abi.doc);
276
277 auto_cp_abi = *abi;
278
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 ();
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");
290 }
291
292 /* Return the ABI operations associated with SHORT_NAME. */
293
294 static struct cp_abi_ops *
295 find_cp_abi (const char *short_name)
296 {
297 int i;
298
299 for (i = 0; i < num_cp_abis; i++)
300 if (strcmp (cp_abis[i]->shortname, short_name) == 0)
301 return cp_abis[i];
302
303 return NULL;
304 }
305
306 /* Display the list of registered C++ ABIs. */
307
308 static void
309 list_cp_abis (int from_tty)
310 {
311 struct ui_out *uiout = current_uiout;
312 int i;
313
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++)
317 {
318 char pad[14];
319 int padcount;
320
321 uiout->text (" ");
322 uiout->field_string ("cp-abi", cp_abis[i]->shortname);
323
324 padcount = 16 - 2 - strlen (cp_abis[i]->shortname);
325 pad[padcount] = 0;
326 while (padcount > 0)
327 pad[--padcount] = ' ';
328 uiout->text (pad);
329
330 uiout->field_string ("doc", cp_abis[i]->doc);
331 uiout->text ("\n");
332 }
333 }
334
335 /* Set the current C++ ABI, or display the list of options if no
336 argument is given. */
337
338 static void
339 set_cp_abi_cmd (const char *args, int from_tty)
340 {
341 if (args == NULL)
342 {
343 list_cp_abis (from_tty);
344 return;
345 }
346
347 if (!switch_to_cp_abi (args))
348 error (_("Could not find \"%s\" in ABI list"), args);
349 }
350
351 /* A completion function for "set cp-abi". */
352
353 static void
354 cp_abi_completer (struct cmd_list_element *ignore,
355 completion_tracker &tracker,
356 const char *text, const char *word)
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;
367 cp_abi_names[i] = NULL;
368 }
369
370 complete_on_enum (tracker, cp_abi_names, text, word);
371 }
372
373 /* Show the currently selected C++ ABI. */
374
375 static void
376 show_cp_abi_cmd (const char *args, int from_tty)
377 {
378 struct ui_out *uiout = current_uiout;
379
380 uiout->text ("The currently selected C++ ABI is \"");
381
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");
386 }
387
388 void _initialize_cp_abi ();
389 void
390 _initialize_cp_abi ()
391 {
392 struct cmd_list_element *c;
393
394 register_cp_abi (&auto_cp_abi);
395 switch_to_cp_abi ("auto");
396
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."),
400 &setlist);
401 set_cmd_completer (c, cp_abi_completer);
402
403 add_cmd ("cp-abi", class_obscure, show_cp_abi_cmd,
404 _("Show the ABI used for inspecting C++ objects."),
405 &showlist);
406 }