mirror of
https://github.com/roc-lang/roc.git
synced 2025-09-26 13:29:12 +00:00
Rename symbol and module lookup struts
This commit is contained in:
parent
33cbaa5cd8
commit
63e722f61d
5 changed files with 53 additions and 34 deletions
|
@ -1,6 +1,6 @@
|
||||||
use crate::env::Env;
|
use crate::env::Env;
|
||||||
use crate::procedure::{QualifiedReference, References};
|
use crate::procedure::{QualifiedReference, References};
|
||||||
use crate::scope::{LookedupSymbol, PendingAbilitiesInScope, Scope};
|
use crate::scope::{PendingAbilitiesInScope, Scope, SymbolLookup};
|
||||||
use roc_collections::{ImMap, MutSet, SendMap, VecMap, VecSet};
|
use roc_collections::{ImMap, MutSet, SendMap, VecMap, VecSet};
|
||||||
use roc_module::ident::{Ident, Lowercase, TagName};
|
use roc_module::ident::{Ident, Lowercase, TagName};
|
||||||
use roc_module::symbol::Symbol;
|
use roc_module::symbol::Symbol;
|
||||||
|
@ -386,7 +386,10 @@ pub(crate) fn make_apply_symbol(
|
||||||
// Look it up in scope!
|
// Look it up in scope!
|
||||||
|
|
||||||
match scope.lookup_str(ident, region) {
|
match scope.lookup_str(ident, region) {
|
||||||
Ok(LookedupSymbol { symbol, params: _ }) => {
|
Ok(SymbolLookup {
|
||||||
|
symbol,
|
||||||
|
module_params: _,
|
||||||
|
}) => {
|
||||||
references.insert_type_lookup(symbol, QualifiedReference::Unqualified);
|
references.insert_type_lookup(symbol, QualifiedReference::Unqualified);
|
||||||
Ok(symbol)
|
Ok(symbol)
|
||||||
}
|
}
|
||||||
|
@ -398,7 +401,10 @@ pub(crate) fn make_apply_symbol(
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
match env.qualified_lookup(scope, module_name, ident, region) {
|
match env.qualified_lookup(scope, module_name, ident, region) {
|
||||||
Ok(LookedupSymbol { symbol, params: _ }) => {
|
Ok(SymbolLookup {
|
||||||
|
symbol,
|
||||||
|
module_params: _,
|
||||||
|
}) => {
|
||||||
references.insert_type_lookup(symbol, QualifiedReference::Qualified);
|
references.insert_type_lookup(symbol, QualifiedReference::Qualified);
|
||||||
Ok(symbol)
|
Ok(symbol)
|
||||||
}
|
}
|
||||||
|
|
|
@ -507,7 +507,7 @@ fn canonicalize_alias<'a>(
|
||||||
#[macro_export]
|
#[macro_export]
|
||||||
macro_rules! params_in_abilities_unimplemented {
|
macro_rules! params_in_abilities_unimplemented {
|
||||||
($lookup:expr) => {
|
($lookup:expr) => {
|
||||||
match $lookup.params {
|
match $lookup.module_params {
|
||||||
None => $lookup.symbol,
|
None => $lookup.symbol,
|
||||||
Some(_) => unimplemented!("params in abilities"),
|
Some(_) => unimplemented!("params in abilities"),
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
use std::path::Path;
|
use std::path::Path;
|
||||||
|
|
||||||
use crate::procedure::References;
|
use crate::procedure::References;
|
||||||
use crate::scope::{LookedupModule, LookedupSymbol, Scope};
|
use crate::scope::{ModuleLookup, Scope, SymbolLookup};
|
||||||
use bumpalo::Bump;
|
use bumpalo::Bump;
|
||||||
use roc_collections::{MutMap, VecSet};
|
use roc_collections::{MutMap, VecSet};
|
||||||
use roc_module::ident::{Ident, ModuleName};
|
use roc_module::ident::{Ident, ModuleName};
|
||||||
|
@ -78,7 +78,7 @@ impl<'a> Env<'a> {
|
||||||
module_name_str: &str,
|
module_name_str: &str,
|
||||||
ident: &str,
|
ident: &str,
|
||||||
region: Region,
|
region: Region,
|
||||||
) -> Result<LookedupSymbol, RuntimeError> {
|
) -> Result<SymbolLookup, RuntimeError> {
|
||||||
debug_assert!(
|
debug_assert!(
|
||||||
!module_name_str.is_empty(),
|
!module_name_str.is_empty(),
|
||||||
"Called env.qualified_lookup with an unqualified ident: {ident:?}"
|
"Called env.qualified_lookup with an unqualified ident: {ident:?}"
|
||||||
|
@ -112,7 +112,7 @@ impl<'a> Env<'a> {
|
||||||
module_id: ModuleId,
|
module_id: ModuleId,
|
||||||
ident: &str,
|
ident: &str,
|
||||||
region: Region,
|
region: Region,
|
||||||
) -> Result<LookedupSymbol, RuntimeError> {
|
) -> Result<SymbolLookup, RuntimeError> {
|
||||||
if let Some(module) = scope.modules.lookup_by_id(&module_id) {
|
if let Some(module) = scope.modules.lookup_by_id(&module_id) {
|
||||||
self.qualified_lookup_help(scope, module, ident, region)
|
self.qualified_lookup_help(scope, module, ident, region)
|
||||||
} else {
|
} else {
|
||||||
|
@ -124,10 +124,10 @@ impl<'a> Env<'a> {
|
||||||
fn qualified_lookup_help(
|
fn qualified_lookup_help(
|
||||||
&mut self,
|
&mut self,
|
||||||
scope: &Scope,
|
scope: &Scope,
|
||||||
module: LookedupModule,
|
module: ModuleLookup,
|
||||||
ident: &str,
|
ident: &str,
|
||||||
region: Region,
|
region: Region,
|
||||||
) -> Result<LookedupSymbol, RuntimeError> {
|
) -> Result<SymbolLookup, RuntimeError> {
|
||||||
let is_type_name = ident.starts_with(|c: char| c.is_uppercase());
|
let is_type_name = ident.starts_with(|c: char| c.is_uppercase());
|
||||||
|
|
||||||
// You can do qualified lookups on your own module, e.g.
|
// You can do qualified lookups on your own module, e.g.
|
||||||
|
@ -143,7 +143,7 @@ impl<'a> Env<'a> {
|
||||||
self.qualified_value_lookups.insert(symbol);
|
self.qualified_value_lookups.insert(symbol);
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(LookedupSymbol::no_params(symbol))
|
Ok(SymbolLookup::no_params(symbol))
|
||||||
}
|
}
|
||||||
None => {
|
None => {
|
||||||
let error = RuntimeError::LookupNotInScope {
|
let error = RuntimeError::LookupNotInScope {
|
||||||
|
|
|
@ -10,7 +10,7 @@ use crate::num::{
|
||||||
use crate::params_in_abilities_unimplemented;
|
use crate::params_in_abilities_unimplemented;
|
||||||
use crate::pattern::{canonicalize_pattern, BindingsFromPattern, Pattern, PermitShadows};
|
use crate::pattern::{canonicalize_pattern, BindingsFromPattern, Pattern, PermitShadows};
|
||||||
use crate::procedure::{QualifiedReference, References};
|
use crate::procedure::{QualifiedReference, References};
|
||||||
use crate::scope::{LookedupSymbol, Scope};
|
use crate::scope::{Scope, SymbolLookup};
|
||||||
use crate::traverse::{walk_expr, Visitor};
|
use crate::traverse::{walk_expr, Visitor};
|
||||||
use roc_collections::soa::Index;
|
use roc_collections::soa::Index;
|
||||||
use roc_collections::{SendMap, VecMap, VecSet};
|
use roc_collections::{SendMap, VecMap, VecSet};
|
||||||
|
@ -1934,7 +1934,13 @@ fn canonicalize_var_lookup(
|
||||||
(can_expr, output)
|
(can_expr, output)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn lookup_to_expr(LookedupSymbol { symbol, params }: LookedupSymbol, var: Variable) -> Expr {
|
fn lookup_to_expr(
|
||||||
|
SymbolLookup {
|
||||||
|
symbol,
|
||||||
|
module_params: params,
|
||||||
|
}: SymbolLookup,
|
||||||
|
var: Variable,
|
||||||
|
) -> Expr {
|
||||||
if let Some(params) = params {
|
if let Some(params) = params {
|
||||||
Expr::ParamsVar {
|
Expr::ParamsVar {
|
||||||
symbol,
|
symbol,
|
||||||
|
|
|
@ -76,7 +76,7 @@ impl Scope {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn lookup(&self, ident: &Ident, region: Region) -> Result<LookedupSymbol, RuntimeError> {
|
pub fn lookup(&self, ident: &Ident, region: Region) -> Result<SymbolLookup, RuntimeError> {
|
||||||
self.lookup_str(ident.as_str(), region)
|
self.lookup_str(ident.as_str(), region)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -91,7 +91,7 @@ impl Scope {
|
||||||
.push(("Set".into(), Symbol::SET_SET, Region::zero()));
|
.push(("Set".into(), Symbol::SET_SET, Region::zero()));
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn lookup_str(&self, ident: &str, region: Region) -> Result<LookedupSymbol, RuntimeError> {
|
pub fn lookup_str(&self, ident: &str, region: Region) -> Result<SymbolLookup, RuntimeError> {
|
||||||
use ContainsIdent::*;
|
use ContainsIdent::*;
|
||||||
|
|
||||||
match self.scope_contains_ident(ident) {
|
match self.scope_contains_ident(ident) {
|
||||||
|
@ -205,14 +205,14 @@ impl Scope {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn has_imported_symbol(&self, ident: &str) -> Option<(LookedupSymbol, Region)> {
|
fn has_imported_symbol(&self, ident: &str) -> Option<(SymbolLookup, Region)> {
|
||||||
self.imported_symbols
|
self.imported_symbols
|
||||||
.iter()
|
.iter()
|
||||||
.find_map(|(import, symbol, original_region)| {
|
.find_map(|(import, symbol, original_region)| {
|
||||||
if ident == import.as_str() {
|
if ident == import.as_str() {
|
||||||
match self.modules.lookup_by_id(&symbol.module_id()) {
|
match self.modules.lookup_by_id(&symbol.module_id()) {
|
||||||
Some(module) => Some((module.into_symbol(*symbol), *original_region)),
|
Some(module) => Some((module.into_symbol(*symbol), *original_region)),
|
||||||
None => Some((LookedupSymbol::no_params(*symbol), *original_region)),
|
None => Some((SymbolLookup::no_params(*symbol), *original_region)),
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
None
|
None
|
||||||
|
@ -394,9 +394,13 @@ impl Scope {
|
||||||
region: Region,
|
region: Region,
|
||||||
) -> Result<(), (Symbol, Region)> {
|
) -> Result<(), (Symbol, Region)> {
|
||||||
match self.scope_contains_ident(ident.as_str()) {
|
match self.scope_contains_ident(ident.as_str()) {
|
||||||
ContainsIdent::InScope(LookedupSymbol { symbol, params: _ }, region) => {
|
ContainsIdent::InScope(
|
||||||
Err((symbol, region))
|
SymbolLookup {
|
||||||
}
|
symbol,
|
||||||
|
module_params: _,
|
||||||
|
},
|
||||||
|
region,
|
||||||
|
) => Err((symbol, region)),
|
||||||
ContainsIdent::NotPresent | ContainsIdent::NotInScope(_) => {
|
ContainsIdent::NotPresent | ContainsIdent::NotInScope(_) => {
|
||||||
self.imported_symbols.push((ident, symbol, region));
|
self.imported_symbols.push((ident, symbol, region));
|
||||||
Ok(())
|
Ok(())
|
||||||
|
@ -541,7 +545,7 @@ pub fn create_alias(
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
enum ContainsIdent {
|
enum ContainsIdent {
|
||||||
InScope(LookedupSymbol, Region),
|
InScope(SymbolLookup, Region),
|
||||||
NotInScope(IdentId),
|
NotInScope(IdentId),
|
||||||
NotPresent,
|
NotPresent,
|
||||||
}
|
}
|
||||||
|
@ -582,7 +586,7 @@ impl ScopedIdentIds {
|
||||||
let index = ident_id.index();
|
let index = ident_id.index();
|
||||||
if self.in_scope[index] {
|
if self.in_scope[index] {
|
||||||
return InScope(
|
return InScope(
|
||||||
LookedupSymbol::no_params(Symbol::new(self.home, ident_id)),
|
SymbolLookup::no_params(Symbol::new(self.home, ident_id)),
|
||||||
self.regions[index],
|
self.regions[index],
|
||||||
);
|
);
|
||||||
} else {
|
} else {
|
||||||
|
@ -699,21 +703,21 @@ impl ScopeModules {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn lookup(&self, module_name: &ModuleName) -> Option<LookedupModule> {
|
pub fn lookup(&self, module_name: &ModuleName) -> Option<ModuleLookup> {
|
||||||
self.names
|
self.names
|
||||||
.iter()
|
.iter()
|
||||||
.position(|name| name == module_name)
|
.position(|name| name == module_name)
|
||||||
.map(|index| LookedupModule {
|
.map(|index| ModuleLookup {
|
||||||
id: self.ids[index],
|
id: self.ids[index],
|
||||||
params: self.params[index],
|
params: self.params[index],
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn lookup_by_id(&self, module_id: &ModuleId) -> Option<LookedupModule> {
|
pub fn lookup_by_id(&self, module_id: &ModuleId) -> Option<ModuleLookup> {
|
||||||
self.ids
|
self.ids
|
||||||
.iter()
|
.iter()
|
||||||
.position(|id| id == module_id)
|
.position(|id| id == module_id)
|
||||||
.map(|index| LookedupModule {
|
.map(|index| ModuleLookup {
|
||||||
id: self.ids[index],
|
id: self.ids[index],
|
||||||
params: self.params[index],
|
params: self.params[index],
|
||||||
})
|
})
|
||||||
|
@ -765,14 +769,17 @@ impl ScopeModules {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
pub struct LookedupSymbol {
|
pub struct SymbolLookup {
|
||||||
pub symbol: Symbol,
|
pub symbol: Symbol,
|
||||||
pub params: Option<Symbol>,
|
pub module_params: Option<Symbol>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl LookedupSymbol {
|
impl SymbolLookup {
|
||||||
pub fn new(symbol: Symbol, params: Option<Symbol>) -> Self {
|
pub fn new(symbol: Symbol, params: Option<Symbol>) -> Self {
|
||||||
Self { symbol, params }
|
Self {
|
||||||
|
symbol,
|
||||||
|
module_params: params,
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn no_params(symbol: Symbol) -> Self {
|
pub fn no_params(symbol: Symbol) -> Self {
|
||||||
|
@ -780,18 +787,18 @@ impl LookedupSymbol {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct LookedupModule {
|
pub struct ModuleLookup {
|
||||||
pub id: ModuleId,
|
pub id: ModuleId,
|
||||||
pub params: Option<Symbol>,
|
pub params: Option<Symbol>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl LookedupModule {
|
impl ModuleLookup {
|
||||||
pub fn into_symbol(&self, symbol: Symbol) -> LookedupSymbol {
|
pub fn into_symbol(&self, symbol: Symbol) -> SymbolLookup {
|
||||||
debug_assert_eq!(symbol.module_id(), self.id);
|
debug_assert_eq!(symbol.module_id(), self.id);
|
||||||
|
|
||||||
LookedupSymbol {
|
SymbolLookup {
|
||||||
symbol,
|
symbol,
|
||||||
params: self.params,
|
module_params: self.params,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue