mirror of
https://github.com/ethereum/solidity
synced 2023-10-03 13:03:40 +00:00
Revert statement.
This commit is contained in:
@@ -86,6 +86,16 @@ void PostTypeChecker::endVisit(EmitStatement const& _emit)
|
||||
callEndVisit(_emit);
|
||||
}
|
||||
|
||||
bool PostTypeChecker::visit(RevertStatement const& _revert)
|
||||
{
|
||||
return callVisit(_revert);
|
||||
}
|
||||
|
||||
void PostTypeChecker::endVisit(RevertStatement const& _revert)
|
||||
{
|
||||
callEndVisit(_revert);
|
||||
}
|
||||
|
||||
bool PostTypeChecker::visit(FunctionCall const& _functionCall)
|
||||
{
|
||||
return callVisit(_functionCall);
|
||||
@@ -281,42 +291,59 @@ private:
|
||||
bool m_insideModifierInvocation = false;
|
||||
};
|
||||
|
||||
struct EventOutsideEmitChecker: public PostTypeChecker::Checker
|
||||
struct EventOutsideEmitErrorOutsideRevertChecker: public PostTypeChecker::Checker
|
||||
{
|
||||
EventOutsideEmitChecker(ErrorReporter& _errorReporter):
|
||||
EventOutsideEmitErrorOutsideRevertChecker(ErrorReporter& _errorReporter):
|
||||
Checker(_errorReporter) {}
|
||||
|
||||
bool visit(EmitStatement const&) override
|
||||
bool visit(EmitStatement const& _emitStatement) override
|
||||
{
|
||||
m_insideEmitStatement = true;
|
||||
m_currentStatement = &_emitStatement;
|
||||
return true;
|
||||
}
|
||||
|
||||
void endVisit(EmitStatement const&) override
|
||||
{
|
||||
m_insideEmitStatement = true;
|
||||
m_currentStatement = nullptr;
|
||||
}
|
||||
|
||||
bool visit(RevertStatement const& _revertStatement) override
|
||||
{
|
||||
m_currentStatement = &_revertStatement;
|
||||
return true;
|
||||
}
|
||||
|
||||
void endVisit(RevertStatement const&) override
|
||||
{
|
||||
m_currentStatement = nullptr;
|
||||
}
|
||||
|
||||
bool visit(FunctionCall const& _functionCall) override
|
||||
{
|
||||
if (*_functionCall.annotation().kind != FunctionCallKind::FunctionCall)
|
||||
return true;
|
||||
|
||||
if (FunctionTypePointer const functionType = dynamic_cast<FunctionTypePointer const>(_functionCall.expression().annotation().type))
|
||||
// Check for event outside of emit statement
|
||||
if (!m_insideEmitStatement && functionType->kind() == FunctionType::Kind::Event)
|
||||
m_errorReporter.typeError(
|
||||
3132_error,
|
||||
_functionCall.location(),
|
||||
"Event invocations have to be prefixed by \"emit\"."
|
||||
);
|
||||
if (*_functionCall.annotation().kind == FunctionCallKind::FunctionCall)
|
||||
if (auto const* functionType = dynamic_cast<FunctionType const*>(_functionCall.expression().annotation().type))
|
||||
{
|
||||
// Check for event outside of emit statement
|
||||
if (!dynamic_cast<EmitStatement const*>(m_currentStatement) && functionType->kind() == FunctionType::Kind::Event)
|
||||
m_errorReporter.typeError(
|
||||
3132_error,
|
||||
_functionCall.location(),
|
||||
"Event invocations have to be prefixed by \"emit\"."
|
||||
);
|
||||
else if (!dynamic_cast<RevertStatement const*>(m_currentStatement) && functionType->kind() == FunctionType::Kind::Error)
|
||||
m_errorReporter.typeError(
|
||||
7757_error,
|
||||
_functionCall.location(),
|
||||
"Errors can only be used with revert statements: \"revert MyError();\"."
|
||||
);
|
||||
}
|
||||
m_currentStatement = nullptr;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
private:
|
||||
/// Flag indicating whether we are currently inside an EmitStatement.
|
||||
bool m_insideEmitStatement = false;
|
||||
Statement const* m_currentStatement = nullptr;
|
||||
};
|
||||
|
||||
struct NoVariablesInInterfaceChecker: public PostTypeChecker::Checker
|
||||
@@ -395,14 +422,16 @@ struct ReservedErrorSelector: public PostTypeChecker::Checker
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
|
||||
PostTypeChecker::PostTypeChecker(langutil::ErrorReporter& _errorReporter): m_errorReporter(_errorReporter)
|
||||
{
|
||||
m_checkers.push_back(make_shared<ConstStateVarCircularReferenceChecker>(_errorReporter));
|
||||
m_checkers.push_back(make_shared<OverrideSpecifierChecker>(_errorReporter));
|
||||
m_checkers.push_back(make_shared<ModifierContextChecker>(_errorReporter));
|
||||
m_checkers.push_back(make_shared<EventOutsideEmitChecker>(_errorReporter));
|
||||
m_checkers.push_back(make_shared<EventOutsideEmitErrorOutsideRevertChecker>(_errorReporter));
|
||||
m_checkers.push_back(make_shared<NoVariablesInInterfaceChecker>(_errorReporter));
|
||||
m_checkers.push_back(make_shared<ReservedErrorSelector>(_errorReporter));
|
||||
}
|
||||
|
||||
@@ -83,6 +83,9 @@ private:
|
||||
bool visit(EmitStatement const& _emit) override;
|
||||
void endVisit(EmitStatement const& _emit) override;
|
||||
|
||||
bool visit(RevertStatement const& _revert) override;
|
||||
void endVisit(RevertStatement const& _revert) override;
|
||||
|
||||
bool visit(FunctionCall const& _functionCall) override;
|
||||
|
||||
bool visit(Identifier const& _identifier) override;
|
||||
|
||||
@@ -1167,6 +1167,18 @@ void TypeChecker::endVisit(EmitStatement const& _emit)
|
||||
m_errorReporter.typeError(9292_error, _emit.eventCall().expression().location(), "Expression has to be an event invocation.");
|
||||
}
|
||||
|
||||
void TypeChecker::endVisit(RevertStatement const& _revert)
|
||||
{
|
||||
FunctionCall const& errorCall = _revert.errorCall();
|
||||
if (
|
||||
*errorCall.annotation().kind != FunctionCallKind::FunctionCall ||
|
||||
type(errorCall.expression())->category() != Type::Category::Function ||
|
||||
dynamic_cast<FunctionType const&>(*type(errorCall.expression())).kind() != FunctionType::Kind::Error
|
||||
)
|
||||
m_errorReporter.typeError(1885_error, errorCall.expression().location(), "Expression has to be an error.");
|
||||
}
|
||||
|
||||
|
||||
bool TypeChecker::visit(VariableDeclarationStatement const& _statement)
|
||||
{
|
||||
if (!_statement.initialValue())
|
||||
|
||||
@@ -134,6 +134,7 @@ private:
|
||||
bool visit(ForStatement const& _forStatement) override;
|
||||
void endVisit(Return const& _return) override;
|
||||
void endVisit(EmitStatement const& _emit) override;
|
||||
void endVisit(RevertStatement const& _revert) override;
|
||||
bool visit(VariableDeclarationStatement const& _variable) override;
|
||||
void endVisit(ExpressionStatement const& _statement) override;
|
||||
bool visit(Conditional const& _conditional) override;
|
||||
|
||||
@@ -1726,6 +1726,31 @@ public:
|
||||
void accept(ASTConstVisitor& _visitor) const override;
|
||||
};
|
||||
|
||||
/**
|
||||
* The revert statement is used to revert state changes and return error data.
|
||||
*/
|
||||
class RevertStatement: public Statement
|
||||
{
|
||||
public:
|
||||
explicit RevertStatement(
|
||||
int64_t _id,
|
||||
SourceLocation const& _location,
|
||||
ASTPointer<ASTString> const& _docString,
|
||||
ASTPointer<FunctionCall> _functionCall
|
||||
):
|
||||
Statement(_id, _location, _docString), m_errorCall(std::move(_functionCall))
|
||||
{
|
||||
solAssert(m_errorCall, "");
|
||||
}
|
||||
void accept(ASTVisitor& _visitor) override;
|
||||
void accept(ASTConstVisitor& _visitor) const override;
|
||||
|
||||
FunctionCall const& errorCall() const { return *m_errorCall; }
|
||||
|
||||
private:
|
||||
ASTPointer<FunctionCall> m_errorCall;
|
||||
};
|
||||
|
||||
/**
|
||||
* The emit statement is used to emit events: emit EventName(arg1, ..., argn)
|
||||
*/
|
||||
|
||||
@@ -90,6 +90,7 @@ public:
|
||||
virtual bool visit(Return& _node) { return visitNode(_node); }
|
||||
virtual bool visit(Throw& _node) { return visitNode(_node); }
|
||||
virtual bool visit(EmitStatement& _node) { return visitNode(_node); }
|
||||
virtual bool visit(RevertStatement& _node) { return visitNode(_node); }
|
||||
virtual bool visit(VariableDeclarationStatement& _node) { return visitNode(_node); }
|
||||
virtual bool visit(ExpressionStatement& _node) { return visitNode(_node); }
|
||||
virtual bool visit(Conditional& _node) { return visitNode(_node); }
|
||||
@@ -144,6 +145,7 @@ public:
|
||||
virtual void endVisit(Return& _node) { endVisitNode(_node); }
|
||||
virtual void endVisit(Throw& _node) { endVisitNode(_node); }
|
||||
virtual void endVisit(EmitStatement& _node) { endVisitNode(_node); }
|
||||
virtual void endVisit(RevertStatement& _node) { endVisitNode(_node); }
|
||||
virtual void endVisit(VariableDeclarationStatement& _node) { endVisitNode(_node); }
|
||||
virtual void endVisit(ExpressionStatement& _node) { endVisitNode(_node); }
|
||||
virtual void endVisit(Conditional& _node) { endVisitNode(_node); }
|
||||
@@ -220,6 +222,7 @@ public:
|
||||
virtual bool visit(Return const& _node) { return visitNode(_node); }
|
||||
virtual bool visit(Throw const& _node) { return visitNode(_node); }
|
||||
virtual bool visit(EmitStatement const& _node) { return visitNode(_node); }
|
||||
virtual bool visit(RevertStatement const& _node) { return visitNode(_node); }
|
||||
virtual bool visit(VariableDeclarationStatement const& _node) { return visitNode(_node); }
|
||||
virtual bool visit(ExpressionStatement const& _node) { return visitNode(_node); }
|
||||
virtual bool visit(Conditional const& _node) { return visitNode(_node); }
|
||||
@@ -274,6 +277,7 @@ public:
|
||||
virtual void endVisit(Return const& _node) { endVisitNode(_node); }
|
||||
virtual void endVisit(Throw const& _node) { endVisitNode(_node); }
|
||||
virtual void endVisit(EmitStatement const& _node) { endVisitNode(_node); }
|
||||
virtual void endVisit(RevertStatement const& _node) { endVisitNode(_node); }
|
||||
virtual void endVisit(VariableDeclarationStatement const& _node) { endVisitNode(_node); }
|
||||
virtual void endVisit(ExpressionStatement const& _node) { endVisitNode(_node); }
|
||||
virtual void endVisit(Conditional const& _node) { endVisitNode(_node); }
|
||||
|
||||
@@ -682,6 +682,20 @@ void Throw::accept(ASTConstVisitor& _visitor) const
|
||||
_visitor.endVisit(*this);
|
||||
}
|
||||
|
||||
void RevertStatement::accept(ASTVisitor& _visitor)
|
||||
{
|
||||
if (_visitor.visit(*this))
|
||||
m_errorCall->accept(_visitor);
|
||||
_visitor.endVisit(*this);
|
||||
}
|
||||
|
||||
void RevertStatement::accept(ASTConstVisitor& _visitor) const
|
||||
{
|
||||
if (_visitor.visit(*this))
|
||||
m_errorCall->accept(_visitor);
|
||||
_visitor.endVisit(*this);
|
||||
}
|
||||
|
||||
void EmitStatement::accept(ASTVisitor& _visitor)
|
||||
{
|
||||
if (_visitor.visit(*this))
|
||||
|
||||
@@ -1250,11 +1250,13 @@ ASTPointer<Statement> Parser::parseStatement(bool _allowUnchecked)
|
||||
statement = parseEmitStatement(docString);
|
||||
break;
|
||||
case Token::Identifier:
|
||||
if (m_insideModifier && m_scanner->currentLiteral() == "_")
|
||||
{
|
||||
statement = ASTNodeFactory(*this).createNode<PlaceholderStatement>(docString);
|
||||
m_scanner->next();
|
||||
}
|
||||
if (m_scanner->currentLiteral() == "revert" && m_scanner->peekNextToken() == Token::Identifier)
|
||||
statement = parseRevertStatement(docString);
|
||||
else if (m_insideModifier && m_scanner->currentLiteral() == "_")
|
||||
{
|
||||
statement = ASTNodeFactory(*this).createNode<PlaceholderStatement>(docString);
|
||||
m_scanner->next();
|
||||
}
|
||||
else
|
||||
statement = parseSimpleStatement(docString);
|
||||
break;
|
||||
@@ -1474,8 +1476,38 @@ ASTPointer<EmitStatement> Parser::parseEmitStatement(ASTPointer<ASTString> const
|
||||
nodeFactory.markEndPosition();
|
||||
expectToken(Token::RParen);
|
||||
auto eventCall = eventCallNodeFactory.createNode<FunctionCall>(eventName, arguments, names);
|
||||
auto statement = nodeFactory.createNode<EmitStatement>(_docString, eventCall);
|
||||
return statement;
|
||||
return nodeFactory.createNode<EmitStatement>(_docString, eventCall);
|
||||
}
|
||||
|
||||
ASTPointer<RevertStatement> Parser::parseRevertStatement(ASTPointer<ASTString> const& _docString)
|
||||
{
|
||||
ASTNodeFactory nodeFactory(*this);
|
||||
solAssert(*expectIdentifierToken() == "revert", "");
|
||||
|
||||
ASTNodeFactory errorCallNodeFactory(*this);
|
||||
|
||||
solAssert(m_scanner->currentToken() == Token::Identifier, "");
|
||||
|
||||
IndexAccessedPath iap;
|
||||
while (true)
|
||||
{
|
||||
iap.path.push_back(parseIdentifier());
|
||||
if (m_scanner->currentToken() != Token::Period)
|
||||
break;
|
||||
m_scanner->next();
|
||||
}
|
||||
|
||||
auto errorName = expressionFromIndexAccessStructure(iap);
|
||||
expectToken(Token::LParen);
|
||||
|
||||
vector<ASTPointer<Expression>> arguments;
|
||||
vector<ASTPointer<ASTString>> names;
|
||||
std::tie(arguments, names) = parseFunctionCallArguments();
|
||||
errorCallNodeFactory.markEndPosition();
|
||||
nodeFactory.markEndPosition();
|
||||
expectToken(Token::RParen);
|
||||
auto errorCall = errorCallNodeFactory.createNode<FunctionCall>(errorName, arguments, names);
|
||||
return nodeFactory.createNode<RevertStatement>(_docString, errorCall);
|
||||
}
|
||||
|
||||
ASTPointer<Statement> Parser::parseSimpleStatement(ASTPointer<ASTString> const& _docString)
|
||||
|
||||
@@ -127,6 +127,7 @@ private:
|
||||
ASTPointer<WhileStatement> parseDoWhileStatement(ASTPointer<ASTString> const& _docString);
|
||||
ASTPointer<ForStatement> parseForStatement(ASTPointer<ASTString> const& _docString);
|
||||
ASTPointer<EmitStatement> parseEmitStatement(ASTPointer<ASTString> const& docString);
|
||||
ASTPointer<RevertStatement> parseRevertStatement(ASTPointer<ASTString> const& docString);
|
||||
/// A "simple statement" can be a variable declaration statement or an expression statement.
|
||||
ASTPointer<Statement> parseSimpleStatement(ASTPointer<ASTString> const& _docString);
|
||||
ASTPointer<VariableDeclarationStatement> parseVariableDeclarationStatement(
|
||||
|
||||
Reference in New Issue
Block a user