Merge pull request #14538 from ethereum/experimentalAnalysisBasicInfrastructure

New Analysis basic infrastructure
This commit is contained in:
Daniel 2023-09-13 13:00:56 +02:00 committed by GitHub
commit 9bce5f91dc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
16 changed files with 329 additions and 157 deletions

View File

@ -1019,15 +1019,28 @@ std::tuple<Token, unsigned, unsigned> Scanner::scanIdentifierOrKeyword()
while (isIdentifierPart(m_char) || (m_char == '.' && m_kind == ScannerKind::Yul))
addLiteralCharAndAdvance();
literal.complete();
auto const token = TokenTraits::fromIdentifierOrKeyword(m_tokens[NextNext].literal);
if (m_kind == ScannerKind::Yul)
switch (m_kind)
{
case ScannerKind::Solidity:
// Turn experimental Solidity keywords that are not keywords in legacy Solidity into identifiers.
if (TokenTraits::isExperimentalSolidityOnlyKeyword(std::get<0>(token)))
return std::make_tuple(Token::Identifier, 0, 0);
break;
case ScannerKind::Yul:
// Turn Solidity identifier into a Yul keyword
if (m_tokens[NextNext].literal == "leave")
return std::make_tuple(Token::Leave, 0, 0);
// Turn non-Yul keywords into identifiers.
if (!TokenTraits::isYulKeyword(std::get<0>(token)))
return std::make_tuple(Token::Identifier, 0, 0);
break;
case ScannerKind::ExperimentalSolidity:
// Turn legacy Solidity keywords that are not keywords in experimental Solidity into identifiers.
if (!TokenTraits::isExperimentalSolidityKeyword(std::get<0>(token)))
return std::make_tuple(Token::Identifier, 0, 0);
break;
}
return token;
}

View File

@ -69,7 +69,8 @@ class ParserRecorder;
enum class ScannerKind
{
Solidity,
Yul
Yul,
ExperimentalSolidity
};
enum class ScannerError

View File

@ -268,6 +268,8 @@ namespace solidity::langutil
/* Yul-specific tokens, but not keywords. */ \
T(Leave, "leave", 0) \
\
T(NonExperimentalEnd, nullptr, 0) /* used as non-experimental enum end marker */ \
T(ExperimentalEnd, nullptr, 0) /* used as experimental enum end marker */ \
/* Illegal token - not able to scan. */ \
T(Illegal, "ILLEGAL", 0) \
\
@ -323,6 +325,39 @@ namespace TokenTraits
tok == Token::TrueLiteral || tok == Token::FalseLiteral || tok == Token::HexStringLiteral || tok == Token::Hex;
}
constexpr bool isExperimentalSolidityKeyword(Token token)
{
return
token == Token::Assembly ||
token == Token::Contract ||
token == Token::External ||
token == Token::Fallback ||
token == Token::Pragma ||
token == Token::Import ||
token == Token::As ||
token == Token::Function ||
token == Token::Let ||
token == Token::Return ||
token == Token::Type ||
token == Token::If ||
token == Token::Else ||
token == Token::Do ||
token == Token::While ||
token == Token::For ||
token == Token::Continue ||
token == Token::Break;
// TODO: see isExperimentalSolidityKeyword below
// || (token > Token::NonExperimentalEnd && token < Token::ExperimentalEnd);
}
constexpr bool isExperimentalSolidityOnlyKeyword(Token)
{
// TODO: use token > Token::NonExperimentalEnd && token < Token::ExperimentalEnd
// as soon as other experimental tokens are added. For now the comparison generates
// a warning from clang because it is always false.
return false;
}
bool isYulKeyword(std::string const& _literal);
Token AssignmentToBinaryOp(Token op);

View File

