2019-11-23 03:58:01 -06: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 ra_syntax::SmolStr;
|
|
|
|
use rustc_hash::FxHashMap;
|
|
|
|
|
|
|
|
use crate::{
|
2019-11-23 05:44:43 -06:00
|
|
|
db::DefDatabase, AdtId, AttrDefId, CrateId, EnumId, FunctionId, ImplId, ModuleDefId, ModuleId,
|
2019-11-23 03:58:01 -06:00
|
|
|
StaticId, StructId, TraitId,
|
|
|
|
};
|
|
|
|
|
|
|
|
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
|
|
|
|
pub enum LangItemTarget {
|
|
|
|
EnumId(EnumId),
|
|
|
|
FunctionId(FunctionId),
|
2020-02-29 14:24:40 -06:00
|
|
|
ImplDefId(ImplId),
|
2019-11-23 03:58:01 -06:00
|
|
|
StaticId(StaticId),
|
|
|
|
StructId(StructId),
|
|
|
|
TraitId(TraitId),
|
|
|
|
}
|
|
|
|
|
2019-12-29 10:38:37 -06: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 14:24:40 -06:00
|
|
|
pub fn as_impl_def(self) -> Option<ImplId> {
|
2019-12-29 10:38:37 -06:00
|
|
|
match self {
|
2020-02-29 14:24:40 -06:00
|
|
|
LangItemTarget::ImplDefId(id) => Some(id),
|
2019-12-29 10:38:37 -06: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,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-23 03:58:01 -06:00
|
|
|
#[derive(Default, Debug, Clone, PartialEq, Eq)]
|
|
|
|
pub struct LangItems {
|
|
|
|
items: FxHashMap<SmolStr, LangItemTarget>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl LangItems {
|
|
|
|
pub fn target<'a>(&'a self, item: &str) -> Option<&'a LangItemTarget> {
|
|
|
|
self.items.get(item)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Salsa query. This will look for lang items in a specific crate.
|
2019-11-23 05:44:43 -06:00
|
|
|
pub(crate) fn crate_lang_items_query(db: &impl DefDatabase, krate: CrateId) -> Arc<LangItems> {
|
2019-11-23 03:58:01 -06:00
|
|
|
let mut lang_items = LangItems::default();
|
|
|
|
|
|
|
|
let crate_def_map = db.crate_def_map(krate);
|
|
|
|
|
|
|
|
crate_def_map
|
2019-11-24 09:05:12 -06:00
|
|
|
.modules
|
|
|
|
.iter()
|
2019-11-27 12:31:51 -06:00
|
|
|
.filter_map(|(local_id, _)| db.module_lang_items(ModuleId { krate, local_id }))
|
2019-11-23 03:58:01 -06:00
|
|
|
.for_each(|it| lang_items.items.extend(it.items.iter().map(|(k, v)| (k.clone(), *v))));
|
|
|
|
|
|
|
|
Arc::new(lang_items)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) fn module_lang_items_query(
|
2019-11-23 05:44:43 -06:00
|
|
|
db: &impl DefDatabase,
|
2019-11-23 03:58:01 -06:00
|
|
|
module: ModuleId,
|
|
|
|
) -> Option<Arc<LangItems>> {
|
|
|
|
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))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Salsa query. Look for a lang item, starting from the specified crate and recursively
|
|
|
|
/// traversing its dependencies.
|
|
|
|
pub(crate) fn lang_item_query(
|
2019-11-23 05:44:43 -06:00
|
|
|
db: &impl DefDatabase,
|
2019-11-23 03:58:01 -06:00
|
|
|
start_crate: CrateId,
|
|
|
|
item: SmolStr,
|
|
|
|
) -> Option<LangItemTarget> {
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
db.crate_graph()
|
2020-03-09 04:26:46 -05:00
|
|
|
.crate_data(&start_crate)
|
|
|
|
.dependencies
|
|
|
|
.iter()
|
2019-11-23 03:58:01 -06:00
|
|
|
.find_map(|dep| db.lang_item(dep.crate_id, item.clone()))
|
|
|
|
}
|
|
|
|
|
2019-11-23 05:44:43 -06:00
|
|
|
fn collect_lang_items(&mut self, db: &impl DefDatabase, module: ModuleId) {
|
2019-11-23 03:58:01 -06:00
|
|
|
// Look for impl targets
|
|
|
|
let def_map = db.crate_def_map(module.krate);
|
2019-11-27 12:31:51 -06:00
|
|
|
let module_data = &def_map[module.local_id];
|
2020-02-29 14:24:40 -06:00
|
|
|
for impl_def in module_data.scope.impls() {
|
|
|
|
self.collect_lang_item(db, impl_def, LangItemTarget::ImplDefId)
|
2019-11-23 03:58:01 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
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,
|
2019-11-23 05:44:43 -06:00
|
|
|
db: &impl DefDatabase,
|
2019-11-23 03:58:01 -06:00
|
|
|
item: T,
|
|
|
|
constructor: fn(T) -> LangItemTarget,
|
|
|
|
) where
|
|
|
|
T: Into<AttrDefId> + Copy,
|
|
|
|
{
|
|
|
|
let attrs = db.attrs(item.into());
|
2019-11-24 07:03:02 -06:00
|
|
|
if let Some(lang_item_name) = attrs.by_key("lang").string_value() {
|
2019-11-24 06:50:45 -06:00
|
|
|
self.items.entry(lang_item_name.clone()).or_insert_with(|| constructor(item));
|
2019-11-23 03:58:01 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|