Refactor name and type resolution.

This commit is contained in:
chriseth
2020-06-12 12:18:02 +02:00
parent 012ba9537b
commit 07c1167136
7 changed files with 46 additions and 66 deletions
+9 -8
View File
@@ -123,11 +123,13 @@ bool NameAndTypeResolver::performImports(SourceUnit& _sourceUnit, map<string, So
return !error;
}
bool NameAndTypeResolver::resolveNamesAndTypes(ASTNode& _node, bool _resolveInsideCode)
bool NameAndTypeResolver::resolveNamesAndTypes(SourceUnit& _source)
{
try
{
return resolveNamesAndTypesInternal(_node, _resolveInsideCode);
for (shared_ptr<ASTNode> const& node: _source.nodes())
if (!resolveNamesAndTypesInternal(*node, true))
return false;
}
catch (langutil::FatalError const&)
{
@@ -135,6 +137,7 @@ bool NameAndTypeResolver::resolveNamesAndTypes(ASTNode& _node, bool _resolveInsi
throw; // Something is weird here, rather throw again.
return false;
}
return true;
}
bool NameAndTypeResolver::updateDeclaration(Declaration const& _declaration)
@@ -227,13 +230,14 @@ bool NameAndTypeResolver::resolveNamesAndTypesInternal(ASTNode& _node, bool _res
bool success = true;
setScope(contract->scope());
solAssert(!!m_currentScope, "");
solAssert(_resolveInsideCode, "");
m_globalContext.setCurrentContract(*contract);
updateDeclaration(*m_globalContext.currentSuper());
updateDeclaration(*m_globalContext.currentThis());
for (ASTPointer<InheritanceSpecifier> const& baseContract: contract->baseContracts())
if (!resolveNamesAndTypes(*baseContract, true))
if (!resolveNamesAndTypesInternal(*baseContract, true))
success = false;
setScope(contract);
@@ -254,23 +258,20 @@ bool NameAndTypeResolver::resolveNamesAndTypesInternal(ASTNode& _node, bool _res
for (ASTPointer<ASTNode> const& node: contract->subNodes())
{
setScope(contract);
if (!resolveNamesAndTypes(*node, false))
if (!resolveNamesAndTypesInternal(*node, false))
success = false;
}
if (!success)
return false;
if (!_resolveInsideCode)
return success;
setScope(contract);
// now resolve references inside the code
for (ASTPointer<ASTNode> const& node: contract->subNodes())
{
setScope(contract);
if (!resolveNamesAndTypes(*node, true))
if (!resolveNamesAndTypesInternal(*node, true))
success = false;
}
return success;
+2 -5
View File
@@ -65,12 +65,9 @@ public:
bool registerDeclarations(SourceUnit& _sourceUnit, ASTNode const* _currentScope = nullptr);
/// Applies the effect of import directives.
bool performImports(SourceUnit& _sourceUnit, std::map<std::string, SourceUnit const*> const& _sourceUnits);
/// Resolves all names and types referenced from the given AST Node.
/// This is usually only called at the contract level, but with a bit of care, it can also
/// be called at deeper levels.
/// @param _resolveInsideCode if false, does not descend into nodes that contain code.
/// Resolves all names and types referenced from the given Source Node.
/// @returns false in case of error.
bool resolveNamesAndTypes(ASTNode& _node, bool _resolveInsideCode = true);
bool resolveNamesAndTypes(SourceUnit& _source);
/// Updates the given global declaration (used for "this"). Not to be used with declarations
/// that create their own scope.
/// @returns false in case of error.
+2 -2
View File
@@ -62,9 +62,9 @@ bool TypeChecker::typeSupportedByOldABIEncoder(Type const& _type, bool _isLibrar
return true;
}
bool TypeChecker::checkTypeRequirements(ASTNode const& _contract)
bool TypeChecker::checkTypeRequirements(SourceUnit const& _source)
{
_contract.accept(*this);
_source.accept(*this);
return Error::containsOnlyWarnings(m_errorReporter.errors());
}
+2 -2
View File
@@ -51,9 +51,9 @@ public:
m_errorReporter(_errorReporter)
{}
/// Performs type checking on the given contract and all of its sub-nodes.
/// Performs type checking on the given source and all of its sub-nodes.
/// @returns true iff all checks passed. Note even if all checks passed, errors() can still contain warnings
bool checkTypeRequirements(ASTNode const& _contract);
bool checkTypeRequirements(SourceUnit const& _source);
/// @returns the type of an expression and asserts that it is present.
TypePointer const& type(Expression const& _expression) const;