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

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 19 additions & 2 deletions crates/hir/src/scope.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use rustc_hash::FxHashMap;
use smol_str::SmolStr;
use triomphe::Arc;
use utils::{
define_enum_deriving_from,
Expand All @@ -15,10 +16,10 @@ use crate::{
expr::declarator::{DeclId, DeclaratorParent},
file::{config::ConfigDeclId, library::LibraryDeclId, udp::UdpDeclId},
module::{
ModuleId,
Module, ModuleId,
generate::GenerateBlockId,
instantiation::InstanceId,
port::{NonAnsiPortId, Ports},
port::{NonAnsiPortId, PortDeclId, Ports},
},
stmt::{StmtId, StmtKind},
subroutine::{SubroutineId, SubroutineLoc, SubroutinePortId},
Expand Down Expand Up @@ -279,6 +280,22 @@ impl ModuleScope {

Arc::new(scope)
}

pub fn non_ansi_port_decl_id_by_name(
&self,
module: &Module,
name: &SmolStr,
) -> Option<PortDeclId> {
Comment on lines +284 to +288
let ModuleEntry::NonAnsiPortEntry(NonAnsiPortEntry { port_decl, .. }) = self.get(name)?
else {
return None;
};
let decl = module.get(port_decl?);
let DeclaratorParent::PortDeclId(port_decl_id) = decl.parent else {
return None;
};
Some(port_decl_id)
}
}

impl GenerateBlockScope {
Expand Down
32 changes: 32 additions & 0 deletions crates/ide/src/definitions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -589,4 +589,36 @@ mod tests {
assert_eq!(range.file_id.file_id(), file_id);
assert_eq!(range.value, TextRange::new(TextSize::from(9), TextSize::from(10)));
}

#[test]
fn named_port_connection_name_resolves_to_target_port() {
let text = "module child(input clk); endmodule\n\
module top; logic clk; child u(.clk(clk)); endmodule";
let (host, file_id) = host_with_file(text);
let db = host.raw_db();
let sema = Semantics::<RootDb>::new(db);
let parsed_file = sema.parse_file(file_id);
let file = parsed_file.compilation_unit().unwrap();
let conn_name_offset = TextSize::from(text.rfind(".clk").unwrap() as u32 + 2);
let token = file
.syntax()
.token_at_offset(conn_name_offset)
.pick_bext_token(crate::goto_definition::token_precedence)
.unwrap();
let DefinitionClass::Definition(def) =
DefinitionClass::resolve(&sema, file_id.into(), token).unwrap()
else {
panic!("expected plain definition");
};

let PathResolution::AnsiPort(port) = def.0 else {
panic!("expected ANSI port resolution");
};
let range = DefinitionOrigin::Decl(port.into())
.name_range(db)
.expect("ANSI port should have a name range");
assert_eq!(range.file_id.file_id(), file_id);
assert_eq!(&text[usize::from(range.value.start())..usize::from(range.value.end())], "clk");
assert!(range.value.start() < conn_name_offset);
}
}
Loading