xref: /dflybsd-src/contrib/gdb-7/gdb/cp-abi.c (revision 5796c8dc12c637f18a1740c26afd8d40ffa9b719)
1*5796c8dcSSimon Schubert /* Generic code for supporting multiple C++ ABI's
2*5796c8dcSSimon Schubert 
3*5796c8dcSSimon Schubert    Copyright (C) 2001, 2002, 2003, 2005, 2006, 2007, 2008, 2009
4*5796c8dcSSimon Schubert    Free Software Foundation, Inc.
5*5796c8dcSSimon Schubert 
6*5796c8dcSSimon Schubert    This file is part of GDB.
7*5796c8dcSSimon Schubert 
8*5796c8dcSSimon Schubert    This program is free software; you can redistribute it and/or modify
9*5796c8dcSSimon Schubert    it under the terms of the GNU General Public License as published by
10*5796c8dcSSimon Schubert    the Free Software Foundation; either version 3 of the License, or
11*5796c8dcSSimon Schubert    (at your option) any later version.
12*5796c8dcSSimon Schubert 
13*5796c8dcSSimon Schubert    This program is distributed in the hope that it will be useful,
14*5796c8dcSSimon Schubert    but WITHOUT ANY WARRANTY; without even the implied warranty of
15*5796c8dcSSimon Schubert    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16*5796c8dcSSimon Schubert    GNU General Public License for more details.
17*5796c8dcSSimon Schubert 
18*5796c8dcSSimon Schubert    You should have received a copy of the GNU General Public License
19*5796c8dcSSimon Schubert    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
20*5796c8dcSSimon Schubert 
21*5796c8dcSSimon Schubert #include "defs.h"
22*5796c8dcSSimon Schubert #include "value.h"
23*5796c8dcSSimon Schubert #include "cp-abi.h"
24*5796c8dcSSimon Schubert #include "command.h"
25*5796c8dcSSimon Schubert #include "exceptions.h"
26*5796c8dcSSimon Schubert #include "gdbcmd.h"
27*5796c8dcSSimon Schubert #include "ui-out.h"
28*5796c8dcSSimon Schubert 
29*5796c8dcSSimon Schubert #include "gdb_string.h"
30*5796c8dcSSimon Schubert 
31*5796c8dcSSimon Schubert static struct cp_abi_ops *find_cp_abi (const char *short_name);
32*5796c8dcSSimon Schubert 
33*5796c8dcSSimon Schubert static struct cp_abi_ops current_cp_abi = { "", NULL };
34*5796c8dcSSimon Schubert static struct cp_abi_ops auto_cp_abi = { "auto", NULL };
35*5796c8dcSSimon Schubert 
36*5796c8dcSSimon Schubert #define CP_ABI_MAX 8
37*5796c8dcSSimon Schubert static struct cp_abi_ops *cp_abis[CP_ABI_MAX];
38*5796c8dcSSimon Schubert static int num_cp_abis = 0;
39*5796c8dcSSimon Schubert 
40*5796c8dcSSimon Schubert enum ctor_kinds
41*5796c8dcSSimon Schubert is_constructor_name (const char *name)
42*5796c8dcSSimon Schubert {
43*5796c8dcSSimon Schubert   if ((current_cp_abi.is_constructor_name) == NULL)
44*5796c8dcSSimon Schubert     error (_("ABI doesn't define required function is_constructor_name"));
45*5796c8dcSSimon Schubert   return (*current_cp_abi.is_constructor_name) (name);
46*5796c8dcSSimon Schubert }
47*5796c8dcSSimon Schubert 
48*5796c8dcSSimon Schubert enum dtor_kinds
49*5796c8dcSSimon Schubert is_destructor_name (const char *name)
50*5796c8dcSSimon Schubert {
51*5796c8dcSSimon Schubert   if ((current_cp_abi.is_destructor_name) == NULL)
52*5796c8dcSSimon Schubert     error (_("ABI doesn't define required function is_destructor_name"));
53*5796c8dcSSimon Schubert   return (*current_cp_abi.is_destructor_name) (name);
54*5796c8dcSSimon Schubert }
55*5796c8dcSSimon Schubert 
56*5796c8dcSSimon Schubert int
57*5796c8dcSSimon Schubert is_vtable_name (const char *name)
58*5796c8dcSSimon Schubert {
59*5796c8dcSSimon Schubert   if ((current_cp_abi.is_vtable_name) == NULL)
60*5796c8dcSSimon Schubert     error (_("ABI doesn't define required function is_vtable_name"));
61*5796c8dcSSimon Schubert   return (*current_cp_abi.is_vtable_name) (name);
62*5796c8dcSSimon Schubert }
63*5796c8dcSSimon Schubert 
64*5796c8dcSSimon Schubert int
65*5796c8dcSSimon Schubert is_operator_name (const char *name)
66*5796c8dcSSimon Schubert {
67*5796c8dcSSimon Schubert   if ((current_cp_abi.is_operator_name) == NULL)
68*5796c8dcSSimon Schubert     error (_("ABI doesn't define required function is_operator_name"));
69*5796c8dcSSimon Schubert   return (*current_cp_abi.is_operator_name) (name);
70*5796c8dcSSimon Schubert }
71*5796c8dcSSimon Schubert 
72*5796c8dcSSimon Schubert int
73*5796c8dcSSimon Schubert baseclass_offset (struct type *type, int index, const bfd_byte *valaddr,
74*5796c8dcSSimon Schubert 		  CORE_ADDR address)
75*5796c8dcSSimon Schubert {
76*5796c8dcSSimon Schubert   if (current_cp_abi.baseclass_offset == NULL)
77*5796c8dcSSimon Schubert     error (_("ABI doesn't define required function baseclass_offset"));
78*5796c8dcSSimon Schubert   return (*current_cp_abi.baseclass_offset) (type, index, valaddr, address);
79*5796c8dcSSimon Schubert }
80*5796c8dcSSimon Schubert 
81*5796c8dcSSimon Schubert struct value *
82*5796c8dcSSimon Schubert value_virtual_fn_field (struct value **arg1p, struct fn_field *f, int j,
83*5796c8dcSSimon Schubert 			struct type *type, int offset)
84*5796c8dcSSimon Schubert {
85*5796c8dcSSimon Schubert   if ((current_cp_abi.virtual_fn_field) == NULL)
86*5796c8dcSSimon Schubert     return NULL;
87*5796c8dcSSimon Schubert   return (*current_cp_abi.virtual_fn_field) (arg1p, f, j, type, offset);
88*5796c8dcSSimon Schubert }
89*5796c8dcSSimon Schubert 
90*5796c8dcSSimon Schubert struct type *
91*5796c8dcSSimon Schubert value_rtti_type (struct value *v, int *full, int *top, int *using_enc)
92*5796c8dcSSimon Schubert {
93*5796c8dcSSimon Schubert   struct type *ret = NULL;
94*5796c8dcSSimon Schubert   struct gdb_exception e;
95*5796c8dcSSimon Schubert   if ((current_cp_abi.rtti_type) == NULL)
96*5796c8dcSSimon Schubert     return NULL;
97*5796c8dcSSimon Schubert   TRY_CATCH (e, RETURN_MASK_ERROR)
98*5796c8dcSSimon Schubert     {
99*5796c8dcSSimon Schubert       ret = (*current_cp_abi.rtti_type) (v, full, top, using_enc);
100*5796c8dcSSimon Schubert     }
101*5796c8dcSSimon Schubert   if (e.reason < 0)
102*5796c8dcSSimon Schubert     return NULL;
103*5796c8dcSSimon Schubert   return ret;
104*5796c8dcSSimon Schubert }
105*5796c8dcSSimon Schubert 
106*5796c8dcSSimon Schubert void
107*5796c8dcSSimon Schubert cplus_print_method_ptr (const gdb_byte *contents, struct type *type,
108*5796c8dcSSimon Schubert 			struct ui_file *stream)
109*5796c8dcSSimon Schubert {
110*5796c8dcSSimon Schubert   if (current_cp_abi.print_method_ptr == NULL)
111*5796c8dcSSimon Schubert     error (_("GDB does not support pointers to methods on this target"));
112*5796c8dcSSimon Schubert   (*current_cp_abi.print_method_ptr) (contents, type, stream);
113*5796c8dcSSimon Schubert }
114*5796c8dcSSimon Schubert 
115*5796c8dcSSimon Schubert int
116*5796c8dcSSimon Schubert cplus_method_ptr_size (struct type *to_type)
117*5796c8dcSSimon Schubert {
118*5796c8dcSSimon Schubert   if (current_cp_abi.method_ptr_size == NULL)
119*5796c8dcSSimon Schubert     error (_("GDB does not support pointers to methods on this target"));
120*5796c8dcSSimon Schubert   return (*current_cp_abi.method_ptr_size) (to_type);
121*5796c8dcSSimon Schubert }
122*5796c8dcSSimon Schubert 
123*5796c8dcSSimon Schubert void
124*5796c8dcSSimon Schubert cplus_make_method_ptr (struct type *type, gdb_byte *contents,
125*5796c8dcSSimon Schubert 		       CORE_ADDR value, int is_virtual)
126*5796c8dcSSimon Schubert {
127*5796c8dcSSimon Schubert   if (current_cp_abi.make_method_ptr == NULL)
128*5796c8dcSSimon Schubert     error (_("GDB does not support pointers to methods on this target"));
129*5796c8dcSSimon Schubert   (*current_cp_abi.make_method_ptr) (type, contents, value, is_virtual);
130*5796c8dcSSimon Schubert }
131*5796c8dcSSimon Schubert 
132*5796c8dcSSimon Schubert CORE_ADDR
133*5796c8dcSSimon Schubert cplus_skip_trampoline (struct frame_info *frame, CORE_ADDR stop_pc)
134*5796c8dcSSimon Schubert {
135*5796c8dcSSimon Schubert   if (current_cp_abi.skip_trampoline == NULL)
136*5796c8dcSSimon Schubert     return 0;
137*5796c8dcSSimon Schubert   return (*current_cp_abi.skip_trampoline) (frame, stop_pc);
138*5796c8dcSSimon Schubert }
139*5796c8dcSSimon Schubert 
140*5796c8dcSSimon Schubert struct value *
141*5796c8dcSSimon Schubert cplus_method_ptr_to_value (struct value **this_p, struct value *method_ptr)
142*5796c8dcSSimon Schubert {
143*5796c8dcSSimon Schubert   if (current_cp_abi.method_ptr_to_value == NULL)
144*5796c8dcSSimon Schubert     error (_("GDB does not support pointers to methods on this target"));
145*5796c8dcSSimon Schubert   return (*current_cp_abi.method_ptr_to_value) (this_p, method_ptr);
146*5796c8dcSSimon Schubert }
147*5796c8dcSSimon Schubert 
148*5796c8dcSSimon Schubert int
149*5796c8dcSSimon Schubert cp_pass_by_reference (struct type *type)
150*5796c8dcSSimon Schubert {
151*5796c8dcSSimon Schubert   if ((current_cp_abi.pass_by_reference) == NULL)
152*5796c8dcSSimon Schubert     return 0;
153*5796c8dcSSimon Schubert   return (*current_cp_abi.pass_by_reference) (type);
154*5796c8dcSSimon Schubert }
155*5796c8dcSSimon Schubert 
156*5796c8dcSSimon Schubert /* Set the current C++ ABI to SHORT_NAME.  */
157*5796c8dcSSimon Schubert 
158*5796c8dcSSimon Schubert static int
159*5796c8dcSSimon Schubert switch_to_cp_abi (const char *short_name)
160*5796c8dcSSimon Schubert {
161*5796c8dcSSimon Schubert   struct cp_abi_ops *abi;
162*5796c8dcSSimon Schubert 
163*5796c8dcSSimon Schubert   abi = find_cp_abi (short_name);
164*5796c8dcSSimon Schubert   if (abi == NULL)
165*5796c8dcSSimon Schubert     return 0;
166*5796c8dcSSimon Schubert 
167*5796c8dcSSimon Schubert   current_cp_abi = *abi;
168*5796c8dcSSimon Schubert   return 1;
169*5796c8dcSSimon Schubert }
170*5796c8dcSSimon Schubert 
171*5796c8dcSSimon Schubert /* Add ABI to the list of supported C++ ABI's.  */
172*5796c8dcSSimon Schubert 
173*5796c8dcSSimon Schubert int
174*5796c8dcSSimon Schubert register_cp_abi (struct cp_abi_ops *abi)
175*5796c8dcSSimon Schubert {
176*5796c8dcSSimon Schubert   if (num_cp_abis == CP_ABI_MAX)
177*5796c8dcSSimon Schubert     internal_error (__FILE__, __LINE__,
178*5796c8dcSSimon Schubert 		    _("Too many C++ ABIs, please increase CP_ABI_MAX in cp-abi.c"));
179*5796c8dcSSimon Schubert 
180*5796c8dcSSimon Schubert   cp_abis[num_cp_abis++] = abi;
181*5796c8dcSSimon Schubert 
182*5796c8dcSSimon Schubert   return 1;
183*5796c8dcSSimon Schubert }
184*5796c8dcSSimon Schubert 
185*5796c8dcSSimon Schubert /* Set the ABI to use in "auto" mode to SHORT_NAME.  */
186*5796c8dcSSimon Schubert 
187*5796c8dcSSimon Schubert void
188*5796c8dcSSimon Schubert set_cp_abi_as_auto_default (const char *short_name)
189*5796c8dcSSimon Schubert {
190*5796c8dcSSimon Schubert   char *new_longname, *new_doc;
191*5796c8dcSSimon Schubert   struct cp_abi_ops *abi = find_cp_abi (short_name);
192*5796c8dcSSimon Schubert 
193*5796c8dcSSimon Schubert   if (abi == NULL)
194*5796c8dcSSimon Schubert     internal_error (__FILE__, __LINE__,
195*5796c8dcSSimon Schubert 		    _("Cannot find C++ ABI \"%s\" to set it as auto default."),
196*5796c8dcSSimon Schubert 		    short_name);
197*5796c8dcSSimon Schubert 
198*5796c8dcSSimon Schubert   if (auto_cp_abi.longname != NULL)
199*5796c8dcSSimon Schubert     xfree ((char *) auto_cp_abi.longname);
200*5796c8dcSSimon Schubert   if (auto_cp_abi.doc != NULL)
201*5796c8dcSSimon Schubert     xfree ((char *) auto_cp_abi.doc);
202*5796c8dcSSimon Schubert 
203*5796c8dcSSimon Schubert   auto_cp_abi = *abi;
204*5796c8dcSSimon Schubert 
205*5796c8dcSSimon Schubert   auto_cp_abi.shortname = "auto";
206*5796c8dcSSimon Schubert   new_longname = xstrprintf ("currently \"%s\"", abi->shortname);
207*5796c8dcSSimon Schubert   auto_cp_abi.longname = new_longname;
208*5796c8dcSSimon Schubert 
209*5796c8dcSSimon Schubert   new_doc = xstrprintf ("Automatically selected; currently \"%s\"",
210*5796c8dcSSimon Schubert 	     abi->shortname);
211*5796c8dcSSimon Schubert   auto_cp_abi.doc = new_doc;
212*5796c8dcSSimon Schubert 
213*5796c8dcSSimon Schubert   /* Since we copy the current ABI into current_cp_abi instead of
214*5796c8dcSSimon Schubert      using a pointer, if auto is currently the default, we need to
215*5796c8dcSSimon Schubert      reset it.  */
216*5796c8dcSSimon Schubert   if (strcmp (current_cp_abi.shortname, "auto") == 0)
217*5796c8dcSSimon Schubert     switch_to_cp_abi ("auto");
218*5796c8dcSSimon Schubert }
219*5796c8dcSSimon Schubert 
220*5796c8dcSSimon Schubert /* Return the ABI operations associated with SHORT_NAME.  */
221*5796c8dcSSimon Schubert 
222*5796c8dcSSimon Schubert static struct cp_abi_ops *
223*5796c8dcSSimon Schubert find_cp_abi (const char *short_name)
224*5796c8dcSSimon Schubert {
225*5796c8dcSSimon Schubert   int i;
226*5796c8dcSSimon Schubert 
227*5796c8dcSSimon Schubert   for (i = 0; i < num_cp_abis; i++)
228*5796c8dcSSimon Schubert     if (strcmp (cp_abis[i]->shortname, short_name) == 0)
229*5796c8dcSSimon Schubert       return cp_abis[i];
230*5796c8dcSSimon Schubert 
231*5796c8dcSSimon Schubert   return NULL;
232*5796c8dcSSimon Schubert }
233*5796c8dcSSimon Schubert 
234*5796c8dcSSimon Schubert /* Display the list of registered C++ ABIs.  */
235*5796c8dcSSimon Schubert 
236*5796c8dcSSimon Schubert static void
237*5796c8dcSSimon Schubert list_cp_abis (int from_tty)
238*5796c8dcSSimon Schubert {
239*5796c8dcSSimon Schubert   struct cleanup *cleanup_chain;
240*5796c8dcSSimon Schubert   int i;
241*5796c8dcSSimon Schubert   ui_out_text (uiout, "The available C++ ABIs are:\n");
242*5796c8dcSSimon Schubert 
243*5796c8dcSSimon Schubert   cleanup_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "cp-abi-list");
244*5796c8dcSSimon Schubert   for (i = 0; i < num_cp_abis; i++)
245*5796c8dcSSimon Schubert     {
246*5796c8dcSSimon Schubert       char pad[14];
247*5796c8dcSSimon Schubert       int padcount;
248*5796c8dcSSimon Schubert 
249*5796c8dcSSimon Schubert       ui_out_text (uiout, "  ");
250*5796c8dcSSimon Schubert       ui_out_field_string (uiout, "cp-abi", cp_abis[i]->shortname);
251*5796c8dcSSimon Schubert 
252*5796c8dcSSimon Schubert       padcount = 16 - 2 - strlen (cp_abis[i]->shortname);
253*5796c8dcSSimon Schubert       pad[padcount] = 0;
254*5796c8dcSSimon Schubert       while (padcount > 0)
255*5796c8dcSSimon Schubert 	pad[--padcount] = ' ';
256*5796c8dcSSimon Schubert       ui_out_text (uiout, pad);
257*5796c8dcSSimon Schubert 
258*5796c8dcSSimon Schubert       ui_out_field_string (uiout, "doc", cp_abis[i]->doc);
259*5796c8dcSSimon Schubert       ui_out_text (uiout, "\n");
260*5796c8dcSSimon Schubert     }
261*5796c8dcSSimon Schubert   do_cleanups (cleanup_chain);
262*5796c8dcSSimon Schubert }
263*5796c8dcSSimon Schubert 
264*5796c8dcSSimon Schubert /* Set the current C++ ABI, or display the list of options if no
265*5796c8dcSSimon Schubert    argument is given.  */
266*5796c8dcSSimon Schubert 
267*5796c8dcSSimon Schubert static void
268*5796c8dcSSimon Schubert set_cp_abi_cmd (char *args, int from_tty)
269*5796c8dcSSimon Schubert {
270*5796c8dcSSimon Schubert   if (args == NULL)
271*5796c8dcSSimon Schubert     {
272*5796c8dcSSimon Schubert       list_cp_abis (from_tty);
273*5796c8dcSSimon Schubert       return;
274*5796c8dcSSimon Schubert     }
275*5796c8dcSSimon Schubert 
276*5796c8dcSSimon Schubert   if (!switch_to_cp_abi (args))
277*5796c8dcSSimon Schubert     error (_("Could not find \"%s\" in ABI list"), args);
278*5796c8dcSSimon Schubert }
279*5796c8dcSSimon Schubert 
280*5796c8dcSSimon Schubert /* Show the currently selected C++ ABI.  */
281*5796c8dcSSimon Schubert 
282*5796c8dcSSimon Schubert static void
283*5796c8dcSSimon Schubert show_cp_abi_cmd (char *args, int from_tty)
284*5796c8dcSSimon Schubert {
285*5796c8dcSSimon Schubert   ui_out_text (uiout, "The currently selected C++ ABI is \"");
286*5796c8dcSSimon Schubert 
287*5796c8dcSSimon Schubert   ui_out_field_string (uiout, "cp-abi", current_cp_abi.shortname);
288*5796c8dcSSimon Schubert   ui_out_text (uiout, "\" (");
289*5796c8dcSSimon Schubert   ui_out_field_string (uiout, "longname", current_cp_abi.longname);
290*5796c8dcSSimon Schubert   ui_out_text (uiout, ").\n");
291*5796c8dcSSimon Schubert }
292*5796c8dcSSimon Schubert 
293*5796c8dcSSimon Schubert extern initialize_file_ftype _initialize_cp_abi; /* -Wmissing-prototypes */
294*5796c8dcSSimon Schubert 
295*5796c8dcSSimon Schubert void
296*5796c8dcSSimon Schubert _initialize_cp_abi (void)
297*5796c8dcSSimon Schubert {
298*5796c8dcSSimon Schubert   register_cp_abi (&auto_cp_abi);
299*5796c8dcSSimon Schubert   switch_to_cp_abi ("auto");
300*5796c8dcSSimon Schubert 
301*5796c8dcSSimon Schubert   add_cmd ("cp-abi", class_obscure, set_cp_abi_cmd, _("\
302*5796c8dcSSimon Schubert Set the ABI used for inspecting C++ objects.\n\
303*5796c8dcSSimon Schubert \"set cp-abi\" with no arguments will list the available ABIs."),
304*5796c8dcSSimon Schubert 	   &setlist);
305*5796c8dcSSimon Schubert 
306*5796c8dcSSimon Schubert   add_cmd ("cp-abi", class_obscure, show_cp_abi_cmd,
307*5796c8dcSSimon Schubert 	   _("Show the ABI used for inspecting C++ objects."), &showlist);
308*5796c8dcSSimon Schubert }
309