2018-11-28 00:42:26 +00:00
|
|
|
use std::{
|
|
|
|
sync::Arc,
|
|
|
|
time::Instant,
|
|
|
|
};
|
|
|
|
|
|
|
|
use rustc_hash::FxHashMap;
|
|
|
|
use ra_syntax::{
|
2019-01-01 20:21:16 +00:00
|
|
|
AstNode, SyntaxNode,
|
2019-01-06 14:44:50 +00:00
|
|
|
ast::{self, ModuleItemOwner}
|
2018-11-28 00:42:26 +00:00
|
|
|
};
|
2019-01-01 20:21:16 +00:00
|
|
|
use ra_db::{SourceRootId, Cancelable,};
|
2018-11-28 00:42:26 +00:00
|
|
|
|
|
|
|
use crate::{
|
2019-01-06 16:58:10 +00:00
|
|
|
SourceFileItems, SourceItemId, DefKind, DefId, HirFileId, ModuleSource,
|
2019-01-01 21:37:36 +00:00
|
|
|
MacroCallLoc,
|
2018-12-04 20:44:00 +00:00
|
|
|
db::HirDatabase,
|
2018-12-27 20:51:44 +00:00
|
|
|
function::FnScopes,
|
2019-01-06 16:58:10 +00:00
|
|
|
module_tree::ModuleId,
|
2019-01-06 14:33:27 +00:00
|
|
|
nameres::{InputModuleItems, ItemMap, Resolver},
|
2018-12-24 18:07:48 +00:00
|
|
|
adt::{StructData, EnumData},
|
2018-11-28 00:42:26 +00:00
|
|
|
};
|
|
|
|
|
2019-01-05 21:37:59 +00:00
|
|
|
pub(super) fn fn_scopes(db: &impl HirDatabase, def_id: DefId) -> Cancelable<Arc<FnScopes>> {
|
|
|
|
let body = db.body_hir(def_id)?;
|
|
|
|
let res = FnScopes::new(body);
|
|
|
|
Ok(Arc::new(res))
|
2018-11-28 00:42:26 +00:00
|
|
|
}
|
|
|
|
|
2018-12-24 18:07:48 +00:00
|
|
|
pub(super) fn struct_data(db: &impl HirDatabase, def_id: DefId) -> Cancelable<Arc<StructData>> {
|
|
|
|
let def_loc = def_id.loc(db);
|
|
|
|
assert!(def_loc.kind == DefKind::Struct);
|
|
|
|
let syntax = db.file_item(def_loc.source_item_id);
|
|
|
|
let struct_def =
|
|
|
|
ast::StructDef::cast(syntax.borrowed()).expect("struct def should point to StructDef node");
|
2018-12-25 20:40:33 +00:00
|
|
|
Ok(Arc::new(StructData::new(struct_def.borrowed())))
|
2018-12-24 18:07:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub(super) fn enum_data(db: &impl HirDatabase, def_id: DefId) -> Cancelable<Arc<EnumData>> {
|
|
|
|
let def_loc = def_id.loc(db);
|
|
|
|
assert!(def_loc.kind == DefKind::Enum);
|
|
|
|
let syntax = db.file_item(def_loc.source_item_id);
|
|
|
|
let enum_def =
|
|
|
|
ast::EnumDef::cast(syntax.borrowed()).expect("enum def should point to EnumDef node");
|
2018-12-25 20:40:33 +00:00
|
|
|
Ok(Arc::new(EnumData::new(enum_def.borrowed())))
|
2018-12-24 18:07:48 +00:00
|
|
|
}
|
|
|
|
|
2019-01-01 20:21:16 +00:00
|
|
|
pub(super) fn file_items(db: &impl HirDatabase, file_id: HirFileId) -> Arc<SourceFileItems> {
|
|
|
|
let source_file = db.hir_source_file(file_id);
|
2018-11-28 00:42:26 +00:00
|
|
|
let source_file = source_file.borrowed();
|
2019-01-01 20:21:16 +00:00
|
|
|
let res = SourceFileItems::new(file_id, source_file);
|
2018-11-28 00:42:26 +00:00
|
|
|
Arc::new(res)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub(super) fn file_item(db: &impl HirDatabase, source_item_id: SourceItemId) -> SyntaxNode {
|
2018-12-18 22:48:46 +00:00
|
|
|
match source_item_id.item_id {
|
2019-01-01 20:21:16 +00:00
|
|
|
Some(id) => db.file_items(source_item_id.file_id)[id].clone(),
|
|
|
|
None => db.hir_source_file(source_item_id.file_id).syntax().owned(),
|
2018-12-18 22:48:46 +00:00
|
|
|
}
|
2018-11-28 00:42:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub(super) fn input_module_items(
|
|
|
|
db: &impl HirDatabase,
|
2019-01-01 18:52:07 +00:00
|
|
|
source_root_id: SourceRootId,
|
2018-11-28 00:42:26 +00:00
|
|
|
module_id: ModuleId,
|
|
|
|
) -> Cancelable<Arc<InputModuleItems>> {
|
2019-01-01 18:52:07 +00:00
|
|
|
let module_tree = db.module_tree(source_root_id)?;
|
2018-11-28 00:42:26 +00:00
|
|
|
let source = module_id.source(&module_tree);
|
2019-01-06 16:58:10 +00:00
|
|
|
let file_id = source.file_id;
|
|
|
|
let source = ModuleSource::from_source_item_id(db, source);
|
2019-01-01 20:21:16 +00:00
|
|
|
let file_items = db.file_items(file_id);
|
2019-01-01 18:52:07 +00:00
|
|
|
let fill = |acc: &mut InputModuleItems, items: &mut Iterator<Item = ast::ItemOrMacro>| {
|
|
|
|
for item in items {
|
|
|
|
match item {
|
|
|
|
ast::ItemOrMacro::Item(it) => {
|
2019-01-01 20:21:16 +00:00
|
|
|
acc.add_item(file_id, &file_items, it);
|
2019-01-01 18:52:07 +00:00
|
|
|
}
|
|
|
|
ast::ItemOrMacro::Macro(macro_call) => {
|
|
|
|
let item_id = file_items.id_of_unchecked(macro_call.syntax());
|
|
|
|
let loc = MacroCallLoc {
|
|
|
|
source_root_id,
|
|
|
|
module_id,
|
|
|
|
source_item_id: SourceItemId {
|
2019-01-01 20:21:16 +00:00
|
|
|
file_id,
|
2019-01-01 18:52:07 +00:00
|
|
|
item_id: Some(item_id),
|
|
|
|
},
|
|
|
|
};
|
|
|
|
let id = loc.id(db);
|
2019-01-01 20:21:16 +00:00
|
|
|
let file_id = HirFileId::from(id);
|
|
|
|
let file_items = db.file_items(file_id);
|
2019-01-01 18:52:07 +00:00
|
|
|
//FIXME: expand recursively
|
2019-01-01 20:21:16 +00:00
|
|
|
for item in db.hir_source_file(file_id).borrowed().items() {
|
|
|
|
acc.add_item(file_id, &file_items, item);
|
2019-01-01 18:52:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-11-28 00:42:26 +00:00
|
|
|
}
|
2019-01-01 18:52:07 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
let mut res = InputModuleItems::default();
|
2019-01-06 16:58:10 +00:00
|
|
|
match source {
|
|
|
|
ModuleSource::SourceFile(it) => fill(&mut res, &mut it.borrowed().items_with_macros()),
|
|
|
|
ModuleSource::Module(it) => {
|
2019-01-01 18:52:07 +00:00
|
|
|
if let Some(item_list) = it.borrowed().item_list() {
|
|
|
|
fill(&mut res, &mut item_list.items_with_macros())
|
|
|
|
}
|
2018-11-28 00:42:26 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
Ok(Arc::new(res))
|
|
|
|
}
|
|
|
|
|
|
|
|
pub(super) fn item_map(
|
|
|
|
db: &impl HirDatabase,
|
|
|
|
source_root: SourceRootId,
|
|
|
|
) -> Cancelable<Arc<ItemMap>> {
|
|
|
|
let start = Instant::now();
|
|
|
|
let module_tree = db.module_tree(source_root)?;
|
|
|
|
let input = module_tree
|
|
|
|
.modules()
|
|
|
|
.map(|id| {
|
|
|
|
let items = db.input_module_items(source_root, id)?;
|
|
|
|
Ok((id, items))
|
|
|
|
})
|
|
|
|
.collect::<Cancelable<FxHashMap<_, _>>>()?;
|
2018-12-09 09:24:52 +00:00
|
|
|
|
|
|
|
let resolver = Resolver::new(db, &input, source_root, module_tree);
|
2018-11-28 00:42:26 +00:00
|
|
|
let res = resolver.resolve()?;
|
|
|
|
let elapsed = start.elapsed();
|
|
|
|
log::info!("item_map: {:?}", elapsed);
|
|
|
|
Ok(Arc::new(res))
|
|
|
|
}
|