Skip to content

Commit

Permalink
Extract Root.resolve_symbol to Module.
Browse files Browse the repository at this point in the history
  • Loading branch information
tritao committed Oct 21, 2024
1 parent 68aef6e commit c867f57
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 60 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1243,7 +1243,7 @@ impl ty::TyExpression {
let storage_key_ident = Ident::new_with_override("StorageKey".into(), span.clone());

// Search for the struct declaration with the call path above.
let (storage_key_decl, _) = ctx.namespace().root().resolve_symbol(
let (storage_key_decl, _) = ctx.namespace().root().module.resolve_symbol(
handler,
engines,
&storage_key_mod_path,
Expand Down
64 changes: 62 additions & 2 deletions sway-core/src/semantic_analysis/namespace/module.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
use crate::{engine_threading::Engines, language::Visibility, Ident};
use crate::{
engine_threading::Engines,
language::Visibility,
semantic_analysis::type_resolve::{resolve_associated_item, resolve_associated_type},
Ident, TypeId,
};

use super::{
lexical_scope::{Items, LexicalScope},
root::Root,
LexicalScopeId, ModuleName, ModulePath, ModulePathBuf,
LexicalScopeId, ModuleName, ModulePath, ModulePathBuf, ResolvedDeclaration,
};

use rustc_hash::FxHasher;
Expand Down Expand Up @@ -285,6 +290,61 @@ impl Module {
let parent_scope_id = self.current_lexical_scope().parent;
self.current_lexical_scope_id = parent_scope_id.unwrap_or(0);
}

/// Given a path to a module and the identifier of a symbol within that module, resolve its
/// declaration.
///
/// If the symbol is within the given module's namespace via import, we recursively traverse
/// imports until we find the original declaration.
pub(crate) fn resolve_symbol(
&self,
handler: &Handler,
engines: &Engines,
mod_path: &ModulePath,
symbol: &Ident,
self_type: Option<TypeId>,
) -> Result<(ResolvedDeclaration, Vec<Ident>), ErrorEmitted> {
// This block tries to resolve associated types
let mut module = self;
let mut current_mod_path = vec![];
let mut decl_opt = None;
for ident in mod_path.iter() {
if let Some(decl) = decl_opt {
decl_opt = Some(resolve_associated_type(
handler, engines, module, ident, decl, None, self_type,
)?);
} else {
match module.submodules.get(ident.as_str()) {
Some(ns) => {
module = ns;
current_mod_path.push(ident.clone());
}
None => {
decl_opt = Some(
module
.current_lexical_scope()
.items
.resolve_symbol(handler, engines, ident)?,
);
}
}
}
}
if let Some(decl) = decl_opt {
let decl =
resolve_associated_item(handler, engines, module, symbol, decl, None, self_type)?;
return Ok((decl, current_mod_path));
}

self.lookup_submodule(handler, engines, mod_path)
.and_then(|module| {
let decl = module
.current_lexical_scope()
.items
.resolve_symbol(handler, engines, symbol)?;
Ok((decl, mod_path.to_vec()))
})
}
}

impl From<Root> for Module {
Expand Down
1 change: 1 addition & 0 deletions sway-core/src/semantic_analysis/namespace/namespace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ impl Namespace {
self_type: Option<TypeId>,
) -> Result<ResolvedDeclaration, ErrorEmitted> {
self.root
.module
.resolve_symbol(handler, engines, &self.mod_path, symbol, self_type)
.map(|r| r.0)
}
Expand Down
58 changes: 1 addition & 57 deletions sway-core/src/semantic_analysis/namespace/root.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ use crate::{
CallPath, Visibility,
},
namespace::{ModulePath, ModulePathBuf},
semantic_analysis::type_resolve::{resolve_associated_item, resolve_associated_type},
TypeId,
};
use sway_error::{
Expand Down Expand Up @@ -730,63 +729,8 @@ impl Root {
.chain(&call_path.prefixes)
.cloned()
.collect();
self.resolve_symbol(handler, engines, &symbol_path, &call_path.suffix, self_type)
}

/// Given a path to a module and the identifier of a symbol within that module, resolve its
/// declaration.
///
/// If the symbol is within the given module's namespace via import, we recursively traverse
/// imports until we find the original declaration.
pub(crate) fn resolve_symbol(
&self,
handler: &Handler,
engines: &Engines,
mod_path: &ModulePath,
symbol: &Ident,
self_type: Option<TypeId>,
) -> Result<(ResolvedDeclaration, Vec<Ident>), ErrorEmitted> {
// This block tries to resolve associated types
let mut module = &self.module;
let mut current_mod_path = vec![];
let mut decl_opt = None;
for ident in mod_path.iter() {
if let Some(decl) = decl_opt {
decl_opt = Some(resolve_associated_type(
handler, engines, module, ident, decl, None, self_type,
)?);
} else {
match module.submodules.get(ident.as_str()) {
Some(ns) => {
module = ns;
current_mod_path.push(ident.clone());
}
None => {
decl_opt = Some(
module
.current_lexical_scope()
.items
.resolve_symbol(handler, engines, ident)?,
);
}
}
}
}
if let Some(decl) = decl_opt {
let decl =
resolve_associated_item(handler, engines, module, symbol, decl, None, self_type)?;
return Ok((decl, current_mod_path));
}

self.module
.lookup_submodule(handler, engines, mod_path)
.and_then(|module| {
let decl = module
.current_lexical_scope()
.items
.resolve_symbol(handler, engines, symbol)?;
Ok((decl, mod_path.to_vec()))
})
.resolve_symbol(handler, engines, &symbol_path, &call_path.suffix, self_type)
}
}

Expand Down

0 comments on commit c867f57

Please sign in to comment.