-
Notifications
You must be signed in to change notification settings - Fork 545
/
db.rs
189 lines (166 loc) · 5.53 KB
/
db.rs
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
use std::sync::Arc;
use anyhow::{anyhow, Result};
use cairo_lang_defs::db::{DefsDatabase, DefsGroup};
use cairo_lang_defs::plugin::{InlineMacroExprPlugin, MacroPlugin};
use cairo_lang_filesystem::cfg::CfgSet;
use cairo_lang_filesystem::db::{
init_dev_corelib, init_files_group, AsFilesGroupMut, FilesDatabase, FilesGroup, FilesGroupEx,
CORELIB_CRATE_NAME,
};
use cairo_lang_filesystem::detect::detect_corelib;
use cairo_lang_filesystem::ids::CrateLongId;
use cairo_lang_lowering::db::{LoweringDatabase, LoweringGroup};
use cairo_lang_parser::db::ParserDatabase;
use cairo_lang_plugins::get_default_plugins;
use cairo_lang_project::ProjectConfig;
use cairo_lang_semantic::db::{SemanticDatabase, SemanticGroup};
use cairo_lang_semantic::inline_macros::get_default_inline_macro_plugins;
use cairo_lang_sierra_generator::db::SierraGenDatabase;
use cairo_lang_syntax::node::db::{SyntaxDatabase, SyntaxGroup};
use cairo_lang_utils::ordered_hash_map::OrderedHashMap;
use cairo_lang_utils::Upcast;
use crate::project::update_crate_roots_from_project_config;
#[salsa::database(
DefsDatabase,
FilesDatabase,
LoweringDatabase,
ParserDatabase,
SemanticDatabase,
SierraGenDatabase,
SyntaxDatabase
)]
pub struct RootDatabase {
storage: salsa::Storage<RootDatabase>,
}
impl salsa::Database for RootDatabase {}
impl salsa::ParallelDatabase for RootDatabase {
fn snapshot(&self) -> salsa::Snapshot<RootDatabase> {
salsa::Snapshot::new(RootDatabase { storage: self.storage.snapshot() })
}
}
impl RootDatabase {
fn new(
plugins: Vec<Arc<dyn MacroPlugin>>,
inline_macro_plugins: OrderedHashMap<String, Arc<dyn InlineMacroExprPlugin>>,
) -> Self {
let mut res = Self { storage: Default::default() };
init_files_group(&mut res);
res.set_macro_plugins(plugins);
res.set_inline_macro_plugins(inline_macro_plugins.into());
res
}
pub fn empty() -> Self {
Self::builder().clear_plugins().build().unwrap()
}
pub fn builder() -> RootDatabaseBuilder {
RootDatabaseBuilder::new()
}
/// Snapshots the db for read only.
pub fn snapshot(&self) -> RootDatabase {
RootDatabase { storage: self.storage.snapshot() }
}
}
impl Default for RootDatabase {
fn default() -> Self {
Self::builder().build().unwrap()
}
}
#[derive(Clone, Debug)]
pub struct RootDatabaseBuilder {
plugins: Vec<Arc<dyn MacroPlugin>>,
inline_macro_plugins: OrderedHashMap<String, Arc<dyn InlineMacroExprPlugin>>,
detect_corelib: bool,
project_config: Option<Box<ProjectConfig>>,
cfg_set: Option<CfgSet>,
}
impl RootDatabaseBuilder {
fn new() -> Self {
Self {
plugins: get_default_plugins(),
inline_macro_plugins: get_default_inline_macro_plugins(),
detect_corelib: false,
project_config: None,
cfg_set: None,
}
}
pub fn with_macro_plugin(&mut self, plugin: Arc<dyn MacroPlugin>) -> &mut Self {
self.plugins.push(plugin);
self
}
pub fn with_inline_macro_plugin(
&mut self,
name: impl Into<String>,
plugin: Arc<dyn InlineMacroExprPlugin>,
) -> &mut Self {
self.inline_macro_plugins.insert(name.into(), plugin);
self
}
pub fn clear_plugins(&mut self) -> &mut Self {
self.plugins.clear();
self
}
pub fn detect_corelib(&mut self) -> &mut Self {
self.detect_corelib = true;
self
}
pub fn with_project_config(&mut self, config: ProjectConfig) -> &mut Self {
self.project_config = Some(Box::new(config));
self
}
pub fn with_cfg(&mut self, cfg_set: impl Into<CfgSet>) -> &mut Self {
self.cfg_set = Some(cfg_set.into());
self
}
pub fn build(&mut self) -> Result<RootDatabase> {
// NOTE: Order of operations matters here!
// Errors if something is not OK are very subtle, mostly this results in missing
// identifier diagnostics, or panics regarding lack of corelib items.
let mut db = RootDatabase::new(self.plugins.clone(), self.inline_macro_plugins.clone());
if let Some(cfg_set) = &self.cfg_set {
db.use_cfg(cfg_set);
}
if self.detect_corelib {
let path =
detect_corelib().ok_or_else(|| anyhow!("Failed to find development corelib."))?;
init_dev_corelib(&mut db, path);
}
if let Some(config) = self.project_config.clone() {
update_crate_roots_from_project_config(&mut db, *config.clone());
if let Some(corelib) = config.corelib {
let core_crate = db.intern_crate(CrateLongId::Real(CORELIB_CRATE_NAME.into()));
db.set_crate_root(core_crate, Some(corelib));
}
}
Ok(db)
}
}
impl AsFilesGroupMut for RootDatabase {
fn as_files_group_mut(&mut self) -> &mut (dyn FilesGroup + 'static) {
self
}
}
impl Upcast<dyn FilesGroup> for RootDatabase {
fn upcast(&self) -> &(dyn FilesGroup + 'static) {
self
}
}
impl Upcast<dyn SyntaxGroup> for RootDatabase {
fn upcast(&self) -> &(dyn SyntaxGroup + 'static) {
self
}
}
impl Upcast<dyn DefsGroup> for RootDatabase {
fn upcast(&self) -> &(dyn DefsGroup + 'static) {
self
}
}
impl Upcast<dyn SemanticGroup> for RootDatabase {
fn upcast(&self) -> &(dyn SemanticGroup + 'static) {
self
}
}
impl Upcast<dyn LoweringGroup> for RootDatabase {
fn upcast(&self) -> &(dyn LoweringGroup + 'static) {
self
}
}