This commit is contained in:
Daniel Kirchner
2023-06-19 16:24:07 +02:00
parent fb959b3066
commit 71f7bf7206
15 changed files with 430 additions and 12 deletions
@@ -17,10 +17,15 @@
// SPDX-License-Identifier: GPL-3.0
#include <libsolidity/analysis/experimental/Analysis.h>
#include <libsolidity/analysis/experimental/SyntaxRestrictor.h>
using namespace solidity::langutil;
using namespace solidity::frontend::experimental;
bool Analysis::check(ASTNode const&)
bool Analysis::check(ASTNode const& _node)
{
SyntaxRestrictor syntaxRestrictor{m_errorReporter};
if (!syntaxRestrictor.check(_node))
return false;
return true;
}
+6 -1
View File
@@ -17,6 +17,8 @@
// SPDX-License-Identifier: GPL-3.0
#pragma once
#include <cstdint>
namespace solidity::frontend
{
class ASTNode;
@@ -33,11 +35,14 @@ namespace solidity::frontend::experimental
class Analysis
{
public:
Analysis(langutil::ErrorReporter& _errorReporter): m_errorReporter(_errorReporter)
Analysis(langutil::ErrorReporter& _errorReporter, uint64_t _maxAstId):
m_errorReporter(_errorReporter),
m_maxAstId(_maxAstId)
{}
bool check(ASTNode const& _ast);
private:
langutil::ErrorReporter& m_errorReporter;
uint64_t m_maxAstId = 0;
};
}
@@ -0,0 +1,110 @@
/*
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/>.
*/
// SPDX-License-Identifier: GPL-3.0
#include <libsolidity/analysis/experimental/SyntaxRestrictor.h>
#include <liblangutil/Exceptions.h>
using namespace solidity::frontend;
using namespace solidity::frontend::experimental;
using namespace solidity::langutil;
bool SyntaxRestrictor::check(ASTNode const& _astRoot)
{
_astRoot.accept(*this);
return !Error::containsErrors(m_errorReporter.errors());
}
bool SyntaxRestrictor::visitNode(ASTNode const& _node)
{
m_errorReporter.syntaxError(0000_error, _node.location(), "Unsupported AST node.");
return false;
}
bool SyntaxRestrictor::visit(ContractDefinition const& _contractDefinition)
{
if (_contractDefinition.contractKind() != ContractKind::Contract)
m_errorReporter.syntaxError(0000_error, _contractDefinition.location(), "Only contracts are supported.");
if (!_contractDefinition.baseContracts().empty())
m_errorReporter.syntaxError(0000_error, _contractDefinition.location(), "Inheritance unsupported.");
return true;
}
bool SyntaxRestrictor::visit(FunctionDefinition const& _functionDefinition)
{
if (!_functionDefinition.isImplemented())
m_errorReporter.syntaxError(0000_error, _functionDefinition.location(), "Functions must be implemented.");
if (!_functionDefinition.parameterList().parameters().empty())
m_errorReporter.syntaxError(0000_error, _functionDefinition.location(), "Function may not have arguments.");
if (_functionDefinition.returnParameterList() && !_functionDefinition.returnParameterList()->parameters().empty())
m_errorReporter.syntaxError(0000_error, _functionDefinition.location(), "Function may not have return variables.");
if (_functionDefinition.isFree())
{
if (_functionDefinition.stateMutability() != StateMutability::NonPayable)
m_errorReporter.syntaxError(0000_error, _functionDefinition.location(), "Free functions may not have a mutability.");
}
else
{
if (_functionDefinition.isFallback())
{
if (_functionDefinition.visibility() != Visibility::External)
m_errorReporter.syntaxError(0000_error, _functionDefinition.location(), "Fallback function must be external.");
}
else
m_errorReporter.syntaxError(0000_error, _functionDefinition.location(), "Only fallback functions are supported in contracts.");
}
_functionDefinition.body().accept(*this);
return false;
}
bool SyntaxRestrictor::visit(VariableDeclarationStatement const& _variableDeclarationStatement)
{
if (_variableDeclarationStatement.initialValue())
m_errorReporter.syntaxError(0000_error, _variableDeclarationStatement.initialValue()->location(), "Variable declarations with initial value not supported.");
if (_variableDeclarationStatement.declarations().size() == 1)
{
if (!_variableDeclarationStatement.declarations().front())
m_errorReporter.syntaxError(0000_error, _variableDeclarationStatement.initialValue()->location(), "Variable declaration has to declare a single variable.");
}
else
m_errorReporter.syntaxError(0000_error, _variableDeclarationStatement.initialValue()->location(), "Variable declarations can only declare a single variable.");
return true;
}
bool SyntaxRestrictor::visit(VariableDeclaration const& _variableDeclaration)
{
if (_variableDeclaration.value())
m_errorReporter.syntaxError(0000_error, _variableDeclaration.value()->location(), "Variable declarations with initial value not supported.");
if (_variableDeclaration.isStateVariable())
m_errorReporter.syntaxError(0000_error, _variableDeclaration.location(), "State variables are not supported.");
if (!_variableDeclaration.isLocalVariable())
m_errorReporter.syntaxError(0000_error, _variableDeclaration.location(), "Only local variables are supported.");
if (_variableDeclaration.mutability() != VariableDeclaration::Mutability::Mutable)
m_errorReporter.syntaxError(0000_error, _variableDeclaration.location(), "Only mutable variables are supported.");
if (_variableDeclaration.isIndexed())
m_errorReporter.syntaxError(0000_error, _variableDeclaration.location(), "Indexed variables are not supported.");
if (!_variableDeclaration.noVisibilitySpecified())
m_errorReporter.syntaxError(0000_error, _variableDeclaration.location(), "Variables with visibility not supported.");
if (_variableDeclaration.overrides())
m_errorReporter.syntaxError(0000_error, _variableDeclaration.location(), "Variables with override specifier not supported.");
if (_variableDeclaration.referenceLocation() != VariableDeclaration::Location::Unspecified)
m_errorReporter.syntaxError(0000_error, _variableDeclaration.location(), "Variables with reference location not supported.");
return true;
}
@@ -0,0 +1,55 @@
/*
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/>.
*/
// SPDX-License-Identifier: GPL-3.0
#pragma once
#include <libsolidity/ast/ASTVisitor.h>
#include <liblangutil/ErrorReporter.h>
#include <liblangutil/Exceptions.h>
namespace solidity::frontend::experimental
{
class SyntaxRestrictor: public ASTConstVisitor
{
public:
/// @param _errorReporter provides the error logging functionality.
explicit SyntaxRestrictor(langutil::ErrorReporter& _errorReporter): m_errorReporter(_errorReporter) {}
bool check(ASTNode const& _astRoot);
private:
/// Default visit will reject all AST nodes that are not explicitly allowed.
bool visitNode(ASTNode const& _node) override;
bool visit(SourceUnit const&) override { return true; }
bool visit(PragmaDirective const&) override { return true; }
bool visit(ImportDirective const&) override { return true; }
bool visit(ContractDefinition const& _contractDefinition) override;
bool visit(FunctionDefinition const& _functionDefinition) override;
bool visit(Block const&) override { return true; }
bool visit(InlineAssembly const&) override { return true; }
bool visit(Identifier const&) override { return true; }
bool visit(VariableDeclarationStatement const&) override;
bool visit(VariableDeclaration const&) override;
bool visit(ElementaryTypeName const&) override { return true; }
langutil::ErrorReporter& m_errorReporter;
};
}
@@ -0,0 +1,37 @@
/*
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/>.
*/
// SPDX-License-Identifier: GPL-3.0
#include <libsolidity/analysis/experimental/TypeInference.h>
#include <liblangutil/Exceptions.h>
using namespace solidity::frontend;
using namespace solidity::frontend::experimental;
using namespace solidity::langutil;
bool TypeInference::visitNode(ASTNode const& _node)
{
m_errorReporter.typeError(0000_error, _node.location(), "Unsupported AST node during type inference.");
return false;
}
bool TypeInference::visit(VariableDeclaration const&)
{
// m_env.assignType(&_varialeDeclaration, m_env.lookupType(_varialeDeclaration.typeName()));
return false;
}
@@ -0,0 +1,111 @@
/*
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/>.
*/
// SPDX-License-Identifier: GPL-3.0
#pragma once
#include <libsolidity/ast/ASTVisitor.h>
#include <liblangutil/ErrorReporter.h>
#include <range/v3/span.hpp>
namespace solidity::frontend::experimental
{
class GlobalTypeContext;
struct SumType;
struct TupleType;
struct FunctionType;
struct WordType;
struct UserDefinedType;
struct TypeVariable;
struct FreeType;
using Type = std::variant<SumType, TupleType, FunctionType, WordType, UserDefinedType, TypeVariable, FreeType>;
struct SumType
{
std::vector<Type const*> alternatives;
};
struct TupleType
{
std::vector<Type const*> components;
};
struct FunctionType
{
Type const* codomain = nullptr;
Type const* domain = nullptr;
};
struct WordType
{
};
struct UserDefinedType
{
Declaration const* declaration = nullptr;
std::vector<Type const*> arguments;
};
struct TypeVariable
{
uint64_t index = 0;
};
struct FreeType
{
uint64_t index = 0;
};
Type unify(Type _a, Type _b)
{
}
class TypeEnvironment
{
public:
TypeEnvironment() {}
void assignType(Declaration const* _declaration, Type _typeAssignment)
{
m_types.emplace(std::piecewise_construct, std::forward_as_tuple(_declaration), std::forward_as_tuple(std::move(_typeAssignment)));
}
private:
uint64_t m_numTypeVariables = 0;
std::map<Declaration const*, Type> m_types;
};
class TypeInference: public ASTConstVisitor
{
public:
TypeInference(langutil::ErrorReporter& _errorReporter): m_errorReporter(_errorReporter) {}
private:
bool visit(Block const&) override { return true; }
bool visit(VariableDeclarationStatement const&) override { return true; }
bool visit(VariableDeclaration const& _variableDeclaration) override;
bool visitNode(ASTNode const& _node) override;
private:
langutil::ErrorReporter& m_errorReporter;
TypeEnvironment m_env;
};
}