rust/crates/ra_hir/src/query_definitions.rs

131 lines
4.5 KiB
Rust
Raw Normal View History

2018-11-28 01:42:26 +01:00
use std::{
sync::Arc,
time::Instant,
};
use rustc_hash::FxHashMap;
use ra_syntax::{
2019-01-01 21:21:16 +01:00
AstNode, SyntaxNode,
ast::{self, ModuleItemOwner}
2018-11-28 01:42:26 +01:00
};
2019-01-01 21:21:16 +01:00
use ra_db::{SourceRootId, Cancelable,};
2018-11-28 01:42:26 +01:00
use crate::{
2019-01-06 17:58:10 +01:00
SourceFileItems, SourceItemId, DefKind, DefId, HirFileId, ModuleSource,
2019-01-01 22:37:36 +01:00
MacroCallLoc,
2018-12-04 21:44:00 +01:00
db::HirDatabase,
2018-12-27 21:51:44 +01:00
function::FnScopes,
2019-01-06 17:58:10 +01:00
module_tree::ModuleId,
2019-01-06 15:33:27 +01:00
nameres::{InputModuleItems, ItemMap, Resolver},
adt::{StructData, EnumData},
2018-11-28 01:42:26 +01: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 01:42:26 +01: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");
Ok(Arc::new(StructData::new(struct_def.borrowed())))
}
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");
Ok(Arc::new(EnumData::new(enum_def.borrowed())))
}
2019-01-01 21:21:16 +01: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 01:42:26 +01:00
let source_file = source_file.borrowed();
2019-01-01 21:21:16 +01:00
let res = SourceFileItems::new(file_id, source_file);
2018-11-28 01:42:26 +01:00
Arc::new(res)
}
pub(super) fn file_item(db: &impl HirDatabase, source_item_id: SourceItemId) -> SyntaxNode {
match source_item_id.item_id {
2019-01-01 21:21:16 +01: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-11-28 01:42:26 +01:00
}
pub(super) fn input_module_items(
db: &impl HirDatabase,
2019-01-01 19:52:07 +01:00
source_root_id: SourceRootId,
2018-11-28 01:42:26 +01:00
module_id: ModuleId,
) -> Cancelable<Arc<InputModuleItems>> {
2019-01-01 19:52:07 +01:00
let module_tree = db.module_tree(source_root_id)?;
2018-11-28 01:42:26 +01:00
let source = module_id.source(&module_tree);
2019-01-06 17:58:10 +01:00
let file_id = source.file_id;
let source = ModuleSource::from_source_item_id(db, source);
2019-01-01 21:21:16 +01:00
let file_items = db.file_items(file_id);
2019-01-01 19:52:07 +01: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 21:21:16 +01:00
acc.add_item(file_id, &file_items, it);
2019-01-01 19:52:07 +01: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 21:21:16 +01:00
file_id,
2019-01-01 19:52:07 +01:00
item_id: Some(item_id),
},
};
let id = loc.id(db);
2019-01-01 21:21:16 +01:00
let file_id = HirFileId::from(id);
let file_items = db.file_items(file_id);
2019-01-01 19:52:07 +01:00
//FIXME: expand recursively
2019-01-01 21:21:16 +01:00
for item in db.hir_source_file(file_id).borrowed().items() {
acc.add_item(file_id, &file_items, item);
2019-01-01 19:52:07 +01:00
}
}
}
2018-11-28 01:42:26 +01:00
}
2019-01-01 19:52:07 +01:00
};
let mut res = InputModuleItems::default();
2019-01-06 17:58:10 +01:00
match source {
ModuleSource::SourceFile(it) => fill(&mut res, &mut it.borrowed().items_with_macros()),
ModuleSource::Module(it) => {
2019-01-01 19:52:07 +01:00
if let Some(item_list) = it.borrowed().item_list() {
fill(&mut res, &mut item_list.items_with_macros())
}
2018-11-28 01:42:26 +01: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 10:24:52 +01:00
let resolver = Resolver::new(db, &input, source_root, module_tree);
2018-11-28 01:42:26 +01:00
let res = resolver.resolve()?;
let elapsed = start.elapsed();
log::info!("item_map: {:?}", elapsed);
Ok(Arc::new(res))
}