Skip to content

Commit 158cd5f

Browse files
committed
feat(hir): resolve checker and covergroup members
1 parent b58a007 commit 158cd5f

14 files changed

Lines changed: 833 additions & 148 deletions

File tree

crates/hir/src/container.rs

Lines changed: 102 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ use crate::{
1111
hir_def::{
1212
aggregate::{StructDef, StructId, StructSrc},
1313
block::{Block, BlockId, BlockInfo, BlockSourceMap, BlockSrc, LocalBlockId},
14+
checker::CheckerId,
15+
covergroup::CovergroupId,
1416
declaration::{Declaration, DeclarationId, DeclarationSrc},
1517
expr::{
1618
Expr, ExprId, ExprSrc,
@@ -40,6 +42,83 @@ define_enum_deriving_from! {
4042
Block(BlockId),
4143
Subroutine(SubroutineScope),
4244
ClockingBlock(InModule<ClockingBlockId>),
45+
Checker(InFileOrModule<CheckerId>),
46+
Covergroup(InFileOrModule<CovergroupId>),
47+
}
48+
}
49+
50+
#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Clone, Copy, Hash)]
51+
pub struct InFileOrModule<T> {
52+
pub value: T,
53+
pub cont_id: FileOrModule,
54+
}
55+
56+
#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Clone, Copy, Hash)]
57+
pub enum FileOrModule {
58+
File(HirFileId),
59+
Module(ModuleId),
60+
}
61+
62+
impl<T> InFileOrModule<T> {
63+
pub fn new(cont_id: FileOrModule, value: T) -> Self {
64+
Self { value, cont_id }
65+
}
66+
67+
pub fn parent_scope(&self) -> ScopeId {
68+
self.cont_id.into()
69+
}
70+
71+
pub fn as_in_container(self) -> InContainer<T> {
72+
InContainer::new(self.cont_id.into(), self.value)
73+
}
74+
}
75+
76+
impl FileOrModule {
77+
pub fn file_id(self) -> FileId {
78+
match self {
79+
FileOrModule::File(file_id) => file_id.file_id(),
80+
FileOrModule::Module(module_id) => module_id.file_id(),
81+
}
82+
}
83+
}
84+
85+
impl From<FileOrModule> for ScopeId {
86+
fn from(cont_id: FileOrModule) -> Self {
87+
match cont_id {
88+
FileOrModule::File(file_id) => file_id.into(),
89+
FileOrModule::Module(module_id) => module_id.into(),
90+
}
91+
}
92+
}
93+
94+
impl TryFrom<ScopeId> for FileOrModule {
95+
type Error = ();
96+
97+
fn try_from(cont_id: ScopeId) -> Result<Self, Self::Error> {
98+
match cont_id {
99+
ScopeId::File(file_id) => Ok(Self::File(file_id)),
100+
ScopeId::Module(module_id) => Ok(Self::Module(module_id)),
101+
ScopeId::GenerateBlock(_)
102+
| ScopeId::Block(_)
103+
| ScopeId::Subroutine(_)
104+
| ScopeId::ClockingBlock(_)
105+
| ScopeId::Checker(_)
106+
| ScopeId::Covergroup(_) => Err(()),
107+
}
108+
}
109+
}
110+
111+
impl<T> TryFrom<InContainer<T>> for InFileOrModule<T> {
112+
type Error = ();
113+
114+
fn try_from(item: InContainer<T>) -> Result<Self, Self::Error> {
115+
Ok(Self::new(FileOrModule::try_from(item.cont_id)?, item.value))
116+
}
117+
}
118+
119+
impl<T> From<InFileOrModule<T>> for InContainer<T> {
120+
fn from(item: InFileOrModule<T>) -> Self {
121+
item.as_in_container()
43122
}
44123
}
45124

@@ -96,7 +175,11 @@ impl TryFrom<ScopeId> for SubroutineParent {
96175
ScopeId::File(file_id) => Ok(Self::File(file_id)),
97176
ScopeId::Module(module_id) => Ok(Self::Module(module_id)),
98177
ScopeId::GenerateBlock(generate_block_id) => Ok(Self::GenerateBlock(generate_block_id)),
99-
ScopeId::Block(_) | ScopeId::Subroutine(_) | ScopeId::ClockingBlock(_) => Err(()),
178+
ScopeId::Block(_)
179+
| ScopeId::Subroutine(_)
180+
| ScopeId::ClockingBlock(_)
181+
| ScopeId::Checker(_)
182+
| ScopeId::Covergroup(_) => Err(()),
100183
}
101184
}
102185
}
@@ -205,6 +288,8 @@ impl ScopeId {
205288
ScopeId::Block(_) => ScopeKind::Block,
206289
ScopeId::Subroutine(_) => ScopeKind::Subroutine,
207290
ScopeId::ClockingBlock(_) => ScopeKind::ClockingBlock,
291+
ScopeId::Checker(_) => ScopeKind::Checker,
292+
ScopeId::Covergroup(_) => ScopeKind::Covergroup,
208293
}
209294
}
210295

