diff options
Diffstat (limited to 'crates/ra_hir/src/code_model_api.rs')
-rw-r--r-- | crates/ra_hir/src/code_model_api.rs | 87 |
1 files changed, 35 insertions, 52 deletions
diff --git a/crates/ra_hir/src/code_model_api.rs b/crates/ra_hir/src/code_model_api.rs index db6e67d7f..c3e5e26c3 100644 --- a/crates/ra_hir/src/code_model_api.rs +++ b/crates/ra_hir/src/code_model_api.rs | |||
@@ -436,65 +436,33 @@ impl Docs for EnumVariant { | |||
436 | /// The defs which have a body. | 436 | /// The defs which have a body. |
437 | #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] | 437 | #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] |
438 | pub enum DefWithBody { | 438 | pub enum DefWithBody { |
439 | Func(Function), | 439 | Function(Function), |
440 | Const(Const), | 440 | Const(Const), |
441 | Static(Static), | 441 | Static(Static), |
442 | } | 442 | } |
443 | 443 | ||
444 | impl DefWithBody { | 444 | impl_froms!(DefWithBody: Function, Const, Static); |
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 | 445 | ||
446 | impl DefWithBody { | ||
473 | pub fn infer(&self, db: &impl HirDatabase) -> Arc<InferenceResult> { | 447 | pub fn infer(&self, db: &impl HirDatabase) -> Arc<InferenceResult> { |
474 | db.infer(*self) | 448 | db.infer(*self) |
475 | } | 449 | } |
476 | 450 | ||
451 | pub fn body_source_map(&self, db: &impl HirDatabase) -> Arc<BodySourceMap> { | ||
452 | db.body_with_source_map(*self).1 | ||
453 | } | ||
454 | |||
477 | pub fn body(&self, db: &impl HirDatabase) -> Arc<Body> { | 455 | pub fn body(&self, db: &impl HirDatabase) -> Arc<Body> { |
478 | db.body_hir(*self) | 456 | db.body_hir(*self) |
479 | } | 457 | } |
480 | 458 | ||
481 | /// Builds a resolver for code inside this item. | 459 | /// Builds a resolver for code inside this item. |
482 | pub fn resolver(&self, db: &impl HirDatabase) -> Resolver { | 460 | pub fn resolver(&self, db: &impl HirDatabase) -> Resolver { |
483 | // // take the outer scope... | 461 | match *self { |
484 | // let r = self | 462 | DefWithBody::Const(ref c) => c.resolver(db), |
485 | // .impl_block(db) | 463 | DefWithBody::Function(ref f) => f.resolver(db), |
486 | // .map(|ib| ib.resolver(db)) | 464 | DefWithBody::Static(ref s) => s.resolver(db), |
487 | // .unwrap_or_else(|| self.module(db).resolver(db)); | 465 | } |
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 | } | 466 | } |
499 | 467 | ||
500 | pub fn scopes(&self, db: &impl HirDatabase) -> ScopesWithSourceMap { | 468 | pub fn scopes(&self, db: &impl HirDatabase) -> ScopesWithSourceMap { |
@@ -502,7 +470,6 @@ impl DefWithBody { | |||
502 | let source_map = db.body_with_source_map(*self).1; | 470 | let source_map = db.body_with_source_map(*self).1; |
503 | ScopesWithSourceMap { scopes, source_map } | 471 | ScopesWithSourceMap { scopes, source_map } |
504 | } | 472 | } |
505 | |||
506 | } | 473 | } |
507 | 474 | ||
508 | #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] | 475 | #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] |
@@ -555,11 +522,11 @@ impl Function { | |||
555 | } | 522 | } |
556 | 523 | ||
557 | pub fn body_source_map(&self, db: &impl HirDatabase) -> Arc<BodySourceMap> { | 524 | pub fn body_source_map(&self, db: &impl HirDatabase) -> Arc<BodySourceMap> { |
558 | db.body_with_source_map(DefWithBody::Func(*self)).1 | 525 | db.body_with_source_map((*self).into()).1 |
559 | } | 526 | } |
560 | 527 | ||
561 | pub fn body(&self, db: &impl HirDatabase) -> Arc<Body> { | 528 | pub fn body(&self, db: &impl HirDatabase) -> Arc<Body> { |
562 | db.body_hir(DefWithBody::Func(*self)) | 529 | db.body_hir((*self).into()) |
563 | } | 530 | } |
564 | 531 | ||
565 | pub fn ty(&self, db: &impl HirDatabase) -> Ty { | 532 | pub fn ty(&self, db: &impl HirDatabase) -> Ty { |
@@ -567,8 +534,8 @@ impl Function { | |||
567 | } | 534 | } |
568 | 535 | ||
569 | pub fn scopes(&self, db: &impl HirDatabase) -> ScopesWithSourceMap { | 536 | pub fn scopes(&self, db: &impl HirDatabase) -> ScopesWithSourceMap { |
570 | let scopes = db.expr_scopes( DefWithBody::Func(*self)); | 537 | let scopes = db.expr_scopes((*self).into()); |
571 | let source_map = db.body_with_source_map(DefWithBody::Func(*self)).1; | 538 | let source_map = db.body_with_source_map((*self).into()).1; |
572 | ScopesWithSourceMap { scopes, source_map } | 539 | ScopesWithSourceMap { scopes, source_map } |
573 | } | 540 | } |
574 | 541 | ||
@@ -577,7 +544,7 @@ impl Function { | |||
577 | } | 544 | } |
578 | 545 | ||
579 | pub fn infer(&self, db: &impl HirDatabase) -> Arc<InferenceResult> { | 546 | pub fn infer(&self, db: &impl HirDatabase) -> Arc<InferenceResult> { |
580 | db.infer(DefWithBody::Func(*self)) | 547 | db.infer((*self).into()) |
581 | } | 548 | } |
582 | 549 | ||
583 | pub fn generic_params(&self, db: &impl DefDatabase) -> Arc<GenericParams> { | 550 | pub fn generic_params(&self, db: &impl DefDatabase) -> Arc<GenericParams> { |
@@ -633,6 +600,14 @@ impl Const { | |||
633 | db.const_signature(*self) | 600 | db.const_signature(*self) |
634 | } | 601 | } |
635 | 602 | ||
603 | pub fn infer(&self, db: &impl HirDatabase) -> Arc<InferenceResult> { | ||
604 | db.infer((*self).into()) | ||
605 | } | ||
606 | |||
607 | pub fn body_source_map(&self, db: &impl HirDatabase) -> Arc<BodySourceMap> { | ||
608 | db.body_with_source_map((*self).into()).1 | ||
609 | } | ||
610 | |||
636 | /// The containing impl block, if this is a method. | 611 | /// The containing impl block, if this is a method. |
637 | pub fn impl_block(&self, db: &impl DefDatabase) -> Option<ImplBlock> { | 612 | pub fn impl_block(&self, db: &impl DefDatabase) -> Option<ImplBlock> { |
638 | let module_impls = db.impls_in_module(self.module(db)); | 613 | let module_impls = db.impls_in_module(self.module(db)); |
@@ -697,6 +672,14 @@ impl Static { | |||
697 | // take the outer scope... | 672 | // take the outer scope... |
698 | self.module(db).resolver(db) | 673 | self.module(db).resolver(db) |
699 | } | 674 | } |
675 | |||
676 | pub fn infer(&self, db: &impl HirDatabase) -> Arc<InferenceResult> { | ||
677 | db.infer((*self).into()) | ||
678 | } | ||
679 | |||
680 | pub fn body_source_map(&self, db: &impl HirDatabase) -> Arc<BodySourceMap> { | ||
681 | db.body_with_source_map((*self).into()).1 | ||
682 | } | ||
700 | } | 683 | } |
701 | 684 | ||
702 | impl Docs for Static { | 685 | impl Docs for Static { |
@@ -788,4 +771,4 @@ impl Docs for TypeAlias { | |||
788 | fn docs(&self, db: &impl HirDatabase) -> Option<Documentation> { | 771 | fn docs(&self, db: &impl HirDatabase) -> Option<Documentation> { |
789 | docs_from_ast(&*self.source(db).1) | 772 | docs_from_ast(&*self.source(db).1) |
790 | } | 773 | } |
791 | } \ No newline at end of file | 774 | } |