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.rs120
1 files changed, 14 insertions, 106 deletions
diff --git a/crates/ra_hir/src/code_model.rs b/crates/ra_hir/src/code_model.rs
index 27850028b..830aea1f3 100644
--- a/crates/ra_hir/src/code_model.rs
+++ b/crates/ra_hir/src/code_model.rs
@@ -1,10 +1,13 @@
1pub(crate) mod src;
2pub(crate) mod docs;
3
1use std::sync::Arc; 4use std::sync::Arc;
2 5
3use ra_db::{CrateId, SourceRootId, Edition, FileId}; 6use ra_db::{CrateId, SourceRootId, Edition, FileId};
4use ra_syntax::{ast::{self, NameOwner, TypeAscriptionOwner}, TreeArc}; 7use ra_syntax::{ast::{self, NameOwner, TypeAscriptionOwner}, TreeArc};
5 8
6use crate::{ 9use crate::{
7 Name, AsName, AstId, Ty, HirFileId, Either, KnownName, 10 Name, AsName, AstId, Ty, Either, KnownName, HasSource,
8 HirDatabase, DefDatabase, AstDatabase, 11 HirDatabase, DefDatabase, AstDatabase,
9 type_ref::TypeRef, 12 type_ref::TypeRef,
10 nameres::{ModuleScope, Namespace, ImportId, CrateModuleId}, 13 nameres::{ModuleScope, Namespace, ImportId, CrateModuleId},
@@ -179,40 +182,15 @@ impl Module {
179 }) 182 })
180 } 183 }
181 184
182 /// Returns a node which defines this module. That is, a file or a `mod foo {}` with items.
183 pub fn definition_source(
184 self,
185 db: &(impl DefDatabase + AstDatabase),
186 ) -> (HirFileId, ModuleSource) {
187 let def_map = db.crate_def_map(self.krate);
188 let decl_id = def_map[self.module_id].declaration;
189 let file_id = def_map[self.module_id].definition;
190 let module_source = ModuleSource::new(db, file_id, decl_id);
191 let file_id = file_id.map(HirFileId::from).unwrap_or_else(|| decl_id.unwrap().file_id());
192 (file_id, module_source)
193 }
194
195 /// Returns a node which declares this module, either a `mod foo;` or a `mod foo {}`.
196 /// `None` for the crate root.
197 pub fn declaration_source(
198 self,
199 db: &(impl DefDatabase + AstDatabase),
200 ) -> Option<(HirFileId, TreeArc<ast::Module>)> {
201 let def_map = db.crate_def_map(self.krate);
202 let decl = def_map[self.module_id].declaration?;
203 let ast = decl.to_node(db);
204 Some((decl.file_id(), ast))
205 }
206
207 /// Returns the syntax of the last path segment corresponding to this import 185 /// Returns the syntax of the last path segment corresponding to this import
208 pub fn import_source( 186 pub fn import_source(
209 self, 187 self,
210 db: &impl HirDatabase, 188 db: &impl HirDatabase,
211 import: ImportId, 189 import: ImportId,
212 ) -> Either<TreeArc<ast::UseTree>, TreeArc<ast::ExternCrateItem>> { 190 ) -> Either<TreeArc<ast::UseTree>, TreeArc<ast::ExternCrateItem>> {
213 let (file_id, source) = self.definition_source(db); 191 let src = self.definition_source(db);
214 let (_, source_map) = db.raw_items_with_source_map(file_id); 192 let (_, source_map) = db.raw_items_with_source_map(src.file_id);
215 source_map.get(&source, import) 193 source_map.get(&src.ast, import)
216 } 194 }
217 195
218 /// Returns the crate this module is part of. 196 /// Returns the crate this module is part of.
@@ -335,10 +313,6 @@ impl StructField {
335 self.parent.variant_data(db).fields().unwrap()[self.id].name.clone() 313 self.parent.variant_data(db).fields().unwrap()[self.id].name.clone()
336 } 314 }
337 315
338 pub fn source(&self, db: &(impl DefDatabase + AstDatabase)) -> (HirFileId, FieldSource) {
339 self.source_impl(db)
340 }
341
342 pub fn ty(&self, db: &impl HirDatabase) -> Ty { 316 pub fn ty(&self, db: &impl HirDatabase) -> Ty {
343 db.type_for_field(*self) 317 db.type_for_field(*self)
344 } 318 }
@@ -354,13 +328,6 @@ pub struct Struct {
354} 328}
355 329
356impl Struct { 330impl Struct {
357 pub fn source(
358 self,
359 db: &(impl DefDatabase + AstDatabase),
360 ) -> (HirFileId, TreeArc<ast::StructDef>) {
361 self.id.source(db)
362 }
363
364 pub fn module(self, db: &impl HirDatabase) -> Module { 331 pub fn module(self, db: &impl HirDatabase) -> Module {
365 self.id.module(db) 332 self.id.module(db)
366 } 333 }
@@ -415,13 +382,6 @@ pub struct Union {
415} 382}
416 383
417impl Union { 384impl Union {
418 pub fn source(
419 self,
420 db: &(impl DefDatabase + AstDatabase),
421 ) -> (HirFileId, TreeArc<ast::StructDef>) {
422 self.id.source(db)
423 }
424
425 pub fn name(self, db: &impl DefDatabase) -> Option<Name> { 385 pub fn name(self, db: &impl DefDatabase) -> Option<Name> {
426 db.struct_data(Struct { id: self.id }).name.clone() 386 db.struct_data(Struct { id: self.id }).name.clone()
427 } 387 }
@@ -448,13 +408,6 @@ pub struct Enum {
448} 408}
449 409
450impl Enum { 410impl Enum {
451 pub fn source(
452 self,
453 db: &(impl DefDatabase + AstDatabase),
454 ) -> (HirFileId, TreeArc<ast::EnumDef>) {
455 self.id.source(db)
456 }
457
458 pub fn module(self, db: &impl HirDatabase) -> Module { 411 pub fn module(self, db: &impl HirDatabase) -> Module {
459 self.id.module(db) 412 self.id.module(db)
460 } 413 }
@@ -498,12 +451,6 @@ pub struct EnumVariant {
498} 451}
499 452
500impl EnumVariant { 453impl EnumVariant {
501 pub fn source(
502 &self,
503 db: &(impl DefDatabase + AstDatabase),
504 ) -> (HirFileId, TreeArc<ast::EnumVariant>) {
505 self.source_impl(db)
506 }
507 pub fn module(&self, db: &impl HirDatabase) -> Module { 454 pub fn module(&self, db: &impl HirDatabase) -> Module {
508 self.parent.module(db) 455 self.parent.module(db)
509 } 456 }
@@ -588,11 +535,11 @@ impl FnSignature {
588 db: &(impl DefDatabase + AstDatabase), 535 db: &(impl DefDatabase + AstDatabase),
589 func: Function, 536 func: Function,
590 ) -> Arc<FnSignature> { 537 ) -> Arc<FnSignature> {
591 let (_, node) = func.source(db); 538 let src = func.source(db);
592 let name = node.name().map(|n| n.as_name()).unwrap_or_else(Name::missing); 539 let name = src.ast.name().map(|n| n.as_name()).unwrap_or_else(Name::missing);
593 let mut params = Vec::new(); 540 let mut params = Vec::new();
594 let mut has_self_param = false; 541 let mut has_self_param = false;
595 if let Some(param_list) = node.param_list() { 542 if let Some(param_list) = src.ast.param_list() {
596 if let Some(self_param) = param_list.self_param() { 543 if let Some(self_param) = param_list.self_param() {
597 let self_type = if let Some(type_ref) = self_param.ascribed_type() { 544 let self_type = if let Some(type_ref) = self_param.ascribed_type() {
598 TypeRef::from_ast(type_ref) 545 TypeRef::from_ast(type_ref)
@@ -616,7 +563,7 @@ impl FnSignature {
616 params.push(type_ref); 563 params.push(type_ref);
617 } 564 }
618 } 565 }
619 let ret_type = if let Some(type_ref) = node.ret_type().and_then(|rt| rt.type_ref()) { 566 let ret_type = if let Some(type_ref) = src.ast.ret_type().and_then(|rt| rt.type_ref()) {
620 TypeRef::from_ast(type_ref) 567 TypeRef::from_ast(type_ref)
621 } else { 568 } else {
622 TypeRef::unit() 569 TypeRef::unit()
@@ -645,10 +592,6 @@ impl FnSignature {
645} 592}
646 593
647impl Function { 594impl Function {
648 pub fn source(self, db: &(impl DefDatabase + AstDatabase)) -> (HirFileId, TreeArc<ast::FnDef>) {
649 self.id.source(db)
650 }
651
652 pub fn module(self, db: &impl DefDatabase) -> Module { 595 pub fn module(self, db: &impl DefDatabase) -> Module {
653 self.id.module(db) 596 self.id.module(db)
654 } 597 }
@@ -723,13 +666,6 @@ pub struct Const {
723} 666}
724 667
725impl Const { 668impl Const {
726 pub fn source(
727 self,
728 db: &(impl DefDatabase + AstDatabase),
729 ) -> (HirFileId, TreeArc<ast::ConstDef>) {
730 self.id.source(db)
731 }
732
733 pub fn module(self, db: &impl DefDatabase) -> Module { 669 pub fn module(self, db: &impl DefDatabase) -> Module {
734 self.id.module(db) 670 self.id.module(db)
735 } 671 }
@@ -780,7 +716,7 @@ impl ConstSignature {
780 db: &(impl DefDatabase + AstDatabase), 716 db: &(impl DefDatabase + AstDatabase),
781 konst: Const, 717 konst: Const,
782 ) -> Arc<ConstSignature> { 718 ) -> Arc<ConstSignature> {
783 let (_, node) = konst.source(db); 719 let node = konst.source(db).ast;
784 const_signature_for(&*node) 720 const_signature_for(&*node)
785 } 721 }
786 722
@@ -788,7 +724,7 @@ impl ConstSignature {
788 db: &(impl DefDatabase + AstDatabase), 724 db: &(impl DefDatabase + AstDatabase),
789 konst: Static, 725 konst: Static,
790 ) -> Arc<ConstSignature> { 726 ) -> Arc<ConstSignature> {
791 let (_, node) = konst.source(db); 727 let node = konst.source(db).ast;
792 const_signature_for(&*node) 728 const_signature_for(&*node)
793 } 729 }
794} 730}
@@ -806,13 +742,6 @@ pub struct Static {
806} 742}
807 743
808impl Static { 744impl Static {
809 pub fn source(
810 self,
811 db: &(impl DefDatabase + AstDatabase),
812 ) -> (HirFileId, TreeArc<ast::StaticDef>) {
813 self.id.source(db)
814 }
815
816 pub fn module(self, db: &impl DefDatabase) -> Module { 745 pub fn module(self, db: &impl DefDatabase) -> Module {
817 self.id.module(db) 746 self.id.module(db)
818 } 747 }
@@ -838,13 +767,6 @@ pub struct Trait {
838} 767}
839 768
840impl Trait { 769impl Trait {
841 pub fn source(
842 self,
843 db: &(impl DefDatabase + AstDatabase),
844 ) -> (HirFileId, TreeArc<ast::TraitDef>) {
845 self.id.source(db)
846 }
847
848 pub fn module(self, db: &impl DefDatabase) -> Module { 770 pub fn module(self, db: &impl DefDatabase) -> Module {
849 self.id.module(db) 771 self.id.module(db)
850 } 772 }
@@ -884,13 +806,6 @@ pub struct TypeAlias {
884} 806}
885 807
886impl TypeAlias { 808impl TypeAlias {
887 pub fn source(
888 self,
889 db: &(impl DefDatabase + AstDatabase),
890 ) -> (HirFileId, TreeArc<ast::TypeAliasDef>) {
891 self.id.source(db)
892 }
893
894 pub fn module(self, db: &impl DefDatabase) -> Module { 809 pub fn module(self, db: &impl DefDatabase) -> Module {
895 self.id.module(db) 810 self.id.module(db)
896 } 811 }
@@ -939,14 +854,7 @@ pub struct MacroDef {
939 pub(crate) id: MacroDefId, 854 pub(crate) id: MacroDefId,
940} 855}
941 856
942impl MacroDef { 857impl MacroDef {}
943 pub fn source(
944 &self,
945 db: &(impl DefDatabase + AstDatabase),
946 ) -> (HirFileId, TreeArc<ast::MacroCall>) {
947 (self.id.0.file_id(), self.id.0.to_node(db))
948 }
949}
950 858
951pub enum Container { 859pub enum Container {
952 Trait(Trait), 860 Trait(Trait),