Merge pull request #7921 from ghallak/func-selector-ast-json

Add function selector to FunctionDefinition AST JSON
This commit is contained in:
chriseth 2019-12-16 14:07:35 +01:00 committed by GitHub
commit bdd338a8de
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
50 changed files with 87 additions and 1 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

@ -3174,6 +3174,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");

View File

@ -123,6 +123,7 @@ JSON AST:
"attributes": "attributes":
{ {
"documentation": null, "documentation": null,
"functionSelector": "af11c34c",
"implemented": true, "implemented": true,
"isConstructor": false, "isConstructor": false,
"kind": "function", "kind": "function",

View File

@ -1 +1 @@
{"sources":{"A":{"ast":{"absolutePath":"A","exportedSymbols":{"C":[6]},"id":7,"nodeType":"SourceUnit","nodes":[{"id":1,"literals":["solidity",">=","0.0"],"nodeType":"PragmaDirective","src":"0:22:0"},{"abstract":false,"baseContracts":[],"contractDependencies":[],"contractKind":"contract","documentation":null,"fullyImplemented":true,"id":6,"linearizedBaseContracts":[6],"name":"C","nodeType":"ContractDefinition","nodes":[{"body":{"id":4,"nodeType":"Block","src":"61:2:0","statements":[]},"documentation":null,"id":5,"implemented":true,"kind":"function","modifiers":[],"name":"f","nodeType":"FunctionDefinition","overrides":null,"parameters":{"id":2,"nodeType":"ParameterList","parameters":[],"src":"46:2:0"},"returnParameters":{"id":3,"nodeType":"ParameterList","parameters":[],"src":"61:0:0"},"scope":6,"src":"36:27:0","stateMutability":"pure","virtual":false,"visibility":"public"}],"scope":7,"src":"23:42:0"}],"src":"0:65:0"},"id":0}}} {"sources":{"A":{"ast":{"absolutePath":"A","exportedSymbols":{"C":[6]},"id":7,"nodeType":"SourceUnit","nodes":[{"id":1,"literals":["solidity",">=","0.0"],"nodeType":"PragmaDirective","src":"0:22:0"},{"abstract":false,"baseContracts":[],"contractDependencies":[],"contractKind":"contract","documentation":null,"fullyImplemented":true,"id":6,"linearizedBaseContracts":[6],"name":"C","nodeType":"ContractDefinition","nodes":[{"body":{"id":4,"nodeType":"Block","src":"61:2:0","statements":[]},"documentation":null,"functionSelector":"26121ff0","id":5,"implemented":true,"kind":"function","modifiers":[],"name":"f","nodeType":"FunctionDefinition","overrides":null,"parameters":{"id":2,"nodeType":"ParameterList","parameters":[],"src":"46:2:0"},"returnParameters":{"id":3,"nodeType":"ParameterList","parameters":[],"src":"61:0:0"},"scope":6,"src":"36:27:0","stateMutability":"pure","virtual":false,"visibility":"public"}],"scope":7,"src":"23:42:0"}],"src":"0:65:0"},"id":0}}}

View File

@ -29,6 +29,7 @@
[ [
{ {
"constant": false, "constant": false,
"functionSelector": "97682884",
"id": 4, "id": 4,
"name": "m", "name": "m",
"nodeType": "VariableDeclaration", "nodeType": "VariableDeclaration",
@ -489,6 +490,7 @@
] ]
}, },
"documentation": null, "documentation": null,
"functionSelector": "fc68521a",
"id": 38, "id": 38,
"implemented": true, "implemented": true,
"kind": "function", "kind": "function",

View File

@ -40,6 +40,7 @@
"attributes": "attributes":
{ {
"constant": false, "constant": false,
"functionSelector": "97682884",
"name": "m", "name": "m",
"overrides": null, "overrides": null,
"scope": 39, "scope": 39,
@ -93,6 +94,7 @@
"attributes": "attributes":
{ {
"documentation": null, "documentation": null,
"functionSelector": "fc68521a",
"implemented": true, "implemented": true,
"isConstructor": false, "isConstructor": false,
"kind": "function", "kind": "function",

View File

@ -132,6 +132,7 @@
] ]
}, },
"documentation": null, "documentation": null,
"functionSelector": "b582ec5f",
"id": 5, "id": 5,
"implemented": true, "implemented": true,
"kind": "function", "kind": "function",

View File

@ -40,6 +40,7 @@
"attributes": "attributes":
{ {
"documentation": null, "documentation": null,
"functionSelector": "b582ec5f",
"implemented": true, "implemented": true,
"isConstructor": false, "isConstructor": false,
"kind": "function", "kind": "function",

View File

@ -119,6 +119,7 @@
] ]
}, },
"documentation": null, "documentation": null,
"functionSelector": "b8c9d365",
"id": 5, "id": 5,
"implemented": true, "implemented": true,
"kind": "function", "kind": "function",

