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. 5
      libsolidity/Types.cpp
  5. 3
      libsolidity/Types.h
  6. 89
      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())
{
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)));
}
@ -202,7 +202,7 @@ vector<pair<FixedHash<4>, FunctionTypePointer>> const& ContractDefinition::getIn
FunctionType ftype(*v);
solAssert(v->getType().get(), "");
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)));
}
}
@ -309,7 +309,7 @@ void FunctionDefinition::checkTypeRequirements()
{
if (!var->getType()->canLiveOutsideStorage())
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"));
}
for (ASTPointer<ModifierInvocation> const& modifier: m_functionModifiers)
@ -320,9 +320,9 @@ void FunctionDefinition::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
@ -663,10 +663,6 @@ void MemberAccess::checkTypeRequirements()
if (!m_type)
BOOST_THROW_EXCEPTION(createTypeError("Member \"" + *m_memberName + "\" not found or not "
"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.
if (type.getCategory() == Type::Category::Struct)
m_isLValue = true;

4
libsolidity/AST.h

@ -422,9 +422,9 @@ public:
void checkTypeRequirements();
/// @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.
std::string externalSignature() const;
std::string externalSignature(bool isExternalCall = false) const;
private:
bool m_isConstructor;

2
libsolidity/ExpressionCompiler.cpp

@ -544,7 +544,7 @@ bool ExpressionCompiler::visit(FunctionCall const& _functionCall)
}
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;
}
solAssert(numIndexed <= 4, "Too many indexed arguments.");

5
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;
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)
{
if (isExternalCall)
solAssert(!!(*it)->externalType(), "Parameter should have external type");
ret += (*it)->externalType()->toString() + (it + 1 == m_parameterTypes.cend() ? "" : ",");
ret += (isExternalCall ? (*it)->externalType()->toString() : (*it)->toString()) + (it + 1 == m_parameterTypes.cend() ? "" : ",");
}
return ret + ")";
}

3
libsolidity/Types.h

@ -553,7 +553,8 @@ public:
/// @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
/// 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
{
solAssert(m_declaration, "Requested declaration from a FunctionType that has none");

89
test/SolidityNameAndTypeResolution.cpp

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

Loading…
Cancel
Save