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,22 +31,59 @@ using namespace dev;
|
||||
using namespace langutil;
|
||||
using namespace dev::solidity;
|
||||
|
||||
|
||||
bool PostTypeChecker::check(ASTNode const& _astRoot)
|
||||
{
|
||||
_astRoot.accept(*this);
|
||||
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_constVariableDependencies.empty(), "");
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
void PostTypeChecker::endVisit(ContractDefinition const&)
|
||||
{
|
||||
void endVisit(ContractDefinition const&) override
|
||||
{
|
||||
solAssert(!m_currentConstVariable, "");
|
||||
for (auto declaration: m_constVariables)
|
||||
if (auto identifier = findCycle(*declaration))
|
||||
@ -58,31 +95,10 @@ void PostTypeChecker::endVisit(ContractDefinition const&)
|
||||
|
||||
m_constVariables.clear();
|
||||
m_constVariableDependencies.clear();
|
||||
}
|
||||
|
||||
void PostTypeChecker::endVisit(OverrideSpecifier const& _overrideSpecifier)
|
||||
{
|
||||
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)
|
||||
{
|
||||
bool visit(VariableDeclaration const& _variable) override
|
||||
{
|
||||
solAssert(!m_currentConstVariable, "");
|
||||
if (_variable.isConstant())
|
||||
{
|
||||
@ -90,28 +106,28 @@ bool PostTypeChecker::visit(VariableDeclaration const& _variable)
|
||||
m_constVariables.push_back(&_variable);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
void PostTypeChecker::endVisit(VariableDeclaration const& _variable)
|
||||
{
|
||||
void endVisit(VariableDeclaration const& _variable) override
|
||||
{
|
||||
if (_variable.isConstant())
|
||||
{
|
||||
solAssert(m_currentConstVariable == &_variable, "");
|
||||
m_currentConstVariable = nullptr;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
bool PostTypeChecker::visit(Identifier const& _identifier)
|
||||
{
|
||||
bool visit(Identifier const& _identifier) override
|
||||
{
|
||||
if (m_currentConstVariable)
|
||||
if (auto var = dynamic_cast<VariableDeclaration const*>(_identifier.annotation().referencedDeclaration))
|
||||
if (var->isConstant())
|
||||
m_constVariableDependencies[m_currentConstVariable].insert(var);
|
||||
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)
|
||||
{
|
||||
if (_depth >= 256)
|
||||
@ -133,4 +149,45 @@ VariableDeclaration const* PostTypeChecker::findCycle(VariableDeclaration const&
|
||||
return;
|
||||
};
|
||||
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:
|
||||
* - whether there are circular references in constant state variables
|
||||
* - 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
|
||||
{
|
||||
public:
|
||||
struct Checker: public ASTConstVisitor
|
||||
{
|
||||
Checker(langutil::ErrorReporter& _errorReporter):
|
||||
m_errorReporter(_errorReporter) {}
|
||||
protected:
|
||||
langutil::ErrorReporter& m_errorReporter;
|
||||
};
|
||||
|
||||
/// @param _errorReporter provides the error logging functionality.
|
||||
PostTypeChecker(langutil::ErrorReporter& _errorReporter): m_errorReporter(_errorReporter) {}
|
||||
PostTypeChecker(langutil::ErrorReporter& _errorReporter);
|
||||
|
||||
bool check(ASTNode const& _astRoot);
|
||||
|
||||
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;
|
||||
void endVisit(ContractDefinition const& _contract) override;
|
||||
void endVisit(OverrideSpecifier const& _overrideSpecifier) override;
|
||||
@ -61,13 +71,25 @@ private:
|
||||
|
||||
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;
|
||||
|
||||
VariableDeclaration const* m_currentConstVariable = nullptr;
|
||||
std::vector<VariableDeclaration const*> m_constVariables; ///< Required for determinism.
|
||||
std::map<VariableDeclaration const*, std::set<VariableDeclaration const*>> m_constVariableDependencies;
|
||||
std::vector<std::shared_ptr<Checker>> m_checkers;
|
||||
};
|
||||
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user