rils_frontend 0.4.0

Static analysis and semantic frontend for the Rils scripting language
Documentation
use super::*;

#[derive(Clone, Debug, Default, PartialEq, Eq)]
pub(super) struct Facts {
    pub kind: Option<CallableKind>,
    pub targets: Vec<Span>,
    pub parameter: Option<(Span, usize)>,
    pub linear: bool,
    pub fields: HashMap<String, Facts>,
}

impl Facts {
    pub fn aggregate(fields: HashMap<String, Self>) -> Self {
        let mut facts = Self {
            fields,
            ..Self::default()
        };
        facts.limit_depth(16);
        facts
    }
    // Recursive return summaries must remain finite. Beyond the projection
    // budget, discard provenance; an unknown callable is never repeatable.
    fn limit_depth(&mut self, depth: usize) {
        if depth == 0 {
            self.fields.clear();
        } else {
            for value in self.fields.values_mut() {
                value.limit_depth(depth - 1);
            }
            self.fields.retain(|_, value| *value != Self::default());
        }
        self.linear |= self.fields.values().any(|value| value.linear);
    }
    pub fn meet(&self, other: &Self) -> Self {
        let mut targets = self.targets.clone();
        targets.extend(other.targets.iter().copied());
        targets.sort_by_key(|span| (span.source, span.start, span.end));
        targets.dedup();
        Self {
            kind: self.kind.zip(other.kind).map(|(a, b)| a.min(b)),
            targets,
            parameter: (self.parameter == other.parameter)
                .then_some(self.parameter)
                .flatten(),
            linear: self.linear || other.linear,
            fields: self
                .fields
                .iter()
                .filter_map(|(name, value)| {
                    Some((name.clone(), value.meet(other.fields.get(name)?)))
                })
                .collect(),
        }
    }
    pub fn substitute(&self, owner: Span, arguments: &[Self]) -> Self {
        if let Some((function, index)) = self.parameter
            && owner == function
        {
            return arguments.get(index).cloned().unwrap_or_default();
        }
        let mut result = self.clone();
        result.fields = self
            .fields
            .iter()
            .map(|(name, value)| (name.clone(), value.substitute(owner, arguments)))
            .collect();
        result.limit_depth(16);
        result
    }
}

#[derive(Clone, Debug, PartialEq, Eq)]
pub(super) struct Summary {
    pub kind: CallableKind,
    pub output: Facts,
}

#[derive(Clone, Default)]
pub(super) struct State {
    pub bindings: HashMap<DefId, Facts>,
    pub consumed: HashMap<Place, Span>,
    pub unreachable: bool,
}

impl State {
    pub fn merge(states: &[Self]) -> Self {
        let reachable = states
            .iter()
            .filter(|state| !state.unreachable)
            .collect::<Vec<_>>();
        let Some(first) = reachable.first() else {
            return Self {
                unreachable: true,
                ..Self::default()
            };
        };
        let mut merged = (*first).clone();
        for state in &reachable[1..] {
            for (id, facts) in &mut merged.bindings {
                *facts = facts.meet(state.bindings.get(id).unwrap_or(&Facts::default()));
            }
            merged
                .consumed
                .extend(state.consumed.iter().map(|(id, span)| (id.clone(), *span)));
        }
        merged
    }
}

#[derive(Clone, PartialEq, Eq, Hash)]
pub(super) struct Place {
    pub root: DefId,
    pub path: Vec<String>,
}

impl Place {
    pub fn overlaps(&self, other: &Self) -> bool {
        self.root == other.root
            && self
                .path
                .iter()
                .zip(&other.path)
                .all(|(a, b)| a == b || a == "*" || b == "*")
    }
}