@ -101,6 +101,8 @@ set(sources
codegen/ir/IRLValue.h
codegen/ir/IRVariable.cpp
codegen/ir/IRVariable.h
experimental/analysis/Analysis.cpp
experimental/analysis/Analysis.h
formal/ArraySlicePredicate.cpp
formal/ArraySlicePredicate.h
formal/BMC.cpp
@ -186,4 +188,3 @@ set(sources
add_library(solidity ${sources})
target_link_libraries(solidity PUBLIC yul evmasm langutil smtutil solutil Boost::boost fmt::fmt-header-only Threads::Threads)

View File

@ -0,0 +1,34 @@
/*
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/experimental/analysis/Analysis.h>
#include <liblangutil/ErrorReporter.h>
using namespace solidity::langutil;
using namespace solidity::frontend::experimental;
bool Analysis::check(std::vector<std::shared_ptr<SourceUnit const>> const&)
{
m_errorReporter.error(
6547_error,
Error::Type::UnimplementedFeatureError,
SourceLocation{},
"Experimental Analysis is not implemented yet."
);
return false;
}

View File

@ -0,0 +1,49 @@
/*
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 <vector>
#include <memory>
namespace solidity::frontend
{
class SourceUnit;
}
namespace solidity::langutil
{
class ErrorReporter;
}
namespace solidity::frontend::experimental
{
class Analysis
{
public:
Analysis(langutil::ErrorReporter& _errorReporter):
m_errorReporter(_errorReporter)
{}
bool check(std::vector<std::shared_ptr<SourceUnit const>> const& _sourceUnits);
private:
langutil::ErrorReporter& m_errorReporter;
};
}

View File

@ -56,6 +56,8 @@
#include <libsolidity/interface/Version.h>
#include <libsolidity/parsing/Parser.h>
#include <libsolidity/experimental/analysis/Analysis.h>
#include <libsolidity/codegen/ir/Common.h>
#include <libsolidity/codegen/ir/IRGenerator.h>
@ -325,6 +327,7 @@ void CompilerStack::reset(bool _keepSettings)
m_metadataHash = MetadataHash::IPFS;
m_stopAfter = State::CompilationSuccessful;
}
m_experimentalAnalysis.reset();
m_globalContext.reset();
m_sourceOrder.clear();
m_contracts.clear();
@ -449,6 +452,8 @@ bool CompilerStack::analyze()
try
{
bool experimentalSolidity = !m_sourceOrder.empty() && m_sourceOrder.front()->ast->experimentalSolidity();
SyntaxChecker syntaxChecker(m_errorReporter, m_optimiserSettings.runYulOptimiser);
for (Source const* source: m_sourceOrder)
if (source->ast && !syntaxChecker.checkSyntax(*source->ast))
@ -480,12 +485,40 @@ bool CompilerStack::analyze()
if (source->ast && !resolver.resolveNamesAndTypes(*source->ast))
return false;
if (experimentalSolidity)
{
if (!analyzeExperimental())
noErrors = false;
}
else if (!analyzeLegacy(noErrors))
noErrors = false;
}
catch (FatalError const&)
{
if (m_errorReporter.errors().empty())
throw; // Something is weird here, rather throw again.
noErrors = false;
}
if (!noErrors)
return false;
m_stackState = AnalysisSuccessful;
return true;
}
bool CompilerStack::analyzeLegacy(bool _noErrorsSoFar)
{
bool noErrors = _noErrorsSoFar;
DeclarationTypeChecker declarationTypeChecker(m_errorReporter, m_evmVersion);
for (Source const* source: m_sourceOrder)
if (source->ast && !declarationTypeChecker.check(*source->ast))
return false;
// Requires DeclarationTypeChecker to have run
DocStringTagParser docStringTagParser(m_errorReporter);
for (Source const* source: m_sourceOrder)
if (source->ast && !docStringTagParser.validateDocStringsUsingTypes(*source->ast))
noErrors = false;
@ -615,19 +648,19 @@ bool CompilerStack::analyze()
modelChecker.analyze(*source->ast);
m_unhandledSMTLib2Queries += modelChecker.unhandledQueries();
}
}
catch (FatalError const&)
{
if (m_errorReporter.errors().empty())
throw; // Something is weird here, rather throw again.
noErrors = false;
}
if (!noErrors)
return false;
return noErrors;
}
m_stackState = AnalysisSuccessful;
return true;
bool CompilerStack::analyzeExperimental()
{
solAssert(!m_experimentalAnalysis);
m_experimentalAnalysis = std::make_unique<experimental::Analysis>(m_errorReporter);
std::vector<std::shared_ptr<SourceUnit const>> sourceAsts;
for (Source const* source: m_sourceOrder)
if (source->ast)
sourceAsts.emplace_back(source->ast);
return m_experimentalAnalysis->check(sourceAsts);
}
bool CompilerStack::parseAndAnalyze(State _stopAfter)
@ -694,9 +727,13 @@ bool CompilerStack::compile(State _stopAfter)
if (m_viaIR)
generateEVMFromIR(*contract);
else
{
if (m_experimentalAnalysis)
solThrow(CompilerError, "Legacy codegen after experimental analysis is unsupported.");
compileContract(*contract, otherCompilers);
}
}
}
catch (Error const& _error)
{
if (_error.type() != Error::Type::CodeGenerationError)
@ -1429,6 +1466,9 @@ void CompilerStack::generateIR(ContractDefinition const& _contract)
{
solAssert(m_stackState >= AnalysisSuccessful, "");
if (m_experimentalAnalysis)
solThrow(CompilerError, "IR codegen after experimental analysis is unsupported.");
Contract& compiledContract = m_contracts.at(_contract.fullyQualifiedName());
if (!compiledContract.yulIR.empty())
return;

View File

@ -81,6 +81,10 @@ class Compiler;
class GlobalContext;
class Natspec;
class DeclarationContainer;
namespace experimental
{
class Analysis;
}
/**
* Easy to use and self-contained Solidity compiler with as few header dependencies as possible.
@ -407,6 +411,14 @@ private:
/// @returns true if the contract is requested to be compiled.
bool isRequestedContract(ContractDefinition const& _contract) const;
/// Perform the analysis steps of legacy language mode.
/// @returns false on error.
bool analyzeLegacy(bool _noErrorsSoFar);
/// Perform the analysis steps of experimental language mode.
/// @returns false on error.
bool analyzeExperimental();
/// Assembles the contract.
/// This function should only be internally called by compileContract and generateEVMFromIR.
void assembleYul(
@ -500,6 +512,7 @@ private:
langutil::ErrorList m_errorList;
langutil::ErrorReporter m_errorReporter;
std::unique_ptr<experimental::Analysis> m_experimentalAnalysis;
bool m_metadataLiteralSources = false;
MetadataHash m_metadataHash = MetadataHash::IPFS;
langutil::DebugInfoSelection m_debugInfoSelection = langutil::DebugInfoSelection::Default();

View File

@ -115,8 +115,9 @@ std::unique_ptr<Block> Parser::parseInline(std::shared_ptr<Scanner> const& _scan
{
m_recursionDepth = 0;
auto previousScannerKind = _scanner->scannerKind();
_scanner->setScannerMode(ScannerKind::Yul);
ScopeGuard resetScanner([&]{ _scanner->setScannerMode(ScannerKind::Solidity); });
ScopeGuard resetScanner([&]{ _scanner->setScannerMode(previousScannerKind); });
try
{

View File

@ -1,21 +0,0 @@
{
"absolutePath": "a",
"experimentalSolidity": true,
"exportedSymbols": {},
"id": 2,
"nodeType": "SourceUnit",
"nodes":
[
{
"id": 1,
"literals":
[
"experimental",
"solidity"
],
"nodeType": "PragmaDirective",
"src": "0:29:1"
}
],
"src": "0:70:1"
}

View File

@ -2,3 +2,4 @@ pragma experimental solidity;
// ====
// EVMVersion: >=constantinople
// ----
// failAfter: Parsed

View File

@ -6,3 +6,4 @@ import std.stub;
// ----
// Warning 2264: (std.stub:63-92): Experimental features are turned on. Do not use experimental features on live deployments.
// Warning 2264: (0-29): Experimental features are turned on. Do not use experimental features on live deployments.
// UnimplementedFeatureError 6547: Experimental Analysis is not implemented yet.

View File

@ -6,3 +6,4 @@ import std.stub as stub;
// ----
// Warning 2264: (std.stub:63-92): Experimental features are turned on. Do not use experimental features on live deployments.
// Warning 2264: (0-29): Experimental features are turned on. Do not use experimental features on live deployments.
// UnimplementedFeatureError 6547: Experimental Analysis is not implemented yet.

View File

@ -6,3 +6,4 @@ import { identity } from std.stub;
// ----
// Warning 2264: (std.stub:63-92): Experimental features are turned on. Do not use experimental features on live deployments.
// Warning 2264: (0-29): Experimental features are turned on. Do not use experimental features on live deployments.
// UnimplementedFeatureError 6547: Experimental Analysis is not implemented yet.

View File

@ -6,3 +6,4 @@ import * as stub from std.stub;
// ----
// Warning 2264: (std.stub:63-92): Experimental features are turned on. Do not use experimental features on live deployments.
// Warning 2264: (0-29): Experimental features are turned on. Do not use experimental features on live deployments.
// UnimplementedFeatureError 6547: Experimental Analysis is not implemented yet.

View File

@ -3,3 +3,4 @@ pragma experimental solidity;
// EVMVersion: >=constantinople
// ----
// Warning 2264: (0-29): Experimental features are turned on. Do not use experimental features on live deployments.
// UnimplementedFeatureError 6547: Experimental Analysis is not implemented yet.