2021-10-19 13:48:00 -07:00
|
|
|
use std::rc::Rc;
|
|
|
|
|
2021-10-29 04:01:38 -07:00
|
|
|
use crate::{
|
|
|
|
ast::*,
|
2021-11-02 01:20:30 -07:00
|
|
|
symbol_table::{Fqsn, ScopeSegment, SymbolSpec, SymbolTable},
|
2021-10-29 04:01:38 -07:00
|
|
|
util::ScopeStack,
|
|
|
|
};
|
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
|
2021-10-24 01:00:32 -07:00
|
|
|
Param(u8), //TODO handle implications of functions being limited to 255 params
|
2021-10-25 01:02:19 -07:00
|
|
|
LocalVariable(ItemId),
|
2021-11-02 18:07:08 -07:00
|
|
|
LocalFunction(ItemId),
|
2021-10-24 01:00:32 -07:00
|
|
|
Import(Fqsn),
|
2021-10-24 00:08:26 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
enum ScopeType {
|
2021-10-29 04:01:38 -07:00
|
|
|
Function { name: Rc<String> },
|
2021-10-24 19:05:41 -07:00
|
|
|
Lambda,
|
2021-10-26 11:37:43 -07:00
|
|
|
PatternMatch,
|
2021-10-24 00:08:26 -07:00
|
|
|
//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-26 11:37:43 -07:00
|
|
|
//TODO maybe this shouldn't be a scope stack, b/c the recursion behavior comes from multiple
|
|
|
|
//instances of ScopeResolver
|
2021-10-24 01:00:32 -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 lexical_scopes = ScopeStack::new(None);
|
2021-10-29 04:01:38 -07:00
|
|
|
Self { symbol_table, lexical_scopes }
|
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-11-01 23:58:27 -07:00
|
|
|
/// This method correctly modifies the id_to_def table (ItemId) to have the appropriate
|
2021-10-24 00:08:26 -07:00
|
|
|
/// mappings.
|
2021-10-24 01:00:32 -07:00
|
|
|
fn lookup_name_in_scope(&mut self, name: &QualifiedName) {
|
|
|
|
let QualifiedName { id, components } = name;
|
2021-11-01 21:34:45 -07:00
|
|
|
|
|
|
|
let local_name = components.first().unwrap().clone();
|
|
|
|
let name_type = self.lexical_scopes.lookup(&local_name);
|
2021-11-02 01:20:30 -07:00
|
|
|
let fqsn = Fqsn { scopes: components.iter().map(|name| ScopeSegment::Name(name.clone())).collect() };
|
2021-11-01 23:58:27 -07:00
|
|
|
let def_id = self.symbol_table.symbol_trie.lookup(&fqsn);
|
2021-11-01 21:34:45 -07:00
|
|
|
|
2021-10-26 13:02:40 -07:00
|
|
|
//TODO handle a "partial" qualified name, and also handle it down in the pattern-matching
|
|
|
|
//section
|
2021-10-24 01:00:32 -07:00
|
|
|
if components.len() == 1 {
|
|
|
|
match name_type {
|
|
|
|
Some(NameType::Import(fqsn)) => {
|
2021-11-01 23:58:27 -07:00
|
|
|
let def_id = self.symbol_table.symbol_trie.lookup(&fqsn);
|
2021-11-01 23:43:03 -07:00
|
|
|
|
2021-11-01 23:58:27 -07:00
|
|
|
if let Some(def_id) = def_id {
|
|
|
|
self.symbol_table.id_to_def.insert(*id, def_id);
|
2021-10-24 01:00:32 -07:00
|
|
|
}
|
2021-10-29 04:01:38 -07:00
|
|
|
}
|
2021-10-24 02:02:04 -07:00
|
|
|
Some(NameType::Param(n)) => {
|
|
|
|
let spec = SymbolSpec::FunctionParam(*n);
|
2021-10-25 14:52:19 -07:00
|
|
|
//TODO need to come up with a better solution for local variable FQSNs
|
2021-11-02 01:20:30 -07:00
|
|
|
let lscope = ScopeSegment::Name(Rc::new("<local-param>".to_string()));
|
|
|
|
let fqsn = Fqsn { scopes: vec![lscope, ScopeSegment::Name(local_name.clone())] };
|
2021-10-24 02:02:04 -07:00
|
|
|
self.symbol_table.add_symbol(id, fqsn, spec);
|
|
|
|
}
|
2021-11-02 18:07:08 -07:00
|
|
|
Some(NameType::LocalFunction(item_id)) => {
|
|
|
|
let def_id = self.symbol_table.id_to_def.get(item_id);
|
|
|
|
if let Some(def_id) = def_id {
|
|
|
|
let def_id = def_id.clone();
|
|
|
|
self.symbol_table.id_to_def.insert(*id, def_id);
|
|
|
|
}
|
|
|
|
}
|
2021-10-25 01:02:19 -07:00
|
|
|
Some(NameType::LocalVariable(item_id)) => {
|
2021-11-01 23:58:27 -07:00
|
|
|
let def_id = self.symbol_table.id_to_def.get(item_id);
|
|
|
|
if let Some(def_id) = def_id {
|
|
|
|
let def_id = def_id.clone();
|
|
|
|
self.symbol_table.id_to_def.insert(*id, def_id);
|
2021-10-25 01:02:19 -07:00
|
|
|
}
|
2021-10-29 04:01:38 -07:00
|
|
|
}
|
2021-11-01 21:34:45 -07:00
|
|
|
None =>
|
2021-11-01 23:58:27 -07:00
|
|
|
if let Some(def_id) = def_id {
|
|
|
|
self.symbol_table.id_to_def.insert(*id, def_id);
|
2021-11-01 21:34:45 -07:00
|
|
|
},
|
2021-10-24 01:00:32 -07:00
|
|
|
}
|
|
|
|
} else {
|
2021-11-01 23:58:27 -07:00
|
|
|
if let Some(def_id) = def_id {
|
|
|
|
self.symbol_table.id_to_def.insert(*id, def_id);
|
2021-10-24 01:00:32 -07:00
|
|
|
}
|
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-29 04:01:38 -07:00
|
|
|
let ImportSpecifier { ref path_components, ref imported_names, .. } = &import_spec;
|
2021-10-21 14:46:42 -07:00
|
|
|
match imported_names {
|
|
|
|
ImportedNames::All => {
|
2021-10-29 04:01:38 -07:00
|
|
|
let prefix =
|
2021-11-02 01:20:30 -07:00
|
|
|
Fqsn { scopes: path_components.iter().map(|c| ScopeSegment::Name(c.clone())).collect() };
|
2021-10-21 14:46:42 -07:00
|
|
|
let members = self.symbol_table.symbol_trie.get_children(&prefix);
|
2021-10-24 01:00:32 -07:00
|
|
|
for fqsn in members.into_iter() {
|
2021-11-02 16:56:12 -07:00
|
|
|
self.lexical_scopes.insert(fqsn.last_elem(), NameType::Import(fqsn));
|
2021-10-21 14:46:42 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
ImportedNames::LastOfPath => {
|
2021-11-02 01:20:30 -07:00
|
|
|
let fqsn =
|
|
|
|
Fqsn { scopes: path_components.iter().map(|c| ScopeSegment::Name(c.clone())).collect() };
|
2021-11-02 16:56:12 -07:00
|
|
|
self.lexical_scopes.insert(fqsn.last_elem(), NameType::Import(fqsn));
|
2021-10-21 14:46:42 -07:00
|
|
|
}
|
|
|
|
ImportedNames::List(ref names) => {
|
2021-11-02 01:20:30 -07:00
|
|
|
let fqsn_prefix: Vec<ScopeSegment> =
|
|
|
|
path_components.iter().map(|c| ScopeSegment::Name(c.clone())).collect();
|
2021-10-21 14:46:42 -07:00
|
|
|
for name in names.iter() {
|
2021-10-24 01:00:32 -07:00
|
|
|
let mut scopes = fqsn_prefix.clone();
|
2021-11-02 01:20:30 -07:00
|
|
|
scopes.push(ScopeSegment::Name(name.clone()));
|
2021-10-24 01:00:32 -07:00
|
|
|
let fqsn = Fqsn { scopes };
|
2021-11-02 16:56:12 -07:00
|
|
|
self.lexical_scopes.insert(fqsn.last_elem(), NameType::Import(fqsn));
|
2021-10-21 14:46:42 -07:00
|
|
|
}
|
|
|
|
}
|
2021-10-19 13:48:00 -07:00
|
|
|
};
|
2021-10-24 00:08:26 -07:00
|
|
|
Recursion::Continue
|
|
|
|
}
|
|
|
|
|
2021-10-25 01:02:19 -07:00
|
|
|
fn declaration(&mut self, declaration: &Declaration, id: &ItemId) -> Recursion {
|
|
|
|
let cur_function_name = match self.lexical_scopes.get_name() {
|
|
|
|
//TODO this needs to be a fqsn
|
|
|
|
Some(ScopeType::Function { name }) => Some(name.clone()),
|
2021-10-29 04:01:38 -07:00
|
|
|
_ => None,
|
2021-10-25 01:02:19 -07:00
|
|
|
};
|
2021-10-24 01:00:32 -07:00
|
|
|
match declaration {
|
|
|
|
Declaration::FuncDecl(signature, block) => {
|
|
|
|
let param_names = signature.params.iter().map(|param| param.name.clone());
|
|
|
|
//TODO I'm 90% sure this is right, until I get to closures
|
|
|
|
//let mut new_scope = self.lexical_scopes.new_scope(Some(ScopeType::Function { name: signature.name.clone() }));
|
2021-10-29 04:01:38 -07:00
|
|
|
let mut new_scope =
|
|
|
|
ScopeStack::new(Some(ScopeType::Function { name: signature.name.clone() }));
|
2021-10-24 01:00:32 -07:00
|
|
|
|
2021-10-30 00:01:59 -07:00
|
|
|
for (n, param) in param_names.enumerate() {
|
2021-10-24 01:00:32 -07:00
|
|
|
new_scope.insert(param, NameType::Param(n as u8));
|
|
|
|
}
|
2021-10-24 00:08:26 -07:00
|
|
|
|
2021-11-02 18:07:08 -07:00
|
|
|
self.lexical_scopes.insert(signature.name.clone(), NameType::LocalFunction(*id));
|
|
|
|
|
2021-10-29 04:01:38 -07:00
|
|
|
let mut new_resolver =
|
|
|
|
ScopeResolver { symbol_table: self.symbol_table, lexical_scopes: new_scope };
|
2021-10-24 01:00:32 -07:00
|
|
|
walk_block(&mut new_resolver, block);
|
|
|
|
Recursion::Stop
|
2021-10-24 00:08:26 -07:00
|
|
|
}
|
2021-10-25 01:02:19 -07:00
|
|
|
Declaration::Binding { name, .. } => {
|
|
|
|
if let Some(fn_name) = cur_function_name {
|
|
|
|
// We are within a function scope
|
2021-11-02 01:20:30 -07:00
|
|
|
let fqsn =
|
|
|
|
Fqsn { scopes: vec![ScopeSegment::Name(fn_name), ScopeSegment::Name(name.clone())] };
|
2021-10-25 01:02:19 -07:00
|
|
|
self.symbol_table.add_symbol(id, fqsn, SymbolSpec::LocalVariable);
|
2021-10-27 01:17:53 -07:00
|
|
|
self.lexical_scopes.insert(name.clone(), NameType::LocalVariable(*id));
|
2021-10-25 01:02:19 -07:00
|
|
|
}
|
2021-10-24 01:00:32 -07:00
|
|
|
Recursion::Continue
|
|
|
|
}
|
2021-10-29 04:01:38 -07:00
|
|
|
_ => Recursion::Continue,
|
2021-10-24 00:08:26 -07:00
|
|
|
}
|
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 01:00:32 -07:00
|
|
|
self.lookup_name_in_scope(name);
|
2021-10-29 04:01:38 -07:00
|
|
|
}
|
2021-10-23 00:22:12 -07:00
|
|
|
NamedStruct { name, fields: _ } => {
|
2021-10-24 01:00:32 -07:00
|
|
|
self.lookup_name_in_scope(name);
|
2021-10-29 04:01:38 -07:00
|
|
|
}
|
2021-10-24 19:05:41 -07:00
|
|
|
Lambda { params, body, .. } => {
|
|
|
|
let param_names = params.iter().map(|param| param.name.clone());
|
|
|
|
//TODO need to properly handle closure scope, this is currently broken
|
|
|
|
//let mut new_scope = self.lexical_scopes.new_scope(Some(ScopeType::Function { name: signature.name.clone() }));
|
|
|
|
let mut new_scope = ScopeStack::new(Some(ScopeType::Lambda));
|
|
|
|
|
2021-10-30 00:01:59 -07:00
|
|
|
for (n, param) in param_names.enumerate() {
|
2021-10-24 19:05:41 -07:00
|
|
|
new_scope.insert(param, NameType::Param(n as u8));
|
|
|
|
}
|
|
|
|
|
2021-10-29 04:01:38 -07:00
|
|
|
let mut new_resolver =
|
|
|
|
ScopeResolver { symbol_table: self.symbol_table, lexical_scopes: new_scope };
|
2021-10-24 19:05:41 -07:00
|
|
|
walk_block(&mut new_resolver, body);
|
|
|
|
return Recursion::Stop;
|
|
|
|
}
|
2021-10-26 11:37:43 -07:00
|
|
|
IfExpression { discriminator, body } => {
|
|
|
|
if let Some(d) = discriminator.as_ref() {
|
2021-10-26 13:37:03 -07:00
|
|
|
walk_expression(self, d);
|
2021-10-26 11:37:43 -07:00
|
|
|
}
|
|
|
|
let mut resolver = ScopeResolver {
|
|
|
|
lexical_scopes: self.lexical_scopes.new_scope(Some(ScopeType::PatternMatch)),
|
2021-10-29 04:01:38 -07:00
|
|
|
symbol_table: self.symbol_table,
|
2021-10-26 11:37:43 -07:00
|
|
|
};
|
|
|
|
let new_resolver = &mut resolver;
|
|
|
|
|
|
|
|
match body.as_ref() {
|
2021-10-29 04:01:38 -07:00
|
|
|
IfExpressionBody::SimpleConditional { then_case, else_case } => {
|
2021-10-26 11:37:43 -07:00
|
|
|
walk_block(new_resolver, then_case);
|
|
|
|
if let Some(block) = else_case.as_ref() {
|
|
|
|
walk_block(new_resolver, block)
|
|
|
|
}
|
|
|
|
}
|
2021-10-29 04:01:38 -07:00
|
|
|
IfExpressionBody::SimplePatternMatch { pattern, then_case, else_case } => {
|
2021-10-26 11:37:43 -07:00
|
|
|
walk_pattern(new_resolver, pattern);
|
2021-10-26 13:37:03 -07:00
|
|
|
walk_block(new_resolver, then_case);
|
|
|
|
if let Some(block) = else_case.as_ref() {
|
|
|
|
walk_block(new_resolver, block)
|
2021-10-26 11:37:43 -07:00
|
|
|
}
|
|
|
|
}
|
2021-10-29 04:01:38 -07:00
|
|
|
IfExpressionBody::CondList(arms) =>
|
2021-10-26 11:37:43 -07:00
|
|
|
for arm in arms {
|
|
|
|
match arm.condition {
|
|
|
|
Condition::Pattern(ref pat) => {
|
|
|
|
walk_pattern(new_resolver, pat);
|
|
|
|
}
|
|
|
|
Condition::TruncatedOp(ref _binop, ref expr) => {
|
|
|
|
walk_expression(new_resolver, expr);
|
|
|
|
}
|
|
|
|
Condition::Expression(ref expr) => {
|
|
|
|
walk_expression(new_resolver, expr);
|
|
|
|
}
|
|
|
|
Condition::Else => (),
|
|
|
|
}
|
|
|
|
if let Some(ref guard) = arm.guard {
|
2021-10-26 13:37:03 -07:00
|
|
|
walk_expression(new_resolver, guard);
|
2021-10-26 11:37:43 -07:00
|
|
|
}
|
|
|
|
walk_block(new_resolver, &arm.body);
|
2021-10-29 04:01:38 -07:00
|
|
|
},
|
2021-10-26 11:37:43 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
return Recursion::Stop;
|
2021-10-29 04:01:38 -07:00
|
|
|
}
|
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 {
|
2021-10-26 11:37:43 -07:00
|
|
|
Literal(..) | Ignored | TuplePattern(..) => (),
|
|
|
|
TupleStruct(name, _) | Record(name, _) => {
|
2021-10-24 01:00:32 -07:00
|
|
|
self.lookup_name_in_scope(name);
|
2021-10-21 14:46:42 -07:00
|
|
|
}
|
2021-10-26 11:37:43 -07:00
|
|
|
//TODO this isn't really the right syntax for a VarOrName
|
2021-10-26 13:12:24 -07:00
|
|
|
VarOrName(QualifiedName { id, components }) => {
|
2021-10-26 13:02:40 -07:00
|
|
|
if components.len() == 1 {
|
|
|
|
//TODO need a better way to construct a FQSN from a QualifiedName
|
|
|
|
let local_name: Rc<String> = components[0].clone();
|
2021-11-02 01:20:30 -07:00
|
|
|
let lscope = ScopeSegment::Name(Rc::new("<local-case-match>".to_string()));
|
|
|
|
let fqsn = Fqsn { scopes: vec![lscope, ScopeSegment::Name(local_name.clone())] };
|
2021-10-26 13:02:40 -07:00
|
|
|
self.symbol_table.add_symbol(id, fqsn, SymbolSpec::LocalVariable);
|
2021-10-27 01:17:53 -07:00
|
|
|
self.lexical_scopes.insert(local_name, NameType::LocalVariable(*id));
|
2021-10-26 13:02:40 -07:00
|
|
|
} else {
|
2021-11-02 01:20:30 -07:00
|
|
|
let fqsn = Fqsn {
|
|
|
|
scopes: components.iter().map(|name| ScopeSegment::Name(name.clone())).collect(),
|
|
|
|
};
|
2021-11-01 23:58:27 -07:00
|
|
|
let def_id = self.symbol_table.symbol_trie.lookup(&fqsn);
|
|
|
|
|
|
|
|
if let Some(def_id) = def_id {
|
|
|
|
self.symbol_table.id_to_def.insert(*id, def_id);
|
2021-10-26 13:02:40 -07:00
|
|
|
}
|
|
|
|
}
|
2021-10-29 04:01:38 -07:00
|
|
|
}
|
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
|
|
|
}
|