Skip to content

Commit 6b888b5

Browse files
committed
Refactor conversion function.
1 parent 26fb231 commit 6b888b5

File tree

3 files changed

+37
-43
lines changed

3 files changed

+37
-43
lines changed

libsolidity/codegen/YulUtilFunctions.cpp

Lines changed: 18 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -3258,68 +3258,52 @@ string YulUtilFunctions::conversionFunction(Type const& _from, Type const& _to)
32583258
switch (fromCategory)
32593259
{
32603260
case Type::Category::Address:
3261+
case Type::Category::Contract:
32613262
body =
32623263
Whiskers("converted := <convert>(value)")
32633264
("convert", conversionFunction(IntegerType(160), _to))
32643265
.render();
32653266
break;
32663267
case Type::Category::Integer:
32673268
case Type::Category::RationalNumber:
3268-
case Type::Category::Contract:
32693269
{
3270+
solAssert(_from.mobileType(), "");
32703271
if (RationalNumberType const* rational = dynamic_cast<RationalNumberType const*>(&_from))
3271-
solUnimplementedAssert(!rational->isFractional(), "Not yet implemented - FixedPointType.");
3272+
if (rational->isFractional())
3273+
solAssert(toCategory == Type::Category::FixedPoint, "");
3274+
32723275
if (toCategory == Type::Category::FixedBytes)
32733276
{
3274-
solAssert(
3275-
fromCategory == Type::Category::Integer || fromCategory == Type::Category::RationalNumber,
3276-
"Invalid conversion to FixedBytesType requested."
3277-
);
32783277
FixedBytesType const& toBytesType = dynamic_cast<FixedBytesType const&>(_to);
32793278
body =
32803279
Whiskers("converted := <shiftLeft>(<clean>(value))")
3281-
("shiftLeft", shiftLeftFunction(256 - toBytesType.numBytes() * 8))
3282-
("clean", cleanupFunction(_from))
3283-
.render();
3280+
("shiftLeft", shiftLeftFunction(256 - toBytesType.numBytes() * 8))
3281+
("clean", cleanupFunction(_from))
3282+
.render();
32843283
}
32853284
else if (toCategory == Type::Category::Enum)
3286-
{
3287-
solAssert(_from.mobileType(), "");
32883285
body =
32893286
Whiskers("converted := <cleanEnum>(<cleanInt>(value))")
32903287
("cleanEnum", cleanupFunction(_to))
3291-
// "mobileType()" returns integer type for rational
3292-
("cleanInt", cleanupFunction(*_from.mobileType()))
3288+
("cleanInt", cleanupFunction(_from))
32933289
.render();
3294-
}
32953290
else if (toCategory == Type::Category::FixedPoint)
32963291
solUnimplemented("Not yet implemented - FixedPointType.");
3297-
else if (toCategory == Type::Category::Address)
3292+
else if (toCategory == Type::Category::Address || toCategory == Type::Category::Contract)
32983293
body =
32993294
Whiskers("converted := <convert>(value)")
3300-
("convert", conversionFunction(_from, IntegerType(160)))
3301-
.render();
3302-
else
3295+
("convert", conversionFunction(_from, IntegerType(160)))
3296+
.render();
3297+
else if (toCategory == Type::Category::Integer)
33033298
{
3304-
solAssert(
3305-
toCategory == Type::Category::Integer ||
3306-
toCategory == Type::Category::Contract,
3307-
"");
3308-
IntegerType const addressType(160);
3309-
IntegerType const& to =
3310-
toCategory == Type::Category::Integer ?
3311-
dynamic_cast<IntegerType const&>(_to) :
3312-
addressType;
3299+
IntegerType const& to = dynamic_cast<IntegerType const&>(_to);
33133300

33143301
// Clean according to the "to" type, except if this is
33153302
// a widening conversion.
33163303
IntegerType const* cleanupType = &to;
3317-
if (fromCategory != Type::Category::RationalNumber)
3304+
if (fromCategory == Type::Category::Integer)
33183305
{
3319-
IntegerType const& from =
3320-
fromCategory == Type::Category::Integer ?
3321-
dynamic_cast<IntegerType const&>(_from) :
3322-
addressType;
3306+
IntegerType const& from = dynamic_cast<IntegerType const&>(_from);
33233307
if (to.numBits() > from.numBits())
33243308
cleanupType = &from;
33253309
}
@@ -3328,6 +3312,8 @@ string YulUtilFunctions::conversionFunction(Type const& _from, Type const& _to)
33283312
("cleanInt", cleanupFunction(*cleanupType))
33293313
.render();
33303314
}
3315+
else
3316+
solAssert(false, "");
33313317
break;
33323318
}
33333319
case Type::Category::Bool:

test/cmdlineTests/yul_source_locations/output.json

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -275,11 +275,7 @@ object \"C_54\" {
275275
}
276276

277277
function convert_t_contract$_C_$54_to_t_address(value) -> converted {
278-
converted := convert_t_contract$_C_$54_to_t_uint160(value)
279-
}
280-
281-
function convert_t_contract$_C_$54_to_t_uint160(value) -> converted {
282-
converted := cleanup_t_uint160(value)
278+
converted := convert_t_uint160_to_t_address(value)
283279
}
284280

285281
function convert_t_int256_to_t_int256(value) -> converted {
@@ -290,6 +286,14 @@ object \"C_54\" {
290286
converted := cleanup_t_int256(value)
291287
}
292288

289+
function convert_t_uint160_to_t_address(value) -> converted {
290+
converted := convert_t_uint160_to_t_uint160(value)
291+
}
292+
293+
function convert_t_uint160_to_t_uint160(value) -> converted {
294+
converted := cleanup_t_uint160(value)
295+
}
296+
293297
function extract_from_storage_value_dynamict_int256(slot_value, offset) -> value {
294298
value := cleanup_from_storage_t_int256(shift_right_unsigned_dynamic(mul(offset, 8), slot_value))
295299
}
@@ -875,11 +879,7 @@ object \"D_72\" {
875879
}
876880

877881
function convert_t_contract$_C_$54_to_t_address(value) -> converted {
878-
converted := convert_t_contract$_C_$54_to_t_uint160(value)
879-
}
880-
881-
function convert_t_contract$_C_$54_to_t_uint160(value) -> converted {
882-
converted := cleanup_t_uint160(value)
882+
converted := convert_t_uint160_to_t_address(value)
883883
}
884884

885885
function convert_t_int256_to_t_int256(value) -> converted {
@@ -890,6 +890,14 @@ object \"D_72\" {
890890
converted := cleanup_t_int256(value)
891891
}
892892

893+
function convert_t_uint160_to_t_address(value) -> converted {
894+
converted := convert_t_uint160_to_t_uint160(value)
895+
}
896+
897+
function convert_t_uint160_to_t_uint160(value) -> converted {
898+
converted := cleanup_t_uint160(value)
899+
}
900+
893901
function extract_from_storage_value_dynamict_int256(slot_value, offset) -> value {
894902
value := cleanup_from_storage_t_int256(shift_right_unsigned_dynamic(mul(offset, 8), slot_value))
895903
}

test/libsolidity/semanticTests/various/external_types_in_calls.sol

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,5 +27,5 @@ contract C {
2727
// compileViaYul: also
2828
// ----
2929
// test() -> 9, 7
30-
// gas legacy: 121594
30+
// gas legacy: 125064
3131
// t2() -> 9

0 commit comments

Comments
 (0)