Skip to content

Commit 0f68d0e

Browse files
committed
Improve code and fix bug raised in code review
1 parent d3ce515 commit 0f68d0e

3 files changed

Lines changed: 43 additions & 36 deletions

File tree

crates/solidity/outputs/cargo/crate/src/backend/abi/mod.rs

Lines changed: 29 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ impl ContractDefinitionStruct {
4848
// be able to obtain it here, but for that we need bi-directional tree
4949
// navigation
5050
pub fn compute_abi_with_file_id(&self, file_id: String) -> Option<ContractAbi> {
51-
let name = self.name().unparse();
51+
let name = self.ir_node.name.unparse();
5252
let functions = self.compute_abi_functions()?;
5353
let storage_layout = self.compute_storage_layout()?;
5454
Some(ContractAbi {
@@ -66,12 +66,12 @@ impl ContractDefinitionStruct {
6666
functions.push(constructor.compute_abi()?);
6767
}
6868
for function in &self.linearised_functions() {
69-
if function.is_public() {
69+
if function.is_externally_visible() {
7070
functions.push(function.compute_abi()?);
7171
}
7272
}
7373
for state_variable in &self.linearised_state_variables() {
74-
if state_variable.is_public() {
74+
if state_variable.is_externally_visible() {
7575
functions.push(state_variable.compute_abi()?);
7676
}
7777
}
@@ -106,7 +106,7 @@ impl ContractDefinitionStruct {
106106
ptr += remaining_bytes;
107107
}
108108

109-
let label = state_variable.name().unparse();
109+
let label = state_variable.ir_node.name.unparse();
110110
let slot = ptr.div(SemanticAnalysis::SLOT_SIZE);
111111
let offset = ptr % SemanticAnalysis::SLOT_SIZE;
112112
let r#type = self.semantic.type_canonical_name(variable_type_id);
@@ -126,15 +126,15 @@ impl ContractDefinitionStruct {
126126
}
127127

128128
impl FunctionDefinitionStruct {
129-
pub fn is_public(&self) -> bool {
129+
pub fn is_externally_visible(&self) -> bool {
130130
matches!(
131131
self.visibility(),
132132
FunctionVisibility::Public | FunctionVisibility::External
133133
)
134134
}
135135

136136
pub fn compute_abi(&self) -> Option<FunctionAbi> {
137-
if !self.is_public() {
137+
if !self.is_externally_visible() {
138138
return None;
139139
}
140140
let inputs = self.parameters().compute_abi()?;
@@ -146,7 +146,7 @@ impl FunctionDefinitionStruct {
146146

147147
Some(FunctionAbi {
148148
node_id: self.ir_node.node_id,
149-
name: self.name().as_ref().map(|name| name.unparse()),
149+
name: self.ir_node.name.as_ref().map(|name| name.unparse()),
150150
kind: self.kind(),
151151
inputs,
152152
outputs,
@@ -155,13 +155,12 @@ impl FunctionDefinitionStruct {
155155
}
156156

157157
pub fn compute_selector(&self) -> Option<u32> {
158-
if !self.is_public() {
158+
if !self.is_externally_visible() {
159159
return None;
160160
}
161-
let name = self.name()?;
161+
let name = self.ir_node.name.as_ref()?.unparse();
162162
let signature = format!(
163163
"{name}({parameters})",
164-
name = name.unparse(),
165164
parameters = self.parameters().compute_canonical_signature()?,
166165
);
167166

@@ -200,28 +199,33 @@ impl ParametersStruct {
200199
}
201200

202201
impl StateVariableDefinitionStruct {
203-
pub fn is_public(&self) -> bool {
202+
pub fn is_externally_visible(&self) -> bool {
204203
matches!(self.visibility(), StateVariableVisibility::Public)
205204
}
206205

207-
pub fn compute_abi(&self) -> Option<FunctionAbi> {
208-
if !self.is_public() {
209-
return None;
210-
}
211-
let Some(Definition::StateVariable(definition)) = self
206+
fn extract_getter_type_parameters_abi(
207+
&self,
208+
) -> Option<(Vec<FunctionParameter>, Vec<FunctionParameter>)> {
209+
let Definition::StateVariable(definition) = self
212210
.semantic
213211
.binder
214-
.find_definition_by_id(self.ir_node.node_id)
212+
.find_definition_by_id(self.ir_node.node_id)?
215213
else {
216-
return None;
214+
unreachable!("definition is not a state variable");
217215
};
218-
let (inputs, outputs) = self
219-
.semantic
220-
.extract_function_type_parameters_abi(definition.getter_type_id?)?;
216+
self.semantic
217+
.extract_function_type_parameters_abi(definition.getter_type_id?)
218+
}
219+
220+
pub fn compute_abi(&self) -> Option<FunctionAbi> {
221+
if !self.is_externally_visible() {
222+
return None;
223+
}
224+
let (inputs, outputs) = self.extract_getter_type_parameters_abi()?;
221225

222226
Some(FunctionAbi {
223227
node_id: self.ir_node.node_id,
224-
name: Some(self.name().unparse()),
228+
name: Some(self.ir_node.name.unparse()),
225229
kind: FunctionKind::Regular,
226230
inputs,
227231
outputs,
@@ -230,23 +234,14 @@ impl StateVariableDefinitionStruct {
230234
}
231235

232236
pub fn compute_selector(&self) -> Option<u32> {
233-
if !self.is_public() {
237+
if !self.is_externally_visible() {
234238
return None;
235239
}
236-
let Some(Definition::StateVariable(definition)) = self
237-
.semantic
238-
.binder
239-
.find_definition_by_id(self.ir_node.node_id)
240-
else {
241-
return None;
242-
};
243-
let (inputs, _) = self
244-
.semantic
245-
.extract_function_type_parameters_abi(definition.getter_type_id?)?;
240+
let (inputs, _) = self.extract_getter_type_parameters_abi()?;
246241

247242
let signature = format!(
248243
"{name}({parameters})",
249-
name = self.name().unparse(),
244+
name = self.ir_node.name.unparse(),
250245
parameters = inputs
251246
.into_iter()
252247
.map(|parameter| parameter.r#type)

crates/solidity/outputs/cargo/crate/src/backend/ir/ast/node_extensions/contracts.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,7 @@ impl ContractDefinitionStruct {
127127
.any(|linearised_function| linearised_function.overrides(function));
128128
!existing
129129
})
130+
// collect to avoid holding the read-borrow on `functions`
130131
.collect::<Vec<_>>();
131132

132133
functions.extend(contract_functions);
@@ -171,8 +172,13 @@ impl ContractMembersStruct {
171172

172173
impl FunctionDefinitionStruct {
173174
pub(crate) fn overrides(&self, other: &FunctionDefinition) -> bool {
174-
let name_matches = match (self.name(), other.name()) {
175-
(None, None) => true,
175+
let name_matches = match (&self.ir_node.name, &other.ir_node.name) {
176+
(None, None) => {
177+
// for unnamed functions, we check the kind because `receive`
178+
// and `fallback` may have the same parameters but they are
179+
// different functions
180+
self.ir_node.kind == other.ir_node.kind
181+
}
176182
(Some(name), Some(other_name)) => name.unparse() == other_name.unparse(),
177183
_ => false,
178184
};

crates/solidity/outputs/cargo/crate/src/backend/ir/ir2_flat_contracts/mod.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,3 +51,9 @@ impl From<&StateVariableVisibility> for binder::StateVariableVisibility {
5151
}
5252
}
5353
}
54+
55+
impl PartialEq for FunctionKind {
56+
fn eq(&self, other: &Self) -> bool {
57+
core::mem::discriminant(self) == core::mem::discriminant(other)
58+
}
59+
}

0 commit comments

Comments
 (0)