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
|
|
|
*/
|
|
|
|
/**
|
|
|
|
* @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>
|
|
|
|
#include <libsolidity/analysis/ConstantEvaluator.h>
|
2018-12-17 11:30:08 +00:00
|
|
|
#include <libsolidity/ast/AST.h>
|
2019-04-15 13:33:39 +00:00
|
|
|
#include <libsolidity/ast/TypeProvider.h>
|
2018-12-17 11:30:08 +00:00
|
|
|
|
2018-11-23 10:18:57 +00:00
|
|
|
#include <libyul/AsmAnalysis.h>
|
|
|
|
#include <libyul/AsmAnalysisInfo.h>
|
|
|
|
#include <libyul/AsmData.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>
|
2018-08-07 13:58:34 +00:00
|
|
|
|
2017-04-21 17:13:46 +00:00
|
|
|
#include <boost/algorithm/string.hpp>
|
2018-08-07 13:58:34 +00:00
|
|
|
#include <boost/range/adaptor/transformed.hpp>
|
2017-04-21 17:13:46 +00:00
|
|
|
|
2015-09-16 14:56:30 +00:00
|
|
|
using namespace std;
|
2019-12-11 16:31:36 +00:00
|
|
|
using namespace solidity::langutil;
|
2015-09-16 14:56:30 +00:00
|
|
|
|
2019-12-11 16:31:36 +00:00
|
|
|
namespace solidity::frontend
|
2018-11-14 16:11:55 +00:00
|
|
|
{
|
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
|
|
|
{
|
2017-02-16 11:36:43 +00:00
|
|
|
_root.accept(*this);
|
2015-11-06 20:07:42 +00:00
|
|
|
return !m_errorOccurred;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
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) + "?";
|
|
|
|
}
|
2017-10-29 08:08:40 +00:00
|
|
|
declarationError(_identifier.location(), errorMessage);
|
|
|
|
}
|
2015-09-16 14:56:30 +00:00
|
|
|
else if (declarations.size() == 1)
|
|
|
|
_identifier.annotation().referencedDeclaration = declarations.front();
|
|
|
|
else
|
|
|
|
_identifier.annotation().overloadedDeclarations =
|
|
|
|
m_resolver.cleanedDeclarations(_identifier, declarations);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-11-26 10:37:29 +00:00
|
|
|
bool ReferencesResolver::visit(ElementaryTypeName const& _typeName)
|
|
|
|
{
|
2018-09-06 16:59:50 +00:00
|
|
|
if (!_typeName.annotation().type)
|
|
|
|
{
|
2019-04-15 13:33:39 +00:00
|
|
|
_typeName.annotation().type = TypeProvider::fromElementaryTypeName(_typeName.typeName());
|
2019-10-28 10:39:30 +00:00
|
|
|
if (_typeName.stateMutability().has_value())
|
2018-09-06 16:59:50 +00:00
|
|
|
{
|
|
|
|
// for non-address types this was already caught by the parser
|
|
|
|
solAssert(_typeName.annotation().type->category() == Type::Category::Address, "");
|
2019-11-11 16:09:59 +00:00
|
|
|
switch (*_typeName.stateMutability())
|
2018-09-05 15:59:55 +00:00
|
|
|
{
|
|
|
|
case StateMutability::Payable:
|
2019-04-17 11:40:50 +00:00
|
|
|
_typeName.annotation().type = TypeProvider::payableAddress();
|
2019-04-15 13:33:39 +00:00
|
|
|
break;
|
2018-09-05 15:59:55 +00:00
|
|
|
case StateMutability::NonPayable:
|
2019-04-17 11:40:50 +00:00
|
|
|
_typeName.annotation().type = TypeProvider::address();
|
2018-09-05 15:59:55 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
m_errorReporter.typeError(
|
|
|
|
_typeName.location(),
|
|
|
|
"Address types can only be payable or non-payable."
|
|
|
|
);
|
|
|
|
break;
|
|
|
|
}
|
2018-09-06 16:59:50 +00:00
|
|
|
}
|
|
|
|
}
|
2015-11-26 10:37:29 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-01-27 17:27:59 +00:00
|
|
|
bool ReferencesResolver::visit(FunctionDefinition const& _functionDefinition)
|
|
|
|
{
|
|
|
|
m_returnParameters.push_back(_functionDefinition.returnParameterList().get());
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ReferencesResolver::endVisit(FunctionDefinition const&)
|
|
|
|
{
|
|
|
|
solAssert(!m_returnParameters.empty(), "");
|
|
|
|
m_returnParameters.pop_back();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ReferencesResolver::visit(ModifierDefinition const&)
|
|
|
|
{
|
|
|
|
m_returnParameters.push_back(nullptr);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ReferencesResolver::endVisit(ModifierDefinition const&)
|
|
|
|
{
|
|
|
|
solAssert(!m_returnParameters.empty(), "");
|
|
|
|
m_returnParameters.pop_back();
|
|
|
|
}
|
|
|
|
|
2015-11-26 10:37:29 +00:00
|
|
|
void ReferencesResolver::endVisit(UserDefinedTypeName const& _typeName)
|
|
|
|
{
|
|
|
|
Declaration const* declaration = m_resolver.pathFromCurrentScope(_typeName.namePath());
|
|
|
|
if (!declaration)
|
2017-10-26 14:02:36 +00:00
|
|
|
{
|
2018-07-30 14:43:50 +00:00
|
|
|
fatalDeclarationError(_typeName.location(), "Identifier not found or not unique.");
|
2017-10-26 14:02:36 +00:00
|
|
|
return;
|
|
|
|
}
|
2015-11-26 10:37:29 +00:00
|
|
|
|
|
|
|
_typeName.annotation().referencedDeclaration = declaration;
|
|
|
|
|
|
|
|
if (StructDefinition const* structDef = dynamic_cast<StructDefinition const*>(declaration))
|
2019-04-15 15:28:32 +00:00
|
|
|
_typeName.annotation().type = TypeProvider::structType(*structDef, DataLocation::Storage);
|
2015-11-26 10:37:29 +00:00
|
|
|
else if (EnumDefinition const* enumDef = dynamic_cast<EnumDefinition const*>(declaration))
|
2019-04-15 13:33:39 +00:00
|
|
|
_typeName.annotation().type = TypeProvider::enumType(*enumDef);
|
2015-11-26 10:37:29 +00:00
|
|
|
else if (ContractDefinition const* contract = dynamic_cast<ContractDefinition const*>(declaration))
|
2019-04-17 11:40:50 +00:00
|
|
|
_typeName.annotation().type = TypeProvider::contract(*contract);
|
2015-11-26 10:37:29 +00:00
|
|
|
else
|
2018-08-07 13:58:34 +00:00
|
|
|
{
|
2019-04-17 11:40:50 +00:00
|
|
|
_typeName.annotation().type = TypeProvider::emptyTuple();
|
2020-03-24 02:43:07 +00:00
|
|
|
fatalTypeError(_typeName.location(), "Name has to refer to a struct, enum or contract.");
|
2018-08-07 13:58:34 +00:00
|
|
|
}
|
2015-11-26 10:37:29 +00:00
|
|
|
}
|
|
|
|
|
2016-09-27 19:37:32 +00:00
|
|
|
void ReferencesResolver::endVisit(FunctionTypeName const& _typeName)
|
|
|
|
{
|
|
|
|
switch (_typeName.visibility())
|
|
|
|
{
|
2019-12-10 14:54:09 +00:00
|
|
|
case Visibility::Internal:
|
|
|
|
case Visibility::External:
|
2016-09-27 19:37:32 +00:00
|
|
|
break;
|
|
|
|
default:
|
2018-08-07 13:58:34 +00:00
|
|
|
fatalTypeError(_typeName.location(), "Invalid visibility, can only be \"external\" or \"internal\".");
|
2017-10-26 14:02:36 +00:00
|
|
|
return;
|
2016-09-27 19:37:32 +00:00
|
|
|
}
|
|
|
|
|
2019-12-10 14:54:09 +00:00
|
|
|
if (_typeName.isPayable() && _typeName.visibility() != Visibility::External)
|
2017-10-26 14:02:36 +00:00
|
|
|
{
|
2018-08-07 13:58:34 +00:00
|
|
|
fatalTypeError(_typeName.location(), "Only external function types can be payable.");
|
2017-10-26 14:02:36 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-12-10 14:54:09 +00:00
|
|
|
if (_typeName.visibility() == Visibility::External)
|
2016-11-11 11:07:30 +00:00
|
|
|
for (auto const& t: _typeName.parameterTypes() + _typeName.returnParameterTypes())
|
|
|
|
{
|
|
|
|
solAssert(t->annotation().type, "Type not set for parameter.");
|
2019-03-18 12:22:42 +00:00
|
|
|
if (!t->annotation().type->interfaceType(false).get())
|
2017-10-26 14:02:36 +00:00
|
|
|
{
|
2018-08-07 13:58:34 +00:00
|
|
|
fatalTypeError(t->location(), "Internal type cannot be used for external function type.");
|
2017-10-26 14:02:36 +00:00
|
|
|
return;
|
|
|
|
}
|
2016-11-11 11:07:30 +00:00
|
|
|
}
|
2016-09-27 19:37:32 +00:00
|
|
|
|
2019-04-17 11:40:50 +00:00
|
|
|
_typeName.annotation().type = TypeProvider::function(_typeName);
|
2016-09-27 19:37:32 +00:00
|
|
|
}
|
|
|
|
|
2020-02-11 12:35:23 +00:00
|
|
|
void ReferencesResolver::endVisit(Mapping const& _mapping)
|
2015-11-26 10:37:29 +00:00
|
|
|
{
|
2020-02-11 12:35:23 +00:00
|
|
|
if (auto const* typeName = dynamic_cast<UserDefinedTypeName const*>(&_mapping.keyType()))
|
|
|
|
{
|
|
|
|
if (auto const* contractType = dynamic_cast<ContractType const*>(typeName->annotation().type))
|
|
|
|
{
|
|
|
|
if (contractType->contractDefinition().isLibrary())
|
|
|
|
m_errorReporter.fatalTypeError(
|
|
|
|
typeName->location(),
|
|
|
|
"Library types cannot be used as mapping keys."
|
|
|
|
);
|
|
|
|
}
|
|
|
|
else if (typeName->annotation().type->category() != Type::Category::Enum)
|
|
|
|
m_errorReporter.fatalTypeError(
|
|
|
|
typeName->location(),
|
|
|
|
"Only elementary types, contract types or enums are allowed as mapping keys."
|
|
|
|
);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
solAssert(dynamic_cast<ElementaryTypeName const*>(&_mapping.keyType()), "");
|
|
|
|
|
|
|
|
TypePointer keyType = _mapping.keyType().annotation().type;
|
|
|
|
TypePointer valueType = _mapping.valueType().annotation().type;
|
|
|
|
|
2015-11-26 10:37:29 +00:00
|
|
|
// Convert key type to memory.
|
2019-04-17 11:25:03 +00:00
|
|
|
keyType = TypeProvider::withLocationIfReference(DataLocation::Memory, keyType);
|
2020-02-11 12:35:23 +00:00
|
|
|
|
2015-11-26 10:37:29 +00:00
|
|
|
// Convert value type to storage reference.
|
2019-04-17 11:25:03 +00:00
|
|
|
valueType = TypeProvider::withLocationIfReference(DataLocation::Storage, valueType);
|
2020-02-11 12:35:23 +00:00
|
|
|
_mapping.annotation().type = TypeProvider::mapping(keyType, valueType);
|
2015-11-26 10:37:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void ReferencesResolver::endVisit(ArrayTypeName const& _typeName)
|
|
|
|
{
|
|
|
|
TypePointer baseType = _typeName.baseType().annotation().type;
|
2018-02-11 22:44:23 +00:00
|
|
|
if (!baseType)
|
|
|
|
{
|
|
|
|
solAssert(!m_errorReporter.errors().empty(), "");
|
|
|
|
return;
|
|
|
|
}
|
2015-11-26 10:37:29 +00:00
|
|
|
if (baseType->storageBytes() == 0)
|
|
|
|
fatalTypeError(_typeName.baseType().location(), "Illegal base type of storage size zero for array.");
|
|
|
|
if (Expression const* length = _typeName.length())
|
|
|
|
{
|
2019-02-19 11:16:15 +00:00
|
|
|
TypePointer& lengthTypeGeneric = length->annotation().type;
|
2017-11-22 11:54:23 +00:00
|
|
|
if (!lengthTypeGeneric)
|
|
|
|
lengthTypeGeneric = ConstantEvaluator(m_errorReporter).evaluate(*length);
|
2019-04-15 13:33:39 +00:00
|
|
|
RationalNumberType const* lengthType = dynamic_cast<RationalNumberType const*>(lengthTypeGeneric);
|
2017-10-02 21:59:39 +00:00
|
|
|
if (!lengthType || !lengthType->mobileType())
|
2017-11-22 12:33:11 +00:00
|
|
|
fatalTypeError(length->location(), "Invalid array length, expected integer literal or constant expression.");
|
2018-09-26 10:46:08 +00:00
|
|
|
else if (lengthType->isZero())
|
2018-09-21 22:18:22 +00:00
|
|
|
fatalTypeError(length->location(), "Array with zero length specified.");
|
2017-10-02 21:59:39 +00:00
|
|
|
else if (lengthType->isFractional())
|
|
|
|
fatalTypeError(length->location(), "Array with fractional length specified.");
|
2017-02-02 00:24:45 +00:00
|
|
|
else if (lengthType->isNegative())
|
|
|
|
fatalTypeError(length->location(), "Array with negative length specified.");
|
2015-11-26 10:37:29 +00:00
|
|
|
else
|
2019-04-17 11:40:50 +00:00
|
|
|
_typeName.annotation().type = TypeProvider::array(DataLocation::Storage, baseType, lengthType->literalValue(nullptr));
|
2015-11-26 10:37:29 +00:00
|
|
|
}
|
|
|
|
else
|
2019-04-17 11:40:50 +00:00
|
|
|
_typeName.annotation().type = TypeProvider::array(DataLocation::Storage, baseType);
|
2015-11-26 10:37:29 +00:00
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2015-09-16 14:56:30 +00:00
|
|
|
void ReferencesResolver::endVisit(VariableDeclaration const& _variable)
|
|
|
|
{
|
|
|
|
if (_variable.annotation().type)
|
|
|
|
return;
|
|
|
|
|
2018-08-07 13:58:34 +00:00
|
|
|
if (_variable.isConstant() && !_variable.isStateVariable())
|
|
|
|
m_errorReporter.declarationError(_variable.location(), "The \"constant\" keyword can only be used for state variables.");
|
2020-02-27 15:13:55 +00:00
|
|
|
if (_variable.immutable() && !_variable.isStateVariable())
|
|
|
|
m_errorReporter.declarationError(_variable.location(), "The \"immutable\" keyword can only be used for state variables.");
|
2018-08-07 13:58:34 +00:00
|
|
|
|
2018-08-06 16:03:17 +00:00
|
|
|
if (!_variable.typeName())
|
|
|
|
{
|
|
|
|
// This can still happen in very unusual cases where a developer uses constructs, such as
|
|
|
|
// `var a;`, however, such code will have generated errors already.
|
|
|
|
// However, we cannot blindingly solAssert() for that here, as the TypeChecker (which is
|
|
|
|
// invoking ReferencesResolver) is generating it, so the error is most likely(!) generated
|
|
|
|
// after this step.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
using Location = VariableDeclaration::Location;
|
|
|
|
Location varLoc = _variable.referenceLocation();
|
|
|
|
DataLocation typeLoc = DataLocation::Memory;
|
2018-08-07 13:58:34 +00:00
|
|
|
|
|
|
|
set<Location> allowedDataLocations = _variable.allowedDataLocations();
|
|
|
|
if (!allowedDataLocations.count(varLoc))
|
2015-09-16 14:56:30 +00:00
|
|
|
{
|
2018-08-07 13:58:34 +00:00
|
|
|
auto locationToString = [](VariableDeclaration::Location _location) -> string
|
2015-09-16 14:56:30 +00:00
|
|
|
{
|
2018-08-07 13:58:34 +00:00
|
|
|
switch (_location)
|
2015-10-02 20:34:47 +00:00
|
|
|
{
|
2018-08-07 13:58:34 +00:00
|
|
|
case Location::Memory: return "\"memory\"";
|
|
|
|
case Location::Storage: return "\"storage\"";
|
|
|
|
case Location::CallData: return "\"calldata\"";
|
2018-08-17 23:09:31 +00:00
|
|
|
case Location::Unspecified: return "none";
|
2018-08-06 16:03:17 +00:00
|
|
|
}
|
2018-08-07 13:58:34 +00:00
|
|
|
return {};
|
|
|
|
};
|
|
|
|
|
|
|
|
string errorString;
|
|
|
|
if (!_variable.hasReferenceOrMappingType())
|
|
|
|
errorString = "Data location can only be specified for array, struct or mapping types";
|
|
|
|
else
|
2018-08-06 16:03:17 +00:00
|
|
|
{
|
2018-08-07 13:58:34 +00:00
|
|
|
errorString = "Data location must be " +
|
2019-12-11 16:31:36 +00:00
|
|
|
util::joinHumanReadable(
|
2018-08-07 13:58:34 +00:00
|
|
|
allowedDataLocations | boost::adaptors::transformed(locationToString),
|
|
|
|
", ",
|
|
|
|
" or "
|
2018-08-06 16:03:17 +00:00
|
|
|
);
|
2019-09-05 18:02:34 +00:00
|
|
|
if (_variable.isCallableOrCatchParameter())
|
2018-08-07 13:58:34 +00:00
|
|
|
errorString +=
|
|
|
|
" for " +
|
|
|
|
string(_variable.isReturnParameter() ? "return " : "") +
|
|
|
|
"parameter in" +
|
|
|
|
string(_variable.isExternalCallableParameter() ? " external" : "") +
|
|
|
|
" function";
|
2018-08-06 16:03:17 +00:00
|
|
|
else
|
2018-08-07 13:58:34 +00:00
|
|
|
errorString += " for variable";
|
2018-08-06 16:03:17 +00:00
|
|
|
}
|
2018-08-07 13:58:34 +00:00
|
|
|
errorString += ", but " + locationToString(varLoc) + " was given.";
|
|
|
|
typeError(_variable.location(), errorString);
|
|
|
|
|
|
|
|
solAssert(!allowedDataLocations.empty(), "");
|
|
|
|
varLoc = *allowedDataLocations.begin();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Find correct data location.
|
|
|
|
if (_variable.isEventParameter())
|
|
|
|
{
|
2018-08-17 23:09:31 +00:00
|
|
|
solAssert(varLoc == Location::Unspecified, "");
|
2018-08-07 13:58:34 +00:00
|
|
|
typeLoc = DataLocation::Memory;
|
|
|
|
}
|
|
|
|
else if (_variable.isStateVariable())
|
|
|
|
{
|
2018-08-17 23:09:31 +00:00
|
|
|
solAssert(varLoc == Location::Unspecified, "");
|
2020-02-27 15:13:55 +00:00
|
|
|
typeLoc = (_variable.isConstant() || _variable.immutable()) ? DataLocation::Memory : DataLocation::Storage;
|
2018-08-07 13:58:34 +00:00
|
|
|
}
|
|
|
|
else if (
|
|
|
|
dynamic_cast<StructDefinition const*>(_variable.scope()) ||
|
|
|
|
dynamic_cast<EnumDefinition const*>(_variable.scope())
|
|
|
|
)
|
|
|
|
// The actual location will later be changed depending on how the type is used.
|
|
|
|
typeLoc = DataLocation::Storage;
|
|
|
|
else
|
|
|
|
switch (varLoc)
|
2018-08-06 16:03:17 +00:00
|
|
|
{
|
2018-08-07 13:58:34 +00:00
|
|
|
case Location::Memory:
|
|
|
|
typeLoc = DataLocation::Memory;
|
|
|
|
break;
|
|
|
|
case Location::Storage:
|
|
|
|
typeLoc = DataLocation::Storage;
|
|
|
|
break;
|
|
|
|
case Location::CallData:
|
|
|
|
typeLoc = DataLocation::CallData;
|
|
|
|
break;
|
2018-08-17 23:09:31 +00:00
|
|
|
case Location::Unspecified:
|
2018-08-07 13:58:34 +00:00
|
|
|
solAssert(!_variable.hasReferenceOrMappingType(), "Data location not properly set.");
|
2018-07-18 15:20:26 +00:00
|
|
|
}
|
2018-08-07 13:58:34 +00:00
|
|
|
|
|
|
|
TypePointer type = _variable.typeName()->annotation().type;
|
2019-04-15 13:33:39 +00:00
|
|
|
if (auto ref = dynamic_cast<ReferenceType const*>(type))
|
2018-08-06 16:03:17 +00:00
|
|
|
{
|
2018-08-07 13:58:34 +00:00
|
|
|
bool isPointer = !_variable.isStateVariable();
|
2019-04-15 13:33:39 +00:00
|
|
|
type = TypeProvider::withLocation(ref, typeLoc, isPointer);
|
2018-08-06 16:03:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
_variable.annotation().type = type;
|
2015-09-16 14:56:30 +00:00
|
|
|
}
|
|
|
|
|
2019-12-19 12:13:48 +00:00
|
|
|
void ReferencesResolver::operator()(yul::FunctionDefinition const& _function)
|
|
|
|
{
|
|
|
|
bool wasInsideFunction = m_yulInsideFunction;
|
|
|
|
m_yulInsideFunction = true;
|
|
|
|
this->operator()(_function.body);
|
|
|
|
m_yulInsideFunction = wasInsideFunction;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ReferencesResolver::operator()(yul::Identifier const& _identifier)
|
|
|
|
{
|
|
|
|
bool isSlot = boost::algorithm::ends_with(_identifier.name.str(), "_slot");
|
|
|
|
bool isOffset = boost::algorithm::ends_with(_identifier.name.str(), "_offset");
|
|
|
|
|
|
|
|
auto declarations = m_resolver.nameFromCurrentScope(_identifier.name.str());
|
|
|
|
if (isSlot || isOffset)
|
|
|
|
{
|
|
|
|
// special mode to access storage variables
|
|
|
|
if (!declarations.empty())
|
|
|
|
// the special identifier exists itself, we should not allow that.
|
|
|
|
return;
|
|
|
|
string realName = _identifier.name.str().substr(0, _identifier.name.str().size() - (
|
|
|
|
isSlot ?
|
|
|
|
string("_slot").size() :
|
|
|
|
string("_offset").size()
|
|
|
|
));
|
|
|
|
if (realName.empty())
|
|
|
|
{
|
|
|
|
declarationError(_identifier.location, "In variable names _slot and _offset can only be used as a suffix.");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
declarations = m_resolver.nameFromCurrentScope(realName);
|
|
|
|
}
|
|
|
|
if (declarations.size() > 1)
|
|
|
|
{
|
|
|
|
declarationError(_identifier.location, "Multiple matching identifiers. Resolving overloaded identifiers is not supported.");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
else if (declarations.size() == 0)
|
|
|
|
return;
|
|
|
|
if (auto var = dynamic_cast<VariableDeclaration const*>(declarations.front()))
|
|
|
|
if (var->isLocalVariable() && m_yulInsideFunction)
|
|
|
|
{
|
|
|
|
declarationError(_identifier.location, "Cannot access local Solidity variables from inside an inline assembly function.");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
m_yulAnnotation->externalReferences[&_identifier].isSlot = isSlot;
|
|
|
|
m_yulAnnotation->externalReferences[&_identifier].isOffset = isOffset;
|
|
|
|
m_yulAnnotation->externalReferences[&_identifier].declaration = declarations.front();
|
|
|
|
}
|
|
|
|
|
|
|
|
void ReferencesResolver::operator()(yul::VariableDeclaration const& _varDecl)
|
|
|
|
{
|
|
|
|
for (auto const& identifier: _varDecl.variables)
|
|
|
|
{
|
|
|
|
bool isSlot = boost::algorithm::ends_with(identifier.name.str(), "_slot");
|
|
|
|
bool isOffset = boost::algorithm::ends_with(identifier.name.str(), "_offset");
|
|
|
|
|
|
|
|
string namePrefix = identifier.name.str().substr(0, identifier.name.str().find('.'));
|
|
|
|
if (isSlot || isOffset)
|
|
|
|
declarationError(identifier.location, "In variable declarations _slot and _offset can not be used as a suffix.");
|
|
|
|
else if (
|
|
|
|
auto declarations = m_resolver.nameFromCurrentScope(namePrefix);
|
|
|
|
!declarations.empty()
|
|
|
|
)
|
|
|
|
{
|
|
|
|
SecondarySourceLocation ssl;
|
|
|
|
for (auto const* decl: declarations)
|
|
|
|
ssl.append("The shadowed declaration is here:", decl->location());
|
|
|
|
if (!ssl.infos.empty())
|
|
|
|
declarationError(
|
|
|
|
identifier.location,
|
|
|
|
ssl,
|
|
|
|
namePrefix.size() < identifier.name.str().size() ?
|
|
|
|
"The prefix of this declaration conflicts with a declaration outside the inline assembly block." :
|
|
|
|
"This declaration shadows a declaration outside the inline assembly block."
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (_varDecl.value)
|
|
|
|
visit(*_varDecl.value);
|
|
|
|
}
|
|
|
|
|
2015-11-06 19:56:14 +00:00
|
|
|
void ReferencesResolver::typeError(SourceLocation const& _location, string const& _description)
|
2015-10-26 16:31:35 +00:00
|
|
|
{
|
2015-11-06 16:22:46 +00:00
|
|
|
m_errorOccurred = true;
|
2017-05-11 13:26:35 +00:00
|
|
|
m_errorReporter.typeError(_location, _description);
|
2015-10-26 16:31:35 +00:00
|
|
|
}
|
|
|
|
|
2015-11-06 19:56:14 +00:00
|
|
|
void ReferencesResolver::fatalTypeError(SourceLocation const& _location, string const& _description)
|
2015-10-26 16:31:35 +00:00
|
|
|
{
|
2015-11-06 16:22:46 +00:00
|
|
|
m_errorOccurred = true;
|
2017-05-11 13:26:35 +00:00
|
|
|
m_errorReporter.fatalTypeError(_location, _description);
|
2015-11-06 16:22:46 +00:00
|
|
|
}
|
|
|
|
|
2017-05-31 10:54:18 +00:00
|
|
|
void ReferencesResolver::declarationError(SourceLocation const& _location, string const& _description)
|
|
|
|
{
|
|
|
|
m_errorOccurred = true;
|
|
|
|
m_errorReporter.declarationError(_location, _description);
|
|
|
|
}
|
|
|
|
|
2019-11-06 17:06:36 +00:00
|
|
|
void ReferencesResolver::declarationError(SourceLocation const& _location, SecondarySourceLocation const& _ssl, string const& _description)
|
|
|
|
{
|
|
|
|
m_errorOccurred = true;
|
|
|
|
m_errorReporter.declarationError(_location, _ssl, _description);
|
|
|
|
}
|
|
|
|
|
2015-11-06 19:56:14 +00:00
|
|
|
void ReferencesResolver::fatalDeclarationError(SourceLocation const& _location, string const& _description)
|
2015-11-06 16:22:46 +00:00
|
|
|
{
|
2017-05-11 13:26:35 +00:00
|
|
|
m_errorOccurred = true;
|
|
|
|
m_errorReporter.fatalDeclarationError(_location, _description);
|
2015-11-06 16:22:46 +00:00
|
|
|
}
|
2018-11-14 16:11:55 +00:00
|
|
|
|
|
|
|
}
|