neo-devpack-solidity 0.22.0

Production-focused Solidity-to-NeoVM compilation system
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
fn try_lower_expression_assignments(
    expr: &Expression,
    ctx: &mut LoweringContext,
    instructions: &mut Vec<Instruction>,
) -> Option<bool> {
    match expr {
        Expression::AssignAdd(_, lhs, rhs) => Some(lower_compound_assignment(
            lhs,
            rhs,
            ctx,
            instructions,
            BinaryOperator::Add,
        )),
        Expression::AssignSubtract(_, lhs, rhs) => Some(lower_compound_assignment(
            lhs,
            rhs,
            ctx,
            instructions,
            BinaryOperator::Sub,
        )),
        Expression::AssignShiftLeft(_, lhs, rhs) => Some(lower_compound_assignment(
            lhs,
            rhs,
            ctx,
            instructions,
            BinaryOperator::Shl,
        )),
        Expression::AssignShiftRight(_, lhs, rhs) => Some(lower_compound_assignment(
            lhs,
            rhs,
            ctx,
            instructions,
            BinaryOperator::Shr,
        )),
        Expression::AssignAnd(_, lhs, rhs) => Some(lower_compound_assignment(
            lhs,
            rhs,
            ctx,
            instructions,
            BinaryOperator::BitAnd,
        )),
        Expression::AssignOr(_, lhs, rhs) => Some(lower_compound_assignment(
            lhs,
            rhs,
            ctx,
            instructions,
            BinaryOperator::BitOr,
        )),
        Expression::AssignXor(_, lhs, rhs) => Some(lower_compound_assignment(
            lhs,
            rhs,
            ctx,
            instructions,
            BinaryOperator::BitXor,
        )),
        Expression::AssignMultiply(_, lhs, rhs) => Some(lower_compound_assignment(
            lhs,
            rhs,
            ctx,
            instructions,
            BinaryOperator::Mul,
        )),
        Expression::AssignDivide(_, lhs, rhs) => Some(lower_compound_assignment(
            lhs,
            rhs,
            ctx,
            instructions,
            BinaryOperator::Div,
        )),
        Expression::AssignModulo(_, lhs, rhs) => Some(lower_compound_assignment(
            lhs,
            rhs,
            ctx,
            instructions,
            BinaryOperator::Mod,
        )),
        Expression::Assign(_, lhs, rhs) => {
            lower_assignment(lhs, rhs, ctx, instructions);
            Some(true)
        }
        Expression::PostIncrement(_, inner) => Some(lower_post_inc_dec(inner, ctx, instructions, true)),
        Expression::PostDecrement(_, inner) => {
            Some(lower_post_inc_dec(inner, ctx, instructions, false))
        }
        Expression::PreIncrement(_, inner) => Some(lower_pre_inc_dec(inner, ctx, instructions, true)),
        Expression::PreDecrement(_, inner) => Some(lower_pre_inc_dec(inner, ctx, instructions, false)),
        Expression::Delete(_, target) => {
            Some(lower_delete(target, ctx, instructions))
        }
        _ => None,
    }
}

