Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
113 changes: 48 additions & 65 deletions crates/hir/src/semantics/hir_to_def.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,10 @@ use crate::{
container::{InContainer, ScopeId},
hir_def::{
Ident,
block::BlockId,
expr::{Expr, ExprId},
module::{ModuleId, generate::GenerateBlockId, instantiation::InstanceId},
},
semantics::pathres::{name_scope, resolve_name},
symbol::{DefLoc, NameContext},
semantics::pathres::{descend_scope, name_scope, resolve_name, resolve_path},
symbol::NameContext,
};

#[derive(Default, Debug)]
Expand All @@ -30,12 +28,20 @@ impl Source2DefCtx<'_, '_> {
let mut resolve = |expr: &Expr| match expr {
Expr::Field { receiver, field } => {
let field = field.as_ref()?;
if let Some(res) = self.resolve_expr_path(cont_id, expr_id, NameContext::Value) {
self.hir_cache.expr_map.insert(InContainer::new(cont_id, expr_id), res.clone());
return Some(res);
}
let receiver_res = self.expr_to_def(InContainer::new(cont_id, *receiver))?;
let res = self.resolve_member_from_resolution(receiver_res, field)?;
self.hir_cache.expr_map.insert(InContainer::new(cont_id, expr_id), res.clone());
Some(res)
}
Expr::ElementSelect { receiver, .. } => {
if let Some(res) = self.resolve_expr_path(cont_id, expr_id, NameContext::Value) {
self.hir_cache.expr_map.insert(InContainer::new(cont_id, expr_id), res.clone());
return Some(res);
}
let res = self.expr_to_def(InContainer::new(cont_id, *receiver))?;
self.hir_cache.expr_map.insert(InContainer::new(cont_id, expr_id), res.clone());
Some(res)
Expand Down Expand Up @@ -89,76 +95,53 @@ impl Source2DefCtx<'_, '_> {
field: &Ident,
) -> Option<PathResolution> {
for def_id in res.def_ids() {
match def_id.loc(self.db) {
DefLoc::Module(module_id) => {
if let Some(res) = self.resolve_member_in_module(module_id, field) {
return Some(res);
}
}
DefLoc::Instance(instance) => {
let target_module =
self.instance_target_module_id(instance.module_id, instance.value)?;
if let Some(res) = self.resolve_member_in_module(target_module, field) {
return Some(res);
}
}
DefLoc::Block(block_id) => {
if let Some(res) = self.resolve_member_in_block(block_id, field) {
return Some(res);
}
}
DefLoc::GenerateBlock(generate_block_id) => {
if let Some(res) =
self.resolve_member_in_generate_block(generate_block_id, field)
{
return Some(res);
}
}
_ => {}
let Some(scope_id) = descend_scope(self.db, *def_id) else {
continue;
};
if let Some(res) = name_scope(self.db, scope_id)
.lookup(NameContext::Value, field)
.and_then(PathResolution::from_def_ids)
{
return Some(res);
}
}
None
}

fn resolve_member_in_module(
&mut self,
module_id: ModuleId,
field: &Ident,
) -> Option<PathResolution> {
name_scope(self.db, module_id.into())
.lookup(NameContext::Value, field)
.and_then(PathResolution::from_def_ids)
}

fn resolve_member_in_block(
&mut self,
block_id: BlockId,
field: &Ident,
fn resolve_expr_path(
&self,
cont_id: ScopeId,
expr_id: ExprId,
ctx: NameContext,
) -> Option<PathResolution> {
name_scope(self.db, block_id.into())
.lookup(NameContext::Value, field)
.and_then(PathResolution::from_def_ids)
let path = self.expr_path(cont_id, expr_id)?;
resolve_path(self.db, cont_id, &path, ctx)
}

fn resolve_member_in_generate_block(
&mut self,
generate_block_id: GenerateBlockId,
field: &Ident,
) -> Option<PathResolution> {
name_scope(self.db, generate_block_id.into())
.lookup(NameContext::Value, field)
.and_then(PathResolution::from_def_ids)
fn expr_path(&self, cont_id: ScopeId, expr_id: ExprId) -> Option<Vec<Ident>> {
match self.expr_in_container(cont_id, expr_id)? {
Expr::Ident(ident) => Some(vec![ident]),
Expr::Field { receiver, field } => {
let mut path = self.expr_path(cont_id, receiver)?;
path.push(field?);
Some(path)
}
Expr::ElementSelect { receiver, .. } => self.expr_path(cont_id, receiver),
_ => None,
}
}

fn instance_target_module_id(
&mut self,
module_id: ModuleId,
instance_id: InstanceId,
) -> Option<ModuleId> {
let module = self.db.module(module_id);
let instance = module.get(instance_id);
let instantiation = module.get(instance.parent);
let module_name = instantiation.module_name.as_ref()?;
self.db.unit_scope().module_ids(self.db, module_name).unique()
fn expr_in_container(&self, cont_id: ScopeId, expr_id: ExprId) -> Option<Expr> {
match cont_id {
ScopeId::File(file_id) => Some(self.db.hir_file(file_id).get(expr_id).clone()),
ScopeId::Module(module_id) => Some(self.db.module(module_id).get(expr_id).clone()),
ScopeId::Block(block_id) => Some(self.db.block(block_id).get(expr_id).clone()),
ScopeId::GenerateBlock(generate_block_id) => {
Some(self.db.generate_block(generate_block_id).get(expr_id).clone())
}
ScopeId::Subroutine(subroutine_id) => {
Some(self.db.subroutine(subroutine_id.as_in_container()).get(expr_id).clone())
}
}
}
}
Loading
Loading