diff --git a/AST.cpp b/AST.cpp index c37e8c374..4c6db6a20 100644 --- a/AST.cpp +++ b/AST.cpp @@ -209,6 +209,33 @@ vector, FunctionTypePointer>> const& ContractDefinition::getIn return *m_interfaceFunctionList; } +vector const& ContractDefinition::getInheritableMembers() const +{ + if (!m_inheritableMembers) + { + set memberSeen; + m_inheritableMembers.reset(new vector()); + auto addInheritableMember = [&](Declaration const* _decl) + { + if (memberSeen.count(_decl->getName()) == 0 && _decl->isVisibleInDerivedContracts()) + { + memberSeen.insert(_decl->getName()); + m_inheritableMembers->push_back(_decl); + } + }; + + for (ASTPointer const& f: getDefinedFunctions()) + addInheritableMember(f.get()); + + for (ASTPointer const& v: getStateVariables()) + addInheritableMember(v.get()); + + for (ASTPointer const& s: getDefinedStructs()) + addInheritableMember(s.get()); + } + return *m_inheritableMembers; +} + TypePointer EnumValue::getType(ContractDefinition const*) const { EnumDefinition const* parentDef = dynamic_cast(getScope()); diff --git a/AST.h b/AST.h index dea0fba63..d028e8a76 100644 --- a/AST.h +++ b/AST.h @@ -144,7 +144,7 @@ public: Visibility getVisibility() const { return m_visibility == Visibility::Default ? getDefaultVisibility() : m_visibility; } bool isPublic() const { return getVisibility() >= Visibility::Public; } bool isVisibleInContract() const { return getVisibility() != Visibility::External; } - bool isVisibleInDerivedContracts() const { return isVisibleInContract() && getVisibility() >= Visibility::Internal; } + virtual bool isVisibleInDerivedContracts() const { return isVisibleInContract() && getVisibility() >= Visibility::Internal; } /// @returns the scope this declaration resides in. Can be nullptr if it is the global scope. /// Available only after name and type resolution step. @@ -247,6 +247,9 @@ public: /// as intended for use by the ABI. std::map, FunctionTypePointer> getInterfaceFunctions() const; + /// @returns a list of the inheritable members of this contract + std::vector const& getInheritableMembers() const; + /// List of all (direct and indirect) base contracts in order from derived to base, including /// the contract itself. Available after name resolution std::vector const& getLinearizedBaseContracts() const { return m_linearizedBaseContracts; } @@ -273,6 +276,7 @@ private: std::vector m_linearizedBaseContracts; mutable std::unique_ptr, FunctionTypePointer>>> m_interfaceFunctionList; mutable std::unique_ptr>> m_interfaceEvents; + mutable std::unique_ptr> m_inheritableMembers; }; class InheritanceSpecifier: public ASTNode @@ -405,6 +409,11 @@ public: ASTPointer const& getReturnParameterList() const { return m_returnParameters; } Block const& getBody() const { return *m_body; } + virtual bool isVisibleInDerivedContracts() const override + { + return !isConstructor() && !getName().empty() && isVisibleInContract() && + getVisibility() >= Visibility::Internal; + } virtual TypePointer getType(ContractDefinition const*) const override; /// Checks that all parameters have allowed types and calls checkTypeRequirements on the body. diff --git a/Types.cpp b/Types.cpp index 8cc1f2589..22d612cd9 100644 --- a/Types.cpp +++ b/Types.cpp @@ -643,8 +643,7 @@ MemberList const& ContractType::getMembers() const { for (ContractDefinition const* base: m_contract.getLinearizedBaseContracts()) for (ASTPointer const& function: base->getDefinedFunctions()) - if (!function->isConstructor() && !function->getName().empty()&& - function->isVisibleInDerivedContracts()) + if (function->isVisibleInDerivedContracts()) members.push_back(make_pair(function->getName(), make_shared(*function, true))); } else @@ -1039,10 +1038,9 @@ MemberList const& TypeType::getMembers() const vector currentBases = m_currentContract->getLinearizedBaseContracts(); if (find(currentBases.begin(), currentBases.end(), &contract) != currentBases.end()) // We are accessing the type of a base contract, so add all public and protected - // functions. Note that this does not add inherited functions on purpose. - for (ASTPointer const& f: contract.getDefinedFunctions()) - if (!f->isConstructor() && !f->getName().empty() && f->isVisibleInDerivedContracts()) - members.push_back(make_pair(f->getName(), make_shared(*f))); + // members. Note that this does not add inherited functions on purpose. + for (Declaration const* decl: contract.getInheritableMembers()) + members.push_back(make_pair(decl->getName(), decl->getType())); } else if (m_actualType->getCategory() == Category::Enum) {