Files
marqdo-cli/crates/marqdo-ir/src/call_binding.rs
T
cflmy a67f344b6b ext(00-08): RFC-EXT-0003 core/extension boundary, math detach and release stdlib
Implements issue #3 (RFC-EXT-0003) slices on the post-3.0.0-beta.1 tree.

PR-00/01/H-06: reproduce and fix sibling heading mode inheritance.
build_units now pops the current and same-or-higher-level headings from the
ancestor stack before deriving the inherited mode, so an unmarked heading can never
inherit the previous sibling. Covered by the units matrix, conformance fixtures
22/23, and black-box heading_inheritance through check --emit/run/call.

PR-02/H-08: the deterministic release archive now ships the complete verified
stdlib/ tree. package.py and smoke.py recompute the runtime tree/alias digests,
record them in RELEASE_INFO.json.stdlib, and fail on mismatch; smoke removes the
bundle and requires a fail-closed run. Release discovery no longer falls back to the
compile-time source tree (debug builds keep that development convenience).

PR-03/H-04: one marqdo_host::CORE_RESOURCE_KINDS registry is used by both the CLI
provider-manifest validator and every trusted host registration path; all six core
kinds are rejected as provider name or declared kind before registration.
H-03: docs/architecture/resource-operations.md records every reviewed
resource_request operation and architecture_boundaries pins the exact allowlist; new
domain operations are forbidden in core (ADR-0062).

PR-04/H-01: remove math.parse/eval/substitute/simplify/differentiate from
BUILTINS, HostOp, the runtime, the host kernel and the workspace
(crates/marqdo-math deleted). Formula stays inert; Int/Float arithmetic stays.
Math algorithms are a pure examples/math-library package plus an optional
examples/math-backend ABI plugin that requests the math.compute capability
(ADR-0061). Removed calls are static symbol.unknown; the math import namespace is
released again. Tests/examples/specs migrated; new math_library black-box test.

PR-05/06 Step A: ADR-0060 classifies @exec/@knowledge as the closed Core Modes
and specifies the Proposed metadata-only Heading Annotations v1 (target 3.1,
sem.invalid_mode_marker unchanged, Analysis v2 unchanged). ADR-0063 records the
foreign F-A retention and deferred F-B removal.

Docs/status/compatibility/changelog and the RFC-EXT-0003 evidence file are updated.
Full local gate: cargo fmt PASS, clippy -D warnings PASS, workspace tests 586
passed/0 failed/2 ignored, python tools/ci 28 passed.
2026-11-22 18:57:33 +08:00

202 lines
6.4 KiB
Rust

