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