diff options
author | chriseth <c@ethdev.com> | 2015-11-24 06:57:17 +0800 |
---|---|---|
committer | chriseth <c@ethdev.com> | 2015-11-26 22:37:55 +0800 |
commit | b47d5932528357939ee29758a8b8027c90bdb1e5 (patch) | |
tree | a32526db014b5a654e193660e883e97ab437aa95 /libsolidity/ast | |
parent | 86495dfc57dde9b825ffd8c219ea809446e978f9 (diff) | |
download | dexon-solidity-b47d5932528357939ee29758a8b8027c90bdb1e5.tar.gz dexon-solidity-b47d5932528357939ee29758a8b8027c90bdb1e5.tar.zst dexon-solidity-b47d5932528357939ee29758a8b8027c90bdb1e5.zip |
Do not store elements of a contract by AST node type.
Diffstat (limited to 'libsolidity/ast')
-rw-r--r-- | libsolidity/ast/AST.cpp | 84 | ||||
-rw-r--r-- | libsolidity/ast/AST.h | 46 | ||||
-rw-r--r-- | libsolidity/ast/AST_accept.h | 14 | ||||
-rw-r--r-- | libsolidity/ast/Types.cpp | 8 |
4 files changed, 91 insertions, 61 deletions
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<FixedHash<4>, FunctionTypePointer> ContractDefinition::interfaceFunctions() FunctionDefinition const* ContractDefinition::constructor() const { - for (ASTPointer<FunctionDefinition> 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<FunctionDefinition> const& f: contract->definedFunctions()) + for (FunctionDefinition const* f: contract->definedFunctions()) if (f->name().empty()) - return f.get(); + return f; return nullptr; } -vector<ASTPointer<EventDefinition>> const& ContractDefinition::interfaceEvents() const +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) { set<string> eventsSeen; - m_interfaceEvents.reset(new vector<ASTPointer<EventDefinition>>()); + m_interfaceEvents.reset(new vector<EventDefinition const*>()); for (ContractDefinition const* contract: annotation().linearizedBaseContracts) - for (ASTPointer<EventDefinition> 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<pair<FixedHash<4>, FunctionTypePointer>> const& ContractDefinition::inter for (ContractDefinition const* contract: annotation().linearizedBaseContracts) { vector<FunctionTypePointer> functions; - for (ASTPointer<FunctionDefinition> const& f: contract->definedFunctions()) + for (FunctionDefinition const* f: contract->definedFunctions()) if (f->isPartOfExternalInterface()) functions.push_back(make_shared<FunctionType>(*f, false)); - for (ASTPointer<VariableDeclaration> const& v: contract->stateVariables()) + for (VariableDeclaration const* v: contract->stateVariables()) if (v->isPartOfExternalInterface()) functions.push_back(make_shared<FunctionType>(*v)); for (FunctionTypePointer const& fun: functions) @@ -176,14 +230,14 @@ vector<Declaration const*> const& ContractDefinition::inheritableMembers() const } }; - for (ASTPointer<FunctionDefinition> const& f: definedFunctions()) - addInheritableMember(f.get()); + for (FunctionDefinition const* f: definedFunctions()) + addInheritableMember(f); - for (ASTPointer<VariableDeclaration> const& v: stateVariables()) - addInheritableMember(v.get()); + for (VariableDeclaration const* v: stateVariables()) + addInheritableMember(v); - for (ASTPointer<StructDefinition> 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 <class T> - static void listAccept(std::vector<ASTPointer<T>>& _list, ASTVisitor& _visitor) + static void listAccept(std::vector<T> const& _list, ASTVisitor& _visitor) { - for (ASTPointer<T>& element: _list) + for (T const& element: _list) element->accept(_visitor); } template <class T> - static void listAccept(std::vector<ASTPointer<T>> const& _list, ASTConstVisitor& _visitor) + static void listAccept(std::vector<T> const& _list, ASTConstVisitor& _visitor) { - for (ASTPointer<T> const& element: _list) + for (T const& element: _list) element->accept(_visitor); } @@ -238,23 +238,13 @@ public: ASTPointer<ASTString> const& _name, ASTPointer<ASTString> const& _documentation, std::vector<ASTPointer<InheritanceSpecifier>> const& _baseContracts, - std::vector<ASTPointer<StructDefinition>> const& _definedStructs, - std::vector<ASTPointer<EnumDefinition>> const& _definedEnums, - std::vector<ASTPointer<VariableDeclaration>> const& _stateVariables, - std::vector<ASTPointer<FunctionDefinition>> const& _definedFunctions, - std::vector<ASTPointer<ModifierDefinition>> const& _functionModifiers, - std::vector<ASTPointer<EventDefinition>> const& _events, + std::vector<ASTPointer<ASTNode>> 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<ASTPointer<InheritanceSpecifier>> const& baseContracts() const { return m_baseContracts; } - std::vector<ASTPointer<StructDefinition>> const& definedStructs() const { return m_definedStructs; } - std::vector<ASTPointer<EnumDefinition>> const& definedEnums() const { return m_definedEnums; } - std::vector<ASTPointer<VariableDeclaration>> const& stateVariables() const { return m_stateVariables; } - std::vector<ASTPointer<ModifierDefinition>> const& functionModifiers() const { return m_functionModifiers; } - std::vector<ASTPointer<FunctionDefinition>> const& definedFunctions() const { return m_definedFunctions; } - std::vector<ASTPointer<EventDefinition>> const& events() const { return m_events; } - std::vector<ASTPointer<EventDefinition>> const& interfaceEvents() const; + 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<EventDefinition const*> 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<ASTPointer<InheritanceSpecifier>> m_baseContracts; - std::vector<ASTPointer<StructDefinition>> m_definedStructs; - std::vector<ASTPointer<EnumDefinition>> m_definedEnums; - std::vector<ASTPointer<VariableDeclaration>> m_stateVariables; - std::vector<ASTPointer<FunctionDefinition>> m_definedFunctions; - std::vector<ASTPointer<ModifierDefinition>> m_functionModifiers; - std::vector<ASTPointer<EventDefinition>> m_events; + std::vector<ASTPointer<ASTNode>> m_subNodes; bool m_isLibrary; // parsed Natspec documentation of the contract. @@ -310,7 +296,7 @@ private: std::vector<ContractDefinition const*> m_linearizedBaseContracts; mutable std::unique_ptr<std::vector<std::pair<FixedHash<4>, FunctionTypePointer>>> m_interfaceFunctionList; - mutable std::unique_ptr<std::vector<ASTPointer<EventDefinition>>> m_interfaceEvents; + mutable std::unique_ptr<std::vector<EventDefinition const*>> m_interfaceEvents; mutable std::unique_ptr<std::vector<Declaration const*>> 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<FunctionDefinition> 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<tuple<VariableDeclaration const*, u256, unsigned>> ContractType::stateVar { vector<VariableDeclaration const*> variables; for (ContractDefinition const* contract: boost::adaptors::reverse(m_contract.annotation().linearizedBaseContracts)) - for (ASTPointer<VariableDeclaration> 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); |