Replace IntegerType(256) with static function IntegerType::uint256()

This commit is contained in:
hydai 2018-11-29 16:34:17 +08:00
parent b4086ac870
commit 9326adc3db
6 changed files with 24 additions and 22 deletions

View File

@ -2506,7 +2506,7 @@ bool TypeChecker::visit(IndexAccess const& _access)
} }
else else
{ {
expectType(*index, IntegerType(256)); expectType(*index, IntegerType::uint256());
if (!m_errorReporter.hasErrors()) if (!m_errorReporter.hasErrors())
if (auto numberType = dynamic_cast<RationalNumberType const*>(type(*index).get())) if (auto numberType = dynamic_cast<RationalNumberType const*>(type(*index).get()))
{ {
@ -2537,7 +2537,7 @@ bool TypeChecker::visit(IndexAccess const& _access)
resultType = make_shared<TypeType>(make_shared<ArrayType>(DataLocation::Memory, typeType.actualType())); resultType = make_shared<TypeType>(make_shared<ArrayType>(DataLocation::Memory, typeType.actualType()));
else else
{ {
expectType(*index, IntegerType(256)); expectType(*index, IntegerType::uint256());
if (auto length = dynamic_cast<RationalNumberType const*>(type(*index).get())) if (auto length = dynamic_cast<RationalNumberType const*>(type(*index).get()))
resultType = make_shared<TypeType>(make_shared<ArrayType>( resultType = make_shared<TypeType>(make_shared<ArrayType>(
DataLocation::Memory, DataLocation::Memory,
@ -2556,7 +2556,7 @@ bool TypeChecker::visit(IndexAccess const& _access)
m_errorReporter.typeError(_access.location(), "Index expression cannot be omitted."); m_errorReporter.typeError(_access.location(), "Index expression cannot be omitted.");
else else
{ {
if (!expectType(*index, IntegerType(256))) if (!expectType(*index, IntegerType::uint256()))
m_errorReporter.fatalTypeError(_access.location(), "Index expression cannot be represented as an unsigned integer."); m_errorReporter.fatalTypeError(_access.location(), "Index expression cannot be represented as an unsigned integer.");
if (auto integerType = dynamic_cast<RationalNumberType const*>(type(*index).get())) if (auto integerType = dynamic_cast<RationalNumberType const*>(type(*index).get()))
if (bytesType.numBytes() <= integerType->literalValue(nullptr)) if (bytesType.numBytes() <= integerType->literalValue(nullptr))

View File

@ -374,6 +374,8 @@ public:
Unsigned, Signed Unsigned, Signed
}; };
static IntegerType& uint256() { static std::shared_ptr<IntegerType> uint256(std::make_shared<IntegerType>(256)); return *uint256; }
Category category() const override { return Category::Integer; } Category category() const override { return Category::Integer; }
explicit IntegerType(unsigned _bits, Modifier _modifier = Modifier::Unsigned); explicit IntegerType(unsigned _bits, Modifier _modifier = Modifier::Unsigned);

View File

@ -558,7 +558,7 @@ string ABIFunctions::abiEncodingFunction(
// special case: convert storage reference type to value type - this is only // special case: convert storage reference type to value type - this is only
// possible for library calls where we just forward the storage reference // possible for library calls where we just forward the storage reference
solAssert(_encodeAsLibraryTypes, ""); solAssert(_encodeAsLibraryTypes, "");
solAssert(to == IntegerType(256), ""); solAssert(to == IntegerType::uint256(), "");
templ("cleanupConvert", "value"); templ("cleanupConvert", "value");
} }
else else

View File

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

View File

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

View File

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