2018-12-28 07:34:00 -06:00
|
|
|
use std::sync::Arc;
|
|
|
|
use rustc_hash::FxHashMap;
|
|
|
|
|
|
|
|
use ra_arena::{Arena, RawId, impl_arena_id};
|
|
|
|
use ra_syntax::ast::{self, AstNode};
|
2019-01-04 12:29:53 -06:00
|
|
|
use ra_db::{LocationIntener, Cancelable, SourceRootId};
|
2018-12-28 07:34:00 -06:00
|
|
|
|
|
|
|
use crate::{
|
2019-01-04 12:29:53 -06:00
|
|
|
DefId, DefLoc, DefKind, SourceItemId, SourceFileItems,
|
2019-01-06 06:16:21 -06:00
|
|
|
Function,
|
2018-12-28 07:34:00 -06:00
|
|
|
db::HirDatabase,
|
|
|
|
type_ref::TypeRef,
|
2019-01-06 08:33:27 -06:00
|
|
|
module_tree::ModuleId,
|
2018-12-28 07:34:00 -06:00
|
|
|
};
|
|
|
|
|
2019-01-06 06:58:45 -06:00
|
|
|
use crate::code_model_api::{Module, ModuleSource};
|
2019-01-06 06:16:21 -06:00
|
|
|
|
2018-12-28 07:34:00 -06:00
|
|
|
#[derive(Debug, Clone, PartialEq, Eq)]
|
|
|
|
pub struct ImplBlock {
|
2019-01-04 12:29:53 -06:00
|
|
|
module_impl_blocks: Arc<ModuleImplBlocks>,
|
2018-12-28 07:34:00 -06:00
|
|
|
impl_id: ImplId,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ImplBlock {
|
|
|
|
pub(crate) fn containing(
|
2019-01-04 12:29:53 -06:00
|
|
|
module_impl_blocks: Arc<ModuleImplBlocks>,
|
2018-12-28 07:34:00 -06:00
|
|
|
def_id: DefId,
|
|
|
|
) -> Option<ImplBlock> {
|
2019-01-04 12:29:53 -06:00
|
|
|
let impl_id = *module_impl_blocks.impls_by_def.get(&def_id)?;
|
2018-12-28 07:34:00 -06:00
|
|
|
Some(ImplBlock {
|
2019-01-04 12:29:53 -06:00
|
|
|
module_impl_blocks,
|
2018-12-28 07:34:00 -06:00
|
|
|
impl_id,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-01-07 06:44:54 -06:00
|
|
|
pub(crate) fn from_id(module_impl_blocks: Arc<ModuleImplBlocks>, impl_id: ImplId) -> ImplBlock {
|
|
|
|
ImplBlock {
|
|
|
|
module_impl_blocks,
|
|
|
|
impl_id,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-28 07:34:00 -06:00
|
|
|
fn impl_data(&self) -> &ImplData {
|
2019-01-04 12:29:53 -06:00
|
|
|
&self.module_impl_blocks.impls[self.impl_id]
|
2018-12-28 07:34:00 -06:00
|
|
|
}
|
|
|
|
|
2018-12-30 12:59:49 -06:00
|
|
|
pub fn target_trait(&self) -> Option<&TypeRef> {
|
2019-01-07 06:44:54 -06:00
|
|
|
self.impl_data().target_trait()
|
2018-12-30 12:59:49 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn target_type(&self) -> &TypeRef {
|
2019-01-07 06:44:54 -06:00
|
|
|
self.impl_data().target_type()
|
2018-12-28 07:34:00 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn items(&self) -> &[ImplItem] {
|
2019-01-07 06:44:54 -06:00
|
|
|
self.impl_data().items()
|
2018-12-28 07:34:00 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, Clone, PartialEq, Eq)]
|
|
|
|
pub struct ImplData {
|
2018-12-30 12:59:49 -06:00
|
|
|
target_trait: Option<TypeRef>,
|
|
|
|
target_type: TypeRef,
|
2018-12-28 07:34:00 -06:00
|
|
|
items: Vec<ImplItem>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ImplData {
|
|
|
|
pub(crate) fn from_ast(
|
|
|
|
db: &impl AsRef<LocationIntener<DefLoc, DefId>>,
|
|
|
|
file_items: &SourceFileItems,
|
|
|
|
module: &Module,
|
2019-01-08 02:28:42 -06:00
|
|
|
node: &ast::ImplBlock,
|
2018-12-28 07:34:00 -06:00
|
|
|
) -> Self {
|
2019-01-07 06:44:54 -06:00
|
|
|
let target_trait = node.target_trait().map(TypeRef::from_ast);
|
2018-12-30 12:59:49 -06:00
|
|
|
let target_type = TypeRef::from_ast_opt(node.target_type());
|
2019-01-06 06:16:21 -06:00
|
|
|
let module_loc = module.def_id.loc(db);
|
2018-12-28 07:34:00 -06:00
|
|
|
let items = if let Some(item_list) = node.item_list() {
|
|
|
|
item_list
|
|
|
|
.impl_items()
|
|
|
|
.map(|item_node| {
|
2019-01-08 02:28:42 -06:00
|
|
|
let kind = match item_node.kind() {
|
|
|
|
ast::ImplItemKind::FnDef(..) => DefKind::Function,
|
|
|
|
ast::ImplItemKind::ConstDef(..) => DefKind::Item,
|
|
|
|
ast::ImplItemKind::TypeDef(..) => DefKind::Item,
|
2018-12-28 07:34:00 -06:00
|
|
|
};
|
|
|
|
let item_id = file_items.id_of_unchecked(item_node.syntax());
|
2019-01-06 06:16:21 -06:00
|
|
|
let source_item_id = SourceItemId {
|
|
|
|
file_id: module_loc.source_item_id.file_id,
|
|
|
|
item_id: Some(item_id),
|
|
|
|
};
|
2018-12-28 07:34:00 -06:00
|
|
|
let def_loc = DefLoc {
|
|
|
|
kind,
|
2019-01-06 06:16:21 -06:00
|
|
|
source_item_id,
|
|
|
|
..module_loc
|
2018-12-28 07:34:00 -06:00
|
|
|
};
|
|
|
|
let def_id = def_loc.id(db);
|
2019-01-08 02:28:42 -06:00
|
|
|
match item_node.kind() {
|
|
|
|
ast::ImplItemKind::FnDef(..) => ImplItem::Method(Function::new(def_id)),
|
|
|
|
ast::ImplItemKind::ConstDef(..) => ImplItem::Const(def_id),
|
|
|
|
ast::ImplItemKind::TypeDef(..) => ImplItem::Type(def_id),
|
2018-12-28 07:34:00 -06:00
|
|
|
}
|
|
|
|
})
|
|
|
|
.collect()
|
|
|
|
} else {
|
|
|
|
Vec::new()
|
|
|
|
};
|
2018-12-30 12:59:49 -06:00
|
|
|
ImplData {
|
|
|
|
target_trait,
|
|
|
|
target_type,
|
|
|
|
items,
|
|
|
|
}
|
2018-12-28 07:34:00 -06:00
|
|
|
}
|
2019-01-07 06:44:54 -06:00
|
|
|
|
|
|
|
pub fn target_trait(&self) -> Option<&TypeRef> {
|
|
|
|
self.target_trait.as_ref()
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn target_type(&self) -> &TypeRef {
|
|
|
|
&self.target_type
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn items(&self) -> &[ImplItem] {
|
|
|
|
&self.items
|
|
|
|
}
|
2018-12-28 07:34:00 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, Clone, PartialEq, Eq)]
|
|
|
|
pub enum ImplItem {
|
|
|
|
Method(Function),
|
|
|
|
// these don't have their own types yet
|
|
|
|
Const(DefId),
|
|
|
|
Type(DefId),
|
|
|
|
// Existential
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ImplItem {
|
|
|
|
pub fn def_id(&self) -> DefId {
|
|
|
|
match self {
|
|
|
|
ImplItem::Method(f) => f.def_id(),
|
|
|
|
ImplItem::Const(def_id) => *def_id,
|
|
|
|
ImplItem::Type(def_id) => *def_id,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
|
|
|
|
pub struct ImplId(pub RawId);
|
|
|
|
impl_arena_id!(ImplId);
|
|
|
|
|
2019-01-08 17:47:12 -06:00
|
|
|
/// The collection of impl blocks is a two-step process: first we collect the
|
|
|
|
/// blocks per-module; then we build an index of all impl blocks in the crate.
|
|
|
|
/// This way, we avoid having to do this process for the whole crate whenever
|
|
|
|
/// a file is changed; as long as the impl blocks in the file don't change,
|
|
|
|
/// we don't need to do the second step again.
|
2018-12-28 07:34:00 -06:00
|
|
|
#[derive(Debug, PartialEq, Eq)]
|
2019-01-04 12:29:53 -06:00
|
|
|
pub struct ModuleImplBlocks {
|
2019-01-07 06:44:54 -06:00
|
|
|
pub(crate) impls: Arena<ImplId, ImplData>,
|
2018-12-28 07:34:00 -06:00
|
|
|
impls_by_def: FxHashMap<DefId, ImplId>,
|
|
|
|
}
|
|
|
|
|
2019-01-04 12:29:53 -06:00
|
|
|
impl ModuleImplBlocks {
|
2018-12-28 07:34:00 -06:00
|
|
|
fn new() -> Self {
|
2019-01-04 12:29:53 -06:00
|
|
|
ModuleImplBlocks {
|
2018-12-28 07:34:00 -06:00
|
|
|
impls: Arena::default(),
|
|
|
|
impls_by_def: FxHashMap::default(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn collect(&mut self, db: &impl HirDatabase, module: Module) -> Cancelable<()> {
|
2019-01-08 16:38:51 -06:00
|
|
|
let (file_id, module_source) = module.definition_source(db)?;
|
2019-01-06 06:58:45 -06:00
|
|
|
let node = match &module_source {
|
2019-01-08 02:28:42 -06:00
|
|
|
ModuleSource::SourceFile(node) => node.syntax(),
|
2019-01-07 06:44:54 -06:00
|
|
|
ModuleSource::Module(node) => match node.item_list() {
|
|
|
|
Some(item_list) => item_list.syntax(),
|
|
|
|
None => return Ok(()),
|
|
|
|
},
|
2018-12-28 07:34:00 -06:00
|
|
|
};
|
|
|
|
|
2019-01-06 06:58:45 -06:00
|
|
|
let source_file_items = db.file_items(file_id.into());
|
2018-12-28 07:34:00 -06:00
|
|
|
|
|
|
|
for impl_block_ast in node.children().filter_map(ast::ImplBlock::cast) {
|
|
|
|
let impl_block = ImplData::from_ast(db, &source_file_items, &module, impl_block_ast);
|
|
|
|
let id = self.impls.alloc(impl_block);
|
|
|
|
for impl_item in &self.impls[id].items {
|
|
|
|
self.impls_by_def.insert(impl_item.def_id(), id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-04 12:29:53 -06:00
|
|
|
pub(crate) fn impls_in_module(
|
2018-12-28 07:34:00 -06:00
|
|
|
db: &impl HirDatabase,
|
2019-01-04 12:29:53 -06:00
|
|
|
source_root_id: SourceRootId,
|
|
|
|
module_id: ModuleId,
|
|
|
|
) -> Cancelable<Arc<ModuleImplBlocks>> {
|
|
|
|
let mut result = ModuleImplBlocks::new();
|
2019-01-06 06:16:21 -06:00
|
|
|
let module = Module::from_module_id(db, source_root_id, module_id)?;
|
2019-01-04 12:29:53 -06:00
|
|
|
result.collect(db, module)?;
|
2018-12-28 07:34:00 -06:00
|
|
|
Ok(Arc::new(result))
|
|
|
|
}
|