xur-resolve 0.1.8

Module loading and name resolution for the Xur language
Documentation
//! Loading modules and resolving names, after `rustc_resolve`.
//!
//! A [`Program`] is loaded from its root file and can grow at runtime as
//! further modules are imported. Each batch of new modules is resolved in
//! phases:
//! 1. The modules reachable through literal-path `std::import`s are parsed.
//! 2. Every module's items are declared in its [`Scope`](scope::Scope).
//! 3. `use` declarations are resolved, repeatedly until no more make
//!    progress, since one import may depend on another.
//! 4. The functions of `impl` blocks are declared in their type's scope.
//! 5. The names used inside items (bodies, types, patterns) are resolved
//!    against local scopes and then the module.
//!
//! Names only ever resolve statically. Members reached through a value
//! (`math.add`, `point.x`) are not names and are left for later stages.

mod collect;
mod def;
pub(crate) mod expand;
mod ident;
mod imports;
mod lang;
mod late;
mod loader;
pub mod macros;
mod names;
mod native;
mod natives;
mod program;
mod rt_type;
mod scope;

#[cfg(test)]
mod tests;

use std::collections::HashMap;
use std::ops::Range;

use xur_ast::NodeId;
use xur_errors::Diag;
use xur_span::{Ident, Symbol};

pub use def::{DefData, DefId, DefKind, Definitions, Namespace, PrimTy, Res};
pub use lang::LangItems;
pub use loader::{Crate, LoadedModule, ModuleIdx};
pub use names::{NameRef, names};
pub use native::{
    NativeFnDecl, NativeFnId, NativeModule, NativeProvider, NativeTypeDecl, NativeTypeId,
    NativeVariantDecl, NativeVariantKind,
};
pub use program::Program;
pub use rt_type::RtType;

use imports::Import;
use scope::{Scope, Vis};

/// The results of name resolution, keyed by AST node.
#[derive(Debug)]
pub struct Resolutions {
    pub defs: Definitions,
    /// What each path and each identifier pattern refers to. A pattern that
    /// binds a variable resolves to [`Res::Local`] of itself.
    pub res: HashMap<NodeId, Res>,
    /// The name of each local variable, by the pattern that binds it.
    pub locals: HashMap<NodeId, Ident>,
    /// The definition created for each item, variant and generic parameter.
    pub node_defs: HashMap<NodeId, DefId>,
    /// The module definition of each loaded file, by [`ModuleIdx`].
    pub module_defs: Vec<DefId>,
    /// File modules whose name is left out of [`Resolutions::def_path`]: the
    /// root module and the prelude.
    unnamed_modules: Vec<DefId>,
    /// The names declared in each module and enum.
    scopes: HashMap<DefId, Scope>,
    /// The top-level native modules, such as `std`, by name.
    native_roots: HashMap<Symbol, DefId>,
    /// Lazy native modules not yet expanded, with their paths.
    lazy_natives: HashMap<DefId, Vec<String>>,
    /// Repr enum variants (native and xur-defined), keyed by their DefId.
    pub repr_values: HashMap<DefId, i64>,
}

impl Resolutions {
    /// Resolutions with nothing but the native modules `natives`.
    fn new(natives: &[NativeModule]) -> Self {
        let mut defs = Definitions::default();
        let mut scopes = HashMap::new();
        let mut lazy_natives = HashMap::new();
        let mut repr_values = HashMap::new();
        let native_roots = natives::declare(
            &mut defs,
            &mut scopes,
            &mut lazy_natives,
            &mut repr_values,
            natives,
        );
        Self {
            defs,
            res: HashMap::new(),
            locals: HashMap::new(),
            node_defs: HashMap::new(),
            module_defs: Vec::new(),
            unnamed_modules: Vec::new(),
            scopes,
            native_roots,
            lazy_natives,
            repr_values,
        }
    }

    /// Empty resolutions with no definitions. For lookups that only need
    /// the registry (no xur-defined types).
    pub fn empty() -> Self {
        Self {
            defs: Definitions::default(),
            res: HashMap::new(),
            locals: HashMap::new(),
            node_defs: HashMap::new(),
            module_defs: Vec::new(),
            unnamed_modules: Vec::new(),
            scopes: HashMap::new(),
            native_roots: HashMap::new(),
            lazy_natives: HashMap::new(),
            repr_values: HashMap::new(),
        }
    }

