aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_hir/src
diff options
context:
space:
mode:
authorAleksey Kladov <[email protected]>2019-11-14 14:37:22 +0000
committerAleksey Kladov <[email protected]>2019-11-14 14:37:22 +0000
commit1583ab1558022d0fdbbc10d3a440a2d3daa4a840 (patch)
treeff8a962228a7532a5f468862bc4b0c62535f5069 /crates/ra_hir/src
parentef02296b9fe2c2e42b9390a3a14ee860f08fd02c (diff)
Move body queries to hir_def
Diffstat (limited to 'crates/ra_hir/src')
-rw-r--r--crates/ra_hir/src/code_model.rs28
-rw-r--r--crates/ra_hir/src/db.rs19
-rw-r--r--crates/ra_hir/src/expr.rs43
-rw-r--r--crates/ra_hir/src/from_id.rs14
-rw-r--r--crates/ra_hir/src/from_source.rs4
-rw-r--r--crates/ra_hir/src/source_binder.rs2
-rw-r--r--crates/ra_hir/src/ty/infer.rs5
-rw-r--r--crates/ra_hir/src/ty/traits/chalk.rs4
8 files changed, 48 insertions, 71 deletions
diff --git a/crates/ra_hir/src/code_model.rs b/crates/ra_hir/src/code_model.rs
index 2fd4ccb10..962d5a8c1 100644
--- a/crates/ra_hir/src/code_model.rs
+++ b/crates/ra_hir/src/code_model.rs
@@ -8,6 +8,7 @@ use std::sync::Arc;
8 8
9use hir_def::{ 9use hir_def::{
10 adt::VariantData, 10 adt::VariantData,
11 body::scope::ExprScopes,
11 builtin_type::BuiltinType, 12 builtin_type::BuiltinType,
12 type_ref::{Mutability, TypeRef}, 13 type_ref::{Mutability, TypeRef},
13 CrateModuleId, LocalEnumVariantId, LocalStructFieldId, ModuleId, UnionId, 14 CrateModuleId, LocalEnumVariantId, LocalStructFieldId, ModuleId, UnionId,
@@ -539,6 +540,7 @@ pub trait HasBody: Copy {
539 fn infer(self, db: &impl HirDatabase) -> Arc<InferenceResult>; 540 fn infer(self, db: &impl HirDatabase) -> Arc<InferenceResult>;
540 fn body(self, db: &impl HirDatabase) -> Arc<Body>; 541 fn body(self, db: &impl HirDatabase) -> Arc<Body>;
541 fn body_source_map(self, db: &impl HirDatabase) -> Arc<BodySourceMap>; 542 fn body_source_map(self, db: &impl HirDatabase) -> Arc<BodySourceMap>;
543 fn expr_scopes(self, db: &impl HirDatabase) -> Arc<ExprScopes>;
542} 544}
543 545
544impl<T> HasBody for T 546impl<T> HasBody for T
@@ -550,11 +552,15 @@ where
550 } 552 }
551 553
552 fn body(self, db: &impl HirDatabase) -> Arc<Body> { 554 fn body(self, db: &impl HirDatabase) -> Arc<Body> {
553 db.body(self.into()) 555 self.into().body(db)
554 } 556 }
555 557
556 fn body_source_map(self, db: &impl HirDatabase) -> Arc<BodySourceMap> { 558 fn body_source_map(self, db: &impl HirDatabase) -> Arc<BodySourceMap> {
557 db.body_with_source_map(self.into()).1 559 self.into().body_source_map(db)
560 }
561
562 fn expr_scopes(self, db: &impl HirDatabase) -> Arc<ExprScopes> {
563 self.into().expr_scopes(db)
558 } 564 }
559} 565}
560 566
@@ -564,11 +570,15 @@ impl HasBody for DefWithBody {
564 } 570 }
565 571
566 fn body(self, db: &impl HirDatabase) -> Arc<Body> { 572 fn body(self, db: &impl HirDatabase) -> Arc<Body> {
567 db.body(self) 573 db.body(self.into())
568 } 574 }
569 575
570 fn body_source_map(self, db: &impl HirDatabase) -> Arc<BodySourceMap> { 576 fn body_source_map(self, db: &impl HirDatabase) -> Arc<BodySourceMap> {
571 db.body_with_source_map(self).1 577 db.body_with_source_map(self.into()).1
578 }
579
580 fn expr_scopes(self, db: &impl HirDatabase) -> Arc<ExprScopes> {
581 db.expr_scopes(self.into())
572 } 582 }
573} 583}
574 584
@@ -662,11 +672,11 @@ impl Function {
662 } 672 }
663 673
664 pub(crate) fn body_source_map(self, db: &impl HirDatabase) -> Arc<BodySourceMap> { 674 pub(crate) fn body_source_map(self, db: &impl HirDatabase) -> Arc<BodySourceMap> {
665 db.body_with_source_map(self.into()).1 675 db.body_with_source_map(self.id.into()).1
666 } 676 }
667 677
668 pub fn body(self, db: &impl HirDatabase) -> Arc<Body> { 678 pub fn body(self, db: &impl HirDatabase) -> Arc<Body> {
669 db.body(self.into()) 679 db.body(self.id.into())
670 } 680 }
671 681
672 pub fn ty(self, db: &impl HirDatabase) -> Ty { 682 pub fn ty(self, db: &impl HirDatabase) -> Ty {
@@ -1079,7 +1089,7 @@ pub struct Local {
1079 1089
1080impl Local { 1090impl Local {
1081 pub fn name(self, db: &impl HirDatabase) -> Option<Name> { 1091 pub fn name(self, db: &impl HirDatabase) -> Option<Name> {
1082 let body = db.body(self.parent); 1092 let body = self.parent.body(db);
1083 match &body[self.pat_id] { 1093 match &body[self.pat_id] {
1084 Pat::Bind { name, .. } => Some(name.clone()), 1094 Pat::Bind { name, .. } => Some(name.clone()),
1085 _ => None, 1095 _ => None,
@@ -1091,7 +1101,7 @@ impl Local {
1091 } 1101 }
1092 1102
1093 pub fn is_mut(self, db: &impl HirDatabase) -> bool { 1103 pub fn is_mut(self, db: &impl HirDatabase) -> bool {
1094 let body = db.body(self.parent); 1104 let body = self.parent.body(db);
1095 match &body[self.pat_id] { 1105 match &body[self.pat_id] {
1096 Pat::Bind { mode, .. } => match mode { 1106 Pat::Bind { mode, .. } => match mode {
1097 BindingAnnotation::Mutable | BindingAnnotation::RefMut => true, 1107 BindingAnnotation::Mutable | BindingAnnotation::RefMut => true,
@@ -1115,7 +1125,7 @@ impl Local {
1115 } 1125 }
1116 1126
1117 pub fn source(self, db: &impl HirDatabase) -> Source<Either<ast::BindPat, ast::SelfParam>> { 1127 pub fn source(self, db: &impl HirDatabase) -> Source<Either<ast::BindPat, ast::SelfParam>> {
1118 let (_body, source_map) = db.body_with_source_map(self.parent); 1128 let source_map = self.parent.body_source_map(db);
1119 let src = source_map.pat_syntax(self.pat_id).unwrap(); // Hmm... 1129 let src = source_map.pat_syntax(self.pat_id).unwrap(); // Hmm...
1120 let root = src.file_syntax(db); 1130 let root = src.file_syntax(db);
1121 src.map(|ast| ast.map(|it| it.cast().unwrap().to_node(&root), |it| it.to_node(&root))) 1131 src.map(|ast| ast.map(|it| it.cast().unwrap().to_node(&root), |it| it.to_node(&root)))
diff --git a/crates/ra_hir/src/db.rs b/crates/ra_hir/src/db.rs
index 14f6b5df4..c60029c01 100644
--- a/crates/ra_hir/src/db.rs
+++ b/crates/ra_hir/src/db.rs
@@ -8,7 +8,6 @@ use ra_syntax::SmolStr;
8 8
9use crate::{ 9use crate::{
10 debug::HirDebugDatabase, 10 debug::HirDebugDatabase,
11 expr::{Body, BodySourceMap},
12 generics::{GenericDef, GenericParams}, 11 generics::{GenericDef, GenericParams},
13 ids, 12 ids,
14 impl_block::{ImplBlock, ImplSourceMap, ModuleImplBlocks}, 13 impl_block::{ImplBlock, ImplSourceMap, ModuleImplBlocks},
@@ -19,13 +18,14 @@ use crate::{
19 InferenceResult, Namespace, Substs, Ty, TypableDef, TypeCtor, 18 InferenceResult, Namespace, Substs, Ty, TypableDef, TypeCtor,
20 }, 19 },
21 type_alias::TypeAliasData, 20 type_alias::TypeAliasData,
22 Const, ConstData, Crate, DefWithBody, ExprScopes, FnData, Function, Module, Static, 21 Const, ConstData, Crate, DefWithBody, FnData, Function, Module, Static, StructField, Trait,
23 StructField, Trait, TypeAlias, 22 TypeAlias,
24}; 23};
25 24
26pub use hir_def::db::{ 25pub use hir_def::db::{
27 CrateDefMapQuery, DefDatabase2, DefDatabase2Storage, EnumDataQuery, InternDatabase, 26 BodyQuery, BodyWithSourceMapQuery, CrateDefMapQuery, DefDatabase2, DefDatabase2Storage,
28 InternDatabaseStorage, RawItemsQuery, RawItemsWithSourceMapQuery, StructDataQuery, 27 EnumDataQuery, ExprScopesQuery, InternDatabase, InternDatabaseStorage, RawItemsQuery,
28 RawItemsWithSourceMapQuery, StructDataQuery,
29}; 29};
30pub use hir_expand::db::{ 30pub use hir_expand::db::{
31 AstDatabase, AstDatabaseStorage, AstIdMapQuery, MacroArgQuery, MacroDefQuery, MacroExpandQuery, 31 AstDatabase, AstDatabaseStorage, AstIdMapQuery, MacroArgQuery, MacroDefQuery, MacroExpandQuery,
@@ -85,9 +85,6 @@ pub trait DefDatabase: HirDebugDatabase + DefDatabase2 {
85#[salsa::query_group(HirDatabaseStorage)] 85#[salsa::query_group(HirDatabaseStorage)]
86#[salsa::requires(salsa::Database)] 86#[salsa::requires(salsa::Database)]
87pub trait HirDatabase: DefDatabase + AstDatabase { 87pub trait HirDatabase: DefDatabase + AstDatabase {
88 #[salsa::invoke(crate::expr::expr_scopes_query)]
89 fn expr_scopes(&self, def: DefWithBody) -> Arc<ExprScopes>;
90
91 #[salsa::invoke(crate::ty::infer_query)] 88 #[salsa::invoke(crate::ty::infer_query)]
92 fn infer(&self, def: DefWithBody) -> Arc<InferenceResult>; 89 fn infer(&self, def: DefWithBody) -> Arc<InferenceResult>;
93 90
@@ -113,12 +110,6 @@ pub trait HirDatabase: DefDatabase + AstDatabase {
113 #[salsa::invoke(crate::ty::generic_defaults_query)] 110 #[salsa::invoke(crate::ty::generic_defaults_query)]
114 fn generic_defaults(&self, def: GenericDef) -> Substs; 111 fn generic_defaults(&self, def: GenericDef) -> Substs;
115 112
116 #[salsa::invoke(crate::expr::body_with_source_map_query)]
117 fn body_with_source_map(&self, def: DefWithBody) -> (Arc<Body>, Arc<BodySourceMap>);
118
119 #[salsa::invoke(crate::expr::body_query)]
120 fn body(&self, def: DefWithBody) -> Arc<Body>;
121
122 #[salsa::invoke(crate::ty::method_resolution::CrateImplBlocks::impls_in_crate_query)] 113 #[salsa::invoke(crate::ty::method_resolution::CrateImplBlocks::impls_in_crate_query)]
123 fn impls_in_crate(&self, krate: Crate) -> Arc<CrateImplBlocks>; 114 fn impls_in_crate(&self, krate: Crate) -> Arc<CrateImplBlocks>;
124 115
diff --git a/crates/ra_hir/src/expr.rs b/crates/ra_hir/src/expr.rs
index f02104b2d..9262325f2 100644
--- a/crates/ra_hir/src/expr.rs
+++ b/crates/ra_hir/src/expr.rs
@@ -4,9 +4,9 @@ pub(crate) mod validation;
4 4
5use std::sync::Arc; 5use std::sync::Arc;
6 6
7use ra_syntax::{ast, AstPtr}; 7use ra_syntax::AstPtr;
8 8
9use crate::{db::HirDatabase, DefWithBody, HasSource, Resolver}; 9use crate::{db::HirDatabase, DefWithBody, HasBody, Resolver};
10 10
11pub use hir_def::{ 11pub use hir_def::{
12 body::{ 12 body::{
@@ -19,48 +19,13 @@ pub use hir_def::{
19 }, 19 },
20}; 20};
21 21
22pub(crate) fn body_with_source_map_query(
23 db: &impl HirDatabase,
24 def: DefWithBody,
25) -> (Arc<Body>, Arc<BodySourceMap>) {
26 let mut params = None;
27
28 let (file_id, body) = match def {
29 DefWithBody::Function(f) => {
30 let src = f.source(db);
31 params = src.ast.param_list();
32 (src.file_id, src.ast.body().map(ast::Expr::from))
33 }
34 DefWithBody::Const(c) => {
35 let src = c.source(db);
36 (src.file_id, src.ast.body())
37 }
38 DefWithBody::Static(s) => {
39 let src = s.source(db);
40 (src.file_id, src.ast.body())
41 }
42 };
43 let expander = hir_def::body::Expander::new(db, file_id, def.module(db).id);
44 let (body, source_map) = Body::new(db, expander, params, body);
45 (Arc::new(body), Arc::new(source_map))
46}
47
48pub(crate) fn body_query(db: &impl HirDatabase, def: DefWithBody) -> Arc<Body> {
49 db.body_with_source_map(def).0
50}
51
52pub(crate) fn expr_scopes_query(db: &impl HirDatabase, def: DefWithBody) -> Arc<ExprScopes> {
53 let body = db.body(def);
54 Arc::new(ExprScopes::new(&*body))
55}
56
57// needs arbitrary_self_types to be a method... or maybe move to the def? 22// needs arbitrary_self_types to be a method... or maybe move to the def?
58pub(crate) fn resolver_for_expr( 23pub(crate) fn resolver_for_expr(
59 db: &impl HirDatabase, 24 db: &impl HirDatabase,
60 owner: DefWithBody, 25 owner: DefWithBody,
61 expr_id: ExprId, 26 expr_id: ExprId,
62) -> Resolver { 27) -> Resolver {
63 let scopes = db.expr_scopes(owner); 28 let scopes = owner.expr_scopes(db);
64 resolver_for_scope(db, owner, scopes.scope_for(expr_id)) 29 resolver_for_scope(db, owner, scopes.scope_for(expr_id))
65} 30}
66 31
@@ -70,7 +35,7 @@ pub(crate) fn resolver_for_scope(
70 scope_id: Option<ScopeId>, 35 scope_id: Option<ScopeId>,
71) -> Resolver { 36) -> Resolver {
72 let mut r = owner.resolver(db); 37 let mut r = owner.resolver(db);
73 let scopes = db.expr_scopes(owner); 38 let scopes = owner.expr_scopes(db);
74 let scope_chain = scopes.scope_chain(scope_id).collect::<Vec<_>>(); 39 let scope_chain = scopes.scope_chain(scope_id).collect::<Vec<_>>();
75 for scope in scope_chain.into_iter().rev() { 40 for scope in scope_chain.into_iter().rev() {
76 r = r.push_expr_scope(Arc::clone(&scopes), scope); 41 r = r.push_expr_scope(Arc::clone(&scopes), scope);
diff --git a/crates/ra_hir/src/from_id.rs b/crates/ra_hir/src/from_id.rs
index 089dbc908..9633ef586 100644
--- a/crates/ra_hir/src/from_id.rs
+++ b/crates/ra_hir/src/from_id.rs
@@ -3,9 +3,9 @@
3//! It's unclear if we need this long-term, but it's definitelly useful while we 3//! It's unclear if we need this long-term, but it's definitelly useful while we
4//! are splitting the hir. 4//! are splitting the hir.
5 5
6use hir_def::{AdtId, EnumVariantId, ModuleDefId}; 6use hir_def::{AdtId, DefWithBodyId, EnumVariantId, ModuleDefId};
7 7
8use crate::{Adt, EnumVariant, ModuleDef}; 8use crate::{Adt, DefWithBody, EnumVariant, ModuleDef};
9 9
10macro_rules! from_id { 10macro_rules! from_id {
11 ($(($id:path, $ty:path)),*) => {$( 11 ($(($id:path, $ty:path)),*) => {$(
@@ -61,3 +61,13 @@ impl From<ModuleDefId> for ModuleDef {
61 } 61 }
62 } 62 }
63} 63}
64
65impl From<DefWithBody> for DefWithBodyId {
66 fn from(def: DefWithBody) -> Self {
67 match def {
68 DefWithBody::Function(it) => DefWithBodyId::FunctionId(it.id),
69 DefWithBody::Static(it) => DefWithBodyId::StaticId(it.id),
70 DefWithBody::Const(it) => DefWithBodyId::ConstId(it.id),
71 }
72 }
73}
diff --git a/crates/ra_hir/src/from_source.rs b/crates/ra_hir/src/from_source.rs
index 4b561c63d..9793af858 100644
--- a/crates/ra_hir/src/from_source.rs
+++ b/crates/ra_hir/src/from_source.rs
@@ -10,7 +10,7 @@ use ra_syntax::{
10use crate::{ 10use crate::{
11 db::{AstDatabase, DefDatabase, HirDatabase}, 11 db::{AstDatabase, DefDatabase, HirDatabase},
12 ids::{AstItemDef, LocationCtx}, 12 ids::{AstItemDef, LocationCtx},
13 AstId, Const, Crate, DefWithBody, Enum, EnumVariant, FieldSource, Function, HasSource, 13 AstId, Const, Crate, DefWithBody, Enum, EnumVariant, FieldSource, Function, HasBody, HasSource,
14 ImplBlock, Local, Module, ModuleSource, Source, Static, Struct, StructField, Trait, TypeAlias, 14 ImplBlock, Local, Module, ModuleSource, Source, Static, Struct, StructField, Trait, TypeAlias,
15 Union, VariantDef, 15 Union, VariantDef,
16}; 16};
@@ -144,7 +144,7 @@ impl Local {
144 }; 144 };
145 Some(res) 145 Some(res)
146 })?; 146 })?;
147 let (_body, source_map) = db.body_with_source_map(parent); 147 let source_map = parent.body_source_map(db);
148 let src = src.map(ast::Pat::from); 148 let src = src.map(ast::Pat::from);
149 let pat_id = source_map.node_pat(src.as_ref())?; 149 let pat_id = source_map.node_pat(src.as_ref())?;
150 Some(Local { parent, pat_id }) 150 Some(Local { parent, pat_id })
diff --git a/crates/ra_hir/src/source_binder.rs b/crates/ra_hir/src/source_binder.rs
index e337a3d4a..ca40e3b54 100644
--- a/crates/ra_hir/src/source_binder.rs
+++ b/crates/ra_hir/src/source_binder.rs
@@ -146,7 +146,7 @@ impl SourceAnalyzer {
146 let def_with_body = def_with_body_from_child_node(db, file_id, node); 146 let def_with_body = def_with_body_from_child_node(db, file_id, node);
147 if let Some(def) = def_with_body { 147 if let Some(def) = def_with_body {
148 let source_map = def.body_source_map(db); 148 let source_map = def.body_source_map(db);
149 let scopes = db.expr_scopes(def); 149 let scopes = def.expr_scopes(db);
150 let scope = match offset { 150 let scope = match offset {
151 None => scope_for(&scopes, &source_map, file_id.into(), &node), 151 None => scope_for(&scopes, &source_map, file_id.into(), &node),
152 Some(offset) => scope_for_offset(&scopes, &source_map, file_id.into(), offset), 152 Some(offset) => scope_for_offset(&scopes, &source_map, file_id.into(), offset),
diff --git a/crates/ra_hir/src/ty/infer.rs b/crates/ra_hir/src/ty/infer.rs
index c09260864..c35378cc4 100644
--- a/crates/ra_hir/src/ty/infer.rs
+++ b/crates/ra_hir/src/ty/infer.rs
@@ -43,7 +43,8 @@ use crate::{
43 expr::{BindingAnnotation, Body, ExprId, PatId}, 43 expr::{BindingAnnotation, Body, ExprId, PatId},
44 resolve::{Resolver, TypeNs}, 44 resolve::{Resolver, TypeNs},
45 ty::infer::diagnostics::InferenceDiagnostic, 45 ty::infer::diagnostics::InferenceDiagnostic,
46 Adt, AssocItem, ConstData, DefWithBody, FloatTy, FnData, Function, IntTy, Path, StructField, 46 Adt, AssocItem, ConstData, DefWithBody, FloatTy, FnData, Function, HasBody, IntTy, Path,
47 StructField,
47}; 48};
48 49
49macro_rules! ty_app { 50macro_rules! ty_app {
@@ -214,7 +215,7 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> {
214 coerce_unsized_map: Self::init_coerce_unsized_map(db, &resolver), 215 coerce_unsized_map: Self::init_coerce_unsized_map(db, &resolver),
215 db, 216 db,
216 owner, 217 owner,
217 body: db.body(owner), 218 body: owner.body(db),
218 resolver, 219 resolver,
219 } 220 }
220 } 221 }
diff --git a/crates/ra_hir/src/ty/traits/chalk.rs b/crates/ra_hir/src/ty/traits/chalk.rs
index de322dd52..75351c17d 100644
--- a/crates/ra_hir/src/ty/traits/chalk.rs
+++ b/crates/ra_hir/src/ty/traits/chalk.rs
@@ -22,7 +22,7 @@ use crate::{
22 ApplicationTy, GenericPredicate, Namespace, ProjectionTy, Substs, TraitRef, Ty, TypeCtor, 22 ApplicationTy, GenericPredicate, Namespace, ProjectionTy, Substs, TraitRef, Ty, TypeCtor,
23 TypeWalk, 23 TypeWalk,
24 }, 24 },
25 AssocItem, Crate, ImplBlock, Trait, TypeAlias, 25 AssocItem, Crate, HasBody, ImplBlock, Trait, TypeAlias,
26}; 26};
27 27
28/// This represents a trait whose name we could not resolve. 28/// This represents a trait whose name we could not resolve.
@@ -714,7 +714,7 @@ fn closure_fn_trait_impl_datum(
714 let fn_once_trait = get_fn_trait(db, krate, super::FnTrait::FnOnce)?; 714 let fn_once_trait = get_fn_trait(db, krate, super::FnTrait::FnOnce)?;
715 let trait_ = get_fn_trait(db, krate, data.fn_trait)?; // get corresponding fn trait 715 let trait_ = get_fn_trait(db, krate, data.fn_trait)?; // get corresponding fn trait
716 716
717 let num_args: u16 = match &db.body(data.def)[data.expr] { 717 let num_args: u16 = match &data.def.body(db)[data.expr] {
718 crate::expr::Expr::Lambda { args, .. } => args.len() as u16, 718 crate::expr::Expr::Lambda { args, .. } => args.len() as u16,
719 _ => { 719 _ => {
720 log::warn!("closure for closure type {:?} not found", data); 720 log::warn!("closure for closure type {:?} not found", data);