1139735Simp/* Generic code for supporting multiple C++ ABI's
2129198Scognet   Copyright 2001, 2002, 2003 Free Software Foundation, Inc.
3129198Scognet
4129198Scognet   This file is part of GDB.
5129198Scognet
6129198Scognet   This program is free software; you can redistribute it and/or modify
7129198Scognet   it under the terms of the GNU General Public License as published by
8129198Scognet   the Free Software Foundation; either version 2 of the License, or
9129198Scognet   (at your option) any later version.
10129198Scognet
11129198Scognet   This program is distributed in the hope that it will be useful,
12129198Scognet   but WITHOUT ANY WARRANTY; without even the implied warranty of
13129198Scognet   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14129198Scognet   GNU General Public License for more details.
15129198Scognet
16129198Scognet   You should have received a copy of the GNU General Public License
17129198Scognet   along with this program; if not, write to the Free Software
18129198Scognet   Foundation, Inc., 59 Temple Place - Suite 330,
19129198Scognet   Boston, MA 02111-1307, USA.  */
20129198Scognet
21129198Scognet#include "defs.h"
22129198Scognet#include "value.h"
23129198Scognet#include "cp-abi.h"
24129198Scognet#include "command.h"
25129198Scognet#include "gdbcmd.h"
26129198Scognet#include "ui-out.h"
27129198Scognet
28129198Scognet#include "gdb_string.h"
29129198Scognet
30129198Scognetstatic struct cp_abi_ops *find_cp_abi (const char *short_name);
31129198Scognet
32129198Scognetstatic struct cp_abi_ops current_cp_abi = { "", NULL };
33129198Scognetstatic struct cp_abi_ops auto_cp_abi = { "auto", NULL };
34129198Scognet
35129198Scognet#define CP_ABI_MAX 8
36129198Scognetstatic struct cp_abi_ops *cp_abis[CP_ABI_MAX];
37129198Scognetstatic int num_cp_abis = 0;
38129198Scognet
39129198Scognetenum ctor_kinds
40143857Scognetis_constructor_name (const char *name)
41143857Scognet{
42143857Scognet  if ((current_cp_abi.is_constructor_name) == NULL)
43143857Scognet    error ("ABI doesn't define required function is_constructor_name");
44143857Scognet  return (*current_cp_abi.is_constructor_name) (name);
45143857Scognet}
46129198Scognet
47230191Sdasenum dtor_kinds
48230191Sdasis_destructor_name (const char *name)
49230191Sdas{
50230191Sdas  if ((current_cp_abi.is_destructor_name) == NULL)
51230191Sdas    error ("ABI doesn't define required function is_destructor_name");
52230198Sdas  return (*current_cp_abi.is_destructor_name) (name);
53230198Sdas}
54143857Scognet
55230198Sdasint
56129198Scognetis_vtable_name (const char *name)
57129198Scognet{
58129198Scognet  if ((current_cp_abi.is_vtable_name) == NULL)
59129198Scognet    error ("ABI doesn't define required function is_vtable_name");
60129198Scognet  return (*current_cp_abi.is_vtable_name) (name);
61129198Scognet}
62129198Scognet
63129198Scognetint
64129198Scognetis_operator_name (const char *name)
65129198Scognet{
66129198Scognet  if ((current_cp_abi.is_operator_name) == NULL)
67129198Scognet    error ("ABI doesn't define required function is_operator_name");
68129198Scognet  return (*current_cp_abi.is_operator_name) (name);
69129198Scognet}
70129198Scognet
71129198Scognetint
72129198Scognetbaseclass_offset (struct type *type, int index, char *valaddr,
73129198Scognet		  CORE_ADDR address)
74129198Scognet{
75129198Scognet  if (current_cp_abi.baseclass_offset == NULL)
76129198Scognet    error ("ABI doesn't define required function baseclass_offset");
77143857Scognet  return (*current_cp_abi.baseclass_offset) (type, index, valaddr, address);
78230366Sdas}
79143857Scognet
80143857Scognetstruct value *
81230366Sdasvalue_virtual_fn_field (struct value **arg1p, struct fn_field *f, int j,
82143857Scognet			struct type *type, int offset)
83143857Scognet{
84230366Sdas  if ((current_cp_abi.virtual_fn_field) == NULL)
85143857Scognet    return NULL;
86129198Scognet  return (*current_cp_abi.virtual_fn_field) (arg1p, f, j, type, offset);
87}
88
89struct type *
90value_rtti_type (struct value *v, int *full, int *top, int *using_enc)
91{
92  if ((current_cp_abi.rtti_type) == NULL)
93    return NULL;
94  return (*current_cp_abi.rtti_type) (v, full, top, using_enc);
95}
96
97/* Set the current C++ ABI to SHORT_NAME.  */
98
99static int
100switch_to_cp_abi (const char *short_name)
101{
102  struct cp_abi_ops *abi;
103
104  abi = find_cp_abi (short_name);
105  if (abi == NULL)
106    return 0;
107
108  current_cp_abi = *abi;
109  return 1;
110}
111
112/* Add ABI to the list of supported C++ ABI's.  */
113
114int
115register_cp_abi (struct cp_abi_ops *abi)
116{
117  if (num_cp_abis == CP_ABI_MAX)
118    internal_error (__FILE__, __LINE__,
119		    "Too many C++ ABIs, please increase CP_ABI_MAX in cp-abi.c");
120
121  cp_abis[num_cp_abis++] = abi;
122
123  return 1;
124}
125
126/* Set the ABI to use in "auto" mode to SHORT_NAME.  */
127
128void
129set_cp_abi_as_auto_default (const char *short_name)
130{
131  char *new_longname, *new_doc;
132  struct cp_abi_ops *abi = find_cp_abi (short_name);
133
134  if (abi == NULL)
135    internal_error (__FILE__, __LINE__,
136		    "Cannot find C++ ABI \"%s\" to set it as auto default.",
137		    short_name);
138
139  if (auto_cp_abi.longname != NULL)
140    xfree ((char *) auto_cp_abi.longname);
141  if (auto_cp_abi.doc != NULL)
142    xfree ((char *) auto_cp_abi.doc);
143
144  auto_cp_abi = *abi;
145
146  auto_cp_abi.shortname = "auto";
147  new_longname = xmalloc (strlen ("currently ") + 1 + strlen (abi->shortname)
148			  + 1 + 1);
149  sprintf (new_longname, "currently \"%s\"", abi->shortname);
150  auto_cp_abi.longname = new_longname;
151
152  new_doc = xmalloc (strlen ("Automatically selected; currently ")
153		     + 1 + strlen (abi->shortname) + 1 + 1);
154  sprintf (new_doc, "Automatically selected; currently \"%s\"", abi->shortname);
155  auto_cp_abi.doc = new_doc;
156
157  /* Since we copy the current ABI into current_cp_abi instead of
158     using a pointer, if auto is currently the default, we need to
159     reset it.  */
160  if (strcmp (current_cp_abi.shortname, "auto") == 0)
161    switch_to_cp_abi ("auto");
162}
163
164/* Return the ABI operations associated with SHORT_NAME.  */
165
166static struct cp_abi_ops *
167find_cp_abi (const char *short_name)
168{
169  int i;
170
171  for (i = 0; i < num_cp_abis; i++)
172    if (strcmp (cp_abis[i]->shortname, short_name) == 0)
173      return cp_abis[i];
174
175  return NULL;
176}
177
178/* Display the list of registered C++ ABIs.  */
179
180static void
181list_cp_abis (int from_tty)
182{
183  struct cleanup *cleanup_chain;
184  int i;
185  ui_out_text (uiout, "The available C++ ABIs are:\n");
186
187  cleanup_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "cp-abi-list");
188  for (i = 0; i < num_cp_abis; i++)
189    {
190      char pad[14];
191      int padcount;
192
193      ui_out_text (uiout, "  ");
194      ui_out_field_string (uiout, "cp-abi", cp_abis[i]->shortname);
195
196      padcount = 16 - 2 - strlen (cp_abis[i]->shortname);
197      pad[padcount] = 0;
198      while (padcount > 0)
199	pad[--padcount] = ' ';
200      ui_out_text (uiout, pad);
201
202      ui_out_field_string (uiout, "doc", cp_abis[i]->doc);
203      ui_out_text (uiout, "\n");
204    }
205  do_cleanups (cleanup_chain);
206}
207
208/* Set the current C++ ABI, or display the list of options if no
209   argument is given.  */
210
211static void
212set_cp_abi_cmd (char *args, int from_tty)
213{
214  if (args == NULL)
215    {
216      list_cp_abis (from_tty);
217      return;
218    }
219
220  if (!switch_to_cp_abi (args))
221    error ("Could not find \"%s\" in ABI list", args);
222}
223
224/* Show the currently selected C++ ABI.  */
225
226static void
227show_cp_abi_cmd (char *args, int from_tty)
228{
229  ui_out_text (uiout, "The currently selected C++ ABI is \"");
230
231  ui_out_field_string (uiout, "cp-abi", current_cp_abi.shortname);
232  ui_out_text (uiout, "\" (");
233  ui_out_field_string (uiout, "longname", current_cp_abi.longname);
234  ui_out_text (uiout, ").\n");
235}
236
237extern initialize_file_ftype _initialize_cp_abi; /* -Wmissing-prototypes */
238
239void
240_initialize_cp_abi (void)
241{
242  register_cp_abi (&auto_cp_abi);
243  switch_to_cp_abi ("auto");
244
245  add_cmd ("cp-abi", class_obscure, set_cp_abi_cmd,
246	   "Set the ABI used for inspecting C++ objects.\n"
247	   "\"set cp-abi\" with no arguments will list the available ABIs.",
248	   &setlist);
249
250  add_cmd ("cp-abi", class_obscure, show_cp_abi_cmd,
251	   "Show the ABI used for inspecting C++ objects.", &showlist);
252}
253