Remove fqsn_to_id table

The trie already takes care of this
This commit is contained in:
Greg Shuflin 2021-11-01 23:43:03 -07:00
parent 0bf0b3e2e8
commit ec92e14fcf
3 changed files with 40 additions and 24 deletions

View File

@ -141,11 +141,6 @@ pub struct SymbolTable {
fq_names: NameTable<NameKind>, //Note that presence of two tables implies that a type and other binding with the same name can co-exist fq_names: NameTable<NameKind>, //Note that presence of two tables implies that a type and other binding with the same name can co-exist
types: NameTable<TypeKind>, types: NameTable<TypeKind>,
/// A map of the Fqsn of an AST definition to a Symbol data structure, which contains
/// some basic information about what that symbol is and (ideally) references to other tables
/// (e.g. typechecking tables) with more information about that symbol.
fqsn_to_symbol: HashMap<Fqsn, Rc<Symbol>>,
id_to_symbol: HashMap<ItemId, Rc<Symbol>>, id_to_symbol: HashMap<ItemId, Rc<Symbol>>,
def_to_symbol: HashMap<DefId, Rc<Symbol>>, def_to_symbol: HashMap<DefId, Rc<Symbol>>,
} }
@ -158,7 +153,6 @@ impl SymbolTable {
fq_names: NameTable::new(), fq_names: NameTable::new(),
types: NameTable::new(), types: NameTable::new(),
fqsn_to_symbol: HashMap::new(),
id_to_symbol: HashMap::new(), id_to_symbol: HashMap::new(),
def_to_symbol: HashMap::new(), def_to_symbol: HashMap::new(),
}; };
@ -207,8 +201,7 @@ impl SymbolTable {
fn add_symbol(&mut self, id: &ItemId, fqsn: Fqsn, spec: SymbolSpec) { fn add_symbol(&mut self, id: &ItemId, fqsn: Fqsn, spec: SymbolSpec) {
let def_id = self.def_id_store.fresh(); let def_id = self.def_id_store.fresh();
let symbol = Rc::new(Symbol { fully_qualified_name: fqsn.clone(), spec, def_id }); let symbol = Rc::new(Symbol { fully_qualified_name: fqsn.clone(), spec, def_id });
self.symbol_trie.insert(&fqsn); self.symbol_trie.insert(&fqsn, def_id);
self.fqsn_to_symbol.insert(fqsn, symbol.clone());
self.id_to_symbol.insert(*id, symbol.clone()); self.id_to_symbol.insert(*id, symbol.clone());
self.def_to_symbol.insert(def_id, symbol); self.def_to_symbol.insert(def_id, symbol);
} }
@ -218,8 +211,7 @@ impl SymbolTable {
let spec = SymbolSpec::Builtin(builtin); let spec = SymbolSpec::Builtin(builtin);
let symbol = Rc::new(Symbol { fully_qualified_name: fqsn.clone(), spec, def_id }); let symbol = Rc::new(Symbol { fully_qualified_name: fqsn.clone(), spec, def_id });
self.symbol_trie.insert(&fqsn); self.symbol_trie.insert(&fqsn, def_id);
self.fqsn_to_symbol.insert(fqsn, symbol.clone());
self.def_to_symbol.insert(def_id, symbol); self.def_to_symbol.insert(def_id, symbol);
} }

View File

