2019-11-03 14:49:44 -06:00
|
|
|
//! This module implements import-resolution/macro expansion algorithm.
|
|
|
|
//!
|
|
|
|
//! The result of this module is `CrateDefMap`: a data structure which contains:
|
|
|
|
//!
|
|
|
|
//! * a tree of modules for the crate
|
|
|
|
//! * for each module, a set of items visible in the module (directly declared
|
|
|
|
//! or imported)
|
|
|
|
//!
|
|
|
|
//! Note that `CrateDefMap` contains fully macro expanded code.
|
|
|
|
//!
|
|
|
|
//! Computing `CrateDefMap` can be partitioned into several logically
|
|
|
|
//! independent "phases". The phases are mutually recursive though, there's no
|
|
|
|
//! strict ordering.
|
|
|
|
//!
|
|
|
|
//! ## Collecting RawItems
|
|
|
|
//!
|
2019-11-08 15:23:19 -06:00
|
|
|
//! This happens in the `raw` module, which parses a single source file into a
|
|
|
|
//! set of top-level items. Nested imports are desugared to flat imports in this
|
|
|
|
//! phase. Macro calls are represented as a triple of (Path, Option<Name>,
|
|
|
|
//! TokenTree).
|
2019-11-03 14:49:44 -06:00
|
|
|
//!
|
|
|
|
//! ## Collecting Modules
|
|
|
|
//!
|
|
|
|
//! This happens in the `collector` module. In this phase, we recursively walk
|
|
|
|
//! tree of modules, collect raw items from submodules, populate module scopes
|
|
|
|
//! with defined items (so, we assign item ids in this phase) and record the set
|
|
|
|
//! of unresolved imports and macros.
|
|
|
|
//!
|
|
|
|
//! While we walk tree of modules, we also record macro_rules definitions and
|
|
|
|
//! expand calls to macro_rules defined macros.
|
|
|
|
//!
|
|
|
|
//! ## Resolving Imports
|
|
|
|
//!
|
|
|
|
//! We maintain a list of currently unresolved imports. On every iteration, we
|
|
|
|
//! try to resolve some imports from this list. If the import is resolved, we
|
|
|
|
//! record it, by adding an item to current module scope and, if necessary, by
|
|
|
|
//! recursively populating glob imports.
|
|
|
|
//!
|
|
|
|
//! ## Resolving Macros
|
|
|
|
//!
|
|
|
|
//! macro_rules from the same crate use a global mutable namespace. We expand
|
|
|
|
//! them immediately, when we collect modules.
|
|
|
|
//!
|
|
|
|
//! Macros from other crates (including proc-macros) can be used with
|
|
|
|
//! `foo::bar!` syntax. We handle them similarly to imports. There's a list of
|
|
|
|
//! unexpanded macros. On every iteration, we try to resolve each macro call
|
2019-11-08 15:23:19 -06:00
|
|
|
//! path and, upon success, we run macro expansion and "collect module" phase on
|
|
|
|
//! the result
|
2019-10-30 09:40:13 -05:00
|
|
|
|
2019-11-23 07:53:16 -06:00
|
|
|
pub(crate) mod raw;
|
2019-11-04 12:42:25 -06:00
|
|
|
mod collector;
|
|
|
|
mod mod_resolution;
|
2019-11-08 15:17:17 -06:00
|
|
|
mod path_resolution;
|
2019-10-31 10:45:10 -05:00
|
|
|
|
2019-11-03 14:35:48 -06:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests;
|
|
|
|
|
2019-10-31 10:45:10 -05:00
|
|
|
use std::sync::Arc;
|
|
|
|
|
2019-11-23 02:14:10 -06:00
|
|
|
use hir_expand::{
|
|
|
|
ast_id_map::FileAstId, diagnostics::DiagnosticSink, either::Either, name::Name, MacroDefId,
|
|
|
|
Source,
|
|
|
|
};
|
2019-10-31 10:45:10 -05:00
|
|
|
use once_cell::sync::Lazy;
|
|
|
|
use ra_arena::Arena;
|
|
|
|
use ra_db::{CrateId, Edition, FileId};
|
|
|
|
use ra_prof::profile;
|
|
|
|
use ra_syntax::ast;
|
2019-11-24 04:57:45 -06:00
|
|
|
use rustc_hash::FxHashMap;
|
2019-10-31 10:45:10 -05:00
|
|
|
|
|
|
|
use crate::{
|
|
|
|
builtin_type::BuiltinType,
|
2019-11-23 05:44:43 -06:00
|
|
|
db::DefDatabase,
|
2019-11-23 07:53:16 -06:00
|
|
|
nameres::{diagnostics::DefDiagnostic, path_resolution::ResolveMode},
|
2019-11-08 15:17:17 -06:00
|
|
|
path::Path,
|
2019-11-23 07:53:16 -06:00
|
|
|
per_ns::PerNs,
|
2019-11-23 07:49:53 -06:00
|
|
|
AstId, FunctionId, ImplId, LocalImportId, LocalModuleId, ModuleDefId, ModuleId, TraitId,
|
2019-10-31 10:45:10 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
/// Contains all top-level defs from a macro-expanded crate
|
|
|
|
#[derive(Debug, PartialEq, Eq)]
|
|
|
|
pub struct CrateDefMap {
|
2019-11-24 09:05:12 -06:00
|
|
|
pub root: LocalModuleId,
|
2019-11-24 09:48:29 -06:00
|
|
|
pub modules: Arena<LocalModuleId, ModuleData>,
|
2019-11-24 09:05:12 -06:00
|
|
|
pub(crate) krate: CrateId,
|
2019-10-31 10:45:10 -05:00
|
|
|
/// The prelude module for this crate. This either comes from an import
|
|
|
|
/// marked with the `prelude_import` attribute, or (in the normal case) from
|
|
|
|
/// a dependency (`std` or `core`).
|
2019-11-24 09:05:12 -06:00
|
|
|
pub(crate) prelude: Option<ModuleId>,
|
|
|
|
pub(crate) extern_prelude: FxHashMap<Name, ModuleDefId>,
|
2019-10-31 10:45:10 -05:00
|
|
|
|
2019-11-24 09:05:12 -06:00
|
|
|
edition: Edition,
|
2019-10-31 10:45:10 -05:00
|
|
|
diagnostics: Vec<DefDiagnostic>,
|
|
|
|
}
|
|
|
|
|
2019-11-23 07:49:53 -06:00
|
|
|
impl std::ops::Index<LocalModuleId> for CrateDefMap {
|
2019-10-31 10:45:10 -05:00
|
|
|
type Output = ModuleData;
|
2019-11-23 07:49:53 -06:00
|
|
|
fn index(&self, id: LocalModuleId) -> &ModuleData {
|
2019-10-31 10:45:10 -05:00
|
|
|
&self.modules[id]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Default, Debug, PartialEq, Eq)]
|
|
|
|
pub struct ModuleData {
|
2019-11-23 07:49:53 -06:00
|
|
|
pub parent: Option<LocalModuleId>,
|
|
|
|
pub children: FxHashMap<Name, LocalModuleId>,
|
2019-10-31 10:45:10 -05:00
|
|
|
pub scope: ModuleScope,
|
2019-11-23 02:14:10 -06:00
|
|
|
|
|
|
|
// FIXME: these can't be both null, we need a three-state enum here.
|
2019-10-31 10:45:10 -05:00
|
|
|
/// None for root
|
|
|
|
pub declaration: Option<AstId<ast::Module>>,
|
|
|
|
/// None for inline modules.
|
|
|
|
///
|
|
|
|
/// Note that non-inline modules, by definition, live inside non-macro file.
|
|
|
|
pub definition: Option<FileId>,
|
2019-11-23 02:14:10 -06:00
|
|
|
|
2019-11-15 10:32:56 -06:00
|
|
|
pub impls: Vec<ImplId>,
|
2019-10-31 10:45:10 -05:00
|
|
|
}
|
|
|
|
|
2019-11-15 16:00:00 -06:00
|
|
|
#[derive(Default, Debug, PartialEq, Eq)]
|
2019-11-15 05:47:26 -06:00
|
|
|
pub(crate) struct Declarations {
|
|
|
|
fns: FxHashMap<FileAstId<ast::FnDef>, FunctionId>,
|
|
|
|
}
|
|
|
|
|
2019-11-15 16:00:00 -06:00
|
|
|
#[derive(Debug, Default, PartialEq, Eq)]
|
2019-10-31 10:45:10 -05:00
|
|
|
pub struct ModuleScope {
|
2019-11-15 10:04:00 -06:00
|
|
|
items: FxHashMap<Name, Resolution>,
|
2019-10-31 10:45:10 -05:00
|
|
|
/// Macros visable in current module in legacy textual scope
|
|
|
|
///
|
|
|
|
/// For macros invoked by an unquatified identifier like `bar!()`, `legacy_macros` will be searched in first.
|
|
|
|
/// If it yields no result, then it turns to module scoped `macros`.
|
|
|
|
/// It macros with name quatified with a path like `crate::foo::bar!()`, `legacy_macros` will be skipped,
|
|
|
|
/// and only normal scoped `macros` will be searched in.
|
|
|
|
///
|
|
|
|
/// Note that this automatically inherit macros defined textually before the definition of module itself.
|
|
|
|
///
|
|
|
|
/// Module scoped macros will be inserted into `items` instead of here.
|
|
|
|
// FIXME: Macro shadowing in one module is not properly handled. Non-item place macros will
|
|
|
|
// be all resolved to the last one defined if shadowing happens.
|
|
|
|
legacy_macros: FxHashMap<Name, MacroDefId>,
|
|
|
|
}
|
|
|
|
|
|
|
|
static BUILTIN_SCOPE: Lazy<FxHashMap<Name, Resolution>> = Lazy::new(|| {
|
|
|
|
BuiltinType::ALL
|
|
|
|
.iter()
|
|
|
|
.map(|(name, ty)| {
|
|
|
|
(name.clone(), Resolution { def: PerNs::types(ty.clone().into()), import: None })
|
|
|
|
})
|
|
|
|
.collect()
|
|
|
|
});
|
|
|
|
|
|
|
|
/// Legacy macros can only be accessed through special methods like `get_legacy_macros`.
|
|
|
|
/// Other methods will only resolve values, types and module scoped macros only.
|
|
|
|
impl ModuleScope {
|
|
|
|
pub fn entries<'a>(&'a self) -> impl Iterator<Item = (&'a Name, &'a Resolution)> + 'a {
|
|
|
|
//FIXME: shadowing
|
|
|
|
self.items.iter().chain(BUILTIN_SCOPE.iter())
|
|
|
|
}
|
|
|
|
|
2019-11-20 05:22:06 -06:00
|
|
|
pub fn declarations(&self) -> impl Iterator<Item = ModuleDefId> + '_ {
|
|
|
|
self.entries()
|
|
|
|
.filter_map(|(_name, res)| if res.import.is_none() { Some(res.def) } else { None })
|
|
|
|
.flat_map(|per_ns| {
|
|
|
|
per_ns.take_types().into_iter().chain(per_ns.take_values().into_iter())
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-10-31 10:45:10 -05:00
|
|
|
/// Iterate over all module scoped macros
|
|
|
|
pub fn macros<'a>(&'a self) -> impl Iterator<Item = (&'a Name, MacroDefId)> + 'a {
|
|
|
|
self.items
|
|
|
|
.iter()
|
2019-11-24 08:00:10 -06:00
|
|
|
.filter_map(|(name, res)| res.def.take_macros().map(|macro_| (name, macro_)))
|
2019-10-31 10:45:10 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Iterate over all legacy textual scoped macros visable at the end of the module
|
|
|
|
pub fn legacy_macros<'a>(&'a self) -> impl Iterator<Item = (&'a Name, MacroDefId)> + 'a {
|
|
|
|
self.legacy_macros.iter().map(|(name, def)| (name, *def))
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Get a name from current module scope, legacy macros are not included
|
|
|
|
pub fn get(&self, name: &Name) -> Option<&Resolution> {
|
|
|
|
self.items.get(name).or_else(|| BUILTIN_SCOPE.get(name))
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn traits<'a>(&'a self) -> impl Iterator<Item = TraitId> + 'a {
|
|
|
|
self.items.values().filter_map(|r| match r.def.take_types() {
|
|
|
|
Some(ModuleDefId::TraitId(t)) => Some(t),
|
|
|
|
_ => None,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
fn get_legacy_macro(&self, name: &Name) -> Option<MacroDefId> {
|
|
|
|
self.legacy_macros.get(name).copied()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, Clone, PartialEq, Eq, Default)]
|
|
|
|
pub struct Resolution {
|
|
|
|
/// None for unresolved
|
|
|
|
pub def: PerNs,
|
|
|
|
/// ident by which this is imported into local scope.
|
2019-11-23 07:49:05 -06:00
|
|
|
pub import: Option<LocalImportId>,
|
2019-10-31 10:45:10 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
impl CrateDefMap {
|
|
|
|
pub(crate) fn crate_def_map_query(
|
|
|
|
// Note that this doesn't have `+ AstDatabase`!
|
|
|
|
// This gurantess that `CrateDefMap` is stable across reparses.
|
2019-11-23 05:44:43 -06:00
|
|
|
db: &impl DefDatabase,
|
2019-10-31 10:45:10 -05:00
|
|
|
krate: CrateId,
|
|
|
|
) -> Arc<CrateDefMap> {
|
|
|
|
let _p = profile("crate_def_map_query");
|
|
|
|
let def_map = {
|
|
|
|
let crate_graph = db.crate_graph();
|
|
|
|
let edition = crate_graph.edition(krate);
|
2019-11-23 07:49:53 -06:00
|
|
|
let mut modules: Arena<LocalModuleId, ModuleData> = Arena::default();
|
2019-10-31 10:45:10 -05:00
|
|
|
let root = modules.alloc(ModuleData::default());
|
|
|
|
CrateDefMap {
|
|
|
|
krate,
|
|
|
|
edition,
|
|
|
|
extern_prelude: FxHashMap::default(),
|
|
|
|
prelude: None,
|
|
|
|
root,
|
|
|
|
modules,
|
|
|
|
diagnostics: Vec::new(),
|
|
|
|
}
|
|
|
|
};
|
|
|
|
let def_map = collector::collect_defs(db, def_map);
|
|
|
|
Arc::new(def_map)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn add_diagnostics(
|
|
|
|
&self,
|
2019-11-23 05:44:43 -06:00
|
|
|
db: &impl DefDatabase,
|
2019-11-23 07:49:53 -06:00
|
|
|
module: LocalModuleId,
|
2019-10-31 10:45:10 -05:00
|
|
|
sink: &mut DiagnosticSink,
|
|
|
|
) {
|
|
|
|
self.diagnostics.iter().for_each(|it| it.add_to(db, module, sink))
|
|
|
|
}
|
|
|
|
|
2019-11-23 07:49:53 -06:00
|
|
|
pub fn modules_for_file(&self, file_id: FileId) -> impl Iterator<Item = LocalModuleId> + '_ {
|
2019-11-15 01:26:31 -06:00
|
|
|
self.modules
|
|
|
|
.iter()
|
|
|
|
.filter(move |(_id, data)| data.definition == Some(file_id))
|
|
|
|
.map(|(id, _data)| id)
|
|
|
|
}
|
2019-11-24 04:34:27 -06:00
|
|
|
|
|
|
|
pub(crate) fn resolve_path(
|
|
|
|
&self,
|
|
|
|
db: &impl DefDatabase,
|
|
|
|
original_module: LocalModuleId,
|
|
|
|
path: &Path,
|
|
|
|
) -> (PerNs, Option<usize>) {
|
|
|
|
let res = self.resolve_path_fp_with_macro(db, ResolveMode::Other, original_module, path);
|
|
|
|
(res.resolved_def, res.segment_index)
|
|
|
|
}
|
2019-10-31 10:45:10 -05:00
|
|
|
}
|
|
|
|
|
2019-11-23 02:14:10 -06:00
|
|
|
impl ModuleData {
|
|
|
|
/// Returns a node which defines this module. That is, a file or a `mod foo {}` with items.
|
|
|
|
pub fn definition_source(
|
|
|
|
&self,
|
2019-11-23 05:44:43 -06:00
|
|
|
db: &impl DefDatabase,
|
2019-11-23 02:14:10 -06:00
|
|
|
) -> Source<Either<ast::SourceFile, ast::Module>> {
|
|
|
|
if let Some(file_id) = self.definition {
|
|
|
|
let sf = db.parse(file_id).tree();
|
|
|
|
return Source::new(file_id.into(), Either::A(sf));
|
|
|
|
}
|
|
|
|
let decl = self.declaration.unwrap();
|
|
|
|
Source::new(decl.file_id(), Either::B(decl.to_node(db)))
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns a node which declares this module, either a `mod foo;` or a `mod foo {}`.
|
|
|
|
/// `None` for the crate root.
|
2019-11-23 05:44:43 -06:00
|
|
|
pub fn declaration_source(&self, db: &impl DefDatabase) -> Option<Source<ast::Module>> {
|
2019-11-23 02:14:10 -06:00
|
|
|
let decl = self.declaration?;
|
|
|
|
let value = decl.to_node(db);
|
|
|
|
Some(Source { file_id: decl.file_id(), value })
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-31 10:45:10 -05:00
|
|
|
mod diagnostics {
|
|
|
|
use hir_expand::diagnostics::DiagnosticSink;
|
2019-11-03 16:14:17 -06:00
|
|
|
use ra_db::RelativePathBuf;
|
2019-10-31 10:45:10 -05:00
|
|
|
use ra_syntax::{ast, AstPtr};
|
|
|
|
|
2019-11-23 07:49:53 -06:00
|
|
|
use crate::{db::DefDatabase, diagnostics::UnresolvedModule, nameres::LocalModuleId, AstId};
|
2019-10-31 10:45:10 -05:00
|
|
|
|
|
|
|
#[derive(Debug, PartialEq, Eq)]
|
|
|
|
pub(super) enum DefDiagnostic {
|
|
|
|
UnresolvedModule {
|
2019-11-23 07:49:53 -06:00
|
|
|
module: LocalModuleId,
|
2019-10-31 10:45:10 -05:00
|
|
|
declaration: AstId<ast::Module>,
|
|
|
|
candidate: RelativePathBuf,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
impl DefDiagnostic {
|
|
|
|
pub(super) fn add_to(
|
|
|
|
&self,
|
2019-11-23 05:44:43 -06:00
|
|
|
db: &impl DefDatabase,
|
2019-11-23 07:49:53 -06:00
|
|
|
target_module: LocalModuleId,
|
2019-10-31 10:45:10 -05:00
|
|
|
sink: &mut DiagnosticSink,
|
|
|
|
) {
|
|
|
|
match self {
|
|
|
|
DefDiagnostic::UnresolvedModule { module, declaration, candidate } => {
|
|
|
|
if *module != target_module {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
let decl = declaration.to_node(db);
|
|
|
|
sink.push(UnresolvedModule {
|
|
|
|
file: declaration.file_id(),
|
|
|
|
decl: AstPtr::new(&decl),
|
|
|
|
candidate: candidate.clone(),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|