2017-02-17 15:05:22 +00:00
|
|
|
/*
|
|
|
|
This file is part of solidity.
|
|
|
|
|
|
|
|
solidity 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.
|
|
|
|
|
|
|
|
solidity 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.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with solidity. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
2020-07-17 14:54:12 +00:00
|
|
|
// SPDX-License-Identifier: GPL-3.0
|
2017-02-17 15:05:22 +00:00
|
|
|
/**
|
|
|
|
* Module responsible for registering identifiers inside their scopes.
|
|
|
|
*/
|
|
|
|
|
2020-11-25 18:10:31 +00:00
|
|
|
#include <libyul/ScopeFiller.h>
|
2017-02-17 15:05:22 +00:00
|
|
|
|
2020-10-29 14:00:27 +00:00
|
|
|
#include <libyul/AST.h>
|
2020-11-25 18:10:31 +00:00
|
|
|
#include <libyul/Scope.h>
|
2018-11-23 10:18:57 +00:00
|
|
|
#include <libyul/AsmAnalysisInfo.h>
|
2019-11-26 21:52:09 +00:00
|
|
|
#include <libyul/Exceptions.h>
|
2017-02-17 15:05:22 +00:00
|
|
|
|
2018-11-14 13:59:30 +00:00
|
|
|
#include <liblangutil/ErrorReporter.h>
|
2017-02-17 15:05:22 +00:00
|
|
|
|
2020-01-06 10:52:23 +00:00
|
|
|
#include <libsolutil/CommonData.h>
|
2017-07-13 09:33:06 +00:00
|
|
|
|
2017-02-17 15:05:22 +00:00
|
|
|
#include <memory>
|
|
|
|
#include <functional>
|
|
|
|
|
|
|
|
using namespace std;
|
2019-12-11 16:31:36 +00:00
|
|
|
using namespace solidity;
|
|
|
|
using namespace solidity::yul;
|
|
|
|
using namespace solidity::util;
|
|
|
|
using namespace solidity::langutil;
|
2017-02-17 15:05:22 +00:00
|
|
|
|
2017-05-11 13:26:35 +00:00
|
|
|
ScopeFiller::ScopeFiller(AsmAnalysisInfo& _info, ErrorReporter& _errorReporter):
|
|
|
|
m_info(_info), m_errorReporter(_errorReporter)
|
2017-02-17 15:05:22 +00:00
|
|
|
{
|
|
|
|
m_currentScope = &scope(nullptr);
|
|
|
|
}
|
|
|
|
|
2017-12-08 13:01:22 +00:00
|
|
|
bool ScopeFiller::operator()(ExpressionStatement const& _expr)
|
|
|
|
{
|
2019-11-19 15:42:49 +00:00
|
|
|
return std::visit(*this, _expr.expression);
|
2017-12-08 13:01:22 +00:00
|
|
|
}
|
|
|
|
|
2018-11-21 11:42:34 +00:00
|
|
|
bool ScopeFiller::operator()(VariableDeclaration const& _varDecl)
|
2017-02-17 15:05:22 +00:00
|
|
|
{
|
2017-05-05 17:35:40 +00:00
|
|
|
for (auto const& variable: _varDecl.variables)
|
2021-04-27 14:53:04 +00:00
|
|
|
if (!registerVariable(variable, _varDecl.debugData->location, *m_currentScope))
|
2017-05-05 17:35:40 +00:00
|
|
|
return false;
|
|
|
|
return true;
|
2017-02-17 15:05:22 +00:00
|
|
|
}
|
|
|
|
|
2018-11-21 11:42:34 +00:00
|
|
|
bool ScopeFiller::operator()(FunctionDefinition const& _funDef)
|
2017-02-17 15:05:22 +00:00
|
|
|
{
|
2017-05-26 19:46:02 +00:00
|
|
|
auto virtualBlock = m_info.virtualBlocks[&_funDef] = make_shared<Block>();
|
|
|
|
Scope& varScope = scope(virtualBlock.get());
|
|
|
|
varScope.superScope = m_currentScope;
|
|
|
|
m_currentScope = &varScope;
|
|
|
|
varScope.functionScope = true;
|
2019-04-02 21:19:23 +00:00
|
|
|
|
|
|
|
bool success = true;
|
2017-12-01 13:10:49 +00:00
|
|
|
for (auto const& var: _funDef.parameters + _funDef.returnVariables)
|
2021-04-27 14:53:04 +00:00
|
|
|
if (!registerVariable(var, _funDef.debugData->location, varScope))
|
2017-02-17 15:05:22 +00:00
|
|
|
success = false;
|
|
|
|
|
|
|
|
if (!(*this)(_funDef.body))
|
|
|
|
success = false;
|
|
|
|
|
2019-11-26 21:52:09 +00:00
|
|
|
yulAssert(m_currentScope == &varScope, "");
|
2017-05-26 19:46:02 +00:00
|
|
|
m_currentScope = m_currentScope->superScope;
|
|
|
|
|
2017-02-17 15:05:22 +00:00
|
|
|
return success;
|
|
|
|
}
|
|
|
|
|
2017-11-21 12:36:41 +00:00
|
|
|
bool ScopeFiller::operator()(If const& _if)
|
|
|
|
{
|
|
|
|
return (*this)(_if.body);
|
|
|
|
}
|
|
|
|
|
2017-05-23 19:51:33 +00:00
|
|
|
bool ScopeFiller::operator()(Switch const& _switch)
|
|
|
|
{
|
|
|
|
bool success = true;
|
|
|
|
for (auto const& _case: _switch.cases)
|
|
|
|
if (!(*this)(_case.body))
|
|
|
|
success = false;
|
|
|
|
return success;
|
|
|
|
}
|
|
|
|
|
2017-06-01 12:16:12 +00:00
|
|
|
bool ScopeFiller::operator()(ForLoop const& _forLoop)
|
2017-04-28 13:33:52 +00:00
|
|
|
{
|
2017-06-01 12:16:12 +00:00
|
|
|
Scope* originalScope = m_currentScope;
|
|
|
|
|
|
|
|
bool success = true;
|
|
|
|
if (!(*this)(_forLoop.pre))
|
|
|
|
success = false;
|
|
|
|
m_currentScope = &scope(&_forLoop.pre);
|
2019-11-19 15:42:49 +00:00
|
|
|
if (!std::visit(*this, *_forLoop.condition))
|
2017-06-01 12:16:12 +00:00
|
|
|
success = false;
|
|
|
|
if (!(*this)(_forLoop.body))
|
|
|
|
success = false;
|
|
|
|
if (!(*this)(_forLoop.post))
|
|
|
|
success = false;
|
|
|
|
|
|
|
|
m_currentScope = originalScope;
|
|
|
|
|
|
|
|
return success;
|
2017-04-28 13:33:52 +00:00
|
|
|
}
|
|
|
|
|
2017-02-17 15:05:22 +00:00
|
|
|
bool ScopeFiller::operator()(Block const& _block)
|
|
|
|
{
|
|
|
|
bool success = true;
|
|
|
|
scope(&_block).superScope = m_currentScope;
|
|
|
|
m_currentScope = &scope(&_block);
|
|
|
|
|
2019-02-18 13:51:05 +00:00
|
|
|
// First visit all functions to make them create
|
|
|
|
// an entry in the scope according to their visibility.
|
2017-02-17 15:05:22 +00:00
|
|
|
for (auto const& s: _block.statements)
|
2019-11-19 15:42:49 +00:00
|
|
|
if (holds_alternative<FunctionDefinition>(s))
|
|
|
|
if (!registerFunction(std::get<FunctionDefinition>(s)))
|
2019-02-18 13:51:05 +00:00
|
|
|
success = false;
|
|
|
|
for (auto const& s: _block.statements)
|
2019-11-19 15:42:49 +00:00
|
|
|
if (!std::visit(*this, s))
|
2019-04-02 21:19:23 +00:00
|
|
|
success = false;
|
2017-02-17 15:05:22 +00:00
|
|
|
|
|
|
|
m_currentScope = m_currentScope->superScope;
|
|
|
|
return success;
|
|
|
|
}
|
|
|
|
|
2017-04-26 22:58:34 +00:00
|
|
|
bool ScopeFiller::registerVariable(TypedName const& _name, SourceLocation const& _location, Scope& _scope)
|
2017-02-17 15:05:22 +00:00
|
|
|
{
|
2017-04-26 22:58:34 +00:00
|
|
|
if (!_scope.registerVariable(_name.name, _name.type))
|
2017-02-17 15:05:22 +00:00
|
|
|
{
|
|
|
|
//@TODO secondary location
|
2017-05-11 13:26:35 +00:00
|
|
|
m_errorReporter.declarationError(
|
2020-05-05 22:38:28 +00:00
|
|
|
1395_error,
|
2017-05-11 13:26:35 +00:00
|
|
|
_location,
|
2018-10-29 14:12:02 +00:00
|
|
|
"Variable name " + _name.name.str() + " already taken in this scope."
|
2017-05-11 13:26:35 +00:00
|
|
|
);
|
2017-02-17 15:05:22 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-04-02 21:19:23 +00:00
|
|
|
bool ScopeFiller::registerFunction(FunctionDefinition const& _funDef)
|
|
|
|
{
|
2020-02-11 15:54:09 +00:00
|
|
|
vector<Scope::YulType> parameters;
|
|
|
|
for (auto const& parameter: _funDef.parameters)
|
|
|
|
parameters.emplace_back(parameter.type);
|
2019-04-02 21:19:23 +00:00
|
|
|
vector<Scope::YulType> returns;
|
2020-02-11 15:54:09 +00:00
|
|
|
for (auto const& returnVariable: _funDef.returnVariables)
|
|
|
|
returns.emplace_back(returnVariable.type);
|
|
|
|
if (!m_currentScope->registerFunction(_funDef.name, std::move(parameters), std::move(returns)))
|
2019-04-02 21:19:23 +00:00
|
|
|
{
|
|
|
|
//@TODO secondary location
|
|
|
|
m_errorReporter.declarationError(
|
2020-05-05 22:38:28 +00:00
|
|
|
6052_error,
|
2021-04-27 14:53:04 +00:00
|
|
|
_funDef.debugData->location,
|
2019-04-02 21:19:23 +00:00
|
|
|
"Function name " + _funDef.name.str() + " already taken in this scope."
|
|
|
|
);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-02-17 15:05:22 +00:00
|
|
|
Scope& ScopeFiller::scope(Block const* _block)
|
|
|
|
{
|
2017-05-26 19:46:02 +00:00
|
|
|
auto& scope = m_info.scopes[_block];
|
2017-02-17 15:05:22 +00:00
|
|
|
if (!scope)
|
|
|
|
scope = make_shared<Scope>();
|
|
|
|
return *scope;
|
|
|
|
}
|