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);
|
||||
}
|
||||
|
||||
VariableDeclarationStatementAnnotation& VariableDeclarationStatement::annotation() const
|
||||
{
|
||||
if (!m_annotation)
|
||||
m_annotation = new VariableDeclarationStatementAnnotation();
|
||||
return static_cast<VariableDeclarationStatementAnnotation&>(*m_annotation);
|
||||
}
|
||||
|
||||
ExpressionAnnotation& Expression::annotation() const
|
||||
{
|
||||
if (!m_annotation)
|
||||
|
@ -982,6 +982,8 @@ public:
|
||||
virtual void accept(ASTVisitor& _visitor) override;
|
||||
virtual void accept(ASTConstVisitor& _visitor) const override;
|
||||
|
||||
VariableDeclarationStatementAnnotation& annotation() const override;
|
||||
|
||||
std::vector<ASTPointer<VariableDeclaration>> const& declarations() const { return m_variables; }
|
||||
Expression const* initialValue() const { return m_initialValue.get(); }
|
||||
|
||||
|
@ -84,6 +84,13 @@ struct UserDefinedTypeNameAnnotation: TypeNameAnnotation
|
||||
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
|
||||
{
|
||||
/// Inferred type of the expression.
|
||||
|
@ -623,13 +623,29 @@ bool Compiler::visit(VariableDeclarationStatement const& _variableDeclarationSta
|
||||
{
|
||||
StackHeightChecker checker(m_context);
|
||||
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())
|
||||
{
|
||||
compileExpression(*expression, varDecl.annotation().type);
|
||||
CompilerUtils(m_context).moveToStackVariable(varDecl);
|
||||
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;
|
||||
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -785,12 +785,13 @@ ASTPointer<VariableDeclarationStatement> Parser::parseVariableDeclarationStateme
|
||||
// Parse `var (a, b, ,, c) = ...` into a single VariableDeclarationStatement with multiple variables.
|
||||
m_scanner->next();
|
||||
m_scanner->next();
|
||||
do
|
||||
while (true)
|
||||
{
|
||||
ASTPointer<VariableDeclaration> var;
|
||||
if (m_scanner->currentToken() == Token::Comma)
|
||||
m_scanner->next();
|
||||
else
|
||||
if (
|
||||
m_scanner->currentToken() != Token::Comma &&
|
||||
m_scanner->currentToken() != Token::RParen
|
||||
)
|
||||
{
|
||||
ASTNodeFactory varDeclNodeFactory(*this);
|
||||
ASTPointer<ASTString> name = expectIdentifierToken();
|
||||
@ -802,7 +803,11 @@ ASTPointer<VariableDeclarationStatement> Parser::parseVariableDeclarationStateme
|
||||
);
|
||||
}
|
||||
variables.push_back(var);
|
||||
} while (m_scanner->currentToken() != Token::RParen);
|
||||
if (m_scanner->currentToken() == Token::RParen)
|
||||
break;
|
||||
else
|
||||
expectToken(Token::Comma);
|
||||
}
|
||||
nodeFactory.markEndPosition();
|
||||
m_scanner->next();
|
||||
}
|
||||
|
@ -617,47 +617,57 @@ bool TypeChecker::visit(VariableDeclarationStatement const& _statement)
|
||||
// the variable declaration(s).
|
||||
|
||||
_statement.initialValue()->accept(*this);
|
||||
shared_ptr<TupleType const> valueType = dynamic_pointer_cast<TupleType const>(_statement.initialValue()->annotation().type);
|
||||
if (!valueType)
|
||||
valueType = make_shared<TupleType const>(TypePointers{_statement.initialValue()->annotation().type});
|
||||
TypePointers valueTypes;
|
||||
if (auto tupleType = dynamic_cast<TupleType const*>(_statement.initialValue()->annotation().type.get()))
|
||||
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 (valueType->components().size() != variables.size())
|
||||
{
|
||||
if (!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."
|
||||
);
|
||||
while (valueType->components().size() > variables.size())
|
||||
if (!variables.front())
|
||||
variables.insert(variables.begin(), shared_ptr<VariableDeclaration>());
|
||||
else
|
||||
variables.push_back(shared_ptr<VariableDeclaration>());
|
||||
while (valueType->components().size() < variables.size())
|
||||
if (!variables.empty() && !variables.front())
|
||||
variables.erase(variables.begin());
|
||||
else if (!variables.empty() && !variables.back())
|
||||
variables.pop_back();
|
||||
else
|
||||
break;
|
||||
if (valueType->components().size() != variables.size())
|
||||
fatalTypeError(
|
||||
_statement,
|
||||
"Unable to match the number of variables to the number of values."
|
||||
);
|
||||
}
|
||||
solAssert(variables.size() == valueType->components().size(), "");
|
||||
vector<VariableDeclaration const*>& assignments = _statement.annotation().assignments;
|
||||
assignments.resize(valueTypes.size(), nullptr);
|
||||
vector<ASTPointer<VariableDeclaration>> const& variables = _statement.declarations();
|
||||
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.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.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;
|
||||
VariableDeclaration const& var = *variables[i];
|
||||
VariableDeclaration const& var = *assignments[i];
|
||||
solAssert(!var.value(), "Value has to be tied to statement.");
|
||||
TypePointer const& valueComponentType = valueType->components()[i];
|
||||
TypePointer const& valueComponentType = valueTypes[i];
|
||||
solAssert(!!valueComponentType, "");
|
||||
if (!var.annotation().type)
|
||||
{
|
||||
|
@ -2405,6 +2405,65 @@ BOOST_AUTO_TEST_CASE(multi_variable_declaration_fail)
|
||||
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()
|
||||
|
||||
}
|
||||
|
@ -952,18 +952,6 @@ BOOST_AUTO_TEST_CASE(multi_variable_declaration)
|
||||
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()
|
||||
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user