2018-12-28 07:34:00 -06:00
|
|
|
use std::sync::Arc;
|
|
|
|
use rustc_hash::FxHashMap;
|
|
|
|
|
2019-01-28 08:26:32 -06:00
|
|
|
use ra_arena::{Arena, RawId, impl_arena_id, map::ArenaMap};
|
|
|
|
use ra_syntax::{
|
|
|
|
AstPtr, SourceFile, TreeArc,
|
2019-03-14 05:14:54 -05:00
|
|
|
ast::{self, AstNode}
|
|
|
|
};
|
2018-12-28 07:34:00 -06:00
|
|
|
|
|
|
|
use crate::{
|
2019-02-24 14:36:49 -06:00
|
|
|
Const, TypeAlias, Function, HirFileId,
|
2019-04-14 06:07:45 -05:00
|
|
|
HirDatabase, DefDatabase, TraitRef,
|
2018-12-28 07:34:00 -06:00
|
|
|
type_ref::TypeRef,
|
2019-01-24 15:26:54 -06:00
|
|
|
ids::LocationCtx,
|
2019-01-23 16:08:41 -06:00
|
|
|
resolve::Resolver,
|
2019-04-14 06:07:45 -05:00
|
|
|
ty::Ty,
|
|
|
|
generics::HasGenericParams,
|
|
|
|
code_model_api::{Module, ModuleSource}
|
2018-12-28 07:34:00 -06:00
|
|
|
};
|
|
|
|
|
2019-01-28 08:26:32 -06:00
|
|
|
#[derive(Debug, Default, PartialEq, Eq)]
|
|
|
|
pub struct ImplSourceMap {
|
|
|
|
map: ArenaMap<ImplId, AstPtr<ast::ImplBlock>>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ImplSourceMap {
|
|
|
|
fn insert(&mut self, impl_id: ImplId, impl_block: &ast::ImplBlock) {
|
|
|
|
self.map.insert(impl_id, AstPtr::new(impl_block))
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn get(&self, source: &ModuleSource, impl_id: ImplId) -> TreeArc<ast::ImplBlock> {
|
|
|
|
let file = match source {
|
|
|
|
ModuleSource::SourceFile(file) => &*file,
|
|
|
|
ModuleSource::Module(m) => m.syntax().ancestors().find_map(SourceFile::cast).unwrap(),
|
|
|
|
};
|
|
|
|
|
|
|
|
self.map[impl_id].to_node(file).to_owned()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-16 14:19:24 -06:00
|
|
|
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
|
2018-12-28 07:34:00 -06:00
|
|
|
pub struct ImplBlock {
|
2019-02-16 14:09:58 -06:00
|
|
|
module: Module,
|
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>,
|
2019-01-24 06:28:50 -06:00
|
|
|
item: ImplItem,
|
2018-12-28 07:34:00 -06:00
|
|
|
) -> Option<ImplBlock> {
|
2019-01-24 06:28:50 -06:00
|
|
|
let impl_id = *module_impl_blocks.impls_by_def.get(&item)?;
|
2019-02-16 14:09:58 -06:00
|
|
|
Some(ImplBlock { module: module_impl_blocks.module, impl_id })
|
2018-12-28 07:34:00 -06:00
|
|
|
}
|
|
|
|
|
2019-02-16 14:09:58 -06:00
|
|
|
pub(crate) fn from_id(module: Module, impl_id: ImplId) -> ImplBlock {
|
|
|
|
ImplBlock { module, impl_id }
|
2019-01-07 06:44:54 -06:00
|
|
|
}
|
|
|
|
|
2019-02-16 14:09:58 -06:00
|
|
|
/// Returns the syntax of the impl block
|
2019-03-23 07:37:04 -05:00
|
|
|
pub fn source(&self, db: &impl DefDatabase) -> (HirFileId, TreeArc<ast::ImplBlock>) {
|
2019-02-16 14:09:58 -06:00
|
|
|
let source_map = db.impls_in_module_source_map(self.module);
|
|
|
|
let (file_id, source) = self.module.definition_source(db);
|
|
|
|
(file_id, source_map.get(&source, self.impl_id))
|
2019-01-28 08:26:32 -06:00
|
|
|
}
|
|
|
|
|
2019-02-16 14:09:58 -06:00
|
|
|
pub fn id(&self) -> ImplId {
|
|
|
|
self.impl_id
|
2018-12-28 07:34:00 -06:00
|
|
|
}
|
|
|
|
|
2019-01-23 16:08:41 -06:00
|
|
|
pub fn module(&self) -> Module {
|
2019-02-16 14:09:58 -06:00
|
|
|
self.module
|
2019-01-23 16:08:41 -06:00
|
|
|
}
|
|
|
|
|
2019-03-26 17:07:26 -05:00
|
|
|
pub fn target_trait(&self, db: &impl DefDatabase) -> Option<TypeRef> {
|
2019-02-16 14:09:58 -06:00
|
|
|
db.impls_in_module(self.module).impls[self.impl_id].target_trait().cloned()
|
2018-12-30 12:59:49 -06:00
|
|
|
}
|
|
|
|
|
2019-03-23 07:37:04 -05:00
|
|
|
pub fn target_type(&self, db: &impl DefDatabase) -> TypeRef {
|
2019-02-16 14:09:58 -06:00
|
|
|
db.impls_in_module(self.module).impls[self.impl_id].target_type().clone()
|
2018-12-28 07:34:00 -06:00
|
|
|
}
|
|
|
|
|
2019-01-26 15:52:04 -06:00
|
|
|
pub fn target_ty(&self, db: &impl HirDatabase) -> Ty {
|
2019-02-16 14:09:58 -06:00
|
|
|
Ty::from_hir(db, &self.resolver(db), &self.target_type(db))
|
2019-01-26 15:52:04 -06:00
|
|
|
}
|
|
|
|
|
2019-03-26 17:07:26 -05:00
|
|
|
pub fn target_trait_ref(&self, db: &impl HirDatabase) -> Option<TraitRef> {
|
2019-03-31 13:02:16 -05:00
|
|
|
let target_ty = self.target_ty(db);
|
|
|
|
TraitRef::from_hir(db, &self.resolver(db), &self.target_trait(db)?, Some(target_ty))
|
2019-01-26 15:52:04 -06:00
|
|
|
}
|
|
|
|
|
2019-03-23 07:37:04 -05:00
|
|
|
pub fn items(&self, db: &impl DefDatabase) -> Vec<ImplItem> {
|
2019-02-16 14:09:58 -06:00
|
|
|
db.impls_in_module(self.module).impls[self.impl_id].items().to_vec()
|
2018-12-28 07:34:00 -06:00
|
|
|
}
|
2019-01-23 16:08:41 -06:00
|
|
|
|
2019-04-14 04:15:11 -05:00
|
|
|
pub(crate) fn resolver(&self, db: &impl DefDatabase) -> Resolver {
|
2019-01-23 16:08:41 -06:00
|
|
|
let r = self.module().resolver(db);
|
2019-02-16 14:21:36 -06:00
|
|
|
// add generic params, if present
|
|
|
|
let p = self.generic_params(db);
|
|
|
|
let r = if !p.params.is_empty() { r.push_generic_params_scope(p) } else { r };
|
2019-01-23 16:08:41 -06:00
|
|
|
let r = r.push_impl_block_scope(self.clone());
|
|
|
|
r
|
|
|
|
}
|
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(
|
2019-03-23 07:37:04 -05:00
|
|
|
db: &impl DefDatabase,
|
2019-01-23 14:14:13 -06:00
|
|
|
file_id: HirFileId,
|
|
|
|
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-24 15:26:54 -06:00
|
|
|
let ctx = LocationCtx::new(db, module, file_id);
|
2018-12-28 07:34:00 -06:00
|
|
|
let items = if let Some(item_list) = node.item_list() {
|
|
|
|
item_list
|
|
|
|
.impl_items()
|
2019-01-24 16:38:21 -06:00
|
|
|
.map(|item_node| match item_node.kind() {
|
2019-02-24 14:36:49 -06:00
|
|
|
ast::ImplItemKind::FnDef(it) => Function { id: ctx.to_def(it) }.into(),
|
|
|
|
ast::ImplItemKind::ConstDef(it) => Const { id: ctx.to_def(it) }.into(),
|
2019-02-25 04:38:52 -06:00
|
|
|
ast::ImplItemKind::TypeAliasDef(it) => TypeAlias { id: ctx.to_def(it) }.into(),
|
2018-12-28 07:34:00 -06:00
|
|
|
})
|
|
|
|
.collect()
|
|
|
|
} else {
|
|
|
|
Vec::new()
|
|
|
|
};
|
2019-02-08 05:49:43 -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
|
|
|
}
|
|
|
|
|
2019-01-24 06:28:50 -06:00
|
|
|
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
|
2019-03-23 02:53:48 -05:00
|
|
|
//FIXME: rename to ImplDef?
|
2018-12-28 07:34:00 -06:00
|
|
|
pub enum ImplItem {
|
|
|
|
Method(Function),
|
2019-01-24 16:38:21 -06:00
|
|
|
Const(Const),
|
2019-02-24 14:36:49 -06:00
|
|
|
TypeAlias(TypeAlias),
|
2018-12-28 07:34:00 -06:00
|
|
|
// Existential
|
|
|
|
}
|
2019-02-24 14:36:49 -06:00
|
|
|
impl_froms!(ImplItem: Const, TypeAlias);
|
2018-12-28 07:34:00 -06:00
|
|
|
|
2019-01-24 06:28:50 -06:00
|
|
|
impl From<Function> for ImplItem {
|
|
|
|
fn from(func: Function) -> ImplItem {
|
|
|
|
ImplItem::Method(func)
|
2018-12-28 07:34:00 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[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-02-16 14:09:58 -06:00
|
|
|
pub(crate) module: Module,
|
2019-01-07 06:44:54 -06:00
|
|
|
pub(crate) impls: Arena<ImplId, ImplData>,
|
2019-01-24 06:28:50 -06:00
|
|
|
impls_by_def: FxHashMap<ImplItem, ImplId>,
|
2018-12-28 07:34:00 -06:00
|
|
|
}
|
|
|
|
|
2019-01-04 12:29:53 -06:00
|
|
|
impl ModuleImplBlocks {
|
2019-03-23 07:37:04 -05:00
|
|
|
fn collect(db: &impl DefDatabase, module: Module, source_map: &mut ImplSourceMap) -> Self {
|
2019-01-23 16:08:41 -06:00
|
|
|
let mut m = ModuleImplBlocks {
|
|
|
|
module,
|
|
|
|
impls: Arena::default(),
|
|
|
|
impls_by_def: FxHashMap::default(),
|
|
|
|
};
|
|
|
|
|
|
|
|
let (file_id, module_source) = m.module.definition_source(db);
|
2019-01-23 14:14:13 -06:00
|
|
|
let file_id: HirFileId = file_id.into();
|
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-02-08 05:49:43 -06:00
|
|
|
ModuleSource::Module(node) => {
|
|
|
|
node.item_list().expect("inline module should have item list").syntax()
|
|
|
|
}
|
2018-12-28 07:34:00 -06:00
|
|
|
};
|
|
|
|
|
|
|
|
for impl_block_ast in node.children().filter_map(ast::ImplBlock::cast) {
|
2019-01-23 16:08:41 -06:00
|
|
|
let impl_block = ImplData::from_ast(db, file_id, m.module, impl_block_ast);
|
|
|
|
let id = m.impls.alloc(impl_block);
|
|
|
|
for &impl_item in &m.impls[id].items {
|
|
|
|
m.impls_by_def.insert(impl_item, id);
|
2018-12-28 07:34:00 -06:00
|
|
|
}
|
2019-01-28 08:26:32 -06:00
|
|
|
|
|
|
|
source_map.insert(id, impl_block_ast);
|
2018-12-28 07:34:00 -06:00
|
|
|
}
|
2019-01-23 16:08:41 -06:00
|
|
|
|
|
|
|
m
|
2018-12-28 07:34:00 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-28 08:26:32 -06:00
|
|
|
pub(crate) fn impls_in_module_with_source_map_query(
|
2019-03-23 07:37:04 -05:00
|
|
|
db: &impl DefDatabase,
|
2019-01-28 08:26:32 -06:00
|
|
|
module: Module,
|
|
|
|
) -> (Arc<ModuleImplBlocks>, Arc<ImplSourceMap>) {
|
|
|
|
let mut source_map = ImplSourceMap::default();
|
|
|
|
|
2019-01-23 16:08:41 -06:00
|
|
|
let result = ModuleImplBlocks::collect(db, module, &mut source_map);
|
2019-01-28 08:26:32 -06:00
|
|
|
|
|
|
|
(Arc::new(result), Arc::new(source_map))
|
|
|
|
}
|
|
|
|
|
2019-03-23 07:37:04 -05:00
|
|
|
pub(crate) fn impls_in_module(db: &impl DefDatabase, module: Module) -> Arc<ModuleImplBlocks> {
|
2019-01-28 08:26:32 -06:00
|
|
|
db.impls_in_module_with_source_map(module).0
|
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) fn impls_in_module_source_map_query(
|
2019-03-23 07:37:04 -05:00
|
|
|
db: &impl DefDatabase,
|
2019-01-28 08:26:32 -06:00
|
|
|
module: Module,
|
|
|
|
) -> Arc<ImplSourceMap> {
|
|
|
|
db.impls_in_module_with_source_map(module).1
|
2018-12-28 07:34:00 -06:00
|
|
|
}
|