@ -47,7 +47,13 @@ impl<'a> ScopeResolver<'a> {
let local_name = components.first().unwrap().clone(); let local_name = components.first().unwrap().clone();
let name_type = self.lexical_scopes.lookup(&local_name); let name_type = self.lexical_scopes.lookup(&local_name);
let fqsn = Fqsn { scopes: components.iter().map(|name| Scope::Name(name.clone())).collect() }; let fqsn = Fqsn { scopes: components.iter().map(|name| Scope::Name(name.clone())).collect() };
let symbol = self.symbol_table.fqsn_to_symbol.get(&fqsn); let symbol = self
.symbol_table
.symbol_trie
.lookup(&fqsn)
.as_ref()
.and_then(|def_id| self.symbol_table.lookup_symbol_by_def(def_id))
.cloned();
//println!("\tFound lexical_scope entry: {:?} and {} symbol: {:?}", name_type, fqsn, symbol); //println!("\tFound lexical_scope entry: {:?} and {} symbol: {:?}", name_type, fqsn, symbol);
//TODO handle a "partial" qualified name, and also handle it down in the pattern-matching //TODO handle a "partial" qualified name, and also handle it down in the pattern-matching
@ -57,9 +63,16 @@ impl<'a> ScopeResolver<'a> {
if components.len() == 1 { if components.len() == 1 {
match name_type { match name_type {
Some(NameType::Import(fqsn)) => { Some(NameType::Import(fqsn)) => {
let symbol = self.symbol_table.fqsn_to_symbol.get(fqsn); let symbol = self
.symbol_table
.symbol_trie
.lookup(&fqsn)
.as_ref()
.and_then(|def_id| self.symbol_table.lookup_symbol_by_def(def_id))
.cloned();
if let Some(symbol) = symbol { if let Some(symbol) = symbol {
self.symbol_table.id_to_symbol.insert(*id, symbol.clone()); self.symbol_table.id_to_symbol.insert(*id, Rc::new(symbol));
} }
} }
Some(NameType::Param(n)) => { Some(NameType::Param(n)) => {
@ -77,12 +90,12 @@ impl<'a> ScopeResolver<'a> {
} }
None => None =>
if let Some(symbol) = symbol { if let Some(symbol) = symbol {
self.symbol_table.id_to_symbol.insert(*id, symbol.clone()); self.symbol_table.id_to_symbol.insert(*id, Rc::new(symbol));
}, },
} }
} else { } else {
if let Some(symbol) = symbol { if let Some(symbol) = symbol {
self.symbol_table.id_to_symbol.insert(*id, symbol.clone()); self.symbol_table.id_to_symbol.insert(*id, Rc::new(symbol));
} }
} }
} }
@ -254,9 +267,15 @@ impl<'a> ASTVisitor for ScopeResolver<'a> {
} else { } else {
let fqsn = let fqsn =
Fqsn { scopes: components.iter().map(|name| Scope::Name(name.clone())).collect() }; Fqsn { scopes: components.iter().map(|name| Scope::Name(name.clone())).collect() };
let symbol = self.symbol_table.fqsn_to_symbol.get(&fqsn); let symbol = self
.symbol_table
.symbol_trie
.lookup(&fqsn)
.as_ref()
.and_then(|def_id| self.symbol_table.lookup_symbol_by_def(def_id))
.cloned();
if let Some(symbol) = symbol { if let Some(symbol) = symbol {
self.symbol_table.id_to_symbol.insert(*id, symbol.clone()); self.symbol_table.id_to_symbol.insert(*id, Rc::new(symbol));
} }
} }
} }

View File

@ -5,10 +5,10 @@ use std::{
use radix_trie::{Trie, TrieCommon, TrieKey}; use radix_trie::{Trie, TrieCommon, TrieKey};
use super::{Fqsn, Scope}; use super::{DefId, Fqsn, Scope};
#[derive(Debug)] #[derive(Debug)]
pub struct SymbolTrie(Trie<Fqsn, ()>); pub struct SymbolTrie(Trie<Fqsn, DefId>);
impl TrieKey for Fqsn { impl TrieKey for Fqsn {
fn encode_bytes(&self) -> Vec<u8> { fn encode_bytes(&self) -> Vec<u8> {
@ -28,8 +28,12 @@ impl SymbolTrie {
SymbolTrie(Trie::new()) SymbolTrie(Trie::new())
} }
pub fn insert(&mut self, fqsn: &Fqsn) { pub fn insert(&mut self, fqsn: &Fqsn, def_id: DefId) {
self.0.insert(fqsn.clone(), ()); self.0.insert(fqsn.clone(), def_id);
}
pub fn lookup(&self, fqsn: &Fqsn) -> Option<DefId> {
self.0.get(fqsn).cloned()
} }
pub fn get_children(&self, fqsn: &Fqsn) -> Vec<Fqsn> { pub fn get_children(&self, fqsn: &Fqsn) -> Vec<Fqsn> {
@ -53,11 +57,12 @@ mod test {
#[test] #[test]
fn test_trie_insertion() { fn test_trie_insertion() {
let id = DefId::default();
let mut trie = SymbolTrie::new(); let mut trie = SymbolTrie::new();
trie.insert(&make_fqsn(&["unrelated", "thing"])); trie.insert(&make_fqsn(&["unrelated", "thing"]), id);
trie.insert(&make_fqsn(&["outer", "inner"])); trie.insert(&make_fqsn(&["outer", "inner"]), id);
trie.insert(&make_fqsn(&["outer", "inner", "still_inner"])); trie.insert(&make_fqsn(&["outer", "inner", "still_inner"]), id);
let children = trie.get_children(&make_fqsn(&["outer", "inner"])); let children = trie.get_children(&make_fqsn(&["outer", "inner"]));
assert_eq!(children.len(), 1); assert_eq!(children.len(), 1);