fn lower_delete(
    target: &Expression,
    ctx: &mut LoweringContext,
    instructions: &mut Vec<Instruction>,
) -> bool {
    // Storage delete: reset to the storage-default value.
    if let Some(reference) = resolve_storage_reference(target, ctx) {
        if !ctx.ensure_state_writable(reference.state_index) {
            return false;
        }

        // Solidity allows `delete` on mappings, but it cannot clear all keys; treat as a no-op.
        if matches!(reference.value_type, ValueType::Mapping { .. }) {
            instructions.push(Instruction::PushLiteral(LiteralValue::Integer(
                BigInt::zero(),
            )));
            return true;
        }

        // Whole-array delete (`delete arr;` / `delete m[k];` where the value
        // is an array). Solidity requires every element to be reset to its
        // default — writing only the base (length) slot leaves the per-index
        // slots `keccak256(serialize(i) || base)` fully readable. For
        // fixed-size `T[N]` arrays the bounds guard uses the compile-time
        // constant N (never the base slot), so without the element-clearing
        // loop every `arr[i]` read returns the pre-delete value.
        if matches!(reference.value_type, ValueType::Array(_))
            && reference.field_path.is_empty()
            && reference.trailing_key_expressions.is_empty()
            && lower_delete_storage_array(&reference, ctx, instructions)
        {
            instructions.push(Instruction::PushLiteral(LiteralValue::Integer(
                BigInt::zero(),
            )));
            return true;
        }

        push_default_for_storage_value_type(&reference.value_type, ctx, instructions);
        if !emit_storage_store(&reference, ctx, instructions) {
            instructions.push(Instruction::Drop(ValueType::Any));
        }

        // `delete` is a statement-only expression in Solidity; return a placeholder value so
        // expression statements can safely DROP it without underflowing.
        instructions.push(Instruction::PushLiteral(LiteralValue::Integer(
            BigInt::zero(),
        )));
        return true;
    }

    // Local/state variable delete.
    if let Expression::Variable(identifier) = target {
        if let Some(local_index) = ctx.resolve_local(&identifier.name) {
            if let Some(value_type) = ctx.local_type(local_index).cloned() {
                push_default_for_value_type(&value_type, ctx, instructions);
            } else {
                instructions.push(Instruction::PushLiteral(LiteralValue::Integer(
                    BigInt::zero(),
                )));
            }
            ctx.clear_call_data_local(local_index);
            instructions.push(Instruction::StoreLocal(local_index));
            instructions.push(Instruction::PushLiteral(LiteralValue::Integer(
                BigInt::zero(),
            )));
            return true;
        }

        if let Some(state_index) = ctx.state_index_map.get(&identifier.name).copied() {
            if !ctx.ensure_state_writable(state_index) {
                return false;
            }

            if let Some(state_type) = ctx.state_type(state_index).cloned() {
                if matches!(state_type, ValueType::Mapping { .. }) {
                    instructions.push(Instruction::PushLiteral(LiteralValue::Integer(
                        BigInt::zero(),
                    )));
                    return true;
                }

                if matches!(state_type, ValueType::Struct { .. }) {
                    let reference = StorageReference {
                        state_index,
                        key_expressions: Vec::new(),
                        key_types: Vec::new(),
                        value_type: state_type.clone(),
                        field_path: Vec::new(),
                        trailing_key_expressions: Vec::new(),
                        trailing_key_types: Vec::new(),
                    };

                    push_default_for_storage_value_type(&reference.value_type, ctx, instructions);
                    if !emit_storage_store(&reference, ctx, instructions) {
                        instructions.push(Instruction::Drop(ValueType::Any));
                    }
                    instructions.push(Instruction::PushLiteral(LiteralValue::Integer(
                        BigInt::zero(),
                    )));
                    return true;
                }

                push_default_for_storage_value_type(&state_type, ctx, instructions);
                instructions.push(Instruction::StoreState(state_index));
                instructions.push(Instruction::PushLiteral(LiteralValue::Integer(
                    BigInt::zero(),
                )));
                return true;
            }
        }
    }

    // Memory array element delete: `delete arr[i]`.
    if let Expression::ArraySubscript(_, array, Some(index)) = target {
        if let Some(ValueType::Array(element_type)) = infer_type_from_expression(array, ctx) {
            let tmp_id = ctx.next_label();
            let array_local = ctx.allocate_local(format!("__delete_arr_{tmp_id}"), None);
            let index_local = ctx.allocate_local(format!("__delete_idx_{tmp_id}"), None);

            if !lower_expression(array, ctx, instructions) {
                return false;
            }
            instructions.push(Instruction::StoreLocal(array_local));

            if !lower_expression(index, ctx, instructions) {
                return false;
            }
            instructions.push(Instruction::StoreLocal(index_local));

            instructions.push(Instruction::LoadLocal(array_local));
            instructions.push(Instruction::LoadLocal(index_local));
            push_default_for_value_type(element_type.as_ref(), ctx, instructions);
            instructions.push(Instruction::ArraySet);
            instructions.push(Instruction::PushLiteral(LiteralValue::Integer(
                BigInt::zero(),
            )));
            return true;
        }
    }

    // Memory struct field delete: `delete tmp.field`.
    if let Expression::MemberAccess(_, inner, member) = target {
        if let Expression::Variable(base) = inner.as_ref() {
            if let Some(local_index) = ctx.resolve_local(&base.name) {
                if let Some(ValueType::Struct { fields, .. }) = infer_type_from_expression(inner, ctx)
                {
                    if let Some((field_index, field)) = fields
                        .iter()
                        .enumerate()
                        .find(|(_, field)| field.name == member.name)
                    {
                        instructions.push(Instruction::LoadLocal(local_index));
                        instructions.push(Instruction::PushLiteral(LiteralValue::Integer(
                            BigInt::from(field_index as u64),
                        )));
                        push_default_for_value_type(&field.ty, ctx, instructions);
                        instructions.push(Instruction::ArraySet);
                        instructions.push(Instruction::PushLiteral(LiteralValue::Integer(
                            BigInt::zero(),
                        )));
                        return true;
                    }
                }
            }
        }
    }

    // Compatibility fallback for unsupported `delete` shapes:
    // preserve control flow by treating as a no-op.
    instructions.push(Instruction::PushLiteral(LiteralValue::Integer(BigInt::zero())));
    true
}