View File

@ -40,6 +40,7 @@
"attributes": "attributes":
{ {
"documentation": null, "documentation": null,
"functionSelector": "b8c9d365",
"implemented": true, "implemented": true,
"isConstructor": false, "isConstructor": false,
"kind": "function", "kind": "function",

View File

@ -69,6 +69,7 @@
] ]
}, },
"documentation": null, "documentation": null,
"functionSelector": "ece866b9",
"id": 5, "id": 5,
"implemented": true, "implemented": true,
"kind": "function", "kind": "function",

View File

@ -40,6 +40,7 @@
"attributes": "attributes":
{ {
"documentation": null, "documentation": null,
"functionSelector": "ece866b9",
"implemented": true, "implemented": true,
"isConstructor": false, "isConstructor": false,
"kind": "function", "kind": "function",

View File

@ -132,6 +132,7 @@
] ]
}, },
"documentation": null, "documentation": null,
"functionSelector": "e2179b8e",
"id": 5, "id": 5,
"implemented": true, "implemented": true,
"kind": "function", "kind": "function",

View File

@ -40,6 +40,7 @@
"attributes": "attributes":
{ {
"documentation": null, "documentation": null,
"functionSelector": "e2179b8e",
"implemented": true, "implemented": true,
"isConstructor": false, "isConstructor": false,
"kind": "function", "kind": "function",

View File

@ -200,6 +200,7 @@
] ]
}, },
"documentation": null, "documentation": null,
"functionSelector": "ffae15ba",
"id": 10, "id": 10,
"implemented": true, "implemented": true,
"kind": "function", "kind": "function",

View File

@ -117,6 +117,7 @@
"attributes": "attributes":
{ {
"documentation": null, "documentation": null,
"functionSelector": "ffae15ba",
"implemented": true, "implemented": true,
"isConstructor": false, "isConstructor": false,
"kind": "function", "kind": "function",

View File

@ -73,6 +73,7 @@
] ]
}, },
"documentation": null, "documentation": null,
"functionSelector": "5a2ee019",
"id": 5, "id": 5,
"implemented": true, "implemented": true,
"kind": "function", "kind": "function",

View File

@ -40,6 +40,7 @@
"attributes": "attributes":
{ {
"documentation": null, "documentation": null,
"functionSelector": "5a2ee019",
"implemented": true, "implemented": true,
"isConstructor": false, "isConstructor": false,
"kind": "function", "kind": "function",

View File

@ -165,6 +165,7 @@
] ]
}, },
"documentation": null, "documentation": null,
"functionSelector": "26121ff0",
"id": 5, "id": 5,
"implemented": true, "implemented": true,
"kind": "function", "kind": "function",

View File

@ -40,6 +40,7 @@
"attributes": "attributes":
{ {
"documentation": null, "documentation": null,
"functionSelector": "26121ff0",
"implemented": true, "implemented": true,
"isConstructor": false, "isConstructor": false,
"kind": "function", "kind": "function",

View File

@ -124,6 +124,7 @@
] ]
}, },
"documentation": null, "documentation": null,
"functionSelector": "26121ff0",
"id": 8, "id": 8,
"implemented": true, "implemented": true,
"kind": "function", "kind": "function",

View File

@ -40,6 +40,7 @@
"attributes": "attributes":
{ {
"documentation": null, "documentation": null,
"functionSelector": "26121ff0",
"implemented": true, "implemented": true,
"isConstructor": false, "isConstructor": false,
"kind": "function", "kind": "function",

View File

@ -150,6 +150,7 @@
"statements": [] "statements": []
}, },
"documentation": "Some comment on fn.", "documentation": "Some comment on fn.",
"functionSelector": "a4a2c40b",
"id": 14, "id": 14,
"implemented": true, "implemented": true,
"kind": "function", "kind": "function",

View File

@ -109,6 +109,7 @@
"attributes": "attributes":
{ {
"documentation": "Some comment on fn.", "documentation": "Some comment on fn.",
"functionSelector": "a4a2c40b",
"implemented": true, "implemented": true,
"isConstructor": false, "isConstructor": false,
"kind": "function", "kind": "function",

View File

@ -36,6 +36,7 @@
"statements": [] "statements": []
}, },
"documentation": null, "documentation": null,
"functionSelector": "d6cd4974",
"id": 16, "id": 16,
"implemented": true, "implemented": true,
"kind": "function", "kind": "function",

