From ae9530addc4c5e9bbfd5c0287d3c3adb2de95e40 Mon Sep 17 00:00:00 2001 From: Florian Diebold Date: Fri, 28 Dec 2018 14:34:00 +0100 Subject: Add HIR for impl blocks Since we need to be able to go from def to containing impl block, as well as the other direction, and to find all impls for a certain type, a design similar to the one for modules, where we collect all impls for the whole crate and keep them in an arena, seemed fitting. The ImplBlock type, which provides the public interface, then consists only of an Arc to the arena containing all impls, and the index into it. --- crates/ra_hir/src/function.rs | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) (limited to 'crates/ra_hir/src/function.rs') diff --git a/crates/ra_hir/src/function.rs b/crates/ra_hir/src/function.rs index 5a44132fc..75ef308ae 100644 --- a/crates/ra_hir/src/function.rs +++ b/crates/ra_hir/src/function.rs @@ -11,11 +11,11 @@ use ra_syntax::{ ast::{self, AstNode, DocCommentsOwner, NameOwner}, }; -use crate::{DefId, DefKind, HirDatabase, ty::InferenceResult, Module}; +use crate::{DefId, DefKind, HirDatabase, ty::InferenceResult, Module, Crate, impl_block::ImplBlock}; pub use self::scope::FnScopes; -#[derive(Debug)] +#[derive(Debug, Clone, PartialEq, Eq)] pub struct Function { def_id: DefId, } @@ -25,6 +25,10 @@ impl Function { Function { def_id } } + pub fn def_id(&self) -> DefId { + self.def_id + } + pub fn syntax(&self, db: &impl HirDatabase) -> ast::FnDefNode { let def_loc = self.def_id.loc(db); assert!(def_loc.kind == DefKind::Function); @@ -48,6 +52,15 @@ impl Function { pub fn module(&self, db: &impl HirDatabase) -> Cancelable { self.def_id.module(db) } + + pub fn krate(&self, db: &impl HirDatabase) -> Cancelable> { + self.def_id.krate(db) + } + + /// The containing impl block, if this is a method. + pub fn impl_block(&self, db: &impl HirDatabase) -> Cancelable> { + self.def_id.impl_block(db) + } } #[derive(Debug, Clone)] -- cgit v1.2.3