Introduce the concept of builtin functions.

This commit is contained in:
chriseth 2018-12-03 15:49:23 +01:00
parent e49f37be7f
commit 99db4e3ff4
16 changed files with 148 additions and 62 deletions

View File

@ -316,7 +316,14 @@ bool ReferencesResolver::visit(InlineAssembly const& _inlineAssembly)
// We use the latest EVM version because we will re-run it anyway. // We use the latest EVM version because we will re-run it anyway.
yul::AsmAnalysisInfo analysisInfo; yul::AsmAnalysisInfo analysisInfo;
boost::optional<Error::Type> errorTypeForLoose = Error::Type::SyntaxError; boost::optional<Error::Type> errorTypeForLoose = Error::Type::SyntaxError;
yul::AsmAnalyzer(analysisInfo, errorsIgnored, EVMVersion(), errorTypeForLoose, yul::AsmFlavour::Loose, resolver).analyze(_inlineAssembly.operations()); yul::AsmAnalyzer(
analysisInfo,
errorsIgnored,
EVMVersion(),
errorTypeForLoose,
yul::Dialect::looseAssemblyForEVM(),
resolver
).analyze(_inlineAssembly.operations());
return false; return false;
} }

View File

@ -658,7 +658,7 @@ bool TypeChecker::visit(InlineAssembly const& _inlineAssembly)
m_errorReporter, m_errorReporter,
m_evmVersion, m_evmVersion,
Error::Type::SyntaxError, Error::Type::SyntaxError,
yul::AsmFlavour::Loose, yul::Dialect::looseAssemblyForEVM(),
identifierAccess identifierAccess
); );
if (!analyzer.analyze(_inlineAssembly.operations())) if (!analyzer.analyze(_inlineAssembly.operations()))

View File

@ -361,7 +361,7 @@ void CompilerContext::appendInlineAssembly(
ErrorList errors; ErrorList errors;
ErrorReporter errorReporter(errors); ErrorReporter errorReporter(errors);
auto scanner = make_shared<langutil::Scanner>(langutil::CharStream(_assembly, "--CODEGEN--")); auto scanner = make_shared<langutil::Scanner>(langutil::CharStream(_assembly, "--CODEGEN--"));
auto parserResult = yul::Parser(errorReporter, yul::AsmFlavour::Strict).parse(scanner, false); auto parserResult = yul::Parser(errorReporter, yul::Dialect::strictAssemblyForEVM()).parse(scanner, false);
#ifdef SOL_OUTPUT_ASM #ifdef SOL_OUTPUT_ASM
cout << yul::AsmPrinter()(*parserResult) << endl; cout << yul::AsmPrinter()(*parserResult) << endl;
#endif #endif
@ -373,7 +373,7 @@ void CompilerContext::appendInlineAssembly(
errorReporter, errorReporter,
m_evmVersion, m_evmVersion,
boost::none, boost::none,
yul::AsmFlavour::Strict, yul::Dialect::strictAssemblyForEVM(),
identifierAccess.resolve identifierAccess.resolve
).analyze(*parserResult); ).analyze(*parserResult);
if (!parserResult || !errorReporter.errors().empty() || !analyzerResult) if (!parserResult || !errorReporter.errors().empty() || !analyzerResult)

View File

