rils_frontend 0.4.0

Static analysis and semantic frontend for the Rils scripting language
Documentation
//! Immutable declarations and exports produced by one project analysis.
use super::*;
use crate::{exports::ExportTable, semantic::DeclarationTypeResolver};
use std::sync::{Arc, OnceLock};

#[derive(Clone, Debug)]
pub struct ProjectSemanticSnapshot {
    pub(super) analysis: DocumentAnalysis,
    document_contexts: HashMap<SourceId, DocumentAnalysis>,
    documents: OnceLock<HashMap<SourceId, Arc<DocumentAnalysis>>>,
    declarations: DeclarationTypeResolver,
    exports: Arc<ExportTable>,
    public_exports: Arc<ExportTable>,
    visible_exports: Arc<ExportTable>,
}

impl ProjectSemanticSnapshot {
    pub(super) fn new(
        analysis: DocumentAnalysis,
        documents: HashMap<SourceId, DocumentAnalysis>,
        declarations: DeclarationTypeResolver,
        units: &[(SourceId, Vec<String>, crate::ast::Program)],
        host_functions: &HashMap<String, FunctionSignature>,
        host_types: &HashSet<String>,
        inherited: &ExportTable,
    ) -> Self {
        let mut exports = ExportTable::new();
        let libraries = units
            .iter()
            .filter(|(_, path, _)| !declarations.file_modules.is_script(path));
        for (_, path, program) in libraries.clone() {
            collect_package_exports(
                program,
                path,
                Some(&analysis),
                &mut exports,
                &declarations.packages,
            );
        }
        crate::exports::extend_external_declarations(&mut exports, host_functions, host_types);
        resolve_package_reexports(
            &mut exports,
            libraries.map(|(_, path, program)| (path.as_slice(), program)),
            &declarations.packages,
        );
        let public_exports = Arc::new(crate::exports::public_exports(&exports));
        let exports = Arc::new(exports);
        let visible_exports = if inherited.is_empty() {
            exports.clone()
        } else {
            let mut visible = exports.as_ref().clone();
            for (path, items) in inherited {
                visible
                    .entry(path.clone())
                    .or_default()
                    .extend(items.iter().cloned());
            }
            Arc::new(visible)
        };
        Self {
            analysis,
            document_contexts: documents,
            documents: OnceLock::new(),
            declarations,
            exports,
            public_exports,
            visible_exports,
        }
    }

    pub fn analysis(&self) -> &DocumentAnalysis {
        &self.analysis
    }
    /// Final project semantics belonging to occurrences in this source. Parse
    /// errors for the current text must be checked separately by the caller.
    pub fn document(&self, source: SourceId) -> Option<&Arc<DocumentAnalysis>> {
        // Compiler-only consumers need the aggregate and do not pay for views.
        self.documents
            .get_or_init(|| {
                self.analysis
                    .clone()
                    .into_documents(self.document_contexts.clone())
                    .into_iter()
                    .map(|(source, mut analysis)| {
                        if !source.is_generated() {
                            analysis.prepare_method_hovers(&self.declarations);
                        }
                        (source, Arc::new(analysis))
                    })
                    .collect()
            })
            .get(&source)
    }
    pub fn declarations(&self) -> &DeclarationTypeResolver {
        &self.declarations
    }
    /// Own declarations, including private bindings for queries inside this project.
    pub fn exports(&self) -> &Arc<ExportTable> {
        &self.exports
    }
    /// Only the public surface may be consumed by another project.
    pub fn public_exports(&self) -> &Arc<ExportTable> {
        &self.public_exports
    }
    pub fn visible_exports(&self) -> &Arc<ExportTable> {
        &self.visible_exports
    }
}