aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_hir/src/code_model_api.rs
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_hir/src/code_model_api.rs')
-rw-r--r--crates/ra_hir/src/code_model_api.rs140
1 files changed, 78 insertions, 62 deletions
diff --git a/crates/ra_hir/src/code_model_api.rs b/crates/ra_hir/src/code_model_api.rs
index 9e6170440..8f1ed1086 100644
--- a/crates/ra_hir/src/code_model_api.rs
+++ b/crates/ra_hir/src/code_model_api.rs
@@ -4,14 +4,14 @@ use ra_db::{CrateId, SourceRootId, Edition};
4use ra_syntax::{ast::self, TreeArc}; 4use ra_syntax::{ast::self, TreeArc};
5 5
6use crate::{ 6use crate::{
7 Name, ScopesWithSourceMap, Ty, HirFileId, ImportSource, 7 Name, 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}, 11 expr::{Body, BodySourceMap},
12 ty::InferenceResult, 12 ty::InferenceResult,
13 adt::{EnumVariantId, StructFieldId, VariantDef}, 13 adt::{EnumVariantId, StructFieldId, VariantDef},
14 generics::GenericParams, 14 generics::HasGenericParams,
15 docs::{Documentation, Docs, docs_from_ast}, 15 docs::{Documentation, Docs, docs_from_ast},
16 ids::{FunctionId, StructId, EnumId, AstItemDef, ConstId, StaticId, TraitId, TypeAliasId}, 16 ids::{FunctionId, StructId, EnumId, AstItemDef, ConstId, StaticId, TraitId, TypeAliasId},
17 impl_block::ImplBlock, 17 impl_block::ImplBlock,
@@ -117,8 +117,14 @@ impl Module {
117 } 117 }
118 118
119 /// Returns the syntax of the last path segment corresponding to this import 119 /// Returns the syntax of the last path segment corresponding to this import
120 pub fn import_source(&self, db: &impl HirDatabase, import: ImportId) -> ImportSource { 120 pub fn import_source(
121 self.import_source_impl(db, import) 121 &self,
122 db: &impl HirDatabase,
123 import: ImportId,
124 ) -> Either<TreeArc<ast::UseTree>, TreeArc<ast::ExternCrateItem>> {
125 let (file_id, source) = self.definition_source(db);
126 let (_, source_map) = db.raw_items_with_source_map(file_id);
127 source_map.get(&source, import)
122 } 128 }
123 129
124 /// Returns the crate this module is part of. 130 /// Returns the crate this module is part of.
@@ -183,12 +189,12 @@ impl Module {
183 } 189 }
184 } 190 }
185 191
186 pub fn resolver(&self, db: &impl HirDatabase) -> Resolver { 192 pub(crate) fn resolver(&self, db: &impl DefDatabase) -> Resolver {
187 let def_map = db.crate_def_map(self.krate); 193 let def_map = db.crate_def_map(self.krate);
188 Resolver::default().push_module_scope(def_map, self.module_id) 194 Resolver::default().push_module_scope(def_map, self.module_id)
189 } 195 }
190 196
191 pub fn declarations(self, db: &impl HirDatabase) -> Vec<ModuleDef> { 197 pub fn declarations(self, db: &impl DefDatabase) -> Vec<ModuleDef> {
192 let def_map = db.crate_def_map(self.krate); 198 let def_map = db.crate_def_map(self.krate);
193 def_map[self.module_id] 199 def_map[self.module_id]
194 .scope 200 .scope
@@ -293,10 +299,6 @@ impl Struct {
293 .map(|(id, _)| StructField { parent: (*self).into(), id }) 299 .map(|(id, _)| StructField { parent: (*self).into(), id })
294 } 300 }
295 301
296 pub fn generic_params(&self, db: &impl DefDatabase) -> Arc<GenericParams> {
297 db.generic_params((*self).into())
298 }
299
300 pub fn ty(&self, db: &impl HirDatabase) -> Ty { 302 pub fn ty(&self, db: &impl HirDatabase) -> Ty {
301 db.type_for_def((*self).into(), Namespace::Types) 303 db.type_for_def((*self).into(), Namespace::Types)
302 } 304 }
@@ -307,7 +309,7 @@ impl Struct {
307 309
308 // FIXME move to a more general type 310 // FIXME move to a more general type
309 /// Builds a resolver for type references inside this struct. 311 /// Builds a resolver for type references inside this struct.
310 pub fn resolver(&self, db: &impl HirDatabase) -> Resolver { 312 pub(crate) fn resolver(&self, db: &impl HirDatabase) -> Resolver {
311 // take the outer scope... 313 // take the outer scope...
312 let r = self.module(db).resolver(db); 314 let r = self.module(db).resolver(db);
313 // ...and add generic params, if present 315 // ...and add generic params, if present
@@ -357,17 +359,13 @@ impl Enum {
357 .map(|(id, _)| EnumVariant { parent: *self, id }) 359 .map(|(id, _)| EnumVariant { parent: *self, id })
358 } 360 }
359 361
360 pub fn generic_params(&self, db: &impl DefDatabase) -> Arc<GenericParams> {
361 db.generic_params((*self).into())
362 }
363
364 pub fn ty(&self, db: &impl HirDatabase) -> Ty { 362 pub fn ty(&self, db: &impl HirDatabase) -> Ty {
365 db.type_for_def((*self).into(), Namespace::Types) 363 db.type_for_def((*self).into(), Namespace::Types)
366 } 364 }
367 365
368 // FIXME: move to a more general type 366 // FIXME: move to a more general type
369 /// Builds a resolver for type references inside this struct. 367 /// Builds a resolver for type references inside this struct.
370 pub fn resolver(&self, db: &impl HirDatabase) -> Resolver { 368 pub(crate) fn resolver(&self, db: &impl HirDatabase) -> Resolver {
371 // take the outer scope... 369 // take the outer scope...
372 let r = self.module(db).resolver(db); 370 let r = self.module(db).resolver(db);
373 // ...and add generic params, if present 371 // ...and add generic params, if present
@@ -444,28 +442,22 @@ impl DefWithBody {
444 db.infer(*self) 442 db.infer(*self)
445 } 443 }
446 444
447 pub fn body_source_map(&self, db: &impl HirDatabase) -> Arc<BodySourceMap> {
448 db.body_with_source_map(*self).1
449 }
450
451 pub fn body(&self, db: &impl HirDatabase) -> Arc<Body> { 445 pub fn body(&self, db: &impl HirDatabase) -> Arc<Body> {
452 db.body_hir(*self) 446 db.body_hir(*self)
453 } 447 }
454 448
449 pub fn body_source_map(&self, db: &impl HirDatabase) -> Arc<BodySourceMap> {
450 db.body_with_source_map(*self).1
451 }
452
455 /// Builds a resolver for code inside this item. 453 /// Builds a resolver for code inside this item.
456 pub fn resolver(&self, db: &impl HirDatabase) -> Resolver { 454 pub(crate) fn resolver(&self, db: &impl HirDatabase) -> Resolver {
457 match *self { 455 match *self {
458 DefWithBody::Const(ref c) => c.resolver(db), 456 DefWithBody::Const(ref c) => c.resolver(db),
459 DefWithBody::Function(ref f) => f.resolver(db), 457 DefWithBody::Function(ref f) => f.resolver(db),
460 DefWithBody::Static(ref s) => s.resolver(db), 458 DefWithBody::Static(ref s) => s.resolver(db),
461 } 459 }
462 } 460 }
463
464 pub fn scopes(&self, db: &impl HirDatabase) -> ScopesWithSourceMap {
465 let scopes = db.expr_scopes(*self);
466 let source_map = db.body_with_source_map(*self).1;
467 ScopesWithSourceMap { scopes, source_map }
468 }
469} 461}
470 462
471#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] 463#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
@@ -517,7 +509,7 @@ impl Function {
517 self.signature(db).name.clone() 509 self.signature(db).name.clone()
518 } 510 }
519 511
520 pub fn body_source_map(&self, db: &impl HirDatabase) -> Arc<BodySourceMap> { 512 pub(crate) fn body_source_map(&self, db: &impl HirDatabase) -> Arc<BodySourceMap> {
521 db.body_with_source_map((*self).into()).1 513 db.body_with_source_map((*self).into()).1
522 } 514 }
523 515
@@ -529,12 +521,6 @@ impl Function {
529 db.type_for_def((*self).into(), Namespace::Values) 521 db.type_for_def((*self).into(), Namespace::Values)
530 } 522 }
531 523
532 pub fn scopes(&self, db: &impl HirDatabase) -> ScopesWithSourceMap {
533 let scopes = db.expr_scopes((*self).into());
534 let source_map = db.body_with_source_map((*self).into()).1;
535 ScopesWithSourceMap { scopes, source_map }
536 }
537
538 pub fn signature(&self, db: &impl HirDatabase) -> Arc<FnSignature> { 524 pub fn signature(&self, db: &impl HirDatabase) -> Arc<FnSignature> {
539 db.fn_signature(*self) 525 db.fn_signature(*self)
540 } 526 }
@@ -543,24 +529,32 @@ impl Function {
543 db.infer((*self).into()) 529 db.infer((*self).into())
544 } 530 }
545 531
546 pub fn generic_params(&self, db: &impl DefDatabase) -> Arc<GenericParams> {
547 db.generic_params((*self).into())
548 }
549
550 /// The containing impl block, if this is a method. 532 /// The containing impl block, if this is a method.
551 pub fn impl_block(&self, db: &impl DefDatabase) -> Option<ImplBlock> { 533 pub fn impl_block(&self, db: &impl DefDatabase) -> Option<ImplBlock> {
552 let module_impls = db.impls_in_module(self.module(db)); 534 let module_impls = db.impls_in_module(self.module(db));
553 ImplBlock::containing(module_impls, (*self).into()) 535 ImplBlock::containing(module_impls, (*self).into())
554 } 536 }
555 537
538 /// The containing trait, if this is a trait method definition.
539 pub fn parent_trait(&self, db: &impl DefDatabase) -> Option<Trait> {
540 db.trait_items_index(self.module(db)).get_parent_trait((*self).into())
541 }
542
543 pub fn container(&self, db: &impl DefDatabase) -> Option<Container> {
544 if let Some(impl_block) = self.impl_block(db) {
545 Some(impl_block.into())
546 } else if let Some(trait_) = self.parent_trait(db) {
547 Some(trait_.into())
548 } else {
549 None
550 }
551 }
552
556 // FIXME: move to a more general type for 'body-having' items 553 // FIXME: move to a more general type for 'body-having' items
557 /// Builds a resolver for code inside this item. 554 /// Builds a resolver for code inside this item.
558 pub fn resolver(&self, db: &impl HirDatabase) -> Resolver { 555 pub(crate) fn resolver(&self, db: &impl HirDatabase) -> Resolver {
559 // take the outer scope... 556 // take the outer scope...
560 let r = self 557 let r = self.container(db).map_or_else(|| self.module(db).resolver(db), |c| c.resolver(db));
561 .impl_block(db)
562 .map(|ib| ib.resolver(db))
563 .unwrap_or_else(|| self.module(db).resolver(db));
564 // ...and add generic params, if present 558 // ...and add generic params, if present
565 let p = self.generic_params(db); 559 let p = self.generic_params(db);
566 let r = if !p.params.is_empty() { r.push_generic_params_scope(p) } else { r }; 560 let r = if !p.params.is_empty() { r.push_generic_params_scope(p) } else { r };
@@ -600,10 +594,6 @@ impl Const {
600 db.infer((*self).into()) 594 db.infer((*self).into())
601 } 595 }
602 596
603 pub fn body_source_map(&self, db: &impl HirDatabase) -> Arc<BodySourceMap> {
604 db.body_with_source_map((*self).into()).1
605 }
606
607 /// The containing impl block, if this is a method. 597 /// The containing impl block, if this is a method.
608 pub fn impl_block(&self, db: &impl DefDatabase) -> Option<ImplBlock> { 598 pub fn impl_block(&self, db: &impl DefDatabase) -> Option<ImplBlock> {
609 let module_impls = db.impls_in_module(self.module(db)); 599 let module_impls = db.impls_in_module(self.module(db));
@@ -612,7 +602,7 @@ impl Const {
612 602
613 // FIXME: move to a more general type for 'body-having' items 603 // FIXME: move to a more general type for 'body-having' items
614 /// Builds a resolver for code inside this item. 604 /// Builds a resolver for code inside this item.
615 pub fn resolver(&self, db: &impl HirDatabase) -> Resolver { 605 pub(crate) fn resolver(&self, db: &impl HirDatabase) -> Resolver {
616 // take the outer scope... 606 // take the outer scope...
617 let r = self 607 let r = self
618 .impl_block(db) 608 .impl_block(db)
@@ -664,7 +654,7 @@ impl Static {
664 } 654 }
665 655
666 /// Builds a resolver for code inside this item. 656 /// Builds a resolver for code inside this item.
667 pub fn resolver(&self, db: &impl HirDatabase) -> Resolver { 657 pub(crate) fn resolver(&self, db: &impl HirDatabase) -> Resolver {
668 // take the outer scope... 658 // take the outer scope...
669 self.module(db).resolver(db) 659 self.module(db).resolver(db)
670 } 660 }
@@ -672,10 +662,6 @@ impl Static {
672 pub fn infer(&self, db: &impl HirDatabase) -> Arc<InferenceResult> { 662 pub fn infer(&self, db: &impl HirDatabase) -> Arc<InferenceResult> {
673 db.infer((*self).into()) 663 db.infer((*self).into())
674 } 664 }
675
676 pub fn body_source_map(&self, db: &impl HirDatabase) -> Arc<BodySourceMap> {
677 db.body_with_source_map((*self).into()).1
678 }
679} 665}
680 666
681impl Docs for Static { 667impl Docs for Static {
@@ -698,10 +684,6 @@ impl Trait {
698 self.id.module(db) 684 self.id.module(db)
699 } 685 }
700 686
701 pub fn generic_params(&self, db: &impl DefDatabase) -> Arc<GenericParams> {
702 db.generic_params((*self).into())
703 }
704
705 pub fn name(self, db: &impl DefDatabase) -> Option<Name> { 687 pub fn name(self, db: &impl DefDatabase) -> Option<Name> {
706 self.trait_data(db).name().clone() 688 self.trait_data(db).name().clone()
707 } 689 }
@@ -713,6 +695,14 @@ impl Trait {
713 pub(crate) fn trait_data(self, db: &impl DefDatabase) -> Arc<TraitData> { 695 pub(crate) fn trait_data(self, db: &impl DefDatabase) -> Arc<TraitData> {
714 db.trait_data(self) 696 db.trait_data(self)
715 } 697 }
698
699 pub(crate) fn resolver(&self, db: &impl DefDatabase) -> Resolver {
700 let r = self.module(db).resolver(db);
701 // add generic params, if present
702 let p = self.generic_params(db);
703 let r = if !p.params.is_empty() { r.push_generic_params_scope(p) } else { r };
704 r
705 }
716} 706}
717 707
718impl Docs for Trait { 708impl Docs for Trait {
@@ -731,10 +721,6 @@ impl TypeAlias {
731 self.id.source(db) 721 self.id.source(db)
732 } 722 }
733 723
734 pub fn generic_params(&self, db: &impl DefDatabase) -> Arc<GenericParams> {
735 db.generic_params((*self).into())
736 }
737
738 pub fn module(&self, db: &impl DefDatabase) -> Module { 724 pub fn module(&self, db: &impl DefDatabase) -> Module {
739 self.id.module(db) 725 self.id.module(db)
740 } 726 }
@@ -745,12 +731,27 @@ impl TypeAlias {
745 ImplBlock::containing(module_impls, (*self).into()) 731 ImplBlock::containing(module_impls, (*self).into())
746 } 732 }
747 733
734 /// The containing trait, if this is a trait method definition.
735 pub fn parent_trait(&self, db: &impl DefDatabase) -> Option<Trait> {
736 db.trait_items_index(self.module(db)).get_parent_trait((*self).into())
737 }
738
739 pub fn container(&self, db: &impl DefDatabase) -> Option<Container> {
740 if let Some(impl_block) = self.impl_block(db) {
741 Some(impl_block.into())
742 } else if let Some(trait_) = self.parent_trait(db) {
743 Some(trait_.into())
744 } else {
745 None
746 }
747 }
748
748 pub fn type_ref(self, db: &impl DefDatabase) -> Arc<TypeRef> { 749 pub fn type_ref(self, db: &impl DefDatabase) -> Arc<TypeRef> {
749 db.type_alias_ref(self) 750 db.type_alias_ref(self)
750 } 751 }
751 752
752 /// Builds a resolver for the type references in this type alias. 753 /// Builds a resolver for the type references in this type alias.
753 pub fn resolver(&self, db: &impl HirDatabase) -> Resolver { 754 pub(crate) fn resolver(&self, db: &impl HirDatabase) -> Resolver {
754 // take the outer scope... 755 // take the outer scope...
755 let r = self 756 let r = self
756 .impl_block(db) 757 .impl_block(db)
@@ -768,3 +769,18 @@ impl Docs for TypeAlias {
768 docs_from_ast(&*self.source(db).1) 769 docs_from_ast(&*self.source(db).1)
769 } 770 }
770} 771}
772
773pub enum Container {
774 Trait(Trait),
775 ImplBlock(ImplBlock),
776}
777impl_froms!(Container: Trait, ImplBlock);
778
779impl Container {
780 pub(crate) fn resolver(&self, db: &impl DefDatabase) -> Resolver {
781 match self {
782 Container::Trait(trait_) => trait_.resolver(db),
783 Container::ImplBlock(impl_block) => impl_block.resolver(db),
784 }
785 }
786}