From: Tom Tromey <tom@tromey.com>
To: gcc-patches@gcc.gnu.org
Cc: Tom Tromey <tom@tromey.com>
Subject: [PATCH v2 19/21] libcc1: use variadic templates for callbacks
Date: Tue, 27 Apr 2021 19:01:17 -0600 [thread overview]
Message-ID: <20210428010119.806184-20-tom@tromey.com> (raw)
In-Reply-To: <20210428010119.806184-1-tom@tromey.com>
This patch completes the transition of libcc1 from the use of separate
template functions for different arities to the use of variadic
functions. This is how I had wanted it to work from the very
beginning, and is possible now with C++11.
I had thought that variadic callbacks required C++17, but it turns out
that the approach taken here is basically equivalent to std::apply --
just a bit wordier.
libcc1/ChangeLog
2021-04-27 Tom Tromey <tom@tromey.com>
* rpc.hh (argument_wrapper) <get>: Replace cast operator.
(argument_wrapper<T *>) <get>: Likewise.
(unmarshall): Add std::tuple overloads.
(callback): Remove.
(class invoker): New.
* libcp1plugin.cc (plugin_init): Update.
* libcp1.cc (libcp1::add_callbacks): Update.
* libcc1plugin.cc (plugin_init): Update.
* libcc1.cc (libcc1::add_callbacks): Update.
* connection.cc (cc1_plugin::connection::do_wait): Update.
---
libcc1/ChangeLog | 13 +++
libcc1/connection.cc | 2 +-
libcc1/libcc1.cc | 12 +--
libcc1/libcc1plugin.cc | 20 ++--
libcc1/libcp1.cc | 17 ++--
libcc1/libcp1plugin.cc | 20 ++--
libcc1/rpc.hh | 219 +++++++++++------------------------------
7 files changed, 101 insertions(+), 202 deletions(-)
diff --git a/libcc1/connection.cc b/libcc1/connection.cc
index 66d573911080..45560b9b790e 100644
--- a/libcc1/connection.cc
+++ b/libcc1/connection.cc
@@ -129,7 +129,7 @@ cc1_plugin::connection::do_wait (bool want_result)
return FAIL;
callback_ftype *callback
- = m_callbacks.find_callback (method_name);
+ = m_callbacks.find_callback (method_name.get ());
// The call to CALLBACK is where we may end up in a
// reentrant call.
if (callback == NULL || !callback (this))
diff --git a/libcc1/libcc1.cc b/libcc1/libcc1.cc
index cbc54ee0a044..febadc8420b0 100644
--- a/libcc1/libcc1.cc
+++ b/libcc1/libcc1.cc
@@ -143,15 +143,13 @@ void
libcc1::add_callbacks ()
{
cc1_plugin::callback_ftype *fun
- = cc1_plugin::callback<int,
- enum gcc_c_oracle_request,
- const char *,
- c_call_binding_oracle>;
+ = cc1_plugin::invoker<int,
+ enum gcc_c_oracle_request,
+ const char *>::invoke<c_call_binding_oracle>;
connection->add_callback ("binding_oracle", fun);
- fun = cc1_plugin::callback<gcc_address,
- const char *,
- c_call_symbol_address>;
+ fun = cc1_plugin::invoker<gcc_address,
+ const char *>::invoke<c_call_symbol_address>;
connection->add_callback ("address_oracle", fun);
}
diff --git a/libcc1/libcc1plugin.cc b/libcc1/libcc1plugin.cc
index d6951ab1a4d2..4d6a3a11ee22 100644
--- a/libcc1/libcc1plugin.cc
+++ b/libcc1/libcc1plugin.cc
@@ -762,46 +762,46 @@ plugin_init (struct plugin_name_args *plugin_info,
#define GCC_METHOD0(R, N) \
{ \
cc1_plugin::callback_ftype *fun \
- = cc1_plugin::callback<R, plugin_ ## N>; \
+ = cc1_plugin::invoker<R>::invoke<plugin_ ## N>; \
current_context->add_callback (# N, fun); \
}
#define GCC_METHOD1(R, N, A) \
{ \
cc1_plugin::callback_ftype *fun \
- = cc1_plugin::callback<R, A, plugin_ ## N>; \
+ = cc1_plugin::invoker<R, A>::invoke<plugin_ ## N>; \
current_context->add_callback (# N, fun); \
}
#define GCC_METHOD2(R, N, A, B) \
{ \
cc1_plugin::callback_ftype *fun \
- = cc1_plugin::callback<R, A, B, plugin_ ## N>; \
+ = cc1_plugin::invoker<R, A, B>::invoke<plugin_ ## N>; \
current_context->add_callback (# N, fun); \
}
#define GCC_METHOD3(R, N, A, B, C) \
{ \
cc1_plugin::callback_ftype *fun \
- = cc1_plugin::callback<R, A, B, C, plugin_ ## N>; \
+ = cc1_plugin::invoker<R, A, B, C>::invoke<plugin_ ## N>; \
current_context->add_callback (# N, fun); \
}
#define GCC_METHOD4(R, N, A, B, C, D) \
{ \
cc1_plugin::callback_ftype *fun \
- = cc1_plugin::callback<R, A, B, C, D, \
- plugin_ ## N>; \
+ = cc1_plugin::invoker<R, A, B, C, \
+ D>::invoke<plugin_ ## N>; \
current_context->add_callback (# N, fun); \
}
#define GCC_METHOD5(R, N, A, B, C, D, E) \
{ \
cc1_plugin::callback_ftype *fun \
- = cc1_plugin::callback<R, A, B, C, D, E, \
- plugin_ ## N>; \
+ = cc1_plugin::invoker<R, A, B, C, D, \
+ E>::invoke<plugin_ ## N>; \
current_context->add_callback (# N, fun); \
}
#define GCC_METHOD7(R, N, A, B, C, D, E, F, G) \
{ \
cc1_plugin::callback_ftype *fun \
- = cc1_plugin::callback<R, A, B, C, D, E, F, G, \
- plugin_ ## N>; \
+ = cc1_plugin::invoker<R, A, B, C, D, \
+ E, F, G>::invoke<plugin_ ## N>; \
current_context->add_callback (# N, fun); \
}
diff --git a/libcc1/libcp1.cc b/libcc1/libcp1.cc
index d22d9dc6af8c..a93349833901 100644
--- a/libcc1/libcp1.cc
+++ b/libcc1/libcp1.cc
@@ -166,23 +166,18 @@ void
libcp1::add_callbacks ()
{
cc1_plugin::callback_ftype *fun
- = cc1_plugin::callback<int,
- enum gcc_cp_oracle_request,
- const char *,
- cp_call_binding_oracle>;
+ = cc1_plugin::invoker<int, enum gcc_cp_oracle_request,
+ const char *>::invoke<cp_call_binding_oracle>;
connection->add_callback ("binding_oracle", fun);
- fun = cc1_plugin::callback<gcc_address,
- const char *,
- cp_call_symbol_address>;
+ fun = cc1_plugin::invoker<gcc_address,
+ const char *>::invoke<cp_call_symbol_address>;
connection->add_callback ("address_oracle", fun);
- fun = cc1_plugin::callback<int,
- cp_call_enter_scope>;
+ fun = cc1_plugin::invoker<int>::invoke<cp_call_enter_scope>;
connection->add_callback ("enter_scope", fun);
- fun = cc1_plugin::callback<int,
- cp_call_leave_scope>;
+ fun = cc1_plugin::invoker<int>::invoke<cp_call_leave_scope>;
connection->add_callback ("leave_scope", fun);
}
diff --git a/libcc1/libcp1plugin.cc b/libcc1/libcp1plugin.cc
index 64cde651139c..79694b919641 100644
--- a/libcc1/libcp1plugin.cc
+++ b/libcc1/libcp1plugin.cc
@@ -3509,46 +3509,46 @@ plugin_init (struct plugin_name_args *plugin_info,
#define GCC_METHOD0(R, N) \
{ \
cc1_plugin::callback_ftype *fun \
- = cc1_plugin::callback<R, plugin_ ## N>; \
+ = cc1_plugin::invoker<R>::invoke<plugin_ ## N>; \
current_context->add_callback (# N, fun); \
}
#define GCC_METHOD1(R, N, A) \
{ \
cc1_plugin::callback_ftype *fun \
- = cc1_plugin::callback<R, A, plugin_ ## N>; \
+ = cc1_plugin::invoker<R, A>::invoke<plugin_ ## N>; \
current_context->add_callback (# N, fun); \
}
#define GCC_METHOD2(R, N, A, B) \
{ \
cc1_plugin::callback_ftype *fun \
- = cc1_plugin::callback<R, A, B, plugin_ ## N>; \
+ = cc1_plugin::invoker<R, A, B>::invoke<plugin_ ## N>; \
current_context->add_callback (# N, fun); \
}
#define GCC_METHOD3(R, N, A, B, C) \
{ \
cc1_plugin::callback_ftype *fun \
- = cc1_plugin::callback<R, A, B, C, plugin_ ## N>; \
+ = cc1_plugin::invoker<R, A, B, C>::invoke<plugin_ ## N>; \
current_context->add_callback (# N, fun); \
}
#define GCC_METHOD4(R, N, A, B, C, D) \
{ \
cc1_plugin::callback_ftype *fun \
- = cc1_plugin::callback<R, A, B, C, D, \
- plugin_ ## N>; \
+ = cc1_plugin::invoker<R, A, B, C, \
+ D>::invoke<plugin_ ## N>; \
current_context->add_callback (# N, fun); \
}
#define GCC_METHOD5(R, N, A, B, C, D, E) \
{ \
cc1_plugin::callback_ftype *fun \
- = cc1_plugin::callback<R, A, B, C, D, E, \
- plugin_ ## N>; \
+ = cc1_plugin::invoker<R, A, B, C, \
+ D, E>::invoke<plugin_ ## N>; \
current_context->add_callback (# N, fun); \
}
#define GCC_METHOD7(R, N, A, B, C, D, E, F, G) \
{ \
cc1_plugin::callback_ftype *fun \
- = cc1_plugin::callback<R, A, B, C, D, E, F, G, \
- plugin_ ## N>; \
+ = cc1_plugin::invoker<R, A, B, C, \
+ D, E, F, G>::invoke<plugin_ ## N>; \
current_context->add_callback (# N, fun); \
}
diff --git a/libcc1/rpc.hh b/libcc1/rpc.hh
index 09cd7bdda616..8e43fa146dcc 100644
--- a/libcc1/rpc.hh
+++ b/libcc1/rpc.hh
@@ -43,7 +43,7 @@ namespace cc1_plugin
argument_wrapper (const argument_wrapper &) = delete;
argument_wrapper &operator= (const argument_wrapper &) = delete;
- operator T () const { return m_object; }
+ T get () const { return m_object; }
status unmarshall (connection *conn)
{
@@ -68,7 +68,7 @@ namespace cc1_plugin
typedef typename std::remove_const<T>::type type;
- operator const type * () const
+ const type *get () const
{
return m_object.get ();
}
@@ -88,17 +88,14 @@ namespace cc1_plugin
};
// There are two kinds of template functions here: "call" and
- // "callback". "call" is implemented with variadic templates, but
- // "callback" is repeated multiple times to handle different numbers
- // of arguments. (This could be improved with C++17 and
- // std::apply.)
+ // "invoker".
// The "call" template is used for making a remote procedure call.
// It starts a query ('Q') packet, marshalls its arguments, waits
// for a result, and finally reads and returns the result via an
// "out" parameter.
- // The "callback" template is used when receiving a remote procedure
+ // The "invoker" template is used when receiving a remote procedure
// call. This template function is suitable for use with the
// "callbacks" and "connection" classes. It decodes incoming
// arguments, passes them to the wrapped function, and finally
@@ -123,175 +120,71 @@ namespace cc1_plugin
return OK;
}
- template<typename R, R (*func) (connection *)>
- status
- callback (connection *conn)
+ // The base case -- just return OK.
+ template<int I, typename... T>
+ typename std::enable_if<I == sizeof... (T), status>::type
+ unmarshall (connection *, std::tuple<T...> &)
{
- R result;
-
- if (!unmarshall_check (conn, 0))
- return FAIL;
- result = func (conn);
- if (!conn->send ('R'))
- return FAIL;
- return marshall (conn, result);
- }
-
- template<typename R, typename A, R (*func) (connection *, A)>
- status
- callback (connection *conn)
- {
- argument_wrapper<A> arg;
- R result;
-
- if (!unmarshall_check (conn, 1))
- return FAIL;
- if (!arg.unmarshall (conn))
- return FAIL;
- result = func (conn, arg);
- if (!conn->send ('R'))
- return FAIL;
- return marshall (conn, result);
+ return OK;
}
- template<typename R, typename A1, typename A2, R (*func) (connection *,
- A1, A2)>
- status
- callback (connection *conn)
+ // Unmarshall this argument, then unmarshall all subsequent args.
+ template<int I, typename... T>
+ typename std::enable_if<I < sizeof... (T), status>::type
+ unmarshall (connection *conn, std::tuple<T...> &value)
{
- argument_wrapper<A1> arg1;
- argument_wrapper<A2> arg2;
- R result;
-
- if (!unmarshall_check (conn, 2))
- return FAIL;
- if (!arg1.unmarshall (conn))
- return FAIL;
- if (!arg2.unmarshall (conn))
+ if (!std::get<I> (value).unmarshall (conn))
return FAIL;
- result = func (conn, arg1, arg2);
- if (!conn->send ('R'))
- return FAIL;
- return marshall (conn, result);
+ return unmarshall<I + 1, T...> (conn, value);
}
- template<typename R, typename A1, typename A2, typename A3,
- R (*func) (connection *, A1, A2, A3)>
- status
- callback (connection *conn)
+ // Wrap a static function that is suitable for use as a callback.
+ // This is a template function inside a template class to work
+ // around limitations with multiple variadic packs.
+ template<typename R, typename... Arg>
+ class invoker
{
- argument_wrapper<A1> arg1;
- argument_wrapper<A2> arg2;
- argument_wrapper<A3> arg3;
- R result;
+ // Base case -- we can call the function.
+ template<int I, R func (connection *, Arg...), typename... T>
+ static typename std::enable_if<I == sizeof... (Arg), R>::type
+ call (connection *conn, const std::tuple<argument_wrapper<Arg>...> &,
+ T... args)
+ {
+ return func (conn, args...);
+ }
- if (!unmarshall_check (conn, 3))
- return FAIL;
- if (!arg1.unmarshall (conn))
- return FAIL;
- if (!arg2.unmarshall (conn))
- return FAIL;
- if (!arg3.unmarshall (conn))
- return FAIL;
- result = func (conn, arg1, arg2, arg3);
- if (!conn->send ('R'))
- return FAIL;
- return marshall (conn, result);
- }
+ // Unpack one argument and continue the recursion.
+ template<int I, R func (connection *, Arg...), typename... T>
+ static typename std::enable_if<I < sizeof... (Arg), R>::type
+ call (connection *conn, const std::tuple<argument_wrapper<Arg>...> &value,
+ T... args)
+ {
+ return call<I + 1, func> (conn, value, args...,
+ std::get<I> (value).get ());
+ }
- template<typename R, typename A1, typename A2, typename A3, typename A4,
- R (*func) (connection *, A1, A2, A3, A4)>
- status
- callback (connection *conn)
- {
- argument_wrapper<A1> arg1;
- argument_wrapper<A2> arg2;
- argument_wrapper<A3> arg3;
- argument_wrapper<A4> arg4;
- R result;
+ public:
- if (!unmarshall_check (conn, 4))
- return FAIL;
- if (!arg1.unmarshall (conn))
- return FAIL;
- if (!arg2.unmarshall (conn))
- return FAIL;
- if (!arg3.unmarshall (conn))
- return FAIL;
- if (!arg4.unmarshall (conn))
- return FAIL;
- result = func (conn, arg1, arg2, arg3, arg4);
- if (!conn->send ('R'))
- return FAIL;
- return marshall (conn, result);
- }
+ // A callback function that reads arguments from the connection,
+ // calls the wrapped function, and then sends the result back on
+ // the connection.
+ template<R func (connection *, Arg...)>
+ static status
+ invoke (connection *conn)
+ {
+ if (!unmarshall_check (conn, sizeof... (Arg)))
+ return FAIL;
+ std::tuple<argument_wrapper<Arg>...> wrapped;
+ if (!unmarshall<0> (conn, wrapped))
+ return FAIL;
- template<typename R, typename A1, typename A2, typename A3, typename A4,
- typename A5, R (*func) (connection *, A1, A2, A3, A4, A5)>
- status
- callback (connection *conn)
- {
- argument_wrapper<A1> arg1;
- argument_wrapper<A2> arg2;
- argument_wrapper<A3> arg3;
- argument_wrapper<A4> arg4;
- argument_wrapper<A5> arg5;
- R result;
-
- if (!unmarshall_check (conn, 5))
- return FAIL;
- if (!arg1.unmarshall (conn))
- return FAIL;
- if (!arg2.unmarshall (conn))
- return FAIL;
- if (!arg3.unmarshall (conn))
- return FAIL;
- if (!arg4.unmarshall (conn))
- return FAIL;
- if (!arg5.unmarshall (conn))
- return FAIL;
- result = func (conn, arg1, arg2, arg3, arg4, arg5);
- if (!conn->send ('R'))
- return FAIL;
- return marshall (conn, result);
- }
+ R result = call<0, func> (conn, wrapped);
- template<typename R, typename A1, typename A2, typename A3, typename A4,
- typename A5, typename A6, typename A7,
- R (*func) (connection *, A1, A2, A3, A4, A5, A6, A7)>
- status
- callback (connection *conn)
- {
- argument_wrapper<A1> arg1;
- argument_wrapper<A2> arg2;
- argument_wrapper<A3> arg3;
- argument_wrapper<A4> arg4;
- argument_wrapper<A5> arg5;
- argument_wrapper<A6> arg6;
- argument_wrapper<A7> arg7;
- R result;
-
- if (!unmarshall_check (conn, 7))
- return FAIL;
- if (!arg1.unmarshall (conn))
- return FAIL;
- if (!arg2.unmarshall (conn))
- return FAIL;
- if (!arg3.unmarshall (conn))
- return FAIL;
- if (!arg4.unmarshall (conn))
- return FAIL;
- if (!arg5.unmarshall (conn))
- return FAIL;
- if (!arg6.unmarshall (conn))
- return FAIL;
- if (!arg7.unmarshall (conn))
- return FAIL;
- result = func (conn, arg1, arg2, arg3, arg4, arg5, arg6, arg7);
- if (!conn->send ('R'))
- return FAIL;
- return marshall (conn, result);
- }
+ if (!conn->send ('R'))
+ return FAIL;
+ return marshall (conn, result);
+ }
+ };
};
#endif // CC1_PLUGIN_RPC_HH
--
2.26.2
next prev parent reply other threads:[~2021-04-28 1:01 UTC|newest]
Thread overview: 46+ messages / expand[flat|nested] mbox.gz Atom feed top
2021-04-28 1:00 [PATCH v2 00/21] C++11-based improvements for libcc1 Tom Tromey
2021-04-28 1:00 ` [PATCH v2 01/21] libcc1: use templates to unmarshall enums Tom Tromey
2021-04-28 18:27 ` Jeff Law
2021-04-28 1:01 ` [PATCH v2 02/21] libcc1: use "override" Tom Tromey
2021-04-28 15:53 ` Jeff Law
2021-04-28 1:01 ` [PATCH v2 03/21] libcc1: inline some simple methods Tom Tromey
2021-04-28 15:54 ` Jeff Law
2021-04-28 1:01 ` [PATCH v2 04/21] libcc1: delete copy constructor and assignment operators Tom Tromey
2021-04-28 15:55 ` Jeff Law
2021-04-28 1:01 ` [PATCH v2 05/21] libcc1: use variadic templates for "call" Tom Tromey
2021-04-28 18:28 ` Jeff Law
2021-04-28 1:01 ` [PATCH v2 06/21] libcc1: use variadic templates for "rpc" Tom Tromey
2021-04-28 18:28 ` Jeff Law
2021-04-28 1:01 ` [PATCH v2 07/21] libcc1: use std::vector when building function types Tom Tromey
2021-04-28 16:01 ` Jeff Law
2021-04-28 19:56 ` Tom Tromey
2021-04-28 20:07 ` Jeff Law
2021-04-28 1:01 ` [PATCH v2 08/21] libcc1: add deleter objects Tom Tromey
2021-04-28 21:06 ` Jeff Law
2021-04-28 1:01 ` [PATCH v2 09/21] libcc1: add more uses of 'deleter' Tom Tromey
2021-04-29 12:43 ` Jeff Law
2021-04-28 1:01 ` [PATCH v2 10/21] libcc1: use unique_ptr more Tom Tromey
2021-04-29 12:44 ` Jeff Law
2021-04-28 1:01 ` [PATCH v2 11/21] libcc1: unify compiler handling Tom Tromey
2021-04-29 12:47 ` Jeff Law
2021-04-28 1:01 ` [PATCH v2 12/21] libcc1: use foreach Tom Tromey
2021-04-28 16:04 ` Jeff Law
2021-04-28 1:01 ` [PATCH v2 13/21] libcc1: use static_assert Tom Tromey
2021-04-28 16:06 ` Jeff Law
2021-04-28 1:01 ` [PATCH v2 14/21] libcc1: share basic context code Tom Tromey
2021-04-29 12:50 ` Jeff Law
2021-04-28 1:01 ` [PATCH v2 15/21] libcc1: share GDB plugin code Tom Tromey
2021-04-30 14:50 ` Jeff Law
2021-04-28 1:01 ` [PATCH v2 16/21] libcc1: use GCC_FE_VERSION_1 in C++ plugin Tom Tromey
2021-04-28 16:06 ` Jeff Law
2021-04-28 1:01 ` [PATCH v2 17/21] libcc1: share the GCC interface code Tom Tromey
2021-04-30 15:07 ` Jeff Law
2021-04-28 1:01 ` [PATCH v2 18/21] libcc1: fix a memory leak Tom Tromey
2021-04-28 16:07 ` Jeff Law
2021-04-28 1:01 ` Tom Tromey [this message]
2021-04-30 15:08 ` [PATCH v2 19/21] libcc1: use variadic templates for callbacks Jeff Law
2021-05-04 22:05 ` Tom Tromey
2021-04-28 1:01 ` [PATCH v2 20/21] libcc1: avoid extra string copies Tom Tromey
2021-04-28 16:07 ` Jeff Law
2021-04-28 1:01 ` [PATCH v2 21/21] libcc1: avoid a call to c_str Tom Tromey
2021-04-28 16:08 ` Jeff Law
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20210428010119.806184-20-tom@tromey.com \
--to=tom@tromey.com \
--cc=gcc-patches@gcc.gnu.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).