Browse Source

tests for external types

cl-refactor
Liana Husikyan 10 years ago
parent
commit
f3e8d2b7e9
  1. 14
      libsolidity/AST.cpp
  2. 4
      libsolidity/AST.h
  3. 2
      libsolidity/ExpressionCompiler.cpp
  4. 7
      libsolidity/Types.cpp
  5. 3
      libsolidity/Types.h
  6. 83
      test/SolidityNameAndTypeResolution.cpp

14
libsolidity/AST.cpp

@ -192,7 +192,7 @@ vector<pair<FixedHash<4>, FunctionTypePointer>> const& ContractDefinition::getIn
if (functionsSeen.count(f->getName()) == 0 && f->isPartOfExternalInterface()) if (functionsSeen.count(f->getName()) == 0 && f->isPartOfExternalInterface())
{ {
functionsSeen.insert(f->getName()); functionsSeen.insert(f->getName());
FixedHash<4> hash(dev::sha3(f->externalSignature())); FixedHash<4> hash(dev::sha3(f->externalSignature(true)));
m_interfaceFunctionList->push_back(make_pair(hash, make_shared<FunctionType>(*f, false))); m_interfaceFunctionList->push_back(make_pair(hash, make_shared<FunctionType>(*f, false)));
} }
@ -202,7 +202,7 @@ vector<pair<FixedHash<4>, FunctionTypePointer>> const& ContractDefinition::getIn
FunctionType ftype(*v); FunctionType ftype(*v);
solAssert(v->getType().get(), ""); solAssert(v->getType().get(), "");
functionsSeen.insert(v->getName()); functionsSeen.insert(v->getName());
FixedHash<4> hash(dev::sha3(ftype.externalSignature(v->getName()))); FixedHash<4> hash(dev::sha3(ftype.externalSignature(true, v->getName())));
m_interfaceFunctionList->push_back(make_pair(hash, make_shared<FunctionType>(*v))); m_interfaceFunctionList->push_back(make_pair(hash, make_shared<FunctionType>(*v)));
} }
} }
@ -309,7 +309,7 @@ void FunctionDefinition::checkTypeRequirements()
{ {
if (!var->getType()->canLiveOutsideStorage()) if (!var->getType()->canLiveOutsideStorage())
BOOST_THROW_EXCEPTION(var->createTypeError("Type is required to live outside storage.")); BOOST_THROW_EXCEPTION(var->createTypeError("Type is required to live outside storage."));
if (!var->getType()->externalType() && getVisibility() >= Visibility::Public) if (!(var->getType()->externalType()) && getVisibility() >= Visibility::Public)
BOOST_THROW_EXCEPTION(var->createTypeError("Internal type is not allowed for function with external visibility")); BOOST_THROW_EXCEPTION(var->createTypeError("Internal type is not allowed for function with external visibility"));
} }
for (ASTPointer<ModifierInvocation> const& modifier: m_functionModifiers) for (ASTPointer<ModifierInvocation> const& modifier: m_functionModifiers)
@ -320,9 +320,9 @@ void FunctionDefinition::checkTypeRequirements()
m_body->checkTypeRequirements(); m_body->checkTypeRequirements();
} }
string FunctionDefinition::externalSignature() const string FunctionDefinition::externalSignature(bool isExternalCall) const
{ {
return FunctionType(*this).externalSignature(getName()); return FunctionType(*this).externalSignature(isExternalCall, getName());
} }
bool VariableDeclaration::isLValue() const bool VariableDeclaration::isLValue() const
@ -663,10 +663,6 @@ void MemberAccess::checkTypeRequirements()
if (!m_type) if (!m_type)
BOOST_THROW_EXCEPTION(createTypeError("Member \"" + *m_memberName + "\" not found or not " BOOST_THROW_EXCEPTION(createTypeError("Member \"" + *m_memberName + "\" not found or not "
"visible in " + type.toString())); "visible in " + type.toString()));
// if (auto f = dynamic_cast<FunctionType const*>(m_expression->getType().get()))
// if (f->getLocation() == FunctionType::Location::External && !m_type->externalType())
// BOOST_THROW_EXCEPTION(createTypeError(*m_memberName + " member has an internal type."));
// This should probably move somewhere else. // This should probably move somewhere else.
if (type.getCategory() == Type::Category::Struct) if (type.getCategory() == Type::Category::Struct)
m_isLValue = true; m_isLValue = true;

4
libsolidity/AST.h

@ -422,9 +422,9 @@ public:
void checkTypeRequirements(); void checkTypeRequirements();
/// @returns the external signature of the function /// @returns the external signature of the function
/// That consists of the name of the function followed by the external types of the /// That consists of the name of the function followed by the types(external types if isExternalCall) of the
/// 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(bool isExternalCall = false) const;
private: private:
bool m_isConstructor; bool m_isConstructor;

