Eliminate one table in SymbolTable

This commit is contained in:
Greg Shuflin 2021-10-21 21:55:21 -07:00
parent b342213826
commit 92a695e523
2 changed files with 46 additions and 51 deletions

View File

@ -40,6 +40,11 @@ impl Fqsn {
} }
Fqsn { scopes } Fqsn { scopes }
} }
fn local_name(&self) -> Rc<String> {
let Scope::Name(name) = self.scopes.last().unwrap();
name.clone()
}
} }
//TODO eventually this should use ItemId's to avoid String-cloning //TODO eventually this should use ItemId's to avoid String-cloning
@ -115,17 +120,12 @@ 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 `ItemId`s of instances of use of names to their fully-canonicalized Fqsn form.
/// Updated by the item id resolver.
id_to_fqsn: HashMap<ItemId, Fqsn>,
/// A map of the Fqsn of an AST definition to a Symbol data structure, which contains /// 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 /// some basic information about what that symbol is and (ideally) references to other tables
/// (e.g. typechecking tables) with more information about that symbol. /// (e.g. typechecking tables) with more information about that symbol.
fqsn_to_symbol: HashMap<Fqsn, Symbol>, fqsn_to_symbol: HashMap<Fqsn, Rc<Symbol>>,
//TODO this should probably eventually replace the pair of id_to_fqsn/fqsn_to_symbol id_to_symbol: HashMap<ItemId, Rc<Symbol>>,
id_to_symbol: HashMap<ItemId, Symbol>,
} }
impl SymbolTable { impl SymbolTable {
@ -134,7 +134,7 @@ impl SymbolTable {
symbol_trie: SymbolTrie::new(), symbol_trie: SymbolTrie::new(),
fq_names: NameTable::new(), fq_names: NameTable::new(),
types: NameTable::new(), types: NameTable::new(),
id_to_fqsn: HashMap::new(),
fqsn_to_symbol: HashMap::new(), fqsn_to_symbol: HashMap::new(),
id_to_symbol: HashMap::new(), id_to_symbol: HashMap::new(),
} }
@ -153,16 +153,15 @@ impl SymbolTable {
} }
pub fn lookup_symbol(&self, id: &ItemId) -> Option<&Symbol> { pub fn lookup_symbol(&self, id: &ItemId) -> Option<&Symbol> {
let fqsn = self.id_to_fqsn.get(id); self.id_to_symbol.get(id).map(|s| s.as_ref())
fqsn.and_then(|fqsn| self.fqsn_to_symbol.get(fqsn))
} }
} }
#[allow(dead_code)] #[allow(dead_code)]
#[derive(Debug)] #[derive(Debug, Clone)]
pub struct Symbol { pub struct Symbol {
pub local_name: Rc<String>, pub local_name: Rc<String>,
//fully_qualified_name: FullyQualifiedSymbolName, fully_qualified_name: Fqsn,
pub spec: SymbolSpec, pub spec: SymbolSpec,
} }
@ -172,7 +171,7 @@ impl fmt::Display for Symbol {
} }
} }
#[derive(Debug)] #[derive(Debug, Clone)]
pub enum SymbolSpec { pub enum SymbolSpec {
Func(Vec<TypeName>), Func(Vec<TypeName>),
DataConstructor { DataConstructor {
@ -220,9 +219,15 @@ impl SymbolTable {
/// Register a new mapping of a fully-qualified symbol name (e.g. `Option::Some`) /// Register a new mapping of a fully-qualified symbol name (e.g. `Option::Some`)
/// to a Symbol, a descriptor of what that name refers to. /// to a Symbol, a descriptor of what that name refers to.
fn add_symbol(&mut self, fqsn: Fqsn, symbol: Symbol) { fn add_symbol(&mut self, id: &ItemId, fqsn: Fqsn, spec: SymbolSpec) {
let symbol = Rc::new(Symbol {
local_name: fqsn.local_name(),
fully_qualified_name: fqsn.clone(),
spec,
});
self.symbol_trie.insert(&fqsn); self.symbol_trie.insert(&fqsn);
self.fqsn_to_symbol.insert(fqsn, symbol); self.fqsn_to_symbol.insert(fqsn, symbol.clone());
self.id_to_symbol.insert(id.clone(), symbol.clone());
} }
/// Walks the AST, matching the ID of an identifier used in some expression to /// Walks the AST, matching the ID of an identifier used in some expression to
@ -250,12 +255,12 @@ impl SymbolTable {
for statement in statements { for statement in statements {
let Statement { let Statement {
id: _, id,
kind, kind,
location, location,
} = statement; //TODO I'm not sure if I need to do anything with this ID } = statement; //TODO I'm not sure if I need to do anything with this ID
let location = *location; let location = *location;
if let Err(err) = self.add_single_statement(kind, location, scope_stack) { if let Err(err) = self.add_single_statement(id, kind, location, scope_stack) {
errors.push(err); errors.push(err);
} else { } else {
// If there's an error with a name, don't recurse into subscopes of that name // If there's an error with a name, don't recurse into subscopes of that name
@ -290,6 +295,7 @@ impl SymbolTable {
fn add_single_statement( fn add_single_statement(
&mut self, &mut self,
id: &ItemId,
kind: &StatementKind, kind: &StatementKind,
location: Location, location: Location,
scope_stack: &[Scope], scope_stack: &[Scope],
@ -313,11 +319,9 @@ impl SymbolTable {
)?; )?;
self.add_symbol( self.add_symbol(
id,
fq_function, fq_function,
Symbol { SymbolSpec::Func(vec![]), //TODO does this inner vec need to exist at all?
local_name: signature.name.clone(),
spec: SymbolSpec::Func(vec![]), //TODO does this inner vec need to exist at all?
},
); );
} }
StatementKind::Declaration(Declaration::FuncDecl(signature, ..)) => { StatementKind::Declaration(Declaration::FuncDecl(signature, ..)) => {
@ -339,11 +343,9 @@ impl SymbolTable {
)?; )?;
self.add_symbol( self.add_symbol(
id,
fq_function, fq_function,
Symbol { SymbolSpec::Func(vec![]), //TODO does this inner vec need to exist at all?
local_name: signature.name.clone(),
spec: SymbolSpec::Func(vec![]), //TODO does this inner vec need to exist at all?
},
); );
} }
StatementKind::Declaration(Declaration::TypeDecl { name, .. }) => { StatementKind::Declaration(Declaration::TypeDecl { name, .. }) => {
@ -366,11 +368,9 @@ impl SymbolTable {
}, },
)?; )?;
self.add_symbol( self.add_symbol(
id,
fq_binding, fq_binding,
Symbol { SymbolSpec::Binding,
local_name: name.clone(),
spec: SymbolSpec::Binding,
},
); );
} }
StatementKind::Module(ModuleSpecifier { name, .. }) => { StatementKind::Module(ModuleSpecifier { name, .. }) => {
@ -399,7 +399,7 @@ impl SymbolTable {
let mut member_errors = vec![]; let mut member_errors = vec![];
let mut errors = vec![]; let mut errors = vec![];
let mut register = |fqsn: Fqsn, spec: SymbolSpec| { let mut register = |id: &ItemId, fqsn: Fqsn, spec: SymbolSpec| {
let name_spec = NameSpec { let name_spec = NameSpec {
location, location,
kind: TypeKind, kind: TypeKind,
@ -407,13 +407,7 @@ impl SymbolTable {
if let Err(err) = self.types.register(fqsn.clone(), name_spec) { if let Err(err) = self.types.register(fqsn.clone(), name_spec) {
errors.push(err); errors.push(err);
} else { } else {
let local_name = match spec { self.add_symbol(id, fqsn, spec);
SymbolSpec::DataConstructor { ref type_name, .. }
| SymbolSpec::RecordConstructor { ref type_name, .. } => type_name.clone(),
_ => panic!("This should never happen"),
};
let symbol = Symbol { local_name, spec };
self.add_symbol(fqsn, symbol);
}; };
}; };
@ -431,7 +425,7 @@ impl SymbolTable {
arity: 0, arity: 0,
type_name: name.clone(), type_name: name.clone(),
}; };
register(fq_name, spec); register(id, fq_name, spec);
} }
VariantKind::TupleStruct(items) => { VariantKind::TupleStruct(items) => {
let fq_name = Fqsn::from_scope_stack(scope_stack.as_ref(), name.clone()); let fq_name = Fqsn::from_scope_stack(scope_stack.as_ref(), name.clone());
@ -440,7 +434,7 @@ impl SymbolTable {
arity: items.len(), arity: items.len(),
type_name: name.clone(), type_name: name.clone(),
}; };
register(fq_name, spec); register(id, fq_name, spec);
} }
VariantKind::Record(members) => { VariantKind::Record(members) => {
let fq_name = Fqsn::from_scope_stack(scope_stack.as_ref(), name.clone()); let fq_name = Fqsn::from_scope_stack(scope_stack.as_ref(), name.clone());
@ -476,7 +470,7 @@ impl SymbolTable {
}) })
.collect(), .collect(),
}; };
register(fq_name, spec); register(id, fq_name, spec);
} }
} }
} }

