[libsolidity] TypeProvider: adds explicit uint256() accessor and removes default params in integerType(...).

This commit is contained in:
Christian Parpart 2019-04-15 18:10:43 +02:00 committed by chriseth
parent 59d4f54729
commit 58a45f2cb6
13 changed files with 57 additions and 54 deletions

View File

@ -57,7 +57,7 @@ inline vector<shared_ptr<MagicVariableDeclaration const>> constructMagicVariable
magicVarDecl("log4", TypeProvider::functionType(strings{"bytes32", "bytes32", "bytes32", "bytes32", "bytes32"}, strings{}, FunctionType::Kind::Log4)),
magicVarDecl("msg", TypeProvider::magicType(MagicType::Kind::Message)),
magicVarDecl("mulmod", TypeProvider::functionType(strings{"uint256", "uint256", "uint256"}, strings{"uint256"}, FunctionType::Kind::MulMod, false, StateMutability::Pure)),
magicVarDecl("now", TypeProvider::integerType(256)),
magicVarDecl("now", TypeProvider::uint256()),
magicVarDecl("require", TypeProvider::functionType(strings{"bool"}, strings{}, FunctionType::Kind::Require, false, StateMutability::Pure)),
magicVarDecl("require", TypeProvider::functionType(strings{"bool", "string memory"}, strings{}, FunctionType::Kind::Require, false, StateMutability::Pure)),
magicVarDecl("revert", TypeProvider::functionType(strings(), strings(), FunctionType::Kind::Revert, false, StateMutability::Pure)),

View File

