From 8692977d2798831d0aea434277ee54d589aa9be3 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Sat, 23 Nov 2019 12:58:01 +0300 Subject: Move lang_items to hir_def --- crates/ra_hir_def/src/lang_item.rs | 120 +++++++++++++++++++++++++++++++++++++ 1 file changed, 120 insertions(+) create mode 100644 crates/ra_hir_def/src/lang_item.rs (limited to 'crates/ra_hir_def/src/lang_item.rs') diff --git a/crates/ra_hir_def/src/lang_item.rs b/crates/ra_hir_def/src/lang_item.rs new file mode 100644 index 000000000..4c1a764ea --- /dev/null +++ b/crates/ra_hir_def/src/lang_item.rs @@ -0,0 +1,120 @@ +//! 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::{ + db::DefDatabase2, AdtId, AttrDefId, CrateId, EnumId, FunctionId, ImplId, ModuleDefId, ModuleId, + StaticId, StructId, TraitId, +}; + +#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] +pub enum LangItemTarget { + EnumId(EnumId), + FunctionId(FunctionId), + ImplBlockId(ImplId), + StaticId(StaticId), + StructId(StructId), + TraitId(TraitId), +} + +#[derive(Default, Debug, Clone, PartialEq, Eq)] +pub struct LangItems { + items: FxHashMap, +} + +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. + pub(crate) fn crate_lang_items_query(db: &impl DefDatabase2, krate: CrateId) -> Arc { + let mut lang_items = LangItems::default(); + + let crate_def_map = db.crate_def_map(krate); + + crate_def_map + .modules() + .filter_map(|module_id| db.module_lang_items(ModuleId { krate, module_id })) + .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( + db: &impl DefDatabase2, + module: ModuleId, + ) -> Option> { + 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( + db: &impl DefDatabase2, + start_crate: CrateId, + item: SmolStr, + ) -> Option { + 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() + .dependencies(start_crate) + .find_map(|dep| db.lang_item(dep.crate_id, item.clone())) + } + + fn collect_lang_items(&mut self, db: &impl DefDatabase2, module: ModuleId) { + // Look for impl targets + let def_map = db.crate_def_map(module.krate); + let module_data = &def_map[module.module_id]; + for &impl_block in module_data.impls.iter() { + self.collect_lang_item(db, impl_block, LangItemTarget::ImplBlockId) + } + + 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( + &mut self, + db: &impl DefDatabase2, + item: T, + constructor: fn(T) -> LangItemTarget, + ) where + T: Into + Copy, + { + let attrs = db.attrs(item.into()); + if let Some(lang_item_name) = attrs.find_string_value("lang") { + self.items.entry(lang_item_name).or_insert_with(|| constructor(item)); + } + } +} -- cgit v1.2.3 From fc1e543f7abb69b8cab308410fa0a127950ee1c5 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Sat, 23 Nov 2019 14:44:43 +0300 Subject: Get rid of DefDatabase2 --- crates/ra_hir_def/src/lang_item.rs | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) (limited to 'crates/ra_hir_def/src/lang_item.rs') diff --git a/crates/ra_hir_def/src/lang_item.rs b/crates/ra_hir_def/src/lang_item.rs index 4c1a764ea..df951c533 100644 --- a/crates/ra_hir_def/src/lang_item.rs +++ b/crates/ra_hir_def/src/lang_item.rs @@ -8,7 +8,7 @@ use ra_syntax::SmolStr; use rustc_hash::FxHashMap; use crate::{ - db::DefDatabase2, AdtId, AttrDefId, CrateId, EnumId, FunctionId, ImplId, ModuleDefId, ModuleId, + db::DefDatabase, AdtId, AttrDefId, CrateId, EnumId, FunctionId, ImplId, ModuleDefId, ModuleId, StaticId, StructId, TraitId, }; @@ -33,7 +33,7 @@ impl LangItems { } /// Salsa query. This will look for lang items in a specific crate. - pub(crate) fn crate_lang_items_query(db: &impl DefDatabase2, krate: CrateId) -> Arc { + pub(crate) fn crate_lang_items_query(db: &impl DefDatabase, krate: CrateId) -> Arc { let mut lang_items = LangItems::default(); let crate_def_map = db.crate_def_map(krate); @@ -47,7 +47,7 @@ impl LangItems { } pub(crate) fn module_lang_items_query( - db: &impl DefDatabase2, + db: &impl DefDatabase, module: ModuleId, ) -> Option> { let mut lang_items = LangItems::default(); @@ -62,7 +62,7 @@ impl LangItems { /// 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 DefDatabase2, + db: &impl DefDatabase, start_crate: CrateId, item: SmolStr, ) -> Option { @@ -76,7 +76,7 @@ impl LangItems { .find_map(|dep| db.lang_item(dep.crate_id, item.clone())) } - fn collect_lang_items(&mut self, db: &impl DefDatabase2, module: ModuleId) { + fn collect_lang_items(&mut self, db: &impl DefDatabase, module: ModuleId) { // Look for impl targets let def_map = db.crate_def_map(module.krate); let module_data = &def_map[module.module_id]; @@ -106,7 +106,7 @@ impl LangItems { fn collect_lang_item( &mut self, - db: &impl DefDatabase2, + db: &impl DefDatabase, item: T, constructor: fn(T) -> LangItemTarget, ) where -- cgit v1.2.3 From 1956d57ed4896bb29dfcfaed2a5291ec69251f52 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Sun, 24 Nov 2019 15:50:45 +0300 Subject: Slightly reduce code duplication --- crates/ra_hir_def/src/lang_item.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'crates/ra_hir_def/src/lang_item.rs') diff --git a/crates/ra_hir_def/src/lang_item.rs b/crates/ra_hir_def/src/lang_item.rs index df951c533..69d7bf21a 100644 --- a/crates/ra_hir_def/src/lang_item.rs +++ b/crates/ra_hir_def/src/lang_item.rs @@ -114,7 +114,7 @@ impl LangItems { { let attrs = db.attrs(item.into()); if let Some(lang_item_name) = attrs.find_string_value("lang") { - self.items.entry(lang_item_name).or_insert_with(|| constructor(item)); + self.items.entry(lang_item_name.clone()).or_insert_with(|| constructor(item)); } } } -- cgit v1.2.3 From 4b74fb1d896ce5a1c8c4c4bf73ad2940fb86abc5 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Sun, 24 Nov 2019 16:03:02 +0300 Subject: Nicer API for attrs --- crates/ra_hir_def/src/lang_item.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'crates/ra_hir_def/src/lang_item.rs') diff --git a/crates/ra_hir_def/src/lang_item.rs b/crates/ra_hir_def/src/lang_item.rs index 69d7bf21a..3b9fb0328 100644 --- a/crates/ra_hir_def/src/lang_item.rs +++ b/crates/ra_hir_def/src/lang_item.rs @@ -113,7 +113,7 @@ impl LangItems { T: Into + Copy, { let attrs = db.attrs(item.into()); - if let Some(lang_item_name) = attrs.find_string_value("lang") { + if let Some(lang_item_name) = attrs.by_key("lang").string_value() { self.items.entry(lang_item_name.clone()).or_insert_with(|| constructor(item)); } } -- cgit v1.2.3 From f5e0a31eaf9ddd7788e6261d49f4d18e8463a719 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Sun, 24 Nov 2019 18:05:12 +0300 Subject: Cleanup nameres --- crates/ra_hir_def/src/lang_item.rs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) (limited to 'crates/ra_hir_def/src/lang_item.rs') diff --git a/crates/ra_hir_def/src/lang_item.rs b/crates/ra_hir_def/src/lang_item.rs index 3b9fb0328..f15c23db9 100644 --- a/crates/ra_hir_def/src/lang_item.rs +++ b/crates/ra_hir_def/src/lang_item.rs @@ -39,8 +39,9 @@ impl LangItems { let crate_def_map = db.crate_def_map(krate); crate_def_map - .modules() - .filter_map(|module_id| db.module_lang_items(ModuleId { krate, module_id })) + .modules + .iter() + .filter_map(|(module_id, _)| db.module_lang_items(ModuleId { krate, module_id })) .for_each(|it| lang_items.items.extend(it.items.iter().map(|(k, v)| (k.clone(), *v)))); Arc::new(lang_items) -- cgit v1.2.3