diff --git a/libsolidity/codegen/ABIFunctions.cpp b/libsolidity/codegen/ABIFunctions.cpp index 47475eced..7288b143c 100644 --- a/libsolidity/codegen/ABIFunctions.cpp +++ b/libsolidity/codegen/ABIFunctions.cpp @@ -82,7 +82,7 @@ string ABIFunctions::tupleEncoder( ( add(headStart, )) )") ); - string values = suffixedVariableNameList("value", stackPos, stackPos + sizeOnStack); + string values = m_utils.suffixedVariableNameList("value", stackPos, stackPos + sizeOnStack); elementTempl("values", values.empty() ? "" : values + ", "); elementTempl("pos", to_string(headPos)); elementTempl("abiEncode", abiEncodingFunction(*_givenTypes[i], *_targetTypes[i], options)); @@ -91,7 +91,7 @@ string ABIFunctions::tupleEncoder( stackPos += sizeOnStack; } solAssert(headPos == headSize_, ""); - string valueParams = suffixedVariableNameList("value", stackPos, 0); + string valueParams = m_utils.suffixedVariableNameList("value", stackPos, 0); templ("valueParams", valueParams.empty() ? "" : ", " + valueParams); templ("encodeElements", encodeElements); @@ -147,7 +147,7 @@ string ABIFunctions::tupleEncoderPacked( pos := add(pos, ) )") ); - string values = suffixedVariableNameList("value", stackPos, stackPos + sizeOnStack); + string values = m_utils.suffixedVariableNameList("value", stackPos, stackPos + sizeOnStack); elementTempl("values", values.empty() ? "" : values + ", "); if (!dynamic) elementTempl("calldataEncodedSize", to_string(_targetTypes[i]->calldataEncodedSize(false))); @@ -155,7 +155,7 @@ string ABIFunctions::tupleEncoderPacked( encodeElements += elementTempl.render(); stackPos += sizeOnStack; } - string valueParams = suffixedVariableNameList("value", stackPos, 0); + string valueParams = m_utils.suffixedVariableNameList("value", stackPos, 0); templ("valueParams", valueParams.empty() ? "" : ", " + valueParams); templ("encodeElements", encodeElements); @@ -659,7 +659,7 @@ string ABIFunctions::abiEncodeAndReturnUpdatedPosFunction( _targetType.identifier() + _options.toFunctionNameSuffix(); return createFunction(functionName, [&]() { - string values = suffixedVariableNameList("value", 0, numVariablesForType(_givenType, _options)); + string values = m_utils.suffixedVariableNameList("value", 0, numVariablesForType(_givenType, _options)); string encoder = abiEncodingFunction(_givenType, _targetType, _options); if (_targetType.isDynamicallyEncoded()) return Whiskers(R"( @@ -1679,24 +1679,6 @@ size_t ABIFunctions::headSize(TypePointers const& _targetTypes) return headSize; } -string ABIFunctions::suffixedVariableNameList(string const& _baseName, size_t _startSuffix, size_t _endSuffix) -{ - string result; - if (_startSuffix < _endSuffix) - { - result = _baseName + to_string(_startSuffix++); - while (_startSuffix < _endSuffix) - result += ", " + _baseName + to_string(_startSuffix++); - } - else if (_endSuffix < _startSuffix) - { - result = _baseName + to_string(_endSuffix++); - while (_endSuffix < _startSuffix) - result = _baseName + to_string(_endSuffix++) + ", " + result; - } - return result; -} - size_t ABIFunctions::numVariablesForType(Type const& _type, EncodingOptions const& _options) { if (_type.category() == Type::Category::Function && !_options.encodeFunctionFromStack) diff --git a/libsolidity/codegen/ABIFunctions.h b/libsolidity/codegen/ABIFunctions.h index 5c5b69637..e700ae63a 100644 --- a/libsolidity/codegen/ABIFunctions.h +++ b/libsolidity/codegen/ABIFunctions.h @@ -275,12 +275,6 @@ private: /// @returns the size of the static part of the encoding of the given types. static size_t headSize(TypePointers const& _targetTypes); - /// @returns a string containing a comma-separated list of variable names consisting of @a _baseName suffixed - /// with increasing integers in the range [@a _startSuffix, @a _endSuffix), if @a _startSuffix < @a _endSuffix, - /// and with decreasing integers in the range [@a _endSuffix, @a _startSuffix), if @a _endSuffix < @a _startSuffix. - /// If @a _startSuffix == @a _endSuffix, the empty string is returned. - static std::string suffixedVariableNameList(std::string const& _baseName, size_t _startSuffix, size_t _endSuffix); - /// @returns the number of variables needed to store a type. /// This is one for almost all types. The exception being dynamically sized calldata arrays or /// external function types (if we are encoding from stack, i.e. _options.encodeFunctionFromStack diff --git a/libsolidity/codegen/YulUtilFunctions.cpp b/libsolidity/codegen/YulUtilFunctions.cpp index 489980f96..25a5fce9c 100644 --- a/libsolidity/codegen/YulUtilFunctions.cpp +++ b/libsolidity/codegen/YulUtilFunctions.cpp @@ -436,3 +436,20 @@ string YulUtilFunctions::allocationFunction() }); } +string YulUtilFunctions::suffixedVariableNameList(string const& _baseName, size_t _startSuffix, size_t _endSuffix) +{ + string result; + if (_startSuffix < _endSuffix) + { + result = _baseName + to_string(_startSuffix++); + while (_startSuffix < _endSuffix) + result += ", " + _baseName + to_string(_startSuffix++); + } + else if (_endSuffix < _startSuffix) + { + result = _baseName + to_string(_endSuffix++); + while (_endSuffix < _startSuffix) + result = _baseName + to_string(_endSuffix++) + ", " + result; + } + return result; +} diff --git a/libsolidity/codegen/YulUtilFunctions.h b/libsolidity/codegen/YulUtilFunctions.h index 92eb5532d..da5a17cf2 100644 --- a/libsolidity/codegen/YulUtilFunctions.h +++ b/libsolidity/codegen/YulUtilFunctions.h @@ -92,6 +92,12 @@ public: /// Return value: pointer std::string allocationFunction(); + /// @returns a string containing a comma-separated list of variable names consisting of @a _baseName suffixed + /// with increasing integers in the range [@a _startSuffix, @a _endSuffix), if @a _startSuffix < @a _endSuffix, + /// and with decreasing integers in the range [@a _endSuffix, @a _startSuffix), if @a _endSuffix < @a _startSuffix. + /// If @a _startSuffix == @a _endSuffix, the empty string is returned. + static std::string suffixedVariableNameList(std::string const& _baseName, size_t _startSuffix, size_t _endSuffix); + private: langutil::EVMVersion m_evmVersion; std::shared_ptr m_functionCollector;