diff options
author | Philip Herron <philip.herron@embecosm.com> | 2022-10-21 13:40:40 +0100 |
---|---|---|
committer | Arthur Cohen <arthur.cohen@embecosm.com> | 2023-02-21 12:36:52 +0100 |
commit | f7c258b291182308538ff18c3ace76b1c11e699a (patch) | |
tree | 44ad68a4642decc066855aded1eb396185c8ba7b /gcc/rust/resolve/rust-name-resolver.h | |
parent | 32a4659c5e405771dc8c83568b30efefc77a2031 (diff) | |
download | gcc-f7c258b291182308538ff18c3ace76b1c11e699a.zip gcc-f7c258b291182308538ff18c3ace76b1c11e699a.tar.gz gcc-f7c258b291182308538ff18c3ace76b1c11e699a.tar.bz2 |
gccrs: Add closure binding's tracking to name resolution
When we have a closure block referencing variables in a parent function,
we must track what these are. We do this by having a context of closures
so if we have a variable reference and its declared in a rib whose node id
is less than the node id of the closure's node id we know it must be a
captured variable. We also need to iterate all possible closure contexts
as we might be in the case of a nested closure.
Addresses #195
gcc/rust/ChangeLog:
* resolve/rust-ast-resolve-expr.cc (ResolveExpr::visit): Use proper closure
contexts.
* resolve/rust-name-resolver.cc (Scope::lookup_decl_type): New function.
(Scope::lookup_rib_for_decl): Likewise.
(Resolver::insert_resolved_name): Insert captured items.
(Resolver::push_closure_context): New function.
(Resolver::pop_closure_context): Likewise.
(Resolver::insert_captured_item): Likewise.
(Resolver::decl_needs_capture): Likewise.
(Resolver::get_captures): Likewise.
* resolve/rust-name-resolver.h: Declare new functions.
Diffstat (limited to 'gcc/rust/resolve/rust-name-resolver.h')
-rw-r--r-- | gcc/rust/resolve/rust-name-resolver.h | 17 |
1 files changed, 17 insertions, 0 deletions
diff --git a/gcc/rust/resolve/rust-name-resolver.h b/gcc/rust/resolve/rust-name-resolver.h index 54d5954..f38cbb9 100644 --- a/gcc/rust/resolve/rust-name-resolver.h +++ b/gcc/rust/resolve/rust-name-resolver.h @@ -96,6 +96,8 @@ public: void insert (const CanonicalPath &ident, NodeId id, Location locus, Rib::ItemType type = Rib::ItemType::Unknown); bool lookup (const CanonicalPath &ident, NodeId *id); + bool lookup_decl_type (NodeId id, Rib::ItemType *type); + bool lookup_rib_for_decl (NodeId id, const Rib **rib); void iterate (std::function<bool (Rib *)> cb); void iterate (std::function<bool (const Rib *)> cb) const; @@ -109,6 +111,8 @@ public: CrateNum get_crate_num () const { return crate_num; } + const std::vector<Rib *> &get_context () const { return stack; }; + private: CrateNum crate_num; std::vector<Rib *> stack; @@ -191,6 +195,15 @@ public: return current_module_stack.at (current_module_stack.size () - 2); } + void push_closure_context (NodeId closure_expr_id); + void pop_closure_context (); + void insert_captured_item (NodeId id); + const std::set<NodeId> &get_captures (NodeId id) const; + +protected: + bool decl_needs_capture (NodeId decl_rib_node_id, NodeId closure_rib_node_id, + const Scope &scope); + private: Resolver (); @@ -234,6 +247,10 @@ private: // keep track of the current module scope ids std::vector<NodeId> current_module_stack; + + // captured variables mappings + std::vector<NodeId> closure_context; + std::map<NodeId, std::set<NodeId>> closures_capture_mappings; }; } // namespace Resolver |