From dc6be7c02258f6c79ed97b6886c81278ef7f264d Mon Sep 17 00:00:00 2001 From: Tom Tromey Date: Tue, 4 May 2021 15:26:58 -0600 Subject: libcc1: use variadic templates for "call" This changes libcc1 to use variadic templates for the "call" functions. The primary benefit is that this simplifies the code. libcc1 * rpc.hh (call): Use variadic template. Remove overloads. * marshall.hh (marshall): Add base overload. Use variadic template. --- libcc1/marshall.hh | 16 +++++ libcc1/rpc.hh | 168 +++-------------------------------------------------- 2 files changed, 25 insertions(+), 159 deletions(-) (limited to 'libcc1') diff --git a/libcc1/marshall.hh b/libcc1/marshall.hh index 8d890eb9b6c..4a28a8fe4ae 100644 --- a/libcc1/marshall.hh +++ b/libcc1/marshall.hh @@ -52,6 +52,14 @@ namespace cc1_plugin status unmarshall_array_start (connection *, char, size_t *); status unmarshall_array_elmts (connection *, size_t, void *); + // An "empty" marshall call -- used to handle the base case for some + // variadic templates. + static inline + status marshall (connection *) + { + return OK; + } + // A template function that can handle marshalling various integer // objects to the connection. template @@ -103,6 +111,14 @@ namespace cc1_plugin // resulting array must be freed by the caller, using 'delete[]' on // the elements, and 'delete' on the array object itself. status unmarshall (connection *, struct gcc_type_array **); + + template + status marshall (connection *c, T1 arg1, T2 arg2, Arg... rest) + { + if (!marshall (c, arg1)) + return FAIL; + return marshall (c, arg2, rest...); + } }; #endif // CC1_PLUGIN_MARSHALL_HH diff --git a/libcc1/rpc.hh b/libcc1/rpc.hh index 429aeb3c127..a3631cb5d7e 100644 --- a/libcc1/rpc.hh +++ b/libcc1/rpc.hh @@ -232,10 +232,10 @@ namespace cc1_plugin #endif /* GCC_CP_INTERFACE_H */ // There are two kinds of template functions here: "call" and - // "callback". They are each repeated multiple times to handle - // different numbers of arguments. (This would be improved with - // C++11, though applying a call is still tricky until C++14 can be - // used.) + // "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.) // The "call" template is used for making a remote procedure call. // It starts a query ('Q') packet, marshalls its arguments, waits @@ -248,15 +248,17 @@ namespace cc1_plugin // arguments, passes them to the wrapped function, and finally // marshalls a reply packet. - template + template status - call (connection *conn, const char *method, R *result) + call (connection *conn, const char *method, R *result, Arg... args) { if (!conn->send ('Q')) return FAIL; if (!marshall (conn, method)) return FAIL; - if (!marshall (conn, 0)) + if (!marshall (conn, (int) sizeof... (Arg))) + return FAIL; + if (!marshall (conn, args...)) return FAIL; if (!conn->wait_for_result ()) return FAIL; @@ -279,25 +281,6 @@ namespace cc1_plugin return marshall (conn, result); } - template - status - call (connection *conn, const char *method, R *result, A arg) - { - if (!conn->send ('Q')) - return FAIL; - if (!marshall (conn, method)) - return FAIL; - if (!marshall (conn, 1)) - return FAIL; - if (!marshall (conn, arg)) - return FAIL; - if (!conn->wait_for_result ()) - return FAIL; - if (!unmarshall (conn, result)) - return FAIL; - return OK; - } - template status callback (connection *conn) @@ -315,27 +298,6 @@ namespace cc1_plugin return marshall (conn, result); } - template - status - call (connection *conn, const char *method, R *result, A1 arg1, A2 arg2) - { - if (!conn->send ('Q')) - return FAIL; - if (!marshall (conn, method)) - return FAIL; - if (!marshall (conn, 2)) - return FAIL; - if (!marshall (conn, arg1)) - return FAIL; - if (!marshall (conn, arg2)) - return FAIL; - if (!conn->wait_for_result ()) - return FAIL; - if (!unmarshall (conn, result)) - return FAIL; - return OK; - } - template status @@ -357,30 +319,6 @@ namespace cc1_plugin return marshall (conn, result); } - template - status - call (connection *conn, const char *method, R *result, A1 arg1, A2 arg2, - A3 arg3) - { - if (!conn->send ('Q')) - return FAIL; - if (!marshall (conn, method)) - return FAIL; - if (!marshall (conn, 3)) - return FAIL; - if (!marshall (conn, arg1)) - return FAIL; - if (!marshall (conn, arg2)) - return FAIL; - if (!marshall (conn, arg3)) - return FAIL; - if (!conn->wait_for_result ()) - return FAIL; - if (!unmarshall (conn, result)) - return FAIL; - return OK; - } - template status @@ -405,32 +343,6 @@ namespace cc1_plugin return marshall (conn, result); } - template - status - call (connection *conn, const char *method, R *result, A1 arg1, A2 arg2, - A3 arg3, A4 arg4) - { - if (!conn->send ('Q')) - return FAIL; - if (!marshall (conn, method)) - return FAIL; - if (!marshall (conn, 4)) - return FAIL; - if (!marshall (conn, arg1)) - return FAIL; - if (!marshall (conn, arg2)) - return FAIL; - if (!marshall (conn, arg3)) - return FAIL; - if (!marshall (conn, arg4)) - return FAIL; - if (!conn->wait_for_result ()) - return FAIL; - if (!unmarshall (conn, result)) - return FAIL; - return OK; - } - template status @@ -458,35 +370,6 @@ namespace cc1_plugin return marshall (conn, result); } - template - status - call (connection *conn, const char *method, R *result, A1 arg1, A2 arg2, - A3 arg3, A4 arg4, A5 arg5) - { - if (!conn->send ('Q')) - return FAIL; - if (!marshall (conn, method)) - return FAIL; - if (!marshall (conn, 5)) - return FAIL; - if (!marshall (conn, arg1)) - return FAIL; - if (!marshall (conn, arg2)) - return FAIL; - if (!marshall (conn, arg3)) - return FAIL; - if (!marshall (conn, arg4)) - return FAIL; - if (!marshall (conn, arg5)) - return FAIL; - if (!conn->wait_for_result ()) - return FAIL; - if (!unmarshall (conn, result)) - return FAIL; - return OK; - } - template status @@ -517,39 +400,6 @@ namespace cc1_plugin return marshall (conn, result); } - template - status - call (connection *conn, const char *method, R *result, A1 arg1, A2 arg2, - A3 arg3, A4 arg4, A5 arg5, A6 arg6, A7 arg7) - { - if (!conn->send ('Q')) - return FAIL; - if (!marshall (conn, method)) - return FAIL; - if (!marshall (conn, 7)) - return FAIL; - if (!marshall (conn, arg1)) - return FAIL; - if (!marshall (conn, arg2)) - return FAIL; - if (!marshall (conn, arg3)) - return FAIL; - if (!marshall (conn, arg4)) - return FAIL; - if (!marshall (conn, arg5)) - return FAIL; - if (!marshall (conn, arg6)) - return FAIL; - if (!marshall (conn, arg7)) - return FAIL; - if (!conn->wait_for_result ()) - return FAIL; - if (!unmarshall (conn, result)) - return FAIL; - return OK; - } - template -- cgit v1.2.1