switched to lowerd module

This commit is contained in:
Aleksey Kladov 2019-01-18 16:36:56 +03:00
parent b93c6bc557
commit c0aeb5204c
9 changed files with 284 additions and 65 deletions

View file

@ -7,7 +7,7 @@ use ra_syntax::{ast, TreeArc, SyntaxNode};
use crate::{ use crate::{
Name, DefId, Path, PerNs, ScopesWithSyntaxMapping, Ty, HirFileId, Name, DefId, Path, PerNs, ScopesWithSyntaxMapping, Ty, HirFileId,
type_ref::TypeRef, type_ref::TypeRef,
nameres::ModuleScope, nameres::{ModuleScope, lower::LoweredImport},
db::HirDatabase, db::HirDatabase,
expr::BodySyntaxMapping, expr::BodySyntaxMapping,
ty::InferenceResult, ty::InferenceResult,
@ -96,6 +96,15 @@ impl Module {
self.declaration_source_impl(db) self.declaration_source_impl(db)
} }
/// Returns the syntax of the last path segment corresponding to this import
pub fn import_source(
&self,
db: &impl HirDatabase,
import: LoweredImport,
) -> TreeArc<ast::PathSegment> {
self.import_source_impl(db, import)
}
/// Returns the crate this module is part of. /// Returns the crate this module is part of.
pub fn krate(&self, db: &impl HirDatabase) -> Option<Crate> { pub fn krate(&self, db: &impl HirDatabase) -> Option<Crate> {
self.krate_impl(db) self.krate_impl(db)

View file

@ -5,7 +5,7 @@ use crate::{
Module, ModuleSource, Problem, Module, ModuleSource, Problem,
Crate, DefId, DefLoc, DefKind, Name, Path, PathKind, PerNs, Def, Crate, DefId, DefLoc, DefKind, Name, Path, PathKind, PerNs, Def,
module_tree::ModuleId, module_tree::ModuleId,
nameres::ModuleScope, nameres::{ModuleScope, lower::LoweredImport},
db::HirDatabase, db::HirDatabase,
}; };
@ -37,7 +37,7 @@ impl Module {
Some(link.name(&module_tree).clone()) Some(link.name(&module_tree).clone())
} }
pub fn definition_source_impl(&self, db: &impl HirDatabase) -> (FileId, ModuleSource) { pub(crate) fn definition_source_impl(&self, db: &impl HirDatabase) -> (FileId, ModuleSource) {
let loc = self.def_id.loc(db); let loc = self.def_id.loc(db);
let file_id = loc.source_item_id.file_id.as_original_file(); let file_id = loc.source_item_id.file_id.as_original_file();
let syntax_node = db.file_item(loc.source_item_id); let syntax_node = db.file_item(loc.source_item_id);
@ -50,7 +50,7 @@ impl Module {
(file_id, module_source) (file_id, module_source)
} }
pub fn declaration_source_impl( pub(crate) fn declaration_source_impl(
&self, &self,
db: &impl HirDatabase, db: &impl HirDatabase,
) -> Option<(FileId, TreeArc<ast::Module>)> { ) -> Option<(FileId, TreeArc<ast::Module>)> {
@ -66,6 +66,17 @@ impl Module {
Some((file_id, src)) Some((file_id, src))
} }
pub(crate) fn import_source_impl(
&self,
db: &impl HirDatabase,
import: LoweredImport,
) -> TreeArc<ast::PathSegment> {
let loc = self.def_id.loc(db);
let source_map = db.lower_module_source_map(loc.source_root_id, loc.module_id);
let (_, source) = self.definition_source(db);
source_map.get(&source, import)
}
pub(crate) fn krate_impl(&self, db: &impl HirDatabase) -> Option<Crate> { pub(crate) fn krate_impl(&self, db: &impl HirDatabase) -> Option<Crate> {
let root = self.crate_root(db); let root = self.crate_root(db);
let loc = root.def_id.loc(db); let loc = root.def_id.loc(db);

View file

@ -10,7 +10,7 @@ use crate::{
FnSignature, FnScopes, FnSignature, FnScopes,
macros::MacroExpansion, macros::MacroExpansion,
module_tree::{ModuleId, ModuleTree}, module_tree::{ModuleId, ModuleTree},
nameres::{ItemMap, lower::InputModuleItems}, nameres::{ItemMap, lower::{InputModuleItems, LoweredModule, ImportSourceMap}},
ty::{InferenceResult, Ty, method_resolution::CrateImplBlocks}, ty::{InferenceResult, Ty, method_resolution::CrateImplBlocks},
adt::{StructData, EnumData, EnumVariantData}, adt::{StructData, EnumData, EnumVariantData},
impl_block::ModuleImplBlocks, impl_block::ModuleImplBlocks,
@ -65,6 +65,27 @@ pub trait HirDatabase:
module_id: ModuleId, module_id: ModuleId,
) -> Arc<InputModuleItems>; ) -> Arc<InputModuleItems>;
#[salsa::invoke(crate::nameres::lower::LoweredModule::lower_module_query)]
fn lower_module(
&self,
source_root_id: SourceRootId,
module_id: ModuleId,
) -> (Arc<LoweredModule>, Arc<ImportSourceMap>);
#[salsa::invoke(crate::nameres::lower::LoweredModule::lower_module_module_query)]
fn lower_module_module(
&self,
source_root_id: SourceRootId,
module_id: ModuleId,
) -> Arc<LoweredModule>;
#[salsa::invoke(crate::nameres::lower::LoweredModule::lower_module_source_map_query)]
fn lower_module_source_map(
&self,
source_root_id: SourceRootId,
module_id: ModuleId,
) -> Arc<ImportSourceMap>;
#[salsa::invoke(query_definitions::item_map)] #[salsa::invoke(query_definitions::item_map)]
fn item_map(&self, source_root_id: SourceRootId) -> Arc<ItemMap>; fn item_map(&self, source_root_id: SourceRootId) -> Arc<ItemMap>;

View file

@ -60,7 +60,7 @@ pub struct Resolution {
/// None for unresolved /// None for unresolved
pub def_id: PerNs<DefId>, pub def_id: PerNs<DefId>,
/// ident by whitch this is imported into local scope. /// ident by whitch this is imported into local scope.
pub import: Option<NamedImport>, pub import: Option<LoweredImport>,
} }
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)] #[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
@ -151,10 +151,10 @@ impl<T> PerNs<T> {
pub(crate) struct Resolver<'a, DB> { pub(crate) struct Resolver<'a, DB> {
db: &'a DB, db: &'a DB,
input: &'a FxHashMap<ModuleId, Arc<InputModuleItems>>, input: &'a FxHashMap<ModuleId, Arc<LoweredModule>>,
source_root: SourceRootId, source_root: SourceRootId,
module_tree: Arc<ModuleTree>, module_tree: Arc<ModuleTree>,
processed_imports: FxHashSet<(ModuleId, usize)>, processed_imports: FxHashSet<(ModuleId, LoweredImport)>,
result: ItemMap, result: ItemMap,
} }
@ -164,7 +164,7 @@ where
{ {
pub(crate) fn new( pub(crate) fn new(
db: &'a DB, db: &'a DB,
input: &'a FxHashMap<ModuleId, Arc<InputModuleItems>>, input: &'a FxHashMap<ModuleId, Arc<LoweredModule>>,
source_root: SourceRootId, source_root: SourceRootId,
module_tree: Arc<ModuleTree>, module_tree: Arc<ModuleTree>,
) -> Resolver<'a, DB> { ) -> Resolver<'a, DB> {
@ -197,7 +197,7 @@ where
self.result self.result
} }
fn populate_module(&mut self, module_id: ModuleId, input: Arc<InputModuleItems>) { fn populate_module(&mut self, module_id: ModuleId, input: Arc<LoweredModule>) {
let mut module_items = ModuleScope::default(); let mut module_items = ModuleScope::default();
// Populate extern crates prelude // Populate extern crates prelude
@ -220,14 +220,14 @@ where
} }
}; };
} }
for import in input.imports.iter() { for (import_id, import_data) in input.imports.iter() {
if let Some(name) = import.path.segments.iter().last() { if let Some(name) = import_data.path.segments.iter().last() {
if let ImportKind::Named(import) = import.kind { if !import_data.is_glob {
module_items.items.insert( module_items.items.insert(
name.clone(), name.clone(),
Resolution { Resolution {
def_id: PerNs::none(), def_id: PerNs::none(),
import: Some(import), import: Some(import_id),
}, },
); );
} }
@ -281,23 +281,27 @@ where
} }
fn resolve_imports(&mut self, module_id: ModuleId) { fn resolve_imports(&mut self, module_id: ModuleId) {
for (i, import) in self.input[&module_id].imports.iter().enumerate() { for (import_id, import_data) in self.input[&module_id].imports.iter() {
if self.processed_imports.contains(&(module_id, i)) { if self.processed_imports.contains(&(module_id, import_id)) {
// already done // already done
continue; continue;
} }
if self.resolve_import(module_id, import) { if self.resolve_import(module_id, import_id, import_data) {
log::debug!("import {:?} resolved (or definite error)", import); log::debug!("import {:?} resolved (or definite error)", import_id);
self.processed_imports.insert((module_id, i)); self.processed_imports.insert((module_id, import_id));
} }
} }
} }
fn resolve_import(&mut self, module_id: ModuleId, import: &Import) -> bool { fn resolve_import(
&mut self,
module_id: ModuleId,
import_id: LoweredImport,
import: &ImportData,
) -> bool {
log::debug!("resolving import: {:?}", import); log::debug!("resolving import: {:?}", import);
let ptr = match import.kind { if import.is_glob {
ImportKind::Glob => return false, return false;
ImportKind::Named(ptr) => ptr,
}; };
let mut curr: ModuleId = match import.path.kind { let mut curr: ModuleId = match import.path.kind {
@ -358,7 +362,7 @@ where
self.update(module_id, |items| { self.update(module_id, |items| {
let res = Resolution { let res = Resolution {
def_id, def_id,
import: Some(ptr), import: Some(import_id),
}; };
items.items.insert(name.clone(), res); items.items.insert(name.clone(), res);
}); });
@ -394,7 +398,7 @@ where
self.update(module_id, |items| { self.update(module_id, |items| {
let res = Resolution { let res = Resolution {
def_id, def_id,
import: Some(ptr), import: Some(import_id),
}; };
items.items.insert(name.clone(), res); items.items.insert(name.clone(), res);
}) })

View file

@ -1,10 +1,11 @@
use std::sync::Arc; use std::sync::Arc;
use ra_syntax::{ use ra_syntax::{
TextRange, SyntaxKind, AstNode, TextRange, SyntaxKind, AstNode, SourceFile, TreeArc,
ast::{self, ModuleItemOwner}, ast::{self, ModuleItemOwner},
}; };
use ra_db::{FileId, SourceRootId}; use ra_db::{SourceRootId, LocalSyntaxPtr};
use ra_arena::{Arena, RawId, impl_arena_id, map::ArenaMap};
use crate::{ use crate::{
SourceItemId, SourceFileItemId, Path, ModuleSource, HirDatabase, Name, SourceFileItems, SourceItemId, SourceFileItemId, Path, ModuleSource, HirDatabase, Name, SourceFileItems,
@ -139,12 +140,12 @@ impl InputModuleItems {
fn add_use_item(&mut self, file_items: &SourceFileItems, item: &ast::UseItem) { fn add_use_item(&mut self, file_items: &SourceFileItems, item: &ast::UseItem) {
let file_item_id = file_items.id_of_unchecked(item.syntax()); let file_item_id = file_items.id_of_unchecked(item.syntax());
let start_offset = item.syntax().range().start(); let start_offset = item.syntax().range().start();
Path::expand_use_item(item, |path, range| { Path::expand_use_item(item, |path, segment| {
let kind = match range { let kind = match segment {
None => ImportKind::Glob, None => ImportKind::Glob,
Some(range) => ImportKind::Named(NamedImport { Some(segment) => ImportKind::Named(NamedImport {
file_item_id, file_item_id,
relative_range: range - start_offset, relative_range: segment.syntax().range() - start_offset,
}), }),
}; };
self.imports.push(Import { kind, path }) self.imports.push(Import { kind, path })
@ -199,22 +200,194 @@ pub struct NamedImport {
pub relative_range: TextRange, pub relative_range: TextRange,
} }
impl NamedImport {
// FIXME: this is only here for one use-case in completion. Seems like a
// pretty gross special case.
pub fn range(&self, db: &impl HirDatabase, file_id: FileId) -> TextRange {
let source_item_id = SourceItemId {
file_id: file_id.into(),
item_id: Some(self.file_item_id),
};
let syntax = db.file_item(source_item_id);
let offset = syntax.range().start();
self.relative_range + offset
}
}
#[derive(Debug, Clone, PartialEq, Eq)] #[derive(Debug, Clone, PartialEq, Eq)]
pub(super) enum ImportKind { pub(super) enum ImportKind {
Glob, Glob,
Named(NamedImport), Named(NamedImport),
} }
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct LoweredImport(RawId);
impl_arena_id!(LoweredImport);
#[derive(Debug, PartialEq, Eq)]
pub(super) struct ImportData {
pub(super) path: Path,
pub(super) is_glob: bool,
}
#[derive(Debug, Default, PartialEq, Eq)]
pub struct LoweredModule {
pub(super) items: Vec<ModuleItem>,
pub(super) imports: Arena<LoweredImport, ImportData>,
}
#[derive(Debug, Default, PartialEq, Eq)]
pub struct ImportSourceMap {
map: ArenaMap<LoweredImport, LocalSyntaxPtr>,
}
impl ImportSourceMap {
fn insert(&mut self, import: LoweredImport, segment: &ast::PathSegment) {
self.map
.insert(import, LocalSyntaxPtr::new(segment.syntax()))
}
pub fn get(&self, source: &ModuleSource, import: LoweredImport) -> TreeArc<ast::PathSegment> {
let file = match source {
ModuleSource::SourceFile(file) => &*file,
ModuleSource::Module(m) => m.syntax().ancestors().find_map(SourceFile::cast).unwrap(),
};
ast::PathSegment::cast(&self.map[import].resolve(file))
.unwrap()
.to_owned()
}
}
impl LoweredModule {
pub(crate) fn lower_module_module_query(
db: &impl HirDatabase,
source_root_id: SourceRootId,
module_id: ModuleId,
) -> Arc<LoweredModule> {
db.lower_module(source_root_id, module_id).0
}
pub(crate) fn lower_module_source_map_query(
db: &impl HirDatabase,
source_root_id: SourceRootId,
module_id: ModuleId,
) -> Arc<ImportSourceMap> {
db.lower_module(source_root_id, module_id).1
}
pub(crate) fn lower_module_query(
db: &impl HirDatabase,
source_root_id: SourceRootId,
module_id: ModuleId,
) -> (Arc<LoweredModule>, Arc<ImportSourceMap>) {
let module_tree = db.module_tree(source_root_id);
let source = module_id.source(&module_tree);
let file_id = source.file_id;
let source = ModuleSource::from_source_item_id(db, source);
let mut source_map = ImportSourceMap::default();
let mut res = LoweredModule::default();
match source {
ModuleSource::SourceFile(it) => res.fill(
&mut source_map,
db,
source_root_id,
module_id,
file_id,
&mut it.items_with_macros(),
),
ModuleSource::Module(it) => {
if let Some(item_list) = it.item_list() {
res.fill(
&mut source_map,
db,
source_root_id,
module_id,
file_id,
&mut item_list.items_with_macros(),
)
}
}
};
(Arc::new(res), Arc::new(source_map))
}
fn fill(
&mut self,
source_map: &mut ImportSourceMap,
db: &impl HirDatabase,
source_root_id: SourceRootId,
module_id: ModuleId,
file_id: HirFileId,
items: &mut Iterator<Item = ast::ItemOrMacro>,
) {
let file_items = db.file_items(file_id);
for item in items {
match item {
ast::ItemOrMacro::Item(it) => {
self.add_item(source_map, file_id, &file_items, it);
}
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 {
file_id,
item_id: Some(item_id),
},
};
let id = loc.id(db);
let file_id = HirFileId::from(id);
let file_items = db.file_items(file_id);
//FIXME: expand recursively
for item in db.hir_source_file(file_id).items() {
self.add_item(source_map, file_id, &file_items, item);
}
}
}
}
}
fn add_item(
&mut self,
source_map: &mut ImportSourceMap,
file_id: HirFileId,
file_items: &SourceFileItems,
item: &ast::ModuleItem,
) -> Option<()> {
match item.kind() {
ast::ModuleItemKind::StructDef(it) => {
self.items.push(ModuleItem::new(file_id, file_items, it)?)
}
ast::ModuleItemKind::EnumDef(it) => {
self.items.push(ModuleItem::new(file_id, file_items, it)?)
}
ast::ModuleItemKind::FnDef(it) => {
self.items.push(ModuleItem::new(file_id, file_items, it)?)
}
ast::ModuleItemKind::TraitDef(it) => {
self.items.push(ModuleItem::new(file_id, file_items, it)?)
}
ast::ModuleItemKind::TypeDef(it) => {
self.items.push(ModuleItem::new(file_id, file_items, it)?)
}
ast::ModuleItemKind::ImplBlock(_) => {
// impls don't define items
}
ast::ModuleItemKind::UseItem(it) => self.add_use_item(source_map, it),
ast::ModuleItemKind::ExternCrateItem(_) => {
// TODO
}
ast::ModuleItemKind::ConstDef(it) => {
self.items.push(ModuleItem::new(file_id, file_items, it)?)
}
ast::ModuleItemKind::StaticDef(it) => {
self.items.push(ModuleItem::new(file_id, file_items, it)?)
}
ast::ModuleItemKind::Module(it) => {
self.items.push(ModuleItem::new(file_id, file_items, it)?)
}
}
Some(())
}
fn add_use_item(&mut self, source_map: &mut ImportSourceMap, item: &ast::UseItem) {
Path::expand_use_item(item, |path, segment| {
let import = self.imports.alloc(ImportData {
path,
is_glob: segment.is_none(),
});
if let Some(segment) = segment {
source_map.insert(import, segment)
}
})
}
}

View file

@ -1,4 +1,4 @@
use ra_syntax::{ast, AstNode, TextRange}; use ra_syntax::{ast, AstNode};
use crate::{Name, AsName}; use crate::{Name, AsName};
@ -18,7 +18,10 @@ pub enum PathKind {
impl Path { impl Path {
/// Calls `cb` with all paths, represented by this use item. /// Calls `cb` with all paths, represented by this use item.
pub fn expand_use_item(item: &ast::UseItem, mut cb: impl FnMut(Path, Option<TextRange>)) { pub fn expand_use_item<'a>(
item: &'a ast::UseItem,
mut cb: impl FnMut(Path, Option<&'a ast::PathSegment>),
) {
if let Some(tree) = item.use_tree() { if let Some(tree) = item.use_tree() {
expand_use_tree(None, tree, &mut cb); expand_use_tree(None, tree, &mut cb);
} }
@ -98,10 +101,10 @@ impl From<Name> for Path {
} }
} }
fn expand_use_tree( fn expand_use_tree<'a>(
prefix: Option<Path>, prefix: Option<Path>,
tree: &ast::UseTree, tree: &'a ast::UseTree,
cb: &mut impl FnMut(Path, Option<TextRange>), cb: &mut impl FnMut(Path, Option<&'a ast::PathSegment>),
) { ) {
if let Some(use_tree_list) = tree.use_tree_list() { if let Some(use_tree_list) = tree.use_tree_list() {
let prefix = match tree.path() { let prefix = match tree.path() {
@ -125,20 +128,18 @@ fn expand_use_tree(
if let Some(segment) = ast_path.segment() { if let Some(segment) = ast_path.segment() {
if segment.kind() == Some(ast::PathSegmentKind::SelfKw) { if segment.kind() == Some(ast::PathSegmentKind::SelfKw) {
if let Some(prefix) = prefix { if let Some(prefix) = prefix {
cb(prefix, Some(segment.syntax().range())); cb(prefix, Some(segment));
return; return;
} }
} }
} }
} }
if let Some(path) = convert_path(prefix, ast_path) { if let Some(path) = convert_path(prefix, ast_path) {
let range = if tree.has_star() { if tree.has_star() {
None cb(path, None)
} else { } else if let Some(segment) = ast_path.segment() {
let range = ast_path.segment().unwrap().syntax().range(); cb(path, Some(segment))
Some(range)
}; };
cb(path, range)
} }
// TODO: report errors somewhere // TODO: report errors somewhere
// We get here if we do // We get here if we do

View file

@ -46,7 +46,7 @@ pub(super) fn item_map(db: &impl HirDatabase, source_root: SourceRootId) -> Arc<
let module_tree = db.module_tree(source_root); let module_tree = db.module_tree(source_root);
let input = module_tree let input = module_tree
.modules() .modules()
.map(|id| (id, db.input_module_items(source_root, id))) .map(|id| (id, db.lower_module_module(source_root, id)))
.collect::<FxHashMap<_, _>>(); .collect::<FxHashMap<_, _>>();
let resolver = Resolver::new(db, &input, source_root, module_tree); let resolver = Resolver::new(db, &input, source_root, module_tree);

View file

@ -1,5 +1,5 @@
use rustc_hash::FxHashSet; use rustc_hash::FxHashSet;
use ra_syntax::TextUnit; use ra_syntax::{AstNode, TextUnit};
use crate::completion::{CompletionItem, CompletionItemKind, Completions, CompletionKind, CompletionContext}; use crate::completion::{CompletionItem, CompletionItemKind, Completions, CompletionKind, CompletionContext};
@ -17,18 +17,15 @@ pub(super) fn complete_scope(acc: &mut Completions, ctx: &CompletionContext) {
} }
let module_scope = module.scope(ctx.db); let module_scope = module.scope(ctx.db);
let (file_id, _) = module.definition_source(ctx.db);
module_scope module_scope
.entries() .entries()
.filter(|(_name, res)| { .filter(|(_name, res)| {
// Don't expose this item // For cases like `use self::foo<|>` don't suggest foo itself.
// FIXME: this penetrates through all kinds of abstractions,
// we need to figura out the way to do it less ugly.
match res.import { match res.import {
None => true, None => true,
Some(import) => { Some(import) => {
let range = import.range(ctx.db, file_id); let source = module.import_source(ctx.db, import);
!range.is_subrange(&ctx.leaf.range()) !source.syntax().range().is_subrange(&ctx.leaf.range())
} }
} }
}) })

View file

@ -114,6 +114,9 @@ salsa::database_storage! {
fn file_items() for hir::db::FileItemsQuery; fn file_items() for hir::db::FileItemsQuery;
fn file_item() for hir::db::FileItemQuery; fn file_item() for hir::db::FileItemQuery;
fn input_module_items() for hir::db::InputModuleItemsQuery; fn input_module_items() for hir::db::InputModuleItemsQuery;
fn lower_module() for hir::db::LowerModuleQuery;
fn lower_module_module() for hir::db::LowerModuleModuleQuery;
fn lower_module_source_map() for hir::db::LowerModuleSourceMapQuery;
fn item_map() for hir::db::ItemMapQuery; fn item_map() for hir::db::ItemMapQuery;
fn submodules() for hir::db::SubmodulesQuery; fn submodules() for hir::db::SubmodulesQuery;
fn infer() for hir::db::InferQuery; fn infer() for hir::db::InferQuery;