Determine encoding type earlier.

This commit is contained in:
chriseth 2021-01-11 11:52:22 +01:00
parent f03245d473
commit 994fdb7517
2 changed files with 22 additions and 6 deletions

View File

@ -37,7 +37,7 @@ using namespace solidity::frontend;
string ABIFunctions::tupleEncoder( string ABIFunctions::tupleEncoder(
TypePointers const& _givenTypes, TypePointers const& _givenTypes,
TypePointers const& _targetTypes, TypePointers _targetTypes,
bool _encodeAsLibraryTypes, bool _encodeAsLibraryTypes,
bool _reversed bool _reversed
) )
@ -49,6 +49,13 @@ string ABIFunctions::tupleEncoder(
options.padded = true; options.padded = true;
options.dynamicInplace = false; options.dynamicInplace = false;
for (Type const*& t: _targetTypes)
{
solAssert(t, "");
t = t->fullEncodingType(options.encodeAsLibraryTypes, true, !options.padded);
solAssert(t, "");
}
string functionName = string("abi_encode_tuple_"); string functionName = string("abi_encode_tuple_");
for (auto const& t: _givenTypes) for (auto const& t: _givenTypes)
functionName += t->identifier() + "_"; functionName += t->identifier() + "_";
@ -111,7 +118,7 @@ string ABIFunctions::tupleEncoder(
string ABIFunctions::tupleEncoderPacked( string ABIFunctions::tupleEncoderPacked(
TypePointers const& _givenTypes, TypePointers const& _givenTypes,
TypePointers const& _targetTypes, TypePointers _targetTypes,
bool _reversed bool _reversed
) )
{ {
@ -121,6 +128,13 @@ string ABIFunctions::tupleEncoderPacked(
options.padded = false; options.padded = false;
options.dynamicInplace = true; options.dynamicInplace = true;
for (Type const*& t: _targetTypes)
{
solAssert(t, "");
t = t->fullEncodingType(options.encodeAsLibraryTypes, true, !options.padded);
solAssert(t, "");
}
string functionName = string("abi_encode_tuple_packed_"); string functionName = string("abi_encode_tuple_packed_");
for (auto const& t: _givenTypes) for (auto const& t: _givenTypes)
functionName += t->identifier() + "_"; functionName += t->identifier() + "_";
@ -392,7 +406,9 @@ string ABIFunctions::abiEncodeAndReturnUpdatedPosFunction(
return createFunction(functionName, [&]() { return createFunction(functionName, [&]() {
string values = suffixedVariableNameList("value", 0, numVariablesForType(_givenType, _options)); string values = suffixedVariableNameList("value", 0, numVariablesForType(_givenType, _options));
string encoder = abiEncodingFunction(_givenType, _targetType, _options); string encoder = abiEncodingFunction(_givenType, _targetType, _options);
if (_targetType.isDynamicallyEncoded()) Type const* targetEncoding = _targetType.fullEncodingType(_options.encodeAsLibraryTypes, true, false);
solAssert(targetEncoding, "");
if (targetEncoding->isDynamicallyEncoded())
return Whiskers(R"( return Whiskers(R"(
function <functionName>(<values>, pos) -> updatedPos { function <functionName>(<values>, pos) -> updatedPos {
updatedPos := <encode>(<values>, pos) updatedPos := <encode>(<values>, pos)
@ -404,7 +420,7 @@ string ABIFunctions::abiEncodeAndReturnUpdatedPosFunction(
.render(); .render();
else else
{ {
unsigned encodedSize = _targetType.calldataEncodedSize(_options.padded); unsigned encodedSize = targetEncoding->calldataEncodedSize(_options.padded);
solAssert(encodedSize != 0, "Invalid encoded size."); solAssert(encodedSize != 0, "Invalid encoded size.");
return Whiskers(R"( return Whiskers(R"(
function <functionName>(<values>, pos) -> updatedPos { function <functionName>(<values>, pos) -> updatedPos {

View File

@ -79,7 +79,7 @@ public:
/// If @reversed is true, the order of the variables after <headStart> is reversed. /// If @reversed is true, the order of the variables after <headStart> is reversed.
std::string tupleEncoder( std::string tupleEncoder(
TypePointers const& _givenTypes, TypePointers const& _givenTypes,
TypePointers const& _targetTypes, TypePointers _targetTypes,
bool _encodeAsLibraryTypes = false, bool _encodeAsLibraryTypes = false,
bool _reversed = false bool _reversed = false
); );
@ -106,7 +106,7 @@ public:
/// If @reversed is true, the order of the variables after <headStart> is reversed. /// If @reversed is true, the order of the variables after <headStart> is reversed.
std::string tupleEncoderPacked( std::string tupleEncoderPacked(
TypePointers const& _givenTypes, TypePointers const& _givenTypes,
TypePointers const& _targetTypes, TypePointers _targetTypes,
bool _reversed = false bool _reversed = false
); );