This commit is contained in:
LianaHus 2015-11-06 17:22:46 +01:00
parent 79177de80b
commit 7eb162c0df
3 changed files with 60 additions and 35 deletions

View File

@ -84,6 +84,9 @@ bool NameAndTypeResolver::resolveNamesAndTypes(ContractDefinition& _contract)
importInheritedScope(*base); importInheritedScope(*base);
} }
if (!success)
return false;
for (ASTPointer<StructDefinition> const& structDef: _contract.definedStructs()) for (ASTPointer<StructDefinition> const& structDef: _contract.definedStructs())
if (!resolver.resolve(*structDef)) if (!resolver.resolve(*structDef))
success = false; success = false;
@ -97,6 +100,8 @@ bool NameAndTypeResolver::resolveNamesAndTypes(ContractDefinition& _contract)
if (!resolver.resolve(*event)) if (!resolver.resolve(*event))
success = false; success = false;
// these can contain code, only resolve parameters for now // these can contain code, only resolve parameters for now
if (!success)
return false;
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()];
@ -104,19 +109,24 @@ bool NameAndTypeResolver::resolveNamesAndTypes(ContractDefinition& _contract)
if (!resolver.resolve(*modifier)) if (!resolver.resolve(*modifier))
success = false; success = false;
} }
if (!success)
return false;
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( if (! ReferencesResolver(
m_errors, m_errors,
*this, *this,
&_contract, &_contract,
function->returnParameterList().get() function->returnParameterList().get()
); ).resolve(*function))
if (!resolver.resolve(*function))
success = false; success = false;
} }
if (!success)
return false;
m_currentScope = &m_scopes[&_contract]; m_currentScope = &m_scopes[&_contract];
// now resolve references inside the code // now resolve references inside the code
@ -127,17 +137,19 @@ bool NameAndTypeResolver::resolveNamesAndTypes(ContractDefinition& _contract)
if (!resolver.resolve(*modifier)) if (!resolver.resolve(*modifier))
success = false; success = false;
} }
if (!success)
return false;
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( if (! ReferencesResolver(
m_errors, m_errors,
*this, *this,
&_contract, &_contract,
function->returnParameterList().get(), function->returnParameterList().get(),
true true
); ).resolve(*function))
if (!resolver.resolve(*function))
success = false; success = false;
} }
if (!success) if (!success)

View File

