Skip to content

Commit

Permalink
Rework built-ins definitions
Browse files Browse the repository at this point in the history
  • Loading branch information
ggiraldez committed Oct 3, 2024
1 parent eed92dd commit fbec218
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -438,6 +438,7 @@ fn check_built_in_type(
let SpannedBuiltInType {
name: _,
fields,
functions,
enabled,
} = built_in;

Expand All @@ -446,14 +447,20 @@ fn check_built_in_type(
for field in fields {
check_built_in_field(analysis, field, &enablement);
}
for function in functions {
check_built_in_function(analysis, function, &enablement);
}
}

fn check_built_in_field(
analysis: &mut Analysis,
built_in: &SpannedBuiltInField,
enablement: &VersionSet,
) {
let SpannedBuiltInField { def: _, enabled } = built_in;
let SpannedBuiltInField {
definition: _,
enabled,
} = built_in;

let _ = update_enablement(analysis, enablement, enabled);
}
Expand Down
5 changes: 3 additions & 2 deletions crates/codegen/language/definition/src/model/manifest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -160,8 +160,8 @@ pub enum BuiltIn {
#[derive_spanned_type(Clone, Debug, ParseInputTokens, WriteOutputTokens)]
pub struct BuiltInFunction {
pub name: String,
pub return_type: Option<String>,
pub parameters: Vec<String>,
pub return_type: Option<String>,
pub enabled: Option<VersionSpecifier>,
}

Expand All @@ -170,12 +170,13 @@ pub struct BuiltInFunction {
pub struct BuiltInType {
pub name: String,
pub fields: Vec<BuiltInField>,
pub functions: Vec<BuiltInFunction>,
pub enabled: Option<VersionSpecifier>,
}

#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
#[derive_spanned_type(Clone, Debug, ParseInputTokens, WriteOutputTokens)]
pub struct BuiltInField {
pub def: String,
pub definition: String,
pub enabled: Option<VersionSpecifier>,
}
7 changes: 7 additions & 0 deletions crates/codegen/runtime/generator/src/bindings/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,9 +99,16 @@ fn filter_built_in_type(typ: &BuiltInType, version: &Version) -> Option<BuiltInT
.iter()
.filter_map(|field| filter_built_in_field(field, version))
.collect();
let functions = typ
.functions
.iter()
.filter_map(|function| filter_built_in_function(function, version))
.collect();

Some(BuiltInType {
name: typ.name.clone(),
fields,
functions,
enabled: typ.enabled.clone(),
})
} else {
Expand Down
26 changes: 16 additions & 10 deletions crates/solidity/inputs/language/src/definition.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6621,8 +6621,8 @@ codegen_language_macros::compile!(Language(
built_ins = [
BuiltInFunction(
name = "addmod",
return_type = "uint",
parameters = ["uint x", "uint y", "uint k"]
parameters = ["uint x", "uint y", "uint k"],
return_type = "uint"
),
BuiltInFunction(name = "assert", parameters = ["bool condition"]),
BuiltInFunction(
Expand All @@ -6639,18 +6639,24 @@ codegen_language_macros::compile!(Language(
BuiltInType(
name = "$BuiltIn$Address",
fields = [
BuiltInField(def = "uint256 balance"),
BuiltInField(def = "bytes code", enabled = From("0.8.0"))
]
BuiltInField(definition = "uint256 balance"),
BuiltInField(definition = "bytes code", enabled = From("0.8.0"))
],
functions = [BuiltInFunction(
name = "send",
parameters = ["uint256 amount"],
return_type = "bool"
)]
),
BuiltInType(
name = "$BuiltIn$TxType",
fields = [
BuiltInField(def = "uint gasprice"),
BuiltInField(def = "address payable origin")
]
BuiltInField(definition = "uint gasprice"),
BuiltInField(definition = "address payable origin")
],
functions = []
),
BuiltInVariable(def = "uint now"),
BuiltInVariable(def = "$BuiltIn$TxType tx")
BuiltInVariable(definition = "uint now"),
BuiltInVariable(definition = "$BuiltIn$TxType tx")
]
));
26 changes: 23 additions & 3 deletions crates/solidity/inputs/language/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,33 @@ pub fn render_built_ins(built_ins: &[BuiltIn]) -> String {
let fields = item
.fields
.iter()
.map(|field| format!(" {field_def};", field_def = field.def))
.map(|field| format!(" {field_def};", field_def = field.definition))
.collect::<Vec<_>>()
.join("\n");
lines.push(format!("struct {name} {{\n{fields}\n}}", name = item.name));
let functions = item
.functions
.iter()
.map(|function| {
format!(
" function({parameters}){return_type} {name};",
parameters = function.parameters.join(", "),
return_type = function
.return_type
.as_deref()
.map(|return_type| format!(" returns ({return_type})"))
.unwrap_or_default(),
name = function.name
)
})
.collect::<Vec<_>>()
.join("\n");
lines.push(format!(
"struct {name} {{\n{fields}\n{functions}\n}}",
name = item.name
));
}
BuiltIn::BuiltInVariable { item } => {
lines.push(format!("{var_def};", var_def = item.def));
lines.push(format!("{var_def};", var_def = item.definition));
}
}
}
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit fbec218

Please sign in to comment.