Skip to content
Draft
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
61 changes: 61 additions & 0 deletions crates/hir/src/container.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ use crate::{
typedef::{Typedef, TypedefId, TypedefSrc},
},
region_tree::RegionTree,
source_map::SourcePresentation,
};

define_enum_deriving_from! {
Expand Down Expand Up @@ -277,6 +278,66 @@ impl ContainerSrcMap {
ContainerSrcMap::SubroutineSourceMap(subroutine) => Some(&subroutine.region_tree),
}
}

pub fn decl_presentation(&self, decl_id: DeclId) -> Option<&SourcePresentation> {
match self {
ContainerSrcMap::FileSourceMap(src_map) => {
src_map.decl_srcs.hir_to_presentation(decl_id)
}
ContainerSrcMap::ModuleSourceMap(src_map) => {
src_map.decl_srcs.hir_to_presentation(decl_id)
}
ContainerSrcMap::GenerateBlockSourceMap(src_map) => {
src_map.decl_srcs.hir_to_presentation(decl_id)
}
ContainerSrcMap::BlockSourceMap(src_map) => {
src_map.decl_srcs.hir_to_presentation(decl_id)
}
ContainerSrcMap::SubroutineSourceMap(src_map) => {
src_map.decl_srcs.hir_to_presentation(decl_id)
}
}
}

pub fn typedef_presentation(&self, typedef_id: TypedefId) -> Option<&SourcePresentation> {
match self {
ContainerSrcMap::FileSourceMap(src_map) => {
src_map.typedef_srcs.hir_to_presentation(typedef_id)
}
ContainerSrcMap::ModuleSourceMap(src_map) => {
src_map.typedef_srcs.hir_to_presentation(typedef_id)
}
ContainerSrcMap::GenerateBlockSourceMap(src_map) => {
src_map.typedef_srcs.hir_to_presentation(typedef_id)
}
ContainerSrcMap::BlockSourceMap(src_map) => {
src_map.typedef_srcs.hir_to_presentation(typedef_id)
}
ContainerSrcMap::SubroutineSourceMap(src_map) => {
src_map.typedef_srcs.hir_to_presentation(typedef_id)
}
}
}

pub fn stmt_presentation(&self, stmt_id: StmtId) -> Option<&SourcePresentation> {
match self {
ContainerSrcMap::FileSourceMap(src_map) => {
src_map.stmt_srcs.hir_to_presentation(stmt_id)
}
ContainerSrcMap::ModuleSourceMap(src_map) => {
src_map.stmt_srcs.hir_to_presentation(stmt_id)
}
ContainerSrcMap::GenerateBlockSourceMap(src_map) => {
src_map.stmt_srcs.hir_to_presentation(stmt_id)
}
ContainerSrcMap::BlockSourceMap(src_map) => {
src_map.stmt_srcs.hir_to_presentation(stmt_id)
}
ContainerSrcMap::SubroutineSourceMap(src_map) => {
src_map.stmt_srcs.hir_to_presentation(stmt_id)
}
}
}
}

impl AsRef<ContainerSrcMap> for ContainerSrcMap {
Expand Down
5 changes: 3 additions & 2 deletions crates/hir/src/hir_def.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,9 @@ macro alloc_idx_and_src($hir:expr => $arena:expr, $ast:expr => $src_map:expr $(,
// HIR lowering can consume include-expanded AST nodes, but source maps only
// store locations that can be navigated in the parsed root file.
if let Some(ast) = $crate::source_map::SourceAst::new($ast) {
let src = $crate::source_map::FromSourceAst::from_source_ast(ast);
$src_map.insert(src, idx);
let (src, presentation) =
$crate::source_map::FromSourceAst::from_source_ast_with_presentation(ast);
$src_map.insert_with_presentation(src, idx, presentation);
}
idx
}}
Expand Down
20 changes: 15 additions & 5 deletions crates/hir/src/hir_def/aggregate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ use utils::text_edit::TextRange;
use super::{Ident, expr::data_ty::DataTy, lower_ident_opt};
use crate::{
container::{ContainerId, InContainer},
source_map::{FromSourceAst, IsNamedSrc, IsSrc, SourceAst, ToAstNode, root_token_in},
source_map::{
FromSourceAst, IsNamedSrc, IsSrc, SourceAst, SourcePresentation, ToAstNode, root_token_in,
},
};

#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
Expand Down Expand Up @@ -85,7 +87,7 @@ impl IsSrc for StructSrc {
}

#[inline]
fn range(&self) -> TextRange {
fn expanded_range(&self) -> TextRange {
self.node.range()
}
}
Expand All @@ -97,7 +99,7 @@ impl IsNamedSrc for StructSrc {
}

#[inline]
fn name_range(&self) -> Option<TextRange> {
fn expanded_name_range(&self) -> Option<TextRange> {
self.name.map(|name| name.range())
}
}
Expand Down Expand Up @@ -128,6 +130,10 @@ impl<'a> FromSourceAst<'a, ast::StructUnionType<'a>> for StructSrc {
.and_then(|name| root_token_in(syntax, name).map(SyntaxTokenPtr::from_token));
StructSrc { node: AstNodeExt::to_ptr(&node), name }
}

