diff options
author | Aleksey Kladov <[email protected]> | 2019-12-22 14:37:07 +0000 |
---|---|---|
committer | Aleksey Kladov <[email protected]> | 2019-12-22 14:37:53 +0000 |
commit | 6c3ddcfa501060cff3a7f81c179f712ef072c808 (patch) | |
tree | 513bb8b0a4ecb487dc1cf7be4bbc05c84ce9a975 /crates/ra_hir_def/src/item_scope.rs | |
parent | e8da7d4061960844502e3064c33eef4a0dc3828e (diff) |
Simplify
Diffstat (limited to 'crates/ra_hir_def/src/item_scope.rs')
-rw-r--r-- | crates/ra_hir_def/src/item_scope.rs | 44 |
1 files changed, 18 insertions, 26 deletions
diff --git a/crates/ra_hir_def/src/item_scope.rs b/crates/ra_hir_def/src/item_scope.rs index 71f44b556..f1adc3b58 100644 --- a/crates/ra_hir_def/src/item_scope.rs +++ b/crates/ra_hir_def/src/item_scope.rs | |||
@@ -9,7 +9,7 @@ use crate::{per_ns::PerNs, AdtId, BuiltinType, ImplId, MacroDefId, ModuleDefId, | |||
9 | 9 | ||
10 | #[derive(Debug, Default, PartialEq, Eq)] | 10 | #[derive(Debug, Default, PartialEq, Eq)] |
11 | pub struct ItemScope { | 11 | pub struct ItemScope { |
12 | visible: FxHashMap<Name, Resolution>, | 12 | visible: FxHashMap<Name, PerNs>, |
13 | defs: Vec<ModuleDefId>, | 13 | defs: Vec<ModuleDefId>, |
14 | impls: Vec<ImplId>, | 14 | impls: Vec<ImplId>, |
15 | /// Macros visible in current module in legacy textual scope | 15 | /// Macros visible in current module in legacy textual scope |
@@ -27,10 +27,10 @@ pub struct ItemScope { | |||
27 | legacy_macros: FxHashMap<Name, MacroDefId>, | 27 | legacy_macros: FxHashMap<Name, MacroDefId>, |
28 | } | 28 | } |
29 | 29 | ||
30 | static BUILTIN_SCOPE: Lazy<FxHashMap<Name, Resolution>> = Lazy::new(|| { | 30 | static BUILTIN_SCOPE: Lazy<FxHashMap<Name, PerNs>> = Lazy::new(|| { |
31 | BuiltinType::ALL | 31 | BuiltinType::ALL |
32 | .iter() | 32 | .iter() |
33 | .map(|(name, ty)| (name.clone(), Resolution { def: PerNs::types(ty.clone().into()) })) | 33 | .map(|(name, ty)| (name.clone(), PerNs::types(ty.clone().into()))) |
34 | .collect() | 34 | .collect() |
35 | }); | 35 | }); |
36 | 36 | ||
@@ -46,9 +46,9 @@ pub(crate) enum BuiltinShadowMode { | |||
46 | /// Legacy macros can only be accessed through special methods like `get_legacy_macros`. | 46 | /// Legacy macros can only be accessed through special methods like `get_legacy_macros`. |
47 | /// Other methods will only resolve values, types and module scoped macros only. | 47 | /// Other methods will only resolve values, types and module scoped macros only. |
48 | impl ItemScope { | 48 | impl ItemScope { |
49 | pub fn entries<'a>(&'a self) -> impl Iterator<Item = (&'a Name, &'a Resolution)> + 'a { | 49 | pub fn entries<'a>(&'a self) -> impl Iterator<Item = (&'a Name, PerNs)> + 'a { |
50 | //FIXME: shadowing | 50 | //FIXME: shadowing |
51 | self.visible.iter().chain(BUILTIN_SCOPE.iter()) | 51 | self.visible.iter().chain(BUILTIN_SCOPE.iter()).map(|(n, def)| (n, *def)) |
52 | } | 52 | } |
53 | 53 | ||
54 | pub fn declarations(&self) -> impl Iterator<Item = ModuleDefId> + '_ { | 54 | pub fn declarations(&self) -> impl Iterator<Item = ModuleDefId> + '_ { |
@@ -61,9 +61,7 @@ impl ItemScope { | |||
61 | 61 | ||
62 | /// Iterate over all module scoped macros | 62 | /// Iterate over all module scoped macros |
63 | pub(crate) fn macros<'a>(&'a self) -> impl Iterator<Item = (&'a Name, MacroDefId)> + 'a { | 63 | pub(crate) fn macros<'a>(&'a self) -> impl Iterator<Item = (&'a Name, MacroDefId)> + 'a { |
64 | self.visible | 64 | self.visible.iter().filter_map(|(name, def)| def.take_macros().map(|macro_| (name, macro_))) |
65 | .iter() | ||
66 | .filter_map(|(name, res)| res.def.take_macros().map(|macro_| (name, macro_))) | ||
67 | } | 65 | } |
68 | 66 | ||
69 | /// Iterate over all legacy textual scoped macros visible at the end of the module | 67 | /// Iterate over all legacy textual scoped macros visible at the end of the module |
@@ -72,13 +70,13 @@ impl ItemScope { | |||
72 | } | 70 | } |
73 | 71 | ||
74 | /// Get a name from current module scope, legacy macros are not included | 72 | /// Get a name from current module scope, legacy macros are not included |
75 | pub(crate) fn get(&self, name: &Name, shadow: BuiltinShadowMode) -> Option<&Resolution> { | 73 | pub(crate) fn get(&self, name: &Name, shadow: BuiltinShadowMode) -> Option<&PerNs> { |
76 | match shadow { | 74 | match shadow { |
77 | BuiltinShadowMode::Module => self.visible.get(name).or_else(|| BUILTIN_SCOPE.get(name)), | 75 | BuiltinShadowMode::Module => self.visible.get(name).or_else(|| BUILTIN_SCOPE.get(name)), |
78 | BuiltinShadowMode::Other => { | 76 | BuiltinShadowMode::Other => { |
79 | let item = self.visible.get(name); | 77 | let item = self.visible.get(name); |
80 | if let Some(res) = item { | 78 | if let Some(def) = item { |
81 | if let Some(ModuleDefId::ModuleId(_)) = res.def.take_types() { | 79 | if let Some(ModuleDefId::ModuleId(_)) = def.take_types() { |
82 | return BUILTIN_SCOPE.get(name).or(item); | 80 | return BUILTIN_SCOPE.get(name).or(item); |
83 | } | 81 | } |
84 | } | 82 | } |
@@ -89,7 +87,7 @@ impl ItemScope { | |||
89 | } | 87 | } |
90 | 88 | ||
91 | pub(crate) fn traits<'a>(&'a self) -> impl Iterator<Item = TraitId> + 'a { | 89 | pub(crate) fn traits<'a>(&'a self) -> impl Iterator<Item = TraitId> + 'a { |
92 | self.visible.values().filter_map(|r| match r.def.take_types() { | 90 | self.visible.values().filter_map(|def| match def.take_types() { |
93 | Some(ModuleDefId::TraitId(t)) => Some(t), | 91 | Some(ModuleDefId::TraitId(t)) => Some(t), |
94 | _ => None, | 92 | _ => None, |
95 | }) | 93 | }) |
@@ -111,27 +109,27 @@ impl ItemScope { | |||
111 | self.legacy_macros.insert(name, mac); | 109 | self.legacy_macros.insert(name, mac); |
112 | } | 110 | } |
113 | 111 | ||
114 | pub(crate) fn push_res(&mut self, name: Name, res: &Resolution) -> bool { | 112 | pub(crate) fn push_res(&mut self, name: Name, def: &PerNs) -> bool { |
115 | let mut changed = false; | 113 | let mut changed = false; |
116 | let existing = self.visible.entry(name.clone()).or_default(); | 114 | let existing = self.visible.entry(name.clone()).or_default(); |
117 | 115 | ||
118 | if existing.def.types.is_none() && res.def.types.is_some() { | 116 | if existing.types.is_none() && def.types.is_some() { |
119 | existing.def.types = res.def.types; | 117 | existing.types = def.types; |
120 | changed = true; | 118 | changed = true; |
121 | } | 119 | } |
122 | if existing.def.values.is_none() && res.def.values.is_some() { | 120 | if existing.values.is_none() && def.values.is_some() { |
123 | existing.def.values = res.def.values; | 121 | existing.values = def.values; |
124 | changed = true; | 122 | changed = true; |
125 | } | 123 | } |
126 | if existing.def.macros.is_none() && res.def.macros.is_some() { | 124 | if existing.macros.is_none() && def.macros.is_some() { |
127 | existing.def.macros = res.def.macros; | 125 | existing.macros = def.macros; |
128 | changed = true; | 126 | changed = true; |
129 | } | 127 | } |
130 | 128 | ||
131 | changed | 129 | changed |
132 | } | 130 | } |
133 | 131 | ||
134 | pub(crate) fn collect_resolutions(&self) -> Vec<(Name, Resolution)> { | 132 | pub(crate) fn collect_resolutions(&self) -> Vec<(Name, PerNs)> { |
135 | self.visible.iter().map(|(name, res)| (name.clone(), res.clone())).collect() | 133 | self.visible.iter().map(|(name, res)| (name.clone(), res.clone())).collect() |
136 | } | 134 | } |
137 | 135 | ||
@@ -140,12 +138,6 @@ impl ItemScope { | |||
140 | } | 138 | } |
141 | } | 139 | } |
142 | 140 | ||
143 | #[derive(Debug, Clone, PartialEq, Eq, Default)] | ||
144 | pub struct Resolution { | ||
145 | /// None for unresolved | ||
146 | pub def: PerNs, | ||
147 | } | ||
148 | |||
149 | impl From<ModuleDefId> for PerNs { | 141 | impl From<ModuleDefId> for PerNs { |
150 | fn from(def: ModuleDefId) -> PerNs { | 142 | fn from(def: ModuleDefId) -> PerNs { |
151 | match def { | 143 | match def { |