use syntax::SmolStr;
use crate::{
- db::DefDatabase, AdtId, AttrDefId, CrateId, EnumId, FunctionId, ImplId, ModuleDefId, ModuleId,
- StaticId, StructId, TraitId,
+ db::DefDatabase, AdtId, AttrDefId, CrateId, EnumId, EnumVariantId, FunctionId, ImplId,
+ ModuleDefId, StaticId, StructId, TraitId,
};
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
StaticId(StaticId),
StructId(StructId),
TraitId(TraitId),
+ EnumVariantId(EnumVariantId),
}
impl LangItemTarget {
_ => None,
}
}
+
+ pub fn as_enum_variant(self) -> Option<EnumVariantId> {
+ match self {
+ LangItemTarget::EnumVariantId(id) => Some(id),
+ _ => None,
+ }
+ }
}
#[derive(Default, Debug, Clone, PartialEq, Eq)]
let crate_def_map = db.crate_def_map(krate);
- crate_def_map
- .modules
- .iter()
- .filter_map(|(local_id, _)| db.module_lang_items(ModuleId { krate, local_id }))
- .for_each(|it| lang_items.items.extend(it.items.iter().map(|(k, v)| (k.clone(), *v))));
-
- Arc::new(lang_items)
- }
+ for (_, module_data) in crate_def_map.modules() {
+ for impl_def in module_data.scope.impls() {
+ lang_items.collect_lang_item(db, impl_def, LangItemTarget::ImplDefId)
+ }
- pub(crate) fn module_lang_items_query(
- db: &dyn DefDatabase,
- module: ModuleId,
- ) -> Option<Arc<LangItems>> {
- let _p = profile::span("module_lang_items_query");
- let mut lang_items = LangItems::default();
- lang_items.collect_lang_items(db, module);
- if lang_items.items.is_empty() {
- None
- } else {
- Some(Arc::new(lang_items))
+ for def in module_data.scope.declarations() {
+ match def {
+ ModuleDefId::TraitId(trait_) => {
+ lang_items.collect_lang_item(db, trait_, LangItemTarget::TraitId);
+ db.trait_data(trait_).items.iter().for_each(|&(_, assoc_id)| {
+ if let crate::AssocItemId::FunctionId(f) = assoc_id {
+ lang_items.collect_lang_item(db, f, LangItemTarget::FunctionId);
+ }
+ });
+ }
+ ModuleDefId::AdtId(AdtId::EnumId(e)) => {
+ lang_items.collect_lang_item(db, e, LangItemTarget::EnumId);
+ db.enum_data(e).variants.iter().for_each(|(local_id, _)| {
+ lang_items.collect_lang_item(
+ db,
+ EnumVariantId { parent: e, local_id },
+ LangItemTarget::EnumVariantId,
+ );
+ });
+ }
+ ModuleDefId::AdtId(AdtId::StructId(s)) => {
+ lang_items.collect_lang_item(db, s, LangItemTarget::StructId);
+ }
+ ModuleDefId::FunctionId(f) => {
+ lang_items.collect_lang_item(db, f, LangItemTarget::FunctionId);
+ }
+ ModuleDefId::StaticId(s) => {
+ lang_items.collect_lang_item(db, s, LangItemTarget::StaticId);
+ }
+ _ => {}
+ }
+ }
}
+
+ Arc::new(lang_items)
}
/// Salsa query. Look for a lang item, starting from the specified crate and recursively
let _p = profile::span("lang_item_query");
let lang_items = db.crate_lang_items(start_crate);
let start_crate_target = lang_items.items.get(&item);
- if let Some(target) = start_crate_target {
- return Some(*target);
+ if let Some(&target) = start_crate_target {
+ return Some(target);
}
db.crate_graph()[start_crate]
.dependencies
.find_map(|dep| db.lang_item(dep.crate_id, item.clone()))
}
- fn collect_lang_items(&mut self, db: &dyn DefDatabase, module: ModuleId) {
- // Look for impl targets
- let def_map = db.crate_def_map(module.krate);
- let module_data = &def_map[module.local_id];
- for impl_def in module_data.scope.impls() {
- self.collect_lang_item(db, impl_def, LangItemTarget::ImplDefId)
- }
-
- for def in module_data.scope.declarations() {
- match def {
- ModuleDefId::TraitId(trait_) => {
- self.collect_lang_item(db, trait_, LangItemTarget::TraitId)
- }
- ModuleDefId::AdtId(AdtId::EnumId(e)) => {
- self.collect_lang_item(db, e, LangItemTarget::EnumId)
- }
- ModuleDefId::AdtId(AdtId::StructId(s)) => {
- self.collect_lang_item(db, s, LangItemTarget::StructId)
- }
- ModuleDefId::FunctionId(f) => {
- self.collect_lang_item(db, f, LangItemTarget::FunctionId)
- }
- ModuleDefId::StaticId(s) => self.collect_lang_item(db, s, LangItemTarget::StaticId),
- _ => {}
- }
- }
- }
-
fn collect_lang_item<T>(
&mut self,
db: &dyn DefDatabase,
) where
T: Into<AttrDefId> + Copy,
{
+ let _p = profile::span("collect_lang_item");
if let Some(lang_item_name) = lang_attr(db, item) {
self.items.entry(lang_item_name).or_insert_with(|| constructor(item));
}