neo_solidity/ir/statements/assignments/
inc_dec.rs

1fn lower_post_inc_dec(
2    expr: &Expression,
3    ctx: &mut LoweringContext,
4    instructions: &mut Vec<Instruction>,
5    increment: bool,
6) -> bool {
7    // Post-increment/decrement semantics:
8    // 1. Load the original value (this will be the result)
9    // 2. Perform the increment/decrement and store back
10    // 3. The original value remains on the stack as the expression result
11
12    // Step 1: Load original value onto stack (this is the return value)
13    if !lower_expression(expr, ctx, instructions) {
14        return false;
15    }
16
17    // Step 2: Perform compound assignment (x = x + 1 or x = x - 1)
18    // This modifies the variable but we don't need its result on stack
19    let one = Expression::NumberLiteral(Default::default(), "1".to_string(), "".to_string(), None);
20    let op = if increment {
21        BinaryOperator::Add
22    } else {
23        BinaryOperator::Sub
24    };
25
26    if !lower_compound_assignment(expr, &one, ctx, instructions, op) {
27        return false;
28    }
29
30    // lower_compound_assignment leaves the updated value on the stack as the expression result.
31    // For post-inc/dec we must discard it and keep the original value from Step 1.
32    instructions.push(Instruction::Drop(ValueType::Any));
33
34    // The original value loaded in Step 1 is already on the stack as the result
35    // Do NOT load the value again - that was the bug!
36    true
37}
38
39fn lower_pre_inc_dec(
40    expr: &Expression,
41    ctx: &mut LoweringContext,
42    instructions: &mut Vec<Instruction>,
43    increment: bool,
44) -> bool {
45    let one = Expression::NumberLiteral(Default::default(), "1".to_string(), "".to_string(), None);
46    let op = if increment {
47        BinaryOperator::Add
48    } else {
49        BinaryOperator::Sub
50    };
51
52    if !lower_compound_assignment(expr, &one, ctx, instructions, op) {
53        return false;
54    }
55
56    // lower_compound_assignment returns the updated value as the expression result.
57    true
58}