Skip to content

Commit

Permalink
Cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
Veykril committed Jan 15, 2024
1 parent 1669344 commit 180e9b2
Show file tree
Hide file tree
Showing 17 changed files with 175 additions and 168 deletions.
13 changes: 8 additions & 5 deletions crates/hir-def/src/attr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ use triomphe::Arc;

use crate::{
db::DefDatabase,
item_tree::{AttrOwner, Fields, ItemTreeId, ItemTreeNode},
item_tree::{AttrOwner, Fields, ItemTreeId, ItemTreeModItemNode},
lang_item::LangItem,
nameres::{ModuleOrigin, ModuleSource},
src::{HasChildSource, HasSource},
Expand Down Expand Up @@ -82,7 +82,7 @@ impl Attrs {
let (fields, item_tree, krate) = match v {
VariantId::EnumVariantId(it) => {
let loc = it.lookup(db);
let krate = loc.container.krate;
let krate = loc.parent.lookup(db).container.krate;
let item_tree = loc.id.item_tree(db);
let variant = &item_tree[loc.id.value];
(variant.fields.clone(), item_tree, krate)
Expand Down Expand Up @@ -606,21 +606,24 @@ fn any_has_attrs<'db>(
id.lookup(db).source(db).map(ast::AnyHasAttrs::new)
}

fn attrs_from_item_tree<N: ItemTreeNode>(db: &dyn DefDatabase, id: ItemTreeId<N>) -> RawAttrs {
fn attrs_from_item_tree<N: ItemTreeModItemNode>(
db: &dyn DefDatabase,
id: ItemTreeId<N>,
) -> RawAttrs {
let tree = id.item_tree(db);
let mod_item = N::id_to_mod_item(id.value);
tree.raw_attrs(mod_item.into()).clone()
}

fn attrs_from_item_tree_loc<'db, N: ItemTreeNode>(
fn attrs_from_item_tree_loc<'db, N: ItemTreeModItemNode>(
db: &(dyn DefDatabase + 'db),
lookup: impl Lookup<Database<'db> = dyn DefDatabase + 'db, Data = ItemLoc<N>>,
) -> RawAttrs {
let id = lookup.lookup(db).id;
attrs_from_item_tree(db, id)
}

fn attrs_from_item_tree_assoc<'db, N: ItemTreeNode>(
fn attrs_from_item_tree_assoc<'db, N: ItemTreeModItemNode>(
db: &(dyn DefDatabase + 'db),
lookup: impl Lookup<Database<'db> = dyn DefDatabase + 'db, Data = AssocItemLoc<N>>,
) -> RawAttrs {
Expand Down
2 changes: 1 addition & 1 deletion crates/hir-def/src/body/pretty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use super::*;
pub(super) fn print_body_hir(db: &dyn DefDatabase, body: &Body, owner: DefWithBodyId) -> String {
let header = match owner {
DefWithBodyId::FunctionId(it) => {
it.lookup(db).id.resolved(db, |it| format!("fn {} = ", it.name.display(db.upcast())))
it.lookup(db).id.resolved(db, |it| format!("fn {}", it.name.display(db.upcast())))
}
DefWithBodyId::StaticId(it) => it
.lookup(db)
Expand Down
13 changes: 5 additions & 8 deletions crates/hir-def/src/data/adt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -334,7 +334,8 @@ impl EnumVariantData {
e: EnumVariantId,
) -> (Arc<EnumVariantData>, DefDiagnostics) {
let loc = e.lookup(db);
let krate = loc.container.krate;
let container = loc.parent.lookup(db).container;
let krate = container.krate;
let item_tree = loc.id.item_tree(db);
let cfg_options = db.crate_graph()[krate].cfg_options.clone();
let variant = &item_tree[loc.id.value];
Expand All @@ -343,7 +344,7 @@ impl EnumVariantData {
db,
krate,
loc.id.file_id(),
loc.container.local_id,
container.local_id,
&item_tree,
&cfg_options,
&variant.fields,
Expand Down Expand Up @@ -395,7 +396,7 @@ impl HasChildSource<LocalFieldId> for VariantId {
(
lookup.source(db).map(|it| it.kind()),
&item_tree[lookup.id.value].fields,
lookup.container,
lookup.parent.lookup(db).container,
)
}
VariantId::StructId(it) => {
Expand All @@ -411,11 +412,7 @@ impl HasChildSource<LocalFieldId> for VariantId {
let lookup = it.lookup(db);
item_tree = lookup.id.item_tree(db);
(
lookup.source(db).map(|it| {
it.record_field_list()
.map(ast::StructKind::Record)
.unwrap_or(ast::StructKind::Unit)
}),
lookup.source(db).map(|it| it.kind()),
&item_tree[lookup.id.value].fields,
lookup.container,
)
Expand Down
6 changes: 3 additions & 3 deletions crates/hir-def/src/item_tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -337,7 +337,7 @@ from_attrs!(
);

/// Trait implemented by all item nodes in the item tree.
pub trait ItemTreeNode: Clone {
pub trait ItemTreeModItemNode: Clone {
type Source: AstIdNode + Into<ast::Item>;

fn ast_id(&self) -> FileAstId<Self::Source>;
Expand Down Expand Up @@ -494,7 +494,7 @@ macro_rules! mod_items {
)+

$(
impl ItemTreeNode for $typ {
impl ItemTreeModItemNode for $typ {
type Source = $ast;

fn ast_id(&self) -> FileAstId<Self::Source> {
Expand Down Expand Up @@ -577,7 +577,7 @@ impl Index<RawVisibilityId> for ItemTree {
}
}

impl<N: ItemTreeNode> Index<FileItemTreeId<N>> for ItemTree {
impl<N: ItemTreeModItemNode> Index<FileItemTreeId<N>> for ItemTree {
type Output = N;
fn index(&self, id: FileItemTreeId<N>) -> &N {
N::lookup(self, id.index())
Expand Down
2 changes: 1 addition & 1 deletion crates/hir-def/src/item_tree/lower.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use crate::{

use super::*;

fn id<N: ItemTreeNode>(index: Idx<N>) -> FileItemTreeId<N> {
fn id<N: ItemTreeModItemNode>(index: Idx<N>) -> FileItemTreeId<N> {
FileItemTreeId(index)
}

Expand Down
63 changes: 40 additions & 23 deletions crates/hir-def/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,8 @@ use crate::{
data::adt::VariantData,
db::DefDatabase,
item_tree::{
Const, Enum, ExternCrate, Function, Impl, ItemTreeId, ItemTreeNode, Macro2, MacroRules,
Static, Struct, Trait, TraitAlias, TypeAlias, Union, Use, Variant,
Const, Enum, ExternCrate, Function, Impl, ItemTreeId, ItemTreeModItemNode, Macro2,
MacroRules, Static, Struct, Trait, TraitAlias, TypeAlias, Union, Use, Variant,
},
};

Expand Down Expand Up @@ -213,57 +213,57 @@ impl ModuleId {
pub type LocalModuleId = Idx<nameres::ModuleData>;

#[derive(Debug)]
pub struct ItemLoc<N: ItemTreeNode> {
pub struct ItemLoc<N: ItemTreeModItemNode> {
pub container: ModuleId,
pub id: ItemTreeId<N>,
}

impl<N: ItemTreeNode> Clone for ItemLoc<N> {
impl<N: ItemTreeModItemNode> Clone for ItemLoc<N> {
fn clone(&self) -> Self {
Self { container: self.container, id: self.id }
}
}

impl<N: ItemTreeNode> Copy for ItemLoc<N> {}
impl<N: ItemTreeModItemNode> Copy for ItemLoc<N> {}

impl<N: ItemTreeNode> PartialEq for ItemLoc<N> {
impl<N: ItemTreeModItemNode> PartialEq for ItemLoc<N> {
fn eq(&self, other: &Self) -> bool {
self.container == other.container && self.id == other.id
}
}

impl<N: ItemTreeNode> Eq for ItemLoc<N> {}
impl<N: ItemTreeModItemNode> Eq for ItemLoc<N> {}

impl<N: ItemTreeNode> Hash for ItemLoc<N> {
impl<N: ItemTreeModItemNode> Hash for ItemLoc<N> {
fn hash<H: Hasher>(&self, state: &mut H) {
self.container.hash(state);
self.id.hash(state);
}
}

#[derive(Debug)]
pub struct AssocItemLoc<N: ItemTreeNode> {
pub struct AssocItemLoc<N: ItemTreeModItemNode> {
pub container: ItemContainerId,
pub id: ItemTreeId<N>,
}

impl<N: ItemTreeNode> Clone for AssocItemLoc<N> {
impl<N: ItemTreeModItemNode> Clone for AssocItemLoc<N> {
fn clone(&self) -> Self {
Self { container: self.container, id: self.id }
}
}

impl<N: ItemTreeNode> Copy for AssocItemLoc<N> {}
impl<N: ItemTreeModItemNode> Copy for AssocItemLoc<N> {}

impl<N: ItemTreeNode> PartialEq for AssocItemLoc<N> {
impl<N: ItemTreeModItemNode> PartialEq for AssocItemLoc<N> {
fn eq(&self, other: &Self) -> bool {
self.container == other.container && self.id == other.id
}
}

impl<N: ItemTreeNode> Eq for AssocItemLoc<N> {}
impl<N: ItemTreeModItemNode> Eq for AssocItemLoc<N> {}

impl<N: ItemTreeNode> Hash for AssocItemLoc<N> {
impl<N: ItemTreeModItemNode> Hash for AssocItemLoc<N> {
fn hash<H: Hasher>(&self, state: &mut H) {
self.container.hash(state);
self.id.hash(state);
Expand Down Expand Up @@ -297,15 +297,14 @@ pub struct EnumId(salsa::InternId);
pub type EnumLoc = ItemLoc<Enum>;
impl_intern!(EnumId, EnumLoc, intern_enum, lookup_intern_enum);

// FIXME: rename to `VariantId`, only enums can ave variants
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct EnumVariantId(salsa::InternId);

#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct EnumVariantLoc {
pub container: ModuleId,
pub id: ItemTreeId<Variant>,
pub parent: EnumId,
pub index: u32,
}
impl_intern!(EnumVariantId, EnumVariantLoc, intern_enum_variant, lookup_intern_enum_variant);

Expand Down Expand Up @@ -992,7 +991,8 @@ impl HasModule for ItemContainerId {
}
}

impl<N: ItemTreeNode> HasModule for AssocItemLoc<N> {
impl<N: ItemTreeModItemNode> HasModule for AssocItemLoc<N> {
#[inline]
fn module(&self, db: &dyn DefDatabase) -> ModuleId {
self.container.module(db)
}
Expand All @@ -1008,7 +1008,22 @@ impl HasModule for AdtId {
}
}

impl HasModule for EnumId {
#[inline]
fn module(&self, db: &dyn DefDatabase) -> ModuleId {
self.lookup(db).container
}
}

impl HasModule for EnumVariantId {
#[inline]
fn module(&self, db: &dyn DefDatabase) -> ModuleId {
self.lookup(db).parent.module(db)
}
}

impl HasModule for ExternCrateId {
#[inline]
fn module(&self, db: &dyn DefDatabase) -> ModuleId {
self.lookup(db).container
}
Expand All @@ -1017,7 +1032,7 @@ impl HasModule for ExternCrateId {
impl HasModule for VariantId {
fn module(&self, db: &dyn DefDatabase) -> ModuleId {
match self {
VariantId::EnumVariantId(it) => it.lookup(db).container,
VariantId::EnumVariantId(it) => it.lookup(db).parent.module(db),
VariantId::StructId(it) => it.lookup(db).container,
VariantId::UnionId(it) => it.lookup(db).container,
}
Expand Down Expand Up @@ -1046,7 +1061,7 @@ impl HasModule for TypeOwnerId {
TypeOwnerId::TraitAliasId(it) => it.lookup(db).container,
TypeOwnerId::TypeAliasId(it) => it.lookup(db).module(db),
TypeOwnerId::ImplId(it) => it.lookup(db).container,
TypeOwnerId::EnumVariantId(it) => it.lookup(db).container,
TypeOwnerId::EnumVariantId(it) => it.lookup(db).parent.module(db),
}
}
}
Expand All @@ -1057,7 +1072,7 @@ impl HasModule for DefWithBodyId {
DefWithBodyId::FunctionId(it) => it.lookup(db).module(db),
DefWithBodyId::StaticId(it) => it.lookup(db).module(db),
DefWithBodyId::ConstId(it) => it.lookup(db).module(db),
DefWithBodyId::VariantId(it) => it.lookup(db).container,
DefWithBodyId::VariantId(it) => it.lookup(db).parent.module(db),
DefWithBodyId::InTypeConstId(it) => it.lookup(db).owner.module(db),
}
}
Expand All @@ -1072,19 +1087,21 @@ impl HasModule for GenericDefId {
GenericDefId::TraitAliasId(it) => it.lookup(db).container,
GenericDefId::TypeAliasId(it) => it.lookup(db).module(db),
GenericDefId::ImplId(it) => it.lookup(db).container,
GenericDefId::EnumVariantId(it) => it.lookup(db).container,
GenericDefId::EnumVariantId(it) => it.lookup(db).parent.lookup(db).container,
GenericDefId::ConstId(it) => it.lookup(db).module(db),
}
}
}

impl HasModule for TypeAliasId {
#[inline]
fn module(&self, db: &dyn DefDatabase) -> ModuleId {
self.lookup(db).module(db)
}
}

impl HasModule for TraitId {
#[inline]
fn module(&self, db: &dyn DefDatabase) -> ModuleId {
self.lookup(db).container
}
Expand All @@ -1099,7 +1116,7 @@ impl ModuleDefId {
ModuleDefId::ModuleId(id) => *id,
ModuleDefId::FunctionId(id) => id.lookup(db).module(db),
ModuleDefId::AdtId(id) => id.module(db),
ModuleDefId::EnumVariantId(id) => id.lookup(db).container,
ModuleDefId::EnumVariantId(id) => id.lookup(db).parent.module(db),
ModuleDefId::ConstId(id) => id.lookup(db).container.module(db),
ModuleDefId::StaticId(id) => id.lookup(db).module(db),
ModuleDefId::TraitId(id) => id.lookup(db).container,
Expand All @@ -1118,7 +1135,7 @@ impl AttrDefId {
AttrDefId::FieldId(it) => it.parent.module(db).krate,
AttrDefId::AdtId(it) => it.module(db).krate,
AttrDefId::FunctionId(it) => it.lookup(db).module(db).krate,
AttrDefId::EnumVariantId(it) => it.lookup(db).container.krate,
AttrDefId::EnumVariantId(it) => it.lookup(db).parent.module(db).krate,
AttrDefId::StaticId(it) => it.lookup(db).module(db).krate,
AttrDefId::ConstId(it) => it.lookup(db).module(db).krate,
AttrDefId::TraitId(it) => it.lookup(db).container.krate,
Expand Down
Loading

0 comments on commit 180e9b2

Please sign in to comment.