This commit is contained in:
Daniel Kirchner 2023-06-14 12:48:38 +02:00
parent fb959b3066
commit 71f7bf7206
15 changed files with 430 additions and 12 deletions

View File

@ -268,6 +268,11 @@ namespace solidity::langutil
/* Yul-specific tokens, but not keywords. */ \ /* Yul-specific tokens, but not keywords. */ \
T(Leave, "leave", 0) \ T(Leave, "leave", 0) \
\ \
/* Experimental Solidity specific keywords. */ \
K(Word, "word", 0) \
K(StaticAssert, "static_assert", 0) \
T(ExperimentalEnd, nullptr, 0) /* used as experimental enum end marker */ \
\
/* Illegal token - not able to scan. */ \ /* Illegal token - not able to scan. */ \
T(Illegal, "ILLEGAL", 0) \ T(Illegal, "ILLEGAL", 0) \
\ \
@ -290,7 +295,7 @@ namespace TokenTraits
constexpr size_t count() { return static_cast<size_t>(Token::NUM_TOKENS); } constexpr size_t count() { return static_cast<size_t>(Token::NUM_TOKENS); }
// Predicates // Predicates
constexpr bool isElementaryTypeName(Token tok) { return Token::Int <= tok && tok < Token::TypesEnd; } constexpr bool isElementaryTypeName(Token tok) { return (Token::Int <= tok && tok < Token::TypesEnd) || tok == Token::Word; }
constexpr bool isAssignmentOp(Token tok) { return Token::Assign <= tok && tok <= Token::AssignMod; } constexpr bool isAssignmentOp(Token tok) { return Token::Assign <= tok && tok <= Token::AssignMod; }
constexpr bool isBinaryOp(Token op) { return Token::Comma <= op && op <= Token::Exp; } constexpr bool isBinaryOp(Token op) { return Token::Comma <= op && op <= Token::Exp; }
constexpr bool isCommutativeOp(Token op) { return op == Token::BitOr || op == Token::BitXor || op == Token::BitAnd || constexpr bool isCommutativeOp(Token op) { return op == Token::BitOr || op == Token::BitXor || op == Token::BitAnd ||
@ -324,11 +329,13 @@ namespace TokenTraits
} }
constexpr bool isExperimentalSolidityKeyword(Token tok) constexpr bool isExperimentalSolidityKeyword(Token tok)
{ {
return tok == Token::Assembly || tok == Token::Contract || tok == Token::External || tok == Token::Fallback; return tok == Token::Assembly || tok == Token::Contract || tok == Token::External || tok == Token::Fallback ||
tok == Token::Pragma || tok == Token::Import || tok == Token::As || tok == Token::Function ||
(tok >= Token::Word && tok < Token::ExperimentalEnd);
} }
constexpr bool isExperimentalSolidityOnlyKeyword(Token) constexpr bool isExperimentalSolidityOnlyKeyword(Token tok)
{ {
return false; return tok >= Token::Word && tok < Token::ExperimentalEnd;
} }
bool isYulKeyword(std::string const& _literal); bool isYulKeyword(std::string const& _literal);

View File

@ -48,6 +48,10 @@ set(sources
analysis/ViewPureChecker.h analysis/ViewPureChecker.h
analysis/experimental/Analysis.cpp analysis/experimental/Analysis.cpp
analysis/experimental/Analysis.h analysis/experimental/Analysis.h
analysis/experimental/TypeInference.cpp
analysis/experimental/TypeInference.h
analysis/experimental/SyntaxRestrictor.cpp
analysis/experimental/SyntaxRestrictor.h
ast/AST.cpp ast/AST.cpp
ast/AST.h ast/AST.h
ast/AST_accept.h ast/AST_accept.h
@ -94,6 +98,8 @@ set(sources
codegen/YulUtilFunctions.cpp codegen/YulUtilFunctions.cpp
codegen/experimental/IRGenerator.cpp codegen/experimental/IRGenerator.cpp
codegen/experimental/IRGenerator.h codegen/experimental/IRGenerator.h
codegen/experimental/IRGeneratorForStatements.cpp
codegen/experimental/IRGeneratorForStatements.h
codegen/ir/Common.cpp codegen/ir/Common.cpp
codegen/ir/Common.h codegen/ir/Common.h
codegen/ir/IRGenerator.cpp codegen/ir/IRGenerator.cpp

View File

@ -17,10 +17,15 @@
// SPDX-License-Identifier: GPL-3.0 // SPDX-License-Identifier: GPL-3.0
#include <libsolidity/analysis/experimental/Analysis.h> #include <libsolidity/analysis/experimental/Analysis.h>
#include <libsolidity/analysis/experimental/SyntaxRestrictor.h>
using namespace solidity::langutil; using namespace solidity::langutil;
using namespace solidity::frontend::experimental; 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; return true;
} }

