Browse Source

Small issues with Canonical Function Signature

- Also added an extra test
cl-refactor
Lefteris Karapetsas 10 years ago
parent
commit
48080e0415
  1. 9
      libsolidity/AST.cpp
  2. 9
      libsolidity/AST.h
  3. 9
      libsolidity/Types.cpp
  4. 19
      test/SolidityNameAndTypeResolution.cpp

9
libsolidity/AST.cpp

@ -110,14 +110,9 @@ void FunctionDefinition::checkTypeRequirements()
m_body->checkTypeRequirements();
}
std::string FunctionDefinition::getCanonicalSignature()
string FunctionDefinition::getCanonicalSignature() const
{
auto parameters = getParameters();
std::string ret = getName() + "(";
for (auto it = parameters.cbegin(); it != parameters.cend(); ++it)
ret += (*it)->getType()->toString() + (it + 1 == parameters.end() ? "" : ",");
return ret + ")";
return getName() + FunctionType(*this).getCanonicalSignature();
}
void Block::checkTypeRequirements()

9
libsolidity/AST.h

@ -277,11 +277,10 @@ public:
/// Checks that all parameters have allowed types and calls checkTypeRequirements on the body.
void checkTypeRequirements();
/// Returns the canonical signature of the function
/// That consists of the name of the function followed by the
/// types of the arguments separated by commas all enclosed in parentheses
/// without any spaces
std::string getCanonicalSignature();
/// @returns the canonical signature of the function
/// That consists of the name of the function followed by the types of the
/// arguments separated by commas all enclosed in parentheses without any spaces.
std::string getCanonicalSignature() const;
private:
bool m_isPublic;

9
libsolidity/Types.cpp

@ -452,13 +452,12 @@ unsigned FunctionType::getSizeOnStack() const
}
}
std::string FunctionType::getCanonicalSignature() const
string FunctionType::getCanonicalSignature() const
{
auto parameters = getParameterTypes();
std::string ret = "NAME("; //TODO: how to get function name from FunctionType
string ret = "(";
for (auto it = parameters.cbegin(); it != parameters.cend(); ++it)
ret += (*it)->toString() + (it + 1 == m_parameterTypes.end() ? "" : ",");
for (auto it = m_parameterTypes.cbegin(); it != m_parameterTypes.cend(); ++it)
ret += (*it)->toString() + (it + 1 == m_parameterTypes.cend() ? "" : ",");
return ret + ")";
}

19
test/SolidityNameAndTypeResolution.cpp

@ -336,7 +336,24 @@ BOOST_AUTO_TEST_CASE(function_canonical_signature)
if (ContractDefinition* contract = dynamic_cast<ContractDefinition*>(node.get()))
{
auto functions = contract->getDefinedFunctions();
BOOST_ASSERT("foo(uint256,uint64,bool)" == functions[0]->getCanonicalSignature());
BOOST_CHECK_EQUAL("foo(uint256,uint64,bool)", functions[0]->getCanonicalSignature());
}
}
BOOST_AUTO_TEST_CASE(function_canonical_signature_type_aliases)
{
ASTPointer<SourceUnit> sourceUnit;
char const* text = "contract Test {\n"
" function boo(uint arg1, hash arg2, address arg3) returns (uint ret) {\n"
" ret = 5;\n"
" }\n"
"}\n";
BOOST_CHECK_NO_THROW(sourceUnit = parseTextAndResolveNames(text));
for (ASTPointer<ASTNode> const& node: sourceUnit->getNodes())
if (ContractDefinition* contract = dynamic_cast<ContractDefinition*>(node.get()))
{
auto functions = contract->getDefinedFunctions();
BOOST_CHECK_EQUAL("boo(uint256,hash256,address)", functions[0]->getCanonicalSignature());
}
}

Loading…
Cancel
Save