1/* Generic code for supporting multiple C++ ABI's
2
3   Copyright (C) 2001-2020 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 "value.h"
22#include "cp-abi.h"
23#include "command.h"
24#include "gdbcmd.h"
25#include "ui-out.h"
26static struct cp_abi_ops *find_cp_abi (const char *short_name);
27
28static struct cp_abi_ops current_cp_abi = { "", NULL };
29static struct cp_abi_ops auto_cp_abi = { "auto", NULL };
30
31#define CP_ABI_MAX 8
32static struct cp_abi_ops *cp_abis[CP_ABI_MAX];
33static int num_cp_abis = 0;
34
35enum ctor_kinds
36is_constructor_name (const char *name)
37{
38  if ((current_cp_abi.is_constructor_name) == NULL)
39    error (_("ABI doesn't define required function is_constructor_name"));
40  return (*current_cp_abi.is_constructor_name) (name);
41}
42
43enum dtor_kinds
44is_destructor_name (const char *name)
45{
46  if ((current_cp_abi.is_destructor_name) == NULL)
47    error (_("ABI doesn't define required function is_destructor_name"));
48  return (*current_cp_abi.is_destructor_name) (name);
49}
50
51int
52is_vtable_name (const char *name)
53{
54  if ((current_cp_abi.is_vtable_name) == NULL)
55    error (_("ABI doesn't define required function is_vtable_name"));
56  return (*current_cp_abi.is_vtable_name) (name);
57}
58
59int
60is_operator_name (const char *name)
61{
62  if ((current_cp_abi.is_operator_name) == NULL)
63    error (_("ABI doesn't define required function is_operator_name"));
64  return (*current_cp_abi.is_operator_name) (name);
65}
66
67int
68baseclass_offset (struct type *type, int index, const gdb_byte *valaddr,
69		  LONGEST embedded_offset, CORE_ADDR address,
70		  const struct value *val)
71{
72  int res = 0;
73
74  gdb_assert (current_cp_abi.baseclass_offset != NULL);
75
76  try
77    {
78      res = (*current_cp_abi.baseclass_offset) (type, index, valaddr,
79						embedded_offset,
80						address, val);
81    }
82  catch (const gdb_exception_error &ex)
83    {
84      if (ex.error != NOT_AVAILABLE_ERROR)
85	throw;
86
87      throw_error (NOT_AVAILABLE_ERROR,
88		   _("Cannot determine virtual baseclass offset "
89		     "of incomplete object"));
90    }
91
92  return res;
93}
94
95struct value *
96value_virtual_fn_field (struct value **arg1p,
97			struct fn_field *f, int j,
98			struct type *type, int offset)
99{
100  if ((current_cp_abi.virtual_fn_field) == NULL)
101    return NULL;
102  return (*current_cp_abi.virtual_fn_field) (arg1p, f, j,
103					     type, offset);
104}
105
106struct type *
107value_rtti_type (struct value *v, int *full,
108		 LONGEST *top, int *using_enc)
109{
110  struct type *ret = NULL;
111
112  if ((current_cp_abi.rtti_type) == NULL
113      || !HAVE_CPLUS_STRUCT (check_typedef (value_type (v))))
114    return NULL;
115  try
116    {
117      ret = (*current_cp_abi.rtti_type) (v, full, top, using_enc);
118    }
119  catch (const gdb_exception_error &e)
120    {
121      return NULL;
122    }
123
124  return ret;
125}
126
127void
128cplus_print_method_ptr (const gdb_byte *contents,
129			struct type *type,
130			struct ui_file *stream)
131{
132  if (current_cp_abi.print_method_ptr == NULL)
133    error (_("GDB does not support pointers to methods on this target"));
134  (*current_cp_abi.print_method_ptr) (contents, type, stream);
135}
136
137int
138cplus_method_ptr_size (struct type *to_type)
139{
140  if (current_cp_abi.method_ptr_size == NULL)
141    error (_("GDB does not support pointers to methods on this target"));
142  return (*current_cp_abi.method_ptr_size) (to_type);
143}
144
145void
146cplus_make_method_ptr (struct type *type, gdb_byte *contents,
147		       CORE_ADDR value, int is_virtual)
148{
149  if (current_cp_abi.make_method_ptr == NULL)
150    error (_("GDB does not support pointers to methods on this target"));
151  (*current_cp_abi.make_method_ptr) (type, contents, value, is_virtual);
152}
153
154CORE_ADDR
155cplus_skip_trampoline (struct frame_info *frame,
156		       CORE_ADDR stop_pc)
157{
158  if (current_cp_abi.skip_trampoline == NULL)
159    return 0;
160  return (*current_cp_abi.skip_trampoline) (frame, stop_pc);
161}
162
163struct value *
164cplus_method_ptr_to_value (struct value **this_p,
165			   struct value *method_ptr)
166{
167  if (current_cp_abi.method_ptr_to_value == NULL)
168    error (_("GDB does not support pointers to methods on this target"));
169  return (*current_cp_abi.method_ptr_to_value) (this_p, method_ptr);
170}
171
172/* See cp-abi.h.  */
173
174void
175cplus_print_vtable (struct value *value)
176{
177  if (current_cp_abi.print_vtable == NULL)
178    error (_("GDB cannot print the vtable on this target"));
179  (*current_cp_abi.print_vtable) (value);
180}
181
182/* See cp-abi.h.  */
183
184struct value *
185cplus_typeid (struct value *value)
186{
187  if (current_cp_abi.get_typeid == NULL)
188    error (_("GDB cannot find the typeid on this target"));
189  return (*current_cp_abi.get_typeid) (value);
190}
191
192/* See cp-abi.h.  */
193
194struct type *
195cplus_typeid_type (struct gdbarch *gdbarch)
196{
197  if (current_cp_abi.get_typeid_type == NULL)
198    error (_("GDB cannot find the type for 'typeid' on this target"));
199  return (*current_cp_abi.get_typeid_type) (gdbarch);
200}
201
202/* See cp-abi.h.  */
203
204struct type *
205cplus_type_from_type_info (struct value *value)
206{
207  if (current_cp_abi.get_type_from_type_info == NULL)
208    error (_("GDB cannot find the type from a std::type_info on this target"));
209  return (*current_cp_abi.get_type_from_type_info) (value);
210}
211
212/* See cp-abi.h.  */
213
214std::string
215cplus_typename_from_type_info (struct value *value)
216{
217  if (current_cp_abi.get_typename_from_type_info == NULL)
218    error (_("GDB cannot find the type name "
219	     "from a std::type_info on this target"));
220  return (*current_cp_abi.get_typename_from_type_info) (value);
221}
222
223/* See cp-abi.h.  */
224
225struct language_pass_by_ref_info
226cp_pass_by_reference (struct type *type)
227{
228  if ((current_cp_abi.pass_by_reference) == NULL)
229    return {};
230  return (*current_cp_abi.pass_by_reference) (type);
231}
232
233/* Set the current C++ ABI to SHORT_NAME.  */
234
235static int
236switch_to_cp_abi (const char *short_name)
237{
238  struct cp_abi_ops *abi;
239
240  abi = find_cp_abi (short_name);
241  if (abi == NULL)
242    return 0;
243
244  current_cp_abi = *abi;
245  return 1;
246}
247
248/* Add ABI to the list of supported C++ ABI's.  */
249
250int
251register_cp_abi (struct cp_abi_ops *abi)
252{
253  if (num_cp_abis == CP_ABI_MAX)
254    internal_error (__FILE__, __LINE__,
255		    _("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
265void
266set_cp_abi_as_auto_default (const char *short_name)
267{
268  char *new_longname, *new_doc;
269  struct cp_abi_ops *abi = find_cp_abi (short_name);
270
271  if (abi == NULL)
272    internal_error (__FILE__, __LINE__,
273		    _("Cannot find C++ ABI \"%s\" to set it as auto default."),
274		    short_name);
275
276  xfree ((char *) auto_cp_abi.longname);
277  xfree ((char *) auto_cp_abi.doc);
278
279  auto_cp_abi = *abi;
280
281  auto_cp_abi.shortname = "auto";
282  new_longname = xstrprintf ("currently \"%s\"", abi->shortname);
283  auto_cp_abi.longname = new_longname;
284
285  new_doc = xstrprintf ("Automatically selected; currently \"%s\"",
286	     abi->shortname);
287  auto_cp_abi.doc = new_doc;
288
289  /* Since we copy the current ABI into current_cp_abi instead of
290     using a pointer, if auto is currently the default, we need to
291     reset it.  */
292  if (strcmp (current_cp_abi.shortname, "auto") == 0)
293    switch_to_cp_abi ("auto");
294}
295
296/* Return the ABI operations associated with SHORT_NAME.  */
297
298static struct cp_abi_ops *
299find_cp_abi (const char *short_name)
300{
301  int i;
302
303  for (i = 0; i < num_cp_abis; i++)
304    if (strcmp (cp_abis[i]->shortname, short_name) == 0)
305      return cp_abis[i];
306
307  return NULL;
308}
309
310/* Display the list of registered C++ ABIs.  */
311
312static void
313list_cp_abis (int from_tty)
314{
315  struct ui_out *uiout = current_uiout;
316  int i;
317
318  uiout->text ("The available C++ ABIs are:\n");
319  ui_out_emit_tuple tuple_emitter (uiout, "cp-abi-list");
320  for (i = 0; i < num_cp_abis; i++)
321    {
322      char pad[14];
323      int padcount;
324
325      uiout->text ("  ");
326      uiout->field_string ("cp-abi", cp_abis[i]->shortname);
327
328      padcount = 16 - 2 - strlen (cp_abis[i]->shortname);
329      pad[padcount] = 0;
330      while (padcount > 0)
331	pad[--padcount] = ' ';
332      uiout->text (pad);
333
334      uiout->field_string ("doc", cp_abis[i]->doc);
335      uiout->text ("\n");
336    }
337}
338
339/* Set the current C++ ABI, or display the list of options if no
340   argument is given.  */
341
342static void
343set_cp_abi_cmd (const char *args, int from_tty)
344{
345  if (args == NULL)
346    {
347      list_cp_abis (from_tty);
348      return;
349    }
350
351  if (!switch_to_cp_abi (args))
352    error (_("Could not find \"%s\" in ABI list"), args);
353}
354
355/* A completion function for "set cp-abi".  */
356
357static void
358cp_abi_completer (struct cmd_list_element *ignore,
359		  completion_tracker &tracker,
360		  const char *text, const char *word)
361{
362  static const char **cp_abi_names;
363
364  if (cp_abi_names == NULL)
365    {
366      int i;
367
368      cp_abi_names = XNEWVEC (const char *, num_cp_abis + 1);
369      for (i = 0; i < num_cp_abis; ++i)
370	cp_abi_names[i] = cp_abis[i]->shortname;
371      cp_abi_names[i] = NULL;
372    }
373
374  complete_on_enum (tracker, cp_abi_names, text, word);
375}
376
377/* Show the currently selected C++ ABI.  */
378
379static void
380show_cp_abi_cmd (const char *args, int from_tty)
381{
382  struct ui_out *uiout = current_uiout;
383
384  uiout->text ("The currently selected C++ ABI is \"");
385
386  uiout->field_string ("cp-abi", current_cp_abi.shortname);
387  uiout->text ("\" (");
388  uiout->field_string ("longname", current_cp_abi.longname);
389  uiout->text (").\n");
390}
391
392void _initialize_cp_abi ();
393void
394_initialize_cp_abi ()
395{
396  struct cmd_list_element *c;
397
398  register_cp_abi (&auto_cp_abi);
399  switch_to_cp_abi ("auto");
400
401  c = add_cmd ("cp-abi", class_obscure, set_cp_abi_cmd, _("\
402Set the ABI used for inspecting C++ objects.\n\
403\"set cp-abi\" with no arguments will list the available ABIs."),
404	       &setlist);
405  set_cmd_completer (c, cp_abi_completer);
406
407  add_cmd ("cp-abi", class_obscure, show_cp_abi_cmd,
408	   _("Show the ABI used for inspecting C++ objects."),
409	   &showlist);
410}
411