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.h | |
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.h')
-rw-r--r-- | Types.h | 47 |
1 files changed, 42 insertions, 5 deletions
@@ -24,6 +24,7 @@ #include <memory> #include <string> +#include <map> #include <boost/noncopyable.hpp> #include <libdevcore/Common.h> #include <libsolidity/Exceptions.h> @@ -37,6 +38,33 @@ namespace solidity // @todo realMxN, string<N> +class Type; // forward + +/** + * List of members of a type. + */ +class MemberList +{ +public: + using TypePointer = std::shared_ptr<Type const>; + using MemberMap = std::map<std::string, TypePointer>; + + MemberList() {} + explicit MemberList(MemberMap const& _members): m_memberTypes(_members) {} + TypePointer getMemberType(std::string const& _name) const + { + auto it = m_memberTypes.find(_name); + return it != m_memberTypes.end() ? it->second : std::shared_ptr<Type const>(); + } + + MemberMap::const_iterator begin() const { return m_memberTypes.begin(); } + MemberMap::const_iterator end() const { return m_memberTypes.end(); } + +private: + MemberMap m_memberTypes; +}; + + /** * Abstract base class that forms the root of the type hierarchy. */ @@ -81,12 +109,21 @@ public: /// Returns false if the type cannot live outside the storage, i.e. if it includes some mapping. virtual bool canLiveOutsideStorage() const { return true; } + /// Returns the list of all members of this type. Default implementation: no members. + virtual MemberList const& getMembers() const { return EmptyMemberList; } + /// Convenience method, returns the type of the given named member or an empty pointer if no such member exists. + std::shared_ptr<Type const> getMemberType(std::string const& _name) const { return getMembers().getMemberType(_name); } + virtual std::string toString() const = 0; virtual u256 literalValue(Literal const&) const { BOOST_THROW_EXCEPTION(InternalCompilerError() << errinfo_comment("Literal value requested " "for type without literals.")); } + +protected: + /// Convenience object used when returning an empty member list. + static const MemberList EmptyMemberList; }; /** @@ -187,14 +224,14 @@ public: virtual bool canLiveOutsideStorage() const; virtual std::string toString() const override; - unsigned getMemberCount() const; - /// Returns the index of the member with name @a _name or unsigned(-1) if it does not exist. - unsigned memberNameToIndex(std::string const& _name) const; - std::shared_ptr<Type const> const& getMemberByIndex(unsigned _index) const; - u256 getStorageOffsetOfMember(unsigned _index) const; + virtual MemberList const& getMembers() const override; + + u256 getStorageOffsetOfMember(std::string const& _name) const; private: StructDefinition const& m_struct; + /// List of member types, will be lazy-initialized because of recursive references. + mutable std::unique_ptr<MemberList> m_members; }; /** |