forked from argotorg/fe
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontext.rs
More file actions
81 lines (72 loc) · 2.46 KB
/
Copy pathcontext.rs
File metadata and controls
81 lines (72 loc) · 2.46 KB
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
use indexmap::IndexSet;
use fe_analyzer::namespace::items::ContractId;
use fe_mir::ir::FunctionId;
use fxhash::FxHashSet;
use yultsur::yul;
use crate::{
db::CodegenDb,
yul::runtime::{DefaultRuntimeProvider, RuntimeProvider},
};
use super::{lower_contract_deployable, lower_function};
pub struct Context {
pub runtime: Box<dyn RuntimeProvider>,
pub(super) contract_dependency: IndexSet<ContractId>,
pub(super) function_dependency: IndexSet<FunctionId>,
pub(super) string_constants: IndexSet<String>,
pub(super) lowered_functions: FxHashSet<FunctionId>,
}
// Currently, `clippy::derivable_impls` causes false positive result,
// see https://github.com/rust-lang/rust-clippy/issues/10158 for more details.
#[allow(clippy::derivable_impls)]
impl Default for Context {
fn default() -> Self {
Self {
runtime: Box::<DefaultRuntimeProvider>::default(),
contract_dependency: IndexSet::default(),
function_dependency: IndexSet::default(),
string_constants: IndexSet::default(),
lowered_functions: FxHashSet::default(),
}
}
}
impl Context {
pub(super) fn resolve_function_dependency(
&mut self,
db: &dyn CodegenDb,
) -> Vec<yul::FunctionDefinition> {
let mut funcs = vec![];
loop {
let dependencies = std::mem::take(&mut self.function_dependency);
if dependencies.is_empty() {
break;
}
for dependency in dependencies {
if self.lowered_functions.contains(&dependency) {
// Ignore dependency if it's already lowered.
continue;
} else {
funcs.push(lower_function(db, self, dependency))
}
}
}
funcs
}
pub(super) fn resolve_constant_dependency(&self, db: &dyn CodegenDb) -> Vec<yul::Data> {
self.string_constants
.iter()
.map(|s| {
let symbol = db.codegen_constant_string_symbol_name(s.to_string());
yul::Data {
name: symbol.as_ref().clone(),
value: s.to_string(),
}
})
.collect()
}
pub(super) fn resolve_contract_dependency(&self, db: &dyn CodegenDb) -> Vec<yul::Object> {
self.contract_dependency
.iter()
.map(|cid| lower_contract_deployable(db, *cid))
.collect()
}
}