remove boilerplate

This commit is contained in:
Aleksey Kladov 2019-01-25 00:26:54 +03:00
parent 90215eb5a0
commit f588535273
6 changed files with 73 additions and 93 deletions

View File

@ -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<VariantData> {
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<Name>,
@ -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<Name>,
@ -92,11 +55,15 @@ impl EnumData {
pub(crate) fn enum_data_query(db: &impl HirDatabase, e: Enum) -> Arc<EnumData> {
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<EnumVariantData> {
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))
}
}

View File

@ -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<Body> {
db.body_hir(*self)
}

View File

@ -16,10 +16,10 @@ use crate::{
pub struct HirInterner {
defs: LocationIntener<DefLoc, DefId>,
macros: LocationIntener<MacroCallLoc, MacroCallId>,
pub(crate) fns: LocationIntener<ItemLoc<ast::FnDef>, FunctionId>,
pub(crate) structs: LocationIntener<ItemLoc<ast::StructDef>, StructId>,
pub(crate) enums: LocationIntener<ItemLoc<ast::EnumDef>, EnumId>,
pub(crate) enum_variants: LocationIntener<ItemLoc<ast::EnumVariant>, EnumVariantId>,
fns: LocationIntener<ItemLoc<ast::FnDef>, FunctionId>,
structs: LocationIntener<ItemLoc<ast::StructDef>, StructId>,
enums: LocationIntener<ItemLoc<ast::EnumDef>, EnumId>,
enum_variants: LocationIntener<ItemLoc<ast::EnumVariant>, EnumVariantId>,
}
impl HirInterner {
@ -144,34 +144,6 @@ pub struct ItemLoc<N: AstNode> {
_ty: PhantomData<N>,
}
impl<N: AstNode> ItemLoc<N> {
pub(crate) fn from_ast(
db: &impl HirDatabase,
module: Module,
file_id: HirFileId,
ast: &N,
) -> ItemLoc<N> {
let items = db.file_items(file_id);
let raw = SourceItemId {
file_id,
item_id: Some(items.id_of(file_id, ast.syntax())),
};
ItemLoc {
module,
raw,
_ty: PhantomData,
}
}
pub(crate) fn source(&self, db: &impl HirDatabase) -> (HirFileId, TreeArc<N>) {
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)
}
}
impl<N: AstNode> Clone for ItemLoc<N> {
fn clone(&self) -> ItemLoc<N> {
ItemLoc {
@ -182,12 +154,54 @@ impl<N: AstNode> Clone for ItemLoc<N> {
}
}
#[derive(Clone, Copy)]
pub(crate) struct LocationCtx<DB> {
db: DB,
module: Module,
file_id: HirFileId,
}
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<N, DEF>(self, ast: &N) -> DEF
where
N: AstNode + Eq + Hash,
DEF: AstItemDef<N>,
{
DEF::from_ast(self, ast)
}
}
pub(crate) trait AstItemDef<N: AstNode + Eq + Hash>: ArenaId + Clone {
fn interner(interner: &HirInterner) -> &LocationIntener<ItemLoc<N>, 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<N>) {
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());

View File

@ -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,

View File

@ -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()));
}

View File

@ -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(