diff options
author | chriseth <c@ethdev.com> | 2015-10-14 04:32:27 +0800 |
---|---|---|
committer | chriseth <c@ethdev.com> | 2015-10-14 04:32:27 +0800 |
commit | e11e10f8176cd6f866e78b5d12c86fe77367c64a (patch) | |
tree | baed9ab43931470a8c60b73e6593de605223ee32 | |
parent | 23865e39295dd9199769727b037c1d126807b20e (diff) | |
parent | 72f7792f8e09b41987ddd493984402891d49238f (diff) | |
download | dexon-solidity-e11e10f8176cd6f866e78b5d12c86fe77367c64a.tar.gz dexon-solidity-e11e10f8176cd6f866e78b5d12c86fe77367c64a.tar.zst dexon-solidity-e11e10f8176cd6f866e78b5d12c86fe77367c64a.zip |
Merge pull request #126 from chriseth/destructuringAssignment
Multi-variable declarations.
-rw-r--r-- | libsolidity/AST.cpp | 7 | ||||
-rw-r--r-- | libsolidity/AST.h | 27 | ||||
-rw-r--r-- | libsolidity/ASTAnnotations.h | 7 | ||||
-rw-r--r-- | libsolidity/AST_accept.h | 16 | ||||
-rw-r--r-- | libsolidity/Compiler.cpp | 25 | ||||
-rw-r--r-- | libsolidity/ExpressionCompiler.cpp | 35 | ||||
-rw-r--r-- | libsolidity/NameAndTypeResolver.cpp | 4 | ||||
-rw-r--r-- | libsolidity/Parser.cpp | 62 | ||||
-rw-r--r-- | libsolidity/TypeChecker.cpp | 183 | ||||
-rw-r--r-- | libsolidity/TypeChecker.h | 1 | ||||
-rw-r--r-- | libsolidity/Types.cpp | 56 | ||||
-rw-r--r-- | libsolidity/Types.h | 44 | ||||
-rw-r--r-- | test/libsolidity/SolidityEndToEndTest.cpp | 24 | ||||
-rw-r--r-- | test/libsolidity/SolidityNameAndTypeResolution.cpp | 97 | ||||
-rw-r--r-- | test/libsolidity/SolidityParser.cpp | 20 |
15 files changed, 480 insertions, 128 deletions
diff --git a/libsolidity/AST.cpp b/libsolidity/AST.cpp index d55bc13c..490ae29a 100644 --- a/libsolidity/AST.cpp +++ b/libsolidity/AST.cpp @@ -325,6 +325,13 @@ ReturnAnnotation& Return::annotation() const return static_cast<ReturnAnnotation&>(*m_annotation); } +VariableDeclarationStatementAnnotation& VariableDeclarationStatement::annotation() const +{ + if (!m_annotation) + m_annotation = new VariableDeclarationStatementAnnotation(); + return static_cast<VariableDeclarationStatementAnnotation&>(*m_annotation); +} + ExpressionAnnotation& Expression::annotation() const { if (!m_annotation) diff --git a/libsolidity/AST.h b/libsolidity/AST.h index 075c1ff5..f257dfb2 100644 --- a/libsolidity/AST.h +++ b/libsolidity/AST.h @@ -558,7 +558,9 @@ protected: private: ASTPointer<TypeName> m_typeName; ///< can be empty ("var") - ASTPointer<Expression> m_value; ///< the assigned value, can be missing + /// Initially assigned value, can be missing. For local variables, this is stored inside + /// VariableDeclarationStatement and not here. + ASTPointer<Expression> m_value; bool m_isStateVariable; ///< Whether or not this is a contract state variable bool m_isIndexed; ///< Whether this is an indexed variable (used by events). bool m_isConstant; ///< Whether the variable is a compile-time constant. @@ -963,20 +965,33 @@ public: * Definition of a variable as a statement inside a function. It requires a type name (which can * also be "var") but the actual assignment can be missing. * Examples: var a = 2; uint256 a; + * As a second form, multiple variables can be declared, cannot have a type and must be assigned + * right away. If the first or last component is unnamed, it can "consume" an arbitrary number + * of components. + * Examples: var (a, b) = f(); var (a,,,c) = g(); var (a,) = d(); */ class VariableDeclarationStatement: public Statement { public: - VariableDeclarationStatement(SourceLocation const& _location, ASTPointer<VariableDeclaration> _variable): - Statement(_location), m_variable(_variable) {} + VariableDeclarationStatement( + SourceLocation const& _location, + std::vector<ASTPointer<VariableDeclaration>> const& _variables, + ASTPointer<Expression> const& _initialValue + ): + Statement(_location), m_variables(_variables), m_initialValue(_initialValue) {} virtual void accept(ASTVisitor& _visitor) override; virtual void accept(ASTConstVisitor& _visitor) const override; - VariableDeclaration const& declaration() const { return *m_variable; } - Expression const* expression() const { return m_variable->value().get(); } + VariableDeclarationStatementAnnotation& annotation() const override; + + std::vector<ASTPointer<VariableDeclaration>> const& declarations() const { return m_variables; } + Expression const* initialValue() const { return m_initialValue.get(); } private: - ASTPointer<VariableDeclaration> m_variable; + /// List of variables, some of which can be empty pointers (unnamed components). + std::vector<ASTPointer<VariableDeclaration>> m_variables; + /// The assigned expression / initial value. + ASTPointer<Expression> m_initialValue; }; /** diff --git a/libsolidity/ASTAnnotations.h b/libsolidity/ASTAnnotations.h index be9b164c..1b772ffa 100644 --- a/libsolidity/ASTAnnotations.h +++ b/libsolidity/ASTAnnotations.h @@ -84,6 +84,13 @@ struct UserDefinedTypeNameAnnotation: TypeNameAnnotation Declaration const* referencedDeclaration = nullptr; }; +struct VariableDeclarationStatementAnnotation: ASTAnnotation +{ + /// Information about which component of the value is assigned to which variable. + /// The pointer can be null to signify that the component is discarded. + std::vector<VariableDeclaration const*> assignments; +}; + struct ExpressionAnnotation: ASTAnnotation { /// Inferred type of the expression. diff --git a/libsolidity/AST_accept.h b/libsolidity/AST_accept.h index dfa9c425..994bfc8d 100644 --- a/libsolidity/AST_accept.h +++ b/libsolidity/AST_accept.h @@ -516,14 +516,26 @@ void ExpressionStatement::accept(ASTConstVisitor& _visitor) const void VariableDeclarationStatement::accept(ASTVisitor& _visitor) { if (_visitor.visit(*this)) - m_variable->accept(_visitor); + { + for (ASTPointer<VariableDeclaration> const& var: m_variables) + if (var) + var->accept(_visitor); + if (m_initialValue) + m_initialValue->accept(_visitor); + } _visitor.endVisit(*this); } void VariableDeclarationStatement::accept(ASTConstVisitor& _visitor) const { if (_visitor.visit(*this)) - m_variable->accept(_visitor); + { + for (ASTPointer<VariableDeclaration> const& var: m_variables) + if (var) + var->accept(_visitor); + if (m_initialValue) + m_initialValue->accept(_visitor); + } _visitor.endVisit(*this); } diff --git a/libsolidity/Compiler.cpp b/libsolidity/Compiler.cpp index 7ce2121e..7c6c7831 100644 --- a/libsolidity/Compiler.cpp +++ b/libsolidity/Compiler.cpp @@ -623,10 +623,29 @@ bool Compiler::visit(VariableDeclarationStatement const& _variableDeclarationSta { StackHeightChecker checker(m_context); CompilerContext::LocationSetter locationSetter(m_context, _variableDeclarationStatement); - if (Expression const* expression = _variableDeclarationStatement.expression()) + if (Expression const* expression = _variableDeclarationStatement.initialValue()) { - compileExpression(*expression, _variableDeclarationStatement.declaration().annotation().type); - CompilerUtils(m_context).moveToStackVariable(_variableDeclarationStatement.declaration()); + CompilerUtils utils(m_context); + compileExpression(*expression); + TypePointers valueTypes; + if (auto tupleType = dynamic_cast<TupleType const*>(expression->annotation().type.get())) + valueTypes = tupleType->components(); + else + valueTypes = TypePointers{expression->annotation().type}; + auto const& assignments = _variableDeclarationStatement.annotation().assignments; + solAssert(assignments.size() == valueTypes.size(), ""); + for (size_t i = 0; i < assignments.size(); ++i) + { + size_t j = assignments.size() - i - 1; + VariableDeclaration const* varDecl = assignments[j]; + if (!varDecl) + utils.popStackElement(*valueTypes[j]); + else + { + utils.convertType(*valueTypes[j], *varDecl->annotation().type); + utils.moveToStackVariable(*varDecl); + } + } } checker.check(); return false; diff --git a/libsolidity/ExpressionCompiler.cpp b/libsolidity/ExpressionCompiler.cpp index c11ef29e..f6eed0de 100644 --- a/libsolidity/ExpressionCompiler.cpp +++ b/libsolidity/ExpressionCompiler.cpp @@ -427,11 +427,6 @@ bool ExpressionCompiler::visit(FunctionCall const& _functionCall) unsigned returnParametersSize = CompilerUtils::sizeOnStack(function.returnParameterTypes()); // callee adds return parameters, but removes arguments and return label m_context.adjustStackOffset(returnParametersSize - CompilerUtils::sizeOnStack(function.parameterTypes()) - 1); - - // @todo for now, the return value of a function is its first return value, so remove - // all others - for (unsigned i = 1; i < function.returnParameterTypes().size(); ++i) - utils().popStackElement(*function.returnParameterTypes()[i]); break; } case Location::External: @@ -1123,19 +1118,15 @@ void ExpressionCompiler::appendExternalFunctionCall( bool returnSuccessCondition = funKind == FunctionKind::Bare || funKind == FunctionKind::BareCallCode; bool isCallCode = funKind == FunctionKind::BareCallCode || funKind == FunctionKind::CallCode; - //@todo only return the first return value for now - Type const* firstReturnType = - _functionType.returnParameterTypes().empty() ? - nullptr : - _functionType.returnParameterTypes().front().get(); unsigned retSize = 0; if (returnSuccessCondition) retSize = 0; // return value actually is success condition - else if (firstReturnType) - { - retSize = firstReturnType->calldataEncodedSize(); - solAssert(retSize > 0, "Unable to return dynamic type from external call."); - } + else + for (auto const& retType: _functionType.returnParameterTypes()) + { + solAssert(retType->calldataEncodedSize() > 0, "Unable to return dynamic type from external call."); + retSize += retType->calldataEncodedSize(); + } // Evaluate arguments. TypePointers argumentTypes; @@ -1255,16 +1246,20 @@ void ExpressionCompiler::appendExternalFunctionCall( utils().loadFromMemoryDynamic(IntegerType(160), false, true, false); utils().convertType(IntegerType(160), FixedBytesType(20)); } - else if (firstReturnType) + else if (!_functionType.returnParameterTypes().empty()) { utils().fetchFreeMemoryPointer(); - if (dynamic_cast<ReferenceType const*>(firstReturnType)) + bool memoryNeeded = false; + for (auto const& retType: _functionType.returnParameterTypes()) { - utils().loadFromMemoryDynamic(*firstReturnType, false, true, true); - utils().storeFreeMemoryPointer(); + utils().loadFromMemoryDynamic(*retType, false, true, true); + if (dynamic_cast<ReferenceType const*>(retType.get())) + memoryNeeded = true; } + if (memoryNeeded) + utils().storeFreeMemoryPointer(); else - utils().loadFromMemoryDynamic(*firstReturnType, false, true, false); + m_context << eth::Instruction::POP; } } diff --git a/libsolidity/NameAndTypeResolver.cpp b/libsolidity/NameAndTypeResolver.cpp index 0ce20f3c..0d2c3b02 100644 --- a/libsolidity/NameAndTypeResolver.cpp +++ b/libsolidity/NameAndTypeResolver.cpp @@ -350,7 +350,9 @@ void DeclarationRegistrationHelper::endVisit(VariableDeclarationStatement& _vari // Register the local variables with the function // This does not fit here perfectly, but it saves us another AST visit. solAssert(m_currentFunction, "Variable declaration without function."); - m_currentFunction->addLocalVariable(_variableDeclarationStatement.declaration()); + for (ASTPointer<VariableDeclaration> const& var: _variableDeclarationStatement.declarations()) + if (var) + m_currentFunction->addLocalVariable(*var); } bool DeclarationRegistrationHelper::visit(VariableDeclaration& _declaration) diff --git a/libsolidity/Parser.cpp b/libsolidity/Parser.cpp index 94e9c0ea..88a6b030 100644 --- a/libsolidity/Parser.cpp +++ b/libsolidity/Parser.cpp @@ -771,13 +771,61 @@ ASTPointer<VariableDeclarationStatement> Parser::parseVariableDeclarationStateme ASTPointer<TypeName> const& _lookAheadArrayType ) { - VarDeclParserOptions options; - options.allowVar = true; - options.allowInitialValue = true; - options.allowLocationSpecifier = true; - ASTPointer<VariableDeclaration> variable = parseVariableDeclaration(options, _lookAheadArrayType); - ASTNodeFactory nodeFactory(*this, variable); - return nodeFactory.createNode<VariableDeclarationStatement>(variable); + ASTNodeFactory nodeFactory(*this); + if (_lookAheadArrayType) + nodeFactory.setLocation(_lookAheadArrayType->location()); + vector<ASTPointer<VariableDeclaration>> variables; + ASTPointer<Expression> value; + if ( + !_lookAheadArrayType && + m_scanner->currentToken() == Token::Var && + m_scanner->peekNextToken() == Token::LParen + ) + { + // Parse `var (a, b, ,, c) = ...` into a single VariableDeclarationStatement with multiple variables. + m_scanner->next(); + m_scanner->next(); + if (m_scanner->currentToken() != Token::RParen) + while (true) + { + ASTPointer<VariableDeclaration> var; + if ( + m_scanner->currentToken() != Token::Comma && + m_scanner->currentToken() != Token::RParen + ) + { + ASTNodeFactory varDeclNodeFactory(*this); + ASTPointer<ASTString> name = expectIdentifierToken(); + var = varDeclNodeFactory.createNode<VariableDeclaration>( + ASTPointer<TypeName>(), + name, + ASTPointer<Expression>(), + VariableDeclaration::Visibility::Default + ); + } + variables.push_back(var); + if (m_scanner->currentToken() == Token::RParen) + break; + else + expectToken(Token::Comma); + } + nodeFactory.markEndPosition(); + m_scanner->next(); + } + else + { + VarDeclParserOptions options; + options.allowVar = true; + options.allowLocationSpecifier = true; + variables.push_back(parseVariableDeclaration(options, _lookAheadArrayType)); + } + if (m_scanner->currentToken() == Token::Assign) + { + m_scanner->next(); + value = parseExpression(); + nodeFactory.setEndPositionFromNode(value); + } + return nodeFactory.createNode<VariableDeclarationStatement>(variables, value); } ASTPointer<ExpressionStatement> Parser::parseExpressionStatement( diff --git a/libsolidity/TypeChecker.cpp b/libsolidity/TypeChecker.cpp index 5bbbd072..afffe344 100644 --- a/libsolidity/TypeChecker.cpp +++ b/libsolidity/TypeChecker.cpp @@ -424,16 +424,17 @@ bool TypeChecker::visit(VariableDeclaration const& _variable) // Note that assignments before the first declaration are legal because of the special scoping // rules inherited from JavaScript. - // This only infers the type from its type name. - // If an explicit type is required, it throws, otherwise it returns TypePointer(); + // type is filled either by ReferencesResolver directly from the type name or by + // TypeChecker at the VariableDeclarationStatement level. TypePointer varType = _variable.annotation().type; + solAssert(!!varType, "Failed to infer variable type."); if (_variable.isConstant()) { if (!dynamic_cast<ContractDefinition const*>(_variable.scope())) typeError(_variable, "Illegal use of \"constant\" specifier."); if (!_variable.value()) typeError(_variable, "Uninitialized \"constant\" variable."); - if (varType && !varType->isValueType()) + if (!varType->isValueType()) { bool constImplemented = false; if (auto arrayType = dynamic_cast<ArrayType const*>(varType.get())) @@ -446,43 +447,8 @@ bool TypeChecker::visit(VariableDeclaration const& _variable) ); } } - if (varType) - { - if (_variable.value()) - expectType(*_variable.value(), *varType); - else - { - if (auto ref = dynamic_cast<ReferenceType const *>(varType.get())) - if (ref->dataStoredIn(DataLocation::Storage) && _variable.isLocalVariable() && !_variable.isCallableParameter()) - { - auto err = make_shared<Warning>(); - *err << - errinfo_sourceLocation(_variable.location()) << - errinfo_comment("Uninitialized storage pointer. Did you mean '<type> memory " + _variable.name() + "'?"); - m_errors.push_back(err); - } - } - } - else - { - // Infer type from value. - if (!_variable.value()) - fatalTypeError(_variable, "Assignment necessary for type detection."); - _variable.value()->accept(*this); - - TypePointer const& valueType = type(*_variable.value()); - solAssert(!!valueType, ""); - if ( - valueType->category() == Type::Category::IntegerConstant && - !dynamic_pointer_cast<IntegerConstantType const>(valueType)->integerType() - ) - fatalTypeError(*_variable.value(), "Invalid integer constant " + valueType->toString() + "."); - else if (valueType->category() == Type::Category::Void) - fatalTypeError(_variable, "Variable cannot have void type."); - varType = valueType->mobileType(); - } - solAssert(!!varType, ""); - _variable.annotation().type = varType; + if (_variable.value()) + expectType(*_variable.value(), *varType); if (!_variable.isStateVariable()) { if (varType->dataStoredIn(DataLocation::Memory) || varType->dataStoredIn(DataLocation::CallData)) @@ -621,6 +587,126 @@ void TypeChecker::endVisit(Return const& _return) } } +bool TypeChecker::visit(VariableDeclarationStatement const& _statement) +{ + if (!_statement.initialValue()) + { + // No initial value is only permitted for single variables with specified type. + if (_statement.declarations().size() != 1 || !_statement.declarations().front()) + fatalTypeError(_statement, "Assignment necessary for type detection."); + VariableDeclaration const& varDecl = *_statement.declarations().front(); + if (!varDecl.annotation().type) + fatalTypeError(_statement, "Assignment necessary for type detection."); + if (auto ref = dynamic_cast<ReferenceType const*>(varDecl.annotation().type.get())) + { + if (ref->dataStoredIn(DataLocation::Storage)) + { + auto err = make_shared<Warning>(); + *err << + errinfo_sourceLocation(varDecl.location()) << + errinfo_comment("Uninitialized storage pointer. Did you mean '<type> memory " + varDecl.name() + "'?"); + m_errors.push_back(err); + } + } + varDecl.accept(*this); + return false; + } + + // Here we have an initial value and might have to derive some types before we can visit + // the variable declaration(s). + + _statement.initialValue()->accept(*this); + TypePointers valueTypes; + if (auto tupleType = dynamic_cast<TupleType const*>(_statement.initialValue()->annotation().type.get())) + valueTypes = tupleType->components(); + else + valueTypes = TypePointers{_statement.initialValue()->annotation().type}; + + // Determine which component is assigned to which variable. + // If numbers do not match, fill up if variables begin or end empty (not both). + vector<VariableDeclaration const*>& assignments = _statement.annotation().assignments; + assignments.resize(valueTypes.size(), nullptr); + vector<ASTPointer<VariableDeclaration>> const& variables = _statement.declarations(); + if (variables.empty()) + { + if (!valueTypes.empty()) + fatalTypeError( + _statement, + "Too many components (" + + toString(valueTypes.size()) + + ") in value for variable assignment (0) needed" + ); + } + else if (valueTypes.size() != variables.size() && !variables.front() && !variables.back()) + fatalTypeError( + _statement, + "Wildcard both at beginning and end of variable declaration list is only allowed " + "if the number of components is equal." + ); + size_t minNumValues = variables.size(); + if (!variables.empty() && (!variables.back() || !variables.front())) + --minNumValues; + if (valueTypes.size() < minNumValues) + fatalTypeError( + _statement, + "Not enough components (" + + toString(valueTypes.size()) + + ") in value to assign all variables (" + + toString(minNumValues) + ")." + ); + if (valueTypes.size() > variables.size() && variables.front() && variables.back()) + fatalTypeError( + _statement, + "Too many components (" + + toString(valueTypes.size()) + + ") in value for variable assignment (" + + toString(minNumValues) + + " needed)." + ); + bool fillRight = !variables.empty() && (!variables.back() || variables.front()); + for (size_t i = 0; i < min(variables.size(), valueTypes.size()); ++i) + if (fillRight) + assignments[i] = variables[i].get(); + else + assignments[assignments.size() - i - 1] = variables[variables.size() - i - 1].get(); + + for (size_t i = 0; i < assignments.size(); ++i) + { + if (!assignments[i]) + continue; + VariableDeclaration const& var = *assignments[i]; + solAssert(!var.value(), "Value has to be tied to statement."); + TypePointer const& valueComponentType = valueTypes[i]; + solAssert(!!valueComponentType, ""); + if (!var.annotation().type) + { + // Infer type from value. + solAssert(!var.typeName(), ""); + if ( + valueComponentType->category() == Type::Category::IntegerConstant && + !dynamic_pointer_cast<IntegerConstantType const>(valueComponentType)->integerType() + ) + fatalTypeError(*_statement.initialValue(), "Invalid integer constant " + valueComponentType->toString() + "."); + var.annotation().type = valueComponentType->mobileType(); + var.accept(*this); + } + else + { + var.accept(*this); + if (!valueComponentType->isImplicitlyConvertibleTo(*var.annotation().type)) + typeError( + _statement, + "Type " + + valueComponentType->toString() + + " is not implicitly convertible to expected type " + + var.annotation().type->toString() + + "." + ); + } + } + return false; +} + void TypeChecker::endVisit(ExpressionStatement const& _statement) { if (type(_statement.expression())->category() == Type::Category::IntegerConstant) @@ -785,23 +871,14 @@ bool TypeChecker::visit(FunctionCall const& _functionCall) if (!functionType) { typeError(_functionCall, "Type is not callable"); - _functionCall.annotation().type = make_shared<VoidType>(); + _functionCall.annotation().type = make_shared<TupleType>(); return false; } + else if (functionType->returnParameterTypes().size() == 1) + _functionCall.annotation().type = functionType->returnParameterTypes().front(); else - { - // @todo actually the return type should be an anonymous struct, - // but we change it to the type of the first return value until we have anonymous - // structs and tuples - if (functionType->returnParameterTypes().empty()) - _functionCall.annotation().type = make_shared<VoidType>(); - else - _functionCall.annotation().type = functionType->returnParameterTypes().front(); - } + _functionCall.annotation().type = make_shared<TupleType>(functionType->returnParameterTypes()); - //@todo would be nice to create a struct type from the arguments - // and then ask if that is implicitly convertible to the struct represented by the - // function parameters TypePointers const& parameterTypes = functionType->parameterTypes(); if (!functionType->takesArbitraryParameters() && parameterTypes.size() != arguments.size()) { diff --git a/libsolidity/TypeChecker.h b/libsolidity/TypeChecker.h index 97262ed0..cb65d5a8 100644 --- a/libsolidity/TypeChecker.h +++ b/libsolidity/TypeChecker.h @@ -87,6 +87,7 @@ private: virtual bool visit(WhileStatement const& _whileStatement) override; virtual bool visit(ForStatement const& _forStatement) override; virtual void endVisit(Return const& _return) override; + virtual bool visit(VariableDeclarationStatement const& _variable) override; virtual void endVisit(ExpressionStatement const& _statement) override; virtual bool visit(Assignment const& _assignment) override; virtual void endVisit(BinaryOperation const& _operation) override; diff --git a/libsolidity/Types.cpp b/libsolidity/Types.cpp index f0c67bba..c800e288 100644 --- a/libsolidity/Types.cpp +++ b/libsolidity/Types.cpp @@ -223,7 +223,7 @@ TypePointer IntegerType::unaryOperatorResult(Token::Value _operator) const { // "delete" is ok for all integer types if (_operator == Token::Delete) - return make_shared<VoidType>(); + return make_shared<TupleType>(); // no further unary operators for addresses else if (isAddress()) return TypePointer(); @@ -562,7 +562,7 @@ TypePointer FixedBytesType::unaryOperatorResult(Token::Value _operator) const { // "delete" and "~" is okay for FixedBytesType if (_operator == Token::Delete) - return make_shared<VoidType>(); + return make_shared<TupleType>(); else if (_operator == Token::BitNot) return shared_from_this(); @@ -617,7 +617,7 @@ u256 BoolType::literalValue(Literal const* _literal) const TypePointer BoolType::unaryOperatorResult(Token::Value _operator) const { if (_operator == Token::Delete) - return make_shared<VoidType>(); + return make_shared<TupleType>(); return (_operator == Token::Not) ? shared_from_this() : TypePointer(); } @@ -658,7 +658,7 @@ bool ContractType::isExplicitlyConvertibleTo(Type const& _convertTo) const TypePointer ContractType::unaryOperatorResult(Token::Value _operator) const { - return _operator == Token::Delete ? make_shared<VoidType>() : TypePointer(); + return _operator == Token::Delete ? make_shared<TupleType>() : TypePointer(); } TypePointer ReferenceType::unaryOperatorResult(Token::Value _operator) const @@ -672,9 +672,9 @@ TypePointer ReferenceType::unaryOperatorResult(Token::Value _operator) const case DataLocation::CallData: return TypePointer(); case DataLocation::Memory: - return make_shared<VoidType>(); + return make_shared<TupleType>(); case DataLocation::Storage: - return m_isPointer ? TypePointer() : make_shared<VoidType>(); + return m_isPointer ? TypePointer() : make_shared<TupleType>(); default: solAssert(false, ""); } @@ -1175,7 +1175,7 @@ set<string> StructType::membersMissingInMemory() const TypePointer EnumType::unaryOperatorResult(Token::Value _operator) const { - return _operator == Token::Delete ? make_shared<VoidType>() : TypePointer(); + return _operator == Token::Delete ? make_shared<TupleType>() : TypePointer(); } bool EnumType::operator==(Type const& _other) const @@ -1222,6 +1222,41 @@ unsigned int EnumType::memberValue(ASTString const& _member) const BOOST_THROW_EXCEPTION(m_enum.createTypeError("Requested unknown enum value ." + _member)); } +bool TupleType::operator==(Type const& _other) const +{ + if (auto tupleType = dynamic_cast<TupleType const*>(&_other)) + return components() == tupleType->components(); + else + return false; +} + +string TupleType::toString(bool _short) const +{ + if (m_components.empty()) + return "tuple()"; + string str = "tuple("; + for (auto const& t: m_components) + str += t->toString(_short) + ", "; + str.resize(str.size() - 2); + return str + ")"; +} + +u256 TupleType::storageSize() const +{ + BOOST_THROW_EXCEPTION( + InternalCompilerError() << + errinfo_comment("Storage size of non-storable tuple type requested.") + ); +} + +unsigned TupleType::sizeOnStack() const +{ + unsigned size = 0; + for (auto const& t: m_components) + size += t->sizeOnStack(); + return size; +} + FunctionType::FunctionType(FunctionDefinition const& _function, bool _isInternal): m_location(_isInternal ? Location::Internal : Location::External), m_isConstant(_function.isDeclaredConst()), @@ -1647,13 +1682,6 @@ string MappingType::canonicalName(bool) const return "mapping(" + keyType()->canonicalName(false) + " => " + valueType()->canonicalName(false) + ")"; } -u256 VoidType::storageSize() const -{ - BOOST_THROW_EXCEPTION( - InternalCompilerError() - << errinfo_comment("Storage size of non-storable void type requested.")); -} - bool TypeType::operator==(Type const& _other) const { if (_other.category() != category()) diff --git a/libsolidity/Types.h b/libsolidity/Types.h index 7a65ca92..e73cd3cd 100644 --- a/libsolidity/Types.h +++ b/libsolidity/Types.h @@ -132,8 +132,8 @@ public: enum class Category { Integer, IntegerConstant, StringLiteral, Bool, Real, Array, - FixedBytes, Contract, Struct, Function, Enum, - Mapping, Void, TypeType, Modifier, Magic + FixedBytes, Contract, Struct, Function, Enum, Tuple, + Mapping, TypeType, Modifier, Magic }; /// @{ @@ -683,6 +683,28 @@ private: }; /** + * Type that can hold a finite sequence of values of different types. + */ +class TupleType: public Type +{ +public: + virtual Category category() const override { return Category::Tuple; } + explicit TupleType(std::vector<TypePointer> const& _types = std::vector<TypePointer>()): m_components(_types) {} + virtual bool operator==(Type const& _other) const override; + virtual TypePointer binaryOperatorResult(Token::Value, TypePointer const&) const override { return TypePointer(); } + virtual std::string toString(bool) const override; + virtual bool canBeStored() const override { return false; } + virtual u256 storageSize() const override; + virtual bool canLiveOutsideStorage() const override { return false; } + virtual unsigned sizeOnStack() const override; + + std::vector<TypePointer> const& components() const { return m_components; } + +private: + std::vector<TypePointer> const m_components; +}; + +/** * The type of a function, identified by its (return) parameter types. * @todo the return parameters should also have names, i.e. return parameters should be a struct * type. @@ -875,24 +897,6 @@ private: }; /** - * The void type, can only be implicitly used as the type that is returned by functions without - * return parameters. - */ -class VoidType: public Type -{ -public: - virtual Category category() const override { return Category::Void; } - VoidType() {} - - virtual TypePointer binaryOperatorResult(Token::Value, TypePointer const&) const override { return TypePointer(); } - virtual std::string toString(bool) const override { return "void"; } - virtual bool canBeStored() const override { return false; } - virtual u256 storageSize() const override; - virtual bool canLiveOutsideStorage() const override { return false; } - virtual unsigned sizeOnStack() const override { return 0; } -}; - -/** * The type of a type reference. The type of "uint32" when used in "a = uint32(2)" is an example * of a TypeType. * For super contracts or libraries, this has members directly. diff --git a/test/libsolidity/SolidityEndToEndTest.cpp b/test/libsolidity/SolidityEndToEndTest.cpp index 3126c1cc..08f62963 100644 --- a/test/libsolidity/SolidityEndToEndTest.cpp +++ b/test/libsolidity/SolidityEndToEndTest.cpp @@ -5613,6 +5613,30 @@ BOOST_AUTO_TEST_CASE(reject_ether_sent_to_library) BOOST_CHECK_EQUAL(m_state.balance(libraryAddress), 0); } +BOOST_AUTO_TEST_CASE(multi_variable_declaration) +{ + char const* sourceCode = R"( + contract C { + function g() returns (uint a, uint b, uint c) { + a = 1; b = 2; c = 3; + } + function f() returns (bool) { + var (x, y, z) = g(); + if (x != 1 || y != 2 || z != 3) return false; + var (, a,) = g(); + if (a != 2) return false; + var (b,) = g(); + if (b != 1) return false; + var (,c) = g(); + if (c != 3) return false; + return true; + } + } + )"; + compileAndRun(sourceCode); + BOOST_CHECK(callContractFunction("f()", encodeArgs()) == encodeArgs(true)); +} + BOOST_AUTO_TEST_SUITE_END() } diff --git a/test/libsolidity/SolidityNameAndTypeResolution.cpp b/test/libsolidity/SolidityNameAndTypeResolution.cpp index b55c92f0..cc990c1e 100644 --- a/test/libsolidity/SolidityNameAndTypeResolution.cpp +++ b/test/libsolidity/SolidityNameAndTypeResolution.cpp @@ -1285,7 +1285,7 @@ BOOST_AUTO_TEST_CASE(empty_name_return_parameter_with_named_one) BOOST_AUTO_TEST_CASE(disallow_declaration_of_void_type) { - char const* sourceCode = "contract c { function f() { var x = f(); } }"; + char const* sourceCode = "contract c { function f() { var (x) = f(); } }"; SOLIDITY_CHECK_ERROR_TYPE(parseAndAnalyseReturnError(sourceCode), TypeError); } @@ -2134,7 +2134,7 @@ BOOST_AUTO_TEST_CASE(dynamic_return_types_not_possible) contract C { function f(uint) returns (string); function g() { - var x = this.f(2); + var (x,) = this.f(2); } } )"; @@ -2397,6 +2397,99 @@ BOOST_AUTO_TEST_CASE(cyclic_binary_dependency_via_inheritance) SOLIDITY_CHECK_ERROR_TYPE(parseAndAnalyseReturnError(text), TypeError); } +BOOST_AUTO_TEST_CASE(multi_variable_declaration_fail) +{ + char const* text = R"( + contract C { function f() { var (x,y); } } + )"; + SOLIDITY_CHECK_ERROR_TYPE(parseAndAnalyseReturnError(text), TypeError); +} + +BOOST_AUTO_TEST_CASE(multi_variable_declaration_wildcards_fine) +{ + char const* text = R"( + contract C { + function three() returns (uint, uint, uint); + function two() returns (uint, uint); + function none(); + function f() { + var (a,) = three(); + var (b,c,) = two(); + var (,d) = three(); + var (,e,g) = two(); + var (,,) = three(); + var () = none(); + } + } + )"; + BOOST_CHECK_NO_THROW(parseAndAnalyse(text)); +} + +BOOST_AUTO_TEST_CASE(multi_variable_declaration_wildcards_fail_1) +{ + char const* text = R"( + contract C { + function one() returns (uint); + function f() { var (a, b, ) = one(); } + } + )"; + SOLIDITY_CHECK_ERROR_TYPE(parseAndAnalyseReturnError(text), TypeError); +} +BOOST_AUTO_TEST_CASE(multi_variable_declaration_wildcards_fail_2) +{ + char const* text = R"( + contract C { + function one() returns (uint); + function f() { var (a, , ) = one(); } + } + )"; + SOLIDITY_CHECK_ERROR_TYPE(parseAndAnalyseReturnError(text), TypeError); +} + +BOOST_AUTO_TEST_CASE(multi_variable_declaration_wildcards_fail_3) +{ + char const* text = R"( + contract C { + function one() returns (uint); + function f() { var (, , a) = one(); } + } + )"; + SOLIDITY_CHECK_ERROR_TYPE(parseAndAnalyseReturnError(text), TypeError); +} + +BOOST_AUTO_TEST_CASE(multi_variable_declaration_wildcards_fail_4) +{ + char const* text = R"( + contract C { + function one() returns (uint); + function f() { var (, a, b) = one(); } + } + )"; + SOLIDITY_CHECK_ERROR_TYPE(parseAndAnalyseReturnError(text), TypeError); +} + +BOOST_AUTO_TEST_CASE(multi_variable_declaration_wildcards_fail_5) +{ + char const* text = R"( + contract C { + function one() returns (uint); + function f() { var (,) = one(); } + } + )"; + SOLIDITY_CHECK_ERROR_TYPE(parseAndAnalyseReturnError(text), TypeError); +} + +BOOST_AUTO_TEST_CASE(multi_variable_declaration_wildcards_fail_6) +{ + char const* text = R"( + contract C { + function two() returns (uint, uint); + function f() { var (a, b, c) = two(); } + } + )"; + SOLIDITY_CHECK_ERROR_TYPE(parseAndAnalyseReturnError(text), TypeError); +} + BOOST_AUTO_TEST_SUITE_END() } diff --git a/test/libsolidity/SolidityParser.cpp b/test/libsolidity/SolidityParser.cpp index 1e034863..03930479 100644 --- a/test/libsolidity/SolidityParser.cpp +++ b/test/libsolidity/SolidityParser.cpp @@ -934,6 +934,26 @@ BOOST_AUTO_TEST_CASE(library_simple) BOOST_CHECK_NO_THROW(parseText(text)); } +BOOST_AUTO_TEST_CASE(multi_variable_declaration) +{ + char const* text = R"( + library Lib { + function f() { + var (a,b,c) = g(); + var (d) = 2; + var (,e) = 3; + var (f,) = 4; + var (x,,) = g(); + var (,y,) = g(); + var () = g(); + var (,,) = g(); + } + function g() returns (uint, uint, uint) {} + } + )"; + BOOST_CHECK_NO_THROW(parseText(text)); +} + BOOST_AUTO_TEST_SUITE_END() } |