diff options
Diffstat (limited to 'crates/ra_hir_def')
-rw-r--r-- | crates/ra_hir_def/src/body.rs | 34 | ||||
-rw-r--r-- | crates/ra_hir_def/src/body/scope.rs | 10 | ||||
-rw-r--r-- | crates/ra_hir_def/src/db.rs | 12 | ||||
-rw-r--r-- | crates/ra_hir_def/src/lib.rs | 10 |
4 files changed, 62 insertions, 4 deletions
diff --git a/crates/ra_hir_def/src/body.rs b/crates/ra_hir_def/src/body.rs index c3e9d0c23..85dc4feb0 100644 --- a/crates/ra_hir_def/src/body.rs +++ b/crates/ra_hir_def/src/body.rs | |||
@@ -17,7 +17,7 @@ use crate::{ | |||
17 | expr::{Expr, ExprId, Pat, PatId}, | 17 | expr::{Expr, ExprId, Pat, PatId}, |
18 | nameres::CrateDefMap, | 18 | nameres::CrateDefMap, |
19 | path::Path, | 19 | path::Path, |
20 | ModuleId, | 20 | AstItemDef, DefWithBodyId, ModuleId, |
21 | }; | 21 | }; |
22 | 22 | ||
23 | pub struct Expander { | 23 | pub struct Expander { |
@@ -141,7 +141,37 @@ pub struct BodySourceMap { | |||
141 | } | 141 | } |
142 | 142 | ||
143 | impl Body { | 143 | impl Body { |
144 | pub fn new( | 144 | pub(crate) fn body_with_source_map_query( |
145 | db: &impl DefDatabase2, | ||
146 | def: DefWithBodyId, | ||
147 | ) -> (Arc<Body>, Arc<BodySourceMap>) { | ||
148 | let mut params = None; | ||
149 | |||
150 | let (file_id, module, body) = match def { | ||
151 | DefWithBodyId::FunctionId(f) => { | ||
152 | let src = f.source(db); | ||
153 | params = src.ast.param_list(); | ||
154 | (src.file_id, f.module(db), src.ast.body().map(ast::Expr::from)) | ||
155 | } | ||
156 | DefWithBodyId::ConstId(c) => { | ||
157 | let src = c.source(db); | ||
158 | (src.file_id, c.module(db), src.ast.body()) | ||
159 | } | ||
160 | DefWithBodyId::StaticId(s) => { | ||
161 | let src = s.source(db); | ||
162 | (src.file_id, s.module(db), src.ast.body()) | ||
163 | } | ||
164 | }; | ||
165 | let expander = Expander::new(db, file_id, module); | ||
166 | let (body, source_map) = Body::new(db, expander, params, body); | ||
167 | (Arc::new(body), Arc::new(source_map)) | ||
168 | } | ||
169 | |||
170 | pub(crate) fn body_query(db: &impl DefDatabase2, def: DefWithBodyId) -> Arc<Body> { | ||
171 | db.body_with_source_map(def).0 | ||
172 | } | ||
173 | |||
174 | fn new( | ||
145 | db: &impl DefDatabase2, | 175 | db: &impl DefDatabase2, |
146 | expander: Expander, | 176 | expander: Expander, |
147 | params: Option<ast::ParamList>, | 177 | params: Option<ast::ParamList>, |
diff --git a/crates/ra_hir_def/src/body/scope.rs b/crates/ra_hir_def/src/body/scope.rs index dd8d06d11..09a39e721 100644 --- a/crates/ra_hir_def/src/body/scope.rs +++ b/crates/ra_hir_def/src/body/scope.rs | |||
@@ -1,4 +1,5 @@ | |||
1 | //! FIXME: write short doc here | 1 | //! FIXME: write short doc here |
2 | use std::sync::Arc; | ||
2 | 3 | ||
3 | use hir_expand::name::Name; | 4 | use hir_expand::name::Name; |
4 | use ra_arena::{impl_arena_id, Arena, RawId}; | 5 | use ra_arena::{impl_arena_id, Arena, RawId}; |
@@ -6,7 +7,9 @@ use rustc_hash::FxHashMap; | |||
6 | 7 | ||
7 | use crate::{ | 8 | use crate::{ |
8 | body::Body, | 9 | body::Body, |
10 | db::DefDatabase2, | ||
9 | expr::{Expr, ExprId, Pat, PatId, Statement}, | 11 | expr::{Expr, ExprId, Pat, PatId, Statement}, |
12 | DefWithBodyId, | ||
10 | }; | 13 | }; |
11 | 14 | ||
12 | #[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)] | 15 | #[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)] |
@@ -42,7 +45,12 @@ pub struct ScopeData { | |||
42 | } | 45 | } |
43 | 46 | ||
44 | impl ExprScopes { | 47 | impl ExprScopes { |
45 | pub fn new(body: &Body) -> ExprScopes { | 48 | pub(crate) fn expr_scopes_query(db: &impl DefDatabase2, def: DefWithBodyId) -> Arc<ExprScopes> { |
49 | let body = db.body(def); | ||
50 | Arc::new(ExprScopes::new(&*body)) | ||
51 | } | ||
52 | |||
53 | fn new(body: &Body) -> ExprScopes { | ||
46 | let mut scopes = | 54 | let mut scopes = |
47 | ExprScopes { scopes: Arena::default(), scope_by_expr: FxHashMap::default() }; | 55 | ExprScopes { scopes: Arena::default(), scope_by_expr: FxHashMap::default() }; |
48 | let root = scopes.root_scope(); | 56 | let root = scopes.root_scope(); |
diff --git a/crates/ra_hir_def/src/db.rs b/crates/ra_hir_def/src/db.rs index 29cf71a59..40b5920d9 100644 --- a/crates/ra_hir_def/src/db.rs +++ b/crates/ra_hir_def/src/db.rs | |||
@@ -7,11 +7,12 @@ use ra_syntax::ast; | |||
7 | 7 | ||
8 | use crate::{ | 8 | use crate::{ |
9 | adt::{EnumData, StructData}, | 9 | adt::{EnumData, StructData}, |
10 | body::{scope::ExprScopes, Body, BodySourceMap}, | ||
10 | nameres::{ | 11 | nameres::{ |
11 | raw::{ImportSourceMap, RawItems}, | 12 | raw::{ImportSourceMap, RawItems}, |
12 | CrateDefMap, | 13 | CrateDefMap, |
13 | }, | 14 | }, |
14 | EnumId, StructOrUnionId, | 15 | DefWithBodyId, EnumId, StructOrUnionId, |
15 | }; | 16 | }; |
16 | 17 | ||
17 | #[salsa::query_group(InternDatabaseStorage)] | 18 | #[salsa::query_group(InternDatabaseStorage)] |
@@ -52,4 +53,13 @@ pub trait DefDatabase2: InternDatabase + AstDatabase { | |||
52 | 53 | ||
53 | #[salsa::invoke(EnumData::enum_data_query)] | 54 | #[salsa::invoke(EnumData::enum_data_query)] |
54 | fn enum_data(&self, e: EnumId) -> Arc<EnumData>; | 55 | fn enum_data(&self, e: EnumId) -> Arc<EnumData>; |
56 | |||
57 | #[salsa::invoke(Body::body_with_source_map_query)] | ||
58 | fn body_with_source_map(&self, def: DefWithBodyId) -> (Arc<Body>, Arc<BodySourceMap>); | ||
59 | |||
60 | #[salsa::invoke(Body::body_query)] | ||
61 | fn body(&self, def: DefWithBodyId) -> Arc<Body>; | ||
62 | |||
63 | #[salsa::invoke(ExprScopes::expr_scopes_query)] | ||
64 | fn expr_scopes(&self, def: DefWithBodyId) -> Arc<ExprScopes>; | ||
55 | } | 65 | } |
diff --git a/crates/ra_hir_def/src/lib.rs b/crates/ra_hir_def/src/lib.rs index 4a758bb83..3fab7965c 100644 --- a/crates/ra_hir_def/src/lib.rs +++ b/crates/ra_hir_def/src/lib.rs | |||
@@ -374,3 +374,13 @@ impl_froms!( | |||
374 | TypeAliasId, | 374 | TypeAliasId, |
375 | BuiltinType | 375 | BuiltinType |
376 | ); | 376 | ); |
377 | |||
378 | /// The defs which have a body. | ||
379 | #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] | ||
380 | pub enum DefWithBodyId { | ||
381 | FunctionId(FunctionId), | ||
382 | StaticId(StaticId), | ||
383 | ConstId(ConstId), | ||
384 | } | ||
385 | |||
386 | impl_froms!(DefWithBodyId: FunctionId, ConstId, StaticId); | ||