2
libsolidity/ExpressionCompiler.cpp

@ -544,7 +544,7 @@ bool ExpressionCompiler::visit(FunctionCall const& _functionCall)
} }
if (!event.isAnonymous()) if (!event.isAnonymous())
{ {
m_context << u256(h256::Arith(dev::sha3(function.externalSignature(event.getName())))); m_context << u256(h256::Arith(dev::sha3(function.externalSignature(false, event.getName()))));
++numIndexed; ++numIndexed;
} }
solAssert(numIndexed <= 4, "Too many indexed arguments."); solAssert(numIndexed <= 4, "Too many indexed arguments.");

7
libsolidity/Types.cpp

@ -1127,7 +1127,7 @@ MemberList const& FunctionType::getMembers() const
} }
} }
string FunctionType::externalSignature(std::string const& _name) const string FunctionType::externalSignature(bool isExternalCall, std::string const& _name) const
{ {
std::string funcName = _name; std::string funcName = _name;
if (_name == "") if (_name == "")
@ -1139,8 +1139,9 @@ string FunctionType::externalSignature(std::string const& _name) const
for (auto it = m_parameterTypes.cbegin(); it != m_parameterTypes.cend(); ++it) for (auto it = m_parameterTypes.cbegin(); it != m_parameterTypes.cend(); ++it)
{ {
solAssert(!!(*it)->externalType(), "Parameter should have external type"); if (isExternalCall)
ret += (*it)->externalType()->toString() + (it + 1 == m_parameterTypes.cend() ? "" : ","); solAssert(!!(*it)->externalType(), "Parameter should have external type");
ret += (isExternalCall ? (*it)->externalType()->toString() : (*it)->toString()) + (it + 1 == m_parameterTypes.cend() ? "" : ",");
} }
return ret + ")"; return ret + ")";
} }

3
libsolidity/Types.h

