Skip to main content

runec/
project.rs

1//! Compiler service for caller-owned sources. This module performs no I/O.
2use crate::bytecode::{CompiledScript, Opcode, Operand};
3use crate::clientscript::{self, Source, format::Format};
4use crate::compiler::{Compiler, compile_file};
5use crate::diagnostics::{Diagnostic, DiagnosticsCollector, Phase, Severity};
6use crate::ide::{Configs, Index};
7use crate::lexer::Lexer;
8use crate::parser::{Parser, ScriptFile};
9use crate::pointer::PointerSet;
10use crate::pointer_checker::{PointerChecker, ScriptSource};
11use crate::sources::Scope;
12use crate::symbol::SymbolRegistry;
13use crate::text::LineIndex;
14use crate::token::Kind;
15use crate::typechecker::TypeChecker;
16use crate::writer::encode_script;
17use crate::{
18    SourceProvider, diagnostic_messages as msg, lints, parallel_chunks, parallel_map_balanced,
19    trigger_table,
20};
21use std::collections::hash_map::DefaultHasher;
22use std::collections::{BTreeMap, HashMap, HashSet};
23use std::hash::{Hash, Hasher};
24use std::ops::Range;
25use std::path::{Path, PathBuf};
26use std::sync::{
27    Arc, Mutex, OnceLock,
28    atomic::{AtomicBool, Ordering},
29};
30
31#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
32pub struct SourceId(pub u64);
33
34/// A source for `open_all`: label, scope, version and text.
35pub type SourceText = (String, Scope, i64, String);
36
37pub enum Target {
38    Rune,
39    Client(Format),
40    /// Server bytecode for lobbyscript, which compiles no .gs2.
41    Lobby,
42}
43
44impl Target {
45    fn includes(&self, scope: Scope) -> bool {
46        match self {
47            Self::Rune => matches!(scope, Scope::Rune | Scope::Global),
48            Self::Client(_) => matches!(scope, Scope::Client | Scope::Global),
49            Self::Lobby => scope == Scope::Lobby,
50        }
51    }
52}
53
54#[derive(Clone, Default)]
55pub struct CancellationToken(Arc<AtomicBool>);
56
57impl CancellationToken {
58    pub fn cancel(&self) {
59        self.0.store(true, Ordering::Relaxed);
60    }
61    pub fn is_cancelled(&self) -> bool {
62        self.0.load(Ordering::Relaxed)
63    }
64}
65
66#[derive(Clone, Default)]
67pub struct Options {
68    pub include_tests: bool,
69    pub lint: bool,
70    pub skip_shared_lints: bool,
71    pub check_pointers: bool,
72    pub cancellation: CancellationToken,
73}
74
75pub struct Document {
76    pub id: SourceId,
77    pub label: PathBuf,
78    pub scope: Scope,
79    pub version: i64,
80    pub text: Arc<str>,
81    pub lines: LineIndex,
82    pub config: Option<String>,
83    pub(crate) parsed: Option<Source>,
84    pub(crate) tokens: Vec<TokenInfo>,
85    pub(crate) spans: Vec<Range<usize>>,
86    pub(crate) diagnostics: DiagnosticsCollector,
87    test_line: Option<usize>,
88}
89
90#[derive(Clone, Copy)]
91pub(crate) struct TokenInfo {
92    pub kind: Kind,
93    pub line: usize,
94}
95
96impl Document {
97    pub fn syntax(&self) -> Option<&ScriptFile> {
98        self.parsed.as_ref().map(|source| &source.file)
99    }
100}
101
102pub struct Artifact {
103    pub source: SourceId,
104    pub id: i32,
105    pub name: String,
106    pub bytes: Vec<u8>,
107}
108
109/// New allocations are returned to the host; the compiler never edits ID files.
110#[derive(Clone, Debug, PartialEq, Eq)]
111pub struct IdAssignment {
112    pub trigger: String,
113    pub name: String,
114    pub id: i32,
115}
116
117pub struct Analysis {
118    pub diagnostics: DiagnosticsCollector,
119    pub assignments: Vec<IdAssignment>,
120    pub cancelled: bool,
121    pub checked_documents: usize,
122}
123
124pub struct Compilation {
125    pub analysis: Analysis,
126    pub artifacts: Vec<Artifact>,
127}
128
129type PointerRequirements = HashMap<SourceId, HashMap<String, PointerSet>>;
130
131/// Retains parsed documents between edits and reuses the supplied config and
132/// command registry. Source names are opaque: URI, database key, or filename.
133pub struct Project {
134    pub(crate) documents: BTreeMap<SourceId, Document>,
135    pub(crate) registry: SymbolRegistry,
136    /// Directory URIs owned by a filesystem-backed editor project. Other hosts
137    /// accept arbitrary source names and leave this unrestricted.
138    pub(crate) editor_roots: Option<Vec<String>>,
139    target: Target,
140    options: Options,
141    next_source: u64,
142    declarations: Vec<(Scope, String, String)>,
143    allocated: BTreeMap<(String, String), i32>,
144    pub(crate) generation: u64,
145    analyzed_generation: Option<u64>,
146    cached_diagnostics: DiagnosticsCollector,
147    checked_full: Option<(u64, DiagnosticsCollector)>,
148    pub(crate) pointer_requirements: Option<(u64, PointerRequirements)>,
149    pub(crate) effects: Mutex<Option<Arc<crate::effects::Effects>>>,
150    deep: Option<(HashMap<PathBuf, i64>, Vec<Diagnostic>)>,
151    pub(crate) index: OnceLock<Index>,
152    pub(crate) configs: Mutex<Option<Arc<Configs>>>,
153    pub(crate) config_generation: u64,
154    pub(crate) registry_generation: u64,
155    checked: HashMap<SourceId, (i64, u64, DiagnosticsCollector)>,
156}
157
158impl Project {
159    pub fn new(target: Target, registry: SymbolRegistry, options: Options) -> Self {
160        Self {
161            documents: BTreeMap::new(),
162            registry,
163            editor_roots: None,
164            target,
165            options,
166            next_source: 0,
167            declarations: Vec::new(),
168            allocated: BTreeMap::new(),
169            generation: 0,
170            analyzed_generation: None,
171            cached_diagnostics: DiagnosticsCollector::new(),
172            checked_full: None,
173            pointer_requirements: None,
174            effects: Mutex::default(),
175            deep: None,
176            index: OnceLock::new(),
177            configs: Mutex::default(),
178            config_generation: 0,
179            registry_generation: 0,
180            checked: HashMap::new(),
181        }
182    }
183
184    pub fn registry(&self) -> &SymbolRegistry {
185        &self.registry
186    }
187    pub(crate) fn target_scope(&self) -> Scope {
188        match self.target {
189            Target::Rune => Scope::Rune,
190            Target::Client(_) => Scope::Client,
191            Target::Lobby => Scope::Lobby,
192        }
193    }
194    pub(crate) fn includes(&self, scope: Scope) -> bool {
195        self.target.includes(scope)
196    }
197    pub(crate) fn analysis_cancelled(&self) -> bool {
198        self.options.cancellation.is_cancelled()
199    }
200    pub(crate) fn pointer_requirements_ready(&self) -> bool {
201        self.pointer_requirements
202            .as_ref()
203            .is_some_and(|(generation, _)| *generation == self.generation)
204    }
205    pub fn set_cancellation(&mut self, token: CancellationToken) {
206        self.options.cancellation = token;
207    }
208    pub fn documents(&self) -> impl Iterator<Item = &Document> {
209        self.documents.values()
210    }
211    pub fn document(&self, id: SourceId) -> Option<&Document> {
212        self.documents.get(&id)
213    }
214
215    /// Replace the environment after config or command definitions change.
216    pub fn set_registry(&mut self, registry: SymbolRegistry) {
217        self.allocated.retain(|(trigger, name), _| {
218            !registry
219                .preloaded_script_ids
220                .contains_key(&format!("{trigger}:{name}"))
221        });
222        self.registry = registry;
223        self.registry_generation += 1;
224        self.index.take();
225        self.generation += 1;
226    }
227
228    pub fn open(
229        &mut self,
230        label: impl Into<PathBuf>,
231        scope: Scope,
232        version: i64,
233        text: impl Into<Arc<str>>,
234    ) -> SourceId {
235        let id = SourceId(self.next_source);
236        self.next_source += 1;
237        self.set_document(id, label.into(), scope, version, text.into());
238        id
239    }
240
241    pub fn open_config(
242        &mut self,
243        label: impl Into<PathBuf>,
244        scope: Scope,
245        version: i64,
246        text: impl Into<Arc<str>>,
247        kind: impl Into<String>,
248    ) -> SourceId {
249        let id = SourceId(self.next_source);
250        self.next_source += 1;
251        self.set_config(id, label.into(), scope, version, text.into(), kind.into());
252        id
253    }
254
255    fn set_config(
256        &mut self,
257        id: SourceId,
258        label: PathBuf,
259        scope: Scope,
260        version: i64,
261        text: Arc<str>,
262        kind: String,
263    ) {
264        Arc::make_mut(&mut self.registry.visibility).bind(label.clone(), scope);
265        self.documents.insert(
266            id,
267            Document {
268                id,
269                label,
270                scope,
271                version,
272                lines: LineIndex::new(&text),
273                text,
274                config: Some(kind),
275                parsed: None,
276                tokens: Vec::new(),
277                spans: Vec::new(),
278                diagnostics: DiagnosticsCollector::new(),
279                test_line: None,
280            },
281        );
282        self.index.take();
283        self.generation += 1;
284        self.config_generation += 1;
285    }
286
287    /// Reject stale editor updates. Unchanged text reuses its existing syntax.
288    pub fn update(
289        &mut self,
290        id: SourceId,
291        version: i64,
292        text: impl Into<Arc<str>>,
293    ) -> Result<(), &'static str> {
294        let old = self.documents.get_mut(&id).ok_or("Unknown source ID")?;
295        if version <= old.version {
296            return Err("Document version must increase");
297        }
298        let text = text.into();
299        if old.text == text {
300            old.version = version;
301            return Ok(());
302        }
303        let (label, scope, config) = (old.label.clone(), old.scope, old.config.clone());
304        if let Some(kind) = config {
305            self.set_config(id, label, scope, version, text, kind);
306        } else {
307            self.set_document(id, label, scope, version, text);
308        }
309        Ok(())
310    }
311
312    pub fn close(&mut self, id: SourceId) -> bool {
313        let Some(document) = self.documents.remove(&id) else {
314            return false;
315        };
316        Arc::make_mut(&mut self.registry.visibility).unbind(&document.label);
317        self.checked.remove(&id);
318        self.index.take();
319        self.generation += 1;
320        self.config_generation += u64::from(document.config.is_some());
321        true
322    }
323
324    /// Open many sources at once, lexing and parsing them in parallel. IDs are
325    /// assigned in input order, as `open` would.
326    pub fn open_all(&mut self, sources: Vec<SourceText>) -> Vec<SourceId> {
327        let first = self.next_source;
328        self.next_source += sources.len() as u64;
329        let include_tests = self.options.include_tests;
330        let sources = sources.into_iter().enumerate().collect::<Vec<_>>();
331        let documents = parallel_map_balanced(&sources, |(i, (label, scope, version, text))| {
332            parse_document(
333                SourceId(first + *i as u64),
334                label.into(),
335                *scope,
336                *version,
337                text.as_str().into(),
338                include_tests,
339            )
340        });
341        documents
342            .into_iter()
343            .map(|document| {
344                let id = document.id;
345                self.insert_document(document);
346                id
347            })
348            .collect()
349    }
350
351    fn set_document(
352        &mut self,
353        id: SourceId,
354        label: PathBuf,
355        scope: Scope,
356        version: i64,
357        text: Arc<str>,
358    ) {
359        let include_tests = self.options.include_tests;
360        self.insert_document(parse_document(
361            id,
362            label,
363            scope,
364            version,
365            text,
366            include_tests,
367        ));
368    }
369
370    fn insert_document(&mut self, document: Document) {
371        Arc::make_mut(&mut self.registry.visibility).bind(document.label.clone(), document.scope);
372        self.documents.insert(document.id, document);
373        self.index.take();
374        self.generation += 1;
375    }
376
377    pub fn analyze(&mut self) -> Analysis {
378        let mut diagnostics = DiagnosticsCollector::new();
379        if self.options.cancellation.is_cancelled() {
380            return self.analysis(diagnostics, true);
381        }
382        if self.analyzed_generation == Some(self.generation) {
383            return self.analysis(self.cached_diagnostics.clone(), false);
384        }
385        for (scope, trigger, name) in self.declarations.drain(..) {
386            if scope == Scope::Client && matches!(trigger.as_str(), "proc" | "clientscript") {
387                self.registry.client_script_params.remove(&name);
388            }
389            self.registry.remove_script(&trigger, &name);
390            Arc::make_mut(&mut self.registry.visibility).undeclare(&trigger, &name, scope);
391        }
392        let mut used: HashMap<i32, String> = self
393            .registry
394            .preloaded_script_ids
395            .iter()
396            .map(|(name, &id)| (id, name.clone()))
397            .collect();
398        for ((trigger, name), &id) in &self.allocated {
399            used.insert(id, format!("{trigger}:{name}"));
400        }
401        let mut next = used.keys().copied().max().unwrap_or(-1).checked_add(1);
402        let mut declared = HashSet::new();
403        for document in self.documents.values() {
404            let visibility = Arc::make_mut(&mut self.registry.visibility);
405            visibility.bind(document.label.clone(), document.scope);
406            let Some(source) = &document.parsed else {
407                continue;
408            };
409            for script in &source.file.scripts {
410                if script.trigger == "command" {
411                    continue;
412                }
413                visibility.declare(&script.trigger, &script.name, document.scope);
414                self.declarations.push((
415                    document.scope,
416                    script.trigger.clone(),
417                    script.name.clone(),
418                ));
419            }
420        }
421        clientscript::hooks::register_callbacks(
422            &mut self.registry,
423            self.documents
424                .values()
425                .filter(|document| document.scope == Scope::Client)
426                .filter_map(|document| document.parsed.as_ref())
427                .flat_map(|source| &source.file.scripts),
428        );
429        for document in self
430            .documents
431            .values()
432            .filter(|doc| self.target.includes(doc.scope))
433        {
434            if self.options.cancellation.is_cancelled() {
435                return self.analysis(diagnostics, true);
436            }
437            diagnostics.merge(document.diagnostics.clone());
438            let Some(source) = &document.parsed else {
439                continue;
440            };
441            for script in &source.file.scripts {
442                if document.scope == Scope::Lobby
443                    && !trigger_table::is_lobby_trigger(&script.trigger, &document.label)
444                {
445                    diagnostics.error(
446                        document.label.clone(),
447                        script.line,
448                        1,
449                        msg::fmt(msg::SCRIPT_TRIGGER_NOT_LOBBY, &[&script.trigger]),
450                        Phase::SymbolRegistration,
451                    );
452                    continue;
453                }
454                if script.trigger == "command" {
455                    continue;
456                }
457                if matches!(self.target, Target::Client(_)) {
458                    for statement in &script.body {
459                        clientscript::register_variables(statement, &mut self.registry);
460                    }
461                }
462                let key = format!("{}:{}", script.trigger, script.name);
463                if !declared.insert(key.clone()) {
464                    diagnostics.error(
465                        document.label.clone(),
466                        script.line,
467                        1,
468                        format!("Duplicate script [{},{}]", script.trigger, script.name),
469                        Phase::SymbolRegistration,
470                    );
471                    continue;
472                }
473                if matches!(self.target, Target::Client(_))
474                    && clientscript::triggers::lookup(&script.trigger).is_none()
475                {
476                    diagnostics.error(
477                        document.label.clone(),
478                        script.line,
479                        1,
480                        format!("Invalid client trigger {}", script.trigger),
481                        Phase::SymbolRegistration,
482                    );
483                    continue;
484                }
485                if let Some(trigger) = self.registry.trigger(&script.trigger) {
486                    if !trigger.allows_returns && !script.return_types.is_empty() {
487                        diagnostics.warning(
488                            document.label.clone(),
489                            script.line,
490                            1,
491                            format!("Trigger '{}' does not allow return values", script.trigger),
492                            Phase::SymbolRegistration,
493                        );
494                    }
495                } else if self.registry.profile.is_some() {
496                    diagnostics.error(
497                        document.label.clone(),
498                        script.line,
499                        1,
500                        format!(
501                            "Trigger '{}' is not defined by the supplied engine profile",
502                            script.trigger
503                        ),
504                        Phase::SymbolRegistration,
505                    );
506                }
507                if let Some(message) = Compiler::validate_trigger_subject(
508                    &script.trigger,
509                    &script.name,
510                    &self.registry,
511                ) {
512                    diagnostics.warning(
513                        document.label.clone(),
514                        script.line,
515                        1,
516                        message,
517                        Phase::SymbolRegistration,
518                    );
519                }
520                let allocated_key = (script.trigger.clone(), script.name.clone());
521                let supplied = self.registry.preloaded_script_ids.get(&key).copied();
522                let id = supplied
523                    .or_else(|| self.allocated.get(&allocated_key).copied())
524                    .or(next);
525                let Some(id) = id.filter(|id| *id >= 0) else {
526                    diagnostics.error(
527                        document.label.clone(),
528                        script.line,
529                        1,
530                        "Script IDs must fit a nonnegative i32".into(),
531                        Phase::SymbolRegistration,
532                    );
533                    continue;
534                };
535                if let Some(other) = used.get(&id).filter(|other| **other != key) {
536                    diagnostics.error(
537                        document.label.clone(),
538                        script.line,
539                        1,
540                        format!("Script ID {id} is also assigned to {other}"),
541                        Phase::SymbolRegistration,
542                    );
543                    continue;
544                }
545                used.insert(id, key);
546                next = next.and_then(|next| {
547                    if id >= next {
548                        id.checked_add(1)
549                    } else {
550                        Some(next)
551                    }
552                });
553                if supplied.is_none() {
554                    self.allocated.insert(allocated_key, id);
555                }
556                self.registry
557                    .set_script_id(&script.trigger, &script.name, id);
558                self.registry.register_script(
559                    script.name.clone(),
560                    script.trigger.clone(),
561                    script.params.iter().map(|p| p.param_type).collect(),
562                    script.return_types.clone(),
563                );
564                if matches!(self.target, Target::Client(_)) {
565                    self.registry
566                        .set_stack_script(&script.trigger, &script.name);
567                }
568                if document.test_line.is_some_and(|line| script.line >= line) {
569                    self.registry
570                        .mark_test_script(&script.trigger, &script.name);
571                }
572            }
573        }
574        let mut interface = DefaultHasher::new();
575        self.registry_generation.hash(&mut interface);
576        self.registry.game_vars.len().hash(&mut interface);
577        for document in self.documents.values() {
578            document.id.hash(&mut interface);
579            document.scope.hash(&mut interface);
580            if let Some(source) = &document.parsed {
581                for script in &source.file.scripts {
582                    script.trigger.hash(&mut interface);
583                    script.name.hash(&mut interface);
584                    script.params.len().hash(&mut interface);
585                    document
586                        .test_line
587                        .is_some_and(|line| script.line >= line)
588                        .hash(&mut interface);
589                    for parameter in &script.params {
590                        parameter.param_type.hash(&mut interface);
591                    }
592                    script.return_types.hash(&mut interface);
593                }
594            }
595        }
596        let interface = interface.finish();
597        let stale = self
598            .documents
599            .values()
600            .filter(|doc| {
601                self.target.includes(doc.scope)
602                    && !self
603                        .checked
604                        .get(&doc.id)
605                        .is_some_and(|(version, signature, _)| {
606                            *version == doc.version && *signature == interface
607                        })
608            })
609            .collect::<Vec<_>>();
610        let cancellation = &self.options.cancellation;
611        let fresh = parallel_map_balanced(&stale, |document| {
612            if cancellation.is_cancelled() {
613                return None;
614            }
615            let mut checker = TypeChecker::new(&self.registry);
616            if let Some(source) = &document.parsed {
617                checker.check_file_with_test_boundary(
618                    &source.file,
619                    &document.label,
620                    document.test_line,
621                );
622            }
623            Some(checker.diagnostics)
624        });
625        let checked_documents = stale.len();
626        let fresh = stale
627            .iter()
628            .map(|document| (document.id, document.version))
629            .zip(fresh)
630            .collect::<Vec<_>>();
631        if self.options.cancellation.is_cancelled() {
632            return self.analysis(diagnostics, true);
633        }
634        for ((id, version), found) in fresh {
635            self.checked
636                .insert(id, (version, interface, found.unwrap_or_default()));
637        }
638        for document in self
639            .documents
640            .values()
641            .filter(|doc| self.target.includes(doc.scope))
642        {
643            diagnostics.merge(self.checked[&document.id].2.clone());
644        }
645        self.cached_diagnostics = diagnostics.clone();
646        self.analyzed_generation = Some(self.generation);
647        let mut analysis = self.analysis(diagnostics, false);
648        analysis.checked_documents = checked_documents;
649        analysis
650    }
651
652    fn analysis(&self, diagnostics: DiagnosticsCollector, cancelled: bool) -> Analysis {
653        Analysis {
654            diagnostics,
655            cancelled,
656            checked_documents: 0,
657            assignments: self
658                .allocated
659                .iter()
660                .map(|((trigger, name), &id)| IdAssignment {
661                    trigger: trigger.clone(),
662                    name: name.clone(),
663                    id,
664                })
665                .collect(),
666        }
667    }
668
669    pub fn compile(&mut self) -> Compilation {
670        let mut analysis = self.analyze();
671        if analysis.cancelled || analysis.diagnostics.has_errors() {
672            return Compilation {
673                analysis,
674                artifacts: Vec::new(),
675            };
676        }
677        let mut scripts = Vec::<(SourceId, CompiledScript)>::new();
678        for document in self
679            .documents
680            .values()
681            .filter(|doc| self.target.includes(doc.scope))
682        {
683            if self.options.cancellation.is_cancelled() {
684                analysis.cancelled = true;
685                break;
686            }
687            let Some(source) = &document.parsed else {
688                continue;
689            };
690            let compiled = compile_file(
691                &source.file,
692                &document.label,
693                &self.registry,
694                matches!(self.target, Target::Client(_)),
695                &mut analysis.diagnostics,
696            );
697            scripts.extend(compiled.into_iter().map(|script| (document.id, script)));
698        }
699
700        analysis.cancelled |= self.options.cancellation.is_cancelled();
701        if analysis.cancelled || analysis.diagnostics.has_errors() {
702            return Compilation {
703                analysis,
704                artifacts: Vec::new(),
705            };
706        }
707
708        let check_pointers =
709            self.options.check_pointers || matches!(self.target, Target::Client(_));
710        if check_pointers || self.options.lint {
711            let sources: HashMap<_, _> = self
712                .documents
713                .values()
714                .map(|doc| {
715                    (
716                        doc.label.to_string_lossy().into_owned(),
717                        Arc::new(doc.text.to_string()),
718                    )
719                })
720                .collect();
721            let source = SourceProvider::Eager(&sources);
722            let (ids, compiled): (Vec<_>, Vec<_>) = scripts.into_iter().unzip();
723            if check_pointers {
724                let mut checker = match self.target {
725                    Target::Client(_) => PointerChecker::new_client(&compiled, &self.registry),
726                    Target::Rune | Target::Lobby => PointerChecker::new(&compiled, &self.registry),
727                };
728                checker.set_source_cache(source);
729                let names = compiled
730                    .iter()
731                    .map(|script| script.name.clone())
732                    .collect::<Vec<_>>();
733                analysis.diagnostics.merge(checker.validate_names(&names));
734            }
735            if self.options.lint {
736                analysis
737                    .diagnostics
738                    .merge(lints::run_lints(&compiled, Some(source)));
739            }
740            scripts = ids.into_iter().zip(compiled).collect();
741        }
742        let mut artifacts = Vec::with_capacity(scripts.len());
743        for (id, script) in scripts {
744            if self.options.cancellation.is_cancelled() {
745                analysis.cancelled = true;
746                break;
747            }
748            let document = &self.documents[&id];
749            let bytes = match &self.target {
750                Target::Rune | Target::Lobby => Ok(encode_script(&script)),
751                Target::Client(format) => clientscript::encode(&script, format),
752            };
753            match bytes {
754                Ok(bytes) => artifacts.push(Artifact {
755                    source: id,
756                    id: script.id,
757                    name: script.name,
758                    bytes,
759                }),
760                Err(error) => analysis.diagnostics.error(
761                    document.label.clone(),
762                    1,
763                    1,
764                    error.to_string(),
765                    Phase::CodeGeneration,
766                ),
767            }
768        }
769        analysis.cancelled |= self.options.cancellation.is_cancelled();
770        if analysis.cancelled || analysis.diagnostics.has_errors() {
771            artifacts.clear();
772        }
773        artifacts.sort_by_key(|artifact| artifact.id);
774        Compilation {
775            analysis,
776            artifacts,
777        }
778    }
779
780    /// The fullest diagnostics on hand without more work: `analyze`, with what
781    /// the last `check` added for each document that has not changed since.
782    pub fn latest(&mut self) -> Analysis {
783        let mut analysis = self.analyze();
784        if analysis.cancelled {
785            return analysis;
786        }
787        if let Some((generation, diagnostics)) = &self.checked_full
788            && *generation == self.generation
789        {
790            analysis.diagnostics = diagnostics.clone();
791        } else if let Some((versions, added)) = &self.deep {
792            let current = self
793                .documents
794                .values()
795                .map(|document| (document.label.as_path(), document.version))
796                .collect::<HashMap<_, _>>();
797            for diagnostic in added {
798                if versions.get(&diagnostic.file) == current.get(diagnostic.file.as_path()) {
799                    analysis.diagnostics.add(diagnostic.clone());
800                }
801            }
802        }
803        analysis
804    }
805
806    pub fn check(&mut self) -> Analysis {
807        let mut analysis = self.analyze();
808        if analysis.cancelled {
809            return analysis;
810        }
811        if let Some((generation, diagnostics)) = &self.checked_full
812            && *generation == self.generation
813        {
814            analysis.diagnostics = diagnostics.clone();
815            return analysis;
816        }
817        let analyzed = analysis.diagnostics.diagnostics().len();
818        let failed = analysis
819            .diagnostics
820            .diagnostics()
821            .iter()
822            .filter(|diagnostic| diagnostic.severity == Severity::Error)
823            .map(|diagnostic| diagnostic.file.as_path())
824            .collect::<HashSet<_>>();
825        let documents = self
826            .documents
827            .values()
828            .filter(|doc| {
829                self.target.includes(doc.scope)
830                    && doc.parsed.is_some()
831                    && !failed.contains(doc.label.as_path())
832            })
833            .collect::<Vec<_>>();
834        let client = matches!(self.target, Target::Client(_));
835        let cancellation = &self.options.cancellation;
836        let files = parallel_map_balanced(&documents, |document| {
837            let mut diagnostics = DiagnosticsCollector::new();
838            if cancellation.is_cancelled() {
839                return (Vec::new(), diagnostics);
840            }
841            let source = document.parsed.as_ref().unwrap();
842            let compiled = compile_file(
843                &source.file,
844                &document.label,
845                &self.registry,
846                client,
847                &mut diagnostics,
848            );
849            (compiled, diagnostics)
850        });
851        if cancellation.is_cancelled() {
852            analysis.cancelled = true;
853            return analysis;
854        }
855        let mut compiled = Vec::new();
856        for (scripts, diagnostics) in files {
857            compiled.extend(scripts);
858            analysis.diagnostics.merge(diagnostics);
859        }
860        let sources = self
861            .documents
862            .values()
863            .filter(|doc| doc.config.is_none())
864            .map(|doc| {
865                (
866                    doc.label.to_string_lossy().into_owned(),
867                    Arc::new(doc.text.to_string()),
868                )
869            })
870            .collect::<HashMap<_, _>>();
871        let source = SourceProvider::Eager(&sources);
872        let lint_diagnostics = lints::run_lints(&compiled, Some(source));
873        if self.options.skip_shared_lints {
874            let shared: HashSet<_> = self
875                .documents
876                .values()
877                .filter(|document| document.scope == Scope::Global)
878                .map(|document| &document.label)
879                .collect();
880            for diagnostic in lint_diagnostics.diagnostics() {
881                if !shared.contains(&diagnostic.file) {
882                    analysis.diagnostics.add(diagnostic.clone());
883                }
884            }
885        } else {
886            analysis.diagnostics.merge(lint_diagnostics);
887        }
888        let shared = PointerChecker::build_shared(&compiled, &self.registry, client);
889        let names = compiled
890            .iter()
891            .map(|script| script.name.clone())
892            .collect::<Vec<_>>();
893        let mut pointer_requirements = HashMap::new();
894        for (diagnostics, requirements) in parallel_chunks(&names, |chunk| {
895            let mut checker = PointerChecker::with_shared(
896                ScriptSource::Resident(&compiled),
897                &self.registry,
898                shared.clone(),
899            );
900            checker.set_source_cache(source);
901            checker.validate_names_with_requirements_until(chunk, &|| cancellation.is_cancelled())
902        }) {
903            analysis.diagnostics.merge(diagnostics);
904            pointer_requirements.extend(requirements);
905        }
906        analysis.cancelled = self.options.cancellation.is_cancelled();
907        if !analysis.cancelled {
908            let Some(incomplete) = incomplete_pointer_scripts(&compiled, cancellation) else {
909                analysis.cancelled = true;
910                return analysis;
911            };
912            let source_ids = documents
913                .iter()
914                .map(|document| (document.label.to_string_lossy(), document.id))
915                .collect::<HashMap<_, _>>();
916            let mut requirements_by_source: HashMap<_, HashMap<_, _>> = HashMap::new();
917            for script in &compiled {
918                if !incomplete.contains(&script.id)
919                    && let Some(&source) = source_ids.get(script.source_path.as_str())
920                    && let Some(&required) = pointer_requirements.get(&script.name)
921                {
922                    requirements_by_source
923                        .entry(source)
924                        .or_default()
925                        .insert(script.name.clone(), required);
926                }
927            }
928            if cancellation.is_cancelled() {
929                analysis.cancelled = true;
930                return analysis;
931            }
932            self.checked_full = Some((self.generation, analysis.diagnostics.clone()));
933            self.pointer_requirements = Some((self.generation, requirements_by_source));
934            self.deep = Some((
935                self.documents
936                    .values()
937                    .map(|document| (document.label.clone(), document.version))
938                    .collect(),
939                analysis.diagnostics.diagnostics()[analyzed..].to_vec(),
940            ));
941        }
942        analysis
943    }
944
945    pub fn source_text(&self, label: &Path) -> Option<&str> {
946        self.documents
947            .values()
948            .find(|doc| doc.label == label)
949            .map(|doc| doc.text.as_ref())
950    }
951}
952
953fn incomplete_pointer_scripts(
954    compiled: &[CompiledScript],
955    cancellation: &CancellationToken,
956) -> Option<HashSet<i32>> {
957    let available = compiled
958        .iter()
959        .map(|script| script.id)
960        .collect::<HashSet<_>>();
961    let mut callers: HashMap<i32, Vec<i32>> = HashMap::new();
962    let mut incomplete = HashSet::new();
963    let mut pending = Vec::new();
964    for script in compiled {
965        if cancellation.is_cancelled() {
966            return None;
967        }
968        for instruction in &script.instructions {
969            if matches!(
970                instruction.opcode,
971                Opcode::Gosub | Opcode::GosubWithParams | Opcode::Jump | Opcode::JumpWithParams
972            ) && let Operand::Int(callee) = instruction.operand
973            {
974                callers.entry(callee).or_default().push(script.id);
975                if !available.contains(&callee) && incomplete.insert(callee) {
976                    pending.push(callee);
977                }
978            }
979        }
980    }
981    while let Some(callee) = pending.pop() {
982        if cancellation.is_cancelled() {
983            return None;
984        }
985        for &caller in callers.get(&callee).into_iter().flatten() {
986            if incomplete.insert(caller) {
987                pending.push(caller);
988            }
989        }
990    }
991    Some(incomplete)
992}
993
994fn parse_document(
995    id: SourceId,
996    label: PathBuf,
997    scope: Scope,
998    version: i64,
999    text: Arc<str>,
1000    include_tests: bool,
1001) -> Document {
1002    let test_line = crate::find_testscript_line(&text);
1003    let end = if !include_tests {
1004        test_line
1005            .and_then(|line| LineIndex::new(&text).line(line - 1))
1006            .map_or(text.len(), |range| range.start)
1007    } else {
1008        text.len()
1009    };
1010    let (tokens, spans, lex_errors) = Lexer::new(&text[..end], &label).tokenize_spanned();
1011    let mut diagnostics = DiagnosticsCollector::new();
1012    for error in lex_errors {
1013        diagnostics.error(
1014            label.clone(),
1015            error.line,
1016            error.position,
1017            error.message,
1018            Phase::Lexing,
1019        );
1020    }
1021    let token_info = tokens
1022        .iter()
1023        .map(|token| TokenInfo {
1024            kind: token.kind,
1025            line: token.line,
1026        })
1027        .collect();
1028    let (file, errors) = Parser::new(tokens, &label).parse_recovering();
1029    for error in errors {
1030        diagnostics.error(
1031            label.clone(),
1032            error.line,
1033            error.position,
1034            error.message,
1035            Phase::Parsing,
1036        );
1037    }
1038    let parsed = Some(Source {
1039        path: label.clone(),
1040        file,
1041    });
1042    Document {
1043        id,
1044        label,
1045        scope,
1046        version,
1047        lines: LineIndex::new(&text),
1048        config: None,
1049        text,
1050        parsed,
1051        tokens: token_info,
1052        spans,
1053        diagnostics,
1054        test_line: test_line.filter(|_| include_tests),
1055    }
1056}