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
use crate::{
parser::ast::{Definition, Program},
shared::*,
};
use std::collections::BTreeSet;
use super::ast::LeadingNameAccess_;
pub fn program(compilation_env: &CompilationEnv, prog: Program) -> Program {
if !compilation_env.flags().sources_shadow_deps() {
return prog;
}
let Program {
source_definitions,
lib_definitions,
} = prog;
let mut modules_defined_in_src = BTreeSet::new();
for def in &source_definitions {
match def {
Definition::Address(a) => {
let addr = a.addr.value;
for module in &a.modules {
modules_defined_in_src.insert((addr, module.name));
}
}
Definition::Module(module) => {
modules_defined_in_src.insert((
module.address.map(|a| a.value).unwrap_or_else(|| {
LeadingNameAccess_::AnonymousAddress(AddressBytes::DEFAULT_ERROR_BYTES)
}),
module.name,
));
}
Definition::Script(_) => (),
}
}
let lib_definitions = lib_definitions
.into_iter()
.map(|def| match def {
Definition::Address(mut a) => {
let addr = a.addr.value;
let modules = std::mem::take(&mut a.modules);
a.modules = modules
.into_iter()
.filter(|module| !modules_defined_in_src.contains(&(addr, module.name)))
.collect();
Definition::Address(a)
}
def => def,
})
.filter(|def| match def {
Definition::Address(_) => true,
Definition::Module(module) => !modules_defined_in_src.contains(&(
module.address.map(|a| a.value).unwrap_or_else(|| {
LeadingNameAccess_::AnonymousAddress(AddressBytes::DEFAULT_ERROR_BYTES)
}),
module.name,
)),
Definition::Script(_) => false,
})
.collect();
Program {
source_definitions,
lib_definitions,
}
}