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

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ triomphe.workspace = true
[workspace.dependencies]
hir = { path = "./crates/hir/", version = "0.0.0" }
ide = { path = "./crates/ide/", version = "0.0.0" }
proc-macro-utils = { path = "./crates/proc-macro-utils/", version = "0.0.0" }
preproc = { path = "./crates/preproc/", version = "0.0.0" }
project-model = { path = "./crates/project-model/", version = "0.0.0" }
syntax = { path = "./crates/syntax/", version = "0.0.0" }
Expand Down
1 change: 0 additions & 1 deletion crates/hir/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ hashbrown = { version = "0.12.3", features = ["inline-more"], default-features =
itertools.workspace = true
la-arena.workspace = true
preproc.workspace = true
proc-macro-utils.workspace = true
rustc-hash.workspace = true
salsa.workspace = true
smallvec.workspace = true
Expand Down
326 changes: 278 additions & 48 deletions crates/hir/src/container.rs

Large diffs are not rendered by default.

30 changes: 17 additions & 13 deletions crates/hir/src/def_id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -182,12 +182,12 @@ impl DefOrigin {
DefOriginLoc::NonAnsiPort(_) => DefKind::NonAnsiPort,
DefOriginLoc::Decl(InContainer { value, cont_id }) => {
let container = cont_id.data(db);
let decl = container.get(value);
let decl = container.declarator(value);
match decl.parent {
DeclaratorParent::PortDeclId(_) => DefKind::Port,
DeclaratorParent::StmtId(_) => DefKind::Variable,
DeclaratorParent::DeclarationId(declaration_id) => {
match container.get(declaration_id) {
match container.declaration(declaration_id) {
Declaration::DataDecl(_) => DefKind::Variable,
Declaration::NetDecl(_) => DefKind::Net,
Declaration::ParamDecl(_) => DefKind::Param,
Expand Down Expand Up @@ -228,7 +228,8 @@ impl DefOrigin {
DefOriginLoc::Block(block_id) => {
let BlockLoc { cont_id, src: InFile { value, file_id: _ } } = block_id.lookup(db);
let cont = cont_id.data(db);
value.hir(&cont, &cont_id.source_map(db))?.name.clone()
let source_map = cont_id.source_map(db);
cont.block_info(source_map.block_from_source(value)?).name.clone()
}
DefOriginLoc::GenerateBlock(generate_block_id) => {
db.generate_block(generate_block_id).name.clone()
Expand All @@ -241,10 +242,10 @@ impl DefOrigin {
module_id.to_container(db).get(value).label.clone()
}
DefOriginLoc::Decl(InContainer { value, cont_id }) => {
cont_id.data(db).get(value).name.clone()
cont_id.data(db).declarator(value).name.clone()
}
DefOriginLoc::Typedef(InContainer { value, cont_id }) => {
cont_id.data(db).get(value).name.clone()
cont_id.data(db).typedef(value).name.clone()
}
DefOriginLoc::Instance(InModule { value, module_id }) => {
module_id.to_container(db).get(value).name.clone()
Expand Down Expand Up @@ -276,7 +277,7 @@ impl DefOrigin {
}
DefOriginLoc::Cross(cross) => cross_of(db, cross).and_then(|(cross, _)| cross.name),
DefOriginLoc::Stmt(InContainer { value, cont_id }) => {
cont_id.data(db).get(value).label.clone()
cont_id.data(db).stmt(value).label.clone()
}
}
}
Expand Down Expand Up @@ -336,11 +337,11 @@ impl DefOrigin {
Some(InFile::new(module_id.file_id, range))
}
DefOriginLoc::Decl(InContainer { value, cont_id }) => {
let range = cont_id.source_map(db).get(value)?.name_range()?;
let range = cont_id.source_map(db).source_of_declarator(value)?.name_range()?;
Some(InFile::new(cont_id.file_id(db), range))
}
DefOriginLoc::Typedef(InContainer { value, cont_id }) => {
let range = cont_id.source_map(db).get(value)?.name_range()?;
let range = cont_id.source_map(db).source_of_typedef(value)?.name_range()?;
Some(InFile::new(cont_id.file_id(db), range))
}
DefOriginLoc::Instance(InModule { value, module_id }) => {
Expand Down Expand Up @@ -420,7 +421,7 @@ impl DefOrigin {
}
}
DefOriginLoc::Stmt(InContainer { value, cont_id }) => {
let range = cont_id.source_map(db).get(value)?.name_range()?;
let range = cont_id.source_map(db).source_of_stmt(value)?.name_range()?;
Some(InFile::new(cont_id.file_id(db), range))
}
}
Expand Down Expand Up @@ -478,11 +479,11 @@ impl DefOrigin {
InFile::new(module_id.file_id, range)
}
DefOriginLoc::Decl(InContainer { value, cont_id }) => {
let range = cont_id.source_map(db).get(value)?.range();
let range = cont_id.source_map(db).source_of_declarator(value)?.range();
InFile::new(cont_id.file_id(db), range)
}
DefOriginLoc::Typedef(InContainer { value, cont_id }) => {
let range = cont_id.source_map(db).get(value)?.range();
let range = cont_id.source_map(db).source_of_typedef(value)?.range();
InFile::new(cont_id.file_id(db), range)
}
DefOriginLoc::Instance(InModule { value, module_id }) => {
Expand Down Expand Up @@ -555,7 +556,7 @@ impl DefOrigin {
}
}
DefOriginLoc::Stmt(InContainer { value, cont_id }) => {
let range = cont_id.source_map(db).get(value)?.range();
let range = cont_id.source_map(db).source_of_stmt(value)?.range();
InFile::new(cont_id.file_id(db), range)
}
})
Expand Down Expand Up @@ -722,5 +723,8 @@ 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.data(db).get(decl_id.value).parent, DeclaratorParent::PortDeclId(_))
matches!(
decl_id.cont_id.data(db).declarator(decl_id.value).parent,
DeclaratorParent::PortDeclId(_)
)
}
13 changes: 6 additions & 7 deletions crates/hir/src/display.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ use std::fmt::{self, Debug};

use syntax::TimeUnit;
use triomphe::Arc;
use utils::get::GetRef;

use crate::{
base_db::intern::Lookup,
Expand Down Expand Up @@ -117,7 +116,7 @@ impl HirDisplay for Ty {
Ty::Chandle => f.write_str("chandle"),
Ty::Alias { typedef, target } => {
let container = typedef.cont_id.data(f.db);
if let Some(name) = &container.get(typedef.value).name {
if let Some(name) = &container.typedef(typedef.value).name {
f.write_str(name)
} else {
target.hir_fmt(f)
Expand Down Expand Up @@ -170,7 +169,7 @@ fn hir_fmt_def_backed_type(
f.write_str(keyword)?;
if let DefOriginLoc::Typedef(typedef) = def.primary_origin(f.db).loc(f.db) {
let container = typedef.cont_id.data(f.db);
if let Some(name) = &container.get(typedef.value).name {
if let Some(name) = &container.typedef(typedef.value).name {
f.write_str(" ")?;
f.write_str(name)?;
}
Expand Down Expand Up @@ -304,7 +303,7 @@ impl HirDisplay for InContainer<DataTy> {
DataTy::Enum => f.write_str("enum"),
DataTy::Struct(struct_ref) => {
let cont = struct_ref.cont_id.data(f.db);
let def = cont.get(struct_ref.value);
let def = cont.struct_def(struct_ref.value);
let keyword = match def.kind {
StructKind::Struct => "struct",
StructKind::Union => "union",
Expand Down Expand Up @@ -370,7 +369,7 @@ impl HirDisplay for InContainer<ExprId> {
fn hir_fmt(&self, f: &mut HirFormatter<'_>) -> Result<(), HirDisplayError> {
let InContainer { cont_id, value: expr_id } = self;
let container = cont_id.data(f.db);
let expr = container.get(*expr_id);
let expr = container.expr(*expr_id);
self.with_value(expr).hir_fmt(f)
}
}
Expand Down Expand Up @@ -676,7 +675,7 @@ impl HirDisplay for InContainer<DeclId> {
fn hir_fmt(&self, f: &mut HirFormatter<'_>) -> Result<(), HirDisplayError> {
let InContainer { cont_id, value: decl_id } = self;
let container = cont_id.data(f.db);
let decl = container.get(*decl_id);
let decl = container.declarator(*decl_id);

if let Some(name) = &decl.name {
f.write_str(name)?;
Expand All @@ -694,7 +693,7 @@ impl HirDisplay for InContainer<TypedefId> {
fn hir_fmt(&self, f: &mut HirFormatter<'_>) -> Result<(), HirDisplayError> {
let InContainer { cont_id, value: typedef_id } = self;
let container = cont_id.data(f.db);
let typedef = container.get(*typedef_id);
let typedef = container.typedef(*typedef_id);

f.write_str("typedef ")?;
if let Some(ty) = typedef.ty {
Expand Down
95 changes: 79 additions & 16 deletions crates/hir/src/hir_def.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ pub mod declaration;
pub mod expr;
pub mod file;
pub mod literal;
pub(crate) mod lower;
pub mod macro_file;
pub mod module;
pub mod proc;
Expand All @@ -14,7 +15,45 @@ pub mod subroutine;
pub mod ty;
pub mod typedef;

use la_arena::{Arena, Idx, RawIdx};
pub(crate) macro impl_arena_getters(
$container:ty;
$($id:ty => $field:ident => $output:ty),* $(,)?
) {
$(
impl utils::get::GetRef<$id> for $container {
type Output = $output;

fn get(&self, id: $id) -> &Self::Output {
utils::get::GetRef::get(&self.$field, id)
}
}
)*
}

pub(crate) macro impl_source_map_getters(
$container:ty;
$($src:ty => $id:ty => $field:ident),* $(,)?
) {
$(
impl utils::get::Get<$src> for $container {
type Output = Option<$id>;

fn get(&self, src: $src) -> Self::Output {
utils::get::Get::get(&self.$field, src)
}
}

impl utils::get::Get<$id> for $container {
type Output = Option<$src>;

fn get(&self, id: $id) -> Self::Output {
utils::get::Get::get(&self.$field, id)
}
}
)*
}

use la_arena::{Arena, Idx};
use smol_str::{SmolStr, ToSmolStr};
use syntax::{SyntaxToken, TokenKind, ast};

Expand Down Expand Up @@ -63,24 +102,48 @@ pub(crate) fn lower_package_imports(
.collect()
}

macro alloc_idx_and_src($file_id:expr; $hir:expr => $arena:expr, $ast:expr => $src_map:expr $(,)?) {{
let idx = $arena.alloc($hir.into());
// 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($file_id, $ast) {
let src = $crate::source_map::FromSourceAst::from_source_ast(ast);
$src_map.insert(src, idx);
pub(crate) fn alloc_with_optional_source_entry<Src, Input, Hir>(
data: &mut Arena<Hir>,
sources: &mut crate::source_map::SourceMap<Src, Hir>,
value: Input,
source: Option<Src>,
) -> Idx<Hir>
where
Input: Into<Hir>,
Src: crate::source_map::IsSrc,
{
let idx = data.alloc(value.into());
if let Some(source) = source {
sources.insert(source, idx);
}
idx
}}
}

trait HirData<T> {
fn nxt_idx(&self) -> Idx<T>;
pub(crate) fn alloc_with_source_entry<Src, Input, Hir>(
data: &mut Arena<Hir>,
sources: &mut crate::source_map::SourceMap<Src, Hir>,
value: Input,
source: Src,
) -> Idx<Hir>
where
Input: Into<Hir>,
Src: crate::source_map::IsSrc,
{
alloc_with_optional_source_entry(data, sources, value, Some(source))
}

impl<T> HirData<T> for Arena<T> {
#[inline]
fn nxt_idx(&self) -> Idx<T> {
Idx::from_raw(RawIdx::from(self.len() as u32))
}
pub(crate) fn alloc_with_source<'ast, Ast, Input, Hir, Src>(
file_id: crate::file::HirFileId,
data: &mut Arena<Hir>,
sources: &mut crate::source_map::SourceMap<Src, Hir>,
value: Input,
ast: Ast,
) -> Idx<Hir>
where
Ast: syntax::ast::AstNode<'ast>,
Input: Into<Hir>,
Src: crate::source_map::FromSourceAst<'ast, Ast> + crate::source_map::IsSrc,
{
let source = crate::source_map::SourceAst::new(file_id, ast).map(Src::from_source_ast);
alloc_with_optional_source_entry(data, sources, value, source)
}
Loading
Loading