@ -43,19 +43,19 @@ using namespace dev::solidity;
namespace namespace
{ {
yul::AsmFlavour languageToAsmFlavour(AssemblyStack::Language _language) yul::Dialect languageToDialect(AssemblyStack::Language _language)
{ {
switch (_language) switch (_language)
{ {
case AssemblyStack::Language::Assembly: case AssemblyStack::Language::Assembly:
return yul::AsmFlavour::Loose; return yul::Dialect::looseAssemblyForEVM();
case AssemblyStack::Language::StrictAssembly: case AssemblyStack::Language::StrictAssembly:
return yul::AsmFlavour::Strict; return yul::Dialect::strictAssemblyForEVM();
case AssemblyStack::Language::Yul: case AssemblyStack::Language::Yul:
return yul::AsmFlavour::Yul; return yul::Dialect::yul();
} }
solAssert(false, ""); solAssert(false, "");
return yul::AsmFlavour::Yul; return yul::Dialect::yul();
} }
} }
@ -72,7 +72,7 @@ bool AssemblyStack::parseAndAnalyze(std::string const& _sourceName, std::string
m_errors.clear(); m_errors.clear();
m_analysisSuccessful = false; m_analysisSuccessful = false;
m_scanner = make_shared<Scanner>(CharStream(_source, _sourceName)); m_scanner = make_shared<Scanner>(CharStream(_source, _sourceName));
m_parserResult = yul::ObjectParser(m_errorReporter, languageToAsmFlavour(m_language)).parse(m_scanner, false); m_parserResult = yul::ObjectParser(m_errorReporter, languageToDialect(m_language)).parse(m_scanner, false);
if (!m_errorReporter.errors().empty()) if (!m_errorReporter.errors().empty())
return false; return false;
solAssert(m_parserResult, ""); solAssert(m_parserResult, "");
@ -93,7 +93,7 @@ bool AssemblyStack::analyzeParsed()
solAssert(m_parserResult, ""); solAssert(m_parserResult, "");
solAssert(m_parserResult->code, ""); solAssert(m_parserResult->code, "");
m_parserResult->analysisInfo = make_shared<yul::AsmAnalysisInfo>(); m_parserResult->analysisInfo = make_shared<yul::AsmAnalysisInfo>();
yul::AsmAnalyzer analyzer(*m_parserResult->analysisInfo, m_errorReporter, m_evmVersion, boost::none, languageToAsmFlavour(m_language)); yul::AsmAnalyzer analyzer(*m_parserResult->analysisInfo, m_errorReporter, m_evmVersion, boost::none, languageToDialect(m_language));
m_analysisSuccessful = analyzer.analyze(*m_parserResult->code); m_analysisSuccessful = analyzer.analyze(*m_parserResult->code);
return m_analysisSuccessful; return m_analysisSuccessful;
} }

View File

