mirror of
https://github.com/rust-lang/rust.git
synced 2024-11-30 02:33:55 +00:00
Removed FromSource
This commit is contained in:
parent
81a45ca1b3
commit
8691ae8ac0
@ -2,139 +2,32 @@
|
||||
//! file.
|
||||
|
||||
use hir_def::{
|
||||
child_by_source::ChildBySource, dyn_map::DynMap, keys, keys::Key, nameres::ModuleSource,
|
||||
ConstId, DefWithBodyId, EnumId, EnumVariantId, FunctionId, GenericDefId, ImplId, ModuleId,
|
||||
StaticId, StructId, TraitId, TypeAliasId, UnionId, VariantId,
|
||||
child_by_source::ChildBySource, keys, nameres::ModuleSource, GenericDefId, ModuleId,
|
||||
};
|
||||
use hir_expand::{name::AsName, AstId, MacroDefId, MacroDefKind};
|
||||
use hir_expand::name::AsName;
|
||||
use ra_db::FileId;
|
||||
use ra_prof::profile;
|
||||
use ra_syntax::{
|
||||
ast::{self, AstNode, NameOwner},
|
||||
match_ast, SyntaxNode,
|
||||
match_ast,
|
||||
};
|
||||
|
||||
use crate::{
|
||||
db::{DefDatabase, HirDatabase},
|
||||
Const, DefWithBody, Enum, EnumVariant, FieldSource, Function, ImplBlock, InFile, Local,
|
||||
MacroDef, Module, Static, Struct, StructField, Trait, TypeAlias, TypeParam, Union,
|
||||
Const, DefWithBody, Enum, Function, ImplBlock, InFile, Local, Module, SourceBinder, Static,
|
||||
Struct, Trait, TypeAlias, TypeParam,
|
||||
};
|
||||
|
||||
pub(crate) trait FromSource: Sized {
|
||||
type Ast;
|
||||
fn from_source(db: &impl DefDatabase, src: InFile<Self::Ast>) -> Option<Self>;
|
||||
}
|
||||
|
||||
pub trait FromSourceByContainer: Sized {
|
||||
type Ast: AstNode + 'static;
|
||||
type Id: Copy + 'static;
|
||||
const KEY: Key<Self::Ast, Self::Id>;
|
||||
}
|
||||
|
||||
impl<T: FromSourceByContainer> FromSource for T
|
||||
where
|
||||
T: From<<T as FromSourceByContainer>::Id>,
|
||||
{
|
||||
type Ast = <T as FromSourceByContainer>::Ast;
|
||||
fn from_source(db: &impl DefDatabase, src: InFile<Self::Ast>) -> Option<Self> {
|
||||
analyze_container(db, src.as_ref().map(|it| it.syntax()))[T::KEY]
|
||||
.get(&src)
|
||||
.copied()
|
||||
.map(Self::from)
|
||||
}
|
||||
}
|
||||
|
||||
macro_rules! from_source_by_container_impls {
|
||||
($(($hir:ident, $id:ident, $ast:path, $key:path)),* ,) => {$(
|
||||
impl FromSourceByContainer for $hir {
|
||||
type Ast = $ast;
|
||||
type Id = $id;
|
||||
const KEY: Key<Self::Ast, Self::Id> = $key;
|
||||
}
|
||||
)*}
|
||||
}
|
||||
|
||||
from_source_by_container_impls![
|
||||
(Struct, StructId, ast::StructDef, keys::STRUCT),
|
||||
(Union, UnionId, ast::UnionDef, keys::UNION),
|
||||
(Enum, EnumId, ast::EnumDef, keys::ENUM),
|
||||
(Trait, TraitId, ast::TraitDef, keys::TRAIT),
|
||||
(Function, FunctionId, ast::FnDef, keys::FUNCTION),
|
||||
(Static, StaticId, ast::StaticDef, keys::STATIC),
|
||||
(Const, ConstId, ast::ConstDef, keys::CONST),
|
||||
(TypeAlias, TypeAliasId, ast::TypeAliasDef, keys::TYPE_ALIAS),
|
||||
(ImplBlock, ImplId, ast::ImplBlock, keys::IMPL),
|
||||
];
|
||||
|
||||
impl FromSource for MacroDef {
|
||||
type Ast = ast::MacroCall;
|
||||
fn from_source(db: &impl DefDatabase, src: InFile<Self::Ast>) -> Option<Self> {
|
||||
let kind = MacroDefKind::Declarative;
|
||||
|
||||
let module_src = ModuleSource::from_child_node(db, src.as_ref().map(|it| it.syntax()));
|
||||
let module = Module::from_definition(db, InFile::new(src.file_id, module_src))?;
|
||||
let krate = Some(module.krate().id);
|
||||
|
||||
let ast_id = Some(AstId::new(src.file_id, db.ast_id_map(src.file_id).ast_id(&src.value)));
|
||||
|
||||
let id: MacroDefId = MacroDefId { krate, ast_id, kind };
|
||||
Some(MacroDef { id })
|
||||
}
|
||||
}
|
||||
|
||||
impl FromSource for EnumVariant {
|
||||
type Ast = ast::EnumVariant;
|
||||
fn from_source(db: &impl DefDatabase, src: InFile<Self::Ast>) -> Option<Self> {
|
||||
let parent_enum = src.value.parent_enum();
|
||||
let src_enum = InFile { file_id: src.file_id, value: parent_enum };
|
||||
let parent_enum = Enum::from_source(db, src_enum)?;
|
||||
parent_enum.id.child_by_source(db)[keys::ENUM_VARIANT]
|
||||
.get(&src)
|
||||
.copied()
|
||||
.map(EnumVariant::from)
|
||||
}
|
||||
}
|
||||
|
||||
impl FromSource for StructField {
|
||||
type Ast = FieldSource;
|
||||
fn from_source(db: &impl DefDatabase, src: InFile<Self::Ast>) -> Option<Self> {
|
||||
let src = src.as_ref();
|
||||
|
||||
// FIXME this is buggy
|
||||
let variant_id: VariantId = match src.value {
|
||||
FieldSource::Named(field) => {
|
||||
let value = field.syntax().ancestors().find_map(ast::StructDef::cast)?;
|
||||
let src = InFile { file_id: src.file_id, value };
|
||||
let def = Struct::from_source(db, src)?;
|
||||
def.id.into()
|
||||
}
|
||||
FieldSource::Pos(field) => {
|
||||
let value = field.syntax().ancestors().find_map(ast::EnumVariant::cast)?;
|
||||
let src = InFile { file_id: src.file_id, value };
|
||||
let def = EnumVariant::from_source(db, src)?;
|
||||
EnumVariantId::from(def).into()
|
||||
}
|
||||
};
|
||||
|
||||
let dyn_map = variant_id.child_by_source(db);
|
||||
match src.value {
|
||||
FieldSource::Pos(it) => dyn_map[keys::TUPLE_FIELD].get(&src.with_value(it.clone())),
|
||||
FieldSource::Named(it) => dyn_map[keys::RECORD_FIELD].get(&src.with_value(it.clone())),
|
||||
}
|
||||
.copied()
|
||||
.map(StructField::from)
|
||||
}
|
||||
}
|
||||
|
||||
impl Local {
|
||||
pub fn from_source(db: &impl HirDatabase, src: InFile<ast::BindPat>) -> Option<Self> {
|
||||
let mut sb = SourceBinder::new(db);
|
||||
let file_id = src.file_id;
|
||||
let parent: DefWithBody = src.value.syntax().ancestors().find_map(|it| {
|
||||
let res = match_ast! {
|
||||
match it {
|
||||
ast::ConstDef(value) => { Const::from_source(db, InFile { value, file_id})?.into() },
|
||||
ast::StaticDef(value) => { Static::from_source(db, InFile { value, file_id})?.into() },
|
||||
ast::FnDef(value) => { Function::from_source(db, InFile { value, file_id})?.into() },
|
||||
ast::ConstDef(value) => { sb.to_def::<Const, _>(InFile { value, file_id})?.into() },
|
||||
ast::StaticDef(value) => { sb.to_def::<Static, _>(InFile { value, file_id})?.into() },
|
||||
ast::FnDef(value) => { sb.to_def::<Function, _>(InFile { value, file_id})?.into() },
|
||||
_ => return None,
|
||||
}
|
||||
};
|
||||
@ -149,16 +42,17 @@ impl Local {
|
||||
|
||||
impl TypeParam {
|
||||
pub fn from_source(db: &impl HirDatabase, src: InFile<ast::TypeParam>) -> Option<Self> {
|
||||
let mut sb = SourceBinder::new(db);
|
||||
let file_id = src.file_id;
|
||||
let parent: GenericDefId = src.value.syntax().ancestors().find_map(|it| {
|
||||
let res = match_ast! {
|
||||
match it {
|
||||
ast::FnDef(value) => { Function::from_source(db, InFile { value, file_id})?.id.into() },
|
||||
ast::StructDef(value) => { Struct::from_source(db, InFile { value, file_id})?.id.into() },
|
||||
ast::EnumDef(value) => { Enum::from_source(db, InFile { value, file_id})?.id.into() },
|
||||
ast::TraitDef(value) => { Trait::from_source(db, InFile { value, file_id})?.id.into() },
|
||||
ast::TypeAliasDef(value) => { TypeAlias::from_source(db, InFile { value, file_id})?.id.into() },
|
||||
ast::ImplBlock(value) => { ImplBlock::from_source(db, InFile { value, file_id})?.id.into() },
|
||||
ast::FnDef(value) => { sb.to_def::<Function, _>(InFile { value, file_id})?.id.into() },
|
||||
ast::StructDef(value) => { sb.to_def::<Struct, _>(InFile { value, file_id})?.id.into() },
|
||||
ast::EnumDef(value) => { sb.to_def::<Enum, _>(InFile { value, file_id})?.id.into() },
|
||||
ast::TraitDef(value) => { sb.to_def::<Trait, _>(InFile { value, file_id})?.id.into() },
|
||||
ast::TypeAliasDef(value) => { sb.to_def::<TypeAlias, _>(InFile { value, file_id})?.id.into() },
|
||||
ast::ImplBlock(value) => { sb.to_def::<ImplBlock, _>(InFile { value, file_id})?.id.into() },
|
||||
_ => return None,
|
||||
}
|
||||
};
|
||||
@ -220,46 +114,3 @@ impl Module {
|
||||
Some(Module { id: ModuleId { krate, local_id } })
|
||||
}
|
||||
}
|
||||
|
||||
fn analyze_container(db: &impl DefDatabase, src: InFile<&SyntaxNode>) -> DynMap {
|
||||
let _p = profile("analyze_container");
|
||||
return child_by_source(db, src).unwrap_or_default();
|
||||
|
||||
fn child_by_source(db: &impl DefDatabase, src: InFile<&SyntaxNode>) -> Option<DynMap> {
|
||||
for container in src.value.ancestors().skip(1) {
|
||||
let res = match_ast! {
|
||||
match container {
|
||||
ast::TraitDef(it) => {
|
||||
let def = Trait::from_source(db, src.with_value(it))?;
|
||||
def.id.child_by_source(db)
|
||||
},
|
||||
ast::ImplBlock(it) => {
|
||||
let def = ImplBlock::from_source(db, src.with_value(it))?;
|
||||
def.id.child_by_source(db)
|
||||
},
|
||||
ast::FnDef(it) => {
|
||||
let def = Function::from_source(db, src.with_value(it))?;
|
||||
DefWithBodyId::from(def.id)
|
||||
.child_by_source(db)
|
||||
},
|
||||
ast::StaticDef(it) => {
|
||||
let def = Static::from_source(db, src.with_value(it))?;
|
||||
DefWithBodyId::from(def.id)
|
||||
.child_by_source(db)
|
||||
},
|
||||
ast::ConstDef(it) => {
|
||||
let def = Const::from_source(db, src.with_value(it))?;
|
||||
DefWithBodyId::from(def.id)
|
||||
.child_by_source(db)
|
||||
},
|
||||
_ => { continue },
|
||||
}
|
||||
};
|
||||
return Some(res);
|
||||
}
|
||||
|
||||
let module_source = ModuleSource::from_child_node(db, src);
|
||||
let c = Module::from_definition(db, src.with_value(module_source))?;
|
||||
Some(c.id.child_by_source(db))
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user