From e0727076d359826ea0dfdc724004131a61232179 Mon Sep 17 00:00:00 2001 From: Alex Beregszaszi Date: Sun, 26 Jun 2022 23:10:10 +0200 Subject: [PATCH] experimental flag --- libsolidity/analysis/GlobalContext.cpp | 20 +++++++++++++------- libsolidity/analysis/GlobalContext.h | 4 +++- libsolidity/ast/ExperimentalFeatures.h | 2 ++ libsolidity/interface/CompilerStack.cpp | 2 +- 4 files changed, 19 insertions(+), 9 deletions(-) diff --git a/libsolidity/analysis/GlobalContext.cpp b/libsolidity/analysis/GlobalContext.cpp index 58ebea7b1..f3e7f8992 100644 --- a/libsolidity/analysis/GlobalContext.cpp +++ b/libsolidity/analysis/GlobalContext.cpp @@ -65,19 +65,18 @@ int magicVariableToID(std::string const& _name) solAssert(false, "Unknown magic variable: \"" + _name + "\"."); } -inline vector> constructMagicVariables() +inline vector> constructMagicVariables(bool _stdlib) { static auto const magicVarDecl = [](string const& _name, Type const* _type) { return make_shared(magicVariableToID(_name), _name, _type); }; - return { + vector> list = { magicVarDecl("abi", TypeProvider::magic(MagicType::Kind::ABI)), magicVarDecl("addmod", TypeProvider::function(strings{"uint256", "uint256", "uint256"}, strings{"uint256"}, FunctionType::Kind::AddMod, StateMutability::Pure)), magicVarDecl("assert", TypeProvider::function(strings{"bool"}, strings{}, FunctionType::Kind::Assert, StateMutability::Pure)), magicVarDecl("block", TypeProvider::magic(MagicType::Kind::Block)), magicVarDecl("blockhash", TypeProvider::function(strings{"uint256"}, strings{"bytes32"}, FunctionType::Kind::BlockHash, StateMutability::View)), - magicVarDecl("ecrecover", TypeProvider::function(strings{"bytes32", "uint8", "bytes32", "bytes32"}, strings{"address"}, FunctionType::Kind::ECRecover, StateMutability::Pure)), magicVarDecl("gasleft", TypeProvider::function(strings(), strings{"uint256"}, FunctionType::Kind::GasLeft, StateMutability::View)), magicVarDecl("keccak256", TypeProvider::function(strings{"bytes memory"}, strings{"bytes32"}, FunctionType::Kind::KECCAK256, StateMutability::Pure)), magicVarDecl("msg", TypeProvider::magic(MagicType::Kind::Message)), @@ -87,10 +86,7 @@ inline vector> constructMagicVariable magicVarDecl("require", TypeProvider::function(strings{"bool", "string memory"}, strings{}, FunctionType::Kind::Require, StateMutability::Pure)), magicVarDecl("revert", TypeProvider::function(strings(), strings(), FunctionType::Kind::Revert, StateMutability::Pure)), magicVarDecl("revert", TypeProvider::function(strings{"string memory"}, strings(), FunctionType::Kind::Revert, StateMutability::Pure)), - magicVarDecl("ripemd160", TypeProvider::function(strings{"bytes memory"}, strings{"bytes20"}, FunctionType::Kind::RIPEMD160, StateMutability::Pure)), magicVarDecl("selfdestruct", TypeProvider::function(strings{"address payable"}, strings{}, FunctionType::Kind::Selfdestruct)), - magicVarDecl("sha256", TypeProvider::function(strings{"bytes memory"}, strings{"bytes32"}, FunctionType::Kind::SHA256, StateMutability::Pure)), - magicVarDecl("sha3", TypeProvider::function(strings{"bytes memory"}, strings{"bytes32"}, FunctionType::Kind::KECCAK256, StateMutability::Pure)), magicVarDecl("suicide", TypeProvider::function(strings{"address payable"}, strings{}, FunctionType::Kind::Selfdestruct)), magicVarDecl("tx", TypeProvider::magic(MagicType::Kind::Transaction)), // Accepts a MagicType that can be any contract type or an Integer type and returns a @@ -103,11 +99,21 @@ inline vector> constructMagicVariable FunctionType::Options::withArbitraryParameters() )), }; + vector> precompiles = { + magicVarDecl("ecrecover", TypeProvider::function(strings{"bytes32", "uint8", "bytes32", "bytes32"}, strings{"address"}, FunctionType::Kind::ECRecover, StateMutability::Pure)), + magicVarDecl("ripemd160", TypeProvider::function(strings{"bytes memory"}, strings{"bytes20"}, FunctionType::Kind::RIPEMD160, StateMutability::Pure)), + magicVarDecl("sha256", TypeProvider::function(strings{"bytes memory"}, strings{"bytes32"}, FunctionType::Kind::SHA256, StateMutability::Pure)), + magicVarDecl("sha3", TypeProvider::function(strings{"bytes memory"}, strings{"bytes32"}, FunctionType::Kind::KECCAK256, StateMutability::Pure)), + }; + if (!_stdlib) { + return list; + } + return list + precompiles; } } -GlobalContext::GlobalContext(): m_magicVariables{constructMagicVariables()} +GlobalContext::GlobalContext(bool _stdlib): m_magicVariables{constructMagicVariables(_stdlib)} { } diff --git a/libsolidity/analysis/GlobalContext.h b/libsolidity/analysis/GlobalContext.h index 69344537c..4eabf5668 100644 --- a/libsolidity/analysis/GlobalContext.h +++ b/libsolidity/analysis/GlobalContext.h @@ -47,7 +47,9 @@ public: GlobalContext(GlobalContext const&) = delete; GlobalContext& operator=(GlobalContext const&) = delete; - GlobalContext(); + /// Set the stdlib flag to true if globals should not be populated by features provided + /// in the stdlib. + GlobalContext(bool _stdlib); void setCurrentContract(ContractDefinition const& _contract); void resetCurrentContract() { m_currentContract = nullptr; } MagicVariableDeclaration const* currentThis() const; diff --git a/libsolidity/ast/ExperimentalFeatures.h b/libsolidity/ast/ExperimentalFeatures.h index 772bc4e08..b71c17588 100644 --- a/libsolidity/ast/ExperimentalFeatures.h +++ b/libsolidity/ast/ExperimentalFeatures.h @@ -31,6 +31,7 @@ enum class ExperimentalFeature { ABIEncoderV2, // new ABI encoder that makes use of Yul SMTChecker, + Stdlib, Test, TestOnlyAnalysis }; @@ -46,6 +47,7 @@ static std::map const ExperimentalFeatureNames { { "ABIEncoderV2", ExperimentalFeature::ABIEncoderV2 }, { "SMTChecker", ExperimentalFeature::SMTChecker }, + { "stdlib", ExperimentalFeature::Stdlib }, { "__test", ExperimentalFeature::Test }, { "__testOnlyAnalysis", ExperimentalFeature::TestOnlyAnalysis }, }; diff --git a/libsolidity/interface/CompilerStack.cpp b/libsolidity/interface/CompilerStack.cpp index 22321a16d..a3d05bd23 100644 --- a/libsolidity/interface/CompilerStack.cpp +++ b/libsolidity/interface/CompilerStack.cpp @@ -433,7 +433,7 @@ bool CompilerStack::analyze() if (source->ast && !syntaxChecker.checkSyntax(*source->ast)) noErrors = false; - m_globalContext = make_shared(); + m_globalContext = make_shared(false); // We need to keep the same resolver during the whole process. NameAndTypeResolver resolver(*m_globalContext, m_evmVersion, m_errorReporter); for (Source const* source: m_sourceOrder)