2019-09-30 03:58:53 -05:00
|
|
|
//! FIXME: write short doc here
|
2019-12-05 08:16:59 -06:00
|
|
|
use hir_def::{
|
2019-12-05 16:34:12 -06:00
|
|
|
child_by_source::ChildBySource, dyn_map::DynMap, keys, nameres::ModuleSource, AstItemDef,
|
2019-12-07 12:52:09 -06:00
|
|
|
EnumVariantId, GenericDefId, LocationCtx, ModuleId, VariantId,
|
2019-12-05 08:16:59 -06:00
|
|
|
};
|
2019-11-15 15:20:00 -06:00
|
|
|
use hir_expand::{name::AsName, AstId, MacroDefId, MacroDefKind};
|
2019-11-09 15:32:00 -06:00
|
|
|
use ra_syntax::{
|
|
|
|
ast::{self, AstNode, NameOwner},
|
2019-12-05 08:16:59 -06:00
|
|
|
match_ast, SyntaxNode,
|
2019-11-09 15:32:00 -06:00
|
|
|
};
|
2019-09-16 05:48:54 -05:00
|
|
|
|
|
|
|
use crate::{
|
|
|
|
db::{AstDatabase, DefDatabase, HirDatabase},
|
2019-12-07 13:09:53 -06:00
|
|
|
Const, DefWithBody, Enum, EnumVariant, FieldSource, Function, ImplBlock, InFile, Local,
|
|
|
|
MacroDef, Module, Static, Struct, StructField, Trait, TypeAlias, TypeParam, Union,
|
2019-09-16 05:48:54 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
pub trait FromSource: Sized {
|
|
|
|
type Ast;
|
2019-11-28 03:50:26 -06:00
|
|
|
fn from_source(db: &(impl DefDatabase + AstDatabase), src: InFile<Self::Ast>) -> Option<Self>;
|
2019-09-16 05:48:54 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
impl FromSource for Struct {
|
|
|
|
type Ast = ast::StructDef;
|
2019-11-28 03:50:26 -06:00
|
|
|
fn from_source(db: &(impl DefDatabase + AstDatabase), src: InFile<Self::Ast>) -> Option<Self> {
|
2019-11-25 08:30:50 -06:00
|
|
|
let id = from_source(db, src)?;
|
|
|
|
Some(Struct { id })
|
2019-09-16 05:48:54 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
impl FromSource for Union {
|
2019-11-25 08:30:50 -06:00
|
|
|
type Ast = ast::UnionDef;
|
2019-11-28 03:50:26 -06:00
|
|
|
fn from_source(db: &(impl DefDatabase + AstDatabase), src: InFile<Self::Ast>) -> Option<Self> {
|
2019-11-25 08:30:50 -06:00
|
|
|
let id = from_source(db, src)?;
|
|
|
|
Some(Union { id })
|
2019-09-16 05:48:54 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
impl FromSource for Enum {
|
|
|
|
type Ast = ast::EnumDef;
|
2019-11-28 03:50:26 -06:00
|
|
|
fn from_source(db: &(impl DefDatabase + AstDatabase), src: InFile<Self::Ast>) -> Option<Self> {
|
2019-09-16 05:48:54 -05:00
|
|
|
let id = from_source(db, src)?;
|
|
|
|
Some(Enum { id })
|
|
|
|
}
|
|
|
|
}
|
|
|
|
impl FromSource for Trait {
|
|
|
|
type Ast = ast::TraitDef;
|
2019-11-28 03:50:26 -06:00
|
|
|
fn from_source(db: &(impl DefDatabase + AstDatabase), src: InFile<Self::Ast>) -> Option<Self> {
|
2019-09-16 05:48:54 -05:00
|
|
|
let id = from_source(db, src)?;
|
|
|
|
Some(Trait { id })
|
|
|
|
}
|
|
|
|
}
|
|
|
|
impl FromSource for Function {
|
|
|
|
type Ast = ast::FnDef;
|
2019-11-28 03:50:26 -06:00
|
|
|
fn from_source(db: &(impl DefDatabase + AstDatabase), src: InFile<Self::Ast>) -> Option<Self> {
|
2019-12-05 16:34:12 -06:00
|
|
|
Container::find(db, src.as_ref().map(|it| it.syntax()))?.child_by_source(db)[keys::FUNCTION]
|
|
|
|
.get(&src)
|
|
|
|
.copied()
|
2019-12-05 09:53:17 -06:00
|
|
|
.map(Function::from)
|
2019-09-16 05:48:54 -05:00
|
|
|
}
|
|
|
|
}
|
2019-11-20 07:03:59 -06:00
|
|
|
|
2019-09-16 05:48:54 -05:00
|
|
|
impl FromSource for Const {
|
|
|
|
type Ast = ast::ConstDef;
|
2019-11-28 03:50:26 -06:00
|
|
|
fn from_source(db: &(impl DefDatabase + AstDatabase), src: InFile<Self::Ast>) -> Option<Self> {
|
2019-12-05 16:34:12 -06:00
|
|
|
Container::find(db, src.as_ref().map(|it| it.syntax()))?.child_by_source(db)[keys::CONST]
|
|
|
|
.get(&src)
|
|
|
|
.copied()
|
2019-12-05 09:53:17 -06:00
|
|
|
.map(Const::from)
|
2019-09-16 05:48:54 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
impl FromSource for Static {
|
|
|
|
type Ast = ast::StaticDef;
|
2019-11-28 03:50:26 -06:00
|
|
|
fn from_source(db: &(impl DefDatabase + AstDatabase), src: InFile<Self::Ast>) -> Option<Self> {
|
2019-12-05 16:34:12 -06:00
|
|
|
Container::find(db, src.as_ref().map(|it| it.syntax()))?.child_by_source(db)[keys::STATIC]
|
|
|
|
.get(&src)
|
|
|
|
.copied()
|
|
|
|
.map(Static::from)
|
2019-09-16 05:48:54 -05:00
|
|
|
}
|
|
|
|
}
|
2019-11-24 06:13:56 -06:00
|
|
|
|
2019-09-16 05:48:54 -05:00
|
|
|
impl FromSource for TypeAlias {
|
|
|
|
type Ast = ast::TypeAliasDef;
|
2019-11-28 03:50:26 -06:00
|
|
|
fn from_source(db: &(impl DefDatabase + AstDatabase), src: InFile<Self::Ast>) -> Option<Self> {
|
2019-12-05 16:34:12 -06:00
|
|
|
Container::find(db, src.as_ref().map(|it| it.syntax()))?.child_by_source(db)
|
|
|
|
[keys::TYPE_ALIAS]
|
|
|
|
.get(&src)
|
|
|
|
.copied()
|
2019-12-05 09:53:17 -06:00
|
|
|
.map(TypeAlias::from)
|
2019-09-16 05:48:54 -05:00
|
|
|
}
|
|
|
|
}
|
2019-11-15 15:20:00 -06:00
|
|
|
|
|
|
|
impl FromSource for MacroDef {
|
|
|
|
type Ast = ast::MacroCall;
|
2019-11-28 03:50:26 -06:00
|
|
|
fn from_source(db: &(impl DefDatabase + AstDatabase), src: InFile<Self::Ast>) -> Option<Self> {
|
2019-11-15 15:20:00 -06:00
|
|
|
let kind = MacroDefKind::Declarative;
|
|
|
|
|
|
|
|
let module_src = ModuleSource::from_child_node(db, src.as_ref().map(|it| it.syntax()));
|
2019-11-28 03:50:26 -06:00
|
|
|
let module = Module::from_definition(db, InFile::new(src.file_id, module_src))?;
|
2019-12-08 05:01:45 -06:00
|
|
|
let krate = Some(module.krate().id);
|
2019-11-15 15:20:00 -06:00
|
|
|
|
2019-12-05 08:10:33 -06:00
|
|
|
let ast_id = Some(AstId::new(src.file_id, db.ast_id_map(src.file_id).ast_id(&src.value)));
|
2019-11-15 15:20:00 -06:00
|
|
|
|
|
|
|
let id: MacroDefId = MacroDefId { krate, ast_id, kind };
|
|
|
|
Some(MacroDef { id })
|
|
|
|
}
|
|
|
|
}
|
2019-09-16 05:48:54 -05:00
|
|
|
|
|
|
|
impl FromSource for ImplBlock {
|
|
|
|
type Ast = ast::ImplBlock;
|
2019-11-28 03:50:26 -06:00
|
|
|
fn from_source(db: &(impl DefDatabase + AstDatabase), src: InFile<Self::Ast>) -> Option<Self> {
|
2019-11-15 12:28:00 -06:00
|
|
|
let id = from_source(db, src)?;
|
|
|
|
Some(ImplBlock { id })
|
2019-09-16 05:48:54 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl FromSource for EnumVariant {
|
|
|
|
type Ast = ast::EnumVariant;
|
2019-11-28 03:50:26 -06:00
|
|
|
fn from_source(db: &(impl DefDatabase + AstDatabase), src: InFile<Self::Ast>) -> Option<Self> {
|
2019-11-20 00:40:36 -06:00
|
|
|
let parent_enum = src.value.parent_enum();
|
2019-11-28 03:50:26 -06:00
|
|
|
let src_enum = InFile { file_id: src.file_id, value: parent_enum };
|
2019-12-05 08:16:59 -06:00
|
|
|
let parent_enum = Enum::from_source(db, src_enum)?;
|
2019-12-05 16:34:12 -06:00
|
|
|
parent_enum.id.child_by_source(db)[keys::ENUM_VARIANT]
|
|
|
|
.get(&src)
|
|
|
|
.copied()
|
|
|
|
.map(EnumVariant::from)
|
2019-09-16 05:48:54 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl FromSource for StructField {
|
|
|
|
type Ast = FieldSource;
|
2019-11-28 03:50:26 -06:00
|
|
|
fn from_source(db: &(impl DefDatabase + AstDatabase), src: InFile<Self::Ast>) -> Option<Self> {
|
2019-12-05 16:34:12 -06:00
|
|
|
let src = src.as_ref();
|
|
|
|
|
|
|
|
// FIXME this is buggy
|
2019-12-05 08:16:59 -06:00
|
|
|
let variant_id: VariantId = match src.value {
|
2019-12-05 16:34:12 -06:00
|
|
|
FieldSource::Named(field) => {
|
2019-11-20 00:40:36 -06:00
|
|
|
let value = field.syntax().ancestors().find_map(ast::StructDef::cast)?;
|
2019-11-28 03:50:26 -06:00
|
|
|
let src = InFile { file_id: src.file_id, value };
|
2019-09-16 05:48:54 -05:00
|
|
|
let def = Struct::from_source(db, src)?;
|
2019-12-05 08:16:59 -06:00
|
|
|
def.id.into()
|
2019-09-16 05:48:54 -05:00
|
|
|
}
|
2019-12-05 16:34:12 -06:00
|
|
|
FieldSource::Pos(field) => {
|
2019-11-20 00:40:36 -06:00
|
|
|
let value = field.syntax().ancestors().find_map(ast::EnumVariant::cast)?;
|
2019-11-28 03:50:26 -06:00
|
|
|
let src = InFile { file_id: src.file_id, value };
|
2019-09-16 05:48:54 -05:00
|
|
|
let def = EnumVariant::from_source(db, src)?;
|
2019-12-05 08:16:59 -06:00
|
|
|
EnumVariantId::from(def).into()
|
2019-09-16 05:48:54 -05:00
|
|
|
}
|
|
|
|
};
|
2019-12-05 16:34:12 -06:00
|
|
|
|
|
|
|
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)
|
2019-09-16 05:48:54 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-09 15:32:00 -06:00
|
|
|
impl Local {
|
2019-11-28 03:50:26 -06:00
|
|
|
pub fn from_source(db: &impl HirDatabase, src: InFile<ast::BindPat>) -> Option<Self> {
|
2019-11-09 15:32:00 -06:00
|
|
|
let file_id = src.file_id;
|
2019-11-20 00:40:36 -06:00
|
|
|
let parent: DefWithBody = src.value.syntax().ancestors().find_map(|it| {
|
2019-11-09 15:32:00 -06:00
|
|
|
let res = match_ast! {
|
|
|
|
match it {
|
2019-11-28 03:50:26 -06:00
|
|
|
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() },
|
2019-11-09 15:32:00 -06:00
|
|
|
_ => return None,
|
|
|
|
}
|
|
|
|
};
|
|
|
|
Some(res)
|
|
|
|
})?;
|
2019-11-24 11:53:42 -06:00
|
|
|
let (_body, source_map) = db.body_with_source_map(parent.into());
|
2019-11-14 01:30:30 -06:00
|
|
|
let src = src.map(ast::Pat::from);
|
|
|
|
let pat_id = source_map.node_pat(src.as_ref())?;
|
2019-11-09 15:32:00 -06:00
|
|
|
Some(Local { parent, pat_id })
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-07 13:09:53 -06:00
|
|
|
impl TypeParam {
|
2019-12-07 12:52:09 -06:00
|
|
|
pub fn from_source(db: &impl HirDatabase, src: InFile<ast::TypeParam>) -> Option<Self> {
|
|
|
|
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() },
|
2019-12-07 14:55:02 -06:00
|
|
|
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() },
|
2019-12-07 12:52:09 -06:00
|
|
|
_ => return None,
|
|
|
|
}
|
|
|
|
};
|
|
|
|
Some(res)
|
|
|
|
})?;
|
|
|
|
let &id = parent.child_by_source(db)[keys::TYPE_PARAM].get(&src)?;
|
2019-12-07 13:09:53 -06:00
|
|
|
Some(TypeParam { id })
|
2019-12-07 12:52:09 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-16 05:48:54 -05:00
|
|
|
impl Module {
|
2019-11-28 03:50:26 -06:00
|
|
|
pub fn from_declaration(db: &impl DefDatabase, src: InFile<ast::Module>) -> Option<Self> {
|
2019-11-20 00:40:36 -06:00
|
|
|
let parent_declaration = src.value.syntax().ancestors().skip(1).find_map(ast::Module::cast);
|
2019-11-15 01:26:31 -06:00
|
|
|
|
|
|
|
let parent_module = match parent_declaration {
|
|
|
|
Some(parent_declaration) => {
|
2019-11-28 03:50:26 -06:00
|
|
|
let src_parent = InFile { file_id: src.file_id, value: parent_declaration };
|
2019-11-15 01:26:31 -06:00
|
|
|
Module::from_declaration(db, src_parent)
|
|
|
|
}
|
2019-12-05 07:28:31 -06:00
|
|
|
None => {
|
|
|
|
let source_file = db.parse(src.file_id.original_file(db)).tree();
|
|
|
|
let src_parent =
|
|
|
|
InFile { file_id: src.file_id, value: ModuleSource::SourceFile(source_file) };
|
2019-11-15 01:26:31 -06:00
|
|
|
Module::from_definition(db, src_parent)
|
|
|
|
}
|
|
|
|
}?;
|
|
|
|
|
2019-12-08 05:20:59 -06:00
|
|
|
let child_name = src.value.name()?.as_name();
|
|
|
|
let def_map = db.crate_def_map(parent_module.id.krate);
|
|
|
|
let child_id = def_map[parent_module.id.local_id].children.get(&child_name)?;
|
|
|
|
Some(parent_module.with_module_id(*child_id))
|
2019-09-16 05:48:54 -05:00
|
|
|
}
|
|
|
|
|
2019-11-28 03:50:26 -06:00
|
|
|
pub fn from_definition(db: &impl DefDatabase, src: InFile<ModuleSource>) -> Option<Self> {
|
2019-11-20 00:40:36 -06:00
|
|
|
match src.value {
|
2019-09-16 05:48:54 -05:00
|
|
|
ModuleSource::Module(ref module) => {
|
2019-10-23 03:31:16 -05:00
|
|
|
assert!(!module.has_semi());
|
2019-11-15 01:26:31 -06:00
|
|
|
return Module::from_declaration(
|
|
|
|
db,
|
2019-11-28 03:50:26 -06:00
|
|
|
InFile { file_id: src.file_id, value: module.clone() },
|
2019-11-15 01:26:31 -06:00
|
|
|
);
|
2019-09-16 05:48:54 -05:00
|
|
|
}
|
2019-12-04 12:35:24 -06:00
|
|
|
ModuleSource::SourceFile(_) => (),
|
2019-09-16 05:48:54 -05:00
|
|
|
};
|
|
|
|
|
2019-11-15 01:26:31 -06:00
|
|
|
let original_file = src.file_id.original_file(db);
|
2019-10-31 10:45:10 -05:00
|
|
|
|
2019-11-27 12:44:38 -06:00
|
|
|
let (krate, local_id) = db.relevant_crates(original_file).iter().find_map(|&crate_id| {
|
|
|
|
let crate_def_map = db.crate_def_map(crate_id);
|
|
|
|
let local_id = crate_def_map.modules_for_file(original_file).next()?;
|
|
|
|
Some((crate_id, local_id))
|
|
|
|
})?;
|
2019-11-27 12:31:51 -06:00
|
|
|
Some(Module { id: ModuleId { krate, local_id } })
|
2019-09-16 05:48:54 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-28 03:50:26 -06:00
|
|
|
fn from_source<N, DEF>(db: &(impl DefDatabase + AstDatabase), src: InFile<N>) -> Option<DEF>
|
2019-09-16 05:48:54 -05:00
|
|
|
where
|
|
|
|
N: AstNode,
|
|
|
|
DEF: AstItemDef<N>,
|
|
|
|
{
|
2019-11-15 14:24:56 -06:00
|
|
|
let module_src = ModuleSource::from_child_node(db, src.as_ref().map(|it| it.syntax()));
|
2019-11-28 03:50:26 -06:00
|
|
|
let module = Module::from_definition(db, InFile::new(src.file_id, module_src))?;
|
2019-10-30 05:10:38 -05:00
|
|
|
let ctx = LocationCtx::new(db, module.id, src.file_id);
|
2019-11-24 06:20:59 -06:00
|
|
|
let items = db.ast_id_map(src.file_id);
|
|
|
|
let item_id = items.ast_id(&src.value);
|
|
|
|
Some(DEF::from_ast_id(ctx, item_id))
|
2019-09-16 05:48:54 -05:00
|
|
|
}
|
2019-11-20 07:03:59 -06:00
|
|
|
|
2019-11-20 08:39:58 -06:00
|
|
|
enum Container {
|
|
|
|
Trait(Trait),
|
|
|
|
ImplBlock(ImplBlock),
|
|
|
|
Module(Module),
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Container {
|
2019-11-28 03:50:26 -06:00
|
|
|
fn find(db: &impl DefDatabase, src: InFile<&SyntaxNode>) -> Option<Container> {
|
2019-11-20 08:39:58 -06:00
|
|
|
// FIXME: this doesn't try to handle nested declarations
|
|
|
|
for container in src.value.ancestors() {
|
|
|
|
let res = match_ast! {
|
|
|
|
match container {
|
|
|
|
ast::TraitDef(it) => {
|
|
|
|
let c = Trait::from_source(db, src.with_value(it))?;
|
|
|
|
Container::Trait(c)
|
|
|
|
},
|
|
|
|
ast::ImplBlock(it) => {
|
|
|
|
let c = ImplBlock::from_source(db, src.with_value(it))?;
|
|
|
|
Container::ImplBlock(c)
|
|
|
|
},
|
|
|
|
_ => { 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(Container::Module(c))
|
|
|
|
}
|
|
|
|
}
|
2019-12-05 09:53:17 -06:00
|
|
|
|
2019-12-05 16:34:12 -06:00
|
|
|
impl ChildBySource for Container {
|
|
|
|
fn child_by_source(&self, db: &impl DefDatabase) -> DynMap {
|
2019-12-05 09:53:17 -06:00
|
|
|
match self {
|
2019-12-05 16:34:12 -06:00
|
|
|
Container::Trait(it) => it.id.child_by_source(db),
|
|
|
|
Container::ImplBlock(it) => it.id.child_by_source(db),
|
|
|
|
Container::Module(it) => it.id.child_by_source(db),
|
2019-12-05 09:53:17 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|