1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
use erg_common::config::ErgConfig;
use erg_common::pathutil::NormalizedPathBuf;
use erg_common::shared::MappedRwLockReadGuard;
use erg_common::spawn::safe_yield;

use crate::context::{Context, ModuleContext};

use super::cache::{ModuleEntry, SharedGeneralizationCache, SharedModuleCache};
use super::errors::{SharedCompileErrors, SharedCompileWarnings};
use super::graph::SharedModuleGraph;
use super::impls::SharedTraitImpls;
use super::index::SharedModuleIndex;
use super::promise::SharedPromises;

#[derive(Debug, Clone, Default)]
pub struct SharedCompilerResource {
    pub mod_cache: SharedModuleCache,
    pub py_mod_cache: SharedModuleCache,
    pub index: SharedModuleIndex,
    pub graph: SharedModuleGraph,
    /// K: name of a trait, V: (type, monomorphised trait that the type implements)
    /// K: トレイトの名前, V: (型, その型が実装する単相化トレイト)
    /// e.g. { "Named": [(Type, Named), (Func, Named), ...], "Add": [(Nat, Add(Nat)), (Int, Add(Int)), ...], ... }
    pub trait_impls: SharedTraitImpls,
    pub promises: SharedPromises,
    pub errors: SharedCompileErrors,
    pub warns: SharedCompileWarnings,
    pub gen_cache: SharedGeneralizationCache,
}

impl SharedCompilerResource {
    /// Initialize the shared compiler resource.
    /// This API is normally called only once throughout the compilation phase.
    pub fn new(cfg: ErgConfig) -> Self {
        let graph = SharedModuleGraph::new();
        let self_ = Self {
            mod_cache: SharedModuleCache::new(),
            py_mod_cache: SharedModuleCache::new(),
            index: SharedModuleIndex::new(),
            graph: graph.clone(),
            trait_impls: SharedTraitImpls::new(),
            promises: SharedPromises::new(graph, NormalizedPathBuf::from(cfg.input.path())),
            errors: SharedCompileErrors::new(),
            warns: SharedCompileWarnings::new(),
            gen_cache: SharedGeneralizationCache::new(),
        };
        Context::init_builtins(cfg, self_.clone());
        self_
    }

    pub fn inherit<P: Into<NormalizedPathBuf>>(&self, path: P) -> Self {
        let mut _self = self.clone();
        _self.promises.path = path.into();
        _self
    }

    /// Clear all but builtin modules
    pub fn clear_all(&self) {
        self.mod_cache.initialize();
        self.py_mod_cache.initialize();
        self.index.initialize();
        self.graph.initialize();
        self.trait_impls.initialize();
        self.promises.initialize();
        self.errors.clear();
        self.warns.clear();
    }

    /// Clear all information about the module.
    /// Graph information is not cleared (due to ELS).
    pub fn clear(&self, path: &NormalizedPathBuf) -> Option<ModuleEntry> {
        let mut old = None;
        for child in self.graph.children(path) {
            self.clear(&child);
        }
        if let Some(ent) = self.mod_cache.remove(path) {
            old = Some(ent);
        }
        if let Some(ent) = self.py_mod_cache.remove(path) {
            old = Some(ent);
        }
        self.index.remove_path(path);
        // self.graph.remove(path);
        self.trait_impls.remove_by_path(path);
        self.promises.remove(path);
        self.errors.remove(path);
        self.warns.remove(path);
        old
    }

    pub fn clear_path(&self, path: &NormalizedPathBuf) {
        self.mod_cache.remove(path);
        self.py_mod_cache.remove(path);
        self.index.remove_path(path);
        // self.graph.remove(path);
        self.trait_impls.remove_by_path(path);
        self.promises.remove(path);
        self.errors.remove(path);
        self.warns.remove(path);
    }

    pub fn rename_path(&self, old: &NormalizedPathBuf, new: NormalizedPathBuf) {
        self.mod_cache.rename_path(old, new.clone());
        self.py_mod_cache.rename_path(old, new.clone());
        self.index.rename_path(old, new.clone());
        self.trait_impls.rename_path(old, new.clone());
        self.graph.rename_path(old, new.clone());
        self.promises.rename(old, new);
    }

    pub fn insert_module(&self, path: NormalizedPathBuf, entry: ModuleEntry) {
        if path.to_string_lossy().ends_with(".d.er") {
            self.py_mod_cache.insert(path, entry);
        } else {
            self.mod_cache.insert(path, entry);
        }
    }

    /// This is intended to be called from a language server, etc.,
    /// this blocks until it gets a lock.
    pub fn remove_module(&self, path: &std::path::Path) -> Option<ModuleEntry> {
        if path.to_string_lossy().ends_with(".d.er") {
            loop {
                if let Ok(entry) = self.py_mod_cache.try_remove(path) {
                    return entry;
                }
                safe_yield();
            }
        } else {
            loop {
                if let Ok(entry) = self.mod_cache.try_remove(path) {
                    return entry;
                }
                safe_yield();
            }
        }
    }

    pub fn get_module(&self, path: &std::path::Path) -> Option<MappedRwLockReadGuard<ModuleEntry>> {
        if path.to_string_lossy().ends_with(".d.er") {
            self.py_mod_cache.get(path)
        } else {
            self.mod_cache.get(path)
        }
    }

    pub fn raw_ref_ctx_with_timeout(
        &self,
        path: &std::path::Path,
        timeout: std::time::Duration,
    ) -> Option<&ModuleContext> {
        if path.to_string_lossy().ends_with(".d.er") {
            self.py_mod_cache.raw_ref_ctx_with_timeout(path, timeout)
        } else {
            self.mod_cache.raw_ref_ctx_with_timeout(path, timeout)
        }
    }

    pub fn raw_ref_builtins_ctx(&self) -> Option<&ModuleContext> {
        self.mod_cache.raw_ref_builtins_ctx()
    }

    pub fn raw_modules(&self) -> impl Iterator<Item = &ModuleEntry> {
        self.mod_cache
            .raw_values()
            .chain(self.py_mod_cache.raw_values())
    }

    pub fn raw_path_and_modules(&self) -> impl Iterator<Item = (&NormalizedPathBuf, &ModuleEntry)> {
        self.mod_cache
            .raw_iter()
            .chain(self.py_mod_cache.raw_iter())
    }
}