Skip to content

Commit

Permalink
[red-knot] re-organize code in symbols.rs (#11116)
Browse files Browse the repository at this point in the history
Just a reorganization per feedback from Micha that I agreed with. Puts
all `impl` right after the corresponding `struct`, and moves iterators
further down in the file, since they are implementation detail.
  • Loading branch information
carljm authored Apr 23, 2024
1 parent 113f372 commit f685605
Showing 1 changed file with 77 additions and 77 deletions.
154 changes: 77 additions & 77 deletions crates/red_knot/src/symbols.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,83 +76,6 @@ pub(crate) struct Symbols<'a> {
pub(crate) defs: FxDashMap<SymbolId, Vec<&'a ast::Stmt>>,
}

/// Table of all symbols in all scopes for a module.
/// Derived from module AST, but holds no references to it.
#[derive(Debug)]
pub(crate) struct SymbolTable {
scopes_by_id: IndexVec<ScopeId, Scope>,
symbols_by_id: IndexVec<SymbolId, Symbol>,
}

pub(crate) struct SymbolIterator<'a, I> {
table: &'a SymbolTable,
ids: I,
}

impl<'a, I> Iterator for SymbolIterator<'a, I>
where
I: Iterator<Item = SymbolId>,
{
type Item = &'a Symbol;

fn next(&mut self) -> Option<Self::Item> {
let id = self.ids.next()?;
Some(&self.table.symbols_by_id[id])
}

fn size_hint(&self) -> (usize, Option<usize>) {
self.ids.size_hint()
}
}

impl<'a, I> FusedIterator for SymbolIterator<'a, I> where
I: Iterator<Item = SymbolId> + FusedIterator
{
}

impl<'a, I> DoubleEndedIterator for SymbolIterator<'a, I>
where
I: Iterator<Item = SymbolId> + DoubleEndedIterator,
{
fn next_back(&mut self) -> Option<Self::Item> {
let id = self.ids.next_back()?;
Some(&self.table.symbols_by_id[id])
}
}

pub(crate) struct ScopeIterator<'a, I> {
table: &'a SymbolTable,
ids: I,
}

impl<'a, I> Iterator for ScopeIterator<'a, I>
where
I: Iterator<Item = ScopeId>,
{
type Item = &'a Scope;

fn next(&mut self) -> Option<Self::Item> {
let id = self.ids.next()?;
Some(&self.table.scopes_by_id[id])
}

fn size_hint(&self) -> (usize, Option<usize>) {
self.ids.size_hint()
}
}

impl<'a, I> FusedIterator for ScopeIterator<'a, I> where I: Iterator<Item = ScopeId> + FusedIterator {}

impl<'a, I> DoubleEndedIterator for ScopeIterator<'a, I>
where
I: Iterator<Item = ScopeId> + DoubleEndedIterator,
{
fn next_back(&mut self) -> Option<Self::Item> {
let id = self.ids.next_back()?;
Some(&self.table.scopes_by_id[id])
}
}

impl<'a> Symbols<'a> {
pub(crate) fn from_ast(module: &'a ast::ModModule) -> Self {
let symbols = Symbols {
Expand All @@ -169,6 +92,14 @@ impl<'a> Symbols<'a> {
}
}

/// Table of all symbols in all scopes for a module.
/// Derived from module AST, but holds no references to it.
#[derive(Debug)]
pub(crate) struct SymbolTable {
scopes_by_id: IndexVec<ScopeId, Scope>,
symbols_by_id: IndexVec<SymbolId, Symbol>,
}

impl SymbolTable {
pub(crate) fn new() -> Self {
let mut table = SymbolTable {
Expand Down Expand Up @@ -288,6 +219,75 @@ impl SymbolTable {
}
}

pub(crate) struct SymbolIterator<'a, I> {
table: &'a SymbolTable,
ids: I,
}

impl<'a, I> Iterator for SymbolIterator<'a, I>
where
I: Iterator<Item = SymbolId>,
{
type Item = &'a Symbol;

fn next(&mut self) -> Option<Self::Item> {
let id = self.ids.next()?;
Some(&self.table.symbols_by_id[id])
}

fn size_hint(&self) -> (usize, Option<usize>) {
self.ids.size_hint()
}
}

impl<'a, I> FusedIterator for SymbolIterator<'a, I> where
I: Iterator<Item = SymbolId> + FusedIterator
{
}

impl<'a, I> DoubleEndedIterator for SymbolIterator<'a, I>
where
I: Iterator<Item = SymbolId> + DoubleEndedIterator,
{
fn next_back(&mut self) -> Option<Self::Item> {
let id = self.ids.next_back()?;
Some(&self.table.symbols_by_id[id])
}
}

pub(crate) struct ScopeIterator<'a, I> {
table: &'a SymbolTable,
ids: I,
}

impl<'a, I> Iterator for ScopeIterator<'a, I>
where
I: Iterator<Item = ScopeId>,
{
type Item = &'a Scope;

fn next(&mut self) -> Option<Self::Item> {
let id = self.ids.next()?;
Some(&self.table.scopes_by_id[id])
}

fn size_hint(&self) -> (usize, Option<usize>) {
self.ids.size_hint()
}
}

impl<'a, I> FusedIterator for ScopeIterator<'a, I> where I: Iterator<Item = ScopeId> + FusedIterator {}

impl<'a, I> DoubleEndedIterator for ScopeIterator<'a, I>
where
I: Iterator<Item = ScopeId> + DoubleEndedIterator,
{
fn next_back(&mut self) -> Option<Self::Item> {
let id = self.ids.next_back()?;
Some(&self.table.scopes_by_id[id])
}
}

struct SymbolsBuilder<'a> {
symbols: Symbols<'a>,
scopes: Vec<ScopeId>,
Expand Down

0 comments on commit f685605

Please sign in to comment.