ast: condense duplicate code

AST.cpp:
* Added findClause() helper function to anonymous namespace.
This commit is contained in:
tcoyvwac 2022-08-17 19:43:51 +02:00
parent 22a0c46eae
commit 3d54bfd0c3
No known key found for this signature in database
GPG Key ID: 88BAEF34B4B127DC

View File

@ -29,6 +29,8 @@
#include <libsolidity/ast/TypeProvider.h>
#include <libsolutil/Keccak256.h>
#include <range/v3/view/tail.hpp>
#include <boost/algorithm/string.hpp>
#include <functional>
@ -38,6 +40,17 @@ using namespace std;
using namespace solidity;
using namespace solidity::frontend;
namespace
{
TryCatchClause const* findClause(vector<ASTPointer<TryCatchClause>> const& _clauses, optional<string> _errorName = {})
{
for (auto const& clause: ranges::views::tail(_clauses))
if (_errorName.has_value() ? clause->errorName() == _errorName : clause->errorName().empty())
return clause.get();
return nullptr;
}
}
ASTNode::ASTNode(int64_t _id, SourceLocation _location):
m_id(static_cast<size_t>(_id)),
m_location(std::move(_location))
@ -981,26 +994,14 @@ TryCatchClause const* TryStatement::successClause() const
return m_clauses[0].get();
}
TryCatchClause const* TryStatement::panicClause() const
{
for (size_t i = 1; i < m_clauses.size(); ++i)
if (m_clauses[i]->errorName() == "Panic")
return m_clauses[i].get();
return nullptr;
TryCatchClause const* TryStatement::panicClause() const {
return findClause(m_clauses, "Panic");
}
TryCatchClause const* TryStatement::errorClause() const
{
for (size_t i = 1; i < m_clauses.size(); ++i)
if (m_clauses[i]->errorName() == "Error")
return m_clauses[i].get();
return nullptr;
TryCatchClause const* TryStatement::errorClause() const {
return findClause(m_clauses, "Error");
}
TryCatchClause const* TryStatement::fallbackClause() const
{
for (size_t i = 1; i < m_clauses.size(); ++i)
if (m_clauses[i]->errorName().empty())
return m_clauses[i].get();
return nullptr;
TryCatchClause const* TryStatement::fallbackClause() const {
return findClause(m_clauses);
}