mirror of
https://github.com/ethereum/solidity
synced 2023-10-03 13:03:40 +00:00
Introducing EnumType and some Parser tests
This commit is contained in:
parent
86e1d671cc
commit
c3a9ae0b06
6
AST.cpp
6
AST.cpp
@ -208,7 +208,7 @@ vector<pair<FixedHash<4>, FunctionTypePointer>> const& ContractDefinition::getIn
|
|||||||
|
|
||||||
TypePointer EnumDeclaration::getType(ContractDefinition const*) const
|
TypePointer EnumDeclaration::getType(ContractDefinition const*) const
|
||||||
{
|
{
|
||||||
// LTODO
|
// LTODO: How to get the parent EnumDefinition and return its type here?
|
||||||
return nullptr;
|
return nullptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -274,9 +274,7 @@ void EnumDefinition::checkValidityOfMembers() const
|
|||||||
|
|
||||||
TypePointer EnumDefinition::getType(ContractDefinition const*) const
|
TypePointer EnumDefinition::getType(ContractDefinition const*) const
|
||||||
{
|
{
|
||||||
//LTODO:
|
return make_shared<TypeType>(make_shared<EnumType>(*this));
|
||||||
return nullptr;
|
|
||||||
// return make_shared<TypeType>(make_shared<EnumType>(*this));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
TypePointer FunctionDefinition::getType(ContractDefinition const*) const
|
TypePointer FunctionDefinition::getType(ContractDefinition const*) const
|
||||||
|
2
AST.h
2
AST.h
@ -177,7 +177,7 @@ class EnumDeclaration : public Declaration
|
|||||||
|
|
||||||
virtual void accept(ASTVisitor& _visitor) override;
|
virtual void accept(ASTVisitor& _visitor) override;
|
||||||
virtual void accept(ASTConstVisitor& _visitor) const 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();
|
nodeFactory.markEndPosition();
|
||||||
expectToken(Token::RBrace);
|
expectToken(Token::RBrace);
|
||||||
|
expectToken(Token::Semicolon);
|
||||||
return nodeFactory.createNode<EnumDefinition>(name, members);
|
return nodeFactory.createNode<EnumDefinition>(name, members);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
31
Types.cpp
31
Types.cpp
@ -662,6 +662,37 @@ u256 StructType::getStorageOffsetOfMember(string const& _name) const
|
|||||||
BOOST_THROW_EXCEPTION(InternalCompilerError() << errinfo_comment("Storage offset of non-existing member requested."));
|
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):
|
FunctionType::FunctionType(FunctionDefinition const& _function, bool _isInternal):
|
||||||
m_location(_isInternal ? Location::Internal : Location::External),
|
m_location(_isInternal ? Location::Internal : Location::External),
|
||||||
m_isConstant(_function.isDeclaredConst()),
|
m_isConstant(_function.isDeclaredConst()),
|
||||||
|
28
Types.h
28
Types.h
@ -76,10 +76,9 @@ class Type: private boost::noncopyable, public std::enable_shared_from_this<Type
|
|||||||
public:
|
public:
|
||||||
enum class Category
|
enum class Category
|
||||||
{
|
{
|
||||||
Integer, IntegerConstant, Bool, Real, String,
|
Integer, IntegerConstant, Bool, Real, ByteArray,
|
||||||
ByteArray, Mapping,
|
String, Contract, Struct, Function, Enum,
|
||||||
Contract, Struct, Function,
|
Mapping, Void, TypeType, Modifier, Magic
|
||||||
Void, TypeType, Modifier, Magic
|
|
||||||
};
|
};
|
||||||
|
|
||||||
///@{
|
///@{
|
||||||
@ -368,6 +367,27 @@ private:
|
|||||||
mutable std::unique_ptr<MemberList> m_members;
|
mutable std::unique_ptr<MemberList> m_members;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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.
|
* 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
|
* @todo the return parameters should also have names, i.e. return parameters should be a struct
|
||||||
|
Loading…
Reference in New Issue
Block a user