solidity/NameAndTypeResolver.cpp

244 lines
8.1 KiB
C++
Raw Normal View History

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
void NameAndTypeResolver::resolveNamesAndTypes(ContractDefinition& _contract)
{
reset();
DeclarationRegistrationHelper registrar(m_scopes, _contract);
m_currentScope = &m_scopes[&_contract];
2014-11-13 00:12:57 +00:00
for (ASTPointer<StructDefinition> const& structDef: _contract.getDefinedStructs())
ReferencesResolver resolver(*structDef, *this, nullptr);
for (ASTPointer<StructDefinition> const& structDef: _contract.getDefinedStructs())
checkForRecursion(*structDef);
for (ASTPointer<VariableDeclaration> const& variable: _contract.getStateVariables())
ReferencesResolver resolver(*variable, *this, nullptr);
for (ASTPointer<FunctionDefinition> const& function: _contract.getDefinedFunctions())
2014-10-16 12:08:54 +00:00
{
m_currentScope = &m_scopes[function.get()];
ReferencesResolver referencesResolver(*function, *this,
function->getReturnParameterList().get());
}
2014-10-16 21:49:45 +00:00
// First, the parameter types of all functions need to be resolved before we can check
// the types, since it is possible to call functions that are only defined later
// in the source.
for (ASTPointer<FunctionDefinition> const& function: _contract.getDefinedFunctions())
2014-10-16 12:08:54 +00:00
{
m_currentScope = &m_scopes[function.get()];
2014-11-10 16:31:09 +00:00
function->checkTypeRequirements();
}
m_currentScope = &m_scopes[nullptr];
2014-10-13 13:07:21 +00:00
}
Declaration* NameAndTypeResolver::resolveName(ASTString const& _name, Declaration const* _scope) const
2014-10-13 13:07:21 +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-10-13 16:22:15 +00:00
Declaration* NameAndTypeResolver::getNameFromCurrentScope(ASTString const& _name, bool _recursive)
2014-10-13 13:07:21 +00:00
{
return m_currentScope->resolveName(_name, _recursive);
}
2014-11-13 00:12:57 +00:00
void NameAndTypeResolver::checkForRecursion(StructDefinition const& _struct)
{
set<StructDefinition const*> definitionsSeen;
vector<StructDefinition const*> queue = {&_struct};
while (!queue.empty())
{
StructDefinition const* def = queue.back();
queue.pop_back();
if (definitionsSeen.count(def))
BOOST_THROW_EXCEPTION(ParserError() << errinfo_sourceLocation(def->getLocation())
<< errinfo_comment("Recursive struct definition."));
definitionsSeen.insert(def);
for (ASTPointer<VariableDeclaration> const& member: def->getMembers())
if (member->getType()->getCategory() == Type::Category::STRUCT)
queue.push_back(dynamic_cast<UserDefinedTypeName&>(*member->getTypeName()).getReferencedStruct());
}
}
void NameAndTypeResolver::reset()
{
m_scopes.clear();
m_currentScope = nullptr;
}
DeclarationRegistrationHelper::DeclarationRegistrationHelper(map<ASTNode const*, Scope>& _scopes,
2014-10-22 22:24:07 +00:00
ASTNode& _astRoot):
m_scopes(_scopes), m_currentScope(&m_scopes[nullptr])
{
_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);
m_currentFunction = &_function;
return true;
}
void DeclarationRegistrationHelper::endVisit(FunctionDefinition&)
{
m_currentFunction = nullptr;
closeCurrentScope();
}
void DeclarationRegistrationHelper::endVisit(VariableDefinition& _variableDefinition)
{
// Register the local variables with the function
// This does not fit here perfectly, but it saves us another AST visit.
2014-11-05 13:20:56 +00:00
if (asserts(m_currentFunction))
BOOST_THROW_EXCEPTION(InternalCompilerError() << errinfo_comment("Variable definition without function."));
m_currentFunction->addLocalVariable(_variableDefinition.getDeclaration());
}
bool DeclarationRegistrationHelper::visit(VariableDeclaration& _declaration)
2014-10-13 13:07:21 +00:00
{
registerDeclaration(_declaration, false);
return true;
}
void DeclarationRegistrationHelper::enterNewSubScope(ASTNode& _node)
{
map<ASTNode const*, Scope>::iterator iter;
2014-10-13 13:07:21 +00:00
bool newlyAdded;
2014-10-24 17:06:30 +00:00
tie(iter, newlyAdded) = m_scopes.emplace(&_node, Scope(m_currentScope));
2014-11-05 13:20:56 +00:00
if (asserts(newlyAdded))
BOOST_THROW_EXCEPTION(InternalCompilerError() << errinfo_comment("Unable to add new scope."));
2014-10-13 13:07:21 +00:00
m_currentScope = &iter->second;
}
void DeclarationRegistrationHelper::closeCurrentScope()
2014-10-13 13:07:21 +00:00
{
2014-11-05 13:20:56 +00:00
if (asserts(m_currentScope))
BOOST_THROW_EXCEPTION(InternalCompilerError() << errinfo_comment("Closed non-existing scope."));
2014-10-23 15:06:12 +00:00
m_currentScope = m_currentScope->getEnclosingScope();
2014-10-13 13:07:21 +00:00
}
void DeclarationRegistrationHelper::registerDeclaration(Declaration& _declaration, bool _opensScope)
{
2014-11-05 13:20:56 +00:00
if (asserts(m_currentScope))
BOOST_THROW_EXCEPTION(InternalCompilerError() << errinfo_comment("Declaration registered without scope."));
if (!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."));
//@todo the exception should also contain the location of the first declaration
if (_opensScope)
enterNewSubScope(_declaration);
}
ReferencesResolver::ReferencesResolver(ASTNode& _root, NameAndTypeResolver& _resolver,
2014-11-13 00:12:57 +00:00
ParameterList* _returnParameters, bool _allowLazyTypes):
m_resolver(_resolver), m_returnParameters(_returnParameters), m_allowLazyTypes(_allowLazyTypes)
{
_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())
_variable.setType(_variable.getTypeName()->toType());
2014-11-13 00:12:57 +00:00
else if (!m_allowLazyTypes)
BOOST_THROW_EXCEPTION(_variable.createTypeError("Explicit type needed."));
// otherwise we have a "var"-declaration whose type is resolved by the first assignment
}
bool ReferencesResolver::visit(Return& _return)
{
2014-11-05 13:20:56 +00:00
if (asserts(m_returnParameters))
BOOST_THROW_EXCEPTION(InternalCompilerError() << errinfo_comment("Return parameters not set."));
_return.setFunctionReturnParameters(*m_returnParameters);
return true;
}
2014-11-10 16:31:09 +00:00
bool ReferencesResolver::visit(Mapping& _mapping)
{
return true;
}
bool ReferencesResolver::visit(UserDefinedTypeName& _typeName)
{
Declaration* 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."));
StructDefinition* referencedStruct = dynamic_cast<StructDefinition*>(declaration);
//@todo later, contracts are also valid types
2014-10-24 14:43:11 +00:00
if (!referencedStruct)
BOOST_THROW_EXCEPTION(_typeName.createTypeError("Identifier does not name a type name."));
_typeName.setReferencedStruct(*referencedStruct);
return false;
}
bool ReferencesResolver::visit(Identifier& _identifier)
{
Declaration* 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."));
_identifier.setReferencedDeclaration(*declaration);
return false;
}
2014-10-16 12:08:54 +00:00
}
}