gccrs: Add initial support for argument capture of closures
authorPhilip Herron <philip.herron@embecosm.com>
Fri, 21 Oct 2022 14:43:54 +0000 (15:43 +0100)
committerArthur Cohen <arthur.cohen@embecosm.com>
Tue, 21 Feb 2023 11:36:52 +0000 (12:36 +0100)
commit92389b46a96ce2274f5d9a3658d771b8b0ad99c9
tree10ad93f9c578725e9249f1303f096da0b0c5c016
parenteb1202224f8e9be687589d66011485b5fc582eb5
gccrs: Add initial support for argument capture of closures

When we have a closure expression that captures a parent function's
variable we must setup the closure data to contain this. Ignoring
moveability and mutability requires for now, this patch creates the closure
structure with fields for each of the captured variables. When it comes to
compilation of the closure expression in order to support nested closures
we must setup a context of implicit mappings so that for all path
resolution we hit this implicit closure mappings lookups code before any
lookup_var_decl as this decl will not exist so the order here is important
during path resolution which is a similar problem to match expression
destructuring.

Fixes #195

gcc/rust/ChangeLog:

* backend/rust-compile-context.cc (Context::push_closure_context): New function.
(Context::pop_closure_context): Likewise.
(Context::insert_closure_binding): Likewise.
(Context::lookup_closure_binding): Likewise.
* backend/rust-compile-context.h: Declare new functions and closure mappings.
* backend/rust-compile-expr.cc (CompileExpr::visit): Visit captures properly.
(CompileExpr::generate_closure_function): Compile captures properly.
* backend/rust-compile-resolve-path.cc (ResolvePathRef::resolve): Check for
closure bindings.
* backend/rust-compile-type.cc (TyTyResolveCompile::visit): Compile capture list's
types as well.

gcc/testsuite/ChangeLog:

* rust/execute/torture/closure3.rs: New test.
gcc/rust/backend/rust-compile-context.cc
gcc/rust/backend/rust-compile-context.h
gcc/rust/backend/rust-compile-expr.cc
gcc/rust/backend/rust-compile-resolve-path.cc
gcc/rust/backend/rust-compile-type.cc
gcc/testsuite/rust/execute/torture/closure3.rs [new file with mode: 0644]