2016-02-22 01:13:41 +00:00
|
|
|
/*
|
2016-11-18 23:13:20 +00:00
|
|
|
This file is part of solidity.
|
2016-02-22 01:13:41 +00:00
|
|
|
|
2016-11-18 23:13:20 +00:00
|
|
|
solidity is free software: you can redistribute it and/or modify
|
2016-02-22 01:13:41 +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,
|
2016-02-22 01:13:41 +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/>.
|
2016-02-22 01:13:41 +00:00
|
|
|
*/
|
2020-07-17 14:54:12 +00:00
|
|
|
// SPDX-License-Identifier: GPL-3.0
|
2016-02-22 01:13:41 +00:00
|
|
|
/**
|
|
|
|
* @author Christian <c@ethdev.com>
|
|
|
|
* @date 2016
|
|
|
|
* Parsed inline assembly to be used by the AST
|
|
|
|
*/
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
2020-10-29 14:00:27 +00:00
|
|
|
#include <libyul/ASTForward.h>
|
2018-11-23 10:31:45 +00:00
|
|
|
#include <libyul/YulString.h>
|
|
|
|
|
2018-11-14 13:59:30 +00:00
|
|
|
#include <liblangutil/SourceLocation.h>
|
2016-02-22 01:13:41 +00:00
|
|
|
|
2018-10-29 14:12:02 +00:00
|
|
|
#include <memory>
|
2021-08-31 14:23:16 +00:00
|
|
|
#include <optional>
|
2017-06-01 13:41:51 +00:00
|
|
|
|
2019-12-11 16:31:36 +00:00
|
|
|
namespace solidity::yul
|
2016-02-22 01:13:41 +00:00
|
|
|
{
|
|
|
|
|
2018-10-29 14:12:02 +00:00
|
|
|
using Type = YulString;
|
2017-04-26 22:58:34 +00:00
|
|
|
|
2021-04-27 14:53:04 +00:00
|
|
|
struct DebugData
|
|
|
|
{
|
2021-09-20 15:51:01 +00:00
|
|
|
explicit DebugData(
|
|
|
|
langutil::SourceLocation _nativeLocation,
|
|
|
|
langutil::SourceLocation _originLocation = {},
|
|
|
|
std::optional<int64_t> _astID = {}
|
|
|
|
):
|
|
|
|
nativeLocation(std::move(_nativeLocation)),
|
|
|
|
originLocation(std::move(_originLocation)),
|
2021-09-15 13:55:03 +00:00
|
|
|
astID(std::move(_astID))
|
|
|
|
{}
|
|
|
|
|
|
|
|
static std::shared_ptr<DebugData const> create(
|
2021-09-20 15:51:01 +00:00
|
|
|
langutil::SourceLocation _nativeLocation = {},
|
|
|
|
langutil::SourceLocation _originLocation = {},
|
2021-09-15 13:55:03 +00:00
|
|
|
std::optional<int64_t> _astID = {}
|
|
|
|
)
|
|
|
|
{
|
2021-09-20 15:51:01 +00:00
|
|
|
return std::make_shared<DebugData const>(
|
|
|
|
std::move(_nativeLocation),
|
|
|
|
std::move(_originLocation),
|
|
|
|
std::move(_astID)
|
|
|
|
);
|
2021-09-15 13:55:03 +00:00
|
|
|
}
|
|
|
|
|
2021-09-20 15:51:01 +00:00
|
|
|
/// Location in the Yul code.
|
|
|
|
langutil::SourceLocation nativeLocation;
|
|
|
|
/// Location in the original source that the Yul code was produced from.
|
|
|
|
/// Optional. Only present if the Yul source contains location annotations.
|
|
|
|
langutil::SourceLocation originLocation;
|
2021-08-31 14:23:16 +00:00
|
|
|
/// ID in the (Solidity) source AST.
|
|
|
|
std::optional<int64_t> astID;
|
2021-04-27 14:53:04 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
struct TypedName { std::shared_ptr<DebugData const> debugData; YulString name; Type type; };
|
2017-04-26 22:58:34 +00:00
|
|
|
using TypedNameList = std::vector<TypedName>;
|
|
|
|
|
2016-03-01 21:56:39 +00:00
|
|
|
/// Literal number or string (up to 32 bytes)
|
2017-05-02 17:26:33 +00:00
|
|
|
enum class LiteralKind { Number, Boolean, String };
|
2021-04-27 14:53:04 +00:00
|
|
|
struct Literal { std::shared_ptr<DebugData const> debugData; LiteralKind kind; YulString value; Type type; };
|
2016-03-01 21:56:39 +00:00
|
|
|
/// External / internal identifier or label reference
|
2021-04-27 14:53:04 +00:00
|
|
|
struct Identifier { std::shared_ptr<DebugData const> debugData; YulString name; };
|
2017-05-24 00:07:00 +00:00
|
|
|
/// Assignment ("x := mload(20:u256)", expects push-1-expression on the right hand
|
2016-03-01 21:56:39 +00:00
|
|
|
/// side and requires x to occupy exactly one stack slot.
|
2017-05-19 16:06:26 +00:00
|
|
|
///
|
|
|
|
/// Multiple assignment ("x, y := f()"), where the left hand side variables each occupy
|
|
|
|
/// a single stack slot and expects a single expression on the right hand returning
|
|
|
|
/// the same amount of items as the number of variables.
|
2021-04-27 14:53:04 +00:00
|
|
|
struct Assignment { std::shared_ptr<DebugData const> debugData; std::vector<Identifier> variableNames; std::unique_ptr<Expression> value; };
|
|
|
|
struct FunctionCall { std::shared_ptr<DebugData const> debugData; Identifier functionName; std::vector<Expression> arguments; };
|
2017-12-08 13:01:22 +00:00
|
|
|
/// Statement that contains only a single expression
|
2021-04-27 14:53:04 +00:00
|
|
|
struct ExpressionStatement { std::shared_ptr<DebugData const> debugData; Expression expression; };
|
2017-04-26 22:58:34 +00:00
|
|
|
/// Block-scope variable declaration ("let x:u256 := mload(20:u256)"), non-hoisted
|
2021-04-27 14:53:04 +00:00
|
|
|
struct VariableDeclaration { std::shared_ptr<DebugData const> debugData; TypedNameList variables; std::unique_ptr<Expression> value; };
|
2016-03-01 21:56:39 +00:00
|
|
|
/// Block that creates a scope (frees declared stack variables)
|
2021-04-27 14:53:04 +00:00
|
|
|
struct Block { std::shared_ptr<DebugData const> debugData; std::vector<Statement> statements; };
|
2017-01-31 22:59:41 +00:00
|
|
|
/// Function definition ("function f(a, b) -> (d, e) { ... }")
|
2021-04-27 14:53:04 +00:00
|
|
|
struct FunctionDefinition { std::shared_ptr<DebugData const> debugData; YulString name; TypedNameList parameters; TypedNameList returnVariables; Block body; };
|
2017-11-21 12:36:41 +00:00
|
|
|
/// Conditional execution without "else" part.
|
2021-04-27 14:53:04 +00:00
|
|
|
struct If { std::shared_ptr<DebugData const> debugData; std::unique_ptr<Expression> condition; Block body; };
|
2017-04-28 13:27:56 +00:00
|
|
|
/// Switch case or default case
|
2021-04-27 14:53:04 +00:00
|
|
|
struct Case { std::shared_ptr<DebugData const> debugData; std::unique_ptr<Literal> value; Block body; };
|
2017-04-28 13:27:56 +00:00
|
|
|
/// Switch statement
|
2021-04-27 14:53:04 +00:00
|
|
|
struct Switch { std::shared_ptr<DebugData const> debugData; std::unique_ptr<Expression> expression; std::vector<Case> cases; };
|
|
|
|
struct ForLoop { std::shared_ptr<DebugData const> debugData; Block pre; std::unique_ptr<Expression> condition; Block post; Block body; };
|
2019-03-04 14:38:05 +00:00
|
|
|
/// Break statement (valid within for loop)
|
2021-04-27 14:53:04 +00:00
|
|
|
struct Break { std::shared_ptr<DebugData const> debugData; };
|
2019-03-04 14:38:05 +00:00
|
|
|
/// Continue statement (valid within for loop)
|
2021-04-27 14:53:04 +00:00
|
|
|
struct Continue { std::shared_ptr<DebugData const> debugData; };
|
2019-10-28 14:25:02 +00:00
|
|
|
/// Leave statement (valid within function)
|
2021-04-27 14:53:04 +00:00
|
|
|
struct Leave { std::shared_ptr<DebugData const> debugData; };
|
2016-04-18 11:47:40 +00:00
|
|
|
|
2021-09-20 15:51:01 +00:00
|
|
|
/// Extracts the IR source location from a Yul node.
|
|
|
|
template <class T> inline langutil::SourceLocation nativeLocationOf(T const& _node)
|
|
|
|
{
|
|
|
|
return _node.debugData ? _node.debugData->nativeLocation : langutil::SourceLocation{};
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Extracts the IR source location from a Yul node.
|
|
|
|
template <class... Args> inline langutil::SourceLocation nativeLocationOf(std::variant<Args...> const& _node)
|
|
|
|
{
|
|
|
|
return std::visit([](auto const& _arg) { return nativeLocationOf(_arg); }, _node);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Extracts the original source location from a Yul node.
|
|
|
|
template <class T> inline langutil::SourceLocation originLocationOf(T const& _node)
|
2016-04-18 11:47:40 +00:00
|
|
|
{
|
2021-09-20 15:51:01 +00:00
|
|
|
return _node.debugData ? _node.debugData->originLocation : langutil::SourceLocation{};
|
2021-07-05 16:19:18 +00:00
|
|
|
}
|
2016-04-18 11:47:40 +00:00
|
|
|
|
2021-09-20 15:51:01 +00:00
|
|
|
/// Extracts the original source location from a Yul node.
|
|
|
|
template <class... Args> inline langutil::SourceLocation originLocationOf(std::variant<Args...> const& _node)
|
2016-04-18 11:47:40 +00:00
|
|
|
{
|
2021-09-20 15:51:01 +00:00
|
|
|
return std::visit([](auto const& _arg) { return originLocationOf(_arg); }, _node);
|
2016-04-18 11:47:40 +00:00
|
|
|
}
|
2016-02-22 01:13:41 +00:00
|
|
|
|
2021-09-09 14:49:36 +00:00
|
|
|
/// Extracts the debug data from a Yul node.
|
|
|
|
template <class T> inline std::shared_ptr<DebugData const> debugDataOf(T const& _node)
|
2021-04-27 14:53:04 +00:00
|
|
|
{
|
2021-09-09 14:49:36 +00:00
|
|
|
return _node.debugData;
|
|
|
|
}
|
2021-04-27 14:53:04 +00:00
|
|
|
|
|
|
|
/// Extracts the debug data from a Yul node.
|
2021-09-09 14:49:36 +00:00
|
|
|
template <class... Args> inline std::shared_ptr<DebugData const> debugDataOf(std::variant<Args...> const& _node)
|
2021-04-27 14:53:04 +00:00
|
|
|
{
|
2021-09-09 14:49:36 +00:00
|
|
|
return std::visit([](auto const& _arg) { return debugDataOf(_arg); }, _node);
|
2021-04-27 14:53:04 +00:00
|
|
|
}
|
|
|
|
|
2016-02-22 01:13:41 +00:00
|
|
|
}
|