    /// The path a definition is known by, e.g. `std::io::println`,
    /// `math::div` or `Result::Ok`. Items of the root module and the prelude
    /// are written without a module prefix.
    pub fn def_path(&self, id: DefId) -> String {
        format_def_path(&self.defs, &self.unnamed_modules, id)
    }

    /// The binding of `name` in namespace `ns` declared directly in the
    /// module or type `scope`.
    pub fn lookup(&self, scope: DefId, ns: Namespace, name: Symbol) -> Option<Res> {
        Some(self.scopes.get(&scope)?.get(ns, name)?.res)
    }

    /// The function `name` of the struct, enum or native type `ty`: from an
    /// `impl` block, or registered with a native type. For dispatching
    /// `value.name()` at runtime.
    pub fn assoc_fn(&self, ty: DefId, name: Symbol) -> Option<Res> {
        match self.scopes.get(&ty)?.get(Namespace::Value, name)?.res {
            res @ Res::Def(DefKind::Fn | DefKind::NativeFn(_), _) => Some(res),
            _ => None,
        }
    }

    /// Every name declared in `scope` in namespace `ns`.
    pub fn scope_entries(&self, scope: DefId, ns: Namespace) -> Vec<(Symbol, Res)> {
        let Some(s) = self.scopes.get(&scope) else {
            return Vec::new();
        };
        s.iter(ns).map(|(name, b)| (name, b.res)).collect()
    }

    /// Every name declared in `scope` in namespace `ns` that may be named
    /// from module `from`: all names when `scope` belongs to `from`, and
    /// only the public ones otherwise. Mirrors the privacy check applied to
    /// `scope::name` paths, so completion only offers reachable names.
    pub fn accessible_entries(
        &self,
        scope: DefId,
        ns: Namespace,
        from: DefId,
    ) -> Vec<(Symbol, Res)> {
        let Some(s) = self.scopes.get(&scope) else {
            return Vec::new();
        };
        let own_module = s.module == from;
        s.iter(ns)
            .filter(|(_, b)| own_module || b.vis == Vis::Public)
            .map(|(name, b)| (name, b.res))
            .collect()
    }

    /// The definition of a top-level root module by name (e.g. `std`).
    pub fn root_module(&self, name: Symbol) -> Option<DefId> {
        self.native_roots.get(&name).copied()
    }

    /// All top-level root modules (currently native; will include packages).
    pub fn root_modules(&self) -> impl Iterator<Item = (Symbol, DefId)> + '_ {
        self.native_roots.iter().map(|(&name, &def)| (name, def))
    }

    /// The DefId of a struct or enum with the given name (e.g. `Result`),
    /// searching all definitions.
    pub fn find_type(&self, name: &str) -> Option<DefId> {
        self.defs.iter().find_map(|(id, data)| {
            matches!(data.kind, DefKind::Struct | DefKind::Enum)
                .then(|| (data.name.as_str() == name).then_some(id))
                .flatten()
        })
    }

    /// Every function of the struct, enum or native type `ty`, in the order
    /// they were defined.
    pub fn assoc_fns(&self, ty: DefId) -> Vec<(Symbol, DefId)> {
        let Some(scope) = self.scopes.get(&ty) else {
            return Vec::new();
        };
        let mut fns: Vec<_> = scope
            .iter(Namespace::Value)
            .filter_map(|(name, binding)| match binding.res {
                Res::Def(DefKind::Fn | DefKind::NativeFn(_), def) => Some((name, def)),
                _ => None,
            })
            .collect();
        fns.sort_by_key(|&(_, def)| def);
        fns
    }
}

/// Resolves the modules `new` of `krate`, which were added since
/// `resolutions` was last updated.
fn resolve_modules(
    krate: &Crate,
    natives: &dyn NativeProvider,
    resolutions: Resolutions,
    new: Range<usize>,
) -> (Resolutions, Vec<Diag>) {
    let mut resolver = Resolver::resume(krate, natives, resolutions);
    let new: Vec<_> = new.map(ModuleIdx).collect();
    resolver.declare_modules(&new);
    resolver.collect_items(&new);
    resolver.resolve_imports();
    resolver.collect_impls(&new);
    resolver.inject_enum_builtins();
    resolver.resolve_bodies(&new);
    resolver.inject_enum_builtins();
    resolver.suspend()
}

