aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_hir_def/src/item_scope.rs
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_hir_def/src/item_scope.rs')
-rw-r--r--crates/ra_hir_def/src/item_scope.rs105
1 files changed, 105 insertions, 0 deletions
diff --git a/crates/ra_hir_def/src/item_scope.rs b/crates/ra_hir_def/src/item_scope.rs
index 8b1378917..62e7a02cb 100644
--- a/crates/ra_hir_def/src/item_scope.rs
+++ b/crates/ra_hir_def/src/item_scope.rs
@@ -1 +1,106 @@
1use hir_expand::name::Name;
2use once_cell::sync::Lazy;
3use rustc_hash::FxHashMap;
1 4
5use crate::{per_ns::PerNs, BuiltinType, LocalImportId, MacroDefId, ModuleDefId, TraitId};
6
7#[derive(Debug, Default, PartialEq, Eq)]
8pub struct ModuleScope {
9 pub(crate) items: FxHashMap<Name, Resolution>,
10 /// Macros visable in current module in legacy textual scope
11 ///
12 /// For macros invoked by an unquatified identifier like `bar!()`, `legacy_macros` will be searched in first.
13 /// If it yields no result, then it turns to module scoped `macros`.
14 /// It macros with name quatified with a path like `crate::foo::bar!()`, `legacy_macros` will be skipped,
15 /// and only normal scoped `macros` will be searched in.
16 ///
17 /// Note that this automatically inherit macros defined textually before the definition of module itself.
18 ///
19 /// Module scoped macros will be inserted into `items` instead of here.
20 // FIXME: Macro shadowing in one module is not properly handled. Non-item place macros will
21 // be all resolved to the last one defined if shadowing happens.
22 pub(crate) legacy_macros: FxHashMap<Name, MacroDefId>,
23}
24
25static BUILTIN_SCOPE: Lazy<FxHashMap<Name, Resolution>> = Lazy::new(|| {
26 BuiltinType::ALL
27 .iter()
28 .map(|(name, ty)| {
29 (name.clone(), Resolution { def: PerNs::types(ty.clone().into()), import: None })
30 })
31 .collect()
32});
33
34/// Shadow mode for builtin type which can be shadowed by module.
35#[derive(Debug, Copy, Clone, PartialEq, Eq)]
36pub enum BuiltinShadowMode {
37 // Prefer Module
38 Module,
39 // Prefer Other Types
40 Other,
41}
42
43/// Legacy macros can only be accessed through special methods like `get_legacy_macros`.
44/// Other methods will only resolve values, types and module scoped macros only.
45impl ModuleScope {
46 pub fn entries<'a>(&'a self) -> impl Iterator<Item = (&'a Name, &'a Resolution)> + 'a {
47 //FIXME: shadowing
48 self.items.iter().chain(BUILTIN_SCOPE.iter())
49 }
50
51 pub fn declarations(&self) -> impl Iterator<Item = ModuleDefId> + '_ {
52 self.entries()
53 .filter_map(|(_name, res)| if res.import.is_none() { Some(res.def) } else { None })
54 .flat_map(|per_ns| {
55 per_ns.take_types().into_iter().chain(per_ns.take_values().into_iter())
56 })
57 }
58
59 /// Iterate over all module scoped macros
60 pub fn macros<'a>(&'a self) -> impl Iterator<Item = (&'a Name, MacroDefId)> + 'a {
61 self.items
62 .iter()
63 .filter_map(|(name, res)| res.def.take_macros().map(|macro_| (name, macro_)))
64 }
65
66 /// Iterate over all legacy textual scoped macros visable at the end of the module
67 pub fn legacy_macros<'a>(&'a self) -> impl Iterator<Item = (&'a Name, MacroDefId)> + 'a {
68 self.legacy_macros.iter().map(|(name, def)| (name, *def))
69 }
70
71 /// Get a name from current module scope, legacy macros are not included
72 pub fn get(&self, name: &Name, shadow: BuiltinShadowMode) -> Option<&Resolution> {
73 match shadow {
74 BuiltinShadowMode::Module => self.items.get(name).or_else(|| BUILTIN_SCOPE.get(name)),
75 BuiltinShadowMode::Other => {
76 let item = self.items.get(name);
77 if let Some(res) = item {
78 if let Some(ModuleDefId::ModuleId(_)) = res.def.take_types() {
79 return BUILTIN_SCOPE.get(name).or(item);
80 }
81 }
82
83 item.or_else(|| BUILTIN_SCOPE.get(name))
84 }
85 }
86 }
87
88 pub fn traits<'a>(&'a self) -> impl Iterator<Item = TraitId> + 'a {
89 self.items.values().filter_map(|r| match r.def.take_types() {
90 Some(ModuleDefId::TraitId(t)) => Some(t),
91 _ => None,
92 })
93 }
94
95 pub(crate) fn get_legacy_macro(&self, name: &Name) -> Option<MacroDefId> {
96 self.legacy_macros.get(name).copied()
97 }
98}
99
100#[derive(Debug, Clone, PartialEq, Eq, Default)]
101pub struct Resolution {
102 /// None for unresolved
103 pub def: PerNs,
104 /// ident by which this is imported into local scope.
105 pub import: Option<LocalImportId>,
106}