More flexible array literals.

This commit is contained in:
chriseth
2021-09-15 18:11:21 +02:00
parent 8735d3fb6c
commit 83e7f5e83e
4 changed files with 66 additions and 8 deletions
+34 -3
View File
@@ -3256,6 +3256,11 @@ string YulUtilFunctions::conversionFunction(Type const& _from, Type const& _to)
solAssert(_to.category() == Type::Category::Array, "");
return arrayConversionFunction(fromArrayType, dynamic_cast<ArrayType const&>(_to));
}
else if (auto tupleType = dynamic_cast<TupleType const*>(&_from))
{
if (tupleType->isArrayLiteral())
return arrayLiteralConversionFunction(*tupleType, dynamic_cast<ArrayType const&>(_to));
}
if (_from.sizeOnStack() != 1 || _to.sizeOnStack() != 1)
return conversionFunctionSpecial(_from, _to);
@@ -3418,10 +3423,8 @@ string YulUtilFunctions::conversionFunction(Type const& _from, Type const& _to)
break;
}
case Type::Category::Tuple:
{
solUnimplementedAssert(false, "Tuple conversion not implemented.");
solAssert(false, "");
break;
}
case Type::Category::TypeType:
{
TypeType const& typeType = dynamic_cast<decltype(typeType)>(_from);
@@ -3704,6 +3707,34 @@ string YulUtilFunctions::arrayConversionFunction(ArrayType const& _from, ArrayTy
});
}
string YulUtilFunctions::arrayLiteralConversionFunction(TupleType const& _from, ArrayType const& _to)
{
solUnimplementedAssert(_to.dataStoredIn(DataLocation::Memory, "");
string functionName =
"convert_arrayliteral_" +
_from.identifier() +
"_to_" +
_to.identifier();
return m_functionCollector.createFunction(functionName, [&](vector<string>& _args, vector<string>& _ret) {
_args = _from.stackItems();
_ret = "converted";
Whiskers templ(R"(
// Copy the array to a free position in memory
converted := <allocate>(<length>)
<?dynamic>
mstore(converted, <length>)
</dynamic>
<#items>
mstore(<pos>, <var>)
</items>
)");
return templ.render();
});
}
string YulUtilFunctions::cleanupFunction(Type const& _type)
{
if (auto userDefinedValueType = dynamic_cast<UserDefinedValueType const*>(&_type))
+2
View File
@@ -530,6 +530,8 @@ private:
/// Special case of conversion functions - handles all array conversions.
std::string arrayConversionFunction(ArrayType const& _from, ArrayType const& _to);
std::string arrayLiteralConversionFunction(TupleType const& _from, ArrayType const& _to);
/// Special case of conversionFunction - handles everything that does not
/// use exactly one variable to hold the value.
std::string conversionFunctionSpecial(Type const& _from, Type const& _to);