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
use std::collections::{BTreeMap, BTreeSet};
#[allow(unused_imports)]
use log::{debug, info, warn};
use num::BigUint;
use move_lang::{expansion::ast as EA, parser::ast as PA, shared::AddressBytes};
use move_symbol_pool::Symbol as MoveStringSymbol;
use crate::{
ast::{ModuleName, Operation, QualifiedSymbol, Spec, Value},
builder::spec_builtins,
model::{
FunId, FunctionVisibility, GlobalEnv, Loc, ModuleId, QualifiedId, SpecFunId, SpecVarId,
StructId,
},
project_2nd,
symbol::Symbol,
ty::Type,
};
use codespan_reporting::diagnostic::Severity;
#[derive(Debug)]
pub(crate) struct ModelBuilder<'env> {
pub env: &'env mut GlobalEnv,
pub named_address_mapping: BTreeMap<MoveStringSymbol, AddressBytes>,
pub spec_fun_table: BTreeMap<QualifiedSymbol, Vec<SpecFunEntry>>,
pub spec_var_table: BTreeMap<QualifiedSymbol, SpecVarEntry>,
pub spec_schema_table: BTreeMap<QualifiedSymbol, SpecSchemaEntry>,
pub unused_schema_set: BTreeSet<QualifiedSymbol>,
pub struct_table: BTreeMap<QualifiedSymbol, StructEntry>,
pub reverse_struct_table: BTreeMap<(ModuleId, StructId), QualifiedSymbol>,
pub fun_table: BTreeMap<QualifiedSymbol, FunEntry>,
pub const_table: BTreeMap<QualifiedSymbol, ConstEntry>,
pub move_fun_call_graph: BTreeMap<QualifiedId<SpecFunId>, BTreeSet<QualifiedId<SpecFunId>>>,
}
#[derive(Debug, Clone)]
pub(crate) struct SpecFunEntry {
pub loc: Loc,
pub oper: Operation,
pub type_params: Vec<Type>,
pub arg_types: Vec<Type>,
pub result_type: Type,
}
#[derive(Debug, Clone)]
pub(crate) struct SpecVarEntry {
pub loc: Loc,
pub module_id: ModuleId,
pub var_id: SpecVarId,
pub type_params: Vec<Type>,
pub type_: Type,
}
#[derive(Debug)]
pub(crate) struct SpecSchemaEntry {
pub loc: Loc,
pub name: QualifiedSymbol,
pub module_id: ModuleId,
pub type_params: Vec<(Symbol, Type)>,
pub vars: Vec<(Symbol, Type)>,
pub spec: Spec,
pub all_vars: BTreeMap<Symbol, LocalVarEntry>,
pub included_spec: Spec,
}
#[derive(Debug, Clone)]
pub(crate) struct StructEntry {
pub loc: Loc,
pub module_id: ModuleId,
pub struct_id: StructId,
pub is_resource: bool,
pub type_params: Vec<(Symbol, Type)>,
pub fields: Option<BTreeMap<Symbol, (usize, Type)>>,
}
#[derive(Debug, Clone)]
pub(crate) struct FunEntry {
pub loc: Loc,
pub module_id: ModuleId,
pub fun_id: FunId,
pub visibility: FunctionVisibility,
pub type_params: Vec<(Symbol, Type)>,
pub params: Vec<(Symbol, Type)>,
pub result_type: Type,
pub is_pure: bool,
}
#[derive(Debug, Clone)]
pub(crate) struct ConstEntry {
pub loc: Loc,
pub ty: Type,
pub value: Value,
}
impl<'env> ModelBuilder<'env> {
pub fn new(
env: &'env mut GlobalEnv,
named_address_mapping: BTreeMap<MoveStringSymbol, AddressBytes>,
) -> Self {
let mut translator = ModelBuilder {
env,
named_address_mapping,
spec_fun_table: BTreeMap::new(),
spec_var_table: BTreeMap::new(),
spec_schema_table: BTreeMap::new(),
unused_schema_set: BTreeSet::new(),
struct_table: BTreeMap::new(),
reverse_struct_table: BTreeMap::new(),
fun_table: BTreeMap::new(),
const_table: BTreeMap::new(),
move_fun_call_graph: BTreeMap::new(),
};
spec_builtins::declare_spec_builtins(&mut translator);
translator
}
pub fn to_loc(&self, loc: &move_ir_types::location::Loc) -> Loc {
self.env.to_loc(loc)
}
pub fn error(&self, at: &Loc, msg: &str) {
self.env.error(at, msg)
}
pub fn error_with_notes(&self, at: &Loc, msg: &str, notes: Vec<String>) {
self.env.error_with_notes(at, msg, notes)
}
pub fn define_spec_fun(&mut self, name: QualifiedSymbol, entry: SpecFunEntry) {
self.spec_fun_table
.entry(name)
.or_insert_with(Vec::new)
.push(entry);
}
pub fn define_spec_var(
&mut self,
loc: &Loc,
name: QualifiedSymbol,
module_id: ModuleId,
var_id: SpecVarId,
type_params: Vec<Type>,
type_: Type,
) {
let entry = SpecVarEntry {
loc: loc.clone(),
module_id,
var_id,
type_params,
type_,
};
if let Some(old) = self.spec_var_table.insert(name.clone(), entry) {
let var_name = name.display(self.env.symbol_pool());
self.error(loc, &format!("duplicate declaration of `{}`", var_name));
self.error(&old.loc, &format!("previous declaration of `{}`", var_name));
}
}
pub fn define_spec_schema(
&mut self,
loc: &Loc,
name: QualifiedSymbol,
module_id: ModuleId,
type_params: Vec<(Symbol, Type)>,
vars: Vec<(Symbol, Type)>,
) {
let entry = SpecSchemaEntry {
loc: loc.clone(),
name: name.clone(),
module_id,
type_params,
vars,
spec: Spec::default(),
all_vars: BTreeMap::new(),
included_spec: Spec::default(),
};
if let Some(old) = self.spec_schema_table.insert(name.clone(), entry) {
let schema_display = name.display(self.env.symbol_pool());
self.error(
loc,
&format!("duplicate declaration of `{}`", schema_display),
);
self.error(
&old.loc,
&format!("previous declaration of `{}`", schema_display),
);
}
self.unused_schema_set.insert(name);
}
pub fn define_struct(
&mut self,
loc: Loc,
name: QualifiedSymbol,
module_id: ModuleId,
struct_id: StructId,
is_resource: bool,
type_params: Vec<(Symbol, Type)>,
fields: Option<BTreeMap<Symbol, (usize, Type)>>,
) {
let entry = StructEntry {
loc,
module_id,
struct_id,
is_resource,
type_params,
fields,
};
assert!(self.struct_table.insert(name.clone(), entry).is_none());
self.reverse_struct_table
.insert((module_id, struct_id), name);
}
pub fn define_fun(
&mut self,
loc: Loc,
name: QualifiedSymbol,
module_id: ModuleId,
fun_id: FunId,
visibility: FunctionVisibility,
type_params: Vec<(Symbol, Type)>,
params: Vec<(Symbol, Type)>,
result_type: Type,
) {
let entry = FunEntry {
loc,
module_id,
fun_id,
visibility,
type_params,
params,
result_type,
is_pure: false,
};
assert!(self.fun_table.insert(name, entry).is_none());
}
pub fn define_const(&mut self, name: QualifiedSymbol, entry: ConstEntry) {
assert!(self.const_table.insert(name, entry).is_none());
}
pub fn resolve_address(&self, loc: &Loc, addr: &EA::Address) -> AddressBytes {
match addr {
EA::Address::Anonymous(bytes) => bytes.value,
EA::Address::Named(n) => self
.named_address_mapping
.get(&n.value)
.cloned()
.unwrap_or_else(|| {
self.error(loc, &format!("Undeclared address `{}`", n));
AddressBytes::DEFAULT_ERROR_BYTES
}),
}
}
pub fn lookup_type(&self, loc: &Loc, name: &QualifiedSymbol) -> Type {
self.struct_table
.get(name)
.cloned()
.map(|e| Type::Struct(e.module_id, e.struct_id, project_2nd(&e.type_params)))
.unwrap_or_else(|| {
self.error(
loc,
&format!("undeclared `{}`", name.display_full(self.env.symbol_pool())),
);
Type::Error
})
}
pub fn warn_unused_schemas(&self) {
for name in &self.unused_schema_set {
let entry = self.spec_schema_table.get(name).expect("schema defined");
let schema_name = name.display_simple(self.env.symbol_pool()).to_string();
let module_env = self.env.get_module(entry.module_id);
if module_env.is_target() && !schema_name.starts_with("UNUSED") {
self.env.diag(
Severity::Note,
&entry.loc,
&format!("unused schema {}", name.display(self.env.symbol_pool())),
);
}
}
}
pub fn bin_op_symbol(&self, op: &PA::BinOp_) -> QualifiedSymbol {
QualifiedSymbol {
module_name: self.builtin_module(),
symbol: self.env.symbol_pool().make(op.symbol()),
}
}
pub fn unary_op_symbol(&self, op: &PA::UnaryOp_) -> QualifiedSymbol {
QualifiedSymbol {
module_name: self.builtin_module(),
symbol: self.env.symbol_pool().make(op.symbol()),
}
}
pub fn builtin_qualified_symbol(&self, name: &str) -> QualifiedSymbol {
QualifiedSymbol {
module_name: self.builtin_module(),
symbol: self.env.symbol_pool().make(name),
}
}
pub fn old_symbol(&self) -> Symbol {
self.env.symbol_pool().make("old")
}
pub fn assert_symbol(&self) -> Symbol {
self.env.symbol_pool().make("assert")
}
pub fn builtin_module(&self) -> ModuleName {
ModuleName::new(BigUint::default(), self.env.symbol_pool().make("$$"))
}
pub fn add_used_spec_fun(&mut self, qid: QualifiedId<SpecFunId>) {
self.env.used_spec_funs.insert(qid);
self.propagate_move_fun_usage(qid);
}
pub fn add_edge_to_move_fun_call_graph(
&mut self,
caller: QualifiedId<SpecFunId>,
callee: QualifiedId<SpecFunId>,
) {
self.move_fun_call_graph
.entry(caller)
.or_default()
.insert(callee);
}
pub fn propagate_move_fun_usage(&mut self, qid: QualifiedId<SpecFunId>) {
if let Some(neighbors) = self.move_fun_call_graph.get(&qid) {
neighbors.clone().iter().for_each(|n| {
if self.env.used_spec_funs.insert(*n) {
self.propagate_move_fun_usage(*n);
}
});
}
}
}
#[derive(Debug, Clone)]
pub(crate) struct LocalVarEntry {
pub loc: Loc,
pub type_: Type,
pub operation: Option<Operation>,
pub temp_index: Option<usize>,
}