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
|
|
|
|
* Solidity inline assembly parser.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
2021-06-14 10:03:53 +00:00
|
|
|
#include <libyul/AST.h>
|
2020-10-29 14:00:27 +00:00
|
|
|
#include <libyul/ASTForward.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
|
|
|
|
2021-08-03 13:20:00 +00:00
|
|
|
#include <map>
|
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
|
|
|
|
};
|
|
|
|
|
2021-06-16 10:38:34 +00:00
|
|
|
enum class UseSourceLocationFrom
|
|
|
|
{
|
|
|
|
Scanner, LocationOverride, Comments,
|
|
|
|
};
|
|
|
|
|
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),
|
2021-06-16 10:38:34 +00:00
|
|
|
m_locationOverride{_locationOverride ? *_locationOverride : langutil::SourceLocation{}},
|
2021-07-08 14:38:16 +00:00
|
|
|
m_debugDataOverride{},
|
2021-06-16 10:38:34 +00:00
|
|
|
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,
|
2021-07-12 10:05:26 +00:00
|
|
|
std::optional<std::map<unsigned, std::shared_ptr<std::string const>>> _sourceNames
|
2021-06-16 10:38:34 +00:00
|
|
|
):
|
|
|
|
ParserBase(_errorReporter),
|
|
|
|
m_dialect(_dialect),
|
2021-06-29 12:38:59 +00:00
|
|
|
m_sourceNames{std::move(_sourceNames)},
|
2021-07-08 14:38:16 +00:00
|
|
|
m_debugDataOverride{DebugData::create()},
|
2021-07-12 10:05:26 +00:00
|
|
|
m_useSourceLocationFrom{
|
|
|
|
m_sourceNames.has_value() ?
|
|
|
|
UseSourceLocationFrom::Comments :
|
|
|
|
UseSourceLocationFrom::Scanner
|
|
|
|
}
|
2020-05-26 15:24:52 +00:00
|
|
|
{}
|
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:
|
2020-05-26 15:24:52 +00:00
|
|
|
langutil::SourceLocation currentLocation() const override
|
|
|
|
{
|
2021-06-16 10:38:34 +00:00
|
|
|
if (m_useSourceLocationFrom == UseSourceLocationFrom::Scanner)
|
|
|
|
return ParserBase::currentLocation();
|
|
|
|
return m_locationOverride;
|
2020-05-26 15:24:52 +00:00
|
|
|
}
|
|
|
|
|
2021-06-16 10:38:34 +00:00
|
|
|
langutil::Token advance() override;
|
|
|
|
|
|
|
|
void fetchSourceLocationFromComment();
|
|
|
|
|
2021-07-08 14:38:16 +00:00
|
|
|
/// Creates a DebugData object with the correct source location set.
|
2021-08-03 12:41:10 +00:00
|
|
|
std::shared_ptr<DebugData const> createDebugData() const;
|
2021-07-08 14:38:16 +00:00
|
|
|
|
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;
|
2021-07-08 14:38:16 +00:00
|
|
|
r.debugData = createDebugData();
|
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-05-16 19:19:50 +00:00
|
|
|
/// Parses an elementary operation, i.e. a literal, identifier, instruction or
|
|
|
|
/// builtin functian call (only the name).
|
2020-12-03 16:44:11 +00:00
|
|
|
std::variant<Literal, Identifier> parseLiteralOrIdentifier();
|
2016-03-01 21:56:39 +00:00
|
|
|
VariableDeclaration parseVariableDeclaration();
|
2017-01-31 22:59:41 +00:00
|
|
|
FunctionDefinition parseFunctionDefinition();
|
2020-12-03 16:44:11 +00:00
|
|
|
FunctionCall parseCall(std::variant<Literal, Identifier>&& _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;
|
2021-06-16 10:38:34 +00:00
|
|
|
|
2021-06-29 12:38:59 +00:00
|
|
|
std::optional<std::map<unsigned, std::shared_ptr<std::string const>>> m_sourceNames;
|
2021-06-16 10:38:34 +00:00
|
|
|
langutil::SourceLocation m_locationOverride;
|
2021-07-08 14:38:16 +00:00
|
|
|
std::shared_ptr<DebugData const> m_debugDataOverride;
|
2021-06-16 10:38:34 +00:00
|
|
|
UseSourceLocationFrom m_useSourceLocationFrom = UseSourceLocationFrom::Scanner;
|
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
|
|
|
};
|
|
|
|
|
|
|
|
}
|