@ -41,11 +41,8 @@ bool ReferencesResolver::visit(UserDefinedTypeName const& _typeName)
{ {
Declaration const* declaration = m_resolver.pathFromCurrentScope(_typeName.namePath()); Declaration const* declaration = m_resolver.pathFromCurrentScope(_typeName.namePath());
if (!declaration) if (!declaration)
BOOST_THROW_EXCEPTION( DeclarationFatalError(_typeName.location(), "Identifier not found or not unique.");
Error(Error::Type::DeclarationError) <<
errinfo_sourceLocation(_typeName.location()) <<
errinfo_comment("Identifier not found or not unique.")
);
_typeName.annotation().referencedDeclaration = declaration; _typeName.annotation().referencedDeclaration = declaration;
return true; return true;
} }
@ -54,11 +51,7 @@ bool ReferencesResolver::visit(Identifier const& _identifier)
{ {
auto declarations = m_resolver.nameFromCurrentScope(_identifier.name()); auto declarations = m_resolver.nameFromCurrentScope(_identifier.name());
if (declarations.empty()) if (declarations.empty())
BOOST_THROW_EXCEPTION( DeclarationFatalError(_identifier.location(), "Undeclared identifier.");
Error(Error::Type::DeclarationError) <<
errinfo_sourceLocation(_identifier.location()) <<
errinfo_comment("Undeclared identifier.")
);
else if (declarations.size() == 1) else if (declarations.size() == 1)
{ {
_identifier.annotation().referencedDeclaration = declarations.front(); _identifier.annotation().referencedDeclaration = declarations.front();
@ -93,7 +86,7 @@ void ReferencesResolver::endVisit(VariableDeclaration const& _variable)
if (contract.isLibrary()) if (contract.isLibrary())
{ {
if (loc == Location::Memory) if (loc == Location::Memory)
fatalTypeError(_variable.location(), TypeFatalError(_variable.location(),
"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)."
); );
@ -102,7 +95,7 @@ void ReferencesResolver::endVisit(VariableDeclaration const& _variable)
{ {
// 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)
fatalTypeError(_variable.location(), TypeFatalError(_variable.location(),
"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)."
); );
@ -115,7 +108,7 @@ 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())
fatalTypeError(_variable.location(), TypeFatalError(_variable.location(),
"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)."
); );
@ -127,7 +120,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)
fatalTypeError( TypeFatalError(
_variable.location(), _variable.location(),
"Storage location has to be \"memory\" (or unspecified) for constants." "Storage location has to be \"memory\" (or unspecified) for constants."
); );
@ -145,14 +138,14 @@ void ReferencesResolver::endVisit(VariableDeclaration const& _variable)
} }
} }
else if (loc != Location::Default && !ref) else if (loc != Location::Default && !ref)
fatalTypeError(_variable.location(), "Storage location can only be given for array or struct types."); TypeFatalError(_variable.location(), "Storage location can only be given for array or struct types.");
if (!type) if (!type)
fatalTypeError(_variable.location(), "Invalid type name."); TypeFatalError(_variable.location(), "Invalid type name.");
} }
else if (!_variable.canHaveAutoType()) else if (!_variable.canHaveAutoType())
fatalTypeError(_variable.location(), "Explicit type needed."); TypeFatalError(_variable.location(), "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;
@ -178,7 +171,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
fatalTypeError(typeName->location(), "Name has to refer to a struct, enum or contract."); TypeFatalError(typeName->location(), "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))
{ {
@ -194,7 +187,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)
fatalTypeError(arrayType->location(), "Illegal base type of storage size zero for array."); TypeFatalError(arrayType->baseType().location(), "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)
@ -202,8 +195,8 @@ 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)
fatalTypeError(length->location(), "Invalid array length."); TypeFatalError(length->location(), "Invalid array length.");
type = make_shared<ArrayType>(DataLocation::Storage, baseType, lengthType->literalValue(nullptr)); else type = make_shared<ArrayType>(DataLocation::Storage, baseType, lengthType->literalValue(nullptr));
} }
else else
type = make_shared<ArrayType>(DataLocation::Storage, baseType); type = make_shared<ArrayType>(DataLocation::Storage, baseType);
@ -212,18 +205,31 @@ TypePointer ReferencesResolver::typeFor(TypeName const& _typeName)
return _typeName.annotation().type = move(type); return _typeName.annotation().type = move(type);
} }
void ReferencesResolver::typeError(SourceLocation const& _location, string const& _description) void ReferencesResolver::TypeError(SourceLocation const& _location, string const& _description)
{ {
auto err = make_shared<Error>(Error::Type::TypeError); auto err = make_shared<Error>(Error::Type::TypeError);
*err << errinfo_sourceLocation(_location) << errinfo_comment(_description); *err << errinfo_sourceLocation(_location) << errinfo_comment(_description);
m_errorOccurred = true;
m_errors.push_back(err); m_errors.push_back(err);
} }
void ReferencesResolver::fatalTypeError(SourceLocation const& _location, string const& _description) void ReferencesResolver::TypeFatalError(SourceLocation const& _location, string const& _description)
{ {
typeError(_location, _description); TypeError(_location, _description);
BOOST_THROW_EXCEPTION(FatalError()); BOOST_THROW_EXCEPTION(FatalError());
} }
void ReferencesResolver::DeclarationError(SourceLocation const& _location, string const& _description)
{
auto err = make_shared<Error>(Error::Type::DeclarationError);
*err << errinfo_sourceLocation(_location) << errinfo_comment(_description);
m_errorOccurred = true;
m_errors.push_back(err);
}
void ReferencesResolver::DeclarationFatalError(SourceLocation const& _location, string const& _description)
{
DeclarationError(_location, _description);
BOOST_THROW_EXCEPTION(FatalError());
}

View File

@ -60,7 +60,7 @@ public:
bool resolve(ASTNode& _root) bool resolve(ASTNode& _root)
{ {
_root.accept(*this); _root.accept(*this);
return Error::containsOnlyWarnings(m_errors); return !m_errorOccurred;
} }
private: private:
@ -73,16 +73,23 @@ private:
TypePointer typeFor(TypeName const& _typeName); TypePointer typeFor(TypeName const& _typeName);
/// Adds a new error to the list of errors. /// Adds a new error to the list of errors.
void typeError(SourceLocation const& _location, std::string const& _description); void TypeError(SourceLocation const& _location, std::string const& _description);
/// Adds a new error to the list of errors and throws to abort type checking. /// Adds a new error to the list of errors and throws to abort type checking.
void fatalTypeError(SourceLocation const& _location, std::string const& _description); void TypeFatalError(SourceLocation const& _location, std::string const& _description);
/// Adds a new error to the list of errors.
void DeclarationError(const SourceLocation& _location, std::string const& _description);
/// Adds a new error to the list of errors and throws to abort type checking.
void DeclarationFatalError(const SourceLocation& _location, std::string const& _description);
ErrorList& m_errors; 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;
bool m_resolveInsideCode; bool const m_resolveInsideCode;
bool m_errorOccurred = false;
}; };
} }