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.rs42
1 files changed, 24 insertions, 18 deletions
diff --git a/crates/ra_hir/src/code_model.rs b/crates/ra_hir/src/code_model.rs
index c97ea18a2..181c5d47a 100644
--- a/crates/ra_hir/src/code_model.rs
+++ b/crates/ra_hir/src/code_model.rs
@@ -9,16 +9,18 @@ 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};
14use hir_expand::{
15 diagnostics::DiagnosticSink,
16 name::{self, AsName},
13}; 17};
14use hir_expand::name::{self, AsName};
15use ra_db::{CrateId, Edition}; 18use ra_db::{CrateId, Edition};
16use ra_syntax::ast::{self, NameOwner, TypeAscriptionOwner}; 19use ra_syntax::ast::{self, NameOwner, TypeAscriptionOwner};
17 20
18use crate::{ 21use crate::{
19 adt::VariantDef, 22 adt::VariantDef,
20 db::{AstDatabase, DefDatabase, HirDatabase}, 23 db::{AstDatabase, DefDatabase, HirDatabase},
21 diagnostics::DiagnosticSink,
22 expr::{validation::ExprValidator, Body, BodySourceMap}, 24 expr::{validation::ExprValidator, Body, BodySourceMap},
23 generics::HasGenericParams, 25 generics::HasGenericParams,
24 ids::{ 26 ids::{
@@ -26,11 +28,10 @@ use crate::{
26 TypeAliasId, 28 TypeAliasId,
27 }, 29 },
28 impl_block::ImplBlock, 30 impl_block::ImplBlock,
29 nameres::{ImportId, ModuleScope, Namespace},
30 resolve::{Resolver, Scope, TypeNs}, 31 resolve::{Resolver, Scope, TypeNs},
31 traits::TraitData, 32 traits::TraitData,
32 ty::{InferenceResult, TraitRef}, 33 ty::{InferenceResult, TraitRef},
33 Either, HasSource, Name, Ty, 34 Either, HasSource, Name, ScopeDef, Ty, {ImportId, Namespace},
34}; 35};
35 36
36/// 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
@@ -64,7 +65,7 @@ impl Crate {
64 } 65 }
65 66
66 pub fn root_module(self, db: &impl DefDatabase) -> Option<Module> { 67 pub fn root_module(self, db: &impl DefDatabase) -> Option<Module> {
67 let module_id = db.crate_def_map(self).root(); 68 let module_id = db.crate_def_map(self.crate_id).root();
68 Some(Module::new(self, module_id)) 69 Some(Module::new(self, module_id))
69 } 70 }
70 71
@@ -118,7 +119,7 @@ impl Module {
118 119
119 /// Name of this module. 120 /// Name of this module.
120 pub fn name(self, db: &impl DefDatabase) -> Option<Name> { 121 pub fn name(self, db: &impl DefDatabase) -> Option<Name> {
121 let def_map = db.crate_def_map(self.krate()); 122 let def_map = db.crate_def_map(self.id.krate);
122 let parent = def_map[self.id.module_id].parent?; 123 let parent = def_map[self.id.module_id].parent?;
123 def_map[parent].children.iter().find_map(|(name, module_id)| { 124 def_map[parent].children.iter().find_map(|(name, module_id)| {
124 if *module_id == self.id.module_id { 125 if *module_id == self.id.module_id {
@@ -149,20 +150,20 @@ impl Module {
149 /// 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
150 /// in the module tree of any target in `Cargo.toml`. 151 /// in the module tree of any target in `Cargo.toml`.
151 pub fn crate_root(self, db: &impl DefDatabase) -> Module { 152 pub fn crate_root(self, db: &impl DefDatabase) -> Module {
152 let def_map = db.crate_def_map(self.krate()); 153 let def_map = db.crate_def_map(self.id.krate);
153 self.with_module_id(def_map.root()) 154 self.with_module_id(def_map.root())
154 } 155 }
155 156
156 /// Finds a child module with the specified name. 157 /// Finds a child module with the specified name.
157 pub fn child(self, db: &impl HirDatabase, name: &Name) -> Option<Module> { 158 pub fn child(self, db: &impl HirDatabase, name: &Name) -> Option<Module> {
158 let def_map = db.crate_def_map(self.krate()); 159 let def_map = db.crate_def_map(self.id.krate);
159 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)?;
160 Some(self.with_module_id(*child_id)) 161 Some(self.with_module_id(*child_id))
161 } 162 }
162 163
163 /// Iterates over all child modules. 164 /// Iterates over all child modules.
164 pub fn children(self, db: &impl DefDatabase) -> impl Iterator<Item = Module> { 165 pub fn children(self, db: &impl DefDatabase) -> impl Iterator<Item = Module> {
165 let def_map = db.crate_def_map(self.krate()); 166 let def_map = db.crate_def_map(self.id.krate);
166 let children = def_map[self.id.module_id] 167 let children = def_map[self.id.module_id]
167 .children 168 .children
168 .iter() 169 .iter()
@@ -173,7 +174,7 @@ impl Module {
173 174
174 /// Finds a parent module. 175 /// Finds a parent module.
175 pub fn parent(self, db: &impl DefDatabase) -> Option<Module> { 176 pub fn parent(self, db: &impl DefDatabase) -> Option<Module> {
176 let def_map = db.crate_def_map(self.krate()); 177 let def_map = db.crate_def_map(self.id.krate);
177 let parent_id = def_map[self.id.module_id].parent?; 178 let parent_id = def_map[self.id.module_id].parent?;
178 Some(self.with_module_id(parent_id)) 179 Some(self.with_module_id(parent_id))
179 } 180 }
@@ -189,12 +190,16 @@ impl Module {
189 } 190 }
190 191
191 /// Returns a `ModuleScope`: a set of items, visible in this module. 192 /// Returns a `ModuleScope`: a set of items, visible in this module.
192 pub fn scope(self, db: &impl HirDatabase) -> ModuleScope { 193 pub fn scope(self, db: &impl HirDatabase) -> Vec<(Name, ScopeDef, Option<ImportId>)> {
193 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()
194 } 199 }
195 200
196 pub fn diagnostics(self, db: &impl HirDatabase, sink: &mut DiagnosticSink) { 201 pub fn diagnostics(self, db: &impl HirDatabase, sink: &mut DiagnosticSink) {
197 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);
198 for decl in self.declarations(db) { 203 for decl in self.declarations(db) {
199 match decl { 204 match decl {
200 crate::ModuleDef::Function(f) => f.diagnostics(db, sink), 205 crate::ModuleDef::Function(f) => f.diagnostics(db, sink),
@@ -218,12 +223,12 @@ impl Module {
218 } 223 }
219 224
220 pub(crate) fn resolver(self, db: &impl DefDatabase) -> Resolver { 225 pub(crate) fn resolver(self, db: &impl DefDatabase) -> Resolver {
221 let def_map = db.crate_def_map(self.krate()); 226 let def_map = db.crate_def_map(self.id.krate);
222 Resolver::default().push_module_scope(def_map, self.id.module_id) 227 Resolver::default().push_module_scope(def_map, self.id.module_id)
223 } 228 }
224 229
225 pub fn declarations(self, db: &impl DefDatabase) -> Vec<ModuleDef> { 230 pub fn declarations(self, db: &impl DefDatabase) -> Vec<ModuleDef> {
226 let def_map = db.crate_def_map(self.krate()); 231 let def_map = db.crate_def_map(self.id.krate);
227 def_map[self.id.module_id] 232 def_map[self.id.module_id]
228 .scope 233 .scope
229 .entries() 234 .entries()
@@ -231,6 +236,7 @@ impl Module {
231 .flat_map(|per_ns| { 236 .flat_map(|per_ns| {
232 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())
233 }) 238 })
239 .map(ModuleDef::from)
234 .collect() 240 .collect()
235 } 241 }
236 242
@@ -334,12 +340,12 @@ impl Struct {
334 340
335#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] 341#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
336pub struct Union { 342pub struct Union {
337 pub(crate) id: StructId, 343 pub(crate) id: UnionId,
338} 344}
339 345
340impl Union { 346impl Union {
341 pub fn name(self, db: &impl DefDatabase) -> Option<Name> { 347 pub fn name(self, db: &impl DefDatabase) -> Option<Name> {
342 db.struct_data(self.id).name.clone() 348 db.union_data(self.id).name.clone()
343 } 349 }
344 350
345 pub fn module(self, db: &impl HirDatabase) -> Module { 351 pub fn module(self, db: &impl HirDatabase) -> Module {