From 3f6f7d33bc029d8bb2d9738495f5af81e15976d1 Mon Sep 17 00:00:00 2001 From: alexion Date: Sat, 26 Sep 2026 15:11:26 -0400 Subject: [PATCH] refactor(xatu): unify scope graph occurrence storage --- src/xatu/scope_graph.rs | 36 ++++++++++++++++++++++++------------ 1 file changed, 24 insertions(+), 12 deletions(-) diff --git a/src/xatu/scope_graph.rs b/src/xatu/scope_graph.rs index 8bf577d..f5f5d28 100644 --- a/src/xatu/scope_graph.rs +++ b/src/xatu/scope_graph.rs @@ -1,8 +1,7 @@ #[derive(Debug)] pub struct ScopeGraph { scopes: Vec, - declarations: Vec, - references: Vec, + occurrences: Vec, } impl Default for ScopeGraph { @@ -15,8 +14,7 @@ impl ScopeGraph { pub fn new() -> Self { Self { scopes: vec![Scope::new(None)], - declarations: Vec::new(), - references: Vec::new(), + occurrences: Vec::new(), } } @@ -34,22 +32,22 @@ impl ScopeGraph { } pub fn add_declaration(&mut self, scope: ScopeId, name: impl Into) -> DeclarationId { - let declaration_id = DeclarationId(self.declarations.len()); - self.declarations.push(Declaration { + let declaration_id = DeclarationId(self.occurrences.len()); + self.occurrences.push(Occurrence::Declaration(Declaration { scope, name: name.into(), - }); + })); self.scope_mut(scope).declarations.push(declaration_id); declaration_id } pub fn add_reference(&mut self, scope: ScopeId, name: impl Into) -> ReferenceId { - let reference_id = ReferenceId(self.references.len()); - self.references.push(Reference { + let reference_id = ReferenceId(self.occurrences.len()); + self.occurrences.push(Occurrence::Reference(Reference { scope, name: name.into(), - }); + })); self.scope_mut(scope).references.push(reference_id); reference_id @@ -108,11 +106,19 @@ impl ScopeGraph { } fn declaration(&self, declaration: DeclarationId) -> &Declaration { - &self.declarations[declaration.0] + let Occurrence::Declaration(declaration) = &self.occurrences[declaration.0] else { + unreachable!("declaration IDs point to declarations") + }; + + declaration } fn reference(&self, reference: ReferenceId) -> &Reference { - &self.references[reference.0] + let Occurrence::Reference(reference) = &self.occurrences[reference.0] else { + unreachable!("reference IDs point to references") + }; + + reference } } @@ -159,6 +165,12 @@ impl Scope { } } +#[derive(Debug)] +enum Occurrence { + Declaration(Declaration), + Reference(Reference), +} + #[derive(Debug)] struct Declaration { scope: ScopeId,