struct Resolver<'a> {
    krate: &'a Crate,
    natives: &'a dyn NativeProvider,
    defs: Definitions,
    /// The names declared in each module and enum.
    scopes: HashMap<DefId, Scope>,
    res: HashMap<NodeId, Res>,
    locals: HashMap<NodeId, Ident>,
    node_defs: HashMap<NodeId, DefId>,
    module_defs: Vec<DefId>,
    native_roots: HashMap<Symbol, DefId>,
    lazy_natives: HashMap<DefId, Vec<String>>,
    repr_values: HashMap<DefId, i64>,
    /// `use` declarations not yet resolved.
    pending_imports: Vec<Import<'a>>,
    /// What the type of each `impl` block resolved to, by the impl item.
    impl_self_tys: HashMap<NodeId, Res>,
    diags: Vec<Diag>,
}

impl<'a> Resolver<'a> {
    /// Picks up resolution where `resolutions` left off.
    fn resume(krate: &'a Crate, natives: &'a dyn NativeProvider, resolutions: Resolutions) -> Self {
        let Resolutions {
            defs,
            res,
            locals,
            node_defs,
            module_defs,
            unnamed_modules: _,
            scopes,
            native_roots,
            lazy_natives,
            repr_values,
        } = resolutions;
        Self {
            krate,
            natives,
            defs,
            scopes,
            res,
            locals,
            node_defs,
            module_defs,
            native_roots,
            lazy_natives,
            repr_values,
            pending_imports: Vec::new(),
            impl_self_tys: HashMap::new(),
            diags: Vec::new(),
        }
    }

    /// Creates the module definitions of the newly loaded modules `new`.
    fn declare_modules(&mut self, new: &[ModuleIdx]) {
        for &idx in new {
            debug_assert_eq!(
                idx.0,
                self.module_defs.len(),
                "modules are resolved in order"
            );
            let module = self.krate.module(idx);
            let data = DefData {
                kind: DefKind::Mod,
                name: module.name,
                parent: None,
                span: Some(module.ast.span),
            };
            let def = create_scoped_def(&mut self.defs, &mut self.scopes, data, None);
            self.module_defs.push(def);
        }
    }

    fn module_def(&self, idx: ModuleIdx) -> DefId {
        self.module_defs[idx.0]
    }

    fn prelude(&self) -> DefId {
        self.module_def(self.krate.prelude)
    }

    fn report(&mut self, diag: Diag) {
        self.diags.push(diag);
    }

    fn scope(&self, def: DefId) -> &Scope {
        &self.scopes[&def]
    }

    /// Declares the contents of `scope` if it is a lazy native module not
    /// yet expanded. Called before looking names up inside a scope.
    fn expand_lazy(&mut self, scope: DefId) {
        if let Some(path) = self.lazy_natives.remove(&scope) {
            let contents = self.natives.expand(&path);
            natives::populate(
                &mut self.defs,
                &mut self.scopes,
                &mut self.lazy_natives,
                &mut self.repr_values,
                scope,
                path,
                &contents,
            );
        }
    }

    /// Hands the accumulated state back, with the diagnostics of this batch.
    fn suspend(self) -> (Resolutions, Vec<Diag>) {
        let unnamed_modules = [self.krate.root, self.krate.prelude]
            .into_iter()
            .filter_map(|idx| self.module_defs.get(idx.0).copied())
            .collect();
        let resolutions = Resolutions {
            defs: self.defs,
            res: self.res,
            locals: self.locals,
            node_defs: self.node_defs,
            module_defs: self.module_defs,
            unnamed_modules,
            scopes: self.scopes,
            native_roots: self.native_roots,
            lazy_natives: self.lazy_natives,
            repr_values: self.repr_values,
        };
        (resolutions, self.diags)
    }
}

/// Joins the names of `id` and its parents with `::`, leaving out the name
/// of an `unnamed` outermost module unless it is `id` itself.
fn format_def_path(defs: &Definitions, unnamed: &[DefId], id: DefId) -> String {
    let lineage = defs.lineage(id);
    let skip = usize::from(lineage.len() > 1 && unnamed.contains(&lineage[0]));
    let names: Vec<_> = lineage[skip..]
        .iter()
        .map(|&id| defs.get(id).name.as_str())
        .collect();
    names.join("::")
}

/// Creates a module or enum definition along with its (empty) scope.
/// `module` is the module an enum is declared in; `None` for a module
/// itself.
fn create_scoped_def(
    defs: &mut Definitions,
    scopes: &mut HashMap<DefId, Scope>,
    data: DefData,
    module: Option<DefId>,
) -> DefId {
    let def = defs.create(data);
    scopes.insert(def, Scope::new(module.unwrap_or(def)));
    def
}