From f588535273db261c32e23c1b0221d03ad82cd94d Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Fri, 25 Jan 2019 00:26:54 +0300 Subject: remove boilerplate --- crates/ra_hir/src/adt.rs | 54 +++++-------------- crates/ra_hir/src/code_model_impl/function.rs | 14 +---- crates/ra_hir/src/ids.rs | 78 ++++++++++++++++----------- crates/ra_hir/src/impl_block.rs | 4 +- crates/ra_hir/src/nameres/lower.rs | 8 +-- crates/ra_hir/src/source_binder.rs | 6 ++- 6 files changed, 72 insertions(+), 92 deletions(-) (limited to 'crates/ra_hir/src') diff --git a/crates/ra_hir/src/adt.rs b/crates/ra_hir/src/adt.rs index b2631d6a6..ad3f9c405 100644 --- a/crates/ra_hir/src/adt.rs +++ b/crates/ra_hir/src/adt.rs @@ -8,42 +8,18 @@ use ra_syntax::{ }; use crate::{ - Name, AsName, Struct, Enum, EnumVariant, Module, HirFileId, + Name, AsName, Struct, Enum, EnumVariant, HirDatabase, type_ref::TypeRef, - ids::ItemLoc, + ids::LocationCtx, }; impl Struct { - pub(crate) fn from_ast( - db: &impl HirDatabase, - module: Module, - file_id: HirFileId, - ast: &ast::StructDef, - ) -> Struct { - let loc = ItemLoc::from_ast(db, module, file_id, ast); - let id = db.as_ref().structs.loc2id(&loc); - Struct { id } - } - pub(crate) fn variant_data(&self, db: &impl HirDatabase) -> Arc { db.struct_data((*self).into()).variant_data.clone() } } -impl Enum { - pub(crate) fn from_ast( - db: &impl HirDatabase, - module: Module, - file_id: HirFileId, - ast: &ast::EnumDef, - ) -> Enum { - let loc = ItemLoc::from_ast(db, module, file_id, ast); - let id = db.as_ref().enums.loc2id(&loc); - Enum { id } - } -} - #[derive(Debug, Clone, PartialEq, Eq)] pub struct StructData { pub(crate) name: Option, @@ -64,19 +40,6 @@ impl StructData { } } -impl EnumVariant { - pub(crate) fn from_ast( - db: &impl HirDatabase, - module: Module, - file_id: HirFileId, - ast: &ast::EnumVariant, - ) -> EnumVariant { - let loc = ItemLoc::from_ast(db, module, file_id, ast); - let id = db.as_ref().enum_variants.loc2id(&loc); - EnumVariant { id } - } -} - #[derive(Debug, Clone, PartialEq, Eq)] pub struct EnumData { pub(crate) name: Option, @@ -92,11 +55,15 @@ impl EnumData { pub(crate) fn enum_data_query(db: &impl HirDatabase, e: Enum) -> Arc { let (file_id, enum_def) = e.source(db); let module = e.module(db); + let ctx = LocationCtx::new(db, module, file_id); let variants = if let Some(vl) = enum_def.variant_list() { vl.variants() .filter_map(|variant_def| { - let name = variant_def.name().map(|n| n.as_name()); - name.map(|n| (n, EnumVariant::from_ast(db, module, file_id, variant_def))) + let name = variant_def.name()?.as_name(); + let var = EnumVariant { + id: ctx.to_def(variant_def), + }; + Some((name, var)) }) .collect() } else { @@ -131,7 +98,10 @@ impl EnumVariantData { ) -> Arc { let (file_id, variant_def) = var.source(db); let enum_def = variant_def.parent_enum(); - let e = Enum::from_ast(db, var.module(db), file_id, enum_def); + let ctx = LocationCtx::new(db, var.module(db), file_id); + let e = Enum { + id: ctx.to_def(enum_def), + }; Arc::new(EnumVariantData::new(&*variant_def, e)) } } diff --git a/crates/ra_hir/src/code_model_impl/function.rs b/crates/ra_hir/src/code_model_impl/function.rs index 6ce5c77b1..e0dd4d629 100644 --- a/crates/ra_hir/src/code_model_impl/function.rs +++ b/crates/ra_hir/src/code_model_impl/function.rs @@ -5,27 +5,15 @@ use std::sync::Arc; use ra_syntax::ast::{self, NameOwner}; use crate::{ - HirDatabase, Name, AsName, Function, FnSignature, Module, HirFileId, + HirDatabase, Name, AsName, Function, FnSignature, type_ref::{TypeRef, Mutability}, expr::Body, impl_block::ImplBlock, - ids::ItemLoc, }; pub use self::scope::{FnScopes, ScopesWithSyntaxMapping, ScopeEntryWithSyntax}; impl Function { - pub(crate) fn from_ast( - db: &impl HirDatabase, - module: Module, - file_id: HirFileId, - ast: &ast::FnDef, - ) -> Function { - let loc = ItemLoc::from_ast(db, module, file_id, ast); - let id = db.as_ref().fns.loc2id(&loc); - Function { id } - } - pub(crate) fn body(&self, db: &impl HirDatabase) -> Arc { db.body_hir(*self) } diff --git a/crates/ra_hir/src/ids.rs b/crates/ra_hir/src/ids.rs index 262047849..4b4e07e58 100644 --- a/crates/ra_hir/src/ids.rs +++ b/crates/ra_hir/src/ids.rs @@ -16,10 +16,10 @@ use crate::{ pub struct HirInterner { defs: LocationIntener, macros: LocationIntener, - pub(crate) fns: LocationIntener, FunctionId>, - pub(crate) structs: LocationIntener, StructId>, - pub(crate) enums: LocationIntener, EnumId>, - pub(crate) enum_variants: LocationIntener, EnumVariantId>, + fns: LocationIntener, FunctionId>, + structs: LocationIntener, StructId>, + enums: LocationIntener, EnumId>, + enum_variants: LocationIntener, EnumVariantId>, } impl HirInterner { @@ -144,50 +144,64 @@ pub struct ItemLoc { _ty: PhantomData, } -impl ItemLoc { - pub(crate) fn from_ast( - db: &impl HirDatabase, - module: Module, - file_id: HirFileId, - ast: &N, - ) -> ItemLoc { - let items = db.file_items(file_id); - let raw = SourceItemId { - file_id, - item_id: Some(items.id_of(file_id, ast.syntax())), - }; +impl Clone for ItemLoc { + fn clone(&self) -> ItemLoc { ItemLoc { - module, - raw, + module: self.module, + raw: self.raw, _ty: PhantomData, } } +} - pub(crate) fn source(&self, db: &impl HirDatabase) -> (HirFileId, TreeArc) { - let syntax = db.file_item(self.raw); - let ast = N::cast(&syntax) - .unwrap_or_else(|| panic!("invalid ItemLoc: {:?}", self.raw)) - .to_owned(); - (self.raw.file_id, ast) - } +#[derive(Clone, Copy)] +pub(crate) struct LocationCtx { + db: DB, + module: Module, + file_id: HirFileId, } -impl Clone for ItemLoc { - fn clone(&self) -> ItemLoc { - ItemLoc { - module: self.module, - raw: self.raw, - _ty: PhantomData, +impl<'a, DB: HirDatabase> LocationCtx<&'a DB> { + pub(crate) fn new(db: &'a DB, module: Module, file_id: HirFileId) -> LocationCtx<&'a DB> { + LocationCtx { + db, + module, + file_id, } } + pub(crate) fn to_def(self, ast: &N) -> DEF + where + N: AstNode + Eq + Hash, + DEF: AstItemDef, + { + DEF::from_ast(self, ast) + } } pub(crate) trait AstItemDef: ArenaId + Clone { fn interner(interner: &HirInterner) -> &LocationIntener, Self>; + fn from_ast(ctx: LocationCtx<&impl HirDatabase>, ast: &N) -> Self { + let items = ctx.db.file_items(ctx.file_id); + let raw = SourceItemId { + file_id: ctx.file_id, + item_id: Some(items.id_of(ctx.file_id, ast.syntax())), + }; + let loc = ItemLoc { + module: ctx.module, + raw, + _ty: PhantomData, + }; + + Self::interner(ctx.db.as_ref()).loc2id(&loc) + } fn source(self, db: &impl HirDatabase) -> (HirFileId, TreeArc) { let int = Self::interner(db.as_ref()); let loc = int.id2loc(self); - loc.source(db) + let syntax = db.file_item(loc.raw); + let ast = N::cast(&syntax) + .unwrap_or_else(|| panic!("invalid ItemLoc: {:?}", loc.raw)) + .to_owned(); + (loc.raw.file_id, ast) } fn module(self, db: &impl HirDatabase) -> Module { let int = Self::interner(db.as_ref()); diff --git a/crates/ra_hir/src/impl_block.rs b/crates/ra_hir/src/impl_block.rs index 274169f93..3df0d3a3b 100644 --- a/crates/ra_hir/src/impl_block.rs +++ b/crates/ra_hir/src/impl_block.rs @@ -9,6 +9,7 @@ use crate::{ Function, HirFileId, db::HirDatabase, type_ref::TypeRef, + ids::LocationCtx, }; use crate::code_model_api::{Module, ModuleSource}; @@ -72,13 +73,14 @@ impl ImplData { ) -> Self { let target_trait = node.target_trait().map(TypeRef::from_ast); let target_type = TypeRef::from_ast_opt(node.target_type()); + let ctx = LocationCtx::new(db, module, file_id); let items = if let Some(item_list) = node.item_list() { item_list .impl_items() .map(|item_node| { let kind = match item_node.kind() { ast::ImplItemKind::FnDef(it) => { - return ImplItem::Method(Function::from_ast(db, module, file_id, it)); + return ImplItem::Method(Function { id: ctx.to_def(it) }); } ast::ImplItemKind::ConstDef(..) => DefKind::Item, ast::ImplItemKind::TypeDef(..) => DefKind::Item, diff --git a/crates/ra_hir/src/nameres/lower.rs b/crates/ra_hir/src/nameres/lower.rs index 435804e2d..0056bdd5a 100644 --- a/crates/ra_hir/src/nameres/lower.rs +++ b/crates/ra_hir/src/nameres/lower.rs @@ -11,6 +11,7 @@ use crate::{ SourceItemId, Path, ModuleSource, HirDatabase, Name, SourceFileItems, HirFileId, MacroCallLoc, AsName, PerNs, DefKind, DefLoc, Function, ModuleDef, Module, Struct, Enum, + ids::LocationCtx, }; #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] @@ -146,10 +147,11 @@ impl LoweredModule { file_items: &SourceFileItems, item: &ast::ModuleItem, ) { + let ctx = LocationCtx::new(db, module, file_id); let name = match item.kind() { ast::ModuleItemKind::StructDef(it) => { if let Some(name) = it.name() { - let s = Struct::from_ast(db, module, file_id, it); + let s = Struct { id: ctx.to_def(it) }; let s: ModuleDef = s.into(); self.declarations.insert(name.as_name(), PerNs::both(s, s)); } @@ -157,7 +159,7 @@ impl LoweredModule { } ast::ModuleItemKind::EnumDef(it) => { if let Some(name) = it.name() { - let e = Enum::from_ast(db, module, file_id, it); + let e = Enum { id: ctx.to_def(it) }; let e: ModuleDef = e.into(); self.declarations.insert(name.as_name(), PerNs::types(e)); } @@ -165,7 +167,7 @@ impl LoweredModule { } ast::ModuleItemKind::FnDef(it) => { if let Some(name) = it.name() { - let func = Function::from_ast(db, module, file_id, it); + let func = Function { id: ctx.to_def(it) }; self.declarations .insert(name.as_name(), PerNs::values(func.into())); } diff --git a/crates/ra_hir/src/source_binder.rs b/crates/ra_hir/src/source_binder.rs index d1bf163d1..ea8185853 100644 --- a/crates/ra_hir/src/source_binder.rs +++ b/crates/ra_hir/src/source_binder.rs @@ -15,6 +15,7 @@ use ra_syntax::{ use crate::{ HirDatabase, Function, SourceItemId, ModuleDef, AsName, Module, + ids::LocationCtx, }; /// Locates the module by `FileId`. Picks topmost module in the file. @@ -116,7 +117,10 @@ pub fn function_from_module( ) -> Function { let (file_id, _) = module.definition_source(db); let file_id = file_id.into(); - Function::from_ast(db, module, file_id, fn_def) + let ctx = LocationCtx::new(db, module, file_id); + Function { + id: ctx.to_def(fn_def), + } } pub fn function_from_child_node( -- cgit v1.2.3