Sort includes for files gdb/[a-f]*.[chyl].
[deliverable/binutils-gdb.git] / gdb / cp-abi.c
... / ...
CommitLineData
1/* Generic code for supporting multiple C++ ABI's
2
3 Copyright (C) 2001-2019 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
22/* Local non-gdb includes. */
23#include "command.h"
24#include "cp-abi.h"
25#include "gdbcmd.h"
26#include "ui-out.h"
27#include "value.h"
28
29static struct cp_abi_ops *find_cp_abi (const char *short_name);
30
31static struct cp_abi_ops current_cp_abi = { "", NULL };
32static struct cp_abi_ops auto_cp_abi = { "auto", NULL };
33
34#define CP_ABI_MAX 8
35static struct cp_abi_ops *cp_abis[CP_ABI_MAX];
36static int num_cp_abis = 0;
37
38enum ctor_kinds
39is_constructor_name (const char *name)
40{
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);
44}
45
46enum dtor_kinds
47is_destructor_name (const char *name)
48{
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);
52}
53
54int
55is_vtable_name (const char *name)
56{
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);
60}
61
62int
63is_operator_name (const char *name)
64{
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);
68}
69
70int
71baseclass_offset (struct type *type, int index, const gdb_byte *valaddr,
72 LONGEST embedded_offset, CORE_ADDR address,
73 const struct value *val)
74{
75 int res = 0;
76
77 gdb_assert (current_cp_abi.baseclass_offset != NULL);
78
79 TRY
80 {
81 res = (*current_cp_abi.baseclass_offset) (type, index, valaddr,
82 embedded_offset,
83 address, val);
84 }
85 CATCH (ex, RETURN_MASK_ERROR)
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 }
94 END_CATCH
95
96 return res;
97}
98
99struct value *
100value_virtual_fn_field (struct value **arg1p,
101 struct fn_field *f, int j,
102 struct type *type, int offset)
103{
104 if ((current_cp_abi.virtual_fn_field) == NULL)
105 return NULL;
106 return (*current_cp_abi.virtual_fn_field) (arg1p, f, j,
107 type, offset);
108}
109
110struct type *
111value_rtti_type (struct value *v, int *full,
112 LONGEST *top, int *using_enc)
113{
114 struct type *ret = NULL;
115
116 if ((current_cp_abi.rtti_type) == NULL)
117 return NULL;
118 TRY
119 {
120 ret = (*current_cp_abi.rtti_type) (v, full, top, using_enc);
121 }
122 CATCH (e, RETURN_MASK_ERROR)
123 {
124 return NULL;
125 }
126 END_CATCH
127
128 return ret;
129}
130
131void
132cplus_print_method_ptr (const gdb_byte *contents,
133 struct type *type,
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
141int
142cplus_method_ptr_size (struct type *to_type)
143{
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);
147}
148
149void
150cplus_make_method_ptr (struct type *type, gdb_byte *contents,
151 CORE_ADDR value, int is_virtual)
152{
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);
156}
157
158CORE_ADDR
159cplus_skip_trampoline (struct frame_info *frame,
160 CORE_ADDR stop_pc)
161{
162 if (current_cp_abi.skip_trampoline == NULL)
163 return 0;
164 return (*current_cp_abi.skip_trampoline) (frame, stop_pc);
165}
166
167struct value *
168cplus_method_ptr_to_value (struct value **this_p,
169 struct value *method_ptr)
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
176/* See cp-abi.h. */
177
178void
179cplus_print_vtable (struct value *value)
180{
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);
184}
185
186/* See cp-abi.h. */
187
188struct value *
189cplus_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
198struct type *
199cplus_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
206/* See cp-abi.h. */
207
208struct type *
209cplus_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
216/* See cp-abi.h. */
217
218std::string
219cplus_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
227int
228cp_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
235/* Set the current C++ ABI to SHORT_NAME. */
236
237static int
238switch_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
252int
253register_cp_abi (struct cp_abi_ops *abi)
254{
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"));
259
260 cp_abis[num_cp_abis++] = abi;
261
262 return 1;
263}
264
265/* Set the ABI to use in "auto" mode to SHORT_NAME. */
266
267void
268set_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__,
275 _("Cannot find C++ ABI \"%s\" to set it as auto default."),
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";
286 new_longname = xstrprintf ("currently \"%s\"", abi->shortname);
287 auto_cp_abi.longname = new_longname;
288
289 new_doc = xstrprintf ("Automatically selected; currently \"%s\"",
290 abi->shortname);
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");
298}
299
300/* Return the ABI operations associated with SHORT_NAME. */
301
302static struct cp_abi_ops *
303find_cp_abi (const char *short_name)
304{
305 int i;
306
307 for (i = 0; i < num_cp_abis; i++)
308 if (strcmp (cp_abis[i]->shortname, short_name) == 0)
309 return cp_abis[i];
310
311 return NULL;
312}
313
314/* Display the list of registered C++ ABIs. */
315
316static void
317list_cp_abis (int from_tty)
318{
319 struct ui_out *uiout = current_uiout;
320 int i;
321
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++)
325 {
326 char pad[14];
327 int padcount;
328
329 uiout->text (" ");
330 uiout->field_string ("cp-abi", cp_abis[i]->shortname);
331
332 padcount = 16 - 2 - strlen (cp_abis[i]->shortname);
333 pad[padcount] = 0;
334 while (padcount > 0)
335 pad[--padcount] = ' ';
336 uiout->text (pad);
337
338 uiout->field_string ("doc", cp_abis[i]->doc);
339 uiout->text ("\n");
340 }
341}
342
343/* Set the current C++ ABI, or display the list of options if no
344 argument is given. */
345
346static void
347set_cp_abi_cmd (const char *args, int from_tty)
348{
349 if (args == NULL)
350 {
351 list_cp_abis (from_tty);
352 return;
353 }
354
355 if (!switch_to_cp_abi (args))
356 error (_("Could not find \"%s\" in ABI list"), args);
357}
358
359/* A completion function for "set cp-abi". */
360
361static void
362cp_abi_completer (struct cmd_list_element *ignore,
363 completion_tracker &tracker,
364 const char *text, const char *word)
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;
375 cp_abi_names[i] = NULL;
376 }
377
378 complete_on_enum (tracker, cp_abi_names, text, word);
379}
380
381/* Show the currently selected C++ ABI. */
382
383static void
384show_cp_abi_cmd (const char *args, int from_tty)
385{
386 struct ui_out *uiout = current_uiout;
387
388 uiout->text ("The currently selected C++ ABI is \"");
389
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");
394}
395
396void
397_initialize_cp_abi (void)
398{
399 struct cmd_list_element *c;
400
401 register_cp_abi (&auto_cp_abi);
402 switch_to_cp_abi ("auto");
403
404 c = add_cmd ("cp-abi", class_obscure, set_cp_abi_cmd, _("\
405Set the ABI used for inspecting C++ objects.\n\
406\"set cp-abi\" with no arguments will list the available ABIs."),
407 &setlist);
408 set_cmd_completer (c, cp_abi_completer);
409
410 add_cmd ("cp-abi", class_obscure, show_cp_abi_cmd,
411 _("Show the ABI used for inspecting C++ objects."),
412 &showlist);
413}
This page took 0.025255 seconds and 4 git commands to generate.