Browse Source

getMemberTypes() respects source order

cl-refactor
Lefteris Karapetsas 10 years ago
parent
commit
307e42a6c3
  1. 30
      libsolidity/Types.cpp
  2. 8
      libsolidity/Types.h

30
libsolidity/Types.cpp

@ -573,19 +573,19 @@ MemberList const& ContractType::getMembers() const
if (!m_members) if (!m_members)
{ {
// All address members and all interface functions // All address members and all interface functions
map<string, TypePointer> members(IntegerType::AddressMemberList.begin(), vector<pair<string, TypePointer>> members(IntegerType::AddressMemberList.begin(),
IntegerType::AddressMemberList.end()); IntegerType::AddressMemberList.end());
if (m_super) if (m_super)
{ {
for (ContractDefinition const* base: m_contract.getLinearizedBaseContracts()) for (ContractDefinition const* base: m_contract.getLinearizedBaseContracts())
for (ASTPointer<FunctionDefinition> const& function: base->getDefinedFunctions()) for (ASTPointer<FunctionDefinition> const& function: base->getDefinedFunctions())
if (!function->isConstructor() && !function->getName().empty() && if (!function->isConstructor() && !function->getName().empty()&&
function->isVisibleInDerivedContracts()) function->isVisibleInDerivedContracts())
members.insert(make_pair(function->getName(), make_shared<FunctionType>(*function, true))); members.push_back(make_pair(function->getName(), make_shared<FunctionType>(*function, true)));
} }
else else
for (auto const& it: m_contract.getInterfaceFunctions()) for (auto const& it: m_contract.getInterfaceFunctions())
members[it.second->getDeclaration().getName()] = it.second; members.push_back(make_pair(it.second->getDeclaration().getName(), it.second));
m_members.reset(new MemberList(members)); m_members.reset(new MemberList(members));
} }
return *m_members; return *m_members;
@ -653,9 +653,9 @@ MemberList const& StructType::getMembers() const
// We need to lazy-initialize it because of recursive references. // We need to lazy-initialize it because of recursive references.
if (!m_members) if (!m_members)
{ {
map<string, TypePointer> members; vector<pair<string, TypePointer>> members;
for (ASTPointer<VariableDeclaration> const& variable: m_struct.getMembers()) for (ASTPointer<VariableDeclaration> const& variable: m_struct.getMembers())
members[variable->getName()] = variable->getType(); members.push_back(make_pair(variable->getName(), variable->getType()));
m_members.reset(new MemberList(members)); m_members.reset(new MemberList(members));
} }
return *m_members; return *m_members;
@ -857,15 +857,15 @@ MemberList const& FunctionType::getMembers() const
case Location::Bare: case Location::Bare:
if (!m_members) if (!m_members)
{ {
map<string, TypePointer> members{ vector<pair<string, TypePointer>> members{
{"gas", make_shared<FunctionType>(parseElementaryTypeVector({"uint"}),
TypePointers{copyAndSetGasOrValue(true, false)},
Location::SetGas, false, m_gasSet, m_valueSet)},
{"value", make_shared<FunctionType>(parseElementaryTypeVector({"uint"}), {"value", make_shared<FunctionType>(parseElementaryTypeVector({"uint"}),
TypePointers{copyAndSetGasOrValue(false, true)}, TypePointers{copyAndSetGasOrValue(false, true)},
Location::SetValue, false, m_gasSet, m_valueSet)}}; Location::SetValue, false, m_gasSet, m_valueSet)}};
if (m_location == Location::Creation) if (m_location != Location::Creation)
members.erase("gas"); members.push_back(make_pair("gas", make_shared<FunctionType>(
parseElementaryTypeVector({"uint"}),
TypePointers{copyAndSetGasOrValue(true, false)},
Location::SetGas, false, m_gasSet, m_valueSet)));
m_members.reset(new MemberList(members)); m_members.reset(new MemberList(members));
} }
return *m_members; return *m_members;
@ -959,7 +959,7 @@ MemberList const& TypeType::getMembers() const
// We need to lazy-initialize it because of recursive references. // We need to lazy-initialize it because of recursive references.
if (!m_members) if (!m_members)
{ {
map<string, TypePointer> members; vector<pair<string, TypePointer>> members;
if (m_actualType->getCategory() == Category::Contract && m_currentContract != nullptr) if (m_actualType->getCategory() == Category::Contract && m_currentContract != nullptr)
{ {
ContractDefinition const& contract = dynamic_cast<ContractType const&>(*m_actualType).getContractDefinition(); ContractDefinition const& contract = dynamic_cast<ContractType const&>(*m_actualType).getContractDefinition();
@ -969,14 +969,14 @@ MemberList const& TypeType::getMembers() const
// functions. Note that this does not add inherited functions on purpose. // functions. Note that this does not add inherited functions on purpose.
for (ASTPointer<FunctionDefinition> const& f: contract.getDefinedFunctions()) for (ASTPointer<FunctionDefinition> const& f: contract.getDefinedFunctions())
if (!f->isConstructor() && !f->getName().empty() && f->isVisibleInDerivedContracts()) if (!f->isConstructor() && !f->getName().empty() && f->isVisibleInDerivedContracts())
members[f->getName()] = make_shared<FunctionType>(*f); members.push_back(make_pair(f->getName(), make_shared<FunctionType>(*f)));
} }
else if (m_actualType->getCategory() == Category::Enum) else if (m_actualType->getCategory() == Category::Enum)
{ {
EnumDefinition const& enumDef = dynamic_cast<EnumType const&>(*m_actualType).getEnumDefinition(); EnumDefinition const& enumDef = dynamic_cast<EnumType const&>(*m_actualType).getEnumDefinition();
auto enumType = make_shared<EnumType>(enumDef); auto enumType = make_shared<EnumType>(enumDef);
for (ASTPointer<EnumValue> const& enumValue: enumDef.getMembers()) for (ASTPointer<EnumValue> const& enumValue: enumDef.getMembers())
members.insert(make_pair(enumValue->getName(), enumType)); members.push_back(make_pair(enumValue->getName(), enumType));
} }
m_members.reset(new MemberList(members)); m_members.reset(new MemberList(members));
} }

8
libsolidity/Types.h

@ -50,14 +50,16 @@ using TypePointers = std::vector<TypePointer>;
class MemberList class MemberList
{ {
public: public:
using MemberMap = std::map<std::string, TypePointer>; using MemberMap = std::vector<std::pair<std::string, TypePointer>>;
MemberList() {} MemberList() {}
explicit MemberList(MemberMap const& _members): m_memberTypes(_members) {} explicit MemberList(MemberMap const& _members): m_memberTypes(_members) {}
TypePointer getMemberType(std::string const& _name) const TypePointer getMemberType(std::string const& _name) const
{ {
auto it = m_memberTypes.find(_name); for (auto const& it: m_memberTypes)
return it != m_memberTypes.end() ? it->second : TypePointer(); if (it.first == _name)
return it.second;
return TypePointer();
} }
MemberMap::const_iterator begin() const { return m_memberTypes.begin(); } MemberMap::const_iterator begin() const { return m_memberTypes.begin(); }

Loading…
Cancel
Save