rust/crates/ra_hir/src/from_source.rs

204 lines
7.4 KiB
Rust
Raw Normal View History

//! FIXME: write short doc here
use hir_def::{ModuleId, StructId, StructOrUnionId, UnionId};
2019-10-30 10:56:20 -05:00
use hir_expand::name::AsName;
2019-11-09 15:32:00 -06:00
use ra_syntax::{
ast::{self, AstNode, NameOwner},
match_ast,
};
2019-09-16 05:48:54 -05:00
use crate::{
db::{AstDatabase, DefDatabase, HirDatabase},
ids::{AstItemDef, LocationCtx},
Const, DefWithBody, Enum, EnumVariant, FieldSource, Function, HasBody, HasSource, ImplBlock,
Local, Module, 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> {
let id = from_source(db, src)?;
Some(Function { id })
}
}
impl FromSource for Const {
type Ast = ast::ConstDef;
fn from_source(db: &(impl DefDatabase + AstDatabase), src: Source<Self::Ast>) -> Option<Self> {
let id = from_source(db, src)?;
Some(Const { id })
}
}
impl FromSource for Static {
type Ast = ast::StaticDef;
fn from_source(db: &(impl DefDatabase + AstDatabase), src: Source<Self::Ast>) -> Option<Self> {
let id = from_source(db, src)?;
Some(Static { id })
}
}
impl FromSource for TypeAlias {
type Ast = ast::TypeAliasDef;
fn from_source(db: &(impl DefDatabase + AstDatabase), src: Source<Self::Ast>) -> Option<Self> {
let id = from_source(db, src)?;
Some(TypeAlias { id })
}
}
// FIXME: add impl FromSource for MacroDef
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> {
let parent_enum = src.ast.parent_enum();
let src_enum = Source { file_id: src.file_id, ast: parent_enum };
let variants = Enum::from_source(db, src_enum)?.variants(db);
variants.into_iter().find(|v| v.source(db) == src)
}
}
impl FromSource for StructField {
type Ast = FieldSource;
fn from_source(db: &(impl DefDatabase + AstDatabase), src: Source<Self::Ast>) -> Option<Self> {
let variant_def: VariantDef = match src.ast {
FieldSource::Named(ref field) => {
let ast = field.syntax().ancestors().find_map(ast::StructDef::cast)?;
let src = Source { file_id: src.file_id, ast };
let def = Struct::from_source(db, src)?;
VariantDef::from(def)
}
FieldSource::Pos(ref field) => {
let ast = field.syntax().ancestors().find_map(ast::EnumVariant::cast)?;
let src = Source { file_id: src.file_id, ast };
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;
let parent: DefWithBody = src.ast.syntax().ancestors().find_map(|it| {
let res = match_ast! {
match it {
ast::ConstDef(ast) => { Const::from_source(db, Source { ast, file_id})?.into() },
ast::StaticDef(ast) => { Static::from_source(db, Source { ast, file_id})?.into() },
ast::FnDef(ast) => { Function::from_source(db, Source { ast, file_id})?.into() },
_ => return None,
}
};
Some(res)
})?;
2019-11-14 08:37:22 -06:00
let source_map = parent.body_source_map(db);
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 {
pub fn from_declaration(db: &impl DefDatabase, src: Source<ast::Module>) -> Option<Self> {
let parent_declaration = src.ast.syntax().ancestors().skip(1).find_map(ast::Module::cast);
let parent_module = match parent_declaration {
Some(parent_declaration) => {
let src_parent = Source { file_id: src.file_id, ast: parent_declaration };
Module::from_declaration(db, src_parent)
}
_ => {
let src_parent = Source {
file_id: src.file_id,
ast: ModuleSource::new(db, Some(src.file_id.original_file(db)), None),
};
Module::from_definition(db, src_parent)
}
}?;
2019-09-16 05:48:54 -05:00
let child_name = src.ast.name()?;
parent_module.child(db, &child_name.as_name())
}
pub fn from_definition(db: &impl DefDatabase, src: Source<ModuleSource>) -> Option<Self> {
match src.ast {
2019-09-16 05:48:54 -05:00
ModuleSource::Module(ref module) => {
2019-10-23 03:31:16 -05:00
assert!(!module.has_semi());
return Module::from_declaration(
db,
Source { file_id: src.file_id, ast: module.clone() },
);
2019-09-16 05:48:54 -05:00
}
ModuleSource::SourceFile(_) => (),
2019-09-16 05:48:54 -05:00
};
let original_file = src.file_id.original_file(db);
2019-10-31 10:45:10 -05: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>,
{
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-09-16 05:48:54 -05:00
Some(DEF::from_ast(ctx, &src.ast))
}