View File

@ -49,11 +49,9 @@ impl<'a> Resolver<'a> {
// This might be a variable or a pattern, depending on whether this symbol // This might be a variable or a pattern, depending on whether this symbol
// already exists in the table. // already exists in the table.
fn qualified_name_in_pattern(&mut self, qualified_name: &QualifiedName) { fn qualified_name_in_pattern(&mut self, qualified_name: &QualifiedName) {
let fqsn = self.lookup_name_in_scope(qualified_name); let maybe_sym = self.symbol_table.id_to_symbol.get(&qualified_name.id).cloned();
if self.symbol_table.fqsn_to_symbol.get(&fqsn).is_some() { if let Some(symbol) = maybe_sym {
self.symbol_table self.symbol_table.id_to_symbol.insert(qualified_name.id.clone(), symbol);
.id_to_fqsn
.insert(qualified_name.id.clone(), fqsn); //TODO maybe set this to an explicit value if none?
} }
} }
} }
@ -104,9 +102,10 @@ impl<'a> ASTVisitor for Resolver<'a> {
fn qualified_name(&mut self, qualified_name: &QualifiedName) { fn qualified_name(&mut self, qualified_name: &QualifiedName) {
let fqsn = self.lookup_name_in_scope(qualified_name); let fqsn = self.lookup_name_in_scope(qualified_name);
self.symbol_table let symbol = self.symbol_table.fqsn_to_symbol.get(&fqsn);
.id_to_fqsn if let Some(symbol) = symbol {
.insert(qualified_name.id.clone(), fqsn); self.symbol_table.id_to_symbol.insert(qualified_name.id.clone(), symbol.clone());
}
} }
fn named_struct( fn named_struct(
@ -115,9 +114,11 @@ impl<'a> ASTVisitor for Resolver<'a> {
_fields: &[(Rc<String>, Expression)], _fields: &[(Rc<String>, Expression)],
) { ) {
let fqsn = self.lookup_name_in_scope(qualified_name); let fqsn = self.lookup_name_in_scope(qualified_name);
self.symbol_table
.id_to_fqsn let symbol = self.symbol_table.fqsn_to_symbol.get(&fqsn);
.insert(qualified_name.id.clone(), fqsn); if let Some(symbol) = symbol {
self.symbol_table.id_to_symbol.insert(qualified_name.id.clone(), symbol.clone());
}
} }
fn pattern(&mut self, pat: &Pattern) { fn pattern(&mut self, pat: &Pattern) {