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, ModuleId,
12 StaticId, StructId, TraitId,
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);
90 .filter_map(|(local_id, _)| db.module_lang_items(ModuleId { krate, local_id }))
91 .for_each(|it| lang_items.items.extend(it.items.iter().map(|(k, v)| (k.clone(), *v))));
96 pub(crate) fn module_lang_items_query(
99 ) -> Option<Arc<LangItems>> {
100 let _p = profile::span("module_lang_items_query");
101 let mut lang_items = LangItems::default();
102 lang_items.collect_lang_items(db, module);
103 if lang_items.items.is_empty() {
106 Some(Arc::new(lang_items))
110 /// Salsa query. Look for a lang item, starting from the specified crate and recursively
111 /// traversing its dependencies.
112 pub(crate) fn lang_item_query(
113 db: &dyn DefDatabase,
114 start_crate: CrateId,
116 ) -> Option<LangItemTarget> {
117 let _p = profile::span("lang_item_query");
118 let lang_items = db.crate_lang_items(start_crate);
119 let start_crate_target = lang_items.items.get(&item);
120 if let Some(target) = start_crate_target {
121 return Some(*target);
123 db.crate_graph()[start_crate]
126 .find_map(|dep| db.lang_item(dep.crate_id, item.clone()))
129 fn collect_lang_items(&mut self, db: &dyn DefDatabase, module: ModuleId) {
130 // Look for impl targets
131 let def_map = db.crate_def_map(module.krate);
132 let module_data = &def_map[module.local_id];
133 for impl_def in module_data.scope.impls() {
134 self.collect_lang_item(db, impl_def, LangItemTarget::ImplDefId)
137 for def in module_data.scope.declarations() {
139 ModuleDefId::TraitId(trait_) => {
140 self.collect_lang_item(db, trait_, LangItemTarget::TraitId)
142 ModuleDefId::AdtId(AdtId::EnumId(e)) => {
143 self.collect_lang_item(db, e, LangItemTarget::EnumId)
145 ModuleDefId::AdtId(AdtId::StructId(s)) => {
146 self.collect_lang_item(db, s, LangItemTarget::StructId)
148 ModuleDefId::FunctionId(f) => {
149 self.collect_lang_item(db, f, LangItemTarget::FunctionId)
151 ModuleDefId::StaticId(s) => self.collect_lang_item(db, s, LangItemTarget::StaticId),
157 fn collect_lang_item<T>(
159 db: &dyn DefDatabase,
161 constructor: fn(T) -> LangItemTarget,
163 T: Into<AttrDefId> + Copy,
165 if let Some(lang_item_name) = lang_attr(db, item) {
166 self.items.entry(lang_item_name).or_insert_with(|| constructor(item));
171 pub fn lang_attr(db: &dyn DefDatabase, item: impl Into<AttrDefId> + Copy) -> Option<SmolStr> {
172 let attrs = db.attrs(item.into());
173 attrs.by_key("lang").string_value().cloned()