From 4aaa150674d5970f651ab3e95e5b0e2daac0e7e0 Mon Sep 17 00:00:00 2001 From: chriseth Date: Thu, 26 Nov 2015 17:28:44 +0100 Subject: Convert filter to template function. --- libsolidity/ast/AST.cpp | 54 ------------------------------------------------- libsolidity/ast/AST.h | 26 ++++++++++++++++++------ 2 files changed, 20 insertions(+), 60 deletions(-) diff --git a/libsolidity/ast/AST.cpp b/libsolidity/ast/AST.cpp index 65535388..6006d441 100644 --- a/libsolidity/ast/AST.cpp +++ b/libsolidity/ast/AST.cpp @@ -89,60 +89,6 @@ FunctionDefinition const* ContractDefinition::fallbackFunction() const return nullptr; } -vector ContractDefinition::definedStructs() const -{ - vector ret; - for (auto const& node: m_subNodes) - if (auto v = dynamic_cast(node.get())) - ret.push_back(v); - return ret; -} - -vector ContractDefinition::definedEnums() const -{ - vector ret; - for (auto const& node: m_subNodes) - if (auto v = dynamic_cast(node.get())) - ret.push_back(v); - return ret; -} - -vector ContractDefinition::stateVariables() const -{ - vector ret; - for (auto const& node: m_subNodes) - if (auto v = dynamic_cast(node.get())) - ret.push_back(v); - return ret; -} - -vector ContractDefinition::functionModifiers() const -{ - vector ret; - for (auto const& node: m_subNodes) - if (auto v = dynamic_cast(node.get())) - ret.push_back(v); - return ret; -} - -vector ContractDefinition::definedFunctions() const -{ - vector ret; - for (auto const& node: m_subNodes) - if (auto v = dynamic_cast(node.get())) - ret.push_back(v); - return ret; -} - -vector ContractDefinition::events() const -{ - vector ret; - for (auto const& node: m_subNodes) - if (auto v = dynamic_cast(node.get())) - ret.push_back(v); - return ret; -} - vector const& ContractDefinition::interfaceEvents() const { if (!m_interfaceEvents) diff --git a/libsolidity/ast/AST.h b/libsolidity/ast/AST.h index ab872750..a28d9f4f 100644 --- a/libsolidity/ast/AST.h +++ b/libsolidity/ast/AST.h @@ -70,6 +70,10 @@ public: element->accept(_visitor); } + /// @returns a copy of the vector containing only the nodes which derive from T. + template + static std::vector<_T const*> filteredNodes(std::vector> const& _nodes); + /// Returns the source code location of this node. SourceLocation const& location() const { return m_location; } @@ -95,6 +99,16 @@ private: SourceLocation m_location; }; +template +std::vector<_T const*> ASTNode::filteredNodes(std::vector> const& _nodes) +{ + std::vector<_T const*> ret; + for (auto const& n: _nodes) + if (auto const* nt = dynamic_cast<_T const*>(n.get())) + ret.push_back(nt); + return ret; +} + /** * Source unit containing import directives and contract definitions. */ @@ -253,12 +267,12 @@ public: std::vector> const& baseContracts() const { return m_baseContracts; } std::vector> const& subNodes() const { return m_subNodes; } - std::vector definedStructs() const; - std::vector definedEnums() const; - std::vector stateVariables() const; - std::vector functionModifiers() const; - std::vector definedFunctions() const; - std::vector events() const; + std::vector definedStructs() const { return filteredNodes(m_subNodes); } + std::vector definedEnums() const { return filteredNodes(m_subNodes); } + std::vector stateVariables() const { return filteredNodes(m_subNodes); } + std::vector functionModifiers() const { return filteredNodes(m_subNodes); } + std::vector definedFunctions() const { return filteredNodes(m_subNodes); } + std::vector events() const { return filteredNodes(m_subNodes); } std::vector const& interfaceEvents() const; bool isLibrary() const { return m_isLibrary; } -- cgit v1.2.3