2014-10-13 15:13:48 +00:00
|
|
|
/*
|
2014-10-16 12:08:54 +00:00
|
|
|
This file is part of cpp-ethereum.
|
2014-10-13 15:13:48 +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-13 15:13:48 +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-13 15:13:48 +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-13 15:13:48 +00:00
|
|
|
*/
|
|
|
|
/**
|
|
|
|
* @author Christian <c@ethdev.com>
|
|
|
|
* @date 2014
|
|
|
|
* Parser part that determines the declarations corresponding to names and the types of expressions.
|
|
|
|
*/
|
|
|
|
|
2014-10-13 13:07:21 +00:00
|
|
|
#include <libsolidity/NameAndTypeResolver.h>
|
|
|
|
#include <libsolidity/AST.h>
|
2014-10-15 12:45:51 +00:00
|
|
|
#include <libsolidity/Exceptions.h>
|
2014-10-13 13:07: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-13 13:07:21 +00:00
|
|
|
|
2014-12-06 01:32:51 +00:00
|
|
|
NameAndTypeResolver::NameAndTypeResolver(std::vector<Declaration const*> const& _globals)
|
2014-11-20 17:33:23 +00:00
|
|
|
{
|
2014-12-06 01:32:51 +00:00
|
|
|
for (Declaration const* declaration: _globals)
|
2014-11-20 17:33:23 +00:00
|
|
|
m_scopes[nullptr].registerDeclaration(*declaration);
|
|
|
|
}
|
|
|
|
|
2014-12-03 16:45:12 +00:00
|
|
|
void NameAndTypeResolver::registerDeclarations(SourceUnit& _sourceUnit)
|
|
|
|
{
|
2014-12-05 14:35:05 +00:00
|
|
|
// The helper registers all declarations in m_scopes as a side-effect of its construction.
|
2014-12-03 16:45:12 +00:00
|
|
|
DeclarationRegistrationHelper registrar(m_scopes, _sourceUnit);
|
|
|
|
}
|
|
|
|
|
2014-10-13 13:07:21 +00:00
|
|
|
void NameAndTypeResolver::resolveNamesAndTypes(ContractDefinition& _contract)
|
|
|
|
{
|
2015-01-15 19:04:06 +00:00
|
|
|
m_currentScope = &m_scopes[nullptr];
|
|
|
|
|
2015-01-19 20:05:47 +00:00
|
|
|
for (ASTPointer<InheritanceSpecifier> const& baseContract: _contract.getBaseContracts())
|
2015-01-19 18:18:34 +00:00
|
|
|
ReferencesResolver resolver(*baseContract, *this, &_contract, nullptr);
|
2015-01-15 19:04:06 +00:00
|
|
|
|
2014-10-15 13:54:41 +00:00
|
|
|
m_currentScope = &m_scopes[&_contract];
|
2015-01-15 19:04:06 +00:00
|
|
|
|
|
|
|
linearizeBaseContracts(_contract);
|
|
|
|
for (ContractDefinition const* base: _contract.getLinearizedBaseContracts())
|
|
|
|
importInheritedScope(*base);
|
|
|
|
|
2014-11-13 00:12:57 +00:00
|
|
|
for (ASTPointer<StructDefinition> const& structDef: _contract.getDefinedStructs())
|
2015-01-19 18:18:34 +00:00
|
|
|
ReferencesResolver resolver(*structDef, *this, &_contract, nullptr);
|
2014-10-20 12:00:37 +00:00
|
|
|
for (ASTPointer<VariableDeclaration> const& variable: _contract.getStateVariables())
|
2015-01-19 18:18:34 +00:00
|
|
|
ReferencesResolver resolver(*variable, *this, &_contract, nullptr);
|
2015-01-22 00:02:38 +00:00
|
|
|
for (ASTPointer<ModifierDefinition> const& modifier: _contract.getFunctionModifiers())
|
|
|
|
{
|
|
|
|
m_currentScope = &m_scopes[modifier.get()];
|
|
|
|
ReferencesResolver resolver(*modifier, *this, &_contract, nullptr);
|
|
|
|
}
|
2014-10-20 12:00:37 +00:00
|
|
|
for (ASTPointer<FunctionDefinition> const& function: _contract.getDefinedFunctions())
|
2014-10-16 12:08:54 +00:00
|
|
|
{
|
2014-10-15 13:54:41 +00:00
|
|
|
m_currentScope = &m_scopes[function.get()];
|
2015-01-19 18:18:34 +00:00
|
|
|
ReferencesResolver referencesResolver(*function, *this, &_contract,
|
2014-10-15 13:54:41 +00:00
|
|
|
function->getReturnParameterList().get());
|
|
|
|
}
|
2014-10-13 13:07:21 +00:00
|
|
|
}
|
|
|
|
|
2014-12-16 22:45:24 +00:00
|
|
|
void NameAndTypeResolver::checkTypeRequirements(ContractDefinition& _contract)
|
|
|
|
{
|
|
|
|
for (ASTPointer<StructDefinition> const& structDef: _contract.getDefinedStructs())
|
|
|
|
structDef->checkValidityOfMembers();
|
|
|
|
_contract.checkTypeRequirements();
|
|
|
|
}
|
|
|
|
|
2014-12-06 01:32:51 +00:00
|
|
|
void NameAndTypeResolver::updateDeclaration(Declaration const& _declaration)
|
2014-12-03 16:45:12 +00:00
|
|
|
{
|
|
|
|
m_scopes[nullptr].registerDeclaration(_declaration, true);
|
2014-12-17 15:23:18 +00:00
|
|
|
solAssert(_declaration.getScope() == nullptr, "Updated declaration outside global scope.");
|
2014-12-03 16:45:12 +00:00
|
|
|
}
|
|
|
|
|
2014-12-06 01:32:51 +00:00
|
|
|
Declaration const* NameAndTypeResolver::resolveName(ASTString const& _name, Declaration const* _scope) const
|
2014-10-13 13:07:21 +00:00
|
|
|
{
|
2014-10-30 00:20:32 +00:00
|
|
|
auto iterator = m_scopes.find(_scope);
|
|
|
|
if (iterator == end(m_scopes))
|
|
|
|
return nullptr;
|
|
|
|
return iterator->second.resolveName(_name, false);
|
2014-10-13 13:07:21 +00:00
|
|
|
}
|
|
|
|
|
2014-12-06 01:32:51 +00:00
|
|
|
Declaration const* NameAndTypeResolver::getNameFromCurrentScope(ASTString const& _name, bool _recursive)
|
2014-10-13 13:07:21 +00:00
|
|
|
{
|
|
|
|
return m_currentScope->resolveName(_name, _recursive);
|
|
|
|
}
|
|
|
|
|
2015-01-15 19:04:06 +00:00
|
|
|
void NameAndTypeResolver::importInheritedScope(ContractDefinition const& _base)
|
|
|
|
{
|
|
|
|
auto iterator = m_scopes.find(&_base);
|
|
|
|
solAssert(iterator != end(m_scopes), "");
|
|
|
|
for (auto const& nameAndDeclaration: iterator->second.getDeclarations())
|
|
|
|
{
|
|
|
|
Declaration const* declaration = nameAndDeclaration.second;
|
2015-01-16 16:50:10 +00:00
|
|
|
// Import if it was declared in the base and is not the constructor
|
|
|
|
if (declaration->getScope() == &_base && declaration->getName() != _base.getName())
|
2015-01-15 19:04:06 +00:00
|
|
|
m_currentScope->registerDeclaration(*declaration);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void NameAndTypeResolver::linearizeBaseContracts(ContractDefinition& _contract) const
|
|
|
|
{
|
|
|
|
// order in the lists is from derived to base
|
|
|
|
// list of lists to linearize, the last element is the list of direct bases
|
2015-01-26 09:20:46 +00:00
|
|
|
list<list<ContractDefinition const*>> input(1, {});
|
2015-01-19 20:05:47 +00:00
|
|
|
for (ASTPointer<InheritanceSpecifier> const& baseSpecifier: _contract.getBaseContracts())
|
2015-01-15 19:04:06 +00:00
|
|
|
{
|
2015-01-19 20:05:47 +00:00
|
|
|
ASTPointer<Identifier> baseName = baseSpecifier->getName();
|
2015-01-15 19:04:06 +00:00
|
|
|
ContractDefinition const* base = dynamic_cast<ContractDefinition const*>(
|
2015-01-19 20:05:47 +00:00
|
|
|
baseName->getReferencedDeclaration());
|
2015-01-15 19:04:06 +00:00
|
|
|
if (!base)
|
2015-01-19 20:05:47 +00:00
|
|
|
BOOST_THROW_EXCEPTION(baseName->createTypeError("Contract expected."));
|
2015-01-26 09:20:46 +00:00
|
|
|
// "push_front" has the effect that bases mentioned later can overwrite members of bases
|
|
|
|
// mentioned earlier
|
|
|
|
input.back().push_front(base);
|
2015-01-15 19:04:06 +00:00
|
|
|
vector<ContractDefinition const*> const& basesBases = base->getLinearizedBaseContracts();
|
|
|
|
if (basesBases.empty())
|
2015-01-19 20:05:47 +00:00
|
|
|
BOOST_THROW_EXCEPTION(baseName->createTypeError("Definition of base has to precede definition of derived contract"));
|
2015-01-15 19:04:06 +00:00
|
|
|
input.push_front(list<ContractDefinition const*>(basesBases.begin(), basesBases.end()));
|
|
|
|
}
|
2015-01-26 09:20:46 +00:00
|
|
|
input.back().push_front(&_contract);
|
2015-01-15 19:04:06 +00:00
|
|
|
vector<ContractDefinition const*> result = cThreeMerge(input);
|
|
|
|
if (result.empty())
|
|
|
|
BOOST_THROW_EXCEPTION(_contract.createTypeError("Linearization of inheritance graph impossible"));
|
|
|
|
_contract.setLinearizedBaseContracts(result);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class _T>
|
|
|
|
vector<_T const*> NameAndTypeResolver::cThreeMerge(list<list<_T const*>>& _toMerge)
|
|
|
|
{
|
|
|
|
// returns true iff _candidate appears only as last element of the lists
|
|
|
|
auto appearsOnlyAtHead = [&](_T const* _candidate) -> bool
|
|
|
|
{
|
|
|
|
for (list<_T const*> const& bases: _toMerge)
|
|
|
|
{
|
|
|
|
solAssert(!bases.empty(), "");
|
|
|
|
if (find(++bases.begin(), bases.end(), _candidate) != bases.end())
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
};
|
|
|
|
// returns the next candidate to append to the linearized list or nullptr on failure
|
|
|
|
auto nextCandidate = [&]() -> _T const*
|
|
|
|
{
|
|
|
|
for (list<_T const*> const& bases: _toMerge)
|
|
|
|
{
|
|
|
|
solAssert(!bases.empty(), "");
|
|
|
|
if (appearsOnlyAtHead(bases.front()))
|
|
|
|
return bases.front();
|
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
};
|
|
|
|
// removes the given contract from all lists
|
|
|
|
auto removeCandidate = [&](_T const* _candidate)
|
|
|
|
{
|
|
|
|
for (auto it = _toMerge.begin(); it != _toMerge.end();)
|
|
|
|
{
|
|
|
|
it->remove(_candidate);
|
|
|
|
if (it->empty())
|
|
|
|
it = _toMerge.erase(it);
|
|
|
|
else
|
|
|
|
++it;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
_toMerge.remove_if([](list<_T const*> const& _bases) { return _bases.empty(); });
|
|
|
|
vector<_T const*> result;
|
|
|
|
while (!_toMerge.empty())
|
|
|
|
{
|
|
|
|
_T const* candidate = nextCandidate();
|
|
|
|
if (!candidate)
|
|
|
|
return vector<_T const*>();
|
|
|
|
result.push_back(candidate);
|
|
|
|
removeCandidate(candidate);
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2014-12-01 14:22:45 +00:00
|
|
|
DeclarationRegistrationHelper::DeclarationRegistrationHelper(map<ASTNode const*, DeclarationContainer>& _scopes,
|
2014-10-22 22:24:07 +00:00
|
|
|
ASTNode& _astRoot):
|
2014-12-01 14:22:45 +00:00
|
|
|
m_scopes(_scopes), m_currentScope(nullptr)
|
2014-10-15 13:54:41 +00:00
|
|
|
{
|
|
|
|
_astRoot.accept(*this);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool DeclarationRegistrationHelper::visit(ContractDefinition& _contract)
|
|
|
|
{
|
|
|
|
registerDeclaration(_contract, true);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void DeclarationRegistrationHelper::endVisit(ContractDefinition&)
|
|
|
|
{
|
|
|
|
closeCurrentScope();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool DeclarationRegistrationHelper::visit(StructDefinition& _struct)
|
|
|
|
{
|
|
|
|
registerDeclaration(_struct, true);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void DeclarationRegistrationHelper::endVisit(StructDefinition&)
|
|
|
|
{
|
|
|
|
closeCurrentScope();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool DeclarationRegistrationHelper::visit(FunctionDefinition& _function)
|
|
|
|
{
|
|
|
|
registerDeclaration(_function, true);
|
2014-10-30 00:20:32 +00:00
|
|
|
m_currentFunction = &_function;
|
2014-10-15 13:54:41 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void DeclarationRegistrationHelper::endVisit(FunctionDefinition&)
|
|
|
|
{
|
2014-10-30 00:20:32 +00:00
|
|
|
m_currentFunction = nullptr;
|
2014-10-15 13:54:41 +00:00
|
|
|
closeCurrentScope();
|
|
|
|
}
|
|
|
|
|
2015-01-22 00:02:38 +00:00
|
|
|
bool DeclarationRegistrationHelper::visit(ModifierDefinition& _modifier)
|
|
|
|
{
|
|
|
|
registerDeclaration(_modifier, true);
|
|
|
|
m_currentFunction = &_modifier;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void DeclarationRegistrationHelper::endVisit(ModifierDefinition&)
|
|
|
|
{
|
|
|
|
m_currentFunction = nullptr;
|
|
|
|
closeCurrentScope();
|
|
|
|
}
|
|
|
|
|
2014-10-30 00:20:32 +00:00
|
|
|
void DeclarationRegistrationHelper::endVisit(VariableDefinition& _variableDefinition)
|
2014-10-15 13:54:41 +00:00
|
|
|
{
|
2014-10-30 00:20:32 +00:00
|
|
|
// Register the local variables with the function
|
|
|
|
// This does not fit here perfectly, but it saves us another AST visit.
|
2014-12-17 15:23:18 +00:00
|
|
|
solAssert(m_currentFunction, "Variable definition without function.");
|
2014-10-30 00:20:32 +00:00
|
|
|
m_currentFunction->addLocalVariable(_variableDefinition.getDeclaration());
|
2014-10-15 13:54:41 +00:00
|
|
|
}
|
|
|
|
|
2014-10-30 00:20:32 +00:00
|
|
|
bool DeclarationRegistrationHelper::visit(VariableDeclaration& _declaration)
|
2014-10-13 13:07:21 +00:00
|
|
|
{
|
2014-10-30 00:20:32 +00:00
|
|
|
registerDeclaration(_declaration, false);
|
|
|
|
return true;
|
2014-10-15 13:54:41 +00:00
|
|
|
}
|
|
|
|
|
2014-12-06 01:32:51 +00:00
|
|
|
void DeclarationRegistrationHelper::enterNewSubScope(Declaration const& _declaration)
|
2014-10-15 13:54:41 +00:00
|
|
|
{
|
2014-12-01 14:22:45 +00:00
|
|
|
map<ASTNode const*, DeclarationContainer>::iterator iter;
|
2014-10-13 13:07:21 +00:00
|
|
|
bool newlyAdded;
|
2014-12-01 14:22:45 +00:00
|
|
|
tie(iter, newlyAdded) = m_scopes.emplace(&_declaration, DeclarationContainer(m_currentScope, &m_scopes[m_currentScope]));
|
2014-12-17 15:23:18 +00:00
|
|
|
solAssert(newlyAdded, "Unable to add new scope.");
|
2014-12-01 14:22:45 +00:00
|
|
|
m_currentScope = &_declaration;
|
2014-10-13 13:07:21 +00:00
|
|
|
}
|
|
|
|
|
2014-10-15 13:54:41 +00:00
|
|
|
void DeclarationRegistrationHelper::closeCurrentScope()
|
2014-10-13 13:07:21 +00:00
|
|
|
{
|
2014-12-17 15:23:18 +00:00
|
|
|
solAssert(m_currentScope, "Closed non-existing scope.");
|
2014-12-01 14:22:45 +00:00
|
|
|
m_currentScope = m_scopes[m_currentScope].getEnclosingDeclaration();
|
2014-10-13 13:07:21 +00:00
|
|
|
}
|
|
|
|
|
2014-10-15 13:54:41 +00:00
|
|
|
void DeclarationRegistrationHelper::registerDeclaration(Declaration& _declaration, bool _opensScope)
|
|
|
|
{
|
2014-12-01 14:22:45 +00:00
|
|
|
if (!m_scopes[m_currentScope].registerDeclaration(_declaration))
|
2014-10-23 17:22:30 +00:00
|
|
|
BOOST_THROW_EXCEPTION(DeclarationError() << errinfo_sourceLocation(_declaration.getLocation())
|
|
|
|
<< errinfo_comment("Identifier already declared."));
|
2014-10-16 15:56:10 +00:00
|
|
|
//@todo the exception should also contain the location of the first declaration
|
2014-12-01 14:22:45 +00:00
|
|
|
_declaration.setScope(m_currentScope);
|
2014-10-15 13:54:41 +00:00
|
|
|
if (_opensScope)
|
|
|
|
enterNewSubScope(_declaration);
|
|
|
|
}
|
|
|
|
|
|
|
|
ReferencesResolver::ReferencesResolver(ASTNode& _root, NameAndTypeResolver& _resolver,
|
2015-01-19 18:18:34 +00:00
|
|
|
ContractDefinition const* _currentContract,
|
|
|
|
ParameterList const* _returnParameters, bool _allowLazyTypes):
|
|
|
|
m_resolver(_resolver), m_currentContract(_currentContract),
|
|
|
|
m_returnParameters(_returnParameters), m_allowLazyTypes(_allowLazyTypes)
|
2014-10-15 13:54:41 +00:00
|
|
|
{
|
|
|
|
_root.accept(*this);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ReferencesResolver::endVisit(VariableDeclaration& _variable)
|
|
|
|
{
|
|
|
|
// endVisit because the internal type needs resolving if it is a user defined type
|
|
|
|
// or mapping
|
2014-10-24 14:43:11 +00:00
|
|
|
if (_variable.getTypeName())
|
2014-11-20 17:33:23 +00:00
|
|
|
{
|
2014-10-15 13:54:41 +00:00
|
|
|
_variable.setType(_variable.getTypeName()->toType());
|
2014-11-20 17:33:23 +00:00
|
|
|
if (!_variable.getType())
|
|
|
|
BOOST_THROW_EXCEPTION(_variable.getTypeName()->createTypeError("Invalid type name"));
|
|
|
|
}
|
2014-11-13 00:12:57 +00:00
|
|
|
else if (!m_allowLazyTypes)
|
|
|
|
BOOST_THROW_EXCEPTION(_variable.createTypeError("Explicit type needed."));
|
2014-10-15 13:54:41 +00:00
|
|
|
// otherwise we have a "var"-declaration whose type is resolved by the first assignment
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ReferencesResolver::visit(Return& _return)
|
|
|
|
{
|
2015-01-23 01:35:27 +00:00
|
|
|
_return.setFunctionReturnParameters(m_returnParameters);
|
2014-10-15 13:54:41 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-12-06 01:32:51 +00:00
|
|
|
bool ReferencesResolver::visit(Mapping&)
|
2014-10-15 13:54:41 +00:00
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ReferencesResolver::visit(UserDefinedTypeName& _typeName)
|
|
|
|
{
|
2014-12-06 01:32:51 +00:00
|
|
|
Declaration const* declaration = m_resolver.getNameFromCurrentScope(_typeName.getName());
|
2014-10-24 14:43:11 +00:00
|
|
|
if (!declaration)
|
2014-10-23 17:22:30 +00:00
|
|
|
BOOST_THROW_EXCEPTION(DeclarationError() << errinfo_sourceLocation(_typeName.getLocation())
|
|
|
|
<< errinfo_comment("Undeclared identifier."));
|
2014-11-20 17:33:23 +00:00
|
|
|
_typeName.setReferencedDeclaration(*declaration);
|
2014-10-15 13:54:41 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ReferencesResolver::visit(Identifier& _identifier)
|
|
|
|
{
|
2014-12-06 01:32:51 +00:00
|
|
|
Declaration const* declaration = m_resolver.getNameFromCurrentScope(_identifier.getName());
|
2014-10-24 14:43:11 +00:00
|
|
|
if (!declaration)
|
2014-10-23 17:22:30 +00:00
|
|
|
BOOST_THROW_EXCEPTION(DeclarationError() << errinfo_sourceLocation(_identifier.getLocation())
|
|
|
|
<< errinfo_comment("Undeclared identifier."));
|
2015-01-19 18:18:34 +00:00
|
|
|
_identifier.setReferencedDeclaration(*declaration, m_currentContract);
|
2014-10-15 13:54:41 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-10-16 12:08:54 +00:00
|
|
|
}
|
|
|
|
}
|