Skip to content

Commit f4a109d

Browse files
committed
Cleanup, remove Hir:: usage, use ContractIds
1 parent c86c4aa commit f4a109d

File tree

4 files changed

+43
-44
lines changed

4 files changed

+43
-44
lines changed

crates/compilers/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -903,6 +903,7 @@ fn apply_updates(sources: &mut Sources, updates: Updates) {
903903
}
904904

905905
/// Utility function to change source content ranges with provided updates.
906+
/// Assumes that the updates are sorted.
906907
fn replace_source_content<'a>(
907908
source: &str,
908909
updates: impl IntoIterator<Item = (Range<usize>, &'a str)>,

crates/compilers/src/preprocessor/data.rs

Lines changed: 18 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -12,39 +12,39 @@ use std::{
1212
};
1313

1414
/// Keeps data about project contracts definitions referenced from tests and scripts.
15-
/// HIR id -> Contract data definition mapping.
16-
pub type PreprocessorData = BTreeMap<u32, ContractData>;
15+
/// Contract id -> Contract data definition mapping.
16+
pub type PreprocessorData = BTreeMap<ContractId, ContractData>;
1717

1818
/// Collects preprocessor data from referenced contracts.
19-
pub fn collect_preprocessor_data(
19+
pub(crate) fn collect_preprocessor_data(
2020
sess: &Session,
2121
hir: &Hir<'_>,
22-
referenced_contracts: HashSet<u32>,
22+
referenced_contracts: &HashSet<ContractId>,
2323
) -> PreprocessorData {
2424
let mut data = PreprocessorData::default();
2525
for contract_id in referenced_contracts {
26-
let contract = Hir::contract(hir, ContractId::new(contract_id));
27-
let source = Hir::source(hir, contract.source);
26+
let contract = hir.contract(*contract_id);
27+
let source = hir.source(contract.source);
2828

2929
let FileName::Real(path) = &source.file.name else {
3030
continue;
3131
};
3232

3333
let contract_data =
34-
ContractData::new(hir, contract_id, contract, path, source, sess.source_map());
35-
data.insert(contract_id, contract_data);
34+
ContractData::new(hir, *contract_id, contract, path, source, sess.source_map());
35+
data.insert(*contract_id, contract_data);
3636
}
3737
data
3838
}
3939

4040
/// Creates helper libraries for contracts with a non-empty constructor.
4141
///
4242
/// See [`ContractData::build_helper`] for more details.
43-
pub fn create_deploy_helpers(data: &BTreeMap<u32, ContractData>) -> Sources {
43+
pub(crate) fn create_deploy_helpers(data: &BTreeMap<ContractId, ContractData>) -> Sources {
4444
let mut deploy_helpers = Sources::new();
4545
for (contract_id, contract) in data {
4646
if let Some(code) = contract.build_helper() {
47-
let path = format!("foundry-pp/DeployHelper{contract_id}.sol");
47+
let path = format!("foundry-pp/DeployHelper{}.sol", contract_id.get());
4848
deploy_helpers.insert(path.into(), Source::new(code));
4949
}
5050
}
@@ -62,9 +62,9 @@ pub struct ContractConstructorData {
6262

6363
/// Keeps data about a single contract definition.
6464
#[derive(Debug)]
65-
pub struct ContractData {
65+
pub(crate) struct ContractData {
6666
/// HIR Id of the contract.
67-
contract_id: u32,
67+
contract_id: ContractId,
6868
/// Path of the source file.
6969
path: PathBuf,
7070
/// Name of the contract
@@ -78,7 +78,7 @@ pub struct ContractData {
7878
impl ContractData {
7979
fn new(
8080
hir: &Hir<'_>,
81-
contract_id: u32,
81+
contract_id: ContractId,
8282
contract: &Contract<'_>,
8383
path: &Path,
8484
source: &solar_sema::hir::Source<'_>,
@@ -89,25 +89,21 @@ impl ContractData {
8989
// Process data for contracts with constructor and parameters.
9090
let constructor_data = contract
9191
.ctor
92-
.map(|ctor_id| Hir::function(hir, ctor_id))
92+
.map(|ctor_id| hir.function(ctor_id))
9393
.filter(|ctor| !ctor.parameters.is_empty())
9494
.map(|ctor| {
9595
let abi_encode_args = ctor
9696
.parameters
9797
.iter()
98-
.map(|param_id| {
99-
format!("args.{}", Hir::variable(hir, *param_id).name.unwrap().name)
100-
})
98+
.map(|param_id| format!("args.{}", hir.variable(*param_id).name.unwrap().name))
10199
.join(", ");
102100
let struct_fields = ctor
103101
.parameters
104102
.iter()
105103
.map(|param_id| {
106104
let src = source.file.src.as_str();
107-
let loc = SourceMapLocation::from_span(
108-
source_map,
109-
Hir::variable(hir, *param_id).span,
110-
);
105+
let loc =
106+
SourceMapLocation::from_span(source_map, hir.variable(*param_id).span);
111107
src[loc.start..loc.end].replace(" memory ", " ").replace(" calldata ", " ")
112108
})
113109
.join("; ");
@@ -170,6 +166,7 @@ impl ContractData {
170166
let Self { contract_id, path, name, constructor_data, artifact } = self;
171167

172168
let Some(constructor_details) = constructor_data else { return None };
169+
let contract_id = contract_id.get();
173170
let struct_fields = &constructor_details.struct_fields;
174171
let abi_encode_args = &constructor_details.abi_encode_args;
175172
let vm_interface_name = format!("VmContractHelper{contract_id}");

crates/compilers/src/preprocessor/deps.rs

Lines changed: 23 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -19,20 +19,20 @@ use std::{
1919
};
2020

2121
/// Holds data about referenced source contracts and bytecode dependencies.
22-
pub struct PreprocessorDependencies {
22+
pub(crate) struct PreprocessorDependencies {
2323
// Mapping contract id to preprocess -> contract bytecode dependencies.
24-
pub preprocessed_contracts: BTreeMap<u32, Vec<BytecodeDependency>>,
24+
pub preprocessed_contracts: BTreeMap<ContractId, Vec<BytecodeDependency>>,
2525
// Referenced contract ids.
26-
pub referenced_contracts: HashSet<u32>,
26+
pub referenced_contracts: HashSet<ContractId>,
2727
}
2828

2929
impl PreprocessorDependencies {
3030
pub fn new(sess: &Session, hir: &Hir<'_>, paths: &[PathBuf], src_dir: &PathBuf) -> Self {
3131
let mut preprocessed_contracts = BTreeMap::new();
3232
let mut referenced_contracts = HashSet::new();
33-
for contract_id in Hir::contract_ids(hir) {
34-
let contract = Hir::contract(hir, contract_id);
35-
let source = Hir::source(hir, contract.source);
33+
for contract_id in hir.contract_ids() {
34+
let contract = hir.contract(contract_id);
35+
let source = hir.source(contract.source);
3636

3737
let FileName::Real(path) = &source.file.name else {
3838
continue;
@@ -53,7 +53,7 @@ impl PreprocessorDependencies {
5353
deps_collector.walk_contract(contract);
5454
// Ignore empty test contracts declared in source files with other contracts.
5555
if !deps_collector.dependencies.is_empty() {
56-
preprocessed_contracts.insert(contract_id.get(), deps_collector.dependencies);
56+
preprocessed_contracts.insert(contract_id, deps_collector.dependencies);
5757
}
5858
// Record collected referenced contract ids.
5959
referenced_contracts.extend(deps_collector.referenced_contracts);
@@ -73,13 +73,13 @@ enum BytecodeDependencyKind {
7373

7474
/// Represents a single bytecode dependency.
7575
#[derive(Debug)]
76-
pub struct BytecodeDependency {
76+
pub(crate) struct BytecodeDependency {
7777
/// Dependency kind.
7878
kind: BytecodeDependencyKind,
7979
/// Source map location of this dependency.
8080
loc: SourceMapLocation,
8181
/// HIR id of referenced contract.
82-
referenced_contract: u32,
82+
referenced_contract: ContractId,
8383
}
8484

8585
/// Walks over contract HIR and collects [`BytecodeDependency`]s and referenced contracts.
@@ -95,7 +95,7 @@ struct BytecodeDependencyCollector<'hir> {
9595
/// Dependencies collected for current contract.
9696
dependencies: Vec<BytecodeDependency>,
9797
/// Unique HIR ids of contracts referenced from current contract.
98-
referenced_contracts: HashSet<u32>,
98+
referenced_contracts: HashSet<ContractId>,
9999
}
100100

101101
impl<'hir> BytecodeDependencyCollector<'hir> {
@@ -119,8 +119,8 @@ impl<'hir> BytecodeDependencyCollector<'hir> {
119119
/// Discards any reference that is not in project src directory (e.g. external
120120
/// libraries or mock contracts that extend source contracts).
121121
fn collect_dependency(&mut self, dependency: BytecodeDependency) {
122-
let contract = Hir::contract(self.hir, ContractId::new(dependency.referenced_contract));
123-
let source = Hir::source(self.hir, contract.source);
122+
let contract = self.hir.contract(dependency.referenced_contract);
123+
let source = self.hir.source(contract.source);
124124
let FileName::Real(path) = &source.file.name else {
125125
return;
126126
};
@@ -159,20 +159,20 @@ impl<'hir> Visit<'hir> for BytecodeDependencyCollector<'hir> {
159159
self.source_map,
160160
Span::new(expr.span.lo(), expr.span.hi()),
161161
),
162-
referenced_contract: contract_id.get(),
162+
referenced_contract: contract_id,
163163
});
164164
}
165165
}
166166
}
167167
ExprKind::Member(member_expr, ident) => {
168-
if ident.name.to_string() == "creationCode" {
169-
if let ExprKind::TypeCall(ty) = &member_expr.kind {
170-
if let TypeKind::Custom(contract_id) = &ty.kind {
168+
if let ExprKind::TypeCall(ty) = &member_expr.kind {
169+
if let TypeKind::Custom(contract_id) = &ty.kind {
170+
if ident.name.as_str() == "creationCode" {
171171
if let Some(contract_id) = contract_id.as_contract() {
172172
self.collect_dependency(BytecodeDependency {
173173
kind: BytecodeDependencyKind::CreationCode,
174174
loc: SourceMapLocation::from_span(self.source_map, expr.span),
175-
referenced_contract: contract_id.get(),
175+
referenced_contract: contract_id,
176176
});
177177
}
178178
}
@@ -187,23 +187,23 @@ impl<'hir> Visit<'hir> for BytecodeDependencyCollector<'hir> {
187187

188188
/// Goes over all test/script files and replaces bytecode dependencies with cheatcode
189189
/// invocations.
190-
pub fn remove_bytecode_dependencies(
190+
pub(crate) fn remove_bytecode_dependencies(
191191
hir: &Hir<'_>,
192192
deps: &PreprocessorDependencies,
193193
data: &PreprocessorData,
194194
) -> Updates {
195195
let mut updates = Updates::default();
196196
for (contract_id, deps) in &deps.preprocessed_contracts {
197-
let contract = Hir::contract(hir, ContractId::new(*contract_id));
198-
let source = Hir::source(hir, contract.source);
197+
let contract = hir.contract(*contract_id);
198+
let source = hir.source(contract.source);
199199
let FileName::Real(path) = &source.file.name else {
200200
continue;
201201
};
202202

203203
let updates = updates.entry(path.clone()).or_default();
204204
let mut used_helpers = BTreeSet::new();
205205

206-
let vm_interface_name = format!("VmContractHelper{contract_id}");
206+
let vm_interface_name = format!("VmContractHelper{}", contract_id.get());
207207
let vm = format!("{vm_interface_name}(0x7109709ECfa91a80626fF3989D68f67F5b1DD12D)");
208208

209209
for dep in deps {
@@ -239,7 +239,7 @@ pub fn remove_bytecode_dependencies(
239239
dep.loc.end,
240240
format!(
241241
"deployCode{id}(DeployHelper{id}.ConstructorArgs",
242-
id = dep.referenced_contract
242+
id = dep.referenced_contract.get()
243243
),
244244
));
245245
updates.insert((
@@ -252,6 +252,7 @@ pub fn remove_bytecode_dependencies(
252252
};
253253
}
254254
let helper_imports = used_helpers.into_iter().map(|id| {
255+
let id = id.get();
255256
format!(
256257
"import {{DeployHelper{id}, encodeArgs{id}, deployCode{id}}} from \"foundry-pp/DeployHelper{id}.sol\";",
257258
)

crates/compilers/src/preprocessor/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ impl Preprocessor<SolcCompiler> for TestOptimizerPreprocessor {
9898
&paths.paths_relative().sources,
9999
);
100100
// Collect data of source contracts referenced in tests and scripts.
101-
let data = collect_preprocessor_data(&sess, hir, deps.referenced_contracts.clone());
101+
let data = collect_preprocessor_data(&sess, hir, &deps.referenced_contracts);
102102

103103
// Extend existing sources with preprocessor deploy helper sources.
104104
sources.extend(create_deploy_helpers(&data));

0 commit comments

Comments
 (0)