2021-10-19 13:48:00 -07:00
|
|
|
use std::rc::Rc;
|
|
|
|
|
|
|
|
use crate::ast::*;
|
2021-10-24 00:08:26 -07:00
|
|
|
use crate::symbol_table::{Fqsn, Scope, SymbolTable, DefId};
|
2021-10-19 13:48:00 -07:00
|
|
|
use crate::util::ScopeStack;
|
|
|
|
|
2021-10-19 21:14:15 -07:00
|
|
|
type FqsnPrefix = Vec<Scope>;
|
2021-10-19 13:48:00 -07:00
|
|
|
|
2021-10-24 00:08:26 -07:00
|
|
|
#[derive(Debug)]
|
|
|
|
enum NameType {
|
|
|
|
//TODO eventually this needs to support closures
|
|
|
|
Param(u8), //TODO functions limited to 255 params
|
|
|
|
LocalVariable,
|
|
|
|
ImportedDefinition(DefId),
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
enum ScopeType {
|
|
|
|
Function {
|
|
|
|
name: Rc<String>
|
|
|
|
},
|
|
|
|
//TODO add some notion of a let-like scope?
|
|
|
|
}
|
|
|
|
|
2021-10-21 15:23:48 -07:00
|
|
|
pub struct ScopeResolver<'a> {
|
2021-10-21 14:46:42 -07:00
|
|
|
symbol_table: &'a mut super::SymbolTable,
|
2021-10-24 00:08:26 -07:00
|
|
|
/// Used for import resolution. TODO maybe this can also use the lexical scope table.
|
2021-10-21 14:46:42 -07:00
|
|
|
name_scope_stack: ScopeStack<'a, Rc<String>, FqsnPrefix>,
|
2021-10-24 00:08:26 -07:00
|
|
|
lexical_scopes: ScopeStack<'a, Rc<String>, NameType, ScopeType>
|
2021-10-19 13:48:00 -07:00
|
|
|
}
|
|
|
|
|
2021-10-21 15:23:48 -07:00
|
|
|
impl<'a> ScopeResolver<'a> {
|
2021-10-21 14:46:42 -07:00
|
|
|
pub fn new(symbol_table: &'a mut SymbolTable) -> Self {
|
2021-10-24 00:08:26 -07:00
|
|
|
let name_scope_stack = ScopeStack::new(None);
|
|
|
|
let lexical_scopes = ScopeStack::new(None);
|
2021-10-21 14:46:42 -07:00
|
|
|
Self {
|
|
|
|
symbol_table,
|
|
|
|
name_scope_stack,
|
2021-10-24 00:08:26 -07:00
|
|
|
lexical_scopes,
|
2021-10-19 13:48:00 -07:00
|
|
|
}
|
2021-10-21 14:46:42 -07:00
|
|
|
}
|
2021-10-24 00:08:26 -07:00
|
|
|
|
2021-10-21 14:46:42 -07:00
|
|
|
pub fn resolve(&mut self, ast: &AST) {
|
|
|
|
walk_ast(self, ast);
|
|
|
|
}
|
2021-10-19 13:48:00 -07:00
|
|
|
|
2021-10-21 14:46:42 -07:00
|
|
|
fn lookup_name_in_scope(&self, sym_name: &QualifiedName) -> Fqsn {
|
|
|
|
let QualifiedName { components, .. } = sym_name;
|
|
|
|
let first_component = &components[0];
|
|
|
|
match self.name_scope_stack.lookup(first_component) {
|
|
|
|
None => Fqsn {
|
|
|
|
scopes: components
|
|
|
|
.iter()
|
|
|
|
.map(|name| Scope::Name(name.clone()))
|
|
|
|
.collect(),
|
|
|
|
},
|
|
|
|
Some(fqsn_prefix) => {
|
|
|
|
let mut full_name = fqsn_prefix.clone();
|
|
|
|
let rest_of_name: FqsnPrefix = components[1..]
|
|
|
|
.iter()
|
|
|
|
.map(|name| Scope::Name(name.clone()))
|
|
|
|
.collect();
|
|
|
|
full_name.extend_from_slice(&rest_of_name);
|
|
|
|
|
|
|
|
Fqsn { scopes: full_name }
|
|
|
|
}
|
2021-10-19 13:48:00 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-24 00:08:26 -07:00
|
|
|
/// This method correctly modifies the id_to_symbol table (ItemId) to have the appropriate
|
|
|
|
/// mappings.
|
|
|
|
fn handle_qualified_name(&mut self, name: &QualifiedName) {
|
2021-10-21 15:23:48 -07:00
|
|
|
println!("Handling qualified_name in resolver.rs: {:?}", name);
|
2021-10-23 00:22:12 -07:00
|
|
|
let fqsn = self.lookup_name_in_scope(name);
|
|
|
|
let symbol = self.symbol_table.fqsn_to_symbol.get(&fqsn);
|
|
|
|
if let Some(symbol) = symbol {
|
|
|
|
self.symbol_table.id_to_symbol.insert(name.id.clone(), symbol.clone());
|
2021-10-21 14:46:42 -07:00
|
|
|
}
|
2021-10-19 13:48:00 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-21 15:23:48 -07:00
|
|
|
impl<'a> ASTVisitor for ScopeResolver<'a> {
|
2021-10-24 00:08:26 -07:00
|
|
|
// Import statements bring in a bunch of local names that all map to a specific FQSN.
|
|
|
|
// FQSNs map to a Symbol (or this is an error), Symbols have a DefId. So for every
|
|
|
|
// name we import, we map a local name (a string) to a NameType::ImportedDefinition(DefId).
|
|
|
|
fn import(&mut self, import_spec: &ImportSpecifier) -> Recursion {
|
2021-10-21 14:46:42 -07:00
|
|
|
let ImportSpecifier {
|
|
|
|
ref path_components,
|
|
|
|
ref imported_names,
|
|
|
|
..
|
|
|
|
} = &import_spec;
|
|
|
|
match imported_names {
|
|
|
|
ImportedNames::All => {
|
|
|
|
let prefix = Fqsn {
|
|
|
|
scopes: path_components
|
|
|
|
.iter()
|
|
|
|
.map(|c| Scope::Name(c.clone()))
|
|
|
|
.collect(),
|
|
|
|
};
|
|
|
|
let members = self.symbol_table.symbol_trie.get_children(&prefix);
|
|
|
|
for member in members.into_iter() {
|
|
|
|
let Scope::Name(n) = member.scopes.last().unwrap();
|
|
|
|
let local_name = n.clone();
|
|
|
|
self.name_scope_stack.insert(local_name, member.scopes);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ImportedNames::LastOfPath => {
|
|
|
|
let name = path_components.last().unwrap(); //TODO handle better
|
|
|
|
let fqsn_prefix = path_components
|
|
|
|
.iter()
|
|
|
|
.map(|c| Scope::Name(c.clone()))
|
|
|
|
.collect();
|
|
|
|
self.name_scope_stack.insert(name.clone(), fqsn_prefix);
|
|
|
|
}
|
|
|
|
ImportedNames::List(ref names) => {
|
|
|
|
let fqsn_prefix: FqsnPrefix = path_components
|
|
|
|
.iter()
|
|
|
|
.map(|c| Scope::Name(c.clone()))
|
|
|
|
.collect();
|
|
|
|
for name in names.iter() {
|
|
|
|
self.name_scope_stack
|
|
|
|
.insert(name.clone(), fqsn_prefix.clone());
|
|
|
|
}
|
|
|
|
}
|
2021-10-19 13:48:00 -07:00
|
|
|
};
|
2021-10-24 00:08:26 -07:00
|
|
|
Recursion::Continue
|
|
|
|
}
|
|
|
|
|
|
|
|
fn declaration(&mut self, declaration: &Declaration) -> Recursion {
|
|
|
|
if let Declaration::FuncDecl(signature, block) = declaration {
|
|
|
|
let param_names = signature.params.iter().map(|param| param.name.clone());
|
|
|
|
let mut new_scope = self.lexical_scopes.new_scope(Some(ScopeType::Function { name: signature.name.clone() }));
|
|
|
|
|
|
|
|
for (n, param) in param_names.enumerate().into_iter() {
|
|
|
|
new_scope.insert(param, NameType::Param(n as u8));
|
|
|
|
}
|
|
|
|
|
|
|
|
let mut new_resolver = ScopeResolver {
|
|
|
|
symbol_table: self.symbol_table,
|
|
|
|
name_scope_stack: self.name_scope_stack.new_scope(None),
|
|
|
|
lexical_scopes: new_scope,
|
|
|
|
};
|
|
|
|
walk_block(&mut new_resolver, block);
|
|
|
|
Recursion::Stop
|
|
|
|
} else {
|
|
|
|
Recursion::Continue
|
|
|
|
}
|
2021-10-21 14:46:42 -07:00
|
|
|
}
|
2021-10-19 13:48:00 -07:00
|
|
|
|
2021-10-24 00:08:26 -07:00
|
|
|
fn expression(&mut self, expression: &Expression) -> Recursion {
|
2021-10-23 00:22:12 -07:00
|
|
|
use ExpressionKind::*;
|
|
|
|
match &expression.kind {
|
|
|
|
Value(name) => {
|
2021-10-24 00:08:26 -07:00
|
|
|
self.handle_qualified_name(name);
|
2021-10-23 00:22:12 -07:00
|
|
|
},
|
|
|
|
NamedStruct { name, fields: _ } => {
|
2021-10-24 00:08:26 -07:00
|
|
|
self.handle_qualified_name(name);
|
2021-10-23 00:22:12 -07:00
|
|
|
},
|
|
|
|
_ => (),
|
2021-10-21 21:55:21 -07:00
|
|
|
}
|
2021-10-24 00:08:26 -07:00
|
|
|
Recursion::Continue
|
2021-10-21 14:46:42 -07:00
|
|
|
}
|
2021-10-19 13:48:00 -07:00
|
|
|
|
2021-10-24 00:08:26 -07:00
|
|
|
fn pattern(&mut self, pat: &Pattern) -> Recursion {
|
2021-10-21 14:46:42 -07:00
|
|
|
use Pattern::*;
|
2021-10-23 00:22:12 -07:00
|
|
|
|
2021-10-21 14:46:42 -07:00
|
|
|
match pat {
|
|
|
|
//TODO I think not handling TuplePattern is an oversight
|
|
|
|
TuplePattern(_) => (),
|
|
|
|
Literal(_) | Ignored => (),
|
|
|
|
TupleStruct(name, _) | Record(name, _) | VarOrName(name) => {
|
2021-10-24 00:08:26 -07:00
|
|
|
self.handle_qualified_name(name);
|
2021-10-21 14:46:42 -07:00
|
|
|
}
|
|
|
|
};
|
2021-10-24 00:08:26 -07:00
|
|
|
Recursion::Continue
|
2021-10-21 14:46:42 -07:00
|
|
|
}
|
2021-10-19 13:48:00 -07:00
|
|
|
}
|