solidity/libyul/AsmParser.h

169 lines
5.0 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 <map>
#include <memory>
#include <variant>
#include <vector>
2021-09-15 13:23:22 +00:00
#include <string_view>
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_useSourceLocationFrom{
_locationOverride ?
UseSourceLocationFrom::LocationOverride :
UseSourceLocationFrom::Scanner
}
{}
2021-09-15 13:23:22 +00:00
/// Constructs a Yul parser that is using the debug data
/// from the comments (via @src and other tags).
explicit Parser(
langutil::ErrorReporter& _errorReporter,
Dialect const& _dialect,
std::optional<std::map<unsigned, std::shared_ptr<std::string const>>> _sourceNames
):
ParserBase(_errorReporter),
m_dialect(_dialect),
2021-06-29 12:38:59 +00:00
m_sourceNames{std::move(_sourceNames)},
m_useSourceLocationFrom{
m_sourceNames.has_value() ?
UseSourceLocationFrom::Comments :
UseSourceLocationFrom::Scanner
}
{}
2016-02-22 01:13:41 +00:00
/// Parses an inline assembly block starting with `{` and ending with `}`.
/// @returns an empty shared pointer on error.
2021-08-03 13:36:00 +00:00
std::unique_ptr<Block> parseInline(std::shared_ptr<langutil::Scanner> const& _scanner);
/// Parses an assembly block starting with `{` and ending with `}`
/// and expects end of input after the '}'.
/// @returns an empty shared pointer on error.
std::unique_ptr<Block> parse(langutil::CharStream& _charStream);
2016-02-22 01:13:41 +00:00
protected:
langutil::SourceLocation currentLocation() const override
{
if (m_useSourceLocationFrom == UseSourceLocationFrom::LocationOverride)
return m_locationOverride;
return ParserBase::currentLocation();
}
langutil::Token advance() override;
2021-09-15 13:23:22 +00:00
void fetchDebugDataFromComment();
2021-09-15 13:55:03 +00:00
std::optional<std::pair<std::string_view, langutil::SourceLocation>> parseSrcComment(
std::string_view _arguments,
langutil::SourceLocation const& _commentLocation
);
std::optional<std::pair<std::string_view, std::optional<int>>> parseASTIDComment(
2021-09-15 13:23:22 +00:00
std::string_view _arguments,
langutil::SourceLocation const& _commentLocation
);
/// Creates a DebugData object with the correct source location set.
std::shared_ptr<DebugData const> createDebugData() const;
void updateLocationEndFrom(
std::shared_ptr<DebugData const>& _debugData,
langutil::SourceLocation const& _location
) const;
/// 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;
2021-06-29 12:38:59 +00:00
std::optional<std::map<unsigned, std::shared_ptr<std::string const>>> m_sourceNames;
langutil::SourceLocation m_locationOverride;
langutil::SourceLocation m_locationFromComment;
std::optional<int64_t> m_astIDFromComment;
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
};
}