aboutsummaryrefslogtreecommitdiff
path: root/gcc/rust/backend/rust-compile-context.h
diff options
context:
space:
mode:
authorPhilip Herron <philip.herron@embecosm.com>2022-10-21 15:43:54 +0100
committerArthur Cohen <arthur.cohen@embecosm.com>2023-02-21 12:36:52 +0100
commit92389b46a96ce2274f5d9a3658d771b8b0ad99c9 (patch)
tree10ad93f9c578725e9249f1303f096da0b0c5c016 /gcc/rust/backend/rust-compile-context.h
parenteb1202224f8e9be687589d66011485b5fc582eb5 (diff)
downloadgcc-92389b46a96ce2274f5d9a3658d771b8b0ad99c9.zip
gcc-92389b46a96ce2274f5d9a3658d771b8b0ad99c9.tar.gz
gcc-92389b46a96ce2274f5d9a3658d771b8b0ad99c9.tar.bz2
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.
Diffstat (limited to 'gcc/rust/backend/rust-compile-context.h')
-rw-r--r--gcc/rust/backend/rust-compile-context.h9
1 files changed, 9 insertions, 0 deletions
diff --git a/gcc/rust/backend/rust-compile-context.h b/gcc/rust/backend/rust-compile-context.h
index d2d3a53..8e8fac8 100644
--- a/gcc/rust/backend/rust-compile-context.h
+++ b/gcc/rust/backend/rust-compile-context.h
@@ -345,6 +345,11 @@ public:
return mangler.mangle_item (ty, path);
}
+ void push_closure_context (HirId id);
+ void pop_closure_context ();
+ void insert_closure_binding (HirId id, tree expr);
+ bool lookup_closure_binding (HirId id, tree *expr);
+
std::vector<tree> &get_type_decls () { return type_decls; }
std::vector<::Bvariable *> &get_var_decls () { return var_decls; }
std::vector<tree> &get_const_decls () { return const_decls; }
@@ -377,6 +382,10 @@ private:
std::map<HirId, tree> implicit_pattern_bindings;
std::map<hashval_t, tree> main_variants;
+ // closure bindings
+ std::vector<HirId> closure_scope_bindings;
+ std::map<HirId, std::map<HirId, tree>> closure_bindings;
+
// To GCC middle-end
std::vector<tree> type_decls;
std::vector<::Bvariable *> var_decls;