@@ -216,6 +301,8 @@ impl ScopeId {
216301
ScopeId::Block(block_id) => block_id.file_id(db),
217302
ScopeId::Subroutine(subroutine) => subroutine.file_id(db),
218303
ScopeId::ClockingBlock(clocking_block) => clocking_block.module_id.file_id(),
304+
ScopeId::Checker(checker) => checker.cont_id.file_id(),
305+
ScopeId::Covergroup(covergroup) => covergroup.cont_id.file_id(),
219306
}
220307
}
221308

@@ -229,6 +316,12 @@ impl ScopeId {
229316
ScopeId::ClockingBlock(_) => {
230317
panic!("clocking block scopes do not expose a generic HIR container")
231318
}
319+
ScopeId::Checker(_) => {
320+
panic!("checker scopes do not expose a generic HIR container")
321+
}
322+
ScopeId::Covergroup(_) => {
323+
panic!("covergroup scopes do not expose a generic HIR container")
324+
}
232325
}
233326
}
234327

@@ -246,6 +339,12 @@ impl ScopeId {
246339
ScopeId::ClockingBlock(_) => {
247340
panic!("clocking block scopes do not expose a generic source map")
248341
}
342+
ScopeId::Checker(_) => {
343+
panic!("checker scopes do not expose a generic source map")
344+
}
345+
ScopeId::Covergroup(_) => {
346+
panic!("covergroup scopes do not expose a generic source map")
347+
}
249348
}
250349
}
251350
}
@@ -396,6 +495,8 @@ impl Iterator for ScopeParent<'_> {
396495
ScopeId::Block(block_id) => Some(block_id.lookup(self.db).cont_id),
397496
ScopeId::Subroutine(subroutine) => Some(subroutine.parent_scope()),
398497
ScopeId::ClockingBlock(clocking_block) => Some(clocking_block.module_id.into()),
498+
ScopeId::Checker(checker) => Some(checker.parent_scope()),
499+
ScopeId::Covergroup(covergroup) => Some(covergroup.parent_scope()),
399500
};
400501
next
401502
}

crates/hir/src/db.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ use crate::{
88
file::HirFileId,
99
hir_def::{
1010
block::{self, Block, BlockId, BlockLoc, BlockSourceMap},
11+
checker::CheckerId,
12+
covergroup::CovergroupId,
1113
expr::{
1214
ExprId,
1315
data_ty::{BuiltinDataTy, BuiltinDataTyId},
@@ -124,6 +126,12 @@ pub trait HirDb: InternDb {
124126
#[salsa::invoke(NameScope::clocking_block_scope_query)]
125127
fn clocking_block_scope(&self, clocking_block_id: InModule<ClockingBlockId>) -> Arc<NameScope>;
126128

129+
#[salsa::invoke(NameScope::checker_scope_query)]
130+
fn checker_scope(&self, checker_id: InContainer<CheckerId>) -> Arc<NameScope>;
131+
132+
#[salsa::invoke(NameScope::covergroup_scope_query)]
133+
fn covergroup_scope(&self, covergroup_id: InContainer<CovergroupId>) -> Arc<NameScope>;
134+
127135
#[salsa::invoke(NameScope::generate_block_scope_query)]
128136
fn generate_block_scope(&self, generate_block_id: GenerateBlockId) -> Arc<NameScope>;
129137

0 commit comments

Comments
 (0)