@ -101,7 +101,7 @@ bool AsmAnalyzer::operator()(Literal const& _literal)
} }
else if (_literal.kind == LiteralKind::Boolean) else if (_literal.kind == LiteralKind::Boolean)
{ {
solAssert(m_flavour == AsmFlavour::Yul, ""); solAssert(m_dialect.flavour == AsmFlavour::Yul, "");
solAssert(_literal.value == YulString{string("true")} || _literal.value == YulString{string("false")}, ""); solAssert(_literal.value == YulString{string("true")} || _literal.value == YulString{string("false")}, "");
} }
m_info.stackHeightInfo[&_literal] = m_stackHeight; m_info.stackHeightInfo[&_literal] = m_stackHeight;
@ -164,7 +164,7 @@ bool AsmAnalyzer::operator()(Identifier const& _identifier)
bool AsmAnalyzer::operator()(FunctionalInstruction const& _instr) bool AsmAnalyzer::operator()(FunctionalInstruction const& _instr)
{ {
solAssert(m_flavour != AsmFlavour::Yul, ""); solAssert(m_dialect.flavour != AsmFlavour::Yul, "");
bool success = true; bool success = true;
for (auto const& arg: _instr.arguments | boost::adaptors::reversed) for (auto const& arg: _instr.arguments | boost::adaptors::reversed)
if (!expectExpression(arg)) if (!expectExpression(arg))
@ -182,9 +182,9 @@ bool AsmAnalyzer::operator()(ExpressionStatement const& _statement)
{ {
int initialStackHeight = m_stackHeight; int initialStackHeight = m_stackHeight;
bool success = boost::apply_visitor(*this, _statement.expression); bool success = boost::apply_visitor(*this, _statement.expression);
if (m_stackHeight != initialStackHeight && (m_flavour != AsmFlavour::Loose || m_errorTypeForLoose)) if (m_stackHeight != initialStackHeight && (m_dialect.flavour != AsmFlavour::Loose || m_errorTypeForLoose))
{ {
Error::Type errorType = m_flavour == AsmFlavour::Loose ? *m_errorTypeForLoose : Error::Type::TypeError; Error::Type errorType = m_dialect.flavour == AsmFlavour::Loose ? *m_errorTypeForLoose : Error::Type::TypeError;
string msg = string msg =
"Top-level expressions are not supposed to return values (this expression returns " + "Top-level expressions are not supposed to return values (this expression returns " +
to_string(m_stackHeight - initialStackHeight) + to_string(m_stackHeight - initialStackHeight) +
@ -563,7 +563,7 @@ Scope& AsmAnalyzer::scope(Block const* _block)
} }
void AsmAnalyzer::expectValidType(string const& type, SourceLocation const& _location) void AsmAnalyzer::expectValidType(string const& type, SourceLocation const& _location)
{ {
if (m_flavour != AsmFlavour::Yul) if (m_dialect.flavour != AsmFlavour::Yul)
return; return;
if (!builtinTypes.count(type)) if (!builtinTypes.count(type))
@ -623,7 +623,7 @@ void AsmAnalyzer::warnOnInstructions(solidity::Instruction _instr, SourceLocatio
if (_instr == solidity::Instruction::JUMP || _instr == solidity::Instruction::JUMPI || _instr == solidity::Instruction::JUMPDEST) if (_instr == solidity::Instruction::JUMP || _instr == solidity::Instruction::JUMPI || _instr == solidity::Instruction::JUMPDEST)
{ {
solAssert(m_flavour == AsmFlavour::Loose, ""); solAssert(m_dialect.flavour == AsmFlavour::Loose, "");
m_errorReporter.error( m_errorReporter.error(
m_errorTypeForLoose ? *m_errorTypeForLoose : Error::Type::Warning, m_errorTypeForLoose ? *m_errorTypeForLoose : Error::Type::Warning,
_location, _location,
@ -636,7 +636,7 @@ void AsmAnalyzer::warnOnInstructions(solidity::Instruction _instr, SourceLocatio
void AsmAnalyzer::checkLooseFeature(SourceLocation const& _location, string const& _description) void AsmAnalyzer::checkLooseFeature(SourceLocation const& _location, string const& _description)
{ {
if (m_flavour != AsmFlavour::Loose) if (m_dialect.flavour != AsmFlavour::Loose)
solAssert(false, _description); solAssert(false, _description);
else if (m_errorTypeForLoose) else if (m_errorTypeForLoose)
m_errorReporter.error(*m_errorTypeForLoose, _location, _description); m_errorReporter.error(*m_errorTypeForLoose, _location, _description);

View File

@ -23,12 +23,12 @@
#include <liblangutil/Exceptions.h> #include <liblangutil/Exceptions.h>
#include <liblangutil/EVMVersion.h> #include <liblangutil/EVMVersion.h>
#include <libyul/Dialect.h>
#include <libyul/AsmScope.h> #include <libyul/AsmScope.h>
#include <libyul/AsmDataForward.h>
#include <libyul/backends/evm/AbstractAssembly.h> #include <libyul/backends/evm/AbstractAssembly.h>
#include <libyul/AsmDataForward.h>
#include <boost/variant.hpp> #include <boost/variant.hpp>
#include <boost/optional.hpp> #include <boost/optional.hpp>
@ -59,14 +59,14 @@ public:
langutil::ErrorReporter& _errorReporter, langutil::ErrorReporter& _errorReporter,
dev::solidity::EVMVersion _evmVersion, dev::solidity::EVMVersion _evmVersion,
boost::optional<langutil::Error::Type> _errorTypeForLoose, boost::optional<langutil::Error::Type> _errorTypeForLoose,
AsmFlavour _flavour = AsmFlavour::Loose, Dialect _dialect = Dialect::looseAssemblyForEVM(),
ExternalIdentifierAccess::Resolver const& _resolver = ExternalIdentifierAccess::Resolver() ExternalIdentifierAccess::Resolver const& _resolver = ExternalIdentifierAccess::Resolver()
): ):
m_resolver(_resolver), m_resolver(_resolver),
m_info(_analysisInfo), m_info(_analysisInfo),
m_errorReporter(_errorReporter), m_errorReporter(_errorReporter),
m_evmVersion(_evmVersion), m_evmVersion(_evmVersion),
m_flavour(_flavour), m_dialect(std::move(_dialect)),
m_errorTypeForLoose(_errorTypeForLoose) m_errorTypeForLoose(_errorTypeForLoose)
{} {}
@ -115,7 +115,7 @@ private:
AsmAnalysisInfo& m_info; AsmAnalysisInfo& m_info;
langutil::ErrorReporter& m_errorReporter; langutil::ErrorReporter& m_errorReporter;
dev::solidity::EVMVersion m_evmVersion; dev::solidity::EVMVersion m_evmVersion;
AsmFlavour m_flavour = AsmFlavour::Loose; Dialect m_dialect = Dialect::looseAssemblyForEVM();
boost::optional<langutil::Error::Type> m_errorTypeForLoose; boost::optional<langutil::Error::Type> m_errorTypeForLoose;
}; };

View File

@ -49,11 +49,4 @@ struct TypedName;
using Expression = boost::variant<FunctionalInstruction, FunctionCall, Identifier, Literal>; using Expression = boost::variant<FunctionalInstruction, FunctionCall, Identifier, Literal>;
using Statement = boost::variant<ExpressionStatement, Instruction, Label, StackAssignment, Assignment, VariableDeclaration, FunctionDefinition, If, Switch, ForLoop, Block>; using Statement = boost::variant<ExpressionStatement, Instruction, Label, StackAssignment, Assignment, VariableDeclaration, FunctionDefinition, If, Switch, ForLoop, Block>;
enum class AsmFlavour
{
Loose, // no types, EVM instructions as function, jumps and direct stack manipulations
Strict, // no types, EVM instructions as functions, but no jumps and no direct stack manipulations
Yul // same as Strict mode with types
};
} }

View File

@ -107,7 +107,7 @@ Statement Parser::parseStatement()
return parseForLoop(); return parseForLoop();
case Token::Assign: case Token::Assign:
{ {
if (m_flavour != AsmFlavour::Loose) if (m_dialect.flavour != AsmFlavour::Loose)
break; break;
StackAssignment assignment = createWithLocation<StackAssignment>(); StackAssignment assignment = createWithLocation<StackAssignment>();
advance(); advance();
@ -174,7 +174,7 @@ Statement Parser::parseStatement()
if (currentToken() == Token::Assign && peekNextToken() != Token::Colon) if (currentToken() == Token::Assign && peekNextToken() != Token::Colon)
{ {
Assignment assignment = createWithLocation<Assignment>(identifier.location); Assignment assignment = createWithLocation<Assignment>(identifier.location);
if (m_flavour != AsmFlavour::Yul && instructions().count(identifier.name.str())) if (m_dialect.flavour != AsmFlavour::Yul && instructions().count(identifier.name.str()))
fatalParserError("Cannot use instruction names for identifier names."); fatalParserError("Cannot use instruction names for identifier names.");
advance(); advance();
assignment.variableNames.emplace_back(identifier); assignment.variableNames.emplace_back(identifier);
@ -185,7 +185,7 @@ Statement Parser::parseStatement()
else else
{ {
// label // label
if (m_flavour != AsmFlavour::Loose) if (m_dialect.flavour != AsmFlavour::Loose)
fatalParserError("Labels are not supported."); fatalParserError("Labels are not supported.");
Label label = createWithLocation<Label>(identifier.location); Label label = createWithLocation<Label>(identifier.location);
label.name = identifier.name; label.name = identifier.name;
@ -193,7 +193,7 @@ Statement Parser::parseStatement()
} }
} }
default: default:
if (m_flavour != AsmFlavour::Loose) if (m_dialect.flavour != AsmFlavour::Loose)
fatalParserError("Call or assignment expected."); fatalParserError("Call or assignment expected.");
break; break;
} }
@ -269,7 +269,7 @@ Expression Parser::parseExpression()
instructionNames().at(instr.instruction) + instructionNames().at(instr.instruction) +
"\" not allowed in this context." "\" not allowed in this context."
); );
if (m_flavour != AsmFlavour::Loose && currentToken() != Token::LParen) if (m_dialect.flavour != AsmFlavour::Loose && currentToken() != Token::LParen)
fatalParserError( fatalParserError(
"Non-functional instructions are not allowed in this context." "Non-functional instructions are not allowed in this context."
); );
@ -289,7 +289,7 @@ Expression Parser::parseExpression()
else if (operation.type() == typeid(Instruction)) else if (operation.type() == typeid(Instruction))
{ {
// Instructions not taking arguments are allowed as expressions. // Instructions not taking arguments are allowed as expressions.
solAssert(m_flavour == AsmFlavour::Loose, ""); solAssert(m_dialect.flavour == AsmFlavour::Loose, "");
Instruction& instr = boost::get<Instruction>(operation); Instruction& instr = boost::get<Instruction>(operation);
return FunctionalInstruction{std::move(instr.location), instr.instruction, {}}; return FunctionalInstruction{std::move(instr.location), instr.instruction, {}};
} }
@ -358,7 +358,7 @@ Parser::ElementaryOperation Parser::parseElementaryOperation()
else else
literal = YulString{currentLiteral()}; literal = YulString{currentLiteral()};
// first search the set of instructions. // first search the set of instructions.
if (m_flavour != AsmFlavour::Yul && instructions().count(literal.str())) if (m_dialect.flavour != AsmFlavour::Yul && instructions().count(literal.str()))
{ {
dev::solidity::Instruction const& instr = instructions().at(literal.str()); dev::solidity::Instruction const& instr = instructions().at(literal.str());
ret = Instruction{location(), instr}; ret = Instruction{location(), instr};
@ -399,7 +399,7 @@ Parser::ElementaryOperation Parser::parseElementaryOperation()
{} {}
}; };
advance(); advance();
if (m_flavour == AsmFlavour::Yul) if (m_dialect.flavour == AsmFlavour::Yul)
{ {
expectToken(Token::Colon); expectToken(Token::Colon);
literal.location.end = endPosition(); literal.location.end = endPosition();
@ -412,7 +412,7 @@ Parser::ElementaryOperation Parser::parseElementaryOperation()
} }
default: default:
fatalParserError( fatalParserError(
m_flavour == AsmFlavour::Yul ? m_dialect.flavour == AsmFlavour::Yul ?
"Literal or identifier expected." : "Literal or identifier expected." :
"Literal, identifier or instruction expected." "Literal, identifier or instruction expected."
); );
@ -482,7 +482,7 @@ Expression Parser::parseCall(Parser::ElementaryOperation&& _initialOp)
RecursionGuard recursionGuard(*this); RecursionGuard recursionGuard(*this);
if (_initialOp.type() == typeid(Instruction)) if (_initialOp.type() == typeid(Instruction))
{ {
solAssert(m_flavour != AsmFlavour::Yul, "Instructions are invalid in Yul"); solAssert(m_dialect.flavour != AsmFlavour::Yul, "Instructions are invalid in Yul");
Instruction& instruction = boost::get<Instruction>(_initialOp); Instruction& instruction = boost::get<Instruction>(_initialOp);
FunctionalInstruction ret; FunctionalInstruction ret;
ret.instruction = instruction.instruction; ret.instruction = instruction.instruction;
@ -553,7 +553,7 @@ Expression Parser::parseCall(Parser::ElementaryOperation&& _initialOp)
} }
else else
fatalParserError( fatalParserError(
m_flavour == AsmFlavour::Yul ? m_dialect.flavour == AsmFlavour::Yul ?
"Function name expected." : "Function name expected." :
"Assembly instruction or function name required in front of \"(\")" "Assembly instruction or function name required in front of \"(\")"
); );
@ -566,7 +566,7 @@ TypedName Parser::parseTypedName()
RecursionGuard recursionGuard(*this); RecursionGuard recursionGuard(*this);
TypedName typedName = createWithLocation<TypedName>(); TypedName typedName = createWithLocation<TypedName>();
typedName.name = expectAsmIdentifier(); typedName.name = expectAsmIdentifier();
if (m_flavour == AsmFlavour::Yul) if (m_dialect.flavour == AsmFlavour::Yul)
{ {
expectToken(Token::Colon); expectToken(Token::Colon);
typedName.location.end = endPosition(); typedName.location.end = endPosition();
@ -578,7 +578,7 @@ TypedName Parser::parseTypedName()
YulString Parser::expectAsmIdentifier() YulString Parser::expectAsmIdentifier()
{ {
YulString name = YulString{currentLiteral()}; YulString name = YulString{currentLiteral()};
if (m_flavour == AsmFlavour::Yul) if (m_dialect.flavour == AsmFlavour::Yul)
{ {
switch (currentToken()) switch (currentToken())
{ {

View File

@ -22,21 +22,24 @@
#pragma once #pragma once
#include <memory>
#include <vector>
#include <libyul/AsmData.h> #include <libyul/AsmData.h>
#include <libyul/Dialect.h>
#include <liblangutil/SourceLocation.h> #include <liblangutil/SourceLocation.h>
#include <liblangutil/Scanner.h> #include <liblangutil/Scanner.h>
#include <liblangutil/ParserBase.h> #include <liblangutil/ParserBase.h>
#include <memory>
#include <vector>
namespace yul namespace yul
{ {
class Parser: public langutil::ParserBase class Parser: public langutil::ParserBase
{ {
public: public:
explicit Parser(langutil::ErrorReporter& _errorReporter, AsmFlavour _flavour = AsmFlavour::Loose): explicit Parser(langutil::ErrorReporter& _errorReporter, Dialect _dialect = Dialect::looseAssemblyForEVM()):
ParserBase(_errorReporter), m_flavour(_flavour) {} ParserBase(_errorReporter), m_dialect(std::move(_dialect)) {}
/// Parses an inline assembly block starting with `{` and ending with `}`. /// Parses an inline assembly block starting with `{` and ending with `}`.
/// @param _reuseScanner if true, do check for end of input after the `}`. /// @param _reuseScanner if true, do check for end of input after the `}`.
@ -83,7 +86,7 @@ protected:
static bool isValidNumberLiteral(std::string const& _literal); static bool isValidNumberLiteral(std::string const& _literal);
private: private:
AsmFlavour m_flavour = AsmFlavour::Loose; Dialect m_dialect = Dialect::looseAssemblyForEVM();
}; };
} }

82
libyul/Dialect.h Normal file
View File

@ -0,0 +1,82 @@
/*
This file is part of solidity.
solidity is free software: you can redistribute it and/or modify
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,
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/>.
*/
/**
* Yul dialect.
*/
#pragma once
#include <libyul/YulString.h>
#include <memory>
namespace yul
{
class YulString;
using Type = YulString;
enum class AsmFlavour
{
Loose, // no types, EVM instructions as function, jumps and direct stack manipulations
Strict, // no types, EVM instructions as functions, but no jumps and no direct stack manipulations
Yul // same as Strict mode with types
};
struct BuiltinFunction
{
YulString name;
std::vector<Type> parameters;
std::vector<Type> returns;
bool movable;
};
/**
* Class to query for builtin functions and their semantics.
*/
struct Builtins
{
virtual ~Builtins() = default;
/// @returns the builtin function of the given name or a nullptr if it is not a builtin function.
virtual BuiltinFunction const* query(YulString /*_name*/) const { return nullptr; }
};
struct Dialect
{
AsmFlavour flavour = AsmFlavour::Loose;
std::shared_ptr<Builtins> builtins;
Dialect(AsmFlavour _flavour, std::shared_ptr<Builtins> _builtins):
flavour(_flavour), builtins(std::move(_builtins))
{}
static Dialect looseAssemblyForEVM()
{
return Dialect{AsmFlavour::Loose, std::make_shared<Builtins>()};
}
static Dialect strictAssemblyForEVM()
{
// The EVM instructions will be moved to builtins at some point.
return Dialect{AsmFlavour::Strict, std::make_shared<Builtins>()};
}
static Dialect yul()
{
// Will have to add builtins later.
return Dialect{AsmFlavour::Yul, std::make_shared<Builtins>()};
}
};
}

View File

@ -104,7 +104,7 @@ shared_ptr<Block> ObjectParser::parseCode()
shared_ptr<Block> ObjectParser::parseBlock() shared_ptr<Block> ObjectParser::parseBlock()
{ {
Parser parser(m_errorReporter, m_flavour); Parser parser(m_errorReporter, m_dialect);
shared_ptr<Block> block = parser.parse(m_scanner, true); shared_ptr<Block> block = parser.parse(m_scanner, true);
yulAssert(block || m_errorReporter.hasErrors(), "Invalid block but no error!"); yulAssert(block || m_errorReporter.hasErrors(), "Invalid block but no error!");
return block; return block;

View File

@ -22,6 +22,7 @@
#include <libyul/YulString.h> #include <libyul/YulString.h>
#include <libyul/Object.h> #include <libyul/Object.h>
#include <libyul/Dialect.h>
#include <liblangutil/ErrorReporter.h> #include <liblangutil/ErrorReporter.h>
#include <liblangutil/ParserBase.h> #include <liblangutil/ParserBase.h>
@ -46,9 +47,9 @@ class ObjectParser: public langutil::ParserBase
public: public:
explicit ObjectParser( explicit ObjectParser(
langutil::ErrorReporter& _errorReporter, langutil::ErrorReporter& _errorReporter,
yul::AsmFlavour _flavour = yul::AsmFlavour::Loose Dialect _dialect = Dialect::looseAssemblyForEVM()
): ):
ParserBase(_errorReporter), m_flavour(_flavour) {} ParserBase(_errorReporter), m_dialect(std::move(_dialect)) {}
/// Parses a Yul object. /// Parses a Yul object.
/// Falls back to code-only parsing if the source starts with `{`. /// Falls back to code-only parsing if the source starts with `{`.
@ -66,7 +67,7 @@ private:
YulString parseUniqueName(Object const* _containingObject); YulString parseUniqueName(Object const* _containingObject);
void addNamedSubObject(Object& _container, YulString _name, std::shared_ptr<ObjectNode> _subObject); void addNamedSubObject(Object& _container, YulString _name, std::shared_ptr<ObjectNode> _subObject);
yul::AsmFlavour m_flavour; Dialect m_dialect;
}; };
} }

View File

@ -54,11 +54,11 @@ void yul::test::printErrors(ErrorList const& _errors, Scanner const& _scanner)
pair<shared_ptr<Block>, shared_ptr<yul::AsmAnalysisInfo>> yul::test::parse(string const& _source, bool _yul) pair<shared_ptr<Block>, shared_ptr<yul::AsmAnalysisInfo>> yul::test::parse(string const& _source, bool _yul)
{ {
auto flavour = _yul ? yul::AsmFlavour::Yul : yul::AsmFlavour::Strict; Dialect dialect = _yul ? yul::Dialect::yul() : yul::Dialect::strictAssemblyForEVM();
ErrorList errors; ErrorList errors;
ErrorReporter errorReporter(errors); ErrorReporter errorReporter(errors);
auto scanner = make_shared<Scanner>(CharStream(_source, "")); auto scanner = make_shared<Scanner>(CharStream(_source, ""));
auto parserResult = yul::Parser(errorReporter, flavour).parse(scanner, false); auto parserResult = yul::Parser(errorReporter, dialect).parse(scanner, false);
if (parserResult) if (parserResult)
{ {
BOOST_REQUIRE(errorReporter.errors().empty()); BOOST_REQUIRE(errorReporter.errors().empty());
@ -68,7 +68,7 @@ pair<shared_ptr<Block>, shared_ptr<yul::AsmAnalysisInfo>> yul::test::parse(strin
errorReporter, errorReporter,
dev::test::Options::get().evmVersion(), dev::test::Options::get().evmVersion(),
boost::none, boost::none,
flavour dialect
); );
if (analyzer.analyze(*parserResult)) if (analyzer.analyze(*parserResult))
{ {

View File

@ -52,7 +52,7 @@ bool parse(string const& _source, ErrorReporter& errorReporter)
try try
{ {
auto scanner = make_shared<Scanner>(CharStream(_source, "")); auto scanner = make_shared<Scanner>(CharStream(_source, ""));
auto parserResult = yul::Parser(errorReporter, yul::AsmFlavour::Yul).parse(scanner, false); auto parserResult = yul::Parser(errorReporter, yul::Dialect::yul()).parse(scanner, false);
if (parserResult) if (parserResult)
{ {
yul::AsmAnalysisInfo analysisInfo; yul::AsmAnalysisInfo analysisInfo;
@ -61,7 +61,7 @@ bool parse(string const& _source, ErrorReporter& errorReporter)
errorReporter, errorReporter,
dev::test::Options::get().evmVersion(), dev::test::Options::get().evmVersion(),
boost::none, boost::none,
yul::AsmFlavour::Yul yul::Dialect::yul()
)).analyze(*parserResult); )).analyze(*parserResult);
} }
} }

View File

@ -256,11 +256,11 @@ void YulOptimizerTest::printIndented(ostream& _stream, string const& _output, st
bool YulOptimizerTest::parse(ostream& _stream, string const& _linePrefix, bool const _formatted) bool YulOptimizerTest::parse(ostream& _stream, string const& _linePrefix, bool const _formatted)
{ {
yul::AsmFlavour flavour = m_yul ? yul::AsmFlavour::Yul : yul::AsmFlavour::Strict; yul::Dialect dialect = m_yul ? yul::Dialect::yul() : yul::Dialect::strictAssemblyForEVM();
ErrorList errors; ErrorList errors;
ErrorReporter errorReporter(errors); ErrorReporter errorReporter(errors);
shared_ptr<Scanner> scanner = make_shared<Scanner>(CharStream(m_source, "")); shared_ptr<Scanner> scanner = make_shared<Scanner>(CharStream(m_source, ""));
m_ast = yul::Parser(errorReporter, flavour).parse(scanner, false); m_ast = yul::Parser(errorReporter, dialect).parse(scanner, false);
if (!m_ast || !errorReporter.errors().empty()) if (!m_ast || !errorReporter.errors().empty())
{ {
FormattedScope(_stream, _formatted, {formatting::BOLD, formatting::RED}) << _linePrefix << "Error parsing source." << endl; FormattedScope(_stream, _formatted, {formatting::BOLD, formatting::RED}) << _linePrefix << "Error parsing source." << endl;
@ -273,7 +273,7 @@ bool YulOptimizerTest::parse(ostream& _stream, string const& _linePrefix, bool c
errorReporter, errorReporter,
dev::test::Options::get().evmVersion(), dev::test::Options::get().evmVersion(),
boost::none, boost::none,
flavour dialect
); );
if (!analyzer.analyze(*m_ast) || !errorReporter.errors().empty()) if (!analyzer.analyze(*m_ast) || !errorReporter.errors().empty())
{ {

View File

@ -82,7 +82,7 @@ public:
{ {
ErrorReporter errorReporter(m_errors); ErrorReporter errorReporter(m_errors);
shared_ptr<Scanner> scanner = make_shared<Scanner>(CharStream(_input, "")); shared_ptr<Scanner> scanner = make_shared<Scanner>(CharStream(_input, ""));
m_ast = yul::Parser(errorReporter, yul::AsmFlavour::Strict).parse(scanner, false); m_ast = yul::Parser(errorReporter, yul::Dialect::strictAssemblyForEVM()).parse(scanner, false);
if (!m_ast || !errorReporter.errors().empty()) if (!m_ast || !errorReporter.errors().empty())
{ {
cout << "Error parsing source." << endl; cout << "Error parsing source." << endl;
@ -95,7 +95,7 @@ public:
errorReporter, errorReporter,
EVMVersion::byzantium(), EVMVersion::byzantium(),
boost::none, boost::none,
AsmFlavour::Strict Dialect::strictAssemblyForEVM()
); );
if (!analyzer.analyze(*m_ast) || !errorReporter.errors().empty()) if (!analyzer.analyze(*m_ast) || !errorReporter.errors().empty())
{ {