aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_hir_def/src/nameres.rs
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_hir_def/src/nameres.rs')
-rw-r--r--crates/ra_hir_def/src/nameres.rs112
1 files changed, 4 insertions, 108 deletions
diff --git a/crates/ra_hir_def/src/nameres.rs b/crates/ra_hir_def/src/nameres.rs
index af52fa36e..5d4ca73a3 100644
--- a/crates/ra_hir_def/src/nameres.rs
+++ b/crates/ra_hir_def/src/nameres.rs
@@ -57,8 +57,7 @@ mod tests;
57 57
58use std::sync::Arc; 58use std::sync::Arc;
59 59
60use hir_expand::{diagnostics::DiagnosticSink, name::Name, InFile, MacroDefId}; 60use hir_expand::{diagnostics::DiagnosticSink, name::Name, InFile};
61use once_cell::sync::Lazy;
62use ra_arena::Arena; 61use ra_arena::Arena;
63use ra_db::{CrateId, Edition, FileId, FilePosition}; 62use ra_db::{CrateId, Edition, FileId, FilePosition};
64use ra_prof::profile; 63use ra_prof::profile;
@@ -69,12 +68,12 @@ use ra_syntax::{
69use rustc_hash::FxHashMap; 68use rustc_hash::FxHashMap;
70 69
71use crate::{ 70use crate::{
72 builtin_type::BuiltinType,
73 db::DefDatabase, 71 db::DefDatabase,
72 item_scope::{BuiltinShadowMode, ItemScope},
74 nameres::{diagnostics::DefDiagnostic, path_resolution::ResolveMode}, 73 nameres::{diagnostics::DefDiagnostic, path_resolution::ResolveMode},
75 path::ModPath, 74 path::ModPath,
76 per_ns::PerNs, 75 per_ns::PerNs,
77 AstId, ImplId, LocalImportId, LocalModuleId, ModuleDefId, ModuleId, TraitId, 76 AstId, LocalModuleId, ModuleDefId, ModuleId,
78}; 77};
79 78
80/// Contains all top-level defs from a macro-expanded crate 79/// Contains all top-level defs from a macro-expanded crate
@@ -166,113 +165,10 @@ impl ModuleOrigin {
166pub struct ModuleData { 165pub struct ModuleData {
167 pub parent: Option<LocalModuleId>, 166 pub parent: Option<LocalModuleId>,
168 pub children: FxHashMap<Name, LocalModuleId>, 167 pub children: FxHashMap<Name, LocalModuleId>,
169 pub scope: ModuleScope, 168 pub scope: ItemScope,
170 169
171 /// Where does this module come from? 170 /// Where does this module come from?
172 pub origin: ModuleOrigin, 171 pub origin: ModuleOrigin,
173
174 pub impls: Vec<ImplId>,
175}
176
177#[derive(Debug, Default, PartialEq, Eq)]
178pub struct ModuleScope {
179 items: FxHashMap<Name, Resolution>,
180 /// Macros visable in current module in legacy textual scope
181 ///
182 /// For macros invoked by an unquatified identifier like `bar!()`, `legacy_macros` will be searched in first.
183 /// If it yields no result, then it turns to module scoped `macros`.
184 /// It macros with name quatified with a path like `crate::foo::bar!()`, `legacy_macros` will be skipped,
185 /// and only normal scoped `macros` will be searched in.
186 ///
187 /// Note that this automatically inherit macros defined textually before the definition of module itself.
188 ///
189 /// Module scoped macros will be inserted into `items` instead of here.
190 // FIXME: Macro shadowing in one module is not properly handled. Non-item place macros will
191 // be all resolved to the last one defined if shadowing happens.
192 legacy_macros: FxHashMap<Name, MacroDefId>,
193}
194
195static BUILTIN_SCOPE: Lazy<FxHashMap<Name, Resolution>> = Lazy::new(|| {
196 BuiltinType::ALL
197 .iter()
198 .map(|(name, ty)| {
199 (name.clone(), Resolution { def: PerNs::types(ty.clone().into()), import: None })
200 })
201 .collect()
202});
203
204/// Shadow mode for builtin type which can be shadowed by module.
205#[derive(Debug, Copy, Clone, PartialEq, Eq)]
206pub enum BuiltinShadowMode {
207 // Prefer Module
208 Module,
209 // Prefer Other Types
210 Other,
211}
212
213/// Legacy macros can only be accessed through special methods like `get_legacy_macros`.
214/// Other methods will only resolve values, types and module scoped macros only.
215impl ModuleScope {
216 pub fn entries<'a>(&'a self) -> impl Iterator<Item = (&'a Name, &'a Resolution)> + 'a {
217 //FIXME: shadowing
218 self.items.iter().chain(BUILTIN_SCOPE.iter())
219 }
220
221 pub fn declarations(&self) -> impl Iterator<Item = ModuleDefId> + '_ {
222 self.entries()
223 .filter_map(|(_name, res)| if res.import.is_none() { Some(res.def) } else { None })
224 .flat_map(|per_ns| {
225 per_ns.take_types().into_iter().chain(per_ns.take_values().into_iter())
226 })
227 }
228
229 /// Iterate over all module scoped macros
230 pub fn macros<'a>(&'a self) -> impl Iterator<Item = (&'a Name, MacroDefId)> + 'a {
231 self.items
232 .iter()
233 .filter_map(|(name, res)| res.def.take_macros().map(|macro_| (name, macro_)))
234 }
235
236 /// Iterate over all legacy textual scoped macros visable at the end of the module
237 pub fn legacy_macros<'a>(&'a self) -> impl Iterator<Item = (&'a Name, MacroDefId)> + 'a {
238 self.legacy_macros.iter().map(|(name, def)| (name, *def))
239 }
240
241 /// Get a name from current module scope, legacy macros are not included
242 pub fn get(&self, name: &Name, shadow: BuiltinShadowMode) -> Option<&Resolution> {
243 match shadow {
244 BuiltinShadowMode::Module => self.items.get(name).or_else(|| BUILTIN_SCOPE.get(name)),
245 BuiltinShadowMode::Other => {
246 let item = self.items.get(name);
247 if let Some(res) = item {
248 if let Some(ModuleDefId::ModuleId(_)) = res.def.take_types() {
249 return BUILTIN_SCOPE.get(name).or(item);
250 }
251 }
252
253 item.or_else(|| BUILTIN_SCOPE.get(name))
254 }
255 }
256 }
257
258 pub fn traits<'a>(&'a self) -> impl Iterator<Item = TraitId> + 'a {
259 self.items.values().filter_map(|r| match r.def.take_types() {
260 Some(ModuleDefId::TraitId(t)) => Some(t),
261 _ => None,
262 })
263 }
264
265 fn get_legacy_macro(&self, name: &Name) -> Option<MacroDefId> {
266 self.legacy_macros.get(name).copied()
267 }
268}
269
270#[derive(Debug, Clone, PartialEq, Eq, Default)]
271pub struct Resolution {
272 /// None for unresolved
273 pub def: PerNs,
274 /// ident by which this is imported into local scope.
275 pub import: Option<LocalImportId>,
276} 172}
277 173
278impl CrateDefMap { 174impl CrateDefMap {