Skip to main content

code_transpiler/
semantic.rs

1use crate::runtime::RValue;
2use std::collections::BTreeMap;
3
4#[derive(Debug, Clone, PartialEq, Eq)]
5pub struct SemanticEffectSummary {
6    pub counts: BTreeMap<String, usize>,
7    pub conservative_pure: bool,
8    pub unknown: bool,
9}
10
11/// Port of the effect-summary decision rule. Input is already-projected effect axes.
12pub fn summarize_effect_axes(axes: &[(String, Vec<bool>)]) -> SemanticEffectSummary {
13    let mut out = SemanticEffectSummary {
14        counts: BTreeMap::new(),
15        conservative_pure: true,
16        unknown: false,
17    };
18    for (axis, rows) in axes {
19        let count = rows.iter().filter(|&&x| x).count();
20        if count == 0 {
21            continue;
22        }
23        out.counts.insert(axis.clone(), count);
24        match axis.as_str() {
25            "local.read" | "control" => {}
26            "call.unknown" => {
27                out.unknown = true;
28                out.conservative_pure = false;
29            }
30            _ => out.conservative_pure = false,
31        }
32    }
33    out
34}
35
36/// Snapshot semantics for iteration: null -> empty, vector -> private outer copy, scalar -> singleton.
37pub fn snapshot_iteration(value: &RValue) -> Vec<RValue> {
38    match value {
39        RValue::Null => Vec::new(),
40        RValue::Vec(xs) => xs.clone(),
41        x => vec![x.clone()],
42    }
43}
44
45#[derive(Debug, Clone, PartialEq, Eq, Default)]
46pub struct StructuredConstructInput {
47    pub family: String,
48    pub node_kind: String,
49    pub roles: Vec<String>,
50    pub operand_count: usize,
51    pub binding_count: usize,
52    pub type_count: usize,
53    pub fields: BTreeMap<String, String>,
54    pub source_offset: usize,
55}
56
57pub fn structured_input_fields(input: &StructuredConstructInput) -> BTreeMap<&'static str, bool> {
58    let mut out = BTreeMap::new();
59    out.insert("node kind", !input.node_kind.is_empty());
60    for role in &input.roles {
61        out.insert("child", true);
62        match role.as_str() {
63            "base" => {
64                out.insert("operand", true);
65            }
66            "index" => {
67                out.insert("index", true);
68            }
69            "start" | "end" | "step" => {
70                out.insert("slice bounds", true);
71            }
72            "parameter" => {
73                out.insert("parameter", true);
74            }
75            "binding" => {
76                out.insert("binding", true);
77            }
78            "body" => {
79                out.insert("control/body reference", true);
80            }
81            "iterable" => {
82                out.insert("iterable", true);
83            }
84            "condition" | "filter" => {
85                out.insert("filter/condition", true);
86            }
87            _ => {}
88        }
89    }
90    if input.family == "CONTAINER" && !input.roles.is_empty() {
91        out.insert("child", true);
92    }
93    if input.operand_count > 0 {
94        out.insert("operand", true);
95    }
96    if input.binding_count > 0 {
97        out.insert("binding", true);
98    }
99    if input.type_count > 0 {
100        out.insert("type", true);
101    }
102    out
103}
104
105pub fn missing_structured_fields(input: &StructuredConstructInput) -> Vec<&'static str> {
106    let required: &[&str] = match input.family.as_str() {
107        "CONTAINER" => &[
108            "node kind",
109            "child",
110            "operand",
111            "type",
112            "binding",
113            "allocation size",
114        ],
115        "ITERATION" => &[
116            "node kind",
117            "iterable",
118            "binding",
119            "child",
120            "control/body reference",
121            "filter/condition",
122        ],
123        "CLOSURE_FUNCTION_VALUE" => &[
124            "node kind",
125            "parameter",
126            "capture",
127            "binding",
128            "child",
129            "control/body reference",
130        ],
131        "INDEX_SLICE" => &["node kind", "operand", "index", "slice bounds", "binding"],
132        _ => &[],
133    };
134    let available = structured_input_fields(input);
135    required
136        .iter()
137        .copied()
138        .filter(|field| !available.get(field).copied().unwrap_or(false))
139        .collect()
140}