mirror of
https://github.com/rust-lang/rust.git
synced 2025-02-04 02:54:00 +00:00
remove boilerplate
This commit is contained in:
parent
90215eb5a0
commit
f588535273
@ -8,42 +8,18 @@ use ra_syntax::{
|
|||||||
};
|
};
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
Name, AsName, Struct, Enum, EnumVariant, Module, HirFileId,
|
Name, AsName, Struct, Enum, EnumVariant,
|
||||||
HirDatabase,
|
HirDatabase,
|
||||||
type_ref::TypeRef,
|
type_ref::TypeRef,
|
||||||
ids::ItemLoc,
|
ids::LocationCtx,
|
||||||
};
|
};
|
||||||
|
|
||||||
impl Struct {
|
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> {
|
pub(crate) fn variant_data(&self, db: &impl HirDatabase) -> Arc<VariantData> {
|
||||||
db.struct_data((*self).into()).variant_data.clone()
|
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)]
|
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||||
pub struct StructData {
|
pub struct StructData {
|
||||||
pub(crate) name: Option<Name>,
|
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)]
|
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||||
pub struct EnumData {
|
pub struct EnumData {
|
||||||
pub(crate) name: Option<Name>,
|
pub(crate) name: Option<Name>,
|
||||||
@ -92,11 +55,15 @@ impl EnumData {
|
|||||||
pub(crate) fn enum_data_query(db: &impl HirDatabase, e: Enum) -> Arc<EnumData> {
|
pub(crate) fn enum_data_query(db: &impl HirDatabase, e: Enum) -> Arc<EnumData> {
|
||||||
let (file_id, enum_def) = e.source(db);
|
let (file_id, enum_def) = e.source(db);
|
||||||
let module = e.module(db);
|
let module = e.module(db);
|
||||||
|
let ctx = LocationCtx::new(db, module, file_id);
|
||||||
let variants = if let Some(vl) = enum_def.variant_list() {
|
let variants = if let Some(vl) = enum_def.variant_list() {
|
||||||
vl.variants()
|
vl.variants()
|
||||||
.filter_map(|variant_def| {
|
.filter_map(|variant_def| {
|
||||||
let name = variant_def.name().map(|n| n.as_name());
|
let name = variant_def.name()?.as_name();
|
||||||
name.map(|n| (n, EnumVariant::from_ast(db, module, file_id, variant_def)))
|
let var = EnumVariant {
|
||||||
|
id: ctx.to_def(variant_def),
|
||||||
|
};
|
||||||
|
Some((name, var))
|
||||||
})
|
})
|
||||||
.collect()
|
.collect()
|
||||||
} else {
|
} else {
|
||||||
@ -131,7 +98,10 @@ impl EnumVariantData {
|
|||||||
) -> Arc<EnumVariantData> {
|
) -> Arc<EnumVariantData> {
|
||||||
let (file_id, variant_def) = var.source(db);
|
let (file_id, variant_def) = var.source(db);
|
||||||
let enum_def = variant_def.parent_enum();
|
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))
|
Arc::new(EnumVariantData::new(&*variant_def, e))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -5,27 +5,15 @@ use std::sync::Arc;
|
|||||||
use ra_syntax::ast::{self, NameOwner};
|
use ra_syntax::ast::{self, NameOwner};
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
HirDatabase, Name, AsName, Function, FnSignature, Module, HirFileId,
|
HirDatabase, Name, AsName, Function, FnSignature,
|
||||||
type_ref::{TypeRef, Mutability},
|
type_ref::{TypeRef, Mutability},
|
||||||
expr::Body,
|
expr::Body,
|
||||||
impl_block::ImplBlock,
|
impl_block::ImplBlock,
|
||||||
ids::ItemLoc,
|
|
||||||
};
|
};
|
||||||
|
|
||||||
pub use self::scope::{FnScopes, ScopesWithSyntaxMapping, ScopeEntryWithSyntax};
|
pub use self::scope::{FnScopes, ScopesWithSyntaxMapping, ScopeEntryWithSyntax};
|
||||||
|
|
||||||
impl Function {
|
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> {
|
pub(crate) fn body(&self, db: &impl HirDatabase) -> Arc<Body> {
|
||||||
db.body_hir(*self)
|
db.body_hir(*self)
|
||||||
}
|
}
|
||||||
|
@ -16,10 +16,10 @@ use crate::{
|
|||||||
pub struct HirInterner {
|
pub struct HirInterner {
|
||||||
defs: LocationIntener<DefLoc, DefId>,
|
defs: LocationIntener<DefLoc, DefId>,
|
||||||
macros: LocationIntener<MacroCallLoc, MacroCallId>,
|
macros: LocationIntener<MacroCallLoc, MacroCallId>,
|
||||||
pub(crate) fns: LocationIntener<ItemLoc<ast::FnDef>, FunctionId>,
|
fns: LocationIntener<ItemLoc<ast::FnDef>, FunctionId>,
|
||||||
pub(crate) structs: LocationIntener<ItemLoc<ast::StructDef>, StructId>,
|
structs: LocationIntener<ItemLoc<ast::StructDef>, StructId>,
|
||||||
pub(crate) enums: LocationIntener<ItemLoc<ast::EnumDef>, EnumId>,
|
enums: LocationIntener<ItemLoc<ast::EnumDef>, EnumId>,
|
||||||
pub(crate) enum_variants: LocationIntener<ItemLoc<ast::EnumVariant>, EnumVariantId>,
|
enum_variants: LocationIntener<ItemLoc<ast::EnumVariant>, EnumVariantId>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl HirInterner {
|
impl HirInterner {
|
||||||
@ -144,34 +144,6 @@ pub struct ItemLoc<N: AstNode> {
|
|||||||
_ty: PhantomData<N>,
|
_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> {
|
impl<N: AstNode> Clone for ItemLoc<N> {
|
||||||
fn clone(&self) -> ItemLoc<N> {
|
fn clone(&self) -> ItemLoc<N> {
|
||||||
ItemLoc {
|
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 {
|
pub(crate) trait AstItemDef<N: AstNode + Eq + Hash>: ArenaId + Clone {
|
||||||
fn interner(interner: &HirInterner) -> &LocationIntener<ItemLoc<N>, Self>;
|
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>) {
|
fn source(self, db: &impl HirDatabase) -> (HirFileId, TreeArc<N>) {
|
||||||
let int = Self::interner(db.as_ref());
|
let int = Self::interner(db.as_ref());
|
||||||
let loc = int.id2loc(self);
|
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 {
|
fn module(self, db: &impl HirDatabase) -> Module {
|
||||||
let int = Self::interner(db.as_ref());
|
let int = Self::interner(db.as_ref());
|
||||||
|
@ -9,6 +9,7 @@ use crate::{
|
|||||||
Function, HirFileId,
|
Function, HirFileId,
|
||||||
db::HirDatabase,
|
db::HirDatabase,
|
||||||
type_ref::TypeRef,
|
type_ref::TypeRef,
|
||||||
|
ids::LocationCtx,
|
||||||
};
|
};
|
||||||
|
|
||||||
use crate::code_model_api::{Module, ModuleSource};
|
use crate::code_model_api::{Module, ModuleSource};
|
||||||
@ -72,13 +73,14 @@ impl ImplData {
|
|||||||
) -> Self {
|
) -> Self {
|
||||||
let target_trait = node.target_trait().map(TypeRef::from_ast);
|
let target_trait = node.target_trait().map(TypeRef::from_ast);
|
||||||
let target_type = TypeRef::from_ast_opt(node.target_type());
|
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() {
|
let items = if let Some(item_list) = node.item_list() {
|
||||||
item_list
|
item_list
|
||||||
.impl_items()
|
.impl_items()
|
||||||
.map(|item_node| {
|
.map(|item_node| {
|
||||||
let kind = match item_node.kind() {
|
let kind = match item_node.kind() {
|
||||||
ast::ImplItemKind::FnDef(it) => {
|
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::ConstDef(..) => DefKind::Item,
|
||||||
ast::ImplItemKind::TypeDef(..) => DefKind::Item,
|
ast::ImplItemKind::TypeDef(..) => DefKind::Item,
|
||||||
|
@ -11,6 +11,7 @@ use crate::{
|
|||||||
SourceItemId, Path, ModuleSource, HirDatabase, Name, SourceFileItems,
|
SourceItemId, Path, ModuleSource, HirDatabase, Name, SourceFileItems,
|
||||||
HirFileId, MacroCallLoc, AsName, PerNs, DefKind, DefLoc, Function,
|
HirFileId, MacroCallLoc, AsName, PerNs, DefKind, DefLoc, Function,
|
||||||
ModuleDef, Module, Struct, Enum,
|
ModuleDef, Module, Struct, Enum,
|
||||||
|
ids::LocationCtx,
|
||||||
};
|
};
|
||||||
|
|
||||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
|
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
|
||||||
@ -146,10 +147,11 @@ impl LoweredModule {
|
|||||||
file_items: &SourceFileItems,
|
file_items: &SourceFileItems,
|
||||||
item: &ast::ModuleItem,
|
item: &ast::ModuleItem,
|
||||||
) {
|
) {
|
||||||
|
let ctx = LocationCtx::new(db, module, file_id);
|
||||||
let name = match item.kind() {
|
let name = match item.kind() {
|
||||||
ast::ModuleItemKind::StructDef(it) => {
|
ast::ModuleItemKind::StructDef(it) => {
|
||||||
if let Some(name) = it.name() {
|
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();
|
let s: ModuleDef = s.into();
|
||||||
self.declarations.insert(name.as_name(), PerNs::both(s, s));
|
self.declarations.insert(name.as_name(), PerNs::both(s, s));
|
||||||
}
|
}
|
||||||
@ -157,7 +159,7 @@ impl LoweredModule {
|
|||||||
}
|
}
|
||||||
ast::ModuleItemKind::EnumDef(it) => {
|
ast::ModuleItemKind::EnumDef(it) => {
|
||||||
if let Some(name) = it.name() {
|
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();
|
let e: ModuleDef = e.into();
|
||||||
self.declarations.insert(name.as_name(), PerNs::types(e));
|
self.declarations.insert(name.as_name(), PerNs::types(e));
|
||||||
}
|
}
|
||||||
@ -165,7 +167,7 @@ impl LoweredModule {
|
|||||||
}
|
}
|
||||||
ast::ModuleItemKind::FnDef(it) => {
|
ast::ModuleItemKind::FnDef(it) => {
|
||||||
if let Some(name) = it.name() {
|
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
|
self.declarations
|
||||||
.insert(name.as_name(), PerNs::values(func.into()));
|
.insert(name.as_name(), PerNs::values(func.into()));
|
||||||
}
|
}
|
||||||
|
@ -15,6 +15,7 @@ use ra_syntax::{
|
|||||||
use crate::{
|
use crate::{
|
||||||
HirDatabase, Function, SourceItemId, ModuleDef,
|
HirDatabase, Function, SourceItemId, ModuleDef,
|
||||||
AsName, Module,
|
AsName, Module,
|
||||||
|
ids::LocationCtx,
|
||||||
};
|
};
|
||||||
|
|
||||||
/// Locates the module by `FileId`. Picks topmost module in the file.
|
/// Locates the module by `FileId`. Picks topmost module in the file.
|
||||||
@ -116,7 +117,10 @@ pub fn function_from_module(
|
|||||||
) -> Function {
|
) -> Function {
|
||||||
let (file_id, _) = module.definition_source(db);
|
let (file_id, _) = module.definition_source(db);
|
||||||
let file_id = file_id.into();
|
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(
|
pub fn function_from_child_node(
|
||||||
|
Loading…
Reference in New Issue
Block a user