mirror of
https://github.com/ethereum/solidity
synced 2023-10-03 13:03:40 +00:00
Refactor PostTypeChecker into multiple classes per usecase
This commit is contained in:
parent
cf6f6a51b0
commit
a8ca96cd3e
@ -31,21 +31,58 @@ using namespace dev;
|
|||||||
using namespace langutil;
|
using namespace langutil;
|
||||||
using namespace dev::solidity;
|
using namespace dev::solidity;
|
||||||
|
|
||||||
|
|
||||||
bool PostTypeChecker::check(ASTNode const& _astRoot)
|
bool PostTypeChecker::check(ASTNode const& _astRoot)
|
||||||
{
|
{
|
||||||
_astRoot.accept(*this);
|
_astRoot.accept(*this);
|
||||||
return Error::containsOnlyWarnings(m_errorReporter.errors());
|
return Error::containsOnlyWarnings(m_errorReporter.errors());
|
||||||
}
|
}
|
||||||
|
|
||||||
bool PostTypeChecker::visit(ContractDefinition const&)
|
bool PostTypeChecker::visit(ContractDefinition const& _contractDefinition)
|
||||||
|
{
|
||||||
|
return callVisit(_contractDefinition);
|
||||||
|
}
|
||||||
|
|
||||||
|
void PostTypeChecker::endVisit(ContractDefinition const& _contractDefinition)
|
||||||
|
{
|
||||||
|
callEndVisit(_contractDefinition);
|
||||||
|
}
|
||||||
|
|
||||||
|
void PostTypeChecker::endVisit(OverrideSpecifier const& _overrideSpecifier)
|
||||||
|
{
|
||||||
|
callEndVisit(_overrideSpecifier);
|
||||||
|
}
|
||||||
|
|
||||||
|
bool PostTypeChecker::visit(VariableDeclaration const& _variable)
|
||||||
|
{
|
||||||
|
return callVisit(_variable);
|
||||||
|
}
|
||||||
|
|
||||||
|
void PostTypeChecker::endVisit(VariableDeclaration const& _variable)
|
||||||
|
{
|
||||||
|
callEndVisit(_variable);
|
||||||
|
}
|
||||||
|
|
||||||
|
bool PostTypeChecker::visit(Identifier const& _identifier)
|
||||||
|
{
|
||||||
|
return callVisit(_identifier);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
namespace
|
||||||
|
{
|
||||||
|
struct ConstStateVarCircularReferenceChecker: public PostTypeChecker::Checker
|
||||||
|
{
|
||||||
|
ConstStateVarCircularReferenceChecker(ErrorReporter& _errorReporter):
|
||||||
|
Checker(_errorReporter) {}
|
||||||
|
|
||||||
|
bool visit(ContractDefinition const&) override
|
||||||
{
|
{
|
||||||
solAssert(!m_currentConstVariable, "");
|
solAssert(!m_currentConstVariable, "");
|
||||||
solAssert(m_constVariableDependencies.empty(), "");
|
solAssert(m_constVariableDependencies.empty(), "");
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
void PostTypeChecker::endVisit(ContractDefinition const&)
|
void endVisit(ContractDefinition const&) override
|
||||||
{
|
{
|
||||||
solAssert(!m_currentConstVariable, "");
|
solAssert(!m_currentConstVariable, "");
|
||||||
for (auto declaration: m_constVariables)
|
for (auto declaration: m_constVariables)
|
||||||
@ -60,28 +97,7 @@ void PostTypeChecker::endVisit(ContractDefinition const&)
|
|||||||
m_constVariableDependencies.clear();
|
m_constVariableDependencies.clear();
|
||||||
}
|
}
|
||||||
|
|
||||||
void PostTypeChecker::endVisit(OverrideSpecifier const& _overrideSpecifier)
|
bool visit(VariableDeclaration const& _variable) override
|
||||||
{
|
|
||||||
for (ASTPointer<UserDefinedTypeName> const& override: _overrideSpecifier.overrides())
|
|
||||||
{
|
|
||||||
Declaration const* decl = override->annotation().referencedDeclaration;
|
|
||||||
solAssert(decl, "Expected declaration to be resolved.");
|
|
||||||
|
|
||||||
if (dynamic_cast<ContractDefinition const*>(decl))
|
|
||||||
continue;
|
|
||||||
|
|
||||||
TypeType const* actualTypeType = dynamic_cast<TypeType const*>(decl->type());
|
|
||||||
|
|
||||||
m_errorReporter.typeError(
|
|
||||||
override->location(),
|
|
||||||
"Expected contract but got " +
|
|
||||||
actualTypeType->actualType()->toString(true) +
|
|
||||||
"."
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
bool PostTypeChecker::visit(VariableDeclaration const& _variable)
|
|
||||||
{
|
{
|
||||||
solAssert(!m_currentConstVariable, "");
|
solAssert(!m_currentConstVariable, "");
|
||||||
if (_variable.isConstant())
|
if (_variable.isConstant())
|
||||||
@ -92,7 +108,7 @@ bool PostTypeChecker::visit(VariableDeclaration const& _variable)
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
void PostTypeChecker::endVisit(VariableDeclaration const& _variable)
|
void endVisit(VariableDeclaration const& _variable) override
|
||||||
{
|
{
|
||||||
if (_variable.isConstant())
|
if (_variable.isConstant())
|
||||||
{
|
{
|
||||||
@ -101,7 +117,7 @@ void PostTypeChecker::endVisit(VariableDeclaration const& _variable)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
bool PostTypeChecker::visit(Identifier const& _identifier)
|
bool visit(Identifier const& _identifier) override
|
||||||
{
|
{
|
||||||
if (m_currentConstVariable)
|
if (m_currentConstVariable)
|
||||||
if (auto var = dynamic_cast<VariableDeclaration const*>(_identifier.annotation().referencedDeclaration))
|
if (auto var = dynamic_cast<VariableDeclaration const*>(_identifier.annotation().referencedDeclaration))
|
||||||
@ -110,7 +126,7 @@ bool PostTypeChecker::visit(Identifier const& _identifier)
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
VariableDeclaration const* PostTypeChecker::findCycle(VariableDeclaration const& _startingFrom)
|
VariableDeclaration const* findCycle(VariableDeclaration const& _startingFrom)
|
||||||
{
|
{
|
||||||
auto visitor = [&](VariableDeclaration const& _variable, CycleDetector<VariableDeclaration>& _cycleDetector, size_t _depth)
|
auto visitor = [&](VariableDeclaration const& _variable, CycleDetector<VariableDeclaration>& _cycleDetector, size_t _depth)
|
||||||
{
|
{
|
||||||
@ -134,3 +150,44 @@ VariableDeclaration const* PostTypeChecker::findCycle(VariableDeclaration const&
|
|||||||
};
|
};
|
||||||
return CycleDetector<VariableDeclaration>(visitor).run(_startingFrom);
|
return CycleDetector<VariableDeclaration>(visitor).run(_startingFrom);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private:
|
||||||
|
VariableDeclaration const* m_currentConstVariable = nullptr;
|
||||||
|
std::map<VariableDeclaration const*, std::set<VariableDeclaration const*>> m_constVariableDependencies;
|
||||||
|
std::vector<VariableDeclaration const*> m_constVariables; ///< Required for determinism.
|
||||||
|
};
|
||||||
|
|
||||||
|
struct OverrideSpecifierChecker: public PostTypeChecker::Checker
|
||||||
|
{
|
||||||
|
OverrideSpecifierChecker(ErrorReporter& _errorReporter):
|
||||||
|
Checker(_errorReporter) {}
|
||||||
|
|
||||||
|
void endVisit(OverrideSpecifier const& _overrideSpecifier) override
|
||||||
|
{
|
||||||
|
for (ASTPointer<UserDefinedTypeName> const& override: _overrideSpecifier.overrides())
|
||||||
|
{
|
||||||
|
Declaration const* decl = override->annotation().referencedDeclaration;
|
||||||
|
solAssert(decl, "Expected declaration to be resolved.");
|
||||||
|
|
||||||
|
if (dynamic_cast<ContractDefinition const*>(decl))
|
||||||
|
continue;
|
||||||
|
|
||||||
|
TypeType const* actualTypeType = dynamic_cast<TypeType const*>(decl->type());
|
||||||
|
|
||||||
|
m_errorReporter.typeError(
|
||||||
|
override->location(),
|
||||||
|
"Expected contract but got " +
|
||||||
|
actualTypeType->actualType()->toString(true) +
|
||||||
|
"."
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
PostTypeChecker::PostTypeChecker(langutil::ErrorReporter& _errorReporter): m_errorReporter(_errorReporter)
|
||||||
|
{
|
||||||
|
m_checkers.push_back(make_shared<ConstStateVarCircularReferenceChecker>(_errorReporter));
|
||||||
|
m_checkers.push_back(make_shared<OverrideSpecifierChecker>(_errorReporter));
|
||||||
|
}
|
||||||
|
@ -38,20 +38,30 @@ namespace solidity
|
|||||||
* This module performs analyses on the AST that are done after type checking and assignments of types:
|
* This module performs analyses on the AST that are done after type checking and assignments of types:
|
||||||
* - whether there are circular references in constant state variables
|
* - whether there are circular references in constant state variables
|
||||||
* - whether override specifiers are actually contracts
|
* - whether override specifiers are actually contracts
|
||||||
* @TODO factor out each use-case into an individual class (but do the traversal only once)
|
*
|
||||||
|
* When adding a new checker, make sure a visitor that forwards calls that your
|
||||||
|
* checker uses exists in PostTypeChecker. Add missing ones.
|
||||||
|
*
|
||||||
|
* The return value for the visit function of a checker is ignored, all nodes
|
||||||
|
* will always be visited.
|
||||||
*/
|
*/
|
||||||
class PostTypeChecker: private ASTConstVisitor
|
class PostTypeChecker: private ASTConstVisitor
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
|
struct Checker: public ASTConstVisitor
|
||||||
|
{
|
||||||
|
Checker(langutil::ErrorReporter& _errorReporter):
|
||||||
|
m_errorReporter(_errorReporter) {}
|
||||||
|
protected:
|
||||||
|
langutil::ErrorReporter& m_errorReporter;
|
||||||
|
};
|
||||||
|
|
||||||
/// @param _errorReporter provides the error logging functionality.
|
/// @param _errorReporter provides the error logging functionality.
|
||||||
PostTypeChecker(langutil::ErrorReporter& _errorReporter): m_errorReporter(_errorReporter) {}
|
PostTypeChecker(langutil::ErrorReporter& _errorReporter);
|
||||||
|
|
||||||
bool check(ASTNode const& _astRoot);
|
bool check(ASTNode const& _astRoot);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
/// Adds a new error to the list of errors.
|
|
||||||
void typeError(langutil::SourceLocation const& _location, std::string const& _description);
|
|
||||||
|
|
||||||
bool visit(ContractDefinition const& _contract) override;
|
bool visit(ContractDefinition const& _contract) override;
|
||||||
void endVisit(ContractDefinition const& _contract) override;
|
void endVisit(ContractDefinition const& _contract) override;
|
||||||
void endVisit(OverrideSpecifier const& _overrideSpecifier) override;
|
void endVisit(OverrideSpecifier const& _overrideSpecifier) override;
|
||||||
@ -61,13 +71,25 @@ private:
|
|||||||
|
|
||||||
bool visit(Identifier const& _identifier) override;
|
bool visit(Identifier const& _identifier) override;
|
||||||
|
|
||||||
VariableDeclaration const* findCycle(VariableDeclaration const& _startingFrom);
|
template <class T>
|
||||||
|
bool callVisit(T const& _node)
|
||||||
|
{
|
||||||
|
for (auto& checker: m_checkers)
|
||||||
|
checker->visit(_node);
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
template <class T>
|
||||||
|
void callEndVisit(T const& _node)
|
||||||
|
{
|
||||||
|
for (auto& checker: m_checkers)
|
||||||
|
checker->endVisit(_node);
|
||||||
|
}
|
||||||
|
|
||||||
langutil::ErrorReporter& m_errorReporter;
|
langutil::ErrorReporter& m_errorReporter;
|
||||||
|
|
||||||
VariableDeclaration const* m_currentConstVariable = nullptr;
|
std::vector<std::shared_ptr<Checker>> m_checkers;
|
||||||
std::vector<VariableDeclaration const*> m_constVariables; ///< Required for determinism.
|
|
||||||
std::map<VariableDeclaration const*, std::set<VariableDeclaration const*>> m_constVariableDependencies;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user