Enum Value member access should now work properly

- Also detection of duplicate enum values and tests for them have been
  added
This commit is contained in:
Lefteris Karapetsas 2015-02-13 13:32:18 +01:00
parent 012e32942d
commit 91943e7368
2 changed files with 27 additions and 14 deletions

14
AST.cpp
View File

@ -263,13 +263,15 @@ void StructDefinition::checkRecursion() const
void EnumDefinition::checkValidityOfMembers() const void EnumDefinition::checkValidityOfMembers() const
{ {
#if 0 // LTODO: Make this work for the Declarations vector<ASTPointer<EnumDeclaration>> members(getMembers());
vector<ASTPointer<ASTString>> members = getMembers(); auto compareDecls = [](ASTPointer<EnumDeclaration> a, ASTPointer<EnumDeclaration> b)
sort(begin(members), end(members)); {
for (size_t i = 0; i < members.size(); ++i) return a->getName() < b->getName();
if (members[i] == members[i + 1]) };
sort(begin(members), end(members), compareDecls);
for (size_t i = 0; i < members.size() - 1; ++i)
if (members[i]->getName() == members[i + 1]->getName())
BOOST_THROW_EXCEPTION(createTypeError("Duplicate member detected in Enum")); BOOST_THROW_EXCEPTION(createTypeError("Duplicate member detected in Enum"));
#endif
} }
TypePointer EnumDefinition::getType(ContractDefinition const*) const TypePointer EnumDefinition::getType(ContractDefinition const*) const

View File

@ -494,20 +494,31 @@ void ExpressionCompiler::endVisit(MemberAccess const& _memberAccess)
EnumType const& type = dynamic_cast<EnumType const&>(*_memberAccess.getExpression().getType()); EnumType const& type = dynamic_cast<EnumType const&>(*_memberAccess.getExpression().getType());
EnumDefinition const& enumDef = type.getEnumDefinition(); EnumDefinition const& enumDef = type.getEnumDefinition();
m_context << enumDef.getMemberValue(_memberAccess.getMemberName()); m_context << enumDef.getMemberValue(_memberAccess.getMemberName());
break;
} }
case Type::Category::TypeType: case Type::Category::TypeType:
{ {
TypeType const& type = dynamic_cast<TypeType const&>(*_memberAccess.getExpression().getType()); TypeType const& type = dynamic_cast<TypeType const&>(*_memberAccess.getExpression().getType());
ContractType const* contractType;
EnumType const* enumType;
if (type.getMembers().getMemberType(member)) if (type.getMembers().getMemberType(member))
{ {
ContractDefinition const& contract = dynamic_cast<ContractType const&>(*type.getActualType()) if ((contractType = dynamic_cast<ContractType const*>(type.getActualType().get())))
.getContractDefinition(); {
for (ASTPointer<FunctionDefinition> const& function: contract.getDefinedFunctions()) ContractDefinition const& contract = contractType->getContractDefinition();
if (function->getName() == member) for (ASTPointer<FunctionDefinition> const& function: contract.getDefinedFunctions())
{ if (function->getName() == member)
m_context << m_context.getFunctionEntryLabel(*function).pushTag(); {
return; m_context << m_context.getFunctionEntryLabel(*function).pushTag();
} return;
}
}
else if ((enumType = dynamic_cast<EnumType const*>(type.getActualType().get())))
{
EnumDefinition const &enumDef = enumType->getEnumDefinition();
m_context << enumDef.getMemberValue(_memberAccess.getMemberName());
return;
}
} }
BOOST_THROW_EXCEPTION(InternalCompilerError() << errinfo_comment("Invalid member access to " + type.toString())); BOOST_THROW_EXCEPTION(InternalCompilerError() << errinfo_comment("Invalid member access to " + type.toString()));
} }