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
// Copyright (C) 2019-2026 Provable Inc.
// This file is part of the Leo library.
// The Leo library is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// The Leo library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with the Leo library. If not, see <https://www.gnu.org/licenses/>.
//! Performs lowering of `storage` variables and storage vectors into explicit `Mapping` operations.
//!
//! This pass rewrites high-level storage constructs—such as `storage` declarations and `Vector` methods concrete
//! calls to the underlying `Mapping` API used in Aleo programs. Each `storage` variable is de-sugared into one or more
//! `Mapping` instances, and all read/write operations are rewritten as `Mapping::get`, `Mapping::set`, or
//! `Mapping::get_or_use` calls.
//!
//! ### Overview
//!
//! - **Storage Variables:** Each top-level `storage` variable (e.g., `storage counter: u32;`) is represented by a
//! `Mapping` that persists across transitions. The pass introduces ternary expressions that check whether the
//! underlying mapping contains a value before reading it.
//!
//! - **Storage Vectors:** A `storage` vector (e.g., `storage vec: [u32];`) is lowered into two mappings:
//! - `<vec_name>__` stores the vector elements keyed by a `u32` index.
//! - `<vec_name>__len__` stores the current vector length under key `false`.
//!
//! Vector operations such as `.push()`, `.pop()`, `.len()`, `.get()`, `.set()`, `.swap_remove()`, and `.clear()`
//! are translated into combinations of `Mapping` calls and helper expressions that manipulate the length mapping and
//! element mappings directly.
//!
//! ### Example: Storage Vector
//!
//! ```leo
//! storage vec: [u32];
//!
//! async transition test_vector_ops() -> Future {
//! return async {
//! vec.push(10u32);
//! let x = vec.get(0u32).unwrap();
//! let y = vec.pop();
//! };
//! }
//! ```
//!
//! is lowered to:
//!
//! ```leo
//! mapping vec__: u32 => u32; // vector values
//! mapping vec__len__: bool => u32; // length
//!
//! // vec.push(10u32);
//! let $len_var = Mapping::get_or_use(vec__len__, false, 0u32);
//! Mapping::set(vec__, $len_var, 10u32);
//! Mapping::set(vec__len__, false, $len_var + 1u32);
//!
//! // let x = vec.get(3u32);
//! let $len_var = Mapping::get_or_use(vec__len__, false, 0u32);
//! let x = 3u32 < $len_var ? Mapping::get_or_use(vec__, 3u32, 0u32) : None;
//!
//! // let y = vec.pop();
//! let $len_var = Mapping::get_or_use(vec__len__, false, 0u32);
//! if ($len_var > 0u32) { Mapping::set(vec__len__, false, $len_var - 1u32); }
//! let y = $len_var > 0u32 ? Mapping::get_or_use(vec__, $len_var - 1u32, 0u32) : None;
//! ```
//!
//! ### Example: Singleton Storage
//!
//! ```leo
//! storage counter: u32;
//!
//! async transition increment() -> Future {
//! return async {
//! let old = counter.unwrap_or(0u32);
//! counter = old + 1u32;
//! };
//! }
//! ```
//!
//! is lowered to:
//!
//! ```leo
//! mapping counter__: bool => u32;
//!
//! // let old = counter.unwrap_or(0u32)
//! let old = counter__.contains(false) ? counter__.get_or_use(false, 0u32) : 0u32
//!
//! // counter = old + 1u32
//! Mapping::set(counter__, false, old + 1u32)
//! ```
use crate::;
use ProgramReconstructor as _;
use Result;
use Symbol;
use IndexMap;
use *;
;