fn source_presentation(node: &SourceAst<ast::StructUnionType<'a>>) -> SourcePresentation {
SourcePresentation::from_node_and_name(node.ast.syntax(), struct_name_token(node.ast))
}
}

fn struct_name_token(node: ast::StructUnionType<'_>) -> Option<syntax::SyntaxToken<'_>> {
Expand Down Expand Up @@ -173,7 +179,7 @@ impl IsSrc for ClassSrc {
}

#[inline]
fn range(&self) -> TextRange {
fn expanded_range(&self) -> TextRange {
self.node.range()
}
}
Expand All @@ -185,7 +191,7 @@ impl IsNamedSrc for ClassSrc {
}

#[inline]
fn name_range(&self) -> Option<TextRange> {
fn expanded_name_range(&self) -> Option<TextRange> {
self.name.map(|name| name.range())
}
}
Expand Down Expand Up @@ -217,4 +223,8 @@ impl<'a> FromSourceAst<'a, ast::ClassDeclaration<'a>> for ClassSrc {
.and_then(|name| root_token_in(syntax, name).map(SyntaxTokenPtr::from_token));
ClassSrc { node: AstNodeExt::to_ptr(&node), name }
}

fn source_presentation(node: &SourceAst<ast::ClassDeclaration<'a>>) -> SourcePresentation {
SourcePresentation::from_node_and_name(node.ast.syntax(), node.ast.name())
}
}
10 changes: 5 additions & 5 deletions crates/hir/src/hir_def/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -152,18 +152,18 @@ pub(crate) fn find_local_block_id(
}

let block_kind = block_src.kind();
let block_range = block_src.range();
let block_name_range = block_src.name_range();
let block_range = block_src.expanded_range();
let block_name_range = block_src.expanded_name_range();
let (stmt_id, _) = stmt_srcs
.iter()
.find(|(_, stmt_src)| {
stmt_src.kind() == block_kind
&& stmt_src.range() == block_range
&& stmt_src.name_range() == block_name_range
&& stmt_src.expanded_range() == block_range
&& stmt_src.expanded_name_range() == block_name_range
})
.or_else(|| {
stmt_srcs.iter().find(|(_, stmt_src)| {
stmt_src.kind() == block_kind && stmt_src.range() == block_range
stmt_src.kind() == block_kind && stmt_src.expanded_range() == block_range
})
})?;
Some(LocalBlockId(stmt_id))
Expand Down
8 changes: 4 additions & 4 deletions crates/hir/src/hir_def/module/generate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ impl IsSrc for GenerateRegionSrc {
self.ptr().kind()
}

fn range(&self) -> utils::text_edit::TextRange {
fn expanded_range(&self) -> utils::text_edit::TextRange {
self.ptr().range()
}
}
Expand All @@ -102,7 +102,7 @@ impl IsNamedSrc for GenerateRegionSrc {
None
}

fn name_range(&self) -> Option<utils::text_edit::TextRange> {
fn expanded_name_range(&self) -> Option<utils::text_edit::TextRange> {
None
}
}
Expand Down Expand Up @@ -184,7 +184,7 @@ impl IsSrc for GenerateBlockSrc {
self.node().kind()
}

