2019-09-30 03:58:53 -05:00
|
|
|
//! FIXME: write short doc here
|
|
|
|
|
2019-11-24 05:02:08 -06:00
|
|
|
use hir_def::{AstItemDef, LocationCtx, ModuleId, StructId, StructOrUnionId, UnionId};
|
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-11-20 08:39:58 -06:00
|
|
|
match_ast, AstPtr, SyntaxNode,
|
2019-11-09 15:32:00 -06:00
|
|
|
};
|
2019-09-16 05:48:54 -05:00
|
|
|
|
|
|
|
use crate::{
|
|
|
|
db::{AstDatabase, DefDatabase, HirDatabase},
|
2019-11-20 07:03:59 -06:00
|
|
|
AssocItem, Const, DefWithBody, Enum, EnumVariant, FieldSource, Function, HasBody, HasSource,
|
|
|
|
ImplBlock, Local, MacroDef, Module, ModuleDef, ModuleSource, Source, Static, Struct,
|
|
|
|
StructField, Trait, TypeAlias, Union, VariantDef,
|
2019-09-16 05:48:54 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
pub trait FromSource: Sized {
|
|
|
|
type Ast;
|
|
|
|
fn from_source(db: &(impl DefDatabase + AstDatabase), src: Source<Self::Ast>) -> Option<Self>;
|
|
|
|
}
|
|
|
|
|
2019-11-09 06:34:00 -06:00
|
|
|
// FIXIME: these two impls are wrong, `ast::StructDef` might produce either a struct or a union
|
2019-09-16 05:48:54 -05:00
|
|
|
impl FromSource for Struct {
|
|
|
|
type Ast = ast::StructDef;
|
|
|
|
fn from_source(db: &(impl DefDatabase + AstDatabase), src: Source<Self::Ast>) -> Option<Self> {
|
2019-11-09 06:34:00 -06:00
|
|
|
let id: StructOrUnionId = from_source(db, src)?;
|
|
|
|
Some(Struct { id: StructId(id) })
|
2019-09-16 05:48:54 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
impl FromSource for Union {
|
|
|
|
type Ast = ast::StructDef;
|
|
|
|
fn from_source(db: &(impl DefDatabase + AstDatabase), src: Source<Self::Ast>) -> Option<Self> {
|
2019-11-09 06:34:00 -06:00
|
|
|
let id: StructOrUnionId = from_source(db, src)?;
|
|
|
|
Some(Union { id: UnionId(id) })
|
2019-09-16 05:48:54 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
impl FromSource for Enum {
|
|
|
|
type Ast = ast::EnumDef;
|
|
|
|
fn from_source(db: &(impl DefDatabase + AstDatabase), src: Source<Self::Ast>) -> Option<Self> {
|
|
|
|
let id = from_source(db, src)?;
|
|
|
|
Some(Enum { id })
|
|
|
|
}
|
|
|
|
}
|
|
|
|
impl FromSource for Trait {
|
|
|
|
type Ast = ast::TraitDef;
|
|
|
|
fn from_source(db: &(impl DefDatabase + AstDatabase), src: Source<Self::Ast>) -> Option<Self> {
|
|
|
|
let id = from_source(db, src)?;
|
|
|
|
Some(Trait { id })
|
|
|
|
}
|
|
|
|
}
|
|
|
|
impl FromSource for Function {
|
|
|
|
type Ast = ast::FnDef;
|
|
|
|
fn from_source(db: &(impl DefDatabase + AstDatabase), src: Source<Self::Ast>) -> Option<Self> {
|
2019-11-20 08:39:58 -06:00
|
|
|
let items = match Container::find(db, src.as_ref().map(|it| it.syntax()))? {
|
|
|
|
Container::Trait(it) => it.items(db),
|
|
|
|
Container::ImplBlock(it) => it.items(db),
|
|
|
|
Container::Module(m) => {
|
|
|
|
return m
|
|
|
|
.declarations(db)
|
|
|
|
.into_iter()
|
|
|
|
.filter_map(|it| match it {
|
|
|
|
ModuleDef::Function(it) => Some(it),
|
|
|
|
_ => None,
|
|
|
|
})
|
|
|
|
.find(|it| same_source(&it.source(db), &src))
|
|
|
|
}
|
|
|
|
};
|
|
|
|
items
|
2019-11-20 07:03:59 -06:00
|
|
|
.into_iter()
|
|
|
|
.filter_map(|it| match it {
|
2019-11-20 08:39:58 -06:00
|
|
|
AssocItem::Function(it) => Some(it),
|
2019-11-20 07:03:59 -06:00
|
|
|
_ => None,
|
|
|
|
})
|
2019-11-20 08:39:58 -06:00
|
|
|
.find(|it| same_source(&it.source(db), &src))
|
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;
|
|
|
|
fn from_source(db: &(impl DefDatabase + AstDatabase), src: Source<Self::Ast>) -> Option<Self> {
|
2019-11-20 09:00:01 -06:00
|
|
|
let items = match Container::find(db, src.as_ref().map(|it| it.syntax()))? {
|
|
|
|
Container::Trait(it) => it.items(db),
|
|
|
|
Container::ImplBlock(it) => it.items(db),
|
|
|
|
Container::Module(m) => {
|
|
|
|
return m
|
|
|
|
.declarations(db)
|
|
|
|
.into_iter()
|
|
|
|
.filter_map(|it| match it {
|
|
|
|
ModuleDef::Const(it) => Some(it),
|
|
|
|
_ => None,
|
|
|
|
})
|
|
|
|
.find(|it| same_source(&it.source(db), &src))
|
|
|
|
}
|
|
|
|
};
|
|
|
|
items
|
|
|
|
.into_iter()
|
|
|
|
.filter_map(|it| match it {
|
|
|
|
AssocItem::Const(it) => Some(it),
|
|
|
|
_ => None,
|
|
|
|
})
|
|
|
|
.find(|it| same_source(&it.source(db), &src))
|
2019-09-16 05:48:54 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
impl FromSource for Static {
|
|
|
|
type Ast = ast::StaticDef;
|
|
|
|
fn from_source(db: &(impl DefDatabase + AstDatabase), src: Source<Self::Ast>) -> Option<Self> {
|
2019-11-24 06:13:56 -06:00
|
|
|
let module = match Container::find(db, src.as_ref().map(|it| it.syntax()))? {
|
|
|
|
Container::Module(it) => it,
|
|
|
|
Container::Trait(_) | Container::ImplBlock(_) => return None,
|
|
|
|
};
|
|
|
|
module
|
|
|
|
.declarations(db)
|
|
|
|
.into_iter()
|
|
|
|
.filter_map(|it| match it {
|
|
|
|
ModuleDef::Static(it) => Some(it),
|
|
|
|
_ => None,
|
|
|
|
})
|
|
|
|
.find(|it| same_source(&it.source(db), &src))
|
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;
|
|
|
|
fn from_source(db: &(impl DefDatabase + AstDatabase), src: Source<Self::Ast>) -> Option<Self> {
|
2019-11-20 08:39:58 -06:00
|
|
|
let items = match Container::find(db, src.as_ref().map(|it| it.syntax()))? {
|
|
|
|
Container::Trait(it) => it.items(db),
|
|
|
|
Container::ImplBlock(it) => it.items(db),
|
|
|
|
Container::Module(m) => {
|
|
|
|
return m
|
|
|
|
.declarations(db)
|
|
|
|
.into_iter()
|
|
|
|
.filter_map(|it| match it {
|
|
|
|
ModuleDef::TypeAlias(it) => Some(it),
|
|
|
|
_ => None,
|
|
|
|
})
|
|
|
|
.find(|it| same_source(&it.source(db), &src))
|
|
|
|
}
|
|
|
|
};
|
|
|
|
items
|
|
|
|
.into_iter()
|
|
|
|
.filter_map(|it| match it {
|
|
|
|
AssocItem::TypeAlias(it) => Some(it),
|
|
|
|
_ => None,
|
|
|
|
})
|
|
|
|
.find(|it| same_source(&it.source(db), &src))
|
2019-09-16 05:48:54 -05:00
|
|
|
}
|
|
|
|
}
|
2019-11-15 15:20:00 -06:00
|
|
|
|
|
|
|
impl FromSource for MacroDef {
|
|
|
|
type Ast = ast::MacroCall;
|
|
|
|
fn from_source(db: &(impl DefDatabase + AstDatabase), src: Source<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, Source::new(src.file_id, module_src))?;
|
|
|
|
let krate = module.krate().crate_id();
|
|
|
|
|
2019-11-20 00:40:36 -06:00
|
|
|
let ast_id = 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;
|
|
|
|
fn from_source(db: &(impl DefDatabase + AstDatabase), src: Source<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;
|
|
|
|
fn from_source(db: &(impl DefDatabase + AstDatabase), src: Source<Self::Ast>) -> Option<Self> {
|
2019-11-20 00:40:36 -06:00
|
|
|
let parent_enum = src.value.parent_enum();
|
|
|
|
let src_enum = Source { file_id: src.file_id, value: parent_enum };
|
2019-09-16 05:48:54 -05:00
|
|
|
let variants = Enum::from_source(db, src_enum)?.variants(db);
|
2019-11-20 07:03:59 -06:00
|
|
|
variants.into_iter().find(|v| same_source(&v.source(db), &src))
|
2019-09-16 05:48:54 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl FromSource for StructField {
|
|
|
|
type Ast = FieldSource;
|
|
|
|
fn from_source(db: &(impl DefDatabase + AstDatabase), src: Source<Self::Ast>) -> Option<Self> {
|
2019-11-20 00:40:36 -06:00
|
|
|
let variant_def: VariantDef = match src.value {
|
2019-09-16 05:48:54 -05:00
|
|
|
FieldSource::Named(ref field) => {
|
2019-11-20 00:40:36 -06:00
|
|
|
let value = field.syntax().ancestors().find_map(ast::StructDef::cast)?;
|
|
|
|
let src = Source { file_id: src.file_id, value };
|
2019-09-16 05:48:54 -05:00
|
|
|
let def = Struct::from_source(db, src)?;
|
|
|
|
VariantDef::from(def)
|
|
|
|
}
|
|
|
|
FieldSource::Pos(ref field) => {
|
2019-11-20 00:40:36 -06:00
|
|
|
let value = field.syntax().ancestors().find_map(ast::EnumVariant::cast)?;
|
|
|
|
let src = Source { file_id: src.file_id, value };
|
2019-09-16 05:48:54 -05:00
|
|
|
let def = EnumVariant::from_source(db, src)?;
|
|
|
|
VariantDef::from(def)
|
|
|
|
}
|
|
|
|
};
|
|
|
|
variant_def
|
|
|
|
.variant_data(db)
|
|
|
|
.fields()
|
|
|
|
.into_iter()
|
|
|
|
.flat_map(|it| it.iter())
|
2019-09-23 13:31:30 -05:00
|
|
|
.map(|(id, _)| StructField { parent: variant_def, id })
|
2019-09-16 05:48:54 -05:00
|
|
|
.find(|f| f.source(db) == src)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-09 15:32:00 -06:00
|
|
|
impl Local {
|
|
|
|
pub fn from_source(db: &impl HirDatabase, src: Source<ast::BindPat>) -> Option<Self> {
|
|
|
|
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-20 00:40:36 -06:00
|
|
|
ast::ConstDef(value) => { Const::from_source(db, Source { value, file_id})?.into() },
|
|
|
|
ast::StaticDef(value) => { Static::from_source(db, Source { value, file_id})?.into() },
|
|
|
|
ast::FnDef(value) => { Function::from_source(db, Source { value, file_id})?.into() },
|
2019-11-09 15:32:00 -06:00
|
|
|
_ => return None,
|
|
|
|
}
|
|
|
|
};
|
|
|
|
Some(res)
|
|
|
|
})?;
|
2019-11-14 08:37:22 -06:00
|
|
|
let source_map = parent.body_source_map(db);
|
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-09-16 05:48:54 -05:00
|
|
|
impl Module {
|
2019-11-15 01:26:31 -06:00
|
|
|
pub fn from_declaration(db: &impl DefDatabase, src: Source<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-20 00:40:36 -06:00
|
|
|
let src_parent = Source { file_id: src.file_id, value: parent_declaration };
|
2019-11-15 01:26:31 -06:00
|
|
|
Module::from_declaration(db, src_parent)
|
|
|
|
}
|
|
|
|
_ => {
|
|
|
|
let src_parent = Source {
|
|
|
|
file_id: src.file_id,
|
2019-11-20 00:40:36 -06:00
|
|
|
value: ModuleSource::new(db, Some(src.file_id.original_file(db)), None),
|
2019-11-15 01:26:31 -06:00
|
|
|
};
|
|
|
|
Module::from_definition(db, src_parent)
|
|
|
|
}
|
|
|
|
}?;
|
|
|
|
|
2019-11-20 00:40:36 -06:00
|
|
|
let child_name = src.value.name()?;
|
2019-09-16 05:48:54 -05:00
|
|
|
parent_module.child(db, &child_name.as_name())
|
|
|
|
}
|
|
|
|
|
2019-11-15 01:26:31 -06:00
|
|
|
pub fn from_definition(db: &impl DefDatabase, src: Source<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-20 00:40:36 -06:00
|
|
|
Source { 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-11-15 01:26:31 -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-15 01:26:31 -06:00
|
|
|
let (krate, module_id) =
|
|
|
|
db.relevant_crates(original_file).iter().find_map(|&crate_id| {
|
|
|
|
let crate_def_map = db.crate_def_map(crate_id);
|
|
|
|
let local_module_id = crate_def_map.modules_for_file(original_file).next()?;
|
|
|
|
Some((crate_id, local_module_id))
|
|
|
|
})?;
|
|
|
|
Some(Module { id: ModuleId { krate, module_id } })
|
2019-09-16 05:48:54 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn from_source<N, DEF>(db: &(impl DefDatabase + AstDatabase), src: Source<N>) -> Option<DEF>
|
|
|
|
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()));
|
|
|
|
let module = Module::from_definition(db, Source::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 {
|
|
|
|
fn find(db: &impl DefDatabase, src: Source<&SyntaxNode>) -> Option<Container> {
|
|
|
|
// 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-11-20 07:03:59 -06:00
|
|
|
/// XXX: AST Nodes and SyntaxNodes have identity equality semantics: nodes are
|
|
|
|
/// equal if they point to exactly the same object.
|
|
|
|
///
|
|
|
|
/// In general, we do not guarantee that we have exactly one instance of a
|
2019-11-20 09:00:01 -06:00
|
|
|
/// syntax tree for each file. We probably should add such guarantee, but, for
|
2019-11-20 07:03:59 -06:00
|
|
|
/// the time being, we will use identity-less AstPtr comparison.
|
|
|
|
fn same_source<N: AstNode>(s1: &Source<N>, s2: &Source<N>) -> bool {
|
|
|
|
s1.as_ref().map(AstPtr::new) == s2.as_ref().map(AstPtr::new)
|
|
|
|
}
|