2015-09-16 14:56:30 +00:00
|
|
|
/*
|
2016-11-18 23:13:20 +00:00
|
|
|
This file is part of solidity.
|
2015-09-16 14:56:30 +00:00
|
|
|
|
2016-11-18 23:13:20 +00:00
|
|
|
solidity is free software: you can redistribute it and/or modify
|
2015-09-16 14:56:30 +00:00
|
|
|
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.
|
|
|
|
|
2016-11-18 23:13:20 +00:00
|
|
|
solidity is distributed in the hope that it will be useful,
|
2015-09-16 14:56:30 +00:00
|
|
|
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
|
2016-11-18 23:13:20 +00:00
|
|
|
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/ast/AST.h>
|
|
|
|
#include <libsolidity/analysis/NameAndTypeResolver.h>
|
|
|
|
#include <libsolidity/interface/Exceptions.h>
|
|
|
|
#include <libsolidity/analysis/ConstantEvaluator.h>
|
2017-03-21 18:38:37 +00:00
|
|
|
#include <libsolidity/inlineasm/AsmAnalysis.h>
|
2017-04-26 13:41:08 +00:00
|
|
|
#include <libsolidity/inlineasm/AsmAnalysisInfo.h>
|
2016-03-01 21:56:39 +00:00
|
|
|
#include <libsolidity/inlineasm/AsmData.h>
|
2017-05-11 13:26:35 +00:00
|
|
|
#include <libsolidity/interface/ErrorReporter.h>
|
2015-09-16 14:56:30 +00:00
|
|
|
|
2017-04-21 17:13:46 +00:00
|
|
|
#include <boost/algorithm/string.hpp>
|
|
|
|
|
2015-09-16 14:56:30 +00:00
|
|
|
using namespace std;
|
|
|
|
using namespace dev;
|
|
|
|
using namespace dev::solidity;
|
|
|
|
|
|
|
|
|
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-02-14 00:48:40 +00:00
|
|
|
m_experimental050Mode = _block.sourceUnit().annotation().experimentalFeatures.count(ExperimentalFeature::V050);
|
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
|
|
|
}
|
|
|
|
|
|
|
|
bool ReferencesResolver::visit(ForStatement const& _for)
|
|
|
|
{
|
|
|
|
if (!m_resolveInsideCode)
|
|
|
|
return false;
|
2018-02-14 00:48:40 +00:00
|
|
|
m_experimental050Mode = _for.sourceUnit().annotation().experimentalFeatures.count(ExperimentalFeature::V050);
|
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)
|
|
|
|
{
|
|
|
|
_typeName.annotation().type = Type::fromElementaryTypeName(_typeName.typeName());
|
|
|
|
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))
|
|
|
|
_typeName.annotation().type = make_shared<StructType>(*structDef);
|
|
|
|
else if (EnumDefinition const* enumDef = dynamic_cast<EnumDefinition const*>(declaration))
|
|
|
|
_typeName.annotation().type = make_shared<EnumType>(*enumDef);
|
|
|
|
else if (ContractDefinition const* contract = dynamic_cast<ContractDefinition const*>(declaration))
|
|
|
|
_typeName.annotation().type = make_shared<ContractType>(*contract);
|
|
|
|
else
|
2017-10-26 14:02:36 +00:00
|
|
|
typeError(_typeName.location(), "Name has to refer to a struct, enum or contract.");
|
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())
|
|
|
|
{
|
|
|
|
case VariableDeclaration::Visibility::Internal:
|
|
|
|
case VariableDeclaration::Visibility::External:
|
|
|
|
break;
|
|
|
|
default:
|
2017-10-26 14:02:36 +00:00
|
|
|
typeError(_typeName.location(), "Invalid visibility, can only be \"external\" or \"internal\".");
|
|
|
|
return;
|
2016-09-27 19:37:32 +00:00
|
|
|
}
|
|
|
|
|
2016-11-09 14:14:16 +00:00
|
|
|
if (_typeName.isPayable() && _typeName.visibility() != VariableDeclaration::Visibility::External)
|
2017-10-26 14:02:36 +00:00
|
|
|
{
|
|
|
|
typeError(_typeName.location(), "Only external function types can be payable.");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-11-11 11:07:30 +00:00
|
|
|
if (_typeName.visibility() == VariableDeclaration::Visibility::External)
|
|
|
|
for (auto const& t: _typeName.parameterTypes() + _typeName.returnParameterTypes())
|
|
|
|
{
|
|
|
|
solAssert(t->annotation().type, "Type not set for parameter.");
|
|
|
|
if (!t->annotation().type->canBeUsedExternally(false))
|
2017-10-26 14:02:36 +00:00
|
|
|
{
|
|
|
|
typeError(t->location(), "Internal type cannot be used for external function type.");
|
|
|
|
return;
|
|
|
|
}
|
2016-11-11 11:07:30 +00:00
|
|
|
}
|
2016-09-27 19:37:32 +00:00
|
|
|
|
|
|
|
_typeName.annotation().type = make_shared<FunctionType>(_typeName);
|
|
|
|
}
|
|
|
|
|
2015-11-26 10:37:29 +00:00
|
|
|
void ReferencesResolver::endVisit(Mapping const& _typeName)
|
|
|
|
{
|
|
|
|
TypePointer keyType = _typeName.keyType().annotation().type;
|
|
|
|
TypePointer valueType = _typeName.valueType().annotation().type;
|
|
|
|
// Convert key type to memory.
|
|
|
|
keyType = ReferenceType::copyForLocationIfReference(DataLocation::Memory, keyType);
|
|
|
|
// Convert value type to storage reference.
|
|
|
|
valueType = ReferenceType::copyForLocationIfReference(DataLocation::Storage, valueType);
|
|
|
|
_typeName.annotation().type = make_shared<MappingType>(keyType, valueType);
|
|
|
|
}
|
|
|
|
|
|
|
|
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())
|
|
|
|
{
|
2017-11-22 11:54:23 +00:00
|
|
|
TypePointer lengthTypeGeneric = length->annotation().type;
|
|
|
|
if (!lengthTypeGeneric)
|
|
|
|
lengthTypeGeneric = ConstantEvaluator(m_errorReporter).evaluate(*length);
|
|
|
|
RationalNumberType const* lengthType = dynamic_cast<RationalNumberType const*>(lengthTypeGeneric.get());
|
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.");
|
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
|
|
|
|
_typeName.annotation().type = make_shared<ArrayType>(DataLocation::Storage, baseType, lengthType->literalValue(nullptr));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
_typeName.annotation().type = make_shared<ArrayType>(DataLocation::Storage, baseType);
|
|
|
|
}
|
|
|
|
|
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();
|
|
|
|
|
2016-03-01 21:56:39 +00:00
|
|
|
// Errors created in this stage are completely ignored because we do not yet know
|
|
|
|
// the type and size of external identifiers, which would result in false errors.
|
2017-03-14 14:41:23 +00:00
|
|
|
// The only purpose of this step is to fill the inline assembly annotation with
|
|
|
|
// external references.
|
2017-05-11 13:26:35 +00:00
|
|
|
ErrorList errors;
|
|
|
|
ErrorReporter errorsIgnored(errors);
|
2017-05-23 17:21:14 +00:00
|
|
|
julia::ExternalIdentifierAccess::Resolver resolver =
|
2017-05-24 16:34:19 +00:00
|
|
|
[&](assembly::Identifier const& _identifier, julia::IdentifierContext, bool _crossesFunctionBoundary) {
|
2016-03-01 21:56:39 +00:00
|
|
|
auto declarations = m_resolver.nameFromCurrentScope(_identifier.name);
|
2017-04-21 17:13:46 +00:00
|
|
|
bool isSlot = boost::algorithm::ends_with(_identifier.name, "_slot");
|
|
|
|
bool isOffset = boost::algorithm::ends_with(_identifier.name, "_offset");
|
|
|
|
if (isSlot || isOffset)
|
|
|
|
{
|
|
|
|
// special mode to access storage variables
|
|
|
|
if (!declarations.empty())
|
|
|
|
// the special identifier exists itself, we should not allow that.
|
|
|
|
return size_t(-1);
|
2017-04-21 17:28:52 +00:00
|
|
|
string realName = _identifier.name.substr(0, _identifier.name.size() - (
|
|
|
|
isSlot ?
|
2017-04-21 17:13:46 +00:00
|
|
|
string("_slot").size() :
|
|
|
|
string("_offset").size()
|
2017-04-21 17:28:52 +00:00
|
|
|
));
|
2017-04-21 17:13:46 +00:00
|
|
|
declarations = m_resolver.nameFromCurrentScope(realName);
|
|
|
|
}
|
2016-03-01 21:56:39 +00:00
|
|
|
if (declarations.size() != 1)
|
2017-03-14 14:41:23 +00:00
|
|
|
return size_t(-1);
|
2017-05-24 16:34:19 +00:00
|
|
|
if (auto var = dynamic_cast<VariableDeclaration const*>(declarations.front()))
|
|
|
|
if (var->isLocalVariable() && _crossesFunctionBoundary)
|
|
|
|
{
|
2017-05-31 10:54:18 +00:00
|
|
|
declarationError(_identifier.location, "Cannot access local Solidity variables from inside an inline assembly function.");
|
2017-05-24 16:34:19 +00:00
|
|
|
return size_t(-1);
|
|
|
|
}
|
2017-04-21 17:13:46 +00:00
|
|
|
_inlineAssembly.annotation().externalReferences[&_identifier].isSlot = isSlot;
|
|
|
|
_inlineAssembly.annotation().externalReferences[&_identifier].isOffset = isOffset;
|
2017-03-14 14:41:23 +00:00
|
|
|
_inlineAssembly.annotation().externalReferences[&_identifier].declaration = declarations.front();
|
|
|
|
return size_t(1);
|
|
|
|
};
|
2017-04-26 13:41:08 +00:00
|
|
|
|
|
|
|
// Will be re-generated later with correct information
|
2018-02-23 10:42:53 +00:00
|
|
|
// We use the latest EVM version because we will re-run it anyway.
|
2017-04-26 13:41:08 +00:00
|
|
|
assembly::AsmAnalysisInfo analysisInfo;
|
2018-07-03 09:51:44 +00:00
|
|
|
boost::optional<Error::Type> errorTypeForLoose = Error::Type::SyntaxError;
|
2018-02-15 14:18:09 +00:00
|
|
|
assembly::AsmAnalyzer(analysisInfo, errorsIgnored, EVMVersion(), errorTypeForLoose, assembly::AsmFlavour::Loose, resolver).analyze(_inlineAssembly.operations());
|
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;
|
|
|
|
|
|
|
|
TypePointer type;
|
|
|
|
if (_variable.typeName())
|
|
|
|
{
|
2015-11-26 10:37:29 +00:00
|
|
|
type = _variable.typeName()->annotation().type;
|
2015-09-16 14:56:30 +00:00
|
|
|
using Location = VariableDeclaration::Location;
|
2015-11-30 18:42:39 +00:00
|
|
|
Location varLoc = _variable.referenceLocation();
|
|
|
|
DataLocation typeLoc = DataLocation::Memory;
|
2015-09-16 14:56:30 +00:00
|
|
|
// References are forced to calldata for external function parameters (not return)
|
|
|
|
// and memory for parameters (also return) of publicly visible functions.
|
|
|
|
// They default to memory for function parameters and storage for local variables.
|
2015-10-02 11:11:38 +00:00
|
|
|
// As an exception, "storage" is allowed for library functions.
|
2015-09-16 14:56:30 +00:00
|
|
|
if (auto ref = dynamic_cast<ReferenceType const*>(type.get()))
|
|
|
|
{
|
2015-11-30 18:42:39 +00:00
|
|
|
bool isPointer = true;
|
2015-10-02 20:34:47 +00:00
|
|
|
if (_variable.isExternalCallableParameter())
|
2015-09-16 14:56:30 +00:00
|
|
|
{
|
2015-12-05 02:09:47 +00:00
|
|
|
auto const& contract = dynamic_cast<ContractDefinition const&>(
|
|
|
|
*dynamic_cast<Declaration const&>(*_variable.scope()).scope()
|
|
|
|
);
|
2015-10-02 20:34:47 +00:00
|
|
|
if (contract.isLibrary())
|
2015-10-02 11:11:38 +00:00
|
|
|
{
|
2015-11-30 18:42:39 +00:00
|
|
|
if (varLoc == Location::Memory)
|
2015-11-06 19:56:14 +00:00
|
|
|
fatalTypeError(_variable.location(),
|
2015-10-02 20:34:47 +00:00
|
|
|
"Location has to be calldata or storage for external "
|
|
|
|
"library functions (remove the \"memory\" keyword)."
|
2015-10-26 16:31:35 +00:00
|
|
|
);
|
2015-10-02 11:11:38 +00:00
|
|
|
}
|
2015-10-02 20:34:47 +00:00
|
|
|
else
|
2015-10-02 11:11:38 +00:00
|
|
|
{
|
2015-10-02 20:34:47 +00:00
|
|
|
// force location of external function parameters (not return) to calldata
|
2018-05-23 04:31:20 +00:00
|
|
|
if (varLoc != Location::CallData && varLoc != Location::Default)
|
2015-11-06 19:56:14 +00:00
|
|
|
fatalTypeError(_variable.location(),
|
2015-10-02 20:34:47 +00:00
|
|
|
"Location has to be calldata for external functions "
|
|
|
|
"(remove the \"memory\" or \"storage\" keyword)."
|
2015-10-26 16:31:35 +00:00
|
|
|
);
|
2015-10-02 11:11:38 +00:00
|
|
|
}
|
2015-11-30 18:42:39 +00:00
|
|
|
if (varLoc == Location::Default)
|
|
|
|
typeLoc = DataLocation::CallData;
|
|
|
|
else
|
|
|
|
typeLoc = varLoc == Location::Memory ? DataLocation::Memory : DataLocation::Storage;
|
2015-10-02 20:34:47 +00:00
|
|
|
}
|
2015-12-05 02:09:47 +00:00
|
|
|
else if (_variable.isCallableParameter() && dynamic_cast<Declaration const&>(*_variable.scope()).isPublic())
|
2015-10-02 20:34:47 +00:00
|
|
|
{
|
2015-12-05 02:09:47 +00:00
|
|
|
auto const& contract = dynamic_cast<ContractDefinition const&>(
|
|
|
|
*dynamic_cast<Declaration const&>(*_variable.scope()).scope()
|
|
|
|
);
|
2015-10-02 20:34:47 +00:00
|
|
|
// force locations of public or external function (return) parameters to memory
|
2018-05-23 04:31:20 +00:00
|
|
|
if (varLoc != Location::Memory && varLoc != Location::Default && !contract.isLibrary())
|
2015-11-06 19:56:14 +00:00
|
|
|
fatalTypeError(_variable.location(),
|
2015-10-02 20:34:47 +00:00
|
|
|
"Location has to be memory for publicly visible functions "
|
2018-05-23 04:31:20 +00:00
|
|
|
"(remove the \"storage\" or \"calldata\" keyword)."
|
2015-10-26 16:31:35 +00:00
|
|
|
);
|
2015-11-30 18:42:39 +00:00
|
|
|
if (varLoc == Location::Default || !contract.isLibrary())
|
|
|
|
typeLoc = DataLocation::Memory;
|
|
|
|
else
|
2018-05-23 04:31:20 +00:00
|
|
|
{
|
|
|
|
if (varLoc == Location::CallData)
|
|
|
|
fatalTypeError(_variable.location(),
|
|
|
|
"Location cannot be calldata for non-external functions "
|
|
|
|
"(remove the \"calldata\" keyword)."
|
|
|
|
);
|
2015-11-30 18:42:39 +00:00
|
|
|
typeLoc = varLoc == Location::Memory ? DataLocation::Memory : DataLocation::Storage;
|
2018-05-23 04:31:20 +00:00
|
|
|
}
|
2015-09-16 14:56:30 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (_variable.isConstant())
|
|
|
|
{
|
2015-11-30 18:42:39 +00:00
|
|
|
if (varLoc != Location::Default && varLoc != Location::Memory)
|
2015-11-06 19:56:14 +00:00
|
|
|
fatalTypeError(
|
2015-11-04 11:30:58 +00:00
|
|
|
_variable.location(),
|
2018-05-23 04:31:20 +00:00
|
|
|
"Data location has to be \"memory\" (or unspecified) for constants."
|
2015-11-04 11:30:58 +00:00
|
|
|
);
|
2015-11-30 18:42:39 +00:00
|
|
|
typeLoc = DataLocation::Memory;
|
2015-09-16 14:56:30 +00:00
|
|
|
}
|
2015-11-30 18:42:39 +00:00
|
|
|
else if (varLoc == Location::Default)
|
2017-07-05 17:38:00 +00:00
|
|
|
{
|
|
|
|
if (_variable.isCallableParameter())
|
|
|
|
typeLoc = DataLocation::Memory;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
typeLoc = DataLocation::Storage;
|
2017-07-10 16:30:41 +00:00
|
|
|
if (_variable.isLocalVariable())
|
2018-07-11 11:23:16 +00:00
|
|
|
typeError(
|
|
|
|
_variable.location(),
|
|
|
|
"Data location must be specified as either \"memory\" or \"storage\"."
|
|
|
|
);
|
2017-07-05 17:38:00 +00:00
|
|
|
}
|
|
|
|
}
|
2015-11-30 18:42:39 +00:00
|
|
|
else
|
2018-05-23 04:31:20 +00:00
|
|
|
{
|
|
|
|
switch (varLoc)
|
|
|
|
{
|
|
|
|
case Location::Memory:
|
|
|
|
typeLoc = DataLocation::Memory;
|
|
|
|
break;
|
|
|
|
case Location::Storage:
|
|
|
|
typeLoc = DataLocation::Storage;
|
|
|
|
break;
|
|
|
|
case Location::CallData:
|
|
|
|
fatalTypeError(_variable.location(),
|
|
|
|
"Variable cannot be declared as \"calldata\" (remove the \"calldata\" keyword)."
|
|
|
|
);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
solAssert(false, "Unknown data location");
|
|
|
|
}
|
|
|
|
}
|
2015-11-30 18:42:39 +00:00
|
|
|
isPointer = !_variable.isStateVariable();
|
2015-09-16 14:56:30 +00:00
|
|
|
}
|
2015-11-30 18:42:39 +00:00
|
|
|
type = ref->copyForLocation(typeLoc, isPointer);
|
2015-09-16 14:56:30 +00:00
|
|
|
}
|
2018-07-18 15:20:26 +00:00
|
|
|
else if (dynamic_cast<MappingType const*>(type.get()))
|
|
|
|
{
|
|
|
|
if (_variable.isLocalVariable() && varLoc != Location::Storage)
|
|
|
|
typeError(
|
|
|
|
_variable.location(),
|
|
|
|
"Data location for mappings must be specified as \"storage\"."
|
|
|
|
);
|
|
|
|
}
|
2015-11-30 18:42:39 +00:00
|
|
|
else if (varLoc != Location::Default && !ref)
|
2018-05-23 04:31:20 +00:00
|
|
|
typeError(_variable.location(), "Data location can only be given for array or struct types.");
|
2015-09-16 14:56:30 +00:00
|
|
|
|
2017-10-26 14:02:36 +00:00
|
|
|
_variable.annotation().type = type;
|
2015-09-16 14:56:30 +00:00
|
|
|
}
|
|
|
|
else if (!_variable.canHaveAutoType())
|
2017-10-26 14:02:36 +00:00
|
|
|
typeError(_variable.location(), "Explicit type needed.");
|
2015-09-16 14:56:30 +00:00
|
|
|
// otherwise we have a "var"-declaration whose type is resolved by the first assignment
|
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|