From b9b4693ce3bf0229ea40f09e6404fad3e7823321 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Wed, 19 Aug 2020 15:16:24 +0200 Subject: Add SelfParam to code_model --- crates/hir/src/code_model.rs | 53 +++++++++++++++++++++++++++++++------------- 1 file changed, 38 insertions(+), 15 deletions(-) (limited to 'crates/hir/src/code_model.rs') diff --git a/crates/hir/src/code_model.rs b/crates/hir/src/code_model.rs index 31d5276b0..3254f316b 100644 --- a/crates/hir/src/code_model.rs +++ b/crates/hir/src/code_model.rs @@ -666,23 +666,11 @@ impl Function { db.function_data(self.id).name.clone() } - pub fn has_self_param(self, db: &dyn HirDatabase) -> bool { - db.function_data(self.id).has_self_param - } - - pub fn mutability_of_self_param(self, db: &dyn HirDatabase) -> Option { - let func_data = db.function_data(self.id); - if !func_data.has_self_param { + pub fn self_param(self, db: &dyn HirDatabase) -> Option { + if !db.function_data(self.id).has_self_param { return None; } - - func_data.params.first().and_then(|param| { - if let TypeRef::Reference(_, mutability) = param { - Some(*mutability) - } else { - None - } - }) + Some(SelfParam { func: self.id }) } pub fn params(self, db: &dyn HirDatabase) -> Vec { @@ -698,6 +686,41 @@ impl Function { } } +// Note: logically, this belongs to `hir_ty`, but we are not using it there yet. +pub enum Access { + Shared, + Exclusive, + Owned, +} + +impl From for Access { + fn from(mutability: Mutability) -> Access { + match mutability { + Mutability::Shared => Access::Shared, + Mutability::Mut => Access::Exclusive, + } + } +} + +#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] +pub struct SelfParam { + func: FunctionId, +} + +impl SelfParam { + pub fn access(self, db: &dyn HirDatabase) -> Access { + let func_data = db.function_data(self.func); + func_data + .params + .first() + .map(|param| match *param { + TypeRef::Reference(_, mutability) => mutability.into(), + _ => Access::Owned, + }) + .unwrap_or(Access::Owned) + } +} + impl HasVisibility for Function { fn visibility(&self, db: &dyn HirDatabase) -> Visibility { let function_data = db.function_data(self.id); -- cgit v1.2.3