refactor(xatu): unify scope graph occurrence storage
This commit is contained in:
@@ -1,8 +1,7 @@
|
||||
#[derive(Debug)]
|
||||
pub struct ScopeGraph {
|
||||
scopes: Vec<Scope>,
|
||||
declarations: Vec<Declaration>,
|
||||
references: Vec<Reference>,
|
||||
occurrences: Vec<Occurrence>,
|
||||
}
|
||||
|
||||
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<String>) -> 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<String>) -> 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,
|
||||
|
||||
Reference in New Issue
Block a user