2018-11-28 00:42:26 +00:00
|
|
|
pub(super) mod imp;
|
|
|
|
pub(super) mod nameres;
|
|
|
|
|
|
|
|
use std::sync::Arc;
|
2018-12-23 16:13:11 +00:00
|
|
|
use log;
|
2018-11-28 00:42:26 +00:00
|
|
|
|
|
|
|
use ra_syntax::{
|
|
|
|
algo::generate,
|
|
|
|
ast::{self, AstNode, NameOwner},
|
2018-12-05 10:16:20 +00:00
|
|
|
SmolStr, SyntaxNode,
|
2018-11-28 00:42:26 +00:00
|
|
|
};
|
2018-12-05 10:16:20 +00:00
|
|
|
use ra_db::{SourceRootId, FileId, Cancelable};
|
2018-11-28 00:42:26 +00:00
|
|
|
use relative_path::RelativePathBuf;
|
|
|
|
|
|
|
|
use crate::{
|
2018-12-08 21:51:06 +00:00
|
|
|
DefKind, DefLoc, DefId, Path, PathKind, HirDatabase, SourceItemId, SourceFileItemId, Crate,
|
2018-11-28 00:42:26 +00:00
|
|
|
arena::{Arena, Id},
|
|
|
|
};
|
|
|
|
|
2018-12-24 19:32:39 +00:00
|
|
|
pub use self::nameres::{ModuleScope, Resolution, Namespace, PerNs};
|
2018-11-28 00:42:26 +00:00
|
|
|
|
|
|
|
/// `Module` is API entry point to get all the information
|
|
|
|
/// about a particular module.
|
|
|
|
#[derive(Debug, Clone)]
|
2018-11-28 01:09:44 +00:00
|
|
|
pub struct Module {
|
2018-11-28 00:42:26 +00:00
|
|
|
tree: Arc<ModuleTree>,
|
2018-12-04 20:44:00 +00:00
|
|
|
pub(crate) source_root_id: SourceRootId,
|
|
|
|
pub(crate) module_id: ModuleId,
|
2018-11-28 00:42:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Module {
|
|
|
|
pub(super) fn new(
|
|
|
|
db: &impl HirDatabase,
|
|
|
|
source_root_id: SourceRootId,
|
|
|
|
module_id: ModuleId,
|
|
|
|
) -> Cancelable<Module> {
|
|
|
|
let module_tree = db.module_tree(source_root_id)?;
|
|
|
|
let res = Module {
|
|
|
|
tree: module_tree,
|
|
|
|
source_root_id,
|
|
|
|
module_id,
|
|
|
|
};
|
|
|
|
Ok(res)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns `mod foo;` or `mod foo {}` node whihc declared this module.
|
|
|
|
/// Returns `None` for the root module
|
|
|
|
pub fn parent_link_source(&self, db: &impl HirDatabase) -> Option<(FileId, ast::ModuleNode)> {
|
|
|
|
let link = self.module_id.parent_link(&self.tree)?;
|
|
|
|
let file_id = link.owner(&self.tree).source(&self.tree).file_id();
|
|
|
|
let src = link.bind_source(&self.tree, db);
|
|
|
|
Some((file_id, src))
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn source(&self) -> ModuleSource {
|
|
|
|
self.module_id.source(&self.tree)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Parent module. Returns `None` if this is a root module.
|
|
|
|
pub fn parent(&self) -> Option<Module> {
|
|
|
|
let parent_id = self.module_id.parent(&self.tree)?;
|
|
|
|
Some(Module {
|
|
|
|
module_id: parent_id,
|
|
|
|
..self.clone()
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2018-12-08 21:51:06 +00:00
|
|
|
/// Returns the crate this module is part of.
|
|
|
|
pub fn krate(&self, db: &impl HirDatabase) -> Option<Crate> {
|
|
|
|
let root_id = self.module_id.crate_root(&self.tree);
|
|
|
|
let file_id = root_id.source(&self.tree).file_id();
|
|
|
|
let crate_graph = db.crate_graph();
|
|
|
|
let crate_id = crate_graph.crate_id_for_crate_root(file_id)?;
|
|
|
|
Some(Crate::new(crate_id))
|
|
|
|
}
|
|
|
|
|
2018-11-28 00:42:26 +00:00
|
|
|
/// The root of the tree this module is part of
|
|
|
|
pub fn crate_root(&self) -> Module {
|
|
|
|
let root_id = self.module_id.crate_root(&self.tree);
|
|
|
|
Module {
|
|
|
|
module_id: root_id,
|
|
|
|
..self.clone()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// `name` is `None` for the crate's root module
|
|
|
|
pub fn name(&self) -> Option<SmolStr> {
|
|
|
|
let link = self.module_id.parent_link(&self.tree)?;
|
|
|
|
Some(link.name(&self.tree))
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn def_id(&self, db: &impl HirDatabase) -> DefId {
|
2018-12-04 20:01:53 +00:00
|
|
|
let def_loc = DefLoc {
|
|
|
|
kind: DefKind::Module,
|
|
|
|
source_root_id: self.source_root_id,
|
|
|
|
module_id: self.module_id,
|
|
|
|
source_item_id: self.module_id.source(&self.tree).0,
|
2018-11-28 00:42:26 +00:00
|
|
|
};
|
|
|
|
def_loc.id(db)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Finds a child module with the specified name.
|
|
|
|
pub fn child(&self, name: &str) -> Option<Module> {
|
|
|
|
let child_id = self.module_id.child(&self.tree, name)?;
|
|
|
|
Some(Module {
|
|
|
|
module_id: child_id,
|
|
|
|
..self.clone()
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns a `ModuleScope`: a set of items, visible in this module.
|
2018-11-28 01:09:44 +00:00
|
|
|
pub fn scope(&self, db: &impl HirDatabase) -> Cancelable<ModuleScope> {
|
2018-11-28 00:42:26 +00:00
|
|
|
let item_map = db.item_map(self.source_root_id)?;
|
|
|
|
let res = item_map.per_module[&self.module_id].clone();
|
|
|
|
Ok(res)
|
|
|
|
}
|
|
|
|
|
2018-12-25 20:14:13 +00:00
|
|
|
pub fn resolve_path(&self, db: &impl HirDatabase, path: &Path) -> Cancelable<PerNs<DefId>> {
|
2018-12-24 19:32:39 +00:00
|
|
|
let mut curr_per_ns = PerNs::types(
|
|
|
|
match path.kind {
|
|
|
|
PathKind::Crate => self.crate_root(),
|
|
|
|
PathKind::Self_ | PathKind::Plain => self.clone(),
|
|
|
|
PathKind::Super => {
|
|
|
|
if let Some(p) = self.parent() {
|
|
|
|
p
|
|
|
|
} else {
|
|
|
|
return Ok(PerNs::none());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
.def_id(db),
|
|
|
|
);
|
2018-11-28 00:42:26 +00:00
|
|
|
|
2018-12-25 20:14:13 +00:00
|
|
|
let segments = &path.segments;
|
2018-11-28 00:42:26 +00:00
|
|
|
for name in segments.iter() {
|
2018-12-24 19:32:39 +00:00
|
|
|
let curr = if let Some(r) = curr_per_ns.as_ref().take(Namespace::Types) {
|
|
|
|
r
|
|
|
|
} else {
|
|
|
|
return Ok(PerNs::none());
|
|
|
|
};
|
2018-11-28 00:42:26 +00:00
|
|
|
let module = match curr.loc(db) {
|
2018-12-04 20:01:53 +00:00
|
|
|
DefLoc {
|
|
|
|
kind: DefKind::Module,
|
|
|
|
source_root_id,
|
|
|
|
module_id,
|
|
|
|
..
|
|
|
|
} => Module::new(db, source_root_id, module_id)?,
|
2018-12-24 19:32:39 +00:00
|
|
|
// TODO here would be the place to handle enum variants...
|
|
|
|
_ => return Ok(PerNs::none()),
|
2018-11-28 00:42:26 +00:00
|
|
|
};
|
|
|
|
let scope = module.scope(db)?;
|
2018-12-24 19:32:39 +00:00
|
|
|
curr_per_ns = if let Some(r) = scope.get(&name) {
|
|
|
|
r.def_id
|
|
|
|
} else {
|
|
|
|
return Ok(PerNs::none());
|
|
|
|
};
|
2018-11-28 00:42:26 +00:00
|
|
|
}
|
2018-12-24 19:32:39 +00:00
|
|
|
Ok(curr_per_ns)
|
2018-11-28 00:42:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn problems(&self, db: &impl HirDatabase) -> Vec<(SyntaxNode, Problem)> {
|
|
|
|
self.module_id.problems(&self.tree, db)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-24 19:32:39 +00:00
|
|
|
/// Physically, rust source is organized as a set of files, but logically it is
|
2018-11-28 00:42:26 +00: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 01:09:44 +00:00
|
|
|
pub struct ModuleTree {
|
2018-11-28 00:42:26 +00:00
|
|
|
mods: Arena<ModuleData>,
|
|
|
|
links: Arena<LinkData>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ModuleTree {
|
|
|
|
pub(crate) fn modules<'a>(&'a self) -> impl Iterator<Item = ModuleId> + 'a {
|
|
|
|
self.mods.iter().map(|(id, _)| id)
|
|
|
|
}
|
|
|
|
|
2018-12-05 10:20:11 +00: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 00:42:26 +00: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)]
|
2018-12-04 19:46:23 +00:00
|
|
|
pub struct ModuleSource(SourceItemId);
|
2018-11-28 00:42:26 +00:00
|
|
|
|
|
|
|
/// An owned syntax node for a module. Unlike `ModuleSource`,
|
|
|
|
/// this holds onto the AST for the whole file.
|
2018-11-28 13:24:06 +00:00
|
|
|
pub(crate) enum ModuleSourceNode {
|
2018-11-28 00:42:26 +00:00
|
|
|
SourceFile(ast::SourceFileNode),
|
|
|
|
Module(ast::ModuleNode),
|
|
|
|
}
|
|
|
|
|
2018-11-28 01:09:44 +00:00
|
|
|
pub type ModuleId = Id<ModuleData>;
|
2018-11-28 00:42:26 +00:00
|
|
|
type LinkId = Id<LinkData>;
|
|
|
|
|
|
|
|
#[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
|
|
|
|
}
|
|
|
|
fn parent_link(self, tree: &ModuleTree) -> Option<LinkId> {
|
|
|
|
tree.mods[self].parent
|
|
|
|
}
|
|
|
|
fn parent(self, tree: &ModuleTree) -> Option<ModuleId> {
|
|
|
|
let link = self.parent_link(tree)?;
|
|
|
|
Some(tree.links[link].owner)
|
|
|
|
}
|
|
|
|
fn crate_root(self, tree: &ModuleTree) -> ModuleId {
|
|
|
|
generate(Some(self), move |it| it.parent(tree))
|
|
|
|
.last()
|
|
|
|
.unwrap()
|
|
|
|
}
|
|
|
|
fn child(self, tree: &ModuleTree, name: &str) -> Option<ModuleId> {
|
|
|
|
let link = tree.mods[self]
|
|
|
|
.children
|
|
|
|
.iter()
|
|
|
|
.map(|&it| &tree.links[it])
|
|
|
|
.find(|it| it.name == name)?;
|
|
|
|
Some(*link.points_to.first()?)
|
|
|
|
}
|
|
|
|
fn children<'a>(self, tree: &'a ModuleTree) -> impl Iterator<Item = (SmolStr, ModuleId)> + 'a {
|
|
|
|
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))
|
|
|
|
})
|
|
|
|
}
|
|
|
|
fn problems(self, tree: &ModuleTree, db: &impl HirDatabase) -> Vec<(SyntaxNode, Problem)> {
|
|
|
|
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 {
|
|
|
|
fn owner(self, tree: &ModuleTree) -> ModuleId {
|
|
|
|
tree.links[self].owner
|
|
|
|
}
|
|
|
|
fn name(self, tree: &ModuleTree) -> SmolStr {
|
|
|
|
tree.links[self].name.clone()
|
|
|
|
}
|
|
|
|
fn bind_source<'a>(self, tree: &ModuleTree, db: &impl HirDatabase) -> ast::ModuleNode {
|
|
|
|
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 01:09:44 +00:00
|
|
|
pub struct ModuleData {
|
2018-11-28 00:42:26 +00:00
|
|
|
source: ModuleSource,
|
|
|
|
parent: Option<LinkId>,
|
|
|
|
children: Vec<LinkId>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ModuleSource {
|
2018-12-04 19:46:23 +00:00
|
|
|
// precondition: item_id **must** point to module
|
2018-12-18 22:48:46 +00:00
|
|
|
fn new(file_id: FileId, item_id: Option<SourceFileItemId>) -> ModuleSource {
|
2018-12-04 19:46:23 +00:00
|
|
|
let source_item_id = SourceItemId { file_id, item_id };
|
|
|
|
ModuleSource(source_item_id)
|
|
|
|
}
|
|
|
|
|
2018-12-18 22:48:46 +00:00
|
|
|
pub(crate) fn new_file(file_id: FileId) -> ModuleSource {
|
|
|
|
ModuleSource::new(file_id, None)
|
2018-12-04 19:46:23 +00:00
|
|
|
}
|
|
|
|
|
2018-11-28 00:42:26 +00:00
|
|
|
pub(crate) fn new_inline(
|
|
|
|
db: &impl HirDatabase,
|
|
|
|
file_id: FileId,
|
2018-12-04 19:46:23 +00:00
|
|
|
m: ast::Module,
|
2018-11-28 00:42:26 +00:00
|
|
|
) -> ModuleSource {
|
2018-12-04 19:46:23 +00:00
|
|
|
assert!(!m.has_semi());
|
|
|
|
let file_items = db.file_items(file_id);
|
2018-12-09 10:18:46 +00:00
|
|
|
let item_id = file_items.id_of(file_id, m.syntax());
|
2018-12-18 22:48:46 +00:00
|
|
|
ModuleSource::new(file_id, Some(item_id))
|
2018-11-28 00:42:26 +00:00
|
|
|
}
|
|
|
|
|
2018-11-28 01:09:44 +00:00
|
|
|
pub fn file_id(self) -> FileId {
|
2018-12-04 19:46:23 +00:00
|
|
|
self.0.file_id
|
2018-11-28 00:42:26 +00:00
|
|
|
}
|
|
|
|
|
2018-11-28 13:24:06 +00:00
|
|
|
pub(crate) fn resolve(self, db: &impl HirDatabase) -> ModuleSourceNode {
|
2018-12-04 19:46:23 +00: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 00:42:26 +00:00
|
|
|
}
|
2018-12-04 19:46:23 +00:00
|
|
|
let module = ast::Module::cast(syntax_node).unwrap();
|
|
|
|
ModuleSourceNode::Module(module.owned())
|
2018-11-28 00:42:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Hash, Debug, PartialEq, Eq)]
|
|
|
|
struct LinkData {
|
|
|
|
owner: ModuleId,
|
|
|
|
name: SmolStr,
|
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|