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, EnumVariantId, FunctionId, ImplId,
12 ModuleDefId, StaticId, StructId, TraitId,
15 #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
16 pub enum LangItemTarget {
18 FunctionId(FunctionId),
23 EnumVariantId(EnumVariantId),
27 pub fn as_enum(self) -> Option<EnumId> {
29 LangItemTarget::EnumId(id) => Some(id),
34 pub fn as_function(self) -> Option<FunctionId> {
36 LangItemTarget::FunctionId(id) => Some(id),
41 pub fn as_impl_def(self) -> Option<ImplId> {
43 LangItemTarget::ImplDefId(id) => Some(id),
48 pub fn as_static(self) -> Option<StaticId> {
50 LangItemTarget::StaticId(id) => Some(id),
55 pub fn as_struct(self) -> Option<StructId> {
57 LangItemTarget::StructId(id) => Some(id),
62 pub fn as_trait(self) -> Option<TraitId> {
64 LangItemTarget::TraitId(id) => Some(id),
69 pub fn as_enum_variant(self) -> Option<EnumVariantId> {
71 LangItemTarget::EnumVariantId(id) => Some(id),
77 #[derive(Default, Debug, Clone, PartialEq, Eq)]
78 pub struct LangItems {
79 items: FxHashMap<SmolStr, LangItemTarget>,
83 pub fn target(&self, item: &str) -> Option<LangItemTarget> {
84 self.items.get(item).copied()
87 /// Salsa query. This will look for lang items in a specific crate.
88 pub(crate) fn crate_lang_items_query(db: &dyn DefDatabase, krate: CrateId) -> Arc<LangItems> {
89 let _p = profile::span("crate_lang_items_query");
91 let mut lang_items = LangItems::default();
93 let crate_def_map = db.crate_def_map(krate);
95 for (_, module_data) in crate_def_map.modules() {
96 for impl_def in module_data.scope.impls() {
97 lang_items.collect_lang_item(db, impl_def, LangItemTarget::ImplDefId)
100 for def in module_data.scope.declarations() {
102 ModuleDefId::TraitId(trait_) => {
103 lang_items.collect_lang_item(db, trait_, LangItemTarget::TraitId);
104 db.trait_data(trait_).items.iter().for_each(|&(_, assoc_id)| {
105 if let crate::AssocItemId::FunctionId(f) = assoc_id {
106 lang_items.collect_lang_item(db, f, LangItemTarget::FunctionId);
110 ModuleDefId::AdtId(AdtId::EnumId(e)) => {
111 lang_items.collect_lang_item(db, e, LangItemTarget::EnumId);
112 db.enum_data(e).variants.iter().for_each(|(local_id, _)| {
113 lang_items.collect_lang_item(
115 EnumVariantId { parent: e, local_id },
116 LangItemTarget::EnumVariantId,
120 ModuleDefId::AdtId(AdtId::StructId(s)) => {
121 lang_items.collect_lang_item(db, s, LangItemTarget::StructId);
123 ModuleDefId::FunctionId(f) => {
124 lang_items.collect_lang_item(db, f, LangItemTarget::FunctionId);
126 ModuleDefId::StaticId(s) => {
127 lang_items.collect_lang_item(db, s, LangItemTarget::StaticId);
137 /// Salsa query. Look for a lang item, starting from the specified crate and recursively
138 /// traversing its dependencies.
139 pub(crate) fn lang_item_query(
140 db: &dyn DefDatabase,
141 start_crate: CrateId,
143 ) -> Option<LangItemTarget> {
144 let _p = profile::span("lang_item_query");
145 let lang_items = db.crate_lang_items(start_crate);
146 let start_crate_target = lang_items.items.get(&item);
147 if let Some(target) = start_crate_target {
148 return Some(*target);
150 db.crate_graph()[start_crate]
153 .find_map(|dep| db.lang_item(dep.crate_id, item.clone()))
156 fn collect_lang_item<T>(
158 db: &dyn DefDatabase,
160 constructor: fn(T) -> LangItemTarget,
162 T: Into<AttrDefId> + Copy,
164 let _p = profile::span("collect_lang_item");
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()