View File

@ -40,6 +40,7 @@
"attributes": "attributes":
{ {
"documentation": null, "documentation": null,
"functionSelector": "d6cd4974",
"implemented": true, "implemented": true,
"isConstructor": false, "isConstructor": false,
"kind": "function", "kind": "function",

View File

@ -142,6 +142,7 @@
] ]
}, },
"documentation": null, "documentation": null,
"functionSelector": "26121ff0",
"id": 10, "id": 10,
"implemented": true, "implemented": true,
"kind": "function", "kind": "function",

View File

@ -40,6 +40,7 @@
"attributes": "attributes":
{ {
"documentation": null, "documentation": null,
"functionSelector": "26121ff0",
"implemented": true, "implemented": true,
"isConstructor": false, "isConstructor": false,
"kind": "function", "kind": "function",

View File

@ -149,6 +149,7 @@
] ]
}, },
"documentation": null, "documentation": null,
"functionSelector": "26121ff0",
"id": 14, "id": 14,
"implemented": true, "implemented": true,
"kind": "function", "kind": "function",

View File

@ -83,6 +83,7 @@
"attributes": "attributes":
{ {
"documentation": null, "documentation": null,
"functionSelector": "26121ff0",
"implemented": true, "implemented": true,
"isConstructor": false, "isConstructor": false,
"kind": "function", "kind": "function",

View File

@ -99,6 +99,7 @@
"statements": [] "statements": []
}, },
"documentation": null, "documentation": null,
"functionSelector": "28811f59",
"id": 13, "id": 13,
"implemented": true, "implemented": true,
"kind": "function", "kind": "function",

View File

@ -107,6 +107,7 @@
"attributes": "attributes":
{ {
"documentation": null, "documentation": null,
"functionSelector": "28811f59",
"implemented": true, "implemented": true,
"isConstructor": false, "isConstructor": false,
"kind": "function", "kind": "function",

View File

@ -99,6 +99,7 @@
"statements": [] "statements": []
}, },
"documentation": null, "documentation": null,
"functionSelector": "28811f59",
"id": 13, "id": 13,
"implemented": true, "implemented": true,
"kind": "function", "kind": "function",

View File

@ -107,6 +107,7 @@
"attributes": "attributes":
{ {
"documentation": null, "documentation": null,
"functionSelector": "28811f59",
"implemented": true, "implemented": true,
"isConstructor": false, "isConstructor": false,
"kind": "function", "kind": "function",

View File

@ -89,6 +89,7 @@
] ]
}, },
"documentation": null, "documentation": null,
"functionSelector": "26121ff0",
"id": 7, "id": 7,
"implemented": true, "implemented": true,
"kind": "function", "kind": "function",

View File

@ -40,6 +40,7 @@
"attributes": "attributes":
{ {
"documentation": null, "documentation": null,
"functionSelector": "26121ff0",
"implemented": true, "implemented": true,
"isConstructor": false, "isConstructor": false,
"kind": "function", "kind": "function",

View File

@ -44,6 +44,7 @@
"statements": [] "statements": []
}, },
"documentation": null, "documentation": null,
"functionSelector": "a399b6a2",
"id": 4, "id": 4,
"implemented": true, "implemented": true,
"kind": "function", "kind": "function",
@ -120,6 +121,7 @@
{ {
"body": null, "body": null,
"documentation": null, "documentation": null,
"functionSelector": "c2985578",
"id": 10, "id": 10,
"implemented": false, "implemented": false,
"kind": "function", "kind": "function",
@ -160,6 +162,7 @@
"statements": [] "statements": []
}, },
"documentation": null, "documentation": null,
"functionSelector": "a399b6a2",
"id": 15, "id": 15,
"implemented": true, "implemented": true,
"kind": "function", "kind": "function",
@ -254,6 +257,7 @@
"statements": [] "statements": []
}, },
"documentation": null, "documentation": null,
"functionSelector": "c2985578",
"id": 23, "id": 23,
"implemented": true, "implemented": true,
"kind": "function", "kind": "function",
@ -300,6 +304,7 @@
"statements": [] "statements": []
}, },
"documentation": null, "documentation": null,
"functionSelector": "a399b6a2",
"id": 30, "id": 30,
"implemented": true, "implemented": true,
"kind": "function", "kind": "function",

