MemberList.Member's last argument (declaration) made mandatory to avoid accidental missing out during construction.

This commit is contained in:
Christian Parpart 2021-02-08 13:33:39 +01:00 committed by chriseth
parent b0d3412fa9
commit 9ca389d6cd
3 changed files with 38 additions and 26 deletions

View File

@ -111,6 +111,13 @@ util::Result<TypePointers> transformParametersToExternal(TypePointers const& _pa
} }
MemberList::Member::Member(Declaration const* _declaration, Type const* _type):
name(_declaration->name()),
type(_type),
declaration(_declaration)
{
}
void Type::clearCache() const void Type::clearCache() const
{ {
m_members.clear(); m_members.clear();
@ -365,7 +372,7 @@ MemberList::MemberMap Type::boundFunctions(Type const& _type, ASTNode const& _sc
FunctionTypePointer fun = FunctionTypePointer fun =
dynamic_cast<FunctionType const&>(*function->typeViaContractName()).asBoundFunction(); dynamic_cast<FunctionType const&>(*function->typeViaContractName()).asBoundFunction();
if (_type.isImplicitlyConvertibleTo(*fun->selfType())) if (_type.isImplicitlyConvertibleTo(*fun->selfType()))
members.emplace_back(function->name(), fun, function); members.emplace_back(function, fun);
} }
} }
@ -1985,9 +1992,8 @@ MemberList::MemberMap ContractType::nativeMembers(ASTNode const*) const
if (!m_contract.isLibrary()) if (!m_contract.isLibrary())
for (auto const& it: m_contract.interfaceFunctions()) for (auto const& it: m_contract.interfaceFunctions())
members.emplace_back( members.emplace_back(
it.second->declaration().name(), &it.second->declaration(),
it.second->asExternallyCallableFunction(m_contract.isLibrary()), it.second->asExternallyCallableFunction(m_contract.isLibrary())
&it.second->declaration()
); );
return members; return members;
@ -2213,9 +2219,8 @@ MemberList::MemberMap StructType::nativeMembers(ASTNode const*) const
solAssert(type, ""); solAssert(type, "");
solAssert(!(location() != DataLocation::Storage && type->containsNestedMapping()), ""); solAssert(!(location() != DataLocation::Storage && type->containsNestedMapping()), "");
members.emplace_back( members.emplace_back(
variable->name(), variable.get(),
copyForLocationIfReference(type), copyForLocationIfReference(type)
variable.get()
); );
} }
return members; return members;
@ -3687,7 +3692,7 @@ MemberList::MemberMap TypeType::nativeMembers(ASTNode const* _currentScope) cons
break; break;
} }
if (!functionWithEqualArgumentsFound) if (!functionWithEqualArgumentsFound)
members.emplace_back(function->name(), functionType, function); members.emplace_back(function, functionType);
} }
} }
else else
@ -3708,15 +3713,15 @@ MemberList::MemberMap TypeType::nativeMembers(ASTNode const* _currentScope) cons
auto const* functionDefinition = dynamic_cast<FunctionDefinition const*>(declaration); auto const* functionDefinition = dynamic_cast<FunctionDefinition const*>(declaration);
functionDefinition && !functionDefinition->isImplemented() functionDefinition && !functionDefinition->isImplemented()
) )
members.emplace_back(declaration->name(), declaration->typeViaContractName(), declaration); members.emplace_back(declaration, declaration->typeViaContractName());
else else
members.emplace_back(declaration->name(), declaration->type(), declaration); members.emplace_back(declaration, declaration->type());
} }
else if ( else if (
(contract.isLibrary() && declaration->isVisibleAsLibraryMember()) || (contract.isLibrary() && declaration->isVisibleAsLibraryMember()) ||
declaration->isVisibleViaContractTypeAccess() declaration->isVisibleViaContractTypeAccess()
) )
members.emplace_back(declaration->name(), declaration->typeViaContractName(), declaration); members.emplace_back(declaration, declaration->typeViaContractName());
} }
} }
} }
@ -3725,7 +3730,7 @@ MemberList::MemberMap TypeType::nativeMembers(ASTNode const* _currentScope) cons
EnumDefinition const& enumDef = dynamic_cast<EnumType const&>(*m_actualType).enumDefinition(); EnumDefinition const& enumDef = dynamic_cast<EnumType const&>(*m_actualType).enumDefinition();
auto enumType = TypeProvider::enumType(enumDef); auto enumType = TypeProvider::enumType(enumDef);
for (ASTPointer<EnumValue> const& enumValue: enumDef.members()) for (ASTPointer<EnumValue> const& enumValue: enumDef.members())
members.emplace_back(enumValue->name(), enumType, enumValue.get()); members.emplace_back(enumValue.get(), enumType);
} }
return members; return members;
} }
@ -3801,9 +3806,12 @@ bool ModuleType::operator==(Type const& _other) const
MemberList::MemberMap ModuleType::nativeMembers(ASTNode const*) const MemberList::MemberMap ModuleType::nativeMembers(ASTNode const*) const
{ {
MemberList::MemberMap symbols; MemberList::MemberMap symbols;
for (auto const& symbolName: *m_sourceUnit.annotation().exportedSymbols) for (auto const& [name, declarations]: *m_sourceUnit.annotation().exportedSymbols)
for (Declaration const* symbol: symbolName.second) for (Declaration const* symbol: declarations)
symbols.emplace_back(symbolName.first, symbol->type(), symbol); {
solAssert(name == symbol->name(), "");
symbols.emplace_back(symbol, symbol->type());
}
return symbols; return symbols;
} }

