solidity/libyul/AsmParser.h

156 lines
4.6 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
*/
// SPDX-License-Identifier: GPL-3.0
2016-02-22 01:13:41 +00:00
/**
* @author Christian <c@ethdev.com>
* @date 2016
* Solidity inline assembly parser.
*/
#pragma once
#include <libyul/AST.h>
#include <libyul/ASTForward.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
#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
};
enum class UseSourceLocationFrom
{
Scanner, LocationOverride, Comments,
};
explicit Parser(
langutil::ErrorReporter& _errorReporter,
Dialect const& _dialect,
std::optional<langutil::SourceLocation> _locationOverride = {}
):
ParserBase(_errorReporter),
m_dialect(_dialect),
m_locationOverride{_locationOverride ? *_locationOverride : langutil::SourceLocation{}},
m_debugDataOverride{},
m_useSourceLocationFrom{
_locationOverride ?
UseSourceLocationFrom::LocationOverride :
UseSourceLocationFrom::Scanner
}
{}
/// Constructs a Yul parser that is using the source locations
/// from the comments (via @src).
explicit Parser(
langutil::ErrorReporter& _errorReporter,
Dialect const& _dialect,
std::map<unsigned, std::shared_ptr<langutil::CharStream>> _charStreamMap
):
ParserBase(_errorReporter),
m_dialect(_dialect),
m_charStreamMap{std::move(_charStreamMap)},
m_debugDataOverride{DebugData::create()},
m_useSourceLocationFrom{UseSourceLocationFrom::Comments}
{}
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::unique_ptr<Block> parse(std::shared_ptr<langutil::Scanner> const& _scanner, bool _reuseScanner);
2016-02-22 01:13:41 +00:00
protected:
langutil::SourceLocation currentLocation() const override
{
if (m_useSourceLocationFrom == UseSourceLocationFrom::Scanner)
return ParserBase::currentLocation();
return m_locationOverride;
}
langutil::Token advance() override;
void fetchSourceLocationFromComment();
/// Creates a DebugData object with the correct source location set.
std::shared_ptr<DebugData const> createDebugData() const
{
switch (m_useSourceLocationFrom)
{
case UseSourceLocationFrom::Scanner:
return DebugData::create(ParserBase::currentLocation());
case UseSourceLocationFrom::LocationOverride:
return DebugData::create(m_locationOverride);
case UseSourceLocationFrom::Comments:
return m_debugDataOverride;
}
solAssert(false, "");
}
/// 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;
r.debugData = createDebugData();
2016-04-18 11:47:40 +00:00
return r;
}
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-05-16 19:19:50 +00:00
/// Parses an elementary operation, i.e. a literal, identifier, instruction or
/// builtin functian call (only the name).
std::variant<Literal, Identifier> parseLiteralOrIdentifier();
VariableDeclaration parseVariableDeclaration();
2017-01-31 22:59:41 +00:00
FunctionDefinition parseFunctionDefinition();
FunctionCall parseCall(std::variant<Literal, Identifier>&& _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;
std::optional<std::map<unsigned, std::shared_ptr<langutil::CharStream>>> m_charStreamMap;
langutil::SourceLocation m_locationOverride;
std::shared_ptr<DebugData const> m_debugDataOverride;
UseSourceLocationFrom m_useSourceLocationFrom = UseSourceLocationFrom::Scanner;
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
};
}