From a4ffef468ad913859704266e4fe67dfd9315fa0d Mon Sep 17 00:00:00 2001 From: Hong Jiarong Date: Fri, 24 Jul 2026 23:23:10 +0800 Subject: [PATCH 1/2] refactor(hir): separate scopes from arena owners --- crates/hir/src/container.rs | 245 +++++++++------- crates/hir/src/db.rs | 16 +- crates/hir/src/def_id.rs | 323 +++++++--------------- crates/hir/src/display.rs | 14 +- crates/hir/src/hir_def/aggregate.rs | 4 +- crates/hir/src/hir_def/block.rs | 8 +- crates/hir/src/hir_def/file.rs | 9 +- crates/hir/src/hir_def/module.rs | 9 +- crates/hir/src/hir_def/module/generate.rs | 13 +- crates/hir/src/hir_def/proc.rs | 4 +- crates/hir/src/hir_def/stmt.rs | 4 +- crates/hir/src/hir_def/subroutine.rs | 42 +-- crates/hir/src/hir_def/typedef.rs | 4 +- crates/hir/src/scope.rs | 178 ++++++------ crates/hir/src/semantics.rs | 8 +- crates/hir/src/semantics/hir_to_def.rs | 40 ++- crates/hir/src/semantics/pathres.rs | 19 +- crates/hir/src/semantics/resolver.rs | 11 +- crates/hir/src/semantics/source_to_def.rs | 52 ++-- crates/hir/src/symbol.rs | 45 +-- crates/hir/src/type_infer.rs | 118 ++++---- crates/ide/src/completion/engine/expr.rs | 18 +- crates/ide/src/hover.rs | 2 +- crates/ide/src/module_resolution.rs | 6 +- crates/ide/src/navigation_target.rs | 27 +- crates/ide/src/references/search.rs | 6 +- crates/ide/src/render.rs | 51 ++-- crates/ide/src/semantic_tokens.rs | 12 +- 28 files changed, 596 insertions(+), 692 deletions(-) diff --git a/crates/hir/src/container.rs b/crates/hir/src/container.rs index 8743a3ab..21296bfc 100644 --- a/crates/hir/src/container.rs +++ b/crates/hir/src/container.rs @@ -1,7 +1,7 @@ use proc_macro_utils::impl_container; use smol_str::SmolStr; use triomphe::Arc; -use utils::define_enum_deriving_from; +use utils::{define_enum_deriving_from, get::GetRef}; use vfs::FileId; use crate::{ @@ -21,7 +21,7 @@ use crate::{ }, file::{FileSourceMap, HirFile}, module::{ - Module, ModuleId, ModuleSourceMap, + Module, ModuleId, ModuleKind, ModuleSourceMap, clocking::ClockingBlockId, generate::{GenerateBlock, GenerateBlockId, GenerateBlockSourceMap}, }, @@ -47,6 +47,37 @@ define_enum_deriving_from! { } } +define_enum_deriving_from! { + #[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Clone, Copy, Hash)] + pub enum ArenaOwnerId { + File(HirFileId), + Module(ModuleId), + GenerateBlock(GenerateBlockId), + Block(BlockId), + Subroutine(SubroutineScope), + } +} + +#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Clone, Copy, Hash)] +pub struct InScope { + pub value: T, + pub scope_id: ScopeId, +} + +impl InScope { + pub fn new(scope_id: ScopeId, value: T) -> Self { + Self { value, scope_id } + } + + pub fn with_value(self, value: U) -> InScope { + InScope::new(self.scope_id, value) + } + + pub fn map(self, f: impl FnOnce(T) -> U) -> InScope { + InScope::new(self.scope_id, f(self.value)) + } +} + #[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Clone, Copy, Hash)] pub struct InFileOrModule { pub value: T, @@ -67,10 +98,6 @@ impl InFileOrModule { pub fn parent_scope(&self) -> ScopeId { self.cont_id.into() } - - pub fn as_in_container(self) -> InContainer { - InContainer::new(self.cont_id.into(), self.value) - } } impl FileOrModule { @@ -82,7 +109,7 @@ impl FileOrModule { } } -impl From for ScopeId { +impl From for ArenaOwnerId { fn from(cont_id: FileOrModule) -> Self { match cont_id { FileOrModule::File(file_id) => file_id.into(), @@ -91,34 +118,9 @@ impl From for ScopeId { } } -impl TryFrom for FileOrModule { - type Error = (); - - fn try_from(cont_id: ScopeId) -> Result { - match cont_id { - ScopeId::File(file_id) => Ok(Self::File(file_id)), - ScopeId::Module(module_id) => Ok(Self::Module(module_id)), - ScopeId::GenerateBlock(_) - | ScopeId::Block(_) - | ScopeId::Subroutine(_) - | ScopeId::ClockingBlock(_) - | ScopeId::Checker(_) - | ScopeId::Covergroup(_) => Err(()), - } - } -} - -impl TryFrom> for InFileOrModule { - type Error = (); - - fn try_from(item: InContainer) -> Result { - Ok(Self::new(FileOrModule::try_from(item.cont_id)?, item.value)) - } -} - -impl From> for InContainer { - fn from(item: InFileOrModule) -> Self { - item.as_in_container() +impl From for ScopeId { + fn from(cont_id: FileOrModule) -> Self { + ArenaOwnerId::from(cont_id).into() } } @@ -144,10 +146,6 @@ impl SubroutineScope { self.cont_id.into() } - pub fn as_in_container(self) -> InContainer { - InContainer::new(self.parent_scope(), self.value) - } - pub fn file_id(self, db: &dyn InternDb) -> FileId { match self.cont_id { SubroutineParent::File(file_id) => file_id.file_id(), @@ -157,7 +155,7 @@ impl SubroutineScope { } } -impl From for ScopeId { +impl From for ArenaOwnerId { fn from(cont_id: SubroutineParent) -> Self { match cont_id { SubroutineParent::File(file_id) => file_id.into(), @@ -167,45 +165,20 @@ impl From for ScopeId { } } -impl TryFrom for SubroutineParent { - type Error = (); - - fn try_from(cont_id: ScopeId) -> Result { - match cont_id { - ScopeId::File(file_id) => Ok(Self::File(file_id)), - ScopeId::Module(module_id) => Ok(Self::Module(module_id)), - ScopeId::GenerateBlock(generate_block_id) => Ok(Self::GenerateBlock(generate_block_id)), - ScopeId::Block(_) - | ScopeId::Subroutine(_) - | ScopeId::ClockingBlock(_) - | ScopeId::Checker(_) - | ScopeId::Covergroup(_) => Err(()), - } - } -} - -impl From> for SubroutineScope { - fn from(subroutine: InContainer) -> Self { - let parent = SubroutineParent::try_from(subroutine.cont_id) - .expect("subroutines are lowered only in file, module, or generate-block scopes"); - Self::new(parent, subroutine.value) - } -} - -impl From> for ScopeId { - fn from(subroutine: InContainer) -> Self { - ScopeId::Subroutine(subroutine.into()) +impl From for ScopeId { + fn from(cont_id: SubroutineParent) -> Self { + ArenaOwnerId::from(cont_id).into() } } #[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Clone, Copy, Hash)] pub struct InContainer { pub value: T, - pub cont_id: ScopeId, + pub cont_id: ArenaOwnerId, } impl InContainer { - pub fn new(cont_id: ScopeId, value: T) -> InContainer { + pub fn new(cont_id: ArenaOwnerId, value: T) -> InContainer { InContainer { value, cont_id } } @@ -221,11 +194,11 @@ impl InContainer { #[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Clone, Copy, Hash)] pub struct InSubroutine { pub value: T, - pub subroutine: InContainer, + pub subroutine: SubroutineScope, } impl InSubroutine { - pub fn new(subroutine: InContainer, value: T) -> Self { + pub fn new(subroutine: SubroutineScope, value: T) -> Self { Self { value, subroutine } } @@ -279,11 +252,30 @@ define_container_id! { InBlock[block_id: BlockId], } +impl From for ScopeId { + fn from(owner_id: ArenaOwnerId) -> Self { + match owner_id { + ArenaOwnerId::File(file_id) => file_id.into(), + ArenaOwnerId::Module(module_id) => module_id.into(), + ArenaOwnerId::GenerateBlock(generate_block_id) => generate_block_id.into(), + ArenaOwnerId::Block(block_id) => block_id.into(), + ArenaOwnerId::Subroutine(subroutine) => subroutine.into(), + } + } +} + impl ScopeId { - pub fn kind(self) -> ScopeKind { + pub fn kind(self, db: &dyn HirDb) -> ScopeKind { match self { ScopeId::File(_) => ScopeKind::File, - ScopeId::Module(_) => ScopeKind::Module, + ScopeId::Module(module_id) => { + match db.hir_file(module_id.file_id).get(module_id.value).kind { + ModuleKind::Module => ScopeKind::Module, + ModuleKind::Interface => ScopeKind::Interface, + ModuleKind::Program => ScopeKind::Program, + ModuleKind::Package => ScopeKind::Package, + } + } ScopeId::GenerateBlock(_) => ScopeKind::GenerateBlock, ScopeId::Block(_) => ScopeKind::Block, ScopeId::Subroutine(_) => ScopeKind::Subroutine, @@ -293,6 +285,46 @@ impl ScopeId { } } + pub fn name(self, db: &dyn HirDb) -> Option { + match self { + ScopeId::File(_) => None, + ScopeId::Module(module_id) => db.module(module_id).name.clone(), + ScopeId::GenerateBlock(generate_block_id) => { + db.generate_block(generate_block_id).name.clone() + } + ScopeId::Block(block_id) => db.block(block_id).name.clone(), + ScopeId::Subroutine(subroutine) => db.subroutine(subroutine).name.clone(), + ScopeId::ClockingBlock(clocking_block) => { + db.module(clocking_block.module_id).get(clocking_block.value).name.clone() + } + ScopeId::Checker(checker) => match checker.cont_id { + FileOrModule::File(file_id) => db.hir_file(file_id).get(checker.value).name.clone(), + FileOrModule::Module(module_id) => { + db.module(module_id).get(checker.value).name.clone() + } + }, + ScopeId::Covergroup(covergroup) => match covergroup.cont_id { + FileOrModule::File(file_id) => { + db.hir_file(file_id).get(covergroup.value).name.clone() + } + FileOrModule::Module(module_id) => { + db.module(module_id).get(covergroup.value).name.clone() + } + }, + } + } + + pub fn arena_owner(self) -> Option { + match self { + ScopeId::File(file_id) => Some(file_id.into()), + ScopeId::Module(module_id) => Some(module_id.into()), + ScopeId::GenerateBlock(generate_block_id) => Some(generate_block_id.into()), + ScopeId::Block(block_id) => Some(block_id.into()), + ScopeId::Subroutine(subroutine) => Some(subroutine.into()), + ScopeId::ClockingBlock(_) | ScopeId::Checker(_) | ScopeId::Covergroup(_) => None, + } + } + pub fn file_id(self, db: &dyn InternDb) -> FileId { match self { ScopeId::File(file_id) => file_id.file_id(), @@ -305,45 +337,46 @@ impl ScopeId { ScopeId::Covergroup(covergroup) => covergroup.cont_id.file_id(), } } +} - pub fn to_container(self, db: &dyn HirDb) -> Container { +/// Access to generic HIR arenas. +/// +/// Name-resolution-only scopes cannot access arena data: +/// +/// ```compile_fail +/// use hir::{container::ScopeId, db::HirDb}; +/// +/// fn data_for_any_scope(scope: ScopeId, db: &dyn HirDb) { +/// let _ = scope.data(db); +/// } +/// ``` +impl ArenaOwnerId { + pub fn file_id(self, db: &dyn InternDb) -> FileId { + ScopeId::from(self).file_id(db) + } + + pub fn data(self, db: &dyn HirDb) -> Container { match self { - ScopeId::File(file_id) => file_id.to_container(db).into(), - ScopeId::Module(module_id) => module_id.to_container(db).into(), - ScopeId::GenerateBlock(generate_block_id) => generate_block_id.to_container(db).into(), - ScopeId::Block(block_id) => block_id.to_container(db).into(), - ScopeId::Subroutine(subroutine) => db.subroutine(subroutine.as_in_container()).into(), - ScopeId::ClockingBlock(_) => { - panic!("clocking block scopes do not expose a generic HIR container") - } - ScopeId::Checker(_) => { - panic!("checker scopes do not expose a generic HIR container") - } - ScopeId::Covergroup(_) => { - panic!("covergroup scopes do not expose a generic HIR container") + ArenaOwnerId::File(file_id) => file_id.to_container(db).into(), + ArenaOwnerId::Module(module_id) => module_id.to_container(db).into(), + ArenaOwnerId::GenerateBlock(generate_block_id) => { + generate_block_id.to_container(db).into() } + ArenaOwnerId::Block(block_id) => block_id.to_container(db).into(), + ArenaOwnerId::Subroutine(subroutine) => db.subroutine(subroutine).into(), } } - pub fn to_container_src_map(self, db: &dyn HirDb) -> ContainerSrcMap { + pub fn source_map(self, db: &dyn HirDb) -> ContainerSrcMap { match self { - ScopeId::File(file_id) => file_id.to_container_src_map(db).into(), - ScopeId::Module(module_id) => module_id.to_container_src_map(db).into(), - ScopeId::GenerateBlock(generate_block_id) => { + ArenaOwnerId::File(file_id) => file_id.to_container_src_map(db).into(), + ArenaOwnerId::Module(module_id) => module_id.to_container_src_map(db).into(), + ArenaOwnerId::GenerateBlock(generate_block_id) => { generate_block_id.to_container_src_map(db).into() } - ScopeId::Block(block_id) => block_id.to_container_src_map(db).into(), - ScopeId::Subroutine(subroutine) => { - db.subroutine_with_source_map(subroutine.as_in_container()).1.into() - } - ScopeId::ClockingBlock(_) => { - panic!("clocking block scopes do not expose a generic source map") - } - ScopeId::Checker(_) => { - panic!("checker scopes do not expose a generic source map") - } - ScopeId::Covergroup(_) => { - panic!("covergroup scopes do not expose a generic source map") + ArenaOwnerId::Block(block_id) => block_id.to_container_src_map(db).into(), + ArenaOwnerId::Subroutine(subroutine) => { + db.subroutine_with_source_map(subroutine).1.into() } } } @@ -490,9 +523,9 @@ impl Iterator for ScopeParent<'_> { ScopeId::File(_) => None, ScopeId::Module(module_id) => Some(module_id.file_id.into()), ScopeId::GenerateBlock(generate_block_id) => { - Some(generate_block_id.lookup(self.db).cont_id) + Some(generate_block_id.lookup(self.db).cont_id.into()) } - ScopeId::Block(block_id) => Some(block_id.lookup(self.db).cont_id), + ScopeId::Block(block_id) => Some(block_id.lookup(self.db).cont_id.into()), ScopeId::Subroutine(subroutine) => Some(subroutine.parent_scope()), ScopeId::ClockingBlock(clocking_block) => Some(clocking_block.module_id.into()), ScopeId::Checker(checker) => Some(checker.parent_scope()), diff --git a/crates/hir/src/db.rs b/crates/hir/src/db.rs index 398ed9a4..4f4d1817 100644 --- a/crates/hir/src/db.rs +++ b/crates/hir/src/db.rs @@ -3,7 +3,7 @@ use triomphe::Arc; use crate::{ base_db::{salsa, source_db::SourceRootDb}, - container::{InContainer, InModule, InSubroutine}, + container::{InContainer, InFileOrModule, InModule, InSubroutine, SubroutineScope}, def_id::{DefId, Definition}, file::HirFileId, hir_def::{ @@ -26,7 +26,7 @@ use crate::{ self, GenerateBlock, GenerateBlockId, GenerateBlockLoc, GenerateBlockSourceMap, }, }, - subroutine::{self, LocalSubroutineId, Subroutine, SubroutinePortId, SubroutineSourceMap}, + subroutine::{self, Subroutine, SubroutinePortId, SubroutineSourceMap}, typedef::TypedefId, }, impl_intern_key, impl_intern_lookup, @@ -102,10 +102,10 @@ pub trait HirDb: InternDb { #[salsa::invoke(subroutine::subroutine_with_source_map_query)] fn subroutine_with_source_map( &self, - subroutine: InContainer, + subroutine: SubroutineScope, ) -> (Arc, Arc); - fn subroutine(&self, subroutine_id: InContainer) -> Arc; + fn subroutine(&self, subroutine_id: SubroutineScope) -> Arc; #[salsa::invoke(generate::generate_block_with_source_map_query)] fn generate_block_with_source_map( @@ -128,10 +128,10 @@ pub trait HirDb: InternDb { fn clocking_block_scope(&self, clocking_block_id: InModule) -> Arc; #[salsa::invoke(NameScope::checker_scope_query)] - fn checker_scope(&self, checker_id: InContainer) -> Arc; + fn checker_scope(&self, checker_id: InFileOrModule) -> Arc; #[salsa::invoke(NameScope::covergroup_scope_query)] - fn covergroup_scope(&self, covergroup_id: InContainer) -> Arc; + fn covergroup_scope(&self, covergroup_id: InFileOrModule) -> Arc; #[salsa::invoke(NameScope::generate_block_scope_query)] fn generate_block_scope(&self, generate_block_id: GenerateBlockId) -> Arc; @@ -140,7 +140,7 @@ pub trait HirDb: InternDb { fn block_scope(&self, block_id: BlockId) -> Arc; #[salsa::invoke(NameScope::subroutine_scope_query)] - fn subroutine_scope(&self, subroutine_id: InContainer) -> Arc; + fn subroutine_scope(&self, subroutine_id: SubroutineScope) -> Arc; #[salsa::invoke(NameScope::package_export_signature_query)] fn package_export_signature(&self, package_id: PackageId) -> Arc; @@ -183,7 +183,7 @@ fn block(db: &dyn HirDb, block_id: BlockId) -> Arc { db.block_with_source_map(block_id).0 } -fn subroutine(db: &dyn HirDb, subroutine_id: InContainer) -> Arc { +fn subroutine(db: &dyn HirDb, subroutine_id: SubroutineScope) -> Arc { db.subroutine_with_source_map(subroutine_id).0 } diff --git a/crates/hir/src/def_id.rs b/crates/hir/src/def_id.rs index 12ef4bb1..48d4cc16 100644 --- a/crates/hir/src/def_id.rs +++ b/crates/hir/src/def_id.rs @@ -11,7 +11,10 @@ use utils::{ use crate::{ base_db::{intern::Lookup, salsa}, - container::{FileOrModule, InContainer, InFile, InModule, InSubroutine, ScopeId}, + container::{ + ArenaOwnerId, FileOrModule, InContainer, InFile, InFileOrModule, InModule, InScope, + InSubroutine, ScopeId, SubroutineParent, SubroutineScope, + }, db::HirDb, file::HirFileId, hir_def::{ @@ -21,43 +24,35 @@ use crate::{ declaration::Declaration, expr::declarator::DeclaratorParent, module::{ModuleKind, clocking::ClockingSignal, generate::GenerateBlockLoc}, - subroutine::{LocalSubroutineId, SubroutineSrc}, + subroutine::SubroutineSrc, }, source_map::{IsNamedSrc, IsSrc, ToAstNode}, symbol::{DefKind, DefOrigin, DefOriginLoc}, }; -fn subroutine_src( - db: &dyn HirDb, - subroutine: InContainer, -) -> Option> { +fn subroutine_src(db: &dyn HirDb, subroutine: SubroutineScope) -> Option> { match subroutine.cont_id { - ScopeId::File(file_id) => { + SubroutineParent::File(file_id) => { let (_, source_map) = db.hir_file_with_source_map(file_id); Some(InFile::new(file_id, source_map.get(subroutine.value)?)) } - ScopeId::Module(module_id) => { + SubroutineParent::Module(module_id) => { let (_, source_map) = db.module_with_source_map(module_id); Some(InFile::new(module_id.file_id, source_map.get(subroutine.value)?)) } - ScopeId::GenerateBlock(generate_block_id) => { + SubroutineParent::GenerateBlock(generate_block_id) => { let (_, source_map) = db.generate_block_with_source_map(generate_block_id); let file_id = generate_block_id.lookup(db).src.file_id; Some(InFile::new(file_id, source_map.get(subroutine.value)?)) } - ScopeId::Block(_) - | ScopeId::Subroutine(_) - | ScopeId::ClockingBlock(_) - | ScopeId::Checker(_) - | ScopeId::Covergroup(_) => None, } } fn clocking_signal_of( db: &dyn HirDb, - signal: InContainer, + signal: InScope, ) -> Option<(InModule, vfs::FileId)> { - let ScopeId::ClockingBlock(clocking_block) = signal.cont_id else { + let ScopeId::ClockingBlock(clocking_block) = signal.scope_id else { return None; }; let module = db.module(clocking_block.module_id); @@ -68,76 +63,69 @@ fn clocking_signal_of( fn checker_of( db: &dyn HirDb, - checker: InContainer, + checker: InFileOrModule, ) -> Option<(CheckerDef, HirFileId)> { match checker.cont_id { - ScopeId::File(file_id) => Some((db.hir_file(file_id).get(checker.value).clone(), file_id)), - ScopeId::Module(module_id) => { + FileOrModule::File(file_id) => { + Some((db.hir_file(file_id).get(checker.value).clone(), file_id)) + } + FileOrModule::Module(module_id) => { Some((db.module(module_id).get(checker.value).clone(), module_id.file_id)) } - ScopeId::GenerateBlock(_) - | ScopeId::Block(_) - | ScopeId::Subroutine(_) - | ScopeId::ClockingBlock(_) - | ScopeId::Checker(_) - | ScopeId::Covergroup(_) => None, } } fn checker_port_of( db: &dyn HirDb, - port: InContainer, + port: InScope, ) -> Option<(CheckerPort, HirFileId)> { - let ScopeId::Checker(checker) = port.cont_id else { + let ScopeId::Checker(checker) = port.scope_id else { return None; }; - let (checker, file_id) = checker_of(db, checker.as_in_container())?; + let (checker, file_id) = checker_of(db, checker)?; let port = checker.ports.get(port.value.0 as usize)?.clone(); Some((port, file_id)) } +fn file_or_module_storage(scope_id: ScopeId) -> Option { + match scope_id { + ScopeId::Covergroup(covergroup) => Some(covergroup.cont_id), + ScopeId::File(file_id) => Some(FileOrModule::File(file_id)), + ScopeId::Module(module_id) => Some(FileOrModule::Module(module_id)), + ScopeId::GenerateBlock(_) + | ScopeId::Block(_) + | ScopeId::Subroutine(_) + | ScopeId::ClockingBlock(_) + | ScopeId::Checker(_) => None, + } +} + fn coverpoint_of( db: &dyn HirDb, - coverpoint: InContainer, + coverpoint: InScope, ) -> Option<(CoverpointDef, HirFileId)> { - let cont_id = match coverpoint.cont_id { - ScopeId::Covergroup(covergroup) => covergroup.parent_scope(), - cont_id => cont_id, - }; + let cont_id = file_or_module_storage(coverpoint.scope_id)?; match cont_id { - ScopeId::File(file_id) => { + FileOrModule::File(file_id) => { Some((db.hir_file(file_id).get(coverpoint.value).clone(), file_id)) } - ScopeId::Module(module_id) => { + FileOrModule::Module(module_id) => { Some((db.module(module_id).get(coverpoint.value).clone(), module_id.file_id)) } - ScopeId::GenerateBlock(_) - | ScopeId::Block(_) - | ScopeId::Subroutine(_) - | ScopeId::ClockingBlock(_) - | ScopeId::Checker(_) - | ScopeId::Covergroup(_) => None, } } -fn cross_of(db: &dyn HirDb, cross: InContainer) -> Option<(CrossDef, HirFileId)> { - let cont_id = match cross.cont_id { - ScopeId::Covergroup(covergroup) => covergroup.parent_scope(), - cont_id => cont_id, - }; +fn cross_of(db: &dyn HirDb, cross: InScope) -> Option<(CrossDef, HirFileId)> { + let cont_id = file_or_module_storage(cross.scope_id)?; match cont_id { - ScopeId::File(file_id) => Some((db.hir_file(file_id).get(cross.value).clone(), file_id)), - ScopeId::Module(module_id) => { + FileOrModule::File(file_id) => { + Some((db.hir_file(file_id).get(cross.value).clone(), file_id)) + } + FileOrModule::Module(module_id) => { Some((db.module(module_id).get(cross.value).clone(), module_id.file_id)) } - ScopeId::GenerateBlock(_) - | ScopeId::Block(_) - | ScopeId::Subroutine(_) - | ScopeId::ClockingBlock(_) - | ScopeId::Checker(_) - | ScopeId::Covergroup(_) => None, } } @@ -149,25 +137,27 @@ impl DefOrigin { DefOriginLoc::Config(InFile { file_id, .. }) => file_id.into(), DefOriginLoc::Library(InFile { file_id, .. }) => file_id.into(), DefOriginLoc::Udp(InFile { file_id, .. }) => file_id.into(), - DefOriginLoc::Block(block_id) => block_id.lookup(db).cont_id, - DefOriginLoc::GenerateBlock(generate_block_id) => generate_block_id.lookup(db).cont_id, - DefOriginLoc::Subroutine(subroutine_id) => subroutine_id.cont_id, + DefOriginLoc::Block(block_id) => block_id.lookup(db).cont_id.into(), + DefOriginLoc::GenerateBlock(generate_block_id) => { + generate_block_id.lookup(db).cont_id.into() + } + DefOriginLoc::Subroutine(subroutine_id) => subroutine_id.cont_id.into(), DefOriginLoc::SubroutinePort(InSubroutine { subroutine, .. }) => { - ScopeId::Subroutine(subroutine.into()) + ScopeId::Subroutine(subroutine) } DefOriginLoc::NonAnsiPort(InModule { module_id, .. }) => module_id.into(), - DefOriginLoc::Decl(InContainer { cont_id, .. }) => cont_id, - DefOriginLoc::Typedef(InContainer { cont_id, .. }) => cont_id, + DefOriginLoc::Decl(InContainer { cont_id, .. }) => cont_id.into(), + DefOriginLoc::Typedef(InContainer { cont_id, .. }) => cont_id.into(), DefOriginLoc::Instance(InModule { module_id, .. }) => module_id.into(), DefOriginLoc::Modport(InModule { module_id, .. }) => module_id.into(), DefOriginLoc::ClockingBlock(InModule { module_id, .. }) => module_id.into(), - DefOriginLoc::ClockingSignal(InContainer { cont_id, .. }) => cont_id, - DefOriginLoc::Checker(InContainer { cont_id, .. }) => cont_id, - DefOriginLoc::CheckerPort(InContainer { cont_id, .. }) => cont_id, - DefOriginLoc::Covergroup(InContainer { cont_id, .. }) => cont_id, - DefOriginLoc::Coverpoint(InContainer { cont_id, .. }) => cont_id, - DefOriginLoc::Cross(InContainer { cont_id, .. }) => cont_id, - DefOriginLoc::Stmt(InContainer { cont_id, .. }) => cont_id, + DefOriginLoc::ClockingSignal(InScope { scope_id, .. }) => scope_id, + DefOriginLoc::Checker(InFileOrModule { cont_id, .. }) => cont_id.into(), + DefOriginLoc::CheckerPort(InScope { scope_id, .. }) => scope_id, + DefOriginLoc::Covergroup(InFileOrModule { cont_id, .. }) => cont_id.into(), + DefOriginLoc::Coverpoint(InScope { scope_id, .. }) => scope_id, + DefOriginLoc::Cross(InScope { scope_id, .. }) => scope_id, + DefOriginLoc::Stmt(InContainer { cont_id, .. }) => cont_id.into(), } } @@ -191,7 +181,7 @@ impl DefOrigin { DefOriginLoc::SubroutinePort(_) => DefKind::SubroutinePort, DefOriginLoc::NonAnsiPort(_) => DefKind::NonAnsiPort, DefOriginLoc::Decl(InContainer { value, cont_id }) => { - let container = cont_id.to_container(db); + let container = cont_id.data(db); let decl = container.get(value); match decl.parent { DeclaratorParent::PortDeclId(_) => DefKind::Port, @@ -237,8 +227,8 @@ impl DefOrigin { } DefOriginLoc::Block(block_id) => { let BlockLoc { cont_id, src: InFile { value, file_id: _ } } = block_id.lookup(db); - let cont = cont_id.to_container(db); - value.hir(&cont, &cont_id.to_container_src_map(db))?.name.clone() + let cont = cont_id.data(db); + value.hir(&cont, &cont_id.source_map(db))?.name.clone() } DefOriginLoc::GenerateBlock(generate_block_id) => { db.generate_block(generate_block_id).name.clone() @@ -251,10 +241,10 @@ impl DefOrigin { module_id.to_container(db).get(value).label.clone() } DefOriginLoc::Decl(InContainer { value, cont_id }) => { - cont_id.to_container(db).get(value).name.clone() + cont_id.data(db).get(value).name.clone() } DefOriginLoc::Typedef(InContainer { value, cont_id }) => { - cont_id.to_container(db).get(value).name.clone() + cont_id.data(db).get(value).name.clone() } DefOriginLoc::Instance(InModule { value, module_id }) => { module_id.to_container(db).get(value).name.clone() @@ -268,33 +258,25 @@ impl DefOrigin { DefOriginLoc::ClockingSignal(signal) => { clocking_signal_of(db, signal).map(|(signal, _)| signal.value.name) } - DefOriginLoc::Checker(InContainer { value, cont_id }) => match cont_id { - ScopeId::File(file_id) => file_id.to_container(db).get(value).name.clone(), - ScopeId::Module(module_id) => module_id.to_container(db).get(value).name.clone(), - ScopeId::GenerateBlock(_) - | ScopeId::Block(_) - | ScopeId::Subroutine(_) - | ScopeId::ClockingBlock(_) - | ScopeId::Checker(_) - | ScopeId::Covergroup(_) => None, + DefOriginLoc::Checker(InFileOrModule { value, cont_id }) => match cont_id { + FileOrModule::File(file_id) => file_id.to_container(db).get(value).name.clone(), + FileOrModule::Module(module_id) => { + module_id.to_container(db).get(value).name.clone() + } }, DefOriginLoc::CheckerPort(port) => checker_port_of(db, port).map(|(port, _)| port.name), - DefOriginLoc::Covergroup(InContainer { value, cont_id }) => match cont_id { - ScopeId::File(file_id) => file_id.to_container(db).get(value).name.clone(), - ScopeId::Module(module_id) => module_id.to_container(db).get(value).name.clone(), - ScopeId::GenerateBlock(_) - | ScopeId::Block(_) - | ScopeId::Subroutine(_) - | ScopeId::ClockingBlock(_) - | ScopeId::Checker(_) - | ScopeId::Covergroup(_) => None, + DefOriginLoc::Covergroup(InFileOrModule { value, cont_id }) => match cont_id { + FileOrModule::File(file_id) => file_id.to_container(db).get(value).name.clone(), + FileOrModule::Module(module_id) => { + module_id.to_container(db).get(value).name.clone() + } }, DefOriginLoc::Coverpoint(coverpoint) => { coverpoint_of(db, coverpoint).and_then(|(coverpoint, _)| coverpoint.name) } DefOriginLoc::Cross(cross) => cross_of(db, cross).and_then(|(cross, _)| cross.name), DefOriginLoc::Stmt(InContainer { value, cont_id }) => { - cont_id.to_container(db).get(value).label.clone() + cont_id.data(db).get(value).label.clone() } } } @@ -354,11 +336,11 @@ impl DefOrigin { Some(InFile::new(module_id.file_id, range)) } DefOriginLoc::Decl(InContainer { value, cont_id }) => { - let range = cont_id.to_container_src_map(db).get(value)?.name_range()?; + let range = cont_id.source_map(db).get(value)?.name_range()?; Some(InFile::new(cont_id.file_id(db).into(), range)) } DefOriginLoc::Typedef(InContainer { value, cont_id }) => { - let range = cont_id.to_container_src_map(db).get(value)?.name_range()?; + let range = cont_id.source_map(db).get(value)?.name_range()?; Some(InFile::new(cont_id.file_id(db).into(), range)) } DefOriginLoc::Instance(InModule { value, module_id }) => { @@ -377,114 +359,68 @@ impl DefOrigin { let (signal, file_id) = clocking_signal_of(db, signal)?; Some(InFile::new(file_id.into(), signal.value.name_range?)) } - DefOriginLoc::Checker(InContainer { value, cont_id }) => match cont_id { - ScopeId::File(file_id) => { + DefOriginLoc::Checker(InFileOrModule { value, cont_id }) => match cont_id { + FileOrModule::File(file_id) => { let range = file_id.to_container_src_map(db).get(value)?.name_range()?; Some(InFile::new(file_id, range)) } - ScopeId::Module(module_id) => { + FileOrModule::Module(module_id) => { let range = module_id.to_container_src_map(db).get(value)?.name_range()?; Some(InFile::new(module_id.file_id, range)) } - ScopeId::GenerateBlock(_) - | ScopeId::Block(_) - | ScopeId::Subroutine(_) - | ScopeId::ClockingBlock(_) - | ScopeId::Checker(_) - | ScopeId::Covergroup(_) => None, }, DefOriginLoc::CheckerPort(port) => { let (port, file_id) = checker_port_of(db, port)?; Some(InFile::new(file_id, port.name_range?)) } - DefOriginLoc::Covergroup(InContainer { value, cont_id }) => match cont_id { - ScopeId::File(file_id) => { + DefOriginLoc::Covergroup(InFileOrModule { value, cont_id }) => match cont_id { + FileOrModule::File(file_id) => { let range = file_id.to_container_src_map(db).get(value)?.name_range()?; Some(InFile::new(file_id, range)) } - ScopeId::Module(module_id) => { + FileOrModule::Module(module_id) => { let range = module_id.to_container_src_map(db).get(value)?.name_range()?; Some(InFile::new(module_id.file_id, range)) } - ScopeId::GenerateBlock(_) - | ScopeId::Block(_) - | ScopeId::Subroutine(_) - | ScopeId::ClockingBlock(_) - | ScopeId::Checker(_) - | ScopeId::Covergroup(_) => None, }, DefOriginLoc::Coverpoint(coverpoint) => { let (_, file_id) = coverpoint_of(db, coverpoint)?; - match coverpoint.cont_id { - ScopeId::Covergroup(covergroup) => match covergroup.cont_id { - FileOrModule::File(storage_file) => { - let range = storage_file - .to_container_src_map(db) - .get(coverpoint.value)? - .name_range()?; - Some(InFile::new(file_id, range)) - } - FileOrModule::Module(storage_module) => { - let range = storage_module - .to_container_src_map(db) - .get(coverpoint.value)? - .name_range()?; - Some(InFile::new(file_id, range)) - } - }, - ScopeId::File(storage_file) => { + match file_or_module_storage(coverpoint.scope_id)? { + FileOrModule::File(storage_file) => { let range = storage_file .to_container_src_map(db) .get(coverpoint.value)? .name_range()?; Some(InFile::new(file_id, range)) } - ScopeId::Module(storage_module) => { + FileOrModule::Module(storage_module) => { let range = storage_module .to_container_src_map(db) .get(coverpoint.value)? .name_range()?; Some(InFile::new(file_id, range)) } - _ => None, } } DefOriginLoc::Cross(cross) => { let (_, file_id) = cross_of(db, cross)?; - match cross.cont_id { - ScopeId::Covergroup(covergroup) => match covergroup.cont_id { - FileOrModule::File(storage_file) => { - let range = storage_file - .to_container_src_map(db) - .get(cross.value)? - .name_range()?; - Some(InFile::new(file_id, range)) - } - FileOrModule::Module(storage_module) => { - let range = storage_module - .to_container_src_map(db) - .get(cross.value)? - .name_range()?; - Some(InFile::new(file_id, range)) - } - }, - ScopeId::File(storage_file) => { + match file_or_module_storage(cross.scope_id)? { + FileOrModule::File(storage_file) => { let range = storage_file.to_container_src_map(db).get(cross.value)?.name_range()?; Some(InFile::new(file_id, range)) } - ScopeId::Module(storage_module) => { + FileOrModule::Module(storage_module) => { let range = storage_module .to_container_src_map(db) .get(cross.value)? .name_range()?; Some(InFile::new(file_id, range)) } - _ => None, } } DefOriginLoc::Stmt(InContainer { value, cont_id }) => { - let range = cont_id.to_container_src_map(db).get(value)?.name_range()?; + let range = cont_id.source_map(db).get(value)?.name_range()?; Some(InFile::new(cont_id.file_id(db).into(), range)) } } @@ -542,11 +478,11 @@ impl DefOrigin { InFile::new(module_id.file_id, range) } DefOriginLoc::Decl(InContainer { value, cont_id }) => { - let range = cont_id.to_container_src_map(db).get(value)?.range(); + let range = cont_id.source_map(db).get(value)?.range(); InFile::new(cont_id.file_id(db).into(), range) } DefOriginLoc::Typedef(InContainer { value, cont_id }) => { - let range = cont_id.to_container_src_map(db).get(value)?.range(); + let range = cont_id.source_map(db).get(value)?.range(); InFile::new(cont_id.file_id(db).into(), range) } DefOriginLoc::Instance(InModule { value, module_id }) => { @@ -565,107 +501,61 @@ impl DefOrigin { let (signal, file_id) = clocking_signal_of(db, signal)?; InFile::new(file_id.into(), signal.value.name_range?) } - DefOriginLoc::Checker(InContainer { value, cont_id }) => match cont_id { - ScopeId::File(file_id) => { + DefOriginLoc::Checker(InFileOrModule { value, cont_id }) => match cont_id { + FileOrModule::File(file_id) => { let range = file_id.to_container_src_map(db).get(value)?.range(); InFile::new(file_id, range) } - ScopeId::Module(module_id) => { + FileOrModule::Module(module_id) => { let range = module_id.to_container_src_map(db).get(value)?.range(); InFile::new(module_id.file_id, range) } - ScopeId::GenerateBlock(_) - | ScopeId::Block(_) - | ScopeId::Subroutine(_) - | ScopeId::ClockingBlock(_) - | ScopeId::Checker(_) - | ScopeId::Covergroup(_) => { - return None; - } }, DefOriginLoc::CheckerPort(port) => { let (port, file_id) = checker_port_of(db, port)?; InFile::new(file_id, port.name_range?) } - DefOriginLoc::Covergroup(InContainer { value, cont_id }) => match cont_id { - ScopeId::File(file_id) => { + DefOriginLoc::Covergroup(InFileOrModule { value, cont_id }) => match cont_id { + FileOrModule::File(file_id) => { let range = file_id.to_container_src_map(db).get(value)?.range(); InFile::new(file_id, range) } - ScopeId::Module(module_id) => { + FileOrModule::Module(module_id) => { let range = module_id.to_container_src_map(db).get(value)?.range(); InFile::new(module_id.file_id, range) } - ScopeId::GenerateBlock(_) - | ScopeId::Block(_) - | ScopeId::Subroutine(_) - | ScopeId::ClockingBlock(_) - | ScopeId::Checker(_) - | ScopeId::Covergroup(_) => { - return None; - } }, DefOriginLoc::Coverpoint(coverpoint) => { let (_, file_id) = coverpoint_of(db, coverpoint)?; - match coverpoint.cont_id { - ScopeId::Covergroup(covergroup) => match covergroup.cont_id { - FileOrModule::File(storage_file) => { - let range = storage_file - .to_container_src_map(db) - .get(coverpoint.value)? - .range(); - InFile::new(file_id, range) - } - FileOrModule::Module(storage_module) => { - let range = storage_module - .to_container_src_map(db) - .get(coverpoint.value)? - .range(); - InFile::new(file_id, range) - } - }, - ScopeId::File(storage_file) => { + match file_or_module_storage(coverpoint.scope_id)? { + FileOrModule::File(storage_file) => { let range = storage_file.to_container_src_map(db).get(coverpoint.value)?.range(); InFile::new(file_id, range) } - ScopeId::Module(storage_module) => { + FileOrModule::Module(storage_module) => { let range = storage_module.to_container_src_map(db).get(coverpoint.value)?.range(); InFile::new(file_id, range) } - _ => return None, } } DefOriginLoc::Cross(cross) => { let (_, file_id) = cross_of(db, cross)?; - match cross.cont_id { - ScopeId::Covergroup(covergroup) => match covergroup.cont_id { - FileOrModule::File(storage_file) => { - let range = - storage_file.to_container_src_map(db).get(cross.value)?.range(); - InFile::new(file_id, range) - } - FileOrModule::Module(storage_module) => { - let range = - storage_module.to_container_src_map(db).get(cross.value)?.range(); - InFile::new(file_id, range) - } - }, - ScopeId::File(storage_file) => { + match file_or_module_storage(cross.scope_id)? { + FileOrModule::File(storage_file) => { let range = storage_file.to_container_src_map(db).get(cross.value)?.range(); InFile::new(file_id, range) } - ScopeId::Module(storage_module) => { + FileOrModule::Module(storage_module) => { let range = storage_module.to_container_src_map(db).get(cross.value)?.range(); InFile::new(file_id, range) } - _ => return None, } } DefOriginLoc::Stmt(InContainer { value, cont_id }) => { - let range = cont_id.to_container_src_map(db).get(value)?.range(); + let range = cont_id.source_map(db).get(value)?.range(); InFile::new(cont_id.file_id(db).into(), range) } }) @@ -798,7 +688,7 @@ fn non_ansi_port_for_origin( ) -> Option> { match origin.loc(db) { DefOriginLoc::NonAnsiPort(port_id) => Some(port_id), - DefOriginLoc::Decl(InContainer { value, cont_id: ScopeId::Module(module_id) }) => { + DefOriginLoc::Decl(InContainer { value, cont_id: ArenaOwnerId::Module(module_id) }) => { let role = non_ansi_port_origin_role(db, origin)?; let module = db.module(module_id); let name = module.get(value).name.as_ref()?; @@ -832,8 +722,5 @@ fn is_port_decl_origin(db: &dyn HirDb, origin: DefOrigin) -> bool { let DefOriginLoc::Decl(decl_id) = origin.loc(db) else { return false; }; - matches!( - decl_id.cont_id.to_container(db).get(decl_id.value).parent, - DeclaratorParent::PortDeclId(_) - ) + matches!(decl_id.cont_id.data(db).get(decl_id.value).parent, DeclaratorParent::PortDeclId(_)) } diff --git a/crates/hir/src/display.rs b/crates/hir/src/display.rs index 8aeac668..1ec9c2a3 100644 --- a/crates/hir/src/display.rs +++ b/crates/hir/src/display.rs @@ -116,7 +116,7 @@ impl HirDisplay for Ty { Ty::Event => f.write_str("event"), Ty::Chandle => f.write_str("chandle"), Ty::Alias { typedef, target } => { - let container = typedef.cont_id.to_container(f.db); + let container = typedef.cont_id.data(f.db); if let Some(name) = &container.get(typedef.value).name { f.write_str(name) } else { @@ -169,7 +169,7 @@ fn hir_fmt_def_backed_type( ) -> Result<(), HirDisplayError> { f.write_str(keyword)?; if let DefOriginLoc::Typedef(typedef) = def.primary_origin(f.db).loc(f.db) { - let container = typedef.cont_id.to_container(f.db); + let container = typedef.cont_id.data(f.db); if let Some(name) = &container.get(typedef.value).name { f.write_str(" ")?; f.write_str(name)?; @@ -191,7 +191,7 @@ fn hir_fmt_named_def_type( Ok(()) } -fn ty_expr_container(db: &dyn crate::db::HirDb, ty: &Ty) -> Option { +fn ty_expr_container(db: &dyn crate::db::HirDb, ty: &Ty) -> Option { match ty { Ty::Builtin(BuiltinTy::Data { container, .. }) => Some(*container), Ty::Struct(struct_ref) => Some(struct_ref.cont_id), @@ -303,7 +303,7 @@ impl HirDisplay for InContainer { }, DataTy::Enum => f.write_str("enum"), DataTy::Struct(struct_ref) => { - let cont = struct_ref.cont_id.to_container(f.db); + let cont = struct_ref.cont_id.data(f.db); let def = cont.get(struct_ref.value); let keyword = match def.kind { StructKind::Struct => "struct", @@ -369,7 +369,7 @@ impl HirDisplay for InModule { impl HirDisplay for InContainer { fn hir_fmt(&self, f: &mut HirFormatter<'_>) -> Result<(), HirDisplayError> { let InContainer { cont_id, value: expr_id } = self; - let container = cont_id.to_container(f.db); + let container = cont_id.data(f.db); let expr = container.get(*expr_id); self.with_value(expr).hir_fmt(f) } @@ -663,7 +663,7 @@ impl HirDisplay for InContainer { impl HirDisplay for InContainer { fn hir_fmt(&self, f: &mut HirFormatter<'_>) -> Result<(), HirDisplayError> { let InContainer { cont_id, value: decl_id } = self; - let container = cont_id.to_container(f.db); + let container = cont_id.data(f.db); let decl = container.get(*decl_id); if let Some(name) = &decl.name { @@ -681,7 +681,7 @@ impl HirDisplay for InContainer { impl HirDisplay for InContainer { fn hir_fmt(&self, f: &mut HirFormatter<'_>) -> Result<(), HirDisplayError> { let InContainer { cont_id, value: typedef_id } = self; - let container = cont_id.to_container(f.db); + let container = cont_id.data(f.db); let typedef = container.get(*typedef_id); f.write_str("typedef ")?; diff --git a/crates/hir/src/hir_def/aggregate.rs b/crates/hir/src/hir_def/aggregate.rs index 6165b6d5..64b1f182 100644 --- a/crates/hir/src/hir_def/aggregate.rs +++ b/crates/hir/src/hir_def/aggregate.rs @@ -10,7 +10,7 @@ use utils::text_edit::TextRange; use super::{Ident, expr::data_ty::DataTy, lower_ident_opt}; use crate::{ - container::{InContainer, ScopeId}, + container::{ArenaOwnerId, InContainer}, source_map::{FromSourceAst, IsNamedSrc, IsSrc, SourceAst, ToAstNode, root_token_in}, }; @@ -40,7 +40,7 @@ pub type StructId = Idx; pub(crate) fn lower_struct_def( struct_ty: StructUnionType, - container_id: ScopeId, + container_id: ArenaOwnerId, mut lower_data_ty: impl FnMut(DataType) -> DataTy, ) -> StructDef { let kind = match struct_ty { diff --git a/crates/hir/src/hir_def/block.rs b/crates/hir/src/hir_def/block.rs index 9d475d53..5732468a 100644 --- a/crates/hir/src/hir_def/block.rs +++ b/crates/hir/src/hir_def/block.rs @@ -33,7 +33,7 @@ use super::{ }; use crate::{ base_db::intern::Lookup, - container::{InFile, ScopeId}, + container::{ArenaOwnerId, InFile}, db::{HirDb, InternDb}, file::HirFileId, region_tree::{RegionTree, RegionTreeBuilder}, @@ -212,7 +212,7 @@ pub struct BlockId(pub salsa::InternId); #[derive(Debug, Hash, PartialEq, Eq, Clone)] pub struct BlockLoc { - pub cont_id: ScopeId, + pub cont_id: ArenaOwnerId, pub src: InFile, } @@ -235,7 +235,7 @@ impl_lower_declaration!(LowerBlockCtx<'_>, block, block_source_map); impl LowerBlockCtx<'_> { fn lower_struct_type(&mut self, struct_ty: ast::StructUnionType) -> StructId { - let container_id = ScopeId::Block(self.block_id); + let container_id = ArenaOwnerId::Block(self.block_id); let struct_def = lower_struct_def(struct_ty, container_id, |ty| self.expr_ctx().lower_data_ty(ty)); @@ -259,7 +259,7 @@ impl LowerBlockCtx<'_> { let lowered_ty = lower_typedef_data_ty( self, data_ty, - ScopeId::Block(self.block_id), + ArenaOwnerId::Block(self.block_id), |ctx, struct_ty| ctx.lower_struct_type(struct_ty), |ctx, ty| ctx.expr_ctx().lower_data_ty(ty), ); diff --git a/crates/hir/src/hir_def/file.rs b/crates/hir/src/hir_def/file.rs index cf19cdce..967eb404 100644 --- a/crates/hir/src/hir_def/file.rs +++ b/crates/hir/src/hir_def/file.rs @@ -43,7 +43,7 @@ use super::{ typedef::{Typedef, TypedefId, TypedefSrc, lower_typedef_data_ty}, }; use crate::{ - container::{InContainer, ScopeId}, + container::{ArenaOwnerId, SubroutineParent, SubroutineScope}, db::{HirDb, InternDb}, file::HirFileId, hir_def::lower_ident_opt, @@ -195,7 +195,7 @@ impl LowerProc for LowerFileCtx<'_> { impl LowerFileCtx<'_> { fn lower_struct_type(&mut self, struct_ty: ast::StructUnionType) -> StructId { - let container_id = ScopeId::File(self.file_id); + let container_id = ArenaOwnerId::File(self.file_id); let struct_def = lower_struct_def(struct_ty, container_id, |ty| self.expr_ctx().lower_data_ty(ty)); @@ -218,7 +218,7 @@ impl LowerFileCtx<'_> { let lowered_ty = lower_typedef_data_ty( self, data_ty, - ScopeId::File(self.file_id), + ArenaOwnerId::File(self.file_id), |ctx, struct_ty| ctx.lower_struct_type(struct_ty), |ctx, ty| ctx.expr_ctx().lower_data_ty(ty), ); @@ -240,7 +240,8 @@ impl LowerFileCtx<'_> { func => self.file_source_map.subroutine_srcs, }; - let subroutine_id = InContainer::new(self.file_id.into(), local_subroutine_id); + let subroutine_id = + SubroutineScope::new(SubroutineParent::File(self.file_id), local_subroutine_id); if func.end().is_some() { let subroutine = &mut self.file.subroutines[local_subroutine_id]; diff --git a/crates/hir/src/hir_def/module.rs b/crates/hir/src/hir_def/module.rs index 70e65236..cf3534a8 100644 --- a/crates/hir/src/hir_def/module.rs +++ b/crates/hir/src/hir_def/module.rs @@ -60,7 +60,7 @@ use super::{ typedef::{Typedef, TypedefId, TypedefSrc, lower_typedef_data_ty}, }; use crate::{ - container::{InContainer, InFile, ScopeId}, + container::{ArenaOwnerId, InFile, SubroutineParent, SubroutineScope}, db::{HirDb, InternDb}, file::HirFileId, region_tree::{RegionTree, RegionTreeBuilder}, @@ -410,7 +410,7 @@ impl LowerProc for LowerModuleCtx<'_> { impl LowerModuleCtx<'_> { fn lower_struct_type(&mut self, struct_ty: ast::StructUnionType) -> StructId { - let container_id = ScopeId::Module(self.module_id); + let container_id = ArenaOwnerId::Module(self.module_id); let struct_def = lower_struct_def(struct_ty, container_id, |ty| self.expr_ctx().lower_data_ty(ty)); @@ -434,7 +434,7 @@ impl LowerModuleCtx<'_> { let lowered_ty = lower_typedef_data_ty( self, data_ty, - ScopeId::Module(self.module_id), + ArenaOwnerId::Module(self.module_id), |ctx, struct_ty| ctx.lower_struct_type(struct_ty), |ctx, ty| ctx.expr_ctx().lower_data_ty(ty), ); @@ -456,7 +456,8 @@ impl LowerModuleCtx<'_> { func => self.module_source_map.subroutine_srcs, }; - let subroutine_def_id = InContainer::new(self.module_id.into(), subroutine_id); + let subroutine_def_id = + SubroutineScope::new(SubroutineParent::Module(self.module_id), subroutine_id); if func.end().is_some() { let subroutine = &mut self.module.subroutines[subroutine_id]; diff --git a/crates/hir/src/hir_def/module/generate.rs b/crates/hir/src/hir_def/module/generate.rs index 7f6af352..2d8e960d 100644 --- a/crates/hir/src/hir_def/module/generate.rs +++ b/crates/hir/src/hir_def/module/generate.rs @@ -24,7 +24,7 @@ use super::{ }; use crate::{ base_db::intern::Lookup, - container::{InContainer, InFile, ScopeId}, + container::{ArenaOwnerId, InFile, SubroutineParent, SubroutineScope}, db::{HirDb, InternDb}, file::HirFileId, hir_def::{ @@ -397,7 +397,7 @@ pub struct GenerateBlockId(pub salsa::InternId); #[derive(Debug, Hash, PartialEq, Eq, Clone)] pub struct GenerateBlockLoc { - pub cont_id: ScopeId, + pub cont_id: ArenaOwnerId, pub src: InFile, } @@ -453,7 +453,7 @@ impl LowerProc for LowerGenerateBlockCtx<'_> { impl LowerGenerateBlockCtx<'_> { fn lower_struct_type(&mut self, struct_ty: ast::StructUnionType) -> StructId { - let container_id = ScopeId::GenerateBlock(self.generate_block_id); + let container_id = ArenaOwnerId::GenerateBlock(self.generate_block_id); let struct_def = lower_struct_def(struct_ty, container_id, |ty| self.expr_ctx().lower_data_ty(ty)); @@ -477,7 +477,7 @@ impl LowerGenerateBlockCtx<'_> { let lowered_ty = lower_typedef_data_ty( self, data_ty, - ScopeId::GenerateBlock(self.generate_block_id), + ArenaOwnerId::GenerateBlock(self.generate_block_id), |ctx, struct_ty| ctx.lower_struct_type(struct_ty), |ctx, ty| ctx.expr_ctx().lower_data_ty(ty), ); @@ -499,7 +499,10 @@ impl LowerGenerateBlockCtx<'_> { func => self.generate_block_source_map.subroutine_srcs, }; - let subroutine_def_id = InContainer::new(self.generate_block_id.into(), subroutine_id); + let subroutine_def_id = SubroutineScope::new( + SubroutineParent::GenerateBlock(self.generate_block_id), + subroutine_id, + ); if func.end().is_some() { let subroutine = &mut self.generate_block.subroutines[subroutine_id]; diff --git a/crates/hir/src/hir_def/proc.rs b/crates/hir/src/hir_def/proc.rs index fe066216..fff37136 100644 --- a/crates/hir/src/hir_def/proc.rs +++ b/crates/hir/src/hir_def/proc.rs @@ -6,7 +6,7 @@ use super::{ stmt::impl_lower_stmt, }; use crate::{ - container::ScopeId, + container::ArenaOwnerId, db::InternDb, file::HirFileId, hir_def::{ @@ -62,7 +62,7 @@ pub(crate) trait LowerProc: LowerStmt { pub(crate) struct LowerProcCtx<'a> { pub(crate) db: &'a dyn InternDb, pub(crate) file_id: HirFileId, - pub(crate) cont_id: ScopeId, + pub(crate) cont_id: ArenaOwnerId, pub(crate) procs: &'a mut Arena, pub(crate) proc_srcs: &'a mut SourceMap, diff --git a/crates/hir/src/hir_def/stmt.rs b/crates/hir/src/hir_def/stmt.rs index 675161c6..10c9ddf7 100644 --- a/crates/hir/src/hir_def/stmt.rs +++ b/crates/hir/src/hir_def/stmt.rs @@ -20,7 +20,7 @@ use super::{ lower_ident_opt, }; use crate::{ - container::{InFile, ScopeId}, + container::{ArenaOwnerId, InFile}, db::InternDb, file::HirFileId, hir_def::{alloc_idx_and_src, lower_named_label_opt}, @@ -180,7 +180,7 @@ pub(in crate::hir_def) macro impl_lower_stmt { pub(crate) struct LowerStmtCtx<'a> { pub(crate) db: &'a dyn InternDb, pub(crate) file_id: HirFileId, - pub(crate) cont_id: ScopeId, + pub(crate) cont_id: ArenaOwnerId, pub(crate) stmts: &'a mut Arena, pub(crate) stmt_srcs: &'a mut SourceMap, diff --git a/crates/hir/src/hir_def/subroutine.rs b/crates/hir/src/hir_def/subroutine.rs index a0626638..13765016 100644 --- a/crates/hir/src/hir_def/subroutine.rs +++ b/crates/hir/src/hir_def/subroutine.rs @@ -29,7 +29,7 @@ use super::{ typedef::{Typedef, TypedefId, TypedefSrc, lower_typedef_data_ty}, }; use crate::{ - container::{InContainer, ScopeId}, + container::{ArenaOwnerId, SubroutineParent, SubroutineScope}, db::{HirDb, InternDb}, file::HirFileId, hir_def::{ @@ -206,7 +206,7 @@ fn map_direction(kind: Option) -> SubroutinePortDir { pub struct LowerSubroutineBodyCtx<'a> { pub(crate) db: &'a dyn InternDb, pub(crate) file_id: HirFileId, - pub(crate) subroutine_id: InContainer, + pub(crate) subroutine_id: SubroutineScope, pub(crate) subroutine: &'a mut Subroutine, pub(crate) subroutine_source_map: &'a mut SubroutineSourceMap, pub(crate) region_tree: RegionTreeBuilder, @@ -219,7 +219,7 @@ impl_lower_stmt!(LowerSubroutineBodyCtx<'_>, subroutine_id, subroutine, subrouti impl_lower_declaration!(LowerSubroutineBodyCtx<'_>, subroutine, subroutine_source_map); impl LowerSubroutineBodyCtx<'_> { - fn container_id(&self) -> ScopeId { + fn container_id(&self) -> ArenaOwnerId { self.subroutine_id.into() } @@ -331,33 +331,19 @@ pub fn lower_subroutine_body(ctx: &mut LowerSubroutineBodyCtx<'_>, func: ast::Fu pub(crate) fn subroutine_with_source_map_query( db: &dyn HirDb, - subroutine_id: InContainer, + subroutine_id: SubroutineScope, ) -> (Arc, Arc) { - match subroutine_id.cont_id { - ScopeId::File(file_id) => { - let file = db.hir_file(file_id); - let subroutine = file.subroutines[subroutine_id.value].clone(); - let source_map = subroutine.source_map.clone(); - (Arc::new(subroutine), Arc::new(source_map)) + let subroutine = match subroutine_id.cont_id { + SubroutineParent::File(file_id) => { + db.hir_file(file_id).subroutines[subroutine_id.value].clone() } - ScopeId::Module(module_id) => { - let module = db.module(module_id); - let subroutine = module.subroutines[subroutine_id.value].clone(); - let source_map = subroutine.source_map.clone(); - (Arc::new(subroutine), Arc::new(source_map)) + SubroutineParent::Module(module_id) => { + db.module(module_id).subroutines[subroutine_id.value].clone() } - ScopeId::GenerateBlock(generate_block_id) => { - let generate_block = db.generate_block(generate_block_id); - let subroutine = generate_block.subroutines[subroutine_id.value].clone(); - let source_map = subroutine.source_map.clone(); - (Arc::new(subroutine), Arc::new(source_map)) + SubroutineParent::GenerateBlock(generate_block_id) => { + db.generate_block(generate_block_id).subroutines[subroutine_id.value].clone() } - ScopeId::Block(_) - | ScopeId::Subroutine(_) - | ScopeId::ClockingBlock(_) - | ScopeId::Checker(_) - | ScopeId::Covergroup(_) => { - unreachable!("subroutines are lowered only in file, module, or generate-block scopes") - } - } + }; + let source_map = subroutine.source_map.clone(); + (Arc::new(subroutine), Arc::new(source_map)) } diff --git a/crates/hir/src/hir_def/typedef.rs b/crates/hir/src/hir_def/typedef.rs index 5800e5e2..ec1719a1 100644 --- a/crates/hir/src/hir_def/typedef.rs +++ b/crates/hir/src/hir_def/typedef.rs @@ -9,7 +9,7 @@ use utils::text_edit::TextRange; use super::{Ident, aggregate::StructId, expr::data_ty::DataTy}; use crate::{ - container::{InContainer, ScopeId}, + container::{ArenaOwnerId, InContainer}, source_map::{FromSourceAst, IsNamedSrc, IsSrc, SourceAst, ToAstNode, root_token_in}, }; @@ -92,7 +92,7 @@ impl TypedefSrc { pub(crate) fn lower_typedef_data_ty( ctx: &mut Ctx, data_ty: ast::DataType, - container_id: ScopeId, + container_id: ArenaOwnerId, mut lower_struct_type: impl FnMut(&mut Ctx, ast::StructUnionType) -> StructId, mut lower_data_ty: impl FnMut(&mut Ctx, ast::DataType) -> DataTy, ) -> DataTy { diff --git a/crates/hir/src/scope.rs b/crates/hir/src/scope.rs index 9232105b..b876ec52 100644 --- a/crates/hir/src/scope.rs +++ b/crates/hir/src/scope.rs @@ -5,7 +5,10 @@ use triomphe::Arc; use utils::get::{Get, GetRef}; use crate::{ - container::{InContainer, InFile, InFileOrModule, InModule, InSubroutine, ScopeId}, + container::{ + ArenaOwnerId, FileOrModule, InContainer, InFile, InFileOrModule, InModule, InScope, + InSubroutine, ScopeId, SubroutineParent, SubroutineScope, + }, db::HirDb, def_id::DefId, file::HirFileId, @@ -49,7 +52,7 @@ fn def_id(db: &dyn HirDb, loc: impl Into) -> DefId { fn insert_decls_and_typedefs( scope: &mut NameScope, db: &dyn HirDb, - cont_id: ScopeId, + cont_id: ArenaOwnerId, decls: &Arena, typedefs: &Arena, ) { @@ -65,7 +68,7 @@ fn insert_decls_and_typedefs( /// scope owns. Kept separate from `insert_decls_and_typedefs` so callers can /// place module/generate specific members between the two while preserving /// insertion order. -fn insert_stmts(scope: &mut NameScope, db: &dyn HirDb, cont_id: ScopeId, stmts: &Arena) { +fn insert_stmts(scope: &mut NameScope, db: &dyn HirDb, cont_id: ArenaOwnerId, stmts: &Arena) { for (stmt_id, stmt) in stmts.iter() { scope.insert_value_opt(&stmt.label, def_id(db, InContainer::new(cont_id, stmt_id))); if let StmtKind::Block(BlockInfo { name, block_id }) = &stmt.kind { @@ -160,29 +163,26 @@ impl NameScope { for (checker_id, checker) in hir_file.checkers.iter() { scope.insert_type_opt( &checker.name, - def_id(db, InContainer::new(file_id.into(), checker_id)), + def_id(db, InFileOrModule::new(FileOrModule::File(file_id), checker_id)), ); } for (covergroup_id, covergroup) in hir_file.covergroups.iter() { scope.insert_type_opt( &covergroup.name, - def_id(db, InContainer::new(file_id.into(), covergroup_id)), + def_id(db, InFileOrModule::new(FileOrModule::File(file_id), covergroup_id)), ); } for (coverpoint_id, coverpoint) in hir_file.coverpoints.iter() { scope.insert_value_opt( &coverpoint.name, - def_id(db, InContainer::new(file_id.into(), coverpoint_id)), + def_id(db, InScope::new(file_id.into(), coverpoint_id)), ); } for (cross_id, cross) in hir_file.crosses.iter() { - scope.insert_value_opt( - &cross.name, - def_id(db, InContainer::new(file_id.into(), cross_id)), - ); + scope.insert_value_opt(&cross.name, def_id(db, InScope::new(file_id.into(), cross_id))); } for (typedef_id, typedef) in hir_file.typedefs.iter() { @@ -213,7 +213,8 @@ impl NameScope { } for (local_subroutine_id, subroutine) in module.subroutines.iter() { - let subroutine_id = InContainer::new(module_id.into(), local_subroutine_id); + let subroutine_id = + SubroutineScope::new(SubroutineParent::Module(module_id), local_subroutine_id); scope.insert_value_opt(&subroutine.name, def_id(db, subroutine_id)); } @@ -231,28 +232,28 @@ impl NameScope { for (checker_id, checker) in module.checkers.iter() { scope.insert_type_opt( &checker.name, - def_id(db, InContainer::new(module_id.into(), checker_id)), + def_id(db, InFileOrModule::new(FileOrModule::Module(module_id), checker_id)), ); } for (covergroup_id, covergroup) in module.covergroups.iter() { scope.insert_type_opt( &covergroup.name, - def_id(db, InContainer::new(module_id.into(), covergroup_id)), + def_id(db, InFileOrModule::new(FileOrModule::Module(module_id), covergroup_id)), ); } for (coverpoint_id, coverpoint) in module.coverpoints.iter() { scope.insert_value_opt( &coverpoint.name, - def_id(db, InContainer::new(module_id.into(), coverpoint_id)), + def_id(db, InScope::new(module_id.into(), coverpoint_id)), ); } for (cross_id, cross) in module.crosses.iter() { scope.insert_value_opt( &cross.name, - def_id(db, InContainer::new(module_id.into(), cross_id)), + def_id(db, InScope::new(module_id.into(), cross_id)), ); } @@ -302,10 +303,7 @@ impl NameScope { for (idx, signal) in clocking_block.signals.iter().enumerate() { let signal_id = ClockingSignalId(idx as u32); - scope.insert_value( - &signal.name, - def_id(db, InContainer::new(clocking_scope, signal_id)), - ); + scope.insert_value(&signal.name, def_id(db, InScope::new(clocking_scope, signal_id))); } Arc::new(scope) @@ -313,33 +311,26 @@ impl NameScope { pub fn checker_scope_query( db: &dyn HirDb, - checker_id: InContainer, + checker_id: InFileOrModule, ) -> Arc { let mut scope = NameScope::default(); - let Some(checker) = checker_def(db, checker_id) else { - return Arc::new(scope); - }; - let Ok(checker_scope_id) = InFileOrModule::try_from(checker_id) else { - return Arc::new(scope); - }; - let checker_scope = ScopeId::Checker(checker_scope_id); + let checker = checker_def(db, checker_id); + let checker_scope = ScopeId::Checker(checker_id); for (idx, port) in checker.ports.iter().enumerate() { scope.insert_value( &port.name, - def_id(db, InContainer::new(checker_scope, CheckerPortId(idx as u32))), + def_id(db, InScope::new(checker_scope, CheckerPortId(idx as u32))), ); } - let container = checker_id.cont_id.to_container(db); + let owner_id = ArenaOwnerId::from(checker_id.cont_id); + let container = owner_id.data(db); for declaration_id in &checker.declarations { let declaration = container.get(*declaration_id); for decl_id in declaration.decls() { let decl = container.get(decl_id); - scope.insert_value_opt( - &decl.name, - def_id(db, InContainer::new(checker_id.cont_id, decl_id)), - ); + scope.insert_value_opt(&decl.name, def_id(db, InContainer::new(owner_id, decl_id))); } } @@ -348,25 +339,20 @@ impl NameScope { pub fn covergroup_scope_query( db: &dyn HirDb, - covergroup_id: InContainer, + covergroup_id: InFileOrModule, ) -> Arc { let mut scope = NameScope::default(); - let Some(covergroup) = covergroup_def(db, covergroup_id) else { - return Arc::new(scope); - }; - let Ok(covergroup_scope_id) = InFileOrModule::try_from(covergroup_id) else { - return Arc::new(scope); - }; - let covergroup_scope = ScopeId::Covergroup(covergroup_scope_id); + let covergroup = covergroup_def(db, covergroup_id); + let covergroup_scope = ScopeId::Covergroup(covergroup_id); match covergroup_id.cont_id { - ScopeId::File(file_id) => { + FileOrModule::File(file_id) => { let file = db.hir_file(file_id); for coverpoint_id in &covergroup.coverpoints { let coverpoint = file.get(*coverpoint_id); scope.insert_value_opt( &coverpoint.name, - def_id(db, InContainer::new(covergroup_scope, *coverpoint_id)), + def_id(db, InScope::new(covergroup_scope, *coverpoint_id)), ); } @@ -374,17 +360,17 @@ impl NameScope { let cross = file.get(*cross_id); scope.insert_value_opt( &cross.name, - def_id(db, InContainer::new(covergroup_scope, *cross_id)), + def_id(db, InScope::new(covergroup_scope, *cross_id)), ); } } - ScopeId::Module(module_id) => { + FileOrModule::Module(module_id) => { let module = db.module(module_id); for coverpoint_id in &covergroup.coverpoints { let coverpoint = module.get(*coverpoint_id); scope.insert_value_opt( &coverpoint.name, - def_id(db, InContainer::new(covergroup_scope, *coverpoint_id)), + def_id(db, InScope::new(covergroup_scope, *coverpoint_id)), ); } @@ -392,16 +378,10 @@ impl NameScope { let cross = module.get(*cross_id); scope.insert_value_opt( &cross.name, - def_id(db, InContainer::new(covergroup_scope, *cross_id)), + def_id(db, InScope::new(covergroup_scope, *cross_id)), ); } } - ScopeId::GenerateBlock(_) - | ScopeId::Block(_) - | ScopeId::Subroutine(_) - | ScopeId::ClockingBlock(_) - | ScopeId::Checker(_) - | ScopeId::Covergroup(_) => {} } Arc::new(scope) @@ -417,7 +397,10 @@ impl NameScope { scope.insert_value_opt(&generate_block.name, def_id(db, generate_block_id)); for (local_subroutine_id, subroutine) in generate_block.subroutines.iter() { - let subroutine_id = InContainer::new(generate_block_id.into(), local_subroutine_id); + let subroutine_id = SubroutineScope::new( + SubroutineParent::GenerateBlock(generate_block_id), + local_subroutine_id, + ); scope.insert_value_opt(&subroutine.name, def_id(db, subroutine_id)); } @@ -458,7 +441,7 @@ impl NameScope { pub fn subroutine_scope_query( db: &dyn HirDb, - subroutine_id: InContainer, + subroutine_id: SubroutineScope, ) -> Arc { let mut scope = NameScope::default(); let subroutine = db.subroutine(subroutine_id); @@ -522,32 +505,17 @@ impl NameScope { } } -fn checker_def(db: &dyn HirDb, checker_id: InContainer) -> Option { +fn checker_def(db: &dyn HirDb, checker_id: InFileOrModule) -> CheckerDef { match checker_id.cont_id { - ScopeId::File(file_id) => Some(db.hir_file(file_id).get(checker_id.value).clone()), - ScopeId::Module(module_id) => Some(db.module(module_id).get(checker_id.value).clone()), - ScopeId::GenerateBlock(_) - | ScopeId::Block(_) - | ScopeId::Subroutine(_) - | ScopeId::ClockingBlock(_) - | ScopeId::Checker(_) - | ScopeId::Covergroup(_) => None, + FileOrModule::File(file_id) => db.hir_file(file_id).get(checker_id.value).clone(), + FileOrModule::Module(module_id) => db.module(module_id).get(checker_id.value).clone(), } } -fn covergroup_def( - db: &dyn HirDb, - covergroup_id: InContainer, -) -> Option { +fn covergroup_def(db: &dyn HirDb, covergroup_id: InFileOrModule) -> CovergroupDef { match covergroup_id.cont_id { - ScopeId::File(file_id) => Some(db.hir_file(file_id).get(covergroup_id.value).clone()), - ScopeId::Module(module_id) => Some(db.module(module_id).get(covergroup_id.value).clone()), - ScopeId::GenerateBlock(_) - | ScopeId::Block(_) - | ScopeId::Subroutine(_) - | ScopeId::ClockingBlock(_) - | ScopeId::Checker(_) - | ScopeId::Covergroup(_) => None, + FileOrModule::File(file_id) => db.hir_file(file_id).get(covergroup_id.value).clone(), + FileOrModule::Module(module_id) => db.module(module_id).get(covergroup_id.value).clone(), } } @@ -638,7 +606,10 @@ impl PackageExportSignatureBuilder<'_> { let name = lower_name(subroutine.prototype().name()); self.scope.insert_value_opt( &name, - def_id(self.db, InContainer::new(self.package_id.into(), local_id)), + def_id( + self.db, + SubroutineScope::new(SubroutineParent::Module(self.package_id), local_id), + ), ); } @@ -704,12 +675,13 @@ mod tests { }, source_root::{SourceRoot, SourceRootId}, }, - container::{InContainer, ScopeId}, + container::{FileOrModule, InContainer, InFile, InFileOrModule, ScopeId, SubroutineParent}, db::{HirDb, HirDbStorage, InternDbStorage}, def_id::DefId, + file::HirFileId, hir_def::Ident, semantics::pathres::resolve_name, - symbol::{DefKind, DefOriginLoc, NameContext, Resolution}, + symbol::{DefKind, DefOriginLoc, NameContext, Resolution, ScopeKind}, }; const TOP: FileId = FileId::from_raw(0); @@ -779,6 +751,44 @@ mod tests { SmolStr::new(name) } + #[test] + fn scope_kind_reflects_module_declaration_kind() { + let db = db_with_root_text( + r#" +package p; +endpackage +interface i; +endinterface +program pr; +endprogram +module m; +endmodule +"#, + ); + let file_id = HirFileId::File(TOP); + let file = db.hir_file(file_id); + let actual = file + .modules + .iter() + .map(|(local_id, module)| { + ( + module.name.clone().unwrap(), + ScopeId::Module(InFile::new(file_id, local_id)).kind(&db), + ) + }) + .collect::>(); + + assert_eq!( + actual, + vec![ + (ident("p"), ScopeKind::Package), + (ident("i"), ScopeKind::Interface), + (ident("pr"), ScopeKind::Program), + (ident("m"), ScopeKind::Module), + ] + ); + } + #[test] fn name_scope_context_lookup_covers_current_scope_shapes() { let db = db_with_root_text( @@ -1189,15 +1199,15 @@ endmodule .any(|def_id| matches!(def_id.primary_origin(&db).loc(&db), DefOriginLoc::Cross(id) if id.value == cross_id)) ); - let covergroup_scope = - db.covergroup_scope(InContainer::new(module_id.into(), covergroup_id)); + let covergroup_scope = db + .covergroup_scope(InFileOrModule::new(FileOrModule::Module(module_id), covergroup_id)); let scoped_coverpoint_defs = covergroup_scope.lookup(NameContext::Value, &ident("cp")); assert!(scoped_coverpoint_defs.iter().any(|def_id| { - matches!(def_id.primary_origin(&db).loc(&db), DefOriginLoc::Coverpoint(id) if matches!(id.cont_id, ScopeId::Covergroup(_)) && id.value == coverpoint_id) + matches!(def_id.primary_origin(&db).loc(&db), DefOriginLoc::Coverpoint(id) if matches!(id.scope_id, ScopeId::Covergroup(_)) && id.value == coverpoint_id) })); let scoped_cross_defs = covergroup_scope.lookup(NameContext::Value, &ident("cx")); assert!(scoped_cross_defs.iter().any(|def_id| { - matches!(def_id.primary_origin(&db).loc(&db), DefOriginLoc::Cross(id) if matches!(id.cont_id, ScopeId::Covergroup(_)) && id.value == cross_id) + matches!(def_id.primary_origin(&db).loc(&db), DefOriginLoc::Cross(id) if matches!(id.scope_id, ScopeId::Covergroup(_)) && id.value == cross_id) })); let instantiation = module @@ -1366,7 +1376,7 @@ endmodule else { panic!("package f should resolve to a subroutine"); }; - assert_eq!(package_subroutine.cont_id, ScopeId::Module(package_id)); + assert_eq!(package_subroutine.cont_id, SubroutineParent::Module(package_id)); let named_importer = db .unit_scope() diff --git a/crates/hir/src/semantics.rs b/crates/hir/src/semantics.rs index a9ea11b2..951dc2b0 100644 --- a/crates/hir/src/semantics.rs +++ b/crates/hir/src/semantics.rs @@ -11,7 +11,7 @@ use utils::text_edit::TextSize; use vfs::FileId; use crate::{ - container::{InContainer, InFile, ScopeId}, + container::{ArenaOwnerId, InContainer, InFile, SubroutineScope}, db::HirDb, def_id::DefId, file::HirFileId, @@ -20,7 +20,7 @@ use crate::{ block::{BlockId, BlockSrc}, expr::ExprId, module::{ModuleId, ModuleSrc}, - subroutine::{LocalSubroutineId, SubroutineSrc}, + subroutine::SubroutineSrc, }, symbol::{NameContext, Resolution}, }; @@ -115,7 +115,7 @@ impl<'db> SemanticsImpl<'db> { ParsedFile { file_id, tree: self.db.parse(file_id) } } - pub fn container_for_node(&self, file_id: HirFileId, node: SyntaxNode) -> Option { + pub fn container_for_node(&self, file_id: HirFileId, node: SyntaxNode) -> Option { self.with_ctx(|ctx| Some(ctx.find_container(InFile::new(file_id, node)))) } @@ -148,7 +148,7 @@ impl SemanticsImpl<'_> { &self, file_id: HirFileId, subroutine: ast::FunctionDeclaration, - ) -> Option> { + ) -> Option { let subroutine_src = SubroutineSrc::from_ast(file_id, subroutine); self.with_ctx(|ctx| ctx.subroutine_to_def(InFile::new(file_id, subroutine_src))) } diff --git a/crates/hir/src/semantics/hir_to_def.rs b/crates/hir/src/semantics/hir_to_def.rs index 1f5a2342..4a4bcaa7 100644 --- a/crates/hir/src/semantics/hir_to_def.rs +++ b/crates/hir/src/semantics/hir_to_def.rs @@ -3,7 +3,7 @@ use utils::get::GetRef; use super::Source2DefCtx; use crate::{ - container::{InContainer, ScopeId}, + container::{ArenaOwnerId, InContainer}, def_id::DefId, hir_def::{ Ident, @@ -50,29 +50,26 @@ impl Source2DefCtx<'_, '_> { }; match cont_id { - ScopeId::File(file_id) => { + ArenaOwnerId::File(file_id) => { let file = db.hir_file(file_id); resolve(file.get(expr_id)) } - ScopeId::Module(in_file) => { + ArenaOwnerId::Module(in_file) => { let module = db.module(in_file); resolve(module.get(expr_id)) } - ScopeId::Block(block_id) => { + ArenaOwnerId::Block(block_id) => { let block = db.block(block_id); resolve(block.get(expr_id)) } - ScopeId::GenerateBlock(generate_block_id) => { + ArenaOwnerId::GenerateBlock(generate_block_id) => { let generate_block = db.generate_block(generate_block_id); resolve(generate_block.get(expr_id)) } - ScopeId::Subroutine(subroutine_id) => { - let subroutine = db.subroutine(subroutine_id.as_in_container()); + ArenaOwnerId::Subroutine(subroutine_id) => { + let subroutine = db.subroutine(subroutine_id); resolve(subroutine.get(expr_id)) } - ScopeId::ClockingBlock(_) | ScopeId::Checker(_) | ScopeId::Covergroup(_) => { - Resolution::Unresolved - } } } @@ -81,24 +78,24 @@ impl Source2DefCtx<'_, '_> { InContainer { cont_id, value: ident }: InContainer, name_ctx: NameContext, ) -> Resolution { - let res = resolve_name(self.db, cont_id, &ident, name_ctx); + let res = resolve_name(self.db, cont_id.into(), &ident, name_ctx); self.hir_cache.name_map.insert(InContainer::new(cont_id, ident), res.clone()); res } fn resolve_expr_path( &self, - cont_id: ScopeId, + cont_id: ArenaOwnerId, expr_id: ExprId, ctx: NameContext, ) -> Resolution { let Some(path) = self.expr_path(cont_id, expr_id) else { return Resolution::Unresolved; }; - resolve_path(self.db, cont_id, &path, ctx) + resolve_path(self.db, cont_id.into(), &path, ctx) } - fn expr_path(&self, cont_id: ScopeId, expr_id: ExprId) -> Option> { + fn expr_path(&self, cont_id: ArenaOwnerId, expr_id: ExprId) -> Option> { match self.expr_in_container(cont_id, expr_id)? { Expr::Ident(ident) => Some(vec![ident]), Expr::Field { receiver, field } => { @@ -111,18 +108,17 @@ impl Source2DefCtx<'_, '_> { } } - fn expr_in_container(&self, cont_id: ScopeId, expr_id: ExprId) -> Option { + fn expr_in_container(&self, cont_id: ArenaOwnerId, expr_id: ExprId) -> Option { 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) => { + ArenaOwnerId::File(file_id) => Some(self.db.hir_file(file_id).get(expr_id).clone()), + ArenaOwnerId::Module(module_id) => Some(self.db.module(module_id).get(expr_id).clone()), + ArenaOwnerId::Block(block_id) => Some(self.db.block(block_id).get(expr_id).clone()), + ArenaOwnerId::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()) + ArenaOwnerId::Subroutine(subroutine_id) => { + Some(self.db.subroutine(subroutine_id).get(expr_id).clone()) } - ScopeId::ClockingBlock(_) | ScopeId::Checker(_) | ScopeId::Covergroup(_) => None, } } } diff --git a/crates/hir/src/semantics/pathres.rs b/crates/hir/src/semantics/pathres.rs index 66a1b79e..8b4405ff 100644 --- a/crates/hir/src/semantics/pathres.rs +++ b/crates/hir/src/semantics/pathres.rs @@ -5,7 +5,7 @@ use utils::get::GetRef; use super::SemanticsImpl; use crate::{ - container::{InContainer, InFile, ScopeId, ScopeParent}, + container::{ArenaOwnerId, InContainer, InFile, ScopeId, ScopeParent}, db::HirDb, def_id::DefId, file::HirFileId, @@ -44,7 +44,7 @@ impl SemanticsImpl<'_> { }) } - pub(in crate::semantics) fn find_container(&self, node: InFile) -> ScopeId { + pub(in crate::semantics) fn find_container(&self, node: InFile) -> ArenaOwnerId { self.with_ctx(|ctx| ctx.find_container(node)) } @@ -154,13 +154,8 @@ pub fn descend_scope(db: &dyn HirDb, def_id: DefId) -> Option { origin.as_module(db).map(Into::into) } DefKind::ClockingBlock => origin.as_clocking_block(db).map(Into::into), - DefKind::Checker => { - origin.as_checker(db).and_then(|checker| checker.try_into().ok()).map(ScopeId::Checker) - } - DefKind::Covergroup => origin - .as_covergroup(db) - .and_then(|covergroup| covergroup.try_into().ok()) - .map(ScopeId::Covergroup), + DefKind::Checker => origin.as_checker(db).map(ScopeId::Checker), + DefKind::Covergroup => origin.as_covergroup(db).map(ScopeId::Covergroup), DefKind::Instance => { let instance = origin.as_instance(db)?; let target = instance_target_def_id(db, instance.module_id, instance.value)?; @@ -190,11 +185,11 @@ pub(crate) fn name_scope(db: &dyn HirDb, scope_id: ScopeId) -> Arc { ScopeId::File(file_id) => db.file_scope(file_id), ScopeId::Module(module_id) => db.module_scope(module_id), ScopeId::ClockingBlock(clocking_block_id) => db.clocking_block_scope(clocking_block_id), - ScopeId::Checker(checker_id) => db.checker_scope(checker_id.as_in_container()), - ScopeId::Covergroup(covergroup_id) => db.covergroup_scope(covergroup_id.as_in_container()), + ScopeId::Checker(checker_id) => db.checker_scope(checker_id), + ScopeId::Covergroup(covergroup_id) => db.covergroup_scope(covergroup_id), ScopeId::GenerateBlock(generate_block_id) => db.generate_block_scope(generate_block_id), ScopeId::Block(block_id) => db.block_scope(block_id), - ScopeId::Subroutine(subroutine_id) => db.subroutine_scope(subroutine_id.as_in_container()), + ScopeId::Subroutine(subroutine_id) => db.subroutine_scope(subroutine_id), } } diff --git a/crates/hir/src/semantics/resolver.rs b/crates/hir/src/semantics/resolver.rs index b329f7ac..304d01bf 100644 --- a/crates/hir/src/semantics/resolver.rs +++ b/crates/hir/src/semantics/resolver.rs @@ -3,7 +3,7 @@ use utils::get::Get; use super::SemanticsImpl; use crate::{ - container::{InContainer, InFile, InModule, ScopeId}, + container::{ArenaOwnerId, InContainer, InFile, InModule}, file::HirFileId, hir_def::{ expr::{ExprId, ExprSrc}, @@ -22,7 +22,7 @@ impl SemanticsImpl<'_> { instance: ast::HierarchicalInstance, ) -> Option> { let db = self.db; - let ScopeId::Module(module_id) = + let ArenaOwnerId::Module(module_id) = self.find_container(InFile::new(file_id, instance.syntax())) else { return None; @@ -40,7 +40,7 @@ impl SemanticsImpl<'_> { instantiation: ast::HierarchyInstantiation, ) -> Option> { let db = self.db; - let ScopeId::Module(module_id) = + let ArenaOwnerId::Module(module_id) = self.find_container(InFile::new(file_id, instantiation.syntax())) else { return None; @@ -60,7 +60,8 @@ impl SemanticsImpl<'_> { conn: ast::PortConnection, ) -> Option> { let db = self.db; - let ScopeId::Module(module_id) = self.find_container(InFile::new(file_id, conn.syntax())) + let ArenaOwnerId::Module(module_id) = + self.find_container(InFile::new(file_id, conn.syntax())) else { return None; }; @@ -78,7 +79,7 @@ impl SemanticsImpl<'_> { ) -> Option> { let db = self.db; let container_id = self.find_container(InFile::new(file_id, expr.syntax())); - let src_map = container_id.to_container_src_map(db); + let src_map = container_id.source_map(db); let expr_src = ExprSrc::from_ast(file_id, expr); let expr_id = src_map.get(expr_src)?; diff --git a/crates/hir/src/semantics/source_to_def.rs b/crates/hir/src/semantics/source_to_def.rs index b7c06954..12858029 100644 --- a/crates/hir/src/semantics/source_to_def.rs +++ b/crates/hir/src/semantics/source_to_def.rs @@ -9,7 +9,7 @@ use utils::get::{Get, GetRef}; use super::hir_to_def::Hir2DefCache; use crate::{ - container::{InContainer, InFile, ScopeId}, + container::{ArenaOwnerId, InFile, SubroutineParent, SubroutineScope}, db::HirDb, file::HirFileId, hir_def::{ @@ -25,7 +25,7 @@ use crate::{ #[derive(Default, Debug)] pub(super) struct Source2DefCache { - container_map: FxHashMap, ScopeId>, + container_map: FxHashMap, ArenaOwnerId>, } pub(super) struct Source2DefCtx<'db, 'cache> { @@ -55,7 +55,7 @@ impl Source2DefCtx<'_, '_> { pub(super) fn subroutine_to_def( &mut self, InFile { file_id, value: subroutine_src }: InFile, - ) -> Option> { + ) -> Option { let tree = self.db.parse(file_id); let node = subroutine_src.to_node(&tree)?; self.subroutine_to_def_inner(file_id, node, subroutine_src) @@ -72,42 +72,39 @@ impl Source2DefCtx<'_, '_> { let container = self.find_container(InFile::new(file_id, node)); let block_id = match container { - ScopeId::File(file_id) => { + ArenaOwnerId::File(file_id) => { let (file, file_src_map) = self.db.hir_file_with_source_map(file_id); let local_block_id = find_local_block_id(&file_src_map.stmt_srcs, block_src)?; file.get(local_block_id).block_id } - ScopeId::Module(module_id) => { + ArenaOwnerId::Module(module_id) => { let (module, module_src_map) = self.db.module_with_source_map(module_id); let local_block_id = find_local_block_id(&module_src_map.stmt_srcs, block_src)?; module.get(local_block_id).block_id } - ScopeId::Block(block_id) => { + ArenaOwnerId::Block(block_id) => { let (block, block_src_map) = self.db.block_with_source_map(block_id); let local_block_id = *block_src_map.block_srcs.get(&block_src)?; block.get(local_block_id).block_id } - ScopeId::GenerateBlock(generate_block_id) => { + ArenaOwnerId::GenerateBlock(generate_block_id) => { let (generate_block, generate_block_src_map) = self.db.generate_block_with_source_map(generate_block_id); let local_block_id = generate_block_src_map.get(block_src)?; generate_block.get(local_block_id).block_id } - ScopeId::Subroutine(subroutine_id) => { + ArenaOwnerId::Subroutine(subroutine_id) => { let (subroutine, subroutine_src_map) = - self.db.subroutine_with_source_map(subroutine_id.as_in_container()); + self.db.subroutine_with_source_map(subroutine_id); let local_block_id = *subroutine_src_map.block_srcs.get(&block_src)?; subroutine.stmts.get(local_block_id).block_id } - ScopeId::ClockingBlock(_) | ScopeId::Checker(_) | ScopeId::Covergroup(_) => { - return None; - } }; Some(block_id) } - fn container_to_def(&mut self, file_id: HirFileId, node: SyntaxNode) -> Option { + fn container_to_def(&mut self, file_id: HirFileId, node: SyntaxNode) -> Option { let cont_id = match_ast! { node, ast::ModuleDeclaration[module] => { let src = ModuleSrc::from_ast(file_id, module); @@ -152,7 +149,7 @@ impl Source2DefCtx<'_, '_> { file_id: HirFileId, node: ast::FunctionDeclaration, src: SubroutineSrc, - ) -> Option> { + ) -> Option { let parent = ast::Member::cast(node.syntax()) .and_then(|member| self.single_member_generate_block_to_def(file_id, member)) .or_else(|| { @@ -161,33 +158,36 @@ impl Source2DefCtx<'_, '_> { .find_map(|node| self.container_to_def(file_id, node)) }) .unwrap_or(file_id.into()); + let parent = match parent { + ArenaOwnerId::File(file_id) => SubroutineParent::File(file_id), + ArenaOwnerId::Module(module_id) => SubroutineParent::Module(module_id), + ArenaOwnerId::GenerateBlock(generate_block_id) => { + SubroutineParent::GenerateBlock(generate_block_id) + } + ArenaOwnerId::Block(_) | ArenaOwnerId::Subroutine(_) => return None, + }; let local_id = self.local_subroutine_id(parent, src)?; - Some(InContainer::new(parent, local_id)) + Some(SubroutineScope::new(parent, local_id)) } fn local_subroutine_id( &self, - cont_id: ScopeId, + cont_id: SubroutineParent, src: SubroutineSrc, ) -> Option { match cont_id { - ScopeId::File(file_id) => { + SubroutineParent::File(file_id) => { let (_, source_map) = self.db.hir_file_with_source_map(file_id); source_map.get(src) } - ScopeId::Module(module_id) => { + SubroutineParent::Module(module_id) => { let (_, source_map) = self.db.module_with_source_map(module_id); source_map.get(src) } - ScopeId::GenerateBlock(generate_block_id) => { + SubroutineParent::GenerateBlock(generate_block_id) => { let (_, source_map) = self.db.generate_block_with_source_map(generate_block_id); source_map.get(src) } - ScopeId::Block(_) - | ScopeId::Subroutine(_) - | ScopeId::ClockingBlock(_) - | ScopeId::Checker(_) - | ScopeId::Covergroup(_) => None, } } @@ -195,7 +195,7 @@ impl Source2DefCtx<'_, '_> { &mut self, file_id: HirFileId, member: ast::Member, - ) -> Option { + ) -> Option { if matches!(member, ast::Member::GenerateBlock(_) | ast::Member::LoopGenerate(_)) { return None; } @@ -243,7 +243,7 @@ impl Source2DefCtx<'_, '_> { pub(super) fn find_container( &mut self, InFile { value: node, file_id }: InFile, - ) -> ScopeId { + ) -> ArenaOwnerId { let in_file = InFile::new(file_id, SyntaxNodePtr::from_node(node)); if let Some(container_id) = self.source_cache.container_map.get(&in_file) { diff --git a/crates/hir/src/symbol.rs b/crates/hir/src/symbol.rs index 4ae27785..6f44b2c5 100644 --- a/crates/hir/src/symbol.rs +++ b/crates/hir/src/symbol.rs @@ -4,7 +4,9 @@ use utils::impl_from; use crate::{ base_db::salsa, - container::{InContainer, InFile, InModule, InSubroutine}, + container::{ + InContainer, InFile, InFileOrModule, InModule, InScope, InSubroutine, SubroutineScope, + }, db::{HirDb, InternDb}, def_id::DefId, hir_def::{ @@ -23,7 +25,7 @@ use crate::{ port::NonAnsiPortId, }, stmt::StmtId, - subroutine::{LocalSubroutineId, SubroutinePortId}, + subroutine::SubroutinePortId, typedef::TypedefId, }, }; @@ -41,7 +43,7 @@ pub enum DefOriginLoc { Udp(InFile), Block(BlockId), GenerateBlock(GenerateBlockId), - Subroutine(InContainer), + Subroutine(SubroutineScope), SubroutinePort(InSubroutine), NonAnsiPort(InModule), Decl(InContainer), @@ -49,12 +51,12 @@ pub enum DefOriginLoc { Instance(InModule), Modport(InModule), ClockingBlock(InModule), - ClockingSignal(InContainer), - Checker(InContainer), - CheckerPort(InContainer), - Covergroup(InContainer), - Coverpoint(InContainer), - Cross(InContainer), + ClockingSignal(InScope), + Checker(InFileOrModule), + CheckerPort(InScope), + Covergroup(InFileOrModule), + Coverpoint(InScope), + Cross(InScope), Stmt(InContainer), } @@ -65,7 +67,7 @@ impl_from! { DefOriginLoc => Udp(InFile), Block(BlockId), GenerateBlock(GenerateBlockId), - Subroutine(InContainer), + Subroutine(SubroutineScope), SubroutinePort(InSubroutine), NonAnsiPort(InModule), Decl(InContainer), @@ -73,12 +75,12 @@ impl_from! { DefOriginLoc => Instance(InModule), Modport(InModule), ClockingBlock(InModule), - ClockingSignal(InContainer), - Checker(InContainer), - CheckerPort(InContainer), - Covergroup(InContainer), - Coverpoint(InContainer), - Cross(InContainer), + ClockingSignal(InScope), + Checker(InFileOrModule), + CheckerPort(InScope), + Covergroup(InFileOrModule), + Coverpoint(InScope), + Cross(InScope), Stmt(InContainer), } @@ -106,7 +108,7 @@ impl DefOrigin { impl_origin_cast!(as_generate_block, GenerateBlock, GenerateBlockId); - impl_origin_cast!(as_subroutine, Subroutine, InContainer); + impl_origin_cast!(as_subroutine, Subroutine, SubroutineScope); impl_origin_cast!(as_subroutine_port, SubroutinePort, InSubroutine); @@ -122,13 +124,13 @@ impl DefOrigin { impl_origin_cast!(as_clocking_block, ClockingBlock, InModule); - impl_origin_cast!(as_clocking_signal, ClockingSignal, InContainer); + impl_origin_cast!(as_clocking_signal, ClockingSignal, InScope); - impl_origin_cast!(as_checker, Checker, InContainer); + impl_origin_cast!(as_checker, Checker, InFileOrModule); - impl_origin_cast!(as_checker_port, CheckerPort, InContainer); + impl_origin_cast!(as_checker_port, CheckerPort, InScope); - impl_origin_cast!(as_covergroup, Covergroup, InContainer); + impl_origin_cast!(as_covergroup, Covergroup, InFileOrModule); impl_origin_cast!(as_stmt, Stmt, InContainer); @@ -241,7 +243,6 @@ pub enum ScopeKind { Module, Interface, Program, - Class, GenerateBlock, Block, Subroutine, diff --git a/crates/hir/src/type_infer.rs b/crates/hir/src/type_infer.rs index c33153ef..ff797676 100644 --- a/crates/hir/src/type_infer.rs +++ b/crates/hir/src/type_infer.rs @@ -3,7 +3,7 @@ use triomphe::Arc; use utils::get::GetRef; use crate::{ - container::{InContainer, InSubroutine, ScopeId}, + container::{ArenaOwnerId, InContainer, InSubroutine}, db::HirDb, def_id::DefId, hir_def::{ @@ -27,7 +27,7 @@ use crate::{ #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] pub enum BuiltinTy { - Data { id: BuiltinDataTyId, container: ScopeId }, + Data { id: BuiltinDataTyId, container: ArenaOwnerId }, } #[derive(Debug, Clone, PartialEq, Eq)] @@ -83,13 +83,13 @@ pub enum TyClass { String, } -pub fn normalize_data_ty(db: &dyn HirDb, container: ScopeId, data_ty: DataTy) -> TyResult { +pub fn normalize_data_ty(db: &dyn HirDb, container: ArenaOwnerId, data_ty: DataTy) -> TyResult { normalize_data_ty_with_owner(db, container, data_ty, None) } fn normalize_data_ty_with_owner( db: &dyn HirDb, - container: ScopeId, + container: ArenaOwnerId, data_ty: DataTy, owner: Option, ) -> TyResult { @@ -250,7 +250,7 @@ fn type_of_expr_impl(db: &dyn HirDb, expr: InContainer) -> TyResult { match hir_expr { Expr::Ident(ident) => type_of_path_resolution_impl( db, - resolve_name(db, expr.cont_id, &ident, NameContext::Value), + resolve_name(db, expr.cont_id.into(), &ident, NameContext::Value), ), Expr::Field { receiver, field } => { let Some(field) = field else { @@ -412,7 +412,7 @@ pub fn packed_bit_width(db: &dyn HirDb, ty: &Ty) -> Option { fn normalize_data_ty_inner( db: &dyn HirDb, - container: ScopeId, + container: ArenaOwnerId, data_ty: DataTy, owner: Option, seen: &mut FxHashSet>, @@ -440,7 +440,7 @@ fn normalize_data_ty_inner( fn type_of_named_data_ty( db: &dyn HirDb, - container: ScopeId, + container: ArenaOwnerId, named: NamedDataTy, seen: &mut FxHashSet>, ) -> TyResult { @@ -451,7 +451,7 @@ fn type_of_named_data_ty( return TyResult::new(Ty::Unknown); }; - let resolution = resolve_name(db, container, &ident, NameContext::Type); + let resolution = resolve_name(db, container.into(), &ident, NameContext::Type); let Some(def_id) = resolution.unique() else { return TyResult::new(Ty::Unknown); }; @@ -538,7 +538,7 @@ fn struct_kind(db: &dyn HirDb, struct_id: InContainer) -> Option], ) -> Ty { @@ -562,21 +562,25 @@ fn apply_unpacked_dimensions( ty } -fn type_of_dimension_key(db: &dyn HirDb, container: ScopeId, expr_id: ExprId) -> Ty { +fn type_of_dimension_key(db: &dyn HirDb, container: ArenaOwnerId, expr_id: ExprId) -> Ty { if let Some(ty) = builtin_dimension_key_ty(db, container, expr_id) { return ty; } type_of_expr_impl(db, InContainer::new(container, expr_id)).ty } -fn builtin_dimension_key_ty(db: &dyn HirDb, container: ScopeId, expr_id: ExprId) -> Option { +fn builtin_dimension_key_ty( + db: &dyn HirDb, + container: ArenaOwnerId, + expr_id: ExprId, +) -> Option { if let Some(Expr::Ident(ident)) = expr_of(db, InContainer::new(container, expr_id)) { return builtin_type_name_ty(db, container, &ident); } None } -fn builtin_type_name_ty(db: &dyn HirDb, container: ScopeId, ident: &Ident) -> Option { +fn builtin_type_name_ty(db: &dyn HirDb, container: ArenaOwnerId, ident: &Ident) -> Option { let ty = match ident.as_str() { "string" => BuiltinDataTy::String, "byte" => BuiltinDataTy::Int { kind: IntKind::Byte, signing: true }, @@ -674,8 +678,8 @@ fn data_ty_of_decl(db: &dyn HirDb, decl: InContainer) -> Option } } -fn port_decl_ty(db: &dyn HirDb, cont_id: ScopeId, port_decl_id: PortDeclId) -> Option { - let ScopeId::Module(module_id) = cont_id else { +fn port_decl_ty(db: &dyn HirDb, cont_id: ArenaOwnerId, port_decl_id: PortDeclId) -> Option { + let ArenaOwnerId::Module(module_id) = cont_id else { return None; }; let module = db.module(module_id); @@ -684,7 +688,7 @@ fn port_decl_ty(db: &dyn HirDb, cont_id: ScopeId, port_decl_id: PortDeclId) -> O fn for_init_decl_ty( db: &dyn HirDb, - cont_id: ScopeId, + cont_id: ArenaOwnerId, stmt_id: crate::hir_def::stmt::StmtId, decl_id: DeclId, ) -> Option { @@ -724,7 +728,7 @@ fn int_kind_width(kind: IntKind) -> usize { } } -fn eval_const_i128(db: &dyn HirDb, container: ScopeId, expr_id: ExprId) -> Option { +fn eval_const_i128(db: &dyn HirDb, container: ArenaOwnerId, expr_id: ExprId) -> Option { match expr_of(db, InContainer::new(container, expr_id))? { Expr::Literal(Literal::Int(int)) => int.get_single_word().map(|v| v as i128), Expr::Unary { op, expr } => { @@ -762,16 +766,15 @@ fn eval_const_i128(db: &dyn HirDb, container: ScopeId, expr_id: ExprId) -> Optio fn expr_of(db: &dyn HirDb, expr: InContainer) -> Option { match expr.cont_id { - ScopeId::File(file_id) => Some(db.hir_file(file_id).get(expr.value).clone()), - ScopeId::Module(module_id) => Some(db.module(module_id).get(expr.value).clone()), - ScopeId::GenerateBlock(generate_block_id) => { + ArenaOwnerId::File(file_id) => Some(db.hir_file(file_id).get(expr.value).clone()), + ArenaOwnerId::Module(module_id) => Some(db.module(module_id).get(expr.value).clone()), + ArenaOwnerId::GenerateBlock(generate_block_id) => { Some(db.generate_block(generate_block_id).get(expr.value).clone()) } - ScopeId::Block(block_id) => Some(db.block(block_id).get(expr.value).clone()), - ScopeId::Subroutine(subroutine_id) => { - Some(db.subroutine(subroutine_id.as_in_container()).get(expr.value).clone()) + ArenaOwnerId::Block(block_id) => Some(db.block(block_id).get(expr.value).clone()), + ArenaOwnerId::Subroutine(subroutine_id) => { + Some(db.subroutine(subroutine_id).get(expr.value).clone()) } - ScopeId::ClockingBlock(_) | ScopeId::Checker(_) | ScopeId::Covergroup(_) => None, } } @@ -780,16 +783,15 @@ fn decl_of( decl: InContainer, ) -> Option { match decl.cont_id { - ScopeId::File(file_id) => Some(db.hir_file(file_id).get(decl.value).clone()), - ScopeId::Module(module_id) => Some(db.module(module_id).get(decl.value).clone()), - ScopeId::GenerateBlock(generate_block_id) => { + ArenaOwnerId::File(file_id) => Some(db.hir_file(file_id).get(decl.value).clone()), + ArenaOwnerId::Module(module_id) => Some(db.module(module_id).get(decl.value).clone()), + ArenaOwnerId::GenerateBlock(generate_block_id) => { Some(db.generate_block(generate_block_id).get(decl.value).clone()) } - ScopeId::Block(block_id) => Some(db.block(block_id).get(decl.value).clone()), - ScopeId::Subroutine(subroutine_id) => { - Some(db.subroutine(subroutine_id.as_in_container()).get(decl.value).clone()) + ArenaOwnerId::Block(block_id) => Some(db.block(block_id).get(decl.value).clone()), + ArenaOwnerId::Subroutine(subroutine_id) => { + Some(db.subroutine(subroutine_id).get(decl.value).clone()) } - ScopeId::ClockingBlock(_) | ScopeId::Checker(_) | ScopeId::Covergroup(_) => None, } } @@ -798,16 +800,15 @@ fn declaration_of( decl: InContainer, ) -> Option { match decl.cont_id { - ScopeId::File(file_id) => Some(db.hir_file(file_id).get(decl.value).clone()), - ScopeId::Module(module_id) => Some(db.module(module_id).get(decl.value).clone()), - ScopeId::GenerateBlock(generate_block_id) => { + ArenaOwnerId::File(file_id) => Some(db.hir_file(file_id).get(decl.value).clone()), + ArenaOwnerId::Module(module_id) => Some(db.module(module_id).get(decl.value).clone()), + ArenaOwnerId::GenerateBlock(generate_block_id) => { Some(db.generate_block(generate_block_id).get(decl.value).clone()) } - ScopeId::Block(block_id) => Some(db.block(block_id).get(decl.value).clone()), - ScopeId::Subroutine(subroutine_id) => { - Some(db.subroutine(subroutine_id.as_in_container()).get(decl.value).clone()) + ArenaOwnerId::Block(block_id) => Some(db.block(block_id).get(decl.value).clone()), + ArenaOwnerId::Subroutine(subroutine_id) => { + Some(db.subroutine(subroutine_id).get(decl.value).clone()) } - ScopeId::ClockingBlock(_) | ScopeId::Checker(_) | ScopeId::Covergroup(_) => None, } } @@ -816,16 +817,15 @@ fn typedef_of( typedef: InContainer, ) -> Option { match typedef.cont_id { - ScopeId::File(file_id) => Some(db.hir_file(file_id).get(typedef.value).clone()), - ScopeId::Module(module_id) => Some(db.module(module_id).get(typedef.value).clone()), - ScopeId::GenerateBlock(generate_block_id) => { + ArenaOwnerId::File(file_id) => Some(db.hir_file(file_id).get(typedef.value).clone()), + ArenaOwnerId::Module(module_id) => Some(db.module(module_id).get(typedef.value).clone()), + ArenaOwnerId::GenerateBlock(generate_block_id) => { Some(db.generate_block(generate_block_id).get(typedef.value).clone()) } - ScopeId::Block(block_id) => Some(db.block(block_id).get(typedef.value).clone()), - ScopeId::Subroutine(subroutine_id) => { - Some(db.subroutine(subroutine_id.as_in_container()).get(typedef.value).clone()) + ArenaOwnerId::Block(block_id) => Some(db.block(block_id).get(typedef.value).clone()), + ArenaOwnerId::Subroutine(subroutine_id) => { + Some(db.subroutine(subroutine_id).get(typedef.value).clone()) } - ScopeId::ClockingBlock(_) | ScopeId::Checker(_) | ScopeId::Covergroup(_) => None, } } @@ -834,16 +834,15 @@ fn struct_of( struct_id: InContainer, ) -> Option { match struct_id.cont_id { - ScopeId::File(file_id) => Some(db.hir_file(file_id).get(struct_id.value).clone()), - ScopeId::Module(module_id) => Some(db.module(module_id).get(struct_id.value).clone()), - ScopeId::GenerateBlock(generate_block_id) => { + ArenaOwnerId::File(file_id) => Some(db.hir_file(file_id).get(struct_id.value).clone()), + ArenaOwnerId::Module(module_id) => Some(db.module(module_id).get(struct_id.value).clone()), + ArenaOwnerId::GenerateBlock(generate_block_id) => { Some(db.generate_block(generate_block_id).get(struct_id.value).clone()) } - ScopeId::Block(block_id) => Some(db.block(block_id).get(struct_id.value).clone()), - ScopeId::Subroutine(subroutine_id) => { - Some(db.subroutine(subroutine_id.as_in_container()).get(struct_id.value).clone()) + ArenaOwnerId::Block(block_id) => Some(db.block(block_id).get(struct_id.value).clone()), + ArenaOwnerId::Subroutine(subroutine_id) => { + Some(db.subroutine(subroutine_id).get(struct_id.value).clone()) } - ScopeId::ClockingBlock(_) | ScopeId::Checker(_) | ScopeId::Covergroup(_) => None, } } @@ -852,16 +851,15 @@ fn stmt_of( stmt: InContainer, ) -> Option { match stmt.cont_id { - ScopeId::File(file_id) => Some(db.hir_file(file_id).get(stmt.value).clone()), - ScopeId::Module(module_id) => Some(db.module(module_id).get(stmt.value).clone()), - ScopeId::GenerateBlock(generate_block_id) => { + ArenaOwnerId::File(file_id) => Some(db.hir_file(file_id).get(stmt.value).clone()), + ArenaOwnerId::Module(module_id) => Some(db.module(module_id).get(stmt.value).clone()), + ArenaOwnerId::GenerateBlock(generate_block_id) => { Some(db.generate_block(generate_block_id).get(stmt.value).clone()) } - ScopeId::Block(block_id) => Some(db.block(block_id).get(stmt.value).clone()), - ScopeId::Subroutine(subroutine_id) => { - Some(db.subroutine(subroutine_id.as_in_container()).get(stmt.value).clone()) + ArenaOwnerId::Block(block_id) => Some(db.block(block_id).get(stmt.value).clone()), + ArenaOwnerId::Subroutine(subroutine_id) => { + Some(db.subroutine(subroutine_id).get(stmt.value).clone()) } - ScopeId::ClockingBlock(_) | ScopeId::Checker(_) | ScopeId::Covergroup(_) => None, } } @@ -1001,14 +999,14 @@ mod tests { let DefOriginLoc::Decl(decl) = def.primary_origin(db).loc(db) else { panic!("expected {name} owner to be a declaration"); }; - assert_eq!(decl.cont_id.to_container(db).get(decl.value).name.as_deref(), Some(name)); + assert_eq!(decl.cont_id.data(db).get(decl.value).name.as_deref(), Some(name)); } fn assert_owner_is_typedef(db: &TestDb, def: DefId, name: &str) { let DefOriginLoc::Typedef(typedef) = def.primary_origin(db).loc(db) else { panic!("expected {name} owner to be a typedef"); }; - assert_eq!(typedef.cont_id.to_container(db).get(typedef.value).name.as_deref(), Some(name)); + assert_eq!(typedef.cont_id.data(db).get(typedef.value).name.as_deref(), Some(name)); } #[test] diff --git a/crates/ide/src/completion/engine/expr.rs b/crates/ide/src/completion/engine/expr.rs index c45ed6bc..e6c3947e 100644 --- a/crates/ide/src/completion/engine/expr.rs +++ b/crates/ide/src/completion/engine/expr.rs @@ -1,15 +1,11 @@ use std::collections::BTreeMap; use hir::{ - container::{InContainer, ScopeId, ScopeParent}, + container::{InContainer, ScopeId, ScopeParent, SubroutineScope}, db::HirDb, def_id::DefId, file::HirFileId, - hir_def::{ - lower_ident_opt, - module::ModuleId, - subroutine::{LocalSubroutineId, SubroutineKind}, - }, + hir_def::{lower_ident_opt, module::ModuleId, subroutine::SubroutineKind}, semantics::Semantics, symbol::{DefKind, Resolution}, type_infer::{Ty, normalize_data_ty, type_class}, @@ -103,7 +99,7 @@ fn container_id_at_offset( ) -> Option { let elem = root.covering_element(utils::line_index::TextRange::empty(offset)); let node = elem.as_node().or_else(|| elem.parent())?; - sema.container_for_node(file_id, node) + sema.container_for_node(file_id, node).map(Into::into) } fn collect_container_names( @@ -121,13 +117,13 @@ fn collect_container_names( } } ScopeId::Checker(checker_id) => { - let scope = db.checker_scope(checker_id.as_in_container()); + let scope = db.checker_scope(checker_id); for (ident, defs) in scope.iter_listing() { collect_def_names(db, ident, defs, names); } } ScopeId::Covergroup(covergroup_id) => { - let scope = db.covergroup_scope(covergroup_id.as_in_container()); + let scope = db.covergroup_scope(covergroup_id); for (ident, defs) in scope.iter_listing() { collect_def_names(db, ident, defs, names); } @@ -145,7 +141,7 @@ fn collect_container_names( } } ScopeId::Subroutine(subroutine_id) => { - let scope = db.subroutine_scope(subroutine_id.as_in_container()); + let scope = db.subroutine_scope(subroutine_id); for (ident, defs) in scope.iter_listing() { collect_def_names(db, ident, defs, names); } @@ -206,7 +202,7 @@ fn collect_def_names( } } -fn subroutine_return_ty(db: &RootDb, subroutine_id: InContainer) -> Ty { +fn subroutine_return_ty(db: &RootDb, subroutine_id: SubroutineScope) -> Ty { match db.subroutine(subroutine_id).kind { SubroutineKind::Function { return_ty: Some(return_ty) } => { normalize_data_ty(db, subroutine_id.into(), return_ty).ty diff --git a/crates/ide/src/hover.rs b/crates/ide/src/hover.rs index cf844e6e..79df0a80 100644 --- a/crates/ide/src/hover.rs +++ b/crates/ide/src/hover.rs @@ -134,7 +134,7 @@ fn handle_literal( let expr = ast::Expression::cast(parent)?; let InContainer { value: expr_id, cont_id } = sema.resolve_expr(file_id, expr)?; - let container = cont_id.to_container(sema.db); + let container = cont_id.data(sema.db); let Expr::Literal(literal) = container.get(expr_id) else { return None; }; diff --git a/crates/ide/src/module_resolution.rs b/crates/ide/src/module_resolution.rs index 3d712a54..ddc233dd 100644 --- a/crates/ide/src/module_resolution.rs +++ b/crates/ide/src/module_resolution.rs @@ -5,7 +5,7 @@ use hir::{ source_db::{SourceDb, SourceRootDb}, source_root::SourceRootRole, }, - container::ScopeId, + container::ArenaOwnerId, db::HirDb, def_id::DefId, hir_def::{ @@ -171,7 +171,7 @@ pub(crate) fn resolve_named_param_in_module( let Some(decl_id) = def_id.primary_origin(db).as_decl(db) else { return false; }; - if decl_id.cont_id != ScopeId::Module(module_id) { + if decl_id.cont_id != ArenaOwnerId::Module(module_id) { return false; } let DeclaratorParent::DeclarationId(declaration_id) = module.get(decl_id.value).parent @@ -553,7 +553,7 @@ mod tests { } match def_id.primary_origin(db).loc(db) { DefOriginLoc::Decl(decl_id) => match decl_id.cont_id { - ScopeId::Module(module_id) => Some(module_id), + ArenaOwnerId::Module(module_id) => Some(module_id), _ => None, }, DefOriginLoc::NonAnsiPort(nonansi_port_id) => Some(nonansi_port_id.module_id), diff --git a/crates/ide/src/navigation_target.rs b/crates/ide/src/navigation_target.rs index af0a2d30..ab296b24 100644 --- a/crates/ide/src/navigation_target.rs +++ b/crates/ide/src/navigation_target.rs @@ -1,6 +1,6 @@ use hir::{ base_db::intern::Lookup, - container::{InContainer, InFile, InModule, InSubroutine, ScopeId}, + container::{InContainer, InFile, InModule, InSubroutine, SubroutineScope}, db::HirDb, hir_def::{ block::{BlockId, BlockLoc}, @@ -14,7 +14,7 @@ use hir::{ port::NonAnsiPortId, }, stmt::StmtId, - subroutine::{LocalSubroutineId, SubroutinePortId}, + subroutine::SubroutinePortId, typedef::TypedefId, }, source_map::{IsNamedSrc, IsSrc}, @@ -59,10 +59,7 @@ impl ToNav for DefOrigin { let focus_range = self.name_range(db).map(|range| range.value); let name = self.name(db); let kind = self.kind(db).symbol_kind().into(); - let container_name = match self.container_id(db) { - ScopeId::File(_) => None, - cont_id => cont_id.to_container(db).name().cloned(), - }; + let container_name = self.container_id(db).name(db); Some(build(file_id.file_id(), focus_range, full_range, name, kind, container_name)) } @@ -134,7 +131,7 @@ impl ToNav for BlockId { fn to_nav(&self, db: &RootDb) -> Option { let BlockLoc { cont_id, src: InFile { value: src, file_id } } = self.lookup(db); let name = self.to_container(db).name.clone(); - let cont_name = cont_id.to_container(db).name().cloned(); + let cont_name = cont_id.data(db).name().cloned(); let file_id = file_id.file_id(); Some(build(file_id, src.name_range(), src.range(), name, SymbolKind::Block, cont_name)) @@ -145,7 +142,7 @@ impl ToNav for GenerateBlockId { fn to_nav(&self, db: &RootDb) -> Option { let GenerateBlockLoc { cont_id, src: InFile { value: src, file_id } } = self.lookup(db); let name = self.to_container(db).name.clone(); - let cont_name = cont_id.to_container(db).name().cloned(); + let cont_name = cont_id.data(db).name().cloned(); Some(build( file_id.file_id(), @@ -158,7 +155,7 @@ impl ToNav for GenerateBlockId { } } -impl ToNav for InContainer { +impl ToNav for SubroutineScope { fn to_nav(&self, db: &RootDb) -> Option { DefOrigin::new(db, *self).to_nav(db) } @@ -198,9 +195,9 @@ impl ToNav for InContainer { let InContainer { value: decl_id, cont_id } = *self; let file_id = cont_id.file_id(db); - let src = cont_id.to_container_src_map(db).get(decl_id)?; + let src = cont_id.source_map(db).get(decl_id)?; - let cont = cont_id.to_container(db); + let cont = cont_id.data(db); let decl = cont.get(decl_id); let kind = match decl.parent { @@ -227,9 +224,9 @@ impl ToNav for InContainer { let InContainer { value: typedef_id, cont_id } = *self; let file_id = cont_id.file_id(db); - let src = cont_id.to_container_src_map(db).get(typedef_id)?; + let src = cont_id.source_map(db).get(typedef_id)?; - let cont = cont_id.to_container(db); + let cont = cont_id.data(db); let typedef = cont.get(typedef_id); let cont_name = cont.name().cloned(); @@ -264,9 +261,9 @@ impl ToNav for InContainer { let InContainer { value: stmt_id, cont_id } = *self; let file_id = cont_id.file_id(db); - let src = cont_id.to_container_src_map(db).get(stmt_id)?; + let src = cont_id.source_map(db).get(stmt_id)?; - let cont = cont_id.to_container(db); + let cont = cont_id.data(db); let name = cont.get(stmt_id).label.clone(); let cont_name = cont.name().cloned(); diff --git a/crates/ide/src/references/search.rs b/crates/ide/src/references/search.rs index f9011c8e..ab38479a 100644 --- a/crates/ide/src/references/search.rs +++ b/crates/ide/src/references/search.rs @@ -91,7 +91,7 @@ impl SearchScope { Self::single_range(src.file_id.file_id(), src.value.range()) } ScopeId::Subroutine(subroutine_id) => { - let def_id = DefOrigin::new(db, subroutine_id.as_in_container()); + let def_id = DefOrigin::new(db, subroutine_id); if let Some(InFile { file_id, value: range }) = def_id.range(db) { Self::single_range(file_id.file_id(), range) } else { @@ -107,7 +107,7 @@ impl SearchScope { } } ScopeId::Checker(checker_id) => { - let def_id = DefOrigin::new(db, checker_id.as_in_container()); + let def_id = DefOrigin::new(db, checker_id); if let Some(InFile { file_id, value: range }) = def_id.range(db) { Self::single_range(file_id.file_id(), range) } else { @@ -115,7 +115,7 @@ impl SearchScope { } } ScopeId::Covergroup(covergroup_id) => { - let def_id = DefOrigin::new(db, covergroup_id.as_in_container()); + let def_id = DefOrigin::new(db, covergroup_id); if let Some(InFile { file_id, value: range }) = def_id.range(db) { Self::single_range(file_id.file_id(), range) } else { diff --git a/crates/ide/src/render.rs b/crates/ide/src/render.rs index 908695b8..e6c83a98 100644 --- a/crates/ide/src/render.rs +++ b/crates/ide/src/render.rs @@ -1,6 +1,9 @@ use hir::{ base_db::source_db::{SourceDb, SourceRootDb}, - container::{InContainer, InFile, InModule, InSubroutine, ScopeId, ScopeParent}, + container::{ + ArenaOwnerId, InContainer, InFile, InModule, InSubroutine, ScopeId, ScopeParent, + SubroutineScope, + }, db::HirDb, def_id::DefId, display::HirDisplay, @@ -18,7 +21,7 @@ use hir::{ instantiation::InstanceId, port::{NonAnsiPortId, Ports}, }, - subroutine::{LocalSubroutineId, SubroutineKind, SubroutinePortId}, + subroutine::{SubroutineKind, SubroutinePortId}, }, region_tree::RegionParent, semantics::Semantics, @@ -320,7 +323,7 @@ fn render_definition_title(db: &RootDb, origin: &DefOrigin) -> Option { } fn render_decl_title_kind(db: &RootDb, decl_id: InContainer) -> Option<&'static str> { - let container = decl_id.cont_id.to_container(db); + let container = decl_id.cont_id.data(db); let decl = container.get(decl_id.value); Some(match decl.parent { @@ -421,7 +424,7 @@ fn render_subroutine_port_signature( let port = subroutine.ports.get(port_id.value.0 as usize)?; let name = port.name.as_ref()?; let container = port_id.subroutine.cont_id; - let ty = port.ty.and_then(|ty| render_data_ty(db, container, ty)); + let ty = port.ty.and_then(|ty| render_data_ty(db, container.into(), ty)); let dir = port.direction.display_source(db).ok()?; match (dir.is_empty(), ty) { @@ -432,17 +435,16 @@ fn render_subroutine_port_signature( } } -fn render_subroutine_signature( - db: &RootDb, - subroutine_id: InContainer, -) -> Option { +fn render_subroutine_signature(db: &RootDb, subroutine_id: SubroutineScope) -> Option { let subroutine = db.subroutine(subroutine_id); let name = subroutine.name.as_ref()?; let container = subroutine_id.cont_id; let mut signature = match subroutine.kind { SubroutineKind::Task => format!("task {name}"), SubroutineKind::Function { return_ty } => { - if let Some(return_ty) = return_ty.and_then(|ty| render_data_ty(db, container, ty)) { + if let Some(return_ty) = + return_ty.and_then(|ty| render_data_ty(db, container.into(), ty)) + { format!("function {return_ty} {name}") } else { format!("function {name}") @@ -567,13 +569,13 @@ fn render_clocking_block_signature( } fn render_decl_signature(db: &RootDb, decl_id: InContainer) -> Option { - let container = decl_id.cont_id.to_container(db); + let container = decl_id.cont_id.data(db); let decl = container.get(decl_id.value); decl.name.as_ref()?; match decl.parent { DeclaratorParent::PortDeclId(port_decl_id) => { - let ScopeId::Module(module_id) = decl_id.cont_id else { + let ArenaOwnerId::Module(module_id) = decl_id.cont_id else { return None; }; let module = db.module(module_id); @@ -603,7 +605,7 @@ fn render_decl_signature(db: &RootDb, decl_id: InContainer) -> Option Option { let ty = render_data_ty(db, cont_id, declaration.ty()).unwrap_or_default(); @@ -647,7 +649,7 @@ fn render_declaration_prefix( } fn render_initializer(db: &RootDb, decl_id: InContainer) -> Option { - let container = decl_id.cont_id.to_container(db); + let container = decl_id.cont_id.data(db); let decl = container.get(decl_id.value); let init = decl .initializer @@ -663,7 +665,7 @@ fn render_initializer(db: &RootDb, decl_id: InContainer) -> Option Option { +fn render_data_ty(db: &RootDb, container: ArenaOwnerId, ty: DataTy) -> Option { InContainer::new(container, ty).display_source(db).ok() } @@ -741,22 +743,19 @@ fn render_scope_fact(sema: &Semantics, origin: &DefOrigin) -> Option Option<()> { - let res = sema.resolve_name(in_cont.cont_id, &in_cont.value, NameContext::Value); + let res = sema.resolve_name(in_cont.cont_id.into(), &in_cont.value, NameContext::Value); collect_resolved_path(sema, res, range, collector) } fn collect_type_ident_like( sema: &Semantics<'_, RootDb>, - cont_id: ScopeId, + cont_id: ArenaOwnerId, expr: &Expr, range: TextRange, collector: &mut SemaTokenCollector, @@ -577,13 +577,13 @@ fn collect_type_ident_like( let Expr::Ident(name) = expr else { return None; }; - let res = sema.resolve_name(cont_id, name, NameContext::Type); + let res = sema.resolve_name(cont_id.into(), name, NameContext::Type); collect_resolved_path(sema, res, range, collector) } fn collect_field_like( sema: &Semantics<'_, RootDb>, - cont_id: ScopeId, + cont_id: ArenaOwnerId, expr_id: ExprId, src: ExprSrc, tree: &SyntaxTree, @@ -658,7 +658,7 @@ fn collect_resolved_path( match def_id.kind(db) { DefKind::Port => { let decl_id = def_id.primary_origin(db).as_decl(db)?; - let ScopeId::Module(module_id) = decl_id.cont_id else { + let ArenaOwnerId::Module(module_id) = decl_id.cont_id else { return None; }; let module = db.module(module_id); From ac86ac0c5fcd837b24b7fe805e84f5e6d97b035b Mon Sep 17 00:00:00 2001 From: Hong Jiarong Date: Sat, 25 Jul 2026 00:49:00 +0800 Subject: [PATCH 2/2] chore: clippy --- crates/hir/src/scope.rs | 2 +- crates/ide/src/render.rs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/crates/hir/src/scope.rs b/crates/hir/src/scope.rs index b876ec52..48d91daa 100644 --- a/crates/hir/src/scope.rs +++ b/crates/hir/src/scope.rs @@ -675,7 +675,7 @@ mod tests { }, source_root::{SourceRoot, SourceRootId}, }, - container::{FileOrModule, InContainer, InFile, InFileOrModule, ScopeId, SubroutineParent}, + container::{FileOrModule, InFile, InFileOrModule, ScopeId, SubroutineParent}, db::{HirDb, HirDbStorage, InternDbStorage}, def_id::DefId, file::HirFileId, diff --git a/crates/ide/src/render.rs b/crates/ide/src/render.rs index e6c83a98..4dad3f62 100644 --- a/crates/ide/src/render.rs +++ b/crates/ide/src/render.rs @@ -755,7 +755,7 @@ fn render_scope_fact(sema: &Semantics, origin: &DefOrigin) -> Option