use std::collections::{BTreeSet, HashMap, HashSet};
use tpt_telos_agent::FuncOutcome;
use tpt_telos_parser::ast::*;
pub mod eject;
pub mod ffi;
pub mod go;
pub mod project;
pub use project::{generate_project, GeneratedFile, Project};
pub(crate) type TypeFields = HashMap<String, BTreeSet<String>>;
#[derive(Debug, Clone)]
pub(crate) enum InputParam {
Scalar { name: String },
Struct {
name: String,
ty: String,
mutated: bool,
fields: Vec<String>,
},
}
#[derive(Debug, Clone)]
pub(crate) struct FuncAnalysis {
pub inputs: Vec<InputParam>,
pub scalar_out: Option<String>,
}
pub(crate) fn analyze_func(f: &Func, stmts: &[Stmt], types: &TypeFields) -> FuncAnalysis {
let mut mutated: HashSet<String> = HashSet::new();
let mut scalar_out: Option<String> = None;
for stmt in stmts {
let assigns = match stmt {
Stmt::MutateState(a) => a,
Stmt::Assign(a) => std::slice::from_ref(a),
};
for a in assigns {
match &a.target {
Expr::Field { base, .. } => {
mutated.insert(base.clone());
}
Expr::Var(v) if scalar_out.is_none() => {
scalar_out = Some(v.clone());
}
_ => {}
}
}
}
let mut inputs = Vec::new();
for p in &f.params {
if Some(&p.name) == scalar_out.as_ref() {
continue; }
let ty = p.ty.name();
let struct_fields = types.get(ty).cloned().unwrap_or_default();
if struct_fields.is_empty() {
inputs.push(InputParam::Scalar {
name: p.name.clone(),
});
} else {
inputs.push(InputParam::Struct {
name: p.name.clone(),
ty: ty.to_string(),
mutated: mutated.contains(&p.name),
fields: struct_fields.into_iter().collect(),
});
}
}
FuncAnalysis { inputs, scalar_out }
}
pub fn generate_program(modules: &[Module], outcomes: &[FuncOutcome]) -> String {
let bodies = collect_bodies(outcomes);
let refs: Vec<&Module> = modules.iter().collect();
render_rust(&refs, &bodies)
}
pub(crate) fn render_rust(modules: &[&Module], bodies: &HashMap<String, Vec<Stmt>>) -> String {
let mut out = String::new();
out.push_str("// Generated by tpt-telos. Do not edit by hand.\n");
out.push_str("// Mathematical contracts were verified by the tpt-telos SMT core.\n");
out.push_str("#![allow(dead_code)]\n\n");
let mut struct_types: TypeFields = HashMap::new();
for m in modules {
collect_types(m, &mut struct_types);
}
for m in modules {
out.push_str(&generate_module(m, bodies, &struct_types));
out.push('\n');
}
out
}
pub(crate) fn collect_bodies(outcomes: &[FuncOutcome]) -> HashMap<String, Vec<Stmt>> {
let mut bodies: HashMap<String, Vec<Stmt>> = HashMap::new();
for o in outcomes {
bodies.insert(o.func_name.clone(), o.final_candidate.stmts.clone());
}
bodies
}
pub(crate) fn collect_types(module: &Module, types: &mut TypeFields) {
let mut raw_fields: HashMap<String, BTreeSet<String>> = HashMap::new();
let mut field_users: HashSet<String> = HashSet::new();
let scan_expr =
|e: &Expr, users: &mut HashSet<String>, raw: &mut HashMap<String, BTreeSet<String>>| {
let mut fields: Vec<(String, String)> = Vec::new();
collect_fields(e, &mut fields);
for (base, field) in fields {
users.insert(base.clone());
raw.entry(base.clone()).or_default().insert(field);
}
};
for item in &module.items {
if let Item::Func(f) = item {
for r in &f.requires {
scan_expr(r, &mut field_users, &mut raw_fields);
}
for e in &f.ensures {
scan_expr(e, &mut field_users, &mut raw_fields);
}
for stmt in &f.body {
let assigns = match stmt {
Stmt::MutateState(a) => a,
Stmt::Assign(a) => std::slice::from_ref(a),
};
for a in assigns {
if let Expr::Field { base, field } = &a.target {
field_users.insert(base.clone());
raw_fields
.entry(base.clone())
.or_default()
.insert(field.clone());
}
scan_expr(&a.value, &mut field_users, &mut raw_fields);
}
}
}
}
for item in &module.items {
if let Item::Func(f) = item {
for p in &f.params {
if let Some(fields) = raw_fields.remove(&p.name) {
types
.entry(p.ty.name().to_string())
.or_default()
.extend(fields);
}
}
}
}
for (base, fields) in raw_fields {
types.entry(base).or_default().extend(fields);
}
}
fn collect_fields(expr: &Expr, out: &mut Vec<(String, String)>) {
match expr {
Expr::Field { base, field } => out.push((base.clone(), field.clone())),
Expr::Old(e) => collect_fields(e, out),
Expr::Unary { expr, .. } => collect_fields(expr, out),
Expr::Bin { lhs, rhs, .. } => {
collect_fields(lhs, out);
collect_fields(rhs, out);
}
_ => {}
}
}
fn generate_module(
module: &Module,
bodies: &HashMap<String, Vec<Stmt>>,
types: &TypeFields,
) -> String {
let route = tpt_telos_router::route(&module.attributes);
let mut out = String::new();
out.push_str(&format!(
"// ===== module {} (target: {}) =====\n",
module.name,
route.target.as_str()
));
for (ty, fields) in types {
if fields.is_empty() {
continue;
}
out.push_str(&format!("pub struct {} {{\n", ty));
for f in fields {
out.push_str(&format!(" pub {}: i64,\n", f));
}
out.push_str("}\n\n");
}
for item in &module.items {
if let Item::Invariant(inv) = item {
let conds = inv
.constraints
.iter()
.map(render_inv)
.collect::<Vec<_>>()
.join(" && ");
out.push_str(&format!(
"impl {} {{\n pub fn satisfies_invariants(&self) -> bool {{\n {}\n }}\n}}\n\n",
inv.name, conds
));
}
}
for item in &module.items {
if let Item::Func(f) = item {
let key = f.name.clone();
let stmts = bodies.get(&key).cloned().unwrap_or_else(|| f.body.clone());
if f.is_ejected() {
out.push_str(&eject::render_rust_ejected(f, &stmts, types));
} else {
out.push_str(&render_func(f, &stmts, types));
}
out.push('\n');
}
}
out
}
pub(crate) fn render_func(f: &Func, stmts: &[Stmt], types: &TypeFields) -> String {
let mut out = String::new();
out.push_str(&format!("/// {}({})\n", f.name, render_params_sig(f)));
for r in &f.requires {
out.push_str(&format!("/// requires: {}\n", render_expr_doc(r)));
}
for e in &f.ensures {
out.push_str(&format!("/// ensures: {}\n", render_expr_doc(e)));
}
let analysis = analyze_func(f, stmts, types);
let scalar_out = analysis.scalar_out.clone();
let mut params = Vec::new();
for input in &analysis.inputs {
match input {
InputParam::Scalar { name } => {
params.push(format!("{}: i64", name));
}
InputParam::Struct {
name, ty, mutated, ..
} => {
let borrow = if *mutated { "&mut " } else { "&" };
params.push(format!("{}: {}{}", name, borrow, ty));
}
}
}
let ret = if scalar_out.is_some() { " -> i64" } else { "" };
out.push_str(&format!(
"pub fn {}({}){} {{\n",
f.name,
params.join(", "),
ret
));
for stmt in stmts {
match stmt {
Stmt::MutateState(assigns) => {
for a in assigns {
out.push_str(" ");
out.push_str(&render_assign(a));
out.push_str(";\n");
}
}
Stmt::Assign(a) => {
let value = render_expr(&a.value);
if scalar_out.as_deref() == Some(target_var(&a.target)) {
out.push_str(&format!(" let {} = {};\n", target_var(&a.target), value));
} else {
out.push_str(" ");
out.push_str(&render_assign(a));
out.push_str(";\n");
}
}
}
}
if let Some(outvar) = &scalar_out {
out.push_str(&format!(" {}\n", outvar));
}
out.push_str("}\n");
out
}
pub(crate) fn render_params_sig(f: &Func) -> String {
f.params
.iter()
.map(|p| format!("{}: {}", p.name, p.ty.name()))
.collect::<Vec<_>>()
.join(", ")
}
pub(crate) fn target_var(e: &Expr) -> &str {
match e {
Expr::Var(v) => v,
_ => "",
}
}
pub(crate) fn render_assign(a: &Assign) -> String {
let lhs = render_expr(&a.target);
let rhs = render_expr(&a.value);
let op = match a.op {
AssignOp::Set => "=",
AssignOp::Add => "+=",
AssignOp::Sub => "-=",
};
format!("{} {} {}", lhs, op, rhs)
}
pub(crate) fn render_expr(e: &Expr) -> String {
match e {
Expr::Int(n) => n.to_string(),
Expr::Var(v) => v.clone(),
Expr::Field { base, field } => format!("{}.{}", base, field),
Expr::Old(inner) => render_expr(inner),
Expr::Unary { op, expr } => match op {
UnOp::Neg => format!("-{}", render_expr(expr)),
},
Expr::Bin { op, lhs, rhs } => {
let s = match op {
BinOp::Add => "+",
BinOp::Sub => "-",
BinOp::Mul => "*",
BinOp::Div => "/",
BinOp::Eq => "==",
BinOp::Ne => "!=",
BinOp::Lt => "<",
BinOp::Le => "<=",
BinOp::Gt => ">",
BinOp::Ge => ">=",
BinOp::And => "&&",
BinOp::Or => "||",
};
format!("{} {} {}", render_expr(lhs), s, render_expr(rhs))
}
}
}
pub(crate) fn render_expr_doc(e: &Expr) -> String {
match e {
Expr::Int(n) => n.to_string(),
Expr::Var(v) => v.clone(),
Expr::Field { base, field } => format!("{}.{}", base, field),
Expr::Old(inner) => format!("old({})", render_expr_doc(inner)),
Expr::Unary { op, expr } => match op {
UnOp::Neg => format!("-{}", render_expr_doc(expr)),
},
Expr::Bin { op, lhs, rhs } => {
let s = match op {
BinOp::Add => "+",
BinOp::Sub => "-",
BinOp::Mul => "*",
BinOp::Div => "/",
BinOp::Eq => "==",
BinOp::Ne => "!=",
BinOp::Lt => "<",
BinOp::Le => "<=",
BinOp::Gt => ">",
BinOp::Ge => ">=",
BinOp::And => "&&",
BinOp::Or => "||",
};
format!("{} {} {}", render_expr_doc(lhs), s, render_expr_doc(rhs))
}
}
}
fn render_inv(e: &Expr) -> String {
match e {
Expr::Int(n) => n.to_string(),
Expr::Var(v) => format!("self.{}", v),
Expr::Field { field, .. } => format!("self.{}", field),
Expr::Old(inner) => render_inv(inner),
Expr::Unary { op, expr } => match op {
UnOp::Neg => format!("-{}", render_inv(expr)),
},
Expr::Bin { op, lhs, rhs } => {
let s = match op {
BinOp::Add => "+",
BinOp::Sub => "-",
BinOp::Mul => "*",
BinOp::Div => "/",
BinOp::Eq => "==",
BinOp::Ne => "!=",
BinOp::Lt => "<",
BinOp::Le => "<=",
BinOp::Gt => ">",
BinOp::Ge => ">=",
BinOp::And => "&&",
BinOp::Or => "||",
};
format!("{} {} {}", render_inv(lhs), s, render_inv(rhs))
}
}
}
#[cfg(test)]
mod tests {
use std::collections::{BTreeSet, HashMap};
use tpt_telos_parser::ast::*;
use crate::{
analyze_func, collect_types, eject, generate_program, go, render_func, InputParam,
TypeFields,
};
fn t_var(v: &str) -> Expr {
Expr::Var(v.to_string())
}
fn t_int(n: i64) -> Expr {
Expr::Int(n)
}
fn t_field(base: &str, f: &str) -> Expr {
Expr::Field {
base: base.to_string(),
field: f.to_string(),
}
}
fn t_old(e: Expr) -> Expr {
Expr::Old(Box::new(e))
}
fn t_bin(op: BinOp, l: Expr, r: Expr) -> Expr {
Expr::Bin {
op,
lhs: Box::new(l),
rhs: Box::new(r),
}
}
fn t_assign(target: Expr, op: AssignOp, value: Expr) -> Assign {
Assign { target, op, value }
}
fn t_func(
name: &str,
params: Vec<Param>,
requires: Vec<Expr>,
ensures: Vec<Expr>,
body: Vec<Stmt>,
) -> Func {
Func {
attributes: vec![],
name: name.to_string(),
params,
requires,
ensures,
body,
elided: false,
}
}
fn counter_types() -> TypeFields {
let mut m = HashMap::new();
m.insert("Counter".to_string(), BTreeSet::from(["v".to_string()]));
m
}
#[test]
fn analyze_scalar_param() {
let f = t_func(
"f",
vec![Param {
name: "x".into(),
ty: Type::Named("i64".into()),
}],
vec![],
vec![],
vec![],
);
let a = analyze_func(&f, &f.body, &HashMap::new());
assert!(matches!(a.inputs[0], InputParam::Scalar { .. }));
assert!(a.scalar_out.is_none());
}
#[test]
fn analyze_struct_param_mutated_is_borrow_mut() {
let f = t_func(
"f",
vec![Param {
name: "c".into(),
ty: Type::Named("Counter".into()),
}],
vec![],
vec![],
vec![Stmt::MutateState(vec![t_assign(
t_field("c", "v"),
AssignOp::Set,
t_int(0),
)])],
);
let a = analyze_func(&f, &f.body, &counter_types());
match &a.inputs[0] {
InputParam::Struct { mutated, .. } => assert!(*mutated),
other => panic!("expected struct param, got {other:?}"),
}
}
#[test]
fn analyze_struct_param_unmutated_is_borrow_ref() {
let f = t_func(
"f",
vec![Param {
name: "c".into(),
ty: Type::Named("Counter".into()),
}],
vec![],
vec![],
vec![],
);
let a = analyze_func(&f, &f.body, &counter_types());
match &a.inputs[0] {
InputParam::Struct { mutated, .. } => assert!(!*mutated),
other => panic!("expected struct param, got {other:?}"),
}
}
#[test]
fn analyze_scalar_return_value() {
let f = t_func(
"f",
vec![
Param {
name: "c".into(),
ty: Type::Named("Counter".into()),
},
Param {
name: "result".into(),
ty: Type::Named("i64".into()),
},
],
vec![],
vec![],
vec![Stmt::Assign(t_assign(
t_var("result"),
AssignOp::Set,
t_bin(BinOp::Add, t_field("c", "v"), t_int(1)),
))],
);
let a = analyze_func(&f, &f.body, &counter_types());
assert_eq!(a.inputs.len(), 1);
assert_eq!(a.scalar_out.as_deref(), Some("result"));
}
#[test]
fn render_func_emits_contract_doc_comments() {
let f = t_func(
"transfer",
vec![Param {
name: "c".into(),
ty: Type::Named("Counter".into()),
}],
vec![t_bin(BinOp::Ge, t_field("c", "v"), t_int(0))],
vec![t_bin(
BinOp::Eq,
t_field("c", "v"),
t_bin(BinOp::Add, t_old(t_field("c", "v")), t_int(1)),
)],
vec![Stmt::MutateState(vec![t_assign(
t_field("c", "v"),
AssignOp::Set,
t_bin(BinOp::Add, t_field("c", "v"), t_int(1)),
)])],
);
let out = render_func(&f, &f.body, &counter_types());
assert!(out.contains("/// requires:"), "missing requires doc: {out}");
assert!(out.contains("/// ensures:"), "missing ensures doc: {out}");
assert!(out.contains("old(c.v)"), "old() lost in doc: {out}");
assert!(
out.contains("c: &mut Counter"),
"expected &mut param: {out}"
);
}
#[test]
fn render_func_emits_scalar_return() {
let f = t_func(
"f",
vec![
Param {
name: "c".into(),
ty: Type::Named("Counter".into()),
},
Param {
name: "result".into(),
ty: Type::Named("i64".into()),
},
],
vec![],
vec![],
vec![Stmt::Assign(t_assign(
t_var("result"),
AssignOp::Set,
t_bin(BinOp::Add, t_field("c", "v"), t_int(1)),
))],
);
let out = render_func(&f, &f.body, &counter_types());
assert!(out.contains("-> i64"), "missing return type: {out}");
assert!(
out.contains("let result ="),
"missing return binding: {out}"
);
assert!(
out.lines().any(|l| l.trim() == "result"),
"missing return expression: {out}"
);
}
#[test]
fn collect_types_gathers_struct_fields() {
let module = Module {
attributes: vec![],
name: "M".into(),
items: vec![Item::Func(t_func(
"f",
vec![Param {
name: "c".into(),
ty: Type::Named("Counter".into()),
}],
vec![t_bin(BinOp::Ge, t_field("c", "v"), t_int(0))],
vec![],
vec![Stmt::MutateState(vec![t_assign(
t_field("c", "v"),
AssignOp::Set,
t_int(0),
)])],
))],
};
let mut types = HashMap::new();
collect_types(&module, &mut types);
assert_eq!(types.get("Counter").map(|s| s.len()), Some(1));
assert!(types.get("Counter").unwrap().contains("v"));
}
#[test]
fn go_exported_capitalises() {
assert_eq!(go::exported("enqueue"), "Enqueue");
assert_eq!(go::exported("pending"), "Pending");
assert_eq!(go::exported("Queue"), "Queue");
}
#[test]
fn eject_renders_opaque_block_and_guard() {
let mut f = t_func(
"withdraw",
vec![Param {
name: "b".into(),
ty: Type::Named("Balance".into()),
}],
vec![t_bin(BinOp::Ge, t_field("b", "amount"), t_int(0))],
vec![t_bin(
BinOp::Eq,
t_field("b", "amount"),
t_bin(BinOp::Sub, t_old(t_field("b", "amount")), t_int(1)),
)],
vec![Stmt::MutateState(vec![t_assign(
t_field("b", "amount"),
AssignOp::Set,
t_int(0),
)])],
);
f.attributes.push(Attribute {
name: "eject".into(),
args: vec![Arg::Flag("rust".into())],
});
let mut types = HashMap::new();
types.insert(
"Balance".to_string(),
BTreeSet::from(["amount".to_string()]),
);
let out = eject::render_rust_ejected(&f, &f.body, &types);
assert!(
out.contains("fn withdraw_impl"),
"missing opaque block: {out}"
);
assert!(out.contains("pub fn withdraw"), "missing guard: {out}");
assert!(out.contains("assert!("), "missing contract guard: {out}");
assert!(
out.contains("__old_b_amount"),
"missing old snapshot: {out}"
);
}
#[test]
fn generate_program_emits_struct_and_invariant_method() {
let src =
"module M { invariant Counter { v >= 0 } func f(c: Counter) requires c.v >= 0 { } }";
let modules = tpt_telos_parser::parse(src).unwrap();
let rust = generate_program(&modules, &[]);
assert!(rust.contains("pub struct Counter"));
assert!(rust.contains("pub fn satisfies_invariants"));
assert!(rust.contains("pub fn f"));
}
}