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 }
}
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
@ -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
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
/// 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, 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, Symbol>,
id_to_symbol: HashMap<ItemId, Rc<Symbol>>,
}
impl SymbolTable {
@ -134,7 +134,7 @@ impl SymbolTable {
symbol_trie: SymbolTrie::new(),
fq_names: NameTable::new(),
types: NameTable::new(),
id_to_fqsn: HashMap::new(),
fqsn_to_symbol: HashMap::new(),
id_to_symbol: HashMap::new(),
}
@ -153,16 +153,15 @@ impl SymbolTable {
}
pub fn lookup_symbol(&self, id: &ItemId) -> Option<&Symbol> {
let fqsn = self.id_to_fqsn.get(id);
fqsn.and_then(|fqsn| self.fqsn_to_symbol.get(fqsn))
self.id_to_symbol.get(id).map(|s| s.as_ref())
}
}
#[allow(dead_code)]
#[derive(Debug)]
#[derive(Debug, Clone)]
pub struct Symbol {
pub local_name: Rc<String>,
//fully_qualified_name: FullyQualifiedSymbolName,
fully_qualified_name: Fqsn,
pub spec: SymbolSpec,
}
@ -172,7 +171,7 @@ impl fmt::Display for Symbol {
}
}
#[derive(Debug)]
#[derive(Debug, Clone)]
pub enum SymbolSpec {
Func(Vec<TypeName>),
DataConstructor {
@ -220,9 +219,15 @@ impl SymbolTable {
/// 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.
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.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
@ -250,12 +255,12 @@ impl SymbolTable {
for statement in statements {
let Statement {
id: _,
id,
kind,
location,
} = statement; //TODO I'm not sure if I need to do anything with this ID
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);
} else {
// 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(
&mut self,
id: &ItemId,
kind: &StatementKind,
location: Location,
scope_stack: &[Scope],
@ -313,11 +319,9 @@ impl SymbolTable {
)?;
self.add_symbol(
id,
fq_function,
Symbol {
local_name: signature.name.clone(),
spec: SymbolSpec::Func(vec![]), //TODO does this inner vec need to exist at all?
},
SymbolSpec::Func(vec![]), //TODO does this inner vec need to exist at all?
);
}
StatementKind::Declaration(Declaration::FuncDecl(signature, ..)) => {
@ -339,11 +343,9 @@ impl SymbolTable {
)?;
self.add_symbol(
id,
fq_function,
Symbol {
local_name: signature.name.clone(),
spec: SymbolSpec::Func(vec![]), //TODO does this inner vec need to exist at all?
},
SymbolSpec::Func(vec![]), //TODO does this inner vec need to exist at all?
);
}
StatementKind::Declaration(Declaration::TypeDecl { name, .. }) => {
@ -366,11 +368,9 @@ impl SymbolTable {
},
)?;
self.add_symbol(
id,
fq_binding,
Symbol {
local_name: name.clone(),
spec: SymbolSpec::Binding,
},
SymbolSpec::Binding,
);
}
StatementKind::Module(ModuleSpecifier { name, .. }) => {
@ -399,7 +399,7 @@ impl SymbolTable {
let mut member_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 {
location,
kind: TypeKind,
@ -407,13 +407,7 @@ impl SymbolTable {
if let Err(err) = self.types.register(fqsn.clone(), name_spec) {
errors.push(err);
} else {
let local_name = match 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);
self.add_symbol(id, fqsn, spec);
};
};
@ -431,7 +425,7 @@ impl SymbolTable {
arity: 0,
type_name: name.clone(),
};
register(fq_name, spec);
register(id, fq_name, spec);
}
VariantKind::TupleStruct(items) => {
let fq_name = Fqsn::from_scope_stack(scope_stack.as_ref(), name.clone());
@ -440,7 +434,7 @@ impl SymbolTable {
arity: items.len(),
type_name: name.clone(),
};
register(fq_name, spec);
register(id, fq_name, spec);
}
VariantKind::Record(members) => {
let fq_name = Fqsn::from_scope_stack(scope_stack.as_ref(), name.clone());
@ -476,7 +470,7 @@ impl SymbolTable {
})
.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
// already exists in the table.
fn qualified_name_in_pattern(&mut self, qualified_name: &QualifiedName) {
let fqsn = self.lookup_name_in_scope(qualified_name);
if self.symbol_table.fqsn_to_symbol.get(&fqsn).is_some() {
self.symbol_table
.id_to_fqsn
.insert(qualified_name.id.clone(), fqsn); //TODO maybe set this to an explicit value if none?
let maybe_sym = self.symbol_table.id_to_symbol.get(&qualified_name.id).cloned();
if let Some(symbol) = maybe_sym {
self.symbol_table.id_to_symbol.insert(qualified_name.id.clone(), symbol);
}
}
}
@ -104,9 +102,10 @@ impl<'a> ASTVisitor for Resolver<'a> {
fn qualified_name(&mut self, qualified_name: &QualifiedName) {
let fqsn = self.lookup_name_in_scope(qualified_name);
self.symbol_table
.id_to_fqsn
.insert(qualified_name.id.clone(), fqsn);
let symbol = self.symbol_table.fqsn_to_symbol.get(&fqsn);
if let Some(symbol) = symbol {
self.symbol_table.id_to_symbol.insert(qualified_name.id.clone(), symbol.clone());
}
}
fn named_struct(
@ -115,9 +114,11 @@ impl<'a> ASTVisitor for Resolver<'a> {
_fields: &[(Rc<String>, Expression)],
) {
let fqsn = self.lookup_name_in_scope(qualified_name);
self.symbol_table
.id_to_fqsn
.insert(qualified_name.id.clone(), fqsn);
let symbol = self.symbol_table.fqsn_to_symbol.get(&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) {