gdscript_db/lib.rs
1//! `gdscript-db` — the input layer for the analyzer.
2//!
3//! Holds the virtual file system (`FileId` → text, always injected — never `std::fs`), the
4//! project model, and (from Phase 3) the **salsa** query graph: `#[salsa::input]`s set via
5//! `apply_change`, `#[salsa::tracked]` derived queries, durability tiers. The Phase-0/1/2
6//! plain VFS map + reparse-on-change is being replaced here, localized behind the unchanged
7//! `gdscript-ide` public API (Playbook §3.M0).
8//!
9//! Crate boundary: `gdscript-db` is the *base* of the salsa stack — it owns the [`Db`] trait,
10//! the inputs, and the [`parse`] query (it may depend on `gdscript-syntax`, never on
11//! `gdscript-hir`). The higher queries (`item_tree`, `analyze_file`) live in `gdscript-hir`,
12//! which depends on this crate for `&dyn Db`. This one-way layering is what avoids a
13//! `db ↔ hir` dependency cycle.
14//!
15//! `FileId` is deliberately **not** a salsa input. The `FileId → FileText` mapping is a side
16//! table ([`Files`]) the database owns, mirroring rust-analyzer's `base-db`: `FileId`s are
17//! assigned by the client/loader and stay opaque ids, while the salsa input is the *text*.
18//!
19//! Must build for `wasm32` (single-threaded; salsa with `default-features = false`).
20#![cfg_attr(docsrs, feature(doc_cfg))]
21
22use std::sync::Arc;
23
24use dashmap::DashMap;
25use dashmap::mapref::entry::Entry;
26use gdscript_api::EngineApi;
27use gdscript_base::FileId;
28use gdscript_syntax::Parse;
29use rustc_hash::FxBuildHasher;
30use salsa::{Durability, Setter};
31
32/// The database trait `gdscript-hir` / `gdscript-ide` depend on. `#[salsa::db]` on the *trait*
33/// makes it a salsa supertrait, so any `&dyn Db` upcasts to `&dyn salsa::Database` and every
34/// `#[salsa::tracked]` free function downstream can take `db: &dyn Db`.
35/// A host/CLI-level override of the warning-strictness baseline `type_diagnostics` resolves against
36/// (regardless of `project.godot` presence). A plain (non-salsa) per-session policy knob: it is read
37/// **only** inside the non-tracked `type_diagnostics`, so it never enters the salsa query graph and
38/// cannot break the W1 firewall (a warning-level change must never re-run inference).
39#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
40pub enum WarningOverride {
41 /// Auto-select by project presence (the default): standalone ⇒ strict, project ⇒ engine defaults.
42 #[default]
43 None,
44 /// Force the strict baseline (the opt-in group promoted to WARN) even with a `project.godot`.
45 Strict,
46 /// Force Godot's engine defaults (the opt-in group stays IGNORE) even in standalone mode.
47 EngineDefaults,
48}
49
50#[salsa::db]
51pub trait Db: salsa::Database {
52 /// The text input for `file`, or `None` if no text has been set for it.
53 fn file_text(&self, file: FileId) -> Option<FileText>;
54 /// The bundled engine model, or `None` on `wasm32` (no embedded blob — the host wires the
55 /// fetched blob in via `EngineApi::from_bytes` in Phase 5).
56 fn engine(&self) -> Option<&'static EngineApi>;
57 /// The project's file set, or `None` before any file has been applied. Project-wide queries
58 /// (the global `class_name` registry) take this as their salsa-tracked input.
59 fn source_root(&self) -> Option<SourceRoot>;
60 /// The project's `project.godot` config, or `None` in single-file mode. The autoload registry
61 /// (M4) takes this as its salsa-tracked input.
62 fn project_config(&self) -> Option<ProjectConfig>;
63 /// The host-level warning-strictness override (default [`WarningOverride::None`]). A plain
64 /// field, NOT a salsa input — read only by the downstream gate, so it never re-runs inference.
65 fn warning_override(&self) -> WarningOverride;
66}
67
68/// The VFS leaf: one file's UTF-8 text, as a salsa input, plus its [`FileId`] (so a query
69/// holding only a `FileText` can recover the id for cross-file resolution) and its `res://`
70/// path (so `preload`/`extends "res://…"` resolve to the declaring file — M3).
71///
72/// `res_path` is a **separate salsa input field** from `text`: salsa tracks input fields
73/// individually (per-field `revisions`/`durabilities` — verified against salsa 0.27.1
74/// `input.rs`), so a query reading only `res_path` (the `res_path_registry`) *backdates* across
75/// a `text` keystroke — exactly the firewall that protects `file_class_name`. It is held at
76/// `MEDIUM` durability (set on file add, stable across edits); `text` stays `LOW`.
77#[salsa::input(debug)]
78pub struct FileText {
79 /// The file's full text (interned `Arc<str>`; the getter returns `&Arc<str>`).
80 #[returns(ref)]
81 pub text: Arc<str>,
82 /// The opaque file id this text belongs to.
83 pub file_id: FileId,
84 /// The file's project-relative `res://` path, if the loader supplied one (`None` in
85 /// single-file mode / tests — then `preload`/`extends "res://…"` resolve to the seam).
86 pub res_path: Option<smol_str::SmolStr>,
87}
88
89/// The project's file set — a salsa input so project-wide queries (the global `class_name`
90/// registry, M1) iterate the files incrementally. It changes only when a file is **added or
91/// removed**, never on a body edit, and is held at MEDIUM durability — so a keystroke (a `LOW`
92/// change) never invalidates project-wide derived data.
93#[salsa::input]
94pub struct SourceRoot {
95 /// Every file currently in the project, ordered by `FileId` for determinism.
96 #[returns(ref)]
97 pub files: Vec<FileText>,
98}
99
100/// The project's `project.godot`, injected as raw text — the wasm-clean core never reads the
101/// filesystem, so the loader pushes the bytes exactly like a `.gd` file. The autoload index is a
102/// tracked query that parses this text (M4). Held at `MEDIUM` durability (project structure,
103/// stable across `.gd` keystrokes), so a body edit (LOW) never invalidates the autoload registry.
104#[salsa::input]
105pub struct ProjectConfig {
106 /// The full `project.godot` text.
107 #[returns(ref)]
108 pub project_godot_text: Arc<str>,
109}
110
111/// A generation counter that makes the otherwise-untracked runtime engine model **invalidate**
112/// correctly. The engine model is a leaked `&'static` side handle (not a salsa input), so a query
113/// memoized while it was still absent (`engine() == None`, on `wasm32` before `set_engine_api`)
114/// would otherwise return that stale empty result forever. Every `engine()` read records a
115/// dependency on this input; `set_engine_api` bumps it, recomputing those queries. The *value* is
116/// irrelevant — only that setting it advances the revision. Used on `wasm32` only (native has the
117/// bundled model from the start, so it never changes — no generation tracking, no overhead).
118#[salsa::input]
119pub struct EngineGeneration {
120 /// An opaque counter (only its revision matters).
121 pub generation: u32,
122}
123
124/// The `FileId → FileText` side table. `Arc`-backed so a cheap clone shares the same map —
125/// needed to mutate an input (`&mut dyn Db`) without simultaneously borrowing `self.files`.
126#[derive(Debug, Default, Clone)]
127pub struct Files {
128 inner: Arc<DashMap<FileId, FileText, FxBuildHasher>>,
129}
130
131impl Files {
132 /// The input for `file`, if set.
133 #[must_use]
134 pub fn file_text(&self, file: FileId) -> Option<FileText> {
135 self.inner.get(&file).map(|r| *r)
136 }
137
138 /// Create or update `file`'s text input at `durability`. Creating uses `&db`; updating an
139 /// existing input bumps the revision (`&mut db`), which is what cancels live read handles.
140 pub fn set_file_text(&self, db: &mut dyn Db, file: FileId, text: &str, durability: Durability) {
141 match self.inner.entry(file) {
142 Entry::Occupied(occ) => {
143 occ.get()
144 .set_text(db)
145 .with_durability(durability)
146 .to(Arc::from(text));
147 }
148 Entry::Vacant(vac) => {
149 let ft = FileText::builder(Arc::from(text), file, None)
150 .durability(durability)
151 .new(db);
152 vac.insert(ft);
153 }
154 }
155 }
156
157 /// Set `file`'s `res://` path at `MEDIUM` durability (stable project structure, like the
158 /// source root). No-op if the file is unknown or the path is unchanged: salsa does **not**
159 /// value-backdate an input setter (it bumps the field revision on *every* call, even for an
160 /// identical value — verified against salsa 0.27.1 `input.rs:set_field`), so a redundant set
161 /// would needlessly invalidate the `res_path_registry`. The guard keeps a re-`apply_change`
162 /// of an already-known path free.
163 pub fn set_file_path(&self, db: &mut dyn Db, file: FileId, path: &str) {
164 let Some(ft) = self.inner.get(&file).map(|r| *r) else {
165 return;
166 };
167 if ft.res_path(&*db).as_deref() == Some(path) {
168 return;
169 }
170 ft.set_res_path(db)
171 .with_durability(Durability::MEDIUM)
172 .to(Some(smol_str::SmolStr::new(path)));
173 }
174
175 /// Drop `file` from the side table (its salsa input lingers, unreferenced, until GC).
176 pub fn remove(&self, file: FileId) {
177 self.inner.remove(&file);
178 }
179
180 /// Every file, ordered by `FileId` — the deterministic input to project-wide queries.
181 fn all(&self) -> Vec<FileText> {
182 let mut v: Vec<(FileId, FileText)> =
183 self.inner.iter().map(|r| (*r.key(), *r.value())).collect();
184 v.sort_by_key(|(id, _)| *id);
185 v.into_iter().map(|(_, ft)| ft).collect()
186 }
187}
188
189/// Parse a file to its lossless CST. Memoized; re-parses only when the file text changes.
190#[salsa::tracked]
191pub fn parse(db: &dyn Db, file: FileText) -> Parse {
192 gdscript_syntax::parse(file.text(db))
193}
194
195/// The concrete analyzer database — a salsa `Storage` plus the [`Files`] side table.
196#[salsa::db]
197#[derive(Clone, Default)]
198pub struct RootDatabase {
199 storage: salsa::Storage<Self>,
200 files: Files,
201 /// The project file-set input (lazily created on the first file change). Held outside salsa
202 /// as a handle so `apply_change` can update it.
203 root: Option<SourceRoot>,
204 /// The `project.godot` config input (lazily created on the first config push). Held outside
205 /// salsa as a handle so `apply_change` can update it (M4 autoloads).
206 config: Option<ProjectConfig>,
207 /// A runtime-injected engine model. `None` falls back to the bundled blob on native and to "no
208 /// engine model" on `wasm32` (where nothing is embedded). The wasm binding fetches the blob and
209 /// installs it here via [`RootDatabase::set_engine_api`] (Playbook §4.4). Held outside salsa (a
210 /// process-lifetime `&'static`, leaked once).
211 engine: Option<&'static EngineApi>,
212 /// The host-level warning-strictness override (CLI `--strict`/`--engine-defaults`). A plain
213 /// field — read only by the non-tracked `type_diagnostics`, never a salsa input.
214 warning_override: WarningOverride,
215 /// `wasm32`-only: the [`EngineGeneration`] input that makes a *later* `set_engine_api` invalidate
216 /// queries memoized while the model was still absent (so the order "query, then load the engine"
217 /// is correct, not just "load, then query"). Lazily created on the first structural change.
218 #[cfg(target_arch = "wasm32")]
219 engine_gen: Option<EngineGeneration>,
220}
221
222// `salsa::Storage` is not `Debug`, but the public `AnalysisHost`/`Analysis` that will own a
223// `RootDatabase` must stay `Debug` (frozen API); hand-impl an opaque one.
224impl std::fmt::Debug for RootDatabase {
225 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
226 f.debug_struct("RootDatabase").finish_non_exhaustive()
227 }
228}
229
230impl RootDatabase {
231 /// Create/update `file`'s text input (the single input-mutation primitive `apply_change`
232 /// drives). Clones the `Arc`-backed [`Files`] handle first so `self` is free to pass as the
233 /// `&mut dyn Db` the salsa setter needs.
234 pub fn set_file_text(&mut self, file: FileId, text: &str, durability: Durability) {
235 let files = self.files.clone();
236 files.set_file_text(self, file, text, durability);
237 }
238
239 /// Set `file`'s `res://` path (the loader supplies it on add; M3 `preload`/`extends` resolve
240 /// through it). Guarded against no-op re-sets — see [`Files::set_file_path`].
241 pub fn set_file_path(&mut self, file: FileId, path: &str) {
242 let files = self.files.clone();
243 files.set_file_path(self, file, path);
244 }
245
246 /// Remove `file`'s entry from the side table.
247 pub fn remove_file(&mut self, file: FileId) {
248 self.files.remove(file);
249 }
250
251 /// Set the host-level warning-strictness override (a CLI `--strict`/`--engine-defaults`
252 /// policy). A plain field — changing it does not touch salsa, so it never re-runs inference;
253 /// `type_diagnostics` re-reads it on the next snapshot.
254 pub fn set_warning_override(&mut self, ov: WarningOverride) {
255 self.warning_override = ov;
256 }
257
258 /// Set the project's `project.godot` text (the loader supplies it on project open / when it
259 /// changes — M4 autoloads). No-op if unchanged: salsa bumps an input field's revision on
260 /// every set even for an identical value, so a redundant push would needlessly invalidate the
261 /// autoload registry. Held at `MEDIUM` durability, so a `.gd` keystroke never touches it.
262 pub fn set_project_config(&mut self, text: &str) {
263 if let Some(cfg) = self.config {
264 if cfg.project_godot_text(self).as_ref() == text {
265 return;
266 }
267 cfg.set_project_godot_text(self)
268 .with_durability(Durability::MEDIUM)
269 .to(Arc::from(text));
270 } else {
271 self.config = Some(
272 ProjectConfig::builder(Arc::from(text))
273 .durability(Durability::MEDIUM)
274 .new(self),
275 );
276 }
277 }
278
279 /// Install a runtime-loaded engine model (the wasm path: a `fetch`ed `extension_api` blob
280 /// decoded via [`EngineApi::from_bytes`]). Leaked to `&'static` (one per session, process
281 /// lifetime). **Load-once before any query** — the engine model is not a salsa input, so a later
282 /// set would not invalidate cached reads; first-wins (a redundant install is ignored, so the
283 /// leak happens at most once). Native builds normally never call this (they fall back to the
284 /// bundled blob); it is the seam the wasm/wasip1 binding uses.
285 pub fn set_engine_api(&mut self, api: EngineApi) {
286 if self.engine.is_none() {
287 self.engine = Some(Box::leak(Box::new(api)));
288 // wasm: advance the generation so any query memoized while the model was absent (the
289 // "query before load" order) recomputes. Native never reaches here through the bindings,
290 // and its bundled model is present from the start, so it needs no generation tracking.
291 #[cfg(target_arch = "wasm32")]
292 self.bump_engine_generation();
293 }
294 }
295
296 /// wasm-only: create-or-advance the [`EngineGeneration`] input (see its docs). Creating it the
297 /// first time is harmless; advancing it invalidates every query that read `engine()`.
298 #[cfg(target_arch = "wasm32")]
299 fn bump_engine_generation(&mut self) {
300 if let Some(eg) = self.engine_gen {
301 let next = eg.generation(self).wrapping_add(1);
302 eg.set_generation(self)
303 .with_durability(Durability::MEDIUM)
304 .to(next);
305 } else {
306 self.engine_gen = Some(
307 EngineGeneration::builder(0)
308 .durability(Durability::MEDIUM)
309 .new(self),
310 );
311 }
312 }
313
314 /// Rebuild the project file-set input from the current side table. Call this from
315 /// `apply_change` **only when a file was added or removed** — never on a body edit — so the
316 /// MEDIUM-durability project input (and everything derived from it) stays stable across
317 /// keystrokes.
318 pub fn sync_source_root(&mut self) {
319 // wasm: ensure the engine generation exists before the first query runs, so every query's
320 // `engine()` read records a dependency on it — otherwise a `set_engine_api` afterwards could
321 // not invalidate a query that ran before the input existed. (The first structural change
322 // always precedes the first query, since the Session early-returns for unknown URIs.)
323 #[cfg(target_arch = "wasm32")]
324 if self.engine_gen.is_none() {
325 self.engine_gen = Some(
326 EngineGeneration::builder(0)
327 .durability(Durability::MEDIUM)
328 .new(self),
329 );
330 }
331 let files = self.files.all();
332 if let Some(root) = self.root {
333 root.set_files(self)
334 .with_durability(Durability::MEDIUM)
335 .to(files);
336 } else {
337 let root = SourceRoot::builder(files)
338 .durability(Durability::MEDIUM)
339 .new(self);
340 self.root = Some(root);
341 }
342 }
343}
344
345#[salsa::db]
346impl salsa::Database for RootDatabase {}
347
348#[salsa::db]
349impl Db for RootDatabase {
350 fn file_text(&self, file: FileId) -> Option<FileText> {
351 self.files.file_text(file)
352 }
353
354 // A runtime-injected model wins; else native falls back to the bundled blob and wasm32 to
355 // `None` (until the binding installs a fetched blob). clippy sees one target per build.
356 #[allow(clippy::unnecessary_wraps)]
357 fn engine(&self) -> Option<&'static EngineApi> {
358 // wasm: record a dependency on the generation so a later `set_engine_api` invalidates this
359 // read. (Native skips this entirely — the bundled model is constant, so zero overhead.)
360 #[cfg(target_arch = "wasm32")]
361 if let Some(eg) = self.engine_gen {
362 let _ = eg.generation(self);
363 }
364 if let Some(api) = self.engine {
365 return Some(api);
366 }
367 #[cfg(not(target_arch = "wasm32"))]
368 {
369 Some(gdscript_api::bundled())
370 }
371 #[cfg(target_arch = "wasm32")]
372 {
373 None
374 }
375 }
376
377 fn source_root(&self) -> Option<SourceRoot> {
378 self.root
379 }
380
381 fn project_config(&self) -> Option<ProjectConfig> {
382 self.config
383 }
384
385 fn warning_override(&self) -> WarningOverride {
386 self.warning_override
387 }
388}
389
390#[cfg(test)]
391mod tests {
392 use super::*;
393
394 #[test]
395 fn parse_query_returns_a_cst() {
396 let mut db = RootDatabase::default();
397 db.set_file_text(FileId(0), "func f():\n\tpass\n", Durability::LOW);
398 let ft = db.file_text(FileId(0)).unwrap();
399 let p = parse(&db, ft);
400 assert!(p.errors().is_empty());
401 // Re-querying the same input returns the memoized value (no re-parse).
402 assert_eq!(parse(&db, ft).debug_tree(), p.debug_tree());
403 }
404
405 #[test]
406 fn set_get_remove_round_trips() {
407 let mut db = RootDatabase::default();
408 let id = FileId(7);
409 db.set_file_text(id, "var x = 1\n", Durability::LOW);
410 assert_eq!(db.file_text(id).unwrap().text(&db).as_ref(), "var x = 1\n");
411 // Update in place.
412 db.set_file_text(id, "var y = 2\n", Durability::LOW);
413 assert_eq!(db.file_text(id).unwrap().text(&db).as_ref(), "var y = 2\n");
414 // Remove.
415 db.remove_file(id);
416 assert!(db.file_text(id).is_none());
417 }
418
419 #[test]
420 fn res_path_round_trips_and_guards_no_op_sets() {
421 let mut db = RootDatabase::default();
422 let id = FileId(3);
423 // No path until the loader sets one.
424 db.set_file_text(id, "class_name A\n", Durability::LOW);
425 assert_eq!(db.file_text(id).unwrap().res_path(&db), None);
426 // Set, then read back.
427 db.set_file_path(id, "res://a.gd");
428 assert_eq!(
429 db.file_text(id).unwrap().res_path(&db).as_deref(),
430 Some("res://a.gd")
431 );
432 // A re-set of the SAME path is a guarded no-op (does not panic / regress); a real rename
433 // updates it.
434 db.set_file_path(id, "res://a.gd");
435 db.set_file_path(id, "res://b.gd");
436 assert_eq!(
437 db.file_text(id).unwrap().res_path(&db).as_deref(),
438 Some("res://b.gd")
439 );
440 // Setting a path for an unknown file is a no-op (no panic).
441 db.set_file_path(FileId(999), "res://ghost.gd");
442 assert!(db.file_text(FileId(999)).is_none());
443 }
444}