solidity/libyul/AsmParser.h

107 lines
3.3 KiB
C
Raw Normal View History

2016-02-22 01:13:41 +00:00
/*
This file is part of solidity.
2016-02-22 01:13:41 +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.
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
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
#include <libyul/AsmData.h>
#include <libyul/Dialect.h>
#include <liblangutil/SourceLocation.h>
#include <liblangutil/Scanner.h>
#include <liblangutil/ParserBase.h>
2016-02-22 01:13:41 +00:00
2019-12-19 22:22:19 +00:00
#include <libevmasm/Instruction.h>
#include <memory>
#include <variant>
#include <vector>
2019-12-11 16:31:36 +00:00
namespace solidity::yul
{
2016-02-22 01:13:41 +00:00
class Parser: public langutil::ParserBase
2016-02-22 01:13:41 +00:00
{
public:
enum class ForLoopComponent
{
None, ForLoopPre, ForLoopPost, ForLoopBody
};
explicit Parser(langutil::ErrorReporter& _errorReporter, Dialect const& _dialect):
ParserBase(_errorReporter), m_dialect(_dialect) {}
2016-02-22 01:13:41 +00:00
/// Parses an inline assembly block starting with `{` and ending with `}`.
/// @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.
std::shared_ptr<Block> parse(std::shared_ptr<langutil::Scanner> const& _scanner, bool _reuseScanner);
2016-02-22 01:13:41 +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();
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
2016-04-18 11:47:40 +00:00
/// Creates an inline assembly node with the given source location.
template <class T> T createWithLocation(langutil::SourceLocation const& _loc = {}) const
2016-04-18 11:47:40 +00:00
{
T r;
r.location = _loc;
if (r.location.isEmpty())
{
r.location.start = position();
r.location.end = endPosition();
}
if (!r.location.source)
r.location.source = m_scanner->charStream();
2016-04-18 11:47:40 +00:00
return r;
}
langutil::SourceLocation location() const { return {position(), endPosition(), m_scanner->charStream()}; }
2016-04-18 11:47:40 +00:00
Block parseBlock();
Statement parseStatement();
2017-05-19 17:04:40 +00:00
Case parseCase();
ForLoop parseForLoop();
2016-02-22 01:13:41 +00:00
/// Parses a functional expression that has to push exactly one stack element
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).
ElementaryOperation parseElementaryOperation();
VariableDeclaration parseVariableDeclaration();
2017-01-31 22:59:41 +00:00
FunctionDefinition parseFunctionDefinition();
Expression parseCall(ElementaryOperation&& _initialOp);
2017-04-26 22:58:34 +00:00
TypedName parseTypedName();
YulString expectAsmIdentifier();
2017-04-26 16:07:38 +00:00
/// Reports an error if we are currently not inside the body part of a for loop.
void checkBreakContinuePosition(std::string const& _which);
static bool isValidNumberLiteral(std::string const& _literal);
2017-04-26 16:07:38 +00:00
private:
Dialect const& m_dialect;
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
};
}