diff options
author | Christian <c@ethdev.com> | 2014-11-20 17:19:43 +0800 |
---|---|---|
committer | Christian <c@ethdev.com> | 2014-11-24 04:04:39 +0800 |
commit | fa987e0a206bba35cfe6e311f8bad1470d9b5d4f (patch) | |
tree | b38d2fd49846c2dadf83ff6b1458f432927a1bab /Types.cpp | |
parent | 735dbe69861d6f80368c057423a13ab1d1a9b557 (diff) | |
download | dexon-solidity-fa987e0a206bba35cfe6e311f8bad1470d9b5d4f.tar.gz dexon-solidity-fa987e0a206bba35cfe6e311f8bad1470d9b5d4f.tar.zst dexon-solidity-fa987e0a206bba35cfe6e311f8bad1470d9b5d4f.zip |
Convenience class for type members.
Diffstat (limited to 'Types.cpp')
-rw-r--r-- | Types.cpp | 49 |
1 files changed, 24 insertions, 25 deletions
@@ -86,6 +86,8 @@ shared_ptr<Type> Type::forLiteral(Literal const& _literal) } } +const MemberList Type::EmptyMemberList = MemberList(); + shared_ptr<IntegerType> IntegerType::smallestTypeForLiteral(string const& _literal) { bigint value(_literal); @@ -226,15 +228,15 @@ bool StructType::operator==(Type const& _other) const u256 StructType::getStorageSize() const { u256 size = 0; - for (ASTPointer<VariableDeclaration> const& variable: m_struct.getMembers()) - size += variable->getType()->getStorageSize(); + for (pair<string, shared_ptr<Type const>> const& member: getMembers()) + size += member.second->getStorageSize(); return max<u256>(1, size); } bool StructType::canLiveOutsideStorage() const { - for (unsigned i = 0; i < getMemberCount(); ++i) - if (!getMemberByIndex(i)->canLiveOutsideStorage()) + for (pair<string, shared_ptr<Type const>> const& member: getMembers()) + if (!member.second->canLiveOutsideStorage()) return false; return true; } @@ -244,33 +246,30 @@ string StructType::toString() const return string("struct ") + m_struct.getName(); } -unsigned StructType::getMemberCount() const -{ - return m_struct.getMembers().size(); -} - -unsigned StructType::memberNameToIndex(string const& _name) const -{ - vector<ASTPointer<VariableDeclaration>> const& members = m_struct.getMembers(); - for (unsigned index = 0; index < members.size(); ++index) - if (members[index]->getName() == _name) - return index; - return unsigned(-1); -} - -shared_ptr<Type const> const& StructType::getMemberByIndex(unsigned _index) const +MemberList const& StructType::getMembers() const { - return m_struct.getMembers()[_index].getType(); + // We need to lazy-initialize it because of recursive references. + if (!m_members) + { + map<string, shared_ptr<Type const>> members; + for (ASTPointer<VariableDeclaration> const& variable: m_struct.getMembers()) + members[variable->getName()] = variable->getType(); + m_members.reset(new MemberList(members)); + } + return *m_members; } -u256 StructType::getStorageOffsetOfMember(unsigned _index) const +u256 StructType::getStorageOffsetOfMember(string const& _name) const { //@todo cache member offset? u256 offset; -// vector<ASTPointer<VariableDeclaration>> const& members = m_struct.getMembers(); - for (unsigned index = 0; index < _index; ++index) - offset += getMemberByIndex(index)->getStorageSize(); - return offset; + for (ASTPointer<VariableDeclaration> variable: m_struct.getMembers()) + { + offset += variable->getType()->getStorageSize(); + if (variable->getName() == _name) + return offset; + } + BOOST_THROW_EXCEPTION(InternalCompilerError() << errinfo_comment("Storage offset of non-existing member requested.")); } bool FunctionType::operator==(Type const& _other) const |