Enum type conversion and member value access.

- Added tests for the type conversion part.

- Enum member value access still needs some work
This commit is contained in:
Lefteris Karapetsas 2015-02-12 17:59:52 +01:00
parent e7f40863ef
commit 012e32942d
5 changed files with 34 additions and 1 deletions

13
AST.cpp
View File

@ -277,6 +277,19 @@ TypePointer EnumDefinition::getType(ContractDefinition const*) const
return make_shared<TypeType>(make_shared<EnumType>(*this));
}
unsigned int EnumDefinition::getMemberValue(ASTString const& _member) const
{
unsigned int index = 0;
for (ASTPointer<EnumDeclaration> const& decl: m_members)
{
if (decl->getName() == _member)
return index;
++index;
}
BOOST_THROW_EXCEPTION(createTypeError("Requested unknown enum value ." + _member));
}
TypePointer FunctionDefinition::getType(ContractDefinition const*) const
{
return make_shared<FunctionType>(*this);

3
AST.h
View File

@ -348,6 +348,9 @@ public:
virtual TypePointer getType(ContractDefinition const*) const override;
/// @returns the value that the string has in the Enum
unsigned int getMemberValue(ASTString const& _member) const;
/// Checks that the members do not include any duplicate names
void checkValidityOfMembers() const;

View File

@ -489,6 +489,12 @@ void ExpressionCompiler::endVisit(MemberAccess const& _memberAccess)
m_currentLValue.retrieveValueIfLValueNotRequested(_memberAccess);
break;
}
case Type::Category::Enum:
{
EnumType const& type = dynamic_cast<EnumType const&>(*_memberAccess.getExpression().getType());
EnumDefinition const& enumDef = type.getEnumDefinition();
m_context << enumDef.getMemberValue(_memberAccess.getMemberName());
}
case Type::Category::TypeType:
{
TypeType const& type = dynamic_cast<TypeType const&>(*_memberAccess.getExpression().getType());
@ -562,6 +568,10 @@ void ExpressionCompiler::endVisit(Identifier const& _identifier)
{
// no-op
}
else if (dynamic_cast<EnumDefinition const*>(declaration))
{
// no-op
}
else
{
BOOST_THROW_EXCEPTION(InternalCompilerError() << errinfo_comment("Identifier type not expected in expression context."));
@ -746,7 +756,7 @@ void ExpressionCompiler::appendTypeConversion(Type const& _typeOnStack, Type con
}
}
else if (stackTypeCategory == Type::Category::Integer || stackTypeCategory == Type::Category::Contract ||
stackTypeCategory == Type::Category::IntegerConstant)
stackTypeCategory == Type::Category::IntegerConstant || stackTypeCategory == Type::Category::Enum)
{
if (targetTypeCategory == Type::Category::String && stackTypeCategory == Type::Category::Integer)
{

View File

@ -682,6 +682,11 @@ string EnumType::toString() const
return string("enum ") + m_enum.getName();
}
bool EnumType::isExplicitlyConvertibleTo(Type const& _convertTo) const
{
return _convertTo.getCategory() == getCategory() || _convertTo.getCategory() == Category::Integer;
}
FunctionType::FunctionType(FunctionDefinition const& _function, bool _isInternal):
m_location(_isInternal ? Location::Internal : Location::External),
m_isConstant(_function.isDeclaredConst()),

View File

@ -381,6 +381,8 @@ public:
virtual std::string toString() const override;
virtual bool isValueType() const override { return true; }
virtual bool isExplicitlyConvertibleTo(Type const& _convertTo) const override;
EnumDefinition const& getEnumDefinition() const { return m_enum; }
private: