solidity/libyul/AsmParser.cpp

590 lines
17 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.
*/
#include <libyul/AST.h>
2021-04-27 14:53:04 +00:00
#include <libyul/AsmParser.h>
#include <libyul/Exceptions.h>
#include <liblangutil/Scanner.h>
#include <liblangutil/ErrorReporter.h>
#include <libsolutil/Common.h>
#include <libsolutil/Visitor.h>
#include <range/v3/view/subrange.hpp>
#include <boost/algorithm/string.hpp>
2016-02-22 01:13:41 +00:00
#include <algorithm>
#include <regex>
2016-02-22 01:13:41 +00:00
using namespace std;
2019-12-11 16:31:36 +00:00
using namespace solidity;
using namespace solidity::util;
using namespace solidity::langutil;
using namespace solidity::yul;
2016-02-22 01:13:41 +00:00
2021-05-27 15:41:04 +00:00
namespace
{
2021-04-27 14:53:04 +00:00
[[nodiscard]]
shared_ptr<DebugData const> updateLocationEndFrom(
shared_ptr<DebugData const> const& _debugData,
langutil::SourceLocation const& _location
)
{
SourceLocation updatedLocation = _debugData->location;
updatedLocation.end = _location.end;
return make_shared<DebugData const>(updatedLocation);
}
optional<int> toInt(string const& _value)
{
try
{
return stoi(_value);
}
catch (...)
{
return nullopt;
}
}
2021-04-27 14:53:04 +00:00
}
unique_ptr<Block> Parser::parse(std::shared_ptr<Scanner> const& _scanner, bool _reuseScanner)
2016-02-22 01:13:41 +00:00
{
m_recursionDepth = 0;
_scanner->setScannerMode(ScannerKind::Yul);
ScopeGuard resetScanner([&]{ _scanner->setScannerMode(ScannerKind::Solidity); });
2016-02-22 01:13:41 +00:00
try
{
m_scanner = _scanner;
2021-06-29 12:38:59 +00:00
if (m_sourceNames)
fetchSourceLocationFromComment();
auto block = make_unique<Block>(parseBlock());
if (!_reuseScanner)
expectToken(Token::EOS);
return block;
2016-02-22 01:13:41 +00:00
}
catch (FatalError const&)
{
yulAssert(!m_errorReporter.errors().empty(), "Fatal error detected, but no error is reported.");
2016-02-22 01:13:41 +00:00
}
2016-02-22 01:13:41 +00:00
return nullptr;
}
langutil::Token Parser::advance()
{
auto const token = ParserBase::advance();
if (m_useSourceLocationFrom == UseSourceLocationFrom::Comments)
fetchSourceLocationFromComment();
return token;
}
void Parser::fetchSourceLocationFromComment()
{
2021-06-29 12:38:59 +00:00
solAssert(m_sourceNames.has_value(), "");
if (m_scanner->currentCommentLiteral().empty())
return;
static regex const lineRE = std::regex(
R"~~~((^|\s*)@src\s+(-1|\d+):(-1|\d+):(-1|\d+)(\s+|$))~~~",
std::regex_constants::ECMAScript | std::regex_constants::optimize
);
string const text = m_scanner->currentCommentLiteral();
auto from = sregex_iterator(text.begin(), text.end(), lineRE);
auto to = sregex_iterator();
for (auto const& matchResult: ranges::make_subrange(from, to))
{
solAssert(matchResult.size() == 6, "");
auto const sourceIndex = toInt(matchResult[2].str());
auto const start = toInt(matchResult[3].str());
auto const end = toInt(matchResult[4].str());
auto const commentLocation = m_scanner->currentCommentLocation();
m_debugDataOverride = DebugData::create();
if (!sourceIndex || !start || !end)
m_errorReporter.syntaxError(6367_error, commentLocation, "Invalid value in source location mapping. Could not parse location specification.");
else if (sourceIndex == -1)
m_debugDataOverride = DebugData::create(SourceLocation{*start, *end, nullptr});
2021-06-29 12:38:59 +00:00
else if (!(sourceIndex >= 0 && m_sourceNames->count(static_cast<unsigned>(*sourceIndex))))
m_errorReporter.syntaxError(2674_error, commentLocation, "Invalid source mapping. Source index not defined via @use-src.");
else
{
2021-06-29 12:38:59 +00:00
shared_ptr<string const> sourceName = m_sourceNames->at(static_cast<unsigned>(*sourceIndex));
solAssert(sourceName, "");
m_debugDataOverride = DebugData::create(SourceLocation{*start, *end, move(sourceName)});
}
}
}
Block Parser::parseBlock()
2016-02-22 01:13:41 +00:00
{
RecursionGuard recursionGuard(*this);
Block block = createWithLocation<Block>();
2016-02-22 01:13:41 +00:00
expectToken(Token::LBrace);
while (currentToken() != Token::RBrace)
2016-02-22 01:13:41 +00:00
block.statements.emplace_back(parseStatement());
if (m_useSourceLocationFrom == UseSourceLocationFrom::Scanner)
block.debugData = updateLocationEndFrom(block.debugData, currentLocation());
advance();
2016-02-22 01:13:41 +00:00
return block;
}
Statement Parser::parseStatement()
2016-02-22 01:13:41 +00:00
{
RecursionGuard recursionGuard(*this);
switch (currentToken())
2016-02-22 01:13:41 +00:00
{
case Token::Let:
return parseVariableDeclaration();
2017-01-31 22:59:41 +00:00
case Token::Function:
return parseFunctionDefinition();
2016-02-22 01:13:41 +00:00
case Token::LBrace:
return parseBlock();
case Token::If:
{
If _if = createWithLocation<If>();
advance();
_if.condition = make_unique<Expression>(parseExpression());
_if.body = parseBlock();
if (m_useSourceLocationFrom == UseSourceLocationFrom::Scanner)
_if.debugData = updateLocationEndFrom(_if.debugData, _if.body.debugData->location);
return Statement{move(_if)};
}
case Token::Switch:
{
Switch _switch = createWithLocation<Switch>();
advance();
_switch.expression = make_unique<Expression>(parseExpression());
while (currentToken() == Token::Case)
_switch.cases.emplace_back(parseCase());
if (currentToken() == Token::Default)
2017-05-19 17:04:40 +00:00
_switch.cases.emplace_back(parseCase());
if (currentToken() == Token::Default)
fatalParserError(6931_error, "Only one default case allowed.");
else if (currentToken() == Token::Case)
fatalParserError(4904_error, "Case not allowed after default case.");
2018-10-09 03:29:37 +00:00
if (_switch.cases.empty())
fatalParserError(2418_error, "Switch statement without any cases.");
if (m_useSourceLocationFrom == UseSourceLocationFrom::Scanner)
_switch.debugData = updateLocationEndFrom(_switch.debugData, _switch.cases.back().body.debugData->location);
return Statement{move(_switch)};
}
case Token::For:
return parseForLoop();
case Token::Break:
{
Statement stmt{createWithLocation<Break>()};
checkBreakContinuePosition("break");
advance();
return stmt;
}
case Token::Continue:
{
Statement stmt{createWithLocation<Continue>()};
checkBreakContinuePosition("continue");
advance();
return stmt;
}
2020-07-27 18:11:38 +00:00
case Token::Leave:
{
Statement stmt{createWithLocation<Leave>()};
if (!m_insideFunction)
m_errorReporter.syntaxError(8149_error, currentLocation(), "Keyword \"leave\" can only be used inside a function.");
advance();
return stmt;
}
2016-02-22 01:13:41 +00:00
default:
break;
}
2016-02-22 01:13:41 +00:00
// Options left:
// Expression/FunctionCall
// Assignment
variant<Literal, Identifier> elementary(parseLiteralOrIdentifier());
2019-05-16 19:19:50 +00:00
switch (currentToken())
2016-02-22 01:13:41 +00:00
{
case Token::LParen:
2017-12-08 13:01:22 +00:00
{
Expression expr = parseCall(std::move(elementary));
2021-04-27 14:53:04 +00:00
return ExpressionStatement{debugDataOf(expr), move(expr)};
2017-12-08 13:01:22 +00:00
}
case Token::Comma:
case Token::AssemblyAssign:
{
Assignment assignment;
2021-04-27 14:53:04 +00:00
assignment.debugData = debugDataOf(elementary);
while (true)
{
if (!holds_alternative<Identifier>(elementary))
{
auto const token = currentToken() == Token::Comma ? "," : ":=";
fatalParserError(
2856_error,
std::string("Variable name must precede \"") +
token +
"\"" +
(currentToken() == Token::Comma ? " in multiple assignment." : " in assignment.")
);
}
auto const& identifier = std::get<Identifier>(elementary);
if (m_dialect.builtin(identifier.name))
fatalParserError(6272_error, "Cannot assign to builtin function \"" + identifier.name.str() + "\".");
assignment.variableNames.emplace_back(identifier);
if (currentToken() != Token::Comma)
break;
expectToken(Token::Comma);
elementary = parseLiteralOrIdentifier();
}
expectToken(Token::AssemblyAssign);
2019-11-27 16:24:21 +00:00
assignment.value = make_unique<Expression>(parseExpression());
if (m_useSourceLocationFrom == UseSourceLocationFrom::Scanner)
assignment.debugData = updateLocationEndFrom(assignment.debugData, locationOf(*assignment.value));
2020-08-31 14:29:12 +00:00
return Statement{move(assignment)};
}
2016-02-22 01:13:41 +00:00
default:
fatalParserError(6913_error, "Call or assignment expected.");
2016-02-22 01:13:41 +00:00
break;
}
2019-05-16 19:19:50 +00:00
2020-11-05 01:25:53 +00:00
yulAssert(false, "");
return {};
2016-02-22 01:13:41 +00:00
}
Case Parser::parseCase()
{
RecursionGuard recursionGuard(*this);
Case _case = createWithLocation<Case>();
if (currentToken() == Token::Default)
advance();
else if (currentToken() == Token::Case)
{
advance();
variant<Literal, Identifier> literal = parseLiteralOrIdentifier();
if (!holds_alternative<Literal>(literal))
fatalParserError(4805_error, "Literal expected.");
_case.value = make_unique<Literal>(std::get<Literal>(std::move(literal)));
}
2017-05-19 17:04:40 +00:00
else
yulAssert(false, "Case or default case expected.");
_case.body = parseBlock();
if (m_useSourceLocationFrom == UseSourceLocationFrom::Scanner)
_case.debugData = updateLocationEndFrom(_case.debugData, _case.body.debugData->location);
return _case;
}
ForLoop Parser::parseForLoop()
{
RecursionGuard recursionGuard(*this);
ForLoopComponent outerForLoopComponent = m_currentForLoopComponent;
ForLoop forLoop = createWithLocation<ForLoop>();
expectToken(Token::For);
m_currentForLoopComponent = ForLoopComponent::ForLoopPre;
forLoop.pre = parseBlock();
m_currentForLoopComponent = ForLoopComponent::None;
forLoop.condition = make_unique<Expression>(parseExpression());
m_currentForLoopComponent = ForLoopComponent::ForLoopPost;
forLoop.post = parseBlock();
m_currentForLoopComponent = ForLoopComponent::ForLoopBody;
forLoop.body = parseBlock();
if (m_useSourceLocationFrom == UseSourceLocationFrom::Scanner)
forLoop.debugData = updateLocationEndFrom(forLoop.debugData, forLoop.body.debugData->location);
m_currentForLoopComponent = outerForLoopComponent;
return forLoop;
}
Expression Parser::parseExpression()
2016-02-22 01:13:41 +00:00
{
RecursionGuard recursionGuard(*this);
2019-05-16 19:19:50 +00:00
variant<Literal, Identifier> operation = parseLiteralOrIdentifier();
return visit(GenericVisitor{
[&](Identifier& _identifier) -> Expression
{
if (currentToken() == Token::LParen)
return parseCall(std::move(operation));
if (m_dialect.builtin(_identifier.name))
fatalParserError(
7104_error,
2021-04-27 14:53:04 +00:00
_identifier.debugData->location,
"Builtin function \"" + _identifier.name.str() + "\" must be called."
);
return move(_identifier);
},
[&](Literal& _literal) -> Expression
{
return move(_literal);
}
}, operation);
2016-02-22 01:13:41 +00:00
}
variant<Literal, Identifier> Parser::parseLiteralOrIdentifier()
{
RecursionGuard recursionGuard(*this);
switch (currentToken())
2016-02-22 01:13:41 +00:00
{
case Token::Identifier:
{
Identifier identifier{createDebugData(), YulString{currentLiteral()}};
advance();
return identifier;
2016-02-22 01:13:41 +00:00
}
case Token::StringLiteral:
2021-03-25 16:00:05 +00:00
case Token::HexStringLiteral:
2016-02-22 01:13:41 +00:00
case Token::Number:
case Token::TrueLiteral:
case Token::FalseLiteral:
2016-02-22 01:13:41 +00:00
{
LiteralKind kind = LiteralKind::Number;
switch (currentToken())
{
case Token::StringLiteral:
2021-03-25 16:00:05 +00:00
case Token::HexStringLiteral:
kind = LiteralKind::String;
break;
case Token::Number:
if (!isValidNumberLiteral(currentLiteral()))
fatalParserError(4828_error, "Invalid number literal.");
kind = LiteralKind::Number;
break;
case Token::TrueLiteral:
case Token::FalseLiteral:
kind = LiteralKind::Boolean;
break;
default:
break;
}
2017-04-26 22:58:34 +00:00
Literal literal{
createDebugData(),
kind,
YulString{currentLiteral()},
kind == LiteralKind::Boolean ? m_dialect.boolType : m_dialect.defaultType
2016-02-22 01:13:41 +00:00
};
advance();
2019-12-19 16:58:20 +00:00
if (currentToken() == Token::Colon)
2017-04-26 22:58:34 +00:00
{
expectToken(Token::Colon);
if (m_useSourceLocationFrom == UseSourceLocationFrom::Scanner)
literal.debugData = updateLocationEndFrom(literal.debugData, currentLocation());
literal.type = expectAsmIdentifier();
2017-04-26 22:58:34 +00:00
}
2019-12-19 16:58:20 +00:00
return literal;
2016-02-22 01:13:41 +00:00
}
case Token::Illegal:
fatalParserError(1465_error, "Illegal token: " + to_string(m_scanner->currentError()));
break;
2016-02-22 01:13:41 +00:00
default:
fatalParserError(1856_error, "Literal or identifier expected.");
2016-02-22 01:13:41 +00:00
}
return {};
2016-02-22 01:13:41 +00:00
}
VariableDeclaration Parser::parseVariableDeclaration()
2016-02-22 01:13:41 +00:00
{
RecursionGuard recursionGuard(*this);
2016-04-18 11:47:40 +00:00
VariableDeclaration varDecl = createWithLocation<VariableDeclaration>();
2016-02-22 01:13:41 +00:00
expectToken(Token::Let);
while (true)
{
varDecl.variables.emplace_back(parseTypedName());
if (currentToken() == Token::Comma)
expectToken(Token::Comma);
else
break;
}
if (currentToken() == Token::AssemblyAssign)
{
expectToken(Token::AssemblyAssign);
varDecl.value = make_unique<Expression>(parseExpression());
if (m_useSourceLocationFrom == UseSourceLocationFrom::Scanner)
varDecl.debugData = updateLocationEndFrom(varDecl.debugData, locationOf(*varDecl.value));
}
else if (m_useSourceLocationFrom == UseSourceLocationFrom::Scanner)
2021-04-27 14:53:04 +00:00
varDecl.debugData = updateLocationEndFrom(varDecl.debugData, varDecl.variables.back().debugData->location);
2016-04-18 11:47:40 +00:00
return varDecl;
2016-02-22 01:13:41 +00:00
}
FunctionDefinition Parser::parseFunctionDefinition()
2017-01-31 22:59:41 +00:00
{
RecursionGuard recursionGuard(*this);
if (m_currentForLoopComponent == ForLoopComponent::ForLoopPre)
m_errorReporter.syntaxError(
3441_error,
currentLocation(),
"Functions cannot be defined inside a for-loop init block."
);
ForLoopComponent outerForLoopComponent = m_currentForLoopComponent;
m_currentForLoopComponent = ForLoopComponent::None;
2017-01-31 22:59:41 +00:00
FunctionDefinition funDef = createWithLocation<FunctionDefinition>();
expectToken(Token::Function);
funDef.name = expectAsmIdentifier();
2017-01-31 22:59:41 +00:00
expectToken(Token::LParen);
while (currentToken() != Token::RParen)
2017-01-31 22:59:41 +00:00
{
funDef.parameters.emplace_back(parseTypedName());
if (currentToken() == Token::RParen)
2017-01-31 22:59:41 +00:00
break;
expectToken(Token::Comma);
}
expectToken(Token::RParen);
if (currentToken() == Token::RightArrow)
2017-01-31 22:59:41 +00:00
{
expectToken(Token::RightArrow);
2017-01-31 22:59:41 +00:00
while (true)
{
funDef.returnVariables.emplace_back(parseTypedName());
if (currentToken() == Token::LBrace)
2017-01-31 22:59:41 +00:00
break;
expectToken(Token::Comma);
}
}
2019-10-28 14:25:02 +00:00
bool preInsideFunction = m_insideFunction;
m_insideFunction = true;
2017-01-31 22:59:41 +00:00
funDef.body = parseBlock();
2019-10-28 14:25:02 +00:00
m_insideFunction = preInsideFunction;
if (m_useSourceLocationFrom == UseSourceLocationFrom::Scanner)
funDef.debugData = updateLocationEndFrom(funDef.debugData, funDef.body.debugData->location);
m_currentForLoopComponent = outerForLoopComponent;
2017-01-31 22:59:41 +00:00
return funDef;
}
FunctionCall Parser::parseCall(variant<Literal, Identifier>&& _initialOp)
2016-02-22 01:13:41 +00:00
{
RecursionGuard recursionGuard(*this);
2020-07-30 11:12:55 +00:00
if (!holds_alternative<Identifier>(_initialOp))
fatalParserError(9980_error, "Function name expected.");
2017-02-01 20:20:21 +00:00
2020-07-30 11:12:55 +00:00
FunctionCall ret;
ret.functionName = std::move(std::get<Identifier>(_initialOp));
2021-04-27 14:53:04 +00:00
ret.debugData = ret.functionName.debugData;
2020-07-30 11:12:55 +00:00
expectToken(Token::LParen);
if (currentToken() != Token::RParen)
{
ret.arguments.emplace_back(parseExpression());
while (currentToken() != Token::RParen)
{
expectToken(Token::Comma);
ret.arguments.emplace_back(parseExpression());
}
}
if (m_useSourceLocationFrom == UseSourceLocationFrom::Scanner)
ret.debugData = updateLocationEndFrom(ret.debugData, currentLocation());
expectToken(Token::RParen);
return ret;
2016-02-22 01:13:41 +00:00
}
2017-01-31 22:59:41 +00:00
2017-04-26 22:58:34 +00:00
TypedName Parser::parseTypedName()
{
RecursionGuard recursionGuard(*this);
2017-04-26 22:58:34 +00:00
TypedName typedName = createWithLocation<TypedName>();
typedName.name = expectAsmIdentifier();
2019-12-19 16:58:20 +00:00
if (currentToken() == Token::Colon)
2017-04-26 22:58:34 +00:00
{
expectToken(Token::Colon);
if (m_useSourceLocationFrom == UseSourceLocationFrom::Scanner)
typedName.debugData = updateLocationEndFrom(typedName.debugData, currentLocation());
typedName.type = expectAsmIdentifier();
2017-04-26 22:58:34 +00:00
}
else
typedName.type = m_dialect.defaultType;
2017-04-26 22:58:34 +00:00
return typedName;
}
YulString Parser::expectAsmIdentifier()
2017-01-31 22:59:41 +00:00
{
2019-06-18 16:12:30 +00:00
YulString name{currentLiteral()};
if (currentToken() == Token::Identifier && m_dialect.builtin(name))
fatalParserError(5568_error, "Cannot use builtin function name \"" + name.str() + "\" as identifier name.");
// NOTE: We keep the expectation here to ensure the correct source location for the error above.
expectToken(Token::Identifier);
2017-01-31 22:59:41 +00:00
return name;
}
void Parser::checkBreakContinuePosition(string const& _which)
{
switch (m_currentForLoopComponent)
{
case ForLoopComponent::None:
m_errorReporter.syntaxError(2592_error, currentLocation(), "Keyword \"" + _which + "\" needs to be inside a for-loop body.");
break;
case ForLoopComponent::ForLoopPre:
m_errorReporter.syntaxError(9615_error, currentLocation(), "Keyword \"" + _which + "\" in for-loop init block is not allowed.");
break;
case ForLoopComponent::ForLoopPost:
m_errorReporter.syntaxError(2461_error, currentLocation(), "Keyword \"" + _which + "\" in for-loop post block is not allowed.");
break;
case ForLoopComponent::ForLoopBody:
break;
}
}
bool Parser::isValidNumberLiteral(string const& _literal)
{
try
{
// Try to convert _literal to u256.
2020-01-07 14:45:57 +00:00
[[maybe_unused]] auto tmp = u256(_literal);
}
catch (...)
{
return false;
}
if (boost::starts_with(_literal, "0x"))
return true;
else
return _literal.find_first_not_of("0123456789") == string::npos;
}