rils_frontend 0.4.0

Static analysis and semantic frontend for the Rils scripting language
Documentation
//! Function identities selected by lexical resolution, shared by inference and checks.
use std::collections::HashMap;

use crate::{
    Span,
    analysis::{SymbolKind, SymbolOccurrence},
    ast::Expr,
};

pub(super) fn callee_definition(
    callee: &Expr,
    definitions: &super::DefMap,
) -> Option<crate::DefId> {
    definitions.resolution(callee_span(callee)?)
}

pub(crate) fn callee_span(callee: &Expr) -> Option<Span> {
    Some(match callee {
        Expr::Variable { span, .. } => *span,
        Expr::Path { segments, span } => super::member_span(*span, segments.last()?),
        Expr::GenericPath { segments, span, .. } => {
            function_reference_span(&segments.join("::"), *span)
        }
        Expr::QualifiedPath { member, span, .. }
        | Expr::Member {
            name: member, span, ..
        } => super::member_span(*span, member),
        _ => return None,
    })
}

pub(super) fn lexical_callee_definition(
    callee: &Expr,
    definitions: &super::DefMap,
) -> Option<crate::DefId> {
    if !matches!(
        callee,
        Expr::Variable { .. } | Expr::Path { .. } | Expr::GenericPath { .. }
    ) {
        return None;
    }
    let definition = callee_definition(callee, definitions)?;
    let data = definitions.definition(definition)?;
    // Imports without a source body still need native/host declaration lookup.
    // A source function (including a local shadow) already has its own body identity.
    (matches!(data.kind, SymbolKind::Variable | SymbolKind::Parameter)
        || (data.kind == SymbolKind::Function && definitions.body(definition).is_some()))
    .then_some(definition)
}

fn function_reference_span(name: &str, span: Span) -> Span {
    let end = span.start + name.len();
    let member = name.rsplit("::").next().unwrap_or(name);
    Span::in_source(span.source, end - member.len(), end)
}

pub(crate) struct FunctionReferences {
    source: HashMap<Span, Span>,
    external: HashMap<Span, (crate::analysis::ExternalFunction, crate::Type)>,
}

impl FunctionReferences {
    pub(crate) fn new(symbols: &[SymbolOccurrence]) -> Self {
        Self {
            source: symbols
                .iter()
                .filter_map(|symbol| {
                    (!symbol.is_definition && symbol.kind == SymbolKind::Function)
                        .then_some((symbol.span, symbol.definition_span?))
                })
                .collect(),
            external: symbols
                .iter()
                .filter_map(|symbol| {
                    Some((
                        symbol.span,
                        (
                            symbol.external_function.clone()?,
                            symbol.inferred_type.clone()?,
                        ),
                    ))
                })
                .collect(),
        }
    }

    pub(crate) fn get(&self, name: &str, span: Span) -> Option<Span> {
        // A generic expression includes its type arguments. References use only
        // the final identifier, including for module-qualified function paths.
        let member = name.rsplit("::").next().unwrap_or(name);
        self.source
            .get(&super::member_span(span, member))
            .or_else(|| self.source.get(&function_reference_span(name, span)))
            .copied()
    }

    pub(crate) fn external(
        &self,
        span: Span,
    ) -> Option<&(crate::analysis::ExternalFunction, crate::Type)> {
        self.external.get(&span)
    }
}