2017-03-03 11:51:51 +00:00
|
|
|
/*
|
|
|
|
This file is part of solidity.
|
|
|
|
|
|
|
|
solidity is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
solidity is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with solidity. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <libsolidity/analysis/PostTypeChecker.h>
|
2018-12-17 11:30:08 +00:00
|
|
|
|
|
|
|
#include <libsolidity/ast/AST.h>
|
2017-03-03 11:51:51 +00:00
|
|
|
#include <libsolidity/interface/Version.h>
|
2018-12-17 11:30:08 +00:00
|
|
|
#include <liblangutil/ErrorReporter.h>
|
2019-04-17 11:02:30 +00:00
|
|
|
#include <liblangutil/SemVerHandler.h>
|
2020-01-06 10:52:23 +00:00
|
|
|
#include <libsolutil/Algorithms.h>
|
2018-03-15 18:53:29 +00:00
|
|
|
|
2017-03-03 11:51:51 +00:00
|
|
|
#include <boost/range/adaptor/map.hpp>
|
|
|
|
#include <memory>
|
|
|
|
|
|
|
|
using namespace std;
|
2019-12-11 16:31:36 +00:00
|
|
|
using namespace solidity;
|
|
|
|
using namespace solidity::langutil;
|
|
|
|
using namespace solidity::frontend;
|
2017-03-03 11:51:51 +00:00
|
|
|
|
|
|
|
bool PostTypeChecker::check(ASTNode const& _astRoot)
|
|
|
|
{
|
|
|
|
_astRoot.accept(*this);
|
2017-05-11 13:26:35 +00:00
|
|
|
return Error::containsOnlyWarnings(m_errorReporter.errors());
|
2017-03-03 11:51:51 +00:00
|
|
|
}
|
|
|
|
|
2019-12-17 14:56:06 +00:00
|
|
|
bool PostTypeChecker::visit(ContractDefinition const& _contractDefinition)
|
2017-03-03 11:51:51 +00:00
|
|
|
{
|
2019-12-17 14:56:06 +00:00
|
|
|
return callVisit(_contractDefinition);
|
2017-03-03 11:51:51 +00:00
|
|
|
}
|
|
|
|
|
2019-12-17 14:56:06 +00:00
|
|
|
void PostTypeChecker::endVisit(ContractDefinition const& _contractDefinition)
|
2017-03-03 11:51:51 +00:00
|
|
|
{
|
2019-12-17 14:56:06 +00:00
|
|
|
callEndVisit(_contractDefinition);
|
2017-03-03 11:51:51 +00:00
|
|
|
}
|
|
|
|
|
2019-09-16 10:36:50 +00:00
|
|
|
void PostTypeChecker::endVisit(OverrideSpecifier const& _overrideSpecifier)
|
|
|
|
{
|
2019-12-17 14:56:06 +00:00
|
|
|
callEndVisit(_overrideSpecifier);
|
2019-09-16 10:36:50 +00:00
|
|
|
}
|
|
|
|
|
2017-03-03 11:51:51 +00:00
|
|
|
bool PostTypeChecker::visit(VariableDeclaration const& _variable)
|
|
|
|
{
|
2019-12-17 14:56:06 +00:00
|
|
|
return callVisit(_variable);
|
2017-03-03 11:51:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void PostTypeChecker::endVisit(VariableDeclaration const& _variable)
|
|
|
|
{
|
2019-12-17 14:56:06 +00:00
|
|
|
callEndVisit(_variable);
|
2017-03-03 11:51:51 +00:00
|
|
|
}
|
|
|
|
|
2019-12-17 16:04:18 +00:00
|
|
|
bool PostTypeChecker::visit(EmitStatement const& _emit)
|
|
|
|
{
|
|
|
|
return callVisit(_emit);
|
|
|
|
}
|
|
|
|
|
|
|
|
void PostTypeChecker::endVisit(EmitStatement const& _emit)
|
|
|
|
{
|
|
|
|
callEndVisit(_emit);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool PostTypeChecker::visit(FunctionCall const& _functionCall)
|
|
|
|
{
|
|
|
|
return callVisit(_functionCall);
|
|
|
|
}
|
|
|
|
|
2017-03-03 11:51:51 +00:00
|
|
|
bool PostTypeChecker::visit(Identifier const& _identifier)
|
|
|
|
{
|
2019-12-17 14:56:06 +00:00
|
|
|
return callVisit(_identifier);
|
2017-03-03 11:51:51 +00:00
|
|
|
}
|
|
|
|
|
2019-12-17 16:58:53 +00:00
|
|
|
bool PostTypeChecker::visit(StructDefinition const& _struct)
|
|
|
|
{
|
|
|
|
return callVisit(_struct);
|
|
|
|
}
|
|
|
|
|
|
|
|
void PostTypeChecker::endVisit(StructDefinition const& _struct)
|
|
|
|
{
|
|
|
|
callEndVisit(_struct);
|
|
|
|
}
|
|
|
|
|
2019-12-17 15:18:58 +00:00
|
|
|
bool PostTypeChecker::visit(ModifierInvocation const& _modifierInvocation)
|
|
|
|
{
|
|
|
|
return callVisit(_modifierInvocation);
|
|
|
|
}
|
|
|
|
|
|
|
|
void PostTypeChecker::endVisit(ModifierInvocation const& _modifierInvocation)
|
|
|
|
{
|
|
|
|
callEndVisit(_modifierInvocation);
|
|
|
|
}
|
2019-12-17 14:56:06 +00:00
|
|
|
|
|
|
|
namespace
|
|
|
|
{
|
|
|
|
struct ConstStateVarCircularReferenceChecker: public PostTypeChecker::Checker
|
2017-03-03 11:51:51 +00:00
|
|
|
{
|
2019-12-17 14:56:06 +00:00
|
|
|
ConstStateVarCircularReferenceChecker(ErrorReporter& _errorReporter):
|
|
|
|
Checker(_errorReporter) {}
|
|
|
|
|
|
|
|
bool visit(ContractDefinition const&) override
|
|
|
|
{
|
|
|
|
solAssert(!m_currentConstVariable, "");
|
|
|
|
solAssert(m_constVariableDependencies.empty(), "");
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void endVisit(ContractDefinition const&) override
|
|
|
|
{
|
|
|
|
solAssert(!m_currentConstVariable, "");
|
|
|
|
for (auto declaration: m_constVariables)
|
|
|
|
if (auto identifier = findCycle(*declaration))
|
|
|
|
m_errorReporter.typeError(
|
|
|
|
declaration->location(),
|
|
|
|
"The value of the constant " + declaration->name() +
|
|
|
|
" has a cyclic dependency via " + identifier->name() + "."
|
|
|
|
);
|
|
|
|
|
|
|
|
m_constVariables.clear();
|
|
|
|
m_constVariableDependencies.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool visit(VariableDeclaration const& _variable) override
|
|
|
|
{
|
|
|
|
solAssert(!m_currentConstVariable, "");
|
|
|
|
if (_variable.isConstant())
|
|
|
|
{
|
|
|
|
m_currentConstVariable = &_variable;
|
|
|
|
m_constVariables.push_back(&_variable);
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void endVisit(VariableDeclaration const& _variable) override
|
2017-03-03 11:51:51 +00:00
|
|
|
{
|
2019-12-17 14:56:06 +00:00
|
|
|
if (_variable.isConstant())
|
2018-03-15 18:53:29 +00:00
|
|
|
{
|
2019-12-17 14:56:06 +00:00
|
|
|
solAssert(m_currentConstVariable == &_variable, "");
|
|
|
|
m_currentConstVariable = nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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* findCycle(VariableDeclaration const& _startingFrom)
|
|
|
|
{
|
2019-12-11 16:31:36 +00:00
|
|
|
auto visitor = [&](VariableDeclaration const& _variable, util::CycleDetector<VariableDeclaration>& _cycleDetector, size_t _depth)
|
2019-12-17 14:56:06 +00:00
|
|
|
{
|
|
|
|
if (_depth >= 256)
|
|
|
|
m_errorReporter.fatalDeclarationError(_variable.location(), "Variable definition exhausting cyclic dependency validator.");
|
|
|
|
|
|
|
|
// Iterating through the dependencies needs to be deterministic and thus cannot
|
|
|
|
// depend on the memory layout.
|
|
|
|
// Because of that, we sort by AST node id.
|
|
|
|
vector<VariableDeclaration const*> dependencies(
|
|
|
|
m_constVariableDependencies[&_variable].begin(),
|
|
|
|
m_constVariableDependencies[&_variable].end()
|
|
|
|
);
|
|
|
|
sort(dependencies.begin(), dependencies.end(), [](VariableDeclaration const* _a, VariableDeclaration const* _b) -> bool
|
|
|
|
{
|
|
|
|
return _a->id() < _b->id();
|
|
|
|
});
|
|
|
|
for (auto v: dependencies)
|
|
|
|
if (_cycleDetector.run(*v))
|
|
|
|
return;
|
|
|
|
};
|
2019-12-11 16:31:36 +00:00
|
|
|
return util::CycleDetector<VariableDeclaration>(visitor).run(_startingFrom);
|
2019-12-17 14:56:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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) +
|
|
|
|
"."
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2019-12-17 15:18:58 +00:00
|
|
|
struct ModifierContextChecker: public PostTypeChecker::Checker
|
|
|
|
{
|
|
|
|
ModifierContextChecker(ErrorReporter& _errorReporter):
|
|
|
|
Checker(_errorReporter) {}
|
|
|
|
|
|
|
|
bool visit(ModifierInvocation const&) override
|
|
|
|
{
|
|
|
|
m_insideModifierInvocation = true;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void endVisit(ModifierInvocation const&) override
|
|
|
|
{
|
|
|
|
m_insideModifierInvocation = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool visit(Identifier const& _identifier) override
|
|
|
|
{
|
|
|
|
if (m_insideModifierInvocation)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
if (ModifierType const* type = dynamic_cast<decltype(type)>(_identifier.annotation().type))
|
|
|
|
{
|
|
|
|
m_errorReporter.typeError(
|
|
|
|
_identifier.location(),
|
|
|
|
"Modifier can only be referenced in function headers."
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
private:
|
|
|
|
/// Flag indicating whether we are currently inside the invocation of a modifier
|
|
|
|
bool m_insideModifierInvocation = false;
|
|
|
|
};
|
2019-12-17 16:04:18 +00:00
|
|
|
|
|
|
|
struct EventOutsideEmitChecker: public PostTypeChecker::Checker
|
|
|
|
{
|
|
|
|
EventOutsideEmitChecker(ErrorReporter& _errorReporter):
|
|
|
|
Checker(_errorReporter) {}
|
|
|
|
|
|
|
|
bool visit(EmitStatement const&) override
|
|
|
|
{
|
|
|
|
m_insideEmitStatement = true;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void endVisit(EmitStatement const&) override
|
|
|
|
{
|
|
|
|
m_insideEmitStatement = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool visit(FunctionCall const& _functionCall) override
|
|
|
|
{
|
|
|
|
if (_functionCall.annotation().kind != FunctionCallKind::FunctionCall)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
if (FunctionTypePointer const functionType = dynamic_cast<FunctionTypePointer const>(_functionCall.expression().annotation().type))
|
|
|
|
// Check for event outside of emit statement
|
|
|
|
if (!m_insideEmitStatement && functionType->kind() == FunctionType::Kind::Event)
|
|
|
|
m_errorReporter.typeError(
|
|
|
|
_functionCall.location(),
|
|
|
|
"Event invocations have to be prefixed by \"emit\"."
|
|
|
|
);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
/// Flag indicating whether we are currently inside an EmitStatement.
|
|
|
|
bool m_insideEmitStatement = false;
|
|
|
|
};
|
|
|
|
|
2019-12-17 16:58:53 +00:00
|
|
|
struct NoVariablesInInterfaceChecker: public PostTypeChecker::Checker
|
|
|
|
{
|
|
|
|
NoVariablesInInterfaceChecker(ErrorReporter& _errorReporter):
|
|
|
|
Checker(_errorReporter)
|
|
|
|
{}
|
|
|
|
|
|
|
|
bool visit(VariableDeclaration const& _variable) override
|
|
|
|
{
|
|
|
|
// Forbid any variable declarations inside interfaces unless they are part of
|
|
|
|
// * a function's input/output parameters,
|
|
|
|
// * or inside of a struct definition.
|
|
|
|
if (
|
|
|
|
m_scope && m_scope->isInterface()
|
|
|
|
&& !_variable.isCallableOrCatchParameter()
|
|
|
|
&& !m_insideStruct
|
|
|
|
)
|
|
|
|
m_errorReporter.typeError(_variable.location(), "Variables cannot be declared in interfaces.");
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool visit(ContractDefinition const& _contract) override
|
|
|
|
{
|
|
|
|
m_scope = &_contract;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void endVisit(ContractDefinition const&) override
|
|
|
|
{
|
|
|
|
m_scope = nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool visit(StructDefinition const&) override
|
|
|
|
{
|
|
|
|
solAssert(m_insideStruct >= 0, "");
|
|
|
|
m_insideStruct++;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void endVisit(StructDefinition const&) override
|
|
|
|
{
|
|
|
|
m_insideStruct--;
|
|
|
|
solAssert(m_insideStruct >= 0, "");
|
|
|
|
}
|
|
|
|
private:
|
|
|
|
ContractDefinition const* m_scope = nullptr;
|
|
|
|
/// Flag indicating whether we are currently inside a StructDefinition.
|
|
|
|
int m_insideStruct = 0;
|
|
|
|
};
|
2019-12-17 15:18:58 +00:00
|
|
|
}
|
2019-12-17 14:56:06 +00:00
|
|
|
|
|
|
|
PostTypeChecker::PostTypeChecker(langutil::ErrorReporter& _errorReporter): m_errorReporter(_errorReporter)
|
|
|
|
{
|
|
|
|
m_checkers.push_back(make_shared<ConstStateVarCircularReferenceChecker>(_errorReporter));
|
|
|
|
m_checkers.push_back(make_shared<OverrideSpecifierChecker>(_errorReporter));
|
2019-12-17 15:18:58 +00:00
|
|
|
m_checkers.push_back(make_shared<ModifierContextChecker>(_errorReporter));
|
2019-12-17 16:04:18 +00:00
|
|
|
m_checkers.push_back(make_shared<EventOutsideEmitChecker>(_errorReporter));
|
2019-12-17 16:58:53 +00:00
|
|
|
m_checkers.push_back(make_shared<NoVariablesInInterfaceChecker>(_errorReporter));
|
2017-03-03 11:51:51 +00:00
|
|
|
}
|