pub struct SymbolTable<'a> { /* private fields */ }
Expand description
Symbol binding table that keeps track of symbol resolution and scoping.
Nodes may introduce a symbol so that other parts of the IR can refer to the node. Symbols have an associated name and are scoped via regions. A symbol can shadow another symbol with the same name from an outer region, but within any single region each symbol name must be unique.
When a symbol is referred to directly by the id of the node, the symbol must be in scope at the point of reference as if the reference was by name. This guarantees that transformations between directly indexed and named formats are always valid.
§Examples
let mut symbols = SymbolTable::new();
symbols.enter(RegionId(0));
symbols.insert("foo", NodeId(0)).unwrap();
assert_eq!(symbols.resolve("foo").unwrap(), NodeId(0));
symbols.enter(RegionId(1));
assert_eq!(symbols.resolve("foo").unwrap(), NodeId(0));
symbols.insert("foo", NodeId(1)).unwrap();
assert_eq!(symbols.resolve("foo").unwrap(), NodeId(1));
assert!(!symbols.is_visible(NodeId(0)));
symbols.exit();
assert_eq!(symbols.resolve("foo").unwrap(), NodeId(0));
assert!(symbols.is_visible(NodeId(0)));
assert!(!symbols.is_visible(NodeId(1)));
Implementations§
Source§impl<'a> SymbolTable<'a>
impl<'a> SymbolTable<'a>
Sourcepub fn insert(
&mut self,
name: &'a str,
node: NodeId,
) -> Result<(), DuplicateSymbolError<'_>>
pub fn insert( &mut self, name: &'a str, node: NodeId, ) -> Result<(), DuplicateSymbolError<'_>>
Sourcepub fn is_visible(&self, node: NodeId) -> bool
pub fn is_visible(&self, node: NodeId) -> bool
Check whether a symbol is currently visible in the current scope.
Sourcepub fn resolve(&self, name: &'a str) -> Result<NodeId, UnknownSymbolError<'_>>
pub fn resolve(&self, name: &'a str) -> Result<NodeId, UnknownSymbolError<'_>>
Tries to resolve a symbol name in the current scope.
Sourcepub fn region_to_depth(&self, region: RegionId) -> Option<u16>
pub fn region_to_depth(&self, region: RegionId) -> Option<u16>
Returns the depth of the given region, if it corresponds to a currently open scope.
Sourcepub fn depth_to_region(&self, depth: u16) -> Option<RegionId>
pub fn depth_to_region(&self, depth: u16) -> Option<RegionId>
Returns the region corresponding to the scope at the given depth.
Trait Implementations§
Source§impl<'a> Clone for SymbolTable<'a>
impl<'a> Clone for SymbolTable<'a>
Source§fn clone(&self) -> SymbolTable<'a>
fn clone(&self) -> SymbolTable<'a>
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read moreSource§impl<'a> Debug for SymbolTable<'a>
impl<'a> Debug for SymbolTable<'a>
Source§impl<'a> Default for SymbolTable<'a>
impl<'a> Default for SymbolTable<'a>
Source§fn default() -> SymbolTable<'a>
fn default() -> SymbolTable<'a>
Auto Trait Implementations§
impl<'a> Freeze for SymbolTable<'a>
impl<'a> RefUnwindSafe for SymbolTable<'a>
impl<'a> Send for SymbolTable<'a>
impl<'a> Sync for SymbolTable<'a>
impl<'a> Unpin for SymbolTable<'a>
impl<'a> UnwindSafe for SymbolTable<'a>
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self
into a Left
variant of Either<Self, Self>
if into_left
is true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self
into a Left
variant of Either<Self, Self>
if into_left(&self)
returns true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read more