xref: /dflybsd-src/contrib/gdb-7/gdb/cp-abi.c (revision a45ae5f869d9cfcb3e41dbab486e10bfa9e336bf)
15796c8dcSSimon Schubert /* Generic code for supporting multiple C++ ABI's
25796c8dcSSimon Schubert 
3*a45ae5f8SJohn Marino    Copyright (C) 2001-2003, 2005-2012 Free Software Foundation, Inc.
45796c8dcSSimon Schubert 
55796c8dcSSimon Schubert    This file is part of GDB.
65796c8dcSSimon Schubert 
75796c8dcSSimon Schubert    This program is free software; you can redistribute it and/or modify
85796c8dcSSimon Schubert    it under the terms of the GNU General Public License as published by
95796c8dcSSimon Schubert    the Free Software Foundation; either version 3 of the License, or
105796c8dcSSimon Schubert    (at your option) any later version.
115796c8dcSSimon Schubert 
125796c8dcSSimon Schubert    This program is distributed in the hope that it will be useful,
135796c8dcSSimon Schubert    but WITHOUT ANY WARRANTY; without even the implied warranty of
145796c8dcSSimon Schubert    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
155796c8dcSSimon Schubert    GNU General Public License for more details.
165796c8dcSSimon Schubert 
175796c8dcSSimon Schubert    You should have received a copy of the GNU General Public License
185796c8dcSSimon Schubert    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
195796c8dcSSimon Schubert 
205796c8dcSSimon Schubert #include "defs.h"
215796c8dcSSimon Schubert #include "value.h"
225796c8dcSSimon Schubert #include "cp-abi.h"
235796c8dcSSimon Schubert #include "command.h"
245796c8dcSSimon Schubert #include "exceptions.h"
255796c8dcSSimon Schubert #include "gdbcmd.h"
265796c8dcSSimon Schubert #include "ui-out.h"
27c50c785cSJohn Marino #include "gdb_assert.h"
285796c8dcSSimon Schubert #include "gdb_string.h"
295796c8dcSSimon Schubert 
305796c8dcSSimon Schubert static struct cp_abi_ops *find_cp_abi (const char *short_name);
315796c8dcSSimon Schubert 
325796c8dcSSimon Schubert static struct cp_abi_ops current_cp_abi = { "", NULL };
335796c8dcSSimon Schubert static struct cp_abi_ops auto_cp_abi = { "auto", NULL };
345796c8dcSSimon Schubert 
355796c8dcSSimon Schubert #define CP_ABI_MAX 8
365796c8dcSSimon Schubert static struct cp_abi_ops *cp_abis[CP_ABI_MAX];
375796c8dcSSimon Schubert static int num_cp_abis = 0;
385796c8dcSSimon Schubert 
395796c8dcSSimon Schubert enum ctor_kinds
405796c8dcSSimon Schubert is_constructor_name (const char *name)
415796c8dcSSimon Schubert {
425796c8dcSSimon Schubert   if ((current_cp_abi.is_constructor_name) == NULL)
435796c8dcSSimon Schubert     error (_("ABI doesn't define required function is_constructor_name"));
445796c8dcSSimon Schubert   return (*current_cp_abi.is_constructor_name) (name);
455796c8dcSSimon Schubert }
465796c8dcSSimon Schubert 
475796c8dcSSimon Schubert enum dtor_kinds
485796c8dcSSimon Schubert is_destructor_name (const char *name)
495796c8dcSSimon Schubert {
505796c8dcSSimon Schubert   if ((current_cp_abi.is_destructor_name) == NULL)
515796c8dcSSimon Schubert     error (_("ABI doesn't define required function is_destructor_name"));
525796c8dcSSimon Schubert   return (*current_cp_abi.is_destructor_name) (name);
535796c8dcSSimon Schubert }
545796c8dcSSimon Schubert 
555796c8dcSSimon Schubert int
565796c8dcSSimon Schubert is_vtable_name (const char *name)
575796c8dcSSimon Schubert {
585796c8dcSSimon Schubert   if ((current_cp_abi.is_vtable_name) == NULL)
595796c8dcSSimon Schubert     error (_("ABI doesn't define required function is_vtable_name"));
605796c8dcSSimon Schubert   return (*current_cp_abi.is_vtable_name) (name);
615796c8dcSSimon Schubert }
625796c8dcSSimon Schubert 
635796c8dcSSimon Schubert int
645796c8dcSSimon Schubert is_operator_name (const char *name)
655796c8dcSSimon Schubert {
665796c8dcSSimon Schubert   if ((current_cp_abi.is_operator_name) == NULL)
675796c8dcSSimon Schubert     error (_("ABI doesn't define required function is_operator_name"));
685796c8dcSSimon Schubert   return (*current_cp_abi.is_operator_name) (name);
695796c8dcSSimon Schubert }
705796c8dcSSimon Schubert 
715796c8dcSSimon Schubert int
72c50c785cSJohn Marino baseclass_offset (struct type *type, int index, const gdb_byte *valaddr,
73c50c785cSJohn Marino 		  int embedded_offset, CORE_ADDR address,
74c50c785cSJohn Marino 		  const struct value *val)
755796c8dcSSimon Schubert {
76c50c785cSJohn Marino   volatile struct gdb_exception ex;
77c50c785cSJohn Marino   int res = 0;
78c50c785cSJohn Marino 
79c50c785cSJohn Marino   gdb_assert (current_cp_abi.baseclass_offset != NULL);
80c50c785cSJohn Marino 
81c50c785cSJohn Marino   TRY_CATCH (ex, RETURN_MASK_ERROR)
82c50c785cSJohn Marino     {
83c50c785cSJohn Marino       res = (*current_cp_abi.baseclass_offset) (type, index, valaddr,
84c50c785cSJohn Marino 						embedded_offset,
85c50c785cSJohn Marino 						address, val);
86c50c785cSJohn Marino     }
87c50c785cSJohn Marino 
88c50c785cSJohn Marino   if (ex.reason < 0 && ex.error == NOT_AVAILABLE_ERROR)
89c50c785cSJohn Marino     throw_error (NOT_AVAILABLE_ERROR,
90c50c785cSJohn Marino 		 _("Cannot determine virtual baseclass offset "
91c50c785cSJohn Marino 		   "of incomplete object"));
92c50c785cSJohn Marino   else if (ex.reason < 0)
93c50c785cSJohn Marino     throw_exception (ex);
94c50c785cSJohn Marino   else
95c50c785cSJohn Marino     return res;
965796c8dcSSimon Schubert }
975796c8dcSSimon Schubert 
985796c8dcSSimon Schubert struct value *
99c50c785cSJohn Marino value_virtual_fn_field (struct value **arg1p,
100c50c785cSJohn Marino 			struct fn_field *f, int j,
1015796c8dcSSimon Schubert 			struct type *type, int offset)
1025796c8dcSSimon Schubert {
1035796c8dcSSimon Schubert   if ((current_cp_abi.virtual_fn_field) == NULL)
1045796c8dcSSimon Schubert     return NULL;
105c50c785cSJohn Marino   return (*current_cp_abi.virtual_fn_field) (arg1p, f, j,
106c50c785cSJohn Marino 					     type, offset);
1075796c8dcSSimon Schubert }
1085796c8dcSSimon Schubert 
1095796c8dcSSimon Schubert struct type *
110c50c785cSJohn Marino value_rtti_type (struct value *v, int *full,
111c50c785cSJohn Marino 		 int *top, int *using_enc)
1125796c8dcSSimon Schubert {
1135796c8dcSSimon Schubert   struct type *ret = NULL;
1145796c8dcSSimon Schubert   struct gdb_exception e;
115cf7f2e2dSJohn Marino 
1165796c8dcSSimon Schubert   if ((current_cp_abi.rtti_type) == NULL)
1175796c8dcSSimon Schubert     return NULL;
1185796c8dcSSimon Schubert   TRY_CATCH (e, RETURN_MASK_ERROR)
1195796c8dcSSimon Schubert     {
1205796c8dcSSimon Schubert       ret = (*current_cp_abi.rtti_type) (v, full, top, using_enc);
1215796c8dcSSimon Schubert     }
1225796c8dcSSimon Schubert   if (e.reason < 0)
1235796c8dcSSimon Schubert     return NULL;
1245796c8dcSSimon Schubert   return ret;
1255796c8dcSSimon Schubert }
1265796c8dcSSimon Schubert 
1275796c8dcSSimon Schubert void
128c50c785cSJohn Marino cplus_print_method_ptr (const gdb_byte *contents,
129c50c785cSJohn Marino 			struct type *type,
1305796c8dcSSimon Schubert 			struct ui_file *stream)
1315796c8dcSSimon Schubert {
1325796c8dcSSimon Schubert   if (current_cp_abi.print_method_ptr == NULL)
1335796c8dcSSimon Schubert     error (_("GDB does not support pointers to methods on this target"));
1345796c8dcSSimon Schubert   (*current_cp_abi.print_method_ptr) (contents, type, stream);
1355796c8dcSSimon Schubert }
1365796c8dcSSimon Schubert 
1375796c8dcSSimon Schubert int
1385796c8dcSSimon Schubert cplus_method_ptr_size (struct type *to_type)
1395796c8dcSSimon Schubert {
1405796c8dcSSimon Schubert   if (current_cp_abi.method_ptr_size == NULL)
1415796c8dcSSimon Schubert     error (_("GDB does not support pointers to methods on this target"));
1425796c8dcSSimon Schubert   return (*current_cp_abi.method_ptr_size) (to_type);
1435796c8dcSSimon Schubert }
1445796c8dcSSimon Schubert 
1455796c8dcSSimon Schubert void
1465796c8dcSSimon Schubert cplus_make_method_ptr (struct type *type, gdb_byte *contents,
1475796c8dcSSimon Schubert 		       CORE_ADDR value, int is_virtual)
1485796c8dcSSimon Schubert {
1495796c8dcSSimon Schubert   if (current_cp_abi.make_method_ptr == NULL)
1505796c8dcSSimon Schubert     error (_("GDB does not support pointers to methods on this target"));
1515796c8dcSSimon Schubert   (*current_cp_abi.make_method_ptr) (type, contents, value, is_virtual);
1525796c8dcSSimon Schubert }
1535796c8dcSSimon Schubert 
1545796c8dcSSimon Schubert CORE_ADDR
155c50c785cSJohn Marino cplus_skip_trampoline (struct frame_info *frame,
156c50c785cSJohn Marino 		       CORE_ADDR stop_pc)
1575796c8dcSSimon Schubert {
1585796c8dcSSimon Schubert   if (current_cp_abi.skip_trampoline == NULL)
1595796c8dcSSimon Schubert     return 0;
1605796c8dcSSimon Schubert   return (*current_cp_abi.skip_trampoline) (frame, stop_pc);
1615796c8dcSSimon Schubert }
1625796c8dcSSimon Schubert 
1635796c8dcSSimon Schubert struct value *
164c50c785cSJohn Marino cplus_method_ptr_to_value (struct value **this_p,
165c50c785cSJohn Marino 			   struct value *method_ptr)
1665796c8dcSSimon Schubert {
1675796c8dcSSimon Schubert   if (current_cp_abi.method_ptr_to_value == NULL)
1685796c8dcSSimon Schubert     error (_("GDB does not support pointers to methods on this target"));
1695796c8dcSSimon Schubert   return (*current_cp_abi.method_ptr_to_value) (this_p, method_ptr);
1705796c8dcSSimon Schubert }
1715796c8dcSSimon Schubert 
1725796c8dcSSimon Schubert int
1735796c8dcSSimon Schubert cp_pass_by_reference (struct type *type)
1745796c8dcSSimon Schubert {
1755796c8dcSSimon Schubert   if ((current_cp_abi.pass_by_reference) == NULL)
1765796c8dcSSimon Schubert     return 0;
1775796c8dcSSimon Schubert   return (*current_cp_abi.pass_by_reference) (type);
1785796c8dcSSimon Schubert }
1795796c8dcSSimon Schubert 
1805796c8dcSSimon Schubert /* Set the current C++ ABI to SHORT_NAME.  */
1815796c8dcSSimon Schubert 
1825796c8dcSSimon Schubert static int
1835796c8dcSSimon Schubert switch_to_cp_abi (const char *short_name)
1845796c8dcSSimon Schubert {
1855796c8dcSSimon Schubert   struct cp_abi_ops *abi;
1865796c8dcSSimon Schubert 
1875796c8dcSSimon Schubert   abi = find_cp_abi (short_name);
1885796c8dcSSimon Schubert   if (abi == NULL)
1895796c8dcSSimon Schubert     return 0;
1905796c8dcSSimon Schubert 
1915796c8dcSSimon Schubert   current_cp_abi = *abi;
1925796c8dcSSimon Schubert   return 1;
1935796c8dcSSimon Schubert }
1945796c8dcSSimon Schubert 
1955796c8dcSSimon Schubert /* Add ABI to the list of supported C++ ABI's.  */
1965796c8dcSSimon Schubert 
1975796c8dcSSimon Schubert int
1985796c8dcSSimon Schubert register_cp_abi (struct cp_abi_ops *abi)
1995796c8dcSSimon Schubert {
2005796c8dcSSimon Schubert   if (num_cp_abis == CP_ABI_MAX)
2015796c8dcSSimon Schubert     internal_error (__FILE__, __LINE__,
202c50c785cSJohn Marino 		    _("Too many C++ ABIs, please increase "
203c50c785cSJohn Marino 		      "CP_ABI_MAX in cp-abi.c"));
2045796c8dcSSimon Schubert 
2055796c8dcSSimon Schubert   cp_abis[num_cp_abis++] = abi;
2065796c8dcSSimon Schubert 
2075796c8dcSSimon Schubert   return 1;
2085796c8dcSSimon Schubert }
2095796c8dcSSimon Schubert 
2105796c8dcSSimon Schubert /* Set the ABI to use in "auto" mode to SHORT_NAME.  */
2115796c8dcSSimon Schubert 
2125796c8dcSSimon Schubert void
2135796c8dcSSimon Schubert set_cp_abi_as_auto_default (const char *short_name)
2145796c8dcSSimon Schubert {
2155796c8dcSSimon Schubert   char *new_longname, *new_doc;
2165796c8dcSSimon Schubert   struct cp_abi_ops *abi = find_cp_abi (short_name);
2175796c8dcSSimon Schubert 
2185796c8dcSSimon Schubert   if (abi == NULL)
2195796c8dcSSimon Schubert     internal_error (__FILE__, __LINE__,
2205796c8dcSSimon Schubert 		    _("Cannot find C++ ABI \"%s\" to set it as auto default."),
2215796c8dcSSimon Schubert 		    short_name);
2225796c8dcSSimon Schubert 
2235796c8dcSSimon Schubert   if (auto_cp_abi.longname != NULL)
2245796c8dcSSimon Schubert     xfree ((char *) auto_cp_abi.longname);
2255796c8dcSSimon Schubert   if (auto_cp_abi.doc != NULL)
2265796c8dcSSimon Schubert     xfree ((char *) auto_cp_abi.doc);
2275796c8dcSSimon Schubert 
2285796c8dcSSimon Schubert   auto_cp_abi = *abi;
2295796c8dcSSimon Schubert 
2305796c8dcSSimon Schubert   auto_cp_abi.shortname = "auto";
2315796c8dcSSimon Schubert   new_longname = xstrprintf ("currently \"%s\"", abi->shortname);
2325796c8dcSSimon Schubert   auto_cp_abi.longname = new_longname;
2335796c8dcSSimon Schubert 
2345796c8dcSSimon Schubert   new_doc = xstrprintf ("Automatically selected; currently \"%s\"",
2355796c8dcSSimon Schubert 	     abi->shortname);
2365796c8dcSSimon Schubert   auto_cp_abi.doc = new_doc;
2375796c8dcSSimon Schubert 
2385796c8dcSSimon Schubert   /* Since we copy the current ABI into current_cp_abi instead of
2395796c8dcSSimon Schubert      using a pointer, if auto is currently the default, we need to
2405796c8dcSSimon Schubert      reset it.  */
2415796c8dcSSimon Schubert   if (strcmp (current_cp_abi.shortname, "auto") == 0)
2425796c8dcSSimon Schubert     switch_to_cp_abi ("auto");
2435796c8dcSSimon Schubert }
2445796c8dcSSimon Schubert 
2455796c8dcSSimon Schubert /* Return the ABI operations associated with SHORT_NAME.  */
2465796c8dcSSimon Schubert 
2475796c8dcSSimon Schubert static struct cp_abi_ops *
2485796c8dcSSimon Schubert find_cp_abi (const char *short_name)
2495796c8dcSSimon Schubert {
2505796c8dcSSimon Schubert   int i;
2515796c8dcSSimon Schubert 
2525796c8dcSSimon Schubert   for (i = 0; i < num_cp_abis; i++)
2535796c8dcSSimon Schubert     if (strcmp (cp_abis[i]->shortname, short_name) == 0)
2545796c8dcSSimon Schubert       return cp_abis[i];
2555796c8dcSSimon Schubert 
2565796c8dcSSimon Schubert   return NULL;
2575796c8dcSSimon Schubert }
2585796c8dcSSimon Schubert 
2595796c8dcSSimon Schubert /* Display the list of registered C++ ABIs.  */
2605796c8dcSSimon Schubert 
2615796c8dcSSimon Schubert static void
2625796c8dcSSimon Schubert list_cp_abis (int from_tty)
2635796c8dcSSimon Schubert {
264*a45ae5f8SJohn Marino   struct ui_out *uiout = current_uiout;
2655796c8dcSSimon Schubert   struct cleanup *cleanup_chain;
2665796c8dcSSimon Schubert   int i;
2675796c8dcSSimon Schubert 
268cf7f2e2dSJohn Marino   ui_out_text (uiout, "The available C++ ABIs are:\n");
269c50c785cSJohn Marino   cleanup_chain = make_cleanup_ui_out_tuple_begin_end (uiout,
270c50c785cSJohn Marino 						       "cp-abi-list");
2715796c8dcSSimon Schubert   for (i = 0; i < num_cp_abis; i++)
2725796c8dcSSimon Schubert     {
2735796c8dcSSimon Schubert       char pad[14];
2745796c8dcSSimon Schubert       int padcount;
2755796c8dcSSimon Schubert 
2765796c8dcSSimon Schubert       ui_out_text (uiout, "  ");
2775796c8dcSSimon Schubert       ui_out_field_string (uiout, "cp-abi", cp_abis[i]->shortname);
2785796c8dcSSimon Schubert 
2795796c8dcSSimon Schubert       padcount = 16 - 2 - strlen (cp_abis[i]->shortname);
2805796c8dcSSimon Schubert       pad[padcount] = 0;
2815796c8dcSSimon Schubert       while (padcount > 0)
2825796c8dcSSimon Schubert 	pad[--padcount] = ' ';
2835796c8dcSSimon Schubert       ui_out_text (uiout, pad);
2845796c8dcSSimon Schubert 
2855796c8dcSSimon Schubert       ui_out_field_string (uiout, "doc", cp_abis[i]->doc);
2865796c8dcSSimon Schubert       ui_out_text (uiout, "\n");
2875796c8dcSSimon Schubert     }
2885796c8dcSSimon Schubert   do_cleanups (cleanup_chain);
2895796c8dcSSimon Schubert }
2905796c8dcSSimon Schubert 
2915796c8dcSSimon Schubert /* Set the current C++ ABI, or display the list of options if no
2925796c8dcSSimon Schubert    argument is given.  */
2935796c8dcSSimon Schubert 
2945796c8dcSSimon Schubert static void
2955796c8dcSSimon Schubert set_cp_abi_cmd (char *args, int from_tty)
2965796c8dcSSimon Schubert {
2975796c8dcSSimon Schubert   if (args == NULL)
2985796c8dcSSimon Schubert     {
2995796c8dcSSimon Schubert       list_cp_abis (from_tty);
3005796c8dcSSimon Schubert       return;
3015796c8dcSSimon Schubert     }
3025796c8dcSSimon Schubert 
3035796c8dcSSimon Schubert   if (!switch_to_cp_abi (args))
3045796c8dcSSimon Schubert     error (_("Could not find \"%s\" in ABI list"), args);
3055796c8dcSSimon Schubert }
3065796c8dcSSimon Schubert 
3075796c8dcSSimon Schubert /* Show the currently selected C++ ABI.  */
3085796c8dcSSimon Schubert 
3095796c8dcSSimon Schubert static void
3105796c8dcSSimon Schubert show_cp_abi_cmd (char *args, int from_tty)
3115796c8dcSSimon Schubert {
312*a45ae5f8SJohn Marino   struct ui_out *uiout = current_uiout;
313*a45ae5f8SJohn Marino 
3145796c8dcSSimon Schubert   ui_out_text (uiout, "The currently selected C++ ABI is \"");
3155796c8dcSSimon Schubert 
3165796c8dcSSimon Schubert   ui_out_field_string (uiout, "cp-abi", current_cp_abi.shortname);
3175796c8dcSSimon Schubert   ui_out_text (uiout, "\" (");
3185796c8dcSSimon Schubert   ui_out_field_string (uiout, "longname", current_cp_abi.longname);
3195796c8dcSSimon Schubert   ui_out_text (uiout, ").\n");
3205796c8dcSSimon Schubert }
3215796c8dcSSimon Schubert 
3225796c8dcSSimon Schubert extern initialize_file_ftype _initialize_cp_abi; /* -Wmissing-prototypes */
3235796c8dcSSimon Schubert 
3245796c8dcSSimon Schubert void
3255796c8dcSSimon Schubert _initialize_cp_abi (void)
3265796c8dcSSimon Schubert {
3275796c8dcSSimon Schubert   register_cp_abi (&auto_cp_abi);
3285796c8dcSSimon Schubert   switch_to_cp_abi ("auto");
3295796c8dcSSimon Schubert 
3305796c8dcSSimon Schubert   add_cmd ("cp-abi", class_obscure, set_cp_abi_cmd, _("\
3315796c8dcSSimon Schubert Set the ABI used for inspecting C++ objects.\n\
3325796c8dcSSimon Schubert \"set cp-abi\" with no arguments will list the available ABIs."),
3335796c8dcSSimon Schubert 	   &setlist);
3345796c8dcSSimon Schubert 
3355796c8dcSSimon Schubert   add_cmd ("cp-abi", class_obscure, show_cp_abi_cmd,
336c50c785cSJohn Marino 	   _("Show the ABI used for inspecting C++ objects."),
337c50c785cSJohn Marino 	   &showlist);
3385796c8dcSSimon Schubert }
339