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
|
|
|
*/
|
|
|
|
/**
|
|
|
|
* @author Christian <c@ethdev.com>
|
|
|
|
* @date 2016
|
|
|
|
* Solidity inline assembly parser.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
2018-11-23 10:18:57 +00:00
|
|
|
#include <libyul/AsmData.h>
|
2018-12-03 14:49:23 +00:00
|
|
|
#include <libyul/Dialect.h>
|
|
|
|
|
2018-11-14 16:11:55 +00:00
|
|
|
#include <liblangutil/SourceLocation.h>
|
|
|
|
#include <liblangutil/Scanner.h>
|
2018-11-14 13:59:30 +00:00
|
|
|
#include <liblangutil/ParserBase.h>
|
2016-02-22 01:13:41 +00:00
|
|
|
|
2019-12-19 22:22:19 +00:00
|
|
|
#include <libevmasm/Instruction.h>
|
|
|
|
|
2018-12-03 14:49:23 +00:00
|
|
|
#include <memory>
|
2019-11-19 15:42:49 +00:00
|
|
|
#include <variant>
|
2018-12-03 14:49:23 +00:00
|
|
|
#include <vector>
|
|
|
|
|
2019-12-11 16:31:36 +00:00
|
|
|
namespace solidity::yul
|
2016-03-01 21:56:39 +00:00
|
|
|
{
|
2016-02-22 01:13:41 +00:00
|
|
|
|
2018-11-14 16:11:55 +00:00
|
|
|
class Parser: public langutil::ParserBase
|
2016-02-22 01:13:41 +00:00
|
|
|
{
|
|
|
|
public:
|
2019-04-18 10:51:28 +00:00
|
|
|
enum class ForLoopComponent
|
|
|
|
{
|
|
|
|
None, ForLoopPre, ForLoopPost, ForLoopBody
|
|
|
|
};
|
|
|
|
|
2020-05-26 15:24:52 +00:00
|
|
|
explicit Parser(
|
|
|
|
langutil::ErrorReporter& _errorReporter,
|
|
|
|
Dialect const& _dialect,
|
|
|
|
std::optional<langutil::SourceLocation> _locationOverride = {}
|
|
|
|
):
|
|
|
|
ParserBase(_errorReporter),
|
|
|
|
m_dialect(_dialect),
|
|
|
|
m_locationOverride(std::move(_locationOverride))
|
|
|
|
{}
|
2016-02-22 01:13:41 +00:00
|
|
|
|
|
|
|
/// Parses an inline assembly block starting with `{` and ending with `}`.
|
2018-02-20 17:39:00 +00:00
|
|
|
/// @param _reuseScanner if true, do check for end of input after the `}`.
|
2016-02-22 01:13:41 +00:00
|
|
|
/// @returns an empty shared pointer on error.
|
2020-01-22 16:12:04 +00:00
|
|
|
std::unique_ptr<Block> parse(std::shared_ptr<langutil::Scanner> const& _scanner, bool _reuseScanner);
|
2016-02-22 01:13:41 +00:00
|
|
|
|
2019-04-24 11:38:35 +00:00
|
|
|
/// @returns a map of all EVM instructions available to assembly.
|
2019-12-11 16:31:36 +00:00
|
|
|
static std::map<std::string, evmasm::Instruction> const& instructions();
|
2019-04-24 11:38:35 +00:00
|
|
|
|
2016-02-22 01:13:41 +00:00
|
|
|
protected:
|
2019-12-19 22:22:19 +00:00
|
|
|
using ElementaryOperation = std::variant<Literal, Identifier, FunctionCall>;
|
2017-12-08 13:01:22 +00:00
|
|
|
|
2020-05-26 15:24:52 +00:00
|
|
|
langutil::SourceLocation currentLocation() const override
|
|
|
|
{
|
|
|
|
return m_locationOverride ? *m_locationOverride : ParserBase::currentLocation();
|
|
|
|
}
|
|
|
|
|
2020-02-05 21:13:03 +00:00
|
|
|
/// Creates an inline assembly node with the current source location.
|
|
|
|
template <class T> T createWithLocation() const
|
2016-04-18 11:47:40 +00:00
|
|
|
{
|
|
|
|
T r;
|
2020-02-05 23:04:18 +00:00
|
|
|
r.location = currentLocation();
|
2016-04-18 11:47:40 +00:00
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
2016-03-01 21:56:39 +00:00
|
|
|
Block parseBlock();
|
|
|
|
Statement parseStatement();
|
2017-05-19 17:04:40 +00:00
|
|
|
Case parseCase();
|
2017-04-28 13:33:52 +00:00
|
|
|
ForLoop parseForLoop();
|
2016-02-22 01:13:41 +00:00
|
|
|
/// Parses a functional expression that has to push exactly one stack element
|
2018-11-21 11:42:34 +00:00
|
|
|
Expression parseExpression();
|
2019-12-11 16:31:36 +00:00
|
|
|
static std::map<evmasm::Instruction, std::string> const& instructionNames();
|
2019-05-16 19:19:50 +00:00
|
|
|
/// Parses an elementary operation, i.e. a literal, identifier, instruction or
|
|
|
|
/// builtin functian call (only the name).
|
2018-01-06 00:09:08 +00:00
|
|
|
ElementaryOperation parseElementaryOperation();
|
2016-03-01 21:56:39 +00:00
|
|
|
VariableDeclaration parseVariableDeclaration();
|
2017-01-31 22:59:41 +00:00
|
|
|
FunctionDefinition parseFunctionDefinition();
|
2018-11-21 11:42:34 +00:00
|
|
|
Expression parseCall(ElementaryOperation&& _initialOp);
|
2017-04-26 22:58:34 +00:00
|
|
|
TypedName parseTypedName();
|
2018-12-03 16:47:15 +00:00
|
|
|
YulString expectAsmIdentifier();
|
2017-04-26 16:07:38 +00:00
|
|
|
|
2019-04-18 10:51:28 +00:00
|
|
|
/// Reports an error if we are currently not inside the body part of a for loop.
|
|
|
|
void checkBreakContinuePosition(std::string const& _which);
|
|
|
|
|
2017-08-21 10:08:29 +00:00
|
|
|
static bool isValidNumberLiteral(std::string const& _literal);
|
|
|
|
|
2017-04-26 16:07:38 +00:00
|
|
|
private:
|
2019-05-16 08:56:56 +00:00
|
|
|
Dialect const& m_dialect;
|
2020-05-26 15:24:52 +00:00
|
|
|
std::optional<langutil::SourceLocation> m_locationOverride;
|
2019-04-18 10:51:28 +00:00
|
|
|
ForLoopComponent m_currentForLoopComponent = ForLoopComponent::None;
|
2019-10-28 14:25:02 +00:00
|
|
|
bool m_insideFunction = false;
|
2016-02-22 01:13:41 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|