2014-10-07 16:25:04 +00:00
|
|
|
/*
|
2014-10-16 12:08:54 +00:00
|
|
|
This file is part of cpp-ethereum.
|
2014-10-07 16:25:04 +00:00
|
|
|
|
2014-10-16 12:08:54 +00:00
|
|
|
cpp-ethereum is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
2014-10-07 16:25:04 +00:00
|
|
|
|
2014-10-16 12:08:54 +00:00
|
|
|
cpp-ethereum is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
2014-10-07 16:25:04 +00:00
|
|
|
|
2014-10-16 12:08:54 +00:00
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with cpp-ethereum. If not, see <http://www.gnu.org/licenses/>.
|
2014-10-07 16:25:04 +00:00
|
|
|
*/
|
|
|
|
/**
|
|
|
|
* @author Christian <c@ethdev.com>
|
|
|
|
* @date 2014
|
|
|
|
* Solidity abstract syntax tree.
|
|
|
|
*/
|
|
|
|
|
2014-10-13 16:22:15 +00:00
|
|
|
#include <algorithm>
|
2015-06-18 14:35:25 +00:00
|
|
|
#include <functional>
|
2015-03-27 14:15:34 +00:00
|
|
|
#include <boost/range/adaptor/reversed.hpp>
|
2014-12-17 15:23:18 +00:00
|
|
|
#include <libsolidity/Utils.h>
|
2014-10-07 16:25:04 +00:00
|
|
|
#include <libsolidity/AST.h>
|
2014-10-10 14:37:54 +00:00
|
|
|
#include <libsolidity/ASTVisitor.h>
|
2014-10-15 12:45:51 +00:00
|
|
|
#include <libsolidity/Exceptions.h>
|
2014-12-08 12:33:13 +00:00
|
|
|
#include <libsolidity/AST_accept.h>
|
2014-10-10 14:37:54 +00:00
|
|
|
|
2015-05-19 17:51:38 +00:00
|
|
|
#include <libdevcore/SHA3.h>
|
2015-01-07 15:39:21 +00:00
|
|
|
|
2014-10-24 17:06:30 +00:00
|
|
|
using namespace std;
|
|
|
|
|
2014-10-16 12:08:54 +00:00
|
|
|
namespace dev
|
|
|
|
{
|
|
|
|
namespace solidity
|
|
|
|
{
|
2014-10-10 14:37:54 +00:00
|
|
|
|
2014-11-20 17:33:23 +00:00
|
|
|
TypeError ASTNode::createTypeError(string const& _description) const
|
2014-10-23 17:22:30 +00:00
|
|
|
{
|
2015-08-31 16:44:29 +00:00
|
|
|
return TypeError() << errinfo_sourceLocation(location()) << errinfo_comment(_description);
|
2014-10-23 17:22:30 +00:00
|
|
|
}
|
|
|
|
|
2015-08-31 16:44:29 +00:00
|
|
|
TypePointer ContractDefinition::type(ContractDefinition const* _currentContract) const
|
2015-01-22 00:02:38 +00:00
|
|
|
{
|
|
|
|
return make_shared<TypeType>(make_shared<ContractType>(*this), _currentContract);
|
|
|
|
}
|
|
|
|
|
2014-12-15 15:09:50 +00:00
|
|
|
void ContractDefinition::checkTypeRequirements()
|
|
|
|
{
|
2015-08-31 16:44:29 +00:00
|
|
|
for (ASTPointer<InheritanceSpecifier> const& baseSpecifier: baseContracts())
|
2015-01-22 00:02:38 +00:00
|
|
|
baseSpecifier->checkTypeRequirements();
|
2015-01-19 20:05:47 +00:00
|
|
|
|
2015-04-15 15:40:50 +00:00
|
|
|
checkDuplicateFunctions();
|
2015-01-16 16:50:10 +00:00
|
|
|
checkIllegalOverrides();
|
2015-03-27 14:15:34 +00:00
|
|
|
checkAbstractFunctions();
|
2015-04-02 14:56:12 +00:00
|
|
|
checkAbstractConstructors();
|
2015-01-16 16:50:10 +00:00
|
|
|
|
2015-09-09 15:43:01 +00:00
|
|
|
FunctionDefinition const* functionDefinition = constructor();
|
2015-09-09 16:18:34 +00:00
|
|
|
if (functionDefinition && !functionDefinition->returnParameters().empty())
|
|
|
|
BOOST_THROW_EXCEPTION(functionDefinition->returnParameterList()->createTypeError(
|
2015-09-09 15:43:01 +00:00
|
|
|
"Non-empty \"returns\" directive for constructor."
|
|
|
|
));
|
2015-01-29 21:50:20 +00:00
|
|
|
|
2015-02-09 01:06:30 +00:00
|
|
|
FunctionDefinition const* fallbackFunction = nullptr;
|
2015-08-31 16:44:29 +00:00
|
|
|
for (ASTPointer<FunctionDefinition> const& function: definedFunctions())
|
2015-03-27 10:53:17 +00:00
|
|
|
{
|
2015-08-31 16:44:29 +00:00
|
|
|
if (function->name().empty())
|
2015-02-09 01:06:30 +00:00
|
|
|
{
|
|
|
|
if (fallbackFunction)
|
|
|
|
BOOST_THROW_EXCEPTION(DeclarationError() << errinfo_comment("Only one fallback function is allowed."));
|
|
|
|
else
|
|
|
|
{
|
|
|
|
fallbackFunction = function.get();
|
2015-08-31 16:44:29 +00:00
|
|
|
if (!fallbackFunction->parameters().empty())
|
|
|
|
BOOST_THROW_EXCEPTION(fallbackFunction->parameterList().createTypeError("Fallback function cannot take parameters."));
|
2015-02-09 01:06:30 +00:00
|
|
|
}
|
|
|
|
}
|
2015-03-27 10:53:17 +00:00
|
|
|
if (!function->isFullyImplemented())
|
|
|
|
setFullyImplemented(false);
|
|
|
|
}
|
2015-09-08 10:57:27 +00:00
|
|
|
|
|
|
|
for (ASTPointer<VariableDeclaration> const& variable: m_stateVariables)
|
|
|
|
variable->checkTypeRequirements();
|
|
|
|
|
2015-09-11 13:24:11 +00:00
|
|
|
for (ASTPointer<EventDefinition> const& event: events())
|
|
|
|
event->checkTypeRequirements();
|
|
|
|
|
2015-08-31 16:44:29 +00:00
|
|
|
for (ASTPointer<ModifierDefinition> const& modifier: functionModifiers())
|
2015-01-22 00:02:38 +00:00
|
|
|
modifier->checkTypeRequirements();
|
|
|
|
|
2015-08-31 16:44:29 +00:00
|
|
|
for (ASTPointer<FunctionDefinition> const& function: definedFunctions())
|
2014-12-15 15:09:50 +00:00
|
|
|
function->checkTypeRequirements();
|
2015-01-14 09:16:58 +00:00
|
|
|
|
2015-04-15 15:40:50 +00:00
|
|
|
checkExternalTypeClashes();
|
2015-01-14 09:16:58 +00:00
|
|
|
// check for hash collisions in function signatures
|
|
|
|
set<FixedHash<4>> hashes;
|
2015-08-31 16:44:29 +00:00
|
|
|
for (auto const& it: interfaceFunctionList())
|
2015-01-14 09:16:58 +00:00
|
|
|
{
|
2015-01-29 15:39:30 +00:00
|
|
|
FixedHash<4> const& hash = it.first;
|
2015-01-14 09:16:58 +00:00
|
|
|
if (hashes.count(hash))
|
2015-05-04 15:28:28 +00:00
|
|
|
BOOST_THROW_EXCEPTION(createTypeError(
|
|
|
|
string("Function signature hash collision for ") + it.second->externalSignature()
|
|
|
|
));
|
2015-01-14 09:16:58 +00:00
|
|
|
hashes.insert(hash);
|
|
|
|
}
|
2015-09-08 14:48:33 +00:00
|
|
|
|
|
|
|
if (isLibrary())
|
|
|
|
checkLibraryRequirements();
|
2014-12-15 15:09:50 +00:00
|
|
|
}
|
|
|
|
|
2015-08-31 16:44:29 +00:00
|
|
|
map<FixedHash<4>, FunctionTypePointer> ContractDefinition::interfaceFunctions() const
|
2014-11-11 16:41:48 +00:00
|
|
|
{
|
2015-08-31 16:44:29 +00:00
|
|
|
auto exportedFunctionList = interfaceFunctionList();
|
2015-01-23 15:37:06 +00:00
|
|
|
|
2015-01-29 15:48:39 +00:00
|
|
|
map<FixedHash<4>, FunctionTypePointer> exportedFunctions;
|
2015-01-23 15:37:06 +00:00
|
|
|
for (auto const& it: exportedFunctionList)
|
2015-01-29 15:39:30 +00:00
|
|
|
exportedFunctions.insert(it);
|
2015-01-22 16:40:22 +00:00
|
|
|
|
2015-09-10 08:17:17 +00:00
|
|
|
solAssert(
|
|
|
|
exportedFunctionList.size() == exportedFunctions.size(),
|
2015-09-08 12:30:21 +00:00
|
|
|
"Hash collision at Function Definition Hash calculation"
|
|
|
|
);
|
2014-11-11 16:41:48 +00:00
|
|
|
|
|
|
|
return exportedFunctions;
|
|
|
|
}
|
|
|
|
|
2015-08-31 16:44:29 +00:00
|
|
|
FunctionDefinition const* ContractDefinition::constructor() const
|
2014-12-12 15:49:26 +00:00
|
|
|
{
|
|
|
|
for (ASTPointer<FunctionDefinition> const& f: m_definedFunctions)
|
2015-01-20 14:58:04 +00:00
|
|
|
if (f->isConstructor())
|
2014-12-12 15:49:26 +00:00
|
|
|
return f.get();
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2015-08-31 16:44:29 +00:00
|
|
|
FunctionDefinition const* ContractDefinition::fallbackFunction() const
|
2015-01-29 21:50:20 +00:00
|
|
|
{
|
2015-08-31 16:44:29 +00:00
|
|
|
for (ContractDefinition const* contract: linearizedBaseContracts())
|
|
|
|
for (ASTPointer<FunctionDefinition> const& f: contract->definedFunctions())
|
|
|
|
if (f->name().empty())
|
2015-01-29 21:50:20 +00:00
|
|
|
return f.get();
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2015-04-15 15:40:50 +00:00
|
|
|
void ContractDefinition::checkDuplicateFunctions() const
|
|
|
|
{
|
|
|
|
/// Checks that two functions with the same name defined in this contract have different
|
|
|
|
/// argument types and that there is at most one constructor.
|
|
|
|
map<string, vector<FunctionDefinition const*>> functions;
|
2015-08-31 16:44:29 +00:00
|
|
|
for (ASTPointer<FunctionDefinition> const& function: definedFunctions())
|
|
|
|
functions[function->name()].push_back(function.get());
|
2015-04-30 16:06:04 +00:00
|
|
|
|
2015-08-31 16:44:29 +00:00
|
|
|
if (functions[name()].size() > 1)
|
2015-04-30 16:06:04 +00:00
|
|
|
{
|
|
|
|
SecondarySourceLocation ssl;
|
2015-08-31 16:44:29 +00:00
|
|
|
auto it = functions[name()].begin();
|
2015-04-30 16:06:04 +00:00
|
|
|
++it;
|
2015-08-31 16:44:29 +00:00
|
|
|
for (; it != functions[name()].end(); ++it)
|
|
|
|
ssl.append("Another declaration is here:", (*it)->location());
|
2015-04-30 16:06:04 +00:00
|
|
|
|
2015-04-15 15:40:50 +00:00
|
|
|
BOOST_THROW_EXCEPTION(
|
|
|
|
DeclarationError() <<
|
2015-08-31 16:44:29 +00:00
|
|
|
errinfo_sourceLocation(functions[name()].front()->location()) <<
|
2015-04-30 16:06:04 +00:00
|
|
|
errinfo_comment("More than one constructor defined.") <<
|
|
|
|
errinfo_secondarySourceLocation(ssl)
|
2015-04-15 15:40:50 +00:00
|
|
|
);
|
2015-04-30 16:06:04 +00:00
|
|
|
}
|
2015-04-15 15:40:50 +00:00
|
|
|
for (auto const& it: functions)
|
|
|
|
{
|
|
|
|
vector<FunctionDefinition const*> const& overloads = it.second;
|
|
|
|
for (size_t i = 0; i < overloads.size(); ++i)
|
|
|
|
for (size_t j = i + 1; j < overloads.size(); ++j)
|
|
|
|
if (FunctionType(*overloads[i]).hasEqualArgumentTypes(FunctionType(*overloads[j])))
|
|
|
|
BOOST_THROW_EXCEPTION(
|
|
|
|
DeclarationError() <<
|
2015-08-31 16:44:29 +00:00
|
|
|
errinfo_sourceLocation(overloads[j]->location()) <<
|
2015-05-04 13:46:46 +00:00
|
|
|
errinfo_comment("Function with same name and arguments defined twice.") <<
|
2015-04-30 16:06:04 +00:00
|
|
|
errinfo_secondarySourceLocation(SecondarySourceLocation().append(
|
2015-09-09 15:43:01 +00:00
|
|
|
"Other declaration is here:", overloads[i]->location())
|
|
|
|
)
|
|
|
|
|
2015-04-15 15:40:50 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-27 14:15:34 +00:00
|
|
|
void ContractDefinition::checkAbstractFunctions()
|
|
|
|
{
|
2015-06-23 14:56:59 +00:00
|
|
|
// Mapping from name to function definition (exactly one per argument type equality class) and
|
|
|
|
// flag to indicate whether it is fully implemented.
|
|
|
|
using FunTypeAndFlag = std::pair<FunctionTypePointer, bool>;
|
|
|
|
map<string, vector<FunTypeAndFlag>> functions;
|
2015-03-27 14:15:34 +00:00
|
|
|
|
|
|
|
// Search from base to derived
|
2015-08-31 16:44:29 +00:00
|
|
|
for (ContractDefinition const* contract: boost::adaptors::reverse(linearizedBaseContracts()))
|
|
|
|
for (ASTPointer<FunctionDefinition> const& function: contract->definedFunctions())
|
2015-03-27 14:15:34 +00:00
|
|
|
{
|
2015-08-31 16:44:29 +00:00
|
|
|
auto& overloads = functions[function->name()];
|
2015-06-23 14:56:59 +00:00
|
|
|
FunctionTypePointer funType = make_shared<FunctionType>(*function);
|
|
|
|
auto it = find_if(overloads.begin(), overloads.end(), [&](FunTypeAndFlag const& _funAndFlag)
|
|
|
|
{
|
|
|
|
return funType->hasEqualArgumentTypes(*_funAndFlag.first);
|
|
|
|
});
|
|
|
|
if (it == overloads.end())
|
|
|
|
overloads.push_back(make_pair(funType, function->isFullyImplemented()));
|
|
|
|
else if (it->second)
|
|
|
|
{
|
|
|
|
if (!function->isFullyImplemented())
|
|
|
|
BOOST_THROW_EXCEPTION(function->createTypeError("Redeclaring an already implemented function as abstract"));
|
|
|
|
}
|
|
|
|
else if (function->isFullyImplemented())
|
|
|
|
it->second = true;
|
2015-03-27 14:15:34 +00:00
|
|
|
}
|
2015-03-30 15:18:38 +00:00
|
|
|
|
2015-06-23 14:56:59 +00:00
|
|
|
// Set to not fully implemented if at least one flag is false.
|
2015-03-27 14:15:34 +00:00
|
|
|
for (auto const& it: functions)
|
2015-06-23 14:56:59 +00:00
|
|
|
for (auto const& funAndFlag: it.second)
|
|
|
|
if (!funAndFlag.second)
|
|
|
|
{
|
|
|
|
setFullyImplemented(false);
|
|
|
|
return;
|
|
|
|
}
|
2015-03-27 14:15:34 +00:00
|
|
|
}
|
|
|
|
|
2015-04-02 14:56:12 +00:00
|
|
|
void ContractDefinition::checkAbstractConstructors()
|
|
|
|
{
|
2015-04-17 14:17:21 +00:00
|
|
|
set<ContractDefinition const*> argumentsNeeded;
|
2015-04-02 14:56:12 +00:00
|
|
|
// check that we get arguments for all base constructors that need it.
|
|
|
|
// If not mark the contract as abstract (not fully implemented)
|
2015-04-17 14:17:21 +00:00
|
|
|
|
2015-08-31 16:44:29 +00:00
|
|
|
vector<ContractDefinition const*> const& bases = linearizedBaseContracts();
|
2015-04-17 14:17:21 +00:00
|
|
|
for (ContractDefinition const* contract: bases)
|
2015-08-31 16:44:29 +00:00
|
|
|
if (FunctionDefinition const* constructor = contract->constructor())
|
|
|
|
if (contract != this && !constructor->parameters().empty())
|
2015-04-17 14:17:21 +00:00
|
|
|
argumentsNeeded.insert(contract);
|
|
|
|
|
2015-04-02 14:56:12 +00:00
|
|
|
for (ContractDefinition const* contract: bases)
|
|
|
|
{
|
2015-08-31 16:44:29 +00:00
|
|
|
if (FunctionDefinition const* constructor = contract->constructor())
|
|
|
|
for (auto const& modifier: constructor->modifiers())
|
2015-04-02 14:56:12 +00:00
|
|
|
{
|
|
|
|
auto baseContract = dynamic_cast<ContractDefinition const*>(
|
2015-08-31 16:44:29 +00:00
|
|
|
&modifier->name()->referencedDeclaration()
|
2015-04-17 14:17:21 +00:00
|
|
|
);
|
2015-04-02 14:56:12 +00:00
|
|
|
if (baseContract)
|
2015-04-17 14:17:21 +00:00
|
|
|
argumentsNeeded.erase(baseContract);
|
2015-04-02 14:56:12 +00:00
|
|
|
}
|
2015-04-17 14:17:21 +00:00
|
|
|
|
2015-04-02 14:56:12 +00:00
|
|
|
|
2015-08-31 16:44:29 +00:00
|
|
|
for (ASTPointer<InheritanceSpecifier> const& base: contract->baseContracts())
|
2015-04-02 14:56:12 +00:00
|
|
|
{
|
2015-04-17 14:17:21 +00:00
|
|
|
auto baseContract = dynamic_cast<ContractDefinition const*>(
|
2015-08-31 16:44:29 +00:00
|
|
|
&base->name()->referencedDeclaration()
|
2015-04-17 14:17:21 +00:00
|
|
|
);
|
2015-04-02 14:56:12 +00:00
|
|
|
solAssert(baseContract, "");
|
2015-08-31 16:44:29 +00:00
|
|
|
if (!base->arguments().empty())
|
2015-04-17 14:17:21 +00:00
|
|
|
argumentsNeeded.erase(baseContract);
|
2015-04-02 14:56:12 +00:00
|
|
|
}
|
|
|
|
}
|
2015-04-17 14:17:21 +00:00
|
|
|
if (!argumentsNeeded.empty())
|
2015-04-02 14:56:12 +00:00
|
|
|
setFullyImplemented(false);
|
|
|
|
}
|
|
|
|
|
2015-01-16 16:50:10 +00:00
|
|
|
void ContractDefinition::checkIllegalOverrides() const
|
2015-01-14 09:16:58 +00:00
|
|
|
{
|
2015-01-22 00:02:38 +00:00
|
|
|
// TODO unify this at a later point. for this we need to put the constness and the access specifier
|
|
|
|
// into the types
|
2015-04-15 15:40:50 +00:00
|
|
|
map<string, vector<FunctionDefinition const*>> functions;
|
2015-01-22 00:02:38 +00:00
|
|
|
map<string, ModifierDefinition const*> modifiers;
|
2015-01-16 16:50:10 +00:00
|
|
|
|
|
|
|
// We search from derived to base, so the stored item causes the error.
|
2015-08-31 16:44:29 +00:00
|
|
|
for (ContractDefinition const* contract: linearizedBaseContracts())
|
2015-01-22 00:02:38 +00:00
|
|
|
{
|
2015-08-31 16:44:29 +00:00
|
|
|
for (ASTPointer<FunctionDefinition> const& function: contract->definedFunctions())
|
2015-01-14 09:16:58 +00:00
|
|
|
{
|
2015-01-20 14:58:04 +00:00
|
|
|
if (function->isConstructor())
|
2015-01-22 00:02:38 +00:00
|
|
|
continue; // constructors can neither be overridden nor override anything
|
2015-08-31 16:44:29 +00:00
|
|
|
string const& name = function->name();
|
2015-01-22 00:02:38 +00:00
|
|
|
if (modifiers.count(name))
|
|
|
|
BOOST_THROW_EXCEPTION(modifiers[name]->createTypeError("Override changes function to modifier."));
|
2015-04-15 15:40:50 +00:00
|
|
|
FunctionType functionType(*function);
|
|
|
|
// function should not change the return type
|
|
|
|
for (FunctionDefinition const* overriding: functions[name])
|
|
|
|
{
|
|
|
|
FunctionType overridingType(*overriding);
|
|
|
|
if (!overridingType.hasEqualArgumentTypes(functionType))
|
|
|
|
continue;
|
|
|
|
if (
|
2015-08-31 16:44:29 +00:00
|
|
|
overriding->visibility() != function->visibility() ||
|
2015-04-15 15:40:50 +00:00
|
|
|
overriding->isDeclaredConst() != function->isDeclaredConst() ||
|
|
|
|
overridingType != functionType
|
|
|
|
)
|
|
|
|
BOOST_THROW_EXCEPTION(overriding->createTypeError("Override changes extended function signature."));
|
|
|
|
}
|
|
|
|
functions[name].push_back(function.get());
|
2015-01-14 09:16:58 +00:00
|
|
|
}
|
2015-08-31 16:44:29 +00:00
|
|
|
for (ASTPointer<ModifierDefinition> const& modifier: contract->functionModifiers())
|
2015-01-22 00:02:38 +00:00
|
|
|
{
|
2015-08-31 16:44:29 +00:00
|
|
|
string const& name = modifier->name();
|
2015-01-22 00:02:38 +00:00
|
|
|
ModifierDefinition const*& override = modifiers[name];
|
|
|
|
if (!override)
|
|
|
|
override = modifier.get();
|
|
|
|
else if (ModifierType(*override) != ModifierType(*modifier))
|
|
|
|
BOOST_THROW_EXCEPTION(override->createTypeError("Override changes modifier signature."));
|
2015-04-15 15:40:50 +00:00
|
|
|
if (!functions[name].empty())
|
2015-03-01 03:34:39 +00:00
|
|
|
BOOST_THROW_EXCEPTION(override->createTypeError("Override changes modifier to function."));
|
2015-01-22 00:02:38 +00:00
|
|
|
}
|
|
|
|
}
|
2015-01-16 16:50:10 +00:00
|
|
|
}
|
2015-01-14 09:16:58 +00:00
|
|
|
|
2015-04-15 15:40:50 +00:00
|
|
|
void ContractDefinition::checkExternalTypeClashes() const
|
|
|
|
{
|
|
|
|
map<string, vector<pair<Declaration const*, shared_ptr<FunctionType>>>> externalDeclarations;
|
2015-08-31 16:44:29 +00:00
|
|
|
for (ContractDefinition const* contract: linearizedBaseContracts())
|
2015-04-15 15:40:50 +00:00
|
|
|
{
|
2015-08-31 16:44:29 +00:00
|
|
|
for (ASTPointer<FunctionDefinition> const& f: contract->definedFunctions())
|
2015-04-15 15:40:50 +00:00
|
|
|
if (f->isPartOfExternalInterface())
|
|
|
|
{
|
|
|
|
auto functionType = make_shared<FunctionType>(*f);
|
2015-08-31 16:44:29 +00:00
|
|
|
externalDeclarations[functionType->externalSignature(f->name())].push_back(
|
2015-04-15 15:40:50 +00:00
|
|
|
make_pair(f.get(), functionType)
|
|
|
|
);
|
|
|
|
}
|
2015-08-31 16:44:29 +00:00
|
|
|
for (ASTPointer<VariableDeclaration> const& v: contract->stateVariables())
|
2015-04-15 15:40:50 +00:00
|
|
|
if (v->isPartOfExternalInterface())
|
|
|
|
{
|
|
|
|
auto functionType = make_shared<FunctionType>(*v);
|
2015-08-31 16:44:29 +00:00
|
|
|
externalDeclarations[functionType->externalSignature(v->name())].push_back(
|
2015-04-15 15:40:50 +00:00
|
|
|
make_pair(v.get(), functionType)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for (auto const& it: externalDeclarations)
|
|
|
|
for (size_t i = 0; i < it.second.size(); ++i)
|
|
|
|
for (size_t j = i + 1; j < it.second.size(); ++j)
|
|
|
|
if (!it.second[i].second->hasEqualArgumentTypes(*it.second[j].second))
|
|
|
|
BOOST_THROW_EXCEPTION(it.second[j].first->createTypeError(
|
|
|
|
"Function overload clash during conversion to external types for arguments."
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
2015-09-08 14:48:33 +00:00
|
|
|
void ContractDefinition::checkLibraryRequirements() const
|
|
|
|
{
|
|
|
|
solAssert(m_isLibrary, "");
|
|
|
|
if (!m_baseContracts.empty())
|
|
|
|
BOOST_THROW_EXCEPTION(createTypeError("Library is not allowed to inherit."));
|
|
|
|
|
|
|
|
for (auto const& var: m_stateVariables)
|
|
|
|
if (!var->isConstant())
|
|
|
|
BOOST_THROW_EXCEPTION(var->createTypeError("Library cannot have non-constant state variables"));
|
|
|
|
}
|
|
|
|
|
2015-08-31 16:44:29 +00:00
|
|
|
vector<ASTPointer<EventDefinition>> const& ContractDefinition::interfaceEvents() const
|
2015-01-31 13:41:11 +00:00
|
|
|
{
|
|
|
|
if (!m_interfaceEvents)
|
|
|
|
{
|
|
|
|
set<string> eventsSeen;
|
2015-05-04 15:18:01 +00:00
|
|
|
m_interfaceEvents.reset(new vector<ASTPointer<EventDefinition>>());
|
2015-08-31 16:44:29 +00:00
|
|
|
for (ContractDefinition const* contract: linearizedBaseContracts())
|
|
|
|
for (ASTPointer<EventDefinition> const& e: contract->events())
|
|
|
|
if (eventsSeen.count(e->name()) == 0)
|
2015-01-31 13:41:11 +00:00
|
|
|
{
|
2015-08-31 16:44:29 +00:00
|
|
|
eventsSeen.insert(e->name());
|
2015-01-31 13:41:11 +00:00
|
|
|
m_interfaceEvents->push_back(e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return *m_interfaceEvents;
|
|
|
|
}
|
|
|
|
|
2015-08-31 16:44:29 +00:00
|
|
|
vector<pair<FixedHash<4>, FunctionTypePointer>> const& ContractDefinition::interfaceFunctionList() const
|
2015-01-16 16:50:10 +00:00
|
|
|
{
|
|
|
|
if (!m_interfaceFunctionList)
|
|
|
|
{
|
|
|
|
set<string> functionsSeen;
|
2015-03-01 03:34:39 +00:00
|
|
|
set<string> signaturesSeen;
|
2015-01-29 16:28:14 +00:00
|
|
|
m_interfaceFunctionList.reset(new vector<pair<FixedHash<4>, FunctionTypePointer>>());
|
2015-08-31 16:44:29 +00:00
|
|
|
for (ContractDefinition const* contract: linearizedBaseContracts())
|
2015-01-22 16:40:22 +00:00
|
|
|
{
|
2015-08-31 16:44:29 +00:00
|
|
|
for (ASTPointer<FunctionDefinition> const& f: contract->definedFunctions())
|
2015-03-01 03:34:39 +00:00
|
|
|
{
|
2015-06-26 14:52:30 +00:00
|
|
|
if (!f->isPartOfExternalInterface())
|
|
|
|
continue;
|
2015-04-07 15:08:49 +00:00
|
|
|
string functionSignature = f->externalSignature();
|
2015-06-26 14:52:30 +00:00
|
|
|
if (signaturesSeen.count(functionSignature) == 0)
|
2015-01-16 16:50:10 +00:00
|
|
|
{
|
2015-08-31 16:44:29 +00:00
|
|
|
functionsSeen.insert(f->name());
|
2015-03-01 03:34:39 +00:00
|
|
|
signaturesSeen.insert(functionSignature);
|
|
|
|
FixedHash<4> hash(dev::sha3(functionSignature));
|
2015-01-29 15:39:30 +00:00
|
|
|
m_interfaceFunctionList->push_back(make_pair(hash, make_shared<FunctionType>(*f, false)));
|
2015-01-16 16:50:10 +00:00
|
|
|
}
|
2015-03-01 03:34:39 +00:00
|
|
|
}
|
2015-01-22 16:40:22 +00:00
|
|
|
|
2015-08-31 16:44:29 +00:00
|
|
|
for (ASTPointer<VariableDeclaration> const& v: contract->stateVariables())
|
|
|
|
if (functionsSeen.count(v->name()) == 0 && v->isPartOfExternalInterface())
|
2015-01-22 16:40:22 +00:00
|
|
|
{
|
|
|
|
FunctionType ftype(*v);
|
2015-08-31 16:44:29 +00:00
|
|
|
solAssert(v->type().get(), "");
|
|
|
|
functionsSeen.insert(v->name());
|
|
|
|
FixedHash<4> hash(dev::sha3(ftype.externalSignature(v->name())));
|
2015-01-29 15:39:30 +00:00
|
|
|
m_interfaceFunctionList->push_back(make_pair(hash, make_shared<FunctionType>(*v)));
|
2015-01-22 16:40:22 +00:00
|
|
|
}
|
|
|
|
}
|
2015-01-16 16:50:10 +00:00
|
|
|
}
|
|
|
|
return *m_interfaceFunctionList;
|
2015-01-14 09:16:58 +00:00
|
|
|
}
|
|
|
|
|
2015-06-19 13:40:09 +00:00
|
|
|
string const& ContractDefinition::devDocumentation() const
|
2015-06-15 13:21:23 +00:00
|
|
|
{
|
2015-06-19 13:40:09 +00:00
|
|
|
return m_devDocumentation;
|
2015-06-15 13:21:23 +00:00
|
|
|
}
|
|
|
|
|
2015-06-19 13:40:09 +00:00
|
|
|
string const& ContractDefinition::userDocumentation() const
|
2015-06-15 13:21:23 +00:00
|
|
|
{
|
2015-06-19 13:40:09 +00:00
|
|
|
return m_userDocumentation;
|
2015-06-15 13:21:23 +00:00
|
|
|
}
|
|
|
|
|
2015-06-19 13:40:09 +00:00
|
|
|
void ContractDefinition::setDevDocumentation(string const& _devDocumentation)
|
2015-06-15 13:21:23 +00:00
|
|
|
{
|
2015-06-19 13:40:09 +00:00
|
|
|
m_devDocumentation = _devDocumentation;
|
2015-06-15 13:21:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void ContractDefinition::setUserDocumentation(string const& _userDocumentation)
|
|
|
|
{
|
|
|
|
m_userDocumentation = _userDocumentation;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-08-31 16:44:29 +00:00
|
|
|
vector<Declaration const*> const& ContractDefinition::inheritableMembers() const
|
2015-02-25 19:35:55 +00:00
|
|
|
{
|
|
|
|
if (!m_inheritableMembers)
|
|
|
|
{
|
|
|
|
set<string> memberSeen;
|
2015-03-02 11:08:32 +00:00
|
|
|
m_inheritableMembers.reset(new vector<Declaration const*>());
|
|
|
|
auto addInheritableMember = [&](Declaration const* _decl)
|
2015-02-25 19:35:55 +00:00
|
|
|
{
|
2015-08-31 16:44:29 +00:00
|
|
|
if (memberSeen.count(_decl->name()) == 0 && _decl->isVisibleInDerivedContracts())
|
2015-02-27 10:35:25 +00:00
|
|
|
{
|
2015-08-31 16:44:29 +00:00
|
|
|
memberSeen.insert(_decl->name());
|
2015-02-28 17:30:33 +00:00
|
|
|
m_inheritableMembers->push_back(_decl);
|
|
|
|
}
|
|
|
|
};
|
2015-02-27 10:35:25 +00:00
|
|
|
|
2015-08-31 16:44:29 +00:00
|
|
|
for (ASTPointer<FunctionDefinition> const& f: definedFunctions())
|
2015-03-02 11:08:32 +00:00
|
|
|
addInheritableMember(f.get());
|
2015-02-25 19:35:55 +00:00
|
|
|
|
2015-08-31 16:44:29 +00:00
|
|
|
for (ASTPointer<VariableDeclaration> const& v: stateVariables())
|
2015-03-02 11:08:32 +00:00
|
|
|
addInheritableMember(v.get());
|
2015-02-26 11:11:54 +00:00
|
|
|
|
2015-08-31 16:44:29 +00:00
|
|
|
for (ASTPointer<StructDefinition> const& s: definedStructs())
|
2015-03-02 11:08:32 +00:00
|
|
|
addInheritableMember(s.get());
|
2015-02-25 19:35:55 +00:00
|
|
|
}
|
|
|
|
return *m_inheritableMembers;
|
|
|
|
}
|
|
|
|
|
2015-08-31 16:44:29 +00:00
|
|
|
TypePointer EnumValue::type(ContractDefinition const*) const
|
2015-02-10 12:40:21 +00:00
|
|
|
{
|
2015-08-31 16:44:29 +00:00
|
|
|
EnumDefinition const* parentDef = dynamic_cast<EnumDefinition const*>(scope());
|
2015-02-13 21:52:04 +00:00
|
|
|
solAssert(parentDef, "Enclosing Scope of EnumValue was not set");
|
2015-02-13 16:32:34 +00:00
|
|
|
return make_shared<EnumType>(*parentDef);
|
2015-02-10 12:40:21 +00:00
|
|
|
}
|
|
|
|
|
2015-01-19 20:05:47 +00:00
|
|
|
void InheritanceSpecifier::checkTypeRequirements()
|
|
|
|
{
|
2015-04-15 15:40:50 +00:00
|
|
|
m_baseName->checkTypeRequirements(nullptr);
|
2015-01-19 20:05:47 +00:00
|
|
|
for (ASTPointer<Expression> const& argument: m_arguments)
|
2015-04-15 15:40:50 +00:00
|
|
|
argument->checkTypeRequirements(nullptr);
|
2015-01-19 20:05:47 +00:00
|
|
|
|
2015-08-31 16:44:29 +00:00
|
|
|
ContractDefinition const* base = dynamic_cast<ContractDefinition const*>(&m_baseName->referencedDeclaration());
|
2015-01-19 20:05:47 +00:00
|
|
|
solAssert(base, "Base contract not available.");
|
2015-09-08 14:48:33 +00:00
|
|
|
|
|
|
|
if (base->isLibrary())
|
|
|
|
BOOST_THROW_EXCEPTION(createTypeError("Libraries cannot be inherited from."));
|
|
|
|
|
2015-08-31 16:44:29 +00:00
|
|
|
TypePointers parameterTypes = ContractType(*base).constructorType()->parameterTypes();
|
2015-04-02 15:05:27 +00:00
|
|
|
if (!m_arguments.empty() && parameterTypes.size() != m_arguments.size())
|
2015-07-14 14:51:36 +00:00
|
|
|
BOOST_THROW_EXCEPTION(createTypeError(
|
|
|
|
"Wrong argument count for constructor call: " +
|
|
|
|
toString(m_arguments.size()) +
|
|
|
|
" arguments given but expected " +
|
|
|
|
toString(parameterTypes.size()) +
|
|
|
|
"."
|
|
|
|
));
|
|
|
|
|
2015-01-19 20:05:47 +00:00
|
|
|
for (size_t i = 0; i < m_arguments.size(); ++i)
|
2015-08-31 16:44:29 +00:00
|
|
|
if (!m_arguments[i]->type()->isImplicitlyConvertibleTo(*parameterTypes[i]))
|
2015-06-09 09:57:06 +00:00
|
|
|
BOOST_THROW_EXCEPTION(m_arguments[i]->createTypeError(
|
|
|
|
"Invalid type for argument in constructor call. "
|
|
|
|
"Invalid implicit conversion from " +
|
2015-08-31 16:44:29 +00:00
|
|
|
m_arguments[i]->type()->toString() +
|
2015-06-09 09:57:06 +00:00
|
|
|
" to " +
|
|
|
|
parameterTypes[i]->toString() +
|
|
|
|
" requested."
|
|
|
|
));
|
2015-01-19 20:05:47 +00:00
|
|
|
}
|
|
|
|
|
2015-08-31 16:44:29 +00:00
|
|
|
TypePointer StructDefinition::type(ContractDefinition const*) const
|
2015-01-22 00:02:38 +00:00
|
|
|
{
|
|
|
|
return make_shared<TypeType>(make_shared<StructType>(*this));
|
|
|
|
}
|
|
|
|
|
2014-12-06 01:19:10 +00:00
|
|
|
void StructDefinition::checkMemberTypes() const
|
2014-12-03 06:46:55 +00:00
|
|
|
{
|
2015-08-31 16:44:29 +00:00
|
|
|
for (ASTPointer<VariableDeclaration> const& member: members())
|
|
|
|
if (!member->type()->canBeStored())
|
2014-12-03 06:46:55 +00:00
|
|
|
BOOST_THROW_EXCEPTION(member->createTypeError("Type cannot be used in struct."));
|
|
|
|
}
|
|
|
|
|
2014-12-06 01:19:10 +00:00
|
|
|
void StructDefinition::checkRecursion() const
|
2014-12-03 06:46:55 +00:00
|
|
|
{
|
2015-06-18 14:35:25 +00:00
|
|
|
using StructPointer = StructDefinition const*;
|
|
|
|
using StructPointersSet = set<StructPointer>;
|
|
|
|
function<void(StructPointer,StructPointersSet const&)> check = [&](StructPointer _struct, StructPointersSet const& _parents)
|
2014-12-03 06:46:55 +00:00
|
|
|
{
|
2015-06-18 14:35:25 +00:00
|
|
|
if (_parents.count(_struct))
|
|
|
|
BOOST_THROW_EXCEPTION(
|
|
|
|
ParserError() <<
|
2015-08-31 16:44:29 +00:00
|
|
|
errinfo_sourceLocation(_struct->location()) <<
|
2015-06-18 14:35:25 +00:00
|
|
|
errinfo_comment("Recursive struct definition.")
|
|
|
|
);
|
|
|
|
set<StructDefinition const*> parents = _parents;
|
|
|
|
parents.insert(_struct);
|
2015-08-31 16:44:29 +00:00
|
|
|
for (ASTPointer<VariableDeclaration> const& member: _struct->members())
|
|
|
|
if (member->type()->category() == Type::Category::Struct)
|
2014-12-03 06:46:55 +00:00
|
|
|
{
|
2015-08-31 16:44:29 +00:00
|
|
|
auto const& typeName = dynamic_cast<UserDefinedTypeName const&>(*member->typeName());
|
2015-06-18 14:35:25 +00:00
|
|
|
check(
|
2015-08-31 16:44:29 +00:00
|
|
|
&dynamic_cast<StructDefinition const&>(*typeName.referencedDeclaration()),
|
2015-06-18 14:35:25 +00:00
|
|
|
parents
|
|
|
|
);
|
2014-12-03 06:46:55 +00:00
|
|
|
}
|
2015-06-18 14:35:25 +00:00
|
|
|
};
|
2015-07-16 19:04:25 +00:00
|
|
|
check(this, StructPointersSet{});
|
2014-12-03 06:46:55 +00:00
|
|
|
}
|
|
|
|
|
2015-08-31 16:44:29 +00:00
|
|
|
TypePointer EnumDefinition::type(ContractDefinition const*) const
|
2015-02-09 17:08:56 +00:00
|
|
|
{
|
2015-02-11 15:37:46 +00:00
|
|
|
return make_shared<TypeType>(make_shared<EnumType>(*this));
|
2015-02-09 17:08:56 +00:00
|
|
|
}
|
|
|
|
|
2015-08-31 16:44:29 +00:00
|
|
|
TypePointer FunctionDefinition::type(ContractDefinition const*) const
|
2015-01-22 00:02:38 +00:00
|
|
|
{
|
|
|
|
return make_shared<FunctionType>(*this);
|
|
|
|
}
|
|
|
|
|
2014-11-10 16:31:09 +00:00
|
|
|
void FunctionDefinition::checkTypeRequirements()
|
|
|
|
{
|
2015-08-31 16:44:29 +00:00
|
|
|
for (ASTPointer<VariableDeclaration> const& var: parameters() + returnParameters())
|
2015-03-19 16:33:10 +00:00
|
|
|
{
|
2015-08-31 16:44:29 +00:00
|
|
|
if (!var->type()->canLiveOutsideStorage())
|
2014-11-10 16:31:09 +00:00
|
|
|
BOOST_THROW_EXCEPTION(var->createTypeError("Type is required to live outside storage."));
|
2015-08-31 16:44:29 +00:00
|
|
|
if (visibility() >= Visibility::Public && !(var->type()->externalType()))
|
2015-04-21 08:59:48 +00:00
|
|
|
BOOST_THROW_EXCEPTION(var->createTypeError("Internal type is not allowed for public and external functions."));
|
2015-03-19 16:33:10 +00:00
|
|
|
}
|
2015-01-22 00:02:38 +00:00
|
|
|
for (ASTPointer<ModifierInvocation> const& modifier: m_functionModifiers)
|
2015-02-27 16:41:22 +00:00
|
|
|
modifier->checkTypeRequirements(isConstructor() ?
|
2015-08-31 16:44:29 +00:00
|
|
|
dynamic_cast<ContractDefinition const&>(*scope()).linearizedBaseContracts() :
|
2015-04-02 13:10:35 +00:00
|
|
|
vector<ContractDefinition const*>());
|
2015-03-20 16:50:26 +00:00
|
|
|
if (m_body)
|
|
|
|
m_body->checkTypeRequirements();
|
2014-11-10 16:31:09 +00:00
|
|
|
}
|
|
|
|
|
2015-03-27 12:28:32 +00:00
|
|
|
string FunctionDefinition::externalSignature() const
|
2015-01-06 16:42:38 +00:00
|
|
|
{
|
2015-08-31 16:44:29 +00:00
|
|
|
return FunctionType(*this).externalSignature(name());
|
2015-01-06 16:42:38 +00:00
|
|
|
}
|
|
|
|
|
2015-02-14 00:22:44 +00:00
|
|
|
bool VariableDeclaration::isLValue() const
|
2015-01-22 00:02:38 +00:00
|
|
|
{
|
2015-03-03 11:58:01 +00:00
|
|
|
// External function parameters and constant declared variables are Read-Only
|
2015-06-09 12:26:08 +00:00
|
|
|
return !isExternalCallableParameter() && !m_isConstant;
|
2015-02-14 00:22:44 +00:00
|
|
|
}
|
|
|
|
|
2015-02-17 15:21:38 +00:00
|
|
|
void VariableDeclaration::checkTypeRequirements()
|
|
|
|
{
|
2015-03-06 12:44:37 +00:00
|
|
|
// Variables can be declared without type (with "var"), in which case the first assignment
|
|
|
|
// sets the type.
|
|
|
|
// Note that assignments before the first declaration are legal because of the special scoping
|
|
|
|
// rules inherited from JavaScript.
|
2015-03-13 17:16:04 +00:00
|
|
|
if (m_isConstant)
|
|
|
|
{
|
2015-08-31 16:44:29 +00:00
|
|
|
if (!dynamic_cast<ContractDefinition const*>(scope()))
|
2015-03-13 17:16:04 +00:00
|
|
|
BOOST_THROW_EXCEPTION(createTypeError("Illegal use of \"constant\" specifier."));
|
2015-08-21 15:57:57 +00:00
|
|
|
if (!m_value)
|
|
|
|
BOOST_THROW_EXCEPTION(createTypeError("Uninitialized \"constant\" variable."));
|
2015-09-08 10:57:27 +00:00
|
|
|
if (m_type && !m_type->isValueType())
|
|
|
|
{
|
|
|
|
// TODO: const is implemented only for uint, bytesXX, string and enums types.
|
|
|
|
bool constImplemented = false;
|
|
|
|
if (auto arrayType = dynamic_cast<ArrayType const*>(m_type.get()))
|
|
|
|
constImplemented = arrayType->isByteArray();
|
|
|
|
if (!constImplemented)
|
|
|
|
BOOST_THROW_EXCEPTION(createTypeError(
|
|
|
|
"Illegal use of \"constant\" specifier. \"constant\" "
|
2015-09-09 14:15:35 +00:00
|
|
|
"is not yet implemented for this type."
|
2015-09-08 10:57:27 +00:00
|
|
|
));
|
|
|
|
}
|
2015-03-13 17:16:04 +00:00
|
|
|
}
|
2015-03-06 12:44:37 +00:00
|
|
|
if (m_type)
|
2015-03-20 16:02:24 +00:00
|
|
|
{
|
2015-04-15 22:06:57 +00:00
|
|
|
if (m_value)
|
|
|
|
m_value->expectType(*m_type);
|
2015-04-01 13:19:33 +00:00
|
|
|
}
|
2015-03-06 12:44:37 +00:00
|
|
|
else
|
|
|
|
{
|
2015-04-15 22:06:57 +00:00
|
|
|
if (!m_value)
|
|
|
|
// This feature might be extended in the future.
|
|
|
|
BOOST_THROW_EXCEPTION(createTypeError("Assignment necessary for type detection."));
|
2015-04-15 15:40:50 +00:00
|
|
|
m_value->checkTypeRequirements(nullptr);
|
2015-03-08 22:26:36 +00:00
|
|
|
|
2015-08-31 16:44:29 +00:00
|
|
|
TypePointer const& type = m_value->type();
|
2015-06-09 12:26:08 +00:00
|
|
|
if (
|
2015-08-31 16:44:29 +00:00
|
|
|
type->category() == Type::Category::IntegerConstant &&
|
|
|
|
!dynamic_pointer_cast<IntegerConstantType const>(type)->integerType()
|
2015-06-09 12:26:08 +00:00
|
|
|
)
|
|
|
|
BOOST_THROW_EXCEPTION(m_value->createTypeError("Invalid integer constant " + type->toString() + "."));
|
2015-08-31 16:44:29 +00:00
|
|
|
else if (type->category() == Type::Category::Void)
|
2015-03-06 12:44:37 +00:00
|
|
|
BOOST_THROW_EXCEPTION(createTypeError("Variable cannot have void type."));
|
2015-06-09 12:26:08 +00:00
|
|
|
m_type = type->mobileType();
|
2015-03-06 12:44:37 +00:00
|
|
|
}
|
2015-06-26 19:27:53 +00:00
|
|
|
solAssert(!!m_type, "");
|
|
|
|
if (!m_isStateVariable)
|
|
|
|
{
|
|
|
|
if (m_type->dataStoredIn(DataLocation::Memory) || m_type->dataStoredIn(DataLocation::CallData))
|
|
|
|
if (!m_type->canLiveOutsideStorage())
|
|
|
|
BOOST_THROW_EXCEPTION(createTypeError(
|
|
|
|
"Type " + m_type->toString() + " is only valid in storage."
|
|
|
|
));
|
|
|
|
}
|
2015-08-31 16:44:29 +00:00
|
|
|
else if (visibility() >= Visibility::Public && !FunctionType(*this).externalType())
|
2015-04-15 22:06:57 +00:00
|
|
|
BOOST_THROW_EXCEPTION(createTypeError("Internal type is not allowed for public state variables."));
|
2015-02-17 15:21:38 +00:00
|
|
|
}
|
|
|
|
|
2015-06-09 12:26:08 +00:00
|
|
|
bool VariableDeclaration::isCallableParameter() const
|
2015-06-05 09:07:50 +00:00
|
|
|
{
|
2015-08-31 16:44:29 +00:00
|
|
|
auto const* callable = dynamic_cast<CallableDeclaration const*>(scope());
|
2015-06-09 12:26:08 +00:00
|
|
|
if (!callable)
|
2015-06-05 09:07:50 +00:00
|
|
|
return false;
|
2015-08-31 16:44:29 +00:00
|
|
|
for (auto const& variable: callable->parameters())
|
2015-06-05 09:07:50 +00:00
|
|
|
if (variable.get() == this)
|
|
|
|
return true;
|
2015-08-31 16:44:29 +00:00
|
|
|
if (callable->returnParameterList())
|
|
|
|
for (auto const& variable: callable->returnParameterList()->parameters())
|
2015-06-09 12:26:08 +00:00
|
|
|
if (variable.get() == this)
|
|
|
|
return true;
|
2015-06-05 09:07:50 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-06-09 12:26:08 +00:00
|
|
|
bool VariableDeclaration::isExternalCallableParameter() const
|
2015-02-14 00:22:44 +00:00
|
|
|
{
|
2015-08-31 16:44:29 +00:00
|
|
|
auto const* callable = dynamic_cast<CallableDeclaration const*>(scope());
|
|
|
|
if (!callable || callable->visibility() != Declaration::Visibility::External)
|
2015-02-14 00:22:44 +00:00
|
|
|
return false;
|
2015-08-31 16:44:29 +00:00
|
|
|
for (auto const& variable: callable->parameters())
|
2015-02-14 00:22:44 +00:00
|
|
|
if (variable.get() == this)
|
|
|
|
return true;
|
|
|
|
return false;
|
2015-01-22 00:02:38 +00:00
|
|
|
}
|
|
|
|
|
2015-08-31 16:44:29 +00:00
|
|
|
TypePointer ModifierDefinition::type(ContractDefinition const*) const
|
2015-01-22 00:02:38 +00:00
|
|
|
{
|
|
|
|
return make_shared<ModifierType>(*this);
|
|
|
|
}
|
|
|
|
|
2015-01-21 10:16:18 +00:00
|
|
|
void ModifierDefinition::checkTypeRequirements()
|
|
|
|
{
|
|
|
|
m_body->checkTypeRequirements();
|
|
|
|
}
|
|
|
|
|
2015-04-02 13:10:35 +00:00
|
|
|
void ModifierInvocation::checkTypeRequirements(vector<ContractDefinition const*> const& _bases)
|
2015-01-22 00:02:38 +00:00
|
|
|
{
|
2015-04-15 15:40:50 +00:00
|
|
|
TypePointers argumentTypes;
|
2015-01-22 00:02:38 +00:00
|
|
|
for (ASTPointer<Expression> const& argument: m_arguments)
|
2015-04-15 15:40:50 +00:00
|
|
|
{
|
|
|
|
argument->checkTypeRequirements(nullptr);
|
2015-08-31 16:44:29 +00:00
|
|
|
argumentTypes.push_back(argument->type());
|
2015-04-15 15:40:50 +00:00
|
|
|
}
|
|
|
|
m_modifierName->checkTypeRequirements(&argumentTypes);
|
2015-01-22 00:02:38 +00:00
|
|
|
|
2015-08-31 16:44:29 +00:00
|
|
|
auto const* declaration = &m_modifierName->referencedDeclaration();
|
2015-02-27 16:41:22 +00:00
|
|
|
vector<ASTPointer<VariableDeclaration>> emptyParameterList;
|
|
|
|
vector<ASTPointer<VariableDeclaration>> const* parameters = nullptr;
|
|
|
|
if (auto modifier = dynamic_cast<ModifierDefinition const*>(declaration))
|
2015-08-31 16:44:29 +00:00
|
|
|
parameters = &modifier->parameters();
|
2015-02-27 16:41:22 +00:00
|
|
|
else
|
2015-03-02 13:21:12 +00:00
|
|
|
// check parameters for Base constructors
|
2015-04-20 12:55:58 +00:00
|
|
|
for (ContractDefinition const* base: _bases)
|
2015-04-02 13:10:35 +00:00
|
|
|
if (declaration == base)
|
2015-02-27 16:41:22 +00:00
|
|
|
{
|
2015-08-31 16:44:29 +00:00
|
|
|
if (auto referencedConstructor = base->constructor())
|
|
|
|
parameters = &referencedConstructor->parameters();
|
2015-02-27 16:41:22 +00:00
|
|
|
else
|
|
|
|
parameters = &emptyParameterList;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (!parameters)
|
|
|
|
BOOST_THROW_EXCEPTION(createTypeError("Referenced declaration is neither modifier nor base class."));
|
|
|
|
if (parameters->size() != m_arguments.size())
|
2015-07-14 14:51:36 +00:00
|
|
|
BOOST_THROW_EXCEPTION(createTypeError(
|
|
|
|
"Wrong argument count for modifier invocation: " +
|
|
|
|
toString(m_arguments.size()) +
|
|
|
|
" arguments given but expected " +
|
|
|
|
toString(parameters->size()) +
|
|
|
|
"."
|
|
|
|
));
|
2015-01-22 00:02:38 +00:00
|
|
|
for (size_t i = 0; i < m_arguments.size(); ++i)
|
2015-08-31 16:44:29 +00:00
|
|
|
if (!m_arguments[i]->type()->isImplicitlyConvertibleTo(*(*parameters)[i]->type()))
|
2015-06-09 09:57:06 +00:00
|
|
|
BOOST_THROW_EXCEPTION(m_arguments[i]->createTypeError(
|
|
|
|
"Invalid type for argument in modifier invocation. "
|
|
|
|
"Invalid implicit conversion from " +
|
2015-08-31 16:44:29 +00:00
|
|
|
m_arguments[i]->type()->toString() +
|
2015-06-09 09:57:06 +00:00
|
|
|
" to " +
|
2015-08-31 16:44:29 +00:00
|
|
|
(*parameters)[i]->type()->toString() +
|
2015-06-09 09:57:06 +00:00
|
|
|
" requested."
|
|
|
|
));
|
2015-01-22 00:02:38 +00:00
|
|
|
}
|
|
|
|
|
2015-01-29 13:35:28 +00:00
|
|
|
void EventDefinition::checkTypeRequirements()
|
|
|
|
{
|
|
|
|
int numIndexed = 0;
|
2015-08-31 16:44:29 +00:00
|
|
|
for (ASTPointer<VariableDeclaration> const& var: parameters())
|
2015-01-29 13:35:28 +00:00
|
|
|
{
|
|
|
|
if (var->isIndexed())
|
|
|
|
numIndexed++;
|
2015-09-11 13:24:11 +00:00
|
|
|
if (numIndexed > 3)
|
|
|
|
BOOST_THROW_EXCEPTION(createTypeError("More than 3 indexed arguments for event."));
|
2015-08-31 16:44:29 +00:00
|
|
|
if (!var->type()->canLiveOutsideStorage())
|
2015-01-29 13:35:28 +00:00
|
|
|
BOOST_THROW_EXCEPTION(var->createTypeError("Type is required to live outside storage."));
|
2015-08-31 16:44:29 +00:00
|
|
|
if (!var->type()->externalType())
|
2015-03-26 13:11:24 +00:00
|
|
|
BOOST_THROW_EXCEPTION(var->createTypeError("Internal type is not allowed as event parameter type."));
|
2015-01-29 13:35:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-20 12:00:37 +00:00
|
|
|
void Block::checkTypeRequirements()
|
2014-10-13 16:22:15 +00:00
|
|
|
{
|
2014-10-24 17:06:30 +00:00
|
|
|
for (shared_ptr<Statement> const& statement: m_statements)
|
2014-10-13 16:22:15 +00:00
|
|
|
statement->checkTypeRequirements();
|
|
|
|
}
|
|
|
|
|
2014-10-20 12:00:37 +00:00
|
|
|
void IfStatement::checkTypeRequirements()
|
2014-10-13 16:22:15 +00:00
|
|
|
{
|
2014-10-30 00:20:32 +00:00
|
|
|
m_condition->expectType(BoolType());
|
2014-10-13 16:22:15 +00:00
|
|
|
m_trueBody->checkTypeRequirements();
|
2014-10-16 21:49:45 +00:00
|
|
|
if (m_falseBody)
|
|
|
|
m_falseBody->checkTypeRequirements();
|
2014-10-13 16:22:15 +00:00
|
|
|
}
|
|
|
|
|
2014-10-20 12:00:37 +00:00
|
|
|
void WhileStatement::checkTypeRequirements()
|
2014-10-13 16:22:15 +00:00
|
|
|
{
|
2014-10-30 00:20:32 +00:00
|
|
|
m_condition->expectType(BoolType());
|
2014-10-13 16:22:15 +00:00
|
|
|
m_body->checkTypeRequirements();
|
|
|
|
}
|
|
|
|
|
2014-12-15 16:45:18 +00:00
|
|
|
void ForStatement::checkTypeRequirements()
|
|
|
|
{
|
2014-12-16 12:20:50 +00:00
|
|
|
if (m_initExpression)
|
|
|
|
m_initExpression->checkTypeRequirements();
|
|
|
|
if (m_condExpression)
|
|
|
|
m_condExpression->expectType(BoolType());
|
|
|
|
if (m_loopExpression)
|
|
|
|
m_loopExpression->checkTypeRequirements();
|
2014-12-15 16:45:18 +00:00
|
|
|
m_body->checkTypeRequirements();
|
|
|
|
}
|
|
|
|
|
2014-10-20 12:00:37 +00:00
|
|
|
void Return::checkTypeRequirements()
|
2014-10-13 16:22:15 +00:00
|
|
|
{
|
2014-10-30 00:16:11 +00:00
|
|
|
if (!m_expression)
|
|
|
|
return;
|
2015-01-23 01:35:27 +00:00
|
|
|
if (!m_returnParameters)
|
|
|
|
BOOST_THROW_EXCEPTION(createTypeError("Return arguments not allowed."));
|
2015-08-31 16:44:29 +00:00
|
|
|
if (m_returnParameters->parameters().size() != 1)
|
2015-09-09 15:43:01 +00:00
|
|
|
BOOST_THROW_EXCEPTION(createTypeError(
|
|
|
|
"Different number of arguments in return statement "
|
|
|
|
"than in returns declaration."
|
|
|
|
));
|
2014-10-13 16:22:15 +00:00
|
|
|
// this could later be changed such that the paramaters type is an anonymous struct type,
|
|
|
|
// but for now, we only allow one return parameter
|
2015-08-31 16:44:29 +00:00
|
|
|
m_expression->expectType(*m_returnParameters->parameters().front()->type());
|
2014-10-13 16:22:15 +00:00
|
|
|
}
|
|
|
|
|
2015-02-17 15:21:38 +00:00
|
|
|
void VariableDeclarationStatement::checkTypeRequirements()
|
2014-10-13 16:22:15 +00:00
|
|
|
{
|
2015-03-06 12:44:37 +00:00
|
|
|
m_variable->checkTypeRequirements();
|
2014-10-13 16:22:15 +00:00
|
|
|
}
|
2015-03-06 12:44:37 +00:00
|
|
|
|
2015-04-15 15:40:50 +00:00
|
|
|
void Assignment::checkTypeRequirements(TypePointers const*)
|
2014-10-13 16:22:15 +00:00
|
|
|
{
|
2015-04-15 15:40:50 +00:00
|
|
|
m_leftHandSide->checkTypeRequirements(nullptr);
|
2014-11-10 16:31:09 +00:00
|
|
|
m_leftHandSide->requireLValue();
|
2015-08-31 16:44:29 +00:00
|
|
|
if (m_leftHandSide->type()->category() == Type::Category::Mapping)
|
2015-02-12 17:38:07 +00:00
|
|
|
BOOST_THROW_EXCEPTION(createTypeError("Mappings cannot be assigned to."));
|
2015-08-31 16:44:29 +00:00
|
|
|
m_type = m_leftHandSide->type();
|
2015-02-09 13:00:12 +00:00
|
|
|
if (m_assigmentOperator == Token::Assign)
|
2014-12-18 17:53:43 +00:00
|
|
|
m_rightHandSide->expectType(*m_type);
|
|
|
|
else
|
|
|
|
{
|
2014-10-20 10:41:56 +00:00
|
|
|
// compound assignment
|
2015-04-15 15:40:50 +00:00
|
|
|
m_rightHandSide->checkTypeRequirements(nullptr);
|
2015-01-06 17:55:31 +00:00
|
|
|
TypePointer resultType = m_type->binaryOperatorResult(Token::AssignmentToBinaryOp(m_assigmentOperator),
|
2015-08-31 16:44:29 +00:00
|
|
|
m_rightHandSide->type());
|
2014-12-18 17:53:43 +00:00
|
|
|
if (!resultType || *resultType != *m_type)
|
2015-09-09 15:43:01 +00:00
|
|
|
BOOST_THROW_EXCEPTION(createTypeError(
|
|
|
|
"Operator " +
|
|
|
|
string(Token::toString(m_assigmentOperator)) +
|
|
|
|
" not compatible with types " +
|
|
|
|
m_type->toString() +
|
|
|
|
" and " +
|
|
|
|
m_rightHandSide->type()->toString()
|
|
|
|
));
|
2014-12-18 17:53:43 +00:00
|
|
|
}
|
2014-10-13 16:22:15 +00:00
|
|
|
}
|
|
|
|
|
2014-10-30 00:20:32 +00:00
|
|
|
void ExpressionStatement::checkTypeRequirements()
|
|
|
|
{
|
2015-04-15 15:40:50 +00:00
|
|
|
m_expression->checkTypeRequirements(nullptr);
|
2015-08-31 16:44:29 +00:00
|
|
|
if (m_expression->type()->category() == Type::Category::IntegerConstant)
|
|
|
|
if (!dynamic_pointer_cast<IntegerConstantType const>(m_expression->type())->integerType())
|
2014-12-19 10:31:17 +00:00
|
|
|
BOOST_THROW_EXCEPTION(m_expression->createTypeError("Invalid integer constant."));
|
2014-10-30 00:20:32 +00:00
|
|
|
}
|
|
|
|
|
2014-11-05 14:04:33 +00:00
|
|
|
void Expression::expectType(Type const& _expectedType)
|
2014-10-30 00:20:32 +00:00
|
|
|
{
|
2015-04-15 15:40:50 +00:00
|
|
|
checkTypeRequirements(nullptr);
|
2015-08-31 16:44:29 +00:00
|
|
|
Type const& currentType = *type();
|
|
|
|
if (!currentType.isImplicitlyConvertibleTo(_expectedType))
|
|
|
|
BOOST_THROW_EXCEPTION(
|
|
|
|
createTypeError(
|
|
|
|
"Type " +
|
|
|
|
currentType.toString() +
|
|
|
|
" is not implicitly convertible to expected type " +
|
|
|
|
_expectedType.toString() +
|
|
|
|
"."
|
|
|
|
)
|
|
|
|
);
|
2014-10-30 00:20:32 +00:00
|
|
|
}
|
|
|
|
|
2014-11-10 16:31:09 +00:00
|
|
|
void Expression::requireLValue()
|
|
|
|
{
|
2014-12-01 16:32:10 +00:00
|
|
|
if (!isLValue())
|
2014-11-10 16:31:09 +00:00
|
|
|
BOOST_THROW_EXCEPTION(createTypeError("Expression has to be an lvalue."));
|
|
|
|
m_lvalueRequested = true;
|
|
|
|
}
|
|
|
|
|
2015-04-15 15:40:50 +00:00
|
|
|
void UnaryOperation::checkTypeRequirements(TypePointers const*)
|
2014-10-13 16:22:15 +00:00
|
|
|
{
|
2015-02-10 08:52:19 +00:00
|
|
|
// Inc, Dec, Add, Sub, Not, BitNot, Delete
|
2015-04-15 15:40:50 +00:00
|
|
|
m_subExpression->checkTypeRequirements(nullptr);
|
2015-02-09 13:00:12 +00:00
|
|
|
if (m_operator == Token::Value::Inc || m_operator == Token::Value::Dec || m_operator == Token::Value::Delete)
|
2014-11-10 16:31:09 +00:00
|
|
|
m_subExpression->requireLValue();
|
2015-08-31 16:44:29 +00:00
|
|
|
m_type = m_subExpression->type()->unaryOperatorResult(m_operator);
|
2015-01-06 18:08:24 +00:00
|
|
|
if (!m_type)
|
2014-10-23 17:22:30 +00:00
|
|
|
BOOST_THROW_EXCEPTION(createTypeError("Unary operator not compatible with type."));
|
2014-10-13 16:22:15 +00:00
|
|
|
}
|
|
|
|
|
2015-04-15 15:40:50 +00:00
|
|
|
void BinaryOperation::checkTypeRequirements(TypePointers const*)
|
2014-10-13 16:22:15 +00:00
|
|
|
{
|
2015-04-15 15:40:50 +00:00
|
|
|
m_left->checkTypeRequirements(nullptr);
|
|
|
|
m_right->checkTypeRequirements(nullptr);
|
2015-08-31 16:44:29 +00:00
|
|
|
m_commonType = m_left->type()->binaryOperatorResult(m_operator, m_right->type());
|
2014-12-18 17:53:43 +00:00
|
|
|
if (!m_commonType)
|
2015-09-09 15:43:01 +00:00
|
|
|
BOOST_THROW_EXCEPTION(createTypeError(
|
|
|
|
"Operator " + string(Token::toString(m_operator)) +
|
|
|
|
" not compatible with types " +
|
|
|
|
m_left->type()->toString() +
|
|
|
|
" and " +
|
|
|
|
m_right->type()->toString()
|
|
|
|
));
|
2014-12-18 17:53:43 +00:00
|
|
|
m_type = Token::isCompareOp(m_operator) ? make_shared<BoolType>() : m_commonType;
|
2014-10-13 16:22:15 +00:00
|
|
|
}
|
|
|
|
|
2015-04-15 15:40:50 +00:00
|
|
|
void FunctionCall::checkTypeRequirements(TypePointers const*)
|
2014-10-13 16:22:15 +00:00
|
|
|
{
|
2015-04-15 15:40:50 +00:00
|
|
|
bool isPositionalCall = m_names.empty();
|
|
|
|
|
|
|
|
// we need to check arguments' type first as they will be forwarded to
|
|
|
|
// m_expression->checkTypeRequirements
|
|
|
|
TypePointers argumentTypes;
|
2014-10-20 12:00:37 +00:00
|
|
|
for (ASTPointer<Expression> const& argument: m_arguments)
|
2015-04-15 15:40:50 +00:00
|
|
|
{
|
|
|
|
argument->checkTypeRequirements(nullptr);
|
|
|
|
// only store them for positional calls
|
|
|
|
if (isPositionalCall)
|
2015-08-31 16:44:29 +00:00
|
|
|
argumentTypes.push_back(argument->type());
|
2015-04-15 15:40:50 +00:00
|
|
|
}
|
2014-10-20 10:41:56 +00:00
|
|
|
|
2015-04-15 15:40:50 +00:00
|
|
|
m_expression->checkTypeRequirements(isPositionalCall ? &argumentTypes : nullptr);
|
2015-03-01 03:34:39 +00:00
|
|
|
|
2015-08-31 16:44:29 +00:00
|
|
|
TypePointer const& expressionType = m_expression->type();
|
2015-06-30 19:08:34 +00:00
|
|
|
FunctionTypePointer functionType;
|
2014-10-20 10:41:56 +00:00
|
|
|
if (isTypeConversion())
|
2014-10-16 12:08:54 +00:00
|
|
|
{
|
2014-11-05 13:20:56 +00:00
|
|
|
TypeType const& type = dynamic_cast<TypeType const&>(*expressionType);
|
2014-10-13 16:22:15 +00:00
|
|
|
if (m_arguments.size() != 1)
|
2015-05-18 09:34:16 +00:00
|
|
|
BOOST_THROW_EXCEPTION(createTypeError("Exactly one argument expected for explicit type conversion."));
|
2015-04-15 15:40:50 +00:00
|
|
|
if (!isPositionalCall)
|
2015-02-08 23:49:35 +00:00
|
|
|
BOOST_THROW_EXCEPTION(createTypeError("Type conversion cannot allow named arguments."));
|
2015-08-31 16:44:29 +00:00
|
|
|
m_type = type.actualType();
|
|
|
|
auto argType = m_arguments.front()->type();
|
2015-08-04 09:06:57 +00:00
|
|
|
if (auto argRefType = dynamic_cast<ReferenceType const*>(argType.get()))
|
|
|
|
// do not change the data location when converting
|
|
|
|
// (data location cannot yet be specified for type conversions)
|
|
|
|
m_type = ReferenceType::copyForLocationIfReference(argRefType->location(), m_type);
|
|
|
|
if (!argType->isExplicitlyConvertibleTo(*m_type))
|
|
|
|
BOOST_THROW_EXCEPTION(createTypeError("Explicit type conversion not allowed."));
|
|
|
|
|
2015-06-30 19:08:34 +00:00
|
|
|
return;
|
2014-10-16 12:08:54 +00:00
|
|
|
}
|
2015-06-30 19:08:34 +00:00
|
|
|
|
2015-07-15 23:06:19 +00:00
|
|
|
/// For error message: Struct members that were removed during conversion to memory.
|
|
|
|
set<string> membersRemovedForStructConstructor;
|
2015-06-30 19:08:34 +00:00
|
|
|
if (isStructConstructorCall())
|
2014-10-16 12:08:54 +00:00
|
|
|
{
|
2015-06-30 19:08:34 +00:00
|
|
|
TypeType const& type = dynamic_cast<TypeType const&>(*expressionType);
|
2015-08-31 16:44:29 +00:00
|
|
|
auto const& structType = dynamic_cast<StructType const&>(*type.actualType());
|
2015-06-30 19:08:34 +00:00
|
|
|
functionType = structType.constructorType();
|
2015-07-15 23:06:19 +00:00
|
|
|
membersRemovedForStructConstructor = structType.membersMissingInMemory();
|
2015-06-30 19:08:34 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
functionType = dynamic_pointer_cast<FunctionType const>(expressionType);
|
2015-01-29 17:26:00 +00:00
|
|
|
|
2015-06-30 19:08:34 +00:00
|
|
|
if (!functionType)
|
|
|
|
BOOST_THROW_EXCEPTION(createTypeError("Type is not callable."));
|
|
|
|
|
|
|
|
//@todo would be nice to create a struct type from the arguments
|
|
|
|
// and then ask if that is implicitly convertible to the struct represented by the
|
|
|
|
// function parameters
|
2015-08-31 16:44:29 +00:00
|
|
|
TypePointers const& parameterTypes = functionType->parameterTypes();
|
2015-06-30 19:08:34 +00:00
|
|
|
if (!functionType->takesArbitraryParameters() && parameterTypes.size() != m_arguments.size())
|
2015-07-15 23:06:19 +00:00
|
|
|
{
|
|
|
|
string msg =
|
2015-07-14 14:51:36 +00:00
|
|
|
"Wrong argument count for function call: " +
|
|
|
|
toString(m_arguments.size()) +
|
|
|
|
" arguments given but expected " +
|
|
|
|
toString(parameterTypes.size()) +
|
2015-07-15 23:06:19 +00:00
|
|
|
".";
|
|
|
|
// Extend error message in case we try to construct a struct with mapping member.
|
|
|
|
if (isStructConstructorCall() && !membersRemovedForStructConstructor.empty())
|
|
|
|
{
|
|
|
|
msg += " Members that have to be skipped in memory:";
|
|
|
|
for (auto const& member: membersRemovedForStructConstructor)
|
|
|
|
msg += " " + member;
|
|
|
|
}
|
|
|
|
BOOST_THROW_EXCEPTION(createTypeError(msg));
|
|
|
|
}
|
2015-06-30 19:08:34 +00:00
|
|
|
|
|
|
|
if (isPositionalCall)
|
|
|
|
{
|
|
|
|
// call by positional arguments
|
|
|
|
for (size_t i = 0; i < m_arguments.size(); ++i)
|
|
|
|
if (
|
|
|
|
!functionType->takesArbitraryParameters() &&
|
2015-08-31 16:44:29 +00:00
|
|
|
!m_arguments[i]->type()->isImplicitlyConvertibleTo(*parameterTypes[i])
|
2015-06-30 19:08:34 +00:00
|
|
|
)
|
|
|
|
BOOST_THROW_EXCEPTION(m_arguments[i]->createTypeError(
|
|
|
|
"Invalid type for argument in function call. "
|
|
|
|
"Invalid implicit conversion from " +
|
2015-08-31 16:44:29 +00:00
|
|
|
m_arguments[i]->type()->toString() +
|
2015-06-30 19:08:34 +00:00
|
|
|
" to " +
|
|
|
|
parameterTypes[i]->toString() +
|
|
|
|
" requested."
|
|
|
|
));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// call by named arguments
|
|
|
|
if (functionType->takesArbitraryParameters())
|
|
|
|
BOOST_THROW_EXCEPTION(createTypeError(
|
|
|
|
"Named arguments cannnot be used for functions that take arbitrary parameters."
|
|
|
|
));
|
2015-08-31 16:44:29 +00:00
|
|
|
auto const& parameterNames = functionType->parameterNames();
|
2015-06-30 19:08:34 +00:00
|
|
|
if (parameterNames.size() != m_names.size())
|
|
|
|
BOOST_THROW_EXCEPTION(createTypeError("Some argument names are missing."));
|
|
|
|
|
|
|
|
// check duplicate names
|
|
|
|
for (size_t i = 0; i < m_names.size(); i++)
|
|
|
|
for (size_t j = i + 1; j < m_names.size(); j++)
|
|
|
|
if (*m_names[i] == *m_names[j])
|
|
|
|
BOOST_THROW_EXCEPTION(m_arguments[i]->createTypeError("Duplicate named argument."));
|
|
|
|
|
|
|
|
for (size_t i = 0; i < m_names.size(); i++) {
|
|
|
|
bool found = false;
|
|
|
|
for (size_t j = 0; j < parameterNames.size(); j++) {
|
|
|
|
if (parameterNames[j] == *m_names[i]) {
|
|
|
|
// check type convertible
|
2015-08-31 16:44:29 +00:00
|
|
|
if (!m_arguments[i]->type()->isImplicitlyConvertibleTo(*parameterTypes[j]))
|
2015-06-30 19:08:34 +00:00
|
|
|
BOOST_THROW_EXCEPTION(m_arguments[i]->createTypeError(
|
|
|
|
"Invalid type for argument in function call. "
|
|
|
|
"Invalid implicit conversion from " +
|
2015-08-31 16:44:29 +00:00
|
|
|
m_arguments[i]->type()->toString() +
|
2015-06-30 19:08:34 +00:00
|
|
|
" to " +
|
|
|
|
parameterTypes[i]->toString() +
|
|
|
|
" requested."
|
|
|
|
));
|
|
|
|
|
|
|
|
found = true;
|
|
|
|
break;
|
2015-01-29 17:26:00 +00:00
|
|
|
}
|
|
|
|
}
|
2015-06-30 19:08:34 +00:00
|
|
|
if (!found)
|
|
|
|
BOOST_THROW_EXCEPTION(createTypeError("Named argument does not match function declaration."));
|
2015-01-29 17:26:00 +00:00
|
|
|
}
|
2014-10-16 12:08:54 +00:00
|
|
|
}
|
2015-06-30 19:08:34 +00:00
|
|
|
|
|
|
|
// @todo actually the return type should be an anonymous struct,
|
|
|
|
// but we change it to the type of the first return value until we have anonymous
|
|
|
|
// structs and tuples
|
2015-08-31 16:44:29 +00:00
|
|
|
if (functionType->returnParameterTypes().empty())
|
2015-06-30 19:08:34 +00:00
|
|
|
m_type = make_shared<VoidType>();
|
2014-12-12 15:49:26 +00:00
|
|
|
else
|
2015-08-31 16:44:29 +00:00
|
|
|
m_type = functionType->returnParameterTypes().front();
|
2014-10-20 10:41:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool FunctionCall::isTypeConversion() const
|
|
|
|
{
|
2015-08-31 16:44:29 +00:00
|
|
|
return m_expression->type()->category() == Type::Category::TypeType && !isStructConstructorCall();
|
2015-06-30 19:08:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool FunctionCall::isStructConstructorCall() const
|
|
|
|
{
|
2015-08-31 16:44:29 +00:00
|
|
|
if (auto const* type = dynamic_cast<TypeType const*>(m_expression->type().get()))
|
|
|
|
return type->actualType()->category() == Type::Category::Struct;
|
2015-06-30 19:08:34 +00:00
|
|
|
else
|
|
|
|
return false;
|
2014-10-13 16:22:15 +00:00
|
|
|
}
|
|
|
|
|
2015-04-15 15:40:50 +00:00
|
|
|
void NewExpression::checkTypeRequirements(TypePointers const*)
|
2014-12-12 15:49:26 +00:00
|
|
|
{
|
2015-04-15 15:40:50 +00:00
|
|
|
m_contractName->checkTypeRequirements(nullptr);
|
2015-08-31 16:44:29 +00:00
|
|
|
m_contract = dynamic_cast<ContractDefinition const*>(&m_contractName->referencedDeclaration());
|
2015-09-10 15:17:13 +00:00
|
|
|
|
2014-12-12 15:49:26 +00:00
|
|
|
if (!m_contract)
|
|
|
|
BOOST_THROW_EXCEPTION(createTypeError("Identifier is not a contract."));
|
2015-03-27 14:15:34 +00:00
|
|
|
if (!m_contract->isFullyImplemented())
|
2015-04-02 15:12:15 +00:00
|
|
|
BOOST_THROW_EXCEPTION(createTypeError("Trying to create an instance of an abstract contract."));
|
2015-09-10 15:17:13 +00:00
|
|
|
|
|
|
|
auto scopeContract = m_contractName->contractScope();
|
|
|
|
auto bases = m_contract->linearizedBaseContracts();
|
|
|
|
if (find(bases.begin(), bases.end(), scopeContract) != bases.end())
|
|
|
|
BOOST_THROW_EXCEPTION(createTypeError("Circular reference for contract creation: cannot create instance of derived or same contract."));
|
|
|
|
|
2015-01-13 17:12:19 +00:00
|
|
|
shared_ptr<ContractType const> contractType = make_shared<ContractType>(*m_contract);
|
2015-08-31 16:44:29 +00:00
|
|
|
TypePointers const& parameterTypes = contractType->constructorType()->parameterTypes();
|
2015-04-22 16:53:58 +00:00
|
|
|
m_type = make_shared<FunctionType>(
|
|
|
|
parameterTypes,
|
|
|
|
TypePointers{contractType},
|
|
|
|
strings(),
|
2015-04-27 11:07:25 +00:00
|
|
|
strings(),
|
2015-09-09 15:43:01 +00:00
|
|
|
FunctionType::Location::Creation
|
|
|
|
);
|
2014-12-12 15:49:26 +00:00
|
|
|
}
|
|
|
|
|
2015-04-15 15:40:50 +00:00
|
|
|
void MemberAccess::checkTypeRequirements(TypePointers const* _argumentTypes)
|
2014-10-13 16:22:15 +00:00
|
|
|
{
|
2015-04-15 15:40:50 +00:00
|
|
|
m_expression->checkTypeRequirements(nullptr);
|
2015-08-31 16:44:29 +00:00
|
|
|
Type const& type = *m_expression->type();
|
2015-04-15 15:40:50 +00:00
|
|
|
|
2015-08-31 16:44:29 +00:00
|
|
|
MemberList::MemberMap possibleMembers = type.members().membersByName(*m_memberName);
|
2015-04-15 15:40:50 +00:00
|
|
|
if (possibleMembers.size() > 1 && _argumentTypes)
|
|
|
|
{
|
|
|
|
// do override resolution
|
|
|
|
for (auto it = possibleMembers.begin(); it != possibleMembers.end();)
|
|
|
|
if (
|
2015-08-31 16:44:29 +00:00
|
|
|
it->type->category() == Type::Category::Function &&
|
2015-04-15 15:40:50 +00:00
|
|
|
!dynamic_cast<FunctionType const&>(*it->type).canTakeArguments(*_argumentTypes)
|
|
|
|
)
|
|
|
|
it = possibleMembers.erase(it);
|
|
|
|
else
|
|
|
|
++it;
|
|
|
|
}
|
|
|
|
if (possibleMembers.size() == 0)
|
2015-07-15 23:06:19 +00:00
|
|
|
{
|
|
|
|
auto storageType = ReferenceType::copyForLocationIfReference(
|
|
|
|
DataLocation::Storage,
|
2015-08-31 16:44:29 +00:00
|
|
|
m_expression->type()
|
2015-07-15 23:06:19 +00:00
|
|
|
);
|
2015-08-31 16:44:29 +00:00
|
|
|
if (!storageType->members().membersByName(*m_memberName).empty())
|
2015-07-15 23:06:19 +00:00
|
|
|
BOOST_THROW_EXCEPTION(createTypeError(
|
2015-09-09 15:43:01 +00:00
|
|
|
"Member \"" +
|
|
|
|
*m_memberName +
|
|
|
|
"\" is not available in " +
|
2015-07-15 23:06:19 +00:00
|
|
|
type.toString() +
|
|
|
|
" outside of storage."
|
|
|
|
));
|
2015-04-15 15:40:50 +00:00
|
|
|
BOOST_THROW_EXCEPTION(createTypeError(
|
2015-09-09 15:43:01 +00:00
|
|
|
"Member \"" +
|
|
|
|
*m_memberName +
|
|
|
|
"\" not found or not visible after argument-dependent lookup in " +
|
|
|
|
type.toString()
|
2015-04-15 15:40:50 +00:00
|
|
|
));
|
2015-07-15 23:06:19 +00:00
|
|
|
}
|
2015-04-15 15:40:50 +00:00
|
|
|
else if (possibleMembers.size() > 1)
|
|
|
|
BOOST_THROW_EXCEPTION(createTypeError(
|
2015-09-09 15:43:01 +00:00
|
|
|
"Member \"" +
|
|
|
|
*m_memberName +
|
|
|
|
"\" not unique after argument-dependent lookup in " +
|
|
|
|
type.toString()
|
2015-04-15 15:40:50 +00:00
|
|
|
));
|
|
|
|
|
|
|
|
m_referencedDeclaration = possibleMembers.front().declaration;
|
|
|
|
m_type = possibleMembers.front().type;
|
2015-08-31 16:44:29 +00:00
|
|
|
if (type.category() == Type::Category::Struct)
|
2015-02-23 00:31:05 +00:00
|
|
|
m_isLValue = true;
|
2015-08-31 16:44:29 +00:00
|
|
|
else if (type.category() == Type::Category::Array)
|
2015-02-23 00:31:05 +00:00
|
|
|
{
|
|
|
|
auto const& arrayType(dynamic_cast<ArrayType const&>(type));
|
2015-06-29 18:05:41 +00:00
|
|
|
m_isLValue = (
|
|
|
|
*m_memberName == "length" &&
|
|
|
|
arrayType.location() == DataLocation::Storage &&
|
|
|
|
arrayType.isDynamicallySized()
|
|
|
|
);
|
2015-02-23 00:31:05 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
m_isLValue = false;
|
2014-10-13 16:22:15 +00:00
|
|
|
}
|
|
|
|
|
2015-04-15 15:40:50 +00:00
|
|
|
void IndexAccess::checkTypeRequirements(TypePointers const*)
|
2014-10-13 16:22:15 +00:00
|
|
|
{
|
2015-04-15 15:40:50 +00:00
|
|
|
m_base->checkTypeRequirements(nullptr);
|
2015-08-31 16:44:29 +00:00
|
|
|
switch (m_base->type()->category())
|
2015-02-20 23:46:35 +00:00
|
|
|
{
|
|
|
|
case Type::Category::Array:
|
|
|
|
{
|
2015-08-31 16:44:29 +00:00
|
|
|
ArrayType const& type = dynamic_cast<ArrayType const&>(*m_base->type());
|
2015-02-21 17:25:08 +00:00
|
|
|
if (!m_index)
|
|
|
|
BOOST_THROW_EXCEPTION(createTypeError("Index expression cannot be omitted."));
|
2015-05-28 14:20:50 +00:00
|
|
|
if (type.isString())
|
|
|
|
BOOST_THROW_EXCEPTION(createTypeError("Index access for string is not possible."));
|
2015-02-20 23:46:35 +00:00
|
|
|
m_index->expectType(IntegerType(256));
|
2015-09-09 15:43:01 +00:00
|
|
|
|
|
|
|
m_type = type.baseType();
|
2015-09-10 08:17:17 +00:00
|
|
|
if (auto integerType = dynamic_cast<IntegerConstantType const*>(m_index->type().get()))
|
2015-09-09 15:43:01 +00:00
|
|
|
if (!type.isDynamicallySized() && type.length() <= integerType->literalValue(nullptr))
|
|
|
|
BOOST_THROW_EXCEPTION(createTypeError("Out of bounds access."));
|
|
|
|
|
2015-06-17 10:01:39 +00:00
|
|
|
m_isLValue = type.location() != DataLocation::CallData;
|
2015-02-20 23:46:35 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case Type::Category::Mapping:
|
|
|
|
{
|
2015-08-31 16:44:29 +00:00
|
|
|
MappingType const& type = dynamic_cast<MappingType const&>(*m_base->type());
|
2015-02-21 17:25:08 +00:00
|
|
|
if (!m_index)
|
|
|
|
BOOST_THROW_EXCEPTION(createTypeError("Index expression cannot be omitted."));
|
2015-08-31 16:44:29 +00:00
|
|
|
m_index->expectType(*type.keyType());
|
|
|
|
m_type = type.valueType();
|
2015-02-20 23:46:35 +00:00
|
|
|
m_isLValue = true;
|
|
|
|
break;
|
|
|
|
}
|
2015-02-21 17:25:08 +00:00
|
|
|
case Type::Category::TypeType:
|
|
|
|
{
|
2015-08-31 16:44:29 +00:00
|
|
|
TypeType const& type = dynamic_cast<TypeType const&>(*m_base->type());
|
2015-02-21 17:25:08 +00:00
|
|
|
if (!m_index)
|
2015-08-31 16:44:29 +00:00
|
|
|
m_type = make_shared<TypeType>(make_shared<ArrayType>(DataLocation::Memory, type.actualType()));
|
2015-02-21 17:25:08 +00:00
|
|
|
else
|
|
|
|
{
|
2015-04-15 15:40:50 +00:00
|
|
|
m_index->checkTypeRequirements(nullptr);
|
2015-08-31 16:44:29 +00:00
|
|
|
auto length = dynamic_cast<IntegerConstantType const*>(m_index->type().get());
|
2015-02-21 17:25:08 +00:00
|
|
|
if (!length)
|
|
|
|
BOOST_THROW_EXCEPTION(m_index->createTypeError("Integer constant expected."));
|
|
|
|
m_type = make_shared<TypeType>(make_shared<ArrayType>(
|
2015-09-09 15:43:01 +00:00
|
|
|
DataLocation::Memory,
|
|
|
|
type.actualType(),
|
2015-06-17 10:01:39 +00:00
|
|
|
length->literalValue(nullptr)
|
|
|
|
));
|
2015-02-21 17:25:08 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2015-02-20 23:46:35 +00:00
|
|
|
default:
|
|
|
|
BOOST_THROW_EXCEPTION(m_base->createTypeError(
|
2015-09-09 15:43:01 +00:00
|
|
|
"Indexed expression has to be a type, mapping or array (is " +
|
|
|
|
m_base->type()->toString() +
|
|
|
|
")"
|
|
|
|
));
|
2015-02-20 23:46:35 +00:00
|
|
|
}
|
2014-10-13 16:22:15 +00:00
|
|
|
}
|
|
|
|
|
2015-04-15 15:40:50 +00:00
|
|
|
void Identifier::checkTypeRequirements(TypePointers const* _argumentTypes)
|
2015-03-01 03:34:39 +00:00
|
|
|
{
|
|
|
|
if (!m_referencedDeclaration)
|
2015-04-15 15:40:50 +00:00
|
|
|
{
|
|
|
|
if (!_argumentTypes)
|
|
|
|
BOOST_THROW_EXCEPTION(createTypeError("Unable to determine overloaded type."));
|
|
|
|
overloadResolution(*_argumentTypes);
|
|
|
|
}
|
|
|
|
solAssert(!!m_referencedDeclaration, "Referenced declaration is null after overload resolution.");
|
2015-02-14 00:22:44 +00:00
|
|
|
m_isLValue = m_referencedDeclaration->isLValue();
|
2015-09-10 15:17:13 +00:00
|
|
|
m_type = m_referencedDeclaration->type(m_contractScope);
|
2015-01-22 00:02:38 +00:00
|
|
|
if (!m_type)
|
|
|
|
BOOST_THROW_EXCEPTION(createTypeError("Declaration referenced before type could be determined."));
|
2014-10-13 16:22:15 +00:00
|
|
|
}
|
|
|
|
|
2015-08-31 16:44:29 +00:00
|
|
|
Declaration const& Identifier::referencedDeclaration() const
|
2015-04-15 15:40:50 +00:00
|
|
|
{
|
|
|
|
solAssert(!!m_referencedDeclaration, "Identifier not resolved.");
|
|
|
|
return *m_referencedDeclaration;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Identifier::overloadResolution(TypePointers const& _argumentTypes)
|
2015-03-01 03:34:39 +00:00
|
|
|
{
|
|
|
|
solAssert(!m_referencedDeclaration, "Referenced declaration should be null before overload resolution.");
|
2015-04-15 15:40:50 +00:00
|
|
|
solAssert(!m_overloadedDeclarations.empty(), "No candidates for overload resolution found.");
|
2015-03-01 03:34:39 +00:00
|
|
|
|
2015-05-04 15:18:01 +00:00
|
|
|
vector<Declaration const*> possibles;
|
2015-04-15 15:40:50 +00:00
|
|
|
if (m_overloadedDeclarations.size() == 1)
|
|
|
|
m_referencedDeclaration = *m_overloadedDeclarations.begin();
|
2015-03-01 03:34:39 +00:00
|
|
|
|
2015-04-15 15:40:50 +00:00
|
|
|
for (Declaration const* declaration: m_overloadedDeclarations)
|
2015-03-01 03:34:39 +00:00
|
|
|
{
|
2015-08-31 16:44:29 +00:00
|
|
|
TypePointer const& function = declaration->type();
|
2015-04-15 15:40:50 +00:00
|
|
|
auto const* functionType = dynamic_cast<FunctionType const*>(function.get());
|
|
|
|
if (functionType && functionType->canTakeArguments(_argumentTypes))
|
|
|
|
possibles.push_back(declaration);
|
2015-03-01 03:34:39 +00:00
|
|
|
}
|
2015-04-15 15:40:50 +00:00
|
|
|
if (possibles.size() == 1)
|
|
|
|
m_referencedDeclaration = possibles.front();
|
|
|
|
else if (possibles.empty())
|
|
|
|
BOOST_THROW_EXCEPTION(createTypeError("No matching declaration found after argument-dependent lookup."));
|
2015-03-01 03:34:39 +00:00
|
|
|
else
|
2015-04-15 15:40:50 +00:00
|
|
|
BOOST_THROW_EXCEPTION(createTypeError("No unique declaration found after argument-dependent lookup."));
|
2015-03-01 03:34:39 +00:00
|
|
|
}
|
|
|
|
|
2015-04-15 15:40:50 +00:00
|
|
|
void ElementaryTypeNameExpression::checkTypeRequirements(TypePointers const*)
|
2014-10-13 16:22:15 +00:00
|
|
|
{
|
2015-01-07 20:35:35 +00:00
|
|
|
m_type = make_shared<TypeType>(Type::fromElementaryTypeName(m_typeToken));
|
2014-10-13 16:22:15 +00:00
|
|
|
}
|
|
|
|
|
2015-04-15 15:40:50 +00:00
|
|
|
void Literal::checkTypeRequirements(TypePointers const*)
|
2014-10-13 16:22:15 +00:00
|
|
|
{
|
|
|
|
m_type = Type::forLiteral(*this);
|
2014-11-04 12:24:35 +00:00
|
|
|
if (!m_type)
|
2014-12-19 10:31:17 +00:00
|
|
|
BOOST_THROW_EXCEPTION(createTypeError("Invalid literal value."));
|
2014-10-13 16:22:15 +00:00
|
|
|
}
|
|
|
|
|
2014-10-16 12:08:54 +00:00
|
|
|
}
|
|
|
|
}
|