Skip to main content

xur_resolve/
program.rs

1use std::io;
2use std::path::Path;
3
4use xur_errors::Diag;
5use xur_span::FileLoader;
6
7use crate::NativeProvider;
8
9use crate::loader::{Loader, normalize};
10use crate::{Crate, LangItems, ModuleIdx, Resolutions, resolve_modules};
11
12/// A loaded, name-resolved program that can grow while it runs.
13///
14/// [`Program::load`] loads the root file and every module it imports with
15/// a literal path. [`Program::import`] adds more modules later, as a
16/// dynamic `std::import` does; existing definitions, node ids and spans stay
17/// valid.
18pub struct Program {
19    pub krate: Crate,
20    natives: Box<dyn NativeProvider>,
21    /// Only `None` while a batch of modules is being resolved.
22    resolutions: Option<Resolutions>,
23    loader: Loader,
24}
25
26impl Program {
27    /// Loads and resolves the program rooted at `root`, with the native
28    /// modules of `natives` (such as `std`) available by name.
29    ///
30    /// Fails only if `root` itself cannot be read; everything else wrong
31    /// with the program is returned as diagnostics.
32    pub fn load(
33        root: &Path,
34        file_loader: Box<dyn FileLoader>,
35        natives: Box<dyn NativeProvider>,
36    ) -> io::Result<(Self, Vec<Diag>)> {
37        let src = file_loader.read_file(root)?;
38        let mut loader = Loader::new(file_loader);
39        let mut diags = Vec::new();
40        let mut krate = Crate::with_prelude(&mut loader, &mut diags);
41        let root = loader.add_tree(&mut krate, normalize(root), src, &mut diags);
42        debug_assert_eq!(root, krate.root);
43
44        let resolutions = Resolutions::new(&natives.modules());
45        let (resolutions, resolve_diags) = resolve_modules(
46            &krate,
47            natives.as_ref(),
48            resolutions,
49            0..krate.modules.len(),
50        );
51        diags.extend(resolve_diags);
52        let program = Self {
53            krate,
54            natives,
55            resolutions: Some(resolutions),
56            loader,
57        };
58        Ok((program, diags))
59    }
60
61    /// The prelude definitions the language itself relies on.
62    pub fn lang_items(&self) -> LangItems {
63        LangItems::new(self)
64    }
65
66    pub fn resolutions(&self) -> &Resolutions {
67        self.resolutions
68            .as_ref()
69            .expect("resolutions are only taken while resolving")
70    }
71
72    /// Loads and resolves the module at `path` (relative to the working
73    /// directory) and anything it imports, unless already loaded.
74    ///
75    /// Fails if `path` cannot be read. Diagnostics are for the newly loaded
76    /// modules only.
77    pub fn import(&mut self, path: &Path) -> io::Result<(ModuleIdx, Vec<Diag>)> {
78        let path = normalize(path);
79        if let Some(loaded) = self.loader.loaded(&path) {
80            return Ok((loaded, Vec::new()));
81        }
82        let src = self.loader.read(&path)?;
83        let first_new = self.krate.modules.len();
84        let mut diags = Vec::new();
85        let idx = self.loader.add_tree(&mut self.krate, path, src, &mut diags);
86
87        let resolutions = self
88            .resolutions
89            .take()
90            .expect("resolutions are only taken while resolving");
91        let (resolutions, resolve_diags) = resolve_modules(
92            &self.krate,
93            self.natives.as_ref(),
94            resolutions,
95            first_new..self.krate.modules.len(),
96        );
97        self.resolutions = Some(resolutions);
98        diags.extend(resolve_diags);
99        Ok((idx, diags))
100    }
101}