diff options
author | chriseth <c@ethdev.com> | 2015-11-27 00:28:44 +0800 |
---|---|---|
committer | chriseth <c@ethdev.com> | 2015-11-27 00:28:44 +0800 |
commit | 4aaa150674d5970f651ab3e95e5b0e2daac0e7e0 (patch) | |
tree | 83159d4b3367d9278ea67d37409fa856c58f11bb /libsolidity | |
parent | cc2df5f9f2726f92bebe95e2a94afb6ba32c785c (diff) | |
download | dexon-solidity-4aaa150674d5970f651ab3e95e5b0e2daac0e7e0.tar.gz dexon-solidity-4aaa150674d5970f651ab3e95e5b0e2daac0e7e0.tar.zst dexon-solidity-4aaa150674d5970f651ab3e95e5b0e2daac0e7e0.zip |
Convert filter to template function.
Diffstat (limited to 'libsolidity')
-rw-r--r-- | libsolidity/ast/AST.cpp | 54 | ||||
-rw-r--r-- | 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<StructDefinition const*> ContractDefinition::definedStructs() const -{ - vector<StructDefinition const*> ret; - for (auto const& node: m_subNodes) - if (auto v = dynamic_cast<StructDefinition const*>(node.get())) - ret.push_back(v); - return ret; -} - -vector<EnumDefinition const*> ContractDefinition::definedEnums() const -{ - vector<EnumDefinition const*> ret; - for (auto const& node: m_subNodes) - if (auto v = dynamic_cast<EnumDefinition const*>(node.get())) - ret.push_back(v); - return ret; -} - -vector<VariableDeclaration const*> ContractDefinition::stateVariables() const -{ - vector<VariableDeclaration const*> ret; - for (auto const& node: m_subNodes) - if (auto v = dynamic_cast<VariableDeclaration const*>(node.get())) - ret.push_back(v); - return ret; -} - -vector<ModifierDefinition const*> ContractDefinition::functionModifiers() const -{ - vector<ModifierDefinition const*> ret; - for (auto const& node: m_subNodes) - if (auto v = dynamic_cast<ModifierDefinition const*>(node.get())) - ret.push_back(v); - return ret; -} - -vector<FunctionDefinition const*> ContractDefinition::definedFunctions() const -{ - vector<FunctionDefinition const*> ret; - for (auto const& node: m_subNodes) - if (auto v = dynamic_cast<FunctionDefinition const*>(node.get())) - ret.push_back(v); - return ret; -} - -vector<EventDefinition const*> ContractDefinition::events() const -{ - vector<EventDefinition const*> ret; - for (auto const& node: m_subNodes) - if (auto v = dynamic_cast<EventDefinition const*>(node.get())) - ret.push_back(v); - return ret; -} - vector<EventDefinition const*> 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 <class _T> + static std::vector<_T const*> filteredNodes(std::vector<ASTPointer<ASTNode>> 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 <class _T> +std::vector<_T const*> ASTNode::filteredNodes(std::vector<ASTPointer<ASTNode>> 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<ASTPointer<InheritanceSpecifier>> const& baseContracts() const { return m_baseContracts; } std::vector<ASTPointer<ASTNode>> const& subNodes() const { return m_subNodes; } - std::vector<StructDefinition const*> definedStructs() const; - std::vector<EnumDefinition const*> definedEnums() const; - std::vector<VariableDeclaration const*> stateVariables() const; - std::vector<ModifierDefinition const*> functionModifiers() const; - std::vector<FunctionDefinition const*> definedFunctions() const; - std::vector<EventDefinition const*> events() const; + std::vector<StructDefinition const*> definedStructs() const { return filteredNodes<StructDefinition>(m_subNodes); } + std::vector<EnumDefinition const*> definedEnums() const { return filteredNodes<EnumDefinition>(m_subNodes); } + std::vector<VariableDeclaration const*> stateVariables() const { return filteredNodes<VariableDeclaration>(m_subNodes); } + std::vector<ModifierDefinition const*> functionModifiers() const { return filteredNodes<ModifierDefinition>(m_subNodes); } + std::vector<FunctionDefinition const*> definedFunctions() const { return filteredNodes<FunctionDefinition>(m_subNodes); } + std::vector<EventDefinition const*> events() const { return filteredNodes<EventDefinition>(m_subNodes); } std::vector<EventDefinition const*> const& interfaceEvents() const; bool isLibrary() const { return m_isLibrary; } |