diff options
author | Lenard Pratt <[email protected]> | 2019-03-30 10:50:00 +0000 |
---|---|---|
committer | Lenard Pratt <[email protected]> | 2019-03-30 10:50:00 +0000 |
commit | 7f3bf7cc738d02fde80d4fde9f32cbbe72896b87 (patch) | |
tree | e4a3e7121fd4dd8971f0028177b777a77cdcc6c0 /crates | |
parent | 2a770190b07ee43364a66a65b886f7efa822ec6f (diff) |
Added defWithBody
Diffstat (limited to 'crates')
-rw-r--r-- | crates/ra_hir/src/code_model_api.rs | 84 | ||||
-rw-r--r-- | crates/ra_hir/src/db.rs | 9 | ||||
-rw-r--r-- | crates/ra_hir/src/expr.rs | 40 | ||||
-rw-r--r-- | crates/ra_hir/src/expr/scope.rs | 6 | ||||
-rw-r--r-- | crates/ra_hir/src/lib.rs | 2 | ||||
-rw-r--r-- | crates/ra_syntax/src/ast/generated.rs | 1 | ||||
-rw-r--r-- | crates/ra_syntax/src/grammar.ron | 2 |
7 files changed, 120 insertions, 24 deletions
diff --git a/crates/ra_hir/src/code_model_api.rs b/crates/ra_hir/src/code_model_api.rs index 624c25c4d..db6e67d7f 100644 --- a/crates/ra_hir/src/code_model_api.rs +++ b/crates/ra_hir/src/code_model_api.rs | |||
@@ -433,6 +433,78 @@ impl Docs for EnumVariant { | |||
433 | } | 433 | } |
434 | } | 434 | } |
435 | 435 | ||
436 | /// The defs which have a body. | ||
437 | #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] | ||
438 | pub enum DefWithBody { | ||
439 | Func(Function), | ||
440 | Const(Const), | ||
441 | Static(Static), | ||
442 | } | ||
443 | |||
444 | impl DefWithBody { | ||
445 | pub fn get_funct(&self) -> &Function { | ||
446 | match *self { | ||
447 | DefWithBody::Func(ref f) => f, | ||
448 | _ => unreachable!() | ||
449 | } | ||
450 | } | ||
451 | |||
452 | pub fn const_source(&self, db: &impl DefDatabase) -> (HirFileId, TreeArc<ast::ConstDef>) { | ||
453 | match *self { | ||
454 | DefWithBody::Const(ref c) => c.source(db), | ||
455 | _ => unreachable!() | ||
456 | } | ||
457 | } | ||
458 | |||
459 | pub fn func_source(&self, db: &impl DefDatabase) -> (HirFileId, TreeArc<ast::FnDef>) { | ||
460 | match *self { | ||
461 | DefWithBody::Func(ref f) => f.source(db), | ||
462 | _ => unreachable!() | ||
463 | } | ||
464 | } | ||
465 | |||
466 | pub fn static_source(&self, db: &impl DefDatabase) -> (HirFileId, TreeArc<ast::StaticDef>) { | ||
467 | match *self { | ||
468 | DefWithBody::Static(ref s) => s.source(db), | ||
469 | _ => unreachable!() | ||
470 | } | ||
471 | } | ||
472 | |||
473 | pub fn infer(&self, db: &impl HirDatabase) -> Arc<InferenceResult> { | ||
474 | db.infer(*self) | ||
475 | } | ||
476 | |||
477 | pub fn body(&self, db: &impl HirDatabase) -> Arc<Body> { | ||
478 | db.body_hir(*self) | ||
479 | } | ||
480 | |||
481 | /// Builds a resolver for code inside this item. | ||
482 | pub fn resolver(&self, db: &impl HirDatabase) -> Resolver { | ||
483 | // // take the outer scope... | ||
484 | // let r = self | ||
485 | // .impl_block(db) | ||
486 | // .map(|ib| ib.resolver(db)) | ||
487 | // .unwrap_or_else(|| self.module(db).resolver(db)); | ||
488 | // // ...and add generic params, if present | ||
489 | // let p = self.generic_params(db); | ||
490 | // let r = if !p.params.is_empty() { r.push_generic_params_scope(p) } else { r }; | ||
491 | // r | ||
492 | unimplemented!() | ||
493 | } | ||
494 | |||
495 | pub fn signature(&self, db: &impl HirDatabase) -> Arc<FnSignature> { | ||
496 | // db.fn_signature(*self) | ||
497 | unimplemented!() | ||
498 | } | ||
499 | |||
500 | pub fn scopes(&self, db: &impl HirDatabase) -> ScopesWithSourceMap { | ||
501 | let scopes = db.expr_scopes(*self); | ||
502 | let source_map = db.body_with_source_map(*self).1; | ||
503 | ScopesWithSourceMap { scopes, source_map } | ||
504 | } | ||
505 | |||
506 | } | ||
507 | |||
436 | #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] | 508 | #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] |
437 | pub struct Function { | 509 | pub struct Function { |
438 | pub(crate) id: FunctionId, | 510 | pub(crate) id: FunctionId, |
@@ -483,11 +555,11 @@ impl Function { | |||
483 | } | 555 | } |
484 | 556 | ||
485 | pub fn body_source_map(&self, db: &impl HirDatabase) -> Arc<BodySourceMap> { | 557 | pub fn body_source_map(&self, db: &impl HirDatabase) -> Arc<BodySourceMap> { |
486 | db.body_with_source_map(*self).1 | 558 | db.body_with_source_map(DefWithBody::Func(*self)).1 |
487 | } | 559 | } |
488 | 560 | ||
489 | pub fn body(&self, db: &impl HirDatabase) -> Arc<Body> { | 561 | pub fn body(&self, db: &impl HirDatabase) -> Arc<Body> { |
490 | db.body_hir(*self) | 562 | db.body_hir(DefWithBody::Func(*self)) |
491 | } | 563 | } |
492 | 564 | ||
493 | pub fn ty(&self, db: &impl HirDatabase) -> Ty { | 565 | pub fn ty(&self, db: &impl HirDatabase) -> Ty { |
@@ -495,8 +567,8 @@ impl Function { | |||
495 | } | 567 | } |
496 | 568 | ||
497 | pub fn scopes(&self, db: &impl HirDatabase) -> ScopesWithSourceMap { | 569 | pub fn scopes(&self, db: &impl HirDatabase) -> ScopesWithSourceMap { |
498 | let scopes = db.expr_scopes(*self); | 570 | let scopes = db.expr_scopes( DefWithBody::Func(*self)); |
499 | let source_map = db.body_with_source_map(*self).1; | 571 | let source_map = db.body_with_source_map(DefWithBody::Func(*self)).1; |
500 | ScopesWithSourceMap { scopes, source_map } | 572 | ScopesWithSourceMap { scopes, source_map } |
501 | } | 573 | } |
502 | 574 | ||
@@ -505,7 +577,7 @@ impl Function { | |||
505 | } | 577 | } |
506 | 578 | ||
507 | pub fn infer(&self, db: &impl HirDatabase) -> Arc<InferenceResult> { | 579 | pub fn infer(&self, db: &impl HirDatabase) -> Arc<InferenceResult> { |
508 | db.infer(*self) | 580 | db.infer(DefWithBody::Func(*self)) |
509 | } | 581 | } |
510 | 582 | ||
511 | pub fn generic_params(&self, db: &impl DefDatabase) -> Arc<GenericParams> { | 583 | pub fn generic_params(&self, db: &impl DefDatabase) -> Arc<GenericParams> { |
@@ -716,4 +788,4 @@ impl Docs for TypeAlias { | |||
716 | fn docs(&self, db: &impl HirDatabase) -> Option<Documentation> { | 788 | fn docs(&self, db: &impl HirDatabase) -> Option<Documentation> { |
717 | docs_from_ast(&*self.source(db).1) | 789 | docs_from_ast(&*self.source(db).1) |
718 | } | 790 | } |
719 | } | 791 | } \ No newline at end of file |
diff --git a/crates/ra_hir/src/db.rs b/crates/ra_hir/src/db.rs index 147005848..a2aff241c 100644 --- a/crates/ra_hir/src/db.rs +++ b/crates/ra_hir/src/db.rs | |||
@@ -8,6 +8,7 @@ use crate::{ | |||
8 | Function, FnSignature, ExprScopes, TypeAlias, | 8 | Function, FnSignature, ExprScopes, TypeAlias, |
9 | Struct, Enum, StructField, | 9 | Struct, Enum, StructField, |
10 | Const, ConstSignature, Static, | 10 | Const, ConstSignature, Static, |
11 | DefWithBody, | ||
11 | nameres::{Namespace, ImportSourceMap, RawItems, CrateDefMap}, | 12 | nameres::{Namespace, ImportSourceMap, RawItems, CrateDefMap}, |
12 | ty::{InferenceResult, Ty, method_resolution::CrateImplBlocks, TypableDef, CallableDef, FnSig}, | 13 | ty::{InferenceResult, Ty, method_resolution::CrateImplBlocks, TypableDef, CallableDef, FnSig}, |
13 | adt::{StructData, EnumData}, | 14 | adt::{StructData, EnumData}, |
@@ -83,10 +84,10 @@ pub trait DefDatabase: SourceDatabase + AsRef<HirInterner> { | |||
83 | #[salsa::query_group(HirDatabaseStorage)] | 84 | #[salsa::query_group(HirDatabaseStorage)] |
84 | pub trait HirDatabase: DefDatabase { | 85 | pub trait HirDatabase: DefDatabase { |
85 | #[salsa::invoke(ExprScopes::expr_scopes_query)] | 86 | #[salsa::invoke(ExprScopes::expr_scopes_query)] |
86 | fn expr_scopes(&self, func: Function) -> Arc<ExprScopes>; | 87 | fn expr_scopes(&self, def: DefWithBody) -> Arc<ExprScopes>; |
87 | 88 | ||
88 | #[salsa::invoke(crate::ty::infer)] | 89 | #[salsa::invoke(crate::ty::infer)] |
89 | fn infer(&self, func: Function) -> Arc<InferenceResult>; | 90 | fn infer(&self, def:DefWithBody) -> Arc<InferenceResult>; |
90 | 91 | ||
91 | #[salsa::invoke(crate::ty::type_for_def)] | 92 | #[salsa::invoke(crate::ty::type_for_def)] |
92 | fn type_for_def(&self, def: TypableDef, ns: Namespace) -> Ty; | 93 | fn type_for_def(&self, def: TypableDef, ns: Namespace) -> Ty; |
@@ -100,11 +101,11 @@ pub trait HirDatabase: DefDatabase { | |||
100 | #[salsa::invoke(crate::expr::body_with_source_map_query)] | 101 | #[salsa::invoke(crate::expr::body_with_source_map_query)] |
101 | fn body_with_source_map( | 102 | fn body_with_source_map( |
102 | &self, | 103 | &self, |
103 | func: Function, | 104 | def: DefWithBody, |
104 | ) -> (Arc<crate::expr::Body>, Arc<crate::expr::BodySourceMap>); | 105 | ) -> (Arc<crate::expr::Body>, Arc<crate::expr::BodySourceMap>); |
105 | 106 | ||
106 | #[salsa::invoke(crate::expr::body_hir_query)] | 107 | #[salsa::invoke(crate::expr::body_hir_query)] |
107 | fn body_hir(&self, func: Function) -> Arc<crate::expr::Body>; | 108 | fn body_hir(&self, def: DefWithBody) -> Arc<crate::expr::Body>; |
108 | 109 | ||
109 | #[salsa::invoke(crate::ty::method_resolution::CrateImplBlocks::impls_in_crate_query)] | 110 | #[salsa::invoke(crate::ty::method_resolution::CrateImplBlocks::impls_in_crate_query)] |
110 | fn impls_in_crate(&self, krate: Crate) -> Arc<CrateImplBlocks>; | 111 | fn impls_in_crate(&self, krate: Crate) -> Arc<CrateImplBlocks>; |
diff --git a/crates/ra_hir/src/expr.rs b/crates/ra_hir/src/expr.rs index a85422955..280746761 100644 --- a/crates/ra_hir/src/expr.rs +++ b/crates/ra_hir/src/expr.rs | |||
@@ -10,7 +10,7 @@ use ra_syntax::{ | |||
10 | }; | 10 | }; |
11 | 11 | ||
12 | use crate::{ | 12 | use crate::{ |
13 | Path, Name, HirDatabase, Function, Resolver, | 13 | Path, Name, HirDatabase, Function, Resolver,DefWithBody, |
14 | name::AsName, | 14 | name::AsName, |
15 | type_ref::{Mutability, TypeRef}, | 15 | type_ref::{Mutability, TypeRef}, |
16 | }; | 16 | }; |
@@ -29,7 +29,7 @@ impl_arena_id!(ExprId); | |||
29 | pub struct Body { | 29 | pub struct Body { |
30 | // FIXME: this should be more general, consts & statics also have bodies | 30 | // FIXME: this should be more general, consts & statics also have bodies |
31 | /// The Function of the item this body belongs to | 31 | /// The Function of the item this body belongs to |
32 | owner: Function, | 32 | owner: DefWithBody, |
33 | exprs: Arena<ExprId, Expr>, | 33 | exprs: Arena<ExprId, Expr>, |
34 | pats: Arena<PatId, Pat>, | 34 | pats: Arena<PatId, Pat>, |
35 | /// The patterns for the function's parameters. While the parameter types are | 35 | /// The patterns for the function's parameters. While the parameter types are |
@@ -66,7 +66,7 @@ impl Body { | |||
66 | self.body_expr | 66 | self.body_expr |
67 | } | 67 | } |
68 | 68 | ||
69 | pub fn owner(&self) -> Function { | 69 | pub fn owner(&self) -> DefWithBody { |
70 | self.owner | 70 | self.owner |
71 | } | 71 | } |
72 | 72 | ||
@@ -464,7 +464,7 @@ impl Pat { | |||
464 | // Queries | 464 | // Queries |
465 | 465 | ||
466 | struct ExprCollector { | 466 | struct ExprCollector { |
467 | owner: Function, | 467 | owner: DefWithBody, |
468 | exprs: Arena<ExprId, Expr>, | 468 | exprs: Arena<ExprId, Expr>, |
469 | pats: Arena<PatId, Pat>, | 469 | pats: Arena<PatId, Pat>, |
470 | source_map: BodySourceMap, | 470 | source_map: BodySourceMap, |
@@ -473,7 +473,7 @@ struct ExprCollector { | |||
473 | } | 473 | } |
474 | 474 | ||
475 | impl ExprCollector { | 475 | impl ExprCollector { |
476 | fn new(owner: Function) -> Self { | 476 | fn new(owner: DefWithBody) -> Self { |
477 | ExprCollector { | 477 | ExprCollector { |
478 | owner, | 478 | owner, |
479 | exprs: Arena::default(), | 479 | exprs: Arena::default(), |
@@ -503,6 +503,9 @@ impl ExprCollector { | |||
503 | self.exprs.alloc(block) | 503 | self.exprs.alloc(block) |
504 | } | 504 | } |
505 | 505 | ||
506 | |||
507 | |||
508 | |||
506 | fn collect_expr(&mut self, expr: &ast::Expr) -> ExprId { | 509 | fn collect_expr(&mut self, expr: &ast::Expr) -> ExprId { |
507 | let syntax_ptr = SyntaxNodePtr::new(expr.syntax()); | 510 | let syntax_ptr = SyntaxNodePtr::new(expr.syntax()); |
508 | match expr.kind() { | 511 | match expr.kind() { |
@@ -871,6 +874,15 @@ impl ExprCollector { | |||
871 | } | 874 | } |
872 | } | 875 | } |
873 | 876 | ||
877 | |||
878 | fn collect_const_body(&mut self,node:&ast::ConstDef) { | ||
879 | |||
880 | } | ||
881 | |||
882 | fn collect_static_body(&mut self,node:&ast::StaticDef) { | ||
883 | |||
884 | } | ||
885 | |||
874 | fn collect_fn_body(&mut self, node: &ast::FnDef) { | 886 | fn collect_fn_body(&mut self, node: &ast::FnDef) { |
875 | if let Some(param_list) = node.param_list() { | 887 | if let Some(param_list) = node.param_list() { |
876 | if let Some(self_param) = param_list.self_param() { | 888 | if let Some(self_param) = param_list.self_param() { |
@@ -917,19 +929,25 @@ impl ExprCollector { | |||
917 | 929 | ||
918 | pub(crate) fn body_with_source_map_query( | 930 | pub(crate) fn body_with_source_map_query( |
919 | db: &impl HirDatabase, | 931 | db: &impl HirDatabase, |
920 | func: Function, | 932 | def: DefWithBody, |
921 | ) -> (Arc<Body>, Arc<BodySourceMap>) { | 933 | ) -> (Arc<Body>, Arc<BodySourceMap>) { |
922 | let mut collector = ExprCollector::new(func); | ||
923 | 934 | ||
924 | // FIXME: consts, etc. | 935 | let mut collector = ExprCollector::new(def); |
925 | collector.collect_fn_body(&func.source(db).1); | ||
926 | 936 | ||
937 | // FIXME: do can this be turned into a method | ||
938 | |||
939 | match def { | ||
940 | DefWithBody::Const(ref c) => collector.collect_const_body(&def.const_source(db).1), | ||
941 | DefWithBody::Func(ref f) => collector.collect_fn_body(&def.func_source(db).1), | ||
942 | DefWithBody::Static(ref s) => collector.collect_static_body(&def.static_source(db).1) | ||
943 | } | ||
944 | |||
927 | let (body, source_map) = collector.finish(); | 945 | let (body, source_map) = collector.finish(); |
928 | (Arc::new(body), Arc::new(source_map)) | 946 | (Arc::new(body), Arc::new(source_map)) |
929 | } | 947 | } |
930 | 948 | ||
931 | pub(crate) fn body_hir_query(db: &impl HirDatabase, func: Function) -> Arc<Body> { | 949 | pub(crate) fn body_hir_query(db: &impl HirDatabase, def: DefWithBody) -> Arc<Body> { |
932 | db.body_with_source_map(func).0 | 950 | db.body_with_source_map(def).0 |
933 | } | 951 | } |
934 | 952 | ||
935 | #[cfg(test)] | 953 | #[cfg(test)] |
diff --git a/crates/ra_hir/src/expr/scope.rs b/crates/ra_hir/src/expr/scope.rs index ed005c9f7..539da06c3 100644 --- a/crates/ra_hir/src/expr/scope.rs +++ b/crates/ra_hir/src/expr/scope.rs | |||
@@ -10,7 +10,7 @@ use ra_syntax::{ | |||
10 | use ra_arena::{Arena, RawId, impl_arena_id}; | 10 | use ra_arena::{Arena, RawId, impl_arena_id}; |
11 | 11 | ||
12 | use crate::{ | 12 | use crate::{ |
13 | Name, AsName, Function, | 13 | Name, AsName, Function,DefWithBody, |
14 | expr::{PatId, ExprId, Pat, Expr, Body, Statement, BodySourceMap}, | 14 | expr::{PatId, ExprId, Pat, Expr, Body, Statement, BodySourceMap}, |
15 | HirDatabase, | 15 | HirDatabase, |
16 | }; | 16 | }; |
@@ -40,8 +40,8 @@ pub struct ScopeData { | |||
40 | 40 | ||
41 | impl ExprScopes { | 41 | impl ExprScopes { |
42 | // FIXME: This should take something more general than Function | 42 | // FIXME: This should take something more general than Function |
43 | pub(crate) fn expr_scopes_query(db: &impl HirDatabase, function: Function) -> Arc<ExprScopes> { | 43 | pub(crate) fn expr_scopes_query(db: &impl HirDatabase, def: DefWithBody) -> Arc<ExprScopes> { |
44 | let body = db.body_hir(function); | 44 | let body = db.body_hir(def); |
45 | let res = ExprScopes::new(body); | 45 | let res = ExprScopes::new(body); |
46 | Arc::new(res) | 46 | Arc::new(res) |
47 | } | 47 | } |
diff --git a/crates/ra_hir/src/lib.rs b/crates/ra_hir/src/lib.rs index 7c603bbd3..62cec72d9 100644 --- a/crates/ra_hir/src/lib.rs +++ b/crates/ra_hir/src/lib.rs | |||
@@ -67,10 +67,12 @@ pub use self::{ | |||
67 | 67 | ||
68 | pub use self::code_model_api::{ | 68 | pub use self::code_model_api::{ |
69 | Crate, CrateDependency, | 69 | Crate, CrateDependency, |
70 | DefWithBody, | ||
70 | Module, ModuleDef, ModuleSource, | 71 | Module, ModuleDef, ModuleSource, |
71 | Struct, Enum, EnumVariant, | 72 | Struct, Enum, EnumVariant, |
72 | Function, FnSignature, | 73 | Function, FnSignature, |
73 | StructField, FieldSource, | 74 | StructField, FieldSource, |
74 | Static, Const, ConstSignature, | 75 | Static, Const, ConstSignature, |
75 | Trait, TypeAlias, | 76 | Trait, TypeAlias, |
77 | |||
76 | }; | 78 | }; |
diff --git a/crates/ra_syntax/src/ast/generated.rs b/crates/ra_syntax/src/ast/generated.rs index 47a37e4d1..31b4e73fb 100644 --- a/crates/ra_syntax/src/ast/generated.rs +++ b/crates/ra_syntax/src/ast/generated.rs | |||
@@ -657,6 +657,7 @@ impl ToOwned for ContinueExpr { | |||
657 | } | 657 | } |
658 | 658 | ||
659 | 659 | ||
660 | |||
660 | impl ContinueExpr {} | 661 | impl ContinueExpr {} |
661 | 662 | ||
662 | // DynTraitType | 663 | // DynTraitType |
diff --git a/crates/ra_syntax/src/grammar.ron b/crates/ra_syntax/src/grammar.ron index ad6d74162..19e50c9b8 100644 --- a/crates/ra_syntax/src/grammar.ron +++ b/crates/ra_syntax/src/grammar.ron | |||
@@ -313,6 +313,7 @@ Grammar( | |||
313 | "DocCommentsOwner", | 313 | "DocCommentsOwner", |
314 | "TypeAscriptionOwner", | 314 | "TypeAscriptionOwner", |
315 | ], | 315 | ], |
316 | options: ["body","Block"], | ||
316 | ), | 317 | ), |
317 | "StaticDef": ( | 318 | "StaticDef": ( |
318 | traits: [ | 319 | traits: [ |
@@ -323,6 +324,7 @@ Grammar( | |||
323 | "DocCommentsOwner", | 324 | "DocCommentsOwner", |
324 | "TypeAscriptionOwner", | 325 | "TypeAscriptionOwner", |
325 | ], | 326 | ], |
327 | options: ["body","Block"], | ||
326 | ), | 328 | ), |
327 | "TypeAliasDef": ( | 329 | "TypeAliasDef": ( |
328 | traits: [ | 330 | traits: [ |