-
Notifications
You must be signed in to change notification settings - Fork 3.7k
/
mod.rs
618 lines (531 loc) · 18.8 KB
/
mod.rs
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
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
// Copyright (c) The Diem Core Contributors
// Copyright (c) The Move Contributors
// SPDX-License-Identifier: Apache-2.0
use crate::{
command_line as cli,
diagnostics::{codes::Severity, Diagnostic, Diagnostics},
naming::ast::ModuleDefinition,
};
use clap::*;
use move_ir_types::location::*;
use move_symbol_pool::Symbol;
use petgraph::{algo::astar as petgraph_astar, graphmap::DiGraphMap};
use std::{
collections::BTreeMap,
fmt,
hash::Hash,
sync::atomic::{AtomicUsize, Ordering as AtomicOrdering},
};
pub mod ast_debug;
pub mod remembering_unique_map;
pub mod unique_map;
pub mod unique_set;
//**************************************************************************************************
// Numbers
//**************************************************************************************************
//**************************************************************************************************
// Address
//**************************************************************************************************
pub use move_command_line_common::{
address::NumericalAddress,
parser::{
parse_address_number as parse_address, parse_u128, parse_u16, parse_u256, parse_u32,
parse_u64, parse_u8, NumberFormat,
},
};
pub fn parse_named_address(s: &str) -> anyhow::Result<(String, NumericalAddress)> {
let before_after = s.split('=').collect::<Vec<_>>();
if before_after.len() != 2 {
anyhow::bail!(
"Invalid named address assignment. Must be of the form <address_name>=<address>, but \
found '{}'",
s
);
}
let name = before_after[0].parse()?;
let addr = NumericalAddress::parse_str(before_after[1])
.map_err(|err| anyhow::format_err!("{}", err))?;
Ok((name, addr))
}
//**************************************************************************************************
// Name
//**************************************************************************************************
pub trait TName: Eq + Ord + Clone {
type Key: Ord + Clone;
type Loc: Copy;
fn drop_loc(self) -> (Self::Loc, Self::Key);
fn add_loc(loc: Self::Loc, key: Self::Key) -> Self;
fn borrow(&self) -> (&Self::Loc, &Self::Key);
}
pub trait Identifier {
fn value(&self) -> Symbol;
fn loc(&self) -> Loc;
}
// TODO maybe we should intern these strings somehow
pub type Name = Spanned<Symbol>;
impl TName for Name {
type Key = Symbol;
type Loc = Loc;
fn drop_loc(self) -> (Loc, Symbol) {
(self.loc, self.value)
}
fn add_loc(loc: Loc, key: Symbol) -> Self {
sp(loc, key)
}
fn borrow(&self) -> (&Loc, &Symbol) {
(&self.loc, &self.value)
}
}
//**************************************************************************************************
// Graphs
//**************************************************************************************************
pub fn shortest_cycle<'a, T: Ord + Hash>(
dependency_graph: &DiGraphMap<&'a T, ()>,
start: &'a T,
) -> Vec<&'a T> {
let shortest_path = dependency_graph
.neighbors(start)
.fold(None, |shortest_path, neighbor| {
let path_opt = petgraph_astar(
dependency_graph,
neighbor,
|finish| finish == start,
|_e| 1,
|_| 0,
);
match (shortest_path, path_opt) {
(p, None) | (None, p) => p,
(Some((acc_len, acc_path)), Some((cur_len, cur_path))) => Some(
if cur_len < acc_len {
(cur_len, cur_path)
} else {
(acc_len, acc_path)
},
),
}
});
let (_, mut path) = shortest_path.unwrap();
path.insert(0, start);
path
}
//**************************************************************************************************
// Compilation Env
//**************************************************************************************************
pub type NamedAddressMap = BTreeMap<Symbol, NumericalAddress>;
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct NamedAddressMapIndex(usize);
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct NamedAddressMaps(Vec<NamedAddressMap>);
impl NamedAddressMaps {
#[allow(clippy::new_without_default)]
pub fn new() -> Self {
Self(vec![])
}
pub fn insert(&mut self, m: NamedAddressMap) -> NamedAddressMapIndex {
let index = self.0.len();
self.0.push(m);
NamedAddressMapIndex(index)
}
pub fn get(&self, idx: NamedAddressMapIndex) -> &NamedAddressMap {
&self.0[idx.0]
}
}
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct PackagePaths<Path: Into<Symbol> = Symbol, NamedAddress: Into<Symbol> = Symbol> {
pub name: Option<Symbol>,
pub paths: Vec<Path>,
pub named_address_map: BTreeMap<NamedAddress, NumericalAddress>,
}
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct IndexedPackagePath {
pub package: Option<Symbol>,
pub path: Symbol,
pub named_address_map: NamedAddressMapIndex,
}
pub type AttributeDeriver = dyn Fn(&mut CompilationEnv, &mut ModuleDefinition);
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct CompilationEnv {
flags: Flags,
diags: Diagnostics,
// TODO(tzakian): Remove the global counter and use this counter instead
// pub counter: u64,
}
impl CompilationEnv {
pub fn new(flags: Flags) -> Self {
Self {
flags,
diags: Diagnostics::new(),
}
}
pub fn add_diag(&mut self, diag: Diagnostic) {
self.diags.add(diag)
}
pub fn add_diags(&mut self, diags: Diagnostics) {
self.diags.extend(diags)
}
pub fn has_warnings_or_errors(&self) -> bool {
!self.diags.is_empty()
}
pub fn has_errors(&self) -> bool {
// Non-blocking Error is the min level considered an error
self.has_diags_at_or_above_severity(Severity::NonblockingError)
}
pub fn count_diags(&self) -> usize {
self.diags.len()
}
pub fn has_diags_at_or_above_severity(&self, threshold: Severity) -> bool {
match self.diags.max_severity() {
Some(max) if max >= threshold => true,
Some(_) | None => false,
}
}
pub fn check_diags_at_or_above_severity(
&mut self,
threshold: Severity,
) -> Result<(), Diagnostics> {
if self.has_diags_at_or_above_severity(threshold) {
Err(std::mem::take(&mut self.diags))
} else {
Ok(())
}
}
/// Should only be called after compilation is finished
pub fn take_final_warning_diags(&mut self) -> Diagnostics {
let final_diags = std::mem::take(&mut self.diags);
debug_assert!(final_diags
.max_severity()
.map(|s| s == Severity::Warning)
.unwrap_or(true));
final_diags
}
pub fn flags(&self) -> &Flags {
&self.flags
}
}
//**************************************************************************************************
// Counter
//**************************************************************************************************
#[derive(Clone, Debug, Eq, Hash, PartialEq)]
pub struct Counter(usize);
impl Counter {
pub fn next() -> u64 {
static COUNTER_NEXT: AtomicUsize = AtomicUsize::new(0);
COUNTER_NEXT.fetch_add(1, AtomicOrdering::AcqRel) as u64
}
}
//**************************************************************************************************
// Display
//**************************************************************************************************
pub fn format_delim<T: fmt::Display, I: IntoIterator<Item = T>>(items: I, delim: &str) -> String {
items
.into_iter()
.map(|item| format!("{}", item))
.collect::<Vec<_>>()
.join(delim)
}
pub fn format_comma<T: fmt::Display, I: IntoIterator<Item = T>>(items: I) -> String {
format_delim(items, ", ")
}
//**************************************************************************************************
// Flags
//**************************************************************************************************
#[derive(Clone, Debug, Eq, PartialEq, Parser)]
pub struct Flags {
/// Compile in test mode
#[clap(
short = cli::TEST_SHORT,
long = cli::TEST,
)]
test: bool,
/// Compile in verification mode
#[clap(
short = cli::VERIFY_SHORT,
long = cli::VERIFY,
)]
verify: bool,
/// Compilation flavor.
#[clap(
long = cli::FLAVOR,
)]
flavor: String,
/// Bytecode version.
#[clap(
long = cli::BYTECODE_VERSION,
)]
bytecode_version: Option<u32>,
/// If set, source files will not shadow dependency files. If the same file is passed to both,
/// an error will be raised
#[clap(
name = "SOURCES_SHADOW_DEPS",
short = cli::SHADOW_SHORT,
long = cli::SHADOW,
)]
shadow: bool,
/// Internal flag used by the model builder to maintain functions which would be otherwise
/// included only in tests, without creating the unit test code regular tests do.
#[clap(skip)]
keep_testing_functions: bool,
}
impl Flags {
pub fn empty() -> Self {
Self {
test: false,
verify: false,
shadow: false,
flavor: "".to_string(),
bytecode_version: None,
keep_testing_functions: false,
}
}
pub fn testing() -> Self {
Self {
test: true,
verify: false,
shadow: false,
flavor: "".to_string(),
bytecode_version: None,
keep_testing_functions: false,
}
}
pub fn verification() -> Self {
Self {
test: false,
verify: true,
shadow: true, // allows overlapping between sources and deps
flavor: "".to_string(),
bytecode_version: None,
keep_testing_functions: false,
}
}
pub fn model_compilation() -> Self {
Self {
test: false,
verify: true,
shadow: true, // allows overlapping between sources and deps
flavor: "".to_string(),
bytecode_version: None,
keep_testing_functions: true,
}
}
pub fn set_flavor(self, flavor: impl ToString) -> Self {
Self {
flavor: flavor.to_string(),
..self
}
}
pub fn set_keep_testing_functions(self, value: bool) -> Self {
Self {
keep_testing_functions: value,
..self
}
}
pub fn set_sources_shadow_deps(self, sources_shadow_deps: bool) -> Self {
Self {
shadow: sources_shadow_deps,
..self
}
}
pub fn is_empty(&self) -> bool {
self == &Self::empty()
}
pub fn is_testing(&self) -> bool {
self.test
}
pub fn keep_testing_functions(&self) -> bool {
self.test || self.keep_testing_functions
}
pub fn is_verification(&self) -> bool {
self.verify
}
pub fn sources_shadow_deps(&self) -> bool {
self.shadow
}
pub fn has_flavor(&self, flavor: &str) -> bool {
self.flavor == flavor
}
pub fn bytecode_version(&self) -> Option<u32> {
self.bytecode_version
}
}
//**************************************************************************************************
// Attributes
//**************************************************************************************************
pub mod known_attributes {
use once_cell::sync::Lazy;
use std::{collections::BTreeSet, fmt};
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
pub enum AttributePosition {
AddressBlock,
Module,
Script,
Use,
Friend,
Constant,
Struct,
Function,
Spec,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
pub enum KnownAttribute {
Testing(TestingAttribute),
Verification(VerificationAttribute),
Native(NativeAttribute),
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
pub enum TestingAttribute {
// Can be called by other testing code, and included in compilation in test mode
TestOnly,
// Is a test that will be run
Test,
// This test is expected to fail
ExpectedFailure,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
pub enum VerificationAttribute {
// The associated AST node will be included in the compilation in prove mode
VerifyOnly,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
pub enum NativeAttribute {
// It is a fake native function that actually compiles to a bytecode instruction
BytecodeInstruction,
}
impl fmt::Display for AttributePosition {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::AddressBlock => write!(f, "address block"),
Self::Module => write!(f, "module"),
Self::Script => write!(f, "script"),
Self::Use => write!(f, "use"),
Self::Friend => write!(f, "friend"),
Self::Constant => write!(f, "constant"),
Self::Struct => write!(f, "struct"),
Self::Function => write!(f, "function"),
Self::Spec => write!(f, "spec"),
}
}
}
impl KnownAttribute {
pub fn resolve(attribute_str: impl AsRef<str>) -> Option<Self> {
Some(match attribute_str.as_ref() {
TestingAttribute::TEST => Self::Testing(TestingAttribute::Test),
TestingAttribute::TEST_ONLY => Self::Testing(TestingAttribute::TestOnly),
TestingAttribute::EXPECTED_FAILURE => {
Self::Testing(TestingAttribute::ExpectedFailure)
},
VerificationAttribute::VERIFY_ONLY => {
Self::Verification(VerificationAttribute::VerifyOnly)
},
NativeAttribute::BYTECODE_INSTRUCTION => {
Self::Native(NativeAttribute::BytecodeInstruction)
},
_ => return None,
})
}
pub const fn name(&self) -> &str {
match self {
Self::Testing(a) => a.name(),
Self::Verification(a) => a.name(),
Self::Native(a) => a.name(),
}
}
pub fn expected_positions(&self) -> &'static BTreeSet<AttributePosition> {
match self {
Self::Testing(a) => a.expected_positions(),
Self::Verification(a) => a.expected_positions(),
Self::Native(a) => a.expected_positions(),
}
}
}
impl TestingAttribute {
pub const ABORT_CODE_NAME: &'static str = "abort_code";
pub const ARITHMETIC_ERROR_NAME: &'static str = "arithmetic_error";
pub const ERROR_LOCATION: &'static str = "location";
pub const EXPECTED_FAILURE: &'static str = "expected_failure";
pub const MAJOR_STATUS_NAME: &'static str = "major_status";
pub const MINOR_STATUS_NAME: &'static str = "minor_status";
pub const OUT_OF_GAS_NAME: &'static str = "out_of_gas";
pub const TEST: &'static str = "test";
pub const TEST_ONLY: &'static str = "test_only";
pub const VECTOR_ERROR_NAME: &'static str = "vector_error";
pub const fn name(&self) -> &str {
match self {
Self::Test => Self::TEST,
Self::TestOnly => Self::TEST_ONLY,
Self::ExpectedFailure => Self::EXPECTED_FAILURE,
}
}
pub fn expected_positions(&self) -> &'static BTreeSet<AttributePosition> {
static TEST_ONLY_POSITIONS: Lazy<BTreeSet<AttributePosition>> = Lazy::new(|| {
IntoIterator::into_iter([
AttributePosition::AddressBlock,
AttributePosition::Module,
AttributePosition::Use,
AttributePosition::Friend,
AttributePosition::Constant,
AttributePosition::Struct,
AttributePosition::Function,
])
.collect()
});
static TEST_POSITIONS: Lazy<BTreeSet<AttributePosition>> =
Lazy::new(|| IntoIterator::into_iter([AttributePosition::Function]).collect());
static EXPECTED_FAILURE_POSITIONS: Lazy<BTreeSet<AttributePosition>> =
Lazy::new(|| IntoIterator::into_iter([AttributePosition::Function]).collect());
match self {
TestingAttribute::TestOnly => &TEST_ONLY_POSITIONS,
TestingAttribute::Test => &TEST_POSITIONS,
TestingAttribute::ExpectedFailure => &EXPECTED_FAILURE_POSITIONS,
}
}
pub fn expected_failure_cases() -> &'static [&'static str] {
&[
Self::ABORT_CODE_NAME,
Self::ARITHMETIC_ERROR_NAME,
Self::VECTOR_ERROR_NAME,
Self::OUT_OF_GAS_NAME,
Self::MAJOR_STATUS_NAME,
]
}
}
impl VerificationAttribute {
pub const VERIFY_ONLY: &'static str = "verify_only";
pub const fn name(&self) -> &str {
match self {
Self::VerifyOnly => Self::VERIFY_ONLY,
}
}
pub fn expected_positions(&self) -> &'static BTreeSet<AttributePosition> {
static VERIFY_ONLY_POSITIONS: Lazy<BTreeSet<AttributePosition>> = Lazy::new(|| {
IntoIterator::into_iter([
AttributePosition::AddressBlock,
AttributePosition::Module,
AttributePosition::Use,
AttributePosition::Friend,
AttributePosition::Constant,
AttributePosition::Struct,
AttributePosition::Function,
])
.collect()
});
match self {
Self::VerifyOnly => &VERIFY_ONLY_POSITIONS,
}
}
}
impl NativeAttribute {
pub const BYTECODE_INSTRUCTION: &'static str = "bytecode_instruction";
pub const fn name(&self) -> &str {
match self {
NativeAttribute::BytecodeInstruction => Self::BYTECODE_INSTRUCTION,
}
}
pub fn expected_positions(&self) -> &'static BTreeSet<AttributePosition> {
static BYTECODE_INSTRUCTION_POSITIONS: Lazy<BTreeSet<AttributePosition>> =
Lazy::new(|| IntoIterator::into_iter([AttributePosition::Function]).collect());
match self {
NativeAttribute::BytecodeInstruction => &BYTECODE_INSTRUCTION_POSITIONS,
}
}
}
}