mirror of
https://github.com/ethereum/solidity
synced 2023-10-03 13:03:40 +00:00
Contracts are Addresses.
This commit is contained in:
parent
501ad14ed1
commit
c5c8933192
@ -345,6 +345,18 @@ void ExpressionCompiler::endVisit(MemberAccess const& _memberAccess)
|
|||||||
ASTString const& member = _memberAccess.getMemberName();
|
ASTString const& member = _memberAccess.getMemberName();
|
||||||
switch (_memberAccess.getExpression().getType()->getCategory())
|
switch (_memberAccess.getExpression().getType()->getCategory())
|
||||||
{
|
{
|
||||||
|
case Type::Category::CONTRACT:
|
||||||
|
{
|
||||||
|
ContractType const& type = dynamic_cast<ContractType const&>(*_memberAccess.getExpression().getType());
|
||||||
|
u256 identifier = type.getFunctionIdentifier(member);
|
||||||
|
if (identifier != Invalid256)
|
||||||
|
{
|
||||||
|
appendTypeConversion(type, IntegerType(0, IntegerType::Modifier::ADDRESS), true);
|
||||||
|
m_context << identifier;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
// fall-through to "integer" otherwise (address)
|
||||||
|
}
|
||||||
case Type::Category::INTEGER:
|
case Type::Category::INTEGER:
|
||||||
if (member == "balance")
|
if (member == "balance")
|
||||||
{
|
{
|
||||||
@ -358,14 +370,6 @@ void ExpressionCompiler::endVisit(MemberAccess const& _memberAccess)
|
|||||||
else
|
else
|
||||||
BOOST_THROW_EXCEPTION(InternalCompilerError() << errinfo_comment("Invalid member access to integer."));
|
BOOST_THROW_EXCEPTION(InternalCompilerError() << errinfo_comment("Invalid member access to integer."));
|
||||||
break;
|
break;
|
||||||
case Type::Category::CONTRACT:
|
|
||||||
{
|
|
||||||
appendTypeConversion(*_memberAccess.getExpression().getType(),
|
|
||||||
IntegerType(0, IntegerType::Modifier::ADDRESS), true);
|
|
||||||
ContractType const& type = dynamic_cast<ContractType const&>(*_memberAccess.getExpression().getType());
|
|
||||||
m_context << type.getFunctionIdentifier(member);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
case Type::Category::MAGIC:
|
case Type::Category::MAGIC:
|
||||||
// we can ignore the kind of magic and only look at the name of the member
|
// we can ignore the kind of magic and only look at the name of the member
|
||||||
if (member == "coinbase")
|
if (member == "coinbase")
|
||||||
|
15
Types.cpp
15
Types.cpp
@ -424,15 +424,20 @@ TypePointer BoolType::binaryOperatorResult(Token::Value _operator, TypePointer c
|
|||||||
return TypePointer();
|
return TypePointer();
|
||||||
}
|
}
|
||||||
|
|
||||||
bool ContractType::isExplicitlyConvertibleTo(Type const& _convertTo) const
|
bool ContractType::isImplicitlyConvertibleTo(Type const& _convertTo) const
|
||||||
{
|
{
|
||||||
if (isImplicitlyConvertibleTo(_convertTo))
|
if (*this == _convertTo)
|
||||||
return true;
|
return true;
|
||||||
if (_convertTo.getCategory() == Category::INTEGER)
|
if (_convertTo.getCategory() == Category::INTEGER)
|
||||||
return dynamic_cast<IntegerType const&>(_convertTo).isAddress();
|
return dynamic_cast<IntegerType const&>(_convertTo).isAddress();
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool ContractType::isExplicitlyConvertibleTo(Type const& _convertTo) const
|
||||||
|
{
|
||||||
|
return isImplicitlyConvertibleTo(_convertTo) || _convertTo.getCategory() == Category::INTEGER;
|
||||||
|
}
|
||||||
|
|
||||||
bool ContractType::operator==(Type const& _other) const
|
bool ContractType::operator==(Type const& _other) const
|
||||||
{
|
{
|
||||||
if (_other.getCategory() != getCategory())
|
if (_other.getCategory() != getCategory())
|
||||||
@ -459,7 +464,9 @@ MemberList const& ContractType::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, shared_ptr<Type const>> members;
|
// All address members and all interface functions
|
||||||
|
map<string, shared_ptr<Type const>> members(IntegerType::AddressMemberList.begin(),
|
||||||
|
IntegerType::AddressMemberList.end());
|
||||||
for (auto const& it: m_contract.getInterfaceFunctions())
|
for (auto const& it: m_contract.getInterfaceFunctions())
|
||||||
members[it.second->getName()] = make_shared<FunctionType>(*it.second, false);
|
members[it.second->getName()] = make_shared<FunctionType>(*it.second, false);
|
||||||
m_members.reset(new MemberList(members));
|
m_members.reset(new MemberList(members));
|
||||||
@ -487,7 +494,7 @@ u256 ContractType::getFunctionIdentifier(string const& _functionName) const
|
|||||||
if (it->second->getName() == _functionName)
|
if (it->second->getName() == _functionName)
|
||||||
return FixedHash<4>::Arith(it->first);
|
return FixedHash<4>::Arith(it->first);
|
||||||
|
|
||||||
BOOST_THROW_EXCEPTION(InternalCompilerError() << errinfo_comment("Index of non-existing contract function requested."));
|
return Invalid256;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool StructType::operator==(Type const& _other) const
|
bool StructType::operator==(Type const& _other) const
|
||||||
|
9
Types.h
9
Types.h
@ -179,10 +179,11 @@ public:
|
|||||||
bool isAddress() const { return m_modifier == Modifier::ADDRESS; }
|
bool isAddress() const { return m_modifier == Modifier::ADDRESS; }
|
||||||
int isSigned() const { return m_modifier == Modifier::SIGNED; }
|
int isSigned() const { return m_modifier == Modifier::SIGNED; }
|
||||||
|
|
||||||
|
static const MemberList AddressMemberList;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
int m_bits;
|
int m_bits;
|
||||||
Modifier m_modifier;
|
Modifier m_modifier;
|
||||||
static const MemberList AddressMemberList;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -278,7 +279,9 @@ class ContractType: public Type
|
|||||||
public:
|
public:
|
||||||
virtual Category getCategory() const override { return Category::CONTRACT; }
|
virtual Category getCategory() const override { return Category::CONTRACT; }
|
||||||
ContractType(ContractDefinition const& _contract): m_contract(_contract) {}
|
ContractType(ContractDefinition const& _contract): m_contract(_contract) {}
|
||||||
/// Contracts can be converted to themselves and to addresses.
|
/// Contracts can be implicitly converted to super classes and to addresses.
|
||||||
|
virtual bool isImplicitlyConvertibleTo(Type const& _convertTo) const override;
|
||||||
|
/// Contracts can be converted to themselves and to integers.
|
||||||
virtual bool isExplicitlyConvertibleTo(Type const& _convertTo) const override;
|
virtual bool isExplicitlyConvertibleTo(Type const& _convertTo) const override;
|
||||||
virtual bool operator==(Type const& _other) const override;
|
virtual bool operator==(Type const& _other) const override;
|
||||||
virtual u256 getStorageSize() const override;
|
virtual u256 getStorageSize() const override;
|
||||||
@ -291,6 +294,8 @@ public:
|
|||||||
/// is not used, as this type cannot be the type of a variable or expression.
|
/// is not used, as this type cannot be the type of a variable or expression.
|
||||||
std::shared_ptr<FunctionType const> const& getConstructorType() const;
|
std::shared_ptr<FunctionType const> const& getConstructorType() const;
|
||||||
|
|
||||||
|
/// @returns the identifier of the function with the given name or Invalid256 if such a name does
|
||||||
|
/// not exist.
|
||||||
u256 getFunctionIdentifier(std::string const& _functionName) const;
|
u256 getFunctionIdentifier(std::string const& _functionName) const;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
Loading…
Reference in New Issue
Block a user