@ -1999,7 +1999,7 @@ void TypeChecker::endVisit(NewExpression const& _newExpression)
);
type = ReferenceType::copyForLocationIfReference(DataLocation::Memory, type);
_newExpression.annotation().type = TypeProvider::functionType(
TypePointers{TypeProvider::integerType(256)},
TypePointers{TypeProvider::uint256()},
TypePointers{type},
strings(1, ""),
strings(1, ""),
@ -2195,7 +2195,7 @@ bool TypeChecker::visit(IndexAccess const& _access)
}
else
{
expectType(*index, *TypeProvider::integerType());
expectType(*index, *TypeProvider::uint256());
if (!m_errorReporter.hasErrors())
if (auto numberType = dynamic_cast<RationalNumberType const*>(type(*index)))
{
@ -2229,7 +2229,7 @@ bool TypeChecker::visit(IndexAccess const& _access)
else
{
u256 length = 1;
if (expectType(*index, *TypeProvider::integerType()))
if (expectType(*index, *TypeProvider::uint256()))
{
if (auto indexValue = dynamic_cast<RationalNumberType const*>(type(*index)))
length = indexValue->literalValue(nullptr);
@ -2254,7 +2254,7 @@ bool TypeChecker::visit(IndexAccess const& _access)
m_errorReporter.typeError(_access.location(), "Index expression cannot be omitted.");
else
{
if (!expectType(*index, *TypeProvider::integerType()))
if (!expectType(*index, *TypeProvider::uint256()))
m_errorReporter.fatalTypeError(_access.location(), "Index expression cannot be represented as an unsigned integer.");
if (auto integerType = dynamic_cast<RationalNumberType const*>(type(*index)))
if (bytesType.numBytes() <= integerType->literalValue(nullptr))

View File

@ -83,7 +83,7 @@ public:
static AddressType const* payableAddressType() noexcept { return &m_payableAddressType; }
static AddressType const* addressType() noexcept { return &m_addressType; }
static IntegerType const* integerType(unsigned _bits = 256, IntegerType::Modifier _modifier = IntegerType::Modifier::Unsigned)
static IntegerType const* integerType(unsigned _bits, IntegerType::Modifier _modifier)
{
solAssert((_bits % 8) == 0, "");
if (_modifier == IntegerType::Modifier::Unsigned)
@ -91,6 +91,9 @@ public:
else
return &m_intM.at(_bits / 8 - 1);
}
static IntegerType const* uint(unsigned _bits) { return integerType(_bits, IntegerType::Modifier::Unsigned); }
static IntegerType const* uint256() { return uint(256); }
static FixedPointType const* fixedPointType(unsigned m, unsigned n, FixedPointType::Modifier _modifier);

View File

@ -449,7 +449,7 @@ bool AddressType::operator==(Type const& _other) const
MemberList::MemberMap AddressType::nativeMembers(ContractDefinition const*) const
{
MemberList::MemberMap members = {
{"balance", TypeProvider::integerType(256)},
{"balance", TypeProvider::uint256()},
{"call", TypeProvider::functionType(strings{"bytes memory"}, strings{"bool", "bytes memory"}, FunctionType::Kind::BareCall, false, StateMutability::Payable)},
{"callcode", TypeProvider::functionType(strings{"bytes memory"}, strings{"bool", "bytes memory"}, FunctionType::Kind::BareCallCode, false, StateMutability::Payable)},
{"delegatecall", TypeProvider::functionType(strings{"bytes memory"}, strings{"bool", "bytes memory"}, FunctionType::Kind::BareDelegateCall, false, StateMutability::NonPayable)},
@ -1358,7 +1358,7 @@ TypeResult FixedBytesType::binaryOperatorResult(Token _operator, Type const* _ot
MemberList::MemberMap FixedBytesType::nativeMembers(ContractDefinition const*) const
{
return MemberList::MemberMap{MemberList::Member{"length", TypeProvider::integerType(8)}};
return MemberList::MemberMap{MemberList::Member{"length", TypeProvider::uint(8)}};
}
string FixedBytesType::richIdentifier() const
@ -1753,12 +1753,12 @@ MemberList::MemberMap ArrayType::nativeMembers(ContractDefinition const*) const
MemberList::MemberMap members;
if (!isString())
{
members.emplace_back("length", TypeProvider::integerType(256));
members.emplace_back("length", TypeProvider::uint256());
if (isDynamicallySized() && location() == DataLocation::Storage)
{
members.emplace_back("push", TypeProvider::functionType(
TypePointers{baseType()},
TypePointers{TypeProvider::integerType(256)},
TypePointers{TypeProvider::uint256()},
strings{string()},
strings{string()},
isByteArray() ? FunctionType::Kind::ByteArrayPush : FunctionType::Kind::ArrayPush
@ -1778,7 +1778,7 @@ MemberList::MemberMap ArrayType::nativeMembers(ContractDefinition const*) const
TypePointer ArrayType::encodingType() const
{
if (location() == DataLocation::Storage)
return TypeProvider::integerType(256);
return TypeProvider::uint256();
else
return TypeProvider::withLocation(this, DataLocation::Memory, true);
}
@ -1786,7 +1786,7 @@ TypePointer ArrayType::encodingType() const
TypePointer ArrayType::decodingType() const
{
if (location() == DataLocation::Storage)
return TypeProvider::integerType(256);
return TypeProvider::uint256();
else
return this;
}
@ -1955,7 +1955,7 @@ Type const* StructType::encodingType() const
if (location() != DataLocation::Storage)
return this;
return TypeProvider::integerType(256);
return TypeProvider::uint256();
}
BoolResult StructType::isImplicitlyConvertibleTo(Type const& _convertTo) const
@ -2252,7 +2252,7 @@ set<string> StructType::membersMissingInMemory() const
TypePointer EnumType::encodingType() const
{
return TypeProvider::integerType(8 * static_cast<int>(storageBytes()));
return TypeProvider::uint(8 * storageBytes());
}
TypeResult EnumType::unaryOperatorResult(Token _operator) const
@ -2458,7 +2458,7 @@ FunctionType::FunctionType(VariableDeclaration const& _varDecl):
break;
returnType = arrayType->baseType();
m_parameterNames.emplace_back("");
m_parameterTypes.push_back(TypeProvider::integerType(256));
m_parameterTypes.push_back(TypeProvider::uint256());
}
else
break;
@ -3454,24 +3454,24 @@ MemberList::MemberMap MagicType::nativeMembers(ContractDefinition const*) const
case Kind::Block:
return MemberList::MemberMap({
{"coinbase", TypeProvider::payableAddressType()},
{"timestamp", TypeProvider::integerType(256)},
{"timestamp", TypeProvider::uint256()},
{"blockhash", TypeProvider::functionType(strings{"uint"}, strings{"bytes32"}, FunctionType::Kind::BlockHash, false, StateMutability::View)},
{"difficulty", TypeProvider::integerType(256)},
{"number", TypeProvider::integerType(256)},
{"gaslimit", TypeProvider::integerType(256)}
{"difficulty", TypeProvider::uint256()},
{"number", TypeProvider::uint256()},
{"gaslimit", TypeProvider::uint256()}
});
case Kind::Message:
return MemberList::MemberMap({
{"sender", TypeProvider::payableAddressType()},
{"gas", TypeProvider::integerType(256)},
{"value", TypeProvider::integerType(256)},
{"gas", TypeProvider::uint256()},
{"value", TypeProvider::uint256()},
{"data", TypeProvider::arrayType(DataLocation::CallData)},
{"sig", TypeProvider::fixedBytesType(4)}
});
case Kind::Transaction:
return MemberList::MemberMap({
{"origin", TypeProvider::payableAddressType()},
{"gasprice", TypeProvider::integerType(256)}
{"gasprice", TypeProvider::uint256()}
});
case Kind::ABI:
return MemberList::MemberMap({

View File

@ -311,7 +311,7 @@ string ABIFunctions::abiEncodingFunction(
case DataLocation::CallData:
if (
fromArray.isByteArray() ||
*fromArray.baseType() == *TypeProvider::integerType() ||
*fromArray.baseType() == *TypeProvider::uint256() ||
*fromArray.baseType() == FixedBytesType(32)
)
return abiEncodingFunctionCalldataArrayWithoutCleanup(fromArray, *toArray, _options);
@ -369,7 +369,7 @@ string ABIFunctions::abiEncodingFunction(
// possible for library calls where we just forward the storage reference
solAssert(_options.encodeAsLibraryTypes, "");
solAssert(_options.padded && !_options.dynamicInplace, "Non-padded / inplace encoding for library call requested.");
solAssert(to == *TypeProvider::integerType(), "");
solAssert(to == *TypeProvider::uint256(), "");
templ("cleanupConvert", "value");
}
else
@ -445,7 +445,7 @@ string ABIFunctions::abiEncodingFunctionCalldataArrayWithoutCleanup(
solAssert(fromArrayType.location() == DataLocation::CallData, "");
solAssert(
fromArrayType.isByteArray() ||
*fromArrayType.baseType() == *TypeProvider::integerType() ||
*fromArrayType.baseType() == *TypeProvider::uint256() ||
*fromArrayType.baseType() == FixedBytesType(32),
"");
solAssert(fromArrayType.calldataStride() == toArrayType.memoryStride(), "");

View File

@ -45,7 +45,7 @@ void ArrayUtils::copyArrayToStorage(ArrayType const& _targetType, ArrayType cons
// stack layout: [source_ref] [source length] target_ref (top)
solAssert(_targetType.location() == DataLocation::Storage, "");
TypePointer uint256 = TypeProvider::integerType(256);
TypePointer uint256 = TypeProvider::uint256();
TypePointer targetBaseType = _targetType.isByteArray() ? uint256 : _targetType.baseType();
TypePointer sourceBaseType = _sourceType.isByteArray() ? uint256 : _sourceType.baseType();
@ -585,7 +585,7 @@ void ArrayUtils::clearArray(ArrayType const& _typeIn) const
ArrayUtils(_context).convertLengthToSize(_type);
_context << Instruction::ADD << Instruction::SWAP1;
if (_type.baseType()->storageBytes() < 32)
ArrayUtils(_context).clearStorageLoop(TypeProvider::integerType(256));
ArrayUtils(_context).clearStorageLoop(TypeProvider::uint256());
else
ArrayUtils(_context).clearStorageLoop(_type.baseType());
_context << Instruction::POP;
@ -626,7 +626,7 @@ void ArrayUtils::clearDynamicArray(ArrayType const& _type) const
<< Instruction::SWAP1;
// stack: data_pos_end data_pos
if (_type.storageStride() < 32)
clearStorageLoop(TypeProvider::integerType(256));
clearStorageLoop(TypeProvider::uint256());
else
clearStorageLoop(_type.baseType());
// cleanup
@ -733,7 +733,7 @@ void ArrayUtils::resizeDynamicArray(ArrayType const& _typeIn) const
ArrayUtils(_context).convertLengthToSize(_type);
_context << Instruction::DUP2 << Instruction::ADD << Instruction::SWAP1;
// stack: ref new_length current_length first_word data_location_end data_location
ArrayUtils(_context).clearStorageLoop(TypeProvider::integerType(256));
ArrayUtils(_context).clearStorageLoop(TypeProvider::uint256());
_context << Instruction::POP;
// stack: ref new_length current_length first_word
solAssert(_context.stackHeight() - stackHeightStart == 4 - 2, "3");
@ -772,7 +772,7 @@ void ArrayUtils::resizeDynamicArray(ArrayType const& _typeIn) const
_context << Instruction::SWAP2 << Instruction::ADD;
// stack: ref new_length delete_end delete_start
if (_type.storageStride() < 32)
ArrayUtils(_context).clearStorageLoop(TypeProvider::integerType(256));
ArrayUtils(_context).clearStorageLoop(TypeProvider::uint256());
else
ArrayUtils(_context).clearStorageLoop(_type.baseType());

View File

@ -186,7 +186,7 @@ void CompilerUtils::loadFromMemoryDynamic(
void CompilerUtils::storeInMemory(unsigned _offset)
{
unsigned numBytes = prepareMemoryStore(*TypeProvider::integerType(), true);
unsigned numBytes = prepareMemoryStore(*TypeProvider::uint256(), true);
if (numBytes > 0)
m_context << u256(_offset) << Instruction::MSTORE;
}
@ -200,7 +200,7 @@ void CompilerUtils::storeInMemoryDynamic(Type const& _type, bool _padToWordBound
ref->location() == DataLocation::Memory,
"Only in-memory reference type can be stored."
);
storeInMemoryDynamic(*TypeProvider::integerType(), _padToWordBoundaries);
storeInMemoryDynamic(*TypeProvider::uint256(), _padToWordBoundaries);
}
else if (auto str = dynamic_cast<StringLiteralType const*>(&_type))
{
@ -316,7 +316,7 @@ void CompilerUtils::abiDecode(TypePointers const& _typeParameters, bool _fromMem
if (calldataType->isDynamicallySized())
{
// put on stack: data_pointer length
loadFromMemoryDynamic(*TypeProvider::integerType(), !_fromMemory);
loadFromMemoryDynamic(*TypeProvider::uint256(), !_fromMemory);
m_context << Instruction::SWAP1;
// stack: input_end base_offset next_pointer data_offset
m_context.appendInlineAssembly("{ if gt(data_offset, 0x100000000) { revert(0, 0) } }", {"data_offset"});
@ -327,7 +327,7 @@ void CompilerUtils::abiDecode(TypePointers const& _typeParameters, bool _fromMem
{"input_end", "base_offset", "next_ptr", "array_head_ptr"}
);
// retrieve length
loadFromMemoryDynamic(*TypeProvider::integerType(), !_fromMemory, true);
loadFromMemoryDynamic(*TypeProvider::uint256(), !_fromMemory, true);
// stack: input_end base_offset next_pointer array_length data_pointer
m_context << Instruction::SWAP2;
// stack: input_end base_offset data_pointer array_length next_pointer
@ -483,7 +483,7 @@ void CompilerUtils::encodeToMemory(
{
auto const& strType = dynamic_cast<StringLiteralType const&>(*_givenTypes[i]);
m_context << u256(strType.value().size());
storeInMemoryDynamic(*TypeProvider::integerType(), true);
storeInMemoryDynamic(*TypeProvider::uint256(), true);
// stack: ... <end_of_mem'>
storeInMemoryDynamic(strType, _padToWordBoundaries);
}
@ -498,7 +498,7 @@ void CompilerUtils::encodeToMemory(
m_context << dupInstruction(1 + arrayType.sizeOnStack());
ArrayUtils(m_context).retrieveLength(arrayType, 1);
// stack: ... <end_of_mem> <value...> <end_of_mem'> <length>
storeInMemoryDynamic(*TypeProvider::integerType(), true);
storeInMemoryDynamic(*TypeProvider::uint256(), true);
// stack: ... <end_of_mem> <value...> <end_of_mem''>
// copy the new memory pointer
m_context << swapInstruction(arrayType.sizeOnStack() + 1) << Instruction::POP;
@ -869,7 +869,7 @@ void CompilerUtils::convertType(
allocateMemory(storageSize);
// stack: mempos
m_context << Instruction::DUP1 << u256(data.size());
storeInMemoryDynamic(*TypeProvider::integerType());
storeInMemoryDynamic(*TypeProvider::uint256());
// stack: mempos datapos
storeStringData(data);
}
@ -918,7 +918,7 @@ void CompilerUtils::convertType(
if (targetType.isDynamicallySized())
{
m_context << Instruction::DUP2;
storeInMemoryDynamic(*TypeProvider::integerType());
storeInMemoryDynamic(*TypeProvider::uint256());
}
// stack: <mem start> <source ref> (variably sized) <length> <mem data pos>
if (targetType.baseType()->isValueType())
@ -1321,7 +1321,7 @@ void CompilerUtils::storeStringData(bytesConstRef _data)
for (unsigned i = 0; i < _data.size(); i += 32)
{
m_context << h256::Arith(h256(_data.cropped(i), h256::AlignLeft));
storeInMemoryDynamic(*TypeProvider::integerType());
storeInMemoryDynamic(*TypeProvider::uint256());
}
m_context << Instruction::POP;
}

View File

@ -82,7 +82,7 @@ public:
/// @returns the number of bytes consumed in memory.
unsigned loadFromMemory(
unsigned _offset,
Type const& _type = *TypeProvider::integerType(),
Type const& _type = *TypeProvider::uint256(),
bool _fromCalldata = false,
bool _padToWords = false
);

View File

@ -648,7 +648,7 @@ bool ExpressionCompiler::visit(FunctionCall const& _functionCall)
_functionCall.expression().accept(*this);
arguments.front()->accept(*this);
utils().convertType(*arguments.front()->annotation().type, *TypeProvider::integerType(), true);
utils().convertType(*arguments.front()->annotation().type, *TypeProvider::uint256(), true);
// Note that function is not the original function, but the ".gas" function.
// Its values of gasSet and valueSet is equal to the original function's though.
unsigned stackDepth = (function.gasSet() ? 1 : 0) + (function.valueSet() ? 1 : 0);
@ -836,13 +836,13 @@ bool ExpressionCompiler::visit(FunctionCall const& _functionCall)
case FunctionType::Kind::MulMod:
{
arguments[2]->accept(*this);
utils().convertType(*arguments[2]->annotation().type, *TypeProvider::integerType());
utils().convertType(*arguments[2]->annotation().type, *TypeProvider::uint256());
m_context << Instruction::DUP1 << Instruction::ISZERO;
m_context.appendConditionalInvalid();
for (unsigned i = 1; i < 3; i ++)
{
arguments[2 - i]->accept(*this);
utils().convertType(*arguments[2 - i]->annotation().type, *TypeProvider::integerType());
utils().convertType(*arguments[2 - i]->annotation().type, *TypeProvider::uint256());
}
if (function.kind() == FunctionType::Kind::AddMod)
m_context << Instruction::ADDMOD;
@ -928,7 +928,7 @@ bool ExpressionCompiler::visit(FunctionCall const& _functionCall)
// Fetch requested length.
arguments[0]->accept(*this);
utils().convertType(*arguments[0]->annotation().type, *TypeProvider::integerType());
utils().convertType(*arguments[0]->annotation().type, *TypeProvider::uint256());
// Stack: requested_length
utils().fetchFreeMemoryPointer();
@ -1543,7 +1543,7 @@ bool ExpressionCompiler::visit(IndexAccess const& _indexAccess)
TypePointers{keyType}
);
m_context << Instruction::SWAP1;
utils().storeInMemoryDynamic(*TypeProvider::integerType());
utils().storeInMemoryDynamic(*TypeProvider::uint256());
utils().toSizeAfterFreeMemoryPointer();
}
else
@ -1552,7 +1552,7 @@ bool ExpressionCompiler::visit(IndexAccess const& _indexAccess)
appendExpressionCopyToMemory(*keyType, *_indexAccess.indexExpression());
m_context << Instruction::SWAP1;
solAssert(CompilerUtils::freeMemoryPointer >= 0x40, "");
utils().storeInMemoryDynamic(*TypeProvider::integerType());
utils().storeInMemoryDynamic(*TypeProvider::uint256());
m_context << u256(0);
}
m_context << Instruction::KECCAK256;
@ -1565,7 +1565,7 @@ bool ExpressionCompiler::visit(IndexAccess const& _indexAccess)
solAssert(_indexAccess.indexExpression(), "Index expression expected.");
_indexAccess.indexExpression()->accept(*this);
utils().convertType(*_indexAccess.indexExpression()->annotation().type, *TypeProvider::integerType(), true);
utils().convertType(*_indexAccess.indexExpression()->annotation().type, *TypeProvider::uint256(), true);
// stack layout: <base_ref> [<length>] <index>
switch (arrayType.location())
{
@ -1632,7 +1632,7 @@ bool ExpressionCompiler::visit(IndexAccess const& _indexAccess)
solAssert(_indexAccess.indexExpression(), "Index expression expected.");
_indexAccess.indexExpression()->accept(*this);
utils().convertType(*_indexAccess.indexExpression()->annotation().type, *TypeProvider::integerType(), true);
utils().convertType(*_indexAccess.indexExpression()->annotation().type, *TypeProvider::uint256(), true);
// stack layout: <value> <index>
// check out-of-bounds access
m_context << u256(fixedBytesType.numBytes());

View File

@ -1206,7 +1206,7 @@ void SMTChecker::assignment(VariableDeclaration const& _variable, smt::Expressio
if (type->category() == Type::Category::Integer)
addOverflowTarget(OverflowTarget::Type::All, type, _value, _location);
else if (type->category() == Type::Category::Address)
addOverflowTarget(OverflowTarget::Type::All, TypeProvider::integerType(160), _value, _location);
addOverflowTarget(OverflowTarget::Type::All, TypeProvider::uint(160), _value, _location);
else if (type->category() == Type::Category::Mapping)
arrayAssignment();
m_interface->addAssertion(newValue(_variable) == _value);

View File

@ -120,7 +120,7 @@ pair<bool, shared_ptr<SymbolicVariable>> dev::solidity::newSymbolicVariable(
if (!isSupportedTypeDeclaration(_type))
{
abstract = true;
var = make_shared<SymbolicIntVariable>(TypeProvider::integerType(256), _uniqueName, _solver);
var = make_shared<SymbolicIntVariable>(TypeProvider::uint256(), _uniqueName, _solver);
}
else if (isBool(_type.category()))
var = make_shared<SymbolicBoolVariable>(type, _uniqueName, _solver);
@ -143,7 +143,7 @@ pair<bool, shared_ptr<SymbolicVariable>> dev::solidity::newSymbolicVariable(
auto rational = dynamic_cast<RationalNumberType const*>(&_type);
solAssert(rational, "");
if (rational->isFractional())
var = make_shared<SymbolicIntVariable>(TypeProvider::integerType(256), _uniqueName, _solver);
var = make_shared<SymbolicIntVariable>(TypeProvider::uint256(), _uniqueName, _solver);
else
var = make_shared<SymbolicIntVariable>(type, _uniqueName, _solver);
}

View File

@ -103,7 +103,7 @@ SymbolicAddressVariable::SymbolicAddressVariable(
string _uniqueName,
smt::SolverInterface& _interface
):
SymbolicIntVariable(TypeProvider::integerType(160), move(_uniqueName), _interface)
SymbolicIntVariable(TypeProvider::uint(160), move(_uniqueName), _interface)
{
}
@ -112,7 +112,7 @@ SymbolicFixedBytesVariable::SymbolicFixedBytesVariable(
string _uniqueName,
smt::SolverInterface& _interface
):
SymbolicIntVariable(TypeProvider::integerType(_numBytes * 8), move(_uniqueName), _interface)
SymbolicIntVariable(TypeProvider::uint(_numBytes * 8), move(_uniqueName), _interface)
{
}

View File

@ -233,7 +233,7 @@ BOOST_AUTO_TEST_CASE(encoded_sizes)
ArrayType const* uint24Array = TypeProvider::arrayType(
DataLocation::Memory,
TypeProvider::integerType(24),
TypeProvider::uint(24),
9
);
BOOST_CHECK_EQUAL(uint24Array->calldataEncodedSize(true), 9 * 32);