//! Resolved static call bindings (RFC-EXT-0001 §4.2, ADR-0054).
//!
//! The frontend resolves every statically visible call site exactly once and
//! records the decision here. The runtime only dispatches by this table: it
//! never re-resolves a qualified name and never guesses a plugin from a string
//! prefix. The table is internal IR (not part of Analysis/Trace v2 wire
//! contracts) and is deliberately skipped when a [`crate::Program`] is
//! serialized.
//!
//! Identity rules:
//!
//! * a [`CallSiteKey`] is `(module, function, byte span)` and is therefore
//! independent of disk paths, registration order and hash-map iteration;
//! * a bound target names the *resolved implementation*, including the plugin
//! package, complete export name and verified artifact digest for ABI calls;
//! * [`BoundTarget::DynamicValue`] is the only non-static path and may only
//! call language values (for example a `FunctionRef`); it is never a back
//! door to an unknown ABI export.
use std::collections::BTreeMap;
use std::fmt;
use serde::{Deserialize, Serialize};
use crate::{FunctionId, ModuleId};
/// Stable identity of one static call site.
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
pub struct CallSiteKey {
/// Module containing the call.
pub module: ModuleId,
/// Function containing the call.
pub function: FunctionId,
/// First byte offset of the call expression.
pub span_start: usize,
/// End byte offset (exclusive) of the call expression.
pub span_end: usize,
}
impl CallSiteKey {
/// Construct a key from a function identity and an expression span.
pub fn new(function: FunctionId, span: marqdo_span::Span) -> Self {
Self {
module: function.module.clone(),
function,
span_start: span.start,
span_end: span.end,
}
}
}
impl fmt::Display for CallSiteKey {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(
f,
"{}@{}..{}",
self.function, self.span_start, self.span_end
)
}
}
/// Resolved implementation target for a call site.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(tag = "kind", rename_all = "snake_case")]
pub enum BoundTarget {
/// A fixed language builtin, by canonical builtin name.
Builtin {
/// Canonical name (for example `foreign.run`), not a user alias.
canonical_name: String,
},
/// A function defined by a Markdown heading.
Marqdo {
/// Exact function identity.
function: FunctionId,
},
/// A statically declared ABI plugin export.
AbiPlugin {
/// Plugin package that owns the export.
package: String,
/// Complete dotted export name.
export: String,
/// Verified artifact digest from the selected manifest artifact.
digest: String,
/// Declared parameter names in manifest order; used to normalise
/// positional arguments deterministically for the ABI v1 map wire.
param_names: Vec<String>,
},
/// The callee is a language value resolved at runtime (local variable or
/// an expression result). Only language callables such as `FunctionRef`
/// are valid; this path cannot invoke an ABI plugin.
DynamicValue,
}
impl BoundTarget {
/// Whether this binding names a statically known implementation.
pub fn is_static(&self) -> bool {
!matches!(self, BoundTarget::DynamicValue)
}
}
/// One deterministic binding table for a prepared program.
#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
pub struct CallBindingTable {
sites: BTreeMap<CallSiteKey, BoundTarget>,
}
impl CallBindingTable {
/// Insert a binding. Returns `false` when the site was already bound to a
/// different target; re-binding the same target is idempotent.
pub fn insert(&mut self, key: CallSiteKey, target: BoundTarget) -> bool {
match self.sites.get(&key) {
Some(existing) => existing == &target,
None => {
self.sites.insert(key, target);
true
}
}
}
/// Look up the bound target for a call site.
pub fn get(&self, key: &CallSiteKey) -> Option<&BoundTarget> {
self.sites.get(key)
}
/// Number of bound call sites.
pub fn len(&self) -> usize {
self.sites.len()
}
/// Whether no call site is bound.
pub fn is_empty(&self) -> bool {
self.sites.is_empty()
}
/// Iterate bindings deterministically (call site order).
pub fn iter(&self) -> impl Iterator<Item = (&CallSiteKey, &BoundTarget)> {
self.sites.iter()
}
/// Number of statically bound (non-dynamic) call sites.
pub fn static_count(&self) -> usize {
self.sites
.values()
.filter(|target| target.is_static())
.count()
}
/// Number of dynamically resolved call sites.
pub fn dynamic_count(&self) -> usize {
self.sites.len() - self.static_count()
}
}
#[cfg(test)]
mod tests {
use super::*;
use marqdo_span::Span;
fn function(name: &str) -> FunctionId {
FunctionId {
module: ModuleId::root_default("index.mq.md"),
name: name.to_string(),
}
}
fn key(name: &str, span: Span) -> CallSiteKey {
CallSiteKey::new(function(name), span)
}
#[test]
fn keys_are_deterministic_and_span_based() {
let left = key("f", Span::new(10, 20));
let right = key("f", Span::new(10, 20));
let other = key("f", Span::new(21, 30));
assert_eq!(left, right);
assert_ne!(left, other);
assert_eq!(left.to_string(), "root:default::index.mq.md::f@10..20");
}
#[test]
fn insert_is_idempotent_but_rejects_conflicting_targets() {
let mut table = CallBindingTable::default();
let site = key("f", Span::new(1, 2));
assert!(table.insert(
site.clone(),
BoundTarget::Builtin {
canonical_name: "长度".into()
}
));
assert!(table.insert(
site.clone(),
BoundTarget::Builtin {
canonical_name: "长度".into()
}
));
assert!(!table.insert(site, BoundTarget::DynamicValue));
assert_eq!(table.len(), 1);
assert_eq!(table.static_count(), 1);
assert_eq!(table.dynamic_count(), 0);
}
}