xref: /openbsd/gnu/usr.bin/binutils/gdb/cp-abi.c (revision b725ae77)
1 /* Generic code for supporting multiple C++ ABI's
2    Copyright 2001, 2002, 2003 Free Software Foundation, Inc.
3 
4    This file is part of GDB.
5 
6    This program is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 2 of the License, or
9    (at your option) any later version.
10 
11    This program is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15 
16    You should have received a copy of the GNU General Public License
17    along with this program; if not, write to the Free Software
18    Foundation, Inc., 59 Temple Place - Suite 330,
19    Boston, MA 02111-1307, USA.  */
20 
21 #include "defs.h"
22 #include "value.h"
23 #include "cp-abi.h"
24 #include "command.h"
25 #include "gdbcmd.h"
26 #include "ui-out.h"
27 
28 #include "gdb_string.h"
29 
30 static struct cp_abi_ops *find_cp_abi (const char *short_name);
31 
32 static struct cp_abi_ops current_cp_abi = { "", NULL };
33 static struct cp_abi_ops auto_cp_abi = { "auto", NULL };
34 
35 #define CP_ABI_MAX 8
36 static struct cp_abi_ops *cp_abis[CP_ABI_MAX];
37 static int num_cp_abis = 0;
38 
39 enum ctor_kinds
is_constructor_name(const char * name)40 is_constructor_name (const char *name)
41 {
42   if ((current_cp_abi.is_constructor_name) == NULL)
43     error ("ABI doesn't define required function is_constructor_name");
44   return (*current_cp_abi.is_constructor_name) (name);
45 }
46 
47 enum dtor_kinds
is_destructor_name(const char * name)48 is_destructor_name (const char *name)
49 {
50   if ((current_cp_abi.is_destructor_name) == NULL)
51     error ("ABI doesn't define required function is_destructor_name");
52   return (*current_cp_abi.is_destructor_name) (name);
53 }
54 
55 int
is_vtable_name(const char * name)56 is_vtable_name (const char *name)
57 {
58   if ((current_cp_abi.is_vtable_name) == NULL)
59     error ("ABI doesn't define required function is_vtable_name");
60   return (*current_cp_abi.is_vtable_name) (name);
61 }
62 
63 int
is_operator_name(const char * name)64 is_operator_name (const char *name)
65 {
66   if ((current_cp_abi.is_operator_name) == NULL)
67     error ("ABI doesn't define required function is_operator_name");
68   return (*current_cp_abi.is_operator_name) (name);
69 }
70 
71 int
baseclass_offset(struct type * type,int index,char * valaddr,CORE_ADDR address)72 baseclass_offset (struct type *type, int index, char *valaddr,
73 		  CORE_ADDR address)
74 {
75   if (current_cp_abi.baseclass_offset == NULL)
76     error ("ABI doesn't define required function baseclass_offset");
77   return (*current_cp_abi.baseclass_offset) (type, index, valaddr, address);
78 }
79 
80 struct value *
value_virtual_fn_field(struct value ** arg1p,struct fn_field * f,int j,struct type * type,int offset)81 value_virtual_fn_field (struct value **arg1p, struct fn_field *f, int j,
82 			struct type *type, int offset)
83 {
84   if ((current_cp_abi.virtual_fn_field) == NULL)
85     return NULL;
86   return (*current_cp_abi.virtual_fn_field) (arg1p, f, j, type, offset);
87 }
88 
89 struct type *
value_rtti_type(struct value * v,int * full,int * top,int * using_enc)90 value_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 
99 static int
switch_to_cp_abi(const char * short_name)100 switch_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 
114 int
register_cp_abi(struct cp_abi_ops * abi)115 register_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 
128 void
set_cp_abi_as_auto_default(const char * short_name)129 set_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 
166 static struct cp_abi_ops *
find_cp_abi(const char * short_name)167 find_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 
180 static void
list_cp_abis(int from_tty)181 list_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 
211 static void
set_cp_abi_cmd(char * args,int from_tty)212 set_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 
226 static void
show_cp_abi_cmd(char * args,int from_tty)227 show_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 
237 extern initialize_file_ftype _initialize_cp_abi; /* -Wmissing-prototypes */
238 
239 void
_initialize_cp_abi(void)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