2020-04-07 17:31:48 +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
|
2020-04-07 17:31:48 +00:00
|
|
|
|
|
|
|
#include <libsolidity/analysis/DeclarationTypeChecker.h>
|
|
|
|
|
|
|
|
#include <libsolidity/analysis/ConstantEvaluator.h>
|
|
|
|
|
|
|
|
#include <libsolidity/ast/TypeProvider.h>
|
|
|
|
|
|
|
|
#include <liblangutil/ErrorReporter.h>
|
|
|
|
|
2020-04-15 09:37:39 +00:00
|
|
|
#include <libsolutil/Algorithms.h>
|
2021-10-11 08:16:52 +00:00
|
|
|
#include <libsolutil/Visitor.h>
|
2020-04-15 09:37:39 +00:00
|
|
|
|
2021-04-08 12:08:14 +00:00
|
|
|
#include <range/v3/view/transform.hpp>
|
2020-04-07 17:31:48 +00:00
|
|
|
|
|
|
|
using namespace std;
|
|
|
|
using namespace solidity::langutil;
|
|
|
|
using namespace solidity::frontend;
|
|
|
|
|
|
|
|
bool DeclarationTypeChecker::visit(ElementaryTypeName const& _typeName)
|
|
|
|
{
|
2020-04-14 14:36:37 +00:00
|
|
|
if (_typeName.annotation().type)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
_typeName.annotation().type = TypeProvider::fromElementaryTypeName(_typeName.typeName());
|
|
|
|
if (_typeName.stateMutability().has_value())
|
2020-04-07 17:31:48 +00:00
|
|
|
{
|
2020-04-14 14:36:37 +00:00
|
|
|
// for non-address types this was already caught by the parser
|
|
|
|
solAssert(_typeName.annotation().type->category() == Type::Category::Address, "");
|
|
|
|
switch (*_typeName.stateMutability())
|
2020-04-07 17:31:48 +00:00
|
|
|
{
|
2020-04-14 14:36:37 +00:00
|
|
|
case StateMutability::Payable:
|
|
|
|
_typeName.annotation().type = TypeProvider::payableAddress();
|
|
|
|
break;
|
|
|
|
case StateMutability::NonPayable:
|
|
|
|
_typeName.annotation().type = TypeProvider::address();
|
|
|
|
break;
|
|
|
|
default:
|
2020-05-20 21:56:25 +00:00
|
|
|
m_errorReporter.typeError(
|
|
|
|
2311_error,
|
2020-04-14 14:36:37 +00:00
|
|
|
_typeName.location(),
|
|
|
|
"Address types can only be payable or non-payable."
|
|
|
|
);
|
|
|
|
break;
|
2020-04-07 17:31:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-11-10 10:41:38 +00:00
|
|
|
bool DeclarationTypeChecker::visit(EnumDefinition const& _enum)
|
|
|
|
{
|
|
|
|
if (_enum.members().size() > 256)
|
|
|
|
m_errorReporter.declarationError(
|
|
|
|
1611_error,
|
|
|
|
_enum.location(),
|
|
|
|
"Enum with more than 256 members is not allowed."
|
|
|
|
);
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-04-14 14:36:37 +00:00
|
|
|
bool DeclarationTypeChecker::visit(StructDefinition const& _struct)
|
|
|
|
{
|
|
|
|
if (_struct.annotation().recursive.has_value())
|
|
|
|
{
|
|
|
|
if (!m_currentStructsSeen.empty() && *_struct.annotation().recursive)
|
|
|
|
m_recursiveStructSeen = true;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (m_currentStructsSeen.count(&_struct))
|
|
|
|
{
|
|
|
|
_struct.annotation().recursive = true;
|
|
|
|
m_recursiveStructSeen = true;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool previousRecursiveStructSeen = m_recursiveStructSeen;
|
|
|
|
bool hasRecursiveChild = false;
|
|
|
|
|
|
|
|
m_currentStructsSeen.insert(&_struct);
|
|
|
|
|
2020-04-15 09:37:39 +00:00
|
|
|
for (auto const& member: _struct.members())
|
2020-04-14 14:36:37 +00:00
|
|
|
{
|
|
|
|
m_recursiveStructSeen = false;
|
2020-04-15 09:37:39 +00:00
|
|
|
member->accept(*this);
|
|
|
|
solAssert(member->annotation().type, "");
|
2020-04-14 14:36:37 +00:00
|
|
|
if (m_recursiveStructSeen)
|
|
|
|
hasRecursiveChild = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!_struct.annotation().recursive.has_value())
|
|
|
|
_struct.annotation().recursive = hasRecursiveChild;
|
|
|
|
m_recursiveStructSeen = previousRecursiveStructSeen || *_struct.annotation().recursive;
|
|
|
|
m_currentStructsSeen.erase(&_struct);
|
|
|
|
if (m_currentStructsSeen.empty())
|
|
|
|
m_recursiveStructSeen = false;
|
|
|
|
|
2020-04-15 09:37:39 +00:00
|
|
|
// Check direct recursion, fatal error if detected.
|
|
|
|
auto visitor = [&](StructDefinition const& _struct, auto& _cycleDetector, size_t _depth)
|
|
|
|
{
|
|
|
|
if (_depth >= 256)
|
2020-05-21 15:38:47 +00:00
|
|
|
m_errorReporter.fatalDeclarationError(
|
|
|
|
5651_error,
|
|
|
|
_struct.location(),
|
|
|
|
"Struct definition exhausts cyclic dependency validator."
|
|
|
|
);
|
2020-04-15 09:37:39 +00:00
|
|
|
|
|
|
|
for (ASTPointer<VariableDeclaration> const& member: _struct.members())
|
|
|
|
{
|
|
|
|
Type const* memberType = member->annotation().type;
|
2020-06-03 10:38:35 +00:00
|
|
|
|
|
|
|
if (auto arrayType = dynamic_cast<ArrayType const*>(memberType))
|
|
|
|
memberType = arrayType->finalBaseType(true);
|
|
|
|
|
2020-04-15 09:37:39 +00:00
|
|
|
if (auto structType = dynamic_cast<StructType const*>(memberType))
|
|
|
|
if (_cycleDetector.run(structType->structDefinition()))
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
};
|
2020-06-03 10:38:35 +00:00
|
|
|
if (util::CycleDetector<StructDefinition>(visitor).run(_struct))
|
2020-05-20 21:56:25 +00:00
|
|
|
m_errorReporter.fatalTypeError(2046_error, _struct.location(), "Recursive struct definition.");
|
2020-04-15 09:37:39 +00:00
|
|
|
|
2020-04-14 14:36:37 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-08-16 12:43:05 +00:00
|
|
|
void DeclarationTypeChecker::endVisit(UserDefinedValueTypeDefinition const& _userDefined)
|
|
|
|
{
|
|
|
|
TypeName const* typeName = _userDefined.underlyingType();
|
|
|
|
solAssert(typeName, "");
|
|
|
|
if (!dynamic_cast<ElementaryTypeName const*>(typeName))
|
|
|
|
m_errorReporter.fatalTypeError(
|
|
|
|
8657_error,
|
|
|
|
typeName->location(),
|
|
|
|
"The underlying type for a user defined value type has to be an elementary value type."
|
|
|
|
);
|
|
|
|
|
|
|
|
Type const* type = typeName->annotation().type;
|
|
|
|
solAssert(type, "");
|
|
|
|
solAssert(!dynamic_cast<UserDefinedValueType const*>(type), "");
|
|
|
|
if (!type->isValueType())
|
|
|
|
m_errorReporter.typeError(
|
|
|
|
8129_error,
|
|
|
|
_userDefined.location(),
|
|
|
|
"The underlying type of the user defined value type \"" +
|
|
|
|
_userDefined.name() +
|
|
|
|
"\" is not a value type."
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-04-07 17:31:48 +00:00
|
|
|
void DeclarationTypeChecker::endVisit(UserDefinedTypeName const& _typeName)
|
|
|
|
{
|
2020-04-14 14:36:37 +00:00
|
|
|
if (_typeName.annotation().type)
|
|
|
|
return;
|
|
|
|
|
2020-08-11 09:18:22 +00:00
|
|
|
Declaration const* declaration = _typeName.pathNode().annotation().referencedDeclaration;
|
2020-04-07 17:31:48 +00:00
|
|
|
solAssert(declaration, "");
|
|
|
|
|
|
|
|
if (StructDefinition const* structDef = dynamic_cast<StructDefinition const*>(declaration))
|
2020-04-14 14:36:37 +00:00
|
|
|
{
|
|
|
|
if (!m_insideFunctionType && !m_currentStructsSeen.empty())
|
|
|
|
structDef->accept(*this);
|
2020-04-07 17:31:48 +00:00
|
|
|
_typeName.annotation().type = TypeProvider::structType(*structDef, DataLocation::Storage);
|
2020-04-14 14:36:37 +00:00
|
|
|
}
|
2020-04-07 17:31:48 +00:00
|
|
|
else if (EnumDefinition const* enumDef = dynamic_cast<EnumDefinition const*>(declaration))
|
|
|
|
_typeName.annotation().type = TypeProvider::enumType(*enumDef);
|
|
|
|
else if (ContractDefinition const* contract = dynamic_cast<ContractDefinition const*>(declaration))
|
|
|
|
_typeName.annotation().type = TypeProvider::contract(*contract);
|
2021-08-16 12:43:05 +00:00
|
|
|
else if (auto userDefinedValueType = dynamic_cast<UserDefinedValueTypeDefinition const*>(declaration))
|
|
|
|
_typeName.annotation().type = TypeProvider::userDefinedValueType(*userDefinedValueType);
|
2020-04-07 17:31:48 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
_typeName.annotation().type = TypeProvider::emptyTuple();
|
2020-05-21 15:38:47 +00:00
|
|
|
m_errorReporter.fatalTypeError(
|
2020-05-26 12:37:31 +00:00
|
|
|
5172_error,
|
2020-05-21 15:38:47 +00:00
|
|
|
_typeName.location(),
|
2022-08-01 08:42:58 +00:00
|
|
|
"Name has to refer to a user-defined type."
|
2020-05-21 15:38:47 +00:00
|
|
|
);
|
2020-04-07 17:31:48 +00:00
|
|
|
}
|
|
|
|
}
|
2020-05-21 15:38:47 +00:00
|
|
|
|
2020-10-19 17:14:26 +00:00
|
|
|
void DeclarationTypeChecker::endVisit(IdentifierPath const& _path)
|
|
|
|
{
|
|
|
|
Declaration const* declaration = _path.annotation().referencedDeclaration;
|
|
|
|
solAssert(declaration, "");
|
|
|
|
|
|
|
|
if (ContractDefinition const* contract = dynamic_cast<ContractDefinition const*>(declaration))
|
|
|
|
if (contract->isLibrary())
|
|
|
|
m_errorReporter.typeError(1130_error, _path.location(), "Invalid use of a library name.");
|
|
|
|
}
|
|
|
|
|
2020-04-14 14:36:37 +00:00
|
|
|
bool DeclarationTypeChecker::visit(FunctionTypeName const& _typeName)
|
2020-04-07 17:31:48 +00:00
|
|
|
{
|
2020-04-14 14:36:37 +00:00
|
|
|
if (_typeName.annotation().type)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
bool previousInsideFunctionType = m_insideFunctionType;
|
|
|
|
m_insideFunctionType = true;
|
|
|
|
_typeName.parameterTypeList()->accept(*this);
|
|
|
|
_typeName.returnParameterTypeList()->accept(*this);
|
|
|
|
m_insideFunctionType = previousInsideFunctionType;
|
|
|
|
|
2020-04-07 17:31:48 +00:00
|
|
|
switch (_typeName.visibility())
|
|
|
|
{
|
|
|
|
case Visibility::Internal:
|
|
|
|
case Visibility::External:
|
|
|
|
break;
|
|
|
|
default:
|
2020-05-21 15:38:47 +00:00
|
|
|
m_errorReporter.fatalTypeError(
|
2020-05-26 12:37:31 +00:00
|
|
|
6012_error,
|
2020-05-21 15:38:47 +00:00
|
|
|
_typeName.location(),
|
|
|
|
"Invalid visibility, can only be \"external\" or \"internal\"."
|
|
|
|
);
|
2020-04-14 14:36:37 +00:00
|
|
|
return false;
|
2020-04-07 17:31:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (_typeName.isPayable() && _typeName.visibility() != Visibility::External)
|
|
|
|
{
|
2020-05-21 15:38:47 +00:00
|
|
|
m_errorReporter.fatalTypeError(
|
2020-05-26 12:37:31 +00:00
|
|
|
7415_error,
|
2020-05-21 15:38:47 +00:00
|
|
|
_typeName.location(),
|
|
|
|
"Only external function types can be payable."
|
|
|
|
);
|
2020-04-14 14:36:37 +00:00
|
|
|
return false;
|
2020-04-07 17:31:48 +00:00
|
|
|
}
|
|
|
|
_typeName.annotation().type = TypeProvider::function(_typeName);
|
2020-04-14 14:36:37 +00:00
|
|
|
return false;
|
2020-04-07 17:31:48 +00:00
|
|
|
}
|
2020-05-21 15:38:47 +00:00
|
|
|
|
2020-04-07 17:31:48 +00:00
|
|
|
void DeclarationTypeChecker::endVisit(Mapping const& _mapping)
|
|
|
|
{
|
2020-04-14 14:36:37 +00:00
|
|
|
if (_mapping.annotation().type)
|
|
|
|
return;
|
|
|
|
|
2020-04-07 17:31:48 +00:00
|
|
|
if (auto const* typeName = dynamic_cast<UserDefinedTypeName const*>(&_mapping.keyType()))
|
2020-10-16 15:25:37 +00:00
|
|
|
switch (typeName->annotation().type->category())
|
2020-04-07 17:31:48 +00:00
|
|
|
{
|
2020-10-16 15:25:37 +00:00
|
|
|
case Type::Category::Enum:
|
|
|
|
case Type::Category::Contract:
|
2021-09-13 08:02:27 +00:00
|
|
|
case Type::Category::UserDefinedValueType:
|
2020-10-16 15:25:37 +00:00
|
|
|
break;
|
|
|
|
default:
|
2020-04-07 17:31:48 +00:00
|
|
|
m_errorReporter.fatalTypeError(
|
2020-10-16 15:25:37 +00:00
|
|
|
7804_error,
|
2020-04-07 17:31:48 +00:00
|
|
|
typeName->location(),
|
2021-09-13 08:02:27 +00:00
|
|
|
"Only elementary types, user defined value types, contract types or enums are allowed as mapping keys."
|
2020-04-07 17:31:48 +00:00
|
|
|
);
|
2020-10-16 15:25:37 +00:00
|
|
|
break;
|
2020-04-07 17:31:48 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
solAssert(dynamic_cast<ElementaryTypeName const*>(&_mapping.keyType()), "");
|
|
|
|
|
2021-03-22 16:12:05 +00:00
|
|
|
Type const* keyType = _mapping.keyType().annotation().type;
|
2022-08-11 11:35:59 +00:00
|
|
|
ASTString keyName = _mapping.keyName();
|
|
|
|
|
2021-03-22 16:12:05 +00:00
|
|
|
Type const* valueType = _mapping.valueType().annotation().type;
|
2022-08-11 11:35:59 +00:00
|
|
|
ASTString valueName = _mapping.valueName();
|
2020-04-07 17:31:48 +00:00
|
|
|
|
|
|
|
// Convert key type to memory.
|
|
|
|
keyType = TypeProvider::withLocationIfReference(DataLocation::Memory, keyType);
|
|
|
|
|
|
|
|
// Convert value type to storage reference.
|
|
|
|
valueType = TypeProvider::withLocationIfReference(DataLocation::Storage, valueType);
|
2022-08-11 11:35:59 +00:00
|
|
|
_mapping.annotation().type = TypeProvider::mapping(keyType, keyName, valueType, valueName);
|
|
|
|
|
|
|
|
// Check if parameter names are conflicting.
|
|
|
|
if (!keyName.empty())
|
|
|
|
{
|
|
|
|
auto childMappingType = dynamic_cast<MappingType const*>(valueType);
|
|
|
|
ASTString currentValueName = valueName;
|
|
|
|
bool loop = true;
|
|
|
|
while (loop)
|
|
|
|
{
|
|
|
|
bool isError = false;
|
|
|
|
// Value type is a mapping.
|
|
|
|
if (childMappingType)
|
|
|
|
{
|
|
|
|
// Compare top mapping's key name with child mapping's key name.
|
|
|
|
ASTString childKeyName = childMappingType->keyName();
|
|
|
|
if (keyName == childKeyName)
|
|
|
|
isError = true;
|
|
|
|
|
|
|
|
auto valueType = childMappingType->valueType();
|
|
|
|
currentValueName = childMappingType->valueName();
|
|
|
|
childMappingType = dynamic_cast<MappingType const*>(valueType);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Compare top mapping's key name with the value name.
|
|
|
|
if (keyName == currentValueName)
|
|
|
|
isError = true;
|
|
|
|
|
|
|
|
loop = false; // We arrived at the end of mapping recursion.
|
|
|
|
}
|
|
|
|
|
|
|
|
// Report error.
|
|
|
|
if (isError)
|
|
|
|
{
|
|
|
|
m_errorReporter.declarationError(
|
|
|
|
1809_error,
|
|
|
|
_mapping.location(),
|
|
|
|
"Conflicting parameter name \"" + keyName + "\" in mapping."
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-04-07 17:31:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void DeclarationTypeChecker::endVisit(ArrayTypeName const& _typeName)
|
|
|
|
{
|
2020-04-14 14:36:37 +00:00
|
|
|
if (_typeName.annotation().type)
|
|
|
|
return;
|
|
|
|
|
2021-03-22 16:12:05 +00:00
|
|
|
Type const* baseType = _typeName.baseType().annotation().type;
|
2020-04-07 17:31:48 +00:00
|
|
|
if (!baseType)
|
|
|
|
{
|
|
|
|
solAssert(!m_errorReporter.errors().empty(), "");
|
|
|
|
return;
|
|
|
|
}
|
2020-10-16 15:25:37 +00:00
|
|
|
|
2020-04-07 17:31:48 +00:00
|
|
|
if (Expression const* length = _typeName.length())
|
|
|
|
{
|
2020-11-16 11:19:52 +00:00
|
|
|
optional<rational> lengthValue;
|
|
|
|
if (length->annotation().type && length->annotation().type->category() == Type::Category::RationalNumber)
|
|
|
|
lengthValue = dynamic_cast<RationalNumberType const&>(*length->annotation().type).value();
|
|
|
|
else if (optional<ConstantEvaluator::TypedRational> value = ConstantEvaluator::evaluate(m_errorReporter, *length))
|
|
|
|
lengthValue = value->value;
|
|
|
|
|
2022-04-01 01:36:45 +00:00
|
|
|
if (!lengthValue)
|
2020-05-21 15:38:47 +00:00
|
|
|
m_errorReporter.typeError(
|
2020-05-26 12:37:31 +00:00
|
|
|
5462_error,
|
2020-05-21 15:38:47 +00:00
|
|
|
length->location(),
|
|
|
|
"Invalid array length, expected integer literal or constant expression."
|
|
|
|
);
|
2020-11-16 11:19:52 +00:00
|
|
|
else if (*lengthValue == 0)
|
2020-05-26 12:37:31 +00:00
|
|
|
m_errorReporter.typeError(1406_error, length->location(), "Array with zero length specified.");
|
2020-11-16 11:19:52 +00:00
|
|
|
else if (lengthValue->denominator() != 1)
|
2020-05-26 12:37:31 +00:00
|
|
|
m_errorReporter.typeError(3208_error, length->location(), "Array with fractional length specified.");
|
2020-11-16 11:19:52 +00:00
|
|
|
else if (*lengthValue < 0)
|
2020-05-26 12:37:31 +00:00
|
|
|
m_errorReporter.typeError(3658_error, length->location(), "Array with negative length specified.");
|
2022-04-01 01:36:45 +00:00
|
|
|
else if (lengthValue > TypeProvider::uint256()->max())
|
|
|
|
m_errorReporter.typeError(
|
|
|
|
1847_error,
|
|
|
|
length->location(),
|
|
|
|
"Array length too large, maximum is 2**256 - 1."
|
|
|
|
);
|
2020-11-16 11:19:52 +00:00
|
|
|
|
|
|
|
_typeName.annotation().type = TypeProvider::array(
|
|
|
|
DataLocation::Storage,
|
|
|
|
baseType,
|
|
|
|
lengthValue ? u256(lengthValue->numerator()) : u256(0)
|
|
|
|
);
|
2020-04-07 17:31:48 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
_typeName.annotation().type = TypeProvider::array(DataLocation::Storage, baseType);
|
|
|
|
}
|
2020-05-21 15:38:47 +00:00
|
|
|
|
2020-04-07 17:31:48 +00:00
|
|
|
void DeclarationTypeChecker::endVisit(VariableDeclaration const& _variable)
|
|
|
|
{
|
|
|
|
if (_variable.annotation().type)
|
|
|
|
return;
|
|
|
|
|
2020-09-25 10:15:31 +00:00
|
|
|
if (_variable.isFileLevelVariable() && !_variable.isConstant())
|
|
|
|
m_errorReporter.declarationError(
|
|
|
|
8342_error,
|
|
|
|
_variable.location(),
|
|
|
|
"Only constant variables are allowed at file level."
|
|
|
|
);
|
|
|
|
if (_variable.isConstant() && (!_variable.isStateVariable() && !_variable.isFileLevelVariable()))
|
2020-05-21 15:38:47 +00:00
|
|
|
m_errorReporter.declarationError(
|
|
|
|
1788_error,
|
|
|
|
_variable.location(),
|
2020-09-25 10:15:31 +00:00
|
|
|
"The \"constant\" keyword can only be used for state variables or variables at file level."
|
2020-05-21 15:38:47 +00:00
|
|
|
);
|
2020-04-07 17:31:48 +00:00
|
|
|
if (_variable.immutable() && !_variable.isStateVariable())
|
2020-05-21 15:38:47 +00:00
|
|
|
m_errorReporter.declarationError(
|
|
|
|
8297_error,
|
|
|
|
_variable.location(),
|
|
|
|
"The \"immutable\" keyword can only be used for state variables."
|
|
|
|
);
|
2020-04-07 17:31:48 +00:00
|
|
|
|
|
|
|
using Location = VariableDeclaration::Location;
|
|
|
|
Location varLoc = _variable.referenceLocation();
|
|
|
|
DataLocation typeLoc = DataLocation::Memory;
|
|
|
|
|
|
|
|
set<Location> allowedDataLocations = _variable.allowedDataLocations();
|
|
|
|
if (!allowedDataLocations.count(varLoc))
|
|
|
|
{
|
|
|
|
auto locationToString = [](VariableDeclaration::Location _location) -> string
|
|
|
|
{
|
|
|
|
switch (_location)
|
|
|
|
{
|
|
|
|
case Location::Memory: return "\"memory\"";
|
|
|
|
case Location::Storage: return "\"storage\"";
|
|
|
|
case Location::CallData: return "\"calldata\"";
|
|
|
|
case Location::Unspecified: return "none";
|
|
|
|
}
|
|
|
|
return {};
|
|
|
|
};
|
|
|
|
|
|
|
|
string errorString;
|
|
|
|
if (!_variable.hasReferenceOrMappingType())
|
|
|
|
errorString = "Data location can only be specified for array, struct or mapping types";
|
|
|
|
else
|
|
|
|
{
|
|
|
|
errorString = "Data location must be " +
|
|
|
|
util::joinHumanReadable(
|
2021-04-08 12:08:14 +00:00
|
|
|
allowedDataLocations | ranges::views::transform(locationToString),
|
2020-04-07 17:31:48 +00:00
|
|
|
", ",
|
|
|
|
" or "
|
|
|
|
);
|
2020-07-27 10:33:03 +00:00
|
|
|
if (_variable.isConstructorParameter())
|
|
|
|
errorString += " for constructor parameter";
|
|
|
|
else if (_variable.isCallableOrCatchParameter())
|
2020-04-07 17:31:48 +00:00
|
|
|
errorString +=
|
|
|
|
" for " +
|
|
|
|
string(_variable.isReturnParameter() ? "return " : "") +
|
|
|
|
"parameter in" +
|
|
|
|
string(_variable.isExternalCallableParameter() ? " external" : "") +
|
|
|
|
" function";
|
|
|
|
else
|
|
|
|
errorString += " for variable";
|
|
|
|
}
|
|
|
|
errorString += ", but " + locationToString(varLoc) + " was given.";
|
2020-05-26 12:37:31 +00:00
|
|
|
m_errorReporter.typeError(6651_error, _variable.location(), errorString);
|
2020-04-07 17:31:48 +00:00
|
|
|
|
|
|
|
solAssert(!allowedDataLocations.empty(), "");
|
|
|
|
varLoc = *allowedDataLocations.begin();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Find correct data location.
|
2021-01-28 11:56:22 +00:00
|
|
|
if (_variable.isEventOrErrorParameter())
|
2020-04-07 17:31:48 +00:00
|
|
|
{
|
|
|
|
solAssert(varLoc == Location::Unspecified, "");
|
|
|
|
typeLoc = DataLocation::Memory;
|
|
|
|
}
|
2020-09-25 10:15:31 +00:00
|
|
|
else if (_variable.isFileLevelVariable())
|
|
|
|
{
|
|
|
|
solAssert(varLoc == Location::Unspecified, "");
|
|
|
|
typeLoc = DataLocation::Memory;
|
|
|
|
}
|
2020-04-07 17:31:48 +00:00
|
|
|
else if (_variable.isStateVariable())
|
|
|
|
{
|
|
|
|
solAssert(varLoc == Location::Unspecified, "");
|
|
|
|
typeLoc = (_variable.isConstant() || _variable.immutable()) ? DataLocation::Memory : DataLocation::Storage;
|
|
|
|
}
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
case Location::Memory:
|
|
|
|
typeLoc = DataLocation::Memory;
|
|
|
|
break;
|
|
|
|
case Location::Storage:
|
|
|
|
typeLoc = DataLocation::Storage;
|
|
|
|
break;
|
|
|
|
case Location::CallData:
|
|
|
|
typeLoc = DataLocation::CallData;
|
|
|
|
break;
|
|
|
|
case Location::Unspecified:
|
|
|
|
solAssert(!_variable.hasReferenceOrMappingType(), "Data location not properly set.");
|
|
|
|
}
|
|
|
|
|
2021-03-22 16:12:05 +00:00
|
|
|
Type const* type = _variable.typeName().annotation().type;
|
2020-04-07 17:31:48 +00:00
|
|
|
if (auto ref = dynamic_cast<ReferenceType const*>(type))
|
|
|
|
{
|
|
|
|
bool isPointer = !_variable.isStateVariable();
|
|
|
|
type = TypeProvider::withLocation(ref, typeLoc, isPointer);
|
|
|
|
}
|
|
|
|
|
2022-01-12 13:12:03 +00:00
|
|
|
if (_variable.isConstant() && !type->isValueType())
|
|
|
|
{
|
|
|
|
bool allowed = false;
|
|
|
|
if (auto arrayType = dynamic_cast<ArrayType const*>(type))
|
2022-01-28 17:57:05 +00:00
|
|
|
allowed = arrayType->isByteArrayOrString();
|
2022-01-12 13:12:03 +00:00
|
|
|
if (!allowed)
|
|
|
|
m_errorReporter.fatalTypeError(9259_error, _variable.location(), "Only constants of value type and byte array type are implemented.");
|
|
|
|
}
|
2021-01-06 22:27:20 +00:00
|
|
|
|
2020-04-07 17:31:48 +00:00
|
|
|
_variable.annotation().type = type;
|
|
|
|
}
|
|
|
|
|
2020-10-16 15:25:37 +00:00
|
|
|
bool DeclarationTypeChecker::visit(UsingForDirective const& _usingFor)
|
2020-06-17 15:44:48 +00:00
|
|
|
{
|
2021-10-11 08:16:52 +00:00
|
|
|
if (_usingFor.usesBraces())
|
|
|
|
{
|
|
|
|
for (ASTPointer<IdentifierPath> const& function: _usingFor.functionsOrLibrary())
|
|
|
|
if (auto functionDefinition = dynamic_cast<FunctionDefinition const*>(function->annotation().referencedDeclaration))
|
|
|
|
{
|
|
|
|
if (!functionDefinition->isFree() && !(
|
|
|
|
dynamic_cast<ContractDefinition const*>(functionDefinition->scope()) &&
|
|
|
|
dynamic_cast<ContractDefinition const*>(functionDefinition->scope())->isLibrary()
|
|
|
|
))
|
|
|
|
m_errorReporter.typeError(
|
|
|
|
4167_error,
|
|
|
|
function->location(),
|
2022-08-30 09:53:02 +00:00
|
|
|
"Only file-level functions and library functions can be attached to a type in a \"using\" statement"
|
2021-10-11 08:16:52 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
m_errorReporter.fatalTypeError(8187_error, function->location(), "Expected function name." );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ContractDefinition const* library = dynamic_cast<ContractDefinition const*>(
|
|
|
|
_usingFor.functionsOrLibrary().front()->annotation().referencedDeclaration
|
|
|
|
);
|
|
|
|
if (!library || !library->isLibrary())
|
|
|
|
m_errorReporter.fatalTypeError(
|
|
|
|
4357_error,
|
|
|
|
_usingFor.functionsOrLibrary().front()->location(),
|
|
|
|
"Library name expected. If you want to attach a function, use '{...}'."
|
|
|
|
);
|
|
|
|
}
|
2020-10-16 15:25:37 +00:00
|
|
|
|
2021-10-11 08:16:52 +00:00
|
|
|
// We do not visit _usingFor.functions() because it will lead to an error since
|
|
|
|
// library names cannot be mentioned stand-alone.
|
2020-10-16 15:25:37 +00:00
|
|
|
|
|
|
|
if (_usingFor.typeName())
|
|
|
|
_usingFor.typeName()->accept(*this);
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool DeclarationTypeChecker::visit(InheritanceSpecifier const& _inheritanceSpecifier)
|
|
|
|
{
|
|
|
|
auto const* contract = dynamic_cast<ContractDefinition const*>(_inheritanceSpecifier.name().annotation().referencedDeclaration);
|
|
|
|
solAssert(contract, "");
|
|
|
|
if (contract->isLibrary())
|
|
|
|
{
|
|
|
|
m_errorReporter.typeError(
|
|
|
|
2571_error,
|
|
|
|
_inheritanceSpecifier.name().location(),
|
|
|
|
"Libraries cannot be inherited from."
|
|
|
|
);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
2020-06-17 15:44:48 +00:00
|
|
|
}
|
|
|
|
|
2020-04-07 17:31:48 +00:00
|
|
|
bool DeclarationTypeChecker::check(ASTNode const& _node)
|
|
|
|
{
|
2020-05-20 21:56:25 +00:00
|
|
|
auto watcher = m_errorReporter.errorWatcher();
|
2020-04-07 17:31:48 +00:00
|
|
|
_node.accept(*this);
|
2020-05-20 21:56:25 +00:00
|
|
|
return watcher.ok();
|
2020-04-07 17:31:48 +00:00
|
|
|
}
|