aboutsummaryrefslogtreecommitdiff
path: root/gdb/extension.c
diff options
context:
space:
mode:
authorSimon Marchi <simon.marchi@polymtl.ca>2018-01-07 09:25:32 -0500
committerSimon Marchi <simon.marchi@ericsson.com>2018-01-07 09:25:32 -0500
commitba18742c3a1b62ff218db99bee47bb932af6dab9 (patch)
tree14680d97dbbc56e326a6b2bda0bb1ba25dbd6970 /gdb/extension.c
parentd672364615e87e8c366b536940f269fdc3141330 (diff)
downloadgdb-ba18742c3a1b62ff218db99bee47bb932af6dab9.zip
gdb-ba18742c3a1b62ff218db99bee47bb932af6dab9.tar.gz
gdb-ba18742c3a1b62ff218db99bee47bb932af6dab9.tar.bz2
C++ify xmethod_worker, get rid of VEC(xmethod_worker_ptr)
The initial goal of this patch was to remove the usage of VEC(xmethod_worker_ptr) and corresponding cleanups. I ended up having to C++ify the xmethod_worker code, to be able to have xmethod_workers free their data in destructors, and therefore be able to use vectors of xmethod_worker unique_ptr. The operations in extension_language_ops that act on one instance of xmethod_worker (get result type, get args type, invoke) are transformed to methods of xmethod_worker. xmethod_worker becomes an abstract base class with virtual pure methods which python_xmethod_worker implements. The only xmethod-related operation left in extension_language_ops is get_matching_xmethod_workers, which returns a list of xmethod_workers. The changes are relatively straightforward, but here are some notes on things that may raise eyebrows: - I was not really comfortable with the value_of_xmethod function. At first it looks like a simple getter, so I considered making it a method of xmethod_worker. But actually it creates a value and transfers the ownership of the xmethod_worker to it. It would be a bit weird and error-prone if calling a method on an object silently removed the ownership of the object from the caller. To reflect the behavior more accurately, I renamed it to value_from_xmethod and made it accept an rvalue-reference (so the caller knows it gives away the ownership). I noticed the backlink from xmethod_worker to its owning value was not used, so I removed it. - Some code, like get_matching_xmethod_workers, made each callee fill a new vector, which was then merged in the result vector. I think it's safe if we always pass the same vector around, and each implementation just appends to it. - The clone operation does not seem particularly useful, it is removed in the following patch. gdb/ChangeLog: * extension-priv.h (enum ext_lang_rc): Remove, move to extension.h. (struct extension_language_ops) <clone_xmethod_worker_data>: Remove. <free_xmethod_worker_data>: Remove. <get_matching_xmethod_workers>: Chance VEC to std::vector. <get_xmethod_arg_types>: Remove. <get_xmethod_result_type>: Remove. <invoke_xmethod>: Remove. * extension.c (new_xmethod_worker): Remove. (clone_xmethod_worker): Remove. (get_matching_xmethod_workers): Return void, pass std::vector by pointer. (get_xmethod_arg_types): Rename to... (xmethod_worker::get_arg_types): ... this, and adjust. (get_xmethod_result_type): Rename to... (xmethod_worker::get_result_type): ... this, and adjust. (invoke_xmethod): Remove. (free_xmethod_worker): Remove. (free_xmethod_worker_vec): Remove. * extension.h (enum ext_lang_rc): Move here from extension-priv.h. (struct xmethod_worker): Add constructor and destructor. <data>: Remove. <value>: Remove. <invoke, clone, do_get_result_type, do_get_arg_types>: New virtual pure methods. <get_arg_types, get_result_type>: New methods. (xmethod_worker_ptr): Remove typedef. (DEF_VEC_P (xmethod_worker_ptr)): Remove. (xmethod_worker_vec): Remove typedef. (xmethod_worker_up): New typedef. (invoke_xmethod): Remove. (clone_xmethod_worker): Remove. (free_xmethod_worker): Remove. (free_xmethod_worker_vec): Remove. (get_xmethod_arg_types): Remove. (get_xmethod_result_type): Remove. * valops.c (find_method_list): Use std::vector, don't use intermediate vector. (value_find_oload_method_list): Use std::vector. (find_overload_match): Use std::vector. (find_oload_champ): Use std::vector. * value.c (value_free): Use operator delete. (value_of_xmethod): Rename to... (value_from_xmethod): ... this. Don't assign xmethod_worker::value, take rvalue-reference. (result_type_of_xmethod): Adjust. (call_xmethod): Adjust. * value.h: Include extension.h. (struct xmethod_worker): Don't forward-declare. (value_of_xmethod): Rename to... (value_from_xmethod): ... this, take rvalue-reference. * python/py-xmethods.c (struct gdbpy_worker_data): Rename to... (struct python_xmethod_worker): ... this, add constructor and destructor. <invoke, clone, do_get_arg_types, do_get_result_type>: Implement. (gdbpy_free_xmethod_worker_data): Rename to... (python_xmethod_worker::~python_xmethod_worker): ... this and adjust. (gdbpy_clone_xmethod_worker_data): Rename to... (python_xmethod_worker::clone): ... this and adjust. (gdbpy_get_matching_xmethod_workers): Use std::vector, don't use temporary vector. (gdbpy_get_xmethod_arg_types): Rename to... (python_xmethod_worker::do_get_arg_types): ... this and adjust. (gdbpy_get_xmethod_result_type): Rename to... (python_xmethod_worker::do_get_result_type): ... this and adjust. (gdbpy_invoke_xmethod): Rename to... (python_xmethod_worker::invoke): ... this and adjust. (new_python_xmethod_worker): Rename to... (python_xmethod_worker::python_xmethod_worker): ... this and adjust. * python/python-internal.h (gdbpy_clone_xmethod_worker_data): Remove. (gdbpy_free_xmethod_worker_data): Remove. (gdbpy_get_matching_xmethod_workers): Use std::vector. (gdbpy_get_xmethod_arg_types): Remove. (gdbpy_get_xmethod_result_type): Remove. (gdbpy_invoke_xmethod): Remove. * python/python.c (python_extension_ops): Remove obsolete callbacks.
Diffstat (limited to 'gdb/extension.c')
-rw-r--r--gdb/extension.c158
1 files changed, 19 insertions, 139 deletions
diff --git a/gdb/extension.c b/gdb/extension.c
index a1ee351..224b05c 100644
--- a/gdb/extension.c
+++ b/gdb/extension.c
@@ -850,68 +850,18 @@ check_quit_flag (void)
return result;
}
-
-/* xmethod support. */
-
-/* The xmethod API routines do not have "ext_lang" in the name because
- the name "xmethod" implies that this routine deals with extension
- languages. Plus some of the methods take a xmethod_foo * "self/this"
- arg, not an extension_language_defn * arg. */
-
-/* Returns a new xmethod_worker with EXTLANG and DATA. Space for the
- result must be freed with free_xmethod_worker. */
-
-struct xmethod_worker *
-new_xmethod_worker (const struct extension_language_defn *extlang, void *data)
-{
- struct xmethod_worker *worker = XCNEW (struct xmethod_worker);
-
- worker->extlang = extlang;
- worker->data = data;
- worker->value = NULL;
-
- return worker;
-}
-
-/* Clones WORKER and returns a new but identical worker.
- The function get_matching_xmethod_workers (see below), returns a
- vector of matching workers. If a particular worker is selected by GDB
- to invoke a method, then this function can help in cloning the
- selected worker and freeing up the vector via a cleanup.
-
- Space for the result must be freed with free_xmethod_worker. */
-
-struct xmethod_worker *
-clone_xmethod_worker (struct xmethod_worker *worker)
-{
- struct xmethod_worker *new_worker;
- const struct extension_language_defn *extlang = worker->extlang;
-
- gdb_assert (extlang->ops->clone_xmethod_worker_data != NULL);
-
- new_worker = new_xmethod_worker
- (extlang,
- extlang->ops->clone_xmethod_worker_data (extlang, worker->data));
- return new_worker;
-}
-
-/* If a method with name METHOD_NAME is to be invoked on an object of type
- TYPE, then all entension languages are searched for implementations of
- methods with name METHOD. All matches found are returned as a vector
- of 'xmethod_worker_ptr' objects. If no matching methods are
- found, NULL is returned. */
+/* See extension.h. */
-VEC (xmethod_worker_ptr) *
-get_matching_xmethod_workers (struct type *type, const char *method_name)
+void
+get_matching_xmethod_workers (struct type *type, const char *method_name,
+ std::vector<xmethod_worker_up> *workers)
{
- VEC (xmethod_worker_ptr) *workers = NULL;
int i;
const struct extension_language_defn *extlang;
ALL_ENABLED_EXTENSION_LANGUAGES (i, extlang)
{
- VEC (xmethod_worker_ptr) *lang_workers, *new_vec;
enum ext_lang_rc rc;
/* If an extension language does not support xmethods, ignore
@@ -921,115 +871,45 @@ get_matching_xmethod_workers (struct type *type, const char *method_name)
rc = extlang->ops->get_matching_xmethod_workers (extlang,
type, method_name,
- &lang_workers);
+ workers);
if (rc == EXT_LANG_RC_ERROR)
- {
- free_xmethod_worker_vec (workers);
- error (_("Error while looking for matching xmethod workers "
- "defined in %s."), extlang->capitalized_name);
- }
-
- new_vec = VEC_merge (xmethod_worker_ptr, workers, lang_workers);
- /* Free only the vectors and not the elements as NEW_VEC still
- contains them. */
- VEC_free (xmethod_worker_ptr, workers);
- VEC_free (xmethod_worker_ptr, lang_workers);
- workers = new_vec;
+ error (_("Error while looking for matching xmethod workers "
+ "defined in %s."), extlang->capitalized_name);
}
-
- return workers;
}
-/* Return the arg types of the xmethod encapsulated in WORKER.
- An array of arg types is returned. The length of the array is returned in
- NARGS. The type of the 'this' object is returned as the first element of
- array. */
+/* See extension.h. */
-struct type **
-get_xmethod_arg_types (struct xmethod_worker *worker, int *nargs)
+type **
+xmethod_worker::get_arg_types (int *nargs)
{
- enum ext_lang_rc rc;
- struct type **type_array = NULL;
- const struct extension_language_defn *extlang = worker->extlang;
-
- gdb_assert (extlang->ops->get_xmethod_arg_types != NULL);
+ type **type_array = NULL;
- rc = extlang->ops->get_xmethod_arg_types (extlang, worker, nargs,
- &type_array);
+ ext_lang_rc rc = do_get_arg_types (nargs, &type_array);
if (rc == EXT_LANG_RC_ERROR)
- {
- error (_("Error while looking for arg types of a xmethod worker "
- "defined in %s."), extlang->capitalized_name);
- }
+ error (_("Error while looking for arg types of a xmethod worker "
+ "defined in %s."), m_extlang->capitalized_name);
return type_array;
}
-/* Return the type of the result of the xmethod encapsulated in WORKER.
- OBJECT, ARGS, NARGS are the same as for invoke_xmethod. */
+/* See extension.h. */
struct type *
-get_xmethod_result_type (struct xmethod_worker *worker,
- struct value *object, struct value **args, int nargs)
+xmethod_worker::get_result_type (value *object, value **args, int nargs)
{
- enum ext_lang_rc rc;
- struct type *result_type;
- const struct extension_language_defn *extlang = worker->extlang;
-
- gdb_assert (extlang->ops->get_xmethod_arg_types != NULL);
+ type *result_type;
- rc = extlang->ops->get_xmethod_result_type (extlang, worker,
- object, args, nargs,
- &result_type);
+ ext_lang_rc rc = do_get_result_type (object, args, nargs, &result_type);
if (rc == EXT_LANG_RC_ERROR)
{
error (_("Error while fetching result type of an xmethod worker "
- "defined in %s."), extlang->capitalized_name);
+ "defined in %s."), m_extlang->capitalized_name);
}
return result_type;
}
-/* Invokes the xmethod encapsulated in WORKER and returns the result.
- The method is invoked on OBJ with arguments in the ARGS array. NARGS is
- the length of the this array. */
-
-struct value *
-invoke_xmethod (struct xmethod_worker *worker, struct value *obj,
- struct value **args, int nargs)
-{
- gdb_assert (worker->extlang->ops->invoke_xmethod != NULL);
-
- return worker->extlang->ops->invoke_xmethod (worker->extlang, worker,
- obj, args, nargs);
-}
-
-/* Frees the xmethod worker WORKER. */
-
-void
-free_xmethod_worker (struct xmethod_worker *worker)
-{
- gdb_assert (worker->extlang->ops->free_xmethod_worker_data != NULL);
- worker->extlang->ops->free_xmethod_worker_data (worker->extlang,
- worker->data);
- xfree (worker);
-}
-
-/* Frees a vector of xmethod_workers VEC. */
-
-void
-free_xmethod_worker_vec (void *vec)
-{
- int i;
- struct xmethod_worker *worker;
- VEC (xmethod_worker_ptr) *v = (VEC (xmethod_worker_ptr) *) vec;
-
- for (i = 0; VEC_iterate (xmethod_worker_ptr, v, i, worker); i++)
- free_xmethod_worker (worker);
-
- VEC_free (xmethod_worker_ptr, v);
-}
-
/* Called via an observer before gdb prints its prompt.
Iterate over the extension languages giving them a chance to
change the prompt. The first one to change the prompt wins,