diff --git a/libsolidity/analysis/TypeChecker.cpp b/libsolidity/analysis/TypeChecker.cpp index e1a89dd40..fcc6746fd 100644 --- a/libsolidity/analysis/TypeChecker.cpp +++ b/libsolidity/analysis/TypeChecker.cpp @@ -2506,7 +2506,7 @@ bool TypeChecker::visit(IndexAccess const& _access) } else { - expectType(*index, IntegerType(256)); + expectType(*index, IntegerType::uint256()); if (!m_errorReporter.hasErrors()) if (auto numberType = dynamic_cast(type(*index).get())) { @@ -2537,7 +2537,7 @@ bool TypeChecker::visit(IndexAccess const& _access) resultType = make_shared(make_shared(DataLocation::Memory, typeType.actualType())); else { - expectType(*index, IntegerType(256)); + expectType(*index, IntegerType::uint256()); if (auto length = dynamic_cast(type(*index).get())) resultType = make_shared(make_shared( DataLocation::Memory, @@ -2556,7 +2556,7 @@ bool TypeChecker::visit(IndexAccess const& _access) m_errorReporter.typeError(_access.location(), "Index expression cannot be omitted."); 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."); if (auto integerType = dynamic_cast(type(*index).get())) if (bytesType.numBytes() <= integerType->literalValue(nullptr)) diff --git a/libsolidity/ast/Types.h b/libsolidity/ast/Types.h index 953aa557d..0f0548d3b 100644 --- a/libsolidity/ast/Types.h +++ b/libsolidity/ast/Types.h @@ -374,6 +374,8 @@ public: Unsigned, Signed }; + static IntegerType& uint256() { static std::shared_ptr uint256(std::make_shared(256)); return *uint256; } + Category category() const override { return Category::Integer; } explicit IntegerType(unsigned _bits, Modifier _modifier = Modifier::Unsigned); diff --git a/libsolidity/codegen/ABIFunctions.cpp b/libsolidity/codegen/ABIFunctions.cpp index bd29b3820..b02623de6 100644 --- a/libsolidity/codegen/ABIFunctions.cpp +++ b/libsolidity/codegen/ABIFunctions.cpp @@ -558,7 +558,7 @@ string ABIFunctions::abiEncodingFunction( // special case: convert storage reference type to value type - this is only // possible for library calls where we just forward the storage reference solAssert(_encodeAsLibraryTypes, ""); - solAssert(to == IntegerType(256), ""); + solAssert(to == IntegerType::uint256(), ""); templ("cleanupConvert", "value"); } else diff --git a/libsolidity/codegen/CompilerUtils.cpp b/libsolidity/codegen/CompilerUtils.cpp index 93c8cc778..7d2ad9d2b 100644 --- a/libsolidity/codegen/CompilerUtils.cpp +++ b/libsolidity/codegen/CompilerUtils.cpp @@ -136,7 +136,7 @@ void CompilerUtils::loadFromMemoryDynamic( void CompilerUtils::storeInMemory(unsigned _offset) { - unsigned numBytes = prepareMemoryStore(IntegerType(256), true); + unsigned numBytes = prepareMemoryStore(IntegerType::uint256(), true); if (numBytes > 0) m_context << u256(_offset) << Instruction::MSTORE; } @@ -150,7 +150,7 @@ void CompilerUtils::storeInMemoryDynamic(Type const& _type, bool _padToWordBound ref->location() == DataLocation::Memory, "Only in-memory reference type can be stored." ); - storeInMemoryDynamic(IntegerType(256), _padToWordBoundaries); + storeInMemoryDynamic(IntegerType::uint256(), _padToWordBoundaries); } else if (auto str = dynamic_cast(&_type)) { @@ -266,7 +266,7 @@ void CompilerUtils::abiDecode(TypePointers const& _typeParameters, bool _fromMem if (calldataType->isDynamicallySized()) { // put on stack: data_pointer length - loadFromMemoryDynamic(IntegerType(256), !_fromMemory); + loadFromMemoryDynamic(IntegerType::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"}); @@ -277,7 +277,7 @@ void CompilerUtils::abiDecode(TypePointers const& _typeParameters, bool _fromMem {"input_end", "base_offset", "next_ptr", "array_head_ptr"} ); // retrieve length - loadFromMemoryDynamic(IntegerType(256), !_fromMemory, true); + loadFromMemoryDynamic(IntegerType::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 @@ -430,7 +430,7 @@ void CompilerUtils::encodeToMemory( { auto const& strType = dynamic_cast(*_givenTypes[i]); m_context << u256(strType.value().size()); - storeInMemoryDynamic(IntegerType(256), true); + storeInMemoryDynamic(IntegerType::uint256(), true); // stack: ... storeInMemoryDynamic(strType, _padToWordBoundaries); } @@ -445,7 +445,7 @@ void CompilerUtils::encodeToMemory( m_context << dupInstruction(1 + arrayType.sizeOnStack()); ArrayUtils(m_context).retrieveLength(arrayType, 1); // stack: ... - storeInMemoryDynamic(IntegerType(256), true); + storeInMemoryDynamic(IntegerType::uint256(), true); // stack: ... // copy the new memory pointer m_context << swapInstruction(arrayType.sizeOnStack() + 1) << Instruction::POP; @@ -807,7 +807,7 @@ void CompilerUtils::convertType( allocateMemory(); // stack: mempos m_context << Instruction::DUP1 << u256(data.size()); - storeInMemoryDynamic(IntegerType(256)); + storeInMemoryDynamic(IntegerType::uint256()); // stack: mempos datapos storeStringData(data); } @@ -856,7 +856,7 @@ void CompilerUtils::convertType( if (targetType.isDynamicallySized()) { m_context << Instruction::DUP2; - storeInMemoryDynamic(IntegerType(256)); + storeInMemoryDynamic(IntegerType::uint256()); } // stack: (variably sized) if (targetType.baseType()->isValueType()) @@ -1210,7 +1210,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(IntegerType(256)); + storeInMemoryDynamic(IntegerType::uint256()); } m_context << Instruction::POP; } diff --git a/libsolidity/codegen/CompilerUtils.h b/libsolidity/codegen/CompilerUtils.h index bd8170ad1..5f7dce226 100644 --- a/libsolidity/codegen/CompilerUtils.h +++ b/libsolidity/codegen/CompilerUtils.h @@ -69,7 +69,7 @@ public: /// @returns the number of bytes consumed in memory. unsigned loadFromMemory( unsigned _offset, - Type const& _type = IntegerType(256), + Type const& _type = IntegerType::uint256(), bool _fromCalldata = false, bool _padToWords = false ); diff --git a/libsolidity/codegen/ExpressionCompiler.cpp b/libsolidity/codegen/ExpressionCompiler.cpp index c9a1e076b..87eecd2e3 100644 --- a/libsolidity/codegen/ExpressionCompiler.cpp +++ b/libsolidity/codegen/ExpressionCompiler.cpp @@ -631,7 +631,7 @@ bool ExpressionCompiler::visit(FunctionCall const& _functionCall) _functionCall.expression().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. // Its values of gasSet and valueSet is equal to the original function's though. unsigned stackDepth = (function.gasSet() ? 1 : 0) + (function.valueSet() ? 1 : 0); @@ -814,13 +814,13 @@ bool ExpressionCompiler::visit(FunctionCall const& _functionCall) case FunctionType::Kind::MulMod: { 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.appendConditionalInvalid(); for (unsigned i = 1; i < 3; i ++) { 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) m_context << Instruction::ADDMOD; @@ -904,7 +904,7 @@ bool ExpressionCompiler::visit(FunctionCall const& _functionCall) // Fetch requested length. arguments[0]->accept(*this); - utils().convertType(*arguments[0]->annotation().type, IntegerType(256)); + utils().convertType(*arguments[0]->annotation().type, IntegerType::uint256()); // Stack: requested_length utils().fetchFreeMemoryPointer(); @@ -1452,7 +1452,7 @@ bool ExpressionCompiler::visit(IndexAccess const& _indexAccess) TypePointers{keyType} ); m_context << Instruction::SWAP1; - utils().storeInMemoryDynamic(IntegerType(256)); + utils().storeInMemoryDynamic(IntegerType::uint256()); utils().toSizeAfterFreeMemoryPointer(); } else @@ -1461,7 +1461,7 @@ bool ExpressionCompiler::visit(IndexAccess const& _indexAccess) appendExpressionCopyToMemory(*keyType, *_indexAccess.indexExpression()); m_context << Instruction::SWAP1; solAssert(CompilerUtils::freeMemoryPointer >= 0x40, ""); - utils().storeInMemoryDynamic(IntegerType(256)); + utils().storeInMemoryDynamic(IntegerType::uint256()); m_context << u256(0); } m_context << Instruction::KECCAK256; @@ -1474,7 +1474,7 @@ bool ExpressionCompiler::visit(IndexAccess const& _indexAccess) solAssert(_indexAccess.indexExpression(), "Index expression expected."); _indexAccess.indexExpression()->accept(*this); - utils().convertType(*_indexAccess.indexExpression()->annotation().type, IntegerType(256), true); + utils().convertType(*_indexAccess.indexExpression()->annotation().type, IntegerType::uint256(), true); // stack layout: [] ArrayUtils(m_context).accessIndex(arrayType); switch (arrayType.location()) @@ -1510,7 +1510,7 @@ bool ExpressionCompiler::visit(IndexAccess const& _indexAccess) solAssert(_indexAccess.indexExpression(), "Index expression expected."); _indexAccess.indexExpression()->accept(*this); - utils().convertType(*_indexAccess.indexExpression()->annotation().type, IntegerType(256), true); + utils().convertType(*_indexAccess.indexExpression()->annotation().type, IntegerType::uint256(), true); // stack layout: // check out-of-bounds access m_context << u256(fixedBytesType.numBytes());