From b47d5932528357939ee29758a8b8027c90bdb1e5 Mon Sep 17 00:00:00 2001 From: chriseth Date: Mon, 23 Nov 2015 23:57:17 +0100 Subject: Do not store elements of a contract by AST node type. --- libsolidity/ast/AST.cpp | 84 ++++++++++++++++++++++++++++++++++++-------- libsolidity/ast/AST.h | 46 +++++++++--------------- libsolidity/ast/AST_accept.h | 14 ++------ libsolidity/ast/Types.cpp | 8 ++--- 4 files changed, 91 insertions(+), 61 deletions(-) (limited to 'libsolidity/ast') diff --git a/libsolidity/ast/AST.cpp b/libsolidity/ast/AST.cpp index 7ceab752..65535388 100644 --- a/libsolidity/ast/AST.cpp +++ b/libsolidity/ast/AST.cpp @@ -74,29 +74,83 @@ map, FunctionTypePointer> ContractDefinition::interfaceFunctions() FunctionDefinition const* ContractDefinition::constructor() const { - for (ASTPointer const& f: m_definedFunctions) + for (FunctionDefinition const* f: definedFunctions()) if (f->isConstructor()) - return f.get(); + return f; return nullptr; } FunctionDefinition const* ContractDefinition::fallbackFunction() const { for (ContractDefinition const* contract: annotation().linearizedBaseContracts) - for (ASTPointer const& f: contract->definedFunctions()) + for (FunctionDefinition const* f: contract->definedFunctions()) if (f->name().empty()) - return f.get(); + return f; return nullptr; } -vector> const& ContractDefinition::interfaceEvents() const +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) { set eventsSeen; - m_interfaceEvents.reset(new vector>()); + m_interfaceEvents.reset(new vector()); for (ContractDefinition const* contract: annotation().linearizedBaseContracts) - for (ASTPointer const& e: contract->events()) + for (EventDefinition const* e: contract->events()) if (eventsSeen.count(e->name()) == 0) { eventsSeen.insert(e->name()); @@ -116,10 +170,10 @@ vector, FunctionTypePointer>> const& ContractDefinition::inter for (ContractDefinition const* contract: annotation().linearizedBaseContracts) { vector functions; - for (ASTPointer const& f: contract->definedFunctions()) + for (FunctionDefinition const* f: contract->definedFunctions()) if (f->isPartOfExternalInterface()) functions.push_back(make_shared(*f, false)); - for (ASTPointer const& v: contract->stateVariables()) + for (VariableDeclaration const* v: contract->stateVariables()) if (v->isPartOfExternalInterface()) functions.push_back(make_shared(*v)); for (FunctionTypePointer const& fun: functions) @@ -176,14 +230,14 @@ vector const& ContractDefinition::inheritableMembers() const } }; - for (ASTPointer const& f: definedFunctions()) - addInheritableMember(f.get()); + for (FunctionDefinition const* f: definedFunctions()) + addInheritableMember(f); - for (ASTPointer const& v: stateVariables()) - addInheritableMember(v.get()); + for (VariableDeclaration const* v: stateVariables()) + addInheritableMember(v); - for (ASTPointer const& s: definedStructs()) - addInheritableMember(s.get()); + for (StructDefinition const* s: definedStructs()) + addInheritableMember(s); } return *m_inheritableMembers; } diff --git a/libsolidity/ast/AST.h b/libsolidity/ast/AST.h index 11c4fef6..ab872750 100644 --- a/libsolidity/ast/AST.h +++ b/libsolidity/ast/AST.h @@ -58,15 +58,15 @@ public: virtual void accept(ASTVisitor& _visitor) = 0; virtual void accept(ASTConstVisitor& _visitor) const = 0; template - static void listAccept(std::vector>& _list, ASTVisitor& _visitor) + static void listAccept(std::vector const& _list, ASTVisitor& _visitor) { - for (ASTPointer& element: _list) + for (T const& element: _list) element->accept(_visitor); } template - static void listAccept(std::vector> const& _list, ASTConstVisitor& _visitor) + static void listAccept(std::vector const& _list, ASTConstVisitor& _visitor) { - for (ASTPointer const& element: _list) + for (T const& element: _list) element->accept(_visitor); } @@ -238,23 +238,13 @@ public: ASTPointer const& _name, ASTPointer const& _documentation, std::vector> const& _baseContracts, - std::vector> const& _definedStructs, - std::vector> const& _definedEnums, - std::vector> const& _stateVariables, - std::vector> const& _definedFunctions, - std::vector> const& _functionModifiers, - std::vector> const& _events, + std::vector> const& _subNodes, bool _isLibrary ): Declaration(_location, _name), Documented(_documentation), m_baseContracts(_baseContracts), - m_definedStructs(_definedStructs), - m_definedEnums(_definedEnums), - m_stateVariables(_stateVariables), - m_definedFunctions(_definedFunctions), - m_functionModifiers(_functionModifiers), - m_events(_events), + m_subNodes(_subNodes), m_isLibrary(_isLibrary) {} @@ -262,13 +252,14 @@ public: virtual void accept(ASTConstVisitor& _visitor) const override; std::vector> const& baseContracts() const { return m_baseContracts; } - std::vector> const& definedStructs() const { return m_definedStructs; } - std::vector> const& definedEnums() const { return m_definedEnums; } - std::vector> const& stateVariables() const { return m_stateVariables; } - std::vector> const& functionModifiers() const { return m_functionModifiers; } - std::vector> const& definedFunctions() const { return m_definedFunctions; } - std::vector> const& events() const { return m_events; } - std::vector> const& interfaceEvents() const; + 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 const& interfaceEvents() const; bool isLibrary() const { return m_isLibrary; } /// @returns a map of canonical function signatures to FunctionDefinitions @@ -296,12 +287,7 @@ public: private: std::vector> m_baseContracts; - std::vector> m_definedStructs; - std::vector> m_definedEnums; - std::vector> m_stateVariables; - std::vector> m_definedFunctions; - std::vector> m_functionModifiers; - std::vector> m_events; + std::vector> m_subNodes; bool m_isLibrary; // parsed Natspec documentation of the contract. @@ -310,7 +296,7 @@ private: std::vector m_linearizedBaseContracts; mutable std::unique_ptr, FunctionTypePointer>>> m_interfaceFunctionList; - mutable std::unique_ptr>> m_interfaceEvents; + mutable std::unique_ptr> m_interfaceEvents; mutable std::unique_ptr> m_inheritableMembers; }; diff --git a/libsolidity/ast/AST_accept.h b/libsolidity/ast/AST_accept.h index 99d1bf6a..f65595b8 100644 --- a/libsolidity/ast/AST_accept.h +++ b/libsolidity/ast/AST_accept.h @@ -62,12 +62,7 @@ void ContractDefinition::accept(ASTVisitor& _visitor) if (_visitor.visit(*this)) { listAccept(m_baseContracts, _visitor); - listAccept(m_definedStructs, _visitor); - listAccept(m_definedEnums, _visitor); - listAccept(m_stateVariables, _visitor); - listAccept(m_events, _visitor); - listAccept(m_functionModifiers, _visitor); - listAccept(m_definedFunctions, _visitor); + listAccept(m_subNodes, _visitor); } _visitor.endVisit(*this); } @@ -77,12 +72,7 @@ void ContractDefinition::accept(ASTConstVisitor& _visitor) const if (_visitor.visit(*this)) { listAccept(m_baseContracts, _visitor); - listAccept(m_definedStructs, _visitor); - listAccept(m_definedEnums, _visitor); - listAccept(m_stateVariables, _visitor); - listAccept(m_events, _visitor); - listAccept(m_functionModifiers, _visitor); - listAccept(m_definedFunctions, _visitor); + listAccept(m_subNodes, _visitor); } _visitor.endVisit(*this); } diff --git a/libsolidity/ast/Types.cpp b/libsolidity/ast/Types.cpp index 85e1fd49..a4da5b13 100644 --- a/libsolidity/ast/Types.cpp +++ b/libsolidity/ast/Types.cpp @@ -970,7 +970,7 @@ MemberList const& ContractType::members(ContractDefinition const*) const { // add the most derived of all functions which are visible in derived contracts for (ContractDefinition const* base: m_contract.annotation().linearizedBaseContracts) - for (ASTPointer const& function: base->definedFunctions()) + for (FunctionDefinition const* function: base->definedFunctions()) { if (!function->isVisibleInDerivedContracts()) continue; @@ -991,7 +991,7 @@ MemberList const& ContractType::members(ContractDefinition const*) const members.push_back(MemberList::Member( function->name(), functionType, - function.get() + function )); } } @@ -1024,9 +1024,9 @@ vector> ContractType::stateVar { vector variables; for (ContractDefinition const* contract: boost::adaptors::reverse(m_contract.annotation().linearizedBaseContracts)) - for (ASTPointer const& variable: contract->stateVariables()) + for (VariableDeclaration const* variable: contract->stateVariables()) if (!variable->isConstant()) - variables.push_back(variable.get()); + variables.push_back(variable); TypePointers types; for (auto variable: variables) types.push_back(variable->annotation().type); -- cgit