2015-09-16 14:56:30 +00:00
|
|
|
/*
|
|
|
|
This file is part of cpp-ethereum.
|
|
|
|
|
|
|
|
cpp-ethereum 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.
|
|
|
|
|
|
|
|
cpp-ethereum 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 cpp-ethereum. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
/**
|
|
|
|
* @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>
|
2016-03-01 21:56:39 +00:00
|
|
|
#include <libsolidity/inlineasm/AsmCodeGen.h>
|
|
|
|
#include <libsolidity/inlineasm/AsmData.h>
|
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
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
_root.accept(*this);
|
|
|
|
}
|
2015-11-26 13:47:28 +00:00
|
|
|
catch (FatalError const&)
|
2015-11-06 20:07:42 +00:00
|
|
|
{
|
|
|
|
solAssert(m_errorOccurred, "");
|
|
|
|
}
|
|
|
|
return !m_errorOccurred;
|
|
|
|
}
|
|
|
|
|
2015-09-16 14:56:30 +00:00
|
|
|
bool ReferencesResolver::visit(Identifier const& _identifier)
|
|
|
|
{
|
|
|
|
auto declarations = m_resolver.nameFromCurrentScope(_identifier.name());
|
|
|
|
if (declarations.empty())
|
2015-11-06 19:56:14 +00:00
|
|
|
fatalDeclarationError(_identifier.location(), "Undeclared identifier.");
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ReferencesResolver::endVisit(UserDefinedTypeName const& _typeName)
|
|
|
|
{
|
|
|
|
Declaration const* declaration = m_resolver.pathFromCurrentScope(_typeName.namePath());
|
|
|
|
if (!declaration)
|
|
|
|
fatalDeclarationError(_typeName.location(), "Identifier not found or not unique.");
|
|
|
|
|
|
|
|
_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
|
|
|
|
fatalTypeError(_typeName.location(), "Name has to refer to a struct, enum or contract.");
|
|
|
|
}
|
|
|
|
|
2016-09-27 19:37:32 +00:00
|
|
|
void ReferencesResolver::endVisit(FunctionTypeName const& _typeName)
|
|
|
|
{
|
|
|
|
switch (_typeName.visibility())
|
|
|
|
{
|
|
|
|
case VariableDeclaration::Visibility::Default:
|
|
|
|
case VariableDeclaration::Visibility::Internal:
|
|
|
|
case VariableDeclaration::Visibility::External:
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
typeError(_typeName.location(), "Invalid visibility, can only be \"external\" or \"internal\".");
|
|
|
|
}
|
|
|
|
|
|
|
|
// Do we allow storage references for external functions?
|
|
|
|
|
|
|
|
_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;
|
|
|
|
if (baseType->storageBytes() == 0)
|
|
|
|
fatalTypeError(_typeName.baseType().location(), "Illegal base type of storage size zero for array.");
|
|
|
|
if (Expression const* length = _typeName.length())
|
|
|
|
{
|
|
|
|
if (!length->annotation().type)
|
|
|
|
ConstantEvaluator e(*length);
|
2016-03-29 20:56:26 +00:00
|
|
|
auto const* lengthType = dynamic_cast<RationalNumberType const*>(length->annotation().type.get());
|
2016-05-10 12:30:24 +00:00
|
|
|
if (!lengthType || lengthType->isFractional())
|
2016-04-12 17:36:34 +00:00
|
|
|
fatalTypeError(length->location(), "Invalid array length, expected integer literal.");
|
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)
|
|
|
|
{
|
|
|
|
// We need to perform a full code generation pass here as inline assembly does not distinguish
|
|
|
|
// reference resolution and code generation.
|
|
|
|
// 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.
|
|
|
|
ErrorList errorsIgnored;
|
|
|
|
assembly::CodeGenerator codeGen(_inlineAssembly.operations(), errorsIgnored);
|
|
|
|
codeGen.typeCheck([&](assembly::Identifier const& _identifier, eth::Assembly&, assembly::CodeGenerator::IdentifierContext) {
|
|
|
|
auto declarations = m_resolver.nameFromCurrentScope(_identifier.name);
|
|
|
|
if (declarations.size() != 1)
|
|
|
|
return false;
|
|
|
|
_inlineAssembly.annotation().externalReferences[&_identifier] = declarations.front();
|
|
|
|
// At this stage we neither know the code to generate nor the stack size of the identifier,
|
|
|
|
// so we do not modify assembly.
|
|
|
|
return true;
|
|
|
|
});
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-11-26 10:37:29 +00:00
|
|
|
bool ReferencesResolver::visit(Return const& _return)
|
|
|
|
{
|
|
|
|
_return.annotation().functionReturnParameters = m_returnParameters;
|
|
|
|
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
|
2015-11-30 18:42:39 +00:00
|
|
|
if (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
|
2015-11-30 18:42:39 +00:00
|
|
|
if (varLoc == Location::Storage && !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 "
|
|
|
|
"(remove the \"storage\" 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
|
|
|
|
typeLoc = varLoc == Location::Memory ? DataLocation::Memory : DataLocation::Storage;
|
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(),
|
|
|
|
"Storage location has to be \"memory\" (or unspecified) for constants."
|
|
|
|
);
|
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)
|
|
|
|
typeLoc = _variable.isCallableParameter() ? DataLocation::Memory : DataLocation::Storage;
|
|
|
|
else
|
|
|
|
typeLoc = varLoc == Location::Memory ? DataLocation::Memory : DataLocation::Storage;
|
|
|
|
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
|
|
|
}
|
2015-11-30 18:42:39 +00:00
|
|
|
else if (varLoc != Location::Default && !ref)
|
2015-11-06 19:56:14 +00:00
|
|
|
fatalTypeError(_variable.location(), "Storage location can only be given for array or struct types.");
|
2015-09-16 14:56:30 +00:00
|
|
|
|
|
|
|
if (!type)
|
2015-11-06 19:56:14 +00:00
|
|
|
fatalTypeError(_variable.location(), "Invalid type name.");
|
2015-09-16 14:56:30 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
else if (!_variable.canHaveAutoType())
|
2015-11-06 19:56:14 +00:00
|
|
|
fatalTypeError(_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
|
|
|
|
|
|
|
|
_variable.annotation().type = type;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
{
|
|
|
|
auto err = make_shared<Error>(Error::Type::TypeError);
|
2015-11-04 10:49:26 +00:00
|
|
|
*err << errinfo_sourceLocation(_location) << errinfo_comment(_description);
|
2015-11-06 16:22:46 +00:00
|
|
|
m_errorOccurred = true;
|
2015-10-26 16:31:35 +00:00
|
|
|
m_errors.push_back(err);
|
|
|
|
}
|
|
|
|
|
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 19:56:14 +00:00
|
|
|
typeError(_location, _description);
|
2015-10-26 16:31:35 +00:00
|
|
|
BOOST_THROW_EXCEPTION(FatalError());
|
|
|
|
}
|
|
|
|
|
2015-11-06 19:56:14 +00:00
|
|
|
void ReferencesResolver::declarationError(SourceLocation const& _location, string const& _description)
|
2015-11-06 16:22:46 +00:00
|
|
|
{
|
|
|
|
auto err = make_shared<Error>(Error::Type::DeclarationError);
|
|
|
|
*err << errinfo_sourceLocation(_location) << errinfo_comment(_description);
|
|
|
|
m_errorOccurred = true;
|
|
|
|
m_errors.push_back(err);
|
|
|
|
}
|
|
|
|
|
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
|
|
|
{
|
2015-11-06 19:56:14 +00:00
|
|
|
declarationError(_location, _description);
|
2015-11-06 16:22:46 +00:00
|
|
|
BOOST_THROW_EXCEPTION(FatalError());
|
|
|
|
}
|
2015-10-26 16:31:35 +00:00
|
|
|
|