Skip to content

Commit

Permalink
Eagerly lower enum variants in CrateDefMap construction
Browse files Browse the repository at this point in the history
  • Loading branch information
Veykril committed Jan 12, 2024
1 parent 9d8889c commit 521b604
Show file tree
Hide file tree
Showing 49 changed files with 552 additions and 592 deletions.
82 changes: 10 additions & 72 deletions crates/hir-def/src/attr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ use crate::{
lang_item::LangItem,
nameres::{ModuleOrigin, ModuleSource},
src::{HasChildSource, HasSource},
AdtId, AssocItemLoc, AttrDefId, EnumId, GenericParamId, ItemLoc, LocalEnumVariantId,
LocalFieldId, Lookup, MacroId, VariantId,
AdtId, AssocItemLoc, AttrDefId, GenericParamId, ItemLoc, LocalFieldId, Lookup, MacroId,
VariantId,
};

#[derive(Default, Debug, Clone, PartialEq, Eq)]
Expand Down Expand Up @@ -70,33 +70,6 @@ impl ops::Deref for AttrsWithOwner {
impl Attrs {
pub const EMPTY: Self = Self(RawAttrs::EMPTY);

pub(crate) fn variants_attrs_query(
db: &dyn DefDatabase,
e: EnumId,
) -> Arc<ArenaMap<LocalEnumVariantId, Attrs>> {
let _p = profile::span("variants_attrs_query");
// FIXME: There should be some proper form of mapping between item tree enum variant ids and hir enum variant ids
let mut res = ArenaMap::default();

let loc = e.lookup(db);
let krate = loc.container.krate;
let item_tree = loc.id.item_tree(db);
let enum_ = &item_tree[loc.id.value];
let crate_graph = db.crate_graph();
let cfg_options = &crate_graph[krate].cfg_options;

let mut idx = 0;
for variant in enum_.variants.clone() {
let attrs = item_tree.attrs(db, krate, variant.into());
if attrs.is_cfg_enabled(cfg_options) {
res.insert(Idx::from_raw(RawIdx::from(idx)), attrs);
idx += 1;
}
}

Arc::new(res)
}

pub(crate) fn fields_attrs_query(
db: &dyn DefDatabase,
v: VariantId,
Expand All @@ -108,29 +81,11 @@ impl Attrs {
let crate_graph = db.crate_graph();
let (fields, item_tree, krate) = match v {
VariantId::EnumVariantId(it) => {
let e = it.parent;
let loc = e.lookup(db);
let loc = it.lookup(db);
let krate = loc.container.krate;
let item_tree = loc.id.item_tree(db);
let enum_ = &item_tree[loc.id.value];

let cfg_options = &crate_graph[krate].cfg_options;

let Some(variant) = enum_
.variants
.clone()
.filter(|variant| {
let attrs = item_tree.attrs(db, krate, (*variant).into());
attrs.is_cfg_enabled(cfg_options)
})
.zip(0u32..)
.find(|(_variant, idx)| it.local_id == Idx::from_raw(RawIdx::from(*idx)))
.map(|(variant, _idx)| variant)
else {
return Arc::new(res);
};

(item_tree[variant].fields.clone(), item_tree, krate)
let variant = &item_tree[loc.id.value];
(variant.fields.clone(), item_tree, krate)
}
VariantId::StructId(it) => {
let loc = it.lookup(db);
Expand Down Expand Up @@ -401,10 +356,12 @@ impl AttrsWithOwner {
AttrDefId::FieldId(it) => {
return db.fields_attrs(it.parent)[it.local_id].clone();
}
// FIXME: DRY this up
AttrDefId::EnumVariantId(it) => {
return db.variants_attrs(it.parent)[it.local_id].clone();
let id = it.lookup(db).id;
let tree = id.item_tree(db);
tree.raw_attrs(id.value.into()).clone()
}
// FIXME: DRY this up
AttrDefId::AdtId(it) => match it {
AdtId::StructId(it) => attrs_from_item_tree_loc(db, it),
AdtId::EnumId(it) => attrs_from_item_tree_loc(db, it),
Expand Down Expand Up @@ -503,12 +460,7 @@ impl AttrsWithOwner {
AdtId::EnumId(id) => any_has_attrs(db, id),
},
AttrDefId::FunctionId(id) => any_has_attrs(db, id),
AttrDefId::EnumVariantId(id) => {
let map = db.variants_attrs_source_map(id.parent);
let file_id = id.parent.lookup(db).id.file_id();
let root = db.parse_or_expand(file_id);
InFile::new(file_id, ast::AnyHasAttrs::new(map[id.local_id].to_node(&root)))
}
AttrDefId::EnumVariantId(id) => any_has_attrs(db, id),
AttrDefId::StaticId(id) => any_has_attrs(db, id),
AttrDefId::ConstId(id) => any_has_attrs(db, id),
AttrDefId::TraitId(id) => any_has_attrs(db, id),
Expand Down Expand Up @@ -676,20 +628,6 @@ fn attrs_from_item_tree_assoc<'db, N: ItemTreeNode>(
attrs_from_item_tree(db, id)
}

pub(crate) fn variants_attrs_source_map(
db: &dyn DefDatabase,
def: EnumId,
) -> Arc<ArenaMap<LocalEnumVariantId, AstPtr<ast::Variant>>> {
let mut res = ArenaMap::default();
let child_source = def.child_source(db);

for (idx, variant) in child_source.value.iter() {
res.insert(idx, AstPtr::new(variant));
}

Arc::new(res)
}

pub(crate) fn fields_attrs_source_map(
db: &dyn DefDatabase,
def: VariantId,
Expand Down
7 changes: 4 additions & 3 deletions crates/hir-def/src/body.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ use crate::{
},
nameres::DefMap,
path::{ModPath, Path},
src::{HasChildSource, HasSource},
src::HasSource,
BlockId, DefWithBodyId, HasModule, Lookup,
};

Expand Down Expand Up @@ -160,8 +160,9 @@ impl Body {
src.map(|it| it.body())
}
DefWithBodyId::VariantId(v) => {
let src = v.parent.child_source(db);
src.map(|it| it[v.local_id].expr())
let s = v.lookup(db);
let src = s.source(db);
src.map(|it| it.expr())
}
DefWithBodyId::InTypeConstId(c) => c.lookup(db).id.map(|_| c.source(db).expr()),
}
Expand Down
14 changes: 7 additions & 7 deletions crates/hir-def/src/body/pretty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
use std::fmt::{self, Write};

use itertools::Itertools;
use syntax::ast::HasName;

use crate::{
hir::{
Expand Down Expand Up @@ -42,12 +41,13 @@ pub(super) fn print_body_hir(db: &dyn DefDatabase, body: &Body, owner: DefWithBo
}
DefWithBodyId::InTypeConstId(_) => format!("In type const = "),
DefWithBodyId::VariantId(it) => {
let src = it.parent.child_source(db);
let variant = &src.value[it.local_id];
match &variant.name() {
Some(name) => name.to_string(),
None => "_".to_string(),
}
let loc = it.lookup(db);
let enum_loc = loc.parent.lookup(db);
format!(
"enum {}::{}",
enum_loc.id.item_tree(db)[enum_loc.id.value].name.display(db.upcast()),
loc.id.item_tree(db)[loc.id.value].name.display(db.upcast()),
)
}
};

Expand Down
22 changes: 14 additions & 8 deletions crates/hir-def/src/child_by_source.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ use crate::{
item_scope::ItemScope,
nameres::DefMap,
src::{HasChildSource, HasSource},
AdtId, AssocItemId, DefWithBodyId, EnumId, EnumVariantId, ExternCrateId, FieldId, ImplId,
Lookup, MacroId, ModuleDefId, ModuleId, TraitId, UseId, VariantId,
AdtId, AssocItemId, DefWithBodyId, EnumId, ExternCrateId, FieldId, ImplId, Lookup, MacroId,
ModuleDefId, ModuleId, TraitId, UseId, VariantId,
};

pub trait ChildBySource {
Expand Down Expand Up @@ -205,12 +205,18 @@ impl ChildBySource for VariantId {

impl ChildBySource for EnumId {
fn child_by_source_to(&self, db: &dyn DefDatabase, res: &mut DynMap, _: HirFileId) {
let arena_map = self.child_source(db);
let arena_map = arena_map.as_ref();
for (local_id, source) in arena_map.value.iter() {
let id = EnumVariantId { parent: *self, local_id };
res[keys::VARIANT].insert(source.clone(), id)
}
let loc = &self.lookup(db);

let tree = loc.id.item_tree(db);
let ast_id_map = db.ast_id_map(loc.id.file_id());
let root = db.parse_or_expand(loc.id.file_id());

db.enum_data(*self).variants.iter().for_each(|&(variant, _)| {
res[keys::VARIANT].insert(
ast_id_map.get(tree[variant.lookup(db).id.value].ast_id).to_node(&root),
variant,
);
});
}
}

Expand Down
Loading

0 comments on commit 521b604

Please sign in to comment.