diff options
-rw-r--r-- | AST.cpp | 20 | ||||
-rw-r--r-- | Exceptions.h | 2 | ||||
-rw-r--r-- | ExpressionCompiler.cpp | 44 | ||||
-rw-r--r-- | ExpressionCompiler.h | 22 | ||||
-rw-r--r-- | Types.cpp | 2 | ||||
-rw-r--r-- | Types.h | 2 |
6 files changed, 38 insertions, 54 deletions
@@ -486,7 +486,7 @@ void FunctionCall::checkTypeRequirements() if (m_arguments.size() != 1) BOOST_THROW_EXCEPTION(createTypeError("More than one argument for explicit type conversion.")); if (!m_names.empty()) - BOOST_THROW_EXCEPTION(createTypeError("Type conversion can't allow named arguments.")); + BOOST_THROW_EXCEPTION(createTypeError("Type conversion cannot allow named arguments.")); if (!m_arguments.front()->getType()->isExplicitlyConvertibleTo(*type.getActualType())) BOOST_THROW_EXCEPTION(createTypeError("Explicit type conversion not allowed.")); m_type = type.getActualType(); @@ -504,24 +504,22 @@ void FunctionCall::checkTypeRequirements() { for (size_t i = 0; i < m_arguments.size(); ++i) if (functionType->getLocation() != FunctionType::Location::SHA3 && - !m_arguments[i]->getType()->isImplicitlyConvertibleTo(*parameterTypes[i])) - BOOST_THROW_EXCEPTION(createTypeError("Invalid type for argument in function call.")); + !m_arguments[i]->getType()->isImplicitlyConvertibleTo(*parameterTypes[i])) + BOOST_THROW_EXCEPTION(m_arguments[i]->createTypeError("Invalid type for argument in function call.")); } - else if (functionType->getLocation() == FunctionType::Location::SHA3) - BOOST_THROW_EXCEPTION(createTypeError("Named arguments can't be used for SHA3.")); else { + if (functionType->getLocation() == FunctionType::Location::SHA3) + BOOST_THROW_EXCEPTION(createTypeError("Named arguments cannnot be used for SHA3.")); auto const& parameterNames = functionType->getParameterNames(); if (parameterNames.size() != m_names.size()) BOOST_THROW_EXCEPTION(createTypeError("Some argument names are missing.")); // check duplicate names - for (size_t i = 0; i < m_names.size(); i++) { - for (size_t j = i + 1; j < m_names.size(); j++) { + for (size_t i = 0; i < m_names.size(); i++) + for (size_t j = i + 1; j < m_names.size(); j++) if (*m_names[i] == *m_names[j]) - BOOST_THROW_EXCEPTION(createTypeError("Duplicate named argument.")); - } - } + BOOST_THROW_EXCEPTION(m_arguments[i]->createTypeError("Duplicate named argument.")); for (size_t i = 0; i < m_names.size(); i++) { bool found = false; @@ -536,7 +534,7 @@ void FunctionCall::checkTypeRequirements() } } if (!found) - BOOST_THROW_EXCEPTION(createTypeError("Named argument doesn't match function declaration.")); + BOOST_THROW_EXCEPTION(createTypeError("Named argument does not match function declaration.")); } } diff --git a/Exceptions.h b/Exceptions.h index 14f91977..0b25abee 100644 --- a/Exceptions.h +++ b/Exceptions.h @@ -38,7 +38,7 @@ struct CompilerError: virtual Exception {}; struct InternalCompilerError: virtual Exception {}; struct DocstringParsingError: virtual Exception {}; -typedef boost::error_info<struct tag_sourceLocation, Location> errinfo_sourceLocation; +using errinfo_sourceLocation = boost::error_info<struct tag_sourceLocation, Location>; } } diff --git a/ExpressionCompiler.cpp b/ExpressionCompiler.cpp index ac17d65d..f649f1a4 100644 --- a/ExpressionCompiler.cpp +++ b/ExpressionCompiler.cpp @@ -327,7 +327,7 @@ bool ExpressionCompiler::visit(FunctionCall const& _functionCall) break; case Location::SHA3: { - unsigned length = appendArgumentsCopyToMemory(arguments); + unsigned length = appendArgumentsCopyToMemory(arguments, TypePointers(), 0, false); m_context << u256(length) << u256(0) << eth::Instruction::SHA3; break; } @@ -838,27 +838,23 @@ void ExpressionCompiler::appendExternalFunctionCall(FunctionType const& _functio unsigned ExpressionCompiler::appendArgumentsCopyToMemory(vector<ASTPointer<Expression const>> const& _arguments, TypePointers const& _types, - unsigned _memoryOffset) + unsigned _memoryOffset, + bool _padToWordBoundaries) { + solAssert(_types.empty() || _types.size() == _arguments.size(), ""); unsigned length = 0; - if (!_types.empty()) + for (size_t i = 0; i < _arguments.size(); ++i) { - for (unsigned i = 0; i < _arguments.size(); ++i) - length += appendExpressionCopyToMemory(*_types[i], *_arguments[i], _memoryOffset + length); - return length; - } - - // without type conversion - for (unsigned i = 0; i < _arguments.size(); ++i) - { - const bool wantPadding = false; _arguments[i]->accept(*this); - length += moveTypeToMemory(*_arguments[i]->getType()->getRealType(), _arguments[i]->getLocation(), _memoryOffset + length, wantPadding); + TypePointer const& expectedType = _types.empty() ? _arguments[i]->getType()->getRealType() : _types[i]; + appendTypeConversion(*_arguments[i]->getType(), *expectedType, true); + length += appendTypeMoveToMemory(*expectedType, _arguments[i]->getLocation(), + _memoryOffset + length, _padToWordBoundaries); } return length; } -unsigned ExpressionCompiler::moveTypeToMemory(Type const& _type, Location const& _location, unsigned _memoryOffset, bool _padToWordBoundaries) +unsigned ExpressionCompiler::appendTypeMoveToMemory(Type const& _type, Location const& _location, unsigned _memoryOffset, bool _padToWordBoundaries) { unsigned const c_encodedSize = _type.getCalldataEncodedSize(); unsigned const c_numBytes = _padToWordBoundaries ? CompilerUtils::getPaddedSize(c_encodedSize) : c_encodedSize; @@ -870,19 +866,13 @@ unsigned ExpressionCompiler::moveTypeToMemory(Type const& _type, Location const& return CompilerUtils(m_context).storeInMemory(_memoryOffset, c_numBytes, c_leftAligned, _padToWordBoundaries); } -unsigned ExpressionCompiler::appendTypeConversionAndMoveToMemory(Type const& _expectedType, Type const& _type, - Location const& _location, unsigned _memoryOffset) -{ - appendTypeConversion(_type, _expectedType, true); - return moveTypeToMemory(_expectedType, _location, _memoryOffset); -} - unsigned ExpressionCompiler::appendExpressionCopyToMemory(Type const& _expectedType, Expression const& _expression, unsigned _memoryOffset) { _expression.accept(*this); - return appendTypeConversionAndMoveToMemory(_expectedType, *_expression.getType(), _expression.getLocation(), _memoryOffset); + appendTypeConversion(*_expression.getType(), _expectedType, true); + return appendTypeMoveToMemory(_expectedType, _expression.getLocation(), _memoryOffset); } void ExpressionCompiler::appendStateVariableAccessor(VariableDeclaration const& _varDecl) @@ -890,20 +880,20 @@ void ExpressionCompiler::appendStateVariableAccessor(VariableDeclaration const& FunctionType accessorType(_varDecl); unsigned length = 0; - TypePointers const& params = accessorType.getParameterTypes(); + TypePointers const& paramTypes = accessorType.getParameterTypes(); // move arguments to memory - for (TypePointer const& param: boost::adaptors::reverse(params)) - length += appendTypeConversionAndMoveToMemory(*param, *param, Location(), length); + for (TypePointer const& paramType: boost::adaptors::reverse(paramTypes)) + length += appendTypeMoveToMemory(*paramType, Location(), length); // retrieve the position of the variable m_context << m_context.getStorageLocationOfVariable(_varDecl); TypePointer returnType = _varDecl.getType(); - for (TypePointer const& param: params) + for (TypePointer const& paramType: paramTypes) { // move offset to memory CompilerUtils(m_context).storeInMemory(length); - unsigned argLen = CompilerUtils::getPaddedSize(param->getCalldataEncodedSize()); + unsigned argLen = CompilerUtils::getPaddedSize(paramType->getCalldataEncodedSize()); length -= argLen; m_context << u256(argLen + 32) << u256(length) << eth::Instruction::SHA3; diff --git a/ExpressionCompiler.h b/ExpressionCompiler.h index cba308b8..df8a0516 100644 --- a/ExpressionCompiler.h +++ b/ExpressionCompiler.h @@ -92,20 +92,18 @@ private: /// Appends code to call a function of the given type with the given arguments. void appendExternalFunctionCall(FunctionType const& _functionType, std::vector<ASTPointer<Expression const>> const& _arguments, bool bare = false); - /// Appends code that copies the given arguments to memory (with optional offset). - /// @returns the number of bytes copied to memory + /// Appends code that evaluates the given arguments and moves the result to memory (with optional offset). + /// @returns the number of bytes moved to memory unsigned appendArgumentsCopyToMemory(std::vector<ASTPointer<Expression const>> const& _arguments, TypePointers const& _types = {}, - unsigned _memoryOffset = 0); - /// Appends code that copies a type to memory. - /// @returns the number of bytes copied to memory - unsigned appendTypeConversionAndMoveToMemory(Type const& _expectedType, Type const& _type, - Location const& _location, unsigned _memoryOffset = 0); - /// Appends code that moves a type to memory - /// @returns the number of bytes copied to memory - unsigned moveTypeToMemory(Type const& _type, Location const& _location, unsigned _memoryOffset, bool _padToWordBoundaries = true); - /// Appends code that evaluates a single expression and copies it to memory (with optional offset). - /// @returns the number of bytes copied to memory + unsigned _memoryOffset = 0, + bool _padToWordBoundaries = true); + /// Appends code that moves a stack element of the given type to memory + /// @returns the number of bytes moved to memory + unsigned appendTypeMoveToMemory(Type const& _type, Location const& _location, unsigned _memoryOffset, + bool _padToWordBoundaries = true); + /// Appends code that evaluates a single expression and moves the result to memory (with optional offset). + /// @returns the number of bytes moved to memory unsigned appendExpressionCopyToMemory(Type const& _expectedType, Expression const& _expression, unsigned _memoryOffset = 0); @@ -360,7 +360,7 @@ u256 IntegerConstantType::literalValue(Literal const*) const TypePointer IntegerConstantType::getRealType() const { auto intType = getIntegerType(); - solAssert(!!intType, std::string("getRealType called with invalid integer constant") + toString()); + solAssert(!!intType, "getRealType called with invalid integer constant " + toString()); return intType; } @@ -179,7 +179,6 @@ public: virtual MemberList const& getMembers() const { return isAddress() ? AddressMemberList : EmptyMemberList; } virtual std::string toString() const override; - virtual TypePointer getRealType() const { return std::make_shared<IntegerType>(m_bits, m_modifier); } int getNumBits() const { return m_bits; } bool isHash() const { return m_modifier == Modifier::Hash || m_modifier == Modifier::Address; } @@ -250,7 +249,6 @@ public: virtual std::string toString() const override { return "string" + dev::toString(m_bytes); } virtual u256 literalValue(Literal const* _literal) const override; - virtual TypePointer getRealType() const override { return std::make_shared<StaticStringType>(m_bytes); } int getNumBytes() const { return m_bytes; } |