added errors tu ReferencesResolver

This commit is contained in:
LianaHus 2015-10-26 17:31:35 +01:00
parent 48ffa08783
commit 1a27bf8a39
3 changed files with 78 additions and 55 deletions

View File

@ -64,44 +64,52 @@ bool NameAndTypeResolver::resolveNamesAndTypes(ContractDefinition& _contract)
{ {
m_currentScope = &m_scopes[nullptr]; m_currentScope = &m_scopes[nullptr];
ReferencesResolver resolver(m_errors, *this, &_contract, nullptr);
bool result = true;
for (ASTPointer<InheritanceSpecifier> const& baseContract: _contract.baseContracts()) for (ASTPointer<InheritanceSpecifier> const& baseContract: _contract.baseContracts())
ReferencesResolver resolver(*baseContract, *this, &_contract, nullptr); result = resolver.resolve(*baseContract);
m_currentScope = &m_scopes[&_contract]; m_currentScope = &m_scopes[&_contract];
linearizeBaseContracts(_contract); if (result)
std::vector<ContractDefinition const*> properBases( {
++_contract.annotation().linearizedBaseContracts.begin(),
_contract.annotation().linearizedBaseContracts.end() linearizeBaseContracts(_contract);
); std::vector<ContractDefinition const*> properBases(
++_contract.annotation().linearizedBaseContracts.begin(),
_contract.annotation().linearizedBaseContracts.end()
);
for (ContractDefinition const* base: properBases)
importInheritedScope(*base);
}
for (ContractDefinition const* base: properBases)
importInheritedScope(*base);
for (ASTPointer<StructDefinition> const& structDef: _contract.definedStructs()) for (ASTPointer<StructDefinition> const& structDef: _contract.definedStructs())
ReferencesResolver resolver(*structDef, *this, &_contract, nullptr); result = result && resolver.resolve(*structDef);
for (ASTPointer<EnumDefinition> const& enumDef: _contract.definedEnums()) for (ASTPointer<EnumDefinition> const& enumDef: _contract.definedEnums())
ReferencesResolver resolver(*enumDef, *this, &_contract, nullptr); result = result && resolver.resolve(*enumDef);
for (ASTPointer<VariableDeclaration> const& variable: _contract.stateVariables()) for (ASTPointer<VariableDeclaration> const& variable: _contract.stateVariables())
ReferencesResolver resolver(*variable, *this, &_contract, nullptr); result = result && resolver.resolve(*variable);
for (ASTPointer<EventDefinition> const& event: _contract.events()) for (ASTPointer<EventDefinition> const& event: _contract.events())
ReferencesResolver resolver(*event, *this, &_contract, nullptr); result = result && resolver.resolve(*event);
// these can contain code, only resolve parameters for now // these can contain code, only resolve parameters for now
for (ASTPointer<ModifierDefinition> const& modifier: _contract.functionModifiers()) for (ASTPointer<ModifierDefinition> const& modifier: _contract.functionModifiers())
{ {
m_currentScope = &m_scopes[modifier.get()]; m_currentScope = &m_scopes[modifier.get()];
ReferencesResolver resolver(*modifier, *this, &_contract, nullptr); ReferencesResolver resolver(m_errors);
result = result && resolver.resolve(*modifier);
} }
for (ASTPointer<FunctionDefinition> const& function: _contract.definedFunctions()) for (ASTPointer<FunctionDefinition> const& function: _contract.definedFunctions())
{ {
m_currentScope = &m_scopes[function.get()]; m_currentScope = &m_scopes[function.get()];
ReferencesResolver referencesResolver( ReferencesResolver referencesResolver(
*function, m_errors;
*this, *this,
&_contract, &_contract,
function->returnParameterList().get() function->returnParameterList().get()
); );
result = result && resolver.resolve(*function);
} }
m_currentScope = &m_scopes[&_contract]; m_currentScope = &m_scopes[&_contract];
@ -110,19 +118,22 @@ bool NameAndTypeResolver::resolveNamesAndTypes(ContractDefinition& _contract)
for (ASTPointer<ModifierDefinition> const& modifier: _contract.functionModifiers()) for (ASTPointer<ModifierDefinition> const& modifier: _contract.functionModifiers())
{ {
m_currentScope = &m_scopes[modifier.get()]; m_currentScope = &m_scopes[modifier.get()];
ReferencesResolver resolver(*modifier, *this, &_contract, nullptr, true); ReferencesResolver resolver(m_errors, *this, &_contract, nullptr, true);
result = result && resolver.resolve(*modifier);
} }
for (ASTPointer<FunctionDefinition> const& function: _contract.definedFunctions()) for (ASTPointer<FunctionDefinition> const& function: _contract.definedFunctions())
{ {
m_currentScope = &m_scopes[function.get()]; m_currentScope = &m_scopes[function.get()];
ReferencesResolver referencesResolver( ReferencesResolver referencesResolver(
*function,
*this, *this,
&_contract, &_contract,
function->returnParameterList().get(), function->returnParameterList().get(),
true true
); );
result = result && resolver.resolve(*function);
} }
if (!result)
return false;
} }
catch (FatalError const& _e) catch (FatalError const& _e)
{ {

View File

@ -31,21 +31,6 @@ using namespace dev;
using namespace dev::solidity; using namespace dev::solidity;
ReferencesResolver::ReferencesResolver(
ASTNode& _root,
NameAndTypeResolver& _resolver,
ContractDefinition const* _currentContract,
ParameterList const* _returnParameters,
bool _resolveInsideCode
):
m_resolver(_resolver),
m_currentContract(_currentContract),
m_returnParameters(_returnParameters),
m_resolveInsideCode(_resolveInsideCode)
{
_root.accept(*this);
}
bool ReferencesResolver::visit(Return const& _return) bool ReferencesResolver::visit(Return const& _return)
{ {
_return.annotation().functionReturnParameters = m_returnParameters; _return.annotation().functionReturnParameters = m_returnParameters;
@ -108,19 +93,19 @@ void ReferencesResolver::endVisit(VariableDeclaration const& _variable)
if (contract.isLibrary()) if (contract.isLibrary())
{ {
if (loc == Location::Memory) if (loc == Location::Memory)
BOOST_THROW_EXCEPTION(_variable.createTypeError( fatalTypeError(
"Location has to be calldata or storage for external " "Location has to be calldata or storage for external "
"library functions (remove the \"memory\" keyword)." "library functions (remove the \"memory\" keyword)."
)); );
} }
else else
{ {
// force location of external function parameters (not return) to calldata // force location of external function parameters (not return) to calldata
if (loc != Location::Default) if (loc != Location::Default)
BOOST_THROW_EXCEPTION(_variable.createTypeError( fatalTypeError(
"Location has to be calldata for external functions " "Location has to be calldata for external functions "
"(remove the \"memory\" or \"storage\" keyword)." "(remove the \"memory\" or \"storage\" keyword)."
)); );
} }
if (loc == Location::Default) if (loc == Location::Default)
type = ref->copyForLocation(DataLocation::CallData, true); type = ref->copyForLocation(DataLocation::CallData, true);
@ -130,10 +115,10 @@ void ReferencesResolver::endVisit(VariableDeclaration const& _variable)
auto const& contract = dynamic_cast<ContractDefinition const&>(*_variable.scope()->scope()); auto const& contract = dynamic_cast<ContractDefinition const&>(*_variable.scope()->scope());
// force locations of public or external function (return) parameters to memory // force locations of public or external function (return) parameters to memory
if (loc == Location::Storage && !contract.isLibrary()) if (loc == Location::Storage && !contract.isLibrary())
BOOST_THROW_EXCEPTION(_variable.createTypeError( fatalTypeError(
"Location has to be memory for publicly visible functions " "Location has to be memory for publicly visible functions "
"(remove the \"storage\" keyword)." "(remove the \"storage\" keyword)."
)); );
if (loc == Location::Default || !contract.isLibrary()) if (loc == Location::Default || !contract.isLibrary())
type = ref->copyForLocation(DataLocation::Memory, true); type = ref->copyForLocation(DataLocation::Memory, true);
} }
@ -142,9 +127,7 @@ void ReferencesResolver::endVisit(VariableDeclaration const& _variable)
if (_variable.isConstant()) if (_variable.isConstant())
{ {
if (loc != Location::Default && loc != Location::Memory) if (loc != Location::Default && loc != Location::Memory)
BOOST_THROW_EXCEPTION(_variable.createTypeError( fatalTypeError("Storage location has to be \"memory\" (or unspecified) for constants.");
"Storage location has to be \"memory\" (or unspecified) for constants."
));
loc = Location::Memory; loc = Location::Memory;
} }
if (loc == Location::Default) if (loc == Location::Default)
@ -159,16 +142,14 @@ void ReferencesResolver::endVisit(VariableDeclaration const& _variable)
} }
} }
else if (loc != Location::Default && !ref) else if (loc != Location::Default && !ref)
BOOST_THROW_EXCEPTION(_variable.createTypeError( fatalTypeError("Storage location can only be given for array or struct types.");
"Storage location can only be given for array or struct types."
));
if (!type) if (!type)
BOOST_THROW_EXCEPTION(_variable.typeName()->createTypeError("Invalid type name.")); fatalTypeError("Invalid type name.");
} }
else if (!_variable.canHaveAutoType()) else if (!_variable.canHaveAutoType())
BOOST_THROW_EXCEPTION(_variable.createTypeError("Explicit type needed.")); fatalTypeError("Explicit type needed.");
// otherwise we have a "var"-declaration whose type is resolved by the first assignment // otherwise we have a "var"-declaration whose type is resolved by the first assignment
_variable.annotation().type = type; _variable.annotation().type = type;
@ -194,9 +175,7 @@ TypePointer ReferencesResolver::typeFor(TypeName const& _typeName)
else if (ContractDefinition const* contract = dynamic_cast<ContractDefinition const*>(declaration)) else if (ContractDefinition const* contract = dynamic_cast<ContractDefinition const*>(declaration))
type = make_shared<ContractType>(*contract); type = make_shared<ContractType>(*contract);
else else
BOOST_THROW_EXCEPTION(typeName->createTypeError( fatalTypeError("Name has to refer to a struct, enum or contract.");
"Name has to refer to a struct, enum or contract."
));
} }
else if (auto mapping = dynamic_cast<Mapping const*>(&_typeName)) else if (auto mapping = dynamic_cast<Mapping const*>(&_typeName))
{ {
@ -212,9 +191,7 @@ TypePointer ReferencesResolver::typeFor(TypeName const& _typeName)
{ {
TypePointer baseType = typeFor(arrayType->baseType()); TypePointer baseType = typeFor(arrayType->baseType());
if (baseType->storageBytes() == 0) if (baseType->storageBytes() == 0)
BOOST_THROW_EXCEPTION(arrayType->baseType().createTypeError( fatalTypeError("Illegal base type of storage size zero for array.");
"Illegal base type of storage size zero for array."
));
if (Expression const* length = arrayType->length()) if (Expression const* length = arrayType->length())
{ {
if (!length->annotation().type) if (!length->annotation().type)
@ -222,7 +199,7 @@ TypePointer ReferencesResolver::typeFor(TypeName const& _typeName)
auto const* lengthType = dynamic_cast<IntegerConstantType const*>(length->annotation().type.get()); auto const* lengthType = dynamic_cast<IntegerConstantType const*>(length->annotation().type.get());
if (!lengthType) if (!lengthType)
BOOST_THROW_EXCEPTION(length->createTypeError("Invalid array length.")); fatalTypeError("Invalid array length.");
type = make_shared<ArrayType>(DataLocation::Storage, baseType, lengthType->literalValue(nullptr)); type = make_shared<ArrayType>(DataLocation::Storage, baseType, lengthType->literalValue(nullptr));
} }
else else
@ -232,3 +209,18 @@ TypePointer ReferencesResolver::typeFor(TypeName const& _typeName)
return _typeName.annotation().type = move(type); return _typeName.annotation().type = move(type);
} }
void ReferencesResolver::typeError(string const& _description)
{
auto err = make_shared<Error>(Error::Type::TypeError);
*err << errinfo_comment(_description);
m_errors.push_back(err);
}
void ReferencesResolver::fatalTypeError(string const& _description)
{
typeError(_description);
BOOST_THROW_EXCEPTION(FatalError());
}

View File

@ -43,12 +43,25 @@ class ReferencesResolver: private ASTConstVisitor
{ {
public: public:
ReferencesResolver( ReferencesResolver(
ASTNode& _root, ErrorList& _errors,
NameAndTypeResolver& _resolver, NameAndTypeResolver& _resolver,
ContractDefinition const* _currentContract, ContractDefinition const* _currentContract,
ParameterList const* _returnParameters, ParameterList const* _returnParameters,
bool _resolveInsideCode = false bool _resolveInsideCode = false
); ):
m_errors(_errors),
m_resolver(_resolver),
m_currentContract(_currentContract),
m_returnParameters(_returnParameters),
m_resolveInsideCode(_resolveInsideCode)
{}
/// @returns true if no errors during resolving
bool resolve(ASTNode& _root)
{
_root.accept(*this);
return m_errors.empty();
}
private: private:
virtual bool visit(Block const&) override { return m_resolveInsideCode; } virtual bool visit(Block const&) override { return m_resolveInsideCode; }
@ -59,6 +72,13 @@ private:
TypePointer typeFor(TypeName const& _typeName); TypePointer typeFor(TypeName const& _typeName);
/// Adds a new error to the list of errors.
void typeError(std::string const& _description);
/// Adds a new error to the list of errors and throws to abort type checking.
void fatalTypeError(std::string const& _description);
ErrorList& m_errors;
NameAndTypeResolver& m_resolver; NameAndTypeResolver& m_resolver;
ContractDefinition const* m_currentContract; ContractDefinition const* m_currentContract;
ParameterList const* m_returnParameters; ParameterList const* m_returnParameters;