2019-11-23 09:58:01 +00:00
|
|
|
//! Collects lang items: items marked with `#[lang = "..."]` attribute.
|
|
|
|
//!
|
|
|
|
//! This attribute to tell the compiler about semi built-in std library
|
|
|
|
//! features, such as Fn family of traits.
|
|
|
|
use std::sync::Arc;
|
|
|
|
|
|
|
|
use rustc_hash::FxHashMap;
|
2020-08-12 16:26:51 +00:00
|
|
|
use syntax::SmolStr;
|
2019-11-23 09:58:01 +00:00
|
|
|
|
|
|
|
use crate::{
|
2021-08-07 19:56:42 +00:00
|
|
|
db::DefDatabase, AdtId, AttrDefId, CrateId, EnumId, EnumVariantId, FunctionId, ImplId,
|
|
|
|
ModuleDefId, StaticId, StructId, TraitId,
|
2019-11-23 09:58:01 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
|
|
|
|
pub enum LangItemTarget {
|
|
|
|
EnumId(EnumId),
|
|
|
|
FunctionId(FunctionId),
|
2020-02-29 20:24:40 +00:00
|
|
|
ImplDefId(ImplId),
|
2019-11-23 09:58:01 +00:00
|
|
|
StaticId(StaticId),
|
|
|
|
StructId(StructId),
|
|
|
|
TraitId(TraitId),
|
2021-08-07 19:56:42 +00:00
|
|
|
EnumVariantId(EnumVariantId),
|
2019-11-23 09:58:01 +00:00
|
|
|
}
|
|
|
|
|
2019-12-29 16:38:37 +00:00
|
|
|
impl LangItemTarget {
|
|
|
|
pub fn as_enum(self) -> Option<EnumId> {
|
|
|
|
match self {
|
|
|
|
LangItemTarget::EnumId(id) => Some(id),
|
|
|
|
_ => None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn as_function(self) -> Option<FunctionId> {
|
|
|
|
match self {
|
|
|
|
LangItemTarget::FunctionId(id) => Some(id),
|
|
|
|
_ => None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-29 20:24:40 +00:00
|
|
|
pub fn as_impl_def(self) -> Option<ImplId> {
|
2019-12-29 16:38:37 +00:00
|
|
|
match self {
|
2020-02-29 20:24:40 +00:00
|
|
|
LangItemTarget::ImplDefId(id) => Some(id),
|
2019-12-29 16:38:37 +00:00
|
|
|
_ => None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn as_static(self) -> Option<StaticId> {
|
|
|
|
match self {
|
|
|
|
LangItemTarget::StaticId(id) => Some(id),
|
|
|
|
_ => None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn as_struct(self) -> Option<StructId> {
|
|
|
|
match self {
|
|
|
|
LangItemTarget::StructId(id) => Some(id),
|
|
|
|
_ => None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn as_trait(self) -> Option<TraitId> {
|
|
|
|
match self {
|
|
|
|
LangItemTarget::TraitId(id) => Some(id),
|
|
|
|
_ => None,
|
|
|
|
}
|
|
|
|
}
|
2021-08-07 19:56:42 +00:00
|
|
|
|
|
|
|
pub fn as_enum_variant(self) -> Option<EnumVariantId> {
|
|
|
|
match self {
|
|
|
|
LangItemTarget::EnumVariantId(id) => Some(id),
|
|
|
|
_ => None,
|
|
|
|
}
|
|
|
|
}
|
2019-12-29 16:38:37 +00:00
|
|
|
}
|
|
|
|
|
2019-11-23 09:58:01 +00:00
|
|
|
#[derive(Default, Debug, Clone, PartialEq, Eq)]
|
|
|
|
pub struct LangItems {
|
|
|
|
items: FxHashMap<SmolStr, LangItemTarget>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl LangItems {
|
2020-05-22 13:55:15 +00:00
|
|
|
pub fn target(&self, item: &str) -> Option<LangItemTarget> {
|
|
|
|
self.items.get(item).copied()
|
2019-11-23 09:58:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Salsa query. This will look for lang items in a specific crate.
|
2020-03-13 15:05:46 +00:00
|
|
|
pub(crate) fn crate_lang_items_query(db: &dyn DefDatabase, krate: CrateId) -> Arc<LangItems> {
|
2020-08-12 14:32:36 +00:00
|
|
|
let _p = profile::span("crate_lang_items_query");
|
2020-04-10 18:25:55 +00:00
|
|
|
|
2019-11-23 09:58:01 +00:00
|
|
|
let mut lang_items = LangItems::default();
|
|
|
|
|
|
|
|
let crate_def_map = db.crate_def_map(krate);
|
|
|
|
|
2021-01-20 14:41:18 +00:00
|
|
|
for (_, module_data) in crate_def_map.modules() {
|
2020-12-16 23:26:01 +00:00
|
|
|
for impl_def in module_data.scope.impls() {
|
|
|
|
lang_items.collect_lang_item(db, impl_def, LangItemTarget::ImplDefId)
|
|
|
|
}
|
2019-11-23 09:58:01 +00:00
|
|
|
|
2020-12-16 23:26:01 +00:00
|
|
|
for def in module_data.scope.declarations() {
|
|
|
|
match def {
|
|
|
|
ModuleDefId::TraitId(trait_) => {
|
2021-08-07 19:56:42 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
});
|
2020-12-16 23:26:01 +00:00
|
|
|
}
|
|
|
|
ModuleDefId::AdtId(AdtId::EnumId(e)) => {
|
2021-08-07 19:56:42 +00:00
|
|
|
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,
|
|
|
|
);
|
|
|
|
});
|
2020-12-16 23:26:01 +00:00
|
|
|
}
|
|
|
|
ModuleDefId::AdtId(AdtId::StructId(s)) => {
|
2021-08-07 19:56:42 +00:00
|
|
|
lang_items.collect_lang_item(db, s, LangItemTarget::StructId);
|
2020-12-16 23:26:01 +00:00
|
|
|
}
|
|
|
|
ModuleDefId::FunctionId(f) => {
|
2021-08-07 19:56:42 +00:00
|
|
|
lang_items.collect_lang_item(db, f, LangItemTarget::FunctionId);
|
2020-12-16 23:26:01 +00:00
|
|
|
}
|
|
|
|
ModuleDefId::StaticId(s) => {
|
2021-08-07 19:56:42 +00:00
|
|
|
lang_items.collect_lang_item(db, s, LangItemTarget::StaticId);
|
2020-12-16 23:26:01 +00:00
|
|
|
}
|
|
|
|
_ => {}
|
|
|
|
}
|
|
|
|
}
|
2019-11-23 09:58:01 +00:00
|
|
|
}
|
2020-12-16 23:26:01 +00:00
|
|
|
|
|
|
|
Arc::new(lang_items)
|
2019-11-23 09:58:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Salsa query. Look for a lang item, starting from the specified crate and recursively
|
|
|
|
/// traversing its dependencies.
|
|
|
|
pub(crate) fn lang_item_query(
|
2020-03-13 15:05:46 +00:00
|
|
|
db: &dyn DefDatabase,
|
2019-11-23 09:58:01 +00:00
|
|
|
start_crate: CrateId,
|
|
|
|
item: SmolStr,
|
|
|
|
) -> Option<LangItemTarget> {
|
2020-08-12 14:32:36 +00:00
|
|
|
let _p = profile::span("lang_item_query");
|
2019-11-23 09:58:01 +00:00
|
|
|
let lang_items = db.crate_lang_items(start_crate);
|
|
|
|
let start_crate_target = lang_items.items.get(&item);
|
2022-01-06 12:31:36 +00:00
|
|
|
if let Some(&target) = start_crate_target {
|
|
|
|
return Some(target);
|
2019-11-23 09:58:01 +00:00
|
|
|
}
|
2020-03-09 10:11:59 +00:00
|
|
|
db.crate_graph()[start_crate]
|
2020-03-09 09:26:46 +00:00
|
|
|
.dependencies
|
|
|
|
.iter()
|
2019-11-23 09:58:01 +00:00
|
|
|
.find_map(|dep| db.lang_item(dep.crate_id, item.clone()))
|
|
|
|
}
|
|
|
|
|
|
|
|
fn collect_lang_item<T>(
|
|
|
|
&mut self,
|
2020-03-13 15:05:46 +00:00
|
|
|
db: &dyn DefDatabase,
|
2019-11-23 09:58:01 +00:00
|
|
|
item: T,
|
|
|
|
constructor: fn(T) -> LangItemTarget,
|
|
|
|
) where
|
|
|
|
T: Into<AttrDefId> + Copy,
|
|
|
|
{
|
2021-06-10 20:03:16 +00:00
|
|
|
let _p = profile::span("collect_lang_item");
|
2020-05-22 13:55:15 +00:00
|
|
|
if let Some(lang_item_name) = lang_attr(db, item) {
|
2020-05-25 17:35:52 +00:00
|
|
|
self.items.entry(lang_item_name).or_insert_with(|| constructor(item));
|
2019-11-23 09:58:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-05-22 13:55:15 +00:00
|
|
|
|
|
|
|
pub fn lang_attr(db: &dyn DefDatabase, item: impl Into<AttrDefId> + Copy) -> Option<SmolStr> {
|
|
|
|
let attrs = db.attrs(item.into());
|
|
|
|
attrs.by_key("lang").string_value().cloned()
|
|
|
|
}
|