2019-03-02 14:59:04 -06:00
|
|
|
use std::{
|
|
|
|
sync::Arc,
|
|
|
|
ops::Index,
|
|
|
|
};
|
|
|
|
|
2019-03-13 08:38:02 -05:00
|
|
|
use test_utils::tested_by;
|
2019-03-16 09:17:50 -05:00
|
|
|
use ra_arena::{Arena, impl_arena_id, RawId, map::ArenaMap};
|
2019-03-02 14:59:04 -06:00
|
|
|
use ra_syntax::{
|
2019-03-16 09:17:50 -05:00
|
|
|
AstNode, SourceFile, AstPtr, TreeArc,
|
2019-03-02 14:59:04 -06:00
|
|
|
ast::{self, NameOwner, AttrsOwner},
|
|
|
|
};
|
|
|
|
|
|
|
|
use crate::{
|
2019-03-23 07:37:04 -05:00
|
|
|
DefDatabase, Name, AsName, Path, HirFileId, ModuleSource,
|
2019-03-26 11:00:11 -05:00
|
|
|
AstIdMap, FileAstId,
|
2019-03-02 14:59:04 -06:00
|
|
|
};
|
|
|
|
|
2019-03-26 05:53:50 -05:00
|
|
|
/// `RawItems` is a set of top-level items in a file (except for impls).
|
|
|
|
///
|
|
|
|
/// It is the input to name resolution algorithm. `RawItems` are not invalidated
|
|
|
|
/// on most edits.
|
2019-03-13 08:38:02 -05:00
|
|
|
#[derive(Debug, Default, PartialEq, Eq)]
|
|
|
|
pub struct RawItems {
|
2019-03-02 14:59:04 -06:00
|
|
|
modules: Arena<Module, ModuleData>,
|
2019-03-13 08:04:28 -05:00
|
|
|
imports: Arena<ImportId, ImportData>,
|
2019-03-02 14:59:04 -06:00
|
|
|
defs: Arena<Def, DefData>,
|
|
|
|
macros: Arena<Macro, MacroData>,
|
|
|
|
/// items for top-level module
|
|
|
|
items: Vec<RawItem>,
|
|
|
|
}
|
|
|
|
|
2019-03-16 09:17:50 -05:00
|
|
|
#[derive(Debug, Default, PartialEq, Eq)]
|
|
|
|
pub struct ImportSourceMap {
|
2019-04-02 05:26:09 -05:00
|
|
|
map: ArenaMap<ImportId, AstPtr<ast::UseTree>>,
|
2019-03-16 09:17:50 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
impl ImportSourceMap {
|
2019-04-02 05:26:09 -05:00
|
|
|
fn insert(&mut self, import: ImportId, use_tree: &ast::UseTree) {
|
|
|
|
self.map.insert(import, AstPtr::new(use_tree))
|
2019-03-16 09:17:50 -05:00
|
|
|
}
|
|
|
|
|
2019-04-02 05:26:09 -05:00
|
|
|
pub(crate) fn get(&self, source: &ModuleSource, import: ImportId) -> TreeArc<ast::UseTree> {
|
2019-03-16 09:17:50 -05:00
|
|
|
let file = match source {
|
|
|
|
ModuleSource::SourceFile(file) => &*file,
|
|
|
|
ModuleSource::Module(m) => m.syntax().ancestors().find_map(SourceFile::cast).unwrap(),
|
|
|
|
};
|
|
|
|
|
|
|
|
self.map[import].to_node(file).to_owned()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-02 14:59:04 -06:00
|
|
|
impl RawItems {
|
2019-03-26 05:09:39 -05:00
|
|
|
pub(crate) fn raw_items_query(db: &impl DefDatabase, file_id: HirFileId) -> Arc<RawItems> {
|
2019-03-14 05:14:54 -05:00
|
|
|
db.raw_items_with_source_map(file_id).0
|
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) fn raw_items_with_source_map_query(
|
2019-03-23 07:37:04 -05:00
|
|
|
db: &impl DefDatabase,
|
2019-03-26 05:09:39 -05:00
|
|
|
file_id: HirFileId,
|
2019-03-14 05:14:54 -05:00
|
|
|
) -> (Arc<RawItems>, Arc<ImportSourceMap>) {
|
2019-03-02 14:59:04 -06:00
|
|
|
let mut collector = RawItemsCollector {
|
|
|
|
raw_items: RawItems::default(),
|
2019-03-26 11:00:11 -05:00
|
|
|
source_ast_id_map: db.ast_id_map(file_id.into()),
|
2019-03-14 05:14:54 -05:00
|
|
|
source_map: ImportSourceMap::default(),
|
2019-03-02 14:59:04 -06:00
|
|
|
};
|
2019-03-26 05:09:39 -05:00
|
|
|
let source_file = db.hir_parse(file_id);
|
2019-03-02 14:59:04 -06:00
|
|
|
collector.process_module(None, &*source_file);
|
2019-03-14 05:14:54 -05:00
|
|
|
(Arc::new(collector.raw_items), Arc::new(collector.source_map))
|
2019-03-13 08:38:02 -05:00
|
|
|
}
|
|
|
|
|
2019-03-26 05:53:50 -05:00
|
|
|
pub(super) fn items(&self) -> &[RawItem] {
|
2019-03-13 08:38:02 -05:00
|
|
|
&self.items
|
2019-03-02 14:59:04 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Index<Module> for RawItems {
|
|
|
|
type Output = ModuleData;
|
|
|
|
fn index(&self, idx: Module) -> &ModuleData {
|
|
|
|
&self.modules[idx]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-13 08:04:28 -05:00
|
|
|
impl Index<ImportId> for RawItems {
|
2019-03-02 14:59:04 -06:00
|
|
|
type Output = ImportData;
|
2019-03-13 08:04:28 -05:00
|
|
|
fn index(&self, idx: ImportId) -> &ImportData {
|
2019-03-02 14:59:04 -06:00
|
|
|
&self.imports[idx]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Index<Def> for RawItems {
|
|
|
|
type Output = DefData;
|
|
|
|
fn index(&self, idx: Def) -> &DefData {
|
|
|
|
&self.defs[idx]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Index<Macro> for RawItems {
|
|
|
|
type Output = MacroData;
|
|
|
|
fn index(&self, idx: Macro) -> &MacroData {
|
|
|
|
&self.macros[idx]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-13 08:38:02 -05:00
|
|
|
#[derive(Debug, PartialEq, Eq, Clone, Copy)]
|
2019-03-26 05:53:50 -05:00
|
|
|
pub(super) enum RawItem {
|
2019-03-02 14:59:04 -06:00
|
|
|
Module(Module),
|
2019-03-13 08:04:28 -05:00
|
|
|
Import(ImportId),
|
2019-03-02 14:59:04 -06:00
|
|
|
Def(Def),
|
|
|
|
Macro(Macro),
|
|
|
|
}
|
|
|
|
|
2019-03-13 08:38:02 -05:00
|
|
|
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
|
2019-03-26 05:53:50 -05:00
|
|
|
pub(super) struct Module(RawId);
|
2019-03-02 14:59:04 -06:00
|
|
|
impl_arena_id!(Module);
|
|
|
|
|
2019-03-13 08:38:02 -05:00
|
|
|
#[derive(Debug, PartialEq, Eq)]
|
2019-03-26 05:53:50 -05:00
|
|
|
pub(super) enum ModuleData {
|
2019-03-26 09:25:14 -05:00
|
|
|
Declaration { name: Name, ast_id: FileAstId<ast::Module> },
|
|
|
|
Definition { name: Name, ast_id: FileAstId<ast::Module>, items: Vec<RawItem> },
|
2019-03-02 14:59:04 -06:00
|
|
|
}
|
|
|
|
|
2019-03-16 09:17:50 -05:00
|
|
|
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
|
|
|
|
pub struct ImportId(RawId);
|
|
|
|
impl_arena_id!(ImportId);
|
|
|
|
|
|
|
|
#[derive(Debug, Clone, PartialEq, Eq)]
|
|
|
|
pub struct ImportData {
|
2019-03-26 05:53:50 -05:00
|
|
|
pub(super) path: Path,
|
|
|
|
pub(super) alias: Option<Name>,
|
|
|
|
pub(super) is_glob: bool,
|
|
|
|
pub(super) is_prelude: bool,
|
|
|
|
pub(super) is_extern_crate: bool,
|
2019-03-16 09:17:50 -05:00
|
|
|
}
|
2019-03-02 14:59:04 -06:00
|
|
|
|
2019-03-13 08:38:02 -05:00
|
|
|
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
|
2019-03-26 05:53:50 -05:00
|
|
|
pub(super) struct Def(RawId);
|
2019-03-02 14:59:04 -06:00
|
|
|
impl_arena_id!(Def);
|
|
|
|
|
2019-03-13 08:38:02 -05:00
|
|
|
#[derive(Debug, PartialEq, Eq)]
|
2019-03-26 05:53:50 -05:00
|
|
|
pub(super) struct DefData {
|
|
|
|
pub(super) name: Name,
|
|
|
|
pub(super) kind: DefKind,
|
2019-03-02 14:59:04 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, PartialEq, Eq, Clone, Copy)]
|
2019-03-26 05:53:50 -05:00
|
|
|
pub(super) enum DefKind {
|
2019-03-26 10:27:22 -05:00
|
|
|
Function(FileAstId<ast::FnDef>),
|
|
|
|
Struct(FileAstId<ast::StructDef>),
|
|
|
|
Enum(FileAstId<ast::EnumDef>),
|
|
|
|
Const(FileAstId<ast::ConstDef>),
|
|
|
|
Static(FileAstId<ast::StaticDef>),
|
|
|
|
Trait(FileAstId<ast::TraitDef>),
|
|
|
|
TypeAlias(FileAstId<ast::TypeAliasDef>),
|
2019-03-02 14:59:04 -06:00
|
|
|
}
|
|
|
|
|
2019-03-13 08:38:02 -05:00
|
|
|
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
|
2019-03-26 05:53:50 -05:00
|
|
|
pub(super) struct Macro(RawId);
|
2019-03-02 14:59:04 -06:00
|
|
|
impl_arena_id!(Macro);
|
|
|
|
|
2019-03-13 08:38:02 -05:00
|
|
|
#[derive(Debug, PartialEq, Eq)]
|
2019-03-26 05:53:50 -05:00
|
|
|
pub(super) struct MacroData {
|
2019-03-26 10:03:17 -05:00
|
|
|
pub(super) ast_id: FileAstId<ast::MacroCall>,
|
2019-03-26 05:53:50 -05:00
|
|
|
pub(super) path: Path,
|
|
|
|
pub(super) name: Option<Name>,
|
|
|
|
pub(super) export: bool,
|
2019-03-02 14:59:04 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
struct RawItemsCollector {
|
|
|
|
raw_items: RawItems,
|
2019-03-26 11:00:11 -05:00
|
|
|
source_ast_id_map: Arc<AstIdMap>,
|
2019-03-14 05:14:54 -05:00
|
|
|
source_map: ImportSourceMap,
|
2019-03-02 14:59:04 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
impl RawItemsCollector {
|
|
|
|
fn process_module(&mut self, current_module: Option<Module>, body: &impl ast::ModuleItemOwner) {
|
|
|
|
for item_or_macro in body.items_with_macros() {
|
|
|
|
match item_or_macro {
|
|
|
|
ast::ItemOrMacro::Macro(m) => self.add_macro(current_module, m),
|
|
|
|
ast::ItemOrMacro::Item(item) => self.add_item(current_module, item),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn add_item(&mut self, current_module: Option<Module>, item: &ast::ModuleItem) {
|
|
|
|
let (kind, name) = match item.kind() {
|
|
|
|
ast::ModuleItemKind::Module(module) => {
|
|
|
|
self.add_module(current_module, module);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
ast::ModuleItemKind::UseItem(use_item) => {
|
|
|
|
self.add_use_item(current_module, use_item);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
ast::ModuleItemKind::ExternCrateItem(extern_crate) => {
|
|
|
|
self.add_extern_crate_item(current_module, extern_crate);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
ast::ModuleItemKind::ImplBlock(_) => {
|
|
|
|
// impls don't participate in name resolution
|
|
|
|
return;
|
|
|
|
}
|
2019-03-26 10:27:22 -05:00
|
|
|
ast::ModuleItemKind::StructDef(it) => {
|
2019-03-26 11:00:11 -05:00
|
|
|
(DefKind::Struct(self.source_ast_id_map.ast_id(it)), it.name())
|
2019-03-26 10:27:22 -05:00
|
|
|
}
|
|
|
|
ast::ModuleItemKind::EnumDef(it) => {
|
2019-03-26 11:00:11 -05:00
|
|
|
(DefKind::Enum(self.source_ast_id_map.ast_id(it)), it.name())
|
2019-03-26 10:27:22 -05:00
|
|
|
}
|
|
|
|
ast::ModuleItemKind::FnDef(it) => {
|
2019-03-26 11:00:11 -05:00
|
|
|
(DefKind::Function(self.source_ast_id_map.ast_id(it)), it.name())
|
2019-03-26 10:27:22 -05:00
|
|
|
}
|
|
|
|
ast::ModuleItemKind::TraitDef(it) => {
|
2019-03-26 11:00:11 -05:00
|
|
|
(DefKind::Trait(self.source_ast_id_map.ast_id(it)), it.name())
|
2019-03-26 10:27:22 -05:00
|
|
|
}
|
|
|
|
ast::ModuleItemKind::TypeAliasDef(it) => {
|
2019-03-26 11:00:11 -05:00
|
|
|
(DefKind::TypeAlias(self.source_ast_id_map.ast_id(it)), it.name())
|
2019-03-26 10:27:22 -05:00
|
|
|
}
|
|
|
|
ast::ModuleItemKind::ConstDef(it) => {
|
2019-03-26 11:00:11 -05:00
|
|
|
(DefKind::Const(self.source_ast_id_map.ast_id(it)), it.name())
|
2019-03-26 10:27:22 -05:00
|
|
|
}
|
|
|
|
ast::ModuleItemKind::StaticDef(it) => {
|
2019-03-26 11:00:11 -05:00
|
|
|
(DefKind::Static(self.source_ast_id_map.ast_id(it)), it.name())
|
2019-03-26 10:27:22 -05:00
|
|
|
}
|
2019-03-02 14:59:04 -06:00
|
|
|
};
|
|
|
|
if let Some(name) = name {
|
|
|
|
let name = name.as_name();
|
2019-03-26 10:27:22 -05:00
|
|
|
let def = self.raw_items.defs.alloc(DefData { name, kind });
|
2019-03-02 14:59:04 -06:00
|
|
|
self.push_item(current_module, RawItem::Def(def))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn add_module(&mut self, current_module: Option<Module>, module: &ast::Module) {
|
|
|
|
let name = match module.name() {
|
|
|
|
Some(it) => it.as_name(),
|
|
|
|
None => return,
|
|
|
|
};
|
2019-03-26 11:00:11 -05:00
|
|
|
let ast_id = self.source_ast_id_map.ast_id(module);
|
2019-03-02 14:59:04 -06:00
|
|
|
if module.has_semi() {
|
2019-03-26 09:25:14 -05:00
|
|
|
let item = self.raw_items.modules.alloc(ModuleData::Declaration { name, ast_id });
|
2019-03-02 14:59:04 -06:00
|
|
|
self.push_item(current_module, RawItem::Module(item));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if let Some(item_list) = module.item_list() {
|
2019-03-13 08:38:02 -05:00
|
|
|
let item = self.raw_items.modules.alloc(ModuleData::Definition {
|
|
|
|
name,
|
2019-03-26 09:25:14 -05:00
|
|
|
ast_id,
|
2019-03-13 08:38:02 -05:00
|
|
|
items: Vec::new(),
|
|
|
|
});
|
2019-03-02 14:59:04 -06:00
|
|
|
self.process_module(Some(item), item_list);
|
|
|
|
self.push_item(current_module, RawItem::Module(item));
|
2019-03-13 08:38:02 -05:00
|
|
|
return;
|
2019-03-02 14:59:04 -06:00
|
|
|
}
|
2019-03-13 08:38:02 -05:00
|
|
|
tested_by!(name_res_works_for_broken_modules);
|
2019-03-02 14:59:04 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
fn add_use_item(&mut self, current_module: Option<Module>, use_item: &ast::UseItem) {
|
2019-03-13 08:04:28 -05:00
|
|
|
let is_prelude = use_item.has_atom_attr("prelude_import");
|
2019-03-02 14:59:04 -06:00
|
|
|
|
2019-04-02 05:26:09 -05:00
|
|
|
Path::expand_use_item(use_item, |path, use_tree, is_glob, alias| {
|
2019-03-02 14:59:04 -06:00
|
|
|
let import = self.raw_items.imports.alloc(ImportData {
|
|
|
|
path,
|
|
|
|
alias,
|
2019-04-02 05:26:09 -05:00
|
|
|
is_glob,
|
2019-03-02 14:59:04 -06:00
|
|
|
is_prelude,
|
|
|
|
is_extern_crate: false,
|
|
|
|
});
|
2019-04-02 05:26:09 -05:00
|
|
|
self.source_map.insert(import, use_tree);
|
2019-03-02 14:59:04 -06:00
|
|
|
self.push_item(current_module, RawItem::Import(import))
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
fn add_extern_crate_item(
|
|
|
|
&mut self,
|
|
|
|
current_module: Option<Module>,
|
|
|
|
extern_crate: &ast::ExternCrateItem,
|
|
|
|
) {
|
|
|
|
if let Some(name_ref) = extern_crate.name_ref() {
|
|
|
|
let path = Path::from_name_ref(name_ref);
|
|
|
|
let alias = extern_crate.alias().and_then(|a| a.name()).map(AsName::as_name);
|
|
|
|
let import = self.raw_items.imports.alloc(ImportData {
|
|
|
|
path,
|
|
|
|
alias,
|
|
|
|
is_glob: false,
|
|
|
|
is_prelude: false,
|
|
|
|
is_extern_crate: true,
|
|
|
|
});
|
|
|
|
self.push_item(current_module, RawItem::Import(import))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn add_macro(&mut self, current_module: Option<Module>, m: &ast::MacroCall) {
|
2019-03-26 05:09:39 -05:00
|
|
|
let path = match m.path().and_then(Path::from_ast) {
|
|
|
|
Some(it) => it,
|
2019-03-02 14:59:04 -06:00
|
|
|
_ => return,
|
|
|
|
};
|
|
|
|
|
|
|
|
let name = m.name().map(|it| it.as_name());
|
2019-03-26 11:00:11 -05:00
|
|
|
let ast_id = self.source_ast_id_map.ast_id(m);
|
2019-03-13 08:04:28 -05:00
|
|
|
let export = m.has_atom_attr("macro_export");
|
2019-03-26 10:03:17 -05:00
|
|
|
let m = self.raw_items.macros.alloc(MacroData { ast_id, path, name, export });
|
2019-03-02 14:59:04 -06:00
|
|
|
self.push_item(current_module, RawItem::Macro(m));
|
|
|
|
}
|
|
|
|
|
|
|
|
fn push_item(&mut self, current_module: Option<Module>, item: RawItem) {
|
|
|
|
match current_module {
|
|
|
|
Some(module) => match &mut self.raw_items.modules[module] {
|
|
|
|
ModuleData::Definition { items, .. } => items,
|
|
|
|
ModuleData::Declaration { .. } => unreachable!(),
|
|
|
|
},
|
|
|
|
None => &mut self.raw_items.items,
|
|
|
|
}
|
|
|
|
.push(item)
|
|
|
|
}
|
|
|
|
}
|