rils_frontend 0.4.0

Static analysis and semantic frontend for the Rils scripting language
Documentation
//! Partition semantic tables by the occurrence's source, retaining foreign
//! targets unchanged. No resolution or type checking runs while making views.
use super::*;

macro_rules! distribute {
    ($this:ident, $documents:ident, $field:ident, $key:ident => $source:expr) => {
        for ($key, value) in $this.$field {
            $documents
                .entry($source)
                .or_default()
                .$field
                .insert($key, value);
        }
    };
}

impl DefMap {
    pub(crate) fn link_project_symbols(&mut self, symbols: &mut [SymbolOccurrence]) {
        for symbol in symbols {
            if symbol.definition_id.is_none()
                && let Some(definition) = symbol
                    .definition_span
                    .and_then(|span| self.resolution(span))
            {
                symbol.definition_id = Some(definition);
                self.resolutions.insert(symbol.span, definition);
            }
        }
    }

    pub(crate) fn into_documents(self) -> HashMap<SourceId, Self> {
        let mut documents = HashMap::<SourceId, Self>::new();
        distribute!(self, documents, definitions, id => id.source);
        distribute!(self, documents, resolutions, span => span.source);
        distribute!(self, documents, external_functions, span => span.source);
        distribute!(self, documents, bodies, span => span.source);
        distribute!(self, documents, definition_bodies, id => id.source);
        distribute!(self, documents, impls, span => span.source);
        documents
    }
}

impl TypeckResults {
    pub(crate) fn into_documents(self) -> HashMap<SourceId, Self> {
        let mut documents = HashMap::<SourceId, Self>::new();
        distribute!(self, documents, function_signatures, span => span.source);
        distribute!(self, documents, resolved_patterns, id => id.source);
        distribute!(self, documents, executable_patterns, id => id.source);
        distribute!(self, documents, expression_types, id => id.source);
        distribute!(self, documents, resolved_calls, id => id.source);
        distribute!(self, documents, resolved_values, id => id.source);
        distribute!(self, documents, resolved_fields, id => id.source);
        for (source, ids) in self.expression_ids.into_documents() {
            documents.entry(source).or_default().expression_ids = ids;
        }
        for (span, kind) in self.callable_capabilities.bodies {
            documents
                .entry(span.source)
                .or_default()
                .callable_capabilities
                .bodies
                .insert(span, kind);
        }
        for (span, parameters) in self.callable_capabilities.parameters {
            documents
                .entry(span.source)
                .or_default()
                .callable_capabilities
                .parameters
                .insert(span, parameters);
        }
        for (id, kind) in self.callable_capabilities.expressions {
            documents
                .entry(id.source)
                .or_default()
                .callable_capabilities
                .expressions
                .insert(id, kind);
        }
        documents
    }
}