Add function selector to FunctionDefinition AST JSON

This commit is contained in:
Gaith Hallak 2019-12-09 03:26:23 +03:00
parent 200747e298
commit 371e6a4801
6 changed files with 29 additions and 0 deletions

View File

@ -38,6 +38,7 @@ Language Features:
* Introduce ``virtual`` and ``override`` keywords. * Introduce ``virtual`` and ``override`` keywords.
* Modify ``push(element)`` for dynamic storage arrays such that it does not return the new length anymore. * Modify ``push(element)`` for dynamic storage arrays such that it does not return the new length anymore.
* Yul: Introduce ``leave`` statement that exits the current function. * Yul: Introduce ``leave`` statement that exits the current function.
* JSON AST: Add the function selector of each externally-visible FunctonDefinition to the AST JSON export.
Compiler Features: Compiler Features:
* Allow revert strings to be stripped from the binary using the ``--revert-strings`` option or the ``settings.debug.revertStrings`` setting. * Allow revert strings to be stripped from the binary using the ``--revert-strings`` option or the ``settings.debug.revertStrings`` setting.

View File

@ -368,6 +368,11 @@ string FunctionDefinition::externalSignature() const
return TypeProvider::function(*this)->externalSignature(); return TypeProvider::function(*this)->externalSignature();
} }
string FunctionDefinition::externalIdentifierHex() const
{
return TypeProvider::function(*this)->externalIdentifierHex();
}
FunctionDefinitionAnnotation& FunctionDefinition::annotation() const FunctionDefinitionAnnotation& FunctionDefinition::annotation() const
{ {
if (!m_annotation) if (!m_annotation)
@ -596,6 +601,12 @@ set<VariableDeclaration::Location> VariableDeclaration::allowedDataLocations() c
return set<Location>{ Location::Unspecified }; return set<Location>{ Location::Unspecified };
} }
string VariableDeclaration::externalIdentifierHex() const
{
solAssert(isStateVariable() && isPublic(), "Can only be called for public state variables");
return TypeProvider::function(*this)->externalIdentifierHex();
}
TypePointer VariableDeclaration::type() const TypePointer VariableDeclaration::type() const
{ {
return annotation().type; return annotation().type;

View File

@ -708,6 +708,9 @@ public:
/// arguments separated by commas all enclosed in parentheses without any spaces. /// arguments separated by commas all enclosed in parentheses without any spaces.
std::string externalSignature() const; std::string externalSignature() const;
/// @returns the external identifier of this function (the hash of the signature) as a hex string.
std::string externalIdentifierHex() const;
ContractDefinition::ContractKind inContractKind() const; ContractDefinition::ContractKind inContractKind() const;
TypePointer type() const override; TypePointer type() const override;
@ -807,6 +810,9 @@ public:
/// @returns a set of allowed storage locations for the variable. /// @returns a set of allowed storage locations for the variable.
std::set<Location> allowedDataLocations() const; std::set<Location> allowedDataLocations() const;
/// @returns the external identifier of this variable (the hash of the signature) as a hex string (works only for public state variables).
std::string externalIdentifierHex() const;
TypePointer type() const override; TypePointer type() const override;
/// @param _internal false indicates external interface is concerned, true indicates internal interface is concerned. /// @param _internal false indicates external interface is concerned, true indicates internal interface is concerned.

View File

@ -362,6 +362,8 @@ bool ASTJsonConverter::visit(FunctionDefinition const& _node)
make_pair("implemented", _node.isImplemented()), make_pair("implemented", _node.isImplemented()),
make_pair("scope", idOrNull(_node.scope())) make_pair("scope", idOrNull(_node.scope()))
}; };
if (_node.isPartOfExternalInterface())
attributes.emplace_back("functionSelector", _node.externalIdentifierHex());
if (!_node.annotation().baseFunctions.empty()) if (!_node.annotation().baseFunctions.empty())
attributes.emplace_back(make_pair("baseFunctions", getContainerIds(_node.annotation().baseFunctions, true))); attributes.emplace_back(make_pair("baseFunctions", getContainerIds(_node.annotation().baseFunctions, true)));
if (m_legacy) if (m_legacy)
@ -384,6 +386,8 @@ bool ASTJsonConverter::visit(VariableDeclaration const& _node)
make_pair("scope", idOrNull(_node.scope())), make_pair("scope", idOrNull(_node.scope())),
make_pair("typeDescriptions", typePointerToJson(_node.annotation().type, true)) make_pair("typeDescriptions", typePointerToJson(_node.annotation().type, true))
}; };
if (_node.isStateVariable() && _node.isPublic())
attributes.emplace_back("functionSelector", _node.externalIdentifierHex());
if (m_inEvent) if (m_inEvent)
attributes.emplace_back("indexed", _node.isIndexed()); attributes.emplace_back("indexed", _node.isIndexed());
if (!_node.annotation().baseFunctions.empty()) if (!_node.annotation().baseFunctions.empty())

View File

@ -3191,6 +3191,11 @@ u256 FunctionType::externalIdentifier() const
return FixedHash<4>::Arith(FixedHash<4>(dev::keccak256(externalSignature()))); return FixedHash<4>::Arith(FixedHash<4>(dev::keccak256(externalSignature())));
} }
string FunctionType::externalIdentifierHex() const
{
return FixedHash<4>(dev::keccak256(externalSignature())).hex();
}
bool FunctionType::isPure() const bool FunctionType::isPure() const
{ {
// TODO: replace this with m_stateMutability == StateMutability::Pure once // TODO: replace this with m_stateMutability == StateMutability::Pure once

View File

@ -1190,6 +1190,8 @@ public:
std::string externalSignature() const; std::string externalSignature() const;
/// @returns the external identifier of this function (the hash of the signature). /// @returns the external identifier of this function (the hash of the signature).
u256 externalIdentifier() const; u256 externalIdentifier() const;
/// @returns the external identifier of this function (the hash of the signature) as a hex string.
std::string externalIdentifierHex() const;
Declaration const& declaration() const Declaration const& declaration() const
{ {
solAssert(m_declaration, "Requested declaration from a FunctionType that has none"); solAssert(m_declaration, "Requested declaration from a FunctionType that has none");