fn range(&self) -> utils::text_edit::TextRange {
fn expanded_range(&self) -> utils::text_edit::TextRange {
self.node().range()
}
}
Expand All @@ -194,7 +194,7 @@ impl IsNamedSrc for GenerateBlockSrc {
self.name().map(|name| name.kind())
}

fn name_range(&self) -> Option<utils::text_edit::TextRange> {
fn expanded_name_range(&self) -> Option<utils::text_edit::TextRange> {
self.name().map(|name| name.range())
}
}
Expand Down
16 changes: 15 additions & 1 deletion crates/hir/src/hir_def/module/port.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ use crate::{
module::LowerModuleCtx,
ty::{NetType, lower_net_kind},
},
source_map::SourceMap,
source_map::{SourceMap, SourcePresentation},
};

// structure:
Expand Down Expand Up @@ -189,6 +189,20 @@ impl PortSrcs {
}
}
}

pub fn ref_presentation(&self, port_ref_id: PortRefId) -> Option<&SourcePresentation> {
match self {
PortSrcs::NonAnsi { refs, .. } => refs.hir_to_presentation(port_ref_id),
PortSrcs::Ansi { .. } => None,
}
}

pub fn port_presentation(&self, port_id: NonAnsiPortId) -> Option<&SourcePresentation> {
match self {
PortSrcs::NonAnsi { ports, .. } => ports.hir_to_presentation(port_id),
PortSrcs::Ansi { .. } => None,
}
}
}

define_src!(PortListSrc(ast::NonAnsiPortList, ast::AnsiPortList, ast::WildcardPortList));
Expand Down
2 changes: 1 addition & 1 deletion crates/hir/src/hir_def/module/specify.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ impl IsNamedSrc for SpecifyBlockSrc {
None
}

fn name_range(&self) -> Option<utils::text_edit::TextRange> {
fn expanded_name_range(&self) -> Option<utils::text_edit::TextRange> {
None
}
}
Expand Down
12 changes: 9 additions & 3 deletions crates/hir/src/hir_def/typedef.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ use utils::text_edit::TextRange;
use super::{Ident, aggregate::StructId, expr::data_ty::DataTy};
use crate::{
container::{ContainerId, InContainer},
source_map::{FromSourceAst, IsNamedSrc, IsSrc, SourceAst, ToAstNode, root_token_in},
source_map::{
FromSourceAst, IsNamedSrc, IsSrc, SourceAst, SourcePresentation, ToAstNode, root_token_in,
},
};

#[derive(Debug, PartialEq, Eq, Clone)]
Expand All @@ -34,7 +36,7 @@ impl IsSrc for TypedefSrc {
}

#[inline]
fn range(&self) -> TextRange {
fn expanded_range(&self) -> TextRange {
self.node.range()
}
}
Expand All @@ -46,7 +48,7 @@ impl IsNamedSrc for TypedefSrc {
}

#[inline]
fn name_range(&self) -> Option<TextRange> {
fn expanded_name_range(&self) -> Option<TextRange> {
self.name.map(|name| name.range())
}
}
Expand Down Expand Up @@ -81,6 +83,10 @@ impl<'a> FromSourceAst<'a, ast::TypedefDeclaration<'a>> for TypedefSrc {
.and_then(|name| root_token_in(syntax, name).map(SyntaxTokenPtr::from_token));
TypedefSrc { node: AstNodeExt::to_ptr(&node), name }
}

fn source_presentation(node: &SourceAst<ast::TypedefDeclaration<'a>>) -> SourcePresentation {
SourcePresentation::from_node_and_name(node.ast.syntax(), node.ast.name())
}
}

impl TypedefSrc {
Expand Down
5 changes: 3 additions & 2 deletions crates/hir/src/preproc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,15 @@ mod expansion;
mod helpers;
mod includes;
mod predefines;
mod presentation;
mod reference_index;
mod reference_queries;
mod types;

use self::helpers::*;
pub use self::{
conditionals::*, definitions::*, expansion::*, includes::*, reference_index::*,
reference_queries::*, types::*,
conditionals::*, definitions::*, expansion::*, includes::*, presentation::*,
reference_index::*, reference_queries::*, types::*,
};

#[cfg(test)]
Expand Down
Loading
Loading