View File

@ -17,6 +17,8 @@
// SPDX-License-Identifier: GPL-3.0 // SPDX-License-Identifier: GPL-3.0
#pragma once #pragma once
#include <cstdint>
namespace solidity::frontend namespace solidity::frontend
{ {
class ASTNode; class ASTNode;
@ -33,11 +35,14 @@ namespace solidity::frontend::experimental
class Analysis class Analysis
{ {
public: 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); bool check(ASTNode const& _ast);
private: private:
langutil::ErrorReporter& m_errorReporter; langutil::ErrorReporter& m_errorReporter;
uint64_t m_maxAstId = 0;
}; };
} }

View File

@ -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;
}

View File

@ -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;
};
}

View File

@ -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;
}

View File

@ -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;
};
}

View File

@ -0,0 +1,26 @@
/*
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/codegen/experimental/IRGeneratorForStatements.h>
using namespace std;
using namespace solidity;
using namespace solidity::util;
using namespace solidity::frontend;
using namespace solidity::frontend::experimental;
using namespace std::string_literals;

View File

@ -0,0 +1,38 @@
/*
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 <functional>
namespace solidity::frontend::experimental
{
class IRGeneratorForStatements: public ASTConstVisitor
{
public:
IRGeneratorForStatements() {}
private:
/// Default visit will reject all AST nodes that are not explicitly supported.
bool visitNode(ASTNode const& _node) override;
};
}

View File

@ -668,8 +668,8 @@ bool CompilerStack::analyzeLegacy(bool _noErrorsSoFar)
bool CompilerStack::analyzeExperimental() bool CompilerStack::analyzeExperimental()
{ {
bool noErrors = true; bool noErrors = true;
solAssert(m_maxAstId); solAssert(m_maxAstId && *m_maxAstId >= 0);
m_experimentalAnalysis = make_unique<experimental::Analysis>(m_errorReporter); m_experimentalAnalysis = make_unique<experimental::Analysis>(m_errorReporter, static_cast<uint64_t>(*m_maxAstId));
for (Source const* source: m_sourceOrder) for (Source const* source: m_sourceOrder)
if (source->ast) if (source->ast)
if (!m_experimentalAnalysis->check(*source->ast)) if (!m_experimentalAnalysis->check(*source->ast))

View File

@ -49,6 +49,8 @@ public:
ASTPointer<SourceUnit> parse(langutil::CharStream& _charStream); ASTPointer<SourceUnit> parse(langutil::CharStream& _charStream);
/// Returns the maximal AST node ID assigned so far
int64_t maxID() const { return m_currentNodeID; }
private: private:
class ASTNodeFactory; class ASTNodeFactory;
@ -198,8 +200,6 @@ private:
/// Returns the next AST node ID /// Returns the next AST node ID
int64_t nextID() { return ++m_currentNodeID; } int64_t nextID() { return ++m_currentNodeID; }
/// Returns the maximal AST node ID assigned so far
int64_t maxID() const { return m_currentNodeID; }
std::pair<LookAheadInfo, IndexAccessedPath> tryParseIndexAccessedPath(); std::pair<LookAheadInfo, IndexAccessedPath> tryParseIndexAccessedPath();
/// Performs limited look-ahead to distinguish between variable declaration and expression statement. /// Performs limited look-ahead to distinguish between variable declaration and expression statement.

View File

@ -3,7 +3,6 @@ pragma solidity >=0.0;
pragma experimental solidity; pragma experimental solidity;
function identity(uint256 x) pure returns (uint256) function identity()
{ {
return x;
} }

View File

@ -0,0 +1,15 @@
pragma experimental solidity;
contract C {
fallback() external {
word x;
assembly {
mstore(0, 42)
return(0, 32)
}
}
}
// ====
// compileViaYul: true
// ----
// () -> 42

View File

@ -0,0 +1,4 @@
function f() pure {
uint word; word;
uint static_assert; static_assert;
}