mirror of
https://github.com/ethereum/solidity
synced 2023-10-03 13:03:40 +00:00
Multi-variable declarations.
This commit is contained in:
parent
a5d12b8761
commit
deebc7e860
@ -325,6 +325,13 @@ ReturnAnnotation& Return::annotation() const
|
|||||||
return static_cast<ReturnAnnotation&>(*m_annotation);
|
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
|
ExpressionAnnotation& Expression::annotation() const
|
||||||
{
|
{
|
||||||
if (!m_annotation)
|
if (!m_annotation)
|
||||||
|
@ -982,6 +982,8 @@ public:
|
|||||||
virtual void accept(ASTVisitor& _visitor) override;
|
virtual void accept(ASTVisitor& _visitor) override;
|
||||||
virtual void accept(ASTConstVisitor& _visitor) const override;
|
virtual void accept(ASTConstVisitor& _visitor) const override;
|
||||||
|
|
||||||
|
VariableDeclarationStatementAnnotation& annotation() const override;
|
||||||
|
|
||||||
std::vector<ASTPointer<VariableDeclaration>> const& declarations() const { return m_variables; }
|
std::vector<ASTPointer<VariableDeclaration>> const& declarations() const { return m_variables; }
|
||||||
Expression const* initialValue() const { return m_initialValue.get(); }
|
Expression const* initialValue() const { return m_initialValue.get(); }
|
||||||
|
|
||||||
|
@ -84,6 +84,13 @@ struct UserDefinedTypeNameAnnotation: TypeNameAnnotation
|
|||||||
Declaration const* referencedDeclaration = nullptr;
|
Declaration const* referencedDeclaration = nullptr;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
struct VariableDeclarationStatementAnnotation: ASTAnnotation
|
||||||
|
{
|
||||||
|
/// Information about which component of the vaule 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
|
struct ExpressionAnnotation: ASTAnnotation
|
||||||
{
|
{
|
||||||
/// Inferred type of the expression.
|
/// Inferred type of the expression.
|
||||||
|
@ -623,13 +623,29 @@ bool Compiler::visit(VariableDeclarationStatement const& _variableDeclarationSta
|
|||||||
{
|
{
|
||||||
StackHeightChecker checker(m_context);
|
StackHeightChecker checker(m_context);
|
||||||
CompilerContext::LocationSetter locationSetter(m_context, _variableDeclarationStatement);
|
CompilerContext::LocationSetter locationSetter(m_context, _variableDeclarationStatement);
|
||||||
solAssert(_variableDeclarationStatement.declarations().size() == 1, "To be implemented.");
|
|
||||||
solAssert(!!_variableDeclarationStatement.declarations().front(), "");
|
|
||||||
VariableDeclaration const& varDecl = *_variableDeclarationStatement.declarations().front();
|
|
||||||
if (Expression const* expression = _variableDeclarationStatement.initialValue())
|
if (Expression const* expression = _variableDeclarationStatement.initialValue())
|
||||||
{
|
{
|
||||||
compileExpression(*expression, varDecl.annotation().type);
|
CompilerUtils utils(m_context);
|
||||||
CompilerUtils(m_context).moveToStackVariable(varDecl);
|
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();
|
checker.check();
|
||||||
return false;
|
return false;
|
||||||
|
@ -427,11 +427,6 @@ bool ExpressionCompiler::visit(FunctionCall const& _functionCall)
|
|||||||
unsigned returnParametersSize = CompilerUtils::sizeOnStack(function.returnParameterTypes());
|
unsigned returnParametersSize = CompilerUtils::sizeOnStack(function.returnParameterTypes());
|
||||||
// callee adds return parameters, but removes arguments and return label
|
// callee adds return parameters, but removes arguments and return label
|
||||||
m_context.adjustStackOffset(returnParametersSize - CompilerUtils::sizeOnStack(function.parameterTypes()) - 1);
|
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;
|
break;
|
||||||
}
|
}
|
||||||
case Location::External:
|
case Location::External:
|
||||||
@ -1123,19 +1118,15 @@ void ExpressionCompiler::appendExternalFunctionCall(
|
|||||||
bool returnSuccessCondition = funKind == FunctionKind::Bare || funKind == FunctionKind::BareCallCode;
|
bool returnSuccessCondition = funKind == FunctionKind::Bare || funKind == FunctionKind::BareCallCode;
|
||||||
bool isCallCode = funKind == FunctionKind::BareCallCode || funKind == FunctionKind::CallCode;
|
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;
|
unsigned retSize = 0;
|
||||||
if (returnSuccessCondition)
|
if (returnSuccessCondition)
|
||||||
retSize = 0; // return value actually is success condition
|
retSize = 0; // return value actually is success condition
|
||||||
else if (firstReturnType)
|
else
|
||||||
{
|
for (auto const& retType: _functionType.returnParameterTypes())
|
||||||
retSize = firstReturnType->calldataEncodedSize();
|
{
|
||||||
solAssert(retSize > 0, "Unable to return dynamic type from external call.");
|
solAssert(retType->calldataEncodedSize() > 0, "Unable to return dynamic type from external call.");
|
||||||
}
|
retSize += retType->calldataEncodedSize();
|
||||||
|
}
|
||||||
|
|
||||||
// Evaluate arguments.
|
// Evaluate arguments.
|
||||||
TypePointers argumentTypes;
|
TypePointers argumentTypes;
|
||||||
@ -1255,16 +1246,20 @@ void ExpressionCompiler::appendExternalFunctionCall(
|
|||||||
utils().loadFromMemoryDynamic(IntegerType(160), false, true, false);
|
utils().loadFromMemoryDynamic(IntegerType(160), false, true, false);
|
||||||
utils().convertType(IntegerType(160), FixedBytesType(20));
|
utils().convertType(IntegerType(160), FixedBytesType(20));
|
||||||
}
|
}
|
||||||
else if (firstReturnType)
|
else if (!_functionType.returnParameterTypes().empty())
|
||||||
{
|
{
|
||||||
utils().fetchFreeMemoryPointer();
|
utils().fetchFreeMemoryPointer();
|
||||||
if (dynamic_cast<ReferenceType const*>(firstReturnType))
|
bool memoryNeeded = false;
|
||||||
|
for (auto const& retType: _functionType.returnParameterTypes())
|
||||||
{
|
{
|
||||||
utils().loadFromMemoryDynamic(*firstReturnType, false, true, true);
|
utils().loadFromMemoryDynamic(*retType, false, true, true);
|
||||||
utils().storeFreeMemoryPointer();
|
if (dynamic_cast<ReferenceType const*>(retType.get()))
|
||||||
|
memoryNeeded = true;
|
||||||
}
|
}
|
||||||
|
if (memoryNeeded)
|
||||||
|
utils().storeFreeMemoryPointer();
|
||||||
else
|
else
|
||||||
utils().loadFromMemoryDynamic(*firstReturnType, false, true, false);
|
m_context << eth::Instruction::POP;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -785,12 +785,13 @@ ASTPointer<VariableDeclarationStatement> Parser::parseVariableDeclarationStateme
|
|||||||
// Parse `var (a, b, ,, c) = ...` into a single VariableDeclarationStatement with multiple variables.
|
// Parse `var (a, b, ,, c) = ...` into a single VariableDeclarationStatement with multiple variables.
|
||||||
m_scanner->next();
|
m_scanner->next();
|
||||||
m_scanner->next();
|
m_scanner->next();
|
||||||
do
|
while (true)
|
||||||
{
|
{
|
||||||
ASTPointer<VariableDeclaration> var;
|
ASTPointer<VariableDeclaration> var;
|
||||||
if (m_scanner->currentToken() == Token::Comma)
|
if (
|
||||||
m_scanner->next();
|
m_scanner->currentToken() != Token::Comma &&
|
||||||
else
|
m_scanner->currentToken() != Token::RParen
|
||||||
|
)
|
||||||
{
|
{
|
||||||
ASTNodeFactory varDeclNodeFactory(*this);
|
ASTNodeFactory varDeclNodeFactory(*this);
|
||||||
ASTPointer<ASTString> name = expectIdentifierToken();
|
ASTPointer<ASTString> name = expectIdentifierToken();
|
||||||
@ -802,7 +803,11 @@ ASTPointer<VariableDeclarationStatement> Parser::parseVariableDeclarationStateme
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
variables.push_back(var);
|
variables.push_back(var);
|
||||||
} while (m_scanner->currentToken() != Token::RParen);
|
if (m_scanner->currentToken() == Token::RParen)
|
||||||
|
break;
|
||||||
|
else
|
||||||
|
expectToken(Token::Comma);
|
||||||
|
}
|
||||||
nodeFactory.markEndPosition();
|
nodeFactory.markEndPosition();
|
||||||
m_scanner->next();
|
m_scanner->next();
|
||||||
}
|
}
|
||||||
|
@ -617,47 +617,57 @@ bool TypeChecker::visit(VariableDeclarationStatement const& _statement)
|
|||||||
// the variable declaration(s).
|
// the variable declaration(s).
|
||||||
|
|
||||||
_statement.initialValue()->accept(*this);
|
_statement.initialValue()->accept(*this);
|
||||||
shared_ptr<TupleType const> valueType = dynamic_pointer_cast<TupleType const>(_statement.initialValue()->annotation().type);
|
TypePointers valueTypes;
|
||||||
if (!valueType)
|
if (auto tupleType = dynamic_cast<TupleType const*>(_statement.initialValue()->annotation().type.get()))
|
||||||
valueType = make_shared<TupleType const>(TypePointers{_statement.initialValue()->annotation().type});
|
valueTypes = tupleType->components();
|
||||||
|
else
|
||||||
|
valueTypes = TypePointers{_statement.initialValue()->annotation().type};
|
||||||
|
|
||||||
vector<ASTPointer<VariableDeclaration>> variables = _statement.declarations();
|
// Determine which component is assigned to which variable.
|
||||||
// If numbers do not match, fill up if variables begin or end empty (not both).
|
// If numbers do not match, fill up if variables begin or end empty (not both).
|
||||||
if (valueType->components().size() != variables.size())
|
vector<VariableDeclaration const*>& assignments = _statement.annotation().assignments;
|
||||||
{
|
assignments.resize(valueTypes.size(), nullptr);
|
||||||
if (!variables.front() && !variables.back())
|
vector<ASTPointer<VariableDeclaration>> const& variables = _statement.declarations();
|
||||||
fatalTypeError(
|
if (valueTypes.size() != variables.size() && !variables.front() && !variables.back())
|
||||||
_statement,
|
fatalTypeError(
|
||||||
"Wildcard both at beginning and end of variable declaration list is only allowed "
|
_statement,
|
||||||
"if the number of components is equal."
|
"Wildcard both at beginning and end of variable declaration list is only allowed "
|
||||||
);
|
"if the number of components is equal."
|
||||||
while (valueType->components().size() > variables.size())
|
);
|
||||||
if (!variables.front())
|
size_t minNumValues = variables.size();
|
||||||
variables.insert(variables.begin(), shared_ptr<VariableDeclaration>());
|
if (!variables.back() || !variables.front())
|
||||||
else
|
--minNumValues;
|
||||||
variables.push_back(shared_ptr<VariableDeclaration>());
|
if (valueTypes.size() < minNumValues)
|
||||||
while (valueType->components().size() < variables.size())
|
fatalTypeError(
|
||||||
if (!variables.empty() && !variables.front())
|
_statement,
|
||||||
variables.erase(variables.begin());
|
"Not enough components (" +
|
||||||
else if (!variables.empty() && !variables.back())
|
toString(valueTypes.size()) +
|
||||||
variables.pop_back();
|
") in value to assign all variables (" +
|
||||||
else
|
toString(minNumValues) + ")."
|
||||||
break;
|
);
|
||||||
if (valueType->components().size() != variables.size())
|
if (valueTypes.size() > variables.size() && variables.front() && variables.back())
|
||||||
fatalTypeError(
|
fatalTypeError(
|
||||||
_statement,
|
_statement,
|
||||||
"Unable to match the number of variables to the number of values."
|
"Too many components (" +
|
||||||
);
|
toString(valueTypes.size()) +
|
||||||
}
|
") in value for variable assignment (" +
|
||||||
solAssert(variables.size() == valueType->components().size(), "");
|
toString(minNumValues) +
|
||||||
|
" needed)."
|
||||||
|
);
|
||||||
|
bool fillRight = (!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 < variables.size(); ++i)
|
for (size_t i = 0; i < assignments.size(); ++i)
|
||||||
{
|
{
|
||||||
if (!variables[i])
|
if (!assignments[i])
|
||||||
continue;
|
continue;
|
||||||
VariableDeclaration const& var = *variables[i];
|
VariableDeclaration const& var = *assignments[i];
|
||||||
solAssert(!var.value(), "Value has to be tied to statement.");
|
solAssert(!var.value(), "Value has to be tied to statement.");
|
||||||
TypePointer const& valueComponentType = valueType->components()[i];
|
TypePointer const& valueComponentType = valueTypes[i];
|
||||||
solAssert(!!valueComponentType, "");
|
solAssert(!!valueComponentType, "");
|
||||||
if (!var.annotation().type)
|
if (!var.annotation().type)
|
||||||
{
|
{
|
||||||
|
@ -2405,6 +2405,65 @@ BOOST_AUTO_TEST_CASE(multi_variable_declaration_fail)
|
|||||||
SOLIDITY_CHECK_ERROR_TYPE(parseAndAnalyseReturnError(text), TypeError);
|
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 f() {
|
||||||
|
var (a,) = three();
|
||||||
|
var (b,c,) = two();
|
||||||
|
var (,d) = three();
|
||||||
|
var (,e,g) = two();
|
||||||
|
}
|
||||||
|
)";
|
||||||
|
BOOST_CHECK_NO_THROW(parseAndAnalyseReturnError(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_SUITE_END()
|
BOOST_AUTO_TEST_SUITE_END()
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -952,18 +952,6 @@ BOOST_AUTO_TEST_CASE(multi_variable_declaration)
|
|||||||
BOOST_CHECK_NO_THROW(parseText(text));
|
BOOST_CHECK_NO_THROW(parseText(text));
|
||||||
}
|
}
|
||||||
|
|
||||||
BOOST_AUTO_TEST_CASE(multi_variable_declaration_invalid)
|
|
||||||
{
|
|
||||||
char const* text = R"(
|
|
||||||
library Lib {
|
|
||||||
function f() {
|
|
||||||
var () = g();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
)";
|
|
||||||
BOOST_CHECK_THROW(parseText(text), ParserError);
|
|
||||||
}
|
|
||||||
|
|
||||||
BOOST_AUTO_TEST_SUITE_END()
|
BOOST_AUTO_TEST_SUITE_END()
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user