/// Lower `delete arr;` for a storage-backed array (top-level state variable
/// or `m[k]` mapping value) by clearing every element slot, mirroring the
/// shrink loop in `lower_storage_array_assign_from_memory`
/// (src/ir/statements/assignments/lower_assignment.rs):
///
///   * fixed-size `T[N]` state arrays — the bound is the compile-time `N`
///     parsed from the declared type string (same parse as Task #199's
///     `StorageArrayBound::FixedSizeKnown`); these arrays have no length
///     slot, so no base-slot write is needed (or possible).
///   * dynamic `T[]` arrays — the old length is read from the base slot
///     BEFORE it is zeroed, each element slot is reset to the element
///     default, then the base (length) slot is set to 0.
///
/// Returns `false` (without emitting the clear loop) for element types whose
/// defaults are not directly storable via a raw `Storage.Put` (structs,
/// mappings, `Any`); the caller then falls back to the legacy base-slot-only
/// write so behaviour for those shapes is unchanged.
fn lower_delete_storage_array(
    reference: &StorageReference,
    ctx: &mut LoweringContext,
    instructions: &mut Vec<Instruction>,
) -> bool {
    let element_type = match &reference.value_type {
        ValueType::Array(element) => (**element).clone(),
        _ => return false,
    };
    // Struct elements are stored per-field (see `emit_storage_store`'s
    // Struct branch) and Mapping/Any defaults are `Null` — a raw Put of
    // those values would fault on real Neo N3. Leave them on the legacy
    // base-slot-only path rather than make things worse.
    if matches!(
        element_type,
        ValueType::Struct { .. } | ValueType::Mapping { .. } | ValueType::Any
    ) {
        return false;
    }

    let uint256 = ValueType::Integer {
        signed: false,
        bits: 256,
    };
    let tmp_id = ctx.next_label();

    // Evaluate any outer mapping/array keys once (left-to-right, Solidity
    // order) so the per-element loop can re-push them without re-running
    // side effects.
    let mut key_locals: Vec<usize> = Vec::new();
    for (index, expr) in reference.key_expressions.iter().enumerate() {
        let local = ctx.allocate_local(
            format!("__delete_sarr_key_{tmp_id}_{index}"),
            reference.key_types.get(index).cloned(),
        );
        if !lower_expression(expr, ctx, instructions) {
            return false;
        }
        instructions.push(Instruction::StoreLocal(local));
        key_locals.push(local);
    }

    // Bound: compile-time N for a fixed-size `T[N]` state array (only
    // detectable on the direct state-variable shape — the declared type
    // string ends with `[N]`), otherwise the dynamic length read from the
    // base slot. The read MUST happen before the base slot is zeroed.
    let is_fixed_size = reference.key_expressions.is_empty()
        && ctx
            .state_metadata(reference.state_index)
            .map(|meta| meta.ty.clone())
            .and_then(|ty| extract_fixed_array_bound_at_depth(&ty, 0))
            .is_some();
    let len_local = ctx.allocate_local(
        format!("__delete_sarr_len_{tmp_id}"),
        Some(uint256.clone()),
    );
    if is_fixed_size {
        let bound = ctx
            .state_metadata(reference.state_index)
            .map(|meta| meta.ty.clone())
            .and_then(|ty| extract_fixed_array_bound_at_depth(&ty, 0))
            .unwrap_or(0);
        instructions.push(Instruction::PushLiteral(LiteralValue::Integer(
            BigInt::from(bound),
        )));
    } else if reference.key_expressions.is_empty() {
        // Dynamic state-var array: length lives at LoadState(state_index).
        instructions.push(Instruction::LoadState(reference.state_index));
    } else {
        // Mapping-of-dynamic-array: length lives at the mapping head slot.
        for local in key_locals.iter().rev() {
            instructions.push(Instruction::LoadLocal(*local));
        }
        instructions.push(Instruction::LoadMappingElement {
            state_index: reference.state_index,
            key_types: reference.key_types.clone(),
        });
    }
    instructions.push(Instruction::StoreLocal(len_local));

    // Clear loop: for (i = 0; i < len; i++) { arr[i] = default; }
    let mut element_key_types = reference.key_types.clone();
    element_key_types.push(uint256.clone());
    let cond_label = ctx.next_label();
    let end_label = ctx.next_label();
    let idx_local = ctx.allocate_local(
        format!("__delete_sarr_idx_{tmp_id}"),
        Some(uint256.clone()),
    );
    instructions.push(Instruction::PushLiteral(LiteralValue::Integer(BigInt::zero())));
    instructions.push(Instruction::StoreLocal(idx_local));

    instructions.push(Instruction::Label(cond_label));
    instructions.push(Instruction::LoadLocal(idx_local));
    instructions.push(Instruction::LoadLocal(len_local));
    instructions.push(Instruction::BinaryOp(BinaryOperator::Lt));
    // IR JumpIf branches when the condition is FALSE: exit once idx >= len.
    instructions.push(Instruction::JumpIf { target: end_label });

    push_default_for_value_type(&element_type, ctx, instructions);
    // Keys are pushed deepest-first (the bytecode slot-hash helper expects
    // the outermost key on top once the base slot is pushed).
    instructions.push(Instruction::LoadLocal(idx_local));
    for local in key_locals.iter().rev() {
        instructions.push(Instruction::LoadLocal(*local));
    }
    if matches!(element_type, ValueType::Array(_)) {
        instructions.push(Instruction::StoreArrayDeepCopy {
            state_index: reference.state_index,
            key_types: element_key_types.clone(),
        });
    } else {
        instructions.push(Instruction::StoreMappingElement {
            state_index: reference.state_index,
            key_types: element_key_types.clone(),
        });
    }

    instructions.push(Instruction::LoadLocal(idx_local));
    instructions.push(Instruction::PushLiteral(LiteralValue::Integer(BigInt::one())));
    instructions.push(Instruction::BinaryOp(BinaryOperator::Add));
    instructions.push(Instruction::StoreLocal(idx_local));
    instructions.push(Instruction::Jump { target: cond_label });
    instructions.push(Instruction::Label(end_label));

    // Dynamic arrays: zero the base (length) slot. Fixed-size arrays have no
    // length slot — leave the never-read base slot untouched.
    if !is_fixed_size {
        instructions.push(Instruction::PushLiteral(LiteralValue::Integer(BigInt::zero())));
        for local in key_locals.iter().rev() {
            instructions.push(Instruction::LoadLocal(*local));
        }
        instructions.push(Instruction::StoreMappingElement {
            state_index: reference.state_index,
            key_types: reference.key_types.clone(),
        });
    }

    true
}