Struct SymbolTable

Source
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>

Source

pub fn new() -> Self

Create a new symbol table.

Source

pub fn enter(&mut self, region: RegionId)

Enter a new scope for the given region.

Source

pub fn exit(&mut self)

Exit a previously entered scope.

§Panics

Panics if there are no remaining open scopes.

Source

pub fn insert( &mut self, name: &'a str, node: NodeId, ) -> Result<(), DuplicateSymbolError<'_>>

Insert a new symbol into the current scope.

§Errors

Returns an error if the symbol is already defined in the current scope. In the case of an error the table remains unchanged.

§Panics

Panics if there is no current scope.

Source

pub fn is_visible(&self, node: NodeId) -> bool

Check whether a symbol is currently visible in the current scope.

Source

pub fn resolve(&self, name: &'a str) -> Result<NodeId, UnknownSymbolError<'_>>

Tries to resolve a symbol name in the current scope.

Source

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.

Source

pub fn depth_to_region(&self, depth: u16) -> Option<RegionId>

Returns the region corresponding to the scope at the given depth.

Source

pub fn clear(&mut self)

Resets the symbol table to its initial state while maintaining its allocated memory.

Trait Implementations§

Source§

impl<'a> Clone for SymbolTable<'a>

Source§

fn clone(&self) -> SymbolTable<'a>

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<'a> Debug for SymbolTable<'a>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<'a> Default for SymbolTable<'a>

Source§

fn default() -> SymbolTable<'a>

Returns the “default value” for a type. Read more

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> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts 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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts 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
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<'a, S, T> View<'a, &S> for T
where T: View<'a, S>, S: Copy,

Source§

fn view(module: &'a Module<'a>, id: &S) -> Option<T>

Attempt to interpret a subpart of a module as this type.