2017-02-17 15:05:22 +00:00
|
|
|
/*
|
|
|
|
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/>.
|
|
|
|
*/
|
2020-07-17 14:54:12 +00:00
|
|
|
// SPDX-License-Identifier: GPL-3.0
|
2017-02-17 15:05:22 +00:00
|
|
|
/**
|
|
|
|
* Scopes for identifiers.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
2018-11-14 13:59:30 +00:00
|
|
|
#include <liblangutil/Exceptions.h>
|
2017-02-17 15:05:22 +00:00
|
|
|
|
2018-10-29 14:12:02 +00:00
|
|
|
#include <libyul/YulString.h>
|
|
|
|
|
2017-02-17 15:05:22 +00:00
|
|
|
#include <functional>
|
|
|
|
#include <memory>
|
2019-10-28 10:39:30 +00:00
|
|
|
#include <optional>
|
2019-11-19 15:42:49 +00:00
|
|
|
#include <variant>
|
2017-02-17 15:05:22 +00:00
|
|
|
|
2019-12-11 16:31:36 +00:00
|
|
|
namespace solidity::yul
|
2017-02-17 15:05:22 +00:00
|
|
|
{
|
|
|
|
|
|
|
|
struct Scope
|
|
|
|
{
|
2018-11-21 11:42:34 +00:00
|
|
|
using YulType = YulString;
|
2017-04-26 22:58:34 +00:00
|
|
|
|
2021-06-11 17:17:41 +00:00
|
|
|
struct Variable
|
|
|
|
{
|
|
|
|
YulType type;
|
|
|
|
YulString name;
|
|
|
|
};
|
2017-02-17 15:05:22 +00:00
|
|
|
struct Function
|
|
|
|
{
|
2018-06-12 17:38:17 +00:00
|
|
|
std::vector<YulType> arguments;
|
|
|
|
std::vector<YulType> returns;
|
2021-06-11 17:17:41 +00:00
|
|
|
YulString name;
|
2017-02-17 15:05:22 +00:00
|
|
|
};
|
|
|
|
|
2020-01-16 18:13:25 +00:00
|
|
|
using Identifier = std::variant<Variable, Function>;
|
2017-02-17 15:05:22 +00:00
|
|
|
|
2018-11-21 11:42:34 +00:00
|
|
|
bool registerVariable(YulString _name, YulType const& _type);
|
2017-04-26 22:58:34 +00:00
|
|
|
bool registerFunction(
|
2018-11-21 11:42:34 +00:00
|
|
|
YulString _name,
|
2019-04-04 15:48:02 +00:00
|
|
|
std::vector<YulType> _arguments,
|
|
|
|
std::vector<YulType> _returns
|
2017-04-26 22:58:34 +00:00
|
|
|
);
|
2017-02-17 15:05:22 +00:00
|
|
|
|
|
|
|
/// Looks up the identifier in this or super scopes and returns a valid pointer if found
|
|
|
|
/// or a nullptr if not found. Variable lookups up across function boundaries will fail, as
|
|
|
|
/// will any lookups across assembly boundaries.
|
|
|
|
/// The pointer will be invalidated if the scope is modified.
|
|
|
|
/// @param _crossedFunction if true, we already crossed a function boundary during recursive lookup
|
2018-11-21 11:42:34 +00:00
|
|
|
Identifier* lookup(YulString _name);
|
2017-02-17 15:05:22 +00:00
|
|
|
/// Looks up the identifier in this and super scopes (will not find variables across function
|
|
|
|
/// boundaries and generally stops at assembly boundaries) and calls the visitor, returns
|
|
|
|
/// false if not found.
|
|
|
|
template <class V>
|
2018-11-21 11:42:34 +00:00
|
|
|
bool lookup(YulString _name, V const& _visitor)
|
2017-02-17 15:05:22 +00:00
|
|
|
{
|
|
|
|
if (Identifier* id = lookup(_name))
|
|
|
|
{
|
2019-11-19 15:42:49 +00:00
|
|
|
std::visit(_visitor, *id);
|
2017-02-17 15:05:22 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
/// @returns true if the name exists in this scope or in super scopes (also searches
|
|
|
|
/// across function and assembly boundaries).
|
2018-11-21 11:42:34 +00:00
|
|
|
bool exists(YulString _name) const;
|
2017-02-17 15:05:22 +00:00
|
|
|
|
2017-06-01 13:47:11 +00:00
|
|
|
/// @returns the number of variables directly registered inside the scope.
|
|
|
|
size_t numberOfVariables() const;
|
2017-05-24 16:34:19 +00:00
|
|
|
/// @returns true if this scope is inside a function.
|
|
|
|
bool insideFunction() const;
|
|
|
|
|
2017-02-17 15:05:22 +00:00
|
|
|
Scope* superScope = nullptr;
|
|
|
|
/// If true, variables from the super scope are not visible here (other identifiers are),
|
|
|
|
/// but they are still taken into account to prevent shadowing.
|
|
|
|
bool functionScope = false;
|
2018-11-21 11:42:34 +00:00
|
|
|
std::map<YulString, Identifier> identifiers;
|
2017-02-17 15:05:22 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|