View File

@ -100,15 +100,19 @@ class MemberList
public: public:
struct Member struct Member
{ {
Member(std::string _name, Type const* _type, Declaration const* _declaration = nullptr): /// Manual constructor for members that are not taken from a declaration.
name(std::move(_name)), Member(char const* _name, Type const* _type):
name(_name),
type(_type), type(_type),
declaration(_declaration) declaration(nullptr)
{ {
} }
/// Constructs a Member with the name extracted from @p _declaration's name.
Member(Declaration const* _declaration, Type const* _type);
std::string name; std::string name;
Type const* type; Type const* type = nullptr;
Declaration const* declaration = nullptr; Declaration const* declaration = nullptr;
}; };

View File

@ -78,9 +78,9 @@ BOOST_AUTO_TEST_CASE(ufixed_types)
BOOST_AUTO_TEST_CASE(storage_layout_simple) BOOST_AUTO_TEST_CASE(storage_layout_simple)
{ {
MemberList members(MemberList::MemberMap({ MemberList members(MemberList::MemberMap({
{string("first"), TypeProvider::fromElementaryTypeName("uint128")}, {"first", TypeProvider::fromElementaryTypeName("uint128")},
{string("second"), TypeProvider::fromElementaryTypeName("uint120")}, {"second", TypeProvider::fromElementaryTypeName("uint120")},
{string("wraps"), TypeProvider::fromElementaryTypeName("uint16")} {"wraps", TypeProvider::fromElementaryTypeName("uint16")}
})); }));
BOOST_REQUIRE_EQUAL(u256(2), members.storageSize()); BOOST_REQUIRE_EQUAL(u256(2), members.storageSize());
BOOST_REQUIRE(members.memberStorageOffset("first") != nullptr); BOOST_REQUIRE(members.memberStorageOffset("first") != nullptr);
@ -94,13 +94,13 @@ BOOST_AUTO_TEST_CASE(storage_layout_simple)
BOOST_AUTO_TEST_CASE(storage_layout_mapping) BOOST_AUTO_TEST_CASE(storage_layout_mapping)
{ {
MemberList members(MemberList::MemberMap({ MemberList members(MemberList::MemberMap({
{string("first"), TypeProvider::fromElementaryTypeName("uint128")}, {"first", TypeProvider::fromElementaryTypeName("uint128")},
{string("second"), TypeProvider::mapping( {"second", TypeProvider::mapping(
TypeProvider::fromElementaryTypeName("uint8"), TypeProvider::fromElementaryTypeName("uint8"),
TypeProvider::fromElementaryTypeName("uint8") TypeProvider::fromElementaryTypeName("uint8")
)}, )},
{string("third"), TypeProvider::fromElementaryTypeName("uint16")}, {"third", TypeProvider::fromElementaryTypeName("uint16")},
{string("final"), TypeProvider::mapping( {"final", TypeProvider::mapping(
TypeProvider::fromElementaryTypeName("uint8"), TypeProvider::fromElementaryTypeName("uint8"),
TypeProvider::fromElementaryTypeName("uint8") TypeProvider::fromElementaryTypeName("uint8")
)}, )},