diff options
Diffstat (limited to 'crates')
-rw-r--r-- | crates/ra_hir/src/code_model.rs (renamed from crates/ra_hir/src/code_model_api.rs) | 361 | ||||
-rw-r--r-- | crates/ra_hir/src/code_model_impl.rs | 4 | ||||
-rw-r--r-- | crates/ra_hir/src/code_model_impl/function.rs | 50 | ||||
-rw-r--r-- | crates/ra_hir/src/code_model_impl/konst.rs | 34 | ||||
-rw-r--r-- | crates/ra_hir/src/code_model_impl/krate.rs | 22 | ||||
-rw-r--r-- | crates/ra_hir/src/code_model_impl/module.rs | 99 | ||||
-rw-r--r-- | crates/ra_hir/src/impl_block.rs | 2 | ||||
-rw-r--r-- | crates/ra_hir/src/lib.rs | 5 | ||||
-rw-r--r-- | crates/ra_hir/src/resolve.rs | 2 |
9 files changed, 244 insertions, 335 deletions
diff --git a/crates/ra_hir/src/code_model_api.rs b/crates/ra_hir/src/code_model.rs index 970b78412..49030ce67 100644 --- a/crates/ra_hir/src/code_model_api.rs +++ b/crates/ra_hir/src/code_model.rs | |||
@@ -1,15 +1,15 @@ | |||
1 | use std::sync::Arc; | 1 | use std::sync::Arc; |
2 | 2 | ||
3 | use ra_db::{CrateId, SourceRootId, Edition}; | 3 | use ra_db::{CrateId, SourceRootId, Edition, FileId}; |
4 | use ra_syntax::{ast::self, TreeArc}; | 4 | use ra_syntax::{ast::{self, NameOwner, TypeAscriptionOwner}, TreeArc}; |
5 | 5 | ||
6 | use crate::{ | 6 | use crate::{ |
7 | Name, Ty, HirFileId, Either, | 7 | Name, AsName, AstId, Ty, HirFileId, Either, |
8 | HirDatabase, DefDatabase, | 8 | HirDatabase, DefDatabase, |
9 | type_ref::TypeRef, | 9 | type_ref::TypeRef, |
10 | nameres::{ModuleScope, Namespace, ImportId, CrateModuleId}, | 10 | nameres::{ModuleScope, Namespace, ImportId, CrateModuleId}, |
11 | expr::{Body, BodySourceMap, validation::ExprValidator}, | 11 | expr::{Body, BodySourceMap, validation::ExprValidator}, |
12 | ty::{ TraitRef, InferenceResult}, | 12 | ty::{TraitRef, InferenceResult}, |
13 | adt::{EnumVariantId, StructFieldId, VariantDef}, | 13 | adt::{EnumVariantId, StructFieldId, VariantDef}, |
14 | generics::HasGenericParams, | 14 | generics::HasGenericParams, |
15 | docs::{Documentation, Docs, docs_from_ast}, | 15 | docs::{Documentation, Docs, docs_from_ast}, |
@@ -18,6 +18,7 @@ use crate::{ | |||
18 | resolve::Resolver, | 18 | resolve::Resolver, |
19 | diagnostics::{DiagnosticSink}, | 19 | diagnostics::{DiagnosticSink}, |
20 | traits::{TraitItem, TraitData}, | 20 | traits::{TraitItem, TraitData}, |
21 | type_ref::Mutability, | ||
21 | }; | 22 | }; |
22 | 23 | ||
23 | /// hir::Crate describes a single crate. It's the main interface with which | 24 | /// hir::Crate describes a single crate. It's the main interface with which |
@@ -35,19 +36,28 @@ pub struct CrateDependency { | |||
35 | } | 36 | } |
36 | 37 | ||
37 | impl Crate { | 38 | impl Crate { |
38 | pub fn crate_id(&self) -> CrateId { | 39 | pub fn crate_id(self) -> CrateId { |
39 | self.crate_id | 40 | self.crate_id |
40 | } | 41 | } |
41 | 42 | ||
42 | pub fn dependencies(&self, db: &impl DefDatabase) -> Vec<CrateDependency> { | 43 | pub fn dependencies(self, db: &impl DefDatabase) -> Vec<CrateDependency> { |
43 | self.dependencies_impl(db) | 44 | db.crate_graph() |
45 | .dependencies(self.crate_id) | ||
46 | .map(|dep| { | ||
47 | let krate = Crate { crate_id: dep.crate_id() }; | ||
48 | let name = dep.as_name(); | ||
49 | CrateDependency { krate, name } | ||
50 | }) | ||
51 | .collect() | ||
44 | } | 52 | } |
45 | 53 | ||
46 | pub fn root_module(&self, db: &impl DefDatabase) -> Option<Module> { | 54 | pub fn root_module(self, db: &impl DefDatabase) -> Option<Module> { |
47 | self.root_module_impl(db) | 55 | let module_id = db.crate_def_map(self).root(); |
56 | let module = Module { krate: self, module_id }; | ||
57 | Some(module) | ||
48 | } | 58 | } |
49 | 59 | ||
50 | pub fn edition(&self, db: &impl DefDatabase) -> Edition { | 60 | pub fn edition(self, db: &impl DefDatabase) -> Edition { |
51 | let crate_graph = db.crate_graph(); | 61 | let crate_graph = db.crate_graph(); |
52 | crate_graph.edition(self.crate_id) | 62 | crate_graph.edition(self.crate_id) |
53 | } | 63 | } |
@@ -98,29 +108,66 @@ pub enum ModuleSource { | |||
98 | Module(TreeArc<ast::Module>), | 108 | Module(TreeArc<ast::Module>), |
99 | } | 109 | } |
100 | 110 | ||
111 | impl ModuleSource { | ||
112 | pub(crate) fn new( | ||
113 | db: &impl DefDatabase, | ||
114 | file_id: Option<FileId>, | ||
115 | decl_id: Option<AstId<ast::Module>>, | ||
116 | ) -> ModuleSource { | ||
117 | match (file_id, decl_id) { | ||
118 | (Some(file_id), _) => { | ||
119 | let source_file = db.parse(file_id); | ||
120 | ModuleSource::SourceFile(source_file) | ||
121 | } | ||
122 | (None, Some(item_id)) => { | ||
123 | let module = item_id.to_node(db); | ||
124 | assert!(module.item_list().is_some(), "expected inline module"); | ||
125 | ModuleSource::Module(module.to_owned()) | ||
126 | } | ||
127 | (None, None) => panic!(), | ||
128 | } | ||
129 | } | ||
130 | } | ||
131 | |||
101 | impl Module { | 132 | impl Module { |
102 | /// Name of this module. | 133 | /// Name of this module. |
103 | pub fn name(&self, db: &impl HirDatabase) -> Option<Name> { | 134 | pub fn name(self, db: &impl HirDatabase) -> Option<Name> { |
104 | self.name_impl(db) | 135 | let def_map = db.crate_def_map(self.krate); |
136 | let parent = def_map[self.module_id].parent?; | ||
137 | def_map[parent].children.iter().find_map(|(name, module_id)| { | ||
138 | if *module_id == self.module_id { | ||
139 | Some(name.clone()) | ||
140 | } else { | ||
141 | None | ||
142 | } | ||
143 | }) | ||
105 | } | 144 | } |
106 | 145 | ||
107 | /// Returns a node which defines this module. That is, a file or a `mod foo {}` with items. | 146 | /// Returns a node which defines this module. That is, a file or a `mod foo {}` with items. |
108 | pub fn definition_source(&self, db: &impl DefDatabase) -> (HirFileId, ModuleSource) { | 147 | pub fn definition_source(self, db: &impl DefDatabase) -> (HirFileId, ModuleSource) { |
109 | self.definition_source_impl(db) | 148 | let def_map = db.crate_def_map(self.krate); |
149 | let decl_id = def_map[self.module_id].declaration; | ||
150 | let file_id = def_map[self.module_id].definition; | ||
151 | let module_source = ModuleSource::new(db, file_id, decl_id); | ||
152 | let file_id = file_id.map(HirFileId::from).unwrap_or_else(|| decl_id.unwrap().file_id()); | ||
153 | (file_id, module_source) | ||
110 | } | 154 | } |
111 | 155 | ||
112 | /// Returns a node which declares this module, either a `mod foo;` or a `mod foo {}`. | 156 | /// Returns a node which declares this module, either a `mod foo;` or a `mod foo {}`. |
113 | /// `None` for the crate root. | 157 | /// `None` for the crate root. |
114 | pub fn declaration_source( | 158 | pub fn declaration_source( |
115 | &self, | 159 | self, |
116 | db: &impl HirDatabase, | 160 | db: &impl HirDatabase, |
117 | ) -> Option<(HirFileId, TreeArc<ast::Module>)> { | 161 | ) -> Option<(HirFileId, TreeArc<ast::Module>)> { |
118 | self.declaration_source_impl(db) | 162 | let def_map = db.crate_def_map(self.krate); |
163 | let decl = def_map[self.module_id].declaration?; | ||
164 | let ast = decl.to_node(db); | ||
165 | Some((decl.file_id(), ast)) | ||
119 | } | 166 | } |
120 | 167 | ||
121 | /// Returns the syntax of the last path segment corresponding to this import | 168 | /// Returns the syntax of the last path segment corresponding to this import |
122 | pub fn import_source( | 169 | pub fn import_source( |
123 | &self, | 170 | self, |
124 | db: &impl HirDatabase, | 171 | db: &impl HirDatabase, |
125 | import: ImportId, | 172 | import: ImportId, |
126 | ) -> Either<TreeArc<ast::UseTree>, TreeArc<ast::ExternCrateItem>> { | 173 | ) -> Either<TreeArc<ast::UseTree>, TreeArc<ast::ExternCrateItem>> { |
@@ -130,33 +177,44 @@ impl Module { | |||
130 | } | 177 | } |
131 | 178 | ||
132 | /// Returns the crate this module is part of. | 179 | /// Returns the crate this module is part of. |
133 | pub fn krate(&self, _db: &impl DefDatabase) -> Option<Crate> { | 180 | pub fn krate(self, _db: &impl DefDatabase) -> Option<Crate> { |
134 | Some(self.krate) | 181 | Some(self.krate) |
135 | } | 182 | } |
136 | 183 | ||
137 | /// Topmost parent of this module. Every module has a `crate_root`, but some | 184 | /// Topmost parent of this module. Every module has a `crate_root`, but some |
138 | /// might be missing `krate`. This can happen if a module's file is not included | 185 | /// might be missing `krate`. This can happen if a module's file is not included |
139 | /// in the module tree of any target in `Cargo.toml`. | 186 | /// in the module tree of any target in `Cargo.toml`. |
140 | pub fn crate_root(&self, db: &impl DefDatabase) -> Module { | 187 | pub fn crate_root(self, db: &impl DefDatabase) -> Module { |
141 | self.crate_root_impl(db) | 188 | let def_map = db.crate_def_map(self.krate); |
189 | self.with_module_id(def_map.root()) | ||
142 | } | 190 | } |
143 | 191 | ||
144 | /// Finds a child module with the specified name. | 192 | /// Finds a child module with the specified name. |
145 | pub fn child(&self, db: &impl HirDatabase, name: &Name) -> Option<Module> { | 193 | pub fn child(self, db: &impl HirDatabase, name: &Name) -> Option<Module> { |
146 | self.child_impl(db, name) | 194 | let def_map = db.crate_def_map(self.krate); |
195 | let child_id = def_map[self.module_id].children.get(name)?; | ||
196 | Some(self.with_module_id(*child_id)) | ||
147 | } | 197 | } |
148 | 198 | ||
149 | /// Iterates over all child modules. | 199 | /// Iterates over all child modules. |
150 | pub fn children(&self, db: &impl DefDatabase) -> impl Iterator<Item = Module> { | 200 | pub fn children(self, db: &impl DefDatabase) -> impl Iterator<Item = Module> { |
151 | self.children_impl(db) | 201 | let def_map = db.crate_def_map(self.krate); |
202 | let children = def_map[self.module_id] | ||
203 | .children | ||
204 | .iter() | ||
205 | .map(|(_, module_id)| self.with_module_id(*module_id)) | ||
206 | .collect::<Vec<_>>(); | ||
207 | children.into_iter() | ||
152 | } | 208 | } |
153 | 209 | ||
154 | /// Finds a parent module. | 210 | /// Finds a parent module. |
155 | pub fn parent(&self, db: &impl DefDatabase) -> Option<Module> { | 211 | pub fn parent(self, db: &impl DefDatabase) -> Option<Module> { |
156 | self.parent_impl(db) | 212 | let def_map = db.crate_def_map(self.krate); |
213 | let parent_id = def_map[self.module_id].parent?; | ||
214 | Some(self.with_module_id(parent_id)) | ||
157 | } | 215 | } |
158 | 216 | ||
159 | pub fn path_to_root(&self, db: &impl HirDatabase) -> Vec<Module> { | 217 | pub fn path_to_root(self, db: &impl HirDatabase) -> Vec<Module> { |
160 | let mut res = vec![self.clone()]; | 218 | let mut res = vec![self.clone()]; |
161 | let mut curr = self.clone(); | 219 | let mut curr = self.clone(); |
162 | while let Some(next) = curr.parent(db) { | 220 | while let Some(next) = curr.parent(db) { |
@@ -167,11 +225,11 @@ impl Module { | |||
167 | } | 225 | } |
168 | 226 | ||
169 | /// Returns a `ModuleScope`: a set of items, visible in this module. | 227 | /// Returns a `ModuleScope`: a set of items, visible in this module. |
170 | pub fn scope(&self, db: &impl HirDatabase) -> ModuleScope { | 228 | pub fn scope(self, db: &impl HirDatabase) -> ModuleScope { |
171 | db.crate_def_map(self.krate)[self.module_id].scope.clone() | 229 | db.crate_def_map(self.krate)[self.module_id].scope.clone() |
172 | } | 230 | } |
173 | 231 | ||
174 | pub fn diagnostics(&self, db: &impl HirDatabase, sink: &mut DiagnosticSink) { | 232 | pub fn diagnostics(self, db: &impl HirDatabase, sink: &mut DiagnosticSink) { |
175 | db.crate_def_map(self.krate).add_diagnostics(db, self.module_id, sink); | 233 | db.crate_def_map(self.krate).add_diagnostics(db, self.module_id, sink); |
176 | for decl in self.declarations(db) { | 234 | for decl in self.declarations(db) { |
177 | match decl { | 235 | match decl { |
@@ -191,7 +249,7 @@ impl Module { | |||
191 | } | 249 | } |
192 | } | 250 | } |
193 | 251 | ||
194 | pub(crate) fn resolver(&self, db: &impl DefDatabase) -> Resolver { | 252 | pub(crate) fn resolver(self, db: &impl DefDatabase) -> Resolver { |
195 | let def_map = db.crate_def_map(self.krate); | 253 | let def_map = db.crate_def_map(self.krate); |
196 | Resolver::default().push_module_scope(def_map, self.module_id) | 254 | Resolver::default().push_module_scope(def_map, self.module_id) |
197 | } | 255 | } |
@@ -216,6 +274,10 @@ impl Module { | |||
216 | .map(|(impl_id, _)| ImplBlock::from_id(self, impl_id)) | 274 | .map(|(impl_id, _)| ImplBlock::from_id(self, impl_id)) |
217 | .collect() | 275 | .collect() |
218 | } | 276 | } |
277 | |||
278 | fn with_module_id(&self, module_id: CrateModuleId) -> Module { | ||
279 | Module { module_id, krate: self.krate } | ||
280 | } | ||
219 | } | 281 | } |
220 | 282 | ||
221 | impl Docs for Module { | 283 | impl Docs for Module { |
@@ -269,49 +331,49 @@ pub struct Struct { | |||
269 | } | 331 | } |
270 | 332 | ||
271 | impl Struct { | 333 | impl Struct { |
272 | pub fn source(&self, db: &impl DefDatabase) -> (HirFileId, TreeArc<ast::StructDef>) { | 334 | pub fn source(self, db: &impl DefDatabase) -> (HirFileId, TreeArc<ast::StructDef>) { |
273 | self.id.source(db) | 335 | self.id.source(db) |
274 | } | 336 | } |
275 | 337 | ||
276 | pub fn module(&self, db: &impl HirDatabase) -> Module { | 338 | pub fn module(self, db: &impl HirDatabase) -> Module { |
277 | self.id.module(db) | 339 | self.id.module(db) |
278 | } | 340 | } |
279 | 341 | ||
280 | pub fn name(&self, db: &impl HirDatabase) -> Option<Name> { | 342 | pub fn name(self, db: &impl HirDatabase) -> Option<Name> { |
281 | db.struct_data(*self).name.clone() | 343 | db.struct_data(self).name.clone() |
282 | } | 344 | } |
283 | 345 | ||
284 | pub fn fields(&self, db: &impl HirDatabase) -> Vec<StructField> { | 346 | pub fn fields(self, db: &impl HirDatabase) -> Vec<StructField> { |
285 | db.struct_data(*self) | 347 | db.struct_data(self) |
286 | .variant_data | 348 | .variant_data |
287 | .fields() | 349 | .fields() |
288 | .into_iter() | 350 | .into_iter() |
289 | .flat_map(|it| it.iter()) | 351 | .flat_map(|it| it.iter()) |
290 | .map(|(id, _)| StructField { parent: (*self).into(), id }) | 352 | .map(|(id, _)| StructField { parent: self.into(), id }) |
291 | .collect() | 353 | .collect() |
292 | } | 354 | } |
293 | 355 | ||
294 | pub fn field(&self, db: &impl HirDatabase, name: &Name) -> Option<StructField> { | 356 | pub fn field(self, db: &impl HirDatabase, name: &Name) -> Option<StructField> { |
295 | db.struct_data(*self) | 357 | db.struct_data(self) |
296 | .variant_data | 358 | .variant_data |
297 | .fields() | 359 | .fields() |
298 | .into_iter() | 360 | .into_iter() |
299 | .flat_map(|it| it.iter()) | 361 | .flat_map(|it| it.iter()) |
300 | .find(|(_id, data)| data.name == *name) | 362 | .find(|(_id, data)| data.name == *name) |
301 | .map(|(id, _)| StructField { parent: (*self).into(), id }) | 363 | .map(|(id, _)| StructField { parent: self.into(), id }) |
302 | } | 364 | } |
303 | 365 | ||
304 | pub fn ty(&self, db: &impl HirDatabase) -> Ty { | 366 | pub fn ty(self, db: &impl HirDatabase) -> Ty { |
305 | db.type_for_def((*self).into(), Namespace::Types) | 367 | db.type_for_def(self.into(), Namespace::Types) |
306 | } | 368 | } |
307 | 369 | ||
308 | pub fn constructor_ty(&self, db: &impl HirDatabase) -> Ty { | 370 | pub fn constructor_ty(self, db: &impl HirDatabase) -> Ty { |
309 | db.type_for_def((*self).into(), Namespace::Values) | 371 | db.type_for_def(self.into(), Namespace::Values) |
310 | } | 372 | } |
311 | 373 | ||
312 | // FIXME move to a more general type | 374 | // FIXME move to a more general type |
313 | /// Builds a resolver for type references inside this struct. | 375 | /// Builds a resolver for type references inside this struct. |
314 | pub(crate) fn resolver(&self, db: &impl HirDatabase) -> Resolver { | 376 | pub(crate) fn resolver(self, db: &impl HirDatabase) -> Resolver { |
315 | // take the outer scope... | 377 | // take the outer scope... |
316 | let r = self.module(db).resolver(db); | 378 | let r = self.module(db).resolver(db); |
317 | // ...and add generic params, if present | 379 | // ...and add generic params, if present |
@@ -333,21 +395,21 @@ pub struct Union { | |||
333 | } | 395 | } |
334 | 396 | ||
335 | impl Union { | 397 | impl Union { |
336 | pub fn source(&self, db: &impl DefDatabase) -> (HirFileId, TreeArc<ast::StructDef>) { | 398 | pub fn source(self, db: &impl DefDatabase) -> (HirFileId, TreeArc<ast::StructDef>) { |
337 | self.id.source(db) | 399 | self.id.source(db) |
338 | } | 400 | } |
339 | 401 | ||
340 | pub fn name(&self, db: &impl HirDatabase) -> Option<Name> { | 402 | pub fn name(self, db: &impl HirDatabase) -> Option<Name> { |
341 | db.struct_data(Struct { id: self.id }).name.clone() | 403 | db.struct_data(Struct { id: self.id }).name.clone() |
342 | } | 404 | } |
343 | 405 | ||
344 | pub fn module(&self, db: &impl HirDatabase) -> Module { | 406 | pub fn module(self, db: &impl HirDatabase) -> Module { |
345 | self.id.module(db) | 407 | self.id.module(db) |
346 | } | 408 | } |
347 | 409 | ||
348 | // FIXME move to a more general type | 410 | // FIXME move to a more general type |
349 | /// Builds a resolver for type references inside this union. | 411 | /// Builds a resolver for type references inside this union. |
350 | pub(crate) fn resolver(&self, db: &impl HirDatabase) -> Resolver { | 412 | pub(crate) fn resolver(self, db: &impl HirDatabase) -> Resolver { |
351 | // take the outer scope... | 413 | // take the outer scope... |
352 | let r = self.module(db).resolver(db); | 414 | let r = self.module(db).resolver(db); |
353 | // ...and add generic params, if present | 415 | // ...and add generic params, if present |
@@ -369,41 +431,37 @@ pub struct Enum { | |||
369 | } | 431 | } |
370 | 432 | ||
371 | impl Enum { | 433 | impl Enum { |
372 | pub fn source(&self, db: &impl DefDatabase) -> (HirFileId, TreeArc<ast::EnumDef>) { | 434 | pub fn source(self, db: &impl DefDatabase) -> (HirFileId, TreeArc<ast::EnumDef>) { |
373 | self.id.source(db) | 435 | self.id.source(db) |
374 | } | 436 | } |
375 | 437 | ||
376 | pub fn module(&self, db: &impl HirDatabase) -> Module { | 438 | pub fn module(self, db: &impl HirDatabase) -> Module { |
377 | self.id.module(db) | 439 | self.id.module(db) |
378 | } | 440 | } |
379 | 441 | ||
380 | pub fn name(&self, db: &impl HirDatabase) -> Option<Name> { | 442 | pub fn name(self, db: &impl HirDatabase) -> Option<Name> { |
381 | db.enum_data(*self).name.clone() | 443 | db.enum_data(self).name.clone() |
382 | } | 444 | } |
383 | 445 | ||
384 | pub fn variants(&self, db: &impl DefDatabase) -> Vec<EnumVariant> { | 446 | pub fn variants(self, db: &impl DefDatabase) -> Vec<EnumVariant> { |
385 | db.enum_data(*self) | 447 | db.enum_data(self).variants.iter().map(|(id, _)| EnumVariant { parent: self, id }).collect() |
386 | .variants | ||
387 | .iter() | ||
388 | .map(|(id, _)| EnumVariant { parent: *self, id }) | ||
389 | .collect() | ||
390 | } | 448 | } |
391 | 449 | ||
392 | pub fn variant(&self, db: &impl DefDatabase, name: &Name) -> Option<EnumVariant> { | 450 | pub fn variant(self, db: &impl DefDatabase, name: &Name) -> Option<EnumVariant> { |
393 | db.enum_data(*self) | 451 | db.enum_data(self) |
394 | .variants | 452 | .variants |
395 | .iter() | 453 | .iter() |
396 | .find(|(_id, data)| data.name.as_ref() == Some(name)) | 454 | .find(|(_id, data)| data.name.as_ref() == Some(name)) |
397 | .map(|(id, _)| EnumVariant { parent: *self, id }) | 455 | .map(|(id, _)| EnumVariant { parent: self, id }) |
398 | } | 456 | } |
399 | 457 | ||
400 | pub fn ty(&self, db: &impl HirDatabase) -> Ty { | 458 | pub fn ty(self, db: &impl HirDatabase) -> Ty { |
401 | db.type_for_def((*self).into(), Namespace::Types) | 459 | db.type_for_def(self.into(), Namespace::Types) |
402 | } | 460 | } |
403 | 461 | ||
404 | // FIXME: move to a more general type | 462 | // FIXME: move to a more general type |
405 | /// Builds a resolver for type references inside this struct. | 463 | /// Builds a resolver for type references inside this struct. |
406 | pub(crate) fn resolver(&self, db: &impl HirDatabase) -> Resolver { | 464 | pub(crate) fn resolver(self, db: &impl HirDatabase) -> Resolver { |
407 | // take the outer scope... | 465 | // take the outer scope... |
408 | let r = self.module(db).resolver(db); | 466 | let r = self.module(db).resolver(db); |
409 | // ...and add generic params, if present | 467 | // ...and add generic params, if present |
@@ -476,16 +534,16 @@ pub enum DefWithBody { | |||
476 | impl_froms!(DefWithBody: Function, Const, Static); | 534 | impl_froms!(DefWithBody: Function, Const, Static); |
477 | 535 | ||
478 | impl DefWithBody { | 536 | impl DefWithBody { |
479 | pub fn infer(&self, db: &impl HirDatabase) -> Arc<InferenceResult> { | 537 | pub fn infer(self, db: &impl HirDatabase) -> Arc<InferenceResult> { |
480 | db.infer(*self) | 538 | db.infer(self) |
481 | } | 539 | } |
482 | 540 | ||
483 | pub fn body(&self, db: &impl HirDatabase) -> Arc<Body> { | 541 | pub fn body(self, db: &impl HirDatabase) -> Arc<Body> { |
484 | db.body_hir(*self) | 542 | db.body_hir(self) |
485 | } | 543 | } |
486 | 544 | ||
487 | pub fn body_source_map(&self, db: &impl HirDatabase) -> Arc<BodySourceMap> { | 545 | pub fn body_source_map(self, db: &impl HirDatabase) -> Arc<BodySourceMap> { |
488 | db.body_with_source_map(*self).1 | 546 | db.body_with_source_map(self).1 |
489 | } | 547 | } |
490 | 548 | ||
491 | /// Builds a resolver for code inside this item. | 549 | /// Builds a resolver for code inside this item. |
@@ -515,6 +573,44 @@ pub struct FnSignature { | |||
515 | } | 573 | } |
516 | 574 | ||
517 | impl FnSignature { | 575 | impl FnSignature { |
576 | pub(crate) fn fn_signature_query(db: &impl DefDatabase, func: Function) -> Arc<FnSignature> { | ||
577 | let (_, node) = func.source(db); | ||
578 | let name = node.name().map(|n| n.as_name()).unwrap_or_else(Name::missing); | ||
579 | let mut params = Vec::new(); | ||
580 | let mut has_self_param = false; | ||
581 | if let Some(param_list) = node.param_list() { | ||
582 | if let Some(self_param) = param_list.self_param() { | ||
583 | let self_type = if let Some(type_ref) = self_param.ascribed_type() { | ||
584 | TypeRef::from_ast(type_ref) | ||
585 | } else { | ||
586 | let self_type = TypeRef::Path(Name::self_type().into()); | ||
587 | match self_param.kind() { | ||
588 | ast::SelfParamKind::Owned => self_type, | ||
589 | ast::SelfParamKind::Ref => { | ||
590 | TypeRef::Reference(Box::new(self_type), Mutability::Shared) | ||
591 | } | ||
592 | ast::SelfParamKind::MutRef => { | ||
593 | TypeRef::Reference(Box::new(self_type), Mutability::Mut) | ||
594 | } | ||
595 | } | ||
596 | }; | ||
597 | params.push(self_type); | ||
598 | has_self_param = true; | ||
599 | } | ||
600 | for param in param_list.params() { | ||
601 | let type_ref = TypeRef::from_ast_opt(param.ascribed_type()); | ||
602 | params.push(type_ref); | ||
603 | } | ||
604 | } | ||
605 | let ret_type = if let Some(type_ref) = node.ret_type().and_then(|rt| rt.type_ref()) { | ||
606 | TypeRef::from_ast(type_ref) | ||
607 | } else { | ||
608 | TypeRef::unit() | ||
609 | }; | ||
610 | |||
611 | let sig = FnSignature { name, params, ret_type, has_self_param }; | ||
612 | Arc::new(sig) | ||
613 | } | ||
518 | pub fn name(&self) -> &Name { | 614 | pub fn name(&self) -> &Name { |
519 | &self.name | 615 | &self.name |
520 | } | 616 | } |
@@ -535,50 +631,50 @@ impl FnSignature { | |||
535 | } | 631 | } |
536 | 632 | ||
537 | impl Function { | 633 | impl Function { |
538 | pub fn source(&self, db: &impl DefDatabase) -> (HirFileId, TreeArc<ast::FnDef>) { | 634 | pub fn source(self, db: &impl DefDatabase) -> (HirFileId, TreeArc<ast::FnDef>) { |
539 | self.id.source(db) | 635 | self.id.source(db) |
540 | } | 636 | } |
541 | 637 | ||
542 | pub fn module(&self, db: &impl DefDatabase) -> Module { | 638 | pub fn module(self, db: &impl DefDatabase) -> Module { |
543 | self.id.module(db) | 639 | self.id.module(db) |
544 | } | 640 | } |
545 | 641 | ||
546 | pub fn name(&self, db: &impl HirDatabase) -> Name { | 642 | pub fn name(self, db: &impl HirDatabase) -> Name { |
547 | self.signature(db).name.clone() | 643 | self.signature(db).name.clone() |
548 | } | 644 | } |
549 | 645 | ||
550 | pub(crate) fn body_source_map(&self, db: &impl HirDatabase) -> Arc<BodySourceMap> { | 646 | pub(crate) fn body_source_map(self, db: &impl HirDatabase) -> Arc<BodySourceMap> { |
551 | db.body_with_source_map((*self).into()).1 | 647 | db.body_with_source_map(self.into()).1 |
552 | } | 648 | } |
553 | 649 | ||
554 | pub fn body(&self, db: &impl HirDatabase) -> Arc<Body> { | 650 | pub fn body(self, db: &impl HirDatabase) -> Arc<Body> { |
555 | db.body_hir((*self).into()) | 651 | db.body_hir(self.into()) |
556 | } | 652 | } |
557 | 653 | ||
558 | pub fn ty(&self, db: &impl HirDatabase) -> Ty { | 654 | pub fn ty(self, db: &impl HirDatabase) -> Ty { |
559 | db.type_for_def((*self).into(), Namespace::Values) | 655 | db.type_for_def(self.into(), Namespace::Values) |
560 | } | 656 | } |
561 | 657 | ||
562 | pub fn signature(&self, db: &impl HirDatabase) -> Arc<FnSignature> { | 658 | pub fn signature(self, db: &impl HirDatabase) -> Arc<FnSignature> { |
563 | db.fn_signature(*self) | 659 | db.fn_signature(self) |
564 | } | 660 | } |
565 | 661 | ||
566 | pub fn infer(&self, db: &impl HirDatabase) -> Arc<InferenceResult> { | 662 | pub fn infer(self, db: &impl HirDatabase) -> Arc<InferenceResult> { |
567 | db.infer((*self).into()) | 663 | db.infer(self.into()) |
568 | } | 664 | } |
569 | 665 | ||
570 | /// The containing impl block, if this is a method. | 666 | /// The containing impl block, if this is a method. |
571 | pub fn impl_block(&self, db: &impl DefDatabase) -> Option<ImplBlock> { | 667 | pub fn impl_block(self, db: &impl DefDatabase) -> Option<ImplBlock> { |
572 | let module_impls = db.impls_in_module(self.module(db)); | 668 | let module_impls = db.impls_in_module(self.module(db)); |
573 | ImplBlock::containing(module_impls, (*self).into()) | 669 | ImplBlock::containing(module_impls, self.into()) |
574 | } | 670 | } |
575 | 671 | ||
576 | /// The containing trait, if this is a trait method definition. | 672 | /// The containing trait, if this is a trait method definition. |
577 | pub fn parent_trait(&self, db: &impl DefDatabase) -> Option<Trait> { | 673 | pub fn parent_trait(self, db: &impl DefDatabase) -> Option<Trait> { |
578 | db.trait_items_index(self.module(db)).get_parent_trait((*self).into()) | 674 | db.trait_items_index(self.module(db)).get_parent_trait(self.into()) |
579 | } | 675 | } |
580 | 676 | ||
581 | pub fn container(&self, db: &impl DefDatabase) -> Option<Container> { | 677 | pub fn container(self, db: &impl DefDatabase) -> Option<Container> { |
582 | if let Some(impl_block) = self.impl_block(db) { | 678 | if let Some(impl_block) = self.impl_block(db) { |
583 | Some(impl_block.into()) | 679 | Some(impl_block.into()) |
584 | } else if let Some(trait_) = self.parent_trait(db) { | 680 | } else if let Some(trait_) = self.parent_trait(db) { |
@@ -590,7 +686,7 @@ impl Function { | |||
590 | 686 | ||
591 | // FIXME: move to a more general type for 'body-having' items | 687 | // FIXME: move to a more general type for 'body-having' items |
592 | /// Builds a resolver for code inside this item. | 688 | /// Builds a resolver for code inside this item. |
593 | pub(crate) fn resolver(&self, db: &impl HirDatabase) -> Resolver { | 689 | pub(crate) fn resolver(self, db: &impl HirDatabase) -> Resolver { |
594 | // take the outer scope... | 690 | // take the outer scope... |
595 | let r = self.container(db).map_or_else(|| self.module(db).resolver(db), |c| c.resolver(db)); | 691 | let r = self.container(db).map_or_else(|| self.module(db).resolver(db), |c| c.resolver(db)); |
596 | // ...and add generic params, if present | 692 | // ...and add generic params, if present |
@@ -599,10 +695,10 @@ impl Function { | |||
599 | r | 695 | r |
600 | } | 696 | } |
601 | 697 | ||
602 | pub fn diagnostics(&self, db: &impl HirDatabase, sink: &mut DiagnosticSink) { | 698 | pub fn diagnostics(self, db: &impl HirDatabase, sink: &mut DiagnosticSink) { |
603 | let infer = self.infer(db); | 699 | let infer = self.infer(db); |
604 | infer.add_diagnostics(db, *self, sink); | 700 | infer.add_diagnostics(db, self, sink); |
605 | let mut validator = ExprValidator::new(*self, infer, sink); | 701 | let mut validator = ExprValidator::new(self, infer, sink); |
606 | validator.validate_body(db); | 702 | validator.validate_body(db); |
607 | } | 703 | } |
608 | } | 704 | } |
@@ -619,31 +715,31 @@ pub struct Const { | |||
619 | } | 715 | } |
620 | 716 | ||
621 | impl Const { | 717 | impl Const { |
622 | pub fn source(&self, db: &impl DefDatabase) -> (HirFileId, TreeArc<ast::ConstDef>) { | 718 | pub fn source(self, db: &impl DefDatabase) -> (HirFileId, TreeArc<ast::ConstDef>) { |
623 | self.id.source(db) | 719 | self.id.source(db) |
624 | } | 720 | } |
625 | 721 | ||
626 | pub fn module(&self, db: &impl DefDatabase) -> Module { | 722 | pub fn module(self, db: &impl DefDatabase) -> Module { |
627 | self.id.module(db) | 723 | self.id.module(db) |
628 | } | 724 | } |
629 | 725 | ||
630 | pub fn signature(&self, db: &impl HirDatabase) -> Arc<ConstSignature> { | 726 | pub fn signature(self, db: &impl HirDatabase) -> Arc<ConstSignature> { |
631 | db.const_signature(*self) | 727 | db.const_signature(self) |
632 | } | 728 | } |
633 | 729 | ||
634 | pub fn infer(&self, db: &impl HirDatabase) -> Arc<InferenceResult> { | 730 | pub fn infer(self, db: &impl HirDatabase) -> Arc<InferenceResult> { |
635 | db.infer((*self).into()) | 731 | db.infer(self.into()) |
636 | } | 732 | } |
637 | 733 | ||
638 | /// The containing impl block, if this is a method. | 734 | /// The containing impl block, if this is a method. |
639 | pub fn impl_block(&self, db: &impl DefDatabase) -> Option<ImplBlock> { | 735 | pub fn impl_block(self, db: &impl DefDatabase) -> Option<ImplBlock> { |
640 | let module_impls = db.impls_in_module(self.module(db)); | 736 | let module_impls = db.impls_in_module(self.module(db)); |
641 | ImplBlock::containing(module_impls, (*self).into()) | 737 | ImplBlock::containing(module_impls, self.into()) |
642 | } | 738 | } |
643 | 739 | ||
644 | // FIXME: move to a more general type for 'body-having' items | 740 | // FIXME: move to a more general type for 'body-having' items |
645 | /// Builds a resolver for code inside this item. | 741 | /// Builds a resolver for code inside this item. |
646 | pub(crate) fn resolver(&self, db: &impl HirDatabase) -> Resolver { | 742 | pub(crate) fn resolver(self, db: &impl HirDatabase) -> Resolver { |
647 | // take the outer scope... | 743 | // take the outer scope... |
648 | let r = self | 744 | let r = self |
649 | .impl_block(db) | 745 | .impl_block(db) |
@@ -674,6 +770,29 @@ impl ConstSignature { | |||
674 | pub fn type_ref(&self) -> &TypeRef { | 770 | pub fn type_ref(&self) -> &TypeRef { |
675 | &self.type_ref | 771 | &self.type_ref |
676 | } | 772 | } |
773 | |||
774 | pub(crate) fn const_signature_query( | ||
775 | db: &impl DefDatabase, | ||
776 | konst: Const, | ||
777 | ) -> Arc<ConstSignature> { | ||
778 | let (_, node) = konst.source(db); | ||
779 | const_signature_for(&*node) | ||
780 | } | ||
781 | |||
782 | pub(crate) fn static_signature_query( | ||
783 | db: &impl DefDatabase, | ||
784 | konst: Static, | ||
785 | ) -> Arc<ConstSignature> { | ||
786 | let (_, node) = konst.source(db); | ||
787 | const_signature_for(&*node) | ||
788 | } | ||
789 | } | ||
790 | |||
791 | fn const_signature_for<N: NameOwner + TypeAscriptionOwner>(node: &N) -> Arc<ConstSignature> { | ||
792 | let name = node.name().map(|n| n.as_name()).unwrap_or_else(Name::missing); | ||
793 | let type_ref = TypeRef::from_ast_opt(node.ascribed_type()); | ||
794 | let sig = ConstSignature { name, type_ref }; | ||
795 | Arc::new(sig) | ||
677 | } | 796 | } |
678 | 797 | ||
679 | #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] | 798 | #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] |
@@ -682,26 +801,26 @@ pub struct Static { | |||
682 | } | 801 | } |
683 | 802 | ||
684 | impl Static { | 803 | impl Static { |
685 | pub fn source(&self, db: &impl DefDatabase) -> (HirFileId, TreeArc<ast::StaticDef>) { | 804 | pub fn source(self, db: &impl DefDatabase) -> (HirFileId, TreeArc<ast::StaticDef>) { |
686 | self.id.source(db) | 805 | self.id.source(db) |
687 | } | 806 | } |
688 | 807 | ||
689 | pub fn module(&self, db: &impl DefDatabase) -> Module { | 808 | pub fn module(self, db: &impl DefDatabase) -> Module { |
690 | self.id.module(db) | 809 | self.id.module(db) |
691 | } | 810 | } |
692 | 811 | ||
693 | pub fn signature(&self, db: &impl HirDatabase) -> Arc<ConstSignature> { | 812 | pub fn signature(self, db: &impl HirDatabase) -> Arc<ConstSignature> { |
694 | db.static_signature(*self) | 813 | db.static_signature(self) |
695 | } | 814 | } |
696 | 815 | ||
697 | /// Builds a resolver for code inside this item. | 816 | /// Builds a resolver for code inside this item. |
698 | pub(crate) fn resolver(&self, db: &impl HirDatabase) -> Resolver { | 817 | pub(crate) fn resolver(self, db: &impl HirDatabase) -> Resolver { |
699 | // take the outer scope... | 818 | // take the outer scope... |
700 | self.module(db).resolver(db) | 819 | self.module(db).resolver(db) |
701 | } | 820 | } |
702 | 821 | ||
703 | pub fn infer(&self, db: &impl HirDatabase) -> Arc<InferenceResult> { | 822 | pub fn infer(self, db: &impl HirDatabase) -> Arc<InferenceResult> { |
704 | db.infer((*self).into()) | 823 | db.infer(self.into()) |
705 | } | 824 | } |
706 | } | 825 | } |
707 | 826 | ||
@@ -717,11 +836,11 @@ pub struct Trait { | |||
717 | } | 836 | } |
718 | 837 | ||
719 | impl Trait { | 838 | impl Trait { |
720 | pub fn source(&self, db: &impl DefDatabase) -> (HirFileId, TreeArc<ast::TraitDef>) { | 839 | pub fn source(self, db: &impl DefDatabase) -> (HirFileId, TreeArc<ast::TraitDef>) { |
721 | self.id.source(db) | 840 | self.id.source(db) |
722 | } | 841 | } |
723 | 842 | ||
724 | pub fn module(&self, db: &impl DefDatabase) -> Module { | 843 | pub fn module(self, db: &impl DefDatabase) -> Module { |
725 | self.id.module(db) | 844 | self.id.module(db) |
726 | } | 845 | } |
727 | 846 | ||
@@ -745,7 +864,7 @@ impl Trait { | |||
745 | self.trait_data(db).is_auto() | 864 | self.trait_data(db).is_auto() |
746 | } | 865 | } |
747 | 866 | ||
748 | pub(crate) fn resolver(&self, db: &impl DefDatabase) -> Resolver { | 867 | pub(crate) fn resolver(self, db: &impl DefDatabase) -> Resolver { |
749 | let r = self.module(db).resolver(db); | 868 | let r = self.module(db).resolver(db); |
750 | // add generic params, if present | 869 | // add generic params, if present |
751 | let p = self.generic_params(db); | 870 | let p = self.generic_params(db); |
@@ -766,26 +885,26 @@ pub struct TypeAlias { | |||
766 | } | 885 | } |
767 | 886 | ||
768 | impl TypeAlias { | 887 | impl TypeAlias { |
769 | pub fn source(&self, db: &impl DefDatabase) -> (HirFileId, TreeArc<ast::TypeAliasDef>) { | 888 | pub fn source(self, db: &impl DefDatabase) -> (HirFileId, TreeArc<ast::TypeAliasDef>) { |
770 | self.id.source(db) | 889 | self.id.source(db) |
771 | } | 890 | } |
772 | 891 | ||
773 | pub fn module(&self, db: &impl DefDatabase) -> Module { | 892 | pub fn module(self, db: &impl DefDatabase) -> Module { |
774 | self.id.module(db) | 893 | self.id.module(db) |
775 | } | 894 | } |
776 | 895 | ||
777 | /// The containing impl block, if this is a method. | 896 | /// The containing impl block, if this is a method. |
778 | pub fn impl_block(&self, db: &impl DefDatabase) -> Option<ImplBlock> { | 897 | pub fn impl_block(self, db: &impl DefDatabase) -> Option<ImplBlock> { |
779 | let module_impls = db.impls_in_module(self.module(db)); | 898 | let module_impls = db.impls_in_module(self.module(db)); |
780 | ImplBlock::containing(module_impls, (*self).into()) | 899 | ImplBlock::containing(module_impls, self.into()) |
781 | } | 900 | } |
782 | 901 | ||
783 | /// The containing trait, if this is a trait method definition. | 902 | /// The containing trait, if this is a trait method definition. |
784 | pub fn parent_trait(&self, db: &impl DefDatabase) -> Option<Trait> { | 903 | pub fn parent_trait(self, db: &impl DefDatabase) -> Option<Trait> { |
785 | db.trait_items_index(self.module(db)).get_parent_trait((*self).into()) | 904 | db.trait_items_index(self.module(db)).get_parent_trait(self.into()) |
786 | } | 905 | } |
787 | 906 | ||
788 | pub fn container(&self, db: &impl DefDatabase) -> Option<Container> { | 907 | pub fn container(self, db: &impl DefDatabase) -> Option<Container> { |
789 | if let Some(impl_block) = self.impl_block(db) { | 908 | if let Some(impl_block) = self.impl_block(db) { |
790 | Some(impl_block.into()) | 909 | Some(impl_block.into()) |
791 | } else if let Some(trait_) = self.parent_trait(db) { | 910 | } else if let Some(trait_) = self.parent_trait(db) { |
@@ -800,7 +919,7 @@ impl TypeAlias { | |||
800 | } | 919 | } |
801 | 920 | ||
802 | /// Builds a resolver for the type references in this type alias. | 921 | /// Builds a resolver for the type references in this type alias. |
803 | pub(crate) fn resolver(&self, db: &impl HirDatabase) -> Resolver { | 922 | pub(crate) fn resolver(self, db: &impl HirDatabase) -> Resolver { |
804 | // take the outer scope... | 923 | // take the outer scope... |
805 | let r = self | 924 | let r = self |
806 | .impl_block(db) | 925 | .impl_block(db) |
@@ -826,7 +945,7 @@ pub enum Container { | |||
826 | impl_froms!(Container: Trait, ImplBlock); | 945 | impl_froms!(Container: Trait, ImplBlock); |
827 | 946 | ||
828 | impl Container { | 947 | impl Container { |
829 | pub(crate) fn resolver(&self, db: &impl DefDatabase) -> Resolver { | 948 | pub(crate) fn resolver(self, db: &impl DefDatabase) -> Resolver { |
830 | match self { | 949 | match self { |
831 | Container::Trait(trait_) => trait_.resolver(db), | 950 | Container::Trait(trait_) => trait_.resolver(db), |
832 | Container::ImplBlock(impl_block) => impl_block.resolver(db), | 951 | Container::ImplBlock(impl_block) => impl_block.resolver(db), |
diff --git a/crates/ra_hir/src/code_model_impl.rs b/crates/ra_hir/src/code_model_impl.rs deleted file mode 100644 index 24df9a113..000000000 --- a/crates/ra_hir/src/code_model_impl.rs +++ /dev/null | |||
@@ -1,4 +0,0 @@ | |||
1 | mod krate; // `crate` is invalid ident :( | ||
2 | mod konst; // `const` is invalid ident :( | ||
3 | mod module; | ||
4 | pub(crate) mod function; | ||
diff --git a/crates/ra_hir/src/code_model_impl/function.rs b/crates/ra_hir/src/code_model_impl/function.rs deleted file mode 100644 index f8bd0f784..000000000 --- a/crates/ra_hir/src/code_model_impl/function.rs +++ /dev/null | |||
@@ -1,50 +0,0 @@ | |||
1 | use std::sync::Arc; | ||
2 | |||
3 | use ra_syntax::ast::{self, NameOwner, TypeAscriptionOwner}; | ||
4 | |||
5 | use crate::{ | ||
6 | Name, AsName, Function, FnSignature, | ||
7 | type_ref::{TypeRef, Mutability}, | ||
8 | DefDatabase, | ||
9 | }; | ||
10 | |||
11 | impl FnSignature { | ||
12 | pub(crate) fn fn_signature_query(db: &impl DefDatabase, func: Function) -> Arc<FnSignature> { | ||
13 | let (_, node) = func.source(db); | ||
14 | let name = node.name().map(|n| n.as_name()).unwrap_or_else(Name::missing); | ||
15 | let mut params = Vec::new(); | ||
16 | let mut has_self_param = false; | ||
17 | if let Some(param_list) = node.param_list() { | ||
18 | if let Some(self_param) = param_list.self_param() { | ||
19 | let self_type = if let Some(type_ref) = self_param.ascribed_type() { | ||
20 | TypeRef::from_ast(type_ref) | ||
21 | } else { | ||
22 | let self_type = TypeRef::Path(Name::self_type().into()); | ||
23 | match self_param.kind() { | ||
24 | ast::SelfParamKind::Owned => self_type, | ||
25 | ast::SelfParamKind::Ref => { | ||
26 | TypeRef::Reference(Box::new(self_type), Mutability::Shared) | ||
27 | } | ||
28 | ast::SelfParamKind::MutRef => { | ||
29 | TypeRef::Reference(Box::new(self_type), Mutability::Mut) | ||
30 | } | ||
31 | } | ||
32 | }; | ||
33 | params.push(self_type); | ||
34 | has_self_param = true; | ||
35 | } | ||
36 | for param in param_list.params() { | ||
37 | let type_ref = TypeRef::from_ast_opt(param.ascribed_type()); | ||
38 | params.push(type_ref); | ||
39 | } | ||
40 | } | ||
41 | let ret_type = if let Some(type_ref) = node.ret_type().and_then(|rt| rt.type_ref()) { | ||
42 | TypeRef::from_ast(type_ref) | ||
43 | } else { | ||
44 | TypeRef::unit() | ||
45 | }; | ||
46 | |||
47 | let sig = FnSignature { name, params, ret_type, has_self_param }; | ||
48 | Arc::new(sig) | ||
49 | } | ||
50 | } | ||
diff --git a/crates/ra_hir/src/code_model_impl/konst.rs b/crates/ra_hir/src/code_model_impl/konst.rs deleted file mode 100644 index db4e5ce5c..000000000 --- a/crates/ra_hir/src/code_model_impl/konst.rs +++ /dev/null | |||
@@ -1,34 +0,0 @@ | |||
1 | use std::sync::Arc; | ||
2 | |||
3 | use ra_syntax::ast::{NameOwner, TypeAscriptionOwner}; | ||
4 | |||
5 | use crate::{ | ||
6 | Name, AsName, Const, ConstSignature, Static, | ||
7 | type_ref::{TypeRef}, | ||
8 | DefDatabase, | ||
9 | }; | ||
10 | |||
11 | fn const_signature_for<N: NameOwner + TypeAscriptionOwner>(node: &N) -> Arc<ConstSignature> { | ||
12 | let name = node.name().map(|n| n.as_name()).unwrap_or_else(Name::missing); | ||
13 | let type_ref = TypeRef::from_ast_opt(node.ascribed_type()); | ||
14 | let sig = ConstSignature { name, type_ref }; | ||
15 | Arc::new(sig) | ||
16 | } | ||
17 | |||
18 | impl ConstSignature { | ||
19 | pub(crate) fn const_signature_query( | ||
20 | db: &impl DefDatabase, | ||
21 | konst: Const, | ||
22 | ) -> Arc<ConstSignature> { | ||
23 | let (_, node) = konst.source(db); | ||
24 | const_signature_for(&*node) | ||
25 | } | ||
26 | |||
27 | pub(crate) fn static_signature_query( | ||
28 | db: &impl DefDatabase, | ||
29 | konst: Static, | ||
30 | ) -> Arc<ConstSignature> { | ||
31 | let (_, node) = konst.source(db); | ||
32 | const_signature_for(&*node) | ||
33 | } | ||
34 | } | ||
diff --git a/crates/ra_hir/src/code_model_impl/krate.rs b/crates/ra_hir/src/code_model_impl/krate.rs deleted file mode 100644 index 914414fc3..000000000 --- a/crates/ra_hir/src/code_model_impl/krate.rs +++ /dev/null | |||
@@ -1,22 +0,0 @@ | |||
1 | use crate::{ | ||
2 | Crate, CrateDependency, AsName, Module, DefDatabase, | ||
3 | }; | ||
4 | |||
5 | impl Crate { | ||
6 | pub(crate) fn dependencies_impl(&self, db: &impl DefDatabase) -> Vec<CrateDependency> { | ||
7 | let crate_graph = db.crate_graph(); | ||
8 | crate_graph | ||
9 | .dependencies(self.crate_id) | ||
10 | .map(|dep| { | ||
11 | let krate = Crate { crate_id: dep.crate_id() }; | ||
12 | let name = dep.as_name(); | ||
13 | CrateDependency { krate, name } | ||
14 | }) | ||
15 | .collect() | ||
16 | } | ||
17 | pub(crate) fn root_module_impl(&self, db: &impl DefDatabase) -> Option<Module> { | ||
18 | let module_id = db.crate_def_map(*self).root(); | ||
19 | let module = Module { krate: *self, module_id }; | ||
20 | Some(module) | ||
21 | } | ||
22 | } | ||
diff --git a/crates/ra_hir/src/code_model_impl/module.rs b/crates/ra_hir/src/code_model_impl/module.rs deleted file mode 100644 index 5c2ea73ce..000000000 --- a/crates/ra_hir/src/code_model_impl/module.rs +++ /dev/null | |||
@@ -1,99 +0,0 @@ | |||
1 | use ra_db::FileId; | ||
2 | use ra_syntax::{ast, TreeArc}; | ||
3 | |||
4 | use crate::{ | ||
5 | Module, ModuleSource, Name, AstId, | ||
6 | nameres::CrateModuleId, | ||
7 | HirDatabase, DefDatabase, | ||
8 | HirFileId, | ||
9 | }; | ||
10 | |||
11 | impl ModuleSource { | ||
12 | pub(crate) fn new( | ||
13 | db: &impl DefDatabase, | ||
14 | file_id: Option<FileId>, | ||
15 | decl_id: Option<AstId<ast::Module>>, | ||
16 | ) -> ModuleSource { | ||
17 | match (file_id, decl_id) { | ||
18 | (Some(file_id), _) => { | ||
19 | let source_file = db.parse(file_id); | ||
20 | ModuleSource::SourceFile(source_file) | ||
21 | } | ||
22 | (None, Some(item_id)) => { | ||
23 | let module = item_id.to_node(db); | ||
24 | assert!(module.item_list().is_some(), "expected inline module"); | ||
25 | ModuleSource::Module(module.to_owned()) | ||
26 | } | ||
27 | (None, None) => panic!(), | ||
28 | } | ||
29 | } | ||
30 | } | ||
31 | |||
32 | impl Module { | ||
33 | fn with_module_id(&self, module_id: CrateModuleId) -> Module { | ||
34 | Module { module_id, krate: self.krate } | ||
35 | } | ||
36 | |||
37 | pub(crate) fn name_impl(&self, db: &impl HirDatabase) -> Option<Name> { | ||
38 | let def_map = db.crate_def_map(self.krate); | ||
39 | let parent = def_map[self.module_id].parent?; | ||
40 | def_map[parent].children.iter().find_map(|(name, module_id)| { | ||
41 | if *module_id == self.module_id { | ||
42 | Some(name.clone()) | ||
43 | } else { | ||
44 | None | ||
45 | } | ||
46 | }) | ||
47 | } | ||
48 | |||
49 | pub(crate) fn definition_source_impl( | ||
50 | &self, | ||
51 | db: &impl DefDatabase, | ||
52 | ) -> (HirFileId, ModuleSource) { | ||
53 | let def_map = db.crate_def_map(self.krate); | ||
54 | let decl_id = def_map[self.module_id].declaration; | ||
55 | let file_id = def_map[self.module_id].definition; | ||
56 | let module_source = ModuleSource::new(db, file_id, decl_id); | ||
57 | let file_id = file_id.map(HirFileId::from).unwrap_or_else(|| decl_id.unwrap().file_id()); | ||
58 | (file_id, module_source) | ||
59 | } | ||
60 | |||
61 | pub(crate) fn declaration_source_impl( | ||
62 | &self, | ||
63 | db: &impl HirDatabase, | ||
64 | ) -> Option<(HirFileId, TreeArc<ast::Module>)> { | ||
65 | let def_map = db.crate_def_map(self.krate); | ||
66 | let decl = def_map[self.module_id].declaration?; | ||
67 | let ast = decl.to_node(db); | ||
68 | Some((decl.file_id(), ast)) | ||
69 | } | ||
70 | |||
71 | pub(crate) fn crate_root_impl(&self, db: &impl DefDatabase) -> Module { | ||
72 | let def_map = db.crate_def_map(self.krate); | ||
73 | self.with_module_id(def_map.root()) | ||
74 | } | ||
75 | |||
76 | /// Finds a child module with the specified name. | ||
77 | pub(crate) fn child_impl(&self, db: &impl HirDatabase, name: &Name) -> Option<Module> { | ||
78 | let def_map = db.crate_def_map(self.krate); | ||
79 | let child_id = def_map[self.module_id].children.get(name)?; | ||
80 | Some(self.with_module_id(*child_id)) | ||
81 | } | ||
82 | |||
83 | /// Iterates over all child modules. | ||
84 | pub(crate) fn children_impl(&self, db: &impl DefDatabase) -> impl Iterator<Item = Module> { | ||
85 | let def_map = db.crate_def_map(self.krate); | ||
86 | let children = def_map[self.module_id] | ||
87 | .children | ||
88 | .iter() | ||
89 | .map(|(_, module_id)| self.with_module_id(*module_id)) | ||
90 | .collect::<Vec<_>>(); | ||
91 | children.into_iter() | ||
92 | } | ||
93 | |||
94 | pub(crate) fn parent_impl(&self, db: &impl DefDatabase) -> Option<Module> { | ||
95 | let def_map = db.crate_def_map(self.krate); | ||
96 | let parent_id = def_map[self.module_id].parent?; | ||
97 | Some(self.with_module_id(parent_id)) | ||
98 | } | ||
99 | } | ||
diff --git a/crates/ra_hir/src/impl_block.rs b/crates/ra_hir/src/impl_block.rs index 51fa491c3..637f6ab83 100644 --- a/crates/ra_hir/src/impl_block.rs +++ b/crates/ra_hir/src/impl_block.rs | |||
@@ -15,7 +15,7 @@ use crate::{ | |||
15 | resolve::Resolver, | 15 | resolve::Resolver, |
16 | ty::Ty, | 16 | ty::Ty, |
17 | generics::HasGenericParams, | 17 | generics::HasGenericParams, |
18 | code_model_api::{Module, ModuleSource} | 18 | code_model::{Module, ModuleSource} |
19 | }; | 19 | }; |
20 | 20 | ||
21 | #[derive(Debug, Default, PartialEq, Eq)] | 21 | #[derive(Debug, Default, PartialEq, Eq)] |
diff --git a/crates/ra_hir/src/lib.rs b/crates/ra_hir/src/lib.rs index 0135644db..fe2d4adee 100644 --- a/crates/ra_hir/src/lib.rs +++ b/crates/ra_hir/src/lib.rs | |||
@@ -42,8 +42,7 @@ mod docs; | |||
42 | mod resolve; | 42 | mod resolve; |
43 | pub mod diagnostics; | 43 | pub mod diagnostics; |
44 | 44 | ||
45 | mod code_model_api; | 45 | mod code_model; |
46 | mod code_model_impl; | ||
47 | 46 | ||
48 | #[cfg(test)] | 47 | #[cfg(test)] |
49 | mod marks; | 48 | mod marks; |
@@ -73,7 +72,7 @@ pub use self::{ | |||
73 | source_binder::{SourceAnalyzer, PathResolution, ScopeEntryWithSyntax,MacroByExampleDef}, | 72 | source_binder::{SourceAnalyzer, PathResolution, ScopeEntryWithSyntax,MacroByExampleDef}, |
74 | }; | 73 | }; |
75 | 74 | ||
76 | pub use self::code_model_api::{ | 75 | pub use self::code_model::{ |
77 | Crate, CrateDependency, | 76 | Crate, CrateDependency, |
78 | DefWithBody, | 77 | DefWithBody, |
79 | Module, ModuleDef, ModuleSource, | 78 | Module, ModuleDef, ModuleSource, |
diff --git a/crates/ra_hir/src/resolve.rs b/crates/ra_hir/src/resolve.rs index 3874e28bf..fedfe2fee 100644 --- a/crates/ra_hir/src/resolve.rs +++ b/crates/ra_hir/src/resolve.rs | |||
@@ -5,7 +5,7 @@ use rustc_hash::{FxHashMap, FxHashSet}; | |||
5 | 5 | ||
6 | use crate::{ | 6 | use crate::{ |
7 | ModuleDef, Trait, | 7 | ModuleDef, Trait, |
8 | code_model_api::Crate, | 8 | code_model::Crate, |
9 | MacroDefId, | 9 | MacroDefId, |
10 | db::HirDatabase, | 10 | db::HirDatabase, |
11 | name::{Name, KnownName}, | 11 | name::{Name, KnownName}, |