diff options
Diffstat (limited to 'crates')
-rw-r--r-- | crates/ra_hir/src/code_model.rs | 2 | ||||
-rw-r--r-- | crates/ra_hir/src/db.rs | 3 | ||||
-rw-r--r-- | crates/ra_hir/src/lang_item.rs | 50 |
3 files changed, 38 insertions, 17 deletions
diff --git a/crates/ra_hir/src/code_model.rs b/crates/ra_hir/src/code_model.rs index 10f975b31..ebbc37c0e 100644 --- a/crates/ra_hir/src/code_model.rs +++ b/crates/ra_hir/src/code_model.rs | |||
@@ -282,7 +282,7 @@ impl Module { | |||
282 | .collect() | 282 | .collect() |
283 | } | 283 | } |
284 | 284 | ||
285 | pub fn impl_blocks(self, db: &impl HirDatabase) -> Vec<ImplBlock> { | 285 | pub fn impl_blocks(self, db: &impl DefDatabase) -> Vec<ImplBlock> { |
286 | let module_impl_blocks = db.impls_in_module(self); | 286 | let module_impl_blocks = db.impls_in_module(self); |
287 | module_impl_blocks | 287 | module_impl_blocks |
288 | .impls | 288 | .impls |
diff --git a/crates/ra_hir/src/db.rs b/crates/ra_hir/src/db.rs index 23c36014b..d8832a9de 100644 --- a/crates/ra_hir/src/db.rs +++ b/crates/ra_hir/src/db.rs | |||
@@ -121,6 +121,9 @@ pub trait DefDatabase: SourceDatabase { | |||
121 | #[salsa::invoke(crate::ConstData::static_data_query)] | 121 | #[salsa::invoke(crate::ConstData::static_data_query)] |
122 | fn static_data(&self, konst: Static) -> Arc<ConstData>; | 122 | fn static_data(&self, konst: Static) -> Arc<ConstData>; |
123 | 123 | ||
124 | #[salsa::invoke(crate::lang_item::LangItems::module_lang_items_query)] | ||
125 | fn module_lang_items(&self, module: Module) -> Option<Arc<LangItems>>; | ||
126 | |||
124 | #[salsa::invoke(crate::lang_item::LangItems::lang_items_query)] | 127 | #[salsa::invoke(crate::lang_item::LangItems::lang_items_query)] |
125 | fn lang_items(&self, krate: Crate) -> Arc<LangItems>; | 128 | fn lang_items(&self, krate: Crate) -> Arc<LangItems>; |
126 | 129 | ||
diff --git a/crates/ra_hir/src/lang_item.rs b/crates/ra_hir/src/lang_item.rs index 18ac0fcf9..48b60f2dd 100644 --- a/crates/ra_hir/src/lang_item.rs +++ b/crates/ra_hir/src/lang_item.rs | |||
@@ -31,7 +31,7 @@ impl LangItemTarget { | |||
31 | } | 31 | } |
32 | } | 32 | } |
33 | 33 | ||
34 | #[derive(Debug, Clone, PartialEq, Eq)] | 34 | #[derive(Default, Debug, Clone, PartialEq, Eq)] |
35 | pub struct LangItems { | 35 | pub struct LangItems { |
36 | items: FxHashMap<SmolStr, LangItemTarget>, | 36 | items: FxHashMap<SmolStr, LangItemTarget>, |
37 | } | 37 | } |
@@ -46,15 +46,28 @@ impl LangItems { | |||
46 | db: &(impl DefDatabase + AstDatabase), | 46 | db: &(impl DefDatabase + AstDatabase), |
47 | krate: Crate, | 47 | krate: Crate, |
48 | ) -> Arc<LangItems> { | 48 | ) -> Arc<LangItems> { |
49 | let mut lang_items = LangItems { items: FxHashMap::default() }; | 49 | let mut lang_items = LangItems::default(); |
50 | 50 | ||
51 | if let Some(module) = krate.root_module(db) { | 51 | if let Some(module) = krate.root_module(db) { |
52 | lang_items.collect_lang_items_recursive(db, &module); | 52 | lang_items.collect_lang_items_recursive(db, module); |
53 | } | 53 | } |
54 | 54 | ||
55 | Arc::new(lang_items) | 55 | Arc::new(lang_items) |
56 | } | 56 | } |
57 | 57 | ||
58 | pub(crate) fn module_lang_items_query( | ||
59 | db: &(impl DefDatabase + AstDatabase), | ||
60 | module: Module, | ||
61 | ) -> Option<Arc<LangItems>> { | ||
62 | let mut lang_items = LangItems::default(); | ||
63 | lang_items.collect_lang_items(db, module); | ||
64 | if lang_items.items.is_empty() { | ||
65 | None | ||
66 | } else { | ||
67 | Some(Arc::new(lang_items)) | ||
68 | } | ||
69 | } | ||
70 | |||
58 | /// Salsa query. Look for a lang item, starting from the specified crate and recursively | 71 | /// Salsa query. Look for a lang item, starting from the specified crate and recursively |
59 | /// traversing its dependencies. | 72 | /// traversing its dependencies. |
60 | pub(crate) fn lang_item_query( | 73 | pub(crate) fn lang_item_query( |
@@ -78,19 +91,14 @@ impl LangItems { | |||
78 | } | 91 | } |
79 | } | 92 | } |
80 | 93 | ||
81 | fn collect_lang_items_recursive( | 94 | fn collect_lang_items(&mut self, db: &(impl DefDatabase + AstDatabase), module: Module) { |
82 | &mut self, | ||
83 | db: &(impl DefDatabase + AstDatabase), | ||
84 | module: &Module, | ||
85 | ) { | ||
86 | // Look for impl targets | 95 | // Look for impl targets |
87 | let (impl_blocks, source_map) = db.impls_in_module_with_source_map(module.clone()); | 96 | for impl_block in module.impl_blocks(db) { |
88 | let source = module.definition_source(db).ast; | 97 | let src = impl_block.source(db); |
89 | for (impl_id, _) in impl_blocks.impls.iter() { | 98 | if let Some(lang_item_name) = lang_item_name(&*src.ast) { |
90 | let impl_block = source_map.get(&source, impl_id); | 99 | self.items |
91 | if let Some(lang_item_name) = lang_item_name(&*impl_block) { | 100 | .entry(lang_item_name) |
92 | let imp = ImplBlock::from_id(*module, impl_id); | 101 | .or_insert_with(|| LangItemTarget::ImplBlock(impl_block)); |
93 | self.items.entry(lang_item_name).or_insert_with(|| LangItemTarget::ImplBlock(imp)); | ||
94 | } | 102 | } |
95 | } | 103 | } |
96 | 104 | ||
@@ -106,10 +114,20 @@ impl LangItems { | |||
106 | _ => {} | 114 | _ => {} |
107 | } | 115 | } |
108 | } | 116 | } |
117 | } | ||
118 | |||
119 | fn collect_lang_items_recursive( | ||
120 | &mut self, | ||
121 | db: &(impl DefDatabase + AstDatabase), | ||
122 | module: Module, | ||
123 | ) { | ||
124 | if let Some(module_lang_items) = db.module_lang_items(module) { | ||
125 | self.items.extend(module_lang_items.items.iter().map(|(k, v)| (k.clone(), v.clone()))) | ||
126 | } | ||
109 | 127 | ||
110 | // Look for lang items in the children | 128 | // Look for lang items in the children |
111 | for child in module.children(db) { | 129 | for child in module.children(db) { |
112 | self.collect_lang_items_recursive(db, &child); | 130 | self.collect_lang_items_recursive(db, child); |
113 | } | 131 | } |
114 | } | 132 | } |
115 | 133 | ||