neo-decompiler 0.11.0

Neo N3 NEF decompiler: parse, disassemble, lift bytecode to high-level pseudocode and C# skeletons, with a CLI, JSON reports, and optional WebAssembly bindings.
Documentation
use super::model::OverflowCollapse;
use super::parse::{is_temp_identifier, leading_whitespace, parse_bare_assignment};
use super::scan::next_code_line;

/// Apply the collapse: rewrite the operation line and blank the wrapper lines.
pub(super) fn apply_collapse(statements: &mut [String], collapse: &OverflowCollapse) {
    let indent = leading_whitespace(&statements[collapse.op_line]);

    if collapse.is_checked {
        let wrapped = if collapse.expr.starts_with("checked(") {
            collapse.expr.clone()
        } else {
            format!("checked({})", collapse.expr)
        };
        statements[collapse.op_line] = format!("{indent}let {} = {wrapped};", collapse.result_var);
    }

    for statement in statements
        .iter_mut()
        .take(collapse.blank_end + 1)
        .skip(collapse.blank_start)
    {
        statement.clear();
    }

    if let Some((else_open, else_close)) = collapse.else_unwrap {
        statements[else_open].clear();
        statements[else_close].clear();
    }

    if !collapse.is_checked {
        fixup_downstream_reference(statements, collapse.blank_end + 1, &collapse.result_var);
    }
}

/// Fix up a bare assignment after the collapsed block whose RHS references a
/// temp defined inside the now-blanked wrapper.
fn fixup_downstream_reference(statements: &mut [String], start: usize, result_var: &str) {
    let Some(idx) = next_code_line(statements, start) else {
        return;
    };
    let line = statements[idx].trim();
    if line.starts_with("let ") || line.starts_with("if ") || line.starts_with("//") {
        return;
    }
    if let Some((lhs, rhs)) = parse_bare_assignment(line) {
        if rhs != result_var && is_temp_identifier(&rhs) {
            let indent = leading_whitespace(&statements[idx]);
            statements[idx] = format!("{indent}{lhs} = {result_var};");
        }
    }
}