rust/crates/ra_hir/src/module.rs

210 lines
6.3 KiB
Rust
Raw Normal View History

2018-11-28 01:42:26 +01:00
pub(super) mod imp;
pub(super) mod nameres;
use ra_syntax::{
algo::generate,
ast::{self, AstNode, NameOwner},
2018-12-27 18:07:21 +01:00
SyntaxNode,
2018-11-28 01:42:26 +01:00
};
2019-01-04 14:15:50 +01:00
use ra_arena::{Arena, RawId, impl_arena_id};
2018-11-28 01:42:26 +01:00
use relative_path::RelativePathBuf;
2019-01-06 13:58:45 +01:00
use crate::{Name, HirDatabase, SourceItemId, SourceFileItemId, HirFileId};
2018-11-28 01:42:26 +01:00
pub use self::nameres::{ModuleScope, Resolution, Namespace, PerNs};
2018-11-28 01:42:26 +01:00
2019-01-04 14:15:50 +01:00
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct ModuleId(RawId);
impl_arena_id!(ModuleId);
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct LinkId(RawId);
impl_arena_id!(LinkId);
/// Physically, rust source is organized as a set of files, but logically it is
2018-11-28 01:42:26 +01:00
/// organized as a tree of modules. Usually, a single file corresponds to a
/// single module, but it is not nessary the case.
///
/// Module encapsulate the logic of transitioning from the fuzzy world of files
/// (which can have multiple parents) to the precise world of modules (which
/// always have one parent).
#[derive(Default, Debug, PartialEq, Eq)]
2018-11-28 02:09:44 +01:00
pub struct ModuleTree {
2019-01-04 14:15:50 +01:00
mods: Arena<ModuleId, ModuleData>,
links: Arena<LinkId, LinkData>,
2018-11-28 01:42:26 +01:00
}
impl ModuleTree {
pub(crate) fn modules<'a>(&'a self) -> impl Iterator<Item = ModuleId> + 'a {
self.mods.iter().map(|(id, _)| id)
}
2018-12-05 11:20:11 +01:00
pub(crate) fn modules_with_sources<'a>(
&'a self,
) -> impl Iterator<Item = (ModuleId, ModuleSource)> + 'a {
self.mods.iter().map(|(id, m)| (id, m.source))
2018-11-28 01:42:26 +01:00
}
}
/// `ModuleSource` is the syntax tree element that produced this module:
/// either a file, or an inlinde module.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
2019-01-04 23:37:40 +01:00
pub struct ModuleSource(pub(crate) SourceItemId);
2018-11-28 01:42:26 +01:00
/// An owned syntax node for a module. Unlike `ModuleSource`,
/// this holds onto the AST for the whole file.
2018-11-28 14:24:06 +01:00
pub(crate) enum ModuleSourceNode {
2018-11-28 01:42:26 +01:00
SourceFile(ast::SourceFileNode),
Module(ast::ModuleNode),
}
#[derive(Clone, Debug, Hash, PartialEq, Eq)]
pub enum Problem {
UnresolvedModule {
candidate: RelativePathBuf,
},
NotDirOwner {
move_to: RelativePathBuf,
candidate: RelativePathBuf,
},
}
impl ModuleId {
pub(crate) fn source(self, tree: &ModuleTree) -> ModuleSource {
tree.mods[self].source
}
2019-01-06 13:58:45 +01:00
pub(crate) fn parent_link(self, tree: &ModuleTree) -> Option<LinkId> {
2018-11-28 01:42:26 +01:00
tree.mods[self].parent
}
2019-01-06 12:05:03 +01:00
pub(crate) fn parent(self, tree: &ModuleTree) -> Option<ModuleId> {
2018-11-28 01:42:26 +01:00
let link = self.parent_link(tree)?;
Some(tree.links[link].owner)
}
2019-01-04 23:37:40 +01:00
pub(crate) fn crate_root(self, tree: &ModuleTree) -> ModuleId {
2018-11-28 01:42:26 +01:00
generate(Some(self), move |it| it.parent(tree))
.last()
.unwrap()
}
2019-01-04 23:37:40 +01:00
pub(crate) fn child(self, tree: &ModuleTree, name: &Name) -> Option<ModuleId> {
2018-11-28 01:42:26 +01:00
let link = tree.mods[self]
.children
.iter()
.map(|&it| &tree.links[it])
2018-12-27 18:07:21 +01:00
.find(|it| it.name == *name)?;
2018-11-28 01:42:26 +01:00
Some(*link.points_to.first()?)
}
2018-12-27 18:07:21 +01:00
fn children<'a>(self, tree: &'a ModuleTree) -> impl Iterator<Item = (Name, ModuleId)> + 'a {
2018-11-28 01:42:26 +01:00
tree.mods[self].children.iter().filter_map(move |&it| {
let link = &tree.links[it];
let module = *link.points_to.first()?;
Some((link.name.clone(), module))
})
}
2019-01-06 13:58:45 +01:00
pub(crate) fn problems(
self,
tree: &ModuleTree,
db: &impl HirDatabase,
) -> Vec<(SyntaxNode, Problem)> {
2018-11-28 01:42:26 +01:00
tree.mods[self]
.children
.iter()
.filter_map(|&it| {
let p = tree.links[it].problem.clone()?;
let s = it.bind_source(tree, db);
let s = s.borrowed().name().unwrap().syntax().owned();
Some((s, p))
})
.collect()
}
}
impl LinkId {
2019-01-06 13:58:45 +01:00
pub(crate) fn owner(self, tree: &ModuleTree) -> ModuleId {
2018-11-28 01:42:26 +01:00
tree.links[self].owner
}
2019-01-06 13:58:45 +01:00
pub(crate) fn name(self, tree: &ModuleTree) -> &Name {
2018-12-27 18:07:21 +01:00
&tree.links[self].name
2018-11-28 01:42:26 +01:00
}
2019-01-06 13:58:45 +01:00
pub(crate) fn bind_source<'a>(
self,
tree: &ModuleTree,
db: &impl HirDatabase,
) -> ast::ModuleNode {
2018-11-28 01:42:26 +01:00
let owner = self.owner(tree);
match owner.source(tree).resolve(db) {
ModuleSourceNode::SourceFile(root) => {
let ast = imp::modules(root.borrowed())
.find(|(name, _)| name == &tree.links[self].name)
.unwrap()
.1;
ast.owned()
}
ModuleSourceNode::Module(it) => it,
}
}
}
#[derive(Debug, PartialEq, Eq, Hash)]
2018-11-28 02:09:44 +01:00
pub struct ModuleData {
2018-11-28 01:42:26 +01:00
source: ModuleSource,
parent: Option<LinkId>,
children: Vec<LinkId>,
}
impl ModuleSource {
2018-12-04 20:46:23 +01:00
// precondition: item_id **must** point to module
2019-01-01 21:21:16 +01:00
fn new(file_id: HirFileId, item_id: Option<SourceFileItemId>) -> ModuleSource {
let source_item_id = SourceItemId { file_id, item_id };
2018-12-04 20:46:23 +01:00
ModuleSource(source_item_id)
}
2019-01-01 21:21:16 +01:00
pub(crate) fn new_file(file_id: HirFileId) -> ModuleSource {
ModuleSource::new(file_id, None)
2018-12-04 20:46:23 +01:00
}
2018-11-28 01:42:26 +01:00
pub(crate) fn new_inline(
db: &impl HirDatabase,
2019-01-01 21:21:16 +01:00
file_id: HirFileId,
2018-12-04 20:46:23 +01:00
m: ast::Module,
2018-11-28 01:42:26 +01:00
) -> ModuleSource {
2018-12-04 20:46:23 +01:00
assert!(!m.has_semi());
2019-01-01 21:21:16 +01:00
let file_items = db.file_items(file_id);
let item_id = file_items.id_of(file_id, m.syntax());
ModuleSource::new(file_id, Some(item_id))
2018-11-28 01:42:26 +01:00
}
2019-01-01 21:21:16 +01:00
pub(crate) fn file_id(self) -> HirFileId {
self.0.file_id
2018-11-28 01:42:26 +01:00
}
2018-11-28 14:24:06 +01:00
pub(crate) fn resolve(self, db: &impl HirDatabase) -> ModuleSourceNode {
2018-12-04 20:46:23 +01:00
let syntax_node = db.file_item(self.0);
let syntax_node = syntax_node.borrowed();
if let Some(file) = ast::SourceFile::cast(syntax_node) {
return ModuleSourceNode::SourceFile(file.owned());
2018-11-28 01:42:26 +01:00
}
2018-12-04 20:46:23 +01:00
let module = ast::Module::cast(syntax_node).unwrap();
ModuleSourceNode::Module(module.owned())
2018-11-28 01:42:26 +01:00
}
}
#[derive(Hash, Debug, PartialEq, Eq)]
struct LinkData {
owner: ModuleId,
2018-12-27 18:07:21 +01:00
name: Name,
2018-11-28 01:42:26 +01:00
points_to: Vec<ModuleId>,
problem: Option<Problem>,
}
impl ModuleTree {
fn push_mod(&mut self, data: ModuleData) -> ModuleId {
self.mods.alloc(data)
}
fn push_link(&mut self, data: LinkData) -> LinkId {
let owner = data.owner;
let id = self.links.alloc(data);
self.mods[owner].children.push(id);
id
}
}