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