@ -553,7 +553,8 @@ public:
/// @returns the external signature of this function type given the function name /// @returns the external signature of this function type given the function name
/// If @a _name is not provided (empty string) then the @c m_declaration member of the /// If @a _name is not provided (empty string) then the @c m_declaration member of the
/// function type is used /// function type is used
std::string externalSignature(std::string const& _name = "") const; /// @a isExternalCall shows if it is external function call
std::string externalSignature(bool isExternalCall = false, std::string const& _name = "") const;
Declaration const& getDeclaration() const Declaration const& getDeclaration() const
{ {
solAssert(m_declaration, "Requested declaration from a FunctionType that has none"); solAssert(m_declaration, "Requested declaration from a FunctionType that has none");

83
test/SolidityNameAndTypeResolution.cpp

@ -28,6 +28,7 @@
#include <libsolidity/Parser.h> #include <libsolidity/Parser.h>
#include <libsolidity/NameAndTypeResolver.h> #include <libsolidity/NameAndTypeResolver.h>
#include <libsolidity/Exceptions.h> #include <libsolidity/Exceptions.h>
#include <libsolidity/GlobalContext.h>
#include "TestHelper.h" #include "TestHelper.h"
using namespace std; using namespace std;
@ -48,16 +49,28 @@ ASTPointer<SourceUnit> parseTextAndResolveNames(std::string const& _source)
ASTPointer<SourceUnit> sourceUnit = parser.parse(std::make_shared<Scanner>(CharStream(_source))); ASTPointer<SourceUnit> sourceUnit = parser.parse(std::make_shared<Scanner>(CharStream(_source)));
NameAndTypeResolver resolver({}); NameAndTypeResolver resolver({});
resolver.registerDeclarations(*sourceUnit); resolver.registerDeclarations(*sourceUnit);
std::shared_ptr<GlobalContext> globalContext = make_shared<GlobalContext>();
for (ASTPointer<ASTNode> const& node: sourceUnit->getNodes()) for (ASTPointer<ASTNode> const& node: sourceUnit->getNodes())
if (ContractDefinition* contract = dynamic_cast<ContractDefinition*>(node.get())) if (ContractDefinition* contract = dynamic_cast<ContractDefinition*>(node.get()))
{
globalContext->setCurrentContract(*contract);
resolver.updateDeclaration(*globalContext->getCurrentThis());
resolver.updateDeclaration(*globalContext->getCurrentSuper());
resolver.resolveNamesAndTypes(*contract); resolver.resolveNamesAndTypes(*contract);
}
for (ASTPointer<ASTNode> const& node: sourceUnit->getNodes()) for (ASTPointer<ASTNode> const& node: sourceUnit->getNodes())
if (ContractDefinition* contract = dynamic_cast<ContractDefinition*>(node.get())) if (ContractDefinition* contract = dynamic_cast<ContractDefinition*>(node.get()))
{
globalContext->setCurrentContract(*contract);
resolver.updateDeclaration(*globalContext->getCurrentThis());
resolver.checkTypeRequirements(*contract); resolver.checkTypeRequirements(*contract);
}
return sourceUnit; return sourceUnit;
} }
static ContractDefinition const* retrieveContract(ASTPointer<SourceUnit> _source, unsigned index) static ContractDefinition const* retrieveContract(ASTPointer<SourceUnit> _source, unsigned index)
{ {
ContractDefinition* contract; ContractDefinition* contract;
@ -376,6 +389,8 @@ BOOST_AUTO_TEST_CASE(function_canonical_signature_type_aliases)
if (ContractDefinition* contract = dynamic_cast<ContractDefinition*>(node.get())) if (ContractDefinition* contract = dynamic_cast<ContractDefinition*>(node.get()))
{ {
auto functions = contract->getDefinedFunctions(); auto functions = contract->getDefinedFunctions();
if (functions.empty())
continue;
BOOST_CHECK_EQUAL("boo(uint256,bytes32,address)", functions[0]->externalSignature()); BOOST_CHECK_EQUAL("boo(uint256,bytes32,address)", functions[0]->externalSignature());
} }
} }
@ -384,8 +399,11 @@ BOOST_AUTO_TEST_CASE(function_external_types)
{ {
ASTPointer<SourceUnit> sourceUnit; ASTPointer<SourceUnit> sourceUnit;
char const* text = R"( char const* text = R"(
contract C {
uint a;
}
contract Test { contract Test {
function boo(uint arg2, bool arg3, bytes8 arg4, bool[2] pairs) public returns (uint ret) { function boo(uint arg2, bool arg3, bytes8 arg4, bool[2] pairs, uint[] dynamic, C carg) external returns (uint ret) {
ret = 5; ret = 5;
} }
})"; })";
@ -394,40 +412,41 @@ BOOST_AUTO_TEST_CASE(function_external_types)
if (ContractDefinition* contract = dynamic_cast<ContractDefinition*>(node.get())) if (ContractDefinition* contract = dynamic_cast<ContractDefinition*>(node.get()))
{ {
auto functions = contract->getDefinedFunctions(); auto functions = contract->getDefinedFunctions();
BOOST_CHECK_EQUAL("boo(uint256,bool,bytes8)", functions[0]->externalSignature()); if (functions.empty())
continue;
BOOST_CHECK_EQUAL("boo(uint256,bool,bytes8,bool[2],uint256[],address)", functions[0]->externalSignature(true));
} }
} }
//BOOST_AUTO_TEST_CASE(function_external_types_throw) BOOST_AUTO_TEST_CASE(function_external_call_conversion)
//{ {
// ASTPointer<SourceUnit> sourceUnit; char const* sourceCode = R"(
// char const* text = R"( contract C {}
// contract ArrayContract { contract Test {
// bool[2][] m_pairsOfFlags; function externalCall() {
// function setAllFlagPairs(bool[2][] newPairs) { address arg;
// // assignment to array replaces the complete array this.g(arg);
// m_pairsOfFlags = newPairs; }
// } function g (C c) external {}
// })"; })";
// ETH_TEST_REQUIRE_NO_THROW(sourceUnit = parseTextAndResolveNames(text), "Parsing and name Resolving failed"); BOOST_CHECK_THROW(parseTextAndResolveNames(sourceCode), TypeError);
// for (ASTPointer<ASTNode> const& node: sourceUnit->getNodes()) }
// if (ContractDefinition* contract = dynamic_cast<ContractDefinition*>(node.get()))
// { BOOST_AUTO_TEST_CASE(function_internal_call_conversion)
// auto functions = contract->getDefinedFunctions(); {
// BOOST_CHECK_EQUAL("boo(uint256,bool,bytes8)", functions[0]->externalSigniture()); char const* text = R"(
// } contract C {
//todo should check arrays and contract. also event uint a;
//BOOST_AUTO_TEST_CASE(function_external_types_throw) }
//{ contract Test {
// ASTPointer<SourceUnit> sourceUnit; address a;
// char const* text = R"( function g (C c) {}
// contract Test { function internalCall() {
// function boo(uint32[] arg5) returns (uint ret) { g(a);
// ret = 5; }
// } })";
// })"; BOOST_CHECK_THROW(parseTextAndResolveNames(text), TypeError);
// BOOST_CHECK_THROW(parseTextAndResolveNames(text), TypeError); }
//}
BOOST_AUTO_TEST_CASE(hash_collision_in_interface) BOOST_AUTO_TEST_CASE(hash_collision_in_interface)
{ {

Loading…
Cancel
Save