Invert parent-shadowed bindings map (#4847)

This commit is contained in:
Charlie Marsh 2023-06-04 00:18:46 -04:00 committed by GitHub
parent 3fa4440d87
commit 466719247b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 57 additions and 61 deletions

View file

@ -47,7 +47,7 @@ pub struct SemanticModel<'a> {
// Arena of global bindings.
globals: GlobalsArena<'a>,
// Map from binding index to indexes of bindings that shadow it in other scopes.
pub shadowed_bindings: HashMap<BindingId, Vec<BindingId>, BuildNoHashHasher<BindingId>>,
pub shadowed_bindings: HashMap<BindingId, BindingId, BuildNoHashHasher<BindingId>>,
// Body iteration; used to peek at siblings.
pub body: &'a [Stmt],
pub body_index: usize,
@ -145,13 +145,23 @@ impl<'a> SemanticModel<'a> {
})
}
/// Return the current `Binding` for a given `name`.
/// Return the current [`Binding`] for a given `name`.
pub fn find_binding(&self, member: &str) -> Option<&Binding> {
self.scopes()
.find_map(|scope| scope.get(member))
.map(|binding_id| &self.bindings[binding_id])
}
/// Return the [`Binding`] that the given [`BindingId`] shadows, if any.
///
/// Note that this will only return bindings that are shadowed by a binding in a parent scope.
pub fn shadowed_binding(&self, binding_id: BindingId) -> Option<&Binding> {
self.shadowed_bindings
.get(&binding_id)
.copied()
.map(|id| &self.bindings[id])
}
/// Return `true` if `member` is bound as a builtin.
pub fn is_builtin(&self, member: &str) -> bool {
self.find_binding(member)