diff options
author | Lefteris Karapetsas <lefteris@refu.co> | 2015-02-11 23:37:46 +0800 |
---|---|---|
committer | Lefteris Karapetsas <lefteris@refu.co> | 2015-02-14 06:16:14 +0800 |
commit | c3a9ae0b0657861c8cc27f60358b0bfe99ed05ae (patch) | |
tree | 695fbc92111b1853fdee6500c92e55555666759f | |
parent | 86e1d671cc4ed9b89576c80e16e58a8019115068 (diff) | |
download | dexon-solidity-c3a9ae0b0657861c8cc27f60358b0bfe99ed05ae.tar dexon-solidity-c3a9ae0b0657861c8cc27f60358b0bfe99ed05ae.tar.gz dexon-solidity-c3a9ae0b0657861c8cc27f60358b0bfe99ed05ae.tar.bz2 dexon-solidity-c3a9ae0b0657861c8cc27f60358b0bfe99ed05ae.tar.lz dexon-solidity-c3a9ae0b0657861c8cc27f60358b0bfe99ed05ae.tar.xz dexon-solidity-c3a9ae0b0657861c8cc27f60358b0bfe99ed05ae.tar.zst dexon-solidity-c3a9ae0b0657861c8cc27f60358b0bfe99ed05ae.zip |
Introducing EnumType and some Parser tests
-rw-r--r-- | AST.cpp | 6 | ||||
-rw-r--r-- | AST.h | 2 | ||||
-rw-r--r-- | Parser.cpp | 1 | ||||
-rw-r--r-- | Types.cpp | 31 | ||||
-rw-r--r-- | Types.h | 28 |
5 files changed, 59 insertions, 9 deletions
@@ -208,7 +208,7 @@ vector<pair<FixedHash<4>, FunctionTypePointer>> const& ContractDefinition::getIn TypePointer EnumDeclaration::getType(ContractDefinition const*) const { - // LTODO + // LTODO: How to get the parent EnumDefinition and return its type here? return nullptr; } @@ -274,9 +274,7 @@ void EnumDefinition::checkValidityOfMembers() const TypePointer EnumDefinition::getType(ContractDefinition const*) const { - //LTODO: - return nullptr; - // return make_shared<TypeType>(make_shared<EnumType>(*this)); + return make_shared<TypeType>(make_shared<EnumType>(*this)); } TypePointer FunctionDefinition::getType(ContractDefinition const*) const @@ -177,7 +177,7 @@ class EnumDeclaration : public Declaration virtual void accept(ASTVisitor& _visitor) override; virtual void accept(ASTConstVisitor& _visitor) const override; - TypePointer getType(ContractDefinition const*) const; + TypePointer getType(ContractDefinition const* = nullptr) const; }; /** @@ -292,6 +292,7 @@ ASTPointer<EnumDefinition> Parser::parseEnumDefinition() nodeFactory.markEndPosition(); expectToken(Token::RBrace); + expectToken(Token::Semicolon); return nodeFactory.createNode<EnumDefinition>(name, members); } @@ -662,6 +662,37 @@ u256 StructType::getStorageOffsetOfMember(string const& _name) const BOOST_THROW_EXCEPTION(InternalCompilerError() << errinfo_comment("Storage offset of non-existing member requested.")); } +TypePointer EnumType::unaryOperatorResult(Token::Value _operator) const +{ + return _operator == Token::Delete ? make_shared<VoidType>() : TypePointer(); +} + +bool EnumType::operator==(Type const& _other) const +{ + if (_other.getCategory() != getCategory()) + return false; + EnumType const& other = dynamic_cast<EnumType const&>(_other); + return other.m_enum == m_enum; +} + +string EnumType::toString() const +{ + return string("enum ") + m_enum.getName(); +} + +MemberList const& EnumType::getMembers() const +{ + // We need to lazy-initialize it because of recursive references. + if (!m_members) + { + map<string, shared_ptr<Type const>> members; + for (ASTPointer<EnumDeclaration> const& enumValue: m_enum.getMembers()) + members.insert(make_pair(enumValue->getName(), make_shared<EnumType>(m_enum))); + m_members.reset(new MemberList(members)); + } + return *m_members; +} + FunctionType::FunctionType(FunctionDefinition const& _function, bool _isInternal): m_location(_isInternal ? Location::Internal : Location::External), m_isConstant(_function.isDeclaredConst()), @@ -76,10 +76,9 @@ class Type: private boost::noncopyable, public std::enable_shared_from_this<Type public: enum class Category { - Integer, IntegerConstant, Bool, Real, String, - ByteArray, Mapping, - Contract, Struct, Function, - Void, TypeType, Modifier, Magic + Integer, IntegerConstant, Bool, Real, ByteArray, + String, Contract, Struct, Function, Enum, + Mapping, Void, TypeType, Modifier, Magic }; ///@{ @@ -369,6 +368,27 @@ private: }; /** + * The type of an enum instance, there is one distinct type per enum definition. + */ +class EnumType: public Type +{ +public: + virtual Category getCategory() const override { return Category::Enum; } + explicit EnumType(EnumDefinition const& _enum): m_enum(_enum) {} + virtual TypePointer unaryOperatorResult(Token::Value _operator) const override; + virtual bool operator==(Type const& _other) const override; + virtual unsigned getSizeOnStack() const override { return 1; /*@todo*/ } + virtual std::string toString() const override; + + virtual MemberList const& getMembers() const override; + +private: + EnumDefinition const& m_enum; + /// List of member types, will be lazy-initialized because of recursive references. + mutable std::unique_ptr<MemberList> m_members; +}; + +/** * The type of a function, identified by its (return) parameter types. * @todo the return parameters should also have names, i.e. return parameters should be a struct * type. |