rust-analyzer/crates/ra_hir/src/lang_item.rs

147 lines
5.1 KiB
Rust
Raw Normal View History

2019-04-14 22:03:54 +00:00
use std::sync::Arc;
use rustc_hash::FxHashMap;
use ra_syntax::{SmolStr, ast::AttrsOwner};
use crate::{
Crate, DefDatabase, Enum, Function, HirDatabase, ImplBlock, Module, Static, Struct, Trait, ModuleDef, AstDatabase, HasSource
2019-04-14 22:03:54 +00:00
};
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum LangItemTarget {
Enum(Enum),
Function(Function),
ImplBlock(ImplBlock),
2019-04-14 22:03:54 +00:00
Static(Static),
Struct(Struct),
Trait(Trait),
}
impl LangItemTarget {
pub(crate) fn krate(&self, db: &impl HirDatabase) -> Option<Crate> {
match self {
LangItemTarget::Enum(e) => e.module(db).krate(db),
LangItemTarget::Function(f) => f.module(db).krate(db),
LangItemTarget::ImplBlock(i) => i.module().krate(db),
2019-04-14 22:03:54 +00:00
LangItemTarget::Static(s) => s.module(db).krate(db),
LangItemTarget::Struct(s) => s.module(db).krate(db),
LangItemTarget::Trait(t) => t.module(db).krate(db),
}
}
}
#[derive(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)
}
2019-04-18 18:34:10 +00:00
/// Salsa query. This will look for lang items in a specific crate.
2019-06-01 18:17:57 +00:00
pub(crate) fn lang_items_query(
db: &(impl DefDatabase + AstDatabase),
krate: Crate,
) -> Arc<LangItems> {
2019-04-14 22:03:54 +00:00
let mut lang_items = LangItems { items: FxHashMap::default() };
if let Some(module) = krate.root_module(db) {
lang_items.collect_lang_items_recursive(db, &module);
}
Arc::new(lang_items)
}
2019-04-18 18:34:10 +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(
db: &impl DefDatabase,
start_crate: Crate,
item: SmolStr,
) -> Option<LangItemTarget> {
let lang_items = db.lang_items(start_crate);
let start_crate_target = lang_items.items.get(&item);
if let Some(target) = start_crate_target {
Some(*target)
} else {
for dep in start_crate.dependencies(db) {
let dep_crate = dep.krate;
let dep_target = db.lang_item(dep_crate, item.clone());
if dep_target.is_some() {
return dep_target;
}
}
None
}
}
2019-06-01 18:17:57 +00:00
fn collect_lang_items_recursive(
&mut self,
db: &(impl DefDatabase + AstDatabase),
module: &Module,
) {
2019-04-14 22:03:54 +00:00
// Look for impl targets
let (impl_blocks, source_map) = db.impls_in_module_with_source_map(module.clone());
2019-06-11 14:47:24 +00:00
let source = module.definition_source(db).ast;
2019-04-14 22:03:54 +00:00
for (impl_id, _) in impl_blocks.impls.iter() {
let impl_block = source_map.get(&source, impl_id);
if let Some(lang_item_name) = lang_item_name(&*impl_block) {
2019-04-14 22:03:54 +00:00
let imp = ImplBlock::from_id(*module, impl_id);
2019-06-03 14:27:51 +00:00
self.items.entry(lang_item_name).or_insert_with(|| LangItemTarget::ImplBlock(imp));
2019-04-14 22:03:54 +00:00
}
}
// FIXME make this nicer
for def in module.declarations(db) {
match def {
ModuleDef::Trait(trait_) => {
let node = trait_.source(db).ast;
if let Some(lang_item_name) = lang_item_name(&*node) {
self.items.entry(lang_item_name).or_insert(LangItemTarget::Trait(trait_));
}
}
ModuleDef::Enum(e) => {
let node = e.source(db).ast;
if let Some(lang_item_name) = lang_item_name(&*node) {
self.items.entry(lang_item_name).or_insert(LangItemTarget::Enum(e));
}
}
ModuleDef::Struct(s) => {
let node = s.source(db).ast;
if let Some(lang_item_name) = lang_item_name(&*node) {
self.items.entry(lang_item_name).or_insert(LangItemTarget::Struct(s));
}
}
ModuleDef::Function(f) => {
let node = f.source(db).ast;
if let Some(lang_item_name) = lang_item_name(&*node) {
self.items.entry(lang_item_name).or_insert(LangItemTarget::Function(f));
}
}
ModuleDef::Static(s) => {
let node = s.source(db).ast;
if let Some(lang_item_name) = lang_item_name(&*node) {
self.items.entry(lang_item_name).or_insert(LangItemTarget::Static(s));
}
}
_ => {}
}
}
2019-04-14 22:03:54 +00:00
// Look for lang items in the children
for child in module.children(db) {
self.collect_lang_items_recursive(db, &child);
}
}
}
fn lang_item_name<T: AttrsOwner>(node: &T) -> Option<SmolStr> {
node.attrs()
.filter_map(|a| a.as_key_value())
.filter(|(key, _)| key == "lang")
.map(|(_, val)| val)
.nth(0)
}