Skip to content

Commit df92007

Browse files
committed
Pass evmVersion to GlobalContext
1 parent 267d782 commit df92007

File tree

6 files changed

+23
-21
lines changed

6 files changed

+23
-21
lines changed

libsolidity/analysis/GlobalContext.cpp

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -63,14 +63,21 @@ int magicVariableToID(std::string const& _name)
6363
else
6464
solAssert(false, "Unknown magic variable: \"" + _name + "\".");
6565
}
66+
}
6667

67-
inline std::vector<std::shared_ptr<MagicVariableDeclaration const>> constructMagicVariables()
68+
GlobalContext::GlobalContext(langutil::EVMVersion _evmVersion):
69+
m_evmVersion(_evmVersion),
70+
m_magicVariables{constructMagicVariables()}
6871
{
69-
static auto const magicVarDecl = [](std::string const& _name, Type const* _type) {
72+
}
73+
74+
std::vector<std::shared_ptr<MagicVariableDeclaration const>> GlobalContext::constructMagicVariables() const
75+
{
76+
static auto const magicVarDecl = [&](std::string const& _name, Type const* _type) {
7077
return std::make_shared<MagicVariableDeclaration>(magicVariableToID(_name), _name, _type);
7178
};
7279

73-
return {
80+
std::vector<std::shared_ptr<MagicVariableDeclaration const>> magicVariableDeclarations = {
7481
magicVarDecl("abi", TypeProvider::magic(MagicType::Kind::ABI)),
7582
magicVarDecl("addmod", TypeProvider::function(strings{"uint256", "uint256", "uint256"}, strings{"uint256"}, FunctionType::Kind::AddMod, StateMutability::Pure)),
7683
magicVarDecl("assert", TypeProvider::function(strings{"bool"}, strings{}, FunctionType::Kind::Assert, StateMutability::Pure)),
@@ -92,7 +99,6 @@ inline std::vector<std::shared_ptr<MagicVariableDeclaration const>> constructMag
9299
magicVarDecl("sha3", TypeProvider::function(strings{"bytes memory"}, strings{"bytes32"}, FunctionType::Kind::KECCAK256, StateMutability::Pure)),
93100
magicVarDecl("suicide", TypeProvider::function(strings{"address payable"}, strings{}, FunctionType::Kind::Selfdestruct)),
94101
magicVarDecl("tx", TypeProvider::magic(MagicType::Kind::Transaction)),
95-
magicVarDecl("blobhash", TypeProvider::function(strings{"uint256"}, strings{"bytes32"}, FunctionType::Kind::BlobHash, StateMutability::View)),
96102
// Accepts a MagicType that can be any contract type or an Integer type and returns a
97103
// MagicType. The TypeChecker handles the correctness of the input and output types.
98104
magicVarDecl("type", TypeProvider::function(
@@ -103,12 +109,13 @@ inline std::vector<std::shared_ptr<MagicVariableDeclaration const>> constructMag
103109
FunctionType::Options::withArbitraryParameters()
104110
)),
105111
};
106-
}
107112

108-
}
113+
if (m_evmVersion >= langutil::EVMVersion::cancun())
114+
magicVariableDeclarations.push_back(
115+
magicVarDecl("blobhash", TypeProvider::function(strings{"uint256"}, strings{"bytes32"}, FunctionType::Kind::BlobHash, StateMutability::View))
116+
);
109117

110-
GlobalContext::GlobalContext(): m_magicVariables{constructMagicVariables()}
111-
{
118+
return magicVariableDeclarations;
112119
}
113120

114121
void GlobalContext::setCurrentContract(ContractDefinition const& _contract)

libsolidity/analysis/GlobalContext.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323

2424
#pragma once
2525

26+
#include <liblangutil/EVMVersion.h>
2627
#include <libsolidity/ast/ASTForward.h>
2728
#include <map>
2829
#include <memory>
@@ -47,7 +48,8 @@ class GlobalContext
4748
GlobalContext(GlobalContext const&) = delete;
4849
GlobalContext& operator=(GlobalContext const&) = delete;
4950

50-
GlobalContext();
51+
GlobalContext(langutil::EVMVersion _evmVersion);
52+
std::vector<std::shared_ptr<MagicVariableDeclaration const>> constructMagicVariables() const;
5153
void setCurrentContract(ContractDefinition const& _contract);
5254
void resetCurrentContract() { m_currentContract = nullptr; }
5355
MagicVariableDeclaration const* currentThis() const;
@@ -57,6 +59,7 @@ class GlobalContext
5759
std::vector<Declaration const*> declarations() const;
5860

5961
private:
62+
langutil::EVMVersion m_evmVersion;
6063
std::vector<std::shared_ptr<MagicVariableDeclaration const>> m_magicVariables;
6164
ContractDefinition const* m_currentContract = nullptr;
6265
std::map<ContractDefinition const*, std::shared_ptr<MagicVariableDeclaration const>> mutable m_thisPointer;

libsolidity/analysis/ReferencesResolver.cpp

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -404,15 +404,7 @@ void ReferencesResolver::operator()(yul::VariableDeclaration const& _varDecl)
404404
SecondarySourceLocation ssl;
405405
for (auto const* decl: declarations)
406406
ssl.append("The shadowed declaration is here:", decl->location());
407-
if (identifier.name.str() == "blobhash" && !m_evmVersion.hasBlobHash())
408-
m_errorReporter.warning(
409-
7527_error,
410-
nativeLocationOf(identifier),
411-
"\"blobhash\" was introduced as builtin function in EVM version Cancun "
412-
"but you are currently using EVM version " + m_evmVersion.name() +
413-
" and it will not behave as expected for EVM version >= Cancun."
414-
);
415-
else if (!ssl.infos.empty())
407+
if (!ssl.infos.empty())
416408
m_errorReporter.declarationError(
417409
3859_error,
418410
nativeLocationOf(identifier),

libsolidity/interface/CompilerStack.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -469,7 +469,7 @@ bool CompilerStack::analyze()
469469
if (source->ast && !syntaxChecker.checkSyntax(*source->ast))
470470
noErrors = false;
471471

472-
m_globalContext = std::make_shared<GlobalContext>();
472+
m_globalContext = std::make_shared<GlobalContext>(m_evmVersion);
473473
// We need to keep the same resolver during the whole process.
474474
NameAndTypeResolver resolver(*m_globalContext, m_evmVersion, m_errorReporter, experimentalSolidity);
475475
for (Source const* source: m_sourceOrder)

test/libsolidity/Assembly.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ evmasm::AssemblyItems compileContract(std::shared_ptr<CharStream> _sourceCode)
6363

6464
Scoper::assignScopes(*sourceUnit);
6565
BOOST_REQUIRE(SyntaxChecker(errorReporter, false).checkSyntax(*sourceUnit));
66-
GlobalContext globalContext;
66+
GlobalContext globalContext(solidity::test::CommonOptions::get().evmVersion());
6767
NameAndTypeResolver resolver(globalContext, solidity::test::CommonOptions::get().evmVersion(), errorReporter, false);
6868
DeclarationTypeChecker declarationTypeChecker(errorReporter, solidity::test::CommonOptions::get().evmVersion());
6969
solAssert(!Error::containsErrors(errorReporter.errors()), "");

test/libsolidity/SolidityExpressionCompiler.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ bytes compileFirstExpression(
124124

125125
ErrorList errors;
126126
ErrorReporter errorReporter(errors);
127-
GlobalContext globalContext;
127+
GlobalContext globalContext(solidity::test::CommonOptions::get().evmVersion());
128128
Scoper::assignScopes(*sourceUnit);
129129
BOOST_REQUIRE(SyntaxChecker(errorReporter, false).checkSyntax(*sourceUnit));
130130
NameAndTypeResolver resolver(globalContext, solidity::test::CommonOptions::get().evmVersion(), errorReporter, false);

0 commit comments

Comments
 (0)