From 994fdb7517dbde960a5cc65b525210817516159e Mon Sep 17 00:00:00 2001 From: chriseth Date: Mon, 11 Jan 2021 11:52:22 +0100 Subject: [PATCH 1/2] Determine encoding type earlier. --- libsolidity/codegen/ABIFunctions.cpp | 24 ++++++++++++++++++++---- libsolidity/codegen/ABIFunctions.h | 4 ++-- 2 files changed, 22 insertions(+), 6 deletions(-) diff --git a/libsolidity/codegen/ABIFunctions.cpp b/libsolidity/codegen/ABIFunctions.cpp index 858dbefb2..c74272434 100644 --- a/libsolidity/codegen/ABIFunctions.cpp +++ b/libsolidity/codegen/ABIFunctions.cpp @@ -37,7 +37,7 @@ using namespace solidity::frontend; string ABIFunctions::tupleEncoder( TypePointers const& _givenTypes, - TypePointers const& _targetTypes, + TypePointers _targetTypes, bool _encodeAsLibraryTypes, bool _reversed ) @@ -49,6 +49,13 @@ string ABIFunctions::tupleEncoder( options.padded = true; 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_"); for (auto const& t: _givenTypes) functionName += t->identifier() + "_"; @@ -111,7 +118,7 @@ string ABIFunctions::tupleEncoder( string ABIFunctions::tupleEncoderPacked( TypePointers const& _givenTypes, - TypePointers const& _targetTypes, + TypePointers _targetTypes, bool _reversed ) { @@ -121,6 +128,13 @@ string ABIFunctions::tupleEncoderPacked( options.padded = false; 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_"); for (auto const& t: _givenTypes) functionName += t->identifier() + "_"; @@ -392,7 +406,9 @@ string ABIFunctions::abiEncodeAndReturnUpdatedPosFunction( return createFunction(functionName, [&]() { string values = suffixedVariableNameList("value", 0, numVariablesForType(_givenType, _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"( function (, pos) -> updatedPos { updatedPos := (, pos) @@ -404,7 +420,7 @@ string ABIFunctions::abiEncodeAndReturnUpdatedPosFunction( .render(); else { - unsigned encodedSize = _targetType.calldataEncodedSize(_options.padded); + unsigned encodedSize = targetEncoding->calldataEncodedSize(_options.padded); solAssert(encodedSize != 0, "Invalid encoded size."); return Whiskers(R"( function (, pos) -> updatedPos { diff --git a/libsolidity/codegen/ABIFunctions.h b/libsolidity/codegen/ABIFunctions.h index b90494168..81ddcac00 100644 --- a/libsolidity/codegen/ABIFunctions.h +++ b/libsolidity/codegen/ABIFunctions.h @@ -79,7 +79,7 @@ public: /// If @reversed is true, the order of the variables after is reversed. std::string tupleEncoder( TypePointers const& _givenTypes, - TypePointers const& _targetTypes, + TypePointers _targetTypes, bool _encodeAsLibraryTypes = false, bool _reversed = false ); @@ -106,7 +106,7 @@ public: /// If @reversed is true, the order of the variables after is reversed. std::string tupleEncoderPacked( TypePointers const& _givenTypes, - TypePointers const& _targetTypes, + TypePointers _targetTypes, bool _reversed = false ); From 3804ad85f67aa06cac57a2a4602b6713fed353c8 Mon Sep 17 00:00:00 2001 From: chriseth Date: Mon, 11 Jan 2021 14:51:33 +0100 Subject: [PATCH 2/2] Enable tests. --- .../semanticTests/abiEncoderV1/struct/struct_storage_ptr.sol | 2 ++ .../libraries/external_call_with_storage_mapping_parameter.sol | 2 ++ .../semanticTests/types/mapping_contract_key_library.sol | 2 ++ .../semanticTests/types/mapping_enum_key_library_encoderV2.sol | 1 + 4 files changed, 7 insertions(+) diff --git a/test/libsolidity/semanticTests/abiEncoderV1/struct/struct_storage_ptr.sol b/test/libsolidity/semanticTests/abiEncoderV1/struct/struct_storage_ptr.sol index eb67bf38d..8ffd75153 100644 --- a/test/libsolidity/semanticTests/abiEncoderV1/struct/struct_storage_ptr.sol +++ b/test/libsolidity/semanticTests/abiEncoderV1/struct/struct_storage_ptr.sol @@ -21,6 +21,8 @@ contract C { return (r[2], s.x, a, b, c, d); } } +// ==== +// compileViaYul: also // ---- // library: L // f() -> 8, 7, 1, 2, 7, 12 diff --git a/test/libsolidity/semanticTests/libraries/external_call_with_storage_mapping_parameter.sol b/test/libsolidity/semanticTests/libraries/external_call_with_storage_mapping_parameter.sol index d009bed5c..821aebea7 100644 --- a/test/libsolidity/semanticTests/libraries/external_call_with_storage_mapping_parameter.sol +++ b/test/libsolidity/semanticTests/libraries/external_call_with_storage_mapping_parameter.sol @@ -12,6 +12,8 @@ contract C { return L.f(x); } } +// ==== +// compileViaYul: also // ---- // library: L // g(uint256): 4 -> 16 diff --git a/test/libsolidity/semanticTests/types/mapping_contract_key_library.sol b/test/libsolidity/semanticTests/types/mapping_contract_key_library.sol index bedb605c1..8b78209ae 100644 --- a/test/libsolidity/semanticTests/types/mapping_contract_key_library.sol +++ b/test/libsolidity/semanticTests/types/mapping_contract_key_library.sol @@ -16,6 +16,8 @@ contract test { L.set(table, k, v); } } +// ==== +// compileViaYul: also // ---- // library: L // get(address): 0 -> 0 diff --git a/test/libsolidity/semanticTests/types/mapping_enum_key_library_encoderV2.sol b/test/libsolidity/semanticTests/types/mapping_enum_key_library_encoderV2.sol index bb4fa6ad5..50f50dbb1 100644 --- a/test/libsolidity/semanticTests/types/mapping_enum_key_library_encoderV2.sol +++ b/test/libsolidity/semanticTests/types/mapping_enum_key_library_encoderV2.sol @@ -19,6 +19,7 @@ contract test { } } // ==== +// compileViaYul: also // EVMVersion: >=byzantium // ---- // library: L