1 //! Collects lang items: items marked with `#[lang = "..."]` attribute.
3 //! This attribute to tell the compiler about semi built-in std library
4 //! features, such as Fn family of traits.
7 use rustc_hash::FxHashMap;
11 db::DefDatabase, AdtId, AttrDefId, CrateId, EnumId, FunctionId, ImplId, ModuleDefId, StaticId,
15 #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
16 pub enum LangItemTarget {
18 FunctionId(FunctionId),
26 pub fn as_enum(self) -> Option<EnumId> {
28 LangItemTarget::EnumId(id) => Some(id),
33 pub fn as_function(self) -> Option<FunctionId> {
35 LangItemTarget::FunctionId(id) => Some(id),
40 pub fn as_impl_def(self) -> Option<ImplId> {
42 LangItemTarget::ImplDefId(id) => Some(id),
47 pub fn as_static(self) -> Option<StaticId> {
49 LangItemTarget::StaticId(id) => Some(id),
54 pub fn as_struct(self) -> Option<StructId> {
56 LangItemTarget::StructId(id) => Some(id),
61 pub fn as_trait(self) -> Option<TraitId> {
63 LangItemTarget::TraitId(id) => Some(id),
69 #[derive(Default, Debug, Clone, PartialEq, Eq)]
70 pub struct LangItems {
71 items: FxHashMap<SmolStr, LangItemTarget>,
75 pub fn target(&self, item: &str) -> Option<LangItemTarget> {
76 self.items.get(item).copied()
79 /// Salsa query. This will look for lang items in a specific crate.
80 pub(crate) fn crate_lang_items_query(db: &dyn DefDatabase, krate: CrateId) -> Arc<LangItems> {
81 let _p = profile::span("crate_lang_items_query");
83 let mut lang_items = LangItems::default();
85 let crate_def_map = db.crate_def_map(krate);
87 for (_, module_data) in crate_def_map.modules() {
88 for impl_def in module_data.scope.impls() {
89 lang_items.collect_lang_item(db, impl_def, LangItemTarget::ImplDefId)
92 for def in module_data.scope.declarations() {
94 ModuleDefId::TraitId(trait_) => {
95 lang_items.collect_lang_item(db, trait_, LangItemTarget::TraitId)
97 ModuleDefId::AdtId(AdtId::EnumId(e)) => {
98 lang_items.collect_lang_item(db, e, LangItemTarget::EnumId)
100 ModuleDefId::AdtId(AdtId::StructId(s)) => {
101 lang_items.collect_lang_item(db, s, LangItemTarget::StructId)
103 ModuleDefId::FunctionId(f) => {
104 lang_items.collect_lang_item(db, f, LangItemTarget::FunctionId)
106 ModuleDefId::StaticId(s) => {
107 lang_items.collect_lang_item(db, s, LangItemTarget::StaticId)
117 /// Salsa query. Look for a lang item, starting from the specified crate and recursively
118 /// traversing its dependencies.
119 pub(crate) fn lang_item_query(
120 db: &dyn DefDatabase,
121 start_crate: CrateId,
123 ) -> Option<LangItemTarget> {
124 let _p = profile::span("lang_item_query");
125 let lang_items = db.crate_lang_items(start_crate);
126 let start_crate_target = lang_items.items.get(&item);
127 if let Some(target) = start_crate_target {
128 return Some(*target);
130 db.crate_graph()[start_crate]
133 .find_map(|dep| db.lang_item(dep.crate_id, item.clone()))
136 fn collect_lang_item<T>(
138 db: &dyn DefDatabase,
140 constructor: fn(T) -> LangItemTarget,
142 T: Into<AttrDefId> + Copy,
144 let _p = profile::span("collect_lang_item");
145 if let Some(lang_item_name) = lang_attr(db, item) {
146 self.items.entry(lang_item_name).or_insert_with(|| constructor(item));
151 pub fn lang_attr(db: &dyn DefDatabase, item: impl Into<AttrDefId> + Copy) -> Option<SmolStr> {
152 let attrs = db.attrs(item.into());
153 attrs.by_key("lang").string_value().cloned()