Implement nearest-loop transfers and reachable binding analysis

This commit is contained in:
Codex committed 2026-11-23 19:41:01 +08:00
1 parent 095eef1370
commit 69c5dc8d20
40 files changed
+953 -110

No files matched your search

+26 -3
View File
@@ -1,4 +1,4 @@
//! Public Analysis v2 projection (spec/analysis-v2.md).
//! Public Analysis projection (spec/analysis-v2.md and spec/analysis-v3.md).
//!
//! This is deliberately not a serde dump of `marqdo-ir`: the schema and node
//! kinds are the public contract, while internal Rust types may change. The
@@ -17,13 +17,24 @@ use crate::loader::LoadedProject;
/// Schema name and version.
pub const SCHEMA: &str = "marqdo.analysis";
pub const SCHEMA_VERSION: u32 = 2;
pub const BASE_VERSION: u32 = 2;
/// First schema version with structured loop transfers.
pub const LOOP_CONTROL_VERSION: u32 = 3;
/// Build the deterministic public projection.
pub fn build(project: &LoadedProject) -> Value {
let mut modules = Vec::new();
let mut version = BASE_VERSION;
for (module_id, file) in &project.files {
let module = &file.analysis.module;
if version == BASE_VERSION
&& module
.functions
.iter()
.any(|function| has_loop_control(&function.body))
{
version = LOOP_CONTROL_VERSION;
}
modules.push(json!({
"id": module_id.to_string(),
"package_instance_id": module_id.instance,
@@ -49,7 +60,7 @@ pub fn build(project: &LoadedProject) -> Value {
});
json!({
"schema": SCHEMA,
"schema_version": SCHEMA_VERSION,
"schema_version": version,
"producer": {
"name": "marqdo",
"version": env!("CARGO_PKG_VERSION"),
@@ -70,6 +81,15 @@ pub fn build(project: &LoadedProject) -> Value {
})
}
fn has_loop_control(body: &[Stmt]) -> bool {
body.iter().any(|stmt| match stmt {
Stmt::Break { .. } | Stmt::Continue { .. } => true,
Stmt::Branch { arms, .. } => arms.iter().any(|arm| has_loop_control(&arm.body)),
Stmt::Loop { body, .. } => has_loop_control(body),
Stmt::Assign { .. } | Stmt::Eval { .. } | Stmt::Return { .. } => false,
})
}
/// Encode exactly the bytes written by `check --emit`.
fn encode(project: &LoadedProject) -> Result<Vec<u8>, String> {
serde_json::to_vec_pretty(&build(project))
@@ -293,6 +313,8 @@ fn stmt_json(stmt: &Stmt) -> Value {
"body": body.iter().map(stmt_json).collect::<Vec<_>>(),
"span": span_json(*span),
}),
Stmt::Break { span } => json!({"kind": "break", "span": span_json(*span)}),
Stmt::Continue { span } => json!({"kind": "continue", "span": span_json(*span)}),
Stmt::Return { value, span } => json!({
"kind": "return",
"value": expr_json(value),
@@ -523,6 +545,7 @@ fn collect_calls(
expr_calls(project, module, function, condition, calls);
collect_calls(project, module, function, body, calls);
}
Stmt::Break { .. } | Stmt::Continue { .. } => {}
}
}
}
+1
View File
@@ -648,6 +648,7 @@ fn collect_identifiers_stmts(stmts: &[Stmt], names: &mut std::collections::BTree
collect_identifiers_expr(condition, names);
collect_identifiers_stmts(body, names);
}
Stmt::Break { .. } | Stmt::Continue { .. } => {}
}
}
}