2015-09-16 14:56:30 +00:00
|
|
|
/*
|
2019-02-13 15:56:46 +00:00
|
|
|
This file is part of solidity.
|
2015-09-16 14:56:30 +00:00
|
|
|
|
2019-02-13 15:56:46 +00:00
|
|
|
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.
|
2015-09-16 14:56:30 +00:00
|
|
|
|
2019-02-13 15:56:46 +00:00
|
|
|
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.
|
2015-09-16 14:56:30 +00:00
|
|
|
|
2019-02-13 15:56:46 +00:00
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with solidity. If not, see <http://www.gnu.org/licenses/>.
|
2015-09-16 14:56:30 +00:00
|
|
|
*/
|
2020-07-17 14:54:12 +00:00
|
|
|
// SPDX-License-Identifier: GPL-3.0
|
2015-09-16 14:56:30 +00:00
|
|
|
/**
|
|
|
|
* @author Christian <c@ethdev.com>
|
|
|
|
* @date 2015
|
|
|
|
* Component that resolves type names to types and annotates the AST accordingly.
|
|
|
|
*/
|
|
|
|
|
2015-10-20 22:21:52 +00:00
|
|
|
#include <libsolidity/analysis/ReferencesResolver.h>
|
|
|
|
#include <libsolidity/analysis/NameAndTypeResolver.h>
|
2018-12-17 11:30:08 +00:00
|
|
|
#include <libsolidity/ast/AST.h>
|
|
|
|
|
2018-11-23 10:18:57 +00:00
|
|
|
#include <libyul/AsmAnalysis.h>
|
|
|
|
#include <libyul/AsmAnalysisInfo.h>
|
2020-10-29 14:00:27 +00:00
|
|
|
#include <libyul/AST.h>
|
2018-12-06 23:56:16 +00:00
|
|
|
#include <libyul/backends/evm/EVMDialect.h>
|
2018-12-17 11:30:08 +00:00
|
|
|
|
2018-11-14 13:59:30 +00:00
|
|
|
#include <liblangutil/ErrorReporter.h>
|
2018-11-23 10:31:45 +00:00
|
|
|
#include <liblangutil/Exceptions.h>
|
2015-09-16 14:56:30 +00:00
|
|
|
|
2020-01-06 10:52:23 +00:00
|
|
|
#include <libsolutil/StringUtils.h>
|
2020-07-01 17:25:14 +00:00
|
|
|
#include <libsolutil/CommonData.h>
|
2018-08-07 13:58:34 +00:00
|
|
|
|
2017-04-21 17:13:46 +00:00
|
|
|
#include <boost/algorithm/string.hpp>
|
2020-06-29 12:30:09 +00:00
|
|
|
#include <boost/algorithm/string/split.hpp>
|
2017-04-21 17:13:46 +00:00
|
|
|
|
2015-09-16 14:56:30 +00:00
|
|
|
using namespace std;
|
2020-07-14 12:19:37 +00:00
|
|
|
using namespace solidity;
|
2019-12-11 16:31:36 +00:00
|
|
|
using namespace solidity::langutil;
|
2020-07-02 12:38:36 +00:00
|
|
|
using namespace solidity::frontend;
|
2015-09-16 14:56:30 +00:00
|
|
|
|
|
|
|
|
2015-11-23 22:57:17 +00:00
|
|
|
bool ReferencesResolver::resolve(ASTNode const& _root)
|
2015-11-06 20:07:42 +00:00
|
|
|
{
|
2020-05-12 21:23:01 +00:00
|
|
|
auto errorWatcher = m_errorReporter.errorWatcher();
|
2017-02-16 11:36:43 +00:00
|
|
|
_root.accept(*this);
|
2020-05-12 21:23:01 +00:00
|
|
|
return errorWatcher.ok();
|
2015-11-06 20:07:42 +00:00
|
|
|
}
|
|
|
|
|
2018-02-09 15:53:52 +00:00
|
|
|
bool ReferencesResolver::visit(Block const& _block)
|
|
|
|
{
|
|
|
|
if (!m_resolveInsideCode)
|
|
|
|
return false;
|
2018-06-15 10:30:28 +00:00
|
|
|
m_resolver.setScope(&_block);
|
2018-02-09 15:53:52 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ReferencesResolver::endVisit(Block const& _block)
|
|
|
|
{
|
|
|
|
if (!m_resolveInsideCode)
|
|
|
|
return;
|
|
|
|
|
2018-06-15 10:30:28 +00:00
|
|
|
m_resolver.setScope(_block.scope());
|
2018-02-09 15:53:52 +00:00
|
|
|
}
|
|
|
|
|
2020-03-08 16:27:43 +00:00
|
|
|
bool ReferencesResolver::visit(TryCatchClause const& _tryCatchClause)
|
|
|
|
{
|
|
|
|
if (!m_resolveInsideCode)
|
|
|
|
return false;
|
|
|
|
m_resolver.setScope(&_tryCatchClause);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ReferencesResolver::endVisit(TryCatchClause const& _tryCatchClause)
|
|
|
|
{
|
|
|
|
if (!m_resolveInsideCode)
|
|
|
|
return;
|
|
|
|
|
|
|
|
m_resolver.setScope(_tryCatchClause.scope());
|
|
|
|
}
|
|
|
|
|
2018-02-09 15:53:52 +00:00
|
|
|
bool ReferencesResolver::visit(ForStatement const& _for)
|
|
|
|
{
|
|
|
|
if (!m_resolveInsideCode)
|
|
|
|
return false;
|
2018-06-15 10:30:28 +00:00
|
|
|
m_resolver.setScope(&_for);
|
2018-02-09 15:53:52 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ReferencesResolver::endVisit(ForStatement const& _for)
|
|
|
|
{
|
|
|
|
if (!m_resolveInsideCode)
|
|
|
|
return;
|
2018-06-15 10:30:28 +00:00
|
|
|
m_resolver.setScope(_for.scope());
|
2018-02-09 15:53:52 +00:00
|
|
|
}
|
|
|
|
|
2018-02-15 10:30:32 +00:00
|
|
|
void ReferencesResolver::endVisit(VariableDeclarationStatement const& _varDeclStatement)
|
|
|
|
{
|
|
|
|
if (!m_resolveInsideCode)
|
|
|
|
return;
|
2018-06-15 10:30:28 +00:00
|
|
|
for (auto const& var: _varDeclStatement.declarations())
|
|
|
|
if (var)
|
|
|
|
m_resolver.activateVariable(var->name());
|
2018-02-15 10:30:32 +00:00
|
|
|
}
|
|
|
|
|
2020-06-29 12:30:09 +00:00
|
|
|
bool ReferencesResolver::visit(VariableDeclaration const& _varDecl)
|
|
|
|
{
|
|
|
|
if (_varDecl.documentation())
|
|
|
|
resolveInheritDoc(*_varDecl.documentation(), _varDecl.annotation());
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-09-16 14:56:30 +00:00
|
|
|
bool ReferencesResolver::visit(Identifier const& _identifier)
|
|
|
|
{
|
|
|
|
auto declarations = m_resolver.nameFromCurrentScope(_identifier.name());
|
|
|
|
if (declarations.empty())
|
2017-10-29 08:08:40 +00:00
|
|
|
{
|
2018-02-09 16:58:50 +00:00
|
|
|
string suggestions = m_resolver.similarNameSuggestions(_identifier.name());
|
2018-06-15 10:30:28 +00:00
|
|
|
string errorMessage = "Undeclared identifier.";
|
|
|
|
if (!suggestions.empty())
|
|
|
|
{
|
|
|
|
if ("\"" + _identifier.name() + "\"" == suggestions)
|
|
|
|
errorMessage += " " + std::move(suggestions) + " is not (or not yet) visible at this point.";
|
|
|
|
else
|
|
|
|
errorMessage += " Did you mean " + std::move(suggestions) + "?";
|
|
|
|
}
|
2020-05-26 12:37:31 +00:00
|
|
|
m_errorReporter.declarationError(7576_error, _identifier.location(), errorMessage);
|
2017-10-29 08:08:40 +00:00
|
|
|
}
|
2015-09-16 14:56:30 +00:00
|
|
|
else if (declarations.size() == 1)
|
|
|
|
_identifier.annotation().referencedDeclaration = declarations.front();
|
|
|
|
else
|
2020-04-07 17:31:48 +00:00
|
|
|
_identifier.annotation().candidateDeclarations = declarations;
|
2015-09-16 14:56:30 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-01-27 17:27:59 +00:00
|
|
|
bool ReferencesResolver::visit(FunctionDefinition const& _functionDefinition)
|
|
|
|
{
|
|
|
|
m_returnParameters.push_back(_functionDefinition.returnParameterList().get());
|
2020-06-29 12:30:09 +00:00
|
|
|
|
|
|
|
if (_functionDefinition.documentation())
|
|
|
|
resolveInheritDoc(*_functionDefinition.documentation(), _functionDefinition.annotation());
|
|
|
|
|
2017-01-27 17:27:59 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ReferencesResolver::endVisit(FunctionDefinition const&)
|
|
|
|
{
|
|
|
|
solAssert(!m_returnParameters.empty(), "");
|
|
|
|
m_returnParameters.pop_back();
|
|
|
|
}
|
|
|
|
|
2020-06-29 12:30:09 +00:00
|
|
|
bool ReferencesResolver::visit(ModifierDefinition const& _modifierDefinition)
|
2017-01-27 17:27:59 +00:00
|
|
|
{
|
|
|
|
m_returnParameters.push_back(nullptr);
|
2020-06-29 12:30:09 +00:00
|
|
|
|
|
|
|
if (_modifierDefinition.documentation())
|
|
|
|
resolveInheritDoc(*_modifierDefinition.documentation(), _modifierDefinition.annotation());
|
|
|
|
|
2017-01-27 17:27:59 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ReferencesResolver::endVisit(ModifierDefinition const&)
|
|
|
|
{
|
|
|
|
solAssert(!m_returnParameters.empty(), "");
|
|
|
|
m_returnParameters.pop_back();
|
|
|
|
}
|
|
|
|
|
2020-08-31 15:37:03 +00:00
|
|
|
void ReferencesResolver::endVisit(IdentifierPath const& _path)
|
2015-11-26 10:37:29 +00:00
|
|
|
{
|
2020-08-31 15:37:03 +00:00
|
|
|
Declaration const* declaration = m_resolver.pathFromCurrentScope(_path.path());
|
2015-11-26 10:37:29 +00:00
|
|
|
if (!declaration)
|
2017-10-26 14:02:36 +00:00
|
|
|
{
|
2020-08-31 15:37:03 +00:00
|
|
|
m_errorReporter.fatalDeclarationError(7920_error, _path.location(), "Identifier not found or not unique.");
|
2017-10-26 14:02:36 +00:00
|
|
|
return;
|
|
|
|
}
|
2015-11-26 10:37:29 +00:00
|
|
|
|
2020-08-31 15:37:03 +00:00
|
|
|
_path.annotation().referencedDeclaration = declaration;
|
|
|
|
}
|
|
|
|
|
2016-03-01 21:56:39 +00:00
|
|
|
bool ReferencesResolver::visit(InlineAssembly const& _inlineAssembly)
|
|
|
|
{
|
2017-06-09 13:35:27 +00:00
|
|
|
m_resolver.warnVariablesNamedLikeInstructions();
|
|
|
|
|
2019-12-19 12:13:48 +00:00
|
|
|
m_yulAnnotation = &_inlineAssembly.annotation();
|
|
|
|
(*this)(_inlineAssembly.operations());
|
|
|
|
m_yulAnnotation = nullptr;
|
|
|
|
|
2016-03-01 21:56:39 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-11-26 10:37:29 +00:00
|
|
|
bool ReferencesResolver::visit(Return const& _return)
|
|
|
|
{
|
2017-01-27 17:27:59 +00:00
|
|
|
solAssert(!m_returnParameters.empty(), "");
|
|
|
|
_return.annotation().functionReturnParameters = m_returnParameters.back();
|
2015-11-26 10:37:29 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-12-19 12:13:48 +00:00
|
|
|
void ReferencesResolver::operator()(yul::FunctionDefinition const& _function)
|
|
|
|
{
|
2020-07-02 12:38:36 +00:00
|
|
|
validateYulIdentifierName(_function.name, _function.location);
|
|
|
|
for (yul::TypedName const& varName: _function.parameters + _function.returnVariables)
|
|
|
|
validateYulIdentifierName(varName.name, varName.location);
|
|
|
|
|
2019-12-19 12:13:48 +00:00
|
|
|
bool wasInsideFunction = m_yulInsideFunction;
|
|
|
|
m_yulInsideFunction = true;
|
|
|
|
this->operator()(_function.body);
|
|
|
|
m_yulInsideFunction = wasInsideFunction;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ReferencesResolver::operator()(yul::Identifier const& _identifier)
|
|
|
|
{
|
2020-11-05 13:39:39 +00:00
|
|
|
static set<string> suffixes{"slot", "offset", "length"};
|
|
|
|
string suffix;
|
|
|
|
for (string const& s: suffixes)
|
|
|
|
if (boost::algorithm::ends_with(_identifier.name.str(), "." + s))
|
|
|
|
suffix = s;
|
2019-12-19 12:13:48 +00:00
|
|
|
|
2020-07-01 17:25:14 +00:00
|
|
|
// Could also use `pathFromCurrentScope`, split by '.'
|
2019-12-19 12:13:48 +00:00
|
|
|
auto declarations = m_resolver.nameFromCurrentScope(_identifier.name.str());
|
2020-11-05 13:39:39 +00:00
|
|
|
if (!suffix.empty())
|
2019-12-19 12:13:48 +00:00
|
|
|
{
|
|
|
|
// special mode to access storage variables
|
|
|
|
if (!declarations.empty())
|
|
|
|
// the special identifier exists itself, we should not allow that.
|
|
|
|
return;
|
2020-11-05 13:39:39 +00:00
|
|
|
string realName = _identifier.name.str().substr(0, _identifier.name.str().size() - suffix.size() - 1);
|
2020-07-27 23:37:51 +00:00
|
|
|
solAssert(!realName.empty(), "Empty name.");
|
2019-12-19 12:13:48 +00:00
|
|
|
declarations = m_resolver.nameFromCurrentScope(realName);
|
2020-07-01 17:25:14 +00:00
|
|
|
if (!declarations.empty())
|
|
|
|
// To support proper path resolution, we have to use pathFromCurrentScope.
|
|
|
|
solAssert(!util::contains(realName, '.'), "");
|
2019-12-19 12:13:48 +00:00
|
|
|
}
|
|
|
|
if (declarations.size() > 1)
|
|
|
|
{
|
2020-05-21 15:38:47 +00:00
|
|
|
m_errorReporter.declarationError(
|
2020-05-26 12:37:31 +00:00
|
|
|
4718_error,
|
2020-05-21 15:38:47 +00:00
|
|
|
_identifier.location,
|
|
|
|
"Multiple matching identifiers. Resolving overloaded identifiers is not supported."
|
|
|
|
);
|
2019-12-19 12:13:48 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
else if (declarations.size() == 0)
|
2020-07-01 17:25:14 +00:00
|
|
|
{
|
|
|
|
if (
|
|
|
|
boost::algorithm::ends_with(_identifier.name.str(), "_slot") ||
|
|
|
|
boost::algorithm::ends_with(_identifier.name.str(), "_offset")
|
|
|
|
)
|
|
|
|
m_errorReporter.declarationError(
|
|
|
|
9467_error,
|
|
|
|
_identifier.location,
|
2020-11-05 13:39:39 +00:00
|
|
|
"Identifier not found. Use \".slot\" and \".offset\" to access storage variables."
|
2020-07-01 17:25:14 +00:00
|
|
|
);
|
2019-12-19 12:13:48 +00:00
|
|
|
return;
|
2020-07-01 17:25:14 +00:00
|
|
|
}
|
2019-12-19 12:13:48 +00:00
|
|
|
if (auto var = dynamic_cast<VariableDeclaration const*>(declarations.front()))
|
|
|
|
if (var->isLocalVariable() && m_yulInsideFunction)
|
|
|
|
{
|
2020-05-21 15:38:47 +00:00
|
|
|
m_errorReporter.declarationError(
|
2020-05-26 12:37:31 +00:00
|
|
|
6578_error,
|
2020-05-21 15:38:47 +00:00
|
|
|
_identifier.location,
|
|
|
|
"Cannot access local Solidity variables from inside an inline assembly function."
|
|
|
|
);
|
2019-12-19 12:13:48 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-11-05 13:39:39 +00:00
|
|
|
m_yulAnnotation->externalReferences[&_identifier].suffix = move(suffix);
|
2019-12-19 12:13:48 +00:00
|
|
|
m_yulAnnotation->externalReferences[&_identifier].declaration = declarations.front();
|
|
|
|
}
|
|
|
|
|
|
|
|
void ReferencesResolver::operator()(yul::VariableDeclaration const& _varDecl)
|
|
|
|
{
|
|
|
|
for (auto const& identifier: _varDecl.variables)
|
|
|
|
{
|
2020-07-02 12:38:36 +00:00
|
|
|
validateYulIdentifierName(identifier.name, identifier.location);
|
|
|
|
|
2019-12-19 12:13:48 +00:00
|
|
|
|
2020-07-01 17:25:14 +00:00
|
|
|
if (
|
|
|
|
auto declarations = m_resolver.nameFromCurrentScope(identifier.name.str());
|
2019-12-19 12:13:48 +00:00
|
|
|
!declarations.empty()
|
|
|
|
)
|
|
|
|
{
|
|
|
|
SecondarySourceLocation ssl;
|
|
|
|
for (auto const* decl: declarations)
|
|
|
|
ssl.append("The shadowed declaration is here:", decl->location());
|
|
|
|
if (!ssl.infos.empty())
|
2020-05-20 22:04:26 +00:00
|
|
|
m_errorReporter.declarationError(
|
2020-05-26 12:37:31 +00:00
|
|
|
3859_error,
|
2019-12-19 12:13:48 +00:00
|
|
|
identifier.location,
|
|
|
|
ssl,
|
|
|
|
"This declaration shadows a declaration outside the inline assembly block."
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (_varDecl.value)
|
|
|
|
visit(*_varDecl.value);
|
|
|
|
}
|
|
|
|
|
2020-06-29 12:30:09 +00:00
|
|
|
void ReferencesResolver::resolveInheritDoc(StructuredDocumentation const& _documentation, StructurallyDocumentedAnnotation& _annotation)
|
|
|
|
{
|
|
|
|
switch (_annotation.docTags.count("inheritdoc"))
|
|
|
|
{
|
|
|
|
case 0:
|
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
{
|
|
|
|
string const& name = _annotation.docTags.find("inheritdoc")->second.content;
|
2020-08-04 10:13:23 +00:00
|
|
|
if (name.empty())
|
|
|
|
{
|
|
|
|
m_errorReporter.docstringParsingError(
|
|
|
|
1933_error,
|
|
|
|
_documentation.location(),
|
|
|
|
"Expected contract name following documentation tag @inheritdoc."
|
|
|
|
);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-06-29 12:30:09 +00:00
|
|
|
vector<string> path;
|
|
|
|
boost::split(path, name, boost::is_any_of("."));
|
2020-08-26 19:25:22 +00:00
|
|
|
if (any_of(path.begin(), path.end(), [](auto& _str) { return _str.empty(); }))
|
|
|
|
{
|
|
|
|
m_errorReporter.docstringParsingError(
|
|
|
|
5967_error,
|
|
|
|
_documentation.location(),
|
|
|
|
"Documentation tag @inheritdoc reference \"" +
|
|
|
|
name +
|
|
|
|
"\" is malformed."
|
|
|
|
);
|
|
|
|
return;
|
|
|
|
}
|
2020-06-29 12:30:09 +00:00
|
|
|
Declaration const* result = m_resolver.pathFromCurrentScope(path);
|
|
|
|
|
|
|
|
if (result == nullptr)
|
|
|
|
{
|
|
|
|
m_errorReporter.docstringParsingError(
|
|
|
|
9397_error,
|
|
|
|
_documentation.location(),
|
|
|
|
"Documentation tag @inheritdoc references inexistent contract \"" +
|
|
|
|
name +
|
|
|
|
"\"."
|
|
|
|
);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
_annotation.inheritdocReference = dynamic_cast<ContractDefinition const*>(result);
|
|
|
|
|
|
|
|
if (!_annotation.inheritdocReference)
|
|
|
|
m_errorReporter.docstringParsingError(
|
|
|
|
1430_error,
|
|
|
|
_documentation.location(),
|
|
|
|
"Documentation tag @inheritdoc reference \"" +
|
|
|
|
name +
|
|
|
|
"\" is not a contract."
|
|
|
|
);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
m_errorReporter.docstringParsingError(
|
|
|
|
5142_error,
|
|
|
|
_documentation.location(),
|
|
|
|
"Documentation tag @inheritdoc can only be given once."
|
|
|
|
);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-02 12:38:36 +00:00
|
|
|
void ReferencesResolver::validateYulIdentifierName(yul::YulString _name, SourceLocation const& _location)
|
|
|
|
{
|
|
|
|
if (util::contains(_name.str(), '.'))
|
|
|
|
m_errorReporter.declarationError(
|
|
|
|
3927_error,
|
|
|
|
_location,
|
|
|
|
"User-defined identifiers in inline assembly cannot contain '.'."
|
|
|
|
);
|
2020-11-11 11:10:25 +00:00
|
|
|
|
|
|
|
if (set<string>{"this", "super", "_"}.count(_name.str()))
|
|
|
|
m_errorReporter.declarationError(
|
|
|
|
4113_error,
|
|
|
|
_location,
|
|
|
|
"The identifier name \"" + _name.str() + "\" is reserved."
|
|
|
|
);
|
2018-11-14 16:11:55 +00:00
|
|
|
}
|