diff options
author | Iain Sandoe <iain@sandoe.co.uk> | 2020-04-28 02:15:07 +0100 |
---|---|---|
committer | Iain Sandoe <iain@sandoe.co.uk> | 2020-04-28 02:16:34 +0100 |
commit | f5edc76acac7bbadd8d1b00c4cc5994b2a088542 (patch) | |
tree | 4eef2f6a4876fa7ba9a50138918e58e69d295212 /gcc | |
parent | b9c91b7f3279e23aed965c05197acf3b6f439f8d (diff) | |
download | gcc-f5edc76acac7bbadd8d1b00c4cc5994b2a088542.zip gcc-f5edc76acac7bbadd8d1b00c4cc5994b2a088542.tar.gz gcc-f5edc76acac7bbadd8d1b00c4cc5994b2a088542.tar.bz2 |
coroutines: Pass class ref to traits lookup and promise allocator [PR94760].
We changed the argument passed to the promise parameter preview
to match a reference to *this. However to be consistent with the
other ports, we do need to match the reference transformation in
the traits lookup and the promise allocator lookup.
gcc/cp/ChangeLog:
2020-04-28 Iain Sandoe <iain@sandoe.co.uk>
PR c++/94760
* coroutines.cc (instantiate_coro_traits): Pass a reference to
object type rather than a pointer type for 'this', for method
coroutines.
(struct param_info): Add a field to hold that the parm is a lambda
closure pointer.
(morph_fn_to_coro): Check for lambda closure pointers in the
args. Use a reference to *this when building the args list for the
promise allocator lookup.
gcc/testsuite/ChangeLog:
2020-04-28 Iain Sandoe <iain@sandoe.co.uk>
PR c++/94760
* g++.dg/coroutines/pr94760-mismatched-traits-and-promise-prev.C:
New test.
Diffstat (limited to 'gcc')
-rw-r--r-- | gcc/cp/ChangeLog | 12 | ||||
-rw-r--r-- | gcc/cp/coroutines.cc | 55 | ||||
-rw-r--r-- | gcc/testsuite/ChangeLog | 6 | ||||
-rw-r--r-- | gcc/testsuite/g++.dg/coroutines/pr9xxxx-mismatched-traits-and-promise-prev.C | 29 |
4 files changed, 98 insertions, 4 deletions
diff --git a/gcc/cp/ChangeLog b/gcc/cp/ChangeLog index 7a90c4c..dd8ee21 100644 --- a/gcc/cp/ChangeLog +++ b/gcc/cp/ChangeLog @@ -1,5 +1,17 @@ 2020-04-28 Iain Sandoe <iain@sandoe.co.uk> + PR c++/94760 + * coroutines.cc (instantiate_coro_traits): Pass a reference to + object type rather than a pointer type for 'this', for method + coroutines. + (struct param_info): Add a field to hold that the parm is a lambda + closure pointer. + (morph_fn_to_coro): Check for lambda closure pointers in the + args. Use a reference to *this when building the args list for the + promise allocator lookup. + +2020-04-28 Iain Sandoe <iain@sandoe.co.uk> + PR c++/94759 * coroutines.cc (coro_promise_type_found_p): Do not exclude non-classes here (this needs to be handled in the diff --git a/gcc/cp/coroutines.cc b/gcc/cp/coroutines.cc index b4dc731..7bb3e98 100644 --- a/gcc/cp/coroutines.cc +++ b/gcc/cp/coroutines.cc @@ -296,14 +296,26 @@ instantiate_coro_traits (tree fndecl, location_t kw) type. */ tree functyp = TREE_TYPE (fndecl); + tree arg = DECL_ARGUMENTS (fndecl); + bool lambda_p = LAMBDA_FUNCTION_P (fndecl); tree arg_node = TYPE_ARG_TYPES (functyp); tree argtypes = make_tree_vec (list_length (arg_node)-1); unsigned p = 0; while (arg_node != NULL_TREE && !VOID_TYPE_P (TREE_VALUE (arg_node))) { - TREE_VEC_ELT (argtypes, p++) = TREE_VALUE (arg_node); + /* See PR94807, as to why we must exclude lambda here. */ + if (is_this_parameter (arg) && !lambda_p) + { + /* We pass a reference to *this to the param preview. */ + tree ct = TREE_TYPE (TREE_TYPE (arg)); + TREE_VEC_ELT (argtypes, p++) = cp_build_reference_type (ct, false); + } + else + TREE_VEC_ELT (argtypes, p++) = TREE_VALUE (arg_node); + arg_node = TREE_CHAIN (arg_node); + arg = DECL_CHAIN (arg); } tree argtypepack = cxx_make_type (TYPE_ARGUMENT_PACK); @@ -1754,6 +1766,7 @@ struct param_info bool pt_ref; /* Was a pointer to object. */ bool trivial_dtor; /* The frame type has a trivial DTOR. */ bool this_ptr; /* Is 'this' */ + bool lambda_cobj; /* Lambda capture object */ }; struct local_var_info @@ -3654,6 +3667,7 @@ morph_fn_to_coro (tree orig, tree *resumer, tree *destroyer) The second two entries start out empty - and only get populated when we see uses. */ param_uses = new hash_map<tree, param_info>; + bool lambda_p = LAMBDA_FUNCTION_P (orig); unsigned no_name_parm = 0; for (tree arg = DECL_ARGUMENTS (orig); arg != NULL; @@ -3694,7 +3708,19 @@ morph_fn_to_coro (tree orig, tree *resumer, tree *destroyer) } else parm.frame_type = actual_type; + parm.this_ptr = is_this_parameter (arg); + /* See PR94807. When a lambda is in a template instantiation, the + closure object is named 'this' instead of '__closure'. */ + if (lambda_p) + { + parm.lambda_cobj = parm.this_ptr + || (DECL_NAME (arg) == closure_identifier); + parm.this_ptr = false; + } + else + parm.lambda_cobj = false; + parm.trivial_dtor = TYPE_HAS_TRIVIAL_DESTRUCTOR (parm.frame_type); char *buf; if (DECL_NAME (arg)) @@ -3839,9 +3865,28 @@ morph_fn_to_coro (tree orig, tree *resumer, tree *destroyer) those of the original function. */ vec<tree, va_gc> *args = make_tree_vector (); vec_safe_push (args, resizeable); /* Space needed. */ + for (tree arg = DECL_ARGUMENTS (orig); arg != NULL; arg = DECL_CHAIN (arg)) - vec_safe_push (args, arg); + { + param_info *parm_i = param_uses->get (arg); + gcc_checking_assert (parm_i); + if (parm_i->lambda_cobj) + vec_safe_push (args, arg); + else if (parm_i->this_ptr) + { + /* We pass a reference to *this to the allocator lookup. */ + tree tt = TREE_TYPE (TREE_TYPE (arg)); + tree this_ref = build1 (INDIRECT_REF, tt, arg); + tt = cp_build_reference_type (tt, false); + this_ref = convert_to_reference (tt, this_ref, CONV_STATIC, + LOOKUP_NORMAL , NULL_TREE, + tf_warning_or_error); + vec_safe_push (args, this_ref); + } + else + vec_safe_push (args, arg); + } /* We might need to check that the provided function is nothrow. */ tree func; @@ -4031,8 +4076,10 @@ morph_fn_to_coro (tree orig, tree *resumer, tree *destroyer) false, tf_warning_or_error); /* Add this to the promise CTOR arguments list, accounting for - refs and this ptr. */ - if (parm.this_ptr) + refs and special handling for method this ptr. */ + if (parm.lambda_cobj) + vec_safe_push (promise_args, arg); + else if (parm.this_ptr) { /* We pass a reference to *this to the param preview. */ tree tt = TREE_TYPE (arg); diff --git a/gcc/testsuite/ChangeLog b/gcc/testsuite/ChangeLog index fe3375a..77f56c1 100644 --- a/gcc/testsuite/ChangeLog +++ b/gcc/testsuite/ChangeLog @@ -1,5 +1,11 @@ 2020-04-28 Iain Sandoe <iain@sandoe.co.uk> + PR c++/94760 + * g++.dg/coroutines/pr94760-mismatched-traits-and-promise-prev.C: + New test. + +2020-04-28 Iain Sandoe <iain@sandoe.co.uk> + PR c++/94759 * g++.dg/coroutines/coro-bad-alloc-00-bad-op-new.C: Adjust for updated error messages. diff --git a/gcc/testsuite/g++.dg/coroutines/pr9xxxx-mismatched-traits-and-promise-prev.C b/gcc/testsuite/g++.dg/coroutines/pr9xxxx-mismatched-traits-and-promise-prev.C new file mode 100644 index 0000000..235b5e7 --- /dev/null +++ b/gcc/testsuite/g++.dg/coroutines/pr9xxxx-mismatched-traits-and-promise-prev.C @@ -0,0 +1,29 @@ +// { dg-do compile { target c++17 } } + +#include "coro.h" + +// Test that we get matching types to traits and promise param +// preview. + +// A separate issue from allowing non-class return types. +struct Fake {} ; + +template<typename R, typename CallOp, typename ...T> +struct std::coroutine_traits<R, CallOp, T...> { + struct promise_type { + promise_type (CallOp op, T ...args) {} + Fake get_return_object() { return {}; } + std::suspend_always initial_suspend() { return {}; } + std::suspend_never final_suspend() { return {}; } + void return_void() {} + void unhandled_exception() {} + }; +}; + + +struct Foo +{ + Fake operator() (int a) { + co_return; + } +}; |