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.
|
|
|
|
*/
|
|
|
|
|
2015-10-20 22:21:52 +00:00
|
|
|
#include <libsolidity/analysis/NameAndTypeResolver.h>
|
|
|
|
#include <libsolidity/ast/AST.h>
|
|
|
|
#include <libsolidity/analysis/TypeChecker.h>
|
|
|
|
#include <libsolidity/interface/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
|
|
|
|
2015-09-16 14:56:30 +00:00
|
|
|
NameAndTypeResolver::NameAndTypeResolver(
|
2015-10-02 12:41:40 +00:00
|
|
|
vector<Declaration const*> const& _globals,
|
|
|
|
ErrorList& _errors
|
|
|
|
) :
|
|
|
|
m_errors(_errors)
|
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);
|
|
|
|
}
|
|
|
|
|
2015-10-14 18:37:41 +00:00
|
|
|
bool NameAndTypeResolver::registerDeclarations(SourceUnit& _sourceUnit)
|
2014-12-03 16:45:12 +00:00
|
|
|
{
|
2014-12-05 14:35:05 +00:00
|
|
|
// The helper registers all declarations in m_scopes as a side-effect of its construction.
|
2015-10-14 18:37:41 +00:00
|
|
|
try
|
|
|
|
{
|
|
|
|
DeclarationRegistrationHelper registrar(m_scopes, _sourceUnit, m_errors);
|
|
|
|
}
|
2015-10-16 12:52:01 +00:00
|
|
|
catch (FatalError const& _e)
|
2015-10-14 18:37:41 +00:00
|
|
|
{
|
2015-10-16 12:52:01 +00:00
|
|
|
if (m_errors.empty())
|
|
|
|
throw; // Something is weird here, rather throw again.
|
2015-10-14 18:37:41 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
2014-12-03 16:45:12 +00:00
|
|
|
}
|
|
|
|
|
2015-10-14 18:37:41 +00:00
|
|
|
bool NameAndTypeResolver::resolveNamesAndTypes(ContractDefinition& _contract)
|
2014-10-13 13:07:21 +00:00
|
|
|
{
|
2015-10-14 18:37:41 +00:00
|
|
|
try
|
|
|
|
{
|
|
|
|
m_currentScope = &m_scopes[nullptr];
|
2015-01-15 19:04:06 +00:00
|
|
|
|
2015-10-26 16:31:35 +00:00
|
|
|
ReferencesResolver resolver(m_errors, *this, &_contract, nullptr);
|
2015-11-04 10:49:26 +00:00
|
|
|
bool success = true;
|
2015-10-14 18:37:41 +00:00
|
|
|
for (ASTPointer<InheritanceSpecifier> const& baseContract: _contract.baseContracts())
|
2015-11-04 10:49:26 +00:00
|
|
|
if (!resolver.resolve(*baseContract))
|
|
|
|
success = false;
|
2015-01-15 19:04:06 +00:00
|
|
|
|
2015-10-14 18:37:41 +00:00
|
|
|
m_currentScope = &m_scopes[&_contract];
|
2015-01-15 19:04:06 +00:00
|
|
|
|
2015-11-04 10:49:26 +00:00
|
|
|
if (success)
|
2015-10-26 16:31:35 +00:00
|
|
|
{
|
|
|
|
linearizeBaseContracts(_contract);
|
|
|
|
std::vector<ContractDefinition const*> properBases(
|
|
|
|
++_contract.annotation().linearizedBaseContracts.begin(),
|
|
|
|
_contract.annotation().linearizedBaseContracts.end()
|
|
|
|
);
|
|
|
|
|
|
|
|
for (ContractDefinition const* base: properBases)
|
|
|
|
importInheritedScope(*base);
|
|
|
|
}
|
2015-05-07 08:12:27 +00:00
|
|
|
|
2015-10-14 18:37:41 +00:00
|
|
|
for (ASTPointer<StructDefinition> const& structDef: _contract.definedStructs())
|
2015-11-04 10:49:26 +00:00
|
|
|
if (!resolver.resolve(*structDef))
|
|
|
|
success = false;
|
2015-10-14 18:37:41 +00:00
|
|
|
for (ASTPointer<EnumDefinition> const& enumDef: _contract.definedEnums())
|
2015-11-04 10:49:26 +00:00
|
|
|
if (!resolver.resolve(*enumDef))
|
|
|
|
success = false;
|
2015-10-14 18:37:41 +00:00
|
|
|
for (ASTPointer<VariableDeclaration> const& variable: _contract.stateVariables())
|
2015-11-04 10:49:26 +00:00
|
|
|
if (!resolver.resolve(*variable))
|
|
|
|
success = false;
|
2015-10-14 18:37:41 +00:00
|
|
|
for (ASTPointer<EventDefinition> const& event: _contract.events())
|
2015-11-04 10:49:26 +00:00
|
|
|
if (!resolver.resolve(*event))
|
|
|
|
success = false;
|
2015-10-14 18:37:41 +00:00
|
|
|
// these can contain code, only resolve parameters for now
|
|
|
|
for (ASTPointer<ModifierDefinition> const& modifier: _contract.functionModifiers())
|
|
|
|
{
|
|
|
|
m_currentScope = &m_scopes[modifier.get()];
|
2015-10-27 12:16:33 +00:00
|
|
|
ReferencesResolver resolver(m_errors, *this, &_contract, nullptr);
|
2015-11-04 10:49:26 +00:00
|
|
|
if (!resolver.resolve(*modifier))
|
|
|
|
success = false;
|
2015-10-14 18:37:41 +00:00
|
|
|
}
|
2015-11-06 16:22:46 +00:00
|
|
|
|
2015-10-14 18:37:41 +00:00
|
|
|
for (ASTPointer<FunctionDefinition> const& function: _contract.definedFunctions())
|
|
|
|
{
|
|
|
|
m_currentScope = &m_scopes[function.get()];
|
2015-11-06 16:22:46 +00:00
|
|
|
if (! ReferencesResolver(
|
2015-10-27 12:16:33 +00:00
|
|
|
m_errors,
|
2015-10-14 18:37:41 +00:00
|
|
|
*this,
|
|
|
|
&_contract,
|
|
|
|
function->returnParameterList().get()
|
2015-11-06 16:22:46 +00:00
|
|
|
).resolve(*function))
|
2015-11-04 10:49:26 +00:00
|
|
|
success = false;
|
2015-10-14 18:37:41 +00:00
|
|
|
}
|
2015-04-15 15:40:50 +00:00
|
|
|
|
2015-11-06 16:22:46 +00:00
|
|
|
if (!success)
|
|
|
|
return false;
|
|
|
|
|
2015-10-14 18:37:41 +00:00
|
|
|
m_currentScope = &m_scopes[&_contract];
|
2015-04-15 15:40:50 +00:00
|
|
|
|
2015-10-14 18:37:41 +00:00
|
|
|
// now resolve references inside the code
|
|
|
|
for (ASTPointer<ModifierDefinition> const& modifier: _contract.functionModifiers())
|
|
|
|
{
|
|
|
|
m_currentScope = &m_scopes[modifier.get()];
|
2015-10-26 16:31:35 +00:00
|
|
|
ReferencesResolver resolver(m_errors, *this, &_contract, nullptr, true);
|
2015-11-04 10:49:26 +00:00
|
|
|
if (!resolver.resolve(*modifier))
|
|
|
|
success = false;
|
2015-10-14 18:37:41 +00:00
|
|
|
}
|
2015-11-06 16:22:46 +00:00
|
|
|
|
2015-10-14 18:37:41 +00:00
|
|
|
for (ASTPointer<FunctionDefinition> const& function: _contract.definedFunctions())
|
|
|
|
{
|
|
|
|
m_currentScope = &m_scopes[function.get()];
|
2015-11-06 16:22:46 +00:00
|
|
|
if (! ReferencesResolver(
|
2015-10-27 12:16:33 +00:00
|
|
|
m_errors,
|
2015-10-14 18:37:41 +00:00
|
|
|
*this,
|
|
|
|
&_contract,
|
|
|
|
function->returnParameterList().get(),
|
|
|
|
true
|
2015-11-06 16:22:46 +00:00
|
|
|
).resolve(*function))
|
2015-11-04 10:49:26 +00:00
|
|
|
success = false;
|
2015-10-14 18:37:41 +00:00
|
|
|
}
|
2015-11-04 13:49:54 +00:00
|
|
|
if (!success)
|
|
|
|
return false;
|
2015-04-15 15:40:50 +00:00
|
|
|
}
|
2015-10-15 12:36:23 +00:00
|
|
|
catch (FatalError const& _e)
|
2015-04-15 15:40:50 +00:00
|
|
|
{
|
2015-11-04 13:49:54 +00:00
|
|
|
if (m_errors.empty())
|
|
|
|
throw; // Something is weird here, rather throw again.
|
2015-10-14 18:37:41 +00:00
|
|
|
return false;
|
2015-04-15 15:40:50 +00:00
|
|
|
}
|
2015-10-14 18:37:41 +00:00
|
|
|
return true;
|
2014-10-13 13:07:21 +00:00
|
|
|
}
|
|
|
|
|
2015-10-14 18:37:41 +00:00
|
|
|
bool NameAndTypeResolver::updateDeclaration(Declaration const& _declaration)
|
2014-12-03 16:45:12 +00:00
|
|
|
{
|
2015-10-14 18:37:41 +00:00
|
|
|
try
|
|
|
|
{
|
|
|
|
m_scopes[nullptr].registerDeclaration(_declaration, false, true);
|
|
|
|
solAssert(_declaration.scope() == nullptr, "Updated declaration outside global scope.");
|
|
|
|
}
|
2015-10-16 12:52:01 +00:00
|
|
|
catch (FatalError const& _error)
|
2015-10-14 18:37:41 +00:00
|
|
|
{
|
2015-10-16 12:52:01 +00:00
|
|
|
if (m_errors.empty())
|
|
|
|
throw; // Something is weird here, rather throw again.
|
2015-10-14 18:37:41 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
2014-12-03 16:45:12 +00:00
|
|
|
}
|
|
|
|
|
2015-05-07 08:12:27 +00:00
|
|
|
vector<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))
|
2015-05-07 08:12:27 +00:00
|
|
|
return vector<Declaration const*>({});
|
2014-10-30 00:20:32 +00:00
|
|
|
return iterator->second.resolveName(_name, false);
|
2014-10-13 13:07:21 +00:00
|
|
|
}
|
|
|
|
|
2015-10-06 12:43:09 +00:00
|
|
|
vector<Declaration const*> NameAndTypeResolver::nameFromCurrentScope(ASTString const& _name, bool _recursive) const
|
2014-10-13 13:07:21 +00:00
|
|
|
{
|
|
|
|
return m_currentScope->resolveName(_name, _recursive);
|
|
|
|
}
|
|
|
|
|
2015-10-06 12:43:09 +00:00
|
|
|
Declaration const* NameAndTypeResolver::pathFromCurrentScope(vector<ASTString> const& _path, bool _recursive) const
|
2015-10-06 10:35:10 +00:00
|
|
|
{
|
|
|
|
solAssert(!_path.empty(), "");
|
|
|
|
vector<Declaration const*> candidates = m_currentScope->resolveName(_path.front(), _recursive);
|
|
|
|
for (size_t i = 1; i < _path.size() && candidates.size() == 1; i++)
|
|
|
|
{
|
|
|
|
if (!m_scopes.count(candidates.front()))
|
|
|
|
return nullptr;
|
|
|
|
candidates = m_scopes.at(candidates.front()).resolveName(_path[i], false);
|
|
|
|
}
|
|
|
|
if (candidates.size() == 1)
|
|
|
|
return candidates.front();
|
|
|
|
else
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2015-05-08 14:07:25 +00:00
|
|
|
vector<Declaration const*> NameAndTypeResolver::cleanedDeclarations(
|
2015-05-07 16:12:58 +00:00
|
|
|
Identifier const& _identifier,
|
|
|
|
vector<Declaration const*> const& _declarations
|
|
|
|
)
|
2015-05-07 08:12:27 +00:00
|
|
|
{
|
2015-05-08 14:07:25 +00:00
|
|
|
solAssert(_declarations.size() > 1, "");
|
2015-05-07 14:52:06 +00:00
|
|
|
vector<Declaration const*> uniqueFunctions;
|
|
|
|
|
2015-05-07 16:12:58 +00:00
|
|
|
for (auto it = _declarations.begin(); it != _declarations.end(); ++it)
|
2015-05-07 08:12:27 +00:00
|
|
|
{
|
2015-05-07 14:52:06 +00:00
|
|
|
solAssert(*it, "");
|
2015-05-07 08:12:27 +00:00
|
|
|
// the declaration is functionDefinition while declarations > 1
|
2015-05-07 14:52:06 +00:00
|
|
|
FunctionDefinition const& functionDefinition = dynamic_cast<FunctionDefinition const&>(**it);
|
2015-05-07 08:12:27 +00:00
|
|
|
FunctionType functionType(functionDefinition);
|
2015-08-31 16:44:29 +00:00
|
|
|
for (auto parameter: functionType.parameterTypes() + functionType.returnParameterTypes())
|
2015-05-07 08:12:27 +00:00
|
|
|
if (!parameter)
|
2015-10-14 18:37:41 +00:00
|
|
|
reportFatalDeclarationError(_identifier.location(), "Function type can not be used in this context");
|
|
|
|
|
2015-05-07 14:52:06 +00:00
|
|
|
if (uniqueFunctions.end() == find_if(
|
|
|
|
uniqueFunctions.begin(),
|
|
|
|
uniqueFunctions.end(),
|
|
|
|
[&](Declaration const* d)
|
|
|
|
{
|
2015-05-08 14:07:25 +00:00
|
|
|
FunctionType newFunctionType(dynamic_cast<FunctionDefinition const&>(*d));
|
2015-05-07 14:52:06 +00:00
|
|
|
return functionType.hasEqualArgumentTypes(newFunctionType);
|
|
|
|
}
|
|
|
|
))
|
|
|
|
uniqueFunctions.push_back(*it);
|
2015-05-07 08:12:27 +00:00
|
|
|
}
|
2015-05-07 14:52:06 +00:00
|
|
|
return uniqueFunctions;
|
2015-05-07 08:12:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void NameAndTypeResolver::importInheritedScope(ContractDefinition const& _base)
|
2015-01-15 19:04:06 +00:00
|
|
|
{
|
|
|
|
auto iterator = m_scopes.find(&_base);
|
|
|
|
solAssert(iterator != end(m_scopes), "");
|
2015-08-31 16:44:29 +00:00
|
|
|
for (auto const& nameAndDeclaration: iterator->second.declarations())
|
2015-03-01 03:34:39 +00:00
|
|
|
for (auto const& declaration: nameAndDeclaration.second)
|
|
|
|
// Import if it was declared in the base, is not the constructor and is visible in derived classes
|
2015-08-31 16:44:29 +00:00
|
|
|
if (declaration->scope() == &_base && declaration->isVisibleInDerivedContracts())
|
2015-03-01 03:34:39 +00:00
|
|
|
m_currentScope->registerDeclaration(*declaration);
|
2015-01-15 19:04:06 +00:00
|
|
|
}
|
|
|
|
|
2015-10-14 18:37:41 +00:00
|
|
|
void NameAndTypeResolver::linearizeBaseContracts(ContractDefinition& _contract)
|
2015-01-15 19:04:06 +00:00
|
|
|
{
|
|
|
|
// 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-08-31 16:44:29 +00:00
|
|
|
for (ASTPointer<InheritanceSpecifier> const& baseSpecifier: _contract.baseContracts())
|
2015-01-15 19:04:06 +00:00
|
|
|
{
|
2015-09-16 14:56:30 +00:00
|
|
|
Identifier const& baseName = baseSpecifier->name();
|
|
|
|
auto base = dynamic_cast<ContractDefinition const*>(baseName.annotation().referencedDeclaration);
|
2015-01-15 19:04:06 +00:00
|
|
|
if (!base)
|
2015-10-14 18:37:41 +00:00
|
|
|
reportFatalTypeError(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-09-16 14:56:30 +00:00
|
|
|
vector<ContractDefinition const*> const& basesBases = base->annotation().linearizedBaseContracts;
|
2015-01-15 19:04:06 +00:00
|
|
|
if (basesBases.empty())
|
2015-10-14 18:37:41 +00:00
|
|
|
reportFatalTypeError(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())
|
2015-10-14 18:37:41 +00:00
|
|
|
reportFatalTypeError(_contract.createTypeError("Linearization of inheritance graph impossible"));
|
2015-09-16 14:56:30 +00:00
|
|
|
_contract.annotation().linearizedBaseContracts = result;
|
2015-10-07 13:57:17 +00:00
|
|
|
_contract.annotation().contractDependencies.insert(result.begin() + 1, result.end());
|
2015-01-15 19:04:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2015-10-14 18:37:41 +00:00
|
|
|
void NameAndTypeResolver::reportDeclarationError(
|
|
|
|
SourceLocation _sourceLoction,
|
|
|
|
string const& _description,
|
2015-10-15 14:08:02 +00:00
|
|
|
SourceLocation _secondarySourceLocation,
|
|
|
|
string const& _secondaryDescription
|
2015-10-14 18:37:41 +00:00
|
|
|
)
|
|
|
|
{
|
|
|
|
auto err = make_shared<Error>(Error::Type::DeclarationError); // todo remove Error?
|
|
|
|
*err <<
|
2015-10-15 14:08:02 +00:00
|
|
|
errinfo_sourceLocation(_sourceLoction) <<
|
|
|
|
errinfo_comment(_description) <<
|
|
|
|
errinfo_secondarySourceLocation(
|
|
|
|
SecondarySourceLocation().append(_secondaryDescription, _secondarySourceLocation)
|
|
|
|
);
|
|
|
|
|
|
|
|
m_errors.push_back(err);
|
|
|
|
}
|
|
|
|
|
2015-10-15 14:13:52 +00:00
|
|
|
void NameAndTypeResolver::reportDeclarationError(SourceLocation _sourceLocation, string const& _description)
|
2015-10-15 14:08:02 +00:00
|
|
|
{
|
|
|
|
auto err = make_shared<Error>(Error::Type::DeclarationError); // todo remove Error?
|
2015-10-15 14:13:52 +00:00
|
|
|
*err << errinfo_sourceLocation(_sourceLocation) << errinfo_comment(_description);
|
2015-10-14 18:37:41 +00:00
|
|
|
|
|
|
|
m_errors.push_back(err);
|
|
|
|
}
|
|
|
|
|
|
|
|
void NameAndTypeResolver::reportFatalDeclarationError(
|
2015-10-15 14:13:52 +00:00
|
|
|
SourceLocation _sourceLocation,
|
|
|
|
string const& _description
|
2015-10-14 18:37:41 +00:00
|
|
|
)
|
|
|
|
{
|
2015-10-15 14:13:52 +00:00
|
|
|
reportDeclarationError(_sourceLocation, _description);
|
2015-10-15 12:36:23 +00:00
|
|
|
BOOST_THROW_EXCEPTION(FatalError());
|
2015-10-14 18:37:41 +00:00
|
|
|
}
|
|
|
|
|
2015-10-15 14:51:01 +00:00
|
|
|
void NameAndTypeResolver::reportTypeError(Error const& _e)
|
2015-10-14 18:37:41 +00:00
|
|
|
{
|
|
|
|
m_errors.push_back(make_shared<Error>(_e));
|
|
|
|
}
|
|
|
|
|
2015-10-15 14:51:01 +00:00
|
|
|
void NameAndTypeResolver::reportFatalTypeError(Error const& _e)
|
2015-10-14 18:37:41 +00:00
|
|
|
{
|
|
|
|
reportTypeError(_e);
|
2015-10-15 12:36:23 +00:00
|
|
|
BOOST_THROW_EXCEPTION(FatalError());
|
2015-10-14 18:37:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
DeclarationRegistrationHelper::DeclarationRegistrationHelper(
|
|
|
|
map<ASTNode const*, DeclarationContainer>& _scopes,
|
|
|
|
ASTNode& _astRoot,
|
|
|
|
ErrorList& _errors
|
|
|
|
):
|
|
|
|
m_scopes(_scopes),
|
|
|
|
m_currentScope(nullptr),
|
|
|
|
m_errors(_errors)
|
2014-10-15 13:54:41 +00:00
|
|
|
{
|
|
|
|
_astRoot.accept(*this);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool DeclarationRegistrationHelper::visit(ContractDefinition& _contract)
|
|
|
|
{
|
|
|
|
registerDeclaration(_contract, true);
|
2015-10-05 15:19:23 +00:00
|
|
|
_contract.annotation().canonicalName = currentCanonicalName();
|
2014-10-15 13:54:41 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void DeclarationRegistrationHelper::endVisit(ContractDefinition&)
|
|
|
|
{
|
|
|
|
closeCurrentScope();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool DeclarationRegistrationHelper::visit(StructDefinition& _struct)
|
|
|
|
{
|
|
|
|
registerDeclaration(_struct, true);
|
2015-10-05 15:19:23 +00:00
|
|
|
_struct.annotation().canonicalName = currentCanonicalName();
|
2014-10-15 13:54:41 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void DeclarationRegistrationHelper::endVisit(StructDefinition&)
|
|
|
|
{
|
|
|
|
closeCurrentScope();
|
|
|
|
}
|
|
|
|
|
2015-02-11 20:40:47 +00:00
|
|
|
bool DeclarationRegistrationHelper::visit(EnumDefinition& _enum)
|
|
|
|
{
|
|
|
|
registerDeclaration(_enum, true);
|
2015-10-05 15:19:23 +00:00
|
|
|
_enum.annotation().canonicalName = currentCanonicalName();
|
2015-02-11 20:40:47 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void DeclarationRegistrationHelper::endVisit(EnumDefinition&)
|
|
|
|
{
|
|
|
|
closeCurrentScope();
|
|
|
|
}
|
|
|
|
|
2015-02-14 02:06:50 +00:00
|
|
|
bool DeclarationRegistrationHelper::visit(EnumValue& _value)
|
|
|
|
{
|
|
|
|
registerDeclaration(_value, false);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-10-15 13:54:41 +00:00
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
2015-02-17 15:21:38 +00:00
|
|
|
void DeclarationRegistrationHelper::endVisit(VariableDeclarationStatement& _variableDeclarationStatement)
|
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.
|
2015-02-17 15:21:38 +00:00
|
|
|
solAssert(m_currentFunction, "Variable declaration without function.");
|
2015-10-08 16:01:12 +00:00
|
|
|
for (ASTPointer<VariableDeclaration> const& var: _variableDeclarationStatement.declarations())
|
|
|
|
if (var)
|
|
|
|
m_currentFunction->addLocalVariable(*var);
|
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
|
|
|
}
|
|
|
|
|
2015-01-29 13:35:28 +00:00
|
|
|
bool DeclarationRegistrationHelper::visit(EventDefinition& _event)
|
|
|
|
{
|
2015-01-31 15:50:33 +00:00
|
|
|
registerDeclaration(_event, true);
|
2015-01-29 13:35:28 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-01-31 15:50:33 +00:00
|
|
|
void DeclarationRegistrationHelper::endVisit(EventDefinition&)
|
|
|
|
{
|
|
|
|
closeCurrentScope();
|
|
|
|
}
|
|
|
|
|
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.");
|
2015-08-31 16:44:29 +00:00
|
|
|
m_currentScope = m_scopes[m_currentScope].enclosingDeclaration();
|
2014-10-13 13:07:21 +00:00
|
|
|
}
|
|
|
|
|
2014-10-15 13:54:41 +00:00
|
|
|
void DeclarationRegistrationHelper::registerDeclaration(Declaration& _declaration, bool _opensScope)
|
|
|
|
{
|
2015-02-13 23:43:02 +00:00
|
|
|
if (!m_scopes[m_currentScope].registerDeclaration(_declaration, !_declaration.isVisibleInContract()))
|
2015-04-30 16:06:04 +00:00
|
|
|
{
|
2015-05-04 12:46:52 +00:00
|
|
|
SourceLocation firstDeclarationLocation;
|
|
|
|
SourceLocation secondDeclarationLocation;
|
2015-05-04 13:46:46 +00:00
|
|
|
Declaration const* conflictingDeclaration = m_scopes[m_currentScope].conflictingDeclaration(_declaration);
|
|
|
|
solAssert(conflictingDeclaration, "");
|
2015-05-04 12:46:52 +00:00
|
|
|
|
2015-08-31 16:44:29 +00:00
|
|
|
if (_declaration.location().start < conflictingDeclaration->location().start)
|
2015-05-04 12:46:52 +00:00
|
|
|
{
|
2015-08-31 16:44:29 +00:00
|
|
|
firstDeclarationLocation = _declaration.location();
|
|
|
|
secondDeclarationLocation = conflictingDeclaration->location();
|
2015-05-04 12:46:52 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2015-08-31 16:44:29 +00:00
|
|
|
firstDeclarationLocation = conflictingDeclaration->location();
|
|
|
|
secondDeclarationLocation = _declaration.location();
|
2015-05-04 12:46:52 +00:00
|
|
|
}
|
2015-05-04 13:46:46 +00:00
|
|
|
|
2015-10-14 18:37:41 +00:00
|
|
|
declarationError(
|
|
|
|
secondDeclarationLocation,
|
|
|
|
"Identifier already declared.",
|
|
|
|
firstDeclarationLocation,
|
|
|
|
"The previous declaration is here:"
|
2015-05-04 15:30:28 +00:00
|
|
|
);
|
2015-04-30 16:06:04 +00:00
|
|
|
}
|
|
|
|
|
2014-12-01 14:22:45 +00:00
|
|
|
_declaration.setScope(m_currentScope);
|
2014-10-15 13:54:41 +00:00
|
|
|
if (_opensScope)
|
|
|
|
enterNewSubScope(_declaration);
|
|
|
|
}
|
|
|
|
|
2015-10-05 15:19:23 +00:00
|
|
|
string DeclarationRegistrationHelper::currentCanonicalName() const
|
|
|
|
{
|
|
|
|
string ret;
|
|
|
|
for (
|
|
|
|
Declaration const* scope = m_currentScope;
|
|
|
|
scope != nullptr;
|
|
|
|
scope = m_scopes[scope].enclosingDeclaration()
|
|
|
|
)
|
|
|
|
{
|
|
|
|
if (!ret.empty())
|
|
|
|
ret = "." + ret;
|
|
|
|
ret = scope->name() + ret;
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2015-10-14 18:37:41 +00:00
|
|
|
void DeclarationRegistrationHelper::declarationError(
|
2015-10-15 14:13:52 +00:00
|
|
|
SourceLocation _sourceLocation,
|
2015-10-14 18:37:41 +00:00
|
|
|
string const& _description,
|
2015-10-15 14:08:02 +00:00
|
|
|
SourceLocation _secondarySourceLocation,
|
|
|
|
string const& _secondaryDescription
|
2015-10-14 18:37:41 +00:00
|
|
|
)
|
|
|
|
{
|
|
|
|
auto err = make_shared<Error>(Error::Type::DeclarationError);
|
|
|
|
*err <<
|
2015-10-15 14:13:52 +00:00
|
|
|
errinfo_sourceLocation(_sourceLocation) <<
|
2015-10-15 14:08:02 +00:00
|
|
|
errinfo_comment(_description) <<
|
|
|
|
errinfo_secondarySourceLocation(
|
|
|
|
SecondarySourceLocation().append(_secondaryDescription, _secondarySourceLocation)
|
|
|
|
);
|
|
|
|
|
|
|
|
m_errors.push_back(err);
|
|
|
|
}
|
|
|
|
|
2015-10-15 14:13:52 +00:00
|
|
|
void DeclarationRegistrationHelper::declarationError(SourceLocation _sourceLocation, string const& _description)
|
2015-10-15 14:08:02 +00:00
|
|
|
{
|
|
|
|
auto err = make_shared<Error>(Error::Type::DeclarationError);
|
2015-10-15 14:13:52 +00:00
|
|
|
*err << errinfo_sourceLocation(_sourceLocation) << errinfo_comment(_description);
|
2015-10-14 18:37:41 +00:00
|
|
|
|
|
|
|
m_errors.push_back(err);
|
|
|
|
}
|
|
|
|
|
|
|
|
void DeclarationRegistrationHelper::fatalDeclarationError(
|
2015-10-15 14:13:52 +00:00
|
|
|
SourceLocation _sourceLocation,
|
2015-10-14 18:37:41 +00:00
|
|
|
string const& _description
|
|
|
|
)
|
|
|
|
{
|
2015-10-15 14:13:52 +00:00
|
|
|
declarationError(_sourceLocation, _description);
|
2015-10-15 12:36:23 +00:00
|
|
|
BOOST_THROW_EXCEPTION(FatalError());
|
2015-10-14 18:37:41 +00:00
|
|
|
}
|
|
|
|
|
2014-10-16 12:08:54 +00:00
|
|
|
}
|
|
|
|
}
|