Skip to main content

php_lsp/db/
parse.rs

1//! The `parsed_doc` salsa query: parses a `SourceFile` into an `Arc<ParsedDoc>`
2//! under salsa memoization. Downstream queries (file_index, symbol_map,
3//! semantic diagnostics) depend on this one, so each file is parsed at most
4//! once per revision.
5//!
6//! `ParsedDoc` owns a self-referential bumpalo arena and cannot safely
7//! implement the structural `Update` trait — instead we wrap in a `ParsedArc`
8//! newtype whose `Update` impl uses `Arc::ptr_eq`. Every reparse produces a
9//! new `Arc`, so pointer equality is a correct (if conservative) "changed"
10//! signal: salsa never falsely backdates, and downstream queries re-run after
11//! every input text change.
12
13use std::sync::Arc;
14
15use salsa::Database;
16
17use crate::db::input::SourceFile;
18use crate::document::ast::ParsedDoc;
19
20/// Opaque handle to a parsed document. Cheap to clone (refcount bump); never
21/// compared structurally. See module docs for the `Update` contract.
22///
23/// No `Debug` impl because `ParsedDoc` isn't `Debug` (it owns raw pointers
24/// into a bumpalo arena). Salsa doesn't require `Debug` on tracked returns
25/// when `no_eq` is used.
26#[derive(Clone)]
27pub struct ParsedArc(pub Arc<ParsedDoc>);
28
29impl ParsedArc {
30    pub fn get(&self) -> &ParsedDoc {
31        &self.0
32    }
33}
34
35// SAFETY: The `ptr_eq` short-circuit returns `false` without writing, matching
36// salsa's "no observable change" contract. `ParsedDoc` is already `Send + Sync`
37// (see `ast.rs:98`).
38crate::impl_arc_update!(ParsedArc);
39
40/// Parse the file's source text. `no_eq` because `ParsedArc` has no
41/// structural equality — invalidation is driven entirely by input changes,
42/// not by comparing the new value against the old one.
43///
44/// Phase F: `lru = 2048` bounds the number of cached ASTs. Parsed docs own
45/// bumpalo arenas and are the largest memoized values in the db; dropping
46/// older entries caps resident memory at roughly 2048 × avg_ast_size.
47/// Re-reads after eviction reparse from the live `SourceFile::text_input` input
48/// (cheap `Arc<str>` clone). This replaces the hand-written
49/// `DocumentStore::indexed_order` LRU that used to bound `Document` entries.
50#[salsa::tracked(no_eq, lru = 2048)]
51pub fn parsed_doc(db: &dyn Database, file: SourceFile<'_>) -> ParsedArc {
52    // Pass Arc<str> directly — refcount bump, no heap allocation on the hot path.
53    let doc = ParsedDoc::parse(file.text_input(db).text(db));
54    ParsedArc(Arc::new(doc))
55}
56
57/// Parse-error count, derived from `parsed_doc`. Kept as a separate query so
58/// callers that only need the diagnostic count don't clone the parsed AST.
59#[salsa::tracked(lru = 2048)]
60pub fn parse_error_count(db: &dyn Database, file: SourceFile<'_>) -> usize {
61    parsed_doc(db, file).get().errors.len()
62}
63
64#[cfg(test)]
65mod tests {
66    use std::sync::Arc;
67    use std::sync::atomic::{AtomicUsize, Ordering};
68
69    use super::*;
70    use crate::db::analysis::AnalysisHost;
71    use crate::db::input::{FileText, Workspace, workspace_files};
72    use salsa::Setter;
73
74    static CALLS: AtomicUsize = AtomicUsize::new(0);
75
76    #[salsa::tracked]
77    fn counted_parse(db: &dyn Database, file: SourceFile<'_>) -> usize {
78        CALLS.fetch_add(1, Ordering::SeqCst);
79        parsed_doc(db, file).get().errors.len()
80    }
81
82    fn make_ws(host: &AnalysisHost, uri: &str, ft: FileText) -> Workspace {
83        Workspace::new(
84            host.db(),
85            std::sync::Arc::from([(Arc::<str>::from(uri), ft)]),
86            mir_analyzer::PhpVersion::LATEST,
87        )
88    }
89
90    #[test]
91    fn parsed_doc_returns_ast() {
92        let host = AnalysisHost::new();
93        let ft = FileText::new(
94            host.db(),
95            Arc::<str>::from("<?php\nfunction greet() {}"),
96            None,
97        );
98        let ws = make_ws(&host, "file:///t.php", ft);
99        let files = workspace_files(host.db(), ws);
100        let arc = parsed_doc(host.db(), files[0]);
101        assert!(arc.get().errors.is_empty());
102        assert!(!arc.get().program().stmts.is_empty());
103    }
104
105    #[test]
106    fn parsed_doc_memoizes_and_invalidates() {
107        CALLS.store(0, Ordering::SeqCst);
108        let mut host = AnalysisHost::new();
109        let ft = FileText::new(host.db(), Arc::<str>::from("<?php\nfunction a() {}"), None);
110        let ws = make_ws(&host, "file:///t.php", ft);
111        {
112            let files = workspace_files(host.db(), ws);
113            let _ = counted_parse(host.db(), files[0]);
114            let _ = counted_parse(host.db(), files[0]);
115            assert_eq!(
116                CALLS.load(Ordering::SeqCst),
117                1,
118                "salsa should memoize the second call with unchanged input"
119            );
120        }
121
122        ft.set_text(host.db_mut())
123            .to(Arc::<str>::from("<?php\nclass {"));
124        {
125            let files = workspace_files(host.db(), ws);
126            let _ = counted_parse(host.db(), files[0]);
127            assert_eq!(
128                CALLS.load(Ordering::SeqCst),
129                2,
130                "downstream query should re-run after input text changes"
131            );
132        }
133    }
134
135    #[test]
136    fn parse_error_count_reflects_diagnostics() {
137        let host = AnalysisHost::new();
138        let ft = FileText::new(host.db(), Arc::<str>::from("<?php\nclass {"), None);
139        let ws = make_ws(&host, "file:///t.php", ft);
140        let files = workspace_files(host.db(), ws);
141        assert!(parse_error_count(host.db(), files[0]) > 0);
142    }
143}