Merge pull request #6402 from ethereum/moveVarList

Move suffixedVariableNameList to utils.
This commit is contained in:
chriseth 2019-03-28 12:51:14 +01:00 committed by GitHub
commit 2bbc41ad64
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 28 additions and 29 deletions

View File

@ -82,7 +82,7 @@ string ABIFunctions::tupleEncoder(
<abiEncode>(<values> add(headStart, <pos>)) <abiEncode>(<values> add(headStart, <pos>))
)") )")
); );
string values = suffixedVariableNameList("value", stackPos, stackPos + sizeOnStack); string values = m_utils.suffixedVariableNameList("value", stackPos, stackPos + sizeOnStack);
elementTempl("values", values.empty() ? "" : values + ", "); elementTempl("values", values.empty() ? "" : values + ", ");
elementTempl("pos", to_string(headPos)); elementTempl("pos", to_string(headPos));
elementTempl("abiEncode", abiEncodingFunction(*_givenTypes[i], *_targetTypes[i], options)); elementTempl("abiEncode", abiEncodingFunction(*_givenTypes[i], *_targetTypes[i], options));
@ -91,7 +91,7 @@ string ABIFunctions::tupleEncoder(
stackPos += sizeOnStack; stackPos += sizeOnStack;
} }
solAssert(headPos == headSize_, ""); solAssert(headPos == headSize_, "");
string valueParams = suffixedVariableNameList("value", stackPos, 0); string valueParams = m_utils.suffixedVariableNameList("value", stackPos, 0);
templ("valueParams", valueParams.empty() ? "" : ", " + valueParams); templ("valueParams", valueParams.empty() ? "" : ", " + valueParams);
templ("encodeElements", encodeElements); templ("encodeElements", encodeElements);
@ -147,7 +147,7 @@ string ABIFunctions::tupleEncoderPacked(
pos := add(pos, <calldataEncodedSize>) pos := add(pos, <calldataEncodedSize>)
)") )")
); );
string values = suffixedVariableNameList("value", stackPos, stackPos + sizeOnStack); string values = m_utils.suffixedVariableNameList("value", stackPos, stackPos + sizeOnStack);
elementTempl("values", values.empty() ? "" : values + ", "); elementTempl("values", values.empty() ? "" : values + ", ");
if (!dynamic) if (!dynamic)
elementTempl("calldataEncodedSize", to_string(_targetTypes[i]->calldataEncodedSize(false))); elementTempl("calldataEncodedSize", to_string(_targetTypes[i]->calldataEncodedSize(false)));
@ -155,7 +155,7 @@ string ABIFunctions::tupleEncoderPacked(
encodeElements += elementTempl.render(); encodeElements += elementTempl.render();
stackPos += sizeOnStack; stackPos += sizeOnStack;
} }
string valueParams = suffixedVariableNameList("value", stackPos, 0); string valueParams = m_utils.suffixedVariableNameList("value", stackPos, 0);
templ("valueParams", valueParams.empty() ? "" : ", " + valueParams); templ("valueParams", valueParams.empty() ? "" : ", " + valueParams);
templ("encodeElements", encodeElements); templ("encodeElements", encodeElements);
@ -659,7 +659,7 @@ string ABIFunctions::abiEncodeAndReturnUpdatedPosFunction(
_targetType.identifier() + _targetType.identifier() +
_options.toFunctionNameSuffix(); _options.toFunctionNameSuffix();
return createFunction(functionName, [&]() { 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); string encoder = abiEncodingFunction(_givenType, _targetType, _options);
if (_targetType.isDynamicallyEncoded()) if (_targetType.isDynamicallyEncoded())
return Whiskers(R"( return Whiskers(R"(
@ -1679,24 +1679,6 @@ size_t ABIFunctions::headSize(TypePointers const& _targetTypes)
return headSize; 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) size_t ABIFunctions::numVariablesForType(Type const& _type, EncodingOptions const& _options)
{ {
if (_type.category() == Type::Category::Function && !_options.encodeFunctionFromStack) if (_type.category() == Type::Category::Function && !_options.encodeFunctionFromStack)

View File

@ -275,12 +275,6 @@ private:
/// @returns the size of the static part of the encoding of the given types. /// @returns the size of the static part of the encoding of the given types.
static size_t headSize(TypePointers const& _targetTypes); 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. /// @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 /// 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 /// external function types (if we are encoding from stack, i.e. _options.encodeFunctionFromStack

View File

@ -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;
}

View File

@ -92,6 +92,12 @@ public:
/// Return value: pointer /// Return value: pointer
std::string allocationFunction(); 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: private:
langutil::EVMVersion m_evmVersion; langutil::EVMVersion m_evmVersion;
std::shared_ptr<MultiUseYulFunctionCollector> m_functionCollector; std::shared_ptr<MultiUseYulFunctionCollector> m_functionCollector;