Isolate determining the encoding type into its own function.

This commit is contained in:
chriseth 2018-07-18 15:46:23 +02:00 committed by Alex Beregszaszi
parent ef269bf40d
commit 7a8a243eef
5 changed files with 37 additions and 42 deletions

View File

@ -1749,22 +1749,8 @@ bool TypeChecker::visit(FunctionCall const& _functionCall)
m_errorReporter.typeError(arguments[i]->location(), "Invalid rational number (too large or division by zero).");
errored = true;
}
if (!errored)
{
TypePointer encodingType;
if (
argType->mobileType() &&
argType->mobileType()->interfaceType(false) &&
argType->mobileType()->interfaceType(false)->encodingType()
)
encodingType = argType->mobileType()->interfaceType(false)->encodingType();
// Structs are fine as long as ABIV2 is activated and we do not do packed encoding.
if (!encodingType || (
dynamic_cast<StructType const*>(encodingType.get()) &&
!(abiEncodeV2 && functionType->padArguments())
))
m_errorReporter.typeError(arguments[i]->location(), "This type cannot be encoded.");
}
if (!errored && !argType->fullEncodingType(false, abiEncodeV2, !functionType->padArguments()))
m_errorReporter.typeError(arguments[i]->location(), "This type cannot be encoded.");
}
else if (!type(*arguments[i])->isImplicitlyConvertibleTo(*parameterTypes[i]))
{

View File

@ -390,6 +390,27 @@ MemberList const& Type::members(ContractDefinition const* _currentScope) const
return *m_members[_currentScope];
}
TypePointer Type::fullEncodingType(bool _inLibraryCall, bool _encoderV2, bool _packed) const
{
TypePointer encodingType = mobileType();
if (encodingType)
encodingType = encodingType->interfaceType(_inLibraryCall);
if (encodingType)
encodingType = encodingType->encodingType();
if (auto structType = dynamic_cast<StructType const*>(encodingType.get()))
{
// Structs are fine in the following circumstances:
// - ABIv2 without packed encoding or,
// - storage struct for a library
if (!(
(_encoderV2 && !_packed) ||
(structType->location() == DataLocation::Storage && _inLibraryCall)
))
return TypePointer();
}
return encodingType;
}
MemberList::MemberMap Type::boundFunctions(Type const& _type, ContractDefinition const& _scope)
{
// Normalise data location of type.

View File

@ -280,6 +280,11 @@ public:
/// This for example returns address for contract types.
/// If there is no such type, returns an empty shared pointer.
virtual TypePointer encodingType() const { return TypePointer(); }
/// @returns the encoding type used under the given circumstances for the type of an expression
/// when used for e.g. abi.encode(...) or the empty pointer if the object
/// cannot be encoded.
/// This is different from encodingType since it takes implicit conversions into account.
TypePointer fullEncodingType(bool _inLibraryCall, bool _encoderV2, bool _packed) const;
/// @returns a (simpler) type that is used when decoding this type in calldata.
virtual TypePointer decodingType() const { return encodingType(); }
/// @returns a type that will be used outside of Solidity for e.g. function signatures.

View File

@ -471,13 +471,8 @@ string ABIFunctions::abiEncodingFunction(
bool _fromStack
)
{
solUnimplementedAssert(
_to.mobileType() &&
_to.mobileType()->interfaceType(_encodeAsLibraryTypes) &&
_to.mobileType()->interfaceType(_encodeAsLibraryTypes)->encodingType(),
"Encoding type \"" + _to.toString() + "\" not yet implemented."
);
TypePointer toInterface = _to.mobileType()->interfaceType(_encodeAsLibraryTypes)->encodingType();
TypePointer toInterface = _to.fullEncodingType(_encodeAsLibraryTypes, true, false);
solUnimplementedAssert(toInterface, "Encoding type \"" + _to.toString() + "\" not yet implemented.");
Type const& to = *toInterface;
if (_from.category() == Type::Category::StringLiteral)
@ -886,13 +881,8 @@ string ABIFunctions::abiEncodingFunctionStruct(
solAssert(member.type, "");
if (!member.type->canLiveOutsideStorage())
continue;
solUnimplementedAssert(
member.type->mobileType() &&
member.type->mobileType()->interfaceType(_encodeAsLibraryTypes) &&
member.type->mobileType()->interfaceType(_encodeAsLibraryTypes)->encodingType(),
"Encoding type \"" + member.type->toString() + "\" not yet implemented."
);
auto memberTypeTo = member.type->mobileType()->interfaceType(_encodeAsLibraryTypes)->encodingType();
TypePointer memberTypeTo = member.type->fullEncodingType(_encodeAsLibraryTypes, true, false);
solUnimplementedAssert(memberTypeTo, "Encoding type \"" + member.type->toString() + "\" not yet implemented.");
auto memberTypeFrom = _from.memberType(member.name);
solAssert(memberTypeFrom, "");
bool dynamicMember = memberTypeTo->isDynamicallyEncoded();

View File

@ -333,26 +333,19 @@ void CompilerUtils::encodeToMemory(
)
{
// stack: <v1> <v2> ... <vn> <mem>
bool const encoderV2 = m_context.experimentalFeatureActive(ExperimentalFeature::ABIEncoderV2);
TypePointers targetTypes = _targetTypes.empty() ? _givenTypes : _targetTypes;
solAssert(targetTypes.size() == _givenTypes.size(), "");
for (TypePointer& t: targetTypes)
{
solUnimplementedAssert(
t->mobileType() &&
t->mobileType()->interfaceType(_encodeAsLibraryTypes) &&
t->mobileType()->interfaceType(_encodeAsLibraryTypes)->encodingType(),
"Encoding type \"" + t->toString() + "\" not yet implemented."
);
t = t->mobileType()->interfaceType(_encodeAsLibraryTypes)->encodingType();
TypePointer tEncoding = t->fullEncodingType(_encodeAsLibraryTypes, encoderV2, !_padToWordBoundaries);
solUnimplementedAssert(tEncoding, "Encoding type \"" + t->toString() + "\" not yet implemented.");
t = std::move(tEncoding);
}
if (_givenTypes.empty())
return;
else if (
_padToWordBoundaries &&
!_copyDynamicDataInPlace &&
m_context.experimentalFeatureActive(ExperimentalFeature::ABIEncoderV2)
)
else if (_padToWordBoundaries && !_copyDynamicDataInPlace && encoderV2)
{
// Use the new Yul-based encoding function
auto stackHeightBefore = m_context.stackHeight();