aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_hir/src/code_model.rs
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_hir/src/code_model.rs')
-rw-r--r--crates/ra_hir/src/code_model.rs36
1 files changed, 20 insertions, 16 deletions
diff --git a/crates/ra_hir/src/code_model.rs b/crates/ra_hir/src/code_model.rs
index 5b78bdfef..181c5d47a 100644
--- a/crates/ra_hir/src/code_model.rs
+++ b/crates/ra_hir/src/code_model.rs
@@ -9,7 +9,7 @@ use hir_def::{
9 adt::VariantData, 9 adt::VariantData,
10 builtin_type::BuiltinType, 10 builtin_type::BuiltinType,
11 type_ref::{Mutability, TypeRef}, 11 type_ref::{Mutability, TypeRef},
12 CrateModuleId, LocalEnumVariantId, LocalStructFieldId, ModuleId, 12 CrateModuleId, LocalEnumVariantId, LocalStructFieldId, ModuleId, UnionId,
13}; 13};
14use hir_expand::{ 14use hir_expand::{
15 diagnostics::DiagnosticSink, 15 diagnostics::DiagnosticSink,
@@ -28,11 +28,10 @@ use crate::{
28 TypeAliasId, 28 TypeAliasId,
29 }, 29 },
30 impl_block::ImplBlock, 30 impl_block::ImplBlock,
31 nameres::{ImportId, ModuleScope, Namespace},
32 resolve::{Resolver, Scope, TypeNs}, 31 resolve::{Resolver, Scope, TypeNs},
33 traits::TraitData, 32 traits::TraitData,
34 ty::{InferenceResult, TraitRef}, 33 ty::{InferenceResult, TraitRef},
35 Either, HasSource, Name, Ty, 34 Either, HasSource, Name, ScopeDef, Ty, {ImportId, Namespace},
36}; 35};
37 36
38/// hir::Crate describes a single crate. It's the main interface with which 37/// hir::Crate describes a single crate. It's the main interface with which
@@ -66,7 +65,7 @@ impl Crate {
66 } 65 }
67 66
68 pub fn root_module(self, db: &impl DefDatabase) -> Option<Module> { 67 pub fn root_module(self, db: &impl DefDatabase) -> Option<Module> {
69 let module_id = db.crate_def_map(self).root(); 68 let module_id = db.crate_def_map(self.crate_id).root();
70 Some(Module::new(self, module_id)) 69 Some(Module::new(self, module_id))
71 } 70 }
72 71
@@ -120,7 +119,7 @@ impl Module {
120 119
121 /// Name of this module. 120 /// Name of this module.
122 pub fn name(self, db: &impl DefDatabase) -> Option<Name> { 121 pub fn name(self, db: &impl DefDatabase) -> Option<Name> {
123 let def_map = db.crate_def_map(self.krate()); 122 let def_map = db.crate_def_map(self.id.krate);
124 let parent = def_map[self.id.module_id].parent?; 123 let parent = def_map[self.id.module_id].parent?;
125 def_map[parent].children.iter().find_map(|(name, module_id)| { 124 def_map[parent].children.iter().find_map(|(name, module_id)| {
126 if *module_id == self.id.module_id { 125 if *module_id == self.id.module_id {
@@ -151,20 +150,20 @@ impl Module {
151 /// might be missing `krate`. This can happen if a module's file is not included 150 /// might be missing `krate`. This can happen if a module's file is not included
152 /// in the module tree of any target in `Cargo.toml`. 151 /// in the module tree of any target in `Cargo.toml`.
153 pub fn crate_root(self, db: &impl DefDatabase) -> Module { 152 pub fn crate_root(self, db: &impl DefDatabase) -> Module {
154 let def_map = db.crate_def_map(self.krate()); 153 let def_map = db.crate_def_map(self.id.krate);
155 self.with_module_id(def_map.root()) 154 self.with_module_id(def_map.root())
156 } 155 }
157 156
158 /// Finds a child module with the specified name. 157 /// Finds a child module with the specified name.
159 pub fn child(self, db: &impl HirDatabase, name: &Name) -> Option<Module> { 158 pub fn child(self, db: &impl HirDatabase, name: &Name) -> Option<Module> {
160 let def_map = db.crate_def_map(self.krate()); 159 let def_map = db.crate_def_map(self.id.krate);
161 let child_id = def_map[self.id.module_id].children.get(name)?; 160 let child_id = def_map[self.id.module_id].children.get(name)?;
162 Some(self.with_module_id(*child_id)) 161 Some(self.with_module_id(*child_id))
163 } 162 }
164 163
165 /// Iterates over all child modules. 164 /// Iterates over all child modules.
166 pub fn children(self, db: &impl DefDatabase) -> impl Iterator<Item = Module> { 165 pub fn children(self, db: &impl DefDatabase) -> impl Iterator<Item = Module> {
167 let def_map = db.crate_def_map(self.krate()); 166 let def_map = db.crate_def_map(self.id.krate);
168 let children = def_map[self.id.module_id] 167 let children = def_map[self.id.module_id]
169 .children 168 .children
170 .iter() 169 .iter()
@@ -175,7 +174,7 @@ impl Module {
175 174
176 /// Finds a parent module. 175 /// Finds a parent module.
177 pub fn parent(self, db: &impl DefDatabase) -> Option<Module> { 176 pub fn parent(self, db: &impl DefDatabase) -> Option<Module> {
178 let def_map = db.crate_def_map(self.krate()); 177 let def_map = db.crate_def_map(self.id.krate);
179 let parent_id = def_map[self.id.module_id].parent?; 178 let parent_id = def_map[self.id.module_id].parent?;
180 Some(self.with_module_id(parent_id)) 179 Some(self.with_module_id(parent_id))
181 } 180 }
@@ -191,12 +190,16 @@ impl Module {
191 } 190 }
192 191
193 /// Returns a `ModuleScope`: a set of items, visible in this module. 192 /// Returns a `ModuleScope`: a set of items, visible in this module.
194 pub fn scope(self, db: &impl HirDatabase) -> ModuleScope { 193 pub fn scope(self, db: &impl HirDatabase) -> Vec<(Name, ScopeDef, Option<ImportId>)> {
195 db.crate_def_map(self.krate())[self.id.module_id].scope.clone() 194 db.crate_def_map(self.id.krate)[self.id.module_id]
195 .scope
196 .entries()
197 .map(|(name, res)| (name.clone(), res.def.into(), res.import))
198 .collect()
196 } 199 }
197 200
198 pub fn diagnostics(self, db: &impl HirDatabase, sink: &mut DiagnosticSink) { 201 pub fn diagnostics(self, db: &impl HirDatabase, sink: &mut DiagnosticSink) {
199 db.crate_def_map(self.krate()).add_diagnostics(db, self.id.module_id, sink); 202 db.crate_def_map(self.id.krate).add_diagnostics(db, self.id.module_id, sink);
200 for decl in self.declarations(db) { 203 for decl in self.declarations(db) {
201 match decl { 204 match decl {
202 crate::ModuleDef::Function(f) => f.diagnostics(db, sink), 205 crate::ModuleDef::Function(f) => f.diagnostics(db, sink),
@@ -220,12 +223,12 @@ impl Module {
220 } 223 }
221 224
222 pub(crate) fn resolver(self, db: &impl DefDatabase) -> Resolver { 225 pub(crate) fn resolver(self, db: &impl DefDatabase) -> Resolver {
223 let def_map = db.crate_def_map(self.krate()); 226 let def_map = db.crate_def_map(self.id.krate);
224 Resolver::default().push_module_scope(def_map, self.id.module_id) 227 Resolver::default().push_module_scope(def_map, self.id.module_id)
225 } 228 }
226 229
227 pub fn declarations(self, db: &impl DefDatabase) -> Vec<ModuleDef> { 230 pub fn declarations(self, db: &impl DefDatabase) -> Vec<ModuleDef> {
228 let def_map = db.crate_def_map(self.krate()); 231 let def_map = db.crate_def_map(self.id.krate);
229 def_map[self.id.module_id] 232 def_map[self.id.module_id]
230 .scope 233 .scope
231 .entries() 234 .entries()
@@ -233,6 +236,7 @@ impl Module {
233 .flat_map(|per_ns| { 236 .flat_map(|per_ns| {
234 per_ns.take_types().into_iter().chain(per_ns.take_values().into_iter()) 237 per_ns.take_types().into_iter().chain(per_ns.take_values().into_iter())
235 }) 238 })
239 .map(ModuleDef::from)
236 .collect() 240 .collect()
237 } 241 }
238 242
@@ -336,12 +340,12 @@ impl Struct {
336 340
337#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] 341#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
338pub struct Union { 342pub struct Union {
339 pub(crate) id: StructId, 343 pub(crate) id: UnionId,
340} 344}
341 345
342impl Union { 346impl Union {
343 pub fn name(self, db: &impl DefDatabase) -> Option<Name> { 347 pub fn name(self, db: &impl DefDatabase) -> Option<Name> {
344 db.struct_data(self.id).name.clone() 348 db.union_data(self.id).name.clone()
345 } 349 }
346 350
347 pub fn module(self, db: &impl HirDatabase) -> Module { 351 pub fn module(self, db: &impl HirDatabase) -> Module {