View File

@ -48,6 +48,7 @@
"attributes": "attributes":
{ {
"documentation": null, "documentation": null,
"functionSelector": "a399b6a2",
"implemented": true, "implemented": true,
"isConstructor": false, "isConstructor": false,
"kind": "function", "kind": "function",
@ -163,6 +164,7 @@
{ {
"body": null, "body": null,
"documentation": null, "documentation": null,
"functionSelector": "c2985578",
"implemented": false, "implemented": false,
"isConstructor": false, "isConstructor": false,
"kind": "function", "kind": "function",
@ -218,6 +220,7 @@
4 4
], ],
"documentation": null, "documentation": null,
"functionSelector": "a399b6a2",
"implemented": true, "implemented": true,
"isConstructor": false, "isConstructor": false,
"kind": "function", "kind": "function",
@ -349,6 +352,7 @@
10 10
], ],
"documentation": null, "documentation": null,
"functionSelector": "c2985578",
"implemented": true, "implemented": true,
"isConstructor": false, "isConstructor": false,
"kind": "function", "kind": "function",
@ -428,6 +432,7 @@
15 15
], ],
"documentation": null, "documentation": null,
"functionSelector": "a399b6a2",
"implemented": true, "implemented": true,
"isConstructor": false, "isConstructor": false,
"kind": "function", "kind": "function",

View File

@ -93,6 +93,7 @@
] ]
}, },
"documentation": null, "documentation": null,
"functionSelector": "26121ff0",
"id": 10, "id": 10,
"implemented": true, "implemented": true,
"kind": "function", "kind": "function",

View File

@ -40,6 +40,7 @@
"attributes": "attributes":
{ {
"documentation": null, "documentation": null,
"functionSelector": "26121ff0",
"implemented": true, "implemented": true,
"isConstructor": false, "isConstructor": false,
"kind": "function", "kind": "function",

View File

@ -105,6 +105,7 @@
] ]
}, },
"documentation": null, "documentation": null,
"functionSelector": "26121ff0",
"id": 11, "id": 11,
"implemented": true, "implemented": true,
"kind": "function", "kind": "function",

View File

@ -40,6 +40,7 @@
"attributes": "attributes":
{ {
"documentation": null, "documentation": null,
"functionSelector": "26121ff0",
"implemented": true, "implemented": true,
"isConstructor": false, "isConstructor": false,
"kind": "function", "kind": "function",

View File

@ -127,6 +127,7 @@
] ]
}, },
"documentation": null, "documentation": null,
"functionSelector": "26121ff0",
"id": 10, "id": 10,
"implemented": true, "implemented": true,
"kind": "function", "kind": "function",

View File

@ -40,6 +40,7 @@
"attributes": "attributes":
{ {
"documentation": null, "documentation": null,
"functionSelector": "26121ff0",
"implemented": true, "implemented": true,
"isConstructor": false, "isConstructor": false,
"kind": "function", "kind": "function",

View File

@ -44,6 +44,7 @@
"statements": [] "statements": []
}, },
"documentation": null, "documentation": null,
"functionSelector": "26121ff0",
"id": 4, "id": 4,
"implemented": true, "implemented": true,
"kind": "function", "kind": "function",
@ -100,6 +101,7 @@
"statements": [] "statements": []
}, },
"documentation": null, "documentation": null,
"functionSelector": "26121ff0",
"id": 9, "id": 9,
"implemented": true, "implemented": true,
"kind": "function", "kind": "function",
@ -209,6 +211,7 @@
"statements": [] "statements": []
}, },
"documentation": null, "documentation": null,
"functionSelector": "26121ff0",
"id": 21, "id": 21,
"implemented": true, "implemented": true,
"kind": "function", "kind": "function",

View File

@ -48,6 +48,7 @@
"attributes": "attributes":
{ {
"documentation": null, "documentation": null,
"functionSelector": "26121ff0",
"implemented": true, "implemented": true,
"isConstructor": false, "isConstructor": false,
"kind": "function", "kind": "function",
@ -141,6 +142,7 @@
"attributes": "attributes":
{ {
"documentation": null, "documentation": null,
"functionSelector": "26121ff0",
"implemented": true, "implemented": true,
"isConstructor": false, "isConstructor": false,
"kind": "function", "kind": "function",
@ -286,6 +288,7 @@
9 9
], ],
"documentation": null, "documentation": null,
"functionSelector": "26121ff0",
"implemented": true, "implemented": true,
"isConstructor": false, "isConstructor": false,
"kind": "function", "kind": "function",