2014-11-20 17:33:23 +00:00
|
|
|
/*
|
2016-11-18 23:13:20 +00:00
|
|
|
This file is part of solidity.
|
2014-11-20 17:33:23 +00:00
|
|
|
|
2016-11-18 23:13:20 +00:00
|
|
|
solidity is free software: you can redistribute it and/or modify
|
2014-11-20 17:33:23 +00:00
|
|
|
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.
|
|
|
|
|
2016-11-18 23:13:20 +00:00
|
|
|
solidity is distributed in the hope that it will be useful,
|
2014-11-20 17:33:23 +00:00
|
|
|
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
|
2016-11-18 23:13:20 +00:00
|
|
|
along with solidity. If not, see <http://www.gnu.org/licenses/>.
|
2014-11-20 17:33:23 +00:00
|
|
|
*/
|
2020-07-17 14:54:12 +00:00
|
|
|
// SPDX-License-Identifier: GPL-3.0
|
2014-11-20 17:33:23 +00:00
|
|
|
/**
|
|
|
|
* @author Christian <c@ethdev.com>
|
2015-01-09 06:39:30 +00:00
|
|
|
* @author Gav Wood <g@ethdev.com>
|
2014-11-20 17:33:23 +00:00
|
|
|
* @date 2014
|
|
|
|
* Container of the (implicit and explicit) global objects.
|
|
|
|
*/
|
|
|
|
|
2015-10-20 22:21:52 +00:00
|
|
|
#include <libsolidity/analysis/GlobalContext.h>
|
2018-12-17 11:30:08 +00:00
|
|
|
|
2015-10-20 22:21:52 +00:00
|
|
|
#include <libsolidity/ast/AST.h>
|
2019-04-15 13:33:39 +00:00
|
|
|
#include <libsolidity/ast/TypeProvider.h>
|
2015-10-20 22:21:52 +00:00
|
|
|
#include <libsolidity/ast/Types.h>
|
2018-12-17 11:30:08 +00:00
|
|
|
#include <memory>
|
2014-11-20 17:33:23 +00:00
|
|
|
|
|
|
|
using namespace std;
|
|
|
|
|
2019-12-11 16:31:36 +00:00
|
|
|
namespace solidity::frontend
|
2014-11-20 17:33:23 +00:00
|
|
|
{
|
|
|
|
|
2020-12-08 20:06:10 +00:00
|
|
|
namespace
|
|
|
|
{
|
2019-09-11 19:16:35 +00:00
|
|
|
/// Magic variables get negative ids for easy differentiation
|
|
|
|
int magicVariableToID(std::string const& _name)
|
|
|
|
{
|
|
|
|
if (_name == "abi") return -1;
|
|
|
|
else if (_name == "addmod") return -2;
|
|
|
|
else if (_name == "assert") return -3;
|
|
|
|
else if (_name == "block") return -4;
|
|
|
|
else if (_name == "blockhash") return -5;
|
|
|
|
else if (_name == "ecrecover") return -6;
|
|
|
|
else if (_name == "gasleft") return -7;
|
|
|
|
else if (_name == "keccak256") return -8;
|
|
|
|
else if (_name == "msg") return -15;
|
|
|
|
else if (_name == "mulmod") return -16;
|
|
|
|
else if (_name == "now") return -17;
|
|
|
|
else if (_name == "require") return -18;
|
|
|
|
else if (_name == "revert") return -19;
|
|
|
|
else if (_name == "ripemd160") return -20;
|
|
|
|
else if (_name == "selfdestruct") return -21;
|
|
|
|
else if (_name == "sha256") return -22;
|
|
|
|
else if (_name == "sha3") return -23;
|
|
|
|
else if (_name == "suicide") return -24;
|
|
|
|
else if (_name == "super") return -25;
|
|
|
|
else if (_name == "tx") return -26;
|
|
|
|
else if (_name == "type") return -27;
|
|
|
|
else if (_name == "this") return -28;
|
|
|
|
else
|
|
|
|
solAssert(false, "Unknown magic variable: \"" + _name + "\".");
|
|
|
|
}
|
|
|
|
|
2019-04-15 13:33:39 +00:00
|
|
|
inline vector<shared_ptr<MagicVariableDeclaration const>> constructMagicVariables()
|
|
|
|
{
|
|
|
|
static auto const magicVarDecl = [](string const& _name, Type const* _type) {
|
2019-09-11 19:16:35 +00:00
|
|
|
return make_shared<MagicVariableDeclaration>(magicVariableToID(_name), _name, _type);
|
2019-04-15 13:33:39 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
return {
|
2019-04-17 11:40:50 +00:00
|
|
|
magicVarDecl("abi", TypeProvider::magic(MagicType::Kind::ABI)),
|
2021-12-20 11:40:43 +00:00
|
|
|
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)),
|
2019-04-17 11:40:50 +00:00
|
|
|
magicVarDecl("block", TypeProvider::magic(MagicType::Kind::Block)),
|
2021-12-20 11:40:43 +00:00
|
|
|
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)),
|
2019-04-17 11:40:50 +00:00
|
|
|
magicVarDecl("msg", TypeProvider::magic(MagicType::Kind::Message)),
|
2021-12-20 11:40:43 +00:00
|
|
|
magicVarDecl("mulmod", TypeProvider::function(strings{"uint256", "uint256", "uint256"}, strings{"uint256"}, FunctionType::Kind::MulMod, StateMutability::Pure)),
|
2019-04-15 16:10:43 +00:00
|
|
|
magicVarDecl("now", TypeProvider::uint256()),
|
2021-12-20 11:40:43 +00:00
|
|
|
magicVarDecl("require", TypeProvider::function(strings{"bool"}, strings{}, FunctionType::Kind::Require, StateMutability::Pure)),
|
|
|
|
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)),
|
2019-04-17 11:40:50 +00:00
|
|
|
magicVarDecl("selfdestruct", TypeProvider::function(strings{"address payable"}, strings{}, FunctionType::Kind::Selfdestruct)),
|
2021-12-20 11:40:43 +00:00
|
|
|
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)),
|
2019-04-17 11:40:50 +00:00
|
|
|
magicVarDecl("suicide", TypeProvider::function(strings{"address payable"}, strings{}, FunctionType::Kind::Selfdestruct)),
|
|
|
|
magicVarDecl("tx", TypeProvider::magic(MagicType::Kind::Transaction)),
|
2020-05-07 16:24:37 +00:00
|
|
|
// Accepts a MagicType that can be any contract type or an Integer type and returns a
|
|
|
|
// MagicType. The TypeChecker handles the correctness of the input and output types.
|
2019-04-17 11:40:50 +00:00
|
|
|
magicVarDecl("type", TypeProvider::function(
|
2020-05-07 16:24:37 +00:00
|
|
|
strings{},
|
|
|
|
strings{},
|
2019-04-15 13:33:39 +00:00
|
|
|
FunctionType::Kind::MetaType,
|
2021-12-20 11:40:43 +00:00
|
|
|
StateMutability::Pure,
|
|
|
|
FunctionType::Options::withArbitraryParameters()
|
2019-04-15 13:33:39 +00:00
|
|
|
)),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2020-12-08 20:06:10 +00:00
|
|
|
}
|
|
|
|
|
2019-04-15 13:33:39 +00:00
|
|
|
GlobalContext::GlobalContext(): m_magicVariables{constructMagicVariables()}
|
2014-11-20 17:33:23 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void GlobalContext::setCurrentContract(ContractDefinition const& _contract)
|
|
|
|
{
|
2014-11-21 18:14:56 +00:00
|
|
|
m_currentContract = &_contract;
|
2014-11-20 17:33:23 +00:00
|
|
|
}
|
|
|
|
|
2015-08-31 16:44:29 +00:00
|
|
|
vector<Declaration const*> GlobalContext::declarations() const
|
2014-11-20 17:33:23 +00:00
|
|
|
{
|
2014-12-06 01:32:51 +00:00
|
|
|
vector<Declaration const*> declarations;
|
2015-01-16 17:52:27 +00:00
|
|
|
declarations.reserve(m_magicVariables.size());
|
2020-05-11 12:59:06 +00:00
|
|
|
for (ASTPointer<MagicVariableDeclaration const> const& variable: m_magicVariables)
|
2014-11-21 18:14:56 +00:00
|
|
|
declarations.push_back(variable.get());
|
2014-11-20 17:33:23 +00:00
|
|
|
return declarations;
|
|
|
|
}
|
|
|
|
|
2015-08-31 16:44:29 +00:00
|
|
|
MagicVariableDeclaration const* GlobalContext::currentThis() const
|
2014-11-20 17:33:23 +00:00
|
|
|
{
|
2014-11-21 18:14:56 +00:00
|
|
|
if (!m_thisPointer[m_currentContract])
|
2020-05-04 16:38:00 +00:00
|
|
|
{
|
|
|
|
Type const* type = TypeProvider::emptyTuple();
|
|
|
|
if (m_currentContract)
|
|
|
|
type = TypeProvider::contract(*m_currentContract);
|
|
|
|
m_thisPointer[m_currentContract] =
|
|
|
|
make_shared<MagicVariableDeclaration>(magicVariableToID("this"), "this", type);
|
|
|
|
}
|
2014-11-21 18:14:56 +00:00
|
|
|
return m_thisPointer[m_currentContract].get();
|
|
|
|
}
|
|
|
|
|
2015-08-31 16:44:29 +00:00
|
|
|
MagicVariableDeclaration const* GlobalContext::currentSuper() const
|
2015-01-27 13:32:59 +00:00
|
|
|
{
|
|
|
|
if (!m_superPointer[m_currentContract])
|
2020-05-04 16:38:00 +00:00
|
|
|
{
|
|
|
|
Type const* type = TypeProvider::emptyTuple();
|
|
|
|
if (m_currentContract)
|
2020-10-14 09:17:53 +00:00
|
|
|
type = TypeProvider::typeType(TypeProvider::contract(*m_currentContract, true));
|
2020-05-04 16:38:00 +00:00
|
|
|
m_superPointer[m_currentContract] =
|
|
|
|
make_shared<MagicVariableDeclaration>(magicVariableToID("super"), "super", type);
|
|
|
|
}
|
2015-01-27 13:32:59 +00:00
|
|
|
return m_superPointer[m_currentContract].get();
|
|
|
|
}
|
|
|
|
|
2014-11-20